HepMC event record
HepMC2_reader_example.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  * @example HepMC2_reader_example.cc
8  * @brief Example of use of HepMC2 adapter
9  *
10  * Converts selected HepMC2 file to HepMC3 file
11  *
12  */
13 #include "HepMC/GenEvent.h"
14 
15 #include "HepMC/ReaderAsciiHepMC2.h"
16 #include "HepMC/WriterAscii.h"
17 #include "HepMC/Print.h"
18 
19 #include <iostream>
20 #include <cstdlib> // atoi
21 using namespace HepMC;
22 using std::cout;
23 using std::endl;
24 
25 /** Main program */
26 int main(int argc, char **argv) {
27 
28  if( argc < 3 ) {
29  cout<<"Usage: " << argv[0] <<" <input_hepmc2_file> <output_hepmc3_file> [<optional_events_limit>]" << endl;
30  exit(-1);
31  }
32 
33  // Open input and output files
34  ReaderAsciiHepMC2 adapter(argv[1]);
35  WriterAscii output_file(argv[2]);
36  int events_parsed = 0;
37  int events_limit = 0;
38 
39  if( argc >= 4 ) events_limit = atoi(argv[3]);
40 
41  while( !adapter.failed() ) {
42  GenEvent evt(Units::GEV,Units::MM);
43 
44  // Read event from input file
45  adapter.read_event(evt);
46 
47  // If reading failed - exit loop
48  if( adapter.failed() ) break;
49 
50  // Save event to output file
51  output_file.write_event(evt);
52 
53  if(events_parsed==0) {
54  cout << " First event: " << endl;
55  Print::listing(evt);
56  }
57 
58  ++events_parsed;
59  if( events_parsed%100 == 0 ) cout<<"Events parsed: "<<events_parsed<<endl;
60  if( events_limit && events_parsed >= events_limit ) break;
61  }
62 
63  adapter.close();
64  output_file.close();
65 
66  return 0;
67 }
static void listing(const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:57
Stores event-related information.
GenEvent I/O serialization for structured text files.
int main(int argc, char **argv)
Definition of template class SmartPointer.