From 7b2c21c6149117f3fe914faec4c6858121c1f1e5 Mon Sep 17 00:00:00 2001 From: Aliya Nigamova Date: Tue, 23 Jul 2024 16:08:02 +0200 Subject: [PATCH 1/6] add regex processing to --setParameters in Impacts.py, remove old iterators --- python/tool_base/Impacts.py | 27 ++++++++++++++++++++------- python/tool_base/utils.py | 9 ++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/python/tool_base/Impacts.py b/python/tool_base/Impacts.py index 43c06f1bfa8..b737fcf3254 100755 --- a/python/tool_base/Impacts.py +++ b/python/tool_base/Impacts.py @@ -204,6 +204,25 @@ def run_method(self): print("Have parameters: " + str(len(paramList))) + varList = utils.list_from_workspace(ws, 'w', 'variables') + if self.args.setParameters is not None: + set_parameters = self.args.setParameters.split(',') + set_parameters_str = '' + for ind,setParam in enumerate(set_parameters): + if 'rgx{' in setParam: + eqs_to = setParam.split("=")[-1] + pattern = setParam.split("=")[0] + pattern = pattern.replace("'rgx{","").replace("}'","") + pattern = pattern.replace("rgx{","").replace("}","") + set_parameters[ind] = '' + for var in varList: + if re.search(pattern, var): + var_str = var + "=" + eqs_to + set_parameters_str += var_str + "," + else: + set_parameters_str += setParam + "," + self.args.setPhysicsModelParameters = set_parameters_str.rstrip(',') + prefit = utils.prefit_from_workspace(ws, "w", paramList, self.args.setPhysicsModelParameters) res = {} if not self.args.noInitialFit: @@ -264,15 +283,9 @@ def run_method(self): print("Missing inputs: " + ",".join(missing)) def all_free_parameters(self, file, wsp, mc, pois): - res = [] wsFile = ROOT.TFile.Open(file) w = wsFile.Get(wsp) config = w.genobj(mc) pdfvars = config.GetPdf().getParameters(config.GetObservables()) - it = pdfvars.createIterator() - var = it.Next() - while var: - if var.GetName() not in pois and (not var.isConstant()) and var.InheritsFrom("RooRealVar"): - res.append(var.GetName()) - var = it.Next() + res = [var.GetName() for var in pdfvars if (var.GetName() not in pois and (not var.isConstant()) and var.InheritsFrom("RooRealVar")) ] return res diff --git a/python/tool_base/utils.py b/python/tool_base/utils.py index 5dad4de34fe..883a68bc8b6 100644 --- a/python/tool_base/utils.py +++ b/python/tool_base/utils.py @@ -33,15 +33,10 @@ def split_vals(vals, fmt_spec=None): def list_from_workspace(file, workspace, set): """Create a list of strings from a RooWorkspace set""" - res = [] wsFile = ROOT.TFile(file) ws = wsFile.Get(workspace) - argSet = ws.set(set) - it = argSet.createIterator() - var = it.Next() - while var: - res.append(var.GetName()) - var = it.Next() + argSet = ws.allVars() if set=="variables" else ws.set(set) + res = [var.GetName() for var in argSet] return res From 3eeb97b8c92c7f9229e8d0cc9f618996bb04a22d Mon Sep 17 00:00:00 2001 From: Aliya Nigamova Date: Tue, 23 Jul 2024 16:22:10 +0200 Subject: [PATCH 2/6] remove createIterator from FastScan.py too --- python/tool_base/FastScan.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/python/tool_base/FastScan.py b/python/tool_base/FastScan.py index 8df5454cfbc..f5360f3e7dd 100755 --- a/python/tool_base/FastScan.py +++ b/python/tool_base/FastScan.py @@ -42,13 +42,6 @@ def attach_args(self, group): group.add_argument("-o", "--output", default="nll", help="Name of the output file, without the .pdf extension") group.add_argument("-p", "--points", default=200, type=int, help="Number of NLL points to sample in each scan") - def RooColIter(self, coll): - it = coll.createIterator() - var = it.Next() - while var: - yield var - var = it.Next() - def run_method(self): ROOT.gROOT.SetBatch(ROOT.kTRUE) outfile = ROOT.TFile("%s.root" % self.args.output, "RECREATE") @@ -83,7 +76,7 @@ def run_method(self): page = 0 doPars = [] - for par in self.RooColIter(pars): + for par in pars: if par.isConstant(): continue if self.args.match is not None: From b21fd7a67934408c35e6e524ed7783e2daecefd1 Mon Sep 17 00:00:00 2001 From: Aliya Nigamova Date: Tue, 23 Jul 2024 17:03:09 +0200 Subject: [PATCH 3/6] fix linting --- python/tool_base/Impacts.py | 20 ++++++++++---------- python/tool_base/utils.py | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/python/tool_base/Impacts.py b/python/tool_base/Impacts.py index b737fcf3254..f4fae03ec75 100755 --- a/python/tool_base/Impacts.py +++ b/python/tool_base/Impacts.py @@ -204,24 +204,24 @@ def run_method(self): print("Have parameters: " + str(len(paramList))) - varList = utils.list_from_workspace(ws, 'w', 'variables') + varList = utils.list_from_workspace(ws, "w", "variables") if self.args.setParameters is not None: - set_parameters = self.args.setParameters.split(',') - set_parameters_str = '' - for ind,setParam in enumerate(set_parameters): - if 'rgx{' in setParam: + set_parameters = self.args.setParameters.split(",") + set_parameters_str = "" + for ind, setParam in enumerate(set_parameters): + if "rgx{" in setParam: eqs_to = setParam.split("=")[-1] pattern = setParam.split("=")[0] - pattern = pattern.replace("'rgx{","").replace("}'","") - pattern = pattern.replace("rgx{","").replace("}","") - set_parameters[ind] = '' + pattern = pattern.replace("'rgx{", "").replace("}'", "") + pattern = pattern.replace("rgx{", "").replace("}", "") + set_parameters[ind] = "" for var in varList: if re.search(pattern, var): var_str = var + "=" + eqs_to set_parameters_str += var_str + "," else: set_parameters_str += setParam + "," - self.args.setPhysicsModelParameters = set_parameters_str.rstrip(',') + self.args.setPhysicsModelParameters = set_parameters_str.rstrip(",") prefit = utils.prefit_from_workspace(ws, "w", paramList, self.args.setPhysicsModelParameters) res = {} @@ -287,5 +287,5 @@ def all_free_parameters(self, file, wsp, mc, pois): w = wsFile.Get(wsp) config = w.genobj(mc) pdfvars = config.GetPdf().getParameters(config.GetObservables()) - res = [var.GetName() for var in pdfvars if (var.GetName() not in pois and (not var.isConstant()) and var.InheritsFrom("RooRealVar")) ] + res = [var.GetName() for var in pdfvars if (var.GetName() not in pois and (not var.isConstant()) and var.InheritsFrom("RooRealVar"))] return res diff --git a/python/tool_base/utils.py b/python/tool_base/utils.py index 883a68bc8b6..ff1f4119026 100644 --- a/python/tool_base/utils.py +++ b/python/tool_base/utils.py @@ -35,7 +35,7 @@ def list_from_workspace(file, workspace, set): """Create a list of strings from a RooWorkspace set""" wsFile = ROOT.TFile(file) ws = wsFile.Get(workspace) - argSet = ws.allVars() if set=="variables" else ws.set(set) + argSet = ws.allVars() if set == "variables" else ws.set(set) res = [var.GetName() for var in argSet] return res From cbe96e426e9908f8f880134d4d7db494b5e014ec Mon Sep 17 00:00:00 2001 From: Aliya Nigamova Date: Wed, 24 Jul 2024 10:50:58 +0200 Subject: [PATCH 4/6] fix indent --- python/tool_base/Impacts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tool_base/Impacts.py b/python/tool_base/Impacts.py index b737fcf3254..1bc4d2c5d2f 100755 --- a/python/tool_base/Impacts.py +++ b/python/tool_base/Impacts.py @@ -221,7 +221,7 @@ def run_method(self): set_parameters_str += var_str + "," else: set_parameters_str += setParam + "," - self.args.setPhysicsModelParameters = set_parameters_str.rstrip(',') + self.args.setPhysicsModelParameters = set_parameters_str.rstrip(',') prefit = utils.prefit_from_workspace(ws, "w", paramList, self.args.setPhysicsModelParameters) res = {} From eeaf65785334a7f04ad644dab616da61809066fc Mon Sep 17 00:00:00 2001 From: Aliya Nigamova Date: Wed, 24 Jul 2024 10:59:05 +0200 Subject: [PATCH 5/6] lint --- python/tool_base/Impacts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/tool_base/Impacts.py b/python/tool_base/Impacts.py index ec129fcd24c..f0b2762791a 100755 --- a/python/tool_base/Impacts.py +++ b/python/tool_base/Impacts.py @@ -221,7 +221,7 @@ def run_method(self): set_parameters_str += var_str + "," else: set_parameters_str += setParam + "," - self.args.setPhysicsModelParameters = set_parameters_str.rstrip(',') + self.args.setPhysicsModelParameters = set_parameters_str.rstrip(",") prefit = utils.prefit_from_workspace(ws, "w", paramList, self.args.setPhysicsModelParameters) res = {} From c534f67b15891f32f13f1af733a4b4ccdb1d02ec Mon Sep 17 00:00:00 2001 From: Aliya Nigamova Date: Wed, 24 Jul 2024 13:52:57 +0200 Subject: [PATCH 6/6] remove --setPhysicsModelParameters option --- python/tool_base/Impacts.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/python/tool_base/Impacts.py b/python/tool_base/Impacts.py index f0b2762791a..d15742b8994 100755 --- a/python/tool_base/Impacts.py +++ b/python/tool_base/Impacts.py @@ -33,7 +33,6 @@ def attach_intercept_args(self, group): --output stages. Note the ordering of POIs in the list must also be identical in each step.""", ) - group.add_argument("--setPhysicsModelParameters") group.add_argument("--setParameters") group.add_argument("--name", "-n", default="Test") @@ -101,11 +100,8 @@ def run_method(self): # Put intercepted args back passthru.extend(["-m", mh]) passthru.extend(["-d", ws]) - if self.args.setPhysicsModelParameters is not None: - passthru.extend(["--setPhysicsModelParameters", self.args.setPhysicsModelParameters]) if self.args.setParameters is not None: passthru.extend(["--setParameters", self.args.setParameters]) - self.args.setPhysicsModelParameters = self.args.setParameters pass_str = " ".join(passthru) paramList = [] @@ -221,9 +217,9 @@ def run_method(self): set_parameters_str += var_str + "," else: set_parameters_str += setParam + "," - self.args.setPhysicsModelParameters = set_parameters_str.rstrip(",") + self.args.setParameters = set_parameters_str.rstrip(",") - prefit = utils.prefit_from_workspace(ws, "w", paramList, self.args.setPhysicsModelParameters) + prefit = utils.prefit_from_workspace(ws, "w", paramList, self.args.setParameters) res = {} if not self.args.noInitialFit: res["POIs"] = []