ATLAS Offline Software
Loading...
Searching...
No Matches
EGAM7.py
Go to the documentation of this file.
1# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration # ====================================================================
2# EGAM7.py
3# This defines DAOD_EGAM7, a skimmed DAOD format for Run 3.
4# Keep events passing OR of electron triggers, or inclusive
5# electron selection, to retain fake electron candidates
6# Cell collection is saved but thinned (keep only cells associated with
7# egammaClusters)
8# It requires the flag EGAM7 in Derivation_tf.py
9# ====================================================================
10
11from AthenaConfiguration.ComponentAccumulator import ComponentAccumulator
12from AthenaConfiguration.ComponentFactory import CompFactory
13from AthenaConfiguration.Enums import MetadataCategory
14
15from DerivationFrameworkEGamma.PhotonsCPDetailedContent import (
16 PhotonsCPDetailedContent,
17)
18
19from DerivationFrameworkEGamma.TriggerContent import (
20 BkgElectronTriggers,
21 ExtraContainersTrigger,
22 ExtraContainersElectronTrigger,
23 ExtraContainersTriggerDataOnly,
24)
25
26
27thinCells = True
28
29
31 """Configure the EGAM7 skimming tool"""
32 acc = ComponentAccumulator()
33 skimmingTools = []
34
35 # off-line based selection
36 expression = "count(Electrons.pt > 4.5*GeV) >= 1"
37 print("EGAM7 offline skimming expression: ", expression)
38 from DerivationFrameworkTools.DerivationFrameworkToolsConfig import (
39 xAODStringSkimmingToolCfg)
40 EGAM7_OfflineSkimmingTool = acc.getPrimaryAndMerge(xAODStringSkimmingToolCfg(
41 flags, name = "EGAM7_OfflineSkimmingTool", expression = expression))
42 skimmingTools += [EGAM7_OfflineSkimmingTool]
43
44 # trigger-based selection
45 MenuType = ""
46 if flags.Trigger.EDMVersion == 2:
47 MenuType = "Run2"
48 elif flags.Trigger.EDMVersion == 3:
49 MenuType = "Run3"
50
51 if MenuType:
52 triggers = BkgElectronTriggers[MenuType]
53 print("EGAM7 trigger skimming list (OR): ", triggers)
54
55 EGAM7_TriggerSkimmingTool = CompFactory.DerivationFramework.TriggerSkimmingTool(
56 name="EGAM7_TriggerSkimmingTool", TriggerListOR=triggers
57 )
58
59 acc.addPublicTool(EGAM7_TriggerSkimmingTool)
60 skimmingTools += [EGAM7_TriggerSkimmingTool]
61
62 # do the AND of trigger-based and offline-based selection
63 print("EGAM7 skimming is logical AND of previous selections")
64 EGAM7_SkimmingTool = CompFactory.DerivationFramework.FilterCombinationAND(
65 name="EGAM7_SkimmingTool",
66 FilterList=skimmingTools
67 )
68
69 acc.addPublicTool(EGAM7_SkimmingTool, primary=True)
70
71 return acc
72
73
74def EGAM7KernelCfg(flags, name="EGAM7Kernel", **kwargs):
75 """Configure the derivation framework driving algorithm (kernel)
76 for EGAM7"""
77 acc = ComponentAccumulator()
78
79 # Schedule extra jets collections
80 from JetRecConfig.StandardSmallRJets import AntiKt4PV0Track
81 from JetRecConfig.JetRecConfig import JetRecCfg
82
83 jetList = [AntiKt4PV0Track]
84 for jd in jetList:
85 acc.merge(JetRecCfg(flags, jd))
86
87 # Common augmentations
88 from DerivationFrameworkPhys.PhysCommonConfig import PhysCommonAugmentationsCfg
89
90 acc.merge(
91 PhysCommonAugmentationsCfg(
92 flags, TriggerListsHelper=kwargs["TriggerListsHelper"]
93 )
94 )
95
96 # EGAM7 augmentations
97 augmentationTools = []
98
99 # ====================================================================
100 # Common calo decoration tools
101 # ====================================================================
102 from DerivationFrameworkCalo.DerivationFrameworkCaloConfig import (
103 CaloDecoratorKernelCfg)
104 acc.merge(CaloDecoratorKernelCfg(flags))
105
106 # thinning tools
107 thinningTools = []
108
109 streamName = kwargs["StreamName"]
110
111 # Track thinning
112 if flags.Derivation.Egamma.doTrackThinning:
113 from DerivationFrameworkInDet.InDetToolsConfig import (
114 TrackParticleThinningCfg,
115 MuonTrackParticleThinningCfg,
116 TauTrackParticleThinningCfg,
117 )
118
119 TrackThinningKeepElectronTracks = True
120 TrackThinningKeepPhotonTracks = True
121 TrackThinningKeepAllElectronTracks = False
122 TrackThinningKeepJetTracks = False
123 TrackThinningKeepMuonTracks = False
124 TrackThinningKeepTauTracks = False
125 TrackThinningKeepPVTracks = False
126
127 # Tracks associated with Electrons
128 if TrackThinningKeepElectronTracks:
129 EGAM7ElectronTPThinningTool = (
130 CompFactory.DerivationFramework.EgammaTrackParticleThinning(
131 name="EGAM7ElectronTPThinningTool",
132 StreamName=streamName,
133 SGKey="Electrons",
134 GSFTrackParticlesKey="GSFTrackParticles",
135 InDetTrackParticlesKey="InDetTrackParticles",
136 SelectionString="Electrons.pt > 0*GeV",
137 BestMatchOnly=True,
138 ConeSize=0.3,
139 )
140 )
141 acc.addPublicTool(EGAM7ElectronTPThinningTool)
142 thinningTools.append(EGAM7ElectronTPThinningTool)
143
144 # Tracks associated with Electrons (all tracks, large cone, for track
145 # isolation studies of the selected electrons)
146 if TrackThinningKeepAllElectronTracks:
147 EGAM7ElectronTPThinningTool2 = (
148 CompFactory.DerivationFramework.EgammaTrackParticleThinning(
149 name="EGAM7ElectronTPThinningTool2",
150 StreamName=streamName,
151 SGKey="Electrons",
152 GSFTrackParticlesKey="GSFTrackParticles",
153 InDetTrackParticlesKey="InDetTrackParticles",
154 SelectionString="Electrons.pt > 4*GeV",
155 BestMatchOnly=False,
156 ConeSize=0.6,
157 )
158 )
159 acc.addPublicTool(EGAM7ElectronTPThinningTool2)
160 thinningTools.append(EGAM7ElectronTPThinningTool2)
161
162 # Tracks associated with Photons
163 if TrackThinningKeepPhotonTracks:
164 EGAM7PhotonTPThinningTool = (
165 CompFactory.DerivationFramework.EgammaTrackParticleThinning(
166 name="EGAM7PhotonTPThinningTool",
167 StreamName=streamName,
168 SGKey="Photons",
169 GSFTrackParticlesKey="GSFTrackParticles",
170 InDetTrackParticlesKey="InDetTrackParticles",
171 GSFConversionVerticesKey="GSFConversionVertices",
172 SelectionString="Photons.pt > 0*GeV",
173 BestMatchOnly=True,
174 ConeSize=0.3,
175 )
176 )
177 acc.addPublicTool(EGAM7PhotonTPThinningTool)
178 thinningTools.append(EGAM7PhotonTPThinningTool)
179
180 # Tracks associated with Jets
181 if TrackThinningKeepJetTracks:
182 EGAM7JetTPThinningTool = (
183 CompFactory.DerivationFramework.JetTrackParticleThinning(
184 name="EGAM7JetTPThinningTool",
185 StreamName=streamName,
186 JetKey="AntiKt4EMPFlowJets",
187 InDetTrackParticlesKey="InDetTrackParticles",
188 )
189 )
190 acc.addPublicTool(EGAM7JetTPThinningTool)
191 thinningTools.append(EGAM7JetTPThinningTool)
192
193 # Tracks associated with Muons
194 if TrackThinningKeepMuonTracks:
195 EGAM7MuonTPThinningTool = acc.getPrimaryAndMerge(
196 MuonTrackParticleThinningCfg(
197 flags,
198 name="EGAM7MuonTPThinningTool",
199 StreamName=streamName,
200 MuonKey="Muons",
201 InDetTrackParticlesKey="InDetTrackParticles",
202 )
203 )
204 thinningTools.append(EGAM7MuonTPThinningTool)
205
206 # Tracks associated with Taus
207 if TrackThinningKeepTauTracks:
208 EGAM7TauTPThinningTool = acc.getPrimaryAndMerge(
209 TauTrackParticleThinningCfg(
210 flags,
211 name="EGAM7TauTPThinningTool",
212 StreamName=streamName,
213 TauKey="TauJets",
214 ConeSize=0.6,
215 InDetTrackParticlesKey="InDetTrackParticles",
216 DoTauTracksThinning=True,
217 TauTracksKey="TauTracks",
218 )
219 )
220 thinningTools.append(EGAM7TauTPThinningTool)
221
222 # Tracks from primary vertex
223 thinning_expression = " && ".join(
224 [
225 "(InDetTrackParticles.DFCommonTightPrimary)",
226 "(abs(DFCommonInDetTrackZ0AtPV)*sin(InDetTrackParticles.theta)<3*mm)",
227 "(InDetTrackParticles.pt>10*GeV)",
228 ]
229 )
230 if TrackThinningKeepPVTracks:
231 EGAM7TPThinningTool = acc.getPrimaryAndMerge(
232 TrackParticleThinningCfg(
233 flags,
234 name="EGAM7TPThinningTool",
235 StreamName=streamName,
236 SelectionString=thinning_expression,
237 InDetTrackParticlesKey="InDetTrackParticles",
238 )
239 )
240 thinningTools.append(EGAM7TPThinningTool)
241
242 # truth thinning
243 if flags.Input.isMC:
244 # W, Z and Higgs
245 truth_cond_WZH = " && ".join(
246 ["(abs(TruthParticles.pdgId) >= 23)", "(abs(TruthParticles.pdgId) <= 25)"]
247 )
248 # Leptons
249 truth_cond_lep = " && ".join(
250 ["(abs(TruthParticles.pdgId) >= 11)", "(abs(TruthParticles.pdgId) <= 16)"]
251 )
252 # Top quark
253 truth_cond_top = "(abs(TruthParticles.pdgId) == 6)"
254 # Photon
255 truth_cond_gam = " && ".join(
256 ["(abs(TruthParticles.pdgId) == 22)", "(TruthParticles.pt > 1*GeV)"]
257 )
258 # stable particles
259 truth_cond_finalState = "(TruthParticles.isGenStable)"
260 truth_expression = (
261 "( "
262 + truth_cond_WZH
263 + " ) || "
264 + "( "
265 + truth_cond_lep
266 + " ) || "
267 + "( "
268 + truth_cond_top
269 + " ) || "
270 + "( "
271 + truth_cond_gam
272 + " ) || "
273 + "( "
274 + truth_cond_finalState
275 + " )"
276 )
277 print("EGAM7 truth thinning expression: ", truth_expression)
278
279 EGAM7TruthThinningTool = CompFactory.DerivationFramework.GenericTruthThinning(
280 name="EGAM7TruthThinningTool",
281 StreamName=streamName,
282 ParticleSelectionString=truth_expression,
283 PreserveDescendants=False,
284 PreserveGeneratorDescendants=True,
285 PreserveAncestors=True,
286 )
287 acc.addPublicTool(EGAM7TruthThinningTool)
288 thinningTools.append(EGAM7TruthThinningTool)
289
290 # Keep only calo cells associated with the egammaClusters collection
291 if thinCells:
292 from DerivationFrameworkCalo.CaloCellDFGetterConfig import thinCaloCellsForDFCfg
293
294 acc.merge(
295 thinCaloCellsForDFCfg(
296 flags,
297 inputClusterKeys=["egammaClusters"],
298 streamName="StreamDAOD_EGAM7",
299 inputCellKey="AllCalo",
300 outputCellKey="DFEGAMCellContainer",
301 )
302 )
303
304 # skimming
305 skimmingTool = acc.getPrimaryAndMerge(EGAM7SkimmingToolCfg(flags))
306
307 # setup the kernel
308 acc.addEventAlgo(
309 CompFactory.DerivationFramework.DerivationKernel(
310 name,
311 SkimmingTools=[skimmingTool],
312 AugmentationTools=augmentationTools,
313 ThinningTools=thinningTools,
314 )
315 )
316
317 return acc
318
319
320def EGAM7Cfg(flags):
321 acc = ComponentAccumulator()
322
323 # Get the lists of triggers needed for trigger matching.
324 # This is needed at this scope (for the slimming) and further down
325 # in the config chain for actually configuring the matching, so we create
326 # it here and pass it down
327 # TODO: this should ideally be called higher up to avoid it being run
328 # multiple times in a train.
329 # DODO: restrict it to relevant triggers
330 from DerivationFrameworkPhys.TriggerListsHelper import TriggerListsHelper
331
332 EGAM7TriggerListsHelper = TriggerListsHelper(flags)
333
334 # configure skimming/thinning/augmentation tools
335 acc.merge(
337 flags,
338 name="EGAM7Kernel",
339 StreamName="StreamDAOD_EGAM7",
340 TriggerListsHelper=EGAM7TriggerListsHelper,
341 )
342 )
343
344 # configure slimming
345 from OutputStreamAthenaPool.OutputStreamConfig import OutputStreamCfg
346 from xAODMetaDataCnv.InfileMetaDataConfig import SetupMetaDataForStreamCfg
347 from DerivationFrameworkCore.SlimmingHelper import SlimmingHelper
348
349 EGAM7SlimmingHelper = SlimmingHelper(
350 "EGAM7SlimmingHelper",
351 NamesAndTypes=flags.Input.TypedCollections,
352 flags=flags,
353 )
354
355 # ------------------------------------------
356 # containers for which we save all variables
357 # -------------------------------------------
358
359 # baseline
360 EGAM7SlimmingHelper.AllVariables = [
361 "Electrons",
362 "GSFTrackParticles",
363 "egammaClusters",
364 ]
365
366 # for trigger studies we also add:
367 MenuType = ""
368 if flags.Trigger.EDMVersion == 2:
369 MenuType = "Run2"
370 elif flags.Trigger.EDMVersion == 3:
371 MenuType = "Run3"
372
373 if MenuType:
374 EGAM7SlimmingHelper.AllVariables += ExtraContainersTrigger[MenuType]
375 EGAM7SlimmingHelper.AllVariables += ExtraContainersElectronTrigger[MenuType]
376 if not flags.Input.isMC:
377 EGAM7SlimmingHelper.AllVariables += ExtraContainersTriggerDataOnly[MenuType]
378
379 # and on MC we also add:
380 if flags.Input.isMC:
381 EGAM7SlimmingHelper.AllVariables += [
382 "TruthEvents",
383 "TruthParticles",
384 "TruthVertices",
385 "egammaTruthParticles",
386 ]
387
388 # -------------------------------------------
389 # containers that we slim
390 # -------------------------------------------
391
392 # first add variables from smart-slimming
393 # adding only also those for which we add all variables since
394 # the XXXCPContent.py files also bring in some extra variables
395 # for other collections
396 # muons, tau, MET, b-tagging could be switched off if not needed
397 # and use too much space
398 EGAM7SlimmingHelper.SmartCollections = [
399 "Electrons",
400 "Photons",
401 "Muons",
402 "TauJets",
403 "InDetTrackParticles",
404 "PrimaryVertices",
405 "AntiKt4EMPFlowJets",
406 "MET_Baseline_AntiKt4EMPFlow",
407
408 ]
409 if flags.Input.isMC:
410 EGAM7SlimmingHelper.SmartCollections += [
411 "AntiKt4TruthJets",
412 "AntiKt4TruthDressedWZJets",
413 ]
414
415 # then add extra variables:
416
417 # muons
418 EGAM7SlimmingHelper.ExtraVariables += [
419 "Muons.ptcone20.ptcone30.ptcone40.etcone20.etcone30.etcone40"
420 ]
421
422 # conversion vertices
423 EGAM7SlimmingHelper.ExtraVariables += [
424 "GSFConversionVertices.x.y.z.px.py.pz.pt1.pt2.etaAtCalo.phiAtCalo",
425 "GSFConversionVertices.trackParticleLinks",
426 ]
427
428 # primary vertices
429 EGAM7SlimmingHelper.ExtraVariables += ["PrimaryVertices.x.y.sumPt2"]
430
431 # track jets
432 EGAM7SlimmingHelper.ExtraVariables += [
433 "AntiKt4PV0TrackJets.pt.eta.phi.e.m.btaggingLink.constituentLinks"
434 ]
435
436 # photons: detailed shower shape variables
437 EGAM7SlimmingHelper.ExtraVariables += PhotonsCPDetailedContent
438
439 # photons: gain and cluster energy per layer
440 from DerivationFrameworkCalo.DerivationFrameworkCaloConfig import (
441 getGainDecorations,
442 getClusterEnergyPerLayerDecorations,
443 )
444
445 gainDecorations = getGainDecorations(acc, flags, "EGAM7Kernel")
446 print("EGAM7 gain decorations: ", gainDecorations)
447 EGAM7SlimmingHelper.ExtraVariables.extend(gainDecorations)
448 clusterEnergyDecorations = getClusterEnergyPerLayerDecorations(acc, "EGAM7Kernel")
449 print("EGAM7 cluster energy decorations: ", clusterEnergyDecorations)
450 EGAM7SlimmingHelper.ExtraVariables.extend(clusterEnergyDecorations)
451
452 # energy density
453 EGAM7SlimmingHelper.ExtraVariables += [
454 "TopoClusterIsoCentralEventShape.Density",
455 "TopoClusterIsoForwardEventShape.Density",
456 "NeutralParticleFlowIsoCentralEventShape.Density",
457 "NeutralParticleFlowIsoForwardEventShape.Density",
458 ]
459
460 # truth
461 if flags.Input.isMC:
462 EGAM7SlimmingHelper.ExtraVariables += [
463 "MuonTruthParticles.e.px.py.pz.status.pdgId.truthOrigin.truthType"
464 ]
465
466 EGAM7SlimmingHelper.ExtraVariables += [
467 "Photons.truthOrigin.truthType.truthParticleLink"
468 ]
469
470 # Add event info
471 if flags.Derivation.Egamma.doEventInfoSlimming:
472 EGAM7SlimmingHelper.SmartCollections.append("EventInfo")
473 else:
474 EGAM7SlimmingHelper.AllVariables += ["EventInfo"]
475
476 # Add egamma trigger objects
477 EGAM7SlimmingHelper.IncludeEGammaTriggerContent = True
478
479 # Trigger matching
480 # Run 2
481 if flags.Trigger.EDMVersion == 2:
482 from DerivationFrameworkPhys.TriggerMatchingCommonConfig import AddRun2TriggerMatchingToSlimmingHelper
483 AddRun2TriggerMatchingToSlimmingHelper(SlimmingHelper = EGAM7SlimmingHelper,
484 OutputContainerPrefix = "TrigMatch_",
485 TriggerList = EGAM7TriggerListsHelper.Run2TriggerNamesNoTau)
486 # Run 3, or Run 2 with navigation conversion
487 if flags.Trigger.EDMVersion == 3 or (flags.Trigger.EDMVersion == 2 and flags.Trigger.doEDMVersionConversion):
488 from TrigNavSlimmingMT.TrigNavSlimmingMTConfig import AddRun3TrigNavSlimmingCollectionsToSlimmingHelper
489 AddRun3TrigNavSlimmingCollectionsToSlimmingHelper(EGAM7SlimmingHelper)
490
491 # Add CellContainer and cluster->cell links
492 if thinCells:
493 EGAM7SlimmingHelper.StaticContent = [
494 "CaloCellContainer#DFEGAMCellContainer",
495 "CaloClusterCellLinkContainer#egammaClusters_links",
496 ]
497 else:
498 EGAM7SlimmingHelper.StaticContent = [
499 "CaloCellContainer#AllCalo",
500 "CaloClusterCellLinkContainer#egammaClusters_links",
501 ]
502
503 # PLIT variables
504 if flags.Derivation.Egamma.addPLITInputs:
505 from LeptonTaggers.LeptonTaggersConfig import DecorateImprovedPromptLeptonAlgsCfg, GetExtraImprovedPromptVariablesForDxAOD
506 acc.merge(DecorateImprovedPromptLeptonAlgsCfg(flags))
507 EGAM7SlimmingHelper.ExtraVariables += [ item for item in GetExtraImprovedPromptVariablesForDxAOD() if item.startswith("Electrons")]
508 if flags.Derivation.Egamma.addPLITOutputs:
509 from LeptonTaggers.LeptonTaggersConfig import DecoratePLITAlgsCfg, GetExtraPLITVariablesForDxAOD
510 acc.merge(DecoratePLITAlgsCfg(flags))
511 EGAM7SlimmingHelper.ExtraVariables += [ item for item in GetExtraPLITVariablesForDxAOD() if item.startswith("Electrons")]
512
513 EGAM7ItemList = EGAM7SlimmingHelper.GetItemList()
514 acc.merge(
515 OutputStreamCfg(
516 flags,
517 "DAOD_EGAM7",
518 ItemList=EGAM7ItemList,
519 AcceptAlgs=["EGAM7Kernel"],
520 )
521 )
522 acc.merge(
523 SetupMetaDataForStreamCfg(
524 flags,
525 "DAOD_EGAM7",
526 AcceptAlgs=["EGAM7Kernel"],
527 createMetadata=[
528 MetadataCategory.CutFlowMetaData,
529 MetadataCategory.TruthMetaData,
530 ],
531 )
532 )
533
534 return acc
void print(char *figname, TCanvas *c1)
EGAM7KernelCfg(flags, name="EGAM7Kernel", **kwargs)
Definition EGAM7.py:74
EGAM7Cfg(flags)
Definition EGAM7.py:320
EGAM7SkimmingToolCfg(flags)
Definition EGAM7.py:30