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< TrackingUtilities::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 variables 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 addresses.
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),
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
56 m_tTree.registerInDataStore(DataStore::c_DontWriteOut | DataStore::c_ErrorIfAlreadyRegistered);
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 }

◆ SimpleVariableRecorder() [2/2]

SimpleVariableRecorder ( std::vector< TrackingUtilities::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 variables 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 77 of file SimpleVariableRecorder.h.

78 :
79 SimpleVariableRecorder([ & namedVariables](TTree & tree)
80 {
81 for (auto& variable : namedVariables) {
82 tree.Branch(variable.getName().c_str(), variable.getValue());
83 }
84 }, fileName, treeName)
85 { }

◆ ~SimpleVariableRecorder()

Destructor that closes used TTrees and TFiles.

Definition at line 88 of file SimpleVariableRecorder.h.

89 {
90 if (!ProcHandler::parallelProcessingUsed() or ProcHandler::isOutputProcess()) {
91 if (m_tTree) {
92 m_tTree->get().SetDirectory(nullptr);
93 }
94 if (m_tFile) {
95 m_tFile->Close();
96 }
97 }
98 }

Member Function Documentation

◆ record()

void record ( )
inline

Record variables by filling the TTree.

Definition at line 109 of file SimpleVariableRecorder.h.

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

◆ write()

void write ( )
inline

Write out TFile to root file.

Definition at line 112 of file SimpleVariableRecorder.h.

113 {
114 if (!ProcHandler::parallelProcessingUsed() or ProcHandler::isOutputProcess()) {
115 if (m_tTree) {
116 // Due to some weird behaviour we have to cd to the file of the TTree
117 // before saving in order to have the tree in the correct file.
118 TDirectory* tmpDirectory = gDirectory;
119
120 TFile* tFile = m_tTree->get().GetCurrentFile();
121 if (tFile) {
122 // We still own the TFile.
123 tFile->cd();
124 m_tTree->get().Write("", TObject::kOverwrite);
125 m_tTree->get().SetDirectory(nullptr);
126 }
127 // return to previous directory
128 if (tmpDirectory) {
129 tmpDirectory->cd();
130 } else {
131 gROOT->cd();
132 }
133
134 }
135 }
136 }

Member Data Documentation

◆ m_tFile

TFile* m_tFile
protected

ROOT file to which should be written.

Definition at line 140 of file SimpleVariableRecorder.h.

◆ m_tTree

StoreObjPtr<RootMergeable<TTree> > m_tTree
protected

TTree that contains recorded variables.

Definition at line 142 of file SimpleVariableRecorder.h.


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