Belle II Software development
SimpleVariableRecorder Class Reference

Class to write collected variables into a root file, Used by VXDQETrainingDataCollectorModule. More...

#include <SimpleVariableRecorder.h>

Public Member Functions

 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 given function setup branches.
 
 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 given function setup branches.
 
 ~SimpleVariableRecorder ()
 Destructor that closes used TTrees and TFiles.
 
 SimpleVariableRecorder (SimpleVariableRecorder &)=delete
 copy constructor needs to be implemented if needed as class has dynamic memory/resource allocation (as pointed out by cppcheck)
 
SimpleVariableRecorderoperator= (SimpleVariableRecorder &)=delete
 assignment operator ("=") needs to be implemented if needed as class has dynamic memory/resource allocation (as pointed out by cppcheck)
 
void record ()
 Record varibles by filling the TTree.
 
void write ()
 Write out TFile to root file.
 

Protected Attributes

TFile * m_tFile
 ROOT file to which should be written.
 
StoreObjPtr< RootMergeable< TTree > > m_tTree
 TTree that contains recorded variables.
 

Detailed Description

Class to write collected variables into a root file, Used by VXDQETrainingDataCollectorModule.

Definition at line 32 of file SimpleVariableRecorder.h.

Constructor & Destructor Documentation

◆ SimpleVariableRecorder() [1/2]

SimpleVariableRecorder ( const std::function< void(TTree &)> &  setBranches,
const std::string &  rootFileName,
const std::string &  treeName = "recorded_tree" 
)
inline

Construct the Recorder opening the given ROOT file and create the underlying TTree and add let the given function setup branches.

Parameters
setBranchesFunction invoked with the underlying TTree as argument which is supposed create Branches and provide the value adresses.
rootFileNameName of ROOT file to which should be written.
treeNameName of the TTree in the ROOT file.

Definition at line 45 of file SimpleVariableRecorder.h.

47 :
48 m_tFile(nullptr),
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 }
@ 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
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
TFile * m_tFile
ROOT file to which should be written.
StoreObjPtr< RootMergeable< TTree > > m_tTree
TTree that contains recorded variables.

◆ SimpleVariableRecorder() [2/2]

SimpleVariableRecorder ( std::vector< Named< float * > > &  namedVariables,
const std::string &  fileName,
const std::string &  treeName 
)
inline

Construct the Recorder opening the given ROOT file and create the underlying TTree and add let the given function setup branches.

Parameters
namedVariablesVector of collected named varibles to be turned into Branches in a TTree
fileNameName of ROOT file to which should be written.
treeNameName of the TTree in the ROOT file.

Definition at line 79 of file SimpleVariableRecorder.h.

80 :
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 { }
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...

◆ ~SimpleVariableRecorder()

Destructor that closes used TTrees and TFiles.

Definition at line 90 of file SimpleVariableRecorder.h.

91 {
93 if (m_tTree) {
94 m_tTree->get().SetDirectory(nullptr);
95 }
96 if (m_tFile) {
97 m_tFile->Close();
98 }
99 }
100 }
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

Member Function Documentation

◆ record()

void record ( )
inline

Record varibles by filling the TTree.

Definition at line 111 of file SimpleVariableRecorder.h.

111{ m_tTree->get().Fill();}

◆ write()

void write ( )
inline

Write out TFile to root file.

Definition at line 114 of file SimpleVariableRecorder.h.

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 }

Member Data Documentation

◆ m_tFile

TFile* m_tFile
protected

ROOT file to which should be written.

Definition at line 142 of file SimpleVariableRecorder.h.

◆ m_tTree

StoreObjPtr<RootMergeable<TTree> > m_tTree
protected

TTree that contains recorded variables.

Definition at line 144 of file SimpleVariableRecorder.h.


The documentation for this class was generated from the following file: