ATLAS Offline Software
TruthBornLeptonCollectionMaker.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TruthBornLeptonCollectionMaker.cxx
7 // Makes a special collection of Born leptons
8 
9 // R/W/D handles
10 #include "StoreGate/ReadHandle.h"
11 #include "StoreGate/WriteHandle.h"
14 // My own header file
16 // EDM includes for the particles we need
19 // To look up which generator is being used
20 #include "StoreGate/StoreGateSvc.h"
23 // STL includes
24 #include <string>
26 
27 // Constructor
29  const std::string& n,
30  const IInterface* p)
31  : base_class(t,n,p)
32  , m_metaStore( "MetaDataStore", n )
33 {
34  declareProperty( "MetaDataStore", m_metaStore );
35 }
36 
37 // Destructor
39 }
40 
41 // Athena initialize and finalize
43 {
44  ATH_MSG_VERBOSE("initialize() ...");
45 
46  // Input truth particles
47  ATH_CHECK( m_particlesKey.initialize() );
48  ATH_MSG_INFO("Using " << m_particlesKey.key() << " as the input truth container key");
49 
50  // ReadDecorHandleKeys
51  ATH_CHECK(m_originAccessorKey.initialize());
52  ATH_CHECK(m_typeAccessorKey.initialize());
53  ATH_CHECK(m_outcomeAccessorKey.initialize());
54  ATH_CHECK(m_classificationAccessorKey.initialize());
55 
56  // Output truth particles
57  if (m_collectionName.empty()) {
58  ATH_MSG_FATAL("No key provided for the new truth particle collection");
59  return StatusCode::FAILURE;
60  } else {ATH_MSG_INFO("New truth particle collection key: " << m_collectionName.key() );}
61  ATH_CHECK( m_collectionName.initialize());
62 
63  // Decoration keys - FIXME we should not need to use WriteDecorHandleKeys here.
64  ATH_CHECK(m_originDecoratorKey.initialize());
65  ATH_CHECK(m_typeDecoratorKey.initialize());
66  ATH_CHECK(m_outcomeDecoratorKey.initialize());
67  ATH_CHECK(m_classificationDecoratorKey.initialize());
68 
69  // TODO: needs to be made MT-friendly
70  ATH_CHECK( m_metaStore.retrieve() );
71 
72  return StatusCode::SUCCESS;
73 }
74 
75 // Selection and collection creation
77 {
78  // Event context
79  const EventContext& ctx = Gaudi::Hive::currentContext();
80 
81  // Set up for some metadata handling
82  static const bool is_sherpa = [this]() {
83  bool is_sherpa = false;
84  // TODO: needs to be made MT-friendly
85  if (m_metaStore->contains<xAOD::TruthMetaDataContainer>("TruthMetaData")){
86  // Note that I'd like to get this out of metadata in general, but it seems that the
87  // metadata isn't fully available in initialize, and since this is a const function
88  // I can only do the retrieve every event, rather than lazy-initializing, since this
89  // metadata ought not change during a run
90  const xAOD::TruthMetaDataContainer* truthMetaData(nullptr);
91  // Shamelessly stolen from the file meta data tool
92 
93  if (m_metaStore->retrieve(truthMetaData).isSuccess() && !truthMetaData->empty()){
94  // Let's just be super sure...
95  const std::string gens = truthMetaData->at(0)->generators();
96  is_sherpa = (gens.find("sherpa")==std::string::npos &&
97  gens.find("Sherpa")==std::string::npos &&
98  gens.find("SHERPA")==std::string::npos) ? false : true;
99  } // Seems to be the only sure way...
100  else {
101  ATH_MSG_WARNING("Found xAODTruthMetaDataContainer empty! Configuring to be NOT Sherpa.");
102  }
103  ATH_MSG_INFO("From metadata configured: Sherpa? " << is_sherpa);
104  }
105  else {
106  ATH_MSG_WARNING("Could not find metadata container in storegate; assuming NOT Sherpa");
107  }
108  return is_sherpa;
109  }();
110 
111  // Retrieve truth collections
112  SG::ReadHandle<xAOD::TruthParticleContainer> truthParticles(m_particlesKey,ctx);
113  if (!truthParticles.isValid()) {
114  ATH_MSG_ERROR("Couldn't retrieve TruthParticle collection with name " << m_particlesKey);
115  return StatusCode::FAILURE;
116  }
117 
118  // Create the new particle containers and WriteHandles
119  SG::WriteHandle<xAOD::TruthParticleContainer> newParticlesWriteHandle(m_collectionName, ctx);
120  ATH_CHECK(newParticlesWriteHandle.record(std::make_unique<xAOD::TruthParticleContainer>(),
121  std::make_unique<xAOD::TruthParticleAuxContainer>()));
122  ATH_MSG_DEBUG( "Recorded new TruthParticleContainer with key: " << (m_collectionName.key()));
123 
124  // Set up decorators
125  SG::WriteDecorHandle<xAOD::TruthParticleContainer, unsigned int > originDecorator(m_originDecoratorKey, ctx);
126  SG::WriteDecorHandle<xAOD::TruthParticleContainer, unsigned int > typeDecorator(m_typeDecoratorKey, ctx);
127  SG::WriteDecorHandle<xAOD::TruthParticleContainer, unsigned int > outcomeDecorator(m_outcomeDecoratorKey, ctx);
128  SG::WriteDecorHandle<xAOD::TruthParticleContainer, unsigned int > classificationDecorator(m_classificationDecoratorKey, ctx);
129 
130  SG::ReadDecorHandle<xAOD::TruthParticleContainer, unsigned int > originAccessor(m_originAccessorKey, ctx);
131  SG::ReadDecorHandle<xAOD::TruthParticleContainer, unsigned int > typeAccessor(m_typeAccessorKey, ctx);
132  SG::ReadDecorHandle<xAOD::TruthParticleContainer, unsigned int > outcomeAccessor(m_outcomeAccessorKey, ctx);
133  SG::ReadDecorHandle<xAOD::TruthParticleContainer, unsigned int > classificationAccessor(m_classificationAccessorKey, ctx);
134 
135  // add relevant particles to new collection
136  for (unsigned int i=0; i<truthParticles->size(); ++i) {
137  // Grab the particle
138  const xAOD::TruthParticle* theParticle = (*truthParticles)[i];
139  if (!theParticle) continue; // Protection against null pointers
140  if (!theParticle->isLepton()) continue; // Only include leptons!
141 
142  if (is_sherpa) {
143  // For Sherpa, skip is not status 11
144  if (MC::isPhysical(theParticle)) continue;
145  // Sherpa may have two sets of status == 11 leptons. Here we take the first set.
146  // To do so, we check the first status == 11 leptons,
147  // check that it has a child a parent barecode corresponding the the lepton's uniqueID
148  // If so, then save this parent uniqueID and skip all status 11 leptons with this parent uniqueID.
149  bool has_parent_of_same_flavour = false;
150  if (theParticle->prodVtx())
151  for (size_t p = 0; p < theParticle->prodVtx()->nIncomingParticles(); ++p){
152  if (theParticle->parent(p)->pdg_id() == theParticle->pdg_id()) has_parent_of_same_flavour = true;
153  }
154  if (has_parent_of_same_flavour) continue;
155  } else {
156  // Some generators, look for leptons coming from vertices with other leptons
157  bool physical = false, has_V = false;
158  if (!MC::isPhysical(theParticle) && theParticle->hasProdVtx()){
159  const xAOD::TruthVertex * prod = theParticle->prodVtx();
160  // Look for other leptons in the production vertex and check if those are physical
161  for (size_t p = 0; p < prod->nOutgoingParticles(); ++p){
162  if (prod->outgoingParticle(p) && prod->outgoingParticle(p)->isLepton() && MC::isPhysical(prod->outgoingParticle(p))){
163  physical = true;
164  break;
165  }
166  }
167  // See if there was a boson going *into* the vertex
168  for (size_t p = 0; p < prod->nIncomingParticles(); ++p){
169  if (prod->incomingParticle(p) && (prod->incomingParticle(p)->isZ() || prod->incomingParticle(p)->isW() || prod->incomingParticle(p)->isHiggs()) ){
170  has_V = true; // Found a vector boson
171  break;
172  }
173  }
174  }
175 
176  // Now we have all the information for the special case of V->l(born) l(bare) l(born) l(bare)
177  // AV: The comment above, most likely is related to the case when the event is passed to PHOTOS with an option to keep
178  // the "documentation" particles, i.e. particles before the photon emission.
179  // These particles in PHOTOS are attached to the same vertex, but have different status, e.g. 3.
180  // The momentum conservation, is, of course, violated.
181  if ( !(physical && has_V && !MC::isPhysical(theParticle)) && !MC::Pythia8::isConditionB(theParticle)){
182  // Skip if it has no boson parent, or has no descendent that is a bare lepton
183  if (!hasBareDescendent( theParticle ) ) continue;
184  const xAOD::TruthVertex * prod = theParticle->prodVtx();
185  if (!prod) continue;
186  if (prod->nIncomingParticles() < 1) continue;
187  size_t bad_parent = 0;
188  for (size_t p = 0; p < prod->nIncomingParticles(); ++p){
189  if (!theParticle->parent(p)) { bad_parent++; continue;}
190  if (!theParticle->parent(p)->isZ() && !theParticle->parent(p)->isW() && !theParticle->parent(p)->isHiggs()) bad_parent++;
191  }
192  if (bad_parent != 0) continue;
193 
194  }
195  } // End of treatment for generators that are not Sherpa
196 
197  // Add this particle to the new collection
198  xAOD::TruthParticle* xTruthParticle = new xAOD::TruthParticle();
199  newParticlesWriteHandle->push_back( xTruthParticle );
200  // Fill with numerical content
201  *xTruthParticle=*theParticle;
202  // Copy over the decorations if they are available
203  typeDecorator(*xTruthParticle) = typeAccessor.withDefault(*theParticle, 0);
204  originDecorator(*xTruthParticle) = originAccessor.withDefault(*theParticle, 0);
205  outcomeDecorator(*xTruthParticle) = outcomeAccessor.withDefault(*theParticle, 0);
206  classificationDecorator(*xTruthParticle) = classificationAccessor.withDefault(*theParticle, 0);
207  } // Loop over all particles
208 
209  return StatusCode::SUCCESS;
210 }
211 
212 // Find out if a particle has a bare descendent
214 {
215  // Null pointer check
216  if (!p) return false;
217  // If we hit a bare descendent, then we're a winnner
218  if (p->isLepton() && MC::isStable(p) ) return true;
219  // Otherwise look through all the children
220  for (size_t c=0;c<p->nChildren();++c){
221  if (!p->child(c)) continue; // Null pointer protection
222  if (p->pdgId()!=p->child(c)->pdgId()) continue; // Different particle child
223  if (hasBareDescendent( p->child(c) )) return true;
224  }
225  // No luck -- this branch is a dead end
226  return false;
227 }
228 
xAOD::TruthVertex_v1::nOutgoingParticles
size_t nOutgoingParticles() const
Get the number of outgoing particles.
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SG::ReadDecorHandle::withDefault
const_reference_type withDefault(size_t index, const D &deflt)
Fetch the variable for one element, as a const reference.
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle< xAOD::TruthParticleContainer >
TruthParticleContainer.h
DerivationFramework::TruthBornLeptonCollectionMaker::TruthBornLeptonCollectionMaker
TruthBornLeptonCollectionMaker(const std::string &t, const std::string &n, const IInterface *p)
Definition: TruthBornLeptonCollectionMaker.cxx:28
DerivationFramework::TruthBornLeptonCollectionMaker::addBranches
virtual StatusCode addBranches() const
Definition: TruthBornLeptonCollectionMaker.cxx:76
xAOD::TruthParticle_v1::isW
bool isW() const
Check if this particle is a W boson.
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::TruthParticle_v1::pdg_id
int pdg_id() const
PDG ID code.
Definition: TruthParticle_v1.h:52
TruthParticleAuxContainer.h
xAOD::TruthParticle_v1::isZ
bool isZ() const
Check if this particle is a Z boson.
MC::isPhysical
bool isPhysical(const T &p)
Identify if the particle is physical, i.e. is stable or decayed.
Definition: HepMCHelpers.h:51
WriteHandle.h
Handle class for recording to StoreGate.
xAOD::TruthParticle_v1::isLepton
bool isLepton() const
Whether the particle is a lepton.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
SG::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
WriteDecorHandle.h
Handle class for adding a decoration to an object.
DerivationFramework::TruthBornLeptonCollectionMaker::initialize
StatusCode initialize()
Definition: TruthBornLeptonCollectionMaker.cxx:42
xAOD::TruthParticle
TruthParticle_v1 TruthParticle
Typedef to implementation.
Definition: Event/xAOD/xAODTruth/xAODTruth/TruthParticle.h:15
xAOD::TruthParticle_v1::hasProdVtx
bool hasProdVtx() const
Check for a production vertex on this particle.
Definition: TruthParticle_v1.cxx:69
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::TruthVertex_v1::incomingParticle
const TruthParticle_v1 * incomingParticle(size_t index) const
Get one of the incoming particles.
Definition: TruthVertex_v1.cxx:70
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
xAOD::TruthParticle_v1::prodVtx
const TruthVertex_v1 * prodVtx() const
The production vertex of this particle.
Definition: TruthParticle_v1.cxx:75
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:37
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
TruthBornLeptonCollectionMaker.h
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
MC::isStable
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
Definition: HepMCHelpers.h:45
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
xAOD::TruthParticle_v1::isHiggs
bool isHiggs() const
Check if this particle is a Higgs boson.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::TruthVertex_v1::nIncomingParticles
size_t nIncomingParticles() const
Get the number of incoming particles.
DerivationFramework::TruthBornLeptonCollectionMaker::hasBareDescendent
bool hasBareDescendent(const xAOD::TruthParticle *p) const
Helper function for finding bare descendents of born leptons.
Definition: TruthBornLeptonCollectionMaker.cxx:213
xAOD::TruthParticle_v1::parent
const TruthParticle_v1 * parent(size_t i) const
Retrieve the i-th mother (TruthParticle) of this TruthParticle.
Definition: TruthParticle_v1.cxx:126
MC::Pythia8::isConditionB
bool isConditionB(const T &p)
Definition: HepMCHelpers.h:25
DerivationFramework::TruthBornLeptonCollectionMaker::m_metaStore
ServiceHandle< StoreGateSvc > m_metaStore
Handle on the metadata store for init.
Definition: TruthBornLeptonCollectionMaker.h:63
ReadDecorHandle.h
Handle class for reading a decoration on an object.
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
ReadHandle.h
Handle class for reading from StoreGate.
DerivationFramework::TruthBornLeptonCollectionMaker::~TruthBornLeptonCollectionMaker
~TruthBornLeptonCollectionMaker()
Definition: TruthBornLeptonCollectionMaker.cxx:38
TruthMetaDataContainer.h
StoreGateSvc.h
python.compressB64.c
def c
Definition: compressB64.py:93
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
xAOD::TruthVertex_v1::outgoingParticle
const TruthParticle_v1 * outgoingParticle(size_t index) const
Get one of the outgoing particles.
Definition: TruthVertex_v1.cxx:120
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
HepMCHelpers.h