Belle II Software  release-05-01-25
CalibObjManager.cc
1 #include <calibration/CalibObjManager.h>
2 
3 #include <string>
4 #include <boost/algorithm/string/split.hpp>
5 #include <boost/algorithm/string/classification.hpp>
6 #include <TTree.h>
7 
8 using namespace std;
9 using namespace Belle2;
10 using namespace Calibration;
11 
12 namespace Belle2 {
17  template<>
18  TTree* CalibObjManager::cloneObj(TTree* source, const std::string& newName) const
19  {
20  B2DEBUG(100, "Held object is a TTree which will be have CloneTree() called.");
21  // Construct the TTree by making a copy
22  // of the source TTree
23  TTree* dest = source->CloneTree(0);
24  dest->SetName(newName.c_str());
25  return dest;
26  }
27 
28  void CalibObjManager::deleteHeldObjects()
29  {
30  m_templateObjects.clear();
31  }
32 
33  void CalibObjManager::addObject(const string& name, shared_ptr<TNamed> object)
34  {
35  if (m_templateObjects.find(name) != m_templateObjects.end()) {
36  m_templateObjects[name].reset();
37  }
38  m_templateObjects[name] = object;
39  }
40 
41  void CalibObjManager::createDirectories()
42  {
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);
49  }
50  }
51  }
52 
53  void CalibObjManager::createExpRunDirectories(ExpRun& expRun) const
54  {
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());
59  if (!newDir) {
60  newDir = objectDir->mkdir(dirName.c_str());
61  newDir->SetWritable(true);
62  B2DEBUG(100, "Made TDirectory " << newDir->GetPath());
63  }
64  }
65  }
66 
67  void CalibObjManager::writeCurrentObjects(const ExpRun& expRun)
68  {
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()));
75  if (objMemory) {
76  objectDir->WriteTObject(objMemory, key->GetName(), "Overwrite");
77  }
78  }
79  }
80  }
81 
82  void CalibObjManager::clearCurrentObjects(const ExpRun& expRun)
83  {
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();
88  }
89  }
90 
91  unsigned int CalibObjManager::getHighestIndexObject(const string& name, const TDirectory* dir) const
92  {
93  unsigned int index = 0;
94  // Try from the list of objects
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;
102  }
103  }
104  }
105  // Try from List of keys
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;
113  }
114  }
115  }
116  B2DEBUG(1000, "Returning highest index " << index);
117  return index;
118  }
119 
120  string CalibObjManager::getSuffix(const ExpRun& expRun) const
121  {
122  return "_" + encodeExpRun(expRun);
123  }
124 
125  string CalibObjManager::getSuffix(const EventMetaData& emd) const
126  {
127  const ExpRun key = make_pair(emd.getExperiment(), emd.getRun());
128  return getSuffix(key);
129  }
130 
131  string CalibObjManager::getObjectExpRunName(const string& name, const ExpRun& expRun) const
132  {
133  return name + getSuffix(expRun);
134  }
135 
136  unsigned int CalibObjManager::extractKeyIndex(const string& keyName) const
137  {
138  vector<string> strs;
139  boost::split(strs, keyName, boost::is_any_of("_"));
140  string indexString = strs.back();
141  return stoi(indexString);
142  }
143 
144  bool CalibObjManager::isRegistered(const std::string& name) const
145  {
146  if (m_templateObjects.count(name)) return true;
147  else return false;
148  }
150 }
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::ExpRun
Struct containing exp number and run number.
Definition: Splitter.h:55
Belle2::EventMetaData::getExperiment
int getExperiment() const
Experiment Getter.
Definition: EventMetaData.h:174
Belle2::EventMetaData::getRun
int getRun() const
Run Getter.
Definition: EventMetaData.h:161
Belle2::EventMetaData
Store event, run, and experiment numbers.
Definition: EventMetaData.h:43