Debian Codegraph @

examples/ConvertExample/convert_example.cc

raw
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
5 //
6 /// @example convert_example.cc
7 /// @brief Utility to convert between different types of event records
8 ///
9 #include "HepMC3/Print.h"
10 #include "HepMC3/GenEvent.h"
11 #include "HepMC3/ReaderAsciiHepMC2.h"
12 #include "HepMC3/WriterAsciiHepMC2.h"
13 #include "HepMC3/ReaderAscii.h"
14 #include "HepMC3/WriterAscii.h"
15 #include "HepMC3/WriterHEPEVT.h"
16 #include "HepMC3/ReaderHEPEVT.h"
17 #include "HepMC3/ReaderLHEF.h"
18
19 #ifdef HEPMC3_ROOTIO
20 #include "HepMC3/ReaderRoot.h"
21 #include "HepMC3/WriterRoot.h"
22 #include "HepMC3/ReaderRootTree.h"
23 #include "HepMC3/WriterRootTree.h"
24 #endif
25
26 /* Extension example*/
27 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
28 #ifndef HEPMC3_ROOTIO
29 #warning "HEPMCCONVERT_EXTENSION_ROOTTREEOPAL requires compilation with of HepMC with ROOT, i.e. HEPMC3_ROOTIO.This extension will be disabled."
30 #undef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
31 #else
32 #include "WriterRootTreeOPAL.h"
33 #endif
34 #endif
35 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
36 #include "WriterHEPEVTZEUS.h"
37 #endif
38 #ifdef HEPMCCONVERT_EXTENSION_DOT
39 #include "WriterDOT.h"
40 #endif
41 #ifdef HEPMCCONVERT_EXTENSION_GZ
42 #include "ReaderGZ.h"
43 #endif
44
45
46
47 #include "cmdline.h"
48 using namespace HepMC3;
49 enum formats {hepmc2, hepmc3, hpe ,root, treeroot ,treerootopal, hpezeus, lhef, dump, dot, gz, none};
50 int main(int argc, char** argv)
51 {
52 gengetopt_args_info ai;
53 if (cmdline_parser (argc, argv, &ai) != 0) {
54 exit(1);
55 }
56 if (ai.inputs_num!=2)
57 {
58 printf("Exactly two arguments are requred: the name of input and output files\n");
59 exit(1);
60 }
61 std::map<std::string,formats> format_map;
62 format_map.insert(std::pair<std::string,formats> ( "hepmc2", hepmc2 ));
63 format_map.insert(std::pair<std::string,formats> ( "hepmc3", hepmc3 ));
64 format_map.insert(std::pair<std::string,formats> ( "hpe", hpe ));
65 format_map.insert(std::pair<std::string,formats> ( "root", root ));
66 format_map.insert(std::pair<std::string,formats> ( "treeroot", treeroot ));
67 format_map.insert(std::pair<std::string,formats> ( "treerootopal", treerootopal ));
68 format_map.insert(std::pair<std::string,formats> ( "hpezeus", hpezeus ));
69 format_map.insert(std::pair<std::string,formats> ( "lhef", lhef ));
70 format_map.insert(std::pair<std::string,formats> ( "dump", dump ));
71 format_map.insert(std::pair<std::string,formats> ( "dot", dot ));
72 format_map.insert(std::pair<std::string,formats> ( "gz", gz ));
73 format_map.insert(std::pair<std::string,formats> ( "none", none ));
74 std::map<std::string, std::string> options;
75 for (size_t i=0; i<ai.extensions_given; i++)
76 {
77 std::string optarg=std::string(ai.extensions_arg[i]);
78 size_t pos=optarg.find_first_of('=');
79 if (pos<optarg.length())
80 options[std::string(optarg,0,pos)]=std::string(optarg,pos+1,optarg.length());
81 }
82 long int events_parsed = 0;
83 long int events_limit = ai.events_limit_arg;
84 long int first_event_number = ai.first_event_number_arg;
85 long int last_event_number = ai.last_event_number_arg;
86 long int print_each_events_parsed = ai.print_every_events_parsed_arg;
87 Reader* input_file=0;
88 bool ignore_writer=false;
89 switch (format_map.at(std::string(ai.input_format_arg)))
90 {
91 case hepmc2:
92 input_file=new ReaderAsciiHepMC2(ai.inputs[0]);
93 break;
94 case hepmc3:
95 input_file=new ReaderAscii(ai.inputs[0]);
96 break;
97 case hpe:
98 input_file=new ReaderHEPEVT(ai.inputs[0]);
99 break;
100 case lhef:
101 input_file=new ReaderLHEF(ai.inputs[0]);
102 break;
103 case gz:
104 #ifdef HEPMCCONVERT_EXTENSION_GZ
105 input_file=new ReaderGZ(ai.inputs[0]);
106 break;
107 #else
108 printf("Input format %s is not supported\n",ai.input_format_arg);
109 exit(2);
110 #endif
111 case treeroot:
112 #ifdef HEPMC3_ROOTIO
113 input_file=new ReaderRootTree(ai.inputs[0]);
114 break;
115 #else
116 printf("Input format %s is not supported\n",ai.input_format_arg);
117 exit(2);
118 #endif
119 case root:
120 #ifdef HEPMC3_ROOTIO
121 input_file=new ReaderRoot(ai.inputs[0]);
122 break;
123 #else
124 printf("Input format %s is not supported\n",ai.input_format_arg);
125 exit(2);
126 #endif
127 default:
128 printf("Input format %s is not known\n",ai.input_format_arg);
129 exit(2);
130 break;
131 }
132 Writer* output_file=0;
133 switch (format_map.at(std::string(ai.output_format_arg)))
134 {
135 case hepmc2:
136 output_file=new WriterAsciiHepMC2(ai.inputs[1]);
137 break;
138 case hepmc3:
139 output_file=new WriterAscii(ai.inputs[1]);
140 break;
141 case hpe:
142 output_file=new WriterHEPEVT(ai.inputs[1]);
143 break;
144 case root:
145 #ifdef HEPMC3_ROOTIO
146 output_file=new WriterRoot(ai.inputs[1]);
147 break;
148 #else
149 printf("Output format %s is not supported\n",ai.output_format_arg);
150 exit(2);
151 #endif
152 case treeroot:
153 #ifdef HEPMC3_ROOTIO
154 output_file=new WriterRootTree(ai.inputs[1]);
155 break;
156 #else
157 printf("Output format %s is not supported\n",ai.output_format_arg);
158 exit(2);
159 #endif
160 /* Extension example*/
161 case treerootopal:
162 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
163 output_file=new WriterRootTreeOPAL(ai.inputs[1]);
164 ((WriterRootTreeOPAL*)(output_file))->init_branches();
165 if (options.find("Run")!=options.end()) ((WriterRootTreeOPAL*)(output_file))->set_run_number(std::atoi(options.at("Run").c_str()));
166 break;
167 #else
168 printf("Output format %s is not supported\n",ai.output_format_arg);
169 exit(2);
170 break;
171 #endif
172 case hpezeus:
173 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
174 output_file=new WriterHEPEVTZEUS(ai.inputs[1]);
175 break;
176 #else
177 printf("Output format %s is not supported\n",ai.output_format_arg);
178 exit(2);
179 #endif
180 case dot:
181 #ifdef HEPMCCONVERT_EXTENSION_DOT
182 output_file=new WriterDOT(ai.inputs[1]);
183 if (options.find("Style")!=options.end()) ((WriterDOT*)(output_file))->set_style(std::atoi(options.at("Style").c_str()));
184 break;
185 #else
186 printf("Output format %s is not supported\n",ai.output_format_arg);
187 exit(2);
188 break;
189 #endif
190 case dump:
191 output_file=NULL;
192 break;
193 case none:
194 output_file=NULL;
195 ignore_writer=true;
196 break;
197 default:
198 printf("Output format %s is not known\n",ai.output_format_arg);
199 exit(2);
200 break;
201 }
202 while( !input_file->failed() )
203 {
204 GenEvent evt(Units::GEV,Units::MM);
205 input_file->read_event(evt);
206 if( input_file->failed() ) {
207 printf("End of file reached. Exit.\n");
208 break;
209 }
210 if (evt.event_number()<first_event_number) continue;
211 if (evt.event_number()>last_event_number) continue;
212 evt.set_run_info(input_file->run_info());
213 //Note the difference between ROOT and Ascii readers. The former read GenRunInfo before first event and the later at the same time as first event.
214 if (!ignore_writer)
215 if (output_file)
216 {
217 output_file->write_event(evt);
218 }
219 else
220 {
221 Print::content(evt);
222 }
223 evt.clear();
224 ++events_parsed;
225 if( events_parsed%print_each_events_parsed == 0 ) printf("Events parsed: %li\n",events_parsed);
226 if( events_parsed >= events_limit ) {
227 printf("Event limit reached:->events_parsed(%li) >= events_limit(%li)<-. Exit.\n",events_parsed , events_limit);
228 break;
229 }
230 }
231
232 if (input_file) input_file->close();
233 if (output_file) output_file->close();
234 return EXIT_SUCCESS;
235 }