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-2023 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_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, std::shared_ptr<GenRunInfo> run):
31 m_tree_name(treename),
32 m_branch_name(branchname)
33{
34 m_file = TFile::Open(filename.c_str(), "RECREATE");
35 if (!init(run)) return;
36}
37
38bool WriterRootTree::init(std::shared_ptr<GenRunInfo> run )
39{
40 if ( !m_file->IsOpen() )
41 {
42 HEPMC3_ERROR_LEVEL(100,"WriterRootTree: problem opening file: " << m_file->GetName())
43 return false;
44 }
47 set_run_info(run);
48 if ( run_info() ) run_info()->write_data(*m_run_info_data);
49 m_tree = new TTree(m_tree_name.c_str(), "hepmc3_tree");
50 m_tree->Branch(m_branch_name.c_str(), m_event_data);
51 m_tree->Branch("GenRunInfo", m_run_info_data);
52 return true;
53}
54
56{
57 if ( !m_file->IsOpen() ) return;
58 bool refill = false;
59 if ( evt.run_info()&&(!run_info() || (run_info() != evt.run_info()))) { set_run_info(evt.run_info()); refill = true;}
60 if (refill)
61 {
62 m_run_info_data->weight_names.clear();
63 m_run_info_data->tool_name.clear();
64 m_run_info_data->tool_version.clear();
65 m_run_info_data->tool_description.clear();
66 m_run_info_data->attribute_name.clear();
67 m_run_info_data->attribute_string.clear();
68 run_info()->write_data(*m_run_info_data);
69 }
70
71
72
73 m_event_data->particles.clear();
74 m_event_data->vertices.clear();
75 m_event_data->links1.clear();
76 m_event_data->links2.clear();
77 m_event_data->attribute_id.clear();
78 m_event_data->attribute_name.clear();
79 m_event_data->attribute_string.clear();
80
82 m_tree->Fill();
84}
85
86
88
90{
91 m_file->WriteTObject(m_tree);
92 m_file->Close();
93 delete m_event_data;
94 delete m_run_info_data;
95}
96
98{
99 return !m_file->IsOpen();
100}
101
102} // namespace HepMC3
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
Declaration of the Verrion functions and some macros.
Definition of class WriterRootTree.
Stores event-related information.
Definition GenEvent.h:47
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:625
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:144
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.
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Writer.h:42
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Writer.h:45
HepMC3 main namespace.
Stores serializable event information.
Stores serializable run information.