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  */
11 #include "HepMC3/ReaderRootTree.h"
12 #include "HepMC3/Units.h"
13 
14 namespace HepMC3
15 {
16 
17 ReaderRootTree::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 
25 ReaderRootTree::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();
75  m_event_data->attribute_id.clear();
78 
79 
81  m_run_info_data->tool_name.clear();
86 
87 
88  m_tree->GetEntry(m_events_count);
89  evt.read_data(*m_event_data);
90  run_info()->read_data(*m_run_info_data);
91  evt.set_run_info(run_info());
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
void set_run_info(shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:129
HepMC3 main namespace.
Definition: ReaderGZ.h:28
std::vector< int > attribute_id
Attribute owner id.
Definition: GenEventData.h:54
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
std::vector< std::string > attribute_name
Attribute name.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Reader.h:39
std::vector< int > links2
Second id of the vertex links.
Definition: GenEventData.h:52
Definition of class ReaderRootTree.
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
TFile * m_file
File handler.
bool failed()
Get file error state.
std::vector< int > links1
First id of the vertex links.
Definition: GenEventData.h:51
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition: GenEventData.h:56
void close()
Close file.
bool read_event(GenEvent &evt)
Read event from file.
bool init()
init routine
Stores event-related information.
Definition: GenEvent.h:42
Stores serializable event information.
Definition: GenEventData.h:26
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > tool_name
Tool names.
std::vector< std::string > attribute_name
Attribute name.
Definition: GenEventData.h:55
Stores serializable run information.
int m_events_count
Events count. Needed to read the tree.
std::string m_tree_name
Name of TTree.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:705
Definition of class Units.
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Reader.h:46
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
std::vector< std::string > tool_version
Tool versions.
GenEventData * m_event_data
Pointer to structure that holds event data.
std::string m_branch_name
Name of TBranch in TTree.
std::vector< std::string > weight_names
Weight names.
std::vector< std::string > tool_description
Tool descriptions.
ReaderRootTree(const std::string &filename)
Default constructor.