ATLAS Offline Software
GenericTruthThinning.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // GenericTruthThinning.cxx, (c) ATLAS Detector software
8 // Author: James Catmore (James.Catmore@cern.ch)
9 // Removes all truth particles/vertices which do not pass a user-defined cut
10 
16 #include "GaudiKernel/ThreadLocalContext.h"
17 
19 
20 #include <vector>
21 #include <string>
22 
23 // Constructor
25  const std::string& n,
26  const IInterface* p ) :
27 base_class(t,n,p),
28 m_ntotvtx(0),
29 m_ntotpart(0),
30 m_npassvtx(0),
31 m_npasspart(0),
32 m_partString(""),
33 //m_vtxString(""),
34 m_preserveDescendants(false),
35 m_preserveGeneratorDescendants(false),
36 m_preserveAncestors(false),
37 m_tauHandling(true)
38 {
39  declareProperty("EventsKey", m_eventsKey);
40  declareProperty("ParticleSelectionString", m_partString);
41  //declareProperty("VertexSelectionString", m_vtxString);
42  declareProperty("PreserveDescendants", m_preserveDescendants);
43  declareProperty("PreserveGeneratorDescendants", m_preserveGeneratorDescendants);
44  declareProperty("PreserveAncestors", m_preserveAncestors);
45  declareProperty("TauHandling", m_tauHandling);
46 }
47 
48 // Destructor
50 }
51 
52 // Athena initialize and finalize
54 {
55  ATH_MSG_VERBOSE("initialize() ...");
56  ATH_CHECK( m_particlesKey.initialize (m_streamName) );
57  ATH_CHECK( m_verticesKey.initialize (m_streamName) );
58  ATH_MSG_INFO("Using " << m_particlesKey.key() << " and "<< m_verticesKey.key() << " as the source collections for truth thinning");
59 
60  if (m_partString.empty()/* && m_vtxString==""*/) {
61  ATH_MSG_FATAL("No selection string provided either for vertices or particles!");
62  return StatusCode::FAILURE;
63  } else {ATH_MSG_INFO("Truth thinning selection strings: " << m_partString /*<< " " << m_vtxString*/);}
64 
65  if (m_preserveDescendants && m_preserveGeneratorDescendants) {
66  ATH_MSG_FATAL("You are asking to keep both all descendants, and only those from the event generator. Please check your job options.");
67  return StatusCode::FAILURE;
68  }
69  // Set up the text-parsing machinery for thinning the truth directly according to user cuts
70  if (!m_partString.empty()) {
71  ATH_CHECK( initializeParser(m_partString) );
72  }
73  return StatusCode::SUCCESS;
74 }
75 
77 {
78  ATH_MSG_VERBOSE("finalize() ...");
79  ATH_MSG_INFO("Processed "<< m_ntotvtx <<" truth vertices, "<< m_npassvtx << " were retained ");
80  ATH_MSG_INFO("Processed "<< m_ntotpart <<" truth particles, "<< m_npasspart << " were retained ");
81  ATH_CHECK(finalizeParser());
82  return StatusCode::SUCCESS;
83 }
84 
85 // The thinning itself
87 {
88  const EventContext& ctx = Gaudi::Hive::currentContext();
89 
90  // Retrieve truth collections
92  (m_particlesKey, ctx);
94  (m_verticesKey, ctx);
95 
96  const xAOD::TruthEventContainer* importedTruthEvents;
97  if (evtStore()->retrieve(importedTruthEvents,m_eventsKey).isFailure()) {
98  ATH_MSG_ERROR("No TruthEventContainer with name " << m_eventsKey << " found in StoreGate!");
99  return StatusCode::FAILURE;
100  }
101 
102 
103 
104  // Set up a mask with the same entries as the full collections
105  unsigned int nParticles = importedTruthParticles->size();
106  unsigned int nVertices = importedTruthVertices->size();
107  std::vector<bool> partMask, vertMask;
108  partMask.assign(nParticles,false); // default: don't keep any truth items
109  vertMask.assign(nVertices,false);
110  m_ntotvtx += nVertices; m_ntotpart += nParticles;
111 
112  // Execute the text parsers and update the mask
113  if (!m_partString.empty()) {
114  std::vector<int> entries = m_parser->evaluateAsVector();
115  unsigned int nEntries = entries.size();
116  // check the sizes are compatible
117  if (nParticles != nEntries ) {
118  ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string used TruthParticles?");
119  return StatusCode::FAILURE;
120  } else {
121  // set mask
122  for (unsigned int i=0; i<nParticles; ++i) if (entries[i]==1) partMask[i]=true;
123  }
124  }
125 
126  // Special treatment of taus such that only the last one in the chain is kept
127  // Needs another run over the particle collection
128  if (m_tauHandling) {
130  for (unsigned int i=0; i<nParticles; ++i) {
131  const xAOD::TruthParticle* particle = (*importedTruthParticles)[i];
132  if ( abs(particle->pdgId())==15 ) { // This is a tau
133  bool last(true);
134  std::vector<int> tauDecayProducts; // all decay products of the tau
135  std::unordered_set<int> tauDecayEncounteredUniqueIDs; // loop checking
136  tauDecayHelper.descendants(particle,tauDecayProducts,tauDecayEncounteredUniqueIDs); // recursive
137  for (unsigned int tauDecIt=0; tauDecIt<tauDecayProducts.size(); ++tauDecIt) {
138  if (abs(tauDecayProducts[tauDecIt])==15) { // any taus in the decay products?
139  last = false;
140  break;
141  }
142  }
143  if (!last) partMask[i]=false;
144  } // end of code for tau
145  } // end of loop over particles for tau checking
146  } // end of tau handling option
147 
148  // If user requested preservation of descendants/ancestors:
149  // - loop over the masks and work out which particles need to be descended/ascended from
150  // - do the recursive loop
151  // - update the masks including the descendants/ancestors
152  // To ensure graph completeness, this over-rides anything set by the special treatment
153  // of taus in the section above
155  std::unordered_set<int> encounteredUniqueIDs; // to enable loop handling
156  if (m_preserveDescendants || m_preserveGeneratorDescendants || m_preserveAncestors) {
157  for (unsigned int i=0; i<nParticles; ++i) {
158  bool toKeep = partMask[i];
159  if (!toKeep) continue;
160  const xAOD::TruthParticle* particle = (*importedTruthParticles)[i];
161  encounteredUniqueIDs.clear();
162  if (m_preserveDescendants) decayHelper.descendants(particle,partMask,vertMask,encounteredUniqueIDs,true);
163  encounteredUniqueIDs.clear();
164  if (m_preserveGeneratorDescendants) decayHelper.descendants(particle,partMask,vertMask,encounteredUniqueIDs,false);
165  encounteredUniqueIDs.clear();
166  if (m_preserveAncestors) decayHelper.ancestors(particle,partMask,vertMask,encounteredUniqueIDs);
167  encounteredUniqueIDs.clear();
168  }
169  }
170  //for (unsigned int i=0; i<nVertices; ++i) {
171  // bool toKeep = vertMask[i];
172  // if (!toKeep) continue;
173  // const xAOD::TruthVertex* vertex = (*importedTruthVertices)[i];
174  // decayHelper.descend(vertex,partMask,vertMask);
175  //}
176 
177  // Count the masks
178  m_npasspart += std::count (partMask.begin(), partMask.end(), true);
179  m_npassvtx += std::count (vertMask.begin(), vertMask.end(), true);
180 
181  // Execute the thinning service based on the mask. Finish.
182  importedTruthParticles.keep (partMask);
183  importedTruthVertices.keep (vertMask);
184 
185  return StatusCode::SUCCESS;
186 }
187 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
DerivationFramework::GenericTruthThinning::m_tauHandling
bool m_tauHandling
Definition: GenericTruthThinning.h:50
DerivationFramework::GenericTruthThinning::m_eventsKey
std::string m_eventsKey
Definition: GenericTruthThinning.h:44
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
NSWL1::nVertices
int nVertices(const Polygon &p)
Definition: GeoUtils.cxx:35
DerivationFramework::GenericTruthThinning::m_preserveAncestors
bool m_preserveAncestors
Definition: GenericTruthThinning.h:49
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ThinningHandle.h
Handle for requesting thinning for a data object.
TruthVertexContainer.h
TruthParticleContainer.h
DerivationFramework::GenericTruthThinning::m_partString
std::string m_partString
Definition: GenericTruthThinning.h:45
DerivationFramework::GenericTruthThinning::finalize
virtual StatusCode finalize() override
Definition: GenericTruthThinning.cxx:76
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
DerivationFramework::DecayGraphHelper::descendants
void descendants(const xAOD::TruthParticle *pHead, std::vector< int > &particleList, std::unordered_set< int > &encounteredUniqueIDs)
Definition: DecayGraphHelper.h:116
SG::ThinningHandleBase::keep
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
Definition: ThinningHandleBase.cxx:68
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:41
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
GenericTruthThinning.h
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
DerivationFramework::GenericTruthThinning::m_preserveGeneratorDescendants
bool m_preserveGeneratorDescendants
Definition: GenericTruthThinning.h:48
DerivationFramework::GenericTruthThinning::initialize
virtual StatusCode initialize() override
Definition: GenericTruthThinning.cxx:53
MagicNumbers.h
DerivationFramework::DecayGraphHelper::ancestors
void ancestors(const xAOD::TruthParticle *pHead, std::vector< bool > &particleMask, std::vector< bool > &vertexMask, std::unordered_set< int > &encounteredUniqueIDs)
Definition: DecayGraphHelper.h:186
DerivationFramework::DecayGraphHelper
Definition: DecayGraphHelper.h:26
DerivationFramework::GenericTruthThinning::GenericTruthThinning
GenericTruthThinning(const std::string &t, const std::string &n, const IInterface *p)
Definition: GenericTruthThinning.cxx:24
DerivationFramework::GenericTruthThinning::doThinning
virtual StatusCode doThinning() const override
Definition: GenericTruthThinning.cxx:86
DerivationFramework::GenericTruthThinning::m_preserveDescendants
bool m_preserveDescendants
Definition: GenericTruthThinning.h:47
DerivationFramework::GenericTruthThinning::~GenericTruthThinning
virtual ~GenericTruthThinning()
Definition: GenericTruthThinning.cxx:49
entries
double entries
Definition: listroot.cxx:49
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
TruthEventContainer.h
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:73