| 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 |
|
| 15 |
#include "HepMC3/ReaderAsciiHepMC2.h" |
| 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 |
cout<<"Usage: " << argv[0] <<" <input_hepmc2_file> <output_hepmc3_file> [<optional_events_limit>]" << 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 |
cout << " First event: " << endl; |
| 53 |
Print::listing(evt); |
| 54 |
} |
| 55 |
|
| 56 |
++events_parsed; |
| 57 |
if( events_parsed%100 == 0 ) cout<<"Events parsed: "<<events_parsed<<endl; |
| 58 |
if( events_limit && events_parsed >= events_limit ) break; |
| 59 |
} |
| 60 |
|
| 61 |
adapter.close(); |
| 62 |
output_file.close(); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |