4 from AnalysisAlgorithmsConfig.ConfigBlock
import ConfigBlock
6 from AthenaConfiguration.Enums
import LHCPeriod
7 from AnalysisAlgorithmsConfig.ConfigAccumulator
import DataType
8 from TrigGlobalEfficiencyCorrection.TriggerLeg_DictHelpers
import TriggerDict, MapKeysDict
9 from AthenaCommon.Logging
import logging
12 from xAODEgamma.xAODEgammaParameters
import xAOD
18 """the ConfigBlock for the electron four-momentum correction"""
21 super (ElectronCalibrationConfig, self).__init__ ()
22 self.setBlockName(
'Electrons')
23 self.addOption (
'inputContainer',
'', type=str,
24 info=
"select electron input container, by default set to Electrons")
25 self.addOption (
'containerName',
'', type=str,
27 info=
"the name of the output container after calibration.")
28 self.addOption (
'ESModel',
'', type=str,
29 info=
"flag of egamma calibration recommendation.")
30 self.addOption (
'decorrelationModel',
'1NP_v1', type=str,
31 info=
"egamma energy scale decorrelationModel. The default is 1NP_v1. "
32 "Supported Model: 1NP_v1, FULL_v1.")
33 self.addOption (
'postfix',
'', type=str,
34 info=
"a postfix to apply to decorations and algorithm names. Typically "
35 "not needed here since the calibration is common to all electrons.")
36 self.addOption (
'crackVeto',
False, type=bool,
37 info=
"whether to perform LAr crack veto based on the cluster eta, "
38 "i.e. remove electrons within 1.37<|eta|<1.52. The default "
40 self.addOption (
'isolationCorrection',
True, type=bool,
41 info=
"whether or not to perform isolation corrections (leakage "
42 "corrections), i.e. set up an instance of "
43 "CP::EgammaIsolationCorrectionAlg.")
44 self.addOption (
'recalibratePhyslite',
True, type=bool,
45 info=
"whether to run the CP::EgammaCalibrationAndSmearingAlg on "
46 "PHYSLITE derivations. The default is True.")
47 self.addOption (
'minPt', 4.5*GeV, type=float,
48 info=
"the minimum pT cut to apply to calibrated electrons. "
49 "The default is 4.5 GeV.")
50 self.addOption (
'maxEta', 2.47, type=float,
51 info=
"maximum electron |eta| (float). The default is 2.47.")
52 self.addOption (
'forceFullSimConfigForP4',
False, type=bool,
53 info=
"whether to force the tool to use the configuration meant for "
54 "full simulation samples for P4 corrections. Only for testing purposes. "
55 "The default is False.")
56 self.addOption (
'forceFullSimConfigForIso',
False, type=bool,
57 info=
"whether to force the tool to use the configuration meant for "
58 "full simulation samples for isolation corrections. Only for testing purposes. "
59 "The default is False.")
60 self.addOption (
'splitCalibrationAndSmearing',
False, type=bool,
61 info=
"EXPERIMENTAL: This splits the EgammaCalibrationAndSmearingTool "
62 " into two steps. The first step applies a baseline calibration that "
63 "is not affected by systematics. The second step then applies the "
64 "systematics dependent corrections. The net effect is that the "
65 "slower first step only has to be run once, while the second is run "
66 "once per systematic. ATLASG-2358")
68 self.addOption (
'decorateTruth',
False, type=bool,
69 info=
"decorate truth particle information on the reconstructed one")
70 self.addOption (
'decorateCaloClusterEta',
False, type=bool,
71 info=
"decorate the calo cluster eta on the reconstructed one")
72 self.addOption (
'writeTrackD0Z0',
False, type = bool,
73 info=
"save the d0 significance and z0sinTheta variables so they can be written out")
74 self.addOption (
'decorateEmva',
False, type=bool,
75 info=
"decorate E_mva_only on the objects (needed for columnar tools/PHYSLITE)")
77 self.addOption (
'decorateSamplingPattern',
False, type=bool,
78 info=
"add samplingPattern decorations to clusters as part of PHYSLITE")
81 """Return the instance name for this block"""
82 return self.containerName + self.postfix
85 """Create the calibration and smearing algorithm
87 Factoring this out into its own function, as we want to
88 instantiate it in multiple places"""
89 log = logging.getLogger(
'ElectronCalibrationConfig')
92 alg = config.createAlgorithm(
'CP::EgammaCalibrationAndSmearingAlg', name )
93 config.addPrivateTool(
'calibrationAndSmearingTool',
94 'CP::EgammaCalibrationAndSmearingTool' )
97 alg.calibrationAndSmearingTool.ESModel = self.ESModel
99 if config.geometry()
is LHCPeriod.Run2:
100 alg.calibrationAndSmearingTool.ESModel =
'es2023_R22_Run2_v1'
101 elif config.geometry()
is LHCPeriod.Run3:
102 alg.calibrationAndSmearingTool.ESModel =
'es2024_Run3_v0'
103 elif config.geometry()
is LHCPeriod.Run4:
104 log.warning(
"No ESModel set for Run4, using Run 3 model instead")
105 alg.calibrationAndSmearingTool.ESModel =
'es2024_Run3_v0'
107 raise ValueError (f
"Can't set up the ElectronCalibrationConfig with {config.geometry().value}, "
108 "there must be something wrong!")
110 alg.calibrationAndSmearingTool.decorrelationModel = self.decorrelationModel
111 alg.calibrationAndSmearingTool.useFastSim = (
112 0
if self.forceFullSimConfigForP4
113 else int( config.dataType()
is DataType.FastSim ))
114 alg.calibrationAndSmearingTool.decorateEmva = self.decorateEmva
115 alg.egammas = config.readName (self.containerName)
116 alg.egammasOut = config.copyName (self.containerName)
117 alg.preselection = config.getPreselection (self.containerName,
'')
123 log = logging.getLogger(
'ElectronCalibrationConfig')
125 if self.forceFullSimConfigForP4:
126 log.warning(
"You are running ElectronCalibrationConfig forcing full sim config")
127 log.warning(
" This is only intended to be used for testing purposes")
129 inputContainer =
"AnalysisElectrons" if config.isPhyslite()
else "Electrons"
130 if self.inputContainer:
131 inputContainer = self.inputContainer
132 config.setSourceName (self.containerName, inputContainer)
135 if self.decorateCaloClusterEta:
136 alg = config.createAlgorithm(
'CP::EgammaCaloClusterEtaAlg',
137 'ElectronEgammaCaloClusterEtaAlg',
139 alg.particles = config.readName(self.containerName)
140 config.addOutputVar (self.containerName,
'caloEta2',
'caloEta2', noSys=
True)
142 if self.decorateSamplingPattern:
143 config.createAlgorithm(
'CP::EgammaSamplingPatternDecoratorAlg',
'EgammaSamplingPatternDecoratorAlg' )
146 if config.wantCopy (self.containerName) :
147 alg = config.createAlgorithm(
'CP::AsgShallowCopyAlg',
'ElectronShallowCopyAlg' )
148 alg.input = config.readName (self.containerName)
149 alg.output = config.copyName (self.containerName)
152 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronEtaCutAlg' )
153 alg.selectionDecoration =
'selectEta' + self.postfix +
',as_bits'
154 config.addPrivateTool(
'selectionTool',
'CP::AsgPtEtaSelectionTool' )
155 alg.selectionTool.maxEta = self.maxEta
157 alg.selectionTool.etaGapLow = 1.37
158 alg.selectionTool.etaGapHigh = 1.52
159 alg.selectionTool.useClusterEta =
True
160 alg.particles = config.readName (self.containerName)
161 alg.preselection = config.getPreselection (self.containerName,
'')
162 config.addSelection (self.containerName,
'', alg.selectionDecoration)
165 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronObjectQualityAlg' )
166 alg.selectionDecoration =
'goodOQ' + self.postfix +
',as_bits'
167 config.addPrivateTool(
'selectionTool',
'CP::EgammaIsGoodOQSelectionTool' )
168 alg.selectionTool.Mask = xAOD.EgammaParameters.BADCLUSELECTRON
169 alg.particles = config.readName (self.containerName)
170 alg.preselection = config.getPreselection (self.containerName,
'')
171 config.addSelection (self.containerName,
'', alg.selectionDecoration)
173 if not self.splitCalibrationAndSmearing :
176 if config.isPhyslite()
and not self.recalibratePhyslite :
177 alg.skipNominal =
True
198 alg.noToolSystematics =
True
200 alg.calibrationAndSmearingTool.doSmearing =
False
205 alg.calibrationAndSmearingTool.doScaleCorrection =
False
206 alg.calibrationAndSmearingTool.useMVACalibration =
False
207 alg.calibrationAndSmearingTool.decorateEmva =
False
211 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronPtCutAlg' )
212 alg.selectionDecoration =
'selectPt' + self.postfix +
',as_bits'
213 config.addPrivateTool(
'selectionTool',
'CP::AsgPtEtaSelectionTool' )
214 alg.selectionTool.minPt = self.minPt
215 alg.particles = config.readName (self.containerName)
216 alg.preselection = config.getPreselection (self.containerName,
'')
217 config.addSelection (self.containerName,
'', alg.selectionDecoration,
221 if self.isolationCorrection:
222 alg = config.createAlgorithm(
'CP::EgammaIsolationCorrectionAlg',
223 'ElectronIsolationCorrectionAlg' )
224 config.addPrivateTool(
'isolationCorrectionTool',
225 'CP::IsolationCorrectionTool' )
226 alg.isolationCorrectionTool.IsMC = config.dataType()
is not DataType.Data
227 alg.isolationCorrectionTool.AFII_corr = (
228 0
if self.forceFullSimConfigForIso
229 else config.dataType()
is DataType.FastSim)
230 alg.isolationCorrectionTool.ToolVer =
"REL22"
231 alg.isolationCorrectionTool.CorrFile =
"IsolationCorrections/v6/isolation_ptcorrections_rel22_mc20.root"
232 alg.egammas = config.readName (self.containerName)
233 alg.egammasOut = config.copyName (self.containerName)
234 alg.preselection = config.getPreselection (self.containerName,
'')
237 if self.writeTrackD0Z0:
238 alg = config.createAlgorithm(
'CP::AsgLeptonTrackDecorationAlg',
239 'LeptonTrackDecorator',
241 alg.particles = config.readName (self.containerName)
243 alg = config.createAlgorithm(
'CP::AsgEnergyDecoratorAlg',
'EnergyDecorator' )
244 alg.particles = config.readName(self.containerName)
246 config.addOutputVar (self.containerName,
'pt',
'pt')
247 config.addOutputVar (self.containerName,
'eta',
'eta', noSys=
True)
248 config.addOutputVar (self.containerName,
'phi',
'phi', noSys=
True)
249 config.addOutputVar (self.containerName,
'e_%SYS%',
'e')
250 config.addOutputVar (self.containerName,
'charge',
'charge', noSys=
True)
252 if self.writeTrackD0Z0:
253 config.addOutputVar (self.containerName,
'd0_%SYS%',
'd0', noSys=
True)
254 config.addOutputVar (self.containerName,
'd0sig_%SYS%',
'd0sig', noSys=
True)
255 config.addOutputVar (self.containerName,
'z0sintheta_%SYS%',
'z0sintheta', noSys=
True)
256 config.addOutputVar (self.containerName,
'z0sinthetasig_%SYS%',
'z0sinthetasig', noSys=
True)
259 if self.decorateTruth
and config.dataType()
is not DataType.Data:
260 config.addOutputVar (self.containerName,
"truthType",
"truth_type", noSys=
True)
261 config.addOutputVar (self.containerName,
"truthOrigin",
"truth_origin", noSys=
True)
263 config.addOutputVar (self.containerName,
"firstEgMotherPdgId",
"truth_firstEgMotherPdgId", noSys=
True)
264 config.addOutputVar (self.containerName,
"firstEgMotherTruthOrigin",
"truth_firstEgMotherTruthOrigin", noSys=
True)
265 config.addOutputVar (self.containerName,
"firstEgMotherTruthType",
"truth_firstEgMotherTruthType", noSys=
True)
269 """the ConfigBlock for the electron working point
271 This may at some point be split into multiple blocks (29 Aug 22)."""
274 super (ElectronWorkingPointConfig, self).__init__ ()
275 self.addOption (
'containerName',
'', type=str,
277 info=
"the name of the input container.")
278 self.addOption (
'selectionName',
'', type=str,
280 info=
"the name of the electron selection to define (e.g. tight or "
282 self.addOption (
'postfix',
None, type=str,
283 info=
"a postfix to apply to decorations and algorithm names. "
284 "Typically not needed here as selectionName is used internally.")
285 self.addOption (
'trackSelection',
True, type=bool,
286 info=
"whether or not to set up an instance of "
287 "CP::AsgLeptonTrackSelectionAlg, with the recommended d_0 and "
288 "z_0 sin(theta) cuts. The default is True.")
289 self.addOption (
'maxD0Significance', 5, type=float,
290 info=
"maximum d0 significance used for the trackSelection"
292 self.addOption (
'maxDeltaZ0SinTheta', 0.5, type=float,
293 info=
"maximum z0sinTheta in mm used for the trackSelection"
294 "The default is 0.5 mm")
295 self.addOption (
'identificationWP',
None, type=str,
296 info=
"the ID WP (string) to use. Supported ID WPs: TightLH, "
297 "MediumLH, LooseBLayerLH, TightDNN, MediumDNN, LooseDNN, "
298 "TightNoCFDNN, MediumNoCFDNN, VeryLooseNoCF97DNN, NoID.")
299 self.addOption (
'isolationWP',
None, type=str,
300 info=
"the isolation WP (string) to use. Supported isolation WPs: "
301 "HighPtCaloOnly, Loose_VarRad, Tight_VarRad, TightTrackOnly_"
302 "VarRad, TightTrackOnly_FixedRad, NonIso.")
303 self.addOption (
'convSelection',
None, type=str,
304 info=
"enter additional selection (string) to use. To be used with "
305 "TightLH or will crash. Supported keywords:"
306 "Veto, MatConv, GammaStar.")
307 self.addOption (
'addSelectionToPreselection',
True, type=bool,
308 info=
"whether to retain only electrons satisfying the working point "
309 "requirements. The default is True.")
310 self.addOption (
'closeByCorrection',
False, type=bool,
311 info=
"whether to use close-by-corrected isolation working points")
312 self.addOption (
'recomputeID',
False, type=bool,
313 info=
"whether to rerun the ID LH/DNN. The default is False, i.e. to use "
315 self.addOption (
'chargeIDSelectionRun2',
False, type=bool,
316 info=
"whether to run the ECIDS tool. Only available for run 2. "
317 "The default is False.")
318 self.addOption (
'recomputeChargeID',
False, type=bool,
319 info=
"whether to rerun the ECIDS. The default is False, i.e. to use "
321 self.addOption (
'doFSRSelection',
False, type=bool,
322 info=
"whether to accept additional electrons close to muons for "
323 "the purpose of FSR corrections to these muons. Expert feature "
324 "requested by the H4l analysis running on PHYSLITE. "
325 "The default is False.")
326 self.addOption (
'noEffSF',
False, type=bool,
327 info=
"disables the calculation of efficiencies and scale factors. "
328 "Experimental! only useful to test a new WP for which scale "
329 "factors are not available. The default is False.")
330 self.addOption (
'saveDetailedSF',
True, type=bool,
331 info=
"save all the independent detailed object scale factors. "
332 "The default is True.")
333 self.addOption (
'saveCombinedSF',
False, type=bool,
334 info=
"save the combined object scale factor. "
335 "The default is False.")
336 self.addOption (
'forceFullSimConfig',
False, type=bool,
337 info=
"whether to force the tool to use the configuration meant for "
338 "full simulation samples. Only for testing purposes. "
339 "The default is False.")
340 self.addOption (
'correlationModelId',
'SIMPLIFIED', type=str,
341 info=
"the correlation model (string) to use for ID scale factors "
342 "Supported models: SIMPLIFIED (default), FULL, TOTAL, TOYS")
343 self.addOption (
'correlationModelIso',
'SIMPLIFIED', type=str,
344 info=
"the correlation model (string) to use for isolation scale factors "
345 "Supported models: SIMPLIFIED (default), FULL, TOTAL, TOYS")
346 self.addOption (
'correlationModelReco',
'SIMPLIFIED', type=str,
347 info=
"the correlation model (string) to use for reconstruction scale factors "
348 "Supported models: SIMPLIFIED (default), FULL, TOTAL, TOYS")
349 self.addOption(
'addChargeMisIDSF',
False, type=bool,
350 info=
"Adds scale factors for charge-misID.")
353 """Return the instance name for this block"""
354 if self.postfix
is not None :
355 return self.containerName +
'_' + self.selectionName + self.postfix
356 return self.containerName +
'_' + self.selectionName
360 log = logging.getLogger(
'ElectronWorkingPointConfig')
362 if self.forceFullSimConfig:
363 log.warning(
"You are running ElectronWorkingPointConfig forcing full sim config")
364 log.warning(
"This is only intended to be used for testing purposes")
366 selectionPostfix = self.selectionName
367 if selectionPostfix !=
'' and selectionPostfix[0] !=
'_' :
368 selectionPostfix =
'_' + selectionPostfix
371 if config.geometry()
is LHCPeriod.Run1:
372 raise ValueError (
"Can't set up the ElectronWorkingPointConfig with %s, there must be something wrong!" % config.geometry().value)
374 postfix = self.postfix
376 postfix = self.selectionName
377 if postfix !=
'' and postfix[0] !=
'_' :
378 postfix =
'_' + postfix
381 if self.trackSelection :
382 alg = config.createAlgorithm(
'CP::AsgLeptonTrackSelectionAlg',
383 'ElectronTrackSelectionAlg',
385 alg.selectionDecoration =
'trackSelection' + postfix +
',as_bits'
386 alg.maxD0Significance = self.maxD0Significance
387 alg.maxDeltaZ0SinTheta = self.maxDeltaZ0SinTheta
388 alg.particles = config.readName (self.containerName)
389 alg.preselection = config.getPreselection (self.containerName,
'')
390 if self.trackSelection :
391 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
392 preselection=self.addSelectionToPreselection)
397 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronLikelihoodAlg' )
398 alg.selectionDecoration =
'selectLikelihood' + selectionPostfix +
',as_char'
401 config.addPrivateTool(
'selectionTool',
'AsgElectronLikelihoodTool' )
402 alg.selectionTool.primaryVertexContainer =
'PrimaryVertices'
405 if config.geometry() >= LHCPeriod.Run3:
410 elif config.geometry()
is LHCPeriod.Run2:
414 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
416 dfFlag = dfFlag.replace(
"BLayer",
"BL")
417 alg.selectionTool.selectionFlags = [dfFlag]
420 algVeto = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronLikelihoodAlgVeto')
421 algVeto.selectionDecoration =
'selectLikelihoodVeto' + postfix +
',as_char'
422 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
423 algVeto.selectionTool.selectionFlags = [
"DFCommonElectronsLHLoose"]
424 algVeto.selectionTool.invertFlags = [
True]
425 algVeto.particles = config.readName (self.containerName)
426 algVeto.preselection = config.getPreselection (self.containerName, self.selectionName)
428 config.addSelection (self.containerName, self.selectionName, algVeto.selectionDecoration,
429 preselection=self.addSelectionToPreselection)
432 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronLikelihoodAlg' )
433 alg.selectionDecoration =
'selectSiHit' + selectionPostfix +
',as_char'
435 config.addPrivateTool(
'selectionTool',
'CP::AsgMaskSelectionTool' )
436 dfVar =
"DFCommonElectronsLHLooseBLIsEMValue"
437 alg.selectionTool.selectionVars = [dfVar]
438 mask =
int( 0 | 0x1 << 1 | 0x1 << 2)
439 alg.selectionTool.selectionMasks = [mask]
441 if self.chargeIDSelectionRun2:
442 raise ValueError(
'DNN is not intended to be used with '
443 '`chargeIDSelectionRun2` option as there are '
444 'DNN WPs containing charge flip rejection.')
446 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronDNNAlg' )
447 alg.selectionDecoration =
'selectDNN' + selectionPostfix +
',as_char'
450 config.addPrivateTool(
'selectionTool',
'AsgElectronSelectorTool' )
452 if config.geometry()
is LHCPeriod.Run3:
453 raise ValueError (
"DNN working points are not available for Run 3 yet.")
458 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
460 alg.selectionTool.selectionFlags = [dfFlag]
464 raise ValueError (f
"Electron ID working point '{self.identificationWP}' is not recognised!")
467 alg.particles = config.readName (self.containerName)
468 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
469 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
470 preselection=self.addSelectionToPreselection)
475 algDec = config.createAlgorithm(
'CP::ElectronSiHitDecAlg',
'ElectronSiHitDecAlg' )
476 selDec =
'siHitEvtHasLeptonPair' + selectionPostfix +
',as_char'
477 algDec.selectionName = selDec.split(
",")[0]
478 algDec.ElectronContainer = config.readName (self.containerName)
480 algDec.RequireTwoLeptons =
True
481 config.addSelection (self.containerName, self.selectionName, selDec,
482 preselection=self.addSelectionToPreselection)
488 raise ValueError(f
"convSelection can only be used with TightLH ID, "
489 f
"whereas {self.identificationWP} has been selected. convSelection option will be ignored.")
491 allowedValues = [
"Veto",
"GammaStar",
"MatConv"]
493 raise ValueError(f
"convSelection has been set to {self.convSelection}, which is not a valid option. "
494 f
"convSelection option must be one of {allowedValues}.")
497 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronAmbiguityTypeAlg' )
498 alg.selectionDecoration =
'selectAmbiguityType' + selectionPostfix +
',as_char'
499 config.addPrivateTool(
'selectionTool',
'CP::AsgNumDecorationSelectionToolUInt8' )
500 alg.selectionTool.decorationName =
"ambiguityType"
501 alg.selectionTool.doEqual =
True
502 alg.selectionTool.equal = 0
503 alg.particles = config.readName (self.containerName)
504 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
505 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
506 preselection=self.addSelectionToPreselection)
509 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
'ElectronDFCommonAddAmbiguityAlg' )
510 alg.selectionDecoration =
'selectDFCommonAddAmbiguity' + selectionPostfix +
',as_char'
511 config.addPrivateTool(
'selectionTool',
'CP::AsgNumDecorationSelectionToolInt' )
512 alg.selectionTool.decorationName =
"DFCommonAddAmbiguity"
514 alg.selectionTool.doMax =
True
515 alg.selectionTool.max = 1
517 alg.selectionTool.doEqual =
True
518 alg.selectionTool.equal = 1
520 alg.selectionTool.doEqual =
True
521 alg.selectionTool.equal = 2
522 alg.particles = config.readName (self.containerName)
523 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
524 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
525 preselection=self.addSelectionToPreselection)
528 if self.doFSRSelection :
530 wpFlag = alg.selectionDecoration.split(
",")[0]
531 alg = config.createAlgorithm(
'CP::EgammaFSRForMuonsCollectorAlg',
'EgammaFSRForMuonsCollectorAlg' )
532 alg.selectionDecoration = wpFlag
533 alg.ElectronOrPhotonContKey = config.readName (self.containerName)
541 if self.isolationWP !=
'NonIso' :
542 alg = config.createAlgorithm(
'CP::EgammaIsolationSelectionAlg',
543 'ElectronIsolationSelectionAlg' )
544 alg.selectionDecoration =
'isolated' + selectionPostfix +
',as_char'
545 config.addPrivateTool(
'selectionTool',
'CP::IsolationSelectionTool' )
546 alg.selectionTool.ElectronWP = self.isolationWP
547 if self.closeByCorrection:
548 alg.selectionTool.IsoDecSuffix =
"CloseByCorr"
549 alg.egammas = config.readName (self.containerName)
550 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
551 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
552 preselection=self.addSelectionToPreselection)
554 if self.chargeIDSelectionRun2
and config.geometry() >= LHCPeriod.Run3:
555 log.warning(
"ECIDS is only available for Run 2 and will not have any effect in Run 3.")
558 if self.chargeIDSelectionRun2
and config.geometry() < LHCPeriod.Run3:
559 alg = config.createAlgorithm(
'CP::AsgSelectionAlg',
560 'ElectronChargeIDSelectionAlg' )
561 alg.selectionDecoration =
'chargeID' + selectionPostfix +
',as_char'
562 if self.recomputeChargeID:
564 config.addPrivateTool(
'selectionTool',
565 'AsgElectronChargeIDSelectorTool' )
566 alg.selectionTool.TrainingFile = \
567 'ElectronPhotonSelectorTools/ChargeID/ECIDS_20180731rel21Summer2018.root'
568 alg.selectionTool.WorkingPoint =
'Loose'
569 alg.selectionTool.CutOnBDT = -0.337671
572 config.addPrivateTool(
'selectionTool',
'CP::AsgFlagSelectionTool' )
573 alg.selectionTool.selectionFlags = [
"DFCommonElectronsECIDS"]
575 alg.particles = config.readName (self.containerName)
576 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
577 config.addSelection (self.containerName, self.selectionName, alg.selectionDecoration,
578 preselection=self.addSelectionToPreselection)
580 correlationModels = [
"SIMPLIFIED",
"FULL",
"TOTAL",
"TOYS"]
581 map_file =
'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v3/map0.txt' \
582 if config.geometry()
is LHCPeriod.Run2
else \
583 'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run3_Consolidated_Prerecom_v3/map1.txt'
586 if config.dataType()
is not DataType.Data
and not self.noEffSF:
588 raise ValueError(
'DNN does not yet have efficiency correction, '
589 'please disable it by setting `noEffSF` to True.')
591 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
592 'ElectronEfficiencyCorrectionAlgReco' )
593 config.addPrivateTool(
'efficiencyCorrectionTool',
594 'AsgElectronEfficiencyCorrectionTool' )
595 alg.scaleFactorDecoration =
'el_reco_effSF' + selectionPostfix +
'_%SYS%'
596 alg.efficiencyCorrectionTool.MapFilePath = map_file
597 alg.efficiencyCorrectionTool.RecoKey =
"Reconstruction"
598 if self.correlationModelReco
not in correlationModels:
599 raise ValueError(
'Invalid correlation model for reconstruction efficiency, '
600 f
'has to be one of: {", ".join(correlationModels)}')
601 if config.geometry() >= LHCPeriod.Run3
and self.correlationModelReco !=
"TOTAL":
602 log.warning(
"Only TOTAL correlation model is currently supported "
603 "for reconstruction efficiency correction in Run 3.")
604 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
606 alg.efficiencyCorrectionTool.CorrelationModel = self.correlationModelReco
607 if config.dataType()
is DataType.FastSim:
608 alg.efficiencyCorrectionTool.ForceDataType = (
609 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
610 else PATCore.ParticleDataType.Fast)
611 elif config.dataType()
is DataType.FullSim:
612 alg.efficiencyCorrectionTool.ForceDataType = \
613 PATCore.ParticleDataType.Full
614 alg.outOfValidity = 2
615 alg.outOfValidityDeco =
'el_reco_bad_eff' + selectionPostfix
616 alg.electrons = config.readName (self.containerName)
617 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
618 if self.saveDetailedSF:
619 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
620 'reco_effSF' + postfix)
621 sfList += [alg.scaleFactorDecoration]
624 if config.dataType()
is not DataType.Data
and not self.noEffSF
and self.
identificationWP !=
'NoID':
626 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
627 'ElectronEfficiencyCorrectionAlgID' )
628 config.addPrivateTool(
'efficiencyCorrectionTool',
629 'AsgElectronEfficiencyCorrectionTool' )
630 alg.scaleFactorDecoration =
'el_id_effSF' + selectionPostfix +
'_%SYS%'
631 alg.efficiencyCorrectionTool.MapFilePath = map_file
633 if self.correlationModelId
not in correlationModels:
634 raise ValueError(
'Invalid correlation model for identification efficiency, '
635 f
'has to be one of: {", ".join(correlationModels)}')
636 alg.efficiencyCorrectionTool.CorrelationModel = self.correlationModelId
637 if config.dataType()
is DataType.FastSim:
638 alg.efficiencyCorrectionTool.ForceDataType = (
639 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
640 else PATCore.ParticleDataType.Fast)
641 elif config.dataType()
is DataType.FullSim:
642 alg.efficiencyCorrectionTool.ForceDataType = \
643 PATCore.ParticleDataType.Full
644 alg.outOfValidity = 2
645 alg.outOfValidityDeco =
'el_id_bad_eff' + selectionPostfix
646 alg.electrons = config.readName (self.containerName)
647 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
648 if self.saveDetailedSF:
649 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
650 'id_effSF' + postfix)
651 sfList += [alg.scaleFactorDecoration]
654 if config.dataType()
is not DataType.Data
and self.isolationWP !=
'NonIso' and not self.noEffSF:
655 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
656 'ElectronEfficiencyCorrectionAlgIsol' )
657 config.addPrivateTool(
'efficiencyCorrectionTool',
658 'AsgElectronEfficiencyCorrectionTool' )
659 alg.scaleFactorDecoration =
'el_isol_effSF' + selectionPostfix +
'_%SYS%'
660 alg.efficiencyCorrectionTool.MapFilePath = map_file
662 alg.efficiencyCorrectionTool.IsoKey = self.isolationWP
663 if self.correlationModelIso
not in correlationModels:
664 raise ValueError(
'Invalid correlation model for isolation efficiency, '
665 f
'has to be one of: {", ".join(correlationModels)}')
666 if self.correlationModelIso !=
'TOTAL':
667 log.warning(
"Only TOTAL correlation model is currently supported "
668 "for isolation efficiency correction in Run 3.")
669 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
670 if config.dataType()
is DataType.FastSim:
671 alg.efficiencyCorrectionTool.ForceDataType = (
672 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
673 else PATCore.ParticleDataType.Fast)
674 elif config.dataType()
is DataType.FullSim:
675 alg.efficiencyCorrectionTool.ForceDataType = \
676 PATCore.ParticleDataType.Full
677 alg.outOfValidity = 2
678 alg.outOfValidityDeco =
'el_isol_bad_eff' + selectionPostfix
679 alg.electrons = config.readName (self.containerName)
680 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
681 if self.saveDetailedSF:
682 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
683 'isol_effSF' + postfix)
684 sfList += [alg.scaleFactorDecoration]
686 if (self.chargeIDSelectionRun2
and config.geometry() < LHCPeriod.Run3
and
687 config.dataType()
is not DataType.Data
and not self.noEffSF):
688 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
689 'ElectronEfficiencyCorrectionAlgEcids' )
690 config.addPrivateTool(
'efficiencyCorrectionTool',
691 'AsgElectronEfficiencyCorrectionTool' )
692 alg.scaleFactorDecoration =
'el_ecids_effSF' + selectionPostfix +
'_%SYS%'
693 if self.isolationWP !=
'Tight_VarRad':
694 raise ValueError(
'ECIDS SFs are supported only for Tight_VarRad isolation.')
702 raise ValueError(
'ECIDS SFs are supported only for ID LooseBLayerLH, MediumLH, or TightLH')
704 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
705 alg.efficiencyCorrectionTool.CorrectionFileNameList = \
706 [f
'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v2/ecids/efficiencySF.ChargeID.{ecids_lh}_ECIDS_Tight_VarRad.root']
707 if config.dataType()
is DataType.FastSim:
708 alg.efficiencyCorrectionTool.ForceDataType = (
709 PATCore.ParticleDataType.Full
if self.forceFullSimConfig
710 else PATCore.ParticleDataType.Fast)
711 elif config.dataType()
is DataType.FullSim:
712 alg.efficiencyCorrectionTool.ForceDataType = \
713 PATCore.ParticleDataType.Full
714 alg.outOfValidity = 2
715 alg.outOfValidityDeco =
'el_ecids_bad_eff' + selectionPostfix
716 alg.electrons = config.readName (self.containerName)
717 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
718 if self.saveDetailedSF:
719 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
720 'ecids_effSF' + postfix)
721 sfList += [alg.scaleFactorDecoration]
723 if self.addChargeMisIDSF
and config.dataType()
is not DataType.Data
and not self.noEffSF:
724 if config.geometry() >= LHCPeriod.Run3:
725 raise ValueError(
'Run 3 does not yet have charge mis-ID correction, '
726 'please disable it by setting `noEffSF` to False.')
728 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
729 'ElectronEfficiencyCorrectionAlgMisid' )
730 config.addPrivateTool(
'efficiencyCorrectionTool',
731 'CP::ElectronChargeEfficiencyCorrectionTool' )
732 alg.scaleFactorDecoration =
'el_charge_misid_effSF' + selectionPostfix +
'_%SYS%'
733 if self.isolationWP !=
'Tight_VarRad':
734 raise ValueError(
'Charge mis-ID SFs are supported only for Tight_VarRad isolation.')
736 misid_lh =
'LooseAndBLayerLLH'
738 misid_lh =
'MediumLLH'
740 misid_lh =
'TightLLH'
742 raise ValueError(
'Charge mis-ID SFs are supported only for ID LooseBLayerLH, MediumLH, or TightLH')
743 misid_suffix =
'_ECIDSloose' if self.chargeIDSelectionRun2
else ''
745 alg.efficiencyCorrectionTool.CorrectionFileName = \
746 f
'ElectronEfficiencyCorrection/2015_2025/rel22.2/2025_Run2Rel22_Recommendation_v2/charge_misID/chargeEfficiencySF.{misid_lh}_d0z0_TightVarRad{misid_suffix}.root'
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
755 alg.outOfValidityDeco =
'el_misid_bad_eff' + selectionPostfix
756 alg.electrons = config.readName (self.containerName)
757 alg.preselection = config.getPreselection (self.containerName, self.selectionName)
758 if self.saveDetailedSF:
759 config.addOutputVar (self.containerName, alg.scaleFactorDecoration,
760 'charge_misid_effSF' + postfix)
761 sfList += [alg.scaleFactorDecoration]
763 if config.dataType()
is not DataType.Data
and not self.noEffSF
and self.saveCombinedSF:
764 alg = config.createAlgorithm(
'CP::AsgObjectScaleFactorAlg',
765 'ElectronCombinedEfficiencyScaleFactorAlg' )
766 alg.particles = config.readName (self.containerName)
767 alg.inScaleFactors = sfList
768 alg.outScaleFactor =
'effSF' + postfix +
'_%SYS%'
769 config.addOutputVar (self.containerName, alg.outScaleFactor,
'effSF' + postfix)
776 super (ElectronTriggerAnalysisSFBlock, self).__init__ ()
778 self.addOption (
'triggerChainsPerYear', {}, type=
None,
779 info=
"a dictionary with key (string) the year and value (list of "
780 "strings) the trigger chains. The default is {} (empty dictionary).")
781 self.addOption (
'electronID',
'', type=str,
782 info=
"the electron ID WP (string) to use.")
783 self.addOption (
'electronIsol',
'', type=str,
784 info=
"the electron isolation WP (string) to use.")
785 self.addOption (
'saveEff',
False, type=bool,
786 info=
"define whether we decorate also the trigger scale efficiency "
787 "The default is false.")
788 self.addOption (
'prefixSF',
'trigEffSF', type=str,
789 info=
"the decoration prefix for trigger scale factors, "
790 "the default is 'trigEffSF'")
791 self.addOption (
'prefixEff',
'trigEff', type=str,
792 info=
"the decoration prefix for MC trigger efficiencies, "
793 "the default is 'trigEff'")
794 self.addOption (
'includeAllYearsPerRun',
False, type=bool,
795 info=
"if True, all configured years in the LHC run will "
796 "be included in all jobs. The default is False.")
797 self.addOption (
'removeHLTPrefix',
True, type=bool,
798 info=
"remove the HLT prefix from trigger chain names, "
799 "The default is True.")
800 self.addOption (
'useToolKeyAsOutput',
False, type=bool,
801 info=
"use tool trigger key as output, "
802 "The default is False.")
803 self.addOption (
'containerName',
'', type=str,
804 info=
"the input electron container, with a possible selection, in "
805 "the format container or container.selection.")
808 """Return the instance name for this block"""
809 return self.containerName
813 if config.dataType()
is not DataType.Data:
814 log = logging.getLogger(
'ElectronTriggerSFConfig')
816 if self.includeAllYearsPerRun
and not self.useToolKeyAsOutput:
817 log.warning(
'`includeAllYearsPerRun` is set to True, but `useToolKeyAsOutput` is set to False. '
818 'This will cause multiple branches to be written out with the same content.')
826 version_Run2 =
"2015_2018/rel21.2/Precision_Summer2020_v1"
827 map_Run2 = f
"{version_Run2}/map4.txt"
828 version_Run3 =
"2015_2025/rel22.2/2025_Run3_Consolidated_Recommendation_v4"
829 map_Run3 =
"2015_2025/rel22.2/2025_Run3_Consolidated_Recommendation_v4/map2.txt"
831 version = version_Run2
if config.geometry()
is LHCPeriod.Run2
else version_Run3
838 def filterConfFromMap(conf, electronMapKeys):
840 raise ValueError(
"No configuration found for trigger chain.")
845 if c
in electronMapKeys:
850 if self.includeAllYearsPerRun:
851 years = [
int(year)
for year
in self.triggerChainsPerYear.
keys()]
853 from TriggerAnalysisAlgorithms.TriggerAnalysisSFConfig
import (
859 triggerChainsPerYear_Run2 = {}
860 triggerChainsPerYear_Run3 = {}
861 for year, chains
in self.triggerChainsPerYear.
items():
863 log.warning(
"No trigger chains configured for year %s. "
864 "Assuming this is intended, no Electron trigger SF will be computed.", year)
867 chains_split = [chain.replace(
"HLT_",
"").
replace(
" || ",
"_OR_")
for chain
in chains]
868 if int(year) >= 2022:
869 triggerChainsPerYear_Run3[
str(year)] =
' || '.
join(chains_split)
871 triggerChainsPerYear_Run2[
str(year)] =
' || '.
join(chains_split)
872 electronMapKeys_Run2 = ROOT.std.map(
"string",
"string")()
873 electronMapKeys_Run3 = ROOT.std.map(
"string",
"string")()
875 sc_Run2 = ROOT.TrigGlobalEfficiencyCorrectionTool.suggestElectronMapKeys(triggerChainsPerYear_Run2, version_Run2, electronMapKeys_Run2)
876 sc_Run3 = ROOT.TrigGlobalEfficiencyCorrectionTool.suggestElectronMapKeys(triggerChainsPerYear_Run3, version_Run3, electronMapKeys_Run3)
877 if sc_Run2.code() != 2
or sc_Run3.code() != 2:
878 raise RuntimeError(
"Failed to suggest electron map keys")
879 electronMapKeys = dict(electronMapKeys_Run2) | dict(electronMapKeys_Run3)
882 from TriggerAnalysisAlgorithms.TriggerAnalysisConfig
import is_year_in_current_period
888 triggerChains = self.triggerChainsPerYear.
get(
int(year), self.triggerChainsPerYear.
get(
str(year), []))
889 for chain
in triggerChains:
890 chain = chain.replace(
" || ",
"_OR_")
891 chain_noHLT = chain.replace(
"HLT_",
"")
892 chain_out = chain_noHLT
if self.removeHLTPrefix
else chain
893 legs = triggerDict[chain_noHLT]
895 if chain_noHLT[0] ==
'e' and chain_noHLT[1].isdigit:
896 chain_key = f
"{year}_{chain_noHLT}"
897 chain_conf = mapKeysDict[chain_key][0]
898 triggerConfigs[chain_conf
if self.useToolKeyAsOutput
else chain_out] = chain_conf
901 if leg[0] ==
'e' and leg[1].isdigit:
902 leg_out = leg
if self.removeHLTPrefix
else f
"HLT_{leg}"
903 leg_key = f
"{year}_{leg}"
904 leg_conf = filterConfFromMap(mapKeysDict[leg_key], electronMapKeys)
905 triggerConfigs[leg_conf
if self.useToolKeyAsOutput
else leg_out] = leg_conf
907 decorations = [self.prefixSF]
909 decorations += [self.prefixEff]
911 for label, conf
in triggerConfigs.items():
912 for deco
in decorations:
913 alg = config.createAlgorithm(
'CP::ElectronEfficiencyCorrectionAlg',
914 'EleTrigEfficiencyCorrectionsAlg' + deco +
916 config.addPrivateTool(
'efficiencyCorrectionTool',
917 'AsgElectronEfficiencyCorrectionTool' )
920 alg.efficiencyCorrectionTool.MapFilePath =
"ElectronEfficiencyCorrection/" + (map_Run3
if config.geometry()
is LHCPeriod.Run3
else map_Run2)
921 alg.efficiencyCorrectionTool.IdKey = self.electronID.
replace(
"LH",
"")
922 alg.efficiencyCorrectionTool.IsoKey = self.electronIsol
923 alg.efficiencyCorrectionTool.TriggerKey = (
924 (
"Eff_" if deco == self.prefixEff
else "") + conf)
925 alg.efficiencyCorrectionTool.CorrelationModel =
"TOTAL"
926 alg.efficiencyCorrectionTool.ForceDataType = \
927 PATCore.ParticleDataType.Full
929 alg.scaleFactorDecoration = f
"el_{deco}_{label}_%SYS%"
931 alg.outOfValidity = 2
932 alg.outOfValidityDeco = f
"bad_eff_ele{deco}_{label}"
933 alg.electrons = config.readName (self.containerName)
934 alg.preselection = config.getPreselection (self.containerName,
"")
935 config.addOutputVar (self.containerName, alg.scaleFactorDecoration, f
"{deco}_{label}")
940 super (ElectronLRTMergedConfig, self).__init__ ()
942 'inputElectrons',
'Electrons', type=str,
944 info=
"the name of the input electron container."
947 'inputLRTElectrons',
'LRTElectrons', type=str,
949 info=
"the name of the input LRT electron container."
952 'containerName',
'Electrons_LRTMerged', type=str,
954 info=
"the name of the output container after LRT merging."
958 """Return the instance name for this block"""
959 return self.containerName
963 if config.isPhyslite() :
964 raise(RuntimeError(
"Electron LRT merging is not available in Physlite mode"))
966 alg = config.createAlgorithm(
"CP::ElectronLRTMergingAlg",
"ElectronLRTMergingAlg" )
967 alg.PromptElectronLocation = self.inputElectrons
968 alg.LRTElectronLocation = self.inputLRTElectrons
969 alg.OutputCollectionName = self.containerName
970 alg.CreateViewCollection =
False