9#include <top/calibration/TOPFillPatternOffsetAlgorithm.h>
10#include <top/dbobjects/TOPCalFillPatternOffset.h>
37 string expNo = to_string(expRun[0].first);
38 while (expNo.length() < 4) expNo.insert(0,
"0");
39 string runNo = to_string(expRun[0].second);
40 while (runNo.length() < 5) runNo.insert(0,
"0");
41 string outputFileName =
"fillPatternOffset-e" + expNo +
"-r" + runNo +
".root";
42 auto* file = TFile::Open(outputFileName.c_str(),
"recreate");
45 if (fillPattern) fillPattern->Write();
47 if (recBuckets) recBuckets->Write();
55 if (fillPattern and recBuckets and recBuckets->GetEntries() >
m_minEntries) {
57 int offset = chi2->GetMinimumBin() - 1;
58 double fract =
getFraction(recBuckets.get(), fillPattern.get(), offset);
59 fillPatternOffset->set(offset, fract);
61 fillPatternOffset->setUnusable();
79 int RFBuckets = fillPattern->GetNbinsX();
80 auto* a = recBuckets->GetArray();
81 auto* a0 = fillPattern->GetArray();
82 for (
int i = 0; i < RFBuckets; i++) {
83 int k = (i + i0) % RFBuckets;
84 chi2 += a[k] * log(
m_p * a0[i] + 1 -
m_p);
92 int RFBuckets = fillPattern->GetNbinsX();
93 for (
int i = 0; i < RFBuckets; i++) {
94 if (fillPattern->GetBinContent(i + 1) > 0) fillPattern->SetBinContent(i + 1, 1);
96 auto* h_chi2 =
new TH1F(
"chi2",
"#chi^{2} vs. circular shift; circular shift; #chi^{2}", RFBuckets, 0, RFBuckets);
97 for (
int i = 0; i < RFBuckets; i++) {
98 h_chi2->SetBinContent(i + 1,
Chi2(recBuckets, fillPattern, i));
108 int RFBuckets = fillPattern->GetNbinsX();
109 auto* corrBuckets =
new TH1F(
"corrBuckets",
"Offset corrected reconstructed buckets; bucket number",
110 RFBuckets, 0, RFBuckets);
111 auto* a = recBuckets->GetArray();
112 auto* a0 = fillPattern->GetArray();
113 for (
int i = 0; i < RFBuckets; i++) {
114 int k = (i + offset) % RFBuckets;
115 corrBuckets->SetBinContent(i + 1, a[k]);
116 if (a0[i] > 0) n += a[k];
119 double fract = n / n0;
120 double err =
sqrt(fract * (1 - fract) / n0);
122 auto* h_fract =
new TH1F(
"fractions",
"Fractions of matched buckets; ; fraction", 2, 0, 2);
123 h_fract->GetXaxis()->SetBinLabel(1,
"not matched");
124 h_fract->GetXaxis()->SetBinLabel(2,
"matched");
125 h_fract->GetXaxis()->SetLabelSize(0.06);
126 h_fract->GetXaxis()->SetAlphanumeric();
127 h_fract->SetBinContent(1, 1 - fract);
128 h_fract->SetBinContent(2, fract);
129 h_fract->SetBinError(1, err);
130 h_fract->SetBinError(2, err);
131 h_fract->SetMarkerStyle(24);
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.
CalibrationAlgorithm(const std::string &collectorModuleName)
Constructor - sets the prefix for collected objects (won't be accesses until execute(....
Class to store the offset of reconstructed fill pattern.
int m_minEntries
minimal number of entries to perform calibration
double Chi2(TH1F *recBuckets, TH1F *fillPattern, int i0)
Returns chi2 = -2 logL of circularly shifted pattern at a give shift i0.
double m_p
signal fraction for PDF definition
virtual EResult calibrate() final
algorithm implementation
double getFraction(TH1F *recBuckets, TH1F *fillPattern, int offset)
Returns fraction of reconstructed buckets matched with filled buckets after accounting for the offset...
double m_minFract
minimal fraction of matched buckets to save calibration
TOPFillPatternOffsetAlgorithm()
Constructor.
TH1F * getChi2Histogram(TH1F *recBuckets, TH1F *fillPattern)
Returns chi2 values at all possible circular shifts.
std::shared_ptr< T > getObjectPtr(const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
Get calibration data object by name and list of runs, the Merge function will be called to generate t...
double sqrt(double a)
sqrt for double
Abstract base class for different kinds of events.