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