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" 24 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL 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 29 #include "WriterRootTreeOPAL.h" 32 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS 33 #include "WriterHEPEVTZEUS.h" 42 using namespace HepMC;
45 enum formats {hepmc2, hepmc3, hpe
50 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL 53 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS 66 void tokenize(
const std::string& str,
const std::string& delimiters , std::vector<std::string>& tokens)
69 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
71 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
73 while (std::string::npos != pos || std::string::npos != lastPos)
76 tokens.push_back(str.substr(lastPos, pos - lastPos));
78 lastPos = str.find_first_not_of(delimiters, pos);
80 pos = str.find_first_of(delimiters, lastPos);
86 int main(
int argc,
char** argv)
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]);
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;
103 tokenize(opts,
":",parsedoptions);
104 for ( std::vector<std::string>::iterator it=parsedoptions.begin(); it!=parsedoptions.end(); it++)
106 std::vector<std::string> parsedname;
108 tokenize(*it,
"=",parsedname);
109 if (parsedname.size()<2) {printf(
"Error parsing option/value pair: ->%s<-\n",it->c_str()); exit(2);};
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));
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);
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 ));
126 format_map.insert(std::pair<std::string,formats> (
"root", root ));
127 format_map.insert(std::pair<std::string,formats> (
"treeroot", treeroot ));
130 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL 131 format_map.insert(std::pair<std::string,formats> (
"treerootopal", treerootopal ));
133 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS 134 format_map.insert(std::pair<std::string,formats> (
"hpezeus", hpezeus ));
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" ));
142 extention_map.insert(std::pair<std::string,std::string> (
"root",
"root" ));
143 extention_map.insert(std::pair<std::string,std::string> (
"treeroot",
"root" ));
146 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL 147 extention_map.insert(std::pair<std::string,std::string> (
"treerootopal",
"root" ));
149 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS 150 extention_map.insert(std::pair<std::string,std::string> (
"hpezeus",
"zeusmc" ));
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]);
159 for (i=0; i<mode.size(); i++)
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);}
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])));
172 ( convert_list.back().first.substr(convert_list.back().first.find_last_of(
".") + 1) != extention_map.at(convert_formats.first))
174 (convert_list.back().second.substr(convert_list.back().second.find_last_of(
".") + 1) != extention_map.at(convert_formats.second))
176 { printf(
"The conversion mode=%s is not suitable for extensions of %s %s files\n",argv[1],argv[2],argv[3]); exit(1);}
178 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL 182 for (i=0; i<convert_list.size(); i++)
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;
195 switch (format_map.at(convert_formats.first))
211 input_file=
new ReaderRoot(convert_list[i].first);
215 printf(
"Input format %s is unknown.\n",convert_formats.first.c_str());
220 switch (format_map.at(convert_formats.second))
223 printf(
"WARNING: hepmc3 format will be used instead of hepmc2.\n");
224 output_file=
new WriterAscii(convert_list[i].second.c_str());
227 output_file=
new WriterAscii(convert_list[i].second.c_str());
234 output_file=
new WriterRoot(convert_list[i].second);
241 #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL 246 if (options.find(
"Run")!=options.end()) Run=options.at(
"Run").fint;
250 #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS 256 printf(
"Output format %s is unknown.\n",convert_formats.second.c_str());
260 while( !input_file->failed() )
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;
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;}
274 if (input_file) input_file->close();
276 output_file->close();
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 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.
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.
Parser for HepMC2 I/O files.