File: class_example_read.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 (95 lines) | stat: -rw-r--r-- 2,094 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
// -*- C++ -*-
//
// This file is part of HepMC
// Copyright (C) 2014-2019 The HepMC collaboration (see AUTHORS for details)
//
/**
 *  @example class_example_read.cc
 *  @brief Basic example of use of root I/O: reading events from file
 *
 *  @author Witold Pokorski
 *  @date   16/10/14
 */
#include "HepMC3/GenEvent.h"
#include "HepMC3/GenRunInfo.h"
#include "HepMC3/WriterAscii.h"
#include "HepMC3/Print.h"

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

#include "TFile.h"
#include "TSystem.h"
#include "TKey.h"

#include <iostream>

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

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

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


    TFile fo(argv[1]);
    WriterAscii text_output(argv[2]);

    MyClass* myevent;
    int events_parsed = 0;

    // Get GenRunInfo, if available
    MyRunClass *my_run = (MyRunClass*)fo.Get("MyRunClass");
    shared_ptr<GenRunInfo> run_info;

    if( my_run ) run_info.reset(my_run->GetRunInfo());


    fo.GetListOfKeys()->Print();

    TIter next(fo.GetListOfKeys());
    TKey *key;

    while ((key=(TKey*)next()))
    {
        const char *cl = key->GetClassName();

        if( strncmp(cl,"MyClass",7) != 0 ) continue;

        fo.GetObject(key->GetName(), myevent);

        cout << "Event: " << key->GetName() << endl;

        if( events_parsed == 0 ) {
            cout << "First event: " << endl;
	    Print::listing(*(myevent->GetEvent()));
        }

        if( run_info ) {
            cout << "Setting run info" << endl;
            myevent->GetEvent()->set_run_info(run_info);
            run_info.reset();
        }

        text_output.write_event(*(myevent->GetEvent()));
        ++events_parsed;

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

        delete myevent->GetEvent();
        delete myevent;
    }

    text_output.close();

    std::cout << "Events parsed and written: " << events_parsed << std::endl;

    return 0;
}