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/trackFindingCDC/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 TrackFindingCDC {
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 TFile* m_tFile;
51
54 };
55 }
57}
58
60#include <framework/datastore/DataStore.h>
61#include <framework/pcore/ProcHandler.h>
62
63#include <TFile.h>
64#include <TTree.h>
65
66using namespace Belle2;
67using namespace TrackFindingCDC;
68
69Recorder::Impl::Impl(const std::function<void(TTree&)>& setBranches,
70 const std::string& rootFileName,
71 const std::string& treeName)
72 : m_tFile(nullptr)
73 , m_tTree(treeName, DataStore::c_Persistent)
74{
75 TDirectory* ptrSavedCurrentTDirectory = gDirectory;
76
77 m_tFile = new TFile(rootFileName.c_str(), "RECREATE");
78 m_tFile->cd();
79
81 m_tTree.construct(treeName.c_str(), treeName.c_str());
82
83 if (m_tTree) {
84 setBranches(m_tTree->get());
85 }
86
87 if (ptrSavedCurrentTDirectory) {
88 ptrSavedCurrentTDirectory->cd();
89 } else {
90 gROOT->cd();
91 }
92}
93
95{
97 if (m_tTree) {
98 m_tTree->get().SetDirectory(nullptr);
99 }
100 if (m_tFile) {
101 m_tFile->Close();
102 }
103 }
104}
105
107{
109 if (m_tTree) {
110 // Due to some weird behaviour we have to cd to the file of the TTree
111 // before saving in order to have the tree in the correct file.
112 TDirectory* ptrSavedCurrentTDirectory = gDirectory;
113
114 TFile* tFile = m_tTree->get().GetCurrentFile();
115 if (tFile) {
116 // We still own the TFile.
117 tFile->cd();
118 m_tTree->get().Write("", TObject::kOverwrite);
119 m_tTree->get().SetDirectory(nullptr);
120 }
121
122 if (ptrSavedCurrentTDirectory) {
123 ptrSavedCurrentTDirectory->cd();
124 } else {
125 gROOT->cd();
126 }
127
128 }
129 }
130}
131
133{
134 m_tTree->get().Fill();
135}
136
139Recorder::Recorder(const std::function<void(TTree&)>& setBranches,
140 const std::string& rootFileName,
141 const std::string& treeName)
142 : m_impl(std::make_unique<Impl>(setBranches, rootFileName, treeName))
143{
144}
145
146Recorder::Recorder(const std::vector<Named<Float_t* > >& namedVariables,
147 const std::string& rootFileName,
148 const std::string& treeName) :
149 Recorder([ & namedVariables](TTree & tree)
150{
151 for (const Named<Float_t*>& namedVariable : namedVariables) {
152 std::string name = namedVariable.getName();
153 Float_t* variable = namedVariable;
154 tree.Branch(name.c_str(), variable);
155 }
156},
157rootFileName,
158treeName)
159{
160}
161
162Recorder::~Recorder() = default;
163
165{
166 m_impl->write();
167}
168
170{
171 m_impl->capture();
172}
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.
Definition: ProcHandler.cc:232
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:226
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
Record the MVA variables.
Definition: Recorder.cc:28
~Impl()
Destructor finalising the tree.
Definition: Recorder.cc:94
TFile * m_tFile
Reference to the open TFile.
Definition: Recorder.cc:50
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:69
void capture()
Capture the registered variable values and write them out.
Definition: Recorder.cc:132
void write()
Write all captured variables to disk.
Definition: Recorder.cc:106
StoreObjPtr< RootMergeable< TTree > > m_tTree
Reference to the TTree.
Definition: Recorder.cc:53
Class to fill a tree from a set of variables.
Definition: Recorder.h:29
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:139
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:169
void write()
Write all captured variables to disk.
Definition: Recorder.cc:164
~Recorder()
Destructor writing the TTree and closing the ROOT File.
Abstract base class for different kinds of events.
STL namespace.