File: HepMC3_fileIO_example.cc

package info (click to toggle)
hepmc3 3.1.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,124 kB
  • sloc: fortran: 66,849; cpp: 13,604; ansic: 1,374; xml: 109; sh: 72; makefile: 33
file content (85 lines) | stat: -rw-r--r-- 2,311 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
//
/**
 *  @example HepMC3_fileIO_example.cc
 *  @brief Test of file I/O
 *
 *  Parses HepMC3 file and saves it as a new HepMC3 file.
 *  The resulting file should be an exact copy of the input file
 *
 */
#include "HepMC3/GenEvent.h"
#include "HepMC3/ReaderAscii.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/Print.h"

#include <iostream>
using namespace HepMC3;

/** Main program */
int main(int argc, char **argv) {

    if( argc<3 ) {
        cout << "Usage: " << argv[0] << " <HepMC3_input_file> <output_file>" << endl;
        exit(-1);
    }

    ReaderAscii input_file (argv[1]);
    WriterAscii output_file(argv[2]);

    int events_parsed = 0;

    while(!input_file.failed()) {
        GenEvent evt(Units::GEV,Units::MM);

        // Read event from input file
        input_file.read_event(evt);

        // If reading failed - exit loop
        if( input_file.failed() ) break;

        // Save event to output file
        output_file.write_event(evt);

        if(events_parsed==0) {
            cout << " First event: " << endl;
            Print::listing(evt);
            Print::content(evt);

            cout << " Testing attribute reading for the first event: " << endl;

            shared_ptr<GenCrossSection> cs = evt.attribute<GenCrossSection>("GenCrossSection");
            shared_ptr<GenHeavyIon>     hi = evt.attribute<GenHeavyIon>("GenHeavyIon");
            shared_ptr<GenPdfInfo>      pi = evt.attribute<GenPdfInfo>("GenPdfInfo");

            if(cs) {
                cout << " Has GenCrossSection:   ";
                Print::line(cs);
            }
            else cout << " No GenCrossSection " << endl;

            if(pi) {
                cout << " Has GenPdfInfo:        ";
                Print::line(pi);
            }
            else cout << " No GenPdfInfo " << endl;

            if(hi) {
                cout << " Has GenHeavyIon:       ";
                Print::line(hi);
            }
            else cout << " No GenHeavyIon " << endl;
        }

        ++events_parsed;
        if( events_parsed%100 == 0 ) cout<<"Events parsed: "<<events_parsed<<endl;
    }

    input_file.close();
    output_file.close();

    return 0;
}