Belle II Software development
CalibObjManager Class Reference

Manager class for collector registered data. Handles much of the TDirectory/TObject manipulation. More...

#include <CalibObjManager.h>

Public Member Functions

 CalibObjManager (TDirectory *dir=nullptr)
 Constructor.
 
virtual ~CalibObjManager ()
 Destructor: Every object we are managing is ultimately either saved into a file to write out, or was read in from an open file, or was created via merging objects together and passed out.
 
void setDirectory (TDirectory *dir)
 Change the directory that we will be using to find/store all our objects, we don't own it.
 
void addObject (const std::string &name, std::shared_ptr< TNamed > object)
 Add a new object to manage, this is used as a template for creating future/missing objects.
 
void writeCurrentObjects (const Calibration::ExpRun &expRun)
 For each templated object we know about, we find an in memory object for this exprun and write to the TDirectory.
 
void clearCurrentObjects (const Calibration::ExpRun &expRun)
 Deletes all in-memory objects in the exprun directories for all the collector objects we know about.
 
void createDirectories ()
 Each object gets its own TDirectory under the main manager directory to store its objects.
 
void createExpRunDirectories (Calibration::ExpRun &expRun) const
 For each templated object, we create a new TDirectory for this exprun.
 
unsigned int getHighestIndexObject (const std::string &name, const TDirectory *dir) const
 Scans the directory to get the highest "_i" index of an object with this name.
 
void deleteHeldObjects ()
 Clears the map of templated objects -> causing their destruction.
 
bool isRegistered (const std::string &name) const
 Checks for the existence of a name in the templated object map to see if we registered the object.
 
template<class T >
T * getObject (const std::string &name, const Belle2::Calibration::ExpRun expRun)
 Gets the collector object of this name for the given exprun.
 
template<>
TTree * cloneObj (TTree *source, const std::string &newName) const
 Template specialization for TTree needs to be defined here to prevent automatic specialization being created.
 

Private Member Functions

template<class T >
T * cloneObj (T *source, const std::string &newName) const
 Clone object.
 
std::string getSuffix (const Calibration::ExpRun &key) const
 We rename objects based on the Exp,Run that they contain so we need to generate a nice naming convention.
 
std::string getSuffix (const EventMetaData &emd) const
 Sometimes it's nice to just pass in the EventMetaData instead of converting manually.
 
std::string getObjectExpRunName (const std::string &name, const Calibration::ExpRun &expRun) const
 Get object experiment and run name.
 
unsigned int extractKeyIndex (const std::string &keyName) const
 Extract key index.
 

Private Attributes

TDirectory * m_dir
 The TDirectory where all of our managed objects should be found, and where we should create new ones.
 
std::map< std::string, std::shared_ptr< TNamed > > m_templateObjects
 The objects that we are managing, these are template objects for all future objects for each (Exp,Run).
 

Detailed Description

Manager class for collector registered data. Handles much of the TDirectory/TObject manipulation.

Definition at line 29 of file CalibObjManager.h.

Constructor & Destructor Documentation

◆ CalibObjManager()

CalibObjManager ( TDirectory *  dir = nullptr)
inline

Constructor.

Definition at line 33 of file CalibObjManager.h.

33: m_dir(dir) {};
TDirectory * m_dir
The TDirectory where all of our managed objects should be found, and where we should create new ones.

◆ ~CalibObjManager()

virtual ~CalibObjManager ( )
inlinevirtual

Destructor: Every object we are managing is ultimately either saved into a file to write out, or was read in from an open file, or was created via merging objects together and passed out.

Ultimately the objects that are saved or read from a file are memory managed by a TFile/TDirectory. The created/merged objects are returned and are the responsibility of the user to clean up once finished with them. So we are only really responsible for cleaning up the template object which belongs to no directory and is never used directly by an outside user except when creating object that is passed in for the template.

Definition at line 42 of file CalibObjManager.h.

42{m_templateObjects.clear();}
std::map< std::string, std::shared_ptr< TNamed > > m_templateObjects
The objects that we are managing, these are template objects for all future objects for each (Exp,...

Member Function Documentation

◆ cloneObj()

T * cloneObj ( T *  source,
const std::string &  newName 
) const
inlineprivate

Clone object.

Definition at line 119 of file CalibObjManager.h.

120 {
121 B2DEBUG(100, "Held object " << source->GetName() << " will be treated as a generic TNamed and have Clone(newname) called.");
122 return dynamic_cast<T*>(source->Clone(newName.c_str()));
123 }

◆ getObject()

T * getObject ( const std::string &  name,
const Belle2::Calibration::ExpRun  expRun 
)
inline

Gets the collector object of this name for the given exprun.

If there already exists some objects it gets the highest index one if it is in-memory. If the highest index object is file resident it creates a new in memory object with a higher index.

Definition at line 80 of file CalibObjManager.h.

81 {
82 std::string objectDirName = name + '/' + getObjectExpRunName(name, expRun);
83 TDirectory* objectDir = m_dir->GetDirectory(objectDirName.c_str());
84 // Does the object name have a directory available? (framework problem if not)
85 if (not objectDir) {
86 // Is the object name known to us? (user problem if not)
87 if (not isRegistered(name)) {
88 B2ERROR("The requested object name '" << name << "' isn't known to CalibObjManager!");
89 return nullptr;
90 }
91 B2FATAL("TDirectory for registered object " << name << " not found: " << objectDirName);
92 }
93 unsigned int highestIndex = getHighestIndexObject(name, objectDir);
94 std::string highestIndexName = name + "_" + std::to_string(highestIndex);
95 // First check if we currently have an object we're using.
96 T* obj = dynamic_cast<T*>(objectDir->FindObject(highestIndexName.c_str()));
97 if (!obj) {
98 B2DEBUG(100, "Highest index is only file resident for " << highestIndexName << " in " << objectDir->GetPath() <<
99 ". Will make a higher one");
100 std::string newName = name + "_" + std::to_string(highestIndex + 1);
101 obj = cloneObj<T>(dynamic_cast<T*>(m_templateObjects[name].get()), newName);
102 obj->SetDirectory(objectDir);
103 obj->Reset();
104 // Did SetDirectory work? Or was it a dummy class method?
105 T* objTest;
106 objectDir->GetObject(newName.c_str(), objTest);
107 if (!objTest) {
108 B2DEBUG(100, "SetDirectory was a dummy function. Adding to Object to TDirectory manually.");
109 objectDir->Add(obj);
110 }
111 }
112 return obj;
113 }
std::string getObjectExpRunName(const std::string &name, const Calibration::ExpRun &expRun) const
Get object experiment and run name.
unsigned int getHighestIndexObject(const std::string &name, const TDirectory *dir) const
Scans the directory to get the highest "_i" index of an object with this name.
bool isRegistered(const std::string &name) const
Checks for the existence of a name in the templated object map to see if we registered the object.

◆ setDirectory()

void setDirectory ( TDirectory *  dir)
inline

Change the directory that we will be using to find/store all our objects, we don't own it.

Definition at line 45 of file CalibObjManager.h.

45{m_dir = dir;}

Member Data Documentation

◆ m_dir

TDirectory* m_dir
private

The TDirectory where all of our managed objects should be found, and where we should create new ones.

Definition at line 126 of file CalibObjManager.h.

◆ m_templateObjects

std::map<std::string, std::shared_ptr<TNamed> > m_templateObjects
private

The objects that we are managing, these are template objects for all future objects for each (Exp,Run).

They should not contain data themselves.

Definition at line 132 of file CalibObjManager.h.


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