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 #include "HepMC3/Version.h"
14 namespace HepMC3
15 {
16 HEPMC3_DECLARE_READER_FILE(ReaderRootTree)
17 
18 ReaderRootTree::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 
26 ReaderRootTree::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 
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  }
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  }
56  result=m_tree->SetBranchAddress("GenRunInfo",&m_run_info_data);
57  if (result<0)
58  {
59  HEPMC3_ERROR( "ReaderRootTree2: problem reading branch tree: " << "GenRunInfo")
60  return false;
61  }
62  set_run_info(std::make_shared<GenRunInfo>());
63  return true;
64 }
65 
66 bool ReaderRootTree::skip(const int n)
67 {
68  m_events_count+=n;
69  if (m_events_count>m_tree->GetEntries()) return false;
70  return true;
71 }
72 
73 
74 
76 {
77  if (m_events_count>m_tree->GetEntries()) return false;
78  m_event_data->particles.clear();
79  m_event_data->vertices.clear();
80  m_event_data->links1.clear();
81  m_event_data->links2.clear();
82  m_event_data->attribute_id.clear();
85 
86 
88  m_run_info_data->tool_name.clear();
93 
94 
95  m_tree->GetEntry(m_events_count);
96  evt.read_data(*m_event_data);
97  run_info()->read_data(*m_run_info_data);
98  evt.set_run_info(run_info());
100  return true;
101 }
102 
104 {
105  m_file->Close();
106 }
107 
109 {
110  if ( !m_file->IsOpen() ) return true;
111  if (m_events_count>m_tree->GetEntries()) return true;
112  return false;
113 }
114 
115 } // namespace HepMC3
HepMC3::GenRunInfoData::tool_name
std::vector< std::string > tool_name
Tool names.
Definition: GenRunInfoData.h:26
HepMC3::ReaderRootTree::skip
bool skip(const int) override
skip events
Definition: ReaderRootTree.cc:66
HepMC3::ReaderRootTree::m_tree
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
Definition: ReaderRootTree.h:74
HepMC3::Reader::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Reader.h:64
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:41
HepMC3::ReaderRootTree::m_file
TFile * m_file
File handler.
Definition: ReaderRootTree.h:72
HepMC3::ReaderRootTree::m_tree_name
std::string m_tree_name
Name of TTree.
Definition: ReaderRootTree.h:79
HepMC3::ReaderRootTree::m_branch_name
std::string m_branch_name
Name of TBranch in TTree.
Definition: ReaderRootTree.h:80
HepMC3::ReaderRootTree::m_events_count
int m_events_count
Events count. Needed to read the tree.
Definition: ReaderRootTree.h:76
HepMC3::GenRunInfoData::tool_description
std::vector< std::string > tool_description
Tool descriptions.
Definition: GenRunInfoData.h:28
HepMC3::GenRunInfoData::weight_names
std::vector< std::string > weight_names
Weight names.
Definition: GenRunInfoData.h:24
HepMC3
HepMC3 main namespace.
Definition: AnalysisExample.h:18
HepMC3::GenEventData
Stores serializable event information.
Definition: GenEventData.h:26
ReaderRootTree.h
Definition of class ReaderRootTree.
HepMC3::GenEventData::links1
std::vector< int > links1
First id of the vertex links.
Definition: GenEventData.h:51
HepMC3::ReaderRootTree::read_event
bool read_event(GenEvent &evt) override
Read event from file.
Definition: ReaderRootTree.cc:75
HepMC3::GenEventData::links2
std::vector< int > links2
Second id of the vertex links.
Definition: GenEventData.h:52
HepMC3::GenEvent::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the GenRunInfo object by smart pointer.
Definition: GenEvent.h:128
HepMC3::GenRunInfoData::attribute_string
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition: GenRunInfoData.h:31
HepMC3::GenEventData::attribute_name
std::vector< std::string > attribute_name
Attribute name.
Definition: GenEventData.h:55
HepMC3::ReaderRootTree::failed
bool failed() override
Get file error state.
Definition: ReaderRootTree.cc:108
HepMC3::ReaderRootTree::close
void close() override
Close file.
Definition: ReaderRootTree.cc:103
HepMC3::GenRunInfoData
Stores serializable run information.
Definition: GenRunInfoData.h:23
HepMC3::ReaderRootTree::ReaderRootTree
ReaderRootTree(const std::string &filename)
Default constructor.
Definition: ReaderRootTree.cc:18
HepMC3::GenEventData::attribute_string
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition: GenEventData.h:56
HepMC3::ReaderRootTree::init
bool init()
init routine
Definition: ReaderRootTree.cc:33
HepMC3::ReaderRootTree
GenEvent I/O parsing and serialization for root files based on root TTree.
Definition: ReaderRootTree.h:34
HepMC3::Reader::run_info
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Reader.h:44
HepMC3::GenRunInfoData::attribute_name
std::vector< std::string > attribute_name
Attribute name.
Definition: GenRunInfoData.h:30
HEPMC3_ERROR
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
HepMC3::ReaderRootTree::m_run_info_data
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
Definition: ReaderRootTree.h:78
Units.h
Definition of class Units.
HepMC3::GenEventData::particles
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
HepMC3::ReaderRootTree::m_event_data
GenEventData * m_event_data
Pointer to structure that holds event data.
Definition: ReaderRootTree.h:77
HepMC3::GenRunInfoData::tool_version
std::vector< std::string > tool_version
Tool versions.
Definition: GenRunInfoData.h:27
HepMC3::GenEvent::read_data
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:703
HepMC3::GenEventData::vertices
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
HepMC3::GenEventData::attribute_id
std::vector< int > attribute_id
Attribute owner id.
Definition: GenEventData.h:54