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