HepMC3 event record library: class_example_write.cc Source File

HepMC3 event record library

class_example_write.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_write.cc
8 * @brief Basic example of use of root I/O: writing events to 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/ReaderAscii.h"
16#include "HepMC3/Print.h"
17
18#include "TFile.h"
19
20#include <iostream>
21#include <sstream>
22
23using namespace HepMC3;
24
25#include "MyClass.h"
26#include "MyRunClass.h"
27
28/** Main */
29int main(int argc, char **argv) {
30
31 if( argc<3 ) {
32 std::cout << "Usage: " << argv[0] << " <input_hepmc3_file> <output_root_file>" <<std:: endl;
33 exit(-1);
34 }
35
36 ReaderAscii text_input(argv[1]);
37
38 auto* fFile = new TFile(argv[2],"RECREATE");
39
40 int events_parsed = 0;
41
42 bool is_gen_run_info_written = false;
43
44 while( !text_input.failed() ) {
45
46 GenEvent evt(Units::GEV,Units::MM);
47
48 text_input.read_event(evt);
49
50 if( text_input.failed() ) break;
51
52 if( events_parsed == 0 ) {
53 std::cout << "First event: " << std::endl;
54 Print::listing(evt);
55 }
56
57 if(!is_gen_run_info_written) {
58 if(evt.run_info()) {
59 GenRunInfo run_info(*evt.run_info());
60
61 auto *my_run = new MyRunClass();
62
63 my_run->SetRunInfo(&run_info);
64
65 fFile->WriteObject(my_run,"MyRunClass");
66
67 is_gen_run_info_written = true;
68 }
69 }
70
71 auto* myclass = new MyClass();
72
73 myclass->SetEvent(&evt);
74 //
75 std::ostringstream os;
76 os << events_parsed;
77 std::string stevt = "Event_" + os.str();
78 const char* chevt = stevt.c_str();
79
80 std::cout << "writing " << stevt << std::endl;
81
82 fFile->WriteObject(myclass, chevt);
83
84 ++events_parsed;
85
86 if( events_parsed%100 == 0 ) {
87 std::cout << "Event: " << events_parsed << std::endl;
88 }
89 }
90
91 text_input.close();
92 fFile->Close();
93 std::cout << "Events parsed and written: " << events_parsed << std::endl;
94
95 return 0;
96}
GenEvent.h
Definition of class GenEvent.
GenRunInfo.h
Definition of class GenRunInfo.
Print.h
Definition of static class Print.
ReaderAscii.h
Definition of class ReaderAscii.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenRunInfo
Stores run-related information.
Definition GenRunInfo.h:33
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
MyClass
Sample class for root I/O test.
Definition MyClass.h:9
MyRunClass
Sample class for root I/O test.
Definition MyRunClass.h:9
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24