Belle II Software  release-08-01-10
SimpleVariableRecorder.h
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 
9 #pragma once
10 #include <functional>
11 #include <string>
12 #include <vector>
13 
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/pcore/RootMergeable.h>
16 #include <framework/pcore/ProcHandler.h>
17 #include <tracking/trackFindingVXD/utilities/Named.h>
18 #include <TFile.h>
19 #include <TTree.h>
20 #include <TROOT.h>
21 
22 
23 namespace Belle2 {
33 
34  public:
35 
45  SimpleVariableRecorder(const std::function<void(TTree&)>& setBranches,
46  const std::string& rootFileName,
47  const std::string& treeName = "recorded_tree") :
48  m_tFile(nullptr),
49  m_tTree(treeName, DataStore::c_Persistent)
50  {
51  TDirectory* ptrSavedCurrentTDirectory = gDirectory;
52 
53  m_tFile = new TFile(rootFileName.c_str(), "RECREATE");
54  m_tFile->cd();
55 
57  m_tTree.construct(treeName.c_str(), treeName.c_str());
58  if (m_tTree) {
59  setBranches(m_tTree->get());
60  }
61 
62  if (ptrSavedCurrentTDirectory) {
63  ptrSavedCurrentTDirectory->cd();
64  } else {
65  gROOT->cd();
66  }
67  }
68 
77  // cppcheck does not recognize that m_tfile is initialized by calling the other constructor
78  // cppcheck-suppress uninitMemberVar
79  SimpleVariableRecorder(std::vector<Named<float*>>& namedVariables, const std::string& fileName,
80  const std::string& treeName) :
81  SimpleVariableRecorder([ & namedVariables](TTree & tree)
82  {
83  for (auto& variable : namedVariables) {
84  tree.Branch(variable.getName().c_str(), variable.getValue());
85  }
86  }, fileName, treeName)
87  { }
88 
91  {
93  if (m_tTree) {
94  m_tTree->get().SetDirectory(nullptr);
95  }
96  if (m_tFile) {
97  m_tFile->Close();
98  }
99  }
100  }
101 
108 
109 
111  void record() { m_tTree->get().Fill();}
112 
114  void write()
115  {
117  if (m_tTree) {
118  // Due to some weird behaviour we have to cd to the file of the TTree
119  // before saving in order to have the tree in the correct file.
120  TDirectory* tmpDirectory = gDirectory;
121 
122  TFile* tFile = m_tTree->get().GetCurrentFile();
123  if (tFile) {
124  // We still own the TFile.
125  tFile->cd();
126  m_tTree->get().Write("", TObject::kOverwrite);
127  m_tTree->get().SetDirectory(nullptr);
128  }
129  // return to previous directory
130  if (tmpDirectory) {
131  tmpDirectory->cd();
132  } else {
133  gROOT->cd();
134  }
135 
136  }
137  }
138  }
139 
140  protected:
142  TFile* m_tFile;
145  };
147 }
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
Class to write collected variables into a root file, Used by VXDQETrainingDataCollectorModule.
TFile * m_tFile
ROOT file to which should be written.
SimpleVariableRecorder(std::vector< Named< float * >> &namedVariables, const std::string &fileName, const std::string &treeName)
Construct the Recorder opening the given ROOT file and create the underlying TTree and add let the gi...
SimpleVariableRecorder & operator=(SimpleVariableRecorder &)=delete
assignment operator ("=") needs to be implemented if needed as class has dynamic memory/resource allo...
SimpleVariableRecorder(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...
void write()
Write out TFile to root file.
SimpleVariableRecorder(SimpleVariableRecorder &)=delete
copy constructor needs to be implemented if needed as class has dynamic memory/resource allocation (a...
~SimpleVariableRecorder()
Destructor that closes used TTrees and TFiles.
void record()
Record varibles by filling the TTree.
StoreObjPtr< RootMergeable< TTree > > m_tTree
TTree that contains recorded variables.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
Abstract base class for different kinds of events.