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 <cstdio> // sprintf
13#include "HepMC3/Version.h"
14// ROOT header files
15#include "TFile.h"
16#include "TTree.h"
17
18namespace HepMC3
19{
20HEPMC3_DECLARE_WRITER_FILE(WriterRootTree)
21
22WriterRootTree::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
32WriterRootTree::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
42bool 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 {
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();
84
86 m_tree->Fill();
88}
89
90
92
94{
95 m_file->WriteTObject(m_tree);
96 m_file->Close();
97 delete m_event_data;
98 delete m_run_info_data;
99}
100
102{
103 if ( !m_file->IsOpen() ) return true;
104
105 return false;
106}
107
108} // namespace HepMC3
#define HEPMC3_ERROR(MESSAGE)
Macro for printing error messages.
Definition: Errors.h:24
Definition of class WriterRootTree.
Stores event-related information.
Definition: GenEvent.h:41
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition: GenEvent.cc:633
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition: GenEvent.h:137
Stores run-related information.
Definition: GenRunInfo.h:33
GenEvent I/O serialization for root files based on root TTree.
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
bool failed() override
Get stream error state flag.
WriterRootTree(const std::string &filename, std::shared_ptr< GenRunInfo > run=std::shared_ptr< GenRunInfo >())
Default constructor.
GenEventData * m_event_data
Pointer to structure that holds event data.
int m_events_count
Events count. Needed to read the tree.
void close() override
Close file stream.
bool init(std::shared_ptr< GenRunInfo > run)
init routine
std::string m_tree_name
Name of TTree.
std::string m_branch_name
Name of TBranch in TTree.
TFile * m_file
File handler.
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
void write_event(const GenEvent &evt) override
Write event to file.
void write_run_info()
Write the GenRunInfo object to file.
std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition: Writer.h:47
void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition: Writer.h:42
HepMC3 main namespace.
Stores serializable event information.
Definition: GenEventData.h:26
std::vector< GenVertexData > vertices
Vertices.
Definition: GenEventData.h:32
std::vector< int > links2
Second id of the vertex links.
Definition: GenEventData.h:52
std::vector< std::string > attribute_string
Attribute serialized as string.
Definition: GenEventData.h:56
std::vector< GenParticleData > particles
Particles.
Definition: GenEventData.h:31
std::vector< int > links1
First id of the vertex links.
Definition: GenEventData.h:51
std::vector< std::string > attribute_name
Attribute name.
Definition: GenEventData.h:55
std::vector< int > attribute_id
Attribute owner id.
Definition: GenEventData.h:54
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.