HepMC3 event record library
class_example_write.cc

Basic example of use of root I/O: writing events to file

Author
Witold Pokorski
Date
16/10/14
// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
//
/**
* @example class_example_write.cc
* @brief Basic example of use of root I/O: writing events to file
*
* @author Witold Pokorski
* @date 16/10/14
*/
#include "HepMC3/Print.h"
#include "TFile.h"
#include <iostream>
#include <sstream>
using namespace HepMC3;
using std::cout;
using std::endl;
#include "MyClass.h"
#include "MyRunClass.h"
/** Main */
int main(int argc, char **argv) {
if( argc<3 ) {
cout << "Usage: " << argv[0] << " <input_hepmc3_file> <output_root_file>" << endl;
exit(-1);
}
ReaderAscii text_input(argv[1]);
TFile* fFile = new TFile(argv[2],"RECREATE");
int events_parsed = 0;
bool is_gen_run_info_written = false;
while( !text_input.failed() ) {
GenEvent evt(Units::GEV,Units::MM);
text_input.read_event(evt);
if( text_input.failed() ) break;
if( events_parsed == 0 ) {
cout << "First event: " << endl;
}
if(!is_gen_run_info_written) {
if(evt.run_info()) {
GenRunInfo run_info(*evt.run_info());
MyRunClass *my_run = new MyRunClass();
my_run->SetRunInfo(&run_info);
fFile->WriteObject(my_run,"MyRunClass");
is_gen_run_info_written = true;
}
}
MyClass* myclass = new MyClass();
myclass->SetEvent(&evt);
//
std::ostringstream os;
os << events_parsed;
std::string stevt = "Event_" + os.str();
const char* chevt = stevt.c_str();
cout << "writing " << stevt << endl;
fFile->WriteObject(myclass, chevt);
++events_parsed;
if( events_parsed%100 == 0 ) {
cout << "Event: " << events_parsed << endl;
}
}
text_input.close();
fFile->Close();
std::cout << "Events parsed and written: " << events_parsed << std::endl;
return 0;
}
GenEvent.h
Definition of class GenEvent.
HepMC3::ReaderAscii::failed
bool failed()
Return status of the stream.
Definition: ReaderAscii.cc:537
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:42
MyClass
Sample class for root I/O test.
Definition: MyClass.h:9
MyRunClass::SetRunInfo
void SetRunInfo(GenRunInfo *)
Set HepMC event.
Definition: MyRunClass.cc:5
HepMC3
HepMC3 main namespace.
Definition: ReaderGZ.h:28
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:51
MyRunClass
Sample class for root I/O test.
Definition: MyRunClass.h:9
HepMC3::GenEvent::run_info
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:125
MyClass::SetEvent
void SetEvent(GenEvent *)
Set HepMC event.
Definition: MyClass.cc:5
Print.h
Definition of static class Print.
GenRunInfo.h
Definition of class GenRunInfo.
HepMC3::ReaderAscii::read_event
bool read_event(GenEvent &evt)
Load event from file.
Definition: ReaderAscii.cc:47
ReaderAscii.h
Definition of class ReaderAscii.
HepMC3::ReaderAscii::close
void close()
Close file stream.
Definition: ReaderAscii.cc:539
main
int main(int argc, char **argv)
Definition: rootIOTree_example_read.cc:23
HepMC3::GenRunInfo
Stores run-related information.
Definition: GenRunInfo.h:32