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 "HepMC3/Version.h"
13 #include <cstdio> // sprintf
14 // ROOT header files
15 #include "TFile.h"
16 #include "TTree.h"
17 
18 namespace HepMC3
19 {
20 HEPMC3_DECLARE_WRITER_FILE(WriterRootTree)
21 
22 WriterRootTree::WriterRootTree(const std::string &filename, std::shared_ptr<GenRunInfo> run):
23  m_tree(0),
24  m_events_count(0),
25  m_tree_name("hepmc3_tree"),
26  m_branch_name("hepmc3_event")
27 {
28  m_file = TFile::Open(filename.c_str(),"RECREATE");
29  if (!init(run)) return;
30 }
31 
32 WriterRootTree::WriterRootTree(const std::string &filename,const std::string &treename,const std::string &branchname, std::shared_ptr<GenRunInfo> run):
33  m_tree(0),
34  m_events_count(0),
35  m_tree_name(treename.c_str()),
36  m_branch_name(branchname.c_str())
37 {
38  m_file = TFile::Open(filename.c_str(),"RECREATE");
39  if (!init(run)) return;
40 }
41 
42 bool WriterRootTree::init(std::shared_ptr<GenRunInfo> run )
43 {
44  if ( !m_file->IsOpen() )
45  {
46  HEPMC3_ERROR( "WriterRootTree: problem opening file: " <<m_file->GetName() )
47  return false;
48  }
51  set_run_info(run);
52  if ( run_info() ) run_info()->write_data(*m_run_info_data);
53  m_tree= new TTree(m_tree_name.c_str(),"hepmc3_tree");
54  m_tree->Branch(m_branch_name.c_str(), m_event_data);
55  m_tree->Branch("GenRunInfo", m_run_info_data);
56  return true;
57 }
58 
60 {
61  if ( !m_file->IsOpen() ) return;
62  bool refill=false;
63  if ( evt.run_info()&&(!run_info() || (run_info() != evt.run_info()))) { set_run_info(evt.run_info()); refill=true;}
64  if (refill)
65  {
67  m_run_info_data->tool_name.clear();
72  run_info()->write_data(*m_run_info_data);
73  }
74 
75 
76 
77  m_event_data->particles.clear();
78  m_event_data->vertices.clear();
79  m_event_data->links1.clear();
80  m_event_data->links2.clear();
81  m_event_data->attribute_id.clear();
84 
86  m_tree->Fill();
88 }
89 
90 
92 
94 {
95 
96  m_file->WriteTObject(m_tree);
97  m_file->Close();
98  delete m_event_data;
99  delete m_run_info_data;
100 }
101 
103 {
104  if ( !m_file->IsOpen() ) return true;
105 
106  return false;
107 }
108 
109 } // namespace HepMC3
std::string m_tree_name
Name of TTree.
std::vector< int > attribute_id
Attribute owner id.
Definition: GenEventData.h:54
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.
bool failed() override
Get stream error state flag.
Stores run-related information.
Definition: GenRunInfo.h:33
Definition of class WriterRootTree.
std::string m_branch_name
Name of TBranch in TTree.
WriterRootTree(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Default constructor.
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
void write_event(const GenEvent &evt) override
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.
Stores event-related information.
Definition: GenEvent.h:41
Stores serializable event information.
Definition: GenEventData.h:26
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:645
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
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Writer.h:47
Stores serializable run information.
GenEvent I/O serialization for root files based on root TTree.
void close() override
Close file stream.
GenEventData * m_event_data
Pointer to structure that holds event data.
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Writer.h:42
TFile * m_file
File handler.
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:23
std::vector< std::string > tool_version
Tool versions.
void write_run_info()
Write the GenRunInfo object to file.
bool init(std::shared_ptr< GenRunInfo > run)
init routine
std::vector< std::string > weight_names
Weight names.
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:124
std::vector< std::string > tool_description
Tool descriptions.