File: class_example_write.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 (98 lines) | stat: -rw-r--r-- 2,173 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
86
87
88
89
90
91
92
93
94
95
96
97
98
// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
//
/**
 *  @example class_example_write.cc
 *  @brief Basic example of use of root I/O: writing events to file
 *
 *  @author Witold Pokorski
 *  @date   16/10/14
 */
#include "HepMC3/GenEvent.h"
#include "HepMC3/GenRunInfo.h"
#include "HepMC3/ReaderAscii.h"
#include "HepMC3/Print.h"

#include "TFile.h"

#include <iostream>
#include <sstream>

using namespace HepMC3;
using std::cout;
using std::endl;

#include "MyClass.h"
#include "MyRunClass.h"

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

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

    ReaderAscii text_input(argv[1]);

    TFile* fFile = new TFile(argv[2],"RECREATE");

    int events_parsed = 0;

    bool is_gen_run_info_written = false;

    while( !text_input.failed() ) {

        GenEvent evt(Units::GEV,Units::MM);

        text_input.read_event(evt);

        if( text_input.failed() ) break;

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

        if(!is_gen_run_info_written) {
            if(evt.run_info()) {
                GenRunInfo run_info(*evt.run_info());

                MyRunClass *my_run = new MyRunClass();

                my_run->SetRunInfo(&run_info);

                fFile->WriteObject(my_run,"MyRunClass");

                is_gen_run_info_written = true;
            }
        }

        MyClass* myclass = new MyClass();

        myclass->SetEvent(&evt);
        //
        std::ostringstream os;
        os << events_parsed;
        std::string stevt = "Event_" + os.str();
        const char* chevt = stevt.c_str();

        cout << "writing " << stevt << endl;

        fFile->WriteObject(myclass, chevt);

        ++events_parsed;

        if( events_parsed%100 == 0 ) {
            cout << "Event: " << events_parsed << endl;
        }
    }

    text_input.close();
    fFile->Close();
    std::cout << "Events parsed and written: " << events_parsed << std::endl;

    return 0;
}