Fork me on GitHub

source: git/external/HepMC3/rootIO/src/ReaderRootTree.cc@ b30245b

Last change on this file since b30245b was b30245b, checked in by Andrii Verbytskyi <averbyts@…>, 3 years ago

Added root support to HepMC3

  • Property mode set to 100644
File size: 3.5 KB
Line 
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 * @file ReaderRootTree.cc
8 * @brief Implementation of \b class ReaderRootTree
9 *
10 */
11#include "HepMC3/ReaderRootTree.h"
12#include "HepMC3/Units.h"
13#include "HepMC3/Version.h"
14namespace HepMC3
15{
16HEPMC3_DECLARE_READER_FILE(ReaderRootTree)
17
18ReaderRootTree::ReaderRootTree(const std::string &filename):
19 m_tree(0), m_events_count(0), m_tree_name("hepmc3_tree"), m_branch_name("hepmc3_event")
20{
21 m_file = TFile::Open(filename.c_str());
22 if (!init()) return;
23}
24
25
26ReaderRootTree::ReaderRootTree(const std::string &filename, const std::string &treename, const std::string &branchname):
27 m_tree(0), m_events_count(0), m_tree_name(treename.c_str()), m_branch_name(branchname.c_str())
28{
29 m_file = TFile::Open(filename.c_str());
30 if (!init()) return;
31}
32
33bool ReaderRootTree::init()
34{
35 if ( !m_file->IsOpen() )
36 {
37 HEPMC3_ERROR("ReaderRootTree: problem opening file: " << m_file->GetName())
38 return false;
39 }
40
41
42 m_tree = reinterpret_cast<TTree*>(m_file->Get(m_tree_name.c_str()));
43 if (!m_tree)
44 {
45 HEPMC3_ERROR("ReaderRootTree: problem opening tree: " << m_tree_name)
46 return false;
47 }
48 m_event_data = new GenEventData();
49 int result = m_tree->SetBranchAddress(m_branch_name.c_str(), &m_event_data);
50 if (result < 0)
51 {
52 HEPMC3_ERROR("ReaderRootTree: problem reading branch tree: " << m_tree_name)
53 return false;
54 }
55 m_run_info_data = new GenRunInfoData();
56 result = m_tree->SetBranchAddress("GenRunInfo", &m_run_info_data);
57 if (result < 0)
58 {
59 HEPMC3_WARNING("ReaderRootTree: problem reading branch tree: GenRunInfo. Will attempt to read GenRunInfoData object.")
60 std::shared_ptr<GenRunInfo> ri = std::make_shared<GenRunInfo>();
61 GenRunInfoData *run = reinterpret_cast<GenRunInfoData*>(m_file->Get("GenRunInfoData"));
62 if (run) {
63 ri->read_data(*run);
64 delete run;
65 set_run_info(ri);
66 HEPMC3_WARNING("ReaderRootTree::init The object was written with HepMC3 version 3.0")
67 } else {
68 HEPMC3_ERROR("ReaderRootTree: problem reading object GenRunInfoData")
69 return false;
70 }
71 }
72 set_run_info(std::make_shared<GenRunInfo>());
73 return true;
74}
75
76bool ReaderRootTree::skip(const int n)
77{
78 m_events_count+=n;
79 if (m_events_count > m_tree->GetEntries()) return false;
80 return true;
81}
82
83
84
85bool ReaderRootTree::read_event(GenEvent& evt)
86{
87 if (m_events_count > m_tree->GetEntries()) return false;
88 m_event_data->particles.clear();
89 m_event_data->vertices.clear();
90 m_event_data->links1.clear();
91 m_event_data->links2.clear();
92 m_event_data->attribute_id.clear();
93 m_event_data->attribute_name.clear();
94 m_event_data->attribute_string.clear();
95
96
97 m_run_info_data->weight_names.clear();
98 m_run_info_data->tool_name.clear();
99 m_run_info_data->tool_version.clear();
100 m_run_info_data->tool_description.clear();
101 m_run_info_data->attribute_name.clear();
102 m_run_info_data->attribute_string.clear();
103
104
105 m_tree->GetEntry(m_events_count);
106 evt.read_data(*m_event_data);
107 run_info()->read_data(*m_run_info_data);
108 evt.set_run_info(run_info());
109 m_events_count++;
110 return true;
111}
112
113void ReaderRootTree::close()
114{
115 m_file->Close();
116}
117
118bool ReaderRootTree::failed()
119{
120 if ( !m_file->IsOpen() ) return true;
121 if (m_events_count > m_tree->GetEntries()) return true;
122 return false;
123}
124
125} // namespace HepMC3
Note: See TracBrowser for help on using the repository browser.