Belle II Software  release-05-01-25
CalibObjManager.h
1 #pragma once
2 
3 #include <string>
4 #include <memory>
5 
6 #include <TDirectory.h>
7 #include <TNamed.h>
8 #include <TTree.h>
9 
10 #include <framework/dataobjects/EventMetaData.h>
11 #include <framework/logging/Logger.h>
12 
13 #include <calibration/Utilities.h>
14 
15 namespace Belle2 {
21  class CalibObjManager {
23 
24  public:
26  CalibObjManager(TDirectory* dir = nullptr) : m_dir(dir) {};
27 
35  virtual ~CalibObjManager() {m_templateObjects.clear();}
36 
38  void setDirectory(TDirectory* dir) {m_dir = dir;}
39 
43  void addObject(const std::string& name, std::shared_ptr<TNamed> object);
44 
46  void writeCurrentObjects(const Calibration::ExpRun& expRun);
47 
49  void clearCurrentObjects(const Calibration::ExpRun& expRun);
50 
54  void createDirectories();
55 
57  void createExpRunDirectories(Calibration::ExpRun& expRun) const;
58 
60  unsigned int getHighestIndexObject(const std::string& name, const TDirectory* dir) const;
61 
63  void deleteHeldObjects();
64 
66  bool isRegistered(const std::string& name) const;
67 
72  template<class T>
73  T* getObject(const std::string& name, const Belle2::Calibration::ExpRun expRun)
74  {
75  std::string objectDirName = name + '/' + getObjectExpRunName(name, expRun);
76  TDirectory* objectDir = m_dir->GetDirectory(objectDirName.c_str());
77  // Does the object name have a directory available? (framework problem if not)
78  if (not objectDir) {
79  // Is the object name known to us? (user problem if not)
80  if (not isRegistered(name)) {
81  B2ERROR("The requested object name '" << name << "' isn't known to CalibObjManager!");
82  return nullptr;
83  }
84  B2FATAL("TDirectory for registered object " << name << " not found: " << objectDirName);
85  }
86  unsigned int highestIndex = getHighestIndexObject(name, objectDir);
87  std::string highestIndexName = name + "_" + std::to_string(highestIndex);
88  // First check if we currently have an object we're using.
89  T* obj = dynamic_cast<T*>(objectDir->FindObject(highestIndexName.c_str()));
90  if (!obj) {
91  B2DEBUG(100, "Highest index is only file resident for " << highestIndexName << " in " << objectDir->GetPath() <<
92  ". Will make a higher one");
93  std::string newName = name + "_" + std::to_string(highestIndex + 1);
94  obj = cloneObj<T>(dynamic_cast<T*>(m_templateObjects[name].get()), newName);
95  obj->SetDirectory(objectDir);
96  obj->Reset();
97  // Did SetDirectory work? Or was it a dummy class method?
98  T* objTest;
99  objectDir->GetObject(newName.c_str(), objTest);
100  if (!objTest) {
101  B2DEBUG(100, "SetDirectory was a dummy function. Adding to Object to TDirectory manually.");
102  objectDir->Add(obj);
103  }
104  }
105  return obj;
106  }
107 
108  private:
109 
110  template<class T>
111  T* cloneObj(T* source, const std::string& newName) const
112  {
113  B2DEBUG(100, "Held object " << source->GetName() << " will be treated as a generic TNamed and have Clone(newname) called.");
114  return dynamic_cast<T*>(source->Clone(newName.c_str()));
115  }
116 
118  TDirectory* m_dir;
119 
124  std::map<std::string, std::shared_ptr<TNamed>> m_templateObjects;
125 
127  std::string getSuffix(const Belle2::Calibration::ExpRun& key) const;
128 
130  std::string getSuffix(const EventMetaData& emd) const;
131 
132  std::string getObjectExpRunName(const std::string& name, const Calibration::ExpRun& expRun) const;
133 
134  unsigned int extractKeyIndex(const std::string& keyName) const;
135  };
137  template<>
138  TTree* CalibObjManager::cloneObj(TTree* source, const std::string& newName) const;
140 }
Belle2::CalibObjManager::setDirectory
void setDirectory(TDirectory *dir)
Change the directory that we will be using to find/store all our objects, we don't own it.
Definition: CalibObjManager.h:38
Belle2::CalibObjManager::~CalibObjManager
virtual ~CalibObjManager()
Destructor: Every object we are managing is ultimately either saved into a file to write out,...
Definition: CalibObjManager.h:35
Belle2::CalibObjManager::createDirectories
void createDirectories()
Each object gets its own TDirectory under the main manager directory to store its objects.
Definition: CalibObjManager.cc:41
Belle2::CalibObjManager::clearCurrentObjects
void clearCurrentObjects(const Calibration::ExpRun &expRun)
Deletes all in-memory objects in the exprun directories for all the collector objects we know about.
Definition: CalibObjManager.cc:82
Belle2::CalibObjManager::CalibObjManager
CalibObjManager(TDirectory *dir=nullptr)
Constructor.
Definition: CalibObjManager.h:26
Belle2::CalibObjManager::m_templateObjects
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,...
Definition: CalibObjManager.h:124
Belle2::CalibObjManager::addObject
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.
Definition: CalibObjManager.cc:33
Belle2::CalibObjManager::isRegistered
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.
Definition: CalibObjManager.cc:144
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CalibObjManager::deleteHeldObjects
void deleteHeldObjects()
Clears the map of templated objects -> causing their destruction.
Definition: CalibObjManager.cc:28
Belle2::CalibObjManager::createExpRunDirectories
void createExpRunDirectories(Calibration::ExpRun &expRun) const
For each templated object, we create a new TDirectory for this exprun.
Definition: CalibObjManager.cc:53
Belle2::CalibObjManager::getSuffix
std::string getSuffix(const Belle2::Calibration::ExpRun &key) const
We rename objects based on the Exp,Run that they contain so we need to generate a nice naming convent...
Belle2::EventMetaData
Store event, run, and experiment numbers.
Definition: EventMetaData.h:43
Belle2::CalibObjManager::getHighestIndexObject
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.
Definition: CalibObjManager.cc:91
Belle2::CalibObjManager::getObject
T * getObject(const std::string &name, const Belle2::Calibration::ExpRun expRun)
Gets the collector object of this name for the given exprun.
Definition: CalibObjManager.h:73
Belle2::CalibObjManager::m_dir
TDirectory * m_dir
The TDirectory where all of our managed objects should be found, and where we should create new ones.
Definition: CalibObjManager.h:118
Belle2::CalibObjManager::writeCurrentObjects
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...
Definition: CalibObjManager.cc:67