HepMC3 event record library: rootIO_example_read.cc Source File

HepMC3 event record library

rootIO_example_read.cc
1// -*- C++ -*-
2//
3// This file is part of HepMC
4// Copyright (C) 2014-2023 The HepMC collaboration (see AUTHORS for details)
5//
6/**
7 * @example rootIO_example_read.cc
8 * @brief Basic example of use of root I/O: reading events from file
9 *
10 * @author Witold Pokorski
11 * @date 16/10/14
12 */
13#include "HepMC3/GenEvent.h"
14#include "HepMC3/WriterAscii.h"
15#include "HepMC3/ReaderRoot.h"
16#include "HepMC3/Print.h"
17
18#include <iostream>
19
20using namespace HepMC3;
21
22/** Main */
23int main(int argc, char **argv) {
24
25 if( argc<3 ) {
26 std::cout << "Usage: " << argv[0] << " <input_root_file> <output_hepmc3_file>" << std::endl;
27 exit(-1);
28 }
29
30 ReaderRoot root_input (argv[1]);
31 WriterAscii text_output(argv[2]);
32
33 int events_parsed = 0;
34
35 while( !root_input.failed() ) {
36
37 GenEvent evt;
38
39 root_input.read_event(evt);
40
41 if( root_input.failed() ) break;
42
43 if( events_parsed == 0 ) {
44 std::cout << "First event: " << std::endl;
45 Print::listing(evt);
46 }
47
48 text_output.write_event(evt);
49 ++events_parsed;
50
51 if( events_parsed%100 == 0 ) {
52 std::cout << "Event: " << events_parsed << std::endl;
53 }
54 }
55
56 root_input.close();
57 text_output.close();
58
59 std::cout << "Events parsed and written: " << events_parsed << std::endl;
60
61 return 0;
62}
GenEvent.h
Definition of class GenEvent.
Print.h
Definition of static class Print.
ReaderRoot.h
Definition of class ReaderRoot.
WriterAscii.h
Definition of class WriterAscii.
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::ReaderRoot
GenEvent I/O parsing and serialization for root files.
Definition ReaderRoot.h:33
HepMC3::WriterAscii
GenEvent I/O serialization for structured text files.
Definition WriterAscii.h:25
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24