1 #include <cdc/calibration/XTCalibrationAlgorithm.h>
2 #include <cdc/calibration/XTFunction.h>
3 #include <cdc/geometry/CDCGeometryPar.h>
4 #include <cdc/dbobjects/CDCXtRelations.h>
15 #include <framework/database/DBObjPtr.h>
21 typedef std::array<float, 3> array3;
25 " -------------------------- XT Calibration Algorithm -------------------------\n"
32 B2INFO(
"create and fill histo");
35 for (
int i = 0; i < 56; ++i) {
36 for (
int lr = 0; lr < 2; ++lr) {
39 m_hProf[i][lr][al][th] =
new TProfile(Form(
"m_hProf%d_%d_%d_%d", i, lr, al, th),
40 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
42 m_hist2d[i][lr][al][th] =
new TH2F(Form(
"h%d_%d_%d_%d", i, lr, al, th),
43 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
46 m_hist2dDraw[i][al][th] =
new TH2F(Form(
"h_draw%d_%d_%d", i, al, th),
47 Form(
"(L=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
56 auto tree = getObjectPtr<TTree>(
"tree");
61 Float_t Pval, alpha, theta;
64 tree->SetBranchAddress(
"lay", &lay);
65 tree->SetBranchAddress(
"t", &dt);
66 tree->SetBranchAddress(
"x_u", &dx);
67 tree->SetBranchAddress(
"alpha", &alpha);
68 tree->SetBranchAddress(
"theta", &theta);
69 tree->SetBranchAddress(
"Pval", &Pval);
70 tree->SetBranchAddress(
"ndf", &ndf);
73 std::vector<TString> list_vars = {
"lay",
"t",
"x_u",
"alpha",
"theta",
"Pval",
"ndf"};
74 tree->SetBranchStatus(
"*", 0);
76 for (TString brname : list_vars) {
77 tree->SetBranchStatus(brname, 1);
83 const Long64_t nEntries = tree->GetEntries();
84 B2INFO(
"Number of entries " << nEntries);
85 for (Long64_t i = 0; i < nEntries; ++i) {
88 if (fabs(alpha > 90)) {
89 if (alpha < 0) alpha += 180;
90 if (alpha > 0) alpha -= 180;
107 int lr = dx > 0 ? c_Right : c_Left;
109 m_hProf[lay][lr][al][th]->Fill(dt, abs(dx));
110 m_hist2d[lay][lr][al][th]->Fill(dt, abs(dx));
112 m_hProf[lay][0][al][th]->Fill(dt, abs(dx));
113 m_hist2d[lay][0][al][th]->Fill(dt, abs(dx));
114 m_hProf[lay][1][al][th]->Fill(dt, abs(dx));
115 m_hist2d[lay][1][al][th]->Fill(dt, abs(dx));
124 gPrintViaErrorHandler =
true;
125 gErrorIgnoreLevel = 3001;
126 B2INFO(
"Start calibration");
130 B2INFO(
"ExpRun used for DB Geometry : " << exprun.first <<
" " << exprun.second);
132 B2INFO(
"Creating CDCGeometryPar object");
138 B2INFO(
"Start Fitting");
139 std::unique_ptr<XTFunction> xt;
140 for (
int l = 0; l < 56; ++l) {
141 for (
int lr = 0; lr < 2; ++lr) {
151 m_hist2d[l][lr][al][th]->FitSlicesY(0, 0, -1, 5);
152 m_hist2d_1[l][lr][al][th] = (TH1F*)gDirectory->Get(Form(
"h%d_%d_%d_%d_1", l, lr, al, th));
155 B2WARNING(
"Error, not found results of slices fit");
158 m_hist2d_1[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
159 fpol1 = (TF1*)
m_hProf[l][lr][al][th]->GetFunction(
"pol1");
162 for (
int n = 0; n <
m_hProf[l][lr][al][th]->GetNbinsX(); ++n) {
163 if (
m_hProf[l][lr][al][th]->GetBinEntries(n) < 5 &&
m_hProf[l][lr][al][th]->GetBinEntries(n) > 1) {
164 m_hProf[l][lr][al][th]->SetBinError(n, 0.3 /
m_hProf[l][lr][al][th]->GetBinEntries(n));
167 m_hProf[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
168 fpol1 = (TF1*)
m_hProf[l][lr][al][th]->GetFunction(
"pol1");
173 p0 = fpol1->GetParameter(0);
174 p1 = fpol1->GetParameter(1);
175 tmin = -1 * p0 / p1 + 15;
205 double p6 =
m_xtPrior[l][lr][ial_old][ith_old][6];
225 B2WARNING(
"Empty xt");
230 B2WARNING(
"Fit failed");
258 const double tMax = 500;
259 for (
int l = 0; l < 56; ++l) {
260 for (
int lr = 0; lr < 2; ++lr) {
263 if (
m_fitStatus[l][lr][al][th] == FitStatus::c_OK) {
265 double y = fun->Eval(tMax);
267 B2INFO(
"Strange XT function l " << l <<
" lr " << lr <<
" alpha " << al <<
" theta " << th
268 <<
", replaced by initial one");
269 fun->SetParameters(
m_xtPrior[l][lr][al][th]);
281 int nFitCompleted = 0;
282 for (
int l = 0; l < 56; ++l) {
283 for (
int lr = 0; lr < 2; ++lr) {
286 if (
m_fitStatus[l][lr][al][th] == FitStatus::c_OK) {
294 if (
static_cast<double>(nFitCompleted) / nTotal <
m_threshold) {
295 B2WARNING(
"Less than " <<
m_threshold * 100 <<
" % of XTs were fitted.");
303 B2INFO(
"Prepare calibration of XT");
304 const double rad2deg = 180 / M_PI;
310 array3 alpha = dbXT->getAlphaBin(i);
317 array3 theta = dbXT->getThetaBin(i);
325 B2FATAL(
"Function type before calibration is wrong " <<
m_xtModePrior);
330 B2INFO(
"Function type " <<
m_xtMode);
332 for (
unsigned short iCL = 0; iCL < 56; ++iCL) {
333 for (
unsigned short iLR = 0; iLR < 2; ++iLR) {
336 const std::vector<float> params = dbXT->getXtParams(iCL, iLR, iA, iT);
337 unsigned short np = params.size();
338 for (
unsigned short i = 0; i < np; ++i) {
339 m_xtPrior[iCL][iLR][iA][iT][i] = params[i];
349 B2INFO(
"write calibrated XT");
361 const float deg2rad =
static_cast<float>(
Unit::deg);
364 std::array<float, 3> alpha3 = {
m_lowerAlpha[i]* deg2rad,
372 std::array<float, 3> theta3 = {
m_lowerTheta[i]* deg2rad,
383 for (
int l = 0; l < 56; ++l) {
384 for (
int lr = 0; lr < 2; ++lr) {
385 if (
m_fitStatus[l][lr][al][th] != FitStatus::c_OK) {
387 B2DEBUG(21,
"fit failure status = " <<
m_fitStatus[l][lr][al][th]);
388 B2DEBUG(21,
"layer " << l <<
", r " << lr <<
", alpha " <<
m_iAlpha[al] <<
", theta " <<
m_iTheta[th]);
389 B2DEBUG(21,
"number of event: " <<
m_hProf[l][lr][al][th]->GetEntries());
390 if (
m_fitStatus[l][lr][al][th] != FitStatus::c_lowStat) {
392 B2DEBUG(21,
"Probability of fit: " <<
m_xtFunc[l][lr][al][th]->GetProb());
398 for (
int i = 0; i < 8; ++i) {
402 B2FATAL(
"XT mode before/after calibration is different!");
407 for (
int i = 0; i < 8; ++i) {
411 m_xtFunc[l][lr][al][th]->GetParameters(par);
415 std::vector<float> xtbuff;
416 for (
int i = 0; i < 8; ++i) {
417 xtbuff.push_back(par[i]);
432 B2RESULT(
"Successfully Fitted: " << nfitted);
433 B2RESULT(
"Failure Fit: " << nfailure);
440 auto hNDF = getObjectPtr<TH1F>(
"hNDF");
441 auto hPval = getObjectPtr<TH1F>(
"hPval");
442 auto hEvtT0 = getObjectPtr<TH1F>(
"hEventT0");
443 B2INFO(
"saving histograms");
444 TFile* fout =
new TFile(
m_histName.c_str(),
"RECREATE");
445 TDirectory* top = gDirectory;
447 if (hNDF && hPval && hEvtT0) {
454 TDirectory* Direct[56];
456 for (
int l = 0; l < 56; ++l) {
458 Direct[l] = gDirectory->mkdir(Form(
"lay_%d", l));
463 for (
int lr = 0; lr < 2; ++lr) {
472 m_hProf[l][lr][al][th]->Write();
482 B2RESULT(
" " << nhisto <<
" histograms was stored.");