 |
Belle II Software
release-05-02-19
|
20 #include <nlohmann/json.hpp>
21 #include <TClonesArray.h>
22 #include <TDirectory.h>
25 #include <framework/database/Database.h>
26 #include <framework/database/IntervalOfValidity.h>
27 #include <framework/logging/Logger.h>
28 #include <calibration/Utilities.h>
29 #include <calibration/dataobjects/RunRange.h>
39 class CalibrationAlgorithm {
62 B2DEBUG(100,
"Resetting ExecutionData of algorithm");
84 B2DEBUG(29,
"Setting Iteration of Algorithm to " << iteration);
109 void setCalibObj(
const std::string& name,
const RunRange& runRange,
const std::shared_ptr<TNamed>& objectPtr)
122 IntervalOfValidity
m_iov;
133 std::map<std::pair<std::string, RunRange>, std::shared_ptr<TNamed>>
m_mapCalibData;
200 EResult execute(std::vector<Calibration::ExpRun> runs = {},
int iteration = 0, IntervalOfValidity iov = IntervalOfValidity());
203 EResult execute(PyObject* runs,
int iteration = 0, IntervalOfValidity iov = IntervalOfValidity());
215 bool commit(std::list<Database::DBImportQuery> payloads);
231 const std::vector<Calibration::ExpRun>
findPayloadBoundaries(std::vector<Calibration::ExpRun> runs,
int iteration = 0);
247 B2ERROR(
"You didn't implement a isBoundaryRequired() member function in your CalibrationAlgorithm but you are calling it!");
263 std::vector<Calibration::ExpRun> m_boundaries;
281 std::shared_ptr<T>
getObjectPtr(
const std::string& name,
const std::vector<Calibration::ExpRun>& requestedRuns);
318 void updateDBObjPtrs(
const unsigned int event,
const int run,
const int experiment);
366 static const Calibration::ExpRun m_allExpRun;
372 std::string
getFullObjectPath(std::string name, Calibration::ExpRun expRun)
const;
417 const std::vector<Calibration::ExpRun>& requestedRuns)
420 RunRange runRangeRequested(requestedRuns);
421 std::shared_ptr<T> objOutputPtr = std::dynamic_pointer_cast<T>(
m_data.
getCalibObj(name, runRangeRequested));
425 std::shared_ptr<T> mergedObjPtr(
nullptr);
426 bool mergedEmpty =
true;
427 TDirectory* dir = gDirectory;
433 list.SetOwner(
false);
436 std::string runRangeObjName(
getPrefix() +
"/" + Calibration::RUN_RANGE_OBJ_NAME);
440 for (
auto expRunRequested : requestedRuns) {
444 B2WARNING(
"No input file found with data collected from run "
445 "(" << expRunRequested.first <<
"," << expRunRequested.second <<
")");
448 auto files = searchFiles->second;
449 for (
auto fileName : files) {
452 std::unique_ptr<TFile> f;
453 f.reset(TFile::Open(fileName.c_str(),
"READ"));
454 runRangeData =
dynamic_cast<RunRange*
>(f->Get(runRangeObjName.c_str()));
457 if (runSet.find(expRunRequested) == runSet.end()) {
458 B2WARNING(
"Something went wrong with the mapping of ExpRun -> Input Files. "
459 "(" << expRunRequested.first <<
"," << expRunRequested.second <<
") not in " << fileName);
463 TDirectory* objDir = f->GetDirectory(objDirName.c_str());
465 B2ERROR(
"Directory for requested object " << name <<
" not found: " << objDirName);
469 for (
auto key : * (objDir->GetListOfKeys())) {
470 std::string keyName = key->GetName();
471 B2DEBUG(100,
"Adding found object " << keyName <<
" in the directory " << objDir->GetPath());
472 T* objOther = (T*)objDir->Get(keyName.c_str());
475 mergedObjPtr = std::shared_ptr<T>(
dynamic_cast<T*
>(objOther->Clone(name.c_str())));
476 mergedObjPtr->SetDirectory(0);
484 mergedObjPtr->Merge(&list);
492 std::unique_ptr<TFile> f;
493 f.reset(TFile::Open(fileName.c_str(),
"READ"));
496 std::string objPath = objDirName +
"/" + name +
"_1";
497 T* objOther = (T*)f->Get(objPath.c_str());
498 B2DEBUG(100,
"Adding " << objPath);
501 mergedObjPtr = std::shared_ptr<T>(
dynamic_cast<T*
>(objOther->Clone(name.c_str())));
502 mergedObjPtr->SetDirectory(0);
509 mergedObjPtr->Merge(&list);
514 objOutputPtr = mergedObjPtr;
516 B2ERROR(
"No data found for object " << name);
519 objOutputPtr->SetDirectory(0);
521 std::shared_ptr<TNamed> storedObjPtr = std::static_pointer_cast<TNamed>(objOutputPtr);
523 B2DEBUG(100,
"Passing back merged data " << name);
530 template<> std::shared_ptr<TTree>
532 const std::string& name,
533 const std::vector<Calibration::ExpRun>& requestedRuns);
A class that describes the interval of experiments/runs for which an object in the database is valid.
void setIteration(int iteration)
Setter for current iteration.
RunRange getRunRangeFromAllData() const
Get the complete RunRange from inspection of collected data.
CalibrationAlgorithm(const std::string &collectorModuleName)
Constructor - sets the prefix for collected objects (won't be accesses until execute(....
void setInputFileNames(PyObject *inputFileNames)
Set the input file names used for this algorithm from a Python list.
A class to hold all data that is needed ONLY for the most recent single execution of the algorithm.
std::list< Database::DBImportQuery > getPayloadValues()
Get constants (in TObjects) for database update from last calibration but passed by VALUE.
std::map< std::pair< std::string, RunRange >, std::shared_ptr< TNamed > > m_mapCalibData
Map of shared pointers to merged calibration objects created by getObjectPtr() calls.
std::string getGranularityFromData() const
Get the granularity of collected data.
nlohmann::json m_jsonExecutionInput
Optional input JSON object used to make decisions about how to execute the algorithm code.
IntervalOfValidity m_iov
Current IoV to be executed, default empty. Will be either set by user explicitly or generated from co...
std::string getFullObjectPath(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
const nlohmann::json & getInputJsonObject() const
Get the entire top level JSON object. We explicitly say this must be of object type so that we might ...
Calibration::ExpRun convertPyExpRun(PyObject *pyObj)
Performs the conversion of PyObject to ExpRun.
const IntervalOfValidity & getRequestedIov() const
Getter for requested IOV.
void clearCalibrationData()
Clear calibration data.
std::string m_prefix
The name of the TDirectory the collector objects are contained within.
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::vector< std::string > m_inputFileNames
List of input files to the Algorithm, will initially be user defined but then gets the wildcards expa...
const std::string & getDescription() const
Get the description of the algoithm (set by developers in constructor)
Calibration::ExpRun getAllGranularityExpRun() const
Returns the Exp,Run pair that means 'Everything'. Currently unused.
void setPrefix(const std::string &prefix)
Set the prefix used to identify datastore objects.
bool loadInputJson(const std::string &jsonString)
Load the m_inputJson variable from a string (useful from Python interface). The rturn bool indicates ...
std::list< Database::DBImportQuery > & getPayloads()
Get constants (in TObjects) for database update from last execution.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
const std::set< Calibration::ExpRun > & getExpRunSet()
Get access to the stored set.
std::list< Database::DBImportQuery > m_payloads
Payloads saved by execution.
void reset()
Resets this class back to what is needed at the beginning of an execution.
@ c_Iterate
Needs iteration =1 in Python.
std::vector< Calibration::ExpRun > m_requestedRuns
Runs for which the calibration has been last requested, either requested explicitly or generated from...
@ c_OK
Finished successfuly =0 in Python.
void resetInputJson()
Clears the m_inputJson member variable.
const std::vector< Calibration::ExpRun > findPayloadBoundaries(std::vector< Calibration::ExpRun > runs, int iteration=0)
Used to discover the ExpRun boundaries that you want the Python CAF to execute on....
void setDescription(const std::string &description)
Set algorithm description (in constructor)
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 t...
void updateDBObjPtrs(const unsigned int event, const int run, const int experiment)
Updates any DBObjPtrs by calling update(event) for DBStore.
bool checkPyExpRun(PyObject *pyObj)
Checks that a PyObject can be successfully converted to an ExpRun type.
void setRequestedIov(const IntervalOfValidity &iov=IntervalOfValidity(0, 0, -1, -1))
Sets the requested IoV for this execution, based on the.
void clearCalibrationData()
Clear calibration data.
int m_iteration
current iteration for execution
virtual EResult calibrate()=0
Run algo on data - pure virtual: needs to be implemented.
IntervalOfValidity getIovFromAllData() const
Get the complete IoV from inspection of collected data.
std::vector< std::string > getVecInputFileNames() const
Get the input file names used for this algorithm as a STL vector.
bool inputJsonKeyExists(const std::string &key) const
Test for a key in the input JSON object.
const std::vector< Calibration::ExpRun > & getRequestedRuns() const
Returns the vector of ExpRuns.
std::string getCollectorName() const
Alias for prefix.
virtual void boundaryFindingSetup(std::vector< Calibration::ExpRun >, int)
If you need to make some changes to your algorithm class before 'findPayloadBoundaries' is run,...
virtual bool isBoundaryRequired(const Calibration::ExpRun &)
Given the current collector data, make a decision about whether or not this run should be the start o...
std::vector< Calibration::ExpRun > getRunListFromAllData() const
Get the complete list of runs from inspection of collected data.
EResult execute(std::vector< Calibration::ExpRun > runs={}, int iteration=0, IntervalOfValidity iov=IntervalOfValidity())
Runs calibration over vector of runs for a given iteration.
std::string m_granularityOfData
Granularity of input data. This only changes when the input files change so it isn't specific to an e...
void fillRunToInputFilesMap()
Fill the mapping of ExpRun -> Files.
Abstract base class for different kinds of events.
nlohmann::json m_jsonExecutionOutput
Optional output JSON object that can be set during the execution by the underlying algorithm code.
std::string getGranularity() const
Get the granularity of collected data.
@ c_Failure
Failed =3 in Python.
void setResult(EResult result)
Setter for current iteration.
PyObject * getInputFileNames()
Get the input file names used for this algorithm and pass them out as a Python list of unicode string...
void setRequestedRuns(const std::vector< Calibration::ExpRun > &requestedRuns)
Sets the vector of ExpRuns.
void resetOutputJson()
Clears the m_outputJson member variable.
std::list< Database::DBImportQuery > & getPayloads()
Get constants (in TObjects) for database update from last calibration.
std::string getExpRunString(Calibration::ExpRun &expRun) const
Gets the "exp.run" string repr. of (exp,run)
const T getInputJsonValue(const std::string &key) const
Get an input JSON value using a key. The normal exceptions are raised when the key doesn't exist.
int getIteration() const
Getter for current iteration.
void setOutputJsonValue(const std::string &key, const T &value)
Set a key:value pair for the outputJson object, expected to used interally during calibrate()
EResult
The result of calibration.
int getIteration() const
Get current iteration.
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
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...
virtual void boundaryFindingTearDown()
Put your algorithm back into a state ready for normal execution if you need to.
@ c_NotEnoughData
Needs more data =2 in Python.
EResult m_result
Result of execution, default undefined to indicate we haven't run yet.
bool commit()
Submit constants from last calibration into database.
EResult getResult() const
Getter for current result.
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)
Mergeable object holding (unique) set of (exp,run) pairs.
std::string m_description
Description of the algorithm.
const std::string dumpOutputJson() const
Dump the JSON string of the output JSON object.
const T getOutputJsonValue(const std::string &key) const
Get a value using a key from the JSON output object, not sure why you would want to do this.
@ c_Undefined
Not yet known (before execution) =4 in Python.
ExecutionData m_data
Data specific to a SINGLE execution of the algorithm. Gets reset at the beginning of execution.
std::list< Database::DBImportQuery > getPayloadValues()
Get constants (in TObjects) for database update from last execution but passed by VALUE.
std::string getPrefix() const
Get the prefix used for getting calibration data.
virtual ~CalibrationAlgorithm()
Virtual destructor (base class)