21#include "HepMC3/ReaderFactory.h"
29#if HEPMC3_USE_COMPRESSION
33#ifdef HEPMC3_PROTOBUFIO
39#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
41#warning "HEPMCCONVERT_EXTENSION_ROOTTREEOPAL requires compilation with of HepMC with ROOT, i.e. HEPMC3_ROOTIO.This extension will be disabled."
42#undef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
47#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
50#ifdef HEPMCCONVERT_EXTENSION_DOT
53#ifdef HEPMCCONVERT_EXTENSION_UPROOTTREEREADER
54#include "ReaderuprootTree.h"
60enum formats {autodetect, hepmc2, hepmc3, hpe,root, treeroot, treerootopal, hpezeus, lhef, dump, dot, uproot, plugin, none, proto};
63std::shared_ptr<Reader> get_input_file(
const char* name,
const bool input_is_stdin,
const bool use_compression) {
65#if HEPMC3_USE_COMPRESSION
66 if (use_compression) {
70 return (input_is_stdin?std::make_shared<T>(std::cin):std::make_shared<T>(n));
73std::shared_ptr<Writer> get_output_file(
const char* name,
const char* use_compression) {
75#if HEPMC3_USE_COMPRESSION
76 if (std::string(use_compression) ==
"z" )
return std::make_shared< WriterGZ<T,Compression::z> >(n);
80 return std::make_shared<T>(n);
83int main(
int argc,
char** argv)
85 gengetopt_args_info ai;
86 if (cmdline_parser (argc, argv, &ai) != 0) {
89 if ( !( ( ai.inputs_num == 2 && ( std::string(ai.output_format_arg) !=
"none" )) || ( ai.inputs_num == 1 && ( std::string(ai.output_format_arg) ==
"none") ) ) )
91 printf(
"Exactly two arguments are requred: the name of input and output files if the output format in not \"none\"\n");
92 printf(
"In case the output format is \"none\" exactly one argument should be given: the name of input file.\n");
95 std::map<std::string,formats> format_map;
96 format_map.insert(std::pair<std::string,formats> (
"auto", autodetect ));
97 format_map.insert(std::pair<std::string,formats> (
"hepmc2", hepmc2 ));
98 format_map.insert(std::pair<std::string,formats> (
"hepmc3", hepmc3 ));
99 format_map.insert(std::pair<std::string,formats> (
"hpe", hpe ));
100 format_map.insert(std::pair<std::string,formats> (
"root", root ));
101 format_map.insert(std::pair<std::string,formats> (
"treeroot", treeroot ));
102 format_map.insert(std::pair<std::string,formats> (
"treerootopal", treerootopal ));
103 format_map.insert(std::pair<std::string,formats> (
"hpezeus", hpezeus ));
104 format_map.insert(std::pair<std::string,formats> (
"lhef", lhef ));
105 format_map.insert(std::pair<std::string,formats> (
"dump", dump ));
106 format_map.insert(std::pair<std::string,formats> (
"dot", dot ));
107 format_map.insert(std::pair<std::string,formats> (
"uproot", uproot ));
108 format_map.insert(std::pair<std::string,formats> (
"plugin", plugin ));
109 format_map.insert(std::pair<std::string,formats> (
"none", none ));
110 format_map.insert(std::pair<std::string,formats> (
"proto", proto ));
111 std::map<std::string, std::string> options;
112 for (
size_t i=0; i<ai.extensions_given; i++)
114 std::string optarg=std::string(ai.extensions_arg[i]);
115 size_t pos = optarg.find_first_of(
'=');
116 if ( pos < optarg.length() ) {
117 options[std::string(optarg,0,pos)] = std::string(optarg, pos+1, optarg.length());
120 long int events_parsed = 0;
121 long int events_limit = ai.events_limit_arg;
122 long int first_event_number = ai.first_event_number_arg;
123 long int last_event_number = ai.last_event_number_arg;
124 long int print_each_events_parsed = ai.print_every_events_parsed_arg;
125 std::string InputPluginLibrary;
126 std::string InputPluginName;
128 std::string OutputPluginLibrary;
129 std::string OutputPluginName;
131 std::shared_ptr<Reader> input_file;
132 bool input_is_stdin = (std::string(ai.inputs[0]) == std::string(
"-"));
133 if (input_is_stdin) std::ios_base::sync_with_stdio(
false);
134 bool ignore_writer =
false;
135 switch (format_map.at(std::string(ai.input_format_arg)))
141 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]);
146 input_file = get_input_file<ReaderAsciiHepMC2>(ai.inputs[0], input_is_stdin, ai.compressed_input_flag);
149 input_file = get_input_file<ReaderAscii>(ai.inputs[0], input_is_stdin, ai.compressed_input_flag);
152 input_file = get_input_file<ReaderHEPEVT>(ai.inputs[0], input_is_stdin,ai.compressed_input_flag);
155 input_file = get_input_file<ReaderLHEF>(ai.inputs[0], input_is_stdin, ai.compressed_input_flag);
158#ifdef HEPMCCONVERT_EXTENSION_UPROOTTREEREADER
159 input_file = std::make_shared<ReaderuprootTree>(ai.inputs[0]);
162 printf(
"Input format %s is not supported\n", ai.input_format_arg);
167 input_file = std::make_shared<ReaderRootTree>(ai.inputs[0]);
170 printf(
"Input format %s is not supported\n", ai.input_format_arg);
175 input_file = std::make_shared<ReaderRoot>(ai.inputs[0]);
178 printf(
"Input format %s is not supported\n", ai.input_format_arg);
182#ifdef HEPMC3_PROTOBUFIO
183 input_file = std::make_shared<Readerprotobuf>(ai.inputs[0]);
186 printf(
"Input format %s is not supported\n", ai.input_format_arg);
190 if (options.find(
"InputPluginLibrary") == options.end()) {
191 printf(
"InputPluginLibrary option required\n");
194 else InputPluginLibrary = options.at(
"InputPluginLibrary");
195 if (options.find(
"InputPluginName") == options.end()) {
196 printf(
"InputPluginName option required\n");
199 else InputPluginName = options.at(
"InputPluginName");
200 input_file = std::make_shared<ReaderPlugin>(std::string(ai.inputs[0]), InputPluginLibrary, InputPluginName);
201 if (input_file->failed()) {
202 printf(
"Plugin initialization failed\n");
207 printf(
"Input format %s is not known\n", ai.input_format_arg);
211 std::shared_ptr<Writer> output_file;
212 switch (format_map.at(std::string(ai.output_format_arg)))
215 output_file = get_output_file<WriterAsciiHepMC2>(ai.inputs[1], ai.compressed_output_arg);
218 output_file = get_output_file<WriterAscii>(ai.inputs[1], ai.compressed_output_arg);
221 output_file = get_output_file<WriterHEPEVT>(ai.inputs[1], ai.compressed_output_arg);
225 output_file = std::make_shared<WriterRoot>(ai.inputs[1]);
228 printf(
"Output format %s is not supported\n", ai.output_format_arg);
232#ifdef HEPMC3_PROTOBUFIO
233 output_file = std::make_shared<Writerprotobuf>(ai.inputs[1]);
236 printf(
"Output format %s is not supported\n", ai.output_format_arg);
241 output_file = std::make_shared<WriterRootTree>(ai.inputs[1]);
244 printf(
"Output format %s is not supported\n",ai.output_format_arg);
249#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
250 output_file = std::make_shared<WriterRootTreeOPAL>(ai.inputs[1]);
251 (std::dynamic_pointer_cast<WriterRootTreeOPAL>(output_file))->init_branches();
252 if (options.find(
"Run") != options.end()) (std::dynamic_pointer_cast<WriterRootTreeOPAL>(output_file))->set_run_number(std::atoi(options.at(
"Run").c_str()));
255 printf(
"Output format %s is not supported\n",ai.output_format_arg);
260#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
261 output_file = std::make_shared<WriterHEPEVTZEUS>(ai.inputs[1]);
264 printf(
"Output format %s is not supported\n",ai.output_format_arg);
268#ifdef HEPMCCONVERT_EXTENSION_DOT
269 output_file = std::make_shared<WriterDOT>(ai.inputs[1]);
270 if (options.find(
"Style") != options.end()) (std::dynamic_pointer_cast<WriterDOT>(output_file))->set_style(std::atoi(options.at(
"Style").c_str()));
273 printf(
"Output format %s is not supported\n",ai.output_format_arg);
278 if (options.find(
"OutputPluginLibrary") == options.end()) {
279 printf(
"OutputPluginLibrary option required, e.g. OutputPluginLibrary=libAnalysis.so\n");
282 else OutputPluginLibrary = options.at(
"OutputPluginLibrary");
283 if (options.find(
"OutputPluginName") == options.end()) {
284 printf(
"OutputPluginName option required, e.g. OutputPluginName=newAnalysisExamplefile\n");
287 else OutputPluginName = options.at(
"OutputPluginName");
288 output_file = std::make_shared<WriterPlugin>(std::string(ai.inputs[1]), OutputPluginLibrary, OutputPluginName);
289 if (output_file->failed()) {
290 printf(
"Plugin initialization failed\n");
295 output_file =
nullptr;
298 output_file =
nullptr;
299 ignore_writer =
true;
302 printf(
"Output format %s is not known\n", ai.output_format_arg);
306 while( !input_file->failed() )
308 GenEvent evt(Units::GEV, Units::MM);
309 bool res_read = input_file->read_event(evt);
311 if( input_file->failed() ) {
312 printf(
"End of file reached. Exit.\n");
315 if ( !res_read && ai.strict_read_arg) {
316 printf(
"Broken event. Exit.\n");
319 if (evt.event_number() < first_event_number)
continue;
320 if (evt.event_number() > last_event_number)
continue;
321 evt.set_run_info(input_file->run_info());
327 output_file->write_event(evt);
336 if( events_parsed%print_each_events_parsed == 0 ) printf(
"Events parsed: %li\n", events_parsed);
337 if( events_parsed >= events_limit ) {
338 printf(
"Event limit reached:->events_parsed(%li) >= events_limit(%li)<-. Exit.\n", events_parsed, events_limit);
343 if (input_file) input_file->close();
344 if (output_file) output_file->close();
345 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 Readerprotobuf.
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.
Definition of class Writerprotobuf.
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...