ATLAS Offline Software
TruthCollectionsFixerConfig.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
3 from AnalysisAlgorithmsConfig.ConfigBlock import ConfigBlock
4 from AnalysisAlgorithmsConfig.ConfigAccumulator import DataType
5 import AnaAlgorithm.DualUseConfig as DualUseConfig
6 
7 
8 class TruthCollectionsFixerBlock(ConfigBlock):
9  """This Config Block is meant to help fix older DAOD samples that have the old HepMC barcode instead of uid.
10  See also https://gitlab.cern.ch/atlas/athena/-/merge_requests/82613
11  and https://gitlab.cern.ch/atlas/athena/-/merge_requests/82746
12  """
13 
14  def __init__(self):
15  super(TruthCollectionsFixerBlock, self).__init__()
16  self.addOption(
17  "truthParticleContainersToFix",
18  None,
19  type=list,
20  info="list of input DAOD truthParticle containers to fix",
21  )
22  self.addOption(
23  "truthVertexContainersToFix",
24  None,
25  type=list,
26  info="list of input DAOD truthVertex containers to fix",
27  )
28  self.addOption("fixDAODTruthRecord", True, type=bool,
29  info="older derivations have the old HepMC barcodes and need to be fixed, otherwise we get "
30  "a crash on 'missing ::uid'. Schedules an instance of TruthCollectionsFixerBlock. "
31  "Not needed for recent derivations.")
32 
33  def makeAlgs(self, config):
34 
35  # we need this boolean because the responsibility of scheduling the whole block is passed onto CommonServicesConfig
36  if not self.fixDAODTruthRecord: return
37 
38  if config.dataType() is DataType.Data: return
39 
40  partContainers = None
41  if self.truthParticleContainersToFix is not None:
42  partContainers = self.truthParticleContainersToFix
43  elif config.isPhyslite():
44  partContainers = [
45  "TruthBoson", "TruthBosonsWithDecayParticles",
46  "TruthElectrons", "TruthMuons", "TruthPhotons", "TruthNeutrinos",
47  "TruthTaus",
48  "TruthTop", "TruthBottom",
49  "TruthForwardProtons",
50  "TruthBSM", "TruthBSMWithDecayParticles",
51  "BornLeptons"
52  ]
53  else:
54  partContainers = [
55  "TruthBoson", "TruthBosonsWithDecayParticles",
56  "TruthElectrons", "TruthMuons", "TruthPhotons", "TruthNeutrinos",
57  "TruthTaus", "TruthTausWithDecayParticles",
58  "TruthTop", "TruthBottom", "TruthCharm", "TruthHFWithDecayParticles",
59  "TruthForwardProtons", "TruthPileupParticles",
60  "TruthBSM", "TruthBSMWithDecayParticles",
61  "BornLeptons"
62  ]
63 
64  vertContainers = None
65  if self.truthVertexContainersToFix is not None:
66  vertContainers = self.truthVertexContainersToFix
67  elif config.isPhyslite():
68  vertContainers = [
69  "TruthBosonsWithDecayVertices",
70  "TruthBSMWithDecayVertices"
71  ]
72  else:
73  vertContainers = [
74  "TruthBosonsWithDecayVertices",
75  "TruthHFWithDecayVertices",
76  "TruthTausWithDecayVertices",
77  "TruthBSMWithDecayVertices"
78  ]
79 
80  # in Athena, we have to rename the containers. In AnalysisBase, we can just overwrite in place
81  if DualUseConfig.isAthena:
82  # replicate the behaviour of AddressRemappingCfg
83  ars = config.createService("AddressRemappingSvc", "AddressRemappingSvc")
84  pps = config.createService("ProxyProviderSvc", "ProxyProviderSvc")
85  if "AddressRemappingSvc" not in pps.ProviderNames:
86  pps.ProviderNames += ["AddressRemappingSvc"]
87  for container in partContainers:
88  ars.TypeKeyRenameMaps += [
89  f"xAOD::TruthParticleContainer#{container}->InFile{container}",
90  f"xAOD::AuxContainerBase#{container}Aux.->InFile{container}Aux.",
91  ]
92  for container in vertContainers:
93  ars.TypeKeyRenameMaps += [
94  f"xAOD::TruthVertexContainer#{container}->InFile{container}",
95  f"xAOD::AuxContainerBase#{container}Aux.->InFile{container}Aux.",
96  ]
97 
98  # the actual fix for old DAOD truth schema
99  for container in partContainers:
100  alg = config.createAlgorithm(
101  "xAODMaker::TruthParticleFixerAlg",
102  "TruthParticleFixerAlg_" + container,
103  reentrant=True,
104  )
105  alg.InputContainer = (
106  container if not DualUseConfig.isAthena else f"InFile{container}"
107  )
108  alg.OutputContainer = container
109 
110  # in Athena, we further need to remap relevant ElementLinks for containers that have them
111  containers_without_parent_child_links = ["TruthBosonsWithDecayParticles", "TruthTausWithDecayParticles", "BornLeptons", "TruthPileupParticles", "TruthForwardProtons"]
112  if DualUseConfig.isAthena and container not in containers_without_parent_child_links:
113  alg.LinkPrefixToRemove = "InFile"
114  alg.ParticleLinks = ["parentLinks", "childLinks"]
115 
116  for container in vertContainers:
117  alg = config.createAlgorithm(
118  "xAODMaker::TruthVertexFixerAlg",
119  "TruthVertexFixerAlg_" + container,
120  reentrant=True,
121  )
122  alg.InputContainer = (
123  container if not DualUseConfig.isAthena else f"InFile{container}"
124  )
125  alg.OutputContainer = container
126 
127  if DualUseConfig.isAthena :
128  alg.LinkPrefixToRemove = "InFile"
python.TruthCollectionsFixerConfig.TruthCollectionsFixerBlock
Definition: TruthCollectionsFixerConfig.py:8
python.TruthCollectionsFixerConfig.TruthCollectionsFixerBlock.__init__
def __init__(self)
Definition: TruthCollectionsFixerConfig.py:14
python.TruthCollectionsFixerConfig.TruthCollectionsFixerBlock.makeAlgs
def makeAlgs(self, config)
Definition: TruthCollectionsFixerConfig.py:33