HepMC3 event record library
convert_example.cc
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/Reader.h"
14#include "HepMC3/ReaderAscii.h"
15#include "HepMC3/WriterAscii.h"
16#include "HepMC3/WriterHEPEVT.h"
17#include "HepMC3/WriterPlugin.h"
18#include "HepMC3/ReaderHEPEVT.h"
19#include "HepMC3/ReaderLHEF.h"
20#include "HepMC3/ReaderPlugin.h"
21#include "HepMC3/ReaderFactory.h"
22
23#ifdef HEPMC3_ROOTIO
24#include "HepMC3/ReaderRoot.h"
25#include "HepMC3/WriterRoot.h"
28#endif
29
30/* Extension example*/
31#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
32#ifndef HEPMC3_ROOTIO
33#warning "HEPMCCONVERT_EXTENSION_ROOTTREEOPAL requires compilation with of HepMC with ROOT, i.e. HEPMC3_ROOTIO.This extension will be disabled."
34#undef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
35#else
36#include "WriterRootTreeOPAL.h"
37#endif
38#endif
39#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
40#include "WriterHEPEVTZEUS.h"
41#endif
42#ifdef HEPMCCONVERT_EXTENSION_DOT
43#include "WriterDOT.h"
44#endif
45#ifdef HEPMCCONVERT_EXTENSION_GZ
46#include "ReaderGZ.h"
47#endif
48
49
50
51#include "cmdline.h"
52using namespace HepMC3;
53enum formats {autodetect, hepmc2, hepmc3, hpe ,root, treeroot ,treerootopal, hpezeus, lhef, dump, dot, gz, plugin, none};
54int main(int argc, char** argv)
55{
56 gengetopt_args_info ai;
57 if (cmdline_parser (argc, argv, &ai) != 0) {
58 exit(1);
59 }
60 if (ai.inputs_num!=2)
61 {
62 printf("Exactly two arguments are requred: the name of input and output files\n");
63 exit(1);
64 }
65 std::map<std::string,formats> format_map;
66 format_map.insert(std::pair<std::string,formats> ( "auto", autodetect ));
67 format_map.insert(std::pair<std::string,formats> ( "hepmc2", hepmc2 ));
68 format_map.insert(std::pair<std::string,formats> ( "hepmc3", hepmc3 ));
69 format_map.insert(std::pair<std::string,formats> ( "hpe", hpe ));
70 format_map.insert(std::pair<std::string,formats> ( "root", root ));
71 format_map.insert(std::pair<std::string,formats> ( "treeroot", treeroot ));
72 format_map.insert(std::pair<std::string,formats> ( "treerootopal", treerootopal ));
73 format_map.insert(std::pair<std::string,formats> ( "hpezeus", hpezeus ));
74 format_map.insert(std::pair<std::string,formats> ( "lhef", lhef ));
75 format_map.insert(std::pair<std::string,formats> ( "dump", dump ));
76 format_map.insert(std::pair<std::string,formats> ( "dot", dot ));
77 format_map.insert(std::pair<std::string,formats> ( "gz", gz ));
78 format_map.insert(std::pair<std::string,formats> ( "plugin", plugin ));
79 format_map.insert(std::pair<std::string,formats> ( "none", none ));
80 std::map<std::string, std::string> options;
81 for (size_t i=0; i<ai.extensions_given; i++)
82 {
83 std::string optarg=std::string(ai.extensions_arg[i]);
84 size_t pos=optarg.find_first_of('=');
85 if (pos<optarg.length())
86 options[std::string(optarg,0,pos)]=std::string(optarg,pos+1,optarg.length());
87 }
88 long int events_parsed = 0;
89 long int events_limit = ai.events_limit_arg;
90 long int first_event_number = ai.first_event_number_arg;
91 long int last_event_number = ai.last_event_number_arg;
92 long int print_each_events_parsed = ai.print_every_events_parsed_arg;
93 std::string InputPluginLibrary;
94 std::string InputPluginName;
95
96 std::string OutputPluginLibrary;
97 std::string OutputPluginName;
98
99 std::shared_ptr<Reader> input_file;
100 bool input_is_stdin=(std::string(ai.inputs[0])==std::string("-"));
101 if (input_is_stdin) std::ios_base::sync_with_stdio(false);
102 bool ignore_writer=false;
103 switch (format_map.at(std::string(ai.input_format_arg)))
104 {
105 case autodetect:
106 input_file=(input_is_stdin?deduce_reader(std::cin):deduce_reader(ai.inputs[0]));
107 if (!input_file)
108 {
109 input_is_stdin?printf("Input format detection for std input has failed\n"):printf("Input format detection for file %s has failed\n",ai.inputs[0]);
110 exit(2);
111 }
112 break;
113 case hepmc2:
114 input_file=(input_is_stdin?std::make_shared<ReaderAsciiHepMC2>(std::cin):std::make_shared<ReaderAsciiHepMC2>(ai.inputs[0]));
115 break;
116 case hepmc3:
117 input_file=(input_is_stdin?std::make_shared<ReaderAscii>(std::cin):std::make_shared<ReaderAscii>(ai.inputs[0]));
118 break;
119 case hpe:
120 input_file=(input_is_stdin?std::make_shared<ReaderHEPEVT>(std::cin):std::make_shared<ReaderHEPEVT>(ai.inputs[0]));
121 break;
122 case lhef:
123 input_file=(input_is_stdin?std::make_shared<ReaderLHEF>(std::cin):std::make_shared<ReaderLHEF>(ai.inputs[0]));
124 break;
125 case gz:
126#ifdef HEPMCCONVERT_EXTENSION_GZ
127 input_file=std::make_shared<ReaderGZ>(ai.inputs[0]);
128 break;
129#else
130 printf("Input format %s is not supported\n",ai.input_format_arg);
131 exit(2);
132#endif
133 case treeroot:
134#ifdef HEPMC3_ROOTIO
135 input_file=std::make_shared<ReaderRootTree>(ai.inputs[0]);
136 break;
137#else
138 printf("Input format %s is not supported\n",ai.input_format_arg);
139 exit(2);
140#endif
141 case root:
142#ifdef HEPMC3_ROOTIO
143 input_file=std::make_shared<ReaderRoot>(ai.inputs[0]);
144 break;
145#else
146 printf("Input format %s is not supported\n",ai.input_format_arg);
147 exit(2);
148#endif
149 case plugin:
150 if (options.find("InputPluginLibrary")==options.end()) { printf("InputPluginLibrary option required\n"); exit(2);} else InputPluginLibrary=options.at("InputPluginLibrary");
151 if (options.find("InputPluginName")==options.end()) { printf("InputPluginName option required\n"); exit(2);} else InputPluginName=options.at("InputPluginName");
152 input_file=std::make_shared<ReaderPlugin>(std::string(ai.inputs[0]),InputPluginLibrary,InputPluginName);
153 if (input_file->failed()) { printf("Plugin initialization failed\n"); exit(2);}
154 break;
155 default:
156 printf("Input format %s is not known\n",ai.input_format_arg);
157 exit(2);
158 break;
159 }
160 std::shared_ptr<Writer> output_file;
161 switch (format_map.at(std::string(ai.output_format_arg)))
162 {
163 case hepmc2:
164 output_file=std::make_shared<WriterAsciiHepMC2>(ai.inputs[1]);
165 break;
166 case hepmc3:
167 output_file=std::make_shared<WriterAscii>(ai.inputs[1]);
168 break;
169 case hpe:
170 output_file=std::make_shared<WriterHEPEVT>(ai.inputs[1]);
171 break;
172 case root:
173#ifdef HEPMC3_ROOTIO
174 output_file=std::make_shared<WriterRoot>(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 treeroot:
181#ifdef HEPMC3_ROOTIO
182 output_file=std::make_shared<WriterRootTree>(ai.inputs[1]);
183 break;
184#else
185 printf("Output format %s is not supported\n",ai.output_format_arg);
186 exit(2);
187#endif
188 /* Extension example*/
189 case treerootopal:
190#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
191 output_file=std::make_shared<WriterRootTreeOPAL>(ai.inputs[1]);
192 (std::dynamic_pointer_cast<WriterRootTreeOPAL>(output_file))->init_branches();
193 if (options.find("Run")!=options.end()) (std::dynamic_pointer_cast<WriterRootTreeOPAL>(output_file))->set_run_number(std::atoi(options.at("Run").c_str()));
194 break;
195#else
196 printf("Output format %s is not supported\n",ai.output_format_arg);
197 exit(2);
198 break;
199#endif
200 case hpezeus:
201#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
202 output_file=std::make_shared<WriterHEPEVTZEUS>(ai.inputs[1]);
203 break;
204#else
205 printf("Output format %s is not supported\n",ai.output_format_arg);
206 exit(2);
207#endif
208 case dot:
209#ifdef HEPMCCONVERT_EXTENSION_DOT
210 output_file=std::make_shared<WriterDOT>(ai.inputs[1]);
211 if (options.find("Style")!=options.end()) (std::dynamic_pointer_cast<WriterDOT>(output_file))->set_style(std::atoi(options.at("Style").c_str()));
212 break;
213#else
214 printf("Output format %s is not supported\n",ai.output_format_arg);
215 exit(2);
216 break;
217#endif
218 case plugin:
219 if (options.find("OutputPluginLibrary")==options.end()) { printf("OutputPluginLibrary option required, e.g. OutputPluginLibrary=libAnalysis.so\n"); exit(2);} else OutputPluginLibrary=options.at("OutputPluginLibrary");
220 if (options.find("OutputPluginName")==options.end()) { printf("OutputPluginName option required, e.g. OutputPluginName=newAnalysisExamplefile\n"); exit(2);} else OutputPluginName=options.at("OutputPluginName");
221 output_file=std::make_shared<WriterPlugin>(std::string(ai.inputs[1]),OutputPluginLibrary,OutputPluginName);
222 if (output_file->failed()) { printf("Plugin initialization failed\n"); exit(2);}
223 break;
224 case dump:
225 output_file=NULL;
226 break;
227 case none:
228 output_file=NULL;
229 ignore_writer=true;
230 break;
231 default:
232 printf("Output format %s is not known\n",ai.output_format_arg);
233 exit(2);
234 break;
235 }
236 while( !input_file->failed() )
237 {
238 GenEvent evt(Units::GEV,Units::MM);
239 input_file->read_event(evt);
240 if( input_file->failed() ) {
241 printf("End of file reached. Exit.\n");
242 break;
243 }
244 if (evt.event_number()<first_event_number) continue;
245 if (evt.event_number()>last_event_number) continue;
246 evt.set_run_info(input_file->run_info());
247 //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.
248 if (!ignore_writer)
249 {
250 if (output_file)
251 {
252 output_file->write_event(evt);
253 }
254 else
255 {
256 Print::content(evt);
257 }
258 }
259 evt.clear();
260 ++events_parsed;
261 if( events_parsed%print_each_events_parsed == 0 ) printf("Events parsed: %li\n",events_parsed);
262 if( events_parsed >= events_limit ) {
263 printf("Event limit reached:->events_parsed(%li) >= events_limit(%li)<-. Exit.\n",events_parsed , events_limit);
264 break;
265 }
266 }
267
268 if (input_file) input_file->close();
269 if (output_file) output_file->close();
270 cmdline_parser_free(&ai);
271 return EXIT_SUCCESS;
272}
Definition of class GenEvent.
Definition of static class Print.
Definition of class ReaderAsciiHepMC2.
Definition of class ReaderAscii.
Definition of class ReaderGZ.
Definition of class ReaderHEPEVT.
Definition of class ReaderLHEF.
Definition of class ReaderPlugin.
Definition of class ReaderRootTree.
Definition of class ReaderRoot.
Definition of interface Reader.
Definition of class WriterAsciiHepMC2.
Definition of class WriterAscii.
Definition of class WriterDOT.
Definition of class WriterHEPEVTZEUS.
Definition of class WriterHEPEVT.
Definition of class WriterPlugin.
Definition of class WriterRootTreeOPAL.
Definition of class WriterRootTree.
Definition of class WriterRoot.
Stores event-related information.
Definition: GenEvent.h:41
Parser for HepMC2 I/O files.
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
GenEvent I/O parsing and serialization for HEPEVT files.
Definition: ReaderHEPEVT.h:33
GenEvent I/O parsing and serialization for LHEF files.
Definition: ReaderLHEF.h:35
HepMC3 main namespace.
std::shared_ptr< Reader > deduce_reader(std::istream &stream)
THis function will deduce the type of input stream based on its content and will return appropriate R...
int main(int argc, char **argv)