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