ATLAS Offline Software
Loading...
Searching...
No Matches
HepMCTruthReader.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include "AthLinks/ElementLink.h"
7
8#include "GaudiKernel/MsgStream.h"
9#include "GaudiKernel/DataSvc.h"
10#include "GaudiKernel/PhysicalConstants.h"
12
14#include "HepMCTruthReader.h"
16
17using std::cout;
18using std::endl;
19
20HepMCTruthReader::HepMCTruthReader(const std::string& name, ISvcLocator* svcLoc)
21 : AthReentrantAlgorithm(name, svcLoc)
22{}
23
24
26 ATH_MSG_INFO("HepMC container name = " << m_hepMCContainerKey );
27
28 // initialize handles
29 ATH_CHECK(m_hepMCContainerKey.initialize());
30 ATH_MSG_DEBUG("HepMCContainerKey = " << m_hepMCContainerKey.key() );
31 return StatusCode::SUCCESS;
32}
33
34
35StatusCode HepMCTruthReader::execute(const EventContext& ctx) const {
36
37 // Retrieve the HepMC truth:
39 // validity check is only really needed for serial running. Remove when MT is only way.
40 if (!mcColl.isValid()) {
41 ATH_MSG_ERROR("Could not retrieve HepMC with key:" << m_hepMCContainerKey.key());
42 if (m_hepMCContainerKey.key() == "GEN_EVENT") ATH_MSG_ERROR("Try to set 'HepMCContainerKey' to 'TruthEvent'");
43 return StatusCode::FAILURE;
44 } else {
45 ATH_MSG_DEBUG( "Retrieved HepMC with key: " << m_hepMCContainerKey.key() );
46 }
47
48 ATH_MSG_INFO("Number of pile-up events in this Athena event: " << mcColl->size()-1);
49
50 // Loop over events
51 for (unsigned int cntr = 0; cntr < mcColl->size(); ++cntr) {
52 const HepMC::GenEvent* genEvt = (*mcColl)[cntr];
53
54 // Print the event particle/vtx contents
55 if (cntr==0) ATH_MSG_INFO("Printing signal event...");
56 if (cntr>0) ATH_MSG_INFO("Printing pileup events...");
58
59 }
60
61 return StatusCode::SUCCESS;
62}
63
64
65// Print method for event - mimics the HepMC dump.
66// Vertex print method called within here
67void HepMCTruthReader::printEvent(const HepMC::GenEvent* event, bool do4momPtEtaPhi) {
68 cout << "--------------------------------------------------------------------------------\n";
69 cout << "GenEvent: #" << "NNN" << "\n";
70 cout << " Entries this event: " << event->vertices_size() << " vertices, " << event->particles_size() << " particles.\n";
71 cout << " GenParticle Legend\n";
72 if (do4momPtEtaPhi) cout << " Barcode PDG ID ( pt, eta, phi, E ) Stat DecayVtx\n";
73 else cout << " Barcode PDG ID ( Px, Py, Pz, E ) Stat DecayVtx\n";
74 cout << "--------------------------------------------------------------------------------\n";
75 for (const auto& iv: event->vertices()) { printVertex(iv, do4momPtEtaPhi); }
76 cout << "--------------------------------------------------------------------------------\n";
77}
78
79// Print method for vertex - mimics the HepMC dump.
80// Particle print method called within here
81void HepMCTruthReader::printVertex(const HepMC::ConstGenVertexPtr& vertex, bool do4momPtEtaPhi) {
82 if (!vertex) return;
83 std::ios::fmtflags f( cout.flags() );
84 cout << "GenVertex (" << vertex << "):";
85 if (HepMC::barcode(vertex) != 0) {
86 if (vertex->position().x() != 0.0 && vertex->position().y() != 0.0 && vertex->position().z() != 0.0) {
87 cout.width(9);
88 cout <<HepMC::barcode(vertex);
89 cout << " ID:";
90 cout.width(5);
91 cout << vertex->id();
92 cout << " (X,cT)=";
93 cout.width(9);
94 cout.precision(2);
95 cout.setf(std::ios::scientific, std::ios::floatfield);
96 cout.setf(std::ios_base::showpos);
97 cout << vertex->position().x() << ",";
98 cout.width(9);
99 cout.precision(2);
100 cout << vertex->position().y() << ",";
101 cout.width(9);
102 cout.precision(2);
103 cout << vertex->position().z() << ",";
104 cout.width(9);
105 cout.precision(2);
106 cout << vertex->position().t();
107 cout.setf(std::ios::fmtflags(0), std::ios::floatfield);
108 cout.unsetf(std::ios_base::showpos);
109 cout << endl;
110 } else {
111 cout.width(9);
112 cout << HepMC::barcode(vertex);
113 cout << " ID:";
114 cout.width(5);
115 cout << vertex->id();
116 cout << " (X,cT): 0";
117 cout << endl;
118 }
119 } else {
120 // If the vertex doesn't have a unique barcode assigned, then
121 // we print its memory address instead... so that the
122 // print out gives us a unique tag for the particle.
123 if (vertex->position().x() != 0.0 && vertex->position().y() != 0.0 && vertex->position().z() != 0.0) {
124 cout.width(9);
125 cout << " ID:";
126 cout.width(5);
127 cout << vertex->id();
128 cout << " (X,cT)=";
129 cout.width(9);
130 cout.precision(2);
131 cout.setf(std::ios::scientific, std::ios::floatfield);
132 cout.setf(std::ios_base::showpos);
133 cout << vertex->position().x();
134 cout.width(9);
135 cout.precision(2);
136 cout << vertex->position().y();
137 cout.width(9);
138 cout.precision(2);
139 cout << vertex->position().z();
140 cout.width(9);
141 cout.precision(2);
142 cout << vertex->position().t();
143 cout.setf(std::ios::fmtflags(0), std::ios::floatfield);
144 cout.unsetf(std::ios_base::showpos);
145 cout << endl;
146 } else {
147 cout.width(9);
148 cout << " ID:";
149 cout.width(5);
150 cout << vertex->id();
151 cout << " (X,cT):0";
152 cout << endl;
153 }
154 }
155
156 // Print out all the incoming, then outgoing particles
157 for (const auto& iPIn: vertex->particles_in()) {
158 if ( iPIn == vertex->particles_in().front() ) {
159 cout << " I: ";
160 cout.width(2);
161 cout << vertex->particles_in().size();
162 } else cout << " ";
163 printParticle(iPIn, do4momPtEtaPhi);
164 }
165 for (const auto& iPOut: vertex->particles_out()) {
166 if ( iPOut == vertex->particles_out().front()) {
167 cout << " O: ";
168 cout.width(2);
169 cout << vertex->particles_out().size();
170 } else cout << " ";
171 printParticle(iPOut, do4momPtEtaPhi);
172 }
173 cout.flags(f);
174}
175
176
177// Print method for particle - mimics the HepMC dump.
178void HepMCTruthReader::printParticle(const HepMC::ConstGenParticlePtr& particle, bool do4momPtEtaPhi) {
179 if (!particle) return;
180 std::ios::fmtflags f( cout.flags() );
181 cout << " ";
182 cout.width(9);
183 cout << HepMC::barcode(particle);
184 cout.width(9);
185 cout << particle->pdg_id() << " ";
186 cout.width(9);
187 cout.precision(2);
188 cout.setf(std::ios::scientific, std::ios::floatfield);
189 cout.setf(std::ios_base::showpos);
190 if (do4momPtEtaPhi) cout << particle->momentum().perp() << ",";
191 else cout << particle->momentum().px() << ",";
192 cout.width(9);
193 cout.precision(2);
194 if (do4momPtEtaPhi) cout << particle->momentum().pseudoRapidity() << ",";
195 else cout << particle->momentum().py() << ",";
196 cout.width(9);
197 cout.precision(2);
198 if (do4momPtEtaPhi) cout << particle->momentum().phi() << ",";
199 else cout << particle->momentum().pz() << ",";
200 cout.width(9);
201 cout.precision(2);
202 cout << particle->momentum().e() << " ";
203 cout.setf(std::ios::fmtflags(0), std::ios::floatfield);
204 cout.unsetf(std::ios_base::showpos);
205 if ( MC::isDecayed(particle) ) {
206 if ( HepMC::barcode(particle->end_vertex())!=0 ) {
207 cout.width(3);
208 cout << particle->status() << " ";
209 cout.width(9);
210 cout << HepMC::barcode(particle->end_vertex());
211 }
212 } else {
213 cout.width(3);
214 cout << particle->status();
215 }
216 cout << endl;
217 cout.flags(f);
218}
219
220
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
Helpers for checking error return status codes and reporting errors.
ATLAS-specific HepMC functions.
Handle class for reading from StoreGate.
An algorithm that can be simultaneously executed in multiple threads.
static void printVertex(const HepMC::ConstGenVertexPtr &vtx, bool do4momPtEtaPhi)
virtual StatusCode initialize()
Function initialising the algorithm.
HepMCTruthReader(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
Gaudi::Property< bool > m_do4momPtEtaPhi
Flag to printout in pt,eta,phi instead of px,py,pz.
static void printEvent(const HepMC::GenEvent *evt, bool do4momPtEtaPhi)
SG::ReadHandleKey< McEventCollection > m_hepMCContainerKey
The key of the input HepMC truth container.
static void printParticle(const HepMC::ConstGenParticlePtr &part, bool do4momPtEtaPhi)
virtual StatusCode execute(const EventContext &ctx) const
Function executing the algorithm.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
int barcode(const T *p)
Definition Barcode.h:15
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
Definition GenParticle.h:20
HepMC3::ConstGenVertexPtr ConstGenVertexPtr
Definition GenVertex.h:24
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
bool isDecayed(const T &p)
Identify if the particle decayed.