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"
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 
39 #include "cmdline.h"
40 using namespace HepMC3;
41 enum formats {hepmc2, hepmc3, hpe ,root, treeroot ,treerootopal, hpezeus, lhef, dump};
42 int main(int argc, char** argv)
43 {
44  gengetopt_args_info ai;
45  if (cmdline_parser (argc, argv, &ai) != 0) {
46  exit(1);
47  }
48  if (ai.inputs_num!=2)
49  {
50  printf("Exactly two arguments are requred: the name of input and output files\n");
51  exit(1);
52  }
53  std::map<std::string,formats> format_map;
54  format_map.insert(std::pair<std::string,formats> ( "hepmc2", hepmc2 ));
55  format_map.insert(std::pair<std::string,formats> ( "hepmc3", hepmc3 ));
56  format_map.insert(std::pair<std::string,formats> ( "hpe", hpe ));
57  format_map.insert(std::pair<std::string,formats> ( "root", root ));
58  format_map.insert(std::pair<std::string,formats> ( "treeroot", treeroot ));
59  format_map.insert(std::pair<std::string,formats> ( "treerootopal", treerootopal ));
60  format_map.insert(std::pair<std::string,formats> ( "hpezeus", hpezeus ));
61  format_map.insert(std::pair<std::string,formats> ( "lhef", lhef ));
62  format_map.insert(std::pair<std::string,formats> ( "dump", dump ));
63  std::map<std::string, std::string> options;
64  for (size_t i=0; i<ai.extensions_given; i++)
65  {
66  std::string optarg=std::string(ai.extensions_arg[i]);
67  size_t pos=optarg.find_first_of('=');
68  if (pos<optarg.length())
69  options[std::string(optarg,0,pos)]=std::string(optarg,pos+1,optarg.length());
70  }
71  long int events_parsed = 0;
72  long int events_limit = ai.events_limit_arg;
73  long int first_event_number = ai.first_event_number_arg;
74  long int last_event_number = ai.last_event_number_arg;
75  long int print_each_events_parsed = ai.print_every_events_parsed_arg;
76  Reader* input_file=0;
77  switch (format_map.at(std::string(ai.input_format_arg)))
78  {
79  case hepmc2:
80  input_file=new ReaderAsciiHepMC2(ai.inputs[0]);
81  break;
82  case hepmc3:
83  input_file=new ReaderAscii(ai.inputs[0]);
84  break;
85  case hpe:
86  input_file=new ReaderHEPEVT(ai.inputs[0]);
87  break;
88  case lhef:
89  input_file=new ReaderLHEF(ai.inputs[0]);
90  break;
91  case treeroot:
92 #ifdef HEPMC3_ROOTIO
93  input_file=new ReaderRootTree(ai.inputs[0]);
94  break;
95 #else
96  printf("Input format %s is not supported\n",ai.input_format_arg);
97  exit(2);
98 #endif
99  case root:
100 #ifdef HEPMC3_ROOTIO
101  input_file=new ReaderRoot(ai.inputs[0]);
102  break;
103 #else
104  printf("Input format %s is not supported\n",ai.input_format_arg);
105  exit(2);
106 #endif
107  default:
108  printf("Input format %s is not known\n",ai.input_format_arg);
109  exit(2);
110  break;
111  }
112  Writer* output_file=0;
113  switch (format_map.at(std::string(ai.output_format_arg)))
114  {
115  case hepmc2:
116  output_file=new WriterAsciiHepMC2(ai.inputs[1]);
117  break;
118  case hepmc3:
119  output_file=new WriterAscii(ai.inputs[1]);
120  break;
121  case hpe:
122  output_file=new WriterHEPEVT(ai.inputs[1]);
123  break;
124  case root:
125 #ifdef HEPMC3_ROOTIO
126  output_file=new WriterRoot(ai.inputs[1]);
127  break;
128 #else
129  printf("Output format %s is not supported\n",ai.output_format_arg);
130  exit(2);
131 #endif
132  case treeroot:
133 #ifdef HEPMC3_ROOTIO
134  output_file=new WriterRootTree(ai.inputs[1]);
135  break;
136 #else
137  printf("Output format %s is not supported\n",ai.output_format_arg);
138  exit(2);
139 #endif
140  /* Extension example*/
141  case treerootopal:
142 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
143  output_file=new WriterRootTreeOPAL(ai.inputs[1]);
144  ((WriterRootTreeOPAL*)(output_file))->init_branches();
145  if (options.find("Run")!=options.end()) ((WriterRootTreeOPAL*)(output_file))->set_run_number(std::atoi(options.at("Run").c_str()));
146  break;
147 #else
148  printf("Output format %s is not supported\n",ai.output_format_arg);
149  exit(2);
150  break;
151 #endif
152  case hpezeus:
153 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
154  output_file=new WriterHEPEVTZEUS(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  case dump:
161  output_file=NULL;
162  break;
163  default:
164  printf("Output format %s is not known\n",ai.output_format_arg);
165  exit(2);
166  break;
167  }
168  while( !input_file->failed() )
169  {
170  GenEvent evt(Units::GEV,Units::MM);
171  input_file->read_event(evt);
172  if( input_file->failed() ) {
173  printf("End of file reached. Exit.\n");
174  break;
175  }
176  if (evt.event_number()<first_event_number) continue;
177  if (evt.event_number()>last_event_number) continue;
178  evt.set_run_info(input_file->run_info());
179  //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.
180  if (output_file) output_file->write_event(evt);
181  else Print::content(evt);
182  evt.clear();
183  ++events_parsed;
184  if( events_parsed%print_each_events_parsed == 0 ) printf("Events parsed: %i\n",events_parsed);
185  if( events_parsed >= events_limit ) {
186  printf("Event limit reached:->events_parsed(%i) >= events_limit(%i)<-. Exit.\n",events_parsed , events_limit);
187  break;
188  }
189  }
190 
191  if (input_file) input_file->close();
192  if (output_file) output_file->close();
193  return EXIT_SUCCESS;
194 }
GenEvent I/O serialization for structured text files.
GenEvent I/O parsing and serialization for LHEF files.
Definition: ReaderLHEF.h:34
Definition of class WriterHEPEVT.
Definition of class ReaderHEPEVT.
virtual void write_event(const GenEvent &evt)=0
Write event evt to output target.
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:26
virtual bool read_event(GenEvent &evt)=0
Fill next event from input into evt.
Definition of class WriterRootTree.
Definition of class WriterAscii.
GenEvent I/O parsing and serialization for HEPEVT files.
Definition: ReaderHEPEVT.h:28
Definition of class ReaderRootTree.
Definition of class ReaderRoot.
GenEvent I/O serialization for HEPEVT files.
Definition: WriterHEPEVT.h:27
Parser for HepMC2 I/O files.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Reader.h:37
Definition of class ReaderAsciiHepMC2.
Stores event-related information.
Definition: GenEvent.h:42
GenEvent I/O serialization for root files.
Definition: WriterRoot.h:35
GenEvent I/O serialization for root files based on root TTree.
Definition of class ReaderAscii.
Definition of class WriterAsciiHepMC2.
Definition of class WriterRoot.
GenEvent I/O parsing and serialization for root files based on root TTree.
int main(int argc, char **argv)
Base class for all I/O writers.
Definition: Writer.h:25
Definition of class GenEvent.
Base class for all I/O readers.
Definition: Reader.h:25
Definition of class ReaderLHEF.
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
GenEvent I/O parsing and serialization for root files.
Definition: ReaderRoot.h:32
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition: Print.cc:18