Belle II Software  release-05-02-19
Recorder.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/trackFindingCDC/mva/Recorder.h>
11 
13 #include <framework/datastore/StoreObjPtr.h>
14 #include <framework/pcore/RootMergeable.h>
15 
16 #include <functional>
17 #include <string>
18 
19 class TTree;
20 class TFile;
21 
22 namespace Belle2 {
27  namespace TrackFindingCDC {
28 
31 
32  public:
37  Impl(const std::function<void(TTree&)>& setBranches,
38  const std::string& rootFileName,
39  const std::string& treeName = "recorded_tree");
40 
42  ~Impl();
43 
45  void write();
46 
48  void capture();
49 
50  private:
52  TFile* m_tFile;
53 
56  };
57  }
59 }
60 
62 #include <framework/datastore/DataStore.h>
63 #include <framework/pcore/ProcHandler.h>
64 
65 #include <TFile.h>
66 #include <TTree.h>
67 
68 using namespace Belle2;
69 using namespace TrackFindingCDC;
70 
71 Recorder::Impl::Impl(const std::function<void(TTree&)>& setBranches,
72  const std::string& rootFileName,
73  const std::string& treeName)
74  : m_tFile(nullptr)
75  , m_tTree(treeName, DataStore::c_Persistent)
76 {
77  TDirectory* ptrSavedCurrentTDirectory = gDirectory;
78 
79  m_tFile = new TFile(rootFileName.c_str(), "RECREATE");
80  m_tFile->cd();
81 
83  m_tTree.construct(treeName.c_str(), treeName.c_str());
84 
85  if (m_tTree) {
86  setBranches(m_tTree->get());
87  }
88 
89  if (ptrSavedCurrentTDirectory) {
90  ptrSavedCurrentTDirectory->cd();
91  } else {
92  gROOT->cd();
93  }
94 }
95 
97 {
99  if (m_tTree) {
100  m_tTree->get().SetDirectory(nullptr);
101  }
102  if (m_tFile) {
103  m_tFile->Close();
104  }
105  }
106 }
107 
109 {
111  if (m_tTree) {
112  // Due to some weird behaviour we have to cd to the file of the TTree
113  // before saving in order to have the tree in the correct file.
114  TDirectory* ptrSavedCurrentTDirectory = gDirectory;
115 
116  TFile* tFile = m_tTree->get().GetCurrentFile();
117  if (tFile) {
118  // We still own the TFile.
119  tFile->cd();
120  m_tTree->get().Write("", TObject::kOverwrite);
121  m_tTree->get().SetDirectory(nullptr);
122  }
123 
124  if (ptrSavedCurrentTDirectory) {
125  ptrSavedCurrentTDirectory->cd();
126  } else {
127  gROOT->cd();
128  }
129 
130  }
131  }
132 }
133 
135 {
136  m_tTree->get().Fill();
137 }
138 
141 Recorder::Recorder(const std::function<void(TTree&)>& setBranches,
142  const std::string& rootFileName,
143  const std::string& treeName)
144  : m_impl(std::make_unique<Impl>(setBranches, rootFileName, treeName))
145 {
146 }
147 
148 Recorder::Recorder(const std::vector<Named<Float_t* > >& namedVariables,
149  const std::string& rootFileName,
150  const std::string& treeName) :
151  Recorder([ & namedVariables](TTree & tree)
152 {
153  for (const Named<Float_t*>& namedVariable : namedVariables) {
154  std::string name = namedVariable.getName();
155  Float_t* variable = namedVariable;
156  tree.Branch(name.c_str(), variable);
157  }
158 },
159 rootFileName,
160 treeName)
161 {
162 }
163 
164 Recorder::~Recorder() = default;
165 
167 {
168  m_impl->write();
169 }
170 
172 {
173  m_impl->capture();
174 }
Belle2::TrackFindingCDC::Recorder::Impl::m_tTree
StoreObjPtr< RootMergeable< TTree > > m_tTree
Reference to the TTree.
Definition: Recorder.cc:55
Belle2::TrackFindingCDC::Named< Float_t * >
Belle2::TrackFindingCDC::Recorder::Impl::Impl
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:71
Belle2::TrackFindingCDC::Recorder::Impl::m_tFile
TFile * m_tFile
Reference to the open TFile.
Definition: Recorder.cc:52
Belle2::TrackFindingCDC::Recorder::Impl::~Impl
~Impl()
Destructor finalising the tree.
Definition: Recorder.cc:96
Belle2::TrackFindingCDC::Recorder::capture
void capture()
Capture the registered variable values and write them out.
Definition: Recorder.cc:171
Belle2::TrackFindingCDC::Recorder::Recorder
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...
Belle2::TrackFindingCDC::Recorder::Impl::capture
void capture()
Capture the registered variable values and write them out.
Definition: Recorder.cc:134
Belle2::TrackFindingCDC::Recorder
Class to fill a tree from a set of variables.
Definition: Recorder.h:31
Belle2::DataStore::c_DontWriteOut
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:73
Belle2::ProcHandler::isOutputProcess
static bool isOutputProcess()
Return true if the process is an output process.
Definition: ProcHandler.cc:227
Belle2::TrackFindingCDC::Recorder::~Recorder
~Recorder()
Destructor writing the TTree and closing the ROOT File.
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::TrackFindingCDC::Recorder::m_impl
std::unique_ptr< Impl > m_impl
Pointer to implementation hiding the details.
Definition: Recorder.h:70
Belle2::TrackFindingCDC::Recorder::Impl::write
void write()
Write all captured variables to disk.
Definition: Recorder.cc:108
Belle2::TrackFindingCDC::Recorder::Impl
Record the MVA variables.
Definition: Recorder.cc:30
Belle2::DataStore::c_ErrorIfAlreadyRegistered
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:74
Belle2::ProcHandler::parallelProcessingUsed
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:221
Belle2::TrackFindingCDC::Recorder::write
void write()
Write all captured variables to disk.
Definition: Recorder.cc:166
Belle2::DataStore
In the store you can park objects that have to be accessed by various modules.
Definition: DataStore.h:53