HepMC event record
convert_example.cc
1 // -*- C++ -*-
2 #define HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
3 #define HEPMCCONVERT_EXTENSION_HEPEVTZEUS
4 #include "HepMC/GenEvent.h"
5 #include "HepMC/Reader.h"
6 #include "HepMC/Writer.h"
7 #include "HepMC/ReaderAsciiHepMC2.h"
8 #include "HepMC/ReaderAscii.h"
9 #include "HepMC/WriterAscii.h"
10 #include "HepMC/Print.h"
11 #include "HepMC/GenEvent.h"
12 #include "HepMC/WriterHEPEVT.h"
13 #include "HepMC/ReaderHEPEVT.h"
14 
15 #ifdef HEPMC_ROOTIO
16 #include "HepMC/ReaderRoot.h"
17 #include "HepMC/WriterRoot.h"
18 #include "HepMC/ReaderRootTree.h"
19 #include "HepMC/WriterRootTree.h"
20 #endif
21 
22 /* Extension example*/
23 
24 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
25 #ifndef HEPMC_ROOTIO
26 #warning "HEPMCCONVERT_EXTENSION_ROOTTREEOPAL requires compilation with of HepMC with ROOT, i.e. HEPMC_ROOTIO.This extension will be disabled."
27 #undef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
28 #else
29 #include "WriterRootTreeOPAL.h"
30 #endif
31 #endif
32 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
33 #include "WriterHEPEVTZEUS.h"
34 #endif
35 
36 
37 #include <iostream>
38 #include <vector>
39 #include <string>
40 #include <limits>
41 
42 using namespace HepMC;
43 using std::cout;
44 using std::endl;
45 enum formats {hepmc2, hepmc3, hpe
46 #ifdef HEPMC_ROOTIO
47  ,root,treeroot
48 #endif
49  /* Extension example*/
50 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
51  ,treerootopal
52 #endif
53 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
54  ,hpezeus
55 #endif
56  };
57 
59 {
60  bool fbool;
61  int fint;
62  float ffloat;
63 };
64 
65 //http://stackoverflow.com/questions/599989/is-there-a-built-in-way-to-split-strings-in-c
66 void tokenize(const std::string& str, const std::string& delimiters , std::vector<std::string>& tokens)
67 {
68  // Skip delimiters at beginning.
69  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
70  // Find first "non-delimiter".
71  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
72 
73  while (std::string::npos != pos || std::string::npos != lastPos)
74  {
75  // Found a token, add it to the vector.
76  tokens.push_back(str.substr(lastPos, pos - lastPos));
77  // Skip delimiters. Note the "not_of"
78  lastPos = str.find_first_not_of(delimiters, pos);
79  // Find next "non-delimiter"
80  pos = str.find_first_of(delimiters, lastPos);
81  }
82 }
83 
84 
85 
86 int main(int argc, char** argv)
87 {
88  if (argc!=4&&argc!=5)
89  {
90  printf("\
91  Usage: %s <mode> <input_file.input_extension> <output_file.output_extension> [optional list of options]\n\
92  Supported modes are: hepmc2_root hepmc3_root etc.\n\
93  List of options should be given as option1=value1:option2=value2:option3=value3 ...\n",argv[0]);
94  exit(1);
95  }
96 
97  std::string opts="NONE";
98  if (argc==5) opts=std::string(argv[4]);
99  std::map<std::string, parsedoption> options;
100  std::vector<std::string> parsedoptions;
101  if (opts!="NONE")
102  {
103  tokenize(opts,":",parsedoptions);
104  for ( std::vector<std::string>::iterator it=parsedoptions.begin(); it!=parsedoptions.end(); it++)
105  {
106  std::vector<std::string> parsedname;
107  parsedoption o;
108  tokenize(*it,"=",parsedname);
109  if (parsedname.size()<2) {printf("Error parsing option/value pair: ->%s<-\n",it->c_str()); exit(2);};
110  int parsing_isok=0;
111  parsing_isok+=sscanf(parsedname[1].c_str(),"%i",&o.fint);
112  parsing_isok+=sscanf(parsedname[1].c_str(),"%f",&o.ffloat);
113  if (parsing_isok==0) {printf("Error converting value ->%s<- to int of float number.\n",parsedname[1].c_str()); exit(3);};
114  options.insert(std::pair<std::string, parsedoption>(parsedname[0],o));
115  }
116 
117  for (std::map<std::string, parsedoption>::iterator it=options.begin(); it!=options.end(); it++)
118  printf("%s %i %f\n",it->first.c_str(),it->second.fint,it->second.ffloat);
119  }
120 
121  std::map<std::string,formats> format_map;
122  format_map.insert(std::pair<std::string,formats> ( "hepmc2", hepmc2 ));
123  format_map.insert(std::pair<std::string,formats> ( "hepmc3", hepmc3 ));
124  format_map.insert(std::pair<std::string,formats> ( "hpe", hpe ));
125 #ifdef HEPMC_ROOTIO
126  format_map.insert(std::pair<std::string,formats> ( "root", root ));
127  format_map.insert(std::pair<std::string,formats> ( "treeroot", treeroot ));
128 #endif
129  /* Extension example*/
130 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
131  format_map.insert(std::pair<std::string,formats> ( "treerootopal", treerootopal ));
132 #endif
133 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
134  format_map.insert(std::pair<std::string,formats> ( "hpezeus", hpezeus ));
135 #endif
136 
137  std::map<std::string,std::string> extention_map;
138  extention_map.insert(std::pair<std::string,std::string> ( "hepmc2","hepmc2" ));
139  extention_map.insert(std::pair<std::string,std::string> ( "hepmc3", "hepmc3" ));
140  extention_map.insert(std::pair<std::string,std::string> ( "hpe", "hpe" ));
141 #ifdef HEPMC_ROOTIO
142  extention_map.insert(std::pair<std::string,std::string> ( "root", "root" ));
143  extention_map.insert(std::pair<std::string,std::string> ( "treeroot", "root" ));
144 #endif
145  /* Extension example*/
146 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
147  extention_map.insert(std::pair<std::string,std::string> ( "treerootopal", "root" ));
148 #endif
149 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
150  extention_map.insert(std::pair<std::string,std::string> ( "hpezeus", "zeusmc" ));
151 #endif
152 
153 
154 
155  std::vector<std::pair <std::string,std::string> > convert_list;
156  std::pair<std::string,std::string> convert_formats;
157  std::string mode(argv[1]);
158  unsigned int i,j=0;
159  for (i=0; i<mode.size(); i++)
160  if (mode[i]!='_')
161  {
162  if (j==0) convert_formats.first+=mode[i];
163  if (j==1) convert_formats.second+=mode[i];
164  if (j>1) {printf("Wrong mode string: %s\nMode string should be <format>_<format>.\n",argv[1]); exit(1);}
165  }
166  else j++;
167 
168  if (format_map.find(convert_formats.first )==format_map.end()) { printf("Input format %s is unknown.\n",convert_formats.first.c_str()); exit(2); }
169  if (format_map.find(convert_formats.second)==format_map.end()) { printf("Output format %s is unknown.\n",convert_formats.second.c_str()); exit(2); }
170  convert_list.push_back(std::pair<std::string,std::string>(std::string(argv[2]),std::string(argv[3])));
171  if (
172  ( convert_list.back().first.substr(convert_list.back().first.find_last_of(".") + 1) != extention_map.at(convert_formats.first))
173  ||
174  (convert_list.back().second.substr(convert_list.back().second.find_last_of(".") + 1) != extention_map.at(convert_formats.second))
175  )
176  { printf("The conversion mode=%s is not suitable for extensions of %s %s files\n",argv[1],argv[2],argv[3]); exit(1);}
177 
178 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
179  int Run=0;
180 #endif
181  /*So far this is size 1, but it is better to keep as an option for an extension*/
182  for (i=0; i<convert_list.size(); i++)
183  {
184  int events_parsed = 0;
185  int events_limit = std::numeric_limits<int>::max();
186  if (options.find("events_limit")!=options.end()) events_limit=options.at("events_limit").fint;
187  int first_event_number = -1;
188  if (options.find("first_event_number")!=options.end()) first_event_number=options.at("first_event_number").fint;
189  int last_event_number = std::numeric_limits<int>::max();
190  if (options.find("last_event_number")!=options.end()) last_event_number=options.at("last_event_number").fint;
191  int print_each_events_parsed=100;
192  if (options.find("print_each_events_parsed")!=options.end()) last_event_number=options.at("print_each_events_parsed").fint;
193  Reader* input_file=0;
194 
195  switch (format_map.at(convert_formats.first))
196  {
197  case hepmc2:
198  input_file=new ReaderAsciiHepMC2(convert_list[i].first);
199  break;
200  case hepmc3:
201  input_file=new ReaderAscii(convert_list[i].first);
202  break;
203  case hpe:
204  input_file=new ReaderHEPEVT(convert_list[i].first);
205  break;
206 #ifdef HEPMC_ROOTIO
207  case treeroot:
208  input_file=new ReaderRootTree(convert_list[i].first);
209  break;
210  case root:
211  input_file=new ReaderRoot(convert_list[i].first);
212  break;
213 #endif
214  default:
215  printf("Input format %s is unknown.\n",convert_formats.first.c_str());
216  exit(2);
217  break;
218  }
219  Writer* output_file=0;
220  switch (format_map.at(convert_formats.second))
221  {
222  case hepmc2:
223  printf("WARNING: hepmc3 format will be used instead of hepmc2.\n");
224  output_file=new WriterAscii(convert_list[i].second.c_str());
225  break;
226  case hepmc3:
227  output_file=new WriterAscii(convert_list[i].second.c_str());
228  break;
229  case hpe:
230  output_file=new WriterHEPEVT(convert_list[i].second);
231  break;
232 #ifdef HEPMC_ROOTIO
233  case root:
234  output_file=new WriterRoot(convert_list[i].second);
235  break;
236  case treeroot:
237  output_file=new WriterRootTree(convert_list[i].second);
238  break;
239 #endif
240  /* Extension example*/
241 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
242  case treerootopal:
243  output_file=new WriterRootTreeOPAL(convert_list[i].second);
244  ((WriterRootTreeOPAL*)(output_file))->init_branches();
245 
246  if (options.find("Run")!=options.end()) Run=options.at("Run").fint;
247  ((WriterRootTreeOPAL*)(output_file))->set_run_number(Run);
248  break;
249 #endif
250 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
251  case hpezeus:
252  output_file=new WriterHEPEVTZEUS(convert_list[i].second);
253  break;
254 #endif
255  default:
256  printf("Output format %s is unknown.\n",convert_formats.second.c_str());
257  exit(2);
258  break;
259  }
260  while( !input_file->failed() )
261  {
262  GenEvent evt(Units::GEV,Units::MM);
263  input_file->read_event(evt);
264  if( input_file->failed() ) {printf("End of file reached. Exit.\n"); break;}
265  if (evt.event_number()<first_event_number) continue;
266  if (evt.event_number()>last_event_number) continue;
267  output_file->write_event(evt);
268  evt.clear();
269  ++events_parsed;
270  if( events_parsed%print_each_events_parsed == 0 ) cout<<"Events parsed: "<<events_parsed<<endl;
271  if( events_parsed >= events_limit ) {printf("Event limit reached:->events_parsed(%i) >= events_limit(%i)<-. Exit.\n",events_parsed , events_limit); break;}
272  }
273 
274  if (input_file) input_file->close();
275  if (output_file)
276  output_file->close();
277  }
278  return 0;
279 }
GenEvent I/O serialization for HEPEVT files.
virtual void write_event(const GenEvent &evt)=0
Write event evt to output target.
Base class for all I/O readers.
GenEvent I/O parsing and serialization for root files.
Definition: ReaderRoot.h:32
Definition of class WriterRootTree.
Definition of class ReaderRootTree.
Definition of class ReaderRoot.
Stores event-related information.
GenEvent I/O parsing and serialization for HEPEVT files.
GenEvent I/O serialization for structured text files.
GenEvent I/O serialization for root files.
Definition: WriterRoot.h:31
GenEvent I/O serialization for root files based on root TTree.
Base class for all I/O writers.
Definition of class WriterRoot.
int main(int argc, char **argv)
Definition of template class SmartPointer.
GenEvent I/O parsing for structured text files.
virtual bool read_event(GenEvent &evt)=0
Fill next event from input into evt.