HepMC3 event record library: rootIO_example_write.cc

HepMC3 event record library

rootIO_example_write.cc

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

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-2023 The HepMC collaboration (see AUTHORS for details)
//
/**
* @example rootIO_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/GenEvent.h"
#include "HepMC3/ReaderAscii.h"
#include "HepMC3/WriterRoot.h"
#include "HepMC3/Print.h"
#include <iostream>
using namespace HepMC3;
/** Main */
int main(int argc, char **argv) {
if( argc<3 ) {
std::cout << "Usage: " << argv[0] << " <input_hepmc3_file> <output_root_file>" << std::endl;
exit(-1);
}
ReaderAscii text_input (argv[1]);
WriterRoot root_output(argv[2]);
int events_parsed = 0;
while( !text_input. failed() ) {
GenEvent evt(Units::GEV,Units::MM);
text_input. read_event(evt);
if( text_input.failed() ) break;
if( events_parsed == 0 ) {
std::cout << "First event: " << std::endl;
Print::listing(evt);
}
root_output. write_event(evt);
++events_parsed;
if( events_parsed%100 == 0 ) {
std::cout << "Event: " << events_parsed << std::endl;
}
}
text_input. close();
root_output. close();
std::cout << "Events parsed and written: " << events_parsed << std::endl;
return 0;
}
GenEvent.h
Definition of class GenEvent.
Print.h
Definition of static class Print.
ReaderAscii.h
Definition of class ReaderAscii.
WriterRoot.h
Definition of class WriterRoot.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
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::ReaderAscii
GenEvent I/O parsing for structured text files.
Definition ReaderAscii.h:31
HepMC3::ReaderAscii::read_event
bool read_event(GenEvent &evt) override
Load event from file.
Definition ReaderAscii.cc:90
HepMC3::ReaderAscii::failed
bool failed() override
Return status of the stream.
Definition ReaderAscii.cc:596
HepMC3::ReaderAscii::close
void close() override
Close file stream.
Definition ReaderAscii.cc:598
HepMC3::WriterRoot
GenEvent I/O serialization for root files.
Definition WriterRoot.h:37
HepMC3::WriterRoot::close
void close() override
Close file stream.
Definition WriterRoot.cc:75
HepMC3::WriterRoot::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterRoot.cc:35
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24