-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhepmc-convert.cc
161 lines (140 loc) · 4.31 KB
/
hepmc-convert.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
// -*- C++ -*-
//
// 13-Jan-2021 WGS: a simple conversion program to go
// from one HepMC3 file format to another.
//
/**
* @example hepmc-convert.cc
* @brief Convert between HepMC3 file format.
*
* This is a simple HepMC3 file converter. See the README.md in this
* directory for more information.
*
*/
#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"
#include "HepMC3/Writer.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/WriterAsciiHepMC2.h"
#include "HepMC3/WriterHEPEVT.h"
#ifdef HEPMC3_ROOTIO_INSTALLED
#include "HepMC3/ReaderRoot.h"
#include "HepMC3/ReaderRootTree.h"
#include "HepMC3/WriterRoot.h"
#include "HepMC3/WriterRootTree.h"
#endif
#include <cstring>
#include <iostream>
#include <algorithm>
/** Main program */
int main(int argc, char **argv) {
if( argc<3 ) {
std::cout << "Usage: " << argv[0] << " <HepMC3_input_file> <HepMC3_output_file>" << std::endl;
exit(-1);
}
// Parse the input file name, looking for its extension.
std::string inputFile(argv[1]);
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);
}
// Parse the output file name, looking for its extension.
std::string outputFile(argv[2]);
search = outputFile.find('.');
if ( search == std::string::npos ) {
std::cerr
<< "No '.' in output file name, so I can't determine the HepMC3 file type."
<< std::endl;
exit(-1);
}
// Get the characters after the '.'.
if ( search + 2 < outputFile.size() )
extension = outputFile.substr( search+1 );
else {
std::cerr
<< "Nothing after the '.' in output 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::Writer* writer;
if ( extension == "hepmc2" )
writer = new HepMC3::WriterAsciiHepMC2(outputFile);
else if ( extension == "hepmc3" )
writer = new HepMC3::WriterAscii(outputFile);
else if ( extension == "hpe" )
writer = new HepMC3::WriterHEPEVT(outputFile);
#ifdef HEPMC3_ROOTIO_INSTALLED
else if ( extension == "root" )
writer = new HepMC3::WriterRoot(outputFile);
else if ( extension == "treeroot" )
writer = new HepMC3::WriterRootTree(outputFile);
#endif
else {
std::cerr
<< "Did not recognize extension '"
<< extension << "' as a valid HepMC3 output file type"
<< std::endl;
exit (-1);
}
int nevents = 0;
while( !reader->failed() ) {
HepMC3::GenEvent event;
// Read event from input file
reader->read_event(event);
// If reading failed - exit loop
if( reader->failed() ) break;
// Save event to output file
writer->write_event(event);
nevents++;
} // end of input?
std::cout << "Number of HepMC3 events converted = "
<< nevents << std::endl;
reader->close();
writer->close();
return 0;
}