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