-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhepmc-ntuple.cc
177 lines (150 loc) · 4.47 KB
/
hepmc-ntuple.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// -*- C++ -*-
//
// 06-Feb-2022 WGS: a simple conversion program to go
// from an HepMC3 file into a ROOT ntuple.
// The main purpose of this program is to debug the
// output of GramsSky.
//
/**
* @example hepmc-ntuple.cc
* @brief Convert from HepMC3 to ROOT..
*/
#include "HepMC3/Attribute.h"
#include "HepMC3/GenEvent.h"
#include "HepMC3/GenVertex.h"
#include "HepMC3/Reader.h"
#include "HepMC3/ReaderAscii.h"
#include "HepMC3/ReaderAsciiHepMC2.h"
#include "HepMC3/ReaderHEPEVT.h"
#include "HepMC3/ReaderLHEF.h"
#ifdef HEPMC3_ROOTIO_INSTALLED
#include "HepMC3/ReaderRoot.h"
#include "HepMC3/ReaderRootTree.h"
#endif
// ROOT includes
#include "TFile.h"
#include "TTree.h"
// C++ includes
#include <cstring>
#include <iostream>
#include <algorithm>
/** Main program */
int main(int argc, char **argv) {
// Crude argument parsing: <program> [input-file] [output-file].
std::string inputFile("gramssky.hepmc3");
std::string outputFile("hepmc-ntuple.root");
if( argc > 2 ) {
outputFile = std::string(argv[2]);
}
if( argc > 1 ) {
inputFile = std::string(argv[1]);
}
std::cout << argv[0] << ": Reading from '"
<< inputFile << "', Writing to '"
<< outputFile << "'" << std::endl;
// Parse the input file name, looking for its extension.
auto search = inputFile.find('.');
if ( search == std::string::npos ) {
std::cerr
<< "No '.' in input file name, so I can't determine the HepMC3 file type."
<< std::endl;
exit(-1);
}
// Get the characters after the '.'.
std::string extension;
if ( search + 2 < inputFile.size() )
extension = inputFile.substr( search+1 );
else {
std::cerr
<< "Nothing after the '.' in input file name, so I can't determine the HepMC3 file type."
<< std::endl;
exit(-1);
}
// Convert to lower case
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
HepMC3::Reader* reader;
if ( extension == "hepmc2" )
reader = new HepMC3::ReaderAsciiHepMC2(inputFile);
else if ( extension == "hepmc3" )
reader = new HepMC3::ReaderAscii(inputFile);
else if ( extension == "hpe" )
reader = new HepMC3::ReaderHEPEVT(inputFile);
else if ( extension == "lhef" )
reader = new HepMC3::ReaderLHEF(inputFile);
#ifdef HEPMC3_ROOTIO_INSTALLED
else if ( extension == "root" )
reader = new HepMC3::ReaderRoot(inputFile);
else if ( extension == "treeroot" )
reader = new HepMC3::ReaderRootTree(inputFile);
#endif
else {
std::cerr
<< "Did not recognize extension '"
<< extension << "' as a valid HepMC3 input file type"
<< std::endl;
exit (-1);
}
// Create a new TTree within the output file.
TFile* output = new TFile(outputFile.c_str(), "RECREATE");
TTree* ntuple = new TTree("hepmc","Values from HepMC3 file");
// Variables for ntuple.
int Event;
int pdgCode;
double x;
double y;
double z;
double t;
double Px;
double Py;
double Pz;
double energy;
// Define the columns of the ntuple.
ntuple->Branch("Event", &Event, "Event/I");
ntuple->Branch("PDG", &pdgCode, "PDG/I");
ntuple->Branch("x", &x, "x/D");
ntuple->Branch("y", &y, "y/D");
ntuple->Branch("z", &z, "z/D");
ntuple->Branch("t", &t, "t/D");
ntuple->Branch("Px", &Px, "Px/D");
ntuple->Branch("Py", &Py, "Py/D");
ntuple->Branch("Pz", &Pz, "Pz/D");
ntuple->Branch("E", &energy, "E/D");
int nevents = 0;
// For each event in the input file.
while( !reader->failed() ) {
HepMC3::GenEvent event;
// Read event from input file
reader->read_event(event);
// If reading failed - exit loop
if( reader->failed() ) break;
// Fetch the values from the HepMC record and create a row for the
// ntuple.
Event = event.event_number();
// For each vertex in the event:
for (auto vertex: event.vertices()) {
auto position = vertex->position();
x = position.x();
y = position.y();
z = position.z();
t = position.t();
// For each particle coming out of the vertex:
for (auto particle: vertex->particles_out()) {
pdgCode = particle->pid();
auto momentum = particle->momentum();
Px = momentum.px();
Py = momentum.py();
Pz = momentum.pz();
energy = momentum.e();
// Add the ntuple row.
ntuple->Fill();
}
}
nevents++;
} // end of input?
std::cout << "Number of HepMC3 events converted = "
<< nevents << std::endl;
reader->close();
ntuple->Write();
output->Close();
return 0;
}