ATLAS Offline Software
CopyTruthJetParticles.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
10 
17 #include "AsgMessaging/Check.h"
19 
20 #include <mutex> // std::call_once, std::once_flag
21 
22 #ifndef XAOD_STANDALONE
23 // Usage of metadata is for now only possible in Athena...
24 //#include "CoralBase/AttributeListException.h"
26 #endif
27 
28 // For std::find in comesFrom()
29 #include <algorithm>
30 
31 using namespace std;
32 using namespace MCTruthPartClassifier;
33 
37  ATH_CHECK(m_classif.retrieve());
38 
39  ATH_CHECK(m_truthEventKey.initialize());
40  ATH_CHECK(m_outTruthPartKey.initialize());
41 
42  return StatusCode::SUCCESS;
43 }
44 
45 
46 
48  std::vector<const xAOD::TruthParticle*>& promptLeptons,
49  std::map<const xAOD::TruthParticle*,unsigned int>& tc_results) const {
50 
51  // Check if this thing is a candidate to be in a truth jet
52  // First block is largely copied from isGenStable, which works on HepMC only
53  if (HepMC::is_simulation_particle(tp)) return false; // Particle is from G4
54  int pdgid = tp->pdgId();
55  if (MC::isZeroEnergyPhoton(tp)) return false; // Work around for an old generator bug
56 
57  // -- changed for dark jet clustering -- //
58  if ( !MC::isStable(tp) && !m_includeDark ) return false; // dark hadrons will not be status 1
59  // ----------------------------------- //
60 
61  // Easy classifiers by PDG ID
62  if(MC::isNeutrino(pdgid)) {
63  if (!m_includeNu) return false;
64  } else {
65  if (!m_includeBSMNonInt && !MC::isInteracting(pdgid)) return false;
66  }
67  if (!m_includeMu && MC::isMuon(pdgid)) return false;
68 
69  // Already built a list of prompt leptons, just use it here
70  if (!m_includePromptLeptons && std::find(promptLeptons.begin(),promptLeptons.end(),tp)!=promptLeptons.end()){
71  ATH_MSG_VERBOSE("Veto prompt lepton (" << pdgid << ") with pt " << tp->pt());
72  return false;
73  }
74 
75  // Extra catch. If we aren't supposed to include prompt leptons, we aren't supposed to include prompt neutrinos
76  unsigned int tc_res = getTCresult(tp, tc_results);
78  return false;
79  }
80 
81  // -- added for dark jet clustering -- //
82  // new classifiers to account for dark particles
83  // for dark jets: ignore SM particles; include only "stable" dark hadrons
84  if (!m_includeSM && ((abs(tp->pdgId()) < 4.9e6) || (abs(tp->pdgId()) >= 5e6))) return false;
85  if (m_includeDark) {
86  if (abs(tp->pdgId()) <= 4900101) return false; // ignore Xd, qd, gd
87  if (tp->hasDecayVtx() && (abs(tp->child()->pdgId()) >= 4.9e6)) return false; // ignore "non-stable" dark hadrons (decaying to dark sector) -- "stable" if decaying to SM
88  }
89  // for SM jets: ignore dark particles - probably unnecessary bc of status requirement above
90  if (!m_includeDark && (std::abs(tp->pdgId()) >= 4.9e6) && (std::abs(tp->pdgId()) < 5e6)) return false;
91  // ----------------------------------- //
92 
93  if (!m_includePromptPhotons && MC::isPhoton(pdgid) && tp->hasProdVtx()){
94  //ParticleOrigin orig = getPartOrigin(tp, originMap);
95  //if (orig==Higgs || orig==HiggsMSSM) return false;
96  if (MCTruthPartClassifier::isPrompt(tc_res)) return false;
97  }
98 
99  // If we want to remove photons via the dressing decoration
100  if (!m_dressingName.empty()){
101  // Accessor for the dressing decoration above
102  const static SG::AuxElement::Accessor<char> dressAcc(m_dressingName);
103  if (MC::isPhoton(pdgid) && dressAcc(*tp)) return false;
104  } // End of removal via dressing decoration
105 
106  // Pseudo-rapidity cut
107  if(std::abs(tp->eta())>m_maxAbsEta) return false;
108 
109  // Vetoes of specific origins. Not fast, but if no particles are specified should not execute
110  if (m_vetoPDG_IDs.size()>0){
111  std::vector<int> used_vertices;
112  for (int anID : m_vetoPDG_IDs){
113  used_vertices.clear();
114  if (comesFrom(tp,anID,used_vertices)) return false;
115  }
116  }
117 
118  // Made it!
119  return true;
120 }
121 
122 
124  std::map<const xAOD::TruthParticle*,unsigned int>& tc_results) const
125 {
126  if(tc_results.find(tp) == tc_results.end()) {
127  const unsigned int result = tp ? std::get<0>(MCTruthPartClassifier::defOrigOfParticle(tp)) : 0;
128  tc_results[tp] = result;
129  }
130  return tc_results[tp];
131 }
132 
134 
135  // Usage of metadata is only possible in Athena (not supported by dual-use tools yet)...
136 #ifndef XAOD_STANDALONE
137  bool found = false;
138  // retrieve the value for the current sample from metadata
139 
140  int barcodeOffset_tmp(0);
141  ATH_MSG_INFO("Look for barcode offset in metadata ... ");
142  try {
143  StatusCode sc= AthAnalysisHelper::retrieveMetadata("/Simulation/Parameters","SimBarcodeOffset",barcodeOffset_tmp) ;
144  found = sc.isSuccess();
145  } catch(std::exception &e) {
146  ATH_MSG_DEBUG(" Could not retrieve barcode offset in metadata : "<< e.what());
147  }
148  if (found) {
149  if (HepMC::SIM_BARCODE_THRESHOLD!=barcodeOffset_tmp) ATH_MSG_WARNING(" Barcode offset found in metadata. Its value is : "<< barcodeOffset_tmp << "vs the used "<< HepMC::SIM_BARCODE_THRESHOLD);
150  }
151 #else // standalone :
152  ATH_MSG_WARNING("Can't retrieve automatically the truth barcode offset outside Athena. Falling back to offset property: " << HepMC::SIM_BARCODE_THRESHOLD);
153 #endif
154  return 0;
155 }
156 
158 
159  // retrieve barcode Offset for this event from metadata.
160  // We'd need a cleaner solution where this offset is set only at
161  // each new file, but this requires some tool interface which does
162  // not exist in RootCore yet.
163  // So we use the less disruptive solution in Athena for now...
164 
165  // the function used below is
166  // std::call_once(metaDataFlag,basicMetaDataCheck(), this);
167  // std::call_once(metaDataFlag,this->basicMetaDataCheck());
168  // the syntax is explained in http://stackoverflow.com/questions/23197333/why-is-this-pointer-needed-when-calling-stdcall-once
169  // this syntax requires the call_once function to receive the object the function is called upon
170  //": these are all non-static member functions , they act on objects
171  // they need the "this" pointer which always point to the object the function is working on
172  //http://www.learncpp.com/cpp-tutorial/812-static-member-functions/
173 
174 
175  // std::call_once(metaDataFlag,&CopyTruthJetParticles::basicMetaDataCheck,this,barcodeOffset);
176  // this call happens only once and it modifies m_barcodeOffset
177  // Note that catching the return value of this is rather complicated, so it throws rather than returning errors
178  static std::once_flag metaDataFlag;
179  std::call_once(metaDataFlag,[&]() {setBarCodeFromMetaDataCheck();});
180 
181  std::vector<const xAOD::TruthParticle*> promptLeptons;
182  promptLeptons.reserve(10);
183 
184  // Retrieve the xAOD truth objects
185  auto truthEvents = SG::makeHandle(m_truthEventKey);
186  if ( !truthEvents.isValid() ) {
187  ATH_MSG_ERROR("Failed to retrieve truth event container " << m_truthEventKey.key());
188  return 1;
189  }
190 
191  // Classify particles for tagging and add to the TruthParticleContainer
192  std::unique_ptr<ConstDataVector<xAOD::TruthParticleContainer> > ptruth(new ConstDataVector<xAOD::TruthParticleContainer>(SG::VIEW_ELEMENTS));
193  std::map<const xAOD::TruthParticle*,unsigned int> tc_results;
194  tc_results.clear();
195  size_t numCopied = 0;
196  const xAOD::TruthEvent* hsevt = truthEvents->front();
197  if(!hsevt) {
198  ATH_MSG_ERROR("Null pointer received for first truth event!");
199  return 1;
200  }
201 
202  for (unsigned int ip = 0; ip < hsevt->nTruthParticles(); ++ip) {
203  const xAOD::TruthParticle* tp = hsevt->truthParticle(ip);
204  if(!tp) continue;
205  if (tp->pt() < m_ptmin)
206  continue;
207  // Cannot use the truth helper functions; they're written for HepMC
208  // Last two switches only apply if the thing is a lepton and not a tau
209  int pdgid = tp->pdgId();
210  if ((std::abs(pdgid)==11 || std::abs(pdgid)==13) && tp->hasProdVtx()){
211  // If this is a prompt, generator stable lepton, then we can use it
213  promptLeptons.push_back(tp);
214  }
215  }
216  }
217 
218  for (size_t itp(0); itp<hsevt->nTruthParticles(); ++itp) {
219  const xAOD::TruthParticle* tp = hsevt->truthParticle(itp);
220  if(!tp) continue;
221  if (tp->pt() < m_ptmin)
222  continue;
223 
224  if (classifyJetInput(tp, promptLeptons, tc_results)) {
225  ptruth->push_back(tp);
226  numCopied += 1;
227  }
228  }
229 
230  ATH_MSG_DEBUG("Copied " << numCopied << " truth particles into " << m_outTruthPartKey.key() << " TruthParticle container");
231 
232  // record
233  auto truthParticles_out = SG::makeHandle(m_outTruthPartKey);
234  ATH_MSG_DEBUG("Recorded truth particle collection " << m_outTruthPartKey.key());
235  // notify
236  if (!truthParticles_out.put(std::move(ptruth))) {
237  ATH_MSG_ERROR("Unable to write new TruthParticleContainer to event store: "
238  << m_outTruthPartKey.key());
239  } else {
240  ATH_MSG_DEBUG("Created new TruthParticleContainer in event store: "
241  << m_outTruthPartKey.key());
242  }
243 
244  return 0;
245 }
246 
247 
248 bool CopyTruthJetParticles::comesFrom( const xAOD::TruthParticle* tp, const int pdgID, std::vector<int>& used_vertices ) const {
249  // If it's not a particle, then it doesn't come from something...
250  if (!tp) return false;
251  // If it doesn't have a production vertex or has no parents, it doesn't come from much of anything
252  if (!tp->prodVtx() || tp->nParents()==0) return false;
253  // If we have seen it before, then skip this production vertex
254  if (std::find(used_vertices.begin(),used_vertices.end(), HepMC::uniqueID(tp->prodVtx()))!=used_vertices.end()) return false;
255  // Add the production vertex to our used list
256  used_vertices.push_back( HepMC::uniqueID(tp->prodVtx()) );
257  // Loop over the parents
258  for (size_t par=0;par<tp->nParents();++par){
259  // Check for null pointers in case of skimming
260  if (!tp->parent(par)) continue;
261  // Check for a match
262  if (tp->parent(par)->absPdgId()==pdgID) return true;
263  // Recurse on this parent
264  if (comesFrom(tp->parent(par), pdgID, used_vertices)) return true;
265  }
266  // No hits -- all done with the checks!
267  return false;
268 }
269 
WriteHandle.h
Handle class for recording to StoreGate.
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
TruthTest.itp
itp
Definition: TruthTest.py:46
HepMC::SIM_BARCODE_THRESHOLD
constexpr int SIM_BARCODE_THRESHOLD
Constant defining the barcode threshold for simulated particles, eg. can be used to separate generato...
Definition: MagicNumbers.h:36
Check.h
get_generator_info.result
result
Definition: get_generator_info.py:21
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
CopyTruthJetParticles::m_includeSM
Gaudi::Property< bool > m_includeSM
Definition: CopyTruthJetParticles.h:57
CopyTruthJetParticles::comesFrom
bool comesFrom(const xAOD::TruthParticle *tp, const int pdgID, std::vector< int > &used_vertices) const
Definition: CopyTruthJetParticles.cxx:248
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
TruthParticleContainer.h
xAOD::TruthEventBase_v1::truthParticle
const TruthParticle * truthParticle(size_t index) const
Get a pointer to one of the truth particles.
Definition: TruthEventBase_v1.cxx:50
isNeutrino
bool isNeutrino(const T &p)
APID: the fourth generation neutrinos are neutrinos.
Definition: AtlasPID.h:161
ParticleTest.tp
tp
Definition: ParticleTest.py:25
CopyTruthJetParticles::m_maxAbsEta
Gaudi::Property< float > m_maxAbsEta
Maximum allowed eta for particles in jets.
Definition: CopyTruthJetParticles.h:66
MCTruthClassifier.h
MCTruthPartClassifier::defOrigOfParticle
std::tuple< unsigned int, T > defOrigOfParticle(T thePart)
Definition: TruthClassifiers.h:151
CopyTruthJetParticles::m_includeDark
Gaudi::Property< bool > m_includeDark
Definition: CopyTruthJetParticles.h:59
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
CopyTruthJetParticles::m_includeMu
Gaudi::Property< bool > m_includeMu
Definition: CopyTruthJetParticles.h:48
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
TruthParticleAuxContainer.h
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:270
CopyTruthParticles::m_outTruthPartKey
SG::WriteHandleKey< ConstDataVector< xAOD::TruthParticleContainer > > m_outTruthPartKey
Key for output truth particles.
Definition: CopyTruthParticles.h:46
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:342
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
CopyTruthJetParticles::setBarCodeFromMetaDataCheck
int setBarCodeFromMetaDataCheck() const
Definition: CopyTruthJetParticles.cxx:133
calibdata.exception
exception
Definition: calibdata.py:496
HepMC::uniqueID
int uniqueID(const T &p)
Definition: MagicNumbers.h:109
xAOD::TruthEvent_v1
Class describing a signal truth event in the MC record.
Definition: TruthEvent_v1.h:35
find_tgc_unfilled_channelids.ip
ip
Definition: find_tgc_unfilled_channelids.py:3
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CopyTruthParticles::m_truthEventKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventKey
Key for input truth event.
Definition: CopyTruthParticles.h:43
CopyTruthJetParticles::m_includePromptLeptons
Gaudi::Property< bool > m_includePromptLeptons
Definition: CopyTruthJetParticles.h:50
TruthVertex.h
CopyTruthJetParticles.h
CopyTruthJetParticles::m_dressingName
Gaudi::Property< std::string > m_dressingName
Name of the decoration to be used for identifying FSR (dressing) photons.
Definition: CopyTruthJetParticles.h:73
CopyTruthJetParticles::execute
virtual int execute() const
redefine execute so we can call our own classify() with the barcode offset for the current event.
Definition: CopyTruthJetParticles.cxx:157
CopyTruthJetParticles::m_includePromptPhotons
Gaudi::Property< bool > m_includePromptPhotons
Definition: CopyTruthJetParticles.h:52
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
createCoolChannelIdFile.par
par
Definition: createCoolChannelIdFile.py:29
MagicNumbers.h
CopyTruthJetParticles::m_includeBSMNonInt
Gaudi::Property< bool > m_includeBSMNonInt
Definition: CopyTruthJetParticles.h:44
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::TruthEventBase_v1::nTruthParticles
size_t nTruthParticles() const
Get the number of truth particles.
CopyTruthJetParticles::m_classif
ToolHandle< IMCTruthClassifier > m_classif
Handle on MCTruthClassifier for finding prompt leptons.
Definition: CopyTruthJetParticles.h:77
AthAnalysisHelper.h
MC::isStable
bool isStable(const T &p)
Identify if the particle is stable, i.e. has not decayed.
Definition: HepMCHelpers.h:45
CopyTruthJetParticles::classifyJetInput
bool classifyJetInput(const xAOD::TruthParticle *tp, std::vector< const xAOD::TruthParticle * > &promptLeptons, std::map< const xAOD::TruthParticle *, unsigned int > &tc_results) const
Redefine our own Classifier function(s)
Definition: CopyTruthJetParticles.cxx:47
CondAlgsOpts.found
int found
Definition: CondAlgsOpts.py:101
MCTruthPartClassifier::isPrompt
int isPrompt(const unsigned int classify, bool allow_prompt_tau_decays=true)
Definition: TruthClassifiers.h:180
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
MCTruthPartClassifier
Definition: TruthClassifiers.h:12
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
CopyTruthParticles::m_ptmin
Gaudi::Property< float > m_ptmin
Minimum pT for particle selection (in MeV)
Definition: CopyTruthParticles.h:40
xAOD::EgammaHelpers::isPhoton
bool isPhoton(const xAOD::Egamma *eg)
is the object a photon
Definition: EgammaxAODHelpers.cxx:21
CopyTruthJetParticles::m_vetoPDG_IDs
Gaudi::Property< std::vector< int > > m_vetoPDG_IDs
Definition: CopyTruthJetParticles.h:68
CopyTruthJetParticles::initialize
virtual StatusCode initialize()
Function initialising the tool.
Definition: CopyTruthJetParticles.cxx:36
MC::isInteracting
bool isInteracting(const T &p)
Identify if the particle with given PDG ID would not interact with the detector, i....
Definition: HepMCHelpers.h:33
CopyTruthParticles
Definition: CopyTruthParticles.h:18
CopyTruthJetParticles::getTCresult
unsigned int getTCresult(const xAOD::TruthParticle *tp, std::map< const xAOD::TruthParticle *, unsigned int > &tc_results) const
Definition: CopyTruthJetParticles.cxx:123
MC::isZeroEnergyPhoton
bool isZeroEnergyPhoton(const T &p)
Identify a photon with zero energy. Probably a workaround for a generator bug.
Definition: HepMCHelpers.h:71
CopyTruthJetParticles::m_includeNu
Gaudi::Property< bool > m_includeNu
Definition: CopyTruthJetParticles.h:46
TruthEventContainer.h
checker_macros.h
Define macros for attributes used to control the static checker.
HepMCHelpers.h
AthAnalysisHelper::retrieveMetadata
static std::string retrieveMetadata(const std::string &folder, const std::string &key, const ServiceHandle< StoreGateSvc > &inputMetaStore)
method that always returns as a string you can use from, e.g, pyROOT with evt = ROOT....
Definition: AthAnalysisHelper.h:254
CopyTruthJetParticles::CopyTruthJetParticles
CopyTruthJetParticles(const std::string &name)
Constructor.
Definition: CopyTruthJetParticles.cxx:34
isMuon
bool isMuon(const T &p)
Definition: AtlasPID.h:154