HepMC event record
ReaderRootTree.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014 The HepMC collaboration (see AUTHORS for details)
5 //
6 /**
7  * @file ReaderRootTree.cc
8  * @brief Implementation of \b class ReaderRootTree
9  *
10  */
11 #include "HepMC/ReaderRootTree.h"
12 #include "HepMC/Units.h"
13 
14 namespace HepMC
15 {
16 
17 ReaderRootTree::ReaderRootTree(const std::string &filename):
18  m_file(filename.c_str()),m_tree(0),m_events_count(0),m_tree_name("hepmc3_tree"),m_branch_name("hepmc3_event")
19 {
20  if (!init()) return;
21 }
22 
23 
24 ReaderRootTree::ReaderRootTree(const std::string &filename,const std::string &treename,const std::string &branchname):
25  m_file(filename.c_str()),m_tree(0),m_events_count(0),m_tree_name(treename.c_str()),m_branch_name(branchname.c_str())
26 {
27  if (!init()) return;
28 }
29 
31 {
32  if ( !m_file.IsOpen() )
33  {
34  ERROR( "ReaderRootTree: problem opening file: " << m_file.GetName() )
35  return false;
36  }
37  shared_ptr<GenRunInfo> ri = make_shared<GenRunInfo>();
38 
39  GenRunInfoData *run = (GenRunInfoData*)m_file.Get("GenRunInfoData");
40 
41  if(run) {
42  ri->read_data(*run);
43  delete run;
44  }
45 
46  set_run_info(ri);
47 
48 
49 
50  m_tree=(TTree*)m_file.Get(m_tree_name.c_str());
51  if (!m_tree)
52  {
53  ERROR( "ReaderRootTree: problem opening tree: " << m_tree_name)
54  return false;
55  }
56  m_event_data=new GenEventData();
57  int result=m_tree->SetBranchAddress(m_branch_name.c_str(),&m_event_data);
58  if (result<0)
59  {
60  ERROR( "ReaderRootTree: problem reading branch tree: " << m_tree_name)
61  return false;
62  }
63 
64  return true;
65 }
66 
67 
68 
69 
71 {
72  if (m_events_count>m_tree->GetEntries()) return false;
73  m_event_data->particles.clear();
74  m_event_data->vertices.clear();
75  m_event_data->links1.clear();
76  m_event_data->links2.clear();
77  m_event_data->attribute_id.clear();
78  m_event_data->attribute_name.clear();
79  m_event_data->attribute_string.clear();
80 
81  m_tree->GetEntry(m_events_count);
82  evt.read_data(*m_event_data);
83 
85  return true;
86 }
87 
89 {
90  m_file.Close();
91 }
92 
94 {
95  if ( !m_file.IsOpen() ) return true;
96  if (m_events_count>m_tree->GetEntries()) return true;
97  return false;
98 }
99 
100 } // namespace HepMC
std::vector< std::string > attribute_string
Attribute serialized as string.
bool init()
init routine
std::vector< int > links1
First id of the vertex links.
Stores serializable run information.
bool failed()
Get stream error state.
TFile m_file
File handler.
std::vector< GenVertexData > vertices
Vertices.
int m_events_count
Events count. Needed to read the tree.
Definition of class ReaderRootTree.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Stores event-related information.
ReaderRootTree(const std::string &filename)
Default constructor.
std::vector< int > attribute_id
Attribute owner id.
void read_data(const GenEventData &data)
Fill GenEvent based on GenEventData.
Definition: GenEvent.cc:478
std::vector< int > links2
Second id of the vertex links.
void close()
Close file stream.
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
bool read_event(GenEvent &evt)
Read event from file.
std::vector< std::string > attribute_name
Attribute name.
Definition of template class SmartPointer.
std::vector< GenParticleData > particles
Particles.
Stores serializable event information.