Belle II Software development
Recorder.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <tracking/trackingUtilities/mva/Recorder.h>
9
11#include <framework/datastore/StoreObjPtr.h>
12#include <framework/pcore/RootMergeable.h>
13
14#include <functional>
15#include <string>
16
17class TTree;
18class TFile;
19
20namespace Belle2 {
25 namespace TrackingUtilities {
26
29
30 public:
35 Impl(const std::function<void(TTree&)>& setBranches,
36 const std::string& rootFileName,
37 const std::string& treeName = "recorded_tree");
38
40 ~Impl();
41
43 void write();
44
46 void capture();
47
48 private:
50 // cppcheck-suppress unusedStructMember ; owns the output file for the lifetime of the recorder
51 TFile* m_tFile;
52
55 };
56 }
58}
59
61#include <framework/datastore/DataStore.h>
62#include <framework/pcore/ProcHandler.h>
63
64#include <TFile.h>
65#include <TTree.h>
66
67using namespace Belle2;
68using namespace TrackingUtilities;
69
70Recorder::Impl::Impl(const std::function<void(TTree&)>& setBranches,
71 const std::string& rootFileName,
72 const std::string& treeName)
73 : m_tFile(nullptr)
74 , m_tTree(treeName, DataStore::c_Persistent)
75{
76 TDirectory* ptrSavedCurrentTDirectory = gDirectory;
77
78 m_tFile = new TFile(rootFileName.c_str(), "RECREATE");
79 m_tFile->cd();
80
82 m_tTree.construct(treeName.c_str(), treeName.c_str());
83
84 if (m_tTree) {
85 setBranches(m_tTree->get());
86 }
87
88 if (ptrSavedCurrentTDirectory) {
89 ptrSavedCurrentTDirectory->cd();
90 } else {
91 gROOT->cd();
92 }
93}
94
96{
98 if (m_tTree) {
99 m_tTree->get().SetDirectory(nullptr);
100 }
101 if (m_tFile) {
102 m_tFile->Close();
103 }
104 }
105}
106
108{
110 if (m_tTree) {
111 // Due to some weird behaviour we have to cd to the file of the TTree
112 // before saving in order to have the tree in the correct file.
113 TDirectory* ptrSavedCurrentTDirectory = gDirectory;
114
115 TFile* tFile = m_tTree->get().GetCurrentFile();
116 if (tFile) {
117 // We still own the TFile.
118 tFile->cd();
119 m_tTree->get().Write("", TObject::kOverwrite);
120 m_tTree->get().SetDirectory(nullptr);
121 }
122
123 if (ptrSavedCurrentTDirectory) {
124 ptrSavedCurrentTDirectory->cd();
125 } else {
126 gROOT->cd();
127 }
128
129 }
130 }
131}
132
134{
135 m_tTree->get().Fill();
136}
137
139
140Recorder::Recorder(const std::function<void(TTree&)>& setBranches,
141 const std::string& rootFileName,
142 const std::string& treeName)
143 : m_impl(std::make_unique<Impl>(setBranches, rootFileName, treeName))
144{
145}
146
147Recorder::Recorder(const std::vector<Named<Float_t* > >& namedVariables,
148 const std::string& rootFileName,
149 const std::string& treeName) :
150 Recorder([ & namedVariables](TTree & tree)
151{
152 for (const Named<Float_t*>& namedVariable : namedVariables) {
153 std::string name = namedVariable.getName();
154 Float_t* variable = namedVariable;
155 tree.Branch(name.c_str(), variable);
156 }
157},
158rootFileName,
159treeName)
160{
161}
162
163Recorder::~Recorder() = default;
164
166{
167 m_impl->write();
168}
169
171{
172 m_impl->capture();
173}
In the store you can park objects that have to be accessed by various modules.
Definition DataStore.h:51
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition DataStore.h:71
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition DataStore.h:72
static bool isOutputProcess()
Return true if the process is an output process.
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
A mixin class to attach a name to an object.
Definition Named.h:23
~Impl()
Destructor finalising the tree.
Definition Recorder.cc:95
TFile * m_tFile
Reference to the open TFile.
Definition Recorder.cc:51
Impl(const std::function< void(TTree &)> &setBranches, const std::string &rootFileName, const std::string &treeName="recorded_tree")
Constructor creating the TFile and TTree as well as setting up the branches with the given function.
Definition Recorder.cc:70
void capture()
Capture the registered variable values and write them out.
Definition Recorder.cc:133
void write()
Write all captured variables to disk.
Definition Recorder.cc:107
StoreObjPtr< RootMergeable< TTree > > m_tTree
Reference to the TTree.
Definition Recorder.cc:54
Recorder(const std::function< void(TTree &)> &setBranches, const std::string &rootFileName, const std::string &treeName="recorded_tree")
Construct the Recorder opening the given ROOT file and create the underlying TTree and add let the gi...
Definition Recorder.cc:140
std::unique_ptr< Impl > m_impl
Pointer to implementation hiding the details.
Definition Recorder.h:71
void capture()
Capture the registered variable values and write them out.
Definition Recorder.cc:170
void write()
Write all captured variables to disk.
Definition Recorder.cc:165
~Recorder()
Destructor writing the TTree and closing the ROOT File.
Abstract base class for different kinds of events.
STL namespace.