12 #include <top/calibration/TOPAsicShiftsBS13dAlgorithm.h>
13 #include <top/dbobjects/TOPCalAsicShift.h>
29 TOPAsicShiftsBS13dAlgorithm::TOPAsicShiftsBS13dAlgorithm():
32 setDescription(
"Calibration algorithm for carrier shifts of BS13d");
40 auto timeReference = getObjectPtr<TH1F>(
"time_reference");
41 if (not timeReference) {
42 B2ERROR(
"TOPAsicShiftsBS13dAlgorithm: histogram 'time_reference' not found");
45 if (timeReference->GetSumOfWeights() == 0) {
46 B2ERROR(
"TOPAsicShiftsBS13dAlgorithm: histogram 'time_reference' is empty");
50 std::vector<std::shared_ptr<TH1F> > timeCarriers(4,
nullptr);
51 std::vector<int> entries(4, 0);
52 for (
unsigned cb = 0; cb < 4; cb++) {
53 string name =
"time_carr_" + to_string(cb);
54 auto h = getObjectPtr<TH1F>(name);
56 if (h) entries[cb] = h->GetSumOfWeights();
61 for (
int i = 0; i < timeReference->GetNbinsX(); i++) {
69 string expNo = to_string(expRun[0].first);
70 while (expNo.length() < 4) expNo.insert(0,
"0");
71 string runNo = to_string(expRun[0].second);
72 while (runNo.length() < 5) runNo.insert(0,
"0");
73 string outputFileName =
"calibrateBS13d-e" + expNo +
"-r" + runNo +
".root";
74 auto* file = TFile::Open(outputFileName.c_str(),
"recreate");
80 std::vector<TH1F*> chi2Carriers;
81 for (
unsigned i = 0; i < 4; i++) {
82 string name =
"chi2_carr_" + to_string(i);
83 string title =
"chi2 of carrier " + to_string(i);
84 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, xmi, xma);
85 h->SetXTitle(
"shift [127 MHz clk cycles]");
86 h->SetYTitle(
"-2*logL [arbitrary]");
87 chi2Carriers.push_back(h);
90 auto* shiftClk =
new TH1F(
"shift_clk",
"shift in clk units", 4, -0.5, 3.5);
91 shiftClk->SetXTitle(
"boardstack number");
92 shiftClk->SetYTitle(
"shift [127 MHz clk cycles]");
94 auto* shiftTime =
new TH1F(
"shift_time",
"shift in time units", 4, -0.5, 3.5);
95 shiftTime->SetXTitle(
"boardstack number");
96 shiftTime->SetYTitle(
"shift [ns]");
98 auto* signif =
new TH1F(
"significance",
"significance of the result", 4, -0.5, 3.5);
99 signif->SetXTitle(
"boardstack number");
100 signif->SetYTitle(
"significance [sigma]");
104 double timeStep = timeReference->GetBinWidth(1);
105 for (
unsigned cb = 0; cb < 4; cb++) {
106 auto& h_chi = chi2Carriers[cb];
107 auto& h_time = timeCarriers[cb];
108 if (not h_time)
continue;
110 double chi2 = -2.0 *
logL(h_time, shift);
113 double hmin = h_chi->GetMinimum();
114 for (
int i = 0; i < h_chi->GetNbinsX(); i++) {
115 h_chi->SetBinContent(i + 1, h_chi->GetBinContent(i + 1) - hmin);
117 int i0 = h_chi->GetMinimumBin();
118 double x = h_chi->GetBinCenter(i0);
119 shiftClk->SetBinContent(cb + 1, x);
120 shiftTime->SetBinContent(cb + 1, x * timeStep);
121 double s = std::min(h_chi->GetBinContent(i0 - 1), h_chi->GetBinContent(i0 + 1));
122 signif->SetBinContent(cb + 1, sqrt(s));
127 std::vector<double> shifts;
128 std::vector<double> significances;
129 for (
unsigned cb = 0; cb < 4; cb++) {
130 shifts.push_back(shiftTime->GetBinContent(cb + 1));
131 significances.push_back(signif->GetBinContent(cb + 1));
136 auto time_vs_BS = getObjectPtr<TH2F>(
"time_vs_BS");
140 int Nx = time_vs_BS->GetNbinsX();
141 int Ny = time_vs_BS->GetNbinsY();
143 if (step * 16 == Nx) {
144 auto* time_vs_BS_corr = (TH2F*) time_vs_BS->Clone(
"time_vs_BS_corrected");
145 int i0 = (Nx / 4) * 3;
146 for (
int i = i0; i < Nx; i++) {
147 int cb = (i - i0) / step;
148 int shift = shiftClk->GetBinContent(cb + 1);
150 for (
int k = 0; k < Ny; k++) {
153 if (k0 < Ny) val = time_vs_BS->GetBinContent(i + 1, k0 + 1);
154 time_vs_BS_corr->SetBinContent(i + 1, k + 1, val);
159 B2ERROR(
"TOPAsicShiftsBS13dAlgorithm: can't make corrected histogram");
162 timeReference->Write();
163 for (
auto& h : timeCarriers) {
172 for (
unsigned cb = 0; cb < 4; cb++) {
174 significances[cb] = 0;
180 for (
auto significance : significances) {
192 for (
unsigned cb = 0; cb < 4; cb++) {
193 if (significances[cb] == 0)
continue;
194 for (
unsigned a = 0; a < 4; a++) {
195 unsigned asic = a + cb * 4 + bs * 16;
196 asicShift->setT0(moduleID, asic, shifts[cb]);
218 for (
int i = 0; i < nx -
m_winSize; i++) {
232 x = std::min(std::max(x,
m_i0),
m_i1);
239 std::vector<double> p;
241 p.push_back(
fun(i - ishft));
244 for (
auto x : p) s += x;
245 for (
auto& x : p) x /= s;
256 for (
unsigned i = 0; i < pdf.size(); i++) {
257 logl += h->GetBinContent(i + 1) * log(pdf[i]) - pdf[i];