8 #include <calibration/CalibObjManager.h>
11 #include <boost/algorithm/string/split.hpp>
12 #include <boost/algorithm/string/classification.hpp>
17 using namespace Calibration;
25 TTree* CalibObjManager::cloneObj(TTree* source,
const std::string& newName)
const
27 B2DEBUG(100,
"Held object is a TTree which will be have CloneTree() called.");
30 TTree* dest = source->CloneTree(0);
31 dest->SetName(newName.c_str());
35 void CalibObjManager::deleteHeldObjects()
37 m_templateObjects.clear();
40 void CalibObjManager::addObject(
const string& name, shared_ptr<TNamed>
object)
42 if (m_templateObjects.find(name) != m_templateObjects.end()) {
43 m_templateObjects[name].reset();
45 m_templateObjects[name] = object;
48 void CalibObjManager::createDirectories()
50 for (
auto& x : m_templateObjects) {
51 if (m_dir->GetDirectory(x.first.c_str()) == 0) {
52 m_dir->mkdir(x.first.c_str());
53 TDirectory* objectDir = m_dir->GetDirectory(x.first.c_str());
54 objectDir->SetWritable(
true);
55 B2DEBUG(100,
"Made TDirectory: " << x.first);
60 void CalibObjManager::createExpRunDirectories(
ExpRun& expRun)
const
62 for (
auto& x : m_templateObjects) {
63 TDirectory* objectDir = m_dir->GetDirectory(x.first.c_str());
64 string dirName = x.first + getSuffix(expRun);
65 TDirectory* newDir = objectDir->GetDirectory(dirName.c_str());
67 newDir = objectDir->mkdir(dirName.c_str());
68 newDir->SetWritable(
true);
69 B2DEBUG(100,
"Made TDirectory " << newDir->GetPath());
74 void CalibObjManager::writeCurrentObjects(
const ExpRun& expRun)
76 for (
auto& x : m_templateObjects) {
77 TDirectory* objectDir = m_dir->GetDirectory((x.first +
'/' + getObjectExpRunName(x.first, expRun)).c_str());
78 B2DEBUG(100,
"Writing for " << x.first);
79 for (
auto key : * (objectDir->GetList())) {
80 B2DEBUG(100,
"Writing for " << key->GetName());
81 TNamed* objMemory =
dynamic_cast<TNamed*
>(objectDir->FindObject(key->GetName()));
83 objectDir->WriteTObject(objMemory, key->GetName(),
"Overwrite");
89 void CalibObjManager::clearCurrentObjects(
const ExpRun& expRun)
91 for (
auto& x : m_templateObjects) {
92 TDirectory* objectDir = m_dir->GetDirectory((x.first +
'/' + getObjectExpRunName(x.first, expRun)).c_str());
93 B2DEBUG(100,
"We are deleting all the in-memory + file objects " << objectDir->GetPath());
94 objectDir->DeleteAll();
98 unsigned int CalibObjManager::getHighestIndexObject(
const string& name,
const TDirectory* dir)
const
100 unsigned int index = 0;
102 for (
auto key : * (dir->GetList())) {
103 string keyName = key->GetName();
104 if (keyName.find(name) != std::string::npos) {
105 B2DEBUG(1000,
"Found previous Object " << keyName <<
" in the directory " << dir->GetPath());
106 unsigned int currentIndex = extractKeyIndex(keyName);
107 if (currentIndex > index) {
108 index = currentIndex;
113 for (
auto key : * (dir->GetListOfKeys())) {
114 string keyName = key->GetName();
115 if (keyName.find(name) != std::string::npos) {
116 B2DEBUG(1000,
"Found previous Key " << keyName <<
" in the directory " << dir->GetPath());
117 unsigned int currentIndex = extractKeyIndex(keyName);
118 if (currentIndex > index) {
119 index = currentIndex;
123 B2DEBUG(1000,
"Returning highest index " << index);
127 string CalibObjManager::getSuffix(
const ExpRun& expRun)
const
129 return "_" + encodeExpRun(expRun);
135 return getSuffix(key);
138 string CalibObjManager::getObjectExpRunName(
const string& name,
const ExpRun& expRun)
const
140 return name + getSuffix(expRun);
143 unsigned int CalibObjManager::extractKeyIndex(
const string& keyName)
const
146 boost::split(strs, keyName, boost::is_any_of(
"_"));
147 string indexString = strs.back();
148 return stoi(indexString);
151 bool CalibObjManager::isRegistered(
const std::string& name)
const
153 if (m_templateObjects.count(name))
return true;
Abstract base class for different kinds of events.
Struct containing exp number and run number.