| 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 |
cout << "Usage: " << argv[0] << " <HepMC3_input_file> <output_file>" << 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 |
cout << " First event: " << endl; |
| 49 |
Print::listing(evt); |
| 50 |
Print::content(evt); |
| 51 |
|
| 52 |
cout << " Testing attribute reading for the first event: " << endl; |
| 53 |
|
| 54 |
shared_ptr<GenCrossSection> cs = evt.attribute<GenCrossSection>("GenCrossSection"); |
| 55 |
shared_ptr<GenHeavyIon> hi = evt.attribute<GenHeavyIon>("GenHeavyIon"); |
| 56 |
shared_ptr<GenPdfInfo> pi = evt.attribute<GenPdfInfo>("GenPdfInfo"); |
| 57 |
|
| 58 |
if(cs) { |
| 59 |
cout << " Has GenCrossSection: "; |
| 60 |
Print::line(cs); |
| 61 |
} |
| 62 |
else cout << " No GenCrossSection " << endl; |
| 63 |
|
| 64 |
if(pi) { |
| 65 |
cout << " Has GenPdfInfo: "; |
| 66 |
Print::line(pi); |
| 67 |
} |
| 68 |
else cout << " No GenPdfInfo " << endl; |
| 69 |
|
| 70 |
if(hi) { |
| 71 |
cout << " Has GenHeavyIon: "; |
| 72 |
Print::line(hi); |
| 73 |
} |
| 74 |
else cout << " No GenHeavyIon " << endl; |
| 75 |
} |
| 76 |
|
| 77 |
++events_parsed; |
| 78 |
if( events_parsed%100 == 0 ) cout<<"Events parsed: "<<events_parsed<<endl; |
| 79 |
} |
| 80 |
|
| 81 |
input_file.close(); |
| 82 |
output_file.close(); |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |