Skip to content

Commit 9f6e05c

Browse files
authored
Merge pull request #513 from ajgilbert/fullmatch-nuisance-edits
Nuisance edits: always require full match
2 parents fcf48ab + cb10a65 commit 9f6e05c

File tree

1 file changed

+45
-20
lines changed

1 file changed

+45
-20
lines changed

python/NuisanceModifier.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22
import sys
33
from math import log,exp,hypot
44

5+
6+
def fullmatch(regex, line):
7+
"""Performs regex match requiring the full string to match
8+
9+
Args:
10+
regex (str or precompiled regex): The pattern to match
11+
line (str): The line of text to match
12+
13+
Returns:
14+
bool: true if a complete match was found, false otherwise
15+
"""
16+
if isinstance(regex, str):
17+
compiled = re.compile(regex)
18+
else:
19+
compiled = regex
20+
m = compiled.match(line)
21+
if m:
22+
start, stop = m.span()
23+
if stop - start == len(line):
24+
return True
25+
return False
26+
27+
528
def quadratureAdd(pdf, val1, val2, context=None):
629
if type(val1) == list and len(val1) != 2: raise RuntimeError("{} is a list of length != 2".format(val1))
730
if type(val2) == list and len(val2) != 2: raise RuntimeError("{} is a list of length != 2".format(val2))
@@ -44,10 +67,10 @@ def doAddNuisance(datacard, args):
4467
value = float(value)
4568
foundChann, foundProc = False, False
4669
for b in errline.keys():
47-
if channel == "*" or cchannel.search(b):
70+
if channel == "*" or fullmatch(cchannel, b):
4871
foundChann = True
4972
for p in datacard.exp[b]:
50-
if process == "*" or cprocess.search(p):
73+
if process == "*" or fullmatch(cprocess, p):
5174
foundProc = True
5275
if value in [ 0.0, 1.0 ]:
5376
pass #do nothing, there's nothing to add
@@ -74,12 +97,12 @@ def doDropNuisance(datacard, args):
7497
opts = args[3:]
7598
foundProc = False
7699
for lsyst,nofloat,pdf,args0,errline in datacard.systs:
77-
if re.match(name,lsyst):
100+
if fullmatch(name,lsyst):
78101
for b in errline.keys():
79-
if channel == "*" or cchannel.search(b):
102+
if channel == "*" or fullmatch(cchannel, b):
80103
#if channel != "*": foundProc = False
81104
for p in datacard.exp[b]:
82-
if process == "*" or cprocess.search(p):
105+
if process == "*" or fullmatch(cprocess, p):
83106
foundProc = True
84107
errline[b][p] = 0
85108
#if channel != "*" and foundProc == False:
@@ -125,7 +148,9 @@ def doRenameNuisance(datacard, args):
125148
opts = args[4:]
126149
foundChann, foundProc = False, False
127150
for lsyst,nofloat,pdf0,args0,errline0 in datacard.systs[:]:
128-
lsystnew = re.sub(oldname,newname,lsyst)
151+
lsystnew = lsyst
152+
if fullmatch(oldname, lsyst):
153+
lsystnew = newname
129154
if lsystnew != lsyst:
130155
if pdf0=="param" : raise RuntimeError, "Incorrect syntax. Cannot specify process and channel for %s with pdf %s. Use 'nuisance edit rename oldname newname'"% (lsyst,pdf0)
131156
found = False
@@ -138,11 +163,11 @@ def doRenameNuisance(datacard, args):
138163
if not found:
139164
datacard.systs.append([lsystnew,nofloat,pdf0,args0,errline2])
140165
for b in errline0.keys():
141-
if channel == "*" or cchannel.match(b):
166+
if channel == "*" or fullmatch(cchannel, b):
142167
foundChann = True
143168
if channel != "*": foundProc = False
144169
for p in datacard.exp[b].keys():
145-
if process == "*" or cprocess.match(p):
170+
if process == "*" or fullmatch(cprocess, p):
146171
foundProc = True
147172
if errline0[b][p] in [0.0]:
148173
continue
@@ -176,7 +201,7 @@ def doChangeNuisancePdf(datacard, args):
176201
(name, newpdf) = args[:2]
177202
found = False
178203
for i,(lsyst,nofloat,pdf,args0,errline) in enumerate(datacard.systs):
179-
if re.match(name,lsyst):
204+
if fullmatch(name,lsyst):
180205
found = True; ok = False
181206
if newpdf == pdf: continue
182207
if pdf in [ "lnN", "lnU"]:
@@ -215,11 +240,11 @@ def doMergeNuisance(datacard, args):
215240
foundProc = False
216241

217242
for lsyst2,nofloat2,pdf2,args02,errline2 in datacard.systs:
218-
if re.match(name2, lsyst2):
243+
if fullmatch(name2, lsyst2):
219244
for b in errline2.keys():
220-
if channel == "*" or cchannel.search(b):
245+
if channel == "*" or fullmatch(cchannel, b):
221246
for p in datacard.exp[b]:
222-
if process == "*" or cprocess.search(p):
247+
if process == "*" or fullmatch(cprocess, p):
223248
foundProc = True
224249
doAddNuisance(datacard, [p+"$", b+"$", name1, pdf2, errline2[b][p], "addq"])
225250
errline2[b][p] = 0
@@ -240,11 +265,11 @@ def doSplitNuisance(datacard, args):
240265
opts = args[7:]
241266
foundProc = False
242267
for lsyst,nofloat,pdf,args0,errline in datacard.systs:
243-
if re.match(oldname,lsyst):
268+
if fullmatch(oldname,lsyst):
244269
for b in errline.keys():
245-
if channel == "*" or cchannel.search(b):
270+
if channel == "*" or fullmatch(cchannel, b):
246271
for p in datacard.exp[b]:
247-
if process == "*" or cprocess.search(p):
272+
if process == "*" or fullmatch(cprocess, p):
248273
foundProc = True
249274
if errline[b][p] not in [0., 1.]:
250275
doAddNuisance(datacard, [p, b, newname1, pdf, value1, "overwrite"])
@@ -266,13 +291,13 @@ def doFreezeNuisance(datacard, args):
266291

267292
# first check in the list of paramters as flatParam, rateParam or discretes not included in datacard.systs (smaller usually)
268293
for lsyst in datacard.flatParamNuisances.keys()+list(datacard.rateParamsOrder)+datacard.discretes +datacard.extArgs.keys():
269-
if re.match(pat,lsyst):
294+
if fullmatch(pat,lsyst):
270295
datacard.frozenNuisances.add(lsyst)
271296
found.append(lsyst)
272297

273298
if not found:
274299
for lsyst,nofloat,pdf,args0,errline in datacard.systs:
275-
if re.match(pat,lsyst):
300+
if fullmatch(pat,lsyst):
276301
datacard.frozenNuisances.add(lsyst)
277302
found.append(lsyst)
278303

@@ -295,13 +320,13 @@ def doFlipNuisance(datacard,args):
295320
raise RuntimeError, "Error: nuisance edit flip %s missed option n2p and/or p2n" % (args)
296321
foundProc = False
297322
for lsyst,nofloat,pdf,args0,errline in datacard.systs:
298-
if re.match(name,lsyst):
323+
if fullmatch(name,lsyst):
299324
if pdf not in ["lnN"]:
300325
raise RuntimeError, "Error: nuisance edit flip %s currently not support pdftype %s" % (args,pdf)
301326
for b in errline.keys():
302-
if channel == "*" or cchannel.search(b):
327+
if channel == "*" or fullmatch(cchannel, b):
303328
for p in datacard.exp[b]:
304-
if process == "*" or cprocess.search(p):
329+
if process == "*" or fullmatch(cprocess, p):
305330
foundProc = True
306331
if errline[b][p] not in [0., 1.]:
307332
if type(errline[b][p]) is list:

0 commit comments

Comments
 (0)