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
HepMC3::GenRunInfoData::tool_name
std::vector< std::string > tool_name
Tool names.
Definition: GenRunInfoData.h:26
HepMC3::GenEvent
Stores event-related information.
Definition: GenEvent.h:41
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::WriterRootTree::m_branch_name
std::string m_branch_name
Name of TBranch in TTree.
Definition: WriterRootTree.h:87
WriterRootTree.h
Definition of class WriterRootTree.
HepMC3
HepMC3 main namespace.
Definition: AnalysisExample.h:18
HepMC3::GenEventData
Stores serializable event information.
Definition: GenEventData.h:26
HepMC3::GenEventData::links1
std::vector< int > links1
First id of the vertex links.
Definition: GenEventData.h:51
HepMC3::Writer::set_run_info
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Writer.h:42
HepMC3::GenEvent::write_data
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:645
HepMC3::WriterRootTree::m_tree
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
Definition: WriterRootTree.h:81
HepMC3::GenEventData::links2
std::vector< int > links2
Second id of the vertex links.
Definition: GenEventData.h:52
HepMC3::WriterRootTree::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition: WriterRootTree.cc:91
HepMC3::Writer::run_info
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Writer.h:47
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::WriterRootTree::m_file
TFile * m_file
File handler.
Definition: WriterRootTree.h:79
HepMC3::WriterRootTree::m_event_data
GenEventData * m_event_data
Pointer to structure that holds event data.
Definition: WriterRootTree.h:84
HepMC3::GenRunInfoData
Stores serializable run information.
Definition: GenRunInfoData.h:23
HepMC3::WriterRootTree::close
void close()
Close file stream.
Definition: WriterRootTree.cc:93
HepMC3::WriterRootTree::WriterRootTree
WriterRootTree(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Default constructor.
Definition: WriterRootTree.cc:22
HepMC3::WriterRootTree::write_event
void write_event(const GenEvent &evt)
Write event to file.
Definition: WriterRootTree.cc:59
HepMC3::GenEventData::attribute_string
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition: GenEventData.h:56
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::GenEventData::particles
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
HepMC3::WriterRootTree::m_events_count
int m_events_count
Events count. Needed to read the tree.
Definition: WriterRootTree.h:83
HepMC3::GenEvent::run_info
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:124
HepMC3::WriterRootTree::m_run_info_data
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
Definition: WriterRootTree.h:85
HepMC3::WriterRootTree::failed
bool failed()
Get stream error state flag.
Definition: WriterRootTree.cc:102
HepMC3::GenRunInfoData::tool_version
std::vector< std::string > tool_version
Tool versions.
Definition: GenRunInfoData.h:27
HepMC3::WriterRootTree::m_tree_name
std::string m_tree_name
Name of TTree.
Definition: WriterRootTree.h:86
HepMC3::WriterRootTree::init
bool init(std::shared_ptr< GenRunInfo > run)
init routine
Definition: WriterRootTree.cc:42
HepMC3::GenEventData::vertices
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
HepMC3::WriterRootTree
GenEvent I/O serialization for root files based on root TTree.
Definition: WriterRootTree.h:38
HepMC3::GenEventData::attribute_id
std::vector< int > attribute_id
Attribute owner id.
Definition: GenEventData.h:54
HepMC3::GenRunInfo
Stores run-related information.
Definition: GenRunInfo.h:33