HepMC3 event record library: class_example_read.cc Source File

HepMC3 event record library

class_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 class_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/GenRunInfo.h"
15#include "HepMC3/WriterAscii.h"
16#include "HepMC3/Print.h"
17
18#include "MyClass.h"
19#include "MyRunClass.h"
20
21#include "TFile.h"
22#include "TSystem.h"
23#include "TKey.h"
24
25#include <iostream>
26
27using namespace HepMC3;
28
29
30/** Main */
31int main(int argc, char **argv) {
32
33 if( argc<3 ) {
34 std::cout << "Usage: " << argv[0] << " <input_root_file> <output_hepmc3_file>" << std::endl;
35 exit(-1);
36 }
37
38
39 TFile fo(argv[1]);
40 WriterAscii text_output(argv[2]);
41
42 MyClass* myevent = nullptr;
43 int events_parsed = 0;
44
45 // Get GenRunInfo, if available
46 auto *my_run = (MyRunClass*)fo.Get("MyRunClass");
47 std::shared_ptr<GenRunInfo> run_info;
48
49 if( my_run ) run_info.reset(my_run->GetRunInfo());
50
51
52 fo.GetListOfKeys()->Print();
53
54 TIter next(fo.GetListOfKeys());
55 TKey *key = nullptr;
56
57 while ((key=(TKey*)next()))
58 {
59 const char *cl = key->GetClassName();
60
61 if( strncmp(cl,"MyClass",7) != 0 ) continue;
62
63 fo.GetObject(key->GetName(), myevent);
64
65 std::cout << "Event: " << key->GetName() << std::endl;
66
67 if( events_parsed == 0 ) {
68 std::cout << "First event: " << std::endl;
69 Print::listing(*(myevent->GetEvent()));
70 }
71
72 if( run_info ) {
73 std::cout << "Setting run info" << std::endl;
74 myevent->GetEvent()->set_run_info(run_info);
75 run_info.reset();
76 }
77
78 text_output.write_event(*(myevent->GetEvent()));
79 ++events_parsed;
80
81 if( events_parsed%100 == 0 ) {
82 std::cout << "Event: " << events_parsed << std::endl;
83 }
84
85 delete myevent->GetEvent();
86 delete myevent;
87 }
88
89 text_output.close();
90
91 std::cout << "Events parsed and written: " << events_parsed << std::endl;
92
93 return 0;
94}
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
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