Belle II Software  release-06-01-15
SVD3SampleELSTimeCalibrationAlgorithm.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/SVD3SampleELSTimeCalibrationAlgorithm.h>
9 
10 #include <svd/dbobjects/SVDCoGCalibrationFunction.h>
11 #include <svd/calibration/SVD3SampleELSTimeCalibrations.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 SVD3SampleELSTimeCalibrationAlgorithm::SVD3SampleELSTimeCalibrationAlgorithm(const std::string& str) :
25  CalibrationAlgorithm("SVDTimeCalibrationCollector")
26 {
27  setDescription("SVD3SampleELSTimeCalibration 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::SVD3SampleELSTimeCalibrations::t_payload(*timeCal, m_id);
41 
42  std::unique_ptr<TF1> pol1pole(new TF1("pol1pole", "[0] + [1]*x + [2]/(x - [3])", -60,
43  0)); // In the local study, Y. Uematsu tuned the range to (-21.5,0). Original value is (-10,80).
44  pol1pole->SetParameter(1, 0.6);
45  pol1pole->SetParameter(2, -150);
46  pol1pole->SetParLimits(1, 0, 1);
47  pol1pole->SetParLimits(2, -300, 0);
48 
49  FileStat_t info;
50  int cal_rev = 1;
51  while (gSystem->GetPathInfo(Form("algorithm_3SampleELS_output_rev_%d.root", cal_rev), info) == 0)
52  cal_rev++;
53  std::unique_ptr<TFile> f(new TFile(Form("algorithm_3SampleELS_output_rev_%d.root", cal_rev), "RECREATE"));
54 
55  auto m_tree = new TTree(Form("rev_%d", cal_rev), "RECREATE");
56  int layer_num, ladder_num, sensor_num, view, ndf;
57  float a, b, c, d, a_err, b_err, c_err, d_err, chi2, p;
58  m_tree->Branch("layer", &layer_num, "layer/I");
59  m_tree->Branch("ladder", &ladder_num, "ladder/I");
60  m_tree->Branch("sensor", &sensor_num, "sensor/I");
61  m_tree->Branch("isU", &view, "isU/I");
62  m_tree->Branch("a", &a, "a/F");
63  m_tree->Branch("b", &b, "b/F");
64  m_tree->Branch("c", &c, "c/F");
65  m_tree->Branch("d", &d, "d/F");
66  m_tree->Branch("a_err", &a_err, "a_err/F");
67  m_tree->Branch("b_err", &b_err, "b_err/F");
68  m_tree->Branch("c_err", &c_err, "c_err/F");
69  m_tree->Branch("d_err", &d_err, "d_err/F");
70  m_tree->Branch("chi2", &chi2, "chi2/F");
71  m_tree->Branch("ndf", &ndf, "ndf/I");
72  m_tree->Branch("p", &p, "p/F");
73 
74  for (int layer = 0; layer < 4; layer++) {
75  layer_num = layer + 3;
76  for (int ladder = 0; ladder < (int)ladderOfLayer[layer]; ladder++) {
77  ladder_num = ladder + 1;
78  for (int sensor = 0; sensor < (int)sensorOnLayer[layer]; sensor++) {
79  sensor_num = sensor + 1;
80  for (view = 1; view > -1; view--) {
81  char side = 'U';
82  if (view == 0)
83  side = 'V';
84  auto hEventT0vsELS = getObjectPtr<TH2F>(Form("eventT0vsCoG__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
85  auto hEventT0 = getObjectPtr<TH1F>(Form("eventT0__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
86  auto hEventT0nosync = getObjectPtr<TH1F>(Form("eventT0nosync__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
87  B2INFO("Histogram: " << hEventT0vsELS->GetName() <<
88  " Entries (n. clusters): " << hEventT0vsELS->GetEntries());
89  if (layer_num == 3 && hEventT0vsELS->GetEntries() < m_minEntries) {
90  B2INFO("Histogram: " << hEventT0vsELS->GetName() <<
91  " Entries (n. clusters): " << hEventT0vsELS->GetEntries() <<
92  " Entries required: " << m_minEntries);
93  B2WARNING("Not enough data, adding one run to the collector");
94  f->Close();
95  gSystem->Unlink(Form("algorithm_3SampleELS_output_rev_%d.root", cal_rev));
96  return c_NotEnoughData;
97  }
98  for (int i = 1; i <= hEventT0vsELS->GetNbinsX(); i++) {
99  for (int j = 1; j <= hEventT0vsELS->GetNbinsY(); j++) {
100  if (hEventT0vsELS->GetBinContent(i, j) < max(2, int(hEventT0vsELS->GetEntries() * 0.001))) {
101  hEventT0vsELS->SetBinContent(i, j, 0);
102  }
103  }
104  }
105  TProfile* pfx = hEventT0vsELS->ProfileX();
106  std::string name = "pfx_" + std::string(hEventT0vsELS->GetName());
107  pfx->SetName(name.c_str());
108  // Remove non-liniarity from the fit by fixing the pole position
109  pol1pole->FixParameter(3, 7);
110  pfx->Fit("pol1pole", "QMRS");
111  pol1pole->SetParLimits(3, 0, 15);
112  TFitResultPtr tfr = pfx->Fit("pol1pole", "QMRS");
113  // There is small possibility that the chi2 minimization ends at the parameter boundary.
114  // In such case, we may get a completely wrong fit function, thus we refit w/o boundaries.
115  // if (tfr->Chi2() > 500) {
116  // B2WARNING("Correlation fit Chi2 is too large, refit with very wide parameter boundaries.");
117  // pol1pole->SetParLimits(1, 0, 0);
118  // pol1pole->SetParLimits(2, 0, 0);
119  // pol1pole->SetParLimits(3, 0, 0);
120  // tfr = pfx->Fit("pol1pole", "QMRS");
121  // pol1pole->SetParLimits(1, 0, 1);
122  // pol1pole->SetParLimits(2, -300, 0);
123  // }
124  double par[4];
125  pol1pole->GetParameters(par);
126  // double meanT0 = hEventT0->GetMean();
127  // double meanT0NoSync = hEventT0nosync->GetMean();
128  timeCal->set_current(3);
129  timeCal->set_elsparameters(par[0], par[1], par[2], par[3]);
130  payload->set(layer_num, ladder_num, sensor_num, bool(view), 1, *timeCal);
131  f->cd();
132  hEventT0->Write();
133  hEventT0vsELS->Write();
134  hEventT0nosync->Write();
135  pfx->Write();
136 
137  if (!tfr) {
138  f->Close();
139  B2FATAL("Fit to the histogram failed in SVD3SampleELSTimeCalibrationAlgorithm. "
140  << "Check the 2-D histogram to clarify the reason.");
141  } else {
142  a = par[0]; b = par[1]; c = par[2]; d = par[3];
143  a_err = tfr->ParError(0); b_err = tfr->ParError(1); c_err = tfr->ParError(2); d_err = tfr->ParError(3);
144  chi2 = tfr->Chi2();
145  ndf = tfr->Ndf();
146  p = tfr->Prob();
147  m_tree->Fill();
148  }
149 
150  }
151  }
152  }
153  }
154  m_tree->Write();
155  f->Close();
156  saveCalibration(payload, "SVD3SampleELSTimeCalibrations");
157 
158  //delete f;
159 
160  // probably not needed - would trigger re-doing the collection
161  // if ( ... too large corrections ... ) return c_Iterate;
162  return c_OK;
163 }
164 
165 bool SVD3SampleELSTimeCalibrationAlgorithm::isBoundaryRequired(const Calibration::ExpRun& currentRun)
166 {
167  float meanRawTimeL3V = 0;
168  // auto eventT0Hist = getObjectPtr<TH1F>("hEventT0FromCDC");
169  auto rawTimeL3V = getObjectPtr<TH1F>("hRawTimeL3V");
170  // float meanEventT0 = eventT0Hist->GetMean();
171  if (!rawTimeL3V) {
173  meanRawTimeL3V = m_previousRawTimeMeanL3V.value();
174  } else {
175  if (rawTimeL3V->GetEntries() > m_minEntries)
176  meanRawTimeL3V = rawTimeL3V->GetMean();
177  else {
179  meanRawTimeL3V = m_previousRawTimeMeanL3V.value();
180  }
181  }
183  B2INFO("Setting start payload boundary to be the first run ("
184  << currentRun.first << "," << currentRun.second << ")");
185  m_previousRawTimeMeanL3V.emplace(meanRawTimeL3V);
186 
187  return true;
188  } else if (abs(meanRawTimeL3V - m_previousRawTimeMeanL3V.value()) > m_allowedTimeShift) {
189  B2INFO("Histogram mean has shifted from " << m_previousRawTimeMeanL3V.value()
190  << " to " << meanRawTimeL3V << ". We are requesting a new payload boundary for ("
191  << currentRun.first << "," << currentRun.second << ")");
192  m_previousRawTimeMeanL3V.emplace(meanRawTimeL3V);
193  return true;
194  } else {
195  return false;
196  }
197 }
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.