HepMC3 event record library: class_example_read.cc

HepMC3 event record library

class_example_read.cc

Basic example of use of root I/O: reading events from file.

Basic example of use of root I/O: reading events from file

Author
  Witold Pokorski

Date
  16/10/14

// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
//
/**
* @example class_example_read.cc
* @brief Basic example of use of root I/O: reading events from file
*
* @author Witold Pokorski
* @date 16/10/14
*/
#include "HepMC3/GenEvent.h"
#include "HepMC3/GenRunInfo.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/Print.h"
#include "MyClass.h"
#include "MyRunClass.h"
#include "TFile.h"
#include "TSystem.h"
#include "TKey.h"
#include <iostream>
using namespace HepMC3;
/** Main */
int main(int argc, char **argv) {
if( argc<3 ) {
std::cout << "Usage: " << argv[0] << " <input_root_file> <output_hepmc3_file>" << std::endl;
exit(-1);
}
TFile fo(argv[1]);
WriterAscii text_output(argv[2]);
MyClass* myevent = nullptr;
int events_parsed = 0;
// Get GenRunInfo, if available
auto *my_run = (MyRunClass*)fo.Get("MyRunClass");
std::shared_ptr<GenRunInfo> run_info;
if( my_run ) run_info.reset(my_run->GetRunInfo());
fo.GetListOfKeys()->Print();
TIter next(fo.GetListOfKeys());
TKey *key = nullptr;
while ((key=(TKey*)next()))
{
const char *cl = key->GetClassName();
if( strncmp(cl,"MyClass",7) != 0 ) continue;
fo.GetObject(key->GetName(), myevent);
std::cout << "Event: " << key->GetName() << std::endl;
if( events_parsed == 0 ) {
std::cout << "First event: " << std::endl;
Print::listing(*(myevent->GetEvent()));
}
if( run_info ) {
std::cout << "Setting run info" << std::endl;
myevent->GetEvent()->set_run_info(run_info);
run_info.reset();
}
text_output. write_event(*(myevent->GetEvent()));
++events_parsed;
if( events_parsed%100 == 0 ) {
std::cout << "Event: " << events_parsed << std::endl;
}
delete myevent->GetEvent();
delete myevent;
}
text_output. close();
std::cout << "Events parsed and written: " << events_parsed << std::endl;
return 0;
}
GenEvent.h
Definition of class GenEvent.
GenRunInfo.h
Definition of class GenRunInfo.
Print.h
Definition of static class Print.
WriterAscii.h
Definition of class WriterAscii.
HepMC3::GenEvent::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition GenEvent.h:148
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:52
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition WriterAscii.h:25
HepMC3::WriterAscii::close
void close() override
Close file stream.
Definition WriterAscii.cc:348
HepMC3::WriterAscii::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterAscii.cc:92
MyClass
Sample class for root I/O test.
Definition MyClass.h:9
MyClass::GetEvent
GenEvent * GetEvent()
Get HepMC event.
Definition MyClass.cc:10
MyRunClass
Sample class for root I/O test.
Definition MyRunClass.h:9
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24