// -*- C++ -*- // // This file is part of HepMC // Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details) // /// @example convert_example.cc /// @brief Utility to convert between different types of event records /// #include "HepMC3/Print.h" #include "HepMC3/GenEvent.h" #include "HepMC3/ReaderAsciiHepMC2.h" #include "HepMC3/WriterAsciiHepMC2.h" #include "HepMC3/ReaderAscii.h" #include "HepMC3/WriterAscii.h" #include "HepMC3/WriterHEPEVT.h" #include "HepMC3/ReaderHEPEVT.h" #include "HepMC3/ReaderLHEF.h" #ifdef HEPMC3_ROOTIO #include "HepMC3/ReaderRoot.h" #include "HepMC3/WriterRoot.h" #include "HepMC3/ReaderRootTree.h" #include "HepMC3/WriterRootTree.h" #endif /* Extension example*/ #ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL #ifndef HEPMC3_ROOTIO #warning "HEPMCCONVERT_EXTENSION_ROOTTREEOPAL requires compilation with of HepMC with ROOT, i.e. HEPMC3_ROOTIO.This extension will be disabled." #undef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL #else #include "WriterRootTreeOPAL.h" #endif #endif #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS #include "WriterHEPEVTZEUS.h" #endif #ifdef HEPMCCONVERT_EXTENSION_DOT #include "WriterDOT.h" #endif #ifdef HEPMCCONVERT_EXTENSION_GZ #include "ReaderGZ.h" #endif #include "cmdline.h" using namespace HepMC3; enum formats {hepmc2, hepmc3, hpe ,root, treeroot ,treerootopal, hpezeus, lhef, dump, dot, gz, none}; int main(int argc, char** argv) { gengetopt_args_info ai; if (cmdline_parser (argc, argv, &ai) != 0) { exit(1); } if (ai.inputs_num!=2) { printf("Exactly two arguments are requred: the name of input and output files\n"); exit(1); } std::map format_map; format_map.insert(std::pair ( "hepmc2", hepmc2 )); format_map.insert(std::pair ( "hepmc3", hepmc3 )); format_map.insert(std::pair ( "hpe", hpe )); format_map.insert(std::pair ( "root", root )); format_map.insert(std::pair ( "treeroot", treeroot )); format_map.insert(std::pair ( "treerootopal", treerootopal )); format_map.insert(std::pair ( "hpezeus", hpezeus )); format_map.insert(std::pair ( "lhef", lhef )); format_map.insert(std::pair ( "dump", dump )); format_map.insert(std::pair ( "dot", dot )); format_map.insert(std::pair ( "gz", gz )); format_map.insert(std::pair ( "none", none )); std::map options; for (size_t i=0; iinit_branches(); if (options.find("Run")!=options.end()) ((WriterRootTreeOPAL*)(output_file))->set_run_number(std::atoi(options.at("Run").c_str())); break; #else printf("Output format %s is not supported\n",ai.output_format_arg); exit(2); break; #endif case hpezeus: #ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS output_file=new WriterHEPEVTZEUS(ai.inputs[1]); break; #else printf("Output format %s is not supported\n",ai.output_format_arg); exit(2); #endif case dot: #ifdef HEPMCCONVERT_EXTENSION_DOT output_file=new WriterDOT(ai.inputs[1]); if (options.find("Style")!=options.end()) ((WriterDOT*)(output_file))->set_style(std::atoi(options.at("Style").c_str())); break; #else printf("Output format %s is not supported\n",ai.output_format_arg); exit(2); break; #endif case dump: output_file=NULL; break; case none: output_file=NULL; ignore_writer=true; break; default: printf("Output format %s is not known\n",ai.output_format_arg); exit(2); break; } while( !input_file->failed() ) { GenEvent evt(Units::GEV,Units::MM); input_file->read_event(evt); if( input_file->failed() ) { printf("End of file reached. Exit.\n"); break; } if (evt.event_number()last_event_number) continue; evt.set_run_info(input_file->run_info()); //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. if (!ignore_writer) if (output_file) { output_file->write_event(evt); } else { Print::content(evt); } evt.clear(); ++events_parsed; if( events_parsed%print_each_events_parsed == 0 ) printf("Events parsed: %li\n",events_parsed); if( events_parsed >= events_limit ) { printf("Event limit reached:->events_parsed(%li) >= events_limit(%li)<-. Exit.\n",events_parsed , events_limit); break; } } if (input_file) input_file->close(); if (output_file) output_file->close(); return EXIT_SUCCESS; }