HepMC3 event record library
HepMC3_fileIO_example.cc

Test of file I/OParses HepMC3 file and saves it as a new HepMC3 file. The resulting file should be an exact copy of the input file

// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
//
/**
* @example HepMC3_fileIO_example.cc
* @brief Test of file I/O
*
* Parses HepMC3 file and saves it as a new HepMC3 file.
* The resulting file should be an exact copy of the input file
*
*/
#include "HepMC3/Print.h"
#include <iostream>
using namespace HepMC3;
/** Main program */
int main(int argc, char **argv) {
if( argc<3 ) {
std::cout << "Usage: " << argv[0] << " <HepMC3_input_file> <output_file>" << std::endl;
exit(-1);
}
ReaderAscii input_file (argv[1]);
WriterAscii output_file(argv[2]);
int events_parsed = 0;
while(!input_file.failed()) {
GenEvent evt(Units::GEV,Units::MM);
// Read event from input file
input_file.read_event(evt);
// If reading failed - exit loop
if( input_file.failed() ) break;
// Save event to output file
output_file.write_event(evt);
if(events_parsed==0) {
std::cout << " First event: " << std::endl;
std::cout << " Testing attribute reading for the first event: " << std::endl;
std::shared_ptr<GenCrossSection> cs = evt.attribute<GenCrossSection>("GenCrossSection");
std::shared_ptr<GenHeavyIon> hi = evt.attribute<GenHeavyIon>("GenHeavyIon");
std::shared_ptr<GenPdfInfo> pi = evt.attribute<GenPdfInfo>("GenPdfInfo");
if(cs) {
std::cout << " Has GenCrossSection: ";
}
else std::cout << " No GenCrossSection " << std::endl;
if(pi) {
std::cout << " Has GenPdfInfo: ";
}
else std::cout << " No GenPdfInfo " << std::endl;
if(hi) {
std::cout << " Has GenHeavyIon: ";
}
else std::cout << " No GenHeavyIon " << std::endl;
}
++events_parsed;
if( events_parsed%100 == 0 ) std::cout<<"Events parsed: "<<events_parsed<<std::endl;
}
input_file.close();
output_file.close();
return 0;
}
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:41
HepMC3::GenPdfInfo
Stores additional information about PDFs.
Definition: GenPdfInfo.h:32
HepMC3
HepMC3 main namespace.
Definition: AnalysisExample.h:18
HepMC3::Print::content
static void content(std::ostream &os, const GenEvent &event)
Print content of all GenEvent containers.
Definition: Print.cc:17
HepMC3::ReaderAscii
GenEvent I/O parsing for structured text files.
Definition: ReaderAscii.h:29
HepMC3::Print::listing
static void listing(std::ostream &os, const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:50
WriterAscii.h
Definition of class WriterAscii.
HepMC3::ReaderAscii::failed
bool failed() override
Return status of the stream.
Definition: ReaderAscii.cc:553
Print.h
Definition of static class Print.
HepMC3::GenCrossSection
Stores additional information about cross-section.
Definition: GenCrossSection.h:42
ReaderAscii.h
Definition of class ReaderAscii.
HepMC3::ReaderAscii::close
void close() override
Close file stream.
Definition: ReaderAscii.cc:555
HepMC3::ReaderAscii::read_event
bool read_event(GenEvent &evt) override
Load event from file.
Definition: ReaderAscii.cc:63
main
int main(int argc, char **argv)
Definition: rootIOTree_example_read.cc:23
HepMC3::WriterAscii::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition: WriterAscii.cc:63
HepMC3::GenHeavyIon
Stores additional information about Heavy Ion generator.
Definition: GenHeavyIon.h:28
HepMC3::GenEvent::attribute
std::shared_ptr< T > attribute(const std::string &name, const int &id=0) const
Get attribute of type T.
Definition: GenEvent.h:389
HepMC3::Print::line
static void line(std::ostream &os, const GenEvent &event, bool attributes=false)
Print one-line info.
Definition: Print.cc:202
HepMC3::WriterAscii::close
void close() override
Close file stream.
Definition: WriterAscii.cc:354