HepMC3 event record library: WriterRootTree.h Source File

HepMC3 event record library

WriterRootTree.h
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#ifndef HEPMC3_WRITERROOTTREE_H
7#define HEPMC3_WRITERROOTTREE_H
8/**
9 * @file WriterRootTree.h
10 * @brief Definition of \b class WriterRootTree
11 *
12 * @class HepMC3::WriterRootTree
13 * @brief GenEvent I/O serialization for root files based on root TTree
14 *
15 * If HepMC was compiled with path to ROOT available, this class can be used
16 * for root writing in the same manner as with HepMC::WriterAscii class.
17 *
18 * @ingroup IO
19 *
20 */
21#include <string>
22#include <memory>
23#include "HepMC3/Writer.h"
24#include "HepMC3/GenEvent.h"
25#include "HepMC3/Data/GenEventData.h"
26#include "HepMC3/Data/GenRunInfoData.h"
27
28
29// ROOT header files
30#ifdef __CINT__
31#include "TFile.h"
32#include "TTree.h"
33#else
34class TFile;
35class TTree;
36#endif
37
38namespace HepMC3
39{
40 class WriterRootTree : public Writer
41{
42//
43// Constructors
44//
45public:
46 /** @brief Default constructor
47 * @warning If file exists, it will be overwritten
48 */
49 WriterRootTree(const std::string &filename,
50 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
51 /** @brief Constructor with tree name*/
52 WriterRootTree(const std::string &filename, const std::string &treename, const std::string &branchname,
53 std::shared_ptr<GenRunInfo> run = std::shared_ptr<GenRunInfo>());
54//
55// Functions
56//
57public:
58 /** @brief Write event to file
59 *
60 * @param[in] evt Event to be serialized
61 */
62 void write_event(const GenEvent &evt) override;
63
64 /** @brief Write the GenRunInfo object to file. */
65 void write_run_info();
66
67 /** @brief Close file stream */
68 void close() override;
69
70 /** @brief Get stream error state flag */
71 bool failed() override;
72
73private:
74 /** @brief init routine */
75 bool init(std::shared_ptr<GenRunInfo> run);
76//
77// Fields
78//
79private:
80 TFile* m_file = nullptr; //!< File handler
81public:
82 TTree* m_tree = nullptr;//!< Tree handler. Public to allow simple access, e.g. custom branches.
83private:
84 int m_events_count = 0; //!< Events count. Needed to read the tree
85 GenEventData* m_event_data = nullptr; //!< Pointer to structure that holds event data
86 GenRunInfoData* m_run_info_data = nullptr; //!< Pointer to structure that holds run info data
87 std::string m_tree_name;//!< Name of TTree
88 std::string m_branch_name; //!< Name of TBranch in TTree
89};
90
91} // namespace HepMC3
92
93#endif
GenEventData.h
Definition of struct GenEventData.
GenEvent.h
Definition of class GenEvent.
GenRunInfoData.h
Definition of struct GenRunInfoData.
Writer.h
Definition of interface Writer.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
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::Writer
Writer()
Constructor.
Definition Writer.h:29
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