8#include <cdc/calibration/XTCalibrationAlgorithm.h>
9#include <cdc/calibration/XTFunction.h>
10#include <cdc/geometry/CDCGeometryPar.h>
11#include <cdc/dbobjects/CDCXtRelations.h>
14#include <TStopwatch.h>
24#include <framework/database/DBObjPtr.h>
30typedef std::array<float, 3> array3;
34 " -------------------------- XT Calibration Algorithm -------------------------\n"
41 B2INFO(
"create and fill histo");
43 for (
int i = 0; i < 56; ++i) {
44 for (
int lr = 0; lr < 2; ++lr) {
47 m_hProf[i][lr][al][th] =
new TProfile(Form(
"m_hProf%d_%d_%d_%d", i, lr, al, th),
48 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
50 m_hist2d[i][lr][al][th] =
new TH2F(Form(
"h%d_%d_%d_%d", i, lr, al, th),
51 Form(
"(L=%d)-(lr=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
54 m_hist2dDraw[i][al][th] =
new TH2F(Form(
"h_draw%d_%d_%d", i, al, th),
55 Form(
"(L=%d)-(#alpha=%3.0f)-(#theta=%3.0f); Drift time (ns);Drift Length (cm)",
64 auto tree = getObjectPtr<TTree>(
"tree");
69 Float_t 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);
93 const Long64_t nEntries = tree->GetEntries();
94 B2INFO(
"Number of entries " << nEntries);
95 for (Long64_t i = 0; i < nEntries; ++i) {
112 int lr = dx > 0 ? c_Right : c_Left;
114 m_hProf[lay][lr][al][th]->Fill(dt, abs(dx));
115 m_hist2d[lay][lr][al][th]->Fill(dt, abs(dx));
117 m_hProf[lay][0][al][th]->Fill(dt, abs(dx));
118 m_hist2d[lay][0][al][th]->Fill(dt, abs(dx));
119 m_hProf[lay][1][al][th]->Fill(dt, abs(dx));
120 m_hist2d[lay][1][al][th]->Fill(dt, abs(dx));
125 B2INFO(
"Time to fill histograms: " << time.RealTime() <<
"s");
132 gPrintViaErrorHandler =
true;
133 gErrorIgnoreLevel = 3001;
134 B2INFO(
"Start calibration");
138 B2INFO(
"ExpRun used for DB Geometry : " << exprun.first <<
" " << exprun.second);
140 B2INFO(
"Creating CDCGeometryPar object");
146 B2INFO(
"Start Fitting");
147 std::unique_ptr<XTFunction> xt;
148 for (
int l = 0; l < 56; ++l) {
149 for (
int lr = 0; lr < 2; ++lr) {
159 m_hist2d[l][lr][al][th]->FitSlicesY(0, 0, -1, 5);
160 m_hist2d_1[l][lr][al][th] = (TH1F*)gDirectory->Get(Form(
"h%d_%d_%d_%d_1", l, lr, al, th));
163 B2WARNING(
"Error, not found results of slices fit");
166 m_hist2d_1[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
167 fpol1 = (TF1*)
m_hProf[l][lr][al][th]->GetFunction(
"pol1");
170 for (
int n = 0; n <
m_hProf[l][lr][al][th]->GetNbinsX(); ++n) {
171 if (
m_hProf[l][lr][al][th]->GetBinEntries(n) < 5 &&
m_hProf[l][lr][al][th]->GetBinEntries(n) > 1) {
172 m_hProf[l][lr][al][th]->SetBinError(n, 0.3 /
m_hProf[l][lr][al][th]->GetBinEntries(n));
175 m_hProf[l][lr][al][th]->Fit(
"pol1",
"Q",
"", 30, 60);
176 fpol1 = (TF1*)
m_hProf[l][lr][al][th]->GetFunction(
"pol1");
181 p0 = fpol1->GetParameter(0);
182 p1 = fpol1->GetParameter(1);
183 tmin = -1 * p0 / p1 + 15;
213 double p6 =
m_xtPrior[l][lr][ial_old][ith_old][6];
219 xt->setXTParams(
m_xtPrior[l][lr][ial_old][ith_old]);
222 xt->setXTParams(p0, p1, 0., 0., 0., 0., p6,
m_xtPrior[l][lr][ial_old][ith_old][7]);
224 xt->setFitRange(tmin, p6 + 100);
226 xt->setXTParams(p0, p1, 0., 0., 0., 0.,
m_par6[l], 0.0001);
227 xt->setFitRange(tmin,
m_par6[l] + 100);
232 if (xt->isValid() ==
false) {
233 B2WARNING(
"Empty xt");
237 if (xt->getFitStatus() != 1) {
238 B2WARNING(
"Fit failed");
242 if (xt->validate() ==
true) {
244 m_xtFunc[l][lr][al][th] = (TF1*)xt->getXTFunction();
247 m_hist2d_1[l][lr][al][th] = (TH1F*)xt->getFittedHisto();
249 m_hProf[l][lr][al][th] = (TProfile*)xt->getFittedHisto();
266 const double tMax = 500;
267 for (
int l = 0; l < 56; ++l) {
268 for (
int lr = 0; lr < 2; ++lr) {
271 if (
m_fitStatus[l][lr][al][th] == FitStatus::c_OK) {
273 double y = fun->Eval(tMax);
275 B2INFO(
"Strange XT function l " << l <<
" lr " << lr <<
" alpha " << al <<
" theta " << th
276 <<
", replaced by initial one");
277 fun->SetParameters(
m_xtPrior[l][lr][al][th]);
289 int nFitCompleted = 0;
290 for (
int l = 0; l < 56; ++l) {
291 for (
int lr = 0; lr < 2; ++lr) {
294 if (
m_fitStatus[l][lr][al][th] == FitStatus::c_OK) {
302 if (
static_cast<double>(nFitCompleted) / nTotal <
m_threshold) {
303 B2WARNING(
"Less than " <<
m_threshold * 100 <<
" % of XTs were fitted.");
311 B2INFO(
"Prepare calibration of XT");
312 const double rad2deg = 180 / M_PI;
318 array3 alpha = dbXT->getAlphaBin(i);
325 array3 theta = dbXT->getThetaBin(i);
333 B2FATAL(
"Function type before calibration is wrong " <<
m_xtModePrior);
338 B2INFO(
"Function type " <<
m_xtMode);
340 for (
unsigned short iCL = 0; iCL < 56; ++iCL) {
341 for (
unsigned short iLR = 0; iLR < 2; ++iLR) {
344 const std::vector<float> params = dbXT->getXtParams(iCL, iLR, iA, iT);
345 unsigned short np = params.size();
346 for (
unsigned short i = 0; i < np; ++i) {
347 m_xtPrior[iCL][iLR][iA][iT][i] = params[i];
357 B2INFO(
"write calibrated XT");
369 const float deg2rad =
static_cast<float>(
Unit::deg);
372 std::array<float, 3> alpha3 = {
m_lowerAlpha[i]* deg2rad,
380 std::array<float, 3> theta3 = {
m_lowerTheta[i]* deg2rad,
391 for (
int l = 0; l < 56; ++l) {
392 for (
int lr = 0; lr < 2; ++lr) {
393 if (
m_fitStatus[l][lr][al][th] != FitStatus::c_OK) {
395 B2DEBUG(21,
"fit failure status = " <<
m_fitStatus[l][lr][al][th]);
396 B2DEBUG(21,
"layer " << l <<
", r " << lr <<
", alpha " <<
m_iAlpha[al] <<
", theta " <<
m_iTheta[th]);
397 B2DEBUG(21,
"number of event: " <<
m_hProf[l][lr][al][th]->GetEntries());
398 if (
m_fitStatus[l][lr][al][th] != FitStatus::c_lowStat) {
400 B2DEBUG(21,
"Probability of fit: " <<
m_xtFunc[l][lr][al][th]->GetProb());
406 for (
int i = 0; i < 8; ++i) {
410 B2FATAL(
"XT mode before/after calibration is different!");
415 for (
int i = 0; i < 8; ++i) {
419 m_xtFunc[l][lr][al][th]->GetParameters(par);
423 std::vector<float> xtbuff;
424 for (
int i = 0; i < 8; ++i) {
425 xtbuff.push_back(par[i]);
440 B2RESULT(
"Successfully Fitted: " << nfitted);
441 B2RESULT(
"Failure Fit: " << nfailure);
448 auto hNDF = getObjectPtr<TH1F>(
"hNDF");
449 auto hPval = getObjectPtr<TH1F>(
"hPval");
450 auto hEvtT0 = getObjectPtr<TH1F>(
"hEventT0");
451 B2INFO(
"saving histograms");
452 TFile* fout =
new TFile(
m_histName.c_str(),
"RECREATE");
453 TDirectory* top = gDirectory;
455 if (hNDF && hPval && hEvtT0) {
462 TDirectory* Direct[56];
464 for (
int l = 0; l < 56; ++l) {
466 Direct[l] = gDirectory->mkdir(Form(
"lay_%d", l));
471 for (
int lr = 0; lr < 2; ++lr) {
480 m_hProf[l][lr][al][th]->Write();
490 B2RESULT(
" " << nhisto <<
" histograms was stored.");
Database object for xt-relations.
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
void outputToFile(std::string fileName) const
Output the contents in test file format.
void setXtParamMode(unsigned short mode)
Set xt parameterization mode.
void setXtParams(const XtID xtID, const std::vector< float > ¶ms)
Set xt parameters for the specified id.
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
void storeHisto()
Store histogram to file.
void prepare()
Prepare the calibration of XT.
void sanitaryCheck()
Check if there are any wrong xt functions.
double m_par6[56]
boundary parameter for fitting, semi-experiment number
float m_lowerTheta[7]
Lower boundays of theta bins.
double m_xtPrior[56][2][18][7][8]
paremeters of XT before calibration
std::string m_histName
root file name
TF1 * m_xtFunc[56][2][20][10]
XTFunction.
double m_threshold
minimal requirement for the fraction of fitted results
double m_minNdf
minimum ndf required
bool m_LRseparate
Separate LR in calibration or mix.
void createHisto()
Create histogram for calibration.
bool m_debug
run in debug or silent
int m_minEntriesRequired
minimum number of hit per hitosgram.
int m_fitStatus[56][2][20][10]
Fit flag.
TH2F * m_hist2d[56][2][20][10]
2D histo of xt
int m_nAlphaBins
number of alpha bins
int m_nThetaBins
number of theta bins
double m_minPval
minimum pvalue required
TProfile * m_hProf[56][2][20][10]
Profile xt histo.
float m_iAlpha[18]
Represented alpha in alpha bins.
float m_lowerAlpha[18]
Lower boundays of alpha bins.
DBObjPtr< CDCGeometry > m_cdcGeo
Geometry of CDC.
XTCalibrationAlgorithm()
Constructor.
bool m_useSliceFit
Use slice fit or profile.
bool m_bField
with b field or none
void write()
Store calibrated constand.
float m_upperAlpha[18]
Upper boundays of alpha bins.
bool m_textOutput
output text file if true
EResult calibrate() override
Run algo on data.
EResult checkConvergence()
Check the convergence of XT fit.
int m_xtModePrior
Mode of xt before calibration; 0 is polynomial;1 is Chebyshev.
TH2F * m_hist2dDraw[56][20][10]
2d histo for draw
float m_upperTheta[7]
Upper boundays of theta bins.
int m_xtMode
Mode of xt; 0 is polynomial;1 is Chebyshev.
TH1F * m_hist2d_1[56][2][20][10]
1D xt histo, results of slice fit
std::string m_outputFileName
Output xt filename.
float m_iTheta[7]
Represented theta in theta bins.
Class to perform fitting for each xt function.
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
void updateDBObjPtrs(const unsigned int event, const int run, const int experiment)
Updates any DBObjPtrs by calling update(event) for DBStore.
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.
Class for accessing objects in the database.
static const double deg
degree to radians
Abstract base class for different kinds of events.