| 1 |
// -*- C++ -*- |
| 2 |
// |
| 3 |
// This file is part of HepMC |
| 4 |
// Copyright (C) 2014-2019 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 |
|
| 23 |
using namespace HepMC3; |
| 24 |
using std::cout; |
| 25 |
using std::endl; |
| 26 |
|
| 27 |
#include "MyClass.h" |
| 28 |
#include "MyRunClass.h" |
| 29 |
|
| 30 |
/** Main */ |
| 31 |
int main(int argc, char **argv) { |
| 32 |
|
| 33 |
if( argc<3 ) { |
| 34 |
cout << "Usage: " << argv[0] << " <input_hepmc3_file> <output_root_file>" << endl; |
| 35 |
exit(-1); |
| 36 |
} |
| 37 |
|
| 38 |
ReaderAscii text_input(argv[1]); |
| 39 |
|
| 40 |
TFile* fFile = new TFile(argv[2],"RECREATE"); |
| 41 |
|
| 42 |
int events_parsed = 0; |
| 43 |
|
| 44 |
bool is_gen_run_info_written = false; |
| 45 |
|
| 46 |
while( !text_input.failed() ) { |
| 47 |
|
| 48 |
GenEvent evt(Units::GEV,Units::MM); |
| 49 |
|
| 50 |
text_input.read_event(evt); |
| 51 |
|
| 52 |
if( text_input.failed() ) break; |
| 53 |
|
| 54 |
if( events_parsed == 0 ) { |
| 55 |
cout << "First event: " << endl; |
| 56 |
Print::listing(evt); |
| 57 |
} |
| 58 |
|
| 59 |
if(!is_gen_run_info_written) { |
| 60 |
if(evt.run_info()) { |
| 61 |
GenRunInfo run_info(*evt.run_info()); |
| 62 |
|
| 63 |
MyRunClass *my_run = new MyRunClass(); |
| 64 |
|
| 65 |
my_run->SetRunInfo(&run_info); |
| 66 |
|
| 67 |
fFile->WriteObject(my_run,"MyRunClass"); |
| 68 |
|
| 69 |
is_gen_run_info_written = true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
MyClass* myclass = new MyClass(); |
| 74 |
|
| 75 |
myclass->SetEvent(&evt); |
| 76 |
// |
| 77 |
std::ostringstream os; |
| 78 |
os << events_parsed; |
| 79 |
std::string stevt = "Event_" + os.str(); |
| 80 |
const char* chevt = stevt.c_str(); |
| 81 |
|
| 82 |
cout << "writing " << stevt << endl; |
| 83 |
|
| 84 |
fFile->WriteObject(myclass, chevt); |
| 85 |
|
| 86 |
++events_parsed; |
| 87 |
|
| 88 |
if( events_parsed%100 == 0 ) { |
| 89 |
cout << "Event: " << events_parsed << endl; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
text_input.close(); |
| 94 |
fFile->Close(); |
| 95 |
std::cout << "Events parsed and written: " << events_parsed << std::endl; |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |