ATLAS Offline Software
Loading...
Searching...
No Matches
TruthVariables.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
9namespace MuonPRDTest {
10 TruthVariables::TruthVariables(MuonTesterTree& tree, const std::string& container_name, MSG::Level msglvl) :
11 PrdTesterModule(tree, "TruthTestModule", msglvl), m_key{container_name} {}
13
14 bool TruthVariables::fill(const EventContext& ctx) {
15 SG::ReadHandle<McEventCollection> truthContainer{m_key, ctx};
16 if (!truthContainer.isValid()) {
17 ATH_MSG_FATAL("Failed to retrieve container " << m_key.fullKey());
18 return false;
19 }
20 unsigned int truth_vertices{0}, truth_parts{0};
21 for (auto it : *truthContainer) {
22 const HepMC::GenEvent* subEvent = it;
23#ifdef HEPMC3
24 for (const auto& vertex : subEvent->vertices()) {
25 m_Truth_vertex.push_back(vertex->position());
26 m_Truth_vertexId.push_back(vertex->status());
27 ++truth_vertices;
28 }
29 for (const auto& particle : subEvent->particles()) {
30 m_truthParticleP4.push_back(particle->momentum());
31 m_Truth_particlePdg_id.push_back(particle->pdg_id());
32 m_Truth_particleStatus.push_back(particle->status());
33 m_Truth_particleBarcode.push_back(HepMC::barcode(particle));
34 auto production_vertex = particle->production_vertex();
35 m_Truth_particleProduction_vertex_id.push_back(production_vertex ? production_vertex->status() : -1);
36 auto end_vertex = particle->end_vertex();
37 m_Truth_particleEnd_vertex_id.push_back(end_vertex ? end_vertex->status() : -1);
38 ++truth_parts;
39 }
40#else
41 // Vertex
42 HepMC::ConstGenEventVertexRange vertex_range = subEvent->vertex_range();
43 for (auto vit : vertex_range) {
44 const HepMC::GenVertex* vertex = vit;
45 m_Truth_vertex.push_back(vertex->position());
46 m_Truth_vertexId.push_back(vertex->id());
47 ++truth_vertices;
48 }
49 // Particle
50 HepMC::ConstGenEventParticleRange particle_range = subEvent->particle_range();
51 for (auto pit : particle_range) {
52 const HepMC::GenParticle* particle = pit;
53 m_truthParticleP4.push_back(particle->momentum());
54 m_Truth_particlePdg_id.push_back(particle->pdg_id());
55 m_Truth_particleStatus.push_back(particle->status());
56 m_Truth_particleBarcode.push_back(HepMC::barcode(particle));
57 auto production_vertex = particle->production_vertex();
58 m_Truth_particleProduction_vertex_id.push_back(production_vertex ? production_vertex->id() : -1);
59 auto end_vertex = particle->end_vertex();
60 m_Truth_particleEnd_vertex_id.push_back(end_vertex ? end_vertex->id() : -1);
61 ++truth_parts;
62 }
63#endif
64
65 }
66 m_Truth_nVertices = truth_vertices;
67 m_Truth_nParticles = truth_parts;
68 return true;
69 }
70} // namespace MuonPRDTest
#define ATH_MSG_FATAL(x)
PrdTesterModule(MuonTesterTree &tree, const std::string &grp_name, MSG::Level msglvl)
VectorBranch< int > & m_Truth_vertexId
VectorBranch< int > & m_Truth_particleStatus
VectorBranch< int > & m_Truth_particleEnd_vertex_id
ScalarBranch< unsigned int > & m_Truth_nVertices
VectorBranch< int > & m_Truth_particleProduction_vertex_id
SG::ReadHandleKey< McEventCollection > m_key
bool declare_keys() override final
VectorBranch< int > & m_Truth_particleBarcode
TruthVariables(MuonTesterTree &tree, const std::string &container_name, MSG::Level msglvl)
PtEtaPhiEBranch m_truthParticleP4
CartesFourVecBranch m_Truth_vertex
ScalarBranch< unsigned int > & m_Truth_nParticles
bool fill(const EventContext &ctx) override final
The fill method checks if enough information is provided such that the branch is cleared from the inf...
VectorBranch< int > & m_Truth_particlePdg_id
bool declare_dependency(Key &key)
Declares the ReadHandle/ ReadCondHandleKey as data dependency of the algorithm.
TTree * tree() override final
Returns the underlying TTree object.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
int barcode(const T *p)
Definition Barcode.h:16