HepMC3 event record library
WriterRootTree.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 WriterRootTree.cc
8  * @brief Implementation of \b class WriterRootTree
9  *
10  */
11 #include "HepMC3/WriterRootTree.h"
12 #include <cstdio> // sprintf
13 // ROOT header files
14 #include "TFile.h"
15 #include "TTree.h"
16 
17 namespace HepMC3
18 {
19 
20 WriterRootTree::WriterRootTree(const std::string &filename, shared_ptr<GenRunInfo> run):
21  m_tree(0),
22  m_events_count(0),
23  m_tree_name("hepmc3_tree"),
24  m_branch_name("hepmc3_event")
25 {
26  m_file = TFile::Open(filename.c_str(),"RECREATE");
27  if (!init(run)) return;
28 }
29 
30 WriterRootTree::WriterRootTree(const std::string &filename,const std::string &treename,const std::string &branchname, shared_ptr<GenRunInfo> run):
31  m_tree(0),
32  m_events_count(0),
33  m_tree_name(treename.c_str()),
34  m_branch_name(branchname.c_str())
35 {
36  m_file = TFile::Open(filename.c_str(),"RECREATE");
37  if (!init(run)) return;
38 }
39 
40 bool WriterRootTree::init(shared_ptr<GenRunInfo> run )
41 {
42  if ( !m_file->IsOpen() )
43  {
44  ERROR( "WriterRootTree: problem opening file: " <<m_file->GetName() )
45  return false;
46  }
49  set_run_info(run);
50  if ( run_info() ) run_info()->write_data(*m_run_info_data);
51  m_tree= new TTree(m_tree_name.c_str(),"hepmc3_tree");
52  m_tree->Branch(m_branch_name.c_str(), m_event_data);
53  m_tree->Branch("GenRunInfo", m_run_info_data);
54  return true;
55 }
56 
58 {
59  if ( !m_file->IsOpen() ) return;
60  bool refill=false;
61  if ( evt.run_info()&&(!run_info() || (run_info() != evt.run_info()))) { set_run_info(evt.run_info()); refill=true;}
62  if (refill)
63  {
65  m_run_info_data->tool_name.clear();
70  run_info()->write_data(*m_run_info_data);
71  }
72 
73 
74 
75  m_event_data->particles.clear();
76  m_event_data->vertices.clear();
77  m_event_data->links1.clear();
78  m_event_data->links2.clear();
79  m_event_data->attribute_id.clear();
82 
84  m_tree->Fill();
86 }
87 
88 
90 
92 {
93 
94  m_file->WriteTObject(m_tree);
95  m_file->Close();
96  delete m_event_data;
97  delete m_run_info_data;
98 }
99 
101 {
102  if ( !m_file->IsOpen() ) return true;
103 
104  return false;
105 }
106 
107 } // namespace HepMC3
std::string m_tree_name
Name of TTree.
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
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:647
std::vector< std::string > attribute_name
Attribute name.
std::vector< int > links2
Second id of the vertex links.
Definition: GenEventData.h:52
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
Definition of class WriterRootTree.
void close()
Close file stream.
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:125
std::string m_branch_name
Name of TBranch in TTree.
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
void write_event(const GenEvent &evt)
Write event to file.
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
int m_events_count
Events count. Needed to read the tree.
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Writer.h:44
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.
bool failed()
Get stream error state flag.
bool init(shared_ptr< GenRunInfo > run)
init routine
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Writer.h:39
GenEventData * m_event_data
Pointer to structure that holds event data.
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
WriterRootTree(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Default constructor.
TFile * m_file
File handler.
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
std::vector< std::string > tool_version
Tool versions.
void write_run_info()
Write the GenRunInfo object to file.
std::vector< std::string > weight_names
Weight names.
std::vector< std::string > tool_description
Tool descriptions.