ATLAS Offline Software
Loading...
Searching...
No Matches
PrintMC.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
5// PrintMC - dump details of MC events
6// Updated 29/10/2009 by Andy Buckley <andy.buckley@cern.ch>
7// Updated 18.06.2020 by <andrii.verbytskyi@mpp.mpg.de>
8#include "TruthIO/PrintMC.h"
11
12#include "AtlasHepMC/GenEvent.h"
14
15
16inline void drawLine(std::ostream& os) {
17 os << std::string(80, '_') << "\n" << std::endl;
18}
19
20
21PrintMC::PrintMC(const std::string& name, ISvcLocator* pSvcLocator)
22 : GenBase(name, pSvcLocator)
23{
24 // Declare JO interface
25 declareProperty("VerboseOutput", m_VerboseOutput=true, "Print out detailed information or not");
26 declareProperty("PrintStyle", m_printsty="Barcode", "Type of info to print out: 'Barcode' or 'Vertex'");
27 declareProperty("VertexInfo", m_vertexinfo=false, "Print out vertex information");
28 declareProperty("TrustHepMC", m_trustHepMC=false, "Use the event numbers in the HepMC record to choose the events to print");
29 declareProperty("FirstEvent", m_firstEvt=1, "Lowest event number to print");
30 declareProperty("LastEvent", m_lastEvt=100000000, "Highest event number to print");
31}
32
33
34StatusCode PrintMC::initialize() {
36 m_gendata = std::make_shared<GenData>();
37 if(!m_trustHepMC) ATH_CHECK(m_evtInfoKey.initialize());
38
39 // Check settings
40 if (m_printsty != "Barcode" && m_printsty != "Vertex") {
41 ATH_MSG_ERROR("Unknown PrintStyle: " << m_printsty << "! Choose Barcode/Vertex");
42 return StatusCode::FAILURE;
43 }
44
45 // Check that last > first
47
48 m_gendata = std::make_shared<GenData>();
49 return StatusCode::SUCCESS;
50}
51
52
53
55StatusCode PrintMC::execute(const EventContext& ctx) {
56 // If output already turned off by passing last dumped event, just return
58 if (!m_VerboseOutput) return StatusCode::SUCCESS;
59
60 // Loop over all events in McEventCollection
61
62 for (const HepMC::GenEvent* evt : *events_const(ctx)) {
63
64 // Get event number from HepMC
65 uint64_t evtnum = std::max(0,evt->event_number());
66 // Override with evtnum from Athena if enabled and functional
67 if (!m_trustHepMC) {
69 evtnum = evtInfo->eventNumber();
70 }
71
72 // Return immediately if event number is outside the print range
73 if (evtnum < m_firstEvt) {
74 return StatusCode::SUCCESS;
75 }
76 if (evtnum > m_lastEvt) {
78 m_VerboseOutput = false;
79 return StatusCode::SUCCESS;
80 }
81
82 // Printout header
83 ATH_MSG_INFO(">>> PrintMC from execute");
84
85 // VERTEX format
87 if (m_VerboseOutput && m_printsty == "Vertex") {
88 HepMC::Print::line(std::cout, *evt); // standard HepMC dump of vertex list
89 }
90 // BARCODE format
92 else if (m_VerboseOutput && m_printsty == "Barcode") {
93 drawLine(std::cout);
94
95 std::cout << "GenEvent: #" << evt->event_number()
96 << " ID=" << HepMC::signal_process_id(evt)
97 << " SignalProcessGenVertex Barcode: "
99 std::cout << " Entries this event: " << evt->vertices_size() << " vertices, "
100 << evt->particles_size() << " particles.\n";
101 if (evt->heavy_ion()) {
102 std::cout << " HeavyIon: jatt=" << evt->heavy_ion()->Ncoll_hard
103 << " np=" << evt->heavy_ion()->Npart_proj
104 << " nt=" << evt->heavy_ion()->Npart_targ
105 << " ncoll=" << evt->heavy_ion()->Ncoll
106 << " specn=" << evt->heavy_ion()->spectator_neutrons
107 << " specp=" << evt->heavy_ion()->spectator_protons
108 << " n01=" << evt->heavy_ion()->N_Nwounded_collisions
109 << " n10=" << evt->heavy_ion()->Nwounded_N_collisions
110 << " n11=" << evt->heavy_ion()->Nwounded_Nwounded_collisions
111 << " impact=" << evt->heavy_ion()->impact_parameter
112 << " evplane=" << evt->heavy_ion()->event_plane_angle
113 << " ecc=" << evt->heavy_ion()->eccentricity
114 << " sigmaNNinel=" << evt->heavy_ion()->sigma_inel_NN
115 << std::endl;
116 }
117 else {
118 std::cout << "HeavyIon: EMPTY"
119 << std::endl;
120 }
121
122
123 // Weights
124 std::cout << " Weights(" << evt->weights().size() << ")=";
125 for ( auto wgt = evt->weights().begin();
126 wgt != evt->weights().end(); wgt++ ) { std::cout << *wgt << " "; }
127 std::cout << "\n";
128 std::cout << " EventScale " << (evt->attribute<HepMC3::DoubleAttribute>(HepMCStr::event_scale)? evt->attribute<HepMC3::DoubleAttribute>(HepMCStr::event_scale)->value():0.0)
129 << " [energy] \t alphaQCD=" << (evt->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQCD)? evt->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQCD)->value():0.0)
130 << "\t alphaQED=" << (evt->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQED)? evt->attribute<HepMC3::DoubleAttribute>(HepMCStr::alphaQED)->value():0.0) << std::endl;
131
132 if (evt->pdf_info()) {
133 std::cout << "PdfInfo: id1=" << evt->pdf_info()->parton_id[0]
134 << " id2=" << evt->pdf_info()->parton_id[1]
135 << " x1=" << evt->pdf_info()->x[0]
136 << " x2=" << evt->pdf_info()->x[1]
137 << " q=" << evt->pdf_info()->scale
138 << " xpdf1=" << evt->pdf_info()->pdf_id[0]
139 << " xpdf2=" << evt->pdf_info()->pdf_id[1]
140 << std::endl;
141 }
142 else {
143 std::cout << "PdfInfo: EMPTY"
144 << std::endl;
145 }
146
147 // Print a legend to describe the particle info
148 char particle_legend[120];
149 sprintf( particle_legend," %9s %8s %-15s %4s %8s %8s (%9s,%9s,%9s,%9s,%9s)",
150 "Barcode","PDG ID","Name","Stat","ProdVtx","DecayVtx","Px","Py","Pz","E ","m");
151 std::cout << std::endl;
152 std::cout << " GenParticle Legend\n" << particle_legend << "\n";
153 if (m_vertexinfo) {
154 sprintf( particle_legend," %60s (%9s,%9s,%9s,%9s)"," ","Vx","Vy","Vz","Vct ");
155 std::cout << particle_legend << std::endl;
156 }
157 drawLine(std::cout);
158
159 // Print all particles
160 for (auto p: *evt) {
161 int p_bcode = HepMC::barcode(p);
162 int p_pdg_id = p->pdg_id();
163 HepMC::FourVector mom=p->momentum();
164 int p_stat = p->status();
165 int p_prodvtx = p->production_vertex()?HepMC::barcode(p->production_vertex()):0;
166 int p_endvtx = p->end_vertex()?HepMC::barcode(p->end_vertex()):0;
167 HepMC::FourVector prodvtx=p->production_vertex()?p->production_vertex()->position():HepMC::FourVector(0.0,0.0,0.0,0.0);
168 // Access the PDG table to get the particle name (and mass?)
169 std::string sname;
170 double p_mass = p->generated_mass();
171 auto ap = m_gendata->particleMass(std::abs(p_pdg_id));
172 if (!ap) {
173 ATH_MSG_DEBUG("PID " << std::abs(p_pdg_id) << " is not in particle data table");
174 } else {
175 const double p_charge = MC::charge(p_pdg_id);
176 // Build particle name string
177 sname = m_gendata->particleName(std::abs(p_pdg_id)).value();
178 if (p_charge < 0) {
179 const size_t plusidx = sname.rfind("+");
180 if (plusidx != std::string::npos) {
181 sname.replace(plusidx, 1, "-");
182 }
183 }
184 }
185 // PDG table missing or exceptions
186 // quarks & gluons
187 if (p_pdg_id == 21) sname="g";
188 else if (p_pdg_id == 1) sname="d";
189 else if (p_pdg_id == -1) sname="d~";
190 else if (p_pdg_id == 2) sname="u";
191 else if (p_pdg_id == -2) sname="u~";
192 else if (p_pdg_id == 3) sname="s";
193 else if (p_pdg_id == -3) sname="s~";
194 else if (p_pdg_id == 4) sname="c";
195 else if (p_pdg_id == -4) sname="c~";
196 else if (p_pdg_id == 5) sname="b";
197 else if (p_pdg_id == -5) sname="b~";
198 else if (p_pdg_id == 6) sname="t";
199 else if (p_pdg_id == -6) sname="t~";
200 // shower-specific
201 else if (p_pdg_id == 91) sname="cluster";
202 else if (p_pdg_id == 92) sname="string";
203 else if (p_pdg_id == 9922212) sname="remn";
204 else if (p_pdg_id == 2101) sname="ud";
205 else if (p_pdg_id == 2203) sname="uu";
206
207 // Calc mass if missing
209 if (p_mass == 0 && (p_stat == 2 || (p_stat != 1 && p_pdg_id != 22))) {
210 p_mass = mom.m();
211 }
212
213 const char* p_name = sname.c_str() ;
214 char particle_entries[120];
215 sprintf(particle_entries, " %9i %8i %-15s %4i %8i %8i (%+9.3g,%+9.3g,%+9.3g,%+9.3g,%9.3g)",
216 p_bcode, p_pdg_id, p_name, p_stat, p_prodvtx, p_endvtx, mom.px(), mom.py(),mom.pz(), mom.e(), p_mass);
217 std::cout << particle_entries << "\n";
218 if (m_vertexinfo) {
219 sprintf(particle_entries," %60s (%+9.3g,%+9.3g,%+9.3g,%+9.3g)"," ",prodvtx.x(), prodvtx.y(),prodvtx.z(), prodvtx.t());
220 std::cout << particle_entries << "\n";
221 }
222 }
223
224 }
225
226 }
227 // End of execution for each event
228 return StatusCode::SUCCESS;
229}
#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)
#define CHECK(...)
Evaluate an expression and check for errors.
ATLAS-specific HepMC functions.
void drawLine(std::ostream &os)
Definition PrintMC.cxx:16
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
virtual StatusCode initialize() override
Definition GenBase.cxx:17
GenBase(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Definition GenBase.cxx:11
const McEventCollection * events_const(const EventContext &ctx) const
Access the current event's McEventCollection (const).
Definition GenBase.h:95
bool m_vertexinfo
Definition PrintMC.h:31
std::shared_ptr< GenData > m_gendata
Definition PrintMC.h:35
SG::ReadHandleKey< xAOD::EventInfo > m_evtInfoKey
Definition PrintMC.h:23
std::string m_printsty
Definition PrintMC.h:30
virtual StatusCode initialize() override
Definition PrintMC.cxx:34
bool m_trustHepMC
Definition PrintMC.h:34
PrintMC(const std::string &name, ISvcLocator *pSvcLocator)
Definition PrintMC.cxx:21
uint64_t m_firstEvt
Definition PrintMC.h:32
bool m_VerboseOutput
Definition PrintMC.h:29
virtual StatusCode execute(const EventContext &ctx) override
Definition PrintMC.cxx:55
uint64_t m_lastEvt
Definition PrintMC.h:33
const std::string event_scale
const std::string alphaQED
const std::string alphaQCD
int signal_process_id(const GenEvent &evt)
Definition GenEvent.h:572
int barcode(const T *p)
Definition Barcode.h:15
HepMC3::FourVector FourVector
ConstGenVertexPtr signal_process_vertex(const GenEvent *e)
Definition GenEvent.h:597
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39
double charge(const T &p)