HepMC3 event record library: WriterRootTree.cc Source File

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
12#include "HepMC3/WriterRootTree.h"
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
30 WriterRootTree::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
38 bool 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 }
45 m_event_data = new GenEventData();
46 m_run_info_data = new GenRunInfoData();
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
55 void WriterRootTree::write_event(const GenEvent &evt)
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
81 evt.write_data(*m_event_data);
82 m_tree->Fill();
83 ++m_events_count;
84}
85
86
87 void WriterRootTree::write_run_info() {}
88
89 void WriterRootTree::close()
90{
91 m_file->WriteTObject(m_tree);
92 m_file->Close();
93 delete m_event_data;
94 delete m_run_info_data;
95}
96
97 bool WriterRootTree::failed()
98{
99 return !m_file->IsOpen();
100}
101
102} // namespace HepMC3
HEPMC3_ERROR_LEVEL
#define HEPMC3_ERROR_LEVEL(LEVEL, MESSAGE)
Macro for printing error messages.
Definition Errors.h:27
Version.h
Declaration of the Verrion functions and some macros.
WriterRootTree.h
Definition of class WriterRootTree.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::GenEvent::write_data
void write_data(GenEventData &data) const
Fill GenEventData object.
Definition GenEvent.cc:625
HepMC3::GenEvent::run_info
std::shared_ptr< GenRunInfo > run_info() const
Get a pointer to the the GenRunInfo object.
Definition GenEvent.h:144
HepMC3::GenRunInfo
Stores run-related information.
Definition GenRunInfo.h:33
HepMC3::WriterRootTree
GenEvent I/O serialization for root files based on root TTree.
Definition WriterRootTree.h:41
HepMC3::WriterRootTree::m_tree
TTree * m_tree
Tree handler. Public to allow simple access, e.g. custom branches.
Definition WriterRootTree.h:82
HepMC3::WriterRootTree::failed
bool failed() override
Get stream error state flag.
Definition WriterRootTree.cc:97
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::m_event_data
GenEventData * m_event_data
Pointer to structure that holds event data.
Definition WriterRootTree.h:85
HepMC3::WriterRootTree::m_events_count
int m_events_count
Events count. Needed to read the tree.
Definition WriterRootTree.h:84
HepMC3::WriterRootTree::close
void close() override
Close file stream.
Definition WriterRootTree.cc:89
HepMC3::WriterRootTree::init
bool init(std::shared_ptr< GenRunInfo > run)
init routine
Definition WriterRootTree.cc:38
HepMC3::WriterRootTree::m_tree_name
std::string m_tree_name
Name of TTree.
Definition WriterRootTree.h:87
HepMC3::WriterRootTree::m_branch_name
std::string m_branch_name
Name of TBranch in TTree.
Definition WriterRootTree.h:88
HepMC3::WriterRootTree::m_file
TFile * m_file
File handler.
Definition WriterRootTree.h:80
HepMC3::WriterRootTree::m_run_info_data
GenRunInfoData * m_run_info_data
Pointer to structure that holds run info data.
Definition WriterRootTree.h:86
HepMC3::WriterRootTree::write_event
void write_event(const GenEvent &evt) override
Write event to file.
Definition WriterRootTree.cc:55
HepMC3::WriterRootTree::write_run_info
void write_run_info()
Write the GenRunInfo object to file.
Definition WriterRootTree.cc:87
HepMC3::Writer::set_run_info
virtual void set_run_info(std::shared_ptr< GenRunInfo > run)
Set the global GenRunInfo object.
Definition Writer.h:42
HepMC3::Writer::run_info
virtual std::shared_ptr< GenRunInfo > run_info() const
Get the global GenRunInfo object.
Definition Writer.h:45
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::GenEventData
Stores serializable event information.
Definition GenEventData.h:26
HepMC3::GenRunInfoData
Stores serializable run information.
Definition GenRunInfoData.h:23