HepMC event record
WriterRootTree.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 /**
4  * @file WriterRootTree.cc
5  * @brief Implementation of \b class WriterRootTree
6  *
7  */
8 #include "HepMC/WriterRootTree.h"
9 #include <cstdio> // sprintf
10 
11 namespace HepMC
12 {
13 
14 WriterRootTree::WriterRootTree(const std::string &filename, shared_ptr<GenRunInfo> run):
15  m_file(filename.c_str(),"RECREATE"),
16  m_tree(0),
17  m_events_count(0),
18  m_tree_name("hepmc3_tree"),
19  m_branch_name("hepmc3_event")
20 {
21  if (!init(run)) return;
22 }
23 
24 WriterRootTree::WriterRootTree(const std::string &filename,const std::string &treename,const std::string &branchname, shared_ptr<GenRunInfo> run):
25  m_file(filename.c_str(),"RECREATE"),
26  m_tree(0),
27  m_events_count(0),
28  m_tree_name(treename.c_str()),
29  m_branch_name(branchname.c_str())
30 {
31  if (!init(run)) return;
32 }
33 
34 bool WriterRootTree::init(shared_ptr<GenRunInfo> run )
35 {
36  if ( !m_file.IsOpen() )
37  {
38  ERROR( "WriterRootTree: problem opening file: " <<m_file.GetName() )
39  return false;
40  }
41  set_run_info(run);
42  m_event_data= new GenEventData();
43  m_tree= new TTree(m_tree_name.c_str(),"hepmc3_tree");
44  m_tree->Branch(m_branch_name.c_str(), m_event_data);
45  if ( run_info() ) write_run_info();
46  return true;
47 }
48 
50 {
51  if ( !m_file.IsOpen() ) return;
52 
53  if ( !run_info() ) {
54  set_run_info(evt.run_info());
56  } else {
57  if ( evt.run_info() && run_info() != evt.run_info() )
58  WARNING( "WriterAscii::write_event: GenEvents contain "
59  "different GenRunInfo objects from - only the "
60  "first such object will be serialized." )
61  }
62 
63 
64 
65  m_event_data->particles.clear();
66  m_event_data->vertices.clear();
67  m_event_data->links1.clear();
68  m_event_data->links2.clear();
69  m_event_data->attribute_id.clear();
70  m_event_data->attribute_name.clear();
71  m_event_data->attribute_string.clear();
72 
73  evt.write_data(*m_event_data);
74  m_tree->Fill();
76 }
77 
78 
80  if ( !m_file.IsOpen() || !run_info() ) return;
81 
82  GenRunInfoData data;
83  run_info()->write_data(data);
84 
85  int nbytes = m_file.WriteObject(&data,"GenRunInfoData");
86 
87  if( nbytes == 0 ) {
88  ERROR( "WriterRootTree: error writing GenRunInfo")
89  m_file.Close();
90  }
91 }
92 
93 
95 {
96 
97  m_tree->Write();
98  m_file.Close();
99 }
100 
102 {
103  if ( !m_file.IsOpen() ) return true;
104 
105  return false;
106 }
107 
108 } // namespace HepMC
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< int > links1
First id of the vertex links.
bool init(shared_ptr< GenRunInfo > run)
init routine
void close()
Close file stream.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Stores serializable run information.
TFile m_file
File handler.
Definition of class WriterRootTree.
std::vector< GenVertexData > vertices
Vertices.
int m_events_count
Events count. Needed to read the tree.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Stores event-related information.
std::vector< int > attribute_id
Attribute owner id.
std::vector< int > links2
Second id of the vertex links.
void write_run_info()
Write the GenRunInfo object to file.
std::vector< std::string > attribute_name
Attribute name.
Definition of template class SmartPointer.
std::vector< GenParticleData > particles
Particles.
bool failed()
Get stream error state flag.
WriterRootTree(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Default constructor.
void write_event(const GenEvent &evt)
Write event to file.
Stores serializable event information.
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:420