ATLAS Offline Software
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"
11 #include "StoreGate/ReadHandle.h"
12 
13 
14 #include "xAODTruth/TruthEvent.h"
17 
22 
23 #include "xAODTruthReader.h"
24 
25 using namespace std;
26 
27 namespace 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()) {
52  ATH_MSG_INFO("Found " << m_xaodTruthEventContainerKey.key());
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 << " Barcode PDG ID ( pt, eta, phi, E ) Stat DecayVtx\n";
109  else cout << " Barcode PDG ID ( Px, Py, Pz, E ) Stat DecayVtx\n"; // FIXME barcode-based
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:";
124  if (vertex->x() != 0.0 && vertex->y() != 0.0 && vertex->z() != 0.0) {
125  cout.width(9);
126  cout << HepMC::barcode(vertex); // FIXME barcode-based
127  cout << " ID:";
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::barcode(vertex); // FIXME barcode-based
151  cout << " ID:";
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 barcode 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 << " ID:";
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 << " ID:";
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::barcode(particle); // FIXME barcode-based
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::barcode(particle->decayVtx()); // FIXME barcode-based
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
xAODReader::xAODTruthReader::printVertex
static void printVertex(const xAOD::TruthVertex *vtx, bool do4momPtEtaPhi)
Definition: xAODTruthReader.cxx:119
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
xAODReader::xAODTruthReader::execute
virtual StatusCode execute(const EventContext &ctx) const
Function executing the algorithm.
Definition: xAODTruthReader.cxx:46
xAODTruthReader.h
TruthPileupEvent.h
xAODReader::xAODTruthReader::m_xaodTruthEventContainerKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_xaodTruthEventContainerKey
The keys for the input xAOD truth containers.
Definition: xAODTruthReader.h:35
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
TruthPileupEventContainer.h
xAOD::TruthVertex_v1::y
float y() const
Vertex y displacement.
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
xAOD::TruthParticle_v1
Class describing a truth particle in the MC record.
Definition: TruthParticle_v1.h:37
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
xAODReader::xAODTruthReader::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: xAODTruthReader.cxx:35
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:135
xAODReader::xAODTruthReader::printParticle
static void printParticle(const xAOD::TruthParticle *part, bool do4momPtEtaPhi)
Definition: xAODTruthReader.cxx:218
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
HepMC::UNDEFINED_ID
constexpr int UNDEFINED_ID
Definition: MagicNumbers.h:56
xAOD::TruthEventBase_v1
Base class describing a pile-up or signal truth event in the MC record.
Definition: TruthEventBase_v1.h:36
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:37
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
MagicNumbers.h
errorcheck.h
Helpers for checking error return status codes and reporting errors.
xAODReader::xAODTruthReader::printEvent
static void printEvent(const xAOD::TruthEventBase *evt, bool do4momPtEtaPhi)
Definition: xAODTruthReader.cxx:103
xAOD::TruthVertex_v1::x
float x() const
Vertex x displacement.
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
xAOD::TruthVertex_v1::z
float z() const
Vertex longitudinal distance along the beam line form the origin.
TruthEventAuxContainer.h
TruthPileupEventAuxContainer.h
ReadHandle.h
Handle class for reading from StoreGate.
HepMC::status
int status(const T &p)
Definition: MagicNumbers.h:138
xAODReader::xAODTruthReader::m_do4momPtEtaPhi
Gaudi::Property< bool > m_do4momPtEtaPhi
Flag to printout in pt,eta,phi instead of px,py,pz.
Definition: xAODTruthReader.h:42
TruthEventContainer.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TruthEvent.h
xAODReader
Definition: BTaggingReader.cxx:21