ATLAS Offline Software
Loading...
Searching...
No Matches
xAODTruthReader.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#include "AthLinks/ElementLink.h"
7
8#include "GaudiKernel/MsgStream.h"
9#include "GaudiKernel/DataSvc.h"
10#include "GaudiKernel/PhysicalConstants.h"
12
13
17
22
23#include "xAODTruthReader.h"
24
25using namespace std;
26
27namespace xAODReader {
28
29
30 xAODTruthReader::xAODTruthReader(const string& name, ISvcLocator* svcLoc)
31 : AthReentrantAlgorithm(name, svcLoc)
32 {}
33
34
36
37 // initialize handles
38 ATH_MSG_INFO("TruthContainerKey = " << m_xaodTruthEventContainerKey.key() );
40 // ATH_MSG_INFO("TruthPileupContainerKey = " << m_xaodTruthPUEventContainerKey.key() );
41 // ATH_CHECK(m_xaodTruthPUEventContainerKey.initialize());
42 return StatusCode::SUCCESS;
43
44 }
45
46 StatusCode xAODTruthReader::execute(const EventContext& ctx) const {
47 // Retrieve the xAOD truth:
49
51 if (xTruthEventContainer.isValid()) {
53 }
54 else {
55 ATH_MSG_ERROR("Could NOT find " << m_xaodTruthEventContainerKey.key());
56 return StatusCode::FAILURE;
57 }
58
59 ATH_MSG_INFO("Number of signal events in this Athena event: " << xTruthEventContainer->size());
60
61 // Signal process loop
62 ATH_MSG_INFO("Printing signal event...");
63 for (const xAOD::TruthEvent* evt : *xTruthEventContainer) {
64 cout << endl << endl;
65
66 // Print hard-scattering info
67 const xAOD::TruthVertex* vtx = evt->signalProcessVertex();
68 ATH_MSG_INFO("Signal process vertex: " << vtx);
69 if (vtx) ATH_MSG_INFO("Poistion = (" << vtx->x() << ", " << vtx->y() << ", " << vtx->z() << ")");
70 else ATH_MSG_INFO("Position n.a.");
71 // Print the event particle/vtx contents
73
74 }
75
76 // if (m_doPUEventPrintout) {
77
78 // SG::ReadHandle<xAOD::TruthPileupEventContainer> xTruthPUEventContainer(m_xaodTruthPUEventContainerKey, ctx);
79 // if (!xTruthPUEventContainer.isValid()) {
80 // ATH_MSG_INFO("Found " << m_xaodTruthPUEventContainerKey.key());
81 // }
82 // else {
83 // ATH_MSG_ERROR("Could NOT find " << m_xaodTruthPUEventContainerKey.key());
84 // return StatusCode::FAILURE;
85 // }
86
87 // ATH_MSG_INFO("Number of pile-up events in this Athena event: " << xTruthPUEventContainer->size());
88
89 // // Pile-up loop
90 // ATH_MSG_INFO("Printing pileup events...");
91 // for (const xAOD::TruthPileupEvent* evt : *xTruthPUEventContainer) {
92 // cout << endl << endl;
93 // printEvent(evt, m_do4momPtEtaPhi);
94 // }
95 // }
96
97 return StatusCode::SUCCESS;
98 }
99
100
101 // Print method for event - mimics the HepMC dump.
102 // Vertex print method called within here
103 void xAODTruthReader::printEvent(const xAOD::TruthEventBase* event, bool do4momPtEtaPhi) {
104 cout << "--------------------------------------------------------------------------------\n";
105 cout << "GenEvent: #" << "NNN" << "\n";
106 cout << " Entries this event: " << event->nTruthVertices() << " vertices, " << event->nTruthParticles() << " particles.\n";
107 cout << " GenParticle Legend\n";
108 if (do4momPtEtaPhi) cout << " UniqueID PDG ID ( pt, eta, phi, E ) Stat DecayVtx\n";
109 else cout << " UniqueID PDG ID ( Px, Py, Pz, E ) Stat DecayVtx\n";
110 cout << "--------------------------------------------------------------------------------\n";
111 for (unsigned int iv = 0; iv < event->nTruthVertices(); ++iv) {
112 printVertex(event->truthVertex(iv), do4momPtEtaPhi);
113 }
114 cout << "--------------------------------------------------------------------------------\n";
115 }
116
117 // Print method for vertex - mimics the HepMC dump.
118 // Particle print method called within here
119 void xAODTruthReader::printVertex(const xAOD::TruthVertex* vertex, bool do4momPtEtaPhi) {
120 std::ios::fmtflags f( cout.flags() );
121 if (vertex) {
122 cout << "TruthVertex:";
123 if (HepMC::uniqueID(vertex) != HepMC::UNDEFINED_ID) {
124 if (vertex->x() != 0.0 && vertex->y() != 0.0 && vertex->z() != 0.0) {
125 cout.width(9);
126 cout << HepMC::uniqueID(vertex);
127 cout << " Status:";
128 cout.width(5);
129 cout << HepMC::status(vertex);
130 cout << " (X,cT)=";
131 cout.width(9);
132 cout.precision(2);
133 cout.setf(ios::scientific, ios::floatfield);
134 cout.setf(ios_base::showpos);
135 cout << vertex->x() << ",";
136 cout.width(9);
137 cout.precision(2);
138 cout << vertex->y() << ",";
139 cout.width(9);
140 cout.precision(2);
141 cout << vertex->z() << ",";
142 cout.width(9);
143 cout.precision(2);
144 cout << vertex->t();
145 cout.setf(ios::fmtflags(0), ios::floatfield);
146 cout.unsetf(ios_base::showpos);
147 cout << endl;
148 } else {
149 cout.width(9);
150 cout << HepMC::uniqueID(vertex);
151 cout << " Status:";
152 cout.width(5);
153 cout << HepMC::status(vertex);
154 cout << " (X,cT): 0";
155 cout << endl;
156 }
157 } else {
158 // If the vertex doesn't have a unique ID assigned, then
159 // we print its memory address instead... so that the
160 // print out gives us a unique tag for the particle.
161 if (vertex->x() != 0.0 && vertex->y() != 0.0 && vertex->z() != 0.0) {
162 cout.width(9);
163 cout << (void*)vertex;
164 cout << " Status:";
165 cout.width(5);
166 cout << HepMC::status(vertex);
167 cout << " (X,cT)=";
168 cout.width(9);
169 cout.precision(2);
170 cout.setf(ios::scientific, ios::floatfield);
171 cout.setf(ios_base::showpos);
172 cout << vertex->x();
173 cout.width(9);
174 cout.precision(2);
175 cout << vertex->y();
176 cout.width(9);
177 cout.precision(2);
178 cout << vertex->z();
179 cout.width(9);
180 cout.precision(2);
181 cout << vertex->t();
182 cout.setf(ios::fmtflags(0), ios::floatfield);
183 cout.unsetf(ios_base::showpos);
184 cout << endl;
185 } else {
186 cout.width(9);
187 cout << (void*)vertex;
188 cout << " Status:";
189 cout.width(5);
190 cout << HepMC::status(vertex);
191 cout << " (X,cT):0";
192 cout << endl;
193 }
194 }
195 // Print out all the incoming, then outgoing particles
196 for (unsigned int iPIn = 0; iPIn<vertex->nIncomingParticles(); ++iPIn) {
197 if ( iPIn == 0 ) {
198 cout << " I: ";
199 cout.width(2);
200 cout << vertex->nIncomingParticles();
201 } else cout << " ";
202 printParticle(vertex->incomingParticle(iPIn), do4momPtEtaPhi);
203 }
204 for (unsigned int iPOut = 0; iPOut<vertex->nOutgoingParticles(); ++iPOut) {
205 if ( iPOut == 0 ) {
206 cout << " O: ";
207 cout.width(2);
208 cout << vertex->nOutgoingParticles();
209 } else cout << " ";
210 printParticle(vertex->outgoingParticle(iPOut), do4momPtEtaPhi);
211 }
212 }
213 cout.flags(f);
214 }
215
216
217 // Print method for particle - mimics the HepMC dump.
218 void xAODTruthReader::printParticle(const xAOD::TruthParticle* particle, bool do4momPtEtaPhi) {
219 std::ios::fmtflags f( cout.flags() );
220 if (particle) {
221 cout << " ";
222 cout.width(9);
223 cout << HepMC::uniqueID(particle);
224 cout.width(9);
225 cout << particle->pdgId() << " ";
226 cout.width(9);
227 cout.precision(2);
228 cout.setf(ios::scientific, ios::floatfield);
229 cout.setf(ios_base::showpos);
230 if (do4momPtEtaPhi) cout << particle->pt() << ",";
231 else cout << particle->px() << ",";
232 cout.width(9);
233 cout.precision(2);
234 if (do4momPtEtaPhi) cout << particle->eta() << ",";
235 else cout << particle->py() << ",";
236 cout.width(9);
237 cout.precision(2);
238 if (do4momPtEtaPhi) cout << particle->phi() << ",";
239 else cout << particle->pz() << ",";
240 cout.width(9);
241 cout.precision(2);
242 cout << particle->e() << " ";
243 cout.setf(ios::fmtflags(0), ios::floatfield);
244 cout.unsetf(ios_base::showpos);
245 if ( particle->hasDecayVtx() ) {
246 if ( HepMC::uniqueID(particle->decayVtx()) != HepMC::UNDEFINED_ID ) {
247 cout.width(3);
248 cout << particle->status() << " ";
249 cout.width(9);
250 cout << HepMC::uniqueID(particle->decayVtx());
251 }
252 } else {
253 cout.width(3);
254 cout << particle->status();
255 }
256 }
257 cout << endl;
258 cout.flags(f);
259 }
260
261
262} // namespace xAODReader
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
Helpers for checking error return status codes and reporting errors.
Handle class for reading from StoreGate.
An algorithm that can be simultaneously executed in multiple threads.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
virtual StatusCode initialize()
Function initialising the algorithm.
static void printEvent(const xAOD::TruthEventBase *evt, bool do4momPtEtaPhi)
SG::ReadHandleKey< xAOD::TruthEventContainer > m_xaodTruthEventContainerKey
The keys for the input xAOD truth containers.
xAODTruthReader(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
static void printParticle(const xAOD::TruthParticle *part, bool do4momPtEtaPhi)
Gaudi::Property< bool > m_do4momPtEtaPhi
Flag to printout in pt,eta,phi instead of px,py,pz.
static void printVertex(const xAOD::TruthVertex *vtx, bool do4momPtEtaPhi)
virtual StatusCode execute(const EventContext &ctx) const
Function executing the algorithm.
float z() const
Vertex longitudinal distance along the beam line form the origin.
float y() const
Vertex y displacement.
float x() const
Vertex x displacement.
int uniqueID(const T &p)
constexpr int UNDEFINED_ID
int status(const T &p)
STL namespace.
TruthEventBase_v1 TruthEventBase
Typedef to implementation.
TruthVertex_v1 TruthVertex
Typedef to implementation.
Definition TruthVertex.h:15
TruthEvent_v1 TruthEvent
Typedef to implementation.
Definition TruthEvent.h:17
TruthParticle_v1 TruthParticle
Typedef to implementation.