Belle II Software  release-06-00-14
SVD3SampleCoGTimeCalibrationAlgorithm.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 <svd/calibration/SVD3SampleCoGTimeCalibrationAlgorithm.h>
9 
10 #include <svd/dbobjects/SVDCoGCalibrationFunction.h>
11 #include <svd/calibration/SVD3SampleCoGTimeCalibrations.h>
12 
13 #include <TF1.h>
14 #include <TProfile.h>
15 #include <TH2F.h>
16 #include <framework/logging/Logger.h>
17 #include <iostream>
18 #include <TString.h>
19 #include <TFitResult.h>
20 
21 using namespace std;
22 using namespace Belle2;
23 
24 SVD3SampleCoGTimeCalibrationAlgorithm::SVD3SampleCoGTimeCalibrationAlgorithm(const std::string& str) :
25  CalibrationAlgorithm("SVDTimeCalibrationCollector")
26 {
27  setDescription("SVD3SampleCoGTimeCalibration calibration algorithm");
28  m_id = str;
29 }
30 
32 {
33 
34  gROOT->SetBatch(true);
35 
36  int ladderOfLayer[4] = {7, 10, 12, 16};
37  int sensorOnLayer[4] = {2, 3, 4, 5};
38 
39  auto timeCal = new Belle2::SVDCoGCalibrationFunction();
40  auto payload = new Belle2::SVD3SampleCoGTimeCalibrations::t_payload(*timeCal, m_id);
41 
42  // In the local study, Y. Uematsu tuned the range to (31.5,48), because the correlation is not exactly like pol3. (The deviation appears at around 48, very naive.)
43  // However, original value (-10,80) also seems working.
44  std::unique_ptr<TF1> pol3(new TF1("pol3", "[0] + [1]*x + [2]*[3]*[3]*x - [2]*[3]*x*x + [2]*x*x*x/3", -10,
45  80));
46  // apply parameter limits to be monotonic: [1] > 0 and [2] > 0. upper limits are loose on [2].
47  // lower limit on [1] is the minimum tilt (= tilt at the pole)
48  pol3->SetParLimits(1, 0, 100);
49  pol3->SetParLimits(2, 0, 0.1);
50  // the pole position [3] is different in the data (~40) and the current MC (~45)
51  // apply parameter limits on [3] to help non-linear minimization
52  pol3->SetParLimits(3, 30, 60);
53 
54  FileStat_t info;
55  int cal_rev = 1;
56  while (gSystem->GetPathInfo(Form("algorithm_3SampleCoG_output_rev_%d.root", cal_rev), info) == 0)
57  cal_rev++;
58  std::unique_ptr<TFile> f(new TFile(Form("algorithm_3SampleCoG_output_rev_%d.root", cal_rev), "RECREATE"));
59 
60  auto m_tree = new TTree(Form("rev_%d", cal_rev), "RECREATE");
61  int layer_num, ladder_num, sensor_num, view, ndf;
62  float a, b, c, d, a_err, b_err, c_err, d_err, chi2, p;
63  m_tree->Branch("layer", &layer_num, "layer/I");
64  m_tree->Branch("ladder", &ladder_num, "ladder/I");
65  m_tree->Branch("sensor", &sensor_num, "sensor/I");
66  m_tree->Branch("isU", &view, "isU/I");
67  m_tree->Branch("a", &a, "a/F");
68  m_tree->Branch("b", &b, "b/F");
69  m_tree->Branch("c", &c, "c/F");
70  m_tree->Branch("d", &d, "d/F");
71  m_tree->Branch("a_err", &a_err, "a_err/F");
72  m_tree->Branch("b_err", &b_err, "b_err/F");
73  m_tree->Branch("c_err", &c_err, "c_err/F");
74  m_tree->Branch("d_err", &d_err, "d_err/F");
75  m_tree->Branch("chi2", &chi2, "chi2/F");
76  m_tree->Branch("ndf", &ndf, "ndf/I");
77  m_tree->Branch("p", &p, "p/F");
78 
79  for (int layer = 0; layer < 4; layer++) {
80  layer_num = layer + 3;
81  for (int ladder = 0; ladder < (int)ladderOfLayer[layer]; ladder++) {
82  ladder_num = ladder + 1;
83  for (int sensor = 0; sensor < (int)sensorOnLayer[layer]; sensor++) {
84  sensor_num = sensor + 1;
85  for (view = 1; view > -1; view--) {
86  char side = 'U';
87  if (view == 0)
88  side = 'V';
89  auto hEventT0vsCoG = getObjectPtr<TH2F>(Form("eventT0vsCoG__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
90  auto hEventT0 = getObjectPtr<TH1F>(Form("eventT0__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
91  auto hEventT0nosync = getObjectPtr<TH1F>(Form("eventT0nosync__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
92  B2INFO("Histogram: " << hEventT0vsCoG->GetName() <<
93  " Entries (n. clusters): " << hEventT0vsCoG->GetEntries());
94  if (layer_num == 3 && hEventT0vsCoG->GetEntries() < m_minEntries) {
95  B2INFO("Histogram: " << hEventT0vsCoG->GetName() <<
96  " Entries (n. clusters): " << hEventT0vsCoG->GetEntries() <<
97  " Entries required: " << m_minEntries);
98  B2WARNING("Not enough data, adding one run to the collector");
99  f->Close();
100  gSystem->Unlink(Form("algorithm_3SampleCoG_output_rev_%d.root", cal_rev));
101  return c_NotEnoughData;
102  }
103  for (int i = 1; i <= hEventT0vsCoG->GetNbinsX(); i++) {
104  for (int j = 1; j <= hEventT0vsCoG->GetNbinsY(); j++) {
105  if (hEventT0vsCoG->GetBinContent(i, j) < max(2, int(hEventT0vsCoG->GetEntries() * 0.001))) {
106  hEventT0vsCoG->SetBinContent(i, j, 0);
107  }
108  }
109  }
110  TProfile* pfx = hEventT0vsCoG->ProfileX();
111  std::string name = "pfx_" + std::string(hEventT0vsCoG->GetName());
112  pfx->SetName(name.c_str());
113  // set initial value for d, which is quadratic in the formula.
114  // also for b and c, with the nominal value in data.
115  pol3->SetParameter(1, 1.75);
116  pol3->SetParameter(2, 0.005);
117  pol3->SetParameter(3, 40);
118  TFitResultPtr tfr = pfx->Fit("pol3", "QMRS");
119  double par[4];
120  pol3->GetParameters(par);
121  // double meanT0 = hEventT0->GetMean();
122  // double meanT0NoSync = hEventT0nosync->GetMean();
123  timeCal->set_current(1);
124  timeCal->set_pol3parameters(par[0], par[1] + par[2]*par[3]*par[3], -par[2]*par[3], par[2] / 3);
125  payload->set(layer_num, ladder_num, sensor_num, bool(view), 1, *timeCal);
126  f->cd();
127  hEventT0->Write();
128  hEventT0vsCoG->Write();
129  hEventT0nosync->Write();
130  pfx->Write();
131 
132  if (!tfr) {
133  f->Close();
134  B2FATAL("Fit to the histogram failed in SVD3SampleCoGTimeCalibrationAlgorithm. "
135  << "Check the 2-D histogram to clarify the reason.");
136  } else {
137  a = par[0]; b = par[1]; c = par[2]; d = par[3];
138  a_err = tfr->ParError(0); b_err = tfr->ParError(1); c_err = tfr->ParError(2); d_err = tfr->ParError(3);
139  chi2 = tfr->Chi2();
140  ndf = tfr->Ndf();
141  p = tfr->Prob();
142  m_tree->Fill();
143  }
144 
145  }
146  }
147  }
148  }
149  m_tree->Write();
150  f->Close();
151  saveCalibration(payload, "SVD3SampleCoGTimeCalibrations");
152 
153  //delete f;
154 
155  // probably not needed - would trigger re-doing the collection
156  // if ( ... too large corrections ... ) return c_Iterate;
157  return c_OK;
158 }
159 
160 bool SVD3SampleCoGTimeCalibrationAlgorithm::isBoundaryRequired(const Calibration::ExpRun& currentRun)
161 {
162  float meanRawTimeL3V = 0;
163  // auto eventT0Hist = getObjectPtr<TH1F>("hEventT0FromCDC");
164  auto rawTimeL3V = getObjectPtr<TH1F>("hRawTimeL3V");
165  // float meanEventT0 = eventT0Hist->GetMean();
166  if (!rawTimeL3V) {
168  meanRawTimeL3V = m_previousRawTimeMeanL3V.value();
169  } else {
170  if (rawTimeL3V->GetEntries() > m_minEntries)
171  meanRawTimeL3V = rawTimeL3V->GetMean();
172  else {
174  meanRawTimeL3V = m_previousRawTimeMeanL3V.value();
175  }
176  }
178  B2INFO("Setting start payload boundary to be the first run ("
179  << currentRun.first << "," << currentRun.second << ")");
180  m_previousRawTimeMeanL3V.emplace(meanRawTimeL3V);
181 
182  return true;
183  } else if (abs(meanRawTimeL3V - m_previousRawTimeMeanL3V.value()) > m_allowedTimeShift) {
184  B2INFO("Histogram mean has shifted from " << m_previousRawTimeMeanL3V.value()
185  << " to " << meanRawTimeL3V << ". We are requesting a new payload boundary for ("
186  << currentRun.first << "," << currentRun.second << ")");
187  m_previousRawTimeMeanL3V.emplace(meanRawTimeL3V);
188  return true;
189  } else {
190  return false;
191  }
192 }
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
EResult
The result of calibration.
@ c_OK
Finished successfuly =0 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
std::string m_id
Parameter given to set the UniqueID of the payload.
std::optional< float > m_previousRawTimeMeanL3V
Raw CoG of the previous run.
float m_minEntries
Set the minimun number of entries required in the histograms of layer 3.
virtual bool isBoundaryRequired(const Calibration::ExpRun &currentRun) override
If the event T0 changes significantly return true.
SVDCalibrationsBase< SVDCalibrationsScalar< SVDCoGCalibrationFunction > > t_payload
typedef for the SVDCoGCalibrationFunction payload of all SVD sensors
class to contain the CoG Time calibrations
Abstract base class for different kinds of events.