1 #include <calibration/CalibObjManager.h>
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
10 using namespace Calibration;
18 TTree* CalibObjManager::cloneObj(TTree* source,
const std::string& newName)
const
20 B2DEBUG(100,
"Held object is a TTree which will be have CloneTree() called.");
23 TTree* dest = source->CloneTree(0);
24 dest->SetName(newName.c_str());
28 void CalibObjManager::deleteHeldObjects()
30 m_templateObjects.clear();
33 void CalibObjManager::addObject(
const string& name, shared_ptr<TNamed>
object)
35 if (m_templateObjects.find(name) != m_templateObjects.end()) {
36 m_templateObjects[name].reset();
38 m_templateObjects[name] = object;
41 void CalibObjManager::createDirectories()
43 for (
auto& x : m_templateObjects) {
44 if (m_dir->GetDirectory(x.first.c_str()) == 0) {
45 m_dir->mkdir(x.first.c_str());
46 TDirectory* objectDir = m_dir->GetDirectory(x.first.c_str());
47 objectDir->SetWritable(
true);
48 B2DEBUG(100,
"Made TDirectory: " << x.first);
53 void CalibObjManager::createExpRunDirectories(
ExpRun& expRun)
const
55 for (
auto& x : m_templateObjects) {
56 TDirectory* objectDir = m_dir->GetDirectory(x.first.c_str());
57 string dirName = x.first + getSuffix(expRun);
58 TDirectory* newDir = objectDir->GetDirectory(dirName.c_str());
60 newDir = objectDir->mkdir(dirName.c_str());
61 newDir->SetWritable(
true);
62 B2DEBUG(100,
"Made TDirectory " << newDir->GetPath());
67 void CalibObjManager::writeCurrentObjects(
const ExpRun& expRun)
69 for (
auto& x : m_templateObjects) {
70 TDirectory* objectDir = m_dir->GetDirectory((x.first +
'/' + getObjectExpRunName(x.first, expRun)).c_str());
71 B2DEBUG(100,
"Writing for " << x.first);
72 for (
auto key : * (objectDir->GetList())) {
73 B2DEBUG(100,
"Writing for " << key->GetName());
74 TNamed* objMemory =
dynamic_cast<TNamed*
>(objectDir->FindObject(key->GetName()));
76 objectDir->WriteTObject(objMemory, key->GetName(),
"Overwrite");
82 void CalibObjManager::clearCurrentObjects(
const ExpRun& expRun)
84 for (
auto& x : m_templateObjects) {
85 TDirectory* objectDir = m_dir->GetDirectory((x.first +
'/' + getObjectExpRunName(x.first, expRun)).c_str());
86 B2DEBUG(100,
"We are deleting all the in-memory + file objects " << objectDir->GetPath());
87 objectDir->DeleteAll();
91 unsigned int CalibObjManager::getHighestIndexObject(
const string& name,
const TDirectory* dir)
const
93 unsigned int index = 0;
95 for (
auto key : * (dir->GetList())) {
96 string keyName = key->GetName();
97 if (keyName.find(name) != std::string::npos) {
98 B2DEBUG(1000,
"Found previous Object " << keyName <<
" in the directory " << dir->GetPath());
99 unsigned int currentIndex = extractKeyIndex(keyName);
100 if (currentIndex > index) {
101 index = currentIndex;
106 for (
auto key : * (dir->GetListOfKeys())) {
107 string keyName = key->GetName();
108 if (keyName.find(name) != std::string::npos) {
109 B2DEBUG(1000,
"Found previous Key " << keyName <<
" in the directory " << dir->GetPath());
110 unsigned int currentIndex = extractKeyIndex(keyName);
111 if (currentIndex > index) {
112 index = currentIndex;
116 B2DEBUG(1000,
"Returning highest index " << index);
120 string CalibObjManager::getSuffix(
const ExpRun& expRun)
const
122 return "_" + encodeExpRun(expRun);
128 return getSuffix(key);
131 string CalibObjManager::getObjectExpRunName(
const string& name,
const ExpRun& expRun)
const
133 return name + getSuffix(expRun);
136 unsigned int CalibObjManager::extractKeyIndex(
const string& keyName)
const
139 boost::split(strs, keyName, boost::is_any_of(
"_"));
140 string indexString = strs.back();
141 return stoi(indexString);
144 bool CalibObjManager::isRegistered(
const std::string& name)
const
146 if (m_templateObjects.count(name))
return true;