35 gROOT->SetBatch(
true);
37 gStyle->SetFillColor(0);
38 gStyle->SetFillStyle(0);
39 gStyle->SetOptStat(0);
44 while (gSystem->GetPathInfo(Form(
"algorithm_svdClusterAbsoluteTimeShifter_%s_output_rev_%d.root",
m_timeAlgorithms[0].Data(),
52 auto fn_singleGaus = std::make_unique<TF1>(
"fn_singleGaus",
AbssingleGaus, -50., 50., 4);
53 fn_singleGaus->SetLineColor(kGreen + 2);
54 fn_singleGaus->SetParName(0,
"N");
55 fn_singleGaus->SetParName(1,
"#mu");
56 fn_singleGaus->SetParName(2,
"#sigma");
57 fn_singleGaus->SetParName(3,
"C");
60 auto fn_doubleGaus = std::make_unique<TF1>(
"fn_doubleGaus",
AbsdoubleGaus, -50., 50., 7);
61 fn_doubleGaus->SetLineColor(kRed + 2);
62 fn_doubleGaus->SetParName(0,
"N");
63 fn_doubleGaus->SetParName(1,
"f");
64 fn_doubleGaus->SetParName(2,
"#mu_{1}");
65 fn_doubleGaus->SetParName(3,
"#sigma_{1}");
66 fn_doubleGaus->SetParName(4,
"#mu_{2}");
67 fn_doubleGaus->SetParName(5,
"#sigma_{2}");
68 fn_doubleGaus->SetParName(6,
"C");
69 fn_doubleGaus->SetParLimits(1, 0.01, 0.99);
70 fn_doubleGaus->SetParLimits(3, 0.5, 25.);
71 fn_doubleGaus->SetParLimits(5, 0.5, 25.);
77 B2INFO(
"Calculating shift for algorithm " << alg);
83 std::map< TString, Double_t > shiftValues;
85 std::unique_ptr<TFile> f(
new TFile(Form(
"algorithm_svdClusterAbsoluteTimeShifter_%s_output_rev_%d.root", alg.Data(), cal_rev),
88 B2INFO(
"ROOT file created at: " << gSystem->WorkingDirectory() <<
"/" << f->GetName());
90 TH1D* hShiftMean =
new TH1D(
"hShiftMean",
91 Form(
"Fitted shift mean (%s);Bin;Mean (ns)", alg.Data()), nBins, 0.5, nBins + 0.5);
92 TH1D* hShiftSigma =
new TH1D(
"hShiftSigma",
93 Form(
"Fitted shift sigma (%s);Bin;Sigma (ns)", alg.Data()), nBins, 0.5, nBins + 0.5);
95 for (
int s = 0; s < 2; s++) {
97 hShiftMean ->GetXaxis()->SetBinLabel(b, TString::Format(
"L%dS%c", l, s == 0 ?
'U' :
'V'));
98 hShiftSigma->GetXaxis()->SetBinLabel(b, TString::Format(
"L%dS%c", l, s == 0 ?
'U' :
'V'));
101 TString outPDF = Form(
"algorithm_svdClusterAbsoluteTimeShifter_%s_output_rev_%d.pdf", alg.Data(), cal_rev);
102 TCanvas c1(
"c1",
"c1", 640, 480);
103 c1.Print(outPDF +
"[");
104 TPad onePad(
"onePad",
"onePad", 0, 0, 1, 1, kWhite);
105 onePad.SetFillColor(0);
106 onePad.SetBorderMode(0);
107 onePad.SetBorderSize(2);
108 onePad.SetRightMargin(0.1339713);
109 onePad.SetBottomMargin(0.15);
110 onePad.SetFrameBorderMode(0);
111 onePad.SetFrameBorderMode(0);
117 for (
int side = 0; side < 2; side++) {
118 int LayerSensorID = 2 * layer - side;
122 TString binLabel = TString::Format(
"L%iS%c", layer, (side == 0 ?
'U' :
'V'));
123 TH1F* hist = (TH1F*)h_ClustersOnTrack->ProjectionX(Form(
"hClsTimeOnTracks_L%dS%c", layer, (side == 0 ?
'U' :
'V')), LayerSensorID,
125 hist->SetTitle(Form(
"Cluster Time in L%dS%c", layer, (side == 0 ?
'U' :
'V')));
126 hist->SetDirectory(0);
128 B2INFO(
"Histogram: " << hist->GetName() <<
129 " Entries (n. clusters): " << hist->GetEntries());
131 B2INFO(
"Histogram: " << hist->GetName() <<
132 " Entries (n. clusters): " << hist->GetEntries() <<
134 B2WARNING(
"Not enough data, adding one run to the collector");
136 c1.Print(outPDF +
"]");
138 gSystem->Unlink(Form(
"algorithm_svdClusterAbsoluteTimeShifter_%s_output_rev_%d.root",
m_timeAlgorithms[0].Data(), cal_rev));
141 if (hist->GetEntries() == 0) {
142 B2INFO(
"Histogram: " << hist->GetName() <<
143 " Entries (n. clusters): " << hist->GetEntries() <<
144 " No entries, setting shift to 0.");
145 shiftValues[binLabel] = 0.;
152 if (hist->GetMaximum() < 200.) {
153 int rebinValue = 200. / hist->GetMaximum() + 1.;
154 while ((hist->GetNbinsX()) % rebinValue != 0) rebinValue++;
155 hist->Rebin(rebinValue);
159 bool isSingleGausFitValid, isDoubleGausFitValid;
160 while (fitCount++ < 5) {
161 double histMean = hist->GetMean();
162 double histStd = hist->GetStdDev();
163 fn_singleGaus->SetParameter(0, hist->GetSumOfWeights() * hist->GetBinWidth(1));
164 fn_singleGaus->SetParameter(1, histMean);
165 fn_singleGaus->SetParameter(2, histStd * 0.75);
166 fn_singleGaus->SetParameter(3, 1.);
167 fn_singleGaus->SetParLimits(1, histMean - histStd, histMean + histStd);
168 auto singleGausFitStatus = hist->Fit(
"fn_singleGaus",
"SQ",
"",
169 histMean - 2. * histStd, histMean + 2. * histStd);
170 isSingleGausFitValid = singleGausFitStatus->IsValid();
172 fn_doubleGaus->SetParameter(0, hist->GetSumOfWeights() * hist->GetBinWidth(1));
173 fn_doubleGaus->SetParameter(1, 0.95);
174 fn_doubleGaus->SetParameter(2, fn_singleGaus->GetParameter(1));
175 fn_doubleGaus->SetParameter(3, std::fabs(fn_singleGaus->GetParameter(2)));
176 fn_doubleGaus->SetParameter(4, fn_singleGaus->GetParameter(1) - 3.);
177 fn_doubleGaus->SetParameter(5, std::fabs(fn_singleGaus->GetParameter(2)) + 5.);
178 fn_doubleGaus->SetParameter(6, 10.);
179 fn_doubleGaus->SetParLimits(2,
182 fn_doubleGaus->SetParLimits(4,
186 auto doubleGausFitStatus = hist->Fit(
"fn_doubleGaus",
"SQ+");
187 isDoubleGausFitValid = doubleGausFitStatus->IsValid();
188 if (isDoubleGausFitValid)
break;
190 while ((hist->GetNbinsX()) % rebinValue != 0) rebinValue++;
191 hist->Rebin(rebinValue);
194 TPaveStats* ptstats =
new TPaveStats(0.55, 0.73, 0.85, 0.88,
"brNDC");
195 ptstats->SetName(
"stats1");
196 ptstats->SetBorderSize(1);
197 ptstats->SetFillColor(0);
198 ptstats->SetTextAlign(12);
199 ptstats->SetTextFont(42);
200 ptstats->SetTextColor(kGreen + 2);
201 ptstats->SetOptStat(11);
202 ptstats->SetParent(hist);
204 ptstats->AddText(
"Single Gauss");
205 for (
int npar = 0; npar < (fn_singleGaus->GetNpar()); npar++)
206 ptstats->AddText(TString::Format(
"%s = %.3f #pm %.4f",
207 fn_singleGaus->GetParName(npar),
208 fn_singleGaus->GetParameter(npar),
209 fn_singleGaus->GetParError(npar)));
210 ptstats =
new TPaveStats(0.55, 0.49, 0.85, 0.73,
"brNDC");
211 ptstats->SetName(
"stats2");
212 ptstats->SetBorderSize(1);
213 ptstats->SetFillColor(0);
214 ptstats->SetTextAlign(12);
215 ptstats->SetTextFont(42);
216 ptstats->SetTextColor(kRed + 2);
217 ptstats->SetOptStat(11);
218 ptstats->SetParent(hist);
220 ptstats->AddText(
"Double Gauss");
221 for (
int npar = 0; npar < (fn_doubleGaus->GetNpar()); npar++)
222 ptstats->AddText(TString::Format(
"%s = %.3f #pm %.4f",
223 fn_doubleGaus->GetParName(npar),
224 fn_doubleGaus->GetParameter(npar),
225 fn_doubleGaus->GetParError(npar)));
226 ptstats =
new TPaveStats(0.55, 0.43, 0.85, 0.49,
"brNDC");
227 ptstats->SetName(
"stats3");
228 ptstats->SetBorderSize(1);
229 ptstats->SetFillColor(0);
230 ptstats->SetTextAlign(12);
231 ptstats->SetTextFont(42);
232 ptstats->SetTextColor(kBlue + 2);
233 ptstats->SetOptStat(11);
234 ptstats->SetParent(hist);
241 if (isDoubleGausFitValid) {
242 fillShiftVal = (fn_doubleGaus->GetParameter(1) > 0.5 ?
243 fn_doubleGaus->GetParameter(2) : fn_doubleGaus->GetParameter(4));
244 ptstats->AddText(TString::Format(
"#splitline{Shift Value from Double Gauss}{%.3f}", fillShiftVal));
245 }
else if (isSingleGausFitValid) {
246 fillShiftVal = fn_singleGaus->GetParameter(1);
247 B2WARNING(
"Fit failed for " << hist->GetName() <<
248 "; using mean from single gauss fit. ");
249 ptstats->AddText(TString::Format(
"#splitline{Shift Value from Single Gauss}{%.3f}", fillShiftVal));
253 B2WARNING(
"Shift value is more than allowed or fit failed in " <<
254 hist->GetName() <<
" : " <<
255 shiftValues[binLabel] <<
256 "; using mean of the histogram.");
257 fillShiftVal = hist->GetMean();
258 ptstats->AddText(TString::Format(
"#splitline{Shift Value from Histogram Mean}{%.3f}", fillShiftVal));
261 shiftValues[binLabel] = 1. * int(1000. * fillShiftVal) / 1000.;
264 if (isDoubleGausFitValid)
265 fillSigmaVal = (fn_doubleGaus->GetParameter(1) > 0.5 ?
266 fn_doubleGaus->GetParameter(3) : fn_doubleGaus->GetParameter(5));
267 else if (isSingleGausFitValid)
268 fillSigmaVal = fn_singleGaus->GetParameter(2);
270 fillSigmaVal = hist->GetStdDev();
272 int binIdx = 2 * (layer - 3) + side + 1;
273 hShiftMean ->SetBinContent(binIdx, fillShiftVal);
274 hShiftSigma->SetBinContent(binIdx, fillSigmaVal);
277 hist->GetXaxis()->SetRangeUser(-50, 50);
278 gPad->Range(-50, 0, 50, hist->GetMaximum() * 1.2);
281 c1.Print(outPDF, TString(
"Title:") + hist->GetName());
292 for (
auto item : shiftValues)
293 payload->setClusterTimeShift(alg, item.first, item.second);
294 B2INFO(
"Shift values for algorithm " + alg +
" : ");
295 for (
const auto& item : shiftValues) {
296 B2INFO(
" " << item.first <<
" : " << item.second);
304 c1.Print(outPDF +
"]");
307 hShiftSigma->Write();