Belle II Software  release-05-02-19
TOPCommonT0LLAlgorithm.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 - Belle II Collaboration *
4  * *
5  * *
6  * Author: The Belle II Collaboration *
7  * Contributors: Marko Staric *
8  * *
9  * This software is provided "as is" without any warranty. *
10  **************************************************************************/
11 
12 #include <top/calibration/TOPCommonT0LLAlgorithm.h>
13 #include <top/dbobjects/TOPCalCommonT0.h>
14 #include <top/utilities/Chi2MinimumFinder1D.h>
15 
16 #include <math.h>
17 #include <TFile.h>
18 #include <TTree.h>
19 #include <TH1F.h>
20 #include <TH1D.h>
21 #include <TH2F.h>
22 
23 #include <string>
24 #include <vector>
25 
26 using namespace std;
27 
28 namespace Belle2 {
33  namespace TOP {
34 
35  TOPCommonT0LLAlgorithm::TOPCommonT0LLAlgorithm():
36  CalibrationAlgorithm("TOPCommonT0LLCollector")
37  {
38  setDescription("Calibration algorithm for common T0 calibration "
39  "with neg. log likelihood minimization (method LL)");
40  }
41 
43  {
44 
45  // get histograms
46 
47  auto h1 = getObjectPtr<TH1F>("tracks_per_set");
48  if (not h1) {
49  B2ERROR("TOPCommonT0LLAlgorithm: histogram 'tracks_per_set' not found");
50  return c_NotEnoughData;
51  }
52  unsigned numSets = h1->GetNbinsX();
53 
54  vector<Chi2MinimumFinder1D> finders;
55  for (unsigned set = 0; set < numSets; set++) {
56  string name = "chi2_set" + to_string(set);
57  auto h = getObjectPtr<TH1D>(name);
58  if (not h) continue;
59  finders.push_back(Chi2MinimumFinder1D(h));
60  }
61  if (finders.size() != numSets) {
62  B2ERROR("TOPCommonT0LLAlgorithm: got number of chi2 scans not as expected"
63  << LogVar("expected", numSets)
64  << LogVar("found", finders.size()));
65  return c_NotEnoughData;
66  }
67 
68  // bunch separation in time
69 
70  auto h4 = getObjectPtr<TH1F>("offset");
71  if (not h4) {
72  B2ERROR("TOPCommonT0LLAlgorithm: histogram 'offset' not found");
73  return c_NotEnoughData;
74  }
75  double bunchTimeSep = h4->GetXaxis()->GetXmax() - h4->GetXaxis()->GetXmin();
76 
77  // construct file name, open output root file and book output tree
78 
79  const auto& expRun = getRunList();
80  string expNo = to_string(expRun[0].first);
81  while (expNo.length() < 4) expNo.insert(0, "0");
82  string runNo = to_string(expRun[0].second);
83  while (runNo.length() < 5) runNo.insert(0, "0");
84  string outputFileName = "commonT0-e" + expNo + "-r" + runNo + ".root";
85  auto* file = TFile::Open(outputFileName.c_str(), "recreate");
86 
87  auto* tree = new TTree("tree", "common T0 calibration results");
88  tree->Branch<int>("expNum", &m_expNo);
89  tree->Branch<int>("runNum", &m_runNo);
90  tree->Branch<int>("runLast", &m_runLast);
91  tree->Branch<float>("fitted_offset", &m_fittedOffset);
92  tree->Branch<float>("offset", &m_offset);
93  tree->Branch<float>("offsetErr", &m_offsetError);
94  tree->Branch<float>("errorScaling", &m_errorScaling);
95  tree->Branch<int>("nTracks", &m_numTracks);
96  tree->Branch<int>("nEvt", &m_numEvents);
97  tree->Branch<int>("fitStatus", &m_fitStatus);
98 
99  m_expNo = expRun[0].first;
100  m_runNo = expRun[0].second;
101  m_runLast = expRun.back().second;
102 
103  // determine scaling factor for the error from statistically independent results
104 
105  auto h_pulls = new TH1F("pulls", "Pulls of statistically independent results",
106  200, -15.0, 15.0);
107  h_pulls->SetXTitle("pulls");
108  std::vector<double> pos, err;
109  for (auto& finder : finders) {
110  const auto& minimum = finder.getMinimum();
111  if (not minimum.valid) continue;
112  pos.push_back(minimum.position);
113  err.push_back(minimum.error);
114  }
115  for (unsigned i = 0; i < pos.size(); i++) {
116  for (unsigned j = i + 1; j < pos.size(); j++) {
117  double pull = (pos[i] - pos[j]) / sqrt(err[i] * err[i] + err[j] * err[j]);
118  h_pulls->Fill(pull);
119  }
120  }
121  double scaleError = 1;
122  if (h_pulls->GetEntries() > 1) scaleError = h_pulls->GetRMS();
123 
124  // merge statistically independent finders and store results into histograms
125 
126  auto finder = finders[0];
127  for (unsigned i = 1; i < numSets; i++) {
128  finder.add(finders[i]);
129  }
130 
131  auto h_commonT0 = new TH1F("commonT0", "Common T0", 1, 0, 1);
132  h_commonT0->SetYTitle("common T0 [ns]");
133 
134  const auto& minimum = finder.getMinimum();
135  if (minimum.valid) {
136  m_fittedOffset = minimum.position;
137  m_offset = m_fittedOffset - round(m_fittedOffset / bunchTimeSep) * bunchTimeSep;
138  m_offsetError = minimum.error * scaleError;
139  m_fitStatus = 0;
140  h_commonT0->SetBinContent(1, m_offset);
141  h_commonT0->SetBinError(1, m_offsetError);
142  } else {
143  m_fittedOffset = 0;
144  m_offset = 0;
145  m_offsetError = 0;
146  m_fitStatus = 1;
147  }
148  m_errorScaling = scaleError;
149  m_numTracks = h1->GetSumOfWeights();
150  m_numEvents = h4->GetEntries();
151  tree->Fill();
152 
153  // write the results and close the file
154 
155  file->Write();
156  auto h = finder.getHistogram("chi2", "chi2 scan; t0 [ns]; -2 logL");
157  h.Write();
158  h1->Write();
159  auto h2 = getObjectPtr<TH1F>("numHits");
160  if (h2) h2->Write();
161  auto h3 = getObjectPtr<TH2F>("timeHits");
162  if (h3) h3->Write();
163  h4->Write();
164  file->Close();
165 
166  // check the results and return if not good enough
167 
168  if (m_fitStatus != 0 or m_offsetError > m_minError) return c_NotEnoughData;
169 
170  // otherwise create and import payload to DB
171 
172  auto* commonT0 = new TOPCalCommonT0(m_offset, m_offsetError);
173  saveCalibration(commonT0);
174 
175  return c_OK;
176  }
177 
178  } // end namespace TOP
180 } // end namespace Belle2
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::TOP::Chi2MinimumFinder1D
Minimum finder using tabulated chi^2 values in one dimension.
Definition: Chi2MinimumFinder1D.h:37
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
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
LogVar
Class to store variables with their name which were sent to the logging service.
Definition: LogVariableStream.h:24
Belle2::TOP::TOPCommonT0LLAlgorithm::calibrate
virtual EResult calibrate() final
algorithm implementation
Definition: TOPCommonT0LLAlgorithm.cc:42
Belle2::TOP::TOPCommonT0LLAlgorithm::m_errorScaling
float m_errorScaling
factor used for scaling the error
Definition: TOPCommonT0LLAlgorithm.h:67
Belle2::TOP::TOPCommonT0LLAlgorithm::m_runNo
int m_runNo
first run number
Definition: TOPCommonT0LLAlgorithm.h:62
Belle2::TOP::TOPCommonT0LLAlgorithm::m_fitStatus
int m_fitStatus
fit status (0 = OK)
Definition: TOPCommonT0LLAlgorithm.h:70
Belle2::TOP::TOPCommonT0LLAlgorithm::m_runLast
int m_runLast
last run number
Definition: TOPCommonT0LLAlgorithm.h:63
Belle2::CalibrationAlgorithm::EResult
EResult
The result of calibration.
Definition: CalibrationAlgorithm.h:50
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::TOP::TOPCommonT0LLAlgorithm::m_numTracks
int m_numTracks
number of tracks used
Definition: TOPCommonT0LLAlgorithm.h:68
Belle2::CalibrationAlgorithm::c_NotEnoughData
@ c_NotEnoughData
Needs more data =2 in Python.
Definition: CalibrationAlgorithm.h:53
Belle2::TOP::TOPCommonT0LLAlgorithm::m_offsetError
float m_offsetError
error on fitted offset
Definition: TOPCommonT0LLAlgorithm.h:66
Belle2::CalibrationAlgorithm
Base class for calibration algorithms.
Definition: CalibrationAlgorithm.h:47
Belle2::TOP::TOPCommonT0LLAlgorithm::m_numEvents
int m_numEvents
number of events used
Definition: TOPCommonT0LLAlgorithm.h:69
Belle2::TOP::TOPCommonT0LLAlgorithm::m_minError
double m_minError
minimal commonT0 uncertainty [ns] to declare c_OK
Definition: TOPCommonT0LLAlgorithm.h:58
Belle2::TOP::TOPCommonT0LLAlgorithm::m_expNo
int m_expNo
experiment number
Definition: TOPCommonT0LLAlgorithm.h:61
Belle2::TOPCalCommonT0
Common T0 calibration constant.
Definition: TOPCalCommonT0.h:33
Belle2::TOP::TOPCommonT0LLAlgorithm::m_offset
float m_offset
wrap-around of fitted offset (= common T0)
Definition: TOPCommonT0LLAlgorithm.h:65
Belle2::TOP::TOPCommonT0LLAlgorithm::m_fittedOffset
float m_fittedOffset
fitted offset
Definition: TOPCommonT0LLAlgorithm.h:64