21#include "HepMC3/ReaderFactory.h"
29#if HEPMC3_USE_COMPRESSION
35#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
37#warning "HEPMCCONVERT_EXTENSION_ROOTTREEOPAL requires compilation with of HepMC with ROOT, i.e. HEPMC3_ROOTIO.This extension will be disabled."
38#undef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
43#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
46#ifdef HEPMCCONVERT_EXTENSION_DOT
49#ifdef HEPMCCONVERT_EXTENSION_UPROOTTREEREADER
50#include "ReaderuprootTree.h"
56enum formats {autodetect, hepmc2, hepmc3, hpe,root, treeroot, treerootopal, hpezeus, lhef, dump, dot, uproot, plugin, none};
59std::shared_ptr<Reader> get_input_file(
const char* name,
const bool input_is_stdin,
const bool use_compression) {
61#if HEPMC3_USE_COMPRESSION
62 if (use_compression) {
66 return (input_is_stdin?std::make_shared<T>(std::cin):std::make_shared<T>(n));
69std::shared_ptr<Writer> get_output_file(
const char* name,
const char* use_compression) {
71#if HEPMC3_USE_COMPRESSION
72 if (std::string(use_compression) ==
"z" )
return std::make_shared< WriterGZ<T,Compression::z> >(n);
76 return std::make_shared<T>(n);
79int main(
int argc,
char** argv)
81 gengetopt_args_info ai;
82 if (cmdline_parser (argc, argv, &ai) != 0) {
85 if ( !( ( ai.inputs_num == 2 && ( std::string(ai.output_format_arg) !=
"none" )) || ( ai.inputs_num == 1 && ( std::string(ai.output_format_arg) ==
"none") ) ) )
87 printf(
"Exactly two arguments are requred: the name of input and output files if the output format in not \"none\"\n");
88 printf(
"In case the output format is \"none\" exactly one argument should be given: the name of input file.\n");
91 std::map<std::string,formats> format_map;
92 format_map.insert(std::pair<std::string,formats> (
"auto", autodetect ));
93 format_map.insert(std::pair<std::string,formats> (
"hepmc2", hepmc2 ));
94 format_map.insert(std::pair<std::string,formats> (
"hepmc3", hepmc3 ));
95 format_map.insert(std::pair<std::string,formats> (
"hpe", hpe ));
96 format_map.insert(std::pair<std::string,formats> (
"root", root ));
97 format_map.insert(std::pair<std::string,formats> (
"treeroot", treeroot ));
98 format_map.insert(std::pair<std::string,formats> (
"treerootopal", treerootopal ));
99 format_map.insert(std::pair<std::string,formats> (
"hpezeus", hpezeus ));
100 format_map.insert(std::pair<std::string,formats> (
"lhef", lhef ));
101 format_map.insert(std::pair<std::string,formats> (
"dump", dump ));
102 format_map.insert(std::pair<std::string,formats> (
"dot", dot ));
103 format_map.insert(std::pair<std::string,formats> (
"uproot", uproot ));
104 format_map.insert(std::pair<std::string,formats> (
"plugin", plugin ));
105 format_map.insert(std::pair<std::string,formats> (
"none", none ));
106 std::map<std::string, std::string> options;
107 for (
size_t i=0; i<ai.extensions_given; i++)
109 std::string optarg=std::string(ai.extensions_arg[i]);
110 size_t pos = optarg.find_first_of(
'=');
111 if ( pos < optarg.length() )
112 options[std::string(optarg,0,pos)] = std::string(optarg, pos+1, optarg.length());
114 long int events_parsed = 0;
115 long int events_limit = ai.events_limit_arg;
116 long int first_event_number = ai.first_event_number_arg;
117 long int last_event_number = ai.last_event_number_arg;
118 long int print_each_events_parsed = ai.print_every_events_parsed_arg;
119 std::string InputPluginLibrary;
120 std::string InputPluginName;
122 std::string OutputPluginLibrary;
123 std::string OutputPluginName;
125 std::shared_ptr<Reader> input_file;
126 bool input_is_stdin = (std::string(ai.inputs[0]) == std::string(
"-"));
127 if (input_is_stdin) std::ios_base::sync_with_stdio(
false);
128 bool ignore_writer =
false;
129 switch (format_map.at(std::string(ai.input_format_arg)))
135 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]);
140 input_file = get_input_file<ReaderAsciiHepMC2>(ai.inputs[0], input_is_stdin, ai.compressed_input_flag);
143 input_file = get_input_file<ReaderAscii>(ai.inputs[0], input_is_stdin, ai.compressed_input_flag);
146 input_file = get_input_file<ReaderHEPEVT>(ai.inputs[0], input_is_stdin,ai.compressed_input_flag);
149 input_file = get_input_file<ReaderLHEF>(ai.inputs[0], input_is_stdin, ai.compressed_input_flag);
152#ifdef HEPMCCONVERT_EXTENSION_UPROOTTREEREADER
153 input_file = std::make_shared<ReaderuprootTree>(ai.inputs[0]);
156 printf(
"Input format %s is not supported\n", ai.input_format_arg);
161 input_file = std::make_shared<ReaderRootTree>(ai.inputs[0]);
164 printf(
"Input format %s is not supported\n", ai.input_format_arg);
169 input_file = std::make_shared<ReaderRoot>(ai.inputs[0]);
172 printf(
"Input format %s is not supported\n", ai.input_format_arg);
176 if (options.find(
"InputPluginLibrary") == options.end()) {
177 printf(
"InputPluginLibrary option required\n");
180 else InputPluginLibrary = options.at(
"InputPluginLibrary");
181 if (options.find(
"InputPluginName") == options.end()) {
182 printf(
"InputPluginName option required\n");
185 else InputPluginName = options.at(
"InputPluginName");
186 input_file = std::make_shared<ReaderPlugin>(std::string(ai.inputs[0]), InputPluginLibrary, InputPluginName);
187 if (input_file->failed()) {
188 printf(
"Plugin initialization failed\n");
193 printf(
"Input format %s is not known\n", ai.input_format_arg);
197 std::shared_ptr<Writer> output_file;
198 switch (format_map.at(std::string(ai.output_format_arg)))
201 output_file = get_output_file<WriterAsciiHepMC2>(ai.inputs[1], ai.compressed_output_arg);
204 output_file = get_output_file<WriterAscii>(ai.inputs[1], ai.compressed_output_arg);
207 output_file = get_output_file<WriterHEPEVT>(ai.inputs[1], ai.compressed_output_arg);
211 output_file = std::make_shared<WriterRoot>(ai.inputs[1]);
214 printf(
"Output format %s is not supported\n", ai.output_format_arg);
219 output_file = std::make_shared<WriterRootTree>(ai.inputs[1]);
222 printf(
"Output format %s is not supported\n",ai.output_format_arg);
227#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
228 output_file = std::make_shared<WriterRootTreeOPAL>(ai.inputs[1]);
229 (std::dynamic_pointer_cast<WriterRootTreeOPAL>(output_file))->init_branches();
230 if (options.find(
"Run") != options.end()) (std::dynamic_pointer_cast<WriterRootTreeOPAL>(output_file))->set_run_number(std::atoi(options.at(
"Run").c_str()));
233 printf(
"Output format %s is not supported\n",ai.output_format_arg);
238#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
239 output_file = std::make_shared<WriterHEPEVTZEUS>(ai.inputs[1]);
242 printf(
"Output format %s is not supported\n",ai.output_format_arg);
246#ifdef HEPMCCONVERT_EXTENSION_DOT
247 output_file = std::make_shared<WriterDOT>(ai.inputs[1]);
248 if (options.find(
"Style") != options.end()) (std::dynamic_pointer_cast<WriterDOT>(output_file))->set_style(std::atoi(options.at(
"Style").c_str()));
251 printf(
"Output format %s is not supported\n",ai.output_format_arg);
256 if (options.find(
"OutputPluginLibrary") == options.end()) {
257 printf(
"OutputPluginLibrary option required, e.g. OutputPluginLibrary=libAnalysis.so\n");
260 else OutputPluginLibrary = options.at(
"OutputPluginLibrary");
261 if (options.find(
"OutputPluginName") == options.end()) {
262 printf(
"OutputPluginName option required, e.g. OutputPluginName=newAnalysisExamplefile\n");
265 else OutputPluginName = options.at(
"OutputPluginName");
266 output_file = std::make_shared<WriterPlugin>(std::string(ai.inputs[1]), OutputPluginLibrary, OutputPluginName);
267 if (output_file->failed()) {
268 printf(
"Plugin initialization failed\n");
277 ignore_writer =
true;
280 printf(
"Output format %s is not known\n", ai.output_format_arg);
284 while( !input_file->failed() )
286 GenEvent evt(Units::GEV, Units::MM);
287 input_file->read_event(evt);
288 if( input_file->failed() ) {
289 printf(
"End of file reached. Exit.\n");
292 if (evt.event_number() < first_event_number)
continue;
293 if (evt.event_number() > last_event_number)
continue;
294 evt.set_run_info(input_file->run_info());
300 output_file->write_event(evt);
309 if( events_parsed%print_each_events_parsed == 0 ) printf(
"Events parsed: %li\n", events_parsed);
310 if( events_parsed >= events_limit ) {
311 printf(
"Event limit reached:->events_parsed(%li) >= events_limit(%li)<-. Exit.\n", events_parsed, events_limit);
316 if (input_file) input_file->close();
317 if (output_file) output_file->close();
318 cmdline_parser_free(&ai);
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 WriterGZ.
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.
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
GenEvent I/O parsing for compressed files.
GenEvent I/O serialization for compressed files.
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...