3 #ifdef STANDALONE_FITTER 18 double sum_of_weights;
19 double number_of_hits;
20 bool weight_alignment;
26 double getResidual(
double delta_x,
39 return delta_x - (track_x /
R - 3. *
pow(track_x /
R, 3)) * delta_y - track_dxdz * delta_z -
40 track_y * track_dxdz * delta_phix + track_x * track_dxdz * delta_phiy - track_y * delta_phiz +
44 double getResSlope(
double delta_x,
55 return -track_dxdz / 2. /
R * delta_y + (track_x /
R - track_dxdz * track_dydz) * delta_phix +
56 (1. + track_dxdz * track_dxdz) * delta_phiy - track_dydz * delta_phiz;
59 double effectiveR(
double x,
double y) {
69 Double_t residual_trackx_TF1(Double_t *
xx, Double_t *
p) {
72 p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
xx[0],
p[7],
p[8],
p[9], effectiveR(
xx[0],
p[7]),
p[10],
p[11]);
74 Double_t residual_tracky_TF1(Double_t *
xx, Double_t *
p) {
77 p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6],
xx[0],
p[8],
p[9], effectiveR(
p[6],
xx[0]),
p[10],
p[11]);
79 Double_t residual_trackdxdz_TF1(Double_t *
xx, Double_t *
p) {
80 return 10. * getResidual(
81 p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6],
p[7],
xx[0],
p[9], effectiveR(
p[6],
p[7]),
p[10],
p[11]);
83 Double_t residual_trackdydz_TF1(Double_t *
xx, Double_t *
p) {
84 return 10. * getResidual(
85 p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6],
p[7],
p[8],
xx[0], effectiveR(
p[6],
p[7]),
p[10],
p[11]);
88 Double_t resslope_trackx_TF1(Double_t *
xx, Double_t *
p) {
89 return 1000. * getResSlope(
p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
xx[0],
p[7],
p[8],
p[9], effectiveR(
xx[0],
p[7]));
91 Double_t resslope_tracky_TF1(Double_t *
xx, Double_t *
p) {
92 return 1000. * getResSlope(
p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6],
xx[0],
p[8],
p[9], effectiveR(
p[6],
xx[0]));
94 Double_t resslope_trackdxdz_TF1(Double_t *
xx, Double_t *
p) {
95 return 1000. * getResSlope(
p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6],
p[7],
xx[0],
p[9], effectiveR(
p[6],
p[7]));
97 Double_t resslope_trackdydz_TF1(Double_t *
xx, Double_t *
p) {
98 return 1000. * getResSlope(
p[0],
p[1],
p[2],
p[3],
p[4],
p[5],
p[6],
p[7],
p[8],
xx[0], effectiveR(
p[6],
p[7]));
129 double coeff =
alpha;
133 double effr = effectiveR(positionX, positionY);
134 double residpeak = getResidual(alignx,
147 double resslopepeak = getResSlope(
148 alignx, aligny, alignz, alignphix, alignphiy, alignphiz, positionX, positionY, angleX, angleY, effr);
150 double weight = (1. / redchi2) * number_of_hits / sum_of_weights;
151 if (!weight_alignment)
154 if (!weight_alignment || TMath::Prob(redchi2 * 6, 6) < 0.99)
170 residual, resslope, residpeak, resslopepeak, residsigma, resslopesigma,
alpha);
202 double redchi2 = (*resiter)[
kRedChi2];
203 if (TMath::Prob(redchi2 * 6, 6) < 0.99) {
204 sum_of_weights += 1. / redchi2;
205 number_of_hits += 1.;
208 sum_of_weights += 1.;
209 number_of_hits += 1.;
212 return sum_of_weights;
219 #ifndef STANDALONE_FITTER 228 double res_std = 0.5;
229 double resslope_std = 0.002;
251 double starts[10] = {0., 0., 0., 0., 0., res_std, resslope_std, 0., 0.1 * res_std, 0.1 * resslope_std};
253 0.1, 0.1, 0.001, 0.001, 0.001, 0.001 * res_std, 0.001 * resslope_std, 0.001, 0.01 * res_std, 0.01 * resslope_std};
254 double lows[10] = {0., 0., 0., 0., 0., 0., 0., -1., 0., 0.};
255 double highs[10] = {0., 0., 0., 0., 0., 10., 0.1, 1., 0., 0.};
257 std::vector<int>
num(nums, nums + 5);
259 std::vector<double>
start(starts, starts + 5);
261 std::vector<double>
low(lows, lows + 5);
262 std::vector<double>
high(highs, highs + 5);
269 for (ni = 0; ni < 2; ni++)
291 for (
int i = 0;
i < ni;
i++) {
306 double mean_residual = 0., mean_resslope = 0.;
307 double mean_trackx = 0., mean_tracky = 0., mean_trackdxdz = 0., mean_trackdydz = 0.;
311 const double redchi2 = (*rit)[
kRedChi2];
312 double weight = 1. / redchi2;
318 double factor_w = 1. / (sum_w +
weight);
319 mean_residual = factor_w * (sum_w * mean_residual +
weight * (*rit)[
kResid]);
320 mean_resslope = factor_w * (sum_w * mean_resslope +
weight * (*rit)[
kResSlope]);
321 mean_trackx = factor_w * (sum_w * mean_trackx +
weight * (*rit)[
kPositionX]);
322 mean_tracky = factor_w * (sum_w * mean_tracky +
weight * (*rit)[
kPositionY]);
323 mean_trackdxdz = factor_w * (sum_w * mean_trackdxdz +
weight * (*rit)[
kAngleX]);
324 mean_trackdydz = factor_w * (sum_w * mean_trackdydz +
weight * (*rit)[
kAngleY]);
329 std::string name_residual, name_resslope, name_residual_raw, name_resslope_raw, name_residual_cut, name_alpha;
330 std::string name_residual_trackx, name_resslope_trackx;
331 std::string name_residual_tracky, name_resslope_tracky;
332 std::string name_residual_trackdxdz, name_resslope_trackdxdz;
333 std::string name_residual_trackdydz, name_resslope_trackdydz;
335 name_residual =
name +
"_residual";
336 name_resslope =
name +
"_resslope";
337 name_residual_raw =
name +
"_residual_raw";
338 name_resslope_raw =
name +
"_resslope_raw";
339 name_residual_cut =
name +
"_residual_cut";
340 name_alpha =
name +
"_alpha";
341 name_residual_trackx =
name +
"_residual_trackx";
342 name_resslope_trackx =
name +
"_resslope_trackx";
343 name_residual_tracky =
name +
"_residual_tracky";
344 name_resslope_tracky =
name +
"_resslope_tracky";
345 name_residual_trackdxdz =
name +
"_residual_trackdxdz";
346 name_resslope_trackdxdz =
name +
"_resslope_trackdxdz";
347 name_residual_trackdydz =
name +
"_residual_trackdydz";
348 name_resslope_trackdydz =
name +
"_resslope_trackdydz";
352 int bins_residual = 100, bins_resslope = 100;
353 double min_residual = -50., max_residual = 50.;
354 double min_resslope = -50., max_resslope = 50.;
355 double min_trackx = -
width / 2., max_trackx =
width / 2.;
356 double min_tracky = -length / 2., max_tracky = length / 2.;
357 double min_trackdxdz = -1.5, max_trackdxdz = 1.5;
358 double min_trackdydz = -1.5, max_trackdydz = 1.5;
360 TH1F *hist_residual =
dir->make<TH1F>(name_residual.c_str(),
"", bins_residual, min_residual, max_residual);
361 TH1F *hist_resslope =
dir->make<TH1F>(name_resslope.c_str(),
"", bins_resslope, min_resslope, max_resslope);
362 TH1F *hist_residual_raw =
dir->make<TH1F>(name_residual_raw.c_str(),
"", bins_residual, min_residual, max_residual);
363 TH1F *hist_resslope_raw =
dir->make<TH1F>(name_resslope_raw.c_str(),
"", bins_resslope, min_resslope, max_resslope);
364 TH1F *hist_residual_cut =
dir->make<TH1F>(name_residual_cut.c_str(),
"", bins_residual, min_residual, max_residual);
365 TH2F *hist_alpha =
dir->make<TH2F>(name_alpha.c_str(),
"", 50, min_resslope, max_resslope, 50, -50., 50.);
367 TProfile *hist_residual_trackx =
368 dir->make<TProfile>(name_residual_trackx.c_str(),
"", 50, min_trackx, max_trackx, min_residual, max_residual);
369 TProfile *hist_resslope_trackx =
370 dir->make<TProfile>(name_resslope_trackx.c_str(),
"", 50, min_trackx, max_trackx, min_resslope, max_resslope);
371 TProfile *hist_residual_tracky =
372 dir->make<TProfile>(name_residual_tracky.c_str(),
"", 50, min_tracky, max_tracky, min_residual, max_residual);
373 TProfile *hist_resslope_tracky =
374 dir->make<TProfile>(name_resslope_tracky.c_str(),
"", 50, min_tracky, max_tracky, min_resslope, max_resslope);
375 TProfile *hist_residual_trackdxdz =
dir->make<TProfile>(
376 name_residual_trackdxdz.c_str(),
"", 500, min_trackdxdz, max_trackdxdz, min_residual, max_residual);
377 TProfile *hist_resslope_trackdxdz =
dir->make<TProfile>(
378 name_resslope_trackdxdz.c_str(),
"", 500, min_trackdxdz, max_trackdxdz, min_resslope, max_resslope);
379 TProfile *hist_residual_trackdydz =
dir->make<TProfile>(
380 name_residual_trackdydz.c_str(),
"", 500, min_trackdydz, max_trackdydz, min_residual, max_residual);
381 TProfile *hist_resslope_trackdydz =
dir->make<TProfile>(
382 name_resslope_trackdydz.c_str(),
"", 500, min_trackdydz, max_trackdydz, min_resslope, max_resslope);
384 hist_residual_trackx->SetAxisRange(-10., 10.,
"Y");
385 hist_resslope_trackx->SetAxisRange(-10., 10.,
"Y");
386 hist_residual_tracky->SetAxisRange(-10., 10.,
"Y");
387 hist_resslope_tracky->SetAxisRange(-10., 10.,
"Y");
388 hist_residual_trackdxdz->SetAxisRange(-10., 10.,
"Y");
389 hist_resslope_trackdxdz->SetAxisRange(-10., 10.,
"Y");
390 hist_residual_trackdydz->SetAxisRange(-10., 10.,
"Y");
391 hist_resslope_trackdydz->SetAxisRange(-10., 10.,
"Y");
393 name_residual +=
"_fit";
394 name_resslope +=
"_fit";
395 name_alpha +=
"_fit";
396 name_residual_trackx +=
"_fit";
397 name_resslope_trackx +=
"_fit";
398 name_residual_tracky +=
"_fit";
399 name_resslope_tracky +=
"_fit";
400 name_residual_trackdxdz +=
"_fit";
401 name_resslope_trackdxdz +=
"_fit";
402 name_residual_trackdydz +=
"_fit";
403 name_resslope_trackdydz +=
"_fit";
405 TF1 *fit_residual =
nullptr;
406 TF1 *fit_resslope =
nullptr;
409 fit_residual->SetParameters(
412 fit_residual->SetParErrors(er_res);
414 fit_resslope->SetParameters(sum_of_weights * (max_resslope - min_resslope) / bins_resslope,
418 fit_resslope->SetParErrors(er_resslope);
421 fit_residual->SetParameters(sum_of_weights * (max_residual - min_residual) / bins_residual,
426 fit_resslope->SetParameters(sum_of_weights * (max_resslope - min_resslope) / bins_resslope,
432 fit_residual->SetParameters(sum_of_weights * (max_residual - min_residual) / bins_residual,
437 fit_resslope->SetParameters(sum_of_weights * (max_resslope - min_resslope) / bins_resslope,
444 fit_residual->SetParameters(
448 fit_resslope->SetParameters(sum_of_weights * (max_resslope - min_resslope) / bins_resslope,
455 fit_residual->SetLineColor(2);
456 fit_residual->SetLineWidth(2);
457 fit_residual->Write();
458 fit_resslope->SetLineColor(2);
459 fit_resslope->SetLineWidth(2);
460 fit_resslope->Write();
462 TF1 *fit_alpha =
new TF1(name_alpha.c_str(),
"[0] + x*[1]", min_resslope, max_resslope);
470 a = -
b * mean_resslope;
473 fit_alpha->SetParameters(
a,
b);
474 fit_alpha->SetLineColor(2);
475 fit_alpha->SetLineWidth(2);
478 TProfile *fit_residual_trackx =
dir->make<TProfile>(name_residual_trackx.c_str(),
"", 100, min_trackx, max_trackx);
479 TProfile *fit_resslope_trackx =
dir->make<TProfile>(name_resslope_trackx.c_str(),
"", 100, min_trackx, max_trackx);
480 TProfile *fit_residual_tracky =
dir->make<TProfile>(name_residual_tracky.c_str(),
"", 100, min_tracky, max_tracky);
481 TProfile *fit_resslope_tracky =
dir->make<TProfile>(name_resslope_tracky.c_str(),
"", 100, min_tracky, max_tracky);
482 TProfile *fit_residual_trackdxdz =
483 dir->make<TProfile>(name_residual_trackdxdz.c_str(),
"", 500, min_trackdxdz, max_trackdxdz);
484 TProfile *fit_resslope_trackdxdz =
485 dir->make<TProfile>(name_resslope_trackdxdz.c_str(),
"", 500, min_trackdxdz, max_trackdxdz);
486 TProfile *fit_residual_trackdydz =
487 dir->make<TProfile>(name_residual_trackdydz.c_str(),
"", 500, min_trackdydz, max_trackdydz);
488 TProfile *fit_resslope_trackdydz =
489 dir->make<TProfile>(name_resslope_trackdydz.c_str(),
"", 500, min_trackdydz, max_trackdydz);
491 fit_residual_trackx->SetLineColor(2);
492 fit_residual_trackx->SetLineWidth(2);
493 fit_resslope_trackx->SetLineColor(2);
494 fit_resslope_trackx->SetLineWidth(2);
495 fit_residual_tracky->SetLineColor(2);
496 fit_residual_tracky->SetLineWidth(2);
497 fit_resslope_tracky->SetLineColor(2);
498 fit_resslope_tracky->SetLineWidth(2);
499 fit_residual_trackdxdz->SetLineColor(2);
500 fit_residual_trackdxdz->SetLineWidth(2);
501 fit_resslope_trackdxdz->SetLineColor(2);
502 fit_resslope_trackdxdz->SetLineWidth(2);
503 fit_residual_trackdydz->SetLineColor(2);
504 fit_residual_trackdydz->SetLineWidth(2);
505 fit_resslope_trackdydz->SetLineColor(2);
506 fit_resslope_trackdydz->SetLineWidth(2);
508 name_residual_trackx +=
"line";
509 name_resslope_trackx +=
"line";
510 name_residual_tracky +=
"line";
511 name_resslope_tracky +=
"line";
512 name_residual_trackdxdz +=
"line";
513 name_resslope_trackdxdz +=
"line";
514 name_residual_trackdydz +=
"line";
515 name_resslope_trackdydz +=
"line";
517 TF1 *fitline_residual_trackx =
new TF1(name_residual_trackx.c_str(), residual_trackx_TF1, min_trackx, max_trackx, 12);
518 TF1 *fitline_resslope_trackx =
new TF1(name_resslope_trackx.c_str(), resslope_trackx_TF1, min_trackx, max_trackx, 12);
519 TF1 *fitline_residual_tracky =
new TF1(name_residual_tracky.c_str(), residual_tracky_TF1, min_tracky, max_tracky, 12);
520 TF1 *fitline_resslope_tracky =
new TF1(name_resslope_tracky.c_str(), resslope_tracky_TF1, min_tracky, max_tracky, 12);
521 TF1 *fitline_residual_trackdxdz =
522 new TF1(name_residual_trackdxdz.c_str(), residual_trackdxdz_TF1, min_trackdxdz, max_trackdxdz, 12);
523 TF1 *fitline_resslope_trackdxdz =
524 new TF1(name_resslope_trackdxdz.c_str(), resslope_trackdxdz_TF1, min_trackdxdz, max_trackdxdz, 12);
525 TF1 *fitline_residual_trackdydz =
526 new TF1(name_residual_trackdydz.c_str(), residual_trackdydz_TF1, min_trackdydz, max_trackdydz, 12);
527 TF1 *fitline_resslope_trackdydz =
528 new TF1(name_resslope_trackdydz.c_str(), resslope_trackdydz_TF1, min_trackdydz, max_trackdydz, 12);
530 std::vector<TF1 *> fitlines;
531 fitlines.push_back(fitline_residual_trackx);
532 fitlines.push_back(fitline_resslope_trackx);
533 fitlines.push_back(fitline_residual_tracky);
534 fitlines.push_back(fitline_resslope_tracky);
535 fitlines.push_back(fitline_residual_trackdxdz);
536 fitlines.push_back(fitline_resslope_trackdxdz);
537 fitlines.push_back(fitline_residual_trackdydz);
538 fitlines.push_back(fitline_resslope_trackdydz);
553 fitparameters[10] = 0.;
555 for (std::vector<TF1 *>::const_iterator itr = fitlines.begin(); itr != fitlines.end(); itr++) {
556 (*itr)->SetParameters(fitparameters);
557 (*itr)->SetLineColor(2);
558 (*itr)->SetLineWidth(2);
563 const double resid = (*resiter)[
kResid];
564 const double resslope = (*resiter)[
kResSlope];
565 const double positionX = (*resiter)[
kPositionX];
566 const double positionY = (*resiter)[
kPositionY];
567 const double angleX = (*resiter)[
kAngleX];
568 const double angleY = (*resiter)[
kAngleY];
569 const double redchi2 = (*resiter)[
kRedChi2];
570 double weight = 1. / redchi2;
575 hist_alpha->Fill(1000. * resslope, 10. * resid);
590 effectiveR(positionX, positionY),
594 hist_residual_trackx->Fill(positionX, 10. * resid,
weight);
595 hist_residual_tracky->Fill(positionY, 10. * resid,
weight);
596 hist_residual_trackdxdz->Fill(angleX, 10. * resid,
weight);
597 hist_residual_trackdydz->Fill(angleY, 10. * resid,
weight);
598 fit_residual_trackx->Fill(positionX, 10. * geom_resid,
weight);
599 fit_residual_tracky->Fill(positionY, 10. * geom_resid,
weight);
600 fit_residual_trackdxdz->Fill(angleX, 10. * geom_resid,
weight);
601 fit_residual_trackdydz->Fill(angleY, 10. * geom_resid,
weight);
613 effectiveR(positionX, positionY));
615 hist_resslope_trackx->Fill(positionX, 1000. * resslope,
weight);
616 hist_resslope_tracky->Fill(positionY, 1000. * resslope,
weight);
617 hist_resslope_trackdxdz->Fill(angleX, 1000. * resslope,
weight);
618 hist_resslope_trackdydz->Fill(angleY, 1000. * resslope,
weight);
619 fit_resslope_trackx->Fill(positionX, 1000. * geom_resslope,
weight);
620 fit_resslope_tracky->Fill(positionY, 1000. * geom_resslope,
weight);
621 fit_resslope_trackdxdz->Fill(angleX, 1000. * geom_resslope,
weight);
622 fit_resslope_trackdydz->Fill(angleY, 1000. * geom_resslope,
weight);
625 hist_residual_raw->Fill(10. * resid);
626 hist_resslope_raw->Fill(1000. * resslope);
627 if (fabs(resslope) < 0.005)
628 hist_residual_cut->Fill(10. * resid);
633 for (
int i = 1;
i <= hist_residual->GetNbinsX();
i++) {
634 double xi = hist_residual->GetBinCenter(
i);
635 double yi = hist_residual->GetBinContent(
i);
636 double yerri = hist_residual->GetBinError(
i);
637 double yth = fit_residual->Eval(
xi);
639 chi2 +=
pow((yth - yi) / yerri, 2);
643 for (
int i = 1;
i <= hist_resslope->GetNbinsX();
i++) {
644 double xi = hist_resslope->GetBinCenter(
i);
645 double yi = hist_resslope->GetBinContent(
i);
646 double yerri = hist_resslope->GetBinError(
i);
647 double yth = fit_resslope->Eval(
xi);
649 chi2 +=
pow((yth - yi) / yerri, 2);
663 unsigned int preselected) {
664 TFile *
f =
new TFile(
fname.c_str());
665 TTree *
t = (TTree *)
f->Get(
"mual_ttree");
668 TFile *tmpf =
new TFile(
"small_tree_fit.root",
"recreate");
672 TTree *
tt =
t->CopyTree(Form(
"!is_dt && is_plus==%d && ring_wheel==%d && station==%d && sector==%d && select==%d",
680 tt->SetBranchAddress(
"res_x", &
r.res_x);
681 tt->SetBranchAddress(
"res_slope_x", &
r.res_slope_x);
682 tt->SetBranchAddress(
"pos_x", &
r.pos_x);
683 tt->SetBranchAddress(
"pos_y", &
r.pos_y);
684 tt->SetBranchAddress(
"angle_x", &
r.angle_x);
685 tt->SetBranchAddress(
"angle_y", &
r.angle_y);
686 tt->SetBranchAddress(
"pz", &
r.pz);
687 tt->SetBranchAddress(
"pt", &
r.pt);
688 tt->SetBranchAddress(
"q", &
r.q);
690 Long64_t nentries =
tt->GetEntries();
691 for (Long64_t
i = 0;
i < nentries;
i++) {
double MuonResidualsFitter_logGaussPowerTails(double residual, double center, double sigma)
Double_t MuonResidualsFitter_powerLawTails_TF1(Double_t *xvec, Double_t *par)
const AlignableSurface & surface() const
Return the Surface (global position and orientation) of the object.
double MuonResidualsFitter_logPureGaussian2D(double x, double y, double x0, double y0, double sx, double sy, double r)
int residualsModel() const
std::vector< double * >::const_iterator residuals_begin() const
void fill(double *residual)
align::Scalar width() const
MuonResidualsFitter * fitter()
TTree * readNtuple(std::string fname, unsigned int endcap, unsigned int station, unsigned int ring, unsigned int chamber, unsigned int preselected=1)
void correctBField() override
std::vector< double * >::const_iterator residuals_end() const
const std::string names[nVars_]
bool dofit(void(*fcn)(int &, double *, double &, double *, int), std::vector< int > &parNum, std::vector< std::string > &parName, std::vector< double > &start, std::vector< double > &step, std::vector< double > &low, std::vector< double > &high)
double MuonResidualsFitter_logROOTVoigt(double residual, double center, double sigma, double gamma)
void inform(TMinuit *tMinuit) override
const PositionType & globalPosition() const
Return the global position of the object.
double plot(std::string name, TFileDirectory *dir, Alignable *ali) override
void MuonResiduals6DOFrphiFitter_FCN(int &npar, double *gin, double &fval, double *par, int iflag)
double MuonResidualsFitter_logPowerLawTails(double residual, double center, double sigma, double gamma)
Double_t MuonResidualsFitter_GaussPowerTails_TF1(Double_t *xvec, Double_t *par)
void fix(int parNum, bool dofix=true)
bool fit(Alignable *ali) override
virtual void correctBField()=0
Double_t MuonResidualsFitter_pureGaussian_TF1(Double_t *xvec, Double_t *par)
double errorerror(int parNum)
align::Scalar length() const
int useRes(int pattern=-1)
Double_t MuonResidualsFitter_ROOTVoigt_TF1(Double_t *xvec, Double_t *par)
double MuonResidualsFitter_logPureGaussian(double residual, double center, double sigma)
double sumofweights() override