HepMC event record
HepMC3_fileIO_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 HepMC3_fileIO_example.cc
8  * @brief Test of file I/O
9  *
10  * Parses HepMC3 file and saves it as a new HepMC3 file.
11  * The resulting file should be an exact copy of the input file
12  *
13  */
14 #include "HepMC/GenEvent.h"
15 #include "HepMC/ReaderAscii.h"
16 #include "HepMC/WriterAscii.h"
17 #include "HepMC/Print.h"
18 
19 #include <iostream>
20 using namespace HepMC;
21 using std::cout;
22 using std::endl;
23 
24 /** Main program */
25 int main(int argc, char **argv) {
26 
27  if( argc<3 ) {
28  cout << "Usage: " << argv[0] << " <HepMC3_input_file> <output_file>" << endl;
29  exit(-1);
30  }
31 
32  ReaderAscii input_file (argv[1]);
33  WriterAscii output_file(argv[2]);
34 
35  int events_parsed = 0;
36 
37  while(!input_file.failed()) {
38  GenEvent evt(Units::GEV,Units::MM);
39 
40  // Read event from input file
41  input_file.read_event(evt);
42 
43  // If reading failed - exit loop
44  if( input_file.failed() ) break;
45 
46  // Save event to output file
47  output_file.write_event(evt);
48 
49  if(events_parsed==0) {
50  cout << " First event: " << endl;
51  Print::listing(evt);
52  Print::content(evt);
53 
54  cout << " Testing attribute reading for the first event: " << endl;
55 
56  shared_ptr<GenCrossSection> cs = evt.attribute<GenCrossSection>("GenCrossSection");
57  shared_ptr<GenHeavyIon> hi = evt.attribute<GenHeavyIon>("GenHeavyIon");
58  shared_ptr<GenPdfInfo> pi = evt.attribute<GenPdfInfo>("GenPdfInfo");
59 
60  if(cs) {
61  cout << " Has GenCrossSection: ";
62  Print::line(cs);
63  }
64  else cout << " No GenCrossSection " << endl;
65 
66  if(pi) {
67  cout << " Has GenPdfInfo: ";
68  Print::line(pi);
69  }
70  else cout << " No GenPdfInfo " << endl;
71 
72  if(hi) {
73  cout << " Has GenHeavyIon: ";
74  Print::line(hi);
75  }
76  else cout << " No GenHeavyIon " << endl;
77  }
78 
79  ++events_parsed;
80  if( events_parsed%100 == 0 ) cout<<"Events parsed: "<<events_parsed<<endl;
81  }
82 
83  input_file.close();
84  output_file.close();
85 
86  return 0;
87 }
Stores additional information about Heavy Ion generator.
Stores additional information about PDFs.
static void listing(const GenEvent &event, unsigned short precision=2)
Print event in listing (HepMC2) format.
Definition: Print.cc:57
Stores additional information about cross-section.
static void line(const GenEvent &event)
Print one-line info.
Stores event-related information.
GenEvent I/O serialization for structured text files.
static void content(const GenEvent &event)
Print content of all GenEvent containers.
Definition: Print.cc:20
int main(int argc, char **argv)
Definition of template class SmartPointer.
GenEvent I/O parsing for structured text files.