Belle II Software development
TestDBAccessAlgorithm.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/example_caf_lib/TestDBAccessAlgorithm.h>
9
10#include <memory>
11#include <vector>
12#include <numeric>
13
14#include <TTree.h>
15#include <TRandom.h>
16
17using namespace Belle2;
18
20{
22 " -------------------------- Test Calibration Algoritm -------------------------\n"
23 " \n"
24 " Testing algorithm which accesses DBObjPtr to show how it could be done. \n"
25 " ------------------------------------------------------------------------------\n"
26 );
27}
28
30{
31 // Pulling in data from collector output. It now returns shared_ptr<T> so the underlying pointer
32 // will delete itself automatically at the end of this scope unless you do something
33 auto ttree = getObjectPtr<TTree>("MyTree");
34 if (!ttree) return c_Failure;
35 B2INFO("Number of Entries in MyTree was " << ttree->GetEntries());
36
37 if (ttree->GetEntries() < 100)
38 return c_NotEnoughData;
39
40 // Don't generate payloads if this isn't our first try (even if you set it to).
41 if (getIteration() > 0) {
43 }
44
45 // If this was our first try and you did want to generate payloads, do it.
46 if (getGeneratePayloads()) {
48 return c_Iterate;
49 } else {
50 float distance = getAverageDistanceFromAnswer();
51 if (distance < 1.0) {
53 return c_OK;
54 } else {
56 return c_Iterate;
57 }
58 }
59}
60
63{
64 for (auto expRun : getRunList()) {
65 // Key command to make sure your DBObjPtrs are correct
66 updateDBObjPtrs(1, expRun.second, expRun.first);
67
68 B2INFO("Mean from DB found for (Exp, Run) : ("
69 << expRun.first << "," << expRun.second << ") = "
70 << m_dbMean->getMean());
71
72 float mean = m_dbMean->getMean();
73 mean = ((42.0 - mean) / 3.) + mean;
74 float meanError = m_dbMean->getMeanError();
75
76 B2INFO("New Mean from DB found for (Exp, Run) : ("
77 << expRun.first << "," << expRun.second << ") = "
78 << mean);
79
80 TestCalibMean* dbMean = new TestCalibMean(mean, meanError);
81 saveCalibration(dbMean);
82 }
83}
84
87{
88 for (auto expRun : getRunList()) {
89 // Key command to make sure your DBObjPtrs are correct
90 updateDBObjPtrs(1, expRun.second, expRun.first);
91
92 B2INFO("Mean from DB found for (Exp, Run) : ("
93 << expRun.first << "," << expRun.second << ") = "
94 << m_dbMean->getMean());
95
96 float mean = m_dbMean->getMean();
97 float meanError = m_dbMean->getMeanError();
98
99 TestCalibMean* dbMean = new TestCalibMean(mean, meanError);
100 saveCalibration(dbMean);
101 }
102}
103
106{
107 std::vector<float> vecDist;
108 for (auto expRun : getRunList()) {
109 // Key command to make sure your DBObjPtrs are correct
110 updateDBObjPtrs(1, expRun.second, expRun.first);
111
112 B2INFO("Mean from DB found for (Exp, Run) : ("
113 << expRun.first << "," << expRun.second << ") = "
114 << m_dbMean->getMean());
115
116 vecDist.push_back(42.0 - m_dbMean->getMean());
117 }
118 return std::accumulate(std::begin(vecDist), std::end(vecDist), 0.0) / vecDist.size();
119}
120
122{
123 for (auto expRun : getRunList()) {
124 float mean = gRandom->Gaus(42.0, 5.0);
125 float meanError = gRandom->Gaus(5.0, 0.2);
126 // Example of saving a DBObject.
127 B2INFO("Saving new Mean for (Exp, Run) : ("
128 << expRun.first << "," << expRun.second << ") = "
129 << mean);
130 B2INFO("Saving new MeanError for (Exp, Run) : ("
131 << expRun.first << "," << expRun.second << ") = "
132 << meanError);
133 TestCalibMean* dbMean = new TestCalibMean(mean, meanError);
134 saveCalibration(dbMean, IntervalOfValidity(expRun.first, expRun.second, expRun.first, expRun.second));
135 }
136}
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
void updateDBObjPtrs(const unsigned int event, const int run, const int experiment)
Updates any DBObjPtrs by calling update(event) for DBStore.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
int getIteration() const
Get current iteration.
EResult
The result of calibration.
@ c_OK
Finished successfuly =0 in Python.
@ c_Iterate
Needs iteration =1 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
@ c_Failure
Failed =3 in Python.
A class that describes the interval of experiments/runs for which an object in the database is valid.
Test DBObject.
Definition: TestCalibMean.h:17
bool getGeneratePayloads() const
Getter for m_generatePayloads.
void setGeneratePayloads(const bool &value)
Setter for m_generatePayloads.
TestDBAccessAlgorithm()
Constructor set the prefix to TestCalibration.
void saveSameMeans()
Saves the same DB values for each run into a new localdb, otherwise we aren't creating a DB this iter...
float getAverageDistanceFromAnswer()
Grabs DBObjects from the Database and finds out the average distance from 42.
void generateNewPayloads()
Generates new payloads.
virtual EResult calibrate() override
Run algo on data.
DBObjPtr< TestCalibMean > m_dbMean
dbMean
void reduceDistancesAndSave()
Saves new DB values for each run where they are a little closer to 42.
Abstract base class for different kinds of events.