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