9#include <top/calibration/TOPAsicShiftsBS13dAlgorithm.h>
10#include <top/dbobjects/TOPCalAsicShift.h>
29 setDescription(
"Calibration algorithm for carrier shifts of BS13d");
37 auto timeReference = getObjectPtr<TH1F>(
"time_reference");
38 if (not timeReference) {
39 B2ERROR(
"TOPAsicShiftsBS13dAlgorithm: histogram 'time_reference' not found");
42 if (timeReference->GetSumOfWeights() == 0) {
43 B2ERROR(
"TOPAsicShiftsBS13dAlgorithm: histogram 'time_reference' is empty");
47 std::vector<std::shared_ptr<TH1F> > timeCarriers(4,
nullptr);
48 std::vector<int> entries(4, 0);
49 for (
unsigned cb = 0; cb < 4; cb++) {
50 string name =
"time_carr_" + to_string(cb);
51 auto h = getObjectPtr<TH1F>(name);
53 if (h) entries[cb] = h->GetSumOfWeights();
58 for (
int i = 0; i < timeReference->GetNbinsX(); i++) {
66 string expNo = to_string(expRun[0].first);
67 while (expNo.length() < 4) expNo.insert(0,
"0");
68 string runNo = to_string(expRun[0].second);
69 while (runNo.length() < 5) runNo.insert(0,
"0");
70 string outputFileName =
"calibrateBS13d-e" + expNo +
"-r" + runNo +
".root";
71 auto* file = TFile::Open(outputFileName.c_str(),
"recreate");
77 std::vector<TH1F*> chi2Carriers;
78 for (
unsigned i = 0; i < 4; i++) {
79 string name =
"chi2_carr_" + to_string(i);
80 string title =
"chi2 of carrier " + to_string(i);
81 auto* h =
new TH1F(name.c_str(), title.c_str(), nx, xmi, xma);
82 h->SetXTitle(
"shift [127 MHz clk cycles]");
83 h->SetYTitle(
"-2*logL [arbitrary]");
84 chi2Carriers.push_back(h);
87 auto* shiftClk =
new TH1F(
"shift_clk",
"shift in clk units", 4, -0.5, 3.5);
88 shiftClk->SetXTitle(
"boardstack number");
89 shiftClk->SetYTitle(
"shift [127 MHz clk cycles]");
91 auto* shiftTime =
new TH1F(
"shift_time",
"shift in time units", 4, -0.5, 3.5);
92 shiftTime->SetXTitle(
"boardstack number");
93 shiftTime->SetYTitle(
"shift [ns]");
95 auto* signif =
new TH1F(
"significance",
"significance of the result", 4, -0.5, 3.5);
96 signif->SetXTitle(
"boardstack number");
97 signif->SetYTitle(
"significance [sigma]");
101 double timeStep = timeReference->GetBinWidth(1);
102 for (
unsigned cb = 0; cb < 4; cb++) {
103 auto& h_chi = chi2Carriers[cb];
104 auto& h_time = timeCarriers[cb];
105 if (not h_time)
continue;
107 double chi2 = -2.0 *
logL(h_time, shift);
110 double hmin = h_chi->GetMinimum();
111 for (
int i = 0; i < h_chi->GetNbinsX(); i++) {
112 h_chi->SetBinContent(i + 1, h_chi->GetBinContent(i + 1) - hmin);
114 int i0 = h_chi->GetMinimumBin();
115 double x = h_chi->GetBinCenter(i0);
116 shiftClk->SetBinContent(cb + 1, x);
117 shiftTime->SetBinContent(cb + 1, x * timeStep);
118 double s = std::min(h_chi->GetBinContent(i0 - 1), h_chi->GetBinContent(i0 + 1));
119 signif->SetBinContent(cb + 1,
sqrt(s));
124 std::vector<double> shifts;
125 std::vector<double> significances;
126 for (
unsigned cb = 0; cb < 4; cb++) {
127 shifts.push_back(shiftTime->GetBinContent(cb + 1));
128 significances.push_back(signif->GetBinContent(cb + 1));
133 auto time_vs_BS = getObjectPtr<TH2F>(
"time_vs_BS");
137 int Nx = time_vs_BS->GetNbinsX();
138 int Ny = time_vs_BS->GetNbinsY();
140 if (step * 16 == Nx) {
141 auto* time_vs_BS_corr = (TH2F*) time_vs_BS->Clone(
"time_vs_BS_corrected");
142 int i0 = (Nx / 4) * 3;
143 for (
int i = i0; i < Nx; i++) {
144 int cb = (i - i0) / step;
145 int shift = shiftClk->GetBinContent(cb + 1);
147 for (
int k = 0; k < Ny; k++) {
150 if (k0 < Ny) val = time_vs_BS->GetBinContent(i + 1, k0 + 1);
151 time_vs_BS_corr->SetBinContent(i + 1, k + 1, val);
156 B2ERROR(
"TOPAsicShiftsBS13dAlgorithm: can't make corrected histogram");
159 timeReference->Write();
160 for (
auto& h : timeCarriers) {
169 for (
unsigned cb = 0; cb < 4; cb++) {
171 significances[cb] = 0;
177 for (
auto significance : significances) {
189 for (
unsigned cb = 0; cb < 4; cb++) {
190 if (significances[cb] == 0)
continue;
191 for (
unsigned a = 0; a < 4; a++) {
192 unsigned asic = a + cb * 4 + bs * 16;
193 asicShift->setT0(moduleID, asic, shifts[cb]);
215 for (
int i = 0; i < nx -
m_winSize; i++) {
229 x = std::min(std::max(x,
m_i0),
m_i1);
236 std::vector<double> p;
238 p.push_back(
fun(i - ishft));
241 for (
auto x : p) s += x;
242 for (
auto& x : p) x /= s;
253 for (
unsigned i = 0; i < pdf.size(); i++) {
254 logl += h->GetBinContent(i + 1) * log(pdf[i]) - pdf[i];
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)
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
EResult
The result of calibration.
@ c_OK
Finished successfully =0 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
Calibration constants for ASIC shifts of all 16 modules.
double fun(int x)
Returns measured time distribution at x or minVal for outside-range or empty bins.
void setWindow()
Sets the position of a window containing maximum number of entries of reference time distribution.
std::vector< double > getPDF(int shift)
Returns normalized time distribution at a given shift (PDF)
int m_winSize
size of the window on reference time distribution
std::vector< int > m_lastEntries
number of histogram entries
std::vector< double > m_timeReference
reference time distribution
TOPAsicShiftsBS13dAlgorithm()
Constructor.
virtual EResult calibrate() final
algorithm implementation
double m_minVal
minimal function value
double m_minSignificance
minimal result significance to declare c_OK
int m_shiftEnd
shift range: upper limit + 1
int m_i1
last bin of the window on reference time distribution
double logL(std::shared_ptr< TH1F > h, int shift)
Returns log likelihood of a histogram with respect to PDF at a given shift.
int m_shiftBegin
shift range: lower limit
int m_i0
first bin of the window on reference time distribution
double sqrt(double a)
sqrt for double
Abstract base class for different kinds of events.