ATLAS Offline Software
Loading...
Searching...
No Matches
ElectronAnalysisConfig.py
Go to the documentation of this file.
1# Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3# AnaAlgorithm import(s):
4from AnalysisAlgorithmsConfig.ConfigBlock import ConfigBlock
5from AnalysisAlgorithmsConfig.ConfigSequence import groupBlocks
6from AthenaCommon.SystemOfUnits import GeV
7from AthenaConfiguration.Enums import LHCPeriod
8from AnalysisAlgorithmsConfig.ConfigAccumulator import (
9 DataType, ElectronEfficiencyCorrelationWarning,
10 Run4FallbackWarning, TestingOnlyWarning,
11 Run2OnlyFeatureWarning, TriggerSFWarning)
12import warnings
13from TrackingAnalysisAlgorithms.TrackingAnalysisConfig import InDetTrackCalibrationConfig
14from TrigGlobalEfficiencyCorrection.TriggerLeg_DictHelpers import TriggerDict, MapKeysDict
15from AthenaCommon.Logging import logging
16
17# E/gamma import(s).
18from xAODEgamma.xAODEgammaParameters import xAOD
19
21
22
24 """the ConfigBlock for the electron four-momentum correction"""
25
26 def __init__ (self) :
27 super (ElectronMomentumCalibrationConfig, self).__init__ ()
28 self.setBlockName('Electrons')
29 self.addOption ('inputContainer', '', type=str,
30 info="the name of the input electron container. If left empty, automatically defaults "
31 "to `AnalysisElectrons` for PHYSLITE or `Electrons` otherwise.")
32 self.addOption ('containerName', '', type=str,
33 noneAction='error',
34 info="the name of the output container after calibration.")
35 self.addOption ('ESModel', '', type=str,
36 info="flag for Egamma calibration. If left empty, use the current recommendations.")
37 self.addOption ('decorrelationModel', '1NP_v1', type=str,
38 info="decorrelation model for the EGamma energy scale. Supported choices are: `1NP_v1`, `FULL_v1`.")
39 self.addOption ('postfix', '', type=str,
40 info="a postfix to apply to decorations and algorithm names. Typically "
41 "not needed here since the calibration is common to all electrons.")
42 self.addOption ('crackVeto', False, type=bool,
43 info=r"whether to perform LAr crack veto based on the cluster $\eta$, "
44 r"i.e. remove electrons within $1.37<\vert\eta\vert<1.52$.")
45 self.addOption ('isolationCorrection', True, type=bool,
46 info="whether or not to perform isolation corrections (leakage "
47 "corrections), i.e. set up an instance of "
48 "`CP::EgammaIsolationCorrectionAlg`.",
49 expertMode=True)
50 self.addOption ('recalibratePhyslite', True, type=bool,
51 info="whether to run the `CP::EgammaCalibrationAndSmearingAlg` on "
52 "PHYSLITE derivations.")
53 self.addOption ('minPt', 4.5*GeV, type=float,
54 info=r"the minimum $p_\mathrm{T}$ cut (in MeV) to apply to calibrated electrons.")
55 self.addOption ('maxEta', 2.47, type=float,
56 info=r"maximum electron $\vert\eta\vert$.")
57 self.addOption ('forceFullSimConfigForP4', False, type=bool,
58 info="whether to force the tool to use the configuration meant for "
59 "full simulation samples for 4-vector corrections. Only for testing purposes.")
60 self.addOption ('forceFullSimConfigForIso', False, type=bool,
61 info="whether to force the tool to use the configuration meant for "
62 "full simulation samples for isolation corrections. Only for testing purposes.")
63 self.addOption ('splitCalibrationAndSmearing', False, type=bool,
64 info="EXPERIMENTAL: This splits the `EgammaCalibrationAndSmearingTool` "
65 " into two steps. The first step applies a baseline calibration that "
66 "is not affected by systematics. The second step then applies the "
67 "systematics-dependent corrections. The net effect is that the "
68 "slower first step only has to be run once, while the second is run "
69 "once per systematic. ATLASG-2358.",
70 expertMode=True)
71 self.addOption ('decorateTruth', False, type=bool,
72 info="decorate truth particle information on the reconstructed one.")
73 self.addOption ('decorateCaloClusterEta', False, type=bool,
74 info=r"decorate the calo cluster $\eta$.")
75 self.addOption ('decorateEmva', False, type=bool,
76 info="decorate `E_mva_only` on the electrons (needed for columnar tools/PHYSLITE).")
77 self.addOption ('decorateSamplingPattern', False, type=bool,
78 info="decorate `samplingPattern` on the clusters (meant for PHYSLITE).")
79 self.addOption ('addGlobalFELinksDep', False, type=bool,
80 info="whether to add dependencies for the global FE links (needed for PHYSLITE production)",
81 expertMode=True)
82
83 def instanceName (self) :
84 """Return the instance name for this block"""
85 return self.containerName + self.postfix
86
87 def makeCalibrationAndSmearingAlg (self, config, name) :
88 """Create the calibration and smearing algorithm
89
90 Factoring this out into its own function, as we want to
91 instantiate it in multiple places"""
92
93 # Set up the calibration and smearing algorithm:
94 alg = config.createAlgorithm( 'CP::EgammaCalibrationAndSmearingAlg', name )
95 config.addPrivateTool( 'calibrationAndSmearingTool',
96 'CP::EgammaCalibrationAndSmearingTool' )
97 # Set default ESModel per period
98 if self.ESModel:
99 alg.calibrationAndSmearingTool.ESModel = self.ESModel
100 else:
101 if config.geometry() is LHCPeriod.Run2:
102 alg.calibrationAndSmearingTool.ESModel = 'es2023_R22_Run2_v1'
103 elif config.geometry() is LHCPeriod.Run3:
104 alg.calibrationAndSmearingTool.ESModel = 'es2024_Run3_v0'
105 elif config.geometry() is LHCPeriod.Run4:
106 warnings.warn_explicit(
107 "No ESModel set for Run4, using Run 3 model instead",
108 Run4FallbackWarning, filename='', lineno=0)
109 alg.calibrationAndSmearingTool.ESModel = 'es2024_Run3_v0'
110 else:
111 raise ValueError (f"Can't set up the ElectronCalibrationConfig with {config.geometry().value}, "
112 "there must be something wrong!")
113
114 alg.calibrationAndSmearingTool.decorrelationModel = self.decorrelationModel
115 alg.calibrationAndSmearingTool.useFastSim = (
117 else int( config.dataType() is DataType.FastSim ))
118 alg.calibrationAndSmearingTool.decorateEmva = self.decorateEmva
119 alg.egammas = config.readName (self.containerName)
120 alg.egammasOut = config.copyName (self.containerName)
121 alg.preselection = config.getPreselection (self.containerName, '')
122
123 config.setContainerMeta (self.containerName, 'ESModel', alg.calibrationAndSmearingTool.ESModel)
124 config.setContainerMeta (self.containerName, 'decorrelationModel', alg.calibrationAndSmearingTool.decorrelationModel)
125
126 return alg
127
128
129 def makeAlgs (self, config) :
130
132 warnings.warn_explicit(
133 "You are running ElectronCalibrationConfig forcing full sim"
134 " config. This is only intended to be used for testing"
135 " purposes.",
136 TestingOnlyWarning, filename='', lineno=0)
137
138 inputContainer = "AnalysisElectrons" if config.isPhyslite() else "Electrons"
140 inputContainer = self.inputContainer
141 config.setSourceName (self.containerName, inputContainer)
142
143 # Decorate calo cluster eta if required
145 alg = config.createAlgorithm( 'CP::EgammaCaloClusterEtaAlg',
146 'ElectronEgammaCaloClusterEtaAlg',
147 reentrant=True )
148 alg.particles = config.readName(self.containerName)
149 config.addOutputVar (self.containerName, 'caloEta2', 'caloEta2', noSys=True)
150
152 config.createAlgorithm( 'CP::EgammaSamplingPatternDecoratorAlg', 'EgammaSamplingPatternDecoratorAlg' )
153
154 # Set up a shallow copy to decorate
155 if config.wantCopy (self.containerName) :
156 alg = config.createAlgorithm( 'CP::AsgShallowCopyAlg', 'ElectronShallowCopyAlg' )
157 alg.input = config.readName (self.containerName)
158 alg.output = config.copyName (self.containerName)
159 alg.outputType = 'xAOD::ElectronContainer'
160 decorationList = ['DFCommonElectronsLHLoose',
161 'neflowisol20',
162 'ptcone20_Nonprompt_All_MaxWeightTTVALooseCone_pt500',
163 'ptvarcone30_Nonprompt_All_MaxWeightTTVALooseCone_pt500',
164 'ptcone20_Nonprompt_All_MaxWeightTTVALooseCone_pt1000_CloseByCorr',
165 'ptvarcone30_Nonprompt_All_MaxWeightTTVALooseCone_pt1000_CloseByCorr',
166 'topoetcone20_CloseByCorr','DFCommonAddAmbiguity']
168 decorationList += ['neutralGlobalFELinks', 'chargedGlobalFELinks']
169 if config.dataType() is not DataType.Data:
170 decorationList += ['TruthLink']
171 alg.declareDecorations = decorationList
172
173 # Set up the eta-cut on all electrons prior to everything else
174 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronEtaCutAlg' )
175 alg.selectionDecoration = 'selectEta' + self.postfix + ',as_bits'
176 config.addPrivateTool( 'selectionTool', 'CP::AsgPtEtaSelectionTool' )
177 alg.selectionTool.maxEta = self.maxEta
178 if self.crackVeto:
179 alg.selectionTool.etaGapLow = 1.37
180 alg.selectionTool.etaGapHigh = 1.52
181 alg.selectionTool.useClusterEta = True
182 alg.particles = config.readName (self.containerName)
183 alg.preselection = config.getPreselection (self.containerName, '')
184 config.addSelection (self.containerName, '', alg.selectionDecoration)
185
186 # Select electrons only with good object quality.
187 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronObjectQualityAlg' )
188 config.setExtraInputs ({('xAOD::EventInfo', 'EventInfo.RandomRunNumber')})
189 alg.selectionDecoration = 'goodOQ' + self.postfix + ',as_bits'
190 config.addPrivateTool( 'selectionTool', 'CP::EgammaIsGoodOQSelectionTool' )
191 alg.selectionTool.Mask = xAOD.EgammaParameters.BADCLUSELECTRON
192 alg.particles = config.readName (self.containerName)
193 alg.preselection = config.getPreselection (self.containerName, '')
194 config.addSelection (self.containerName, '', alg.selectionDecoration)
195
197 # Set up the calibration and smearing algorithm:
198 alg = self.makeCalibrationAndSmearingAlg (config, 'ElectronCalibrationAndSmearingAlg')
199 if config.isPhyslite() and not self.recalibratePhyslite :
200 alg.skipNominal = True
201 else:
202 # This splits the EgammaCalibrationAndSmearingTool into two
203 # steps. The first step applies a baseline calibration that
204 # is not affected by systematics. The second step then
205 # applies the systematics dependent corrections. The net
206 # effect is that the slower first step only has to be run
207 # once, while the second is run once per systematic.
208 #
209 # For now (22 May 24) this has to happen in the same job, as
210 # the output of the first step is not part of PHYSLITE, and
211 # even for the nominal the output of the first and second
212 # step are different. In the future the plan is to put both
213 # the output of the first and second step into PHYSLITE,
214 # allowing to skip the first step when running on PHYSLITE.
215 #
216 # WARNING: All of this is experimental, see: ATLASG-2358
217
218 # Set up the calibration algorithm:
219 alg = self.makeCalibrationAndSmearingAlg (config, 'ElectronBaseCalibrationAlg')
220 # turn off systematics for the calibration step
221 alg.noToolSystematics = True
222 # turn off smearing for the calibration step
223 alg.calibrationAndSmearingTool.doSmearing = False
224
225 # Set up the smearing algorithm:
226 alg = self.makeCalibrationAndSmearingAlg (config, 'ElectronCalibrationSystematicsAlg')
227 # turn off scale corrections for the smearing step
228 alg.calibrationAndSmearingTool.doScaleCorrection = False
229 alg.calibrationAndSmearingTool.useMVACalibration = False
230 alg.calibrationAndSmearingTool.decorateEmva = False
231
232 if self.minPt > 0 :
233 # Set up the the pt selection
234 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronPtCutAlg' )
235 alg.selectionDecoration = 'selectPt' + self.postfix + ',as_bits'
236 config.addPrivateTool( 'selectionTool', 'CP::AsgPtEtaSelectionTool' )
237 alg.selectionTool.minPt = self.minPt
238 alg.particles = config.readName (self.containerName)
239 alg.preselection = config.getPreselection (self.containerName, '')
240 config.addSelection (self.containerName, '', alg.selectionDecoration,
241 preselection=True)
242
243 # Set up the isolation correction algorithm:
245 alg = config.createAlgorithm( 'CP::EgammaIsolationCorrectionAlg',
246 'ElectronIsolationCorrectionAlg' )
247 config.addPrivateTool( 'isolationCorrectionTool',
248 'CP::IsolationCorrectionTool' )
249 alg.isolationCorrectionTool.IsMC = config.dataType() is not DataType.Data
250 alg.isolationCorrectionTool.AFII_corr = (
251 0 if self.forceFullSimConfigForIso
252 else config.dataType() is DataType.FastSim)
253 alg.isolationCorrectionTool.FixTimingIssueInCore = True
254 alg.isolationCorrectionTool.ToolVer = "REL22"
255 alg.isolationCorrectionTool.CorrFile = "IsolationCorrections/v6/isolation_ptcorrections_rel22_mc20.root"
256 alg.egammas = config.readName (self.containerName)
257 alg.egammasOut = config.copyName (self.containerName)
258 alg.egammasType = 'xAOD::ElectronContainer'
259 alg.preselection = config.getPreselection (self.containerName, '')
260 else:
261 warnings.warn_explicit(
262 "You are not applying the isolation corrections."
263 " This is only intended to be used for testing purposes.",
264 TestingOnlyWarning, filename='', lineno=0)
265
266 alg = config.createAlgorithm( 'CP::AsgEnergyDecoratorAlg', 'EnergyDecorator' )
267 alg.particles = config.readName(self.containerName)
268
269 config.addOutputVar (self.containerName, 'pt', 'pt')
270 config.addOutputVar (self.containerName, 'eta', 'eta', noSys=True)
271 config.addOutputVar (self.containerName, 'phi', 'phi', noSys=True)
272 config.addOutputVar (self.containerName, 'e_%SYS%', 'e')
273 config.addOutputVar (self.containerName, 'charge', 'charge', noSys=True)
274 config.addOutputVar (self.containerName, 'caloClusterEnergyReso_%SYS%', 'caloClusterEnergyReso', noSys=True)
275
276 # decorate truth information on the reconstructed object:
277 if self.decorateTruth and config.dataType() is not DataType.Data:
278 config.addOutputVar (self.containerName, "truthType", "truth_type", noSys=True, auxType='int')
279 config.addOutputVar (self.containerName, "truthOrigin", "truth_origin", noSys=True, auxType='int')
280
281 config.addOutputVar (self.containerName, "firstEgMotherPdgId", "truth_firstEgMotherPdgId", noSys=True, auxType='int')
282 config.addOutputVar (self.containerName, "firstEgMotherTruthOrigin", "truth_firstEgMotherTruthOrigin", noSys=True, auxType='int')
283 config.addOutputVar (self.containerName, "firstEgMotherTruthType", "truth_firstEgMotherTruthType", noSys=True, auxType='int')
284
285
286class ElectronIPCalibrationConfig (ConfigBlock) :
287 """the ConfigBlock for the electron impact parameter correction"""
288
289 def __init__ (self) :
290 super (ElectronIPCalibrationConfig, self).__init__ ()
291 self.setBlockName('ElectronIPCalibration')
292 self.addDependency('Electrons', required=True)
293 self.addDependency('ElectronWorkingPointSelection', required=False)
294 self.addOption ('containerName', '', type=str,
295 noneAction='error',
296 info="the name of the output container after calibration.")
297 self.addOption ('postfix', '', type=str,
298 info="a postfix to apply to decorations and algorithm names. Typically "
299 "not needed here since the calibration is common to all electrons.")
300 self.addOption ('runTrackBiasing', False, type=bool,
301 info="This enables the `InDetTrackBiasingTool`, for "
302 "tracks associated to electrons")
303 self.addOption ('writeTrackD0Z0', False, type = bool,
304 info=r"save the $d_0$ significance and $z_0\sin\theta$ variables.")
305
306 def instanceName (self) :
307 """Return the instance name for this block"""
308 return self.containerName + self.postfix
309
310 def makeAlgs (self, config) :
311
312 # Additional decorations
314 alg = config.createAlgorithm( 'CP::AsgLeptonTrackDecorationAlg',
315 'LeptonTrackDecorator' )
316 if config.dataType() is not DataType.Data:
318 InDetTrackCalibrationConfig.makeTrackBiasingTool(config, alg)
319 InDetTrackCalibrationConfig.makeTrackSmearingTool(config, alg)
320 alg.particles = config.readName (self.containerName)
321
322 config.addOutputVar (self.containerName, 'd0_%SYS%', 'd0')
323 config.addOutputVar (self.containerName, 'd0sig_%SYS%', 'd0sig')
324 config.addOutputVar (self.containerName, 'z0_%SYS%', 'z0')
325 config.addOutputVar (self.containerName, 'z0sintheta_%SYS%', 'z0sintheta')
326 config.addOutputVar (self.containerName, 'z0sinthetasig_%SYS%', 'z0sinthetasig')
327
328
330 """the ConfigBlock for the electron working point selection"""
331
332 def __init__ (self) :
333 super (ElectronWorkingPointSelectionConfig, self).__init__ ()
334 self.setBlockName('ElectronWorkingPointSelection')
335 self.addOption ('containerName', '', type=str,
336 noneAction='error',
337 info="the name of the input container.")
338 self.addOption ('selectionName', '', type=str,
339 noneAction='error',
340 info="the name of the electron selection to define (e.g. `tight` or "
341 "`loose`).")
342 self.addOption ('postfix', None, type=str,
343 info="a postfix to apply to decorations and algorithm names. "
344 "Typically not needed here as `selectionName` is used internally.")
345 self.addOption ('trackSelection', True, type=bool,
346 info="whether or not to set up an instance of "
347 "`CP::AsgLeptonTrackSelectionAlg`, with the recommended $d_0$ and "
348 r"$z_0\sin\theta$ cuts.")
349 self.addOption ('maxD0Significance', 5, type=float,
350 info="maximum $d_0$ significance used for the track selection.")
351 self.addOption ('maxDeltaZ0SinTheta', 0.5, type=float,
352 info=r"maximum $z_0\sin\theta$ (in mm) used for the track selection.")
353 self.addOption ('identificationWP', None, type=str,
354 info="the ID WP to use. Supported ID WPs: `TightLH`, "
355 "`MediumLH`, `LooseBLayerLH`, `TightDNN`, `MediumDNN`, `LooseDNN`, "
356 "`TightNoCFDNN`, `MediumNoCFDNN`, `VeryLooseNoCF97DNN`, `NoID`.",
357 expertMode=["NoID"])
358 self.addOption ('isolationWP', None, type=str,
359 info="the isolation WP to use. Supported isolation WPs: "
360 "`HighPtCaloOnly`, `Loose_VarRad`, `Tight_VarRad`, `TightTrackOnly_"
361 "VarRad`, `TightTrackOnly_FixedRad`, `NonIso`.")
362 self.addOption ('convSelection', None, type=str,
363 info="enter additional selection to use for conversions. To be used with "
364 "`TightLH` or will crash. Supported keywords:"
365 "`Veto`, `MatConv`, `GammaStar`.")
366 self.addOption ('addSelectionToPreselection', True, type=bool,
367 info="whether to retain only electrons satisfying the working point "
368 "requirements.")
369 self.addOption ('closeByCorrection', False, type=bool,
370 info="whether to use close-by-corrected isolation working points.")
371 self.addOption ('recomputeID', False, type=bool,
372 info="whether to rerun the ID LH/DNN, or rely on derivation flags.")
373 self.addOption ('chargeIDSelectionRun2', False, type=bool,
374 info="whether to run the ECIDS tool. Only available for Run 2.")
375 self.addOption ('recomputeChargeID', False, type=bool,
376 info="whether to rerun the ECIDS, or rely on derivation flags.")
377 self.addOption ('doFSRSelection', False, type=bool,
378 info="whether to accept additional electrons close to muons for "
379 "the purpose of FSR corrections to these muons. Expert feature "
380 "requested by the H4l analysis running on PHYSLITE.",
381 expertMode=True)
382 self.addOption ('muonsForFSRSelection', None, type=str,
383 info="the name of the muon container to use for the FSR selection. "
384 "If not specified, AnalysisMuons is used.",
385 expertMode=True)
386 self.addOption ('mainElectronContainer', None, type=str,
387 info="the name of the main electron container to use for the SiHit selection. "
388 "If not specified, this defaults to AnalysisElectrons.",
389 expertMode=True)
390
391 def instanceName (self) :
392 """Return the instance name for this block"""
393 if self.postfix is not None :
394 return self.containerName + '_' + self.selectionName + self.postfix
395 return self.containerName + '_' + self.selectionName
396
397 def makeAlgs (self, config) :
398
399 selectionPostfix = self.selectionName
400 if selectionPostfix != '' and selectionPostfix[0] != '_' :
401 selectionPostfix = '_' + selectionPostfix
402
403 # The setup below is inappropriate for Run 1
404 if config.geometry() is LHCPeriod.Run1:
405 raise ValueError ("Can't set up the ElectronWorkingPointSelectionConfig with %s, there must be something wrong!" % config.geometry().value)
406
407 postfix = self.postfix
408 if postfix is None :
409 postfix = self.selectionName
410 if postfix != '' and postfix[0] != '_' :
411 postfix = '_' + postfix
412
413 # Set up the track selection algorithm:
415 alg = config.createAlgorithm( 'CP::AsgLeptonTrackSelectionAlg',
416 'ElectronTrackSelectionAlg',
417 reentrant=True )
418 alg.selectionDecoration = 'trackSelection' + postfix + ',as_bits'
419 alg.maxD0Significance = self.maxD0Significance
420 alg.maxDeltaZ0SinTheta = self.maxDeltaZ0SinTheta
421 alg.particles = config.readName (self.containerName)
422 alg.preselection = config.getPreselection (self.containerName, '')
423 if self.trackSelection :
424 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
425 preselection=self.addSelectionToPreselection)
426
427 if 'LH' in self.identificationWP:
428 # Set up the likelihood ID selection algorithm
429 # It is safe to do this before calibration, as the cluster E is used
430 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronLikelihoodAlg' )
431 alg.selectionDecoration = 'selectLikelihood' + selectionPostfix + ',as_char'
432 if self.recomputeID:
433 # Rerun the likelihood ID
434 config.addPrivateTool( 'selectionTool', 'AsgElectronLikelihoodTool' )
435 alg.selectionTool.primaryVertexContainer = 'PrimaryVertices'
436 # Here we have to match the naming convention of EGSelectorConfigurationMapping.h
437 # which differ from the one used for scale factors
438 if config.geometry() >= LHCPeriod.Run3:
439 if 'HI' not in self.identificationWP:
440 alg.selectionTool.WorkingPoint = self.identificationWP.replace("BLayer","BL") + 'Electron'
441 else:
442 alg.selectionTool.WorkingPoint = self.identificationWP.replace('_HI', 'Electron_HI')
443 elif config.geometry() is LHCPeriod.Run2:
444 alg.selectionTool.WorkingPoint = self.identificationWP.replace("BLayer","BL") + 'Electron_Run2'
445 else:
446 # Select from Derivation Framework flags
447 config.addPrivateTool( 'selectionTool', 'CP::AsgFlagSelectionTool' )
448 dfFlag = "DFCommonElectronsLH" + self.identificationWP.split('LH')[0]
449 dfFlag = dfFlag.replace("BLayer","BL")
450 alg.selectionTool.selectionFlags = [dfFlag]
451 elif 'SiHit' in self.identificationWP:
452 # Only want SiHit electrons, so veto loose LH electrons
453 algVeto = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronLikelihoodAlgVeto')
454 algVeto.selectionDecoration = 'selectLikelihoodVeto' + postfix + ',as_char'
455 config.addPrivateTool( 'selectionTool', 'CP::AsgFlagSelectionTool' )
456 algVeto.selectionTool.selectionFlags = ["DFCommonElectronsLHLoose"]
457 algVeto.selectionTool.invertFlags = [True]
458 algVeto.particles = config.readName (self.containerName)
459 algVeto.preselection = config.getPreselection (self.containerName, self.selectionName)
460 # add the veto as a selection
461 config.addSelection (self.containerName, self.selectionName, algVeto.selectionDecoration,
462 preselection=self.addSelectionToPreselection)
463
464 # Select SiHit electrons using IsEM bits
465 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronLikelihoodAlg' )
466 alg.selectionDecoration = 'selectSiHit' + selectionPostfix + ',as_char'
467 # Select from Derivation Framework IsEM bits
468 config.addPrivateTool( 'selectionTool', 'CP::AsgMaskSelectionTool' )
469 dfVar = "DFCommonElectronsLHLooseBLIsEMValue"
470 alg.selectionTool.selectionVars = [dfVar]
471 mask = int( 0 | 0x1 << 1 | 0x1 << 2)
472 alg.selectionTool.selectionMasks = [mask]
473 elif 'DNN' in self.identificationWP:
475 raise ValueError('DNN is not intended to be used with '
476 '`chargeIDSelectionRun2` option as there are '
477 'DNN WPs containing charge flip rejection.')
478 # Set up the DNN ID selection algorithm
479 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronDNNAlg' )
480 alg.selectionDecoration = 'selectDNN' + selectionPostfix + ',as_char'
481 if self.recomputeID:
482 # Rerun the DNN ID
483 config.addPrivateTool( 'selectionTool', 'AsgElectronSelectorTool' )
484 # Here we have to match the naming convention of EGSelectorConfigurationMapping.h
485 if config.geometry() is LHCPeriod.Run3:
486 raise ValueError ( "DNN working points are not available for Run 3 yet.")
487 else:
488 alg.selectionTool.WorkingPoint = self.identificationWP + 'Electron'
489 else:
490 # Select from Derivation Framework flags
491 config.addPrivateTool( 'selectionTool', 'CP::AsgFlagSelectionTool' )
492 dfFlag = "DFCommonElectronsDNN" + self.identificationWP.split('DNN')[0]
493 alg.selectionTool.selectionFlags = [dfFlag]
494 elif self.identificationWP == 'NoID':
495 alg = None
496 else:
497 raise ValueError (f"Electron ID working point '{self.identificationWP}' is not recognised!")
498
499 if alg is not None:
500 alg.particles = config.readName (self.containerName)
501 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
502 # Don't register WP selection here if FSR enabled - FSR algorithm will create combined selection
503 if not self.doFSRSelection:
504 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
505 preselection=self.addSelectionToPreselection)
506
507 # maintain order of selections
508 if 'SiHit' in self.identificationWP:
509 # Set up the ElectronSiHitDecAlg algorithm to decorate SiHit electrons with a minimal amount of information:
510 algDec = config.createAlgorithm( 'CP::ElectronSiHitDecAlg', 'ElectronSiHitDecAlg' )
511 selDec = 'siHitEvtHasLeptonPair' + selectionPostfix + ',as_char'
512 algDec.selectionName = selDec.split(",")[0]
513 algDec.ElectronContainer = config.readName (self.containerName)
514 if self.muonsForFSRSelection is not None:
515 algDec.AnalMuonContKey = config.readName (self.muonsForFSRSelection)
516 if self.mainElectronContainer is not None:
517 algDec.AnalElectronContKey = config.readName (self.mainElectronContainer)
518 # Set flag to only collect SiHit electrons for events with an electron or muon pair to minimize size increase from SiHit electrons
519 algDec.RequireTwoLeptons = True
520 config.addSelection (self.containerName, self.selectionName, selDec,
521 preselection=self.addSelectionToPreselection)
522
523 # Additional selection for conversions and gamma*
524 if self.convSelection is not None:
525 # skip if not applied together with TightLH
526 if self.identificationWP != 'TightLH':
527 raise ValueError(f"convSelection can only be used with TightLH ID, "
528 f"whereas {self.identificationWP} has been selected. convSelection option will be ignored.")
529 # check if allowed value
530 allowedValues = ["Veto", "GammaStar", "MatConv"]
531 if self.convSelection not in allowedValues:
532 raise ValueError(f"convSelection has been set to {self.convSelection}, which is not a valid option. "
533 f"convSelection option must be one of {allowedValues}.")
534
535 # ambiguityType == 0
536 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronAmbiguityTypeAlg' )
537 alg.selectionDecoration = 'selectAmbiguityType' + selectionPostfix + ',as_char'
538 config.addPrivateTool( 'selectionTool', 'CP::AsgNumDecorationSelectionToolUInt8' )
539 alg.selectionTool.decorationName = "ambiguityType"
540 alg.selectionTool.doEqual = True
541 alg.selectionTool.equal = 0
542 alg.particles = config.readName (self.containerName)
543 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
544 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
545 preselection=self.addSelectionToPreselection)
546
547 # DFCommonAddAmbiguity selection
548 alg = config.createAlgorithm( 'CP::AsgSelectionAlg', 'ElectronDFCommonAddAmbiguityAlg' )
549 alg.selectionDecoration = 'selectDFCommonAddAmbiguity' + selectionPostfix + ',as_char'
550 config.addPrivateTool( 'selectionTool', 'CP::AsgNumDecorationSelectionToolInt' )
551 alg.selectionTool.decorationName = "DFCommonAddAmbiguity"
552 if self.convSelection == "Veto":
553 alg.selectionTool.doMax = True
554 alg.selectionTool.max = 1
555 elif self.convSelection == "GammaStar":
556 alg.selectionTool.doEqual = True
557 alg.selectionTool.equal = 1
558 elif self.convSelection == "MatConv":
559 alg.selectionTool.doEqual = True
560 alg.selectionTool.equal = 2
561 alg.particles = config.readName (self.containerName)
562 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
563 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
564 preselection=self.addSelectionToPreselection)
565
566 # Set up the FSR selection
567 if self.doFSRSelection :
568 # wpSelection needs the type suffix so SysReadSelectionHandle knows the type
569 # selectionDecoration needs name only for SysWriteDecorHandle
570 wpDecoration = alg.selectionDecoration
571 wpDecorationName = wpDecoration.split(',')[0]
572 # Insert FSR before the postfix (e.g., selectSiHit_SiHits -> selectSiHitFSR_SiHits)
573 underscorePos = wpDecorationName.index('_')
574 outputDecorationName = wpDecorationName[:underscorePos] + 'FSR' + wpDecorationName[underscorePos:]
575 alg = config.createAlgorithm( 'CP::EgammaFSRForMuonsCollectorAlg', 'EgammaFSRForMuonsCollectorAlg' )
576 alg.wpSelection = wpDecoration # Input: read the WP selection (with type suffix)
577 alg.selectionDecoration = outputDecorationName # Output: combined WP||FSR (or WP&&!FSR for vetoFSR) (name only)
578 alg.ElectronOrPhotonContKey = config.readName (self.containerName)
579 if self.muonsForFSRSelection is not None:
580 alg.MuonContKey = config.readName (self.muonsForFSRSelection)
581 # For SiHit electrons, set flag to remove FSR electrons.
582 # For standard electrons, FSR electrons need to be added as they may be missed by the standard selection.
583 # For SiHit electrons FSR electrons are generally always selected, so they should be removed since they will be in the standard electron container.
584 if 'SiHit' in self.identificationWP:
585 alg.vetoFSR = True
586
587 # Register the FSR COMBINED selection
588 config.addSelection (self.containerName, self.selectionName,
589 alg.selectionDecoration + ',as_char',
590 preselection=self.addSelectionToPreselection)
591
592 # Set up the isolation selection algorithm:
593 if self.isolationWP != 'NonIso' :
594 alg = config.createAlgorithm( 'CP::EgammaIsolationSelectionAlg',
595 'ElectronIsolationSelectionAlg' )
596 alg.selectionDecoration = 'isolated' + selectionPostfix + ',as_char'
597 config.addPrivateTool( 'selectionTool', 'CP::IsolationSelectionTool' )
598 alg.selectionTool.ElectronWP = self.isolationWP
600 alg.selectionTool.IsoDecSuffix = "CloseByCorr"
601 alg.egammas = config.readName (self.containerName)
602 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
603 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
604 preselection=self.addSelectionToPreselection)
605
606 if self.chargeIDSelectionRun2 and config.geometry() >= LHCPeriod.Run3:
607 warnings.warn_explicit(
608 "ECIDS is only available for Run 2 and will not have any"
609 " effect in Run 3.",
610 Run2OnlyFeatureWarning, filename='', lineno=0)
611
612 # Select electrons only if they don't appear to have flipped their charge.
613 if self.chargeIDSelectionRun2 and config.geometry() < LHCPeriod.Run3:
614 alg = config.createAlgorithm( 'CP::AsgSelectionAlg',
615 'ElectronChargeIDSelectionAlg' )
616 alg.selectionDecoration = 'chargeID' + selectionPostfix + ',as_char'
618 # Rerun the ECIDS BDT
619 config.addPrivateTool( 'selectionTool',
620 'AsgElectronChargeIDSelectorTool' )
621 alg.selectionTool.TrainingFile = \
622 'ElectronPhotonSelectorTools/ChargeID/ECIDS_20180731rel21Summer2018.root'
623 alg.selectionTool.WorkingPoint = 'Loose'
624 alg.selectionTool.CutOnBDT = -0.337671 # Loose 97%
625 else:
626 # Select from Derivation Framework flags
627 config.addPrivateTool( 'selectionTool', 'CP::AsgFlagSelectionTool' )
628 alg.selectionTool.selectionFlags = ["DFCommonElectronsECIDS"]
629
630 alg.particles = config.readName (self.containerName)
631 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
632 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
633 preselection=self.addSelectionToPreselection)
634
635
637 """the ConfigBlock for the electron working point efficiency computation"""
638
639 def __init__(self) :
640 super (ElectronWorkingPointEfficiencyConfig, self).__init__ ()
641 self.setBlockName('ElectronWorkingPointEfficiency')
642 self.addDependency('ElectronWorkingPointSelection', required=True)
643 self.addDependency('EventSelection', required=False)
644 self.addDependency('EventSelectionMerger', required=False)
645 self.addOption ('containerName', '', type=str,
646 noneAction='error',
647 info="the name of the input container.")
648 self.addOption ('selectionName', '', type=str,
649 noneAction='error',
650 info="the name of the electron selection to define (e.g. `tight` or "
651 "`loose`).")
652 self.addOption ('postfix', None, type=str,
653 info="a postfix to apply to decorations and algorithm names. "
654 "Typically not needed here as `selectionName` is used internally.")
655 self.addOption ('identificationWP', None, type=str,
656 info="the ID WP to use. Supported ID WPs: `TightLH`, "
657 "`MediumLH`, `LooseBLayerLH`, `TightDNN`, `MediumDNN`, `LooseDNN`, "
658 "`TightNoCFDNN`, `MediumNoCFDNN`, `VeryLooseNoCF97DNN`, `NoID`.",
659 expertMode=["NoID"])
660 self.addOption ('isolationWP', None, type=str,
661 info="the isolation WP to use. Supported isolation WPs: "
662 "`HighPtCaloOnly`, `Loose_VarRad`, `Tight_VarRad`, `TightTrackOnly_"
663 "VarRad`, `TightTrackOnly_FixedRad`, `NonIso`.")
664 self.addOption ('noEffSF', False, type=bool,
665 info="disables the calculation of efficiencies and scale factors. "
666 "Experimental! only useful to test a new WP for which scale "
667 "factors are not available.",
668 expertMode=True)
669 self.addOption ('chargeIDSelectionRun2', False, type=bool,
670 info="whether to run the ECIDS tool. Only available for Run 2.")
671 self.addOption ('saveDetailedSF', True, type=bool,
672 info="save all the independent detailed object scale factors.")
673 self.addOption ('saveCombinedSF', False, type=bool,
674 info="save the combined object scale factor.")
675 self.addOption ('forceFullSimConfig', False, type=bool,
676 info="whether to force the tool to use the configuration meant for "
677 "full simulation samples. Only for testing purposes.")
678 self.addOption ('correlationModelId', 'SIMPLIFIED', type=str,
679 info="the correlation model to use for ID scale factors. "
680 "Supported models: `SIMPLIFIED`, `FULL`, `TOTAL`, `TOYS`.")
681 self.addOption ('correlationModelIso', 'SIMPLIFIED', type=str,
682 info="the correlation model to use for isolation scale factors, "
683 "Supported models: `SIMPLIFIED`, `FULL`, `TOTAL`, `TOYS`.")
684 self.addOption ('correlationModelReco', 'SIMPLIFIED', type=str,
685 info="the correlation model to use for reconstruction scale factors. "
686 "Supported models: `SIMPLIFIED`, `FULL`, `TOTAL`, `TOYS`.")
687 self.addOption('addChargeMisIDSF', False, type=bool,
688 info="adds scale factors for charge-misID.")
689
690 def instanceName (self) :
691 """Return the instance name for this block"""
692 if self.postfix is not None :
693 return self.containerName + '_' + self.selectionName + self.postfix
694 return self.containerName + '_' + self.selectionName
695
696 def makeAlgs (self, config) :
697
699 warnings.warn_explicit(
700 "You are running ElectronWorkingPointSelectionConfig forcing"
701 " full sim config. This is only intended to be used for"
702 " testing purposes.",
703 TestingOnlyWarning, filename='', lineno=0)
704
705 selectionPostfix = self.selectionName
706 if selectionPostfix != '' and selectionPostfix[0] != '_' :
707 selectionPostfix = '_' + selectionPostfix
708
709 # The setup below is inappropriate for Run 1
710 if config.geometry() is LHCPeriod.Run1:
711 raise ValueError ("Can't set up the ElectronWorkingPointSelectionConfig with %s, there must be something wrong!" % config.geometry().value)
712
713 postfix = self.postfix
714 if postfix is None :
715 postfix = self.selectionName
716 if postfix != '' and postfix[0] != '_' :
717 postfix = '_' + postfix
718
719 correlationModels = ["SIMPLIFIED", "FULL", "TOTAL", "TOYS"]
720 map_file = 'ElectronEfficiencyCorrection/2015_2025/rel22.2/2026_Run2Run3_Recommendation_v1/map1.txt'
721 sfList = []
722 # Set up the RECO electron efficiency correction algorithm:
723 if config.dataType() is not DataType.Data and not self.noEffSF:
724 if 'DNN' in self.identificationWP:
725 raise ValueError('DNN does not yet have efficiency correction, '
726 'please disable it by setting `noEffSF` to True.')
727
728 alg = config.createAlgorithm( 'CP::ElectronEfficiencyCorrectionAlg',
729 'ElectronEfficiencyCorrectionAlgReco' )
730 config.addPrivateTool( 'efficiencyCorrectionTool',
731 'AsgElectronEfficiencyCorrectionTool' )
732 alg.scaleFactorDecoration = 'el_reco_effSF' + selectionPostfix + '_%SYS%'
733 alg.efficiencyCorrectionTool.MapFilePath = map_file
734 alg.efficiencyCorrectionTool.RecoKey = "Reconstruction"
735 if self.correlationModelReco not in correlationModels:
736 raise ValueError('Invalid correlation model for reconstruction efficiency, '
737 f'has to be one of: {", ".join(correlationModels)}')
738 if config.geometry() >= LHCPeriod.Run3 and self.correlationModelReco != "TOTAL":
739 warnings.warn_explicit(
740 "Only TOTAL correlation model is currently supported "
741 "for reconstruction efficiency correction in Run 3.",
742 ElectronEfficiencyCorrelationWarning,
743 filename='', lineno=0)
744 alg.efficiencyCorrectionTool.CorrelationModel = "TOTAL"
745 else:
746 alg.efficiencyCorrectionTool.CorrelationModel = self.correlationModelReco
747 if config.dataType() is DataType.FastSim:
748 alg.efficiencyCorrectionTool.ForceDataType = (
749 PATCore.ParticleDataType.Full if self.forceFullSimConfig
750 else PATCore.ParticleDataType.Fast)
751 elif config.dataType() is DataType.FullSim:
752 alg.efficiencyCorrectionTool.ForceDataType = \
753 PATCore.ParticleDataType.Full
754 alg.outOfValidity = 2 #silent
755 alg.outOfValidityDeco = 'el_reco_bad_eff' + selectionPostfix
756 alg.electrons = config.readName (self.containerName)
757 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
759 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
760 'reco_effSF' + postfix)
761 sfList += [alg.scaleFactorDecoration]
762
763 # Set up the ID electron efficiency correction algorithm:
764 if config.dataType() is not DataType.Data and not self.noEffSF and self.identificationWP != 'NoID':
765
766 alg = config.createAlgorithm( 'CP::ElectronEfficiencyCorrectionAlg',
767 'ElectronEfficiencyCorrectionAlgID' )
768 config.addPrivateTool( 'efficiencyCorrectionTool',
769 'AsgElectronEfficiencyCorrectionTool' )
770 alg.scaleFactorDecoration = 'el_id_effSF' + selectionPostfix + '_%SYS%'
771 alg.efficiencyCorrectionTool.MapFilePath = map_file
772 alg.efficiencyCorrectionTool.IdKey = self.identificationWP
773 if self.correlationModelId not in correlationModels:
774 raise ValueError('Invalid correlation model for identification efficiency, '
775 f'has to be one of: {", ".join(correlationModels)}')
776 alg.efficiencyCorrectionTool.CorrelationModel = self.correlationModelId
777 if config.dataType() is DataType.FastSim:
778 alg.efficiencyCorrectionTool.ForceDataType = (
779 PATCore.ParticleDataType.Full if self.forceFullSimConfig
780 else PATCore.ParticleDataType.Fast)
781 elif config.dataType() is DataType.FullSim:
782 alg.efficiencyCorrectionTool.ForceDataType = \
783 PATCore.ParticleDataType.Full
784 alg.outOfValidity = 2 #silent
785 alg.outOfValidityDeco = 'el_id_bad_eff' + selectionPostfix
786 alg.electrons = config.readName (self.containerName)
787 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
788 if self.saveDetailedSF:
789 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
790 'id_effSF' + postfix)
791 sfList += [alg.scaleFactorDecoration]
792
793 # Set up the ISO electron efficiency correction algorithm:
794 if config.dataType() is not DataType.Data and self.isolationWP != 'NonIso' and not self.noEffSF:
795 alg = config.createAlgorithm( 'CP::ElectronEfficiencyCorrectionAlg',
796 'ElectronEfficiencyCorrectionAlgIsol' )
797 config.addPrivateTool( 'efficiencyCorrectionTool',
798 'AsgElectronEfficiencyCorrectionTool' )
799 alg.scaleFactorDecoration = 'el_isol_effSF' + selectionPostfix + '_%SYS%'
800 alg.efficiencyCorrectionTool.MapFilePath = map_file
801 alg.efficiencyCorrectionTool.IdKey = self.identificationWP
802 alg.efficiencyCorrectionTool.IsoKey = self.isolationWP
803 if self.correlationModelIso not in correlationModels:
804 raise ValueError('Invalid correlation model for isolation efficiency, '
805 f'has to be one of: {", ".join(correlationModels)}')
806 if self.correlationModelIso != 'TOTAL':
807 warnings.warn_explicit(
808 "Only TOTAL correlation model is currently supported "
809 "for isolation efficiency correction in Run 3.",
810 ElectronEfficiencyCorrelationWarning,
811 filename='', lineno=0)
812 alg.efficiencyCorrectionTool.CorrelationModel = "TOTAL"
813 if config.dataType() is DataType.FastSim:
814 alg.efficiencyCorrectionTool.ForceDataType = (
815 PATCore.ParticleDataType.Full if self.forceFullSimConfig
816 else PATCore.ParticleDataType.Fast)
817 elif config.dataType() is DataType.FullSim:
818 alg.efficiencyCorrectionTool.ForceDataType = \
819 PATCore.ParticleDataType.Full
820 alg.outOfValidity = 2 #silent
821 alg.outOfValidityDeco = 'el_isol_bad_eff' + selectionPostfix
822 alg.electrons = config.readName (self.containerName)
823 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
824 if self.saveDetailedSF:
825 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
826 'isol_effSF' + postfix, auxType='float')
827 sfList += [alg.scaleFactorDecoration]
828
829 if (self.chargeIDSelectionRun2 and config.geometry() < LHCPeriod.Run3 and
830 config.dataType() is not DataType.Data and not self.noEffSF):
831 alg = config.createAlgorithm( 'CP::ElectronEfficiencyCorrectionAlg',
832 'ElectronEfficiencyCorrectionAlgEcids' )
833 config.addPrivateTool( 'efficiencyCorrectionTool',
834 'AsgElectronEfficiencyCorrectionTool' )
835 alg.scaleFactorDecoration = 'el_ecids_effSF' + selectionPostfix + '_%SYS%'
836 if self.isolationWP != 'Tight_VarRad':
837 raise ValueError('ECIDS SFs are supported only for Tight_VarRad isolation.')
838 if self.identificationWP == 'LooseBLayerLH':
839 ecids_lh = 'loose'
840 elif self.identificationWP == 'MediumLH':
841 ecids_lh = 'medium'
842 elif self.identificationWP == 'TightLH':
843 ecids_lh = 'tight'
844 else:
845 raise ValueError('ECIDS SFs are supported only for ID LooseBLayerLH, MediumLH, or TightLH')
846
847 alg.efficiencyCorrectionTool.CorrelationModel = "TOTAL"
848 alg.efficiencyCorrectionTool.CorrectionFileNameList = \
849 [f'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v2/ecids/efficiencySF.ChargeID.{ecids_lh}_ECIDS_Tight_VarRad.root']
850 if config.dataType() is DataType.FastSim:
851 alg.efficiencyCorrectionTool.ForceDataType = (
852 PATCore.ParticleDataType.Full if self.forceFullSimConfig
853 else PATCore.ParticleDataType.Fast)
854 elif config.dataType() is DataType.FullSim:
855 alg.efficiencyCorrectionTool.ForceDataType = \
856 PATCore.ParticleDataType.Full
857 alg.outOfValidity = 2 #silent
858 alg.outOfValidityDeco = 'el_ecids_bad_eff' + selectionPostfix
859 alg.electrons = config.readName (self.containerName)
860 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
861 if self.saveDetailedSF:
862 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
863 'ecids_effSF' + postfix)
864 sfList += [alg.scaleFactorDecoration]
865
866 if self.addChargeMisIDSF and config.dataType() is not DataType.Data and not self.noEffSF and config.geometry() >= LHCPeriod.Run3:
867 warnings.warn_explicit(
868 "Charge mis-ID SFs are only available for Run 2 and will not"
869 " have any effect in Run 3.",
870 Run2OnlyFeatureWarning, filename='', lineno=0)
871
872 elif self.addChargeMisIDSF and config.dataType() is not DataType.Data and not self.noEffSF and config.geometry() < LHCPeriod.Run3:
873 alg = config.createAlgorithm( 'CP::ElectronEfficiencyCorrectionAlg',
874 'ElectronEfficiencyCorrectionAlgMisid' )
875 config.addPrivateTool( 'efficiencyCorrectionTool',
876 'CP::ElectronChargeEfficiencyCorrectionTool' )
877 alg.scaleFactorDecoration = 'el_charge_misid_effSF' + selectionPostfix + '_%SYS%'
878 if self.isolationWP != 'Tight_VarRad':
879 raise ValueError('Charge mis-ID SFs are supported only for Tight_VarRad isolation.')
880 if self.identificationWP == 'LooseBLayerLH':
881 misid_lh = 'LooseAndBLayerLLH'
882 elif self.identificationWP == 'MediumLH':
883 misid_lh = 'MediumLLH'
884 elif self.identificationWP == 'TightLH':
885 misid_lh = 'TightLLH'
886 else:
887 raise ValueError('Charge mis-ID SFs are supported only for ID LooseBLayerLH, MediumLH, or TightLH')
888 misid_suffix = '_ECIDSloose' if self.chargeIDSelectionRun2 else ''
889
890 alg.efficiencyCorrectionTool.CorrectionFileName = \
891 f'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v2/charge_misID/chargeEfficiencySF.{misid_lh}_d0z0_TightVarRad{misid_suffix}.root'
892 if config.dataType() is DataType.FastSim:
893 alg.efficiencyCorrectionTool.ForceDataType = (
894 PATCore.ParticleDataType.Full if self.forceFullSimConfig
895 else PATCore.ParticleDataType.Fast)
896 elif config.dataType() is DataType.FullSim:
897 alg.efficiencyCorrectionTool.ForceDataType = \
898 PATCore.ParticleDataType.Full
899 alg.outOfValidity = 2 #silent
900 alg.outOfValidityDeco = 'el_misid_bad_eff' + selectionPostfix
901 alg.electrons = config.readName (self.containerName)
902 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
903 if self.saveDetailedSF:
904 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
905 'charge_misid_effSF' + postfix)
906 sfList += [alg.scaleFactorDecoration]
907
908 if config.dataType() is not DataType.Data and not self.noEffSF and self.saveCombinedSF:
909 alg = config.createAlgorithm( 'CP::AsgObjectScaleFactorAlg',
910 'ElectronCombinedEfficiencyScaleFactorAlg' )
911 alg.particles = config.readName (self.containerName)
912 alg.inScaleFactors = sfList
913 alg.outScaleFactor = 'effSF' + postfix + '_%SYS%'
914 config.addOutputVar (self.containerName, alg.outScaleFactor, 'effSF' + postfix)
915
917
918 def __init__ (self) :
919 super (ElectronTriggerAnalysisSFBlock, self).__init__ ()
920 self.addDependency('EventSelection', required=False)
921 self.addDependency('EventSelectionMerger', required=False)
922 self.addOption ('triggerChainsPerYear', {}, type=dict,
923 info="a dictionary with key (string) the year and value (list of "
924 "strings) the trigger chains.")
925 self.addOption ('electronID', '', type=str,
926 info="the electron ID WP to use.")
927 self.addOption ('electronIsol', '', type=str,
928 info="the electron isolation WP to use.")
929 self.addOption ('saveEff', False, type=bool,
930 info="define whether we decorate also the trigger scale efficiency.")
931 self.addOption ('prefixSF', 'trigEffSF', type=str,
932 info="the decoration prefix for trigger scale factors.")
933 self.addOption ('prefixEff', 'trigEff', type=str,
934 info="the decoration prefix for MC trigger efficiencies.")
935 self.addOption ('includeAllYearsPerRun', False, type=bool,
936 info="all configured years in the LHC run will "
937 "be included in all jobs.")
938 self.addOption ('removeHLTPrefix', True, type=bool,
939 info="remove the HLT prefix from trigger chain names.")
940 self.addOption ('useToolKeyAsOutput', False, type=bool,
941 info="use the tool trigger key as output.")
942 self.addOption ('containerName', '', type=str,
943 info="the input electron container, with a possible selection, in "
944 "the format `container` or `container.selection`.")
945
946 def instanceName (self) :
947 """Return the instance name for this block"""
948 return self.containerName
949
950 def makeAlgs (self, config) :
951
952 if config.dataType() is not DataType.Data:
953 log = logging.getLogger('ElectronTriggerSFConfig')
954
956 log.warning('`includeAllYearsPerRun` is set to True, but `useToolKeyAsOutput` is set to False. '
957 'This will cause multiple branches to be written out with the same content.')
958
959 # Dictionary from TrigGlobalEfficiencyCorrection/Triggers.cfg
960 # Key is trigger chain (w/o HLT prefix)
961 # Value is empty for single leg trigger or list of legs
962 triggerDict = TriggerDict()
963
964 # currently recommended versions
965 version_Run2 = "2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v3"
966 map_Run2 = f"ElectronEfficiencyCorrection/{version_Run2}/map1.txt"
967 version_Run3 = "2015_2025/rel22.2/2025_Run3_Consolidated_Recommendation_v4"
968 map_Run3 = f"ElectronEfficiencyCorrection/{version_Run3}/map2.txt"
969
970 version = version_Run2 if config.geometry() is LHCPeriod.Run2 else version_Run3
971 # Dictionary from TrigGlobalEfficiencyCorrection/MapKeys.cfg
972 # Key is year_leg
973 # Value is list of configs available, first one will be used
974 mapKeysDict = MapKeysDict(version)
975
976 # helper function for leg filtering, very hardcoded but allows autoconfiguration
977 def filterConfFromMap(conf, electronMapKeys):
978 if not conf:
979 raise ValueError("No configuration found for trigger chain.")
980 if len(conf) == 1:
981 return conf[0]
982
983 for c in conf:
984 if c in electronMapKeys:
985 return c
986
987 return conf[0]
988
990 years = [int(year) for year in self.triggerChainsPerYear.keys()]
991 else:
992 from TriggerAnalysisAlgorithms.TriggerAnalysisSFConfig import (
993 get_input_years)
994 years = get_input_years(config)
995
996 # prepare keys
997 import ROOT
998 triggerChainsPerYear_Run2 = {}
999 triggerChainsPerYear_Run3 = {}
1000 for year, chains in self.triggerChainsPerYear.items():
1001 if not chains:
1002 warnings.warn_explicit(
1003 f"No trigger chains configured for year {year}."
1004 " Assuming this is intended, no Electron trigger SF"
1005 " will be computed.",
1006 TriggerSFWarning, filename='', lineno=0)
1007 continue
1008
1009 chains_split = [chain.replace("HLT_", "").replace(" || ", "_OR_") for chain in chains]
1010 if int(year) >= 2022:
1011 triggerChainsPerYear_Run3[str(year)] = ' || '.join(chains_split)
1012 else:
1013 triggerChainsPerYear_Run2[str(year)] = ' || '.join(chains_split)
1014 electronMapKeys_Run2 = ROOT.std.map("string", "string")()
1015 electronMapKeys_Run3 = ROOT.std.map("string", "string")()
1016
1017 sc_Run2 = ROOT.TrigGlobalEfficiencyCorrectionTool.suggestElectronMapKeys(triggerChainsPerYear_Run2, version_Run2, electronMapKeys_Run2)
1018 sc_Run3 = ROOT.TrigGlobalEfficiencyCorrectionTool.suggestElectronMapKeys(triggerChainsPerYear_Run3, version_Run3, electronMapKeys_Run3)
1019 if sc_Run2.code() != 2 or sc_Run3.code() != 2:
1020 raise RuntimeError("Failed to suggest electron map keys")
1021 electronMapKeys = dict(electronMapKeys_Run2) | dict(electronMapKeys_Run3)
1022
1023 # collect configurations
1024 from TriggerAnalysisAlgorithms.TriggerAnalysisConfig import is_year_in_current_period
1025 triggerConfigs = {}
1026 for year in years:
1027 if not is_year_in_current_period(config, year):
1028 continue
1029
1030 triggerChains = self.triggerChainsPerYear.get(int(year), self.triggerChainsPerYear.get(str(year), []))
1031 for chain in triggerChains:
1032 chain = chain.replace(" || ", "_OR_")
1033 chain_noHLT = chain.replace("HLT_", "")
1034 chain_out = chain_noHLT if self.removeHLTPrefix else chain
1035 legs = triggerDict[chain_noHLT]
1036 if not legs:
1037 if chain_noHLT[0] == 'e' and chain_noHLT[1].isdigit:
1038 chain_key = f"{year}_{chain_noHLT}"
1039 chain_conf = mapKeysDict[chain_key][0]
1040 triggerConfigs[chain_conf if self.useToolKeyAsOutput else chain_out] = chain_conf
1041 else:
1042 for leg in legs:
1043 if leg[0] == 'e' and leg[1].isdigit:
1044 leg_out = leg if self.removeHLTPrefix else f"HLT_{leg}"
1045 leg_key = f"{year}_{leg}"
1046 leg_conf = filterConfFromMap(mapKeysDict[leg_key], electronMapKeys)
1047 triggerConfigs[leg_conf if self.useToolKeyAsOutput else leg_out] = leg_conf
1048
1049 decorations = [self.prefixSF]
1050 if self.saveEff:
1051 decorations += [self.prefixEff]
1052
1053 for label, conf in triggerConfigs.items():
1054 for deco in decorations:
1055 alg = config.createAlgorithm('CP::ElectronEfficiencyCorrectionAlg',
1056 'EleTrigEfficiencyCorrectionsAlg' + deco +
1057 '_' + label)
1058 config.addPrivateTool( 'efficiencyCorrectionTool',
1059 'AsgElectronEfficiencyCorrectionTool' )
1060
1061 # Reproduce config from TrigGlobalEfficiencyAlg
1062 alg.efficiencyCorrectionTool.MapFilePath = map_Run3 if config.geometry() is LHCPeriod.Run3 else map_Run2
1063 alg.efficiencyCorrectionTool.IdKey = self.electronID.replace("LH","")
1064 alg.efficiencyCorrectionTool.IsoKey = self.electronIsol
1065 alg.efficiencyCorrectionTool.TriggerKey = (
1066 ("Eff_" if deco == self.prefixEff else "") + conf)
1067 alg.efficiencyCorrectionTool.CorrelationModel = "TOTAL"
1068 alg.efficiencyCorrectionTool.ForceDataType = \
1069 PATCore.ParticleDataType.Full
1070
1071 alg.scaleFactorDecoration = f"el_{deco}_{label}_%SYS%"
1072
1073 alg.outOfValidity = 2 #silent
1074 alg.outOfValidityDeco = f"bad_eff_ele{deco}_{label}"
1075 alg.electrons = config.readName (self.containerName)
1076 alg.preselection = config.getPreselection (self.containerName, "")
1077 config.addOutputVar (self.containerName, alg.scaleFactorDecoration, f"{deco}_{label}")
1078
1079
1080class ElectronLRTMergedConfig (ConfigBlock) :
1081 def __init__ (self) :
1082 super (ElectronLRTMergedConfig, self).__init__ ()
1083 self.addOption (
1084 'inputElectrons', 'Electrons', type=str,
1085 noneAction='error',
1086 info="the name of the input electron container."
1087 )
1088 self.addOption (
1089 'inputLRTElectrons', 'LRTElectrons', type=str,
1090 noneAction='error',
1091 info="the name of the input LRT electron container."
1092 )
1093 self.addOption (
1094 'containerName', 'Electrons_LRTMerged', type=str,
1095 noneAction='error',
1096 info="the name of the output container after LRT merging."
1097 )
1098
1099 def instanceName (self) :
1100 """Return the instance name for this block"""
1101 return self.containerName
1102
1103 def makeAlgs (self, config) :
1104
1105 if config.isPhyslite() :
1106 raise(RuntimeError("Electron LRT merging is not available in Physlite mode"))
1107
1108 alg = config.createAlgorithm( "CP::ElectronLRTMergingAlg", "ElectronLRTMergingAlg" )
1109 alg.PromptElectronLocation = self.inputElectrons
1110 alg.LRTElectronLocation = self.inputLRTElectrons
1111 alg.OutputCollectionName = self.containerName
1112 alg.CreateViewCollection = False
1113
1114@groupBlocks
1117 seq.append(ElectronIPCalibrationConfig())
1118
1119@groupBlocks
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition hcg.cxx:312
T * get(TKey *tobj)
get a TObject* from a TKey* (why can't a TObject be a TKey?)
Definition hcg.cxx:132
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:179