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 */
12#include <cstdio> // sprintf
13// ROOT header files
14#include "TFile.h"
15#include "TTree.h"
16
17namespace HepMC3
18{
19
20WriterRootTree::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
30WriterRootTree::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
40bool 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 {
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();
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
#define ERROR(MESSAGE)
Macro for printing error messages.
Definition Errors.h:23
Definition of class WriterRootTree.
Stores event-related information.
Definition GenEvent.h:42
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:647
shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:125
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
WriterRootTree(const std::string &filename, shared_ptr< GenRunInfo > run=shared_ptr< GenRunInfo >())
Default constructor.
GenEventData * m_event_data
Pointer to structure that holds event data.
void close()
Close file stream.
int m_events_count
Events count. Needed to read the tree.
std::string m_tree_name
Name of TTree.
std::string m_branch_name
Name of TBranch in TTree.
bool init(shared_ptr< GenRunInfo > run)
init routine
TFile * m_file
File handler.
bool failed()
Get stream error state flag.
void write_event(const GenEvent &evt)
Write event to file.
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
void write_run_info()
Write the GenRunInfo object to file.
void set_run_info(shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Writer.h:39
shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Writer.h:44
HepMC3 main namespace.
Definition ReaderGZ.h:28
Stores serializable event information.
std::vector< GenVertexData > vertices
Vertices.
std::vector< int > links2
Second id of the vertex links.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< GenParticleData > particles
Particles.
std::vector< int > links1
First id of the vertex links.
std::vector< std::string > attribute_name
Attribute name.
std::vector< int > attribute_id
Attribute owner id.
Stores serializable run information.
std::vector< std::string > tool_name
Tool names.
std::vector< std::string > tool_version
Tool versions.
std::vector< std::string > attribute_string
Attribute serialized as string.
std::vector< std::string > attribute_name
Attribute name.
std::vector< std::string > tool_description
Tool descriptions.
std::vector< std::string > weight_names
Weight names.