63def _preselJetHypoToolFromDict(flags, mainChainDict, doTaggingSel=False):
64
65 preselChainDict = dict(mainChainDict)
66 preselChainDict['chainParts']=[]
67 trkpresel = extractPreselection(mainChainDict)
68
69
70
71 presel_matched = re.match(r'presel(?P<cut>(VETOMULT)?\d?\d?(Z[\d\D]+)?[jacf]\d?(HT)?[\d\D]+)', trkpresel)
72 assert presel_matched is not None, "Impossible to match preselection pattern for self.trkpresel=\'{0}\'.".format(trkpresel)
73 presel_cut_str = presel_matched.groupdict()['cut']
74
75 usingDIPZ = bool(re.match(r'.*Z', presel_cut_str))
76 if usingDIPZ:
77 findSel = re.finditer(r'(?P<nJet>\d?[jacf])(?P<ptcut>\d+)', presel_cut_str)
78 findAllJets=[]
79 findAllPts=[]
80 for match in findSel:
81 nJ = match.group("nJet")
82 findAllJets.append('1'+nJ if len(nJ)==1 else nJ)
83 findAllPts.append(int(match.group("ptcut")))
84 nAllJets = sum(int(i[:-1]) for i in findAllJets)
85 nCentralJets = sum(int(i[:-1]) if 'c' in i else 0 for i in findAllJets)
86 ptCut =
min(int(i)
for i
in findAllPts)
87 assert nAllJets == nCentralJets, "Your preselection has a DIPZ part but not only central jets were required. This isn't currently supported. Please investigate."
88
89 preselCommonJetParts = dict(JetChainParts_Default)
90
91 for ip,p in enumerate(presel_cut_str.split('XX')):
92 hascalSel= bool(re.match(r'.*emf\w?\d+', p))
93
94
95 if not doTaggingSel:
96 p = re.sub(r'(b|bg|bgtwo|gntau|uht1tau)\d\d', '', p)
97
98 hasBjetSel = bool(re.match(r'.*(b|bg|bgtwo)\d\d', p))
99 hasTauSel = bool(re.match(r'.*(gntau|uht1tau)\d\d', p))
100 hasDIPZsel = bool(re.match(r'.*Z', p))
101
102 if hasDIPZsel and not doTaggingSel: continue
103 if usingDIPZ and not hasDIPZsel and not hasBjetSel and doTaggingSel: continue
104
105 assert not ( (hasBjetSel or hasDIPZsel) and not doTaggingSel), "Your jet preselection has a b-jet or DIPZ part but a calo-only preselection was requested instead. This should not be possible. Please investigate."
106
107 bmatches = r'(?P<btagger>(b|bg|bgtwo))(?P<bwp>\d\d)' if hasBjetSel else ""
108 taumatches = r'(?P<tauid>(gntau|uht1tau))(?P<tauwp>\d\d)' if hasTauSel else ""
109
110 pattern_to_test = r'(?P<veto>(VETOMULT))?'
111 pattern_to_test += r'(?P<mult>\d*)?(?P<region>[jacf])'
112 pattern_to_test += r'(?P<scenario>(HT))?(?P<cut>\d+)?'
113 pattern_to_test += bmatches
114 pattern_to_test += taumatches
115 pattern_to_test += r'emf(?P<emfc>\d+)' if hascalSel else ''
116 if hasDIPZsel: pattern_to_test = r'(?P<scenario>Z)(?P<dipzwp>\d+)?(?P<prefilt>(MAXMULT\d+[jacf]?)?)'
117 matched = re.match(pattern_to_test, p)
118 assert matched is not None, "Impossible to extract preselection cut for \'{0}\' substring. Please investigate.".format(p)
119 cut_dict = matched.groupdict()
120 if hasDIPZsel: cut_dict['region'] = 'c'
121 if hasDIPZsel: cut_dict['cut'] = ptCut
122
123
124
125 testkeys = \
126 [ "mult" , "emfc" , "cut" , "cut_add" , "prefilt"
127 , "bwp" , "btagger" , "dipzwp"
128 , "tauwp"
129 ]
130
131
132 for k in testkeys:
133 cut_dict.setdefault(k, "")
134
135 veto = 'veto' in cut_dict
136 if veto:
137 veto = cut_dict['veto'] is not None
138 mult = cut_dict['mult']
139 region = cut_dict['region']
140 scenario = cut_dict['scenario']
141 cut = cut_dict['cut']
142 dipzwp = cut_dict['dipzwp']
143 emfc = cut_dict['emfc']
144 prefilters = []
145
146 if mult=='': mult='1'
147 etarange = etaRangeAbbrev[region]
148 if veto:
149 scenario = cut_dict['veto'][4:]
150 assert (not (scenario in ['HT','Z'])), "Veto preselection not yet supported for HT or Z scenarios. Please investigate."
151 if scenario == "HT":
152 hyposcenario=f'HT{cut}XX{etarange}'
153 threshold='0'
154 chainPartName=f'j0_{hyposcenario}'
155 elif scenario == "Z":
156 hyposcenario=f'Z{dipzwp}XX{nCentralJets}c{cut}'
157 prefilt = cut_dict['prefilt']
158 if prefilt != '': prefilters.append(prefilt)
159 threshold='0'
160 chainPartName=f'j0_{hyposcenario}'
161 elif scenario == "MULT":
162 hyposcenario=f'MULT0mult{mult}XX{cut}ptXX{etarange}'
163 threshold='0'
164 chainPartName=f'j0_{hyposcenario}'
165 log.info(f'Generated chainPartName {chainPartName}')
166 else:
167 hyposcenario='simple'
168 threshold=cut
169 chainPartName=f'{mult}j{cut}_{etarange}'
170
171 if hasBjetSel:
172 btagger = cut_dict['btagger']
173 bwp = cut_dict['bwp']
174
175 if btagger == 'bg':
176 btagger = 'gnone'
177 elif btagger =='bgtwo':
178 btagger = 'bgntwo'
179 elif btagger == 'b':
180 btagger = 'bdips'
181
182
183 tmpChainDict = dict(preselCommonJetParts)
184
185 if hasTauSel:
186 tauid = cut_dict["tauid"]
187 tauwp = cut_dict["tauwp"]
188
189 tmpChainDict[tauid] = tauwp+tauid
190
191 tmpChainDict.update(
192 {'L1threshold': 'FSNOSEED',
193 'chainPartName': chainPartName,
194 'multiplicity': mult,
195 'threshold': threshold,
196 'etaRange':etarange,
197 'jvt':'',
198 'clrsel': emfc,
199 'bsel': f'{bwp}{btagger}' if hasBjetSel else "",
200 'chainPartIndex': ip,
201 'hypoScenario': hyposcenario,
202 'prefilters': prefilters,
203 }
204 )
205
206 preselChainDict['chainParts'] += [tmpChainDict]
207
208
209
210
211
212 jetlegs = sum([p['signature'] in ["Jet","Bjet"] for p in mainChainDict['chainParts']])
213 padding = jetlegs-len(preselChainDict['chainParts'])
214 if padding>0:
215 preselChainDict['chainParts'][-1]['chainPartName']+='_SHARED'
216 preselChainDict['chainParts'][-1]['tboundary']='SHARED'
217 dummyLegPart = dict(preselCommonJetParts)
218 dummyLegPart.update(
219 {'L1threshold': 'FSNOSEED',
220 'chainPartName': 'j0_SHARED',
221 'multiplicity': '1',
222 'threshold': '0',
223 'jvt':'',
224 'tboundary': 'SHARED',
225 }
226 )
227 preselChainDict['chainParts'] += [dict(dummyLegPart) for i in range(padding)]
228
229 preselChainDict['chainParts'][-1]['chainPartName']='j0'
230 preselChainDict['chainParts'][-1]['tboundary']=''
231
232
233
234
235 for porig,ppresel in zip(mainChainDict['chainParts'],preselChainDict['chainParts']):
236 for key in ['chainPartIndex','signature']:
237 ppresel[key] = porig[key]
238
239 assert(len(preselChainDict['chainParts'])==len(mainChainDict['chainParts']))
240 try:
241 return trigJetHypoToolFromDict(flags, preselChainDict)
242 except NoHypoToolCreated as nohypo:
243 raise nohypo
244 except Exception as e:
245 log.error("Failure with preselection for chain %s",mainChainDict['chainName'])
246 raise e