ATLAS Offline Software
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"
11 #include "StoreGate/ReadHandle.h"
12 
14 #include "HepMCTruthReader.h"
16 
17 using std::cout;
18 using std::endl;
19 
20 HepMCTruthReader::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
30  ATH_MSG_DEBUG("HepMCContainerKey = " << m_hepMCContainerKey.key() );
31  return StatusCode::SUCCESS;
32 }
33 
34 
35 StatusCode 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
67 void 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 #ifdef HEPMC3
76  for (const auto& iv: event->vertices()) { printVertex(iv, do4momPtEtaPhi); }
77 #else
78  for (HepMC::GenEvent::vertex_const_iterator iv = event->vertices_begin(); iv != event->vertices_end(); ++iv) {
79  printVertex(*iv, do4momPtEtaPhi);
80  }
81 #endif
82  cout << "--------------------------------------------------------------------------------\n";
83 }
84 
85 // Print method for vertex - mimics the HepMC dump.
86 // Particle print method called within here
88  if (!vertex) return;
89  std::ios::fmtflags f( cout.flags() );
90  cout << "GenVertex (" << vertex << "):";
91  if (HepMC::barcode(vertex) != 0) {
92  if (vertex->position().x() != 0.0 && vertex->position().y() != 0.0 && vertex->position().z() != 0.0) {
93  cout.width(9);
94  cout <<HepMC::barcode(vertex);
95  cout << " ID:";
96  cout.width(5);
97  cout << vertex->id();
98  cout << " (X,cT)=";
99  cout.width(9);
100  cout.precision(2);
101  cout.setf(std::ios::scientific, std::ios::floatfield);
102  cout.setf(std::ios_base::showpos);
103  cout << vertex->position().x() << ",";
104  cout.width(9);
105  cout.precision(2);
106  cout << vertex->position().y() << ",";
107  cout.width(9);
108  cout.precision(2);
109  cout << vertex->position().z() << ",";
110  cout.width(9);
111  cout.precision(2);
112  cout << vertex->position().t();
113  cout.setf(std::ios::fmtflags(0), std::ios::floatfield);
114  cout.unsetf(std::ios_base::showpos);
115  cout << endl;
116  } else {
117  cout.width(9);
118  cout << HepMC::barcode(vertex);
119  cout << " ID:";
120  cout.width(5);
121  cout << vertex->id();
122  cout << " (X,cT): 0";
123  cout << endl;
124  }
125  } else {
126  // If the vertex doesn't have a unique barcode assigned, then
127  // we print its memory address instead... so that the
128  // print out gives us a unique tag for the particle.
129  if (vertex->position().x() != 0.0 && vertex->position().y() != 0.0 && vertex->position().z() != 0.0) {
130  cout.width(9);
131  cout << " ID:";
132  cout.width(5);
133  cout << vertex->id();
134  cout << " (X,cT)=";
135  cout.width(9);
136  cout.precision(2);
137  cout.setf(std::ios::scientific, std::ios::floatfield);
138  cout.setf(std::ios_base::showpos);
139  cout << vertex->position().x();
140  cout.width(9);
141  cout.precision(2);
142  cout << vertex->position().y();
143  cout.width(9);
144  cout.precision(2);
145  cout << vertex->position().z();
146  cout.width(9);
147  cout.precision(2);
148  cout << vertex->position().t();
149  cout.setf(std::ios::fmtflags(0), std::ios::floatfield);
150  cout.unsetf(std::ios_base::showpos);
151  cout << endl;
152  } else {
153  cout.width(9);
154  cout << " ID:";
155  cout.width(5);
156  cout << vertex->id();
157  cout << " (X,cT):0";
158  cout << endl;
159  }
160  }
161 
162  // Print out all the incoming, then outgoing particles
163 #ifdef HEPMC3
164  for (const auto& iPIn: vertex->particles_in()) {
165  if ( iPIn == vertex->particles_in().front() ) {
166  cout << " I: ";
167  cout.width(2);
168  cout << vertex->particles_in().size();
169  } else cout << " ";
170  printParticle(iPIn, do4momPtEtaPhi);
171  }
172  for (const auto& iPOut: vertex->particles_out()) {
173  if ( iPOut == vertex->particles_out().front()) {
174  cout << " O: ";
175  cout.width(2);
176  cout << vertex->particles_out().size();
177  } else cout << " ";
178  printParticle(iPOut, do4momPtEtaPhi);
179  }
180 #else
181  for (HepMC::GenVertex::particles_in_const_iterator iPIn = vertex->particles_in_const_begin();
182  iPIn != vertex->particles_in_const_end(); ++iPIn) {
183  if ( iPIn == vertex->particles_in_const_begin() ) {
184  cout << " I: ";
185  cout.width(2);
186  cout << vertex->particles_in_size();
187  } else cout << " ";
188  printParticle(*iPIn, do4momPtEtaPhi);
189  }
190  for (HepMC::GenVertex::particles_out_const_iterator iPOut = vertex->particles_out_const_begin();
191  iPOut != vertex->particles_out_const_end(); ++iPOut) {
192  if ( iPOut == vertex->particles_out_const_begin() ) {
193  cout << " O: ";
194  cout.width(2);
195  cout << vertex->particles_out_size();
196  } else cout << " ";
197  printParticle(*iPOut, do4momPtEtaPhi);
198  }
199 
200 #endif
201  cout.flags(f);
202 }
203 
204 
205 // Print method for particle - mimics the HepMC dump.
207  if (!particle) return;
208  std::ios::fmtflags f( cout.flags() );
209  cout << " ";
210  cout.width(9);
211  cout << HepMC::barcode(particle);
212  cout.width(9);
213  cout << particle->pdg_id() << " ";
214  cout.width(9);
215  cout.precision(2);
216  cout.setf(std::ios::scientific, std::ios::floatfield);
217  cout.setf(std::ios_base::showpos);
218  if (do4momPtEtaPhi) cout << particle->momentum().perp() << ",";
219  else cout << particle->momentum().px() << ",";
220  cout.width(9);
221  cout.precision(2);
222  if (do4momPtEtaPhi) cout << particle->momentum().pseudoRapidity() << ",";
223  else cout << particle->momentum().py() << ",";
224  cout.width(9);
225  cout.precision(2);
226  if (do4momPtEtaPhi) cout << particle->momentum().phi() << ",";
227  else cout << particle->momentum().pz() << ",";
228  cout.width(9);
229  cout.precision(2);
230  cout << particle->momentum().e() << " ";
231  cout.setf(std::ios::fmtflags(0), std::ios::floatfield);
232  cout.unsetf(std::ios_base::showpos);
233  if ( MC::isDecayed(particle) ) {
234  if ( HepMC::barcode(particle->end_vertex())!=0 ) {
235  cout.width(3);
236  cout << particle->status() << " ";
237  cout.width(9);
238  cout << HepMC::barcode(particle->end_vertex());
239  }
240  } else {
241  cout.width(3);
242  cout << particle->status();
243  }
244  cout << endl;
245  cout.flags(f);
246 }
247 
248 
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< McEventCollection >
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
HepMCTruthReader::printParticle
static void printParticle(const HepMC::ConstGenParticlePtr &part, bool do4momPtEtaPhi)
Definition: HepMCTruthReader.cxx:206
HepMCTruthReader.h
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
HepMCTruthReader::m_do4momPtEtaPhi
Gaudi::Property< bool > m_do4momPtEtaPhi
Flag to printout in pt,eta,phi instead of px,py,pz.
Definition: HepMCTruthReader.h:38
HepMCTruthReader::printEvent
static void printEvent(const HepMC::GenEvent *evt, bool do4momPtEtaPhi)
Definition: HepMCTruthReader.cxx:67
HepMCTruthReader::HepMCTruthReader
HepMCTruthReader(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
Definition: HepMCTruthReader.cxx:20
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
McEventCollection.h
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
HepMCTruthReader::m_hepMCContainerKey
SG::ReadHandleKey< McEventCollection > m_hepMCContainerKey
The key of the input HepMC truth container.
Definition: HepMCTruthReader.h:34
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
hist_file_dump.f
f
Definition: hist_file_dump.py:135
HepMCTruthReader::execute
virtual StatusCode execute(const EventContext &ctx) const
Function executing the algorithm.
Definition: HepMCTruthReader.cxx:35
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
HepMCTruthReader::initialize
virtual StatusCode initialize()
Function initialising the algorithm.
Definition: HepMCTruthReader.cxx:25
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
errorcheck.h
Helpers for checking error return status codes and reporting errors.
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
MC::isDecayed
bool isDecayed(const T &p)
Identify if the particle decayed.
Definition: HepMCHelpers.h:42
HepMCTruthReader::printVertex
static void printVertex(const HepMC::ConstGenVertexPtr &vtx, bool do4momPtEtaPhi)
Definition: HepMCTruthReader.cxx:87
HepMC::ConstGenVertexPtr
const HepMC::GenVertex * ConstGenVertexPtr
Definition: GenVertex.h:60
ReadHandle.h
Handle class for reading from StoreGate.
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
HepMCHelpers.h