Belle II Software development
calibration

Modules

 calibration data objects
 
 calibration modules
 

Classes

class  MergeableNamed
 Abstract base class for objects that can be merged but also named. More...
 
class  TestCalibMean
 Test DBObject. More...
 
class  TestCalibObject
 Test DBObject. More...
 
class  TestBoundarySettingAlgorithm
 Test class implementing calibration algorithm. More...
 
class  TestCalibrationAlgorithm
 Test class implementing calibration algorithm. More...
 
class  TestDBAccessAlgorithm
 Test class implementing calibration algorithm. More...
 
class  CalibObjManager
 Manager class for collector registered data. Handles much of the TDirectory/TObject manipulation. More...
 
class  CalibrationAlgorithm
 Base class for calibration algorithms. More...
 
class  CalibrationCollectorModule
 Calibration collector module base class. More...
 
class  DBObjCalibrationConstMapBase
 Base for calibration or alignment parameters identified by two unsigned shorts Typically first is some identifier of a sub-detector element (sensor, layer, wire, board...) and the second is number of parameter. More...
 

Functions

template<>
TTree * cloneObj (TTree *source, const std::string &newName) const
 Template specialization for TTree needs to be defined here to prevent automatic specialization being created.
 
template<class T >
std::shared_ptr< T > getObjectPtr (const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
 Get calibration data object by name and list of runs, the Merge function will be called to generate the overall object.
 
template<>
std::shared_ptr< TTree > getObjectPtr (const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
 Specialization of getObjectPtr<TTree>.
 
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.
 
void createDirectories ()
 Each object gets its own TDirectory under the main manager directory to store its objects.
 
void createExpRunDirectories (Calibration::ExpRun &expRun) const
 For each templated object, we create a new TDirectory for this exprun.
 
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 TDirectory.
 
void clearCurrentObjects (const Calibration::ExpRun &expRun)
 Deletes all in-memory objects in the exprun directories for all the collector objects we know about.
 
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.
 
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 convention.
 
std::string getSuffix (const EventMetaData &emd) const
 Sometimes it's nice to just pass in the EventMetaData instead of converting manually.
 
std::string getObjectExpRunName (const std::string &name, const Calibration::ExpRun &expRun) const
 Get object experiment and run name.
 
unsigned int extractKeyIndex (const std::string &keyName) const
 Extract key index.
 
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.
 
template<>
shared_ptr< TTree > getObjectPtr (const string &name, const vector< ExpRun > &requestedRuns)
 We cheekily cast the TChain to TTree for the returned pointer so that the user never knows Hopefully this doesn't cause issues if people do low level stuff to the tree...
 

Detailed Description

Function Documentation

◆ 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.

We take ownership of this object but cannot guarantee that a user won't alter the wrapped ROOT object :(

Definition at line 40 of file CalibObjManager.cc.

41 {
42 if (m_templateObjects.find(name) != m_templateObjects.end()) {
43 m_templateObjects[name].reset();
44 }
45 m_templateObjects[name] = object;
46 }
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,...

◆ 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 at line 89 of file CalibObjManager.cc.

90 {
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();
95 }
96 }
TDirectory * m_dir
The TDirectory where all of our managed objects should be found, and where we should create new ones.
std::string getObjectExpRunName(const std::string &name, const Calibration::ExpRun &expRun) const
Get object experiment and run name.

◆ cloneObj()

TTree * cloneObj ( TTree *  source,
const std::string &  newName 
) const

Template specialization for TTree needs to be defined here to prevent automatic specialization being created.

Definition at line 25 of file CalibObjManager.cc.

26 {
27 B2DEBUG(100, "Held object is a TTree which will be have CloneTree() called.");
28 // Construct the TTree by making a copy
29 // of the source TTree
30 TTree* dest = source->CloneTree(0);
31 dest->SetName(newName.c_str());
32 return dest;
33 }

◆ createDirectories()

void createDirectories ( )

Each object gets its own TDirectory under the main manager directory to store its objects.

We create them using this function.

Definition at line 48 of file CalibObjManager.cc.

49 {
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);
56 }
57 }
58 }

◆ createExpRunDirectories()

void createExpRunDirectories ( Calibration::ExpRun &  expRun) const

For each templated object, we create a new TDirectory for this exprun.

Definition at line 60 of file CalibObjManager.cc.

61 {
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());
66 if (!newDir) {
67 newDir = objectDir->mkdir(dirName.c_str());
68 newDir->SetWritable(true);
69 B2DEBUG(100, "Made TDirectory " << newDir->GetPath());
70 }
71 }
72 }
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...

◆ deleteHeldObjects()

void deleteHeldObjects ( )

Clears the map of templated objects -> causing their destruction.

Definition at line 35 of file CalibObjManager.cc.

36 {
37 m_templateObjects.clear();
38 }

◆ extractKeyIndex()

unsigned int extractKeyIndex ( const std::string &  keyName) const
private

Extract key index.

Definition at line 143 of file CalibObjManager.cc.

144 {
145 vector<string> strs;
146 boost::split(strs, keyName, boost::is_any_of("_"));
147 string indexString = strs.back();
148 return stoi(indexString);
149 }

◆ 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 at line 98 of file CalibObjManager.cc.

99 {
100 unsigned int index = 0;
101 // Try from the list of objects
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;
109 }
110 }
111 }
112 // Try from List of keys
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;
120 }
121 }
122 }
123 B2DEBUG(1000, "Returning highest index " << index);
124 return index;
125 }
unsigned int extractKeyIndex(const std::string &keyName) const
Extract key index.

◆ getObjectExpRunName()

string getObjectExpRunName ( const std::string &  name,
const Calibration::ExpRun &  expRun 
) const
private

Get object experiment and run name.

Definition at line 138 of file CalibObjManager.cc.

139 {
140 return name + getSuffix(expRun);
141 }

◆ getObjectPtr() [1/2]

std::shared_ptr< T > getObjectPtr ( const std::string &  name,
const std::vector< Calibration::ExpRun > &  requestedRuns 
)
protected

Get calibration data object by name and list of runs, the Merge function will be called to generate the overall object.

Definition at line 414 of file CalibrationAlgorithm.h.

416 {
417 // Check if this object already exists
418 RunRange runRangeRequested(requestedRuns);
419 std::shared_ptr<T> objOutputPtr = std::dynamic_pointer_cast<T>(m_data.getCalibObj(name, runRangeRequested));
420 if (objOutputPtr)
421 return objOutputPtr;
422
423 std::shared_ptr<T> mergedObjPtr(nullptr);
424 bool mergedEmpty = true;
425 TDirectory* dir = gDirectory;
426
427 // Technically we could grab all the objects from all files, add to list and then merge at the end.
428 // But I prefer the (maybe) more memory efficient way of merging with all objects
429 // in a file before moving on to the next one, just in case TDirectory stuff screws us.
430 TList list;
431 list.SetOwner(false);
432
433 // Construct the TDirectory names where we expect our objects to be
434 std::string runRangeObjName(getPrefix() + "/" + Calibration::RUN_RANGE_OBJ_NAME);
435
436 if (strcmp(getGranularity().c_str(), "run") == 0) {
437 // Loop over our runs requested for the right files
438 for (auto expRunRequested : requestedRuns) {
439 // Find the relevant files for this ExpRun
440 auto searchFiles = m_runsToInputFiles.find(expRunRequested);
441 if (searchFiles == m_runsToInputFiles.end()) {
442 B2WARNING("No input file found with data collected from run "
443 "(" << expRunRequested.first << "," << expRunRequested.second << ")");
444 continue;
445 } else {
446 auto files = searchFiles->second;
447 for (auto fileName : files) {
448 RunRange* runRangeData;
449 //Open TFile to get the objects
450 std::unique_ptr<TFile> f;
451 f.reset(TFile::Open(fileName.c_str(), "READ"));
452 runRangeData = dynamic_cast<RunRange*>(f->Get(runRangeObjName.c_str()));
453 // Check that nothing went wrong in the mapping and that this file definitely contains this run's data
454 auto runSet = runRangeData->getExpRunSet();
455 if (runSet.find(expRunRequested) == runSet.end()) {
456 B2WARNING("Something went wrong with the mapping of ExpRun -> Input Files. "
457 "(" << expRunRequested.first << "," << expRunRequested.second << ") not in " << fileName);
458 }
459 // Get the path/directory of the Exp,Run TDirectory that holds the object(s)
460 std::string objDirName = getFullObjectPath(name, expRunRequested);
461 TDirectory* objDir = f->GetDirectory(objDirName.c_str());
462 if (!objDir) {
463 B2ERROR("Directory for requested object " << name << " not found: " << objDirName);
464 return nullptr;
465 }
466 // Find all the objects inside, there may be more than one
467 for (auto key : * (objDir->GetListOfKeys())) {
468 std::string keyName = key->GetName();
469 B2DEBUG(100, "Adding found object " << keyName << " in the directory " << objDir->GetPath());
470 T* objOther = (T*)objDir->Get(keyName.c_str());
471 if (objOther) {
472 if (mergedEmpty) {
473 mergedObjPtr = std::shared_ptr<T>(dynamic_cast<T*>(objOther->Clone(name.c_str())));
474 mergedObjPtr->SetDirectory(0);
475 mergedEmpty = false;
476 } else {
477 list.Add(objOther);
478 }
479 }
480 }
481 if (!mergedEmpty)
482 mergedObjPtr->Merge(&list);
483 list.Clear();
484 }
485 }
486 }
487 } else {
488 for (auto fileName : m_inputFileNames) {
489 //Open TFile to get the objects
490 std::unique_ptr<TFile> f;
491 f.reset(TFile::Open(fileName.c_str(), "READ"));
492 Calibration::ExpRun allGranExpRun = getAllGranularityExpRun();
493 std::string objDirName = getFullObjectPath(name, allGranExpRun);
494 std::string objPath = objDirName + "/" + name + "_1";
495 T* objOther = (T*)f->Get(objPath.c_str()); // Only one index for granularity == all
496 B2DEBUG(100, "Adding " << objPath);
497 if (objOther) {
498 if (mergedEmpty) {
499 mergedObjPtr = std::shared_ptr<T>(dynamic_cast<T*>(objOther->Clone(name.c_str())));
500 mergedObjPtr->SetDirectory(0);
501 mergedEmpty = false;
502 } else {
503 list.Add(objOther);
504 }
505 }
506 if (!mergedEmpty)
507 mergedObjPtr->Merge(&list);
508 list.Clear();
509 }
510 }
511 dir->cd();
512 objOutputPtr = mergedObjPtr;
513 if (!objOutputPtr) {
514 B2ERROR("No data found for object " << name);
515 return nullptr;
516 }
517 objOutputPtr->SetDirectory(0);
518 // make a TNamed version to input to the map of previous calib objects
519 std::shared_ptr<TNamed> storedObjPtr = std::static_pointer_cast<TNamed>(objOutputPtr);
520 m_data.setCalibObj(name, runRangeRequested, storedObjPtr);
521 B2DEBUG(100, "Passing back merged data " << name);
522 return objOutputPtr;
523 }
void setCalibObj(const std::string &name, const RunRange &runRange, const std::shared_ptr< TNamed > &objectPtr)
Insert a newly created object in m_mapCalibData. Overwrites a previous entry if one exists.
std::shared_ptr< TNamed > getCalibObj(const std::string &name, const RunRange &runRange) const
Get a previously created object in m_mapCalibData if one exists, otherwise return shared_ptr(nullptr)
Calibration::ExpRun getAllGranularityExpRun() const
Returns the Exp,Run pair that means 'Everything'. Currently unused.
std::string getFullObjectPath(const std::string &name, Calibration::ExpRun expRun) const
constructs the full TDirectory + Key name of an object in a TFile based on its name and exprun
std::string getPrefix() const
Get the prefix used for getting calibration data.
std::vector< std::string > m_inputFileNames
List of input files to the Algorithm, will initially be user defined but then gets the wildcards expa...
std::map< Calibration::ExpRun, std::vector< std::string > > m_runsToInputFiles
Map of Runs to input files. Gets filled when you call getRunRangeFromAllData, gets cleared when setti...
std::string getGranularity() const
Get the granularity of collected data.
ExecutionData m_data
Data specific to a SINGLE execution of the algorithm. Gets reset at the beginning of execution.

◆ getObjectPtr() [2/2]

shared_ptr< TTree > getObjectPtr ( const string &  name,
const vector< ExpRun > &  requestedRuns 
)
protected

We cheekily cast the TChain to TTree for the returned pointer so that the user never knows Hopefully this doesn't cause issues if people do low level stuff to the tree...

Definition at line 520 of file CalibrationAlgorithm.cc.

426 {
427 // Check if this object already exists
428 RunRange runRangeRequested(requestedRuns);
429 std::shared_ptr<TTree> objOutputPtr = std::dynamic_pointer_cast<TTree>(m_data.getCalibObj(name, runRangeRequested));
430 if (objOutputPtr)
431 return objOutputPtr;
432
433 // If not we best make a new one
434 shared_ptr<TChain> chain = make_shared<TChain>(name.c_str());
435 chain->SetDirectory(0);
436 // Construct the TDirectory names where we expect our objects to be
437 string runRangeObjName(getPrefix() + "/" + RUN_RANGE_OBJ_NAME);
438
439 if (strcmp(getGranularity().c_str(), "run") == 0) {
440 // Loop over our runs requested for the right files
441 for (auto expRunRequested : requestedRuns) {
442 // Find the relevant files for this ExpRun
443 auto searchFiles = m_runsToInputFiles.find(expRunRequested);
444 if (searchFiles == m_runsToInputFiles.end()) {
445 B2WARNING("No input file found with data collected from run "
446 "(" << expRunRequested.first << "," << expRunRequested.second << ")");
447 continue;
448 } else {
449 auto files = searchFiles->second;
450 for (auto fileName : files) {
451 RunRange* runRangeData;
452 //Open TFile to get the objects
453 std::unique_ptr<TFile> f;
454 f.reset(TFile::Open(fileName.c_str(), "READ"));
455 runRangeData = dynamic_cast<RunRange*>(f->Get(runRangeObjName.c_str()));
456 // Check that nothing went wrong in the mapping and that this file definitely contains this run's data
457 auto runSet = runRangeData->getExpRunSet();
458 if (runSet.find(expRunRequested) == runSet.end()) {
459 B2WARNING("Something went wrong with the mapping of ExpRun -> Input Files. "
460 "(" << expRunRequested.first << "," << expRunRequested.second << ") not in " << fileName);
461 }
462 // Get the path/directory of the Exp,Run TDirectory that holds the object(s)
463 std::string objDirName = getFullObjectPath(name, expRunRequested);
464 TDirectory* objDir = f->GetDirectory(objDirName.c_str());
465 if (!objDir) {
466 B2ERROR("Directory for requested object " << name << " not found: " << objDirName);
467 return nullptr;
468 }
469 // Find all the objects inside, there may be more than one
470 for (auto key : * (objDir->GetListOfKeys())) {
471 string keyName = key->GetName();
472 string objectPath = fileName + "/" + objDirName + "/" + keyName;
473 B2DEBUG(29, "Adding TTree " << objectPath);
474 chain->Add(objectPath.c_str());
475 }
476 }
477 }
478 }
479 } else {
480 ExpRun allGranExpRun = getAllGranularityExpRun();
481 string objDirName = getFullObjectPath(name, allGranExpRun);
482 for (const auto& fileName : m_inputFileNames) {
483 string objectPath = fileName + "/" + objDirName + "/" + name + "_1"; // Only one index for this granularity
484 B2DEBUG(29, "Adding TTree " << objectPath);
485 chain->Add(objectPath.c_str());
486 }
487 }
488 if (!chain->GetListOfFiles()->GetEntries()) {
489 B2ERROR("No data found for object " << name);
490 return nullptr;
491 }
492 objOutputPtr = static_pointer_cast<TTree>(chain);
493 // make a TNamed version to input to the map of previous calib objects
494 shared_ptr<TNamed> storedObjPtr = static_pointer_cast<TNamed>(objOutputPtr);
495 m_data.setCalibObj(name, runRangeRequested, storedObjPtr);
496 B2DEBUG(29, "Passing back merged data " << name);
497 return objOutputPtr;
498 }
Mergeable object holding (unique) set of (exp,run) pairs.
Definition: RunRange.h:25
const std::set< Calibration::ExpRun > & getExpRunSet()
Get access to the stored set.
Definition: RunRange.h:64
Struct containing exp number and run number.
Definition: Splitter.h:51

◆ getSuffix() [1/2]

string getSuffix ( const Calibration::ExpRun &  key) const
private

We rename objects based on the Exp,Run that they contain so we need to generate a nice naming convention.

Definition at line 127 of file CalibObjManager.cc.

128 {
129 return "_" + encodeExpRun(expRun);
130 }

◆ getSuffix() [2/2]

string getSuffix ( const EventMetaData emd) const
private

Sometimes it's nice to just pass in the EventMetaData instead of converting manually.

Definition at line 132 of file CalibObjManager.cc.

133 {
134 const ExpRun key = make_pair(emd.getExperiment(), emd.getRun());
135 return getSuffix(key);
136 }
int getRun() const
Run Getter.
int getExperiment() const
Experiment Getter.

◆ 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 at line 151 of file CalibObjManager.cc.

152 {
153 if (m_templateObjects.count(name)) return true;
154 else return false;
155 }

◆ 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 TDirectory.

Definition at line 74 of file CalibObjManager.cc.

75 {
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()));
82 if (objMemory) {
83 objectDir->WriteTObject(objMemory, key->GetName(), "Overwrite");
84 }
85 }
86 }
87 }