232 lines
8.3 KiB
C
232 lines
8.3 KiB
C
|
|
||
|
#ifndef BASIC_ANALYSIS
|
||
|
#define BASIC_ANALYSIS
|
||
|
|
||
|
#include <string>
|
||
|
#include <iostream>
|
||
|
#include <cmath>
|
||
|
#include <algorithm>
|
||
|
#include <filesystem>
|
||
|
#include <string_view>
|
||
|
|
||
|
#include "TTree.h"
|
||
|
|
||
|
class FourVect
|
||
|
{
|
||
|
private:
|
||
|
Float_t px, py, pz, energy;
|
||
|
|
||
|
Float_t *PtrPX()
|
||
|
{
|
||
|
return &px;
|
||
|
}
|
||
|
|
||
|
Float_t *PtrPY()
|
||
|
{
|
||
|
return &py;
|
||
|
}
|
||
|
|
||
|
Float_t *PtrPZ()
|
||
|
{
|
||
|
return &pz;
|
||
|
}
|
||
|
|
||
|
Float_t *PtrEnergy()
|
||
|
{
|
||
|
return &energy;
|
||
|
}
|
||
|
|
||
|
public:
|
||
|
static FourVect *Init(TTree *tree, const char *particle)
|
||
|
{
|
||
|
FourVect *v = new FourVect{};
|
||
|
tree->SetBranchAddress(TString::Format("%s_PX", particle).Data(), v->PtrPX());
|
||
|
tree->SetBranchAddress(TString::Format("%s_PY", particle).Data(), v->PtrPY());
|
||
|
tree->SetBranchAddress(TString::Format("%s_PZ", particle).Data(), v->PtrPZ());
|
||
|
tree->SetBranchAddress(TString::Format("%s_ENERGY", particle).Data(), v->PtrEnergy());
|
||
|
return v;
|
||
|
}
|
||
|
|
||
|
TLorentzVector LorentzVector()
|
||
|
{
|
||
|
return TLorentzVector(px, py, pz, energy);
|
||
|
}
|
||
|
|
||
|
TLorentzVector LorentzVector(double sub_mass)
|
||
|
{
|
||
|
TVector3 momentum(px, py, pz);
|
||
|
float energy = TMath::Sqrt(TMath::Sq(sub_mass) + momentum.Mag2());
|
||
|
return TLorentzVector(momentum, energy);
|
||
|
}
|
||
|
|
||
|
std::string ToString()
|
||
|
{
|
||
|
return TString::Format("(%f, %f, %f, %f)", px, py, pz, energy).Data();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
void DrawInDefaultCanvas(TH1 *histogram, const char *folder, double margin_left = 0, Option_t *option = "")
|
||
|
{
|
||
|
std::filesystem::create_directory(TString::Format("output_files/analysis/%s", folder).Data());
|
||
|
TString name = TString::Format("%s_canvas", histogram->GetName());
|
||
|
TCanvas *c = new TCanvas(name, histogram->GetName(), 0, 0, 800, 600);
|
||
|
c->SetLeftMargin(margin_left);
|
||
|
histogram->SetStats(0);
|
||
|
histogram->Draw(option);
|
||
|
c->Draw();
|
||
|
c->SaveAs(TString::Format("output_files/analysis/%s/%s.pdf", folder, name.Data()).Data());
|
||
|
}
|
||
|
|
||
|
void DrawInDefaultCanvas(RooPlot *rooPlot, const char *folder, double margin_left = 0, Option_t *option = "")
|
||
|
{
|
||
|
std::filesystem::create_directory(TString::Format("output_files/analysis/%s", folder).Data());
|
||
|
TString name = TString::Format("%s_canvas", rooPlot->GetName());
|
||
|
TCanvas *c = new TCanvas(name, rooPlot->GetName(), 0, 0, 800, 600);
|
||
|
c->SetLeftMargin(margin_left);
|
||
|
rooPlot->Draw(option);
|
||
|
c->Draw();
|
||
|
c->SaveAs(TString::Format("output_files/analysis/%s/%s.pdf", folder, name.Data()).Data());
|
||
|
}
|
||
|
|
||
|
void PrintProgress(const char *title, unsigned int total, unsigned int every, unsigned int current)
|
||
|
{
|
||
|
if ((current + 1) % every == 0 || current + 1 == total)
|
||
|
{
|
||
|
std::cout << "["
|
||
|
<< title
|
||
|
<< "] Processed event: " << current + 1 << " (" << TString::Format("%.2f", ((double)(current + 1) / (double)total) * 100.) << "%)" << std::endl;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
RooPlot *CreateRooFitHistogram(TH1D *hist)
|
||
|
{
|
||
|
RooRealVar roo_var_mass("var_mass", "B Mass Variable", B_MASS_VAR_MIN, B_MASS_VAR_MAX);
|
||
|
roo_var_mass.setRange("fitting_range", B_MASS_VAR_MIN, B_MASS_VAR_MAX);
|
||
|
|
||
|
RooDataHist roohist_B_M("roohist_B_M", "B Mass Histogram", roo_var_mass, RooFit::Import(*hist));
|
||
|
|
||
|
RooPlot *roo_frame_mass = roo_var_mass.frame(RooFit::Title(hist->GetTitle()), RooFit::Name(TString::Format("%s_rplt", hist->GetName())));
|
||
|
roohist_B_M.plotOn(roo_frame_mass, RooFit::Binning(B_MASS_HIST_BINS), RooFit::Name("B Mass Distribution"));
|
||
|
roo_frame_mass->GetXaxis()->SetTitle(hist->GetXaxis()->GetTitle());
|
||
|
|
||
|
return roo_frame_mass;
|
||
|
}
|
||
|
|
||
|
struct ShapeParamters
|
||
|
{
|
||
|
double alpha_left;
|
||
|
double n_left;
|
||
|
double alpha_right;
|
||
|
double n_right;
|
||
|
};
|
||
|
|
||
|
struct RooFitSummary
|
||
|
{
|
||
|
RooPlot *fit_histogram;
|
||
|
RooPlot *pull_histogram;
|
||
|
ShapeParamters shape_parameters;
|
||
|
};
|
||
|
|
||
|
RooFitSummary CreateRooFitHistogramAndFitCB(TH1D *hist, bool hasExpBkg, bool useExtShape, ShapeParamters extShape)
|
||
|
{
|
||
|
auto suffix_name = [name = hist->GetName()](const char *text)
|
||
|
{
|
||
|
return TString::Format("%s_%s", text, name);
|
||
|
};
|
||
|
|
||
|
RooRealVar roo_var_mass(suffix_name("var_mass"), "B Mass Variable", B_MASS_VAR_MIN, B_MASS_VAR_MAX);
|
||
|
const char *fitting_range_name = "fitting_range";
|
||
|
roo_var_mass.setRange(fitting_range_name, B_MASS_VAR_MIN, B_MASS_VAR_MAX);
|
||
|
|
||
|
TString hist_name = suffix_name("roohist_B_M");
|
||
|
RooDataHist roohist_B_M(hist_name, "B Mass Histogram", roo_var_mass, RooFit::Import(*hist));
|
||
|
|
||
|
RooPlot *roo_frame_mass = roo_var_mass.frame(RooFit::Title(hist->GetTitle()), RooFit::Name(TString::Format("%s_rplt", hist->GetName())));
|
||
|
roohist_B_M.plotOn(roo_frame_mass, RooFit::Binning(B_MASS_HIST_BINS), RooFit::Name(hist_name));
|
||
|
roo_frame_mass->GetXaxis()->SetTitle(hist->GetXaxis()->GetTitle());
|
||
|
|
||
|
// Crystal Ball for Signal
|
||
|
RooRealVar var_mass_x0(suffix_name("var_mass_x0"), "#mu", 5278., 5170., 5500.);
|
||
|
RooRealVar var_mass_sigmaLR(suffix_name("var_mass_sigmaLR"), "#sigma_{LR}", 16., 5., 40.);
|
||
|
|
||
|
RooRealVar var_mass_alphaL(suffix_name("var_mass_alphaL"), "#alpha_{L}", 2., 0., 4.);
|
||
|
RooRealVar var_mass_nL(suffix_name("var_mass_nL"), "n_{L}", 5., 0., 15.);
|
||
|
RooRealVar var_mass_alphaR(suffix_name("var_mass_alphaR"), "#alpha_{R}", 2., 0., 4.);
|
||
|
RooRealVar var_mass_nR(suffix_name("var_mass_nR"), "n_{R}", 5., 0., 15.);
|
||
|
|
||
|
if (useExtShape) {
|
||
|
var_mass_alphaL.setConstant(true);
|
||
|
var_mass_alphaL.setVal(extShape.alpha_left);
|
||
|
|
||
|
var_mass_nL.setConstant(true);
|
||
|
var_mass_nL.setVal(extShape.n_left);
|
||
|
|
||
|
var_mass_alphaR.setConstant(true);
|
||
|
var_mass_alphaR.setVal(extShape.alpha_right);
|
||
|
|
||
|
var_mass_nR.setConstant(true);
|
||
|
var_mass_nR.setVal(extShape.n_left);
|
||
|
}
|
||
|
|
||
|
TString signal_name = suffix_name("sig_cb");
|
||
|
RooCrystalBall sig_cb(signal_name, "Signal Crystal Ball", roo_var_mass, var_mass_x0, var_mass_sigmaLR, var_mass_alphaL, var_mass_nL, var_mass_alphaR, var_mass_nR);
|
||
|
|
||
|
TString pull_compare_name{};
|
||
|
if (hasExpBkg)
|
||
|
{
|
||
|
// Exponential for Background
|
||
|
RooRealVar var_mass_bkg_c(suffix_name("var_mass_bkg_c"), "#lambda", -0.0014, -0.004, -0.000);
|
||
|
|
||
|
TString background_name = suffix_name("bgk_exp");
|
||
|
RooExponential bkg_exp(background_name, "Exp Background", roo_var_mass, var_mass_bkg_c);
|
||
|
|
||
|
RooRealVar var_mass_nsig(suffix_name("nsig"), "N_{Sig}", 0., hist->GetEntries());
|
||
|
RooRealVar var_mass_nbkg(suffix_name("nbkg"), "N_{Bkg}", 0., hist->GetEntries());
|
||
|
|
||
|
TString fitted_pdf_name = suffix_name("sigplusbkg");
|
||
|
RooAddPdf fitted_pdf(fitted_pdf_name, "Sig and Bkg PDF", RooArgList(sig_cb, bkg_exp), RooArgList(var_mass_nsig, var_mass_nbkg));
|
||
|
|
||
|
RooFitResult *fitres = fitted_pdf.fitTo(roohist_B_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range(fitting_range_name));
|
||
|
|
||
|
// sigplusbkg.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kOrange + 1), RooFit::FillStyle(3144));
|
||
|
fitted_pdf.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range(fitting_range_name), RooFit::Name(fitted_pdf_name));
|
||
|
fitted_pdf.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(bkg_exp)), RooFit::LineColor(kBlue - 7), RooFit::LineStyle(kDashed), RooFit::Range(fitting_range_name), RooFit::Name(background_name));
|
||
|
fitted_pdf.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(sig_cb)), RooFit::LineColor(kRed - 7), RooFit::LineStyle(kDashed), RooFit::Range(fitting_range_name), RooFit::Name(signal_name));
|
||
|
fitted_pdf.paramOn(roo_frame_mass);
|
||
|
|
||
|
pull_compare_name = fitted_pdf_name;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
RooFitResult *fitres = sig_cb.fitTo(roohist_B_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range(fitting_range_name));
|
||
|
|
||
|
// sigplusbkg.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kOrange + 1), RooFit::FillStyle(3144));
|
||
|
sig_cb.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range(fitting_range_name), RooFit::Name(signal_name));
|
||
|
pull_compare_name = signal_name;
|
||
|
sig_cb.paramOn(roo_frame_mass);
|
||
|
}
|
||
|
|
||
|
RooPlot *roo_frame_pull = roo_var_mass.frame(RooFit::Title("Pull Distribution"), RooFit::Name(TString::Format("%s_pull_rplt", hist->GetName())));
|
||
|
roo_frame_pull->addPlotable(roo_frame_mass->pullHist(hist_name, pull_compare_name), "P");
|
||
|
|
||
|
return RooFitSummary{
|
||
|
roo_frame_mass,
|
||
|
roo_frame_pull,
|
||
|
ShapeParamters{
|
||
|
var_mass_alphaL.getVal(),
|
||
|
var_mass_nL.getVal(),
|
||
|
var_mass_alphaR.getVal(),
|
||
|
var_mass_nR.getVal()}};
|
||
|
}
|
||
|
|
||
|
bool InRange(double value, double center, double low_intvl, double up_intvl)
|
||
|
{
|
||
|
return center - low_intvl < value && value < center + up_intvl;
|
||
|
}
|
||
|
|
||
|
bool InRange(double value, double center, double intvl)
|
||
|
{
|
||
|
return InRange(value, center, intvl, intvl);
|
||
|
}
|
||
|
|
||
|
#endif
|