HepMC event record
rootIOTree_example_write.cc
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file rootIO_example_write.cc
8  * @brief Basic example of use of root I/O: writing events to file
9  *
10  * @author Witold Pokorski/Andrii Verbytskyi
11  * @date 29/10/15
12  */
13 #include "HepMC/GenEvent.h"
14 #include "HepMC/ReaderAscii.h"
15 #include "HepMC/WriterRootTree.h"
16 #include "HepMC/Print.h"
17 
18 #include <iostream>
19 
20 using namespace HepMC;
21 using std::cout;
22 using std::endl;
23 
24 /** Main */
25 int main(int argc, char **argv) {
26 
27  if( argc<3 ) {
28  cout << "Usage: " << argv[0] << " <input_hepmc3_file> <output_root_file>" << endl;
29  exit(-1);
30  }
31 
32  ReaderAscii text_input (argv[1]);
33  WriterRootTree root_output(argv[2]);
34 
35  int events_parsed = 0;
36 
37  while( !text_input.failed() ) {
38 
39  GenEvent evt(Units::GEV,Units::MM);
40 
41  text_input.read_event(evt);
42 
43  if( text_input.failed() ) break;
44 
45  if( events_parsed == 0 ) {
46  cout << "First event: " << endl;
47  Print::listing(evt);
48  }
49 
50  root_output.write_event(evt);
51  ++events_parsed;
52 
53  if( events_parsed%1000 == 0 ) {
54  cout << "Event: " << events_parsed << endl;
55  }
56  }
57 
58  text_input.close();
59  root_output.close();
60 
61  std::cout << "Events parsed and written: " << events_parsed << std::endl;
62 
63  return 0;
64 }
static void listing(const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:57
Definition of class WriterRootTree.
Stores event-related information.
GenEvent I/O serialization for root files based on root TTree.
int main(int argc, char **argv)
Definition of template class SmartPointer.
GenEvent I/O parsing for structured text files.