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