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