HepMC3 event record library
ReaderRootTree.cc
Go to the documentation of this file.
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 */
12#include "HepMC3/Units.h"
13
14namespace HepMC3
15{
16
17ReaderRootTree::ReaderRootTree(const std::string &filename):
18 m_tree(0),m_events_count(0),m_tree_name("hepmc3_tree"),m_branch_name("hepmc3_event")
19{
20 m_file = TFile::Open(filename.c_str());
21 if (!init()) return;
22}
23
24
25ReaderRootTree::ReaderRootTree(const std::string &filename,const std::string &treename,const std::string &branchname):
26 m_tree(0),m_events_count(0),m_tree_name(treename.c_str()),m_branch_name(branchname.c_str())
27{
28 m_file = TFile::Open(filename.c_str());
29 if (!init()) return;
30}
31
33{
34 if ( !m_file->IsOpen() )
35 {
36 ERROR( "ReaderRootTree: problem opening file: " << m_file->GetName() )
37 return false;
38 }
39
40
41 m_tree=reinterpret_cast<TTree*>(m_file->Get(m_tree_name.c_str()));
42 if (!m_tree)
43 {
44 ERROR( "ReaderRootTree: problem opening tree: " << m_tree_name)
45 return false;
46 }
48 int result=m_tree->SetBranchAddress(m_branch_name.c_str(),&m_event_data);
49 if (result<0)
50 {
51 ERROR( "ReaderRootTree: problem reading branch tree: " << m_tree_name)
52 return false;
53 }
55 result=m_tree->SetBranchAddress("GenRunInfo",&m_run_info_data);
56 if (result<0)
57 {
58 ERROR( "ReaderRootTree2: problem reading branch tree: " << "GenRunInfo")
59 return false;
60 }
61 set_run_info(make_shared<GenRunInfo>());
62 return true;
63}
64
65
66
67
69{
70 if (m_events_count>m_tree->GetEntries()) return false;
71 m_event_data->particles.clear();
72 m_event_data->vertices.clear();
73 m_event_data->links1.clear();
74 m_event_data->links2.clear();
78
79
86
87
88 m_tree->GetEntry(m_events_count);
90 run_info()->read_data(*m_run_info_data);
93 return true;
94}
95
97{
98 m_file->Close();
99}
100
102{
103 if ( !m_file->IsOpen() ) return true;
104 if (m_events_count>m_tree->GetEntries()) return true;
105 return false;
106}
107
108} // namespace HepMC3
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition Errors.h:23
Definition of class ReaderRootTree.
Definition of class Units.
Stores event-related information.
Definition GenEvent.h:42
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition GenEvent.h:129
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition GenEvent.cc:705
bool read_event(GenEvent &evt)
Read event from file.
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
ReaderRootTree(const std::string &filename)
Default constructor.
GenEventData * m_event_data
Pointer to structure that holds event data.
void close()
Close file.
int m_events_count
Events count. Needed to read the tree.
std::string m_tree_name
Name of TTree.
std::string m_branch_name
Name of TBranch in TTree.
TFile * m_file
File handler.
bool failed()
Get file error state.
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
bool init()
init routine
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Reader.h:46
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Reader.h:39
HepMC3 main namespace.
Definition ReaderGZ.h:28
Stores serializable event information.
std::vector< GenVertexData > vertices
Vertices.
std::vector< int > links2
Second id of the vertex links.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< GenParticleData > particles
Particles.
std::vector< int > links1
First id of the vertex links.
std::vector< std::string > attribute_name
Attribute name.
std::vector< int > attribute_id
Attribute owner id.
Stores serializable run information.
std::vector< std::string > tool_name
Tool names.
std::vector< std::string > tool_version
Tool versions.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > attribute_name
Attribute name.
std::vector< std::string > tool_description
Tool descriptions.
std::vector< std::string > weight_names
Weight names.