23#include <Math/Functor.h>
24#include <Math/SpecFuncMathCore.h>
25#include <Math/DistFunc.h>
28#include <RooRealVar.h>
29#include <RooDataSet.h>
30#include <RooGaussian.h>
34#include <RooPolyVar.h>
35#include <RooArgusBG.h>
36#include <RooSimultaneous.h>
37#include <RooCategory.h>
39#include <RooConstVar.h>
41#include <framework/particledb/EvtGenDatabasePDG.h>
46#include <reconstruction/calibration/BeamSpotBoostInvMass/InvariantMassBhadStandAlone.h>
47#include <reconstruction/calibration/BeamSpotBoostInvMass/Splitter.h>
48#include <reconstruction/calibration/BeamSpotBoostInvMass/tools.h>
50#include <InvariantMassBhadStandAlone.h>
59namespace Belle2::InvariantMassBhadCalib {
64 std::vector<Event> getEvents(TTree* tr)
67 std::vector<Event> events;
68 events.reserve(tr->GetEntries());
72 tr->SetBranchAddress(
"run", &evt.run);
73 tr->SetBranchAddress(
"exp", &evt.exp);
74 tr->SetBranchAddress(
"event", &evt.evtNo);
75 tr->SetBranchAddress(
"time", &evt.t);
78 std::vector<double>* pBcms =
new std::vector<double>;
79 std::vector<double>* mB =
new std::vector<double>;
80 std::vector<int>* pdg =
new std::vector<int>;
81 std::vector<int>* mode =
new std::vector<int>;
82 std::vector<double>* Kpid =
new std::vector<double>;
83 std::vector<double>* R2 =
new std::vector<double>;
84 std::vector<double>* mD =
new std::vector<double>;
85 std::vector<double>* dmDstar =
new std::vector<double>;
89 tr->SetBranchAddress(
"pBcms", &pBcms);
90 tr->SetBranchAddress(
"mB", &mB);
91 tr->SetBranchAddress(
"pdg", &pdg);
92 tr->SetBranchAddress(
"mode", &mode);
93 tr->SetBranchAddress(
"Kpid", &Kpid);
94 tr->SetBranchAddress(
"R2", &R2);
95 tr->SetBranchAddress(
"mD", &mD);
96 tr->SetBranchAddress(
"dmDstar", &dmDstar);
101 for (
int i = 0; i < tr->GetEntries(); ++i) {
104 int nCand = mode->size();
105 evt.cand.resize(nCand);
107 for (
int j = 0; j < nCand; ++j) {
108 evt.cand[j].pBcms = pBcms->at(j);
109 evt.cand[j].mB = mB->at(j);
110 evt.cand[j].pdg = pdg->at(j);
111 evt.cand[j].mode = mode->at(j);
112 evt.cand[j].Kpid = Kpid->at(j);
113 evt.cand[j].R2 = R2->at(j);
114 evt.cand[j].mD = mD->at(j);
115 evt.cand[j].dmDstar = dmDstar->at(j);
117 evt.cand[j].isSig =
true;
122 events.push_back(evt);
126 sort(events.begin(), events.end(), [](Event e1, Event e2) {return e1.t < e2.t;});
134 void plotArgusFit(RooDataSet* dataE0, RooAddPdf& sumB0, RooArgusBG& argus, RooGaussian& gauss, RooRealVar& eNow,
138 bool isBatch = gROOT->IsBatch();
139 gROOT->SetBatch(kTRUE);
141 using namespace RooFit ;
144 RooPlot* mbcframe = eNow.frame(40) ;
145 dataE0->plotOn(mbcframe) ;
147 sumB0.paramOn(mbcframe, dataE0);
150 sumB0.plotOn(mbcframe, Components(argus), LineStyle(kDashed)) ;
151 sumB0.plotOn(mbcframe, Components(gauss), LineStyle(kDashed), LineColor(kRed)) ;
153 sumB0.plotOn(mbcframe);
155 mbcframe->GetXaxis()->SetTitleSize(0.0001);
156 mbcframe->GetXaxis()->SetLabelSize(0.0001);
157 mbcframe->SetTitleSize(0.0001);
158 mbcframe->GetYaxis()->SetTitleSize(0.06);
159 mbcframe->GetYaxis()->SetTitleOffset(0.7);
160 mbcframe->GetYaxis()->SetLabelSize(0.06) ;
162 mbcframe->SetTitle(
"");
165 RooHist* hpull = mbcframe->pullHist() ;
167 RooPlot* frame3 = eNow.frame(Title(
".")) ;
168 frame3->GetYaxis()->SetTitle(
"Pull") ;
169 frame3->GetYaxis()->SetTitleSize(0.13) ;
171 frame3->GetYaxis()->SetNdivisions(504) ;
172 frame3->GetYaxis()->SetLabelSize(0.15) ;
173 frame3->GetXaxis()->SetTitleSize(0.15) ;
174 frame3->GetXaxis()->SetLabelSize(0.15) ;
176 frame3->GetYaxis()->SetTitleOffset(0.3) ;
177 frame3->addPlotable(hpull,
"x0 P E1") ;
178 frame3->SetMaximum(5.);
179 frame3->SetMinimum(-5.);
182 TCanvas* c1 =
new TCanvas(
rn()) ;
183 TPad* pad1 =
new TPad(
"pad1",
"pad1", 0, 0.3, 1, 1.0);
189 TLine* ll =
new TLine;
190 double mRev = 10579.4e-3 / 2;
191 ll->SetLineColor(kGreen);
192 ll->DrawLine(mRev, 0, mRev, mbcframe->GetMaximum());
196 TPad* pad2 =
new TPad(
"pad2",
"pad2", 0, 0.00, 1, 0.3);
197 pad2->SetTopMargin(0.05);
198 pad2->SetBottomMargin(0.35);
204 TLine* l =
new TLine(5279.34e-3, 0.0, 5.37, 0.0);
205 l->SetLineColor(kBlue);
210 if (fName !=
"") c1->SaveAs(fName);
222 gROOT->SetBatch(isBatch);
227 std::map<TString, std::pair<double, double>> argusFit(
const std::vector<Event>& evts,
228 std::vector<std::pair<double, double>> limits)
234 using namespace RooFit;
236 RooRealVar eNow(
"eNow",
"E^{*}_{B} [GeV]", cMBp, 5.37);
239 RooDataSet* dataE0 =
new RooDataSet(
"dataE0",
"dataE0", RooArgSet(eNow));
240 RooDataSet* dataEp =
new RooDataSet(
"dataEp",
"dataEp", RooArgSet(eNow));
243 TH1D* hDeltaE =
new TH1D(
rn(),
"", 30, -0.05, 0.05);
244 TH1D* hMD =
new TH1D(
rn(),
"", 30, 1.7, 1.9);
245 TH1D* hMB =
new TH1D(
rn(),
"", 30, -0.15, 0.15);
253 for (
auto event : evts) {
254 for (
auto cand : event.cand) {
256 double p = cand.pBcms;
257 double mInv = cand.mB;
264 if (1.830 < cand.mD && cand.mD < 1.894)
265 if (abs(mInv - mB) < 0.05)
267 if ((cand.dmDstar < -10) || (0.143 < cand.dmDstar && cand.dmDstar < 0.147)) {
268 double eBC =
sqrt(p * p + pow(mB, 2));
269 if (eBC > 5.37)
continue;
271 for (
unsigned i = 0; i < limits.size(); ++i) {
272 if (limits[i].first <= event.t && event.t < limits[i].second) {
273 if (abs(cand.pdg) == 511) {
275 dataE0->add(RooArgSet(eNow));
278 dataEp->add(RooArgSet(eNow));
283 hDeltaE->Fill(eBC - cmsE0 / 2);
285 hMB->Fill(mInv - mB);
292 RooCategory Bcharge(
"sample",
"sample") ;
293 Bcharge.defineType(
"B0") ;
294 Bcharge.defineType(
"Bp") ;
296 RooDataSet combData(
"combData",
"combined data", eNow, Index(Bcharge), Import(
"B0", *dataE0), Import(
"Bp", *dataEp)) ;
300 RooRealVar sigmean(
"Mean",
"B^{#pm} mass", 5.29, 5.27, 5.30) ;
301 RooRealVar sigwidth(
"#sigma",
"B^{#pm} width", 0.00237, 0.0001, 0.030) ;
305 RooPolyVar sigmeanNow(
"sigmeanNow",
"shape parameter", sigmean, RooArgSet(RooConst(0.000), RooConst(1.)));
306 RooGaussian gauss(
"gauss",
"gaussian PDF", eNow, sigmeanNow, sigwidth) ;
309 RooRealVar argpar(
"Argus_param",
"argus shape parameter", -150.7, -300., +50.0) ;
310 RooRealVar endpointBp(
"EndPointBp",
"endPoint parameter", cMBp, 5.27, 5.291) ;
311 RooRealVar endpointB0(
"EndPointB0",
"endPoint parameter", cMB0, 5.27, 5.291) ;
312 endpointB0.setConstant(kTRUE);
313 endpointBp.setConstant(kTRUE);
317 RooPolyVar shape2B0(
"EndPoint2B0",
"shape parameter", endpointB0, RooArgSet(RooConst(0.), RooConst(2.)));
318 RooPolyVar eNowDifB0(
"eNowDifB0",
"eNowDifB0", eNow, RooArgSet(shape2B0, RooConst(-1.)));
319 RooArgusBG argusB0(
"argusB0",
"Argus PDF", eNowDifB0, endpointB0, argpar) ;
322 RooPolyVar shape2Bp(
"EndPoint2Bp",
"shape parameter", endpointBp, RooArgSet(RooConst(0.), RooConst(2.)));
323 RooPolyVar eNowDifBp(
"eNowDifBp",
"eNowDifBp", eNow, RooArgSet(shape2Bp, RooConst(-1.)));
324 RooArgusBG argusBp(
"argusBp",
"Argus PDF", eNowDifBp, endpointBp, argpar) ;
328 RooRealVar nsigB0(
"nsigB0",
"#signal events", 100, 0., 100000) ;
329 RooRealVar nbkgB0(
"nbkgB0",
"#background events", 100, 0., 500000) ;
331 RooRealVar nsigBp(
"nsigBp",
"#signal events", 100, 0., 100000) ;
332 RooRealVar nbkgBp(
"nbkgBp",
"#background events", 100, 0., 500000) ;
336 RooAddPdf sumB0(
"sumB0",
"g0+a0", RooArgList(gauss, argusB0), RooArgList(nsigB0, nbkgB0)) ;
337 RooAddPdf sumBp(
"sumBp",
"gP+aP", RooArgList(gauss, argusBp), RooArgList(nsigBp, nbkgBp)) ;
341 RooSimultaneous simPdf(
"simPdf",
"simultaneous pdf", Bcharge) ;
344 simPdf.addPdf(sumB0,
"B0") ;
345 simPdf.addPdf(sumBp,
"Bp") ;
347 simPdf.fitTo(combData);
350 std::map<TString, std::pair<double, double>> resMap;
351 resMap[
"sigmean"] = {sigmean.getValV(), sigmean.getError()};
352 resMap[
"sigwidth"] = {sigwidth.getValV(), sigwidth.getError()};
353 resMap[
"argpar"] = {argpar.getValV(), argpar.getError()};
355 std::filesystem::create_directories(
"plotsHadBonly");
357 plotArgusFit(dataE0, sumB0, argusB0, gauss, eNow, Form(
"plotsHadBonly/B0Single_%d.pdf",
int(round(limits[0].first))));
358 plotArgusFit(dataEp, sumBp, argusBp, gauss, eNow, Form(
"plotsHadBonly/BpSingle_%d.pdf",
int(round(limits[0].first))));
379 std::map<TString, std::pair<double, double>> argusFitConstrained(
const std::vector<Event>& evts,
380 std::vector<std::pair<double, double>> limits,
381 std::vector<std::pair<double, double>> mumuVals, std::vector<double> startPars)
384 for (
auto& el : mumuVals) {
390 const double meanInit = startPars[0] / 2;
391 const double sigmaInit = startPars[2] / 2;
392 const double argparInit = startPars[4];
394 double s = 0, sw = 0;
395 for (
auto p : mumuVals) {
396 s += p.first / pow(p.second, 2);
397 sw += 1. / pow(p.second, 2);
399 const double mumuMean = s / sw;
400 const double shiftInit = meanInit - mumuMean;
406 using namespace RooFit;
408 RooRealVar eNow(
"eNow",
"E^{*}_{B} [GeV]", cMBp, 5.37);
410 std::vector<RooDataSet*> dataE0(limits.size());
411 std::vector<RooDataSet*> dataEp(limits.size());
413 for (
unsigned i = 0; i < limits.size(); ++i) {
414 dataE0[i] =
new RooDataSet(Form(
"dataE0_%u", i), Form(
"dataE0_%u", i), RooArgSet(eNow));
415 dataEp[i] =
new RooDataSet(Form(
"dataEp_%u", i), Form(
"dataEp_%u", i), RooArgSet(eNow));
419 TH1D* hDeltaE =
new TH1D(
rn(),
"", 30, -0.05, 0.05);
420 TH1D* hMD =
new TH1D(
rn(),
"", 30, 1.7, 1.9);
421 TH1D* hMB =
new TH1D(
rn(),
"", 30, -0.15, 0.15);
428 for (
auto event : evts) {
429 for (
auto cand : event.cand) {
431 double p = cand.pBcms;
432 double mInv = cand.mB;
439 if (1.830 < cand.mD && cand.mD < 1.894)
440 if (abs(mInv - mB) < 0.05)
442 if ((cand.dmDstar < -10) || (0.143 < cand.dmDstar && cand.dmDstar < 0.147)) {
443 double eBC =
sqrt(p * p + pow(mB, 2));
444 if (eBC > 5.37)
continue;
446 for (
unsigned i = 0; i < limits.size(); ++i) {
447 if (limits[i].first <= event.t && event.t < limits[i].second) {
448 if (abs(cand.pdg) == 511) {
450 dataE0[i]->add(RooArgSet(eNow));
453 dataEp[i]->add(RooArgSet(eNow));
458 hDeltaE->Fill(eBC - cmsE0 / 2);
460 hMB->Fill(mInv - mB);
467 RooCategory Bcharge(
"sample",
"sample") ;
468 for (
unsigned i = 0; i < limits.size(); ++i) {
469 Bcharge.defineType(Form(
"B0_%u", i));
470 Bcharge.defineType(Form(
"Bp_%u", i));
473 RooDataSet* combData =
nullptr;
475 std::map<std::string, RooDataSet*> dataSetMap;
476 for (
unsigned i = 0; i < limits.size(); ++i) {
477 dataSetMap[Form(
"B0_%u", i)] = dataE0[i];
478 dataSetMap[Form(
"Bp_%u", i)] = dataEp[i];
480 combData =
new RooDataSet(
"combData",
"combined data", eNow, Index(Bcharge), Import(dataSetMap));
485 RooRealVar sigwidth(
"#sigma",
"width of B-meson energy in CMS", sigmaInit, 0.0001, 0.030) ;
487 std::vector<RooRealVar*> sigmean(limits.size());
488 std::vector<RooGaussian*> gauss(limits.size());
492 for (
unsigned i = 0; i < limits.size(); ++i) {
493 sigmean[i] =
new RooRealVar(Form(
"Mean_%u", i),
"mean B-meson energy in CMS", meanInit, 5.27, 5.30) ;
494 gauss[i] =
new RooGaussian(Form(
"gauss_%u", i),
"gaussian PDF", eNow, *sigmean[i], sigwidth) ;
500 RooRealVar argpar(
"Argus_param",
"argus shape parameter", argparInit, -300., +50.0) ;
501 RooRealVar endpointBp(
"EndPointBp",
"endPoint parameter", cMBp, 5.27, 5.291) ;
502 RooRealVar endpointB0(
"EndPointB0",
"endPoint parameter", cMB0, 5.27, 5.291) ;
503 endpointB0.setConstant(kTRUE);
504 endpointBp.setConstant(kTRUE);
507 RooRealVar zero(
"zero",
"", 0);
508 RooRealVar two(
"two",
"", 2);
509 RooRealVar minus(
"minus",
"", -1);
512 RooPolyVar shape2B0(
"EndPoint2B0",
"shape parameter", endpointB0, RooArgSet(zero, two));
513 RooPolyVar eNowDifB0(
"eNowDifB0",
"eNowDifB0", eNow, RooArgSet(shape2B0, minus));
514 RooArgusBG argusB0(
"argusB0",
"Argus PDF", eNowDifB0, endpointB0, argpar) ;
517 RooPolyVar shape2Bp(
"EndPoint2Bp",
"shape parameter", endpointBp, RooArgSet(zero, two));
518 RooPolyVar eNowDifBp(
"eNowDifBp",
"eNowDifBp", eNow, RooArgSet(shape2Bp, minus));
519 RooArgusBG argusBp(
"argusBp",
"Argus PDF", eNowDifBp, endpointBp, argpar) ;
522 std::vector<RooRealVar*> nsigB0(limits.size());
523 std::vector<RooRealVar*> nbkgB0(limits.size());
525 std::vector<RooRealVar*> nsigBp(limits.size());
526 std::vector<RooRealVar*> nbkgBp(limits.size());
528 std::vector<RooAddPdf*> sumB0(limits.size());
529 std::vector<RooAddPdf*> sumBp(limits.size());
533 for (
unsigned i = 0; i < limits.size(); ++i) {
534 nsigB0[i] =
new RooRealVar(Form(
"nsigB0_%u", i),
"#signal events", 100, 0., 100000);
535 nbkgB0[i] =
new RooRealVar(Form(
"nbkgB0_%u", i),
"#background events", 100, 0., 500000) ;
537 nsigBp[i] =
new RooRealVar(Form(
"nsigBp_%u", i),
"#signal events", 100, 0., 100000) ;
538 nbkgBp[i] =
new RooRealVar(Form(
"nbkgBp_%u", i),
"#background events", 100, 0., 500000) ;
540 sumB0[i] =
new RooAddPdf(Form(
"sumB0_%u", i),
"g0+a0", RooArgList(*gauss[i], argusB0), RooArgList(*nsigB0[i], *nbkgB0[i]));
541 sumBp[i] =
new RooAddPdf(Form(
"sumBp_%u", i),
"gP+aP", RooArgList(*gauss[i], argusBp), RooArgList(*nsigBp[i], *nbkgBp[i]));
547 RooSimultaneous simPdf(
"simPdf",
"simultaneous pdf", Bcharge) ;
550 for (
unsigned i = 0; i < limits.size(); ++i) {
551 simPdf.addPdf(*sumB0[i], Form(
"B0_%u", i));
552 simPdf.addPdf(*sumBp[i], Form(
"Bp_%u", i));
555 RooRealVar shift(
"shift",
"shift to mumu", shiftInit, -30e-3, 30e-3);
557 std::vector<RooGaussian*> fconstraint(limits.size());
558 std::vector<RooPolyVar*> shiftNow(limits.size());
560 for (
unsigned i = 0; i < limits.size(); ++i) {
561 shiftNow[i] =
new RooPolyVar(Form(
"shiftNow_%u", i),
"shiftShift", shift, RooArgSet(RooConst(mumuVals[i].first), RooConst(1.)));
563 fconstraint[i] =
new RooGaussian(Form(
"fconstraint_%u", i),
"fconstraint", *sigmean[i], *shiftNow[i],
564 RooConst(mumuVals[i].second / 1.)) ;
568 RooArgSet constraintSet;
569 for (
auto& c : fconstraint) {
570 constraintSet.add(*c);
572 simPdf.fitTo(*combData, ExternalConstraints(constraintSet));
575 std::map<TString, std::pair<double, double>> resMap;
576 for (
unsigned i = 0; i < limits.size(); ++i) {
577 resMap[Form(
"sigmean_%u", i)] = {sigmean[i]->getValV(), sigmean[i]->getError()};
578 resMap[Form(
"pull_%u", i)] = { (sigmean[i]->getValV() - shift.getValV() - mumuVals[i].first) / mumuVals[i].second, 0};
581 resMap[
"sigwidth"] = {sigwidth.getValV(), sigwidth.getError()};
582 resMap[
"argpar"] = {argpar.getValV(), argpar.getError()};
583 resMap[
"shift"] = {shift.getValV(), shift.getError()};
586 namespace fs = std::filesystem;
587 fs::create_directories(
"plotsHadB");
589 for (
unsigned i = 0; i < limits.size(); ++i) {
590 plotArgusFit(dataE0[i], *sumB0[i], argusB0, *gauss[i], eNow, Form(
"plotsHadB/B0_%d_%u.pdf",
int(round(limits[0].first)), i));
591 plotArgusFit(dataEp[i], *sumBp[i], argusBp, *gauss[i], eNow, Form(
"plotsHadB/Bp_%d_%u.pdf",
int(round(limits[0].first)), i));
596 for (
unsigned i = 0; i < limits.size(); ++i) {
611 delete fconstraint[i];
629 std::vector<std::vector<double>> doBhadFit(
const std::vector<Event>& evts, std::vector<std::pair<double, double>> limits,
630 std::vector<std::pair<double, double>> mumuVals,
const std::vector<double>& startPars)
633 auto r = argusFitConstrained(evts, limits, mumuVals, startPars);
634 assert(limits.size() == mumuVals.size());
636 std::vector<std::vector<double>> result(limits.size());
639 for (
unsigned i = 0; i < limits.size(); ++i) {
640 std::string n = Form(
"sigmean_%u", i);
641 std::string np = Form(
"pull_%u", i);
644 result[i] = {2 * r.at(n).first, 2 * r.at(n).second, 2 * r.at(
"sigwidth").first, 2 * r.at(
"sigwidth").second, 2 * r.at(
"shift").first, 2 * r.at(
"shift").second, r.at(np).first};
652 std::vector<double> doBhadOnlyFit(
const std::vector<Event>& evts,
const std::vector<std::pair<double, double>>& limits)
655 auto r = argusFit(evts, limits);
657 return {2 * r.at(
"sigmean").first, 2 * r.at(
"sigmean").second, 2 * r.at(
"sigwidth").first, 2 * r.at(
"sigwidth").second, r.at(
"argpar").first};
static EvtGenDatabasePDG * Instance()
Instance method that loads the EvtGen table.
double sqrt(double a)
sqrt for double
TString rn()
Get random string.