12 #include <top/calibration/TOPModuleT0DeltaTAlgorithm.h>
13 #include <top/dbobjects/TOPCalModuleT0.h>
19 #include <TMatrixDSym.h>
20 #include <TDecompChol.h>
34 TOPModuleT0DeltaTAlgorithm::TOPModuleT0DeltaTAlgorithm():
46 auto slotPairs = getObjectPtr<TH2F>(
"slots");
48 B2ERROR(
"TOPModuleT0DeltaTAlgorithm: histogram 'slots' not found");
55 string expNo = to_string(expRun[0].first);
56 while (expNo.length() < 4) expNo.insert(0,
"0");
57 string runNo = to_string(expRun[0].second);
58 while (runNo.length() < 5) runNo.insert(0,
"0");
59 string outputFileName =
"moduleT0_rough-e" + expNo +
"-r" + runNo +
".root";
60 auto* file = TFile::Open(outputFileName.c_str(),
"recreate");
66 std::vector<double> deltaT0;
67 std::vector<double> sigma;
68 std::vector<std::vector<double> > A;
69 auto* chi2Fits =
new TH2F(
"chi2_of_fits",
"normalized chi2 of succesfull fits",
70 16, 0.5, 16.5, 16, 0.5, 16.5);
71 chi2Fits->SetXTitle(
"first slot number");
72 chi2Fits->SetYTitle(
"second slot number");
76 for (
int i = 0; i < slotPairs->GetNbinsX(); i++) {
77 for (
int k = 0; k < slotPairs->GetNbinsY(); k++) {
78 if (slotPairs->GetBinContent(i + 1, k + 1) <
m_minEntries)
continue;
79 int slot1 = slotPairs->GetXaxis()->GetBinCenter(i + 1);
80 int slot2 = slotPairs->GetYaxis()->GetBinCenter(k + 1);
81 string name =
"deltaT0_" + to_string(slot1) +
"-" + to_string(slot2);
82 auto h = getObjectPtr<TH1F>(name);
85 if (status != 0)
continue;
89 std::vector<double> a(16, 0);
93 chi2Fits->SetBinContent(i + 1, k + 1,
m_chi2 /
m_ndf);
99 A.push_back(std::vector<double>(16, 1));
100 deltaT0.push_back(0);
101 sigma.push_back(0.001);
105 int m = deltaT0.size();
110 B2INFO(
"TOPModuleT0DeltaTAlgorithm: NDF < 0");
117 for (
int i = 0; i < 16; i++) {
118 for (
int j = 0; j < 16; j++) {
119 for (
int k = 0; k < m; k++) {
120 B[i][j] += A[k][i] * A[k][j] / (sigma[k] * sigma[k]);
128 bool ok = C.Invert(B);
132 B2INFO(
"TOPModuleT0DeltaTAlgorithm: matrix is not positive definite");
138 std::vector<double> b(16, 0);
139 for (
int i = 0; i < 16; i++) {
140 for (
int k = 0; k < m; k++) {
141 b[i] += A[k][i] * deltaT0[k] / (sigma[k] * sigma[k]);
147 std::vector<double> x(16, 0);
148 std::vector<double> e(16, 0);
149 for (
int i = 0; i < 16; i++) {
150 for (
int j = 0; j < 16; j++) {
151 x[i] += B[i][j] * b[j];
153 e[i] = sqrt(B[i][i]);
159 for (
int k = 0; k < m; k++) {
161 for (
int i = 0; i < 16; i++) {
164 chi2 += pow((s - deltaT0[k]) / sigma[k], 2);
171 ss <<
"Module T0, chi^2/NDF = " << chi2 <<
"/" << ndf;
172 string title = ss.str();
174 auto* h_moduleT0 =
new TH1F(
"moduleT0", title.c_str(), 16, 0.5, 16.5);
175 h_moduleT0->SetXTitle(
"slot number");
176 h_moduleT0->SetYTitle(
"module T0 [ns]");
177 for (
int i = 0; i < 16; i++) {
178 h_moduleT0->SetBinContent(i + 1, x[i]);
179 h_moduleT0->SetBinError(i + 1, e[i]);
196 for (
int i = 0; i < 16; i++) {
197 moduleT0->setT0(i + 1, x[i], e[i]);
199 moduleT0->suppressAverage();
208 int numEntries = h->GetSumOfWeights();
222 double sum = h->GetSumOfWeights();
223 if (sum < 5)
return 5;
224 double maxPosition = h->GetBinCenter(h->GetMaximumBin());
225 double binWidth = h->GetBinWidth(1);
226 double xmin = h->GetXaxis()->GetXmin();
227 double xmax = h->GetXaxis()->GetXmax();
229 auto* func =
new TF1(
"func1g",
230 "[0] + [1]/sqrt(2*pi)/[3]*exp(-0.5*((x-[2])/[3])**2)",
232 func->SetParameter(0, 0);
233 func->SetParameter(1, sum * binWidth);
234 func->SetParameter(2, maxPosition);
237 int status = h->Fit(func,
"LERSQ");
239 m_delT0 = func->GetParameter(2);
240 m_error = func->GetParError(2);
241 m_chi2 = func->GetChisquare();
242 m_ndf = func->GetNDF();
250 double sum = h->GetSumOfWeights();
251 if (sum < 7)
return 7;
252 double maxPosition = h->GetBinCenter(h->GetMaximumBin());
253 double binWidth = h->GetBinWidth(1);
254 double xmin = h->GetXaxis()->GetXmin();
255 double xmax = h->GetXaxis()->GetXmax();
257 auto* func =
new TF1(
"func2g",
258 "[0] + [1]*((1-[4])/sqrt(2*pi)/[3]*exp(-0.5*((x-[2])/[3])**2)"
259 "+ [4]/sqrt(2*pi)/[5]*exp(-0.5*((x-[2])/[5])**2))",
261 func->SetParameter(0, 0);
262 func->SetParameter(1, sum * binWidth);
263 func->SetParameter(2, maxPosition);
268 int status = h->Fit(func,
"LERSQ");
270 m_delT0 = func->GetParameter(2);
271 m_error = func->GetParError(2);
272 m_chi2 = func->GetChisquare();
273 m_ndf = func->GetNDF();