Belle II Software  release-05-02-19
SimpleVariableRecorder.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost, Jonas Wagner *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 #include <functional>
13 #include <string>
14 #include <vector>
15 
16 #include <framework/datastore/StoreObjPtr.h>
17 #include <framework/pcore/RootMergeable.h>
18 #include <framework/pcore/ProcHandler.h>
19 #include <tracking/trackFindingVXD/utilities/Named.h>
20 #include <TFile.h>
21 #include <TTree.h>
22 #include <TROOT.h>
23 
24 
25 namespace Belle2 {
34  class SimpleVariableRecorder {
35 
36  public:
37 
47  SimpleVariableRecorder(const std::function<void(TTree&)>& setBranches,
48  const std::string& rootFileName,
49  const std::string& treeName = "recorded_tree") :
50  m_tFile(nullptr),
51  m_tTree(treeName, DataStore::c_Persistent)
52  {
53  TDirectory* ptrSavedCurrentTDirectory = gDirectory;
54 
55  m_tFile = new TFile(rootFileName.c_str(), "RECREATE");
56  m_tFile->cd();
57 
59  m_tTree.construct(treeName.c_str(), treeName.c_str());
60  if (m_tTree) {
61  setBranches(m_tTree->get());
62  }
63 
64  if (ptrSavedCurrentTDirectory) {
65  ptrSavedCurrentTDirectory->cd();
66  } else {
67  gROOT->cd();
68  }
69  }
70 
79  // cppcheck does not recognize that m_tfile is initialized by calling the other constructor
80  // cppcheck-suppress uninitMemberVar
81  SimpleVariableRecorder(std::vector<Named<float*>>& namedVariables, const std::string& fileName,
82  const std::string& treeName) :
83  SimpleVariableRecorder([ & namedVariables](TTree & tree)
84  {
85  for (auto& variable : namedVariables) {
86  tree.Branch(variable.getName().c_str(), variable.getValue());
87  }
88  }, fileName, treeName)
89  { }
90 
93  {
95  if (m_tTree) {
96  m_tTree->get().SetDirectory(nullptr);
97  }
98  if (m_tFile) {
99  m_tFile->Close();
100  }
101  }
102  }
103 
110 
111 
113  void record() { m_tTree->get().Fill();}
114 
116  void write()
117  {
119  if (m_tTree) {
120  // Due to some weird behaviour we have to cd to the file of the TTree
121  // before saving in order to have the tree in the correct file.
122  TDirectory* tmpDirectory = gDirectory;
123 
124  TFile* tFile = m_tTree->get().GetCurrentFile();
125  if (tFile) {
126  // We still own the TFile.
127  tFile->cd();
128  m_tTree->get().Write("", TObject::kOverwrite);
129  m_tTree->get().SetDirectory(nullptr);
130  }
131  // return to previous directory
132  if (tmpDirectory) {
133  tmpDirectory->cd();
134  } else {
135  gROOT->cd();
136  }
137 
138  }
139  }
140  }
141 
142  protected:
144  TFile* m_tFile;
146  StoreObjPtr<RootMergeable<TTree> > m_tTree;
147  };
149 }
Belle2::SimpleVariableRecorder::SimpleVariableRecorder
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...
Definition: SimpleVariableRecorder.h:55
Belle2::SimpleVariableRecorder::m_tTree
StoreObjPtr< RootMergeable< TTree > > m_tTree
TTree that contains recorded variables.
Definition: SimpleVariableRecorder.h:154
Belle2::DataStore::c_DontWriteOut
@ c_DontWriteOut
Object/array should be NOT saved by output modules.
Definition: DataStore.h:73
Belle2::SimpleVariableRecorder::~SimpleVariableRecorder
~SimpleVariableRecorder()
Destructor that closes used TTrees and TFiles.
Definition: SimpleVariableRecorder.h:100
Belle2::ProcHandler::isOutputProcess
static bool isOutputProcess()
Return true if the process is an output process.
Definition: ProcHandler.cc:227
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::SimpleVariableRecorder::m_tFile
TFile * m_tFile
ROOT file to which should be written.
Definition: SimpleVariableRecorder.h:152
Belle2::DataStore::c_ErrorIfAlreadyRegistered
@ c_ErrorIfAlreadyRegistered
If the object/array was already registered, produce an error (aborting initialisation).
Definition: DataStore.h:74
Belle2::SimpleVariableRecorder::write
void write()
Write out TFile to root file.
Definition: SimpleVariableRecorder.h:124
Belle2::SimpleVariableRecorder::record
void record()
Record varibles by filling the TTree.
Definition: SimpleVariableRecorder.h:121
Belle2::ProcHandler::parallelProcessingUsed
static bool parallelProcessingUsed()
Returns true if multiple processes have been spawned, false in single-core mode.
Definition: ProcHandler.cc:221
Belle2::SimpleVariableRecorder::operator=
SimpleVariableRecorder & operator=(SimpleVariableRecorder &)=delete
assignment operator ("=") needs to be implemented if needed as class has dynamic memory/resource allo...