8 #include <cdc/calibration/XT.h>
9 #include <cdc/calibration/XTCalibration.h>
11 #include <cdc/dbobjects/CDCXtRelations.h>
24 #include <framework/database/DBObjPtr.h>
25 #include <framework/logging/Logger.h>
26 #include <framework/utilities/FileSystem.h>
27 #include <cdc/calibration/CDCDatabaseImporter.h>
29 #include <boost/iostreams/filtering_stream.hpp>
30 #include <boost/iostreams/device/file.hpp>
31 #include <boost/iostreams/filter/gzip.hpp>
36 typedef std::array<float, 3> array3;
37 XTCalibration::XTCalibration()
52 void XTCalibration::CreateHisto()
57 TChain* tree =
new TChain(
"tree");
58 tree->Add(m_inputRootFileNames.c_str());
59 B2INFO(
"Open Files: " << m_inputRootFileNames);
60 if (!tree->GetBranch(
"ndf")) {
61 cout <<
"input data do not exits, please check!" << endl;
62 B2FATAL(
"echo rootfile do not exits or something wrong");
63 gSystem->Exec(
"echo rootfile do not exits or something wrong >> error");
69 double Pval, alpha, theta;
72 tree->SetBranchAddress(
"lay", &lay);
73 tree->SetBranchAddress(
"t", &dt);
74 tree->SetBranchAddress(
"x_u", &dx);
75 tree->SetBranchAddress(
"alpha", &alpha);
76 tree->SetBranchAddress(
"theta", &theta);
77 tree->SetBranchAddress(
"Pval", &Pval);
78 tree->SetBranchAddress(
"ndf", &ndf);
81 std::vector<TString> list_vars = {
"lay",
"t",
"x_u",
"alpha",
"theta",
"Pval",
"ndf"};
82 tree->SetBranchStatus(
"*", 0);
84 for (TString brname : list_vars) {
85 tree->SetBranchStatus(brname, 1);
90 for (
int i = 0; i < 56; ++i) {
91 for (
int lr = 0; lr < 2; ++lr) {
92 for (
int al = 0; al < m_nalpha; ++al) {
93 for (
int th = 0; th < m_ntheta; ++th) {
94 hprof[i][lr][al][th] =
new TProfile(Form(
"hprof%d_%d_%d_%d", i, lr, al, th),
95 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
96 i, lr, ialpha[al], itheta[th]), 210, -20, 600, 0, 1.2,
"i");
97 hist2d[i][lr][al][th] =
new TH2D(Form(
"h%d_%d_%d_%d", i, lr, al, th),
98 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
99 i, lr, ialpha[al], itheta[th]), 210, -20, 600, 110, 0, 1.2);
101 hist2d_draw[i][al][th] =
new TH2D(Form(
"h_draw%d_%d_%d", i, al, th),
102 Form(
"(L=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
103 i, ialpha[al], itheta[th]), 210, -20, 600, 2200, -1.2, 1.2);
113 int nEntries = tree->GetEntries();
114 cout <<
"Number of Hit: " << nEntries << endl;
116 for (
int i = 0; i < nEntries; ++i) {
119 if (fabs(alpha) > 90) {
120 if (alpha < 0) alpha += 180;
121 if (alpha > 0) alpha -= 180;
124 if (Pval < m_Pvalmin)
continue;
125 if (ndf < m_ndfmin)
continue;
127 for (
int k = 0; k < m_nalpha; ++k) {
128 if (alpha < u_alpha[k]) {al = k;
break;}
130 for (
int j = 0; j < m_ntheta; ++j) {
131 if (theta < u_theta[j]) {th = j;
break;}
138 hprof[lay][lr][al][th]->Fill(dt, abs(dx));
139 hist2d[lay][lr][al][th]->Fill(dt, abs(dx));
141 hprof[lay][0][al][th]->Fill(dt, abs(dx));
142 hist2d[lay][0][al][th]->Fill(dt, abs(dx));
143 hprof[lay][1][al][th]->Fill(dt, abs(dx));
144 hist2d[lay][1][al][th]->Fill(dt, abs(dx));
146 hist2d_draw[lay][al][th]->Fill(dt, dx);
149 void XTCalibration::readProfile()
152 if (m_useProfileXTFromInputXT) {
153 B2INFO(
"use XT bining from input XT");
154 m_nalpha = nalpha_old;
155 m_ntheta = ntheta_old;
156 B2INFO(
"Nalpha: " << m_nalpha <<
"\n Ntheta: " << m_ntheta);
157 for (
int i = 0; i < m_nalpha; ++i) {
158 l_alpha[i] = l_alpha_old[i];
159 u_alpha[i] = u_alpha_old[i];
160 ialpha[i] = ialpha_old[i];
161 B2INFO(
"-" << l_alpha[i] <<
" " << u_alpha[i] <<
" " << ialpha[i]);
163 for (
int i = 0; i < m_ntheta; ++i) {
164 l_theta[i] = l_theta_old[i];
165 u_theta[i] = u_theta_old[i];
166 itheta[i] = itheta_old[i];
167 B2INFO(
"-" << l_theta[i] <<
" " << u_theta[i] <<
" " << itheta[i]);
170 B2INFO(
"use XT bining from profile file");
171 ifstream proxt(m_profileFileName.c_str());
173 B2FATAL(
"file not found: " << m_profileFileName);
175 double dumy1, dumy2, dumy3;
177 B2DEBUG(99,
"Number of alpha bin" << m_nalpha);
178 if (m_nalpha > m_MAXalpha) {
179 B2FATAL(
"number of alpha bin excess limit; please increse uplimit: " << m_nalpha <<
" > " << m_MAXalpha);
181 for (
int i = 0; i < m_nalpha; ++i) {
182 proxt >> dumy1 >> dumy2 >> dumy3;
188 B2DEBUG(99,
"Number of theta bin" << m_nalpha);
189 if (m_ntheta > m_MAXtheta) {B2FATAL(
"number of theta bin excess limit; please increse uplimit: " << m_ntheta <<
" > " << m_MAXtheta);}
190 for (
int i = 0; i < m_ntheta; ++i) {
191 proxt >> dumy1 >> dumy2 >> dumy3;
197 B2INFO(
"Finish asssign XT bining");
200 bool XTCalibration::calibrate()
203 gErrorIgnoreLevel = 3001;
206 B2INFO(
"Start Fitting");
207 for (
int l = 0; l < 56; ++l) {
208 for (
int lr = 0; lr < 2; ++lr) {
209 for (
int al = 0; al < m_nalpha; ++al) {
210 for (
int th = 0; th < m_ntheta; ++th) {
212 if (hist2d[l][lr][al][th]->GetEntries() < m_smallestEntryRequire) {
213 fitflag[l][lr][al][th] = -1;
219 hist2d[l][lr][al][th]->FitSlicesY(0, 0, -1, 5);
220 hist2d_1[l][lr][al][th] = (TH1D*)gDirectory->Get(Form(
"h%d_%d_%d_%d_1", l, lr, al, th));
221 if (!hist2d_1[l][lr][al][th]) {
222 fitflag[l][lr][al][th] = -1;
223 B2WARNING(
"error, not found results of slices fit");
226 hist2d_1[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
227 fpol1 = (TF1*)hprof[l][lr][al][th]->GetFunction(
"pol1");
230 for (
int n = 0; n < hprof[l][lr][al][th]->GetNbinsX(); ++n) {
231 if (hprof[l][lr][al][th]->GetBinEntries(n) < 5 && hprof[l][lr][al][th]->GetBinEntries(n) > 1) {
232 hprof[l][lr][al][th]->SetBinError(n, 0.3 / hprof[l][lr][al][th]->GetBinEntries(n));
235 hprof[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
236 fpol1 = (TF1*)hprof[l][lr][al][th]->GetFunction(
"pol1");
240 p0 = fpol1->GetParameter(0);
241 p1 = fpol1->GetParameter(1);
242 tmin = -1 * p0 / p1 + 15;
249 xt =
new XT(hist2d_1[l][lr][al][th], m_xtmode);
251 xt =
new XT(hprof[l][lr][al][th], m_xtmode);
257 for (
int k = 0; k < nalpha_old; ++k) {
258 if (ialpha[al] < u_alpha_old[k]) {ial_old = k;
break;}
260 for (
int j = 0; j < ntheta_old; ++j) {
261 if (itheta[th] < u_theta_old[j]) {ith_old = j;
break;}
263 double p6 = xtold[l][lr][ial_old][ith_old][6];
266 if (m_xtmode == xtmode_old) {
270 xt->
setXTParams(p0, p1, 0., 0., 0., 0., p6, xtold[l][lr][ial_old][ith_old][7]);
274 xt->
setXTParams(p0, p1, 0., 0., 0., 0., m_par6[l], 0.0001);
297 void XTCalibration::Write()
302 ofstream xtout(m_OutputXTFileName.c_str());
303 xtout << m_nalpha << endl;
304 for (
int i = 0; i < m_nalpha; ++i) {
305 xtout << std::setprecision(3) << l_alpha[i] <<
" "
306 << std::setprecision(3) << u_alpha[i] <<
" "
307 << std::setprecision(3) << ialpha[i] << endl;
309 xtout << m_ntheta << endl;
310 for (
int i = 0; i < m_ntheta; ++i) {
311 xtout << std::setprecision(3) << l_theta[i] <<
" "
312 << std::setprecision(3) << u_theta[i] <<
" "
313 << std::setprecision(3) << itheta[i] << endl;
315 xtout << m_xtmode <<
" " << 8 << endl;
320 for (
int th = 0; th < m_ntheta; ++th) {
321 for (
int al = 0; al < m_nalpha; ++al) {
322 for (
int l = 0; l < 56; ++l) {
323 for (
int lr = 0; lr < 2; ++lr) {
325 if (fitflag[l][lr][al][th] != 1) {
327 printf(
"fit failure status = %d \n", fitflag[l][lr][al][th]);
328 printf(
"layer %d, r %d, alpha %3.1f, theta %3.1f \n", l, lr, ialpha[al], itheta[th]);
329 printf(
"number of event: %3.2f", hprof[l][lr][al][th]->GetEntries());
330 if (fitflag[l][lr][al][th] != -1) {
331 printf(
"Probability of fit: %3.4f", xtf5r[l][lr][al][th]->GetProb());
333 if (m_xtmode == xtmode_old) {
336 for (
int k = 0; k < nalpha_old; ++k) {
337 if (ialpha[al] < u_alpha_old[k]) {ial_old = k;
break;}
339 for (
int j = 0; j < ntheta_old; ++j) {
340 if (itheta[th] < u_theta_old[j]) {ith_old = j;
break;}
342 for (
int p = 0; p < 8; ++p) {
343 par[p] = xtold[l][lr][ial_old][ith_old][p];
347 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;
350 xtf5r[l][lr][al][th]->GetParameters(par);
354 xtout << l << std::setw(5) << itheta[th] << std::setw(5) << ialpha[al] << std::setw(5) <<
"0.0" << std::setw(4) << lr << std::setw(
356 for (
int p = 0; p < 8; ++p) {
357 if (p != 7) { xtout << std::setprecision(7) << par[p] << std::setw(15);}
358 if (p == 7) { xtout << std::setprecision(7) << par[p] << std::endl;}
365 B2RESULT(
" Total number of xt fit: " << m_nalpha * m_ntheta * 2 * 56);
366 B2RESULT(
" Successfully Fitted: " << nfitted);
367 B2RESULT(
" Failure Fit: " << nfailure);
368 B2RESULT(
"Finish export xt to text file");
371 import.
importXT(m_OutputXTFileName.c_str());
375 void XTCalibration::storeHisto()
377 B2INFO(
"start store histogram");
378 TFile* fout =
new TFile(
"XTFIT.root",
"RECREATE");
379 TDirectory* top = gDirectory;
380 TDirectory* Direct[56];
382 for (
int l = 0; l < 56; ++l) {
384 Direct[l] = gDirectory->mkdir(Form(
"lay_%d", l));
386 for (
int th = 0; th < m_ntheta; ++th) {
387 for (
int al = 0; al < m_nalpha; ++al) {
388 hist2d_draw[l][al][th]->Write();
389 for (
int lr = 0; lr < 2; ++lr) {
390 hist2d[l][lr][al][th]->Write();
391 if (fitflag[l][lr][al][th] != 1)
continue;
393 if (hist2d_1[l][lr][al][th]) {
394 hist2d_1[l][lr][al][th]->Write();
398 hprof[l][lr][al][th]->Write();
406 B2RESULT(
" " << nhisto <<
" histograms was stored.");
409 void XTCalibration::readXT()
412 B2INFO(
"reading xt from DB");
420 B2INFO(
"Number of theta bin from xt: " << ntheta_old);
421 B2INFO(
"Theta 0: " << itheta_old[0]);
425 B2INFO(
"Read Xt from text");
432 B2INFO(
"nalpha: " << nalpha_old);
438 void XTCalibration::readXTFromText()
440 std::string fileName1 =
"/data/cdc" + m_xtfile;
441 std::string fileName = FileSystem::findFile(fileName1);
442 boost::iostreams::filtering_istream ifs;
443 if (fileName ==
"") {
444 fileName = FileSystem::findFile(m_xtfile);
446 if (fileName ==
"") {
447 B2FATAL(
"CDCGeometryPar: " << fileName1 <<
" not exist!");
449 B2INFO(
"CDCGeometryPar: open " << fileName1);
450 if ((fileName.rfind(
".gz") != string::npos) && (fileName.length() - fileName.rfind(
".gz") == 3)) {
451 ifs.push(boost::iostreams::gzip_decompressor());
453 ifs.push(boost::iostreams::file_source(fileName));
454 if (!ifs) B2FATAL(
"CDCGeometryPar: cannot open " << fileName1 <<
" !");
463 for (
unsigned short i = 0; i < nalpha_old; ++i) {
464 ifs >> l_alpha_old[i] >> u_alpha_old[i] >> ialpha_old[i];
473 for (
unsigned short i = 0; i < ntheta_old; ++i) {
474 ifs >> l_theta_old[i] >> u_theta_old[i] >> itheta_old[i];
479 B2INFO(
"number of alpha - theta bin" << nalpha_old <<
" - " << ntheta_old);
481 unsigned short iCL, iLR;
484 double theta, alpha, dummy1;
486 ifs >> xtmode_old >> np;
488 const double epsi = 0.1;
491 ifs >> theta >> alpha >> dummy1 >> iLR;
492 for (
int i = 0; i < np; ++i) {
497 for (
unsigned short i = 0; i < ntheta_old; ++i) {
498 if (fabs(theta - itheta_old[i]) < epsi) {
504 gSystem->Exec(
"echo xt_theta error binning>> error");
509 for (
unsigned short i = 0; i < nalpha_old; ++i) {
510 if (fabs(alpha - ialpha_old[i]) < epsi) {
516 gSystem->Exec(
"echo xt_alpha error binning>> error");
520 for (
int i = 0; i < np; ++i) {
521 xtold[iCL][iLR][ial][ith][i] = xtc[i];
538 void XTCalibration::readXTFromDB()
541 nalpha_old = dbXT_old->getNoOfAlphaBins();
542 B2INFO(
"Number of alpha" << nalpha_old);
543 double rad2deg = 180 / M_PI;
544 for (
unsigned short i = 0; i < nalpha_old; ++i) {
545 array3 alpha = dbXT_old->getAlphaBin(i);
546 l_alpha_old[i] = alpha[0] * rad2deg;
547 u_alpha_old[i] = alpha[1] * rad2deg;
548 ialpha_old[i] = alpha[2] * rad2deg;
552 ntheta_old = dbXT_old->getNoOfThetaBins();
553 B2INFO(
"Ntheta: " << ntheta_old);
554 for (
unsigned short i = 0; i < ntheta_old; ++i) {
556 array3 theta = dbXT_old->getThetaBin(i);
557 l_theta_old[i] = theta[0] * rad2deg;
558 u_theta_old[i] = theta[1] * rad2deg;
559 itheta_old[i] = theta[2] * rad2deg;
565 xtmode_old = dbXT_old->getXtParamMode();
567 for (
unsigned short iCL = 0; iCL < 56; ++iCL) {
568 for (
unsigned short iLR = 0; iLR < 2; ++iLR) {
569 for (
unsigned short iA = 0; iA < nalpha_old; ++iA) {
570 for (
unsigned short iT = 0; iT < ntheta_old; ++iT) {
571 const std::vector<float> params = dbXT_old->getXtParams(iCL, iLR, iA, iT);
572 unsigned short np = params.size();
574 for (
unsigned short i = 0; i < np; ++i) {
575 xtold[iCL][iLR][iA][iT][i] = params[i];
void importXT(std::string fileName)
Import xt table to the database.
Class for accessing objects in the database.
Class to perform fitting for each xt function.
void setXTParams(const double p[8])
Set Parameters for fit.
void setP6(double p6)
Set Parameter 6 for polynomial fit.
void setFitRange(double tmin, double tmax)
Set Fit range.
TF1 * getXTFunction(int mode)
Get XT function.
void setDebug(bool debug)
Set Debug.
TProfile * getFittedHisto()
Get histogram.
int getFitStatus()
get fitted flag.
void BField(bool bfield)
set to use BField
Abstract base class for different kinds of events.