1 #include <cdc/calibration/XT.h>
2 #include <cdc/calibration/XTCalibration.h>
4 #include <cdc/dbobjects/CDCXtRelations.h>
17 #include <framework/database/DBObjPtr.h>
18 #include <framework/logging/Logger.h>
19 #include <framework/utilities/FileSystem.h>
20 #include <cdc/calibration/CDCDatabaseImporter.h>
22 #include <boost/iostreams/filtering_stream.hpp>
23 #include <boost/iostreams/device/file.hpp>
24 #include <boost/iostreams/filter/gzip.hpp>
29 typedef std::array<float, 3> array3;
30 XTCalibration::XTCalibration():
31 m_firstExperiment(0), m_firstRun(0),
32 m_lastExperiment(-1), m_lastRun(-1)
51 TChain* tree =
new TChain(
"tree");
54 if (!tree->GetBranch(
"ndf")) {
55 cout <<
"input data do not exits, please check!" << endl;
56 B2FATAL(
"echo rootfile do not exits or something wrong");
57 gSystem->Exec(
"echo rootfile do not exits or something wrong >> error");
63 double Pval, alpha, theta;
66 tree->SetBranchAddress(
"lay", &lay);
67 tree->SetBranchAddress(
"t", &dt);
68 tree->SetBranchAddress(
"x_u", &dx);
69 tree->SetBranchAddress(
"alpha", &alpha);
70 tree->SetBranchAddress(
"theta", &theta);
71 tree->SetBranchAddress(
"Pval", &Pval);
72 tree->SetBranchAddress(
"ndf", &ndf);
75 std::vector<TString> list_vars = {
"lay",
"t",
"x_u",
"alpha",
"theta",
"Pval",
"ndf"};
76 tree->SetBranchStatus(
"*", 0);
78 for (TString brname : list_vars) {
79 tree->SetBranchStatus(brname, 1);
84 for (
int i = 0; i < 56; ++i) {
85 for (
int lr = 0; lr < 2; ++lr) {
86 for (
int al = 0; al <
m_nalpha; ++al) {
87 for (
int th = 0; th <
m_ntheta; ++th) {
88 hprof[i][lr][al][th] =
new TProfile(Form(
"hprof%d_%d_%d_%d", i, lr, al, th),
89 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
90 i, lr,
ialpha[al],
itheta[th]), 210, -20, 600, 0, 1.2,
"i");
91 hist2d[i][lr][al][th] =
new TH2D(Form(
"h%d_%d_%d_%d", i, lr, al, th),
92 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
93 i, lr,
ialpha[al],
itheta[th]), 210, -20, 600, 110, 0, 1.2);
95 hist2d_draw[i][al][th] =
new TH2D(Form(
"h_draw%d_%d_%d", i, al, th),
96 Form(
"(L=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
97 i,
ialpha[al],
itheta[th]), 210, -20, 600, 2200, -1.2, 1.2);
108 int nEntries = tree->GetEntries();
109 cout <<
"Number of Hit: " << nEntries << endl;
111 for (
int i = 0; i < nEntries; ++i) {
112 nbytes += tree->GetEntry(i);
114 if (fabs(alpha > 90)) {
115 if (alpha < 0) alpha += 180;
116 if (alpha > 0) alpha -= 180;
122 for (
int k = 0; k <
m_nalpha; ++k) {
123 if (alpha <
u_alpha[k]) {al = k;
break;}
125 for (
int j = 0; j <
m_ntheta; ++j) {
126 if (theta <
u_theta[j]) {th = j;
break;}
133 hprof[lay][lr][al][th]->Fill(dt, abs(dx));
134 hist2d[lay][lr][al][th]->Fill(dt, abs(dx));
136 hprof[lay][0][al][th]->Fill(dt, abs(dx));
137 hist2d[lay][0][al][th]->Fill(dt, abs(dx));
138 hprof[lay][1][al][th]->Fill(dt, abs(dx));
139 hist2d[lay][1][al][th]->Fill(dt, abs(dx));
148 B2INFO(
"use XT bining from input XT");
152 for (
int i = 0; i <
m_nalpha; ++i) {
158 for (
int i = 0; i <
m_ntheta; ++i) {
165 B2INFO(
"use XT bining from profile file");
170 double dumy1, dumy2, dumy3;
172 B2DEBUG(99,
"Number of alpha bin" <<
m_nalpha);
174 B2FATAL(
"number of alpha bin excess limit; please increse uplimit: " <<
m_nalpha <<
" > " <<
m_MAXalpha);
176 for (
int i = 0; i <
m_nalpha; ++i) {
177 proxt >> dumy1 >> dumy2 >> dumy3;
183 B2DEBUG(99,
"Number of theta bin" <<
m_nalpha);
185 for (
int i = 0; i <
m_ntheta; ++i) {
186 proxt >> dumy1 >> dumy2 >> dumy3;
192 B2INFO(
"Finish asssign XT bining");
198 gErrorIgnoreLevel = 3001;
201 B2INFO(
"Start Fitting");
202 for (
int l = 0; l < 56; ++l) {
203 for (
int lr = 0; lr < 2; ++lr) {
204 for (
int al = 0; al <
m_nalpha; ++al) {
205 for (
int th = 0; th <
m_ntheta; ++th) {
214 hist2d[l][lr][al][th]->FitSlicesY(0, 0, -1, 5);
215 hist2d_1[l][lr][al][th] = (TH1D*)gDirectory->Get(Form(
"h%d_%d_%d_%d_1", l, lr, al, th));
218 B2WARNING(
"error, not found results of slices fit");
221 hist2d_1[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
222 fpol1 = (TF1*)
hprof[l][lr][al][th]->GetFunction(
"pol1");
225 for (
int n = 0; n <
hprof[l][lr][al][th]->GetNbinsX(); ++n) {
226 if (
hprof[l][lr][al][th]->GetBinEntries(n) < 5 &&
hprof[l][lr][al][th]->GetBinEntries(n) > 1) {
227 hprof[l][lr][al][th]->SetBinError(n, 0.3 /
hprof[l][lr][al][th]->GetBinEntries(n));
230 hprof[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
231 fpol1 = (TF1*)
hprof[l][lr][al][th]->GetFunction(
"pol1");
235 p0 = fpol1->GetParameter(0);
236 p1 = fpol1->GetParameter(1);
237 tmin = -1 * p0 / p1 + 15;
258 double p6 =
xtold[l][lr][ial_old][ith_old][6];
265 xt->
setXTParams(p0, p1, 0., 0., 0., 0., p6,
xtold[l][lr][ial_old][ith_old][7]);
298 for (
int i = 0; i <
m_nalpha; ++i) {
299 xtout << std::setprecision(3) <<
l_alpha[i] <<
" "
300 << std::setprecision(3) <<
u_alpha[i] <<
" "
301 << std::setprecision(3) <<
ialpha[i] << endl;
304 for (
int i = 0; i <
m_ntheta; ++i) {
305 xtout << std::setprecision(3) <<
l_theta[i] <<
" "
306 << std::setprecision(3) <<
u_theta[i] <<
" "
307 << std::setprecision(3) <<
itheta[i] << endl;
309 xtout <<
m_xtmode <<
" " << 8 << endl;
314 for (
int th = 0; th <
m_ntheta; ++th) {
315 for (
int al = 0; al <
m_nalpha; ++al) {
316 for (
int l = 0; l < 56; ++l) {
317 for (
int lr = 0; lr < 2; ++lr) {
319 if (
fitflag[l][lr][al][th] != 1) {
321 printf(
"fit failure status = %d \n",
fitflag[l][lr][al][th]);
322 printf(
"layer %d, r %d, alpha %3.1f, theta %3.1f \n", l, lr,
ialpha[al],
itheta[th]);
323 printf(
"number of event: %3.2f",
hprof[l][lr][al][th]->GetEntries());
324 if (
fitflag[l][lr][al][th] != -1) {
325 printf(
"Probability of fit: %3.4f",
xtf5r[l][lr][al][th]->GetProb());
336 for (
int p = 0; p < 8; ++p) {
337 par[p] =
xtold[l][lr][ial_old][ith_old][p];
341 par[0] = 0; par[1] = 0.004; par[2] = 0; par[3] = 0; par[4] = 0; par[5] = 0; par[6] =
m_par6[l]; par[7] = 0.00001;
344 xtf5r[l][lr][al][th]->GetParameters(par);
348 xtout << l << std::setw(5) <<
itheta[th] << std::setw(5) <<
ialpha[al] << std::setw(5) <<
"0.0" << std::setw(4) << lr << std::setw(
350 for (
int p = 0; p < 8; ++p) {
351 if (p != 7) { xtout << std::setprecision(7) << par[p] << std::setw(15);}
352 if (p == 7) { xtout << std::setprecision(7) << par[p] << std::endl;}
360 B2RESULT(
" Successfully Fitted: " << nfitted);
361 B2RESULT(
" Failure Fit: " << nfailure);
362 B2RESULT(
"Finish export xt to text file");
371 B2INFO(
"start store histogram");
372 TFile* fout =
new TFile(
"XTFIT.root",
"RECREATE");
373 TDirectory* top = gDirectory;
374 TDirectory* Direct[56];
376 for (
int l = 0; l < 56; ++l) {
378 Direct[l] = gDirectory->mkdir(Form(
"lay_%d", l));
380 for (
int th = 0; th <
m_ntheta; ++th) {
381 for (
int al = 0; al <
m_nalpha; ++al) {
383 for (
int lr = 0; lr < 2; ++lr) {
384 hist2d[l][lr][al][th]->Write();
385 if (
fitflag[l][lr][al][th] != 1)
continue;
392 hprof[l][lr][al][th]->Write();
400 B2RESULT(
" " << nhisto <<
" histograms was stored.");
406 B2INFO(
"reading xt from DB");
414 B2INFO(
"Number of theta bin from xt: " <<
ntheta_old);
419 B2INFO(
"Read Xt from text");
434 std::string fileName1 =
"/data/cdc" +
m_xtfile;
436 boost::iostreams::filtering_istream ifs;
437 if (fileName ==
"") {
440 if (fileName ==
"") {
441 B2FATAL(
"CDCGeometryPar: " << fileName1 <<
" not exist!");
443 B2INFO(
"CDCGeometryPar: open " << fileName1);
444 if ((fileName.rfind(
".gz") != string::npos) && (fileName.length() - fileName.rfind(
".gz") == 3)) {
445 ifs.push(boost::iostreams::gzip_decompressor());
447 ifs.push(boost::iostreams::file_source(fileName));
448 if (!ifs) B2FATAL(
"CDCGeometryPar: cannot open " << fileName1 <<
" !");
457 for (
unsigned short i = 0; i <
nalpha_old; ++i) {
467 for (
unsigned short i = 0; i <
ntheta_old; ++i) {
475 unsigned short iCL, iLR;
478 double theta, alpha, dummy1;
483 const double epsi = 0.1;
486 ifs >> theta >> alpha >> dummy1 >> iLR;
487 for (
int i = 0; i < np; ++i) {
493 for (
unsigned short i = 0; i <
ntheta_old; ++i) {
500 gSystem->Exec(
"echo xt_theta error binning>> error");
505 for (
unsigned short i = 0; i <
nalpha_old; ++i) {
512 gSystem->Exec(
"echo xt_alpha error binning>> error");
516 for (
int i = 0; i < np; ++i) {
517 xtold[iCL][iLR][ial][ith][i] = xtc[i];
539 double rad2deg = 180 / M_PI;
540 for (
unsigned short i = 0; i <
nalpha_old; ++i) {
541 array3 alpha = dbXT_old->getAlphaBin(i);
550 for (
unsigned short i = 0; i <
ntheta_old; ++i) {
552 array3 theta = dbXT_old->getThetaBin(i);
563 for (
unsigned short iCL = 0; iCL < 56; ++iCL) {
564 for (
unsigned short iLR = 0; iLR < 2; ++iLR) {
565 for (
unsigned short iA = 0; iA <
nalpha_old; ++iA) {
566 for (
unsigned short iT = 0; iT <
ntheta_old; ++iT) {
567 const std::vector<float> params = dbXT_old->getXtParams(iCL, iLR, iA, iT);
568 unsigned short np = params.size();
570 for (
unsigned short i = 0; i < np; ++i) {
571 xtold[iCL][iLR][iA][iT][i] = params[i];