Belle II Software development
CalibObjManager.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <calibration/CalibObjManager.h>
9
10#include <string>
11#include <boost/algorithm/string/split.hpp>
12#include <boost/algorithm/string/classification.hpp>
13#include <TTree.h>
14
15using namespace std;
16using namespace Belle2;
17using namespace Calibration;
18
19namespace Belle2 {
34 template<>
35 TTree* CalibObjManager::cloneObj(TTree* source, const std::string& newName) const
36 {
37 B2DEBUG(100, "Held object is a TTree which will be have CloneTree() called.");
38 // Construct the TTree by making a copy
39 // of the source TTree
40 TTree* dest = source->CloneTree(0);
41 dest->SetName(newName.c_str());
42 return dest;
43 }
44
49
50 void CalibObjManager::addObject(const string& name, shared_ptr<TNamed> object)
51 {
52 if (m_templateObjects.find(name) != m_templateObjects.end()) {
53 m_templateObjects[name].reset();
54 }
55 m_templateObjects[name] = object;
56 }
57
59 {
60 for (auto& x : m_templateObjects) {
61 if (m_dir->GetDirectory(x.first.c_str()) == 0) {
62 m_dir->mkdir(x.first.c_str());
63 TDirectory* objectDir = m_dir->GetDirectory(x.first.c_str());
64 objectDir->SetWritable(true);
65 B2DEBUG(100, "Made TDirectory: " << x.first);
66 }
67 }
68 }
69
71 {
72 for (auto& x : m_templateObjects) {
73 TDirectory* objectDir = m_dir->GetDirectory(x.first.c_str());
74 string dirName = x.first + getSuffix(expRun);
75 TDirectory* newDir = objectDir->GetDirectory(dirName.c_str());
76 if (!newDir) {
77 newDir = objectDir->mkdir(dirName.c_str());
78 newDir->SetWritable(true);
79 B2DEBUG(100, "Made TDirectory " << newDir->GetPath());
80 }
81 }
82 }
83
85 {
86 for (auto& x : m_templateObjects) {
87 TDirectory* objectDir = m_dir->GetDirectory((x.first + '/' + getObjectExpRunName(x.first, expRun)).c_str());
88 B2DEBUG(100, "Writing for " << x.first);
89 for (auto key : * (objectDir->GetList())) {
90 B2DEBUG(100, "Writing for " << key->GetName());
91 TNamed* objMemory = dynamic_cast<TNamed*>(objectDir->FindObject(key->GetName()));
92 if (objMemory) {
93 objectDir->WriteTObject(objMemory, key->GetName(), "Overwrite");
94 }
95 }
96 }
97 }
98
100 {
101 for (auto& x : m_templateObjects) {
102 TDirectory* objectDir = m_dir->GetDirectory((x.first + '/' + getObjectExpRunName(x.first, expRun)).c_str());
103 B2DEBUG(100, "We are deleting all the in-memory + file objects " << objectDir->GetPath());
104 objectDir->DeleteAll();
105 }
106 }
107
108 unsigned int CalibObjManager::getHighestIndexObject(const string& name, const TDirectory* dir) const
109 {
110 unsigned int index = 0;
111 // Try from the list of objects
112 for (auto key : * (dir->GetList())) {
113 string keyName = key->GetName();
114 if (keyName.find(name) != std::string::npos) {
115 B2DEBUG(1000, "Found previous Object " << keyName << " in the directory " << dir->GetPath());
116 unsigned int currentIndex = extractKeyIndex(keyName);
117 if (currentIndex > index) {
118 index = currentIndex;
119 }
120 }
121 }
122 // Try from List of keys
123 for (auto key : * (dir->GetListOfKeys())) {
124 string keyName = key->GetName();
125 if (keyName.find(name) != std::string::npos) {
126 B2DEBUG(1000, "Found previous Key " << keyName << " in the directory " << dir->GetPath());
127 unsigned int currentIndex = extractKeyIndex(keyName);
128 if (currentIndex > index) {
129 index = currentIndex;
130 }
131 }
132 }
133 B2DEBUG(1000, "Returning highest index " << index);
134 return index;
135 }
136
137 string CalibObjManager::getSuffix(const Calibration::ExpRun& expRun) const
138 {
139 return "_" + encodeExpRun(expRun);
140 }
141
143 {
144 const ExpRun key = make_pair(emd.getExperiment(), emd.getRun());
145 return getSuffix(key);
146 }
147
148 string CalibObjManager::getObjectExpRunName(const string& name, const ExpRun& expRun) const
149 {
150 return name + getSuffix(expRun);
151 }
152
153 unsigned int CalibObjManager::extractKeyIndex(const string& keyName) const
154 {
155 vector<string> strs;
156 boost::split(strs, keyName, boost::is_any_of("_"));
157 string indexString = strs.back();
158 return stoi(indexString);
159 }
160
161 bool CalibObjManager::isRegistered(const std::string& name) const
162 {
163 if (m_templateObjects.count(name)) return true;
164 else return false;
165 }
166
167}
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,...
T * cloneObj(T *source, const std::string &newName) const
Clone object.
TDirectory * m_dir
The TDirectory where all of our managed objects should be found, and where we should create new ones.
Store event, run, and experiment numbers.
int getRun() const
Run Getter.
int getExperiment() const
Experiment Getter.
unsigned int extractKeyIndex(const std::string &keyName) const
Extract key index.
void clearCurrentObjects(const Calibration::ExpRun &expRun)
Deletes all in-memory objects in the exprun directories for all the collector objects we know about.
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...
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.
void deleteHeldObjects()
Clears the map of templated objects -> causing their destruction.
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.
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.
void createExpRunDirectories(Calibration::ExpRun &expRun) const
For each templated object, we create a new TDirectory for this exprun.
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 convent...
void createDirectories()
Each object gets its own TDirectory under the main manager directory to store its objects.
Abstract base class for different kinds of events.
STL namespace.
Struct containing exp number and run number.
Definition Splitter.h:51