HepMC3 event record library
convert_example.cc

Utility to convert between different types of event records

// -*- 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"
#ifdef HEPMC3_ROOTIO
#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
#endif
#endif
#ifdef HEPMCCONVERT_EXTENSION_HEPEVTZEUS
#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<std::string,formats> format_map;
format_map.insert(std::pair<std::string,formats> ( "hepmc2", hepmc2 ));
format_map.insert(std::pair<std::string,formats> ( "hepmc3", hepmc3 ));
format_map.insert(std::pair<std::string,formats> ( "hpe", hpe ));
format_map.insert(std::pair<std::string,formats> ( "root", root ));
format_map.insert(std::pair<std::string,formats> ( "treeroot", treeroot ));
format_map.insert(std::pair<std::string,formats> ( "treerootopal", treerootopal ));
format_map.insert(std::pair<std::string,formats> ( "hpezeus", hpezeus ));
format_map.insert(std::pair<std::string,formats> ( "lhef", lhef ));
format_map.insert(std::pair<std::string,formats> ( "dump", dump ));
format_map.insert(std::pair<std::string,formats> ( "dot", dot ));
format_map.insert(std::pair<std::string,formats> ( "gz", gz ));
format_map.insert(std::pair<std::string,formats> ( "none", none ));
std::map<std::string, std::string> options;
for (size_t i=0; i<ai.extensions_given; i++)
{
std::string optarg=std::string(ai.extensions_arg[i]);
size_t pos=optarg.find_first_of('=');
if (pos<optarg.length())
options[std::string(optarg,0,pos)]=std::string(optarg,pos+1,optarg.length());
}
long int events_parsed = 0;
long int events_limit = ai.events_limit_arg;
long int first_event_number = ai.first_event_number_arg;
long int last_event_number = ai.last_event_number_arg;
long int print_each_events_parsed = ai.print_every_events_parsed_arg;
Reader* input_file=0;
bool ignore_writer=false;
switch (format_map.at(std::string(ai.input_format_arg)))
{
case hepmc2:
input_file=new ReaderAsciiHepMC2(ai.inputs[0]);
break;
case hepmc3:
input_file=new ReaderAscii(ai.inputs[0]);
break;
case hpe:
input_file=new ReaderHEPEVT(ai.inputs[0]);
break;
case lhef:
input_file=new ReaderLHEF(ai.inputs[0]);
break;
case gz:
#ifdef HEPMCCONVERT_EXTENSION_GZ
input_file=new ReaderGZ(ai.inputs[0]);
break;
#else
printf("Input format %s is not supported\n",ai.input_format_arg);
exit(2);
#endif
case treeroot:
#ifdef HEPMC3_ROOTIO
input_file=new ReaderRootTree(ai.inputs[0]);
break;
#else
printf("Input format %s is not supported\n",ai.input_format_arg);
exit(2);
#endif
case root:
#ifdef HEPMC3_ROOTIO
input_file=new ReaderRoot(ai.inputs[0]);
break;
#else
printf("Input format %s is not supported\n",ai.input_format_arg);
exit(2);
#endif
default:
printf("Input format %s is not known\n",ai.input_format_arg);
exit(2);
break;
}
Writer* output_file=0;
switch (format_map.at(std::string(ai.output_format_arg)))
{
case hepmc2:
output_file=new WriterAsciiHepMC2(ai.inputs[1]);
break;
case hepmc3:
output_file=new WriterAscii(ai.inputs[1]);
break;
case hpe:
output_file=new WriterHEPEVT(ai.inputs[1]);
break;
case root:
#ifdef HEPMC3_ROOTIO
output_file=new WriterRoot(ai.inputs[1]);
break;
#else
printf("Output format %s is not supported\n",ai.output_format_arg);
exit(2);
#endif
case treeroot:
#ifdef HEPMC3_ROOTIO
output_file=new WriterRootTree(ai.inputs[1]);
break;
#else
printf("Output format %s is not supported\n",ai.output_format_arg);
exit(2);
#endif
/* Extension example*/
case treerootopal:
#ifdef HEPMCCONVERT_EXTENSION_ROOTTREEOPAL
output_file=new WriterRootTreeOPAL(ai.inputs[1]);
((WriterRootTreeOPAL*)(output_file))->init_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()<first_event_number) continue;
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
{
}
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;
}
ReaderRoot.h
Definition of class ReaderRoot.
GenEvent.h
Definition of class GenEvent.
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition: WriterAscii.h:25
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:42
WriterDOT.h
Definition of class WriterDOT.
ReaderGZ.h
Definition of class ReaderGZ.
HepMC3::WriterAsciiHepMC2
GenEvent I/O serialization for structured text files.
Definition: WriterAsciiHepMC2.h:26
WriterRootTree.h
Definition of class WriterRootTree.
HepMC3
HepMC3 main namespace.
Definition: ReaderGZ.h:28
ReaderRootTree.h
Definition of class ReaderRootTree.
HepMC3::Print::content
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition: Print.cc:18
WriterRoot.h
Definition of class WriterRoot.
HepMC3::ReaderAscii
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
HepMC3::WriterDOT
GenEvent I/O output to dot files that should be processed by graphviz or other software.
Definition: WriterDOT.h:21
ReaderAsciiHepMC2.h
Definition of class ReaderAsciiHepMC2.
WriterAscii.h
Definition of class WriterAscii.
HepMC3::Reader
Base class for all I/O readers.
Definition: Reader.h:25
WriterHEPEVT.h
Definition of class WriterHEPEVT.
ReaderHEPEVT.h
Definition of class ReaderHEPEVT.
HepMC3::ReaderRoot
GenEvent I/O parsing and serialization for root files.
Definition: ReaderRoot.h:32
WriterHEPEVTZEUS.h
Definition of class WriterHEPEVTZEUS.
HepMC3::WriterHEPEVT
GenEvent I/O serialization for HEPEVT files.
Definition: WriterHEPEVT.h:27
HepMC3::ReaderLHEF
GenEvent I/O parsing and serialization for LHEF files.
Definition: ReaderLHEF.h:34
Print.h
Definition of static class Print.
HepMC3::Writer::close
virtual void close()=0
Close file and/or stream.
HepMC3::ReaderRootTree
GenEvent I/O parsing and serialization for root files based on root TTree.
Definition: ReaderRootTree.h:34
WriterAsciiHepMC2.h
Definition of class WriterAsciiHepMC2.
ReaderLHEF.h
Definition of class ReaderLHEF.
HepMC3::Reader::run_info
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Reader.h:39
HepMC3::Reader::failed
virtual bool failed()=0
Get file and/or stream error state.
HepMC3::ReaderAsciiHepMC2
Parser for HepMC2 I/O files.
Definition: ReaderAsciiHepMC2.h:30
HepMC3::ReaderHEPEVT
GenEvent I/O parsing and serialization for HEPEVT files.
Definition: ReaderHEPEVT.h:32
HepMC3::Writer::write_event
virtual void write_event(const GenEvent &evt)=0
Write event evt to output target.
ReaderAscii.h
Definition of class ReaderAscii.
HepMC3::Reader::close
virtual void close()=0
Close file and/or stream.
HepMC3::WriterRootTreeOPAL
GenEvent I/O output to files similar to these produced by OPAL software.
Definition: WriterRootTreeOPAL.h:18
HepMC3::Writer
Base class for all I/O writers.
Definition: Writer.h:25
HepMC3::ReaderGZ
GenEvent I/O parsing for structured text files compressed with gzip.
Definition: ReaderGZ.h:34
HepMC3::WriterHEPEVTZEUS
GenEvent I/O output to files readable by ZEUS software.
Definition: WriterHEPEVTZEUS.h:18
WriterRootTreeOPAL.h
Definition of class WriterRootTreeOPAL.
main
int main(int argc, char **argv)
Definition: rootIOTree_example_read.cc:23
HepMC3::WriterRootTree
GenEvent I/O serialization for root files based on root TTree.
Definition: WriterRootTree.h:38
HepMC3::WriterRoot
GenEvent I/O serialization for root files.
Definition: WriterRoot.h:35
HepMC3::Reader::read_event
virtual bool read_event(GenEvent &evt)=0
Fill next event from input into evt.