ROOT Analysis for the Inclusive Detachted Dilepton Trigger Lines
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

434 lines
18 KiB

#include "TH1D.h"
#include "TH2D.h"
#include "THStack.h"
#include "TGraph.h"
#include "TTree.h"
#include "TChain.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TROOT.h"
#include "TStyle.h"
#include "TColor.h"
#include "TLorentzVector.h"
#include "TRandom3.h"
#include "TLegend.h"
#include "RooDataHist.h"
#include "RooRealVar.h"
#include "RooPlot.h"
#include "RooGaussian.h"
#include "RooExponential.h"
#include "RooRealConstant.h"
#include "RooAddPdf.h"
#include "RooFitResult.h"
#include "RooProduct.h"
#include "RooCrystalBall.h"
#include "RooBreitWigner.h"
#include "RooArgSet.h"
#include "RooFFTConvPdf.h"
#include "RooNovosibirsk.h"
#include <string>
#include <iostream>
#include <cmath>
const int nBins = 70;
const Double_t MASS_HIST_MIN = 5100.;
const Double_t MASS_HIST_MAX = 6000.;
const Double_t MASS_HIST_FIT_MIN = 5100.;
const Double_t MASS_HIST_FIT_MAX = 6000.;
const Double_t K_MASS = 493.677;
const Double_t PSI2S_MASS = 3686.093;
const Double_t JPSI_MASS = 3096.900;
const Double_t KSTAR_MASS = 891.760;
void CreateRooFitAndDraw(TH1D *hist, const char *name);
const char *TITLE = "SpruceRD_BuToHpMuMu (#pi^{+} #rightarrow K^{+}, Small Cut)";
const char *FILE_NAME = "SpruceRD_BuToHpMuMu_Pip2Kp_smallcut";
const char *MASS_LITERAL = "m(#pi^{+}_{(#rightarrow K^{+})}#mu^{+}#mu^{-})";
struct Hlt1Decision
{
std::string name;
int index;
int flag;
Bool_t value;
std::string GetName() const
{
return TString::Format("Hlt1%sDecision", name.c_str()).Data();
}
Bool_t *GetValuePointer()
{
return &value;
}
};
const int FLAG_TrackMVA = 0b0000100000000000;
const int FLAG_TwoTrackMVA = 0b0001000000000000;
std::vector<Hlt1Decision> Hlt1Decisions{
Hlt1Decision{"DiMuonHighMass", 1, 0b0000000000000001}, // 0001
Hlt1Decision{"DiMuonLowMass", 2, 0b0000000000000010}, // 0002
Hlt1Decision{"DiMuonNoIP", 3, 0b0000000000000100}, // 0004
Hlt1Decision{"DiMuonSoft", 4, 0b0000000000001000}, // 0008
Hlt1Decision{"DisplacedLeptons", 5, 0b0000000000010000}, // 0016
Hlt1Decision{"LowPtDiMuon", 6, 0b0000000000100000}, // 0032
Hlt1Decision{"LowPtMuon", 7, 0b0000000001000000}, // 0064
Hlt1Decision{"OneMuonTrackLine", 8, 0b0000000010000000}, // 0128
Hlt1Decision{"SingleHighEt", 9, 0b0000000100000000}, // 0256
Hlt1Decision{"SingleHighPtMuon", 10, 0b0000001000000000}, // 0512
Hlt1Decision{"TrackMVA", 11, FLAG_TrackMVA}, // 1024
Hlt1Decision{"TrackMuonMVA", 12, 0b0000100000000000}, // 2048
Hlt1Decision{"TwoTrackMVA", 13, FLAG_TwoTrackMVA}, // 4096
};
// 0b0001000000000000
// 0b0001000000110111
void ConnectHlt1Decisions(TChain *chain)
{
for (auto &var : Hlt1Decisions)
{
chain->SetBranchAddress(var.GetName().c_str(), var.GetValuePointer());
}
}
int CombineHlt1DecisionFlags()
{
int result = 0;
for (const auto &var : Hlt1Decisions)
{
if (var.value)
{
result = result | var.flag;
}
}
return result;
}
int analysis_fullstream_bu2hpmumu()
{
TChain *data_chain = new TChain("BuToHpMuMu/DecayTree");
data_chain->Add("/auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/spruce_magdown_2023_v0_tuple_90000000_v0r0p6288631.root");
data_chain->Add("/auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/spruce_magdown_2023_v0r1_tuple_90000000_2023_v0r0p6288631.root");
Float_t B_BPVFDCHI2,
B_BPVIPCHI2,
L1_BPVIPCHI2,
L2_BPVIPCHI2,
L1_PT,
L2_PT,
Jpsi_BPVFDCHI2,
Hp_PT,
Hp_BPVIPCHI2,
Hp_P;
Double_t L1_PID_MU,
L2_PID_MU,
B_CHI2VXNDOF,
Jpsi_MAXDOCACHI2,
Jpsi_CHI2DOF,
Hp_PID_K,
Jpsi_M,
B_M;
Bool_t L1_ISMUON,
L2_ISMUON,
Hlt2RD_BuToKpMuMuDecision,
Hlt2_InclDetDiMuon_4BodyDecision,
Hlt2_InclDetDiMuon_3BodyDecision,
Hlt2_InclDetDiMuonDecision,
Hlt1TrackMVADecision,
Hlt1TwoTrackMVADecision;
UInt_t runNumber;
ULong64_t eventNumber;
data_chain->SetBranchAddress("RUNNUMBER", &runNumber);
data_chain->SetBranchAddress("EVENTNUMBER", &eventNumber);
data_chain->SetBranchAddress("B_M", &B_M);
data_chain->SetBranchAddress("B_BPVFDCHI2", &B_BPVFDCHI2);
data_chain->SetBranchAddress("B_BPVIPCHI2", &B_BPVIPCHI2);
data_chain->SetBranchAddress("L1_BPVIPCHI2", &L1_BPVIPCHI2);
data_chain->SetBranchAddress("L2_BPVIPCHI2", &L2_BPVIPCHI2);
data_chain->SetBranchAddress("L1_PID_MU", &L1_PID_MU);
data_chain->SetBranchAddress("L2_PID_MU", &L2_PID_MU);
data_chain->SetBranchAddress("L1_ISMUON", &L1_ISMUON);
data_chain->SetBranchAddress("L2_ISMUON", &L2_ISMUON);
data_chain->SetBranchAddress("L1_PT", &L1_PT);
data_chain->SetBranchAddress("L2_PT", &L2_PT);
data_chain->SetBranchAddress("B_CHI2VXNDOF", &B_CHI2VXNDOF);
data_chain->SetBranchAddress("Jpsi_MAXDOCACHI2", &Jpsi_MAXDOCACHI2);
data_chain->SetBranchAddress("Jpsi_CHI2DOF", &Jpsi_CHI2DOF);
data_chain->SetBranchAddress("Hp_PT", &Hp_PT);
data_chain->SetBranchAddress("Hp_BPVIPCHI2", &Hp_BPVIPCHI2);
data_chain->SetBranchAddress("Hp_P", &Hp_P);
data_chain->SetBranchAddress("Hp_PID_K", &Hp_PID_K);
data_chain->SetBranchAddress("Jpsi_BPVFDCHI2", &Jpsi_BPVFDCHI2);
data_chain->SetBranchAddress("Jpsi_M", &Jpsi_M);
data_chain->SetBranchAddress("Hlt2RD_BuToKpMuMuDecision", &Hlt2RD_BuToKpMuMuDecision);
data_chain->SetBranchAddress("Hlt2_InclDetDiMuon_4BodyDecision", &Hlt2_InclDetDiMuon_4BodyDecision);
data_chain->SetBranchAddress("Hlt2_InclDetDiMuon_3BodyDecision", &Hlt2_InclDetDiMuon_3BodyDecision);
data_chain->SetBranchAddress("Hlt2_InclDetDiMuonDecision", &Hlt2_InclDetDiMuonDecision);
data_chain->SetBranchAddress("Hlt1TrackMVADecision", &Hlt1TrackMVADecision);
data_chain->SetBranchAddress("Hlt1TwoTrackMVADecision", &Hlt1TwoTrackMVADecision);
Float_t L1_PX, L1_PY, L1_PZ, L1_ENERGY, L2_PX, L2_PY, L2_PZ, L2_ENERGY, Hp_PX, Hp_PY, Hp_PZ, Hp_ENERGY;
data_chain->SetBranchAddress("L1_PX", &L1_PX);
data_chain->SetBranchAddress("L1_PY", &L1_PY);
data_chain->SetBranchAddress("L1_PZ", &L1_PZ);
data_chain->SetBranchAddress("L1_ENERGY", &L1_ENERGY);
data_chain->SetBranchAddress("L2_PX", &L2_PX);
data_chain->SetBranchAddress("L2_PY", &L2_PY);
data_chain->SetBranchAddress("L2_PZ", &L2_PZ);
data_chain->SetBranchAddress("L2_ENERGY", &L2_ENERGY);
data_chain->SetBranchAddress("Hp_PX", &Hp_PX);
data_chain->SetBranchAddress("Hp_PY", &Hp_PY);
data_chain->SetBranchAddress("Hp_PZ", &Hp_PZ);
data_chain->SetBranchAddress("Hp_ENERGY", &Hp_ENERGY);
// ConnectHlt1Decisions(data_chain);
TH1D *h1_B_M_jpsi = new TH1D("h1_B_M_jpsi", "B Mass, J/#psi Mode", nBins, MASS_HIST_MIN, MASS_HIST_MAX);
TH1D *h1_B_M_psi2s = new TH1D("h1_B_M_psi2s", "B Mass, #psi(2S) Mode", nBins, MASS_HIST_MIN, MASS_HIST_MAX);
TH1D *h1_JPsi_M = new TH1D("h1_JPsi_M", "J/#psi", nBins, JPSI_MASS - 100., PSI2S_MASS + 100.);
// TH2D *h2_Hlt1_flags_B_Mass = new TH2D("h2_Hlt1_flags_B_Mass", "Hlt1 Decision vs B Mass", 50, 5100, 5400, 13, 1., 14.);
// TH2D *h2_Hlt1_flags_excl_B_Mass = new TH2D("h2_Hlt1_flags_excl_B_Mass", "Excl Hlt1 Decision vs B Mass", 50, 5100, 5400, 13, 1., 14.);
// h2_Hlt1_flags_B_Mass->SetCanExtend(TH1::kYaxis);
// for (const auto &var : Hlt1Decisions)
// {
// h2_Hlt1_flags_B_Mass->Fill(0., var.name.c_str(), 0.);
// h2_Hlt1_flags_excl_B_Mass->Fill(0., var.name.c_str(), 0.);
// }
std::map<std::pair<unsigned int, unsigned long>, int> run_event_num_dict;
unsigned int entries = data_chain->GetEntries();
unsigned int selected_entries = 0;
for (unsigned int i = 0; i < entries; i++)
{
data_chain->GetEntry(i);
TVector3 K_momentum(Hp_PX, Hp_PY, Hp_PZ);
double K_energy = TMath::Sqrt(TMath::Sq(K_MASS) + K_momentum.Mag2());
TLorentzVector K_4v(K_momentum, K_energy);
TLorentzVector l1_4v(L1_PX, L1_PY, L1_PZ, L1_ENERGY);
TLorentzVector l2_4v(L2_PX, L2_PY, L2_PZ, L2_ENERGY);
Double_t reconstructed_B_Mass = (K_4v + l1_4v + l2_4v).M();
// int combinedFlags = CombineHlt1DecisionFlags();
// bool trackMVADec = (((combinedFlags & FLAG_TrackMVA) == FLAG_TrackMVA) | ((combinedFlags & FLAG_TwoTrackMVA) == FLAG_TwoTrackMVA));
// if (TMath::Abs(Jpsi_M - JPSI_MASS) < 100 && trackMVADec)
// {
// for (const auto &var : Hlt1Decisions)
// {
// if (var.value)
// {
// h2_Hlt1_flags_B_Mass->Fill(reconstructed_B_Mass, var.name.c_str(), 1);
// }
// }
// for (const auto &var : Hlt1Decisions)
// {
// if (var.value)
// {
// h2_Hlt1_flags_excl_B_Mass->Fill(reconstructed_B_Mass, var.name.c_str(), 1);
// break;
// }
// }
// }
// std::cout << "HLT1 Decision Flags: " << combinedFlags << std::endl;
// if (trackMVADec)
// {
// h1_JPsi_M->Fill(Jpsi_M);
// if (TMath::Abs(Jpsi_M - JPSI_MASS) < 100)
// {
// h1_B_M_jpsi->Fill(reconstructed_B_Mass);
// }
// else if (TMath::Abs(Jpsi_M - PSI2S_MASS) < 100)
// {
// h1_B_M_psi2s->Fill(reconstructed_B_Mass);
// }
// }
// if (
// (((B_BPVFDCHI2 > 36) & (B_CHI2VXNDOF < 16) & (B_BPVIPCHI2 < 25) & (Jpsi_MAXDOCACHI2 < 36) & (L1_BPVIPCHI2 > 9) & (L2_BPVIPCHI2 > 9) & (L1_PID_MU > -3) & (L2_PID_MU > -3) & (L1_ISMUON) & (L2_ISMUON) & (L1_PT > 350) & (L2_PT > 350) & (Jpsi_CHI2DOF < 9) & (Jpsi_BPVFDCHI2 > 16) & (Jpsi_M < 5500) & (Hp_PT > 400) & (Hp_BPVIPCHI2 > 6) & (Hp_PT > 400) & (Hp_BPVIPCHI2 > 6) & (Hp_P > 2000) & (Hp_PID_K > -4)) & (Hlt2RD_BuToKpMuMuDecision) & (((Hlt2_InclDetDiMuon_4BodyDecision) | (Hlt2_InclDetDiMuon_3BodyDecision) | (Hlt2_InclDetDiMuonDecision)))))
// {
if ((TMath::Abs(Jpsi_M - 3096.9) < 100) & ((Hlt1TrackMVADecision) | (Hlt1TwoTrackMVADecision)))
{
// check for unique run/event number
selected_entries++;
auto run_event_pair = std::make_pair((unsigned int)runNumber, (unsigned long)eventNumber);
int &run_event_unique_num = run_event_num_dict[run_event_pair];
if (run_event_unique_num)
{
run_event_unique_num = run_event_unique_num + 1;
std::cout << "R: " << runNumber << " / E: " << eventNumber << " / # " << run_event_unique_num
<< ", M = " << reconstructed_B_Mass << " MeV"
<< std::endl;
}
else
{
run_event_unique_num = 1;
h1_B_M_jpsi->Fill(reconstructed_B_Mass);
}
}
// }
if ((i + 1) % 10000 == 0 || i + 1 == entries)
{
std::cout << "["
<< FILE_NAME
<< "] Processed event: " << i + 1 << " (" << TString::Format("%.2f", ((double)(i + 1) / (double)entries) * 100.) << "%)" << std::endl;
}
}
std::cout << "#### " << run_event_num_dict.size() << " out of " << selected_entries << " unique ("
<< TString::Format("%.2f", ((double)(run_event_num_dict.size()) / (double)selected_entries) * 100.) << "%)." << std::endl;
TCanvas *c1 = new TCanvas("c1", "c1", 0, 0, 1400, 600);
c1->Divide(2, 1);
c1->cd(1);
h1_B_M_jpsi->Draw();
c1->cd(2);
h1_B_M_psi2s->Draw();
c1->Draw();
c1->SaveAs(TString::Format("images/root_hist_%s_%s_bmass.pdf", FILE_NAME, "jpsi_psi2s").Data());
TCanvas *c2 = new TCanvas("c2", "c2", 0, 0, 800, 600);
h1_JPsi_M->SetStats(0);
h1_JPsi_M->Draw();
c2->Draw();
// TCanvas *c3 = new TCanvas("c3", "c3", 0, 0, 1400, 600);
// c3->Divide(2, 1, 0.04);
// c3->cd(1);
// //c3->SetLeftMargin(0.40);
// h2_Hlt1_flags_B_Mass->SetStats(0);
// h2_Hlt1_flags_B_Mass->Draw("COLZ");
// c3->cd(2);
// //c3->SetLeftMargin(0.40);
// h2_Hlt1_flags_excl_B_Mass->SetStats(0);
// h2_Hlt1_flags_excl_B_Mass->Draw("COLZ");
// c3->Draw();
// c3->SaveAs(TString::Format("images/root_hist_%s_hlt1dec.pdf", FILE_NAME).Data());
CreateRooFitAndDraw(h1_B_M_jpsi, "jpsi");
// CreateRooFitAndDraw(h1_B_M_psi2s, "psi2s");
// zweiter Plot, welche exklusiv von *einer* HLT1 Line getriggert werden
// DecProd Cut in Decfile von MonteCarlo Production
// --> Lumi * (B Prod Crosssection für B-Prouktion) * (Branching Frcation B->J/Psi K) * (Brranch Fraction J/Psi->mumu) * Effizienz
return 0;
}
void CreateRooFitAndDraw(TH1D *hist, const char *name)
{
auto suffix = [name](const char *text)
{
return TString::Format("%s%s", text, "");
};
RooRealVar roo_var_mass(suffix("var_mass"), "B Mass Variable", MASS_HIST_FIT_MIN, MASS_HIST_FIT_MAX);
roo_var_mass.setRange("fitting_range", MASS_HIST_FIT_MIN, MASS_HIST_FIT_MAX);
TString hist_name = suffix("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(TITLE));
roohist_B_M.plotOn(roo_frame_mass, RooFit::Binning(nBins), RooFit::Name(hist_name));
roo_frame_mass->GetXaxis()->SetTitle(MASS_LITERAL);
// Crystal Ball for Signal
RooRealVar var_mass_x0(suffix("var_mass_x0"), "#mu", 5278., 5170., 5500.);
RooRealVar var_mass_sigmaLR(suffix("var_mass_sigmaLR"), "#sigma_{LR}", 16., 5., 40.);
// Same Variables for Left and Right Tail
// RooRealVar var_mass_alphaL("var_mass_alphaL", "#alpha_{L}", 2., 0., 4.);
// RooRealVar var_mass_nL("var_mass_nL", "n_{L}", 5., 0., 15.);
// RooRealVar var_mass_alphaR("var_mass_alphaR", "#alpha_{R}", 2., 0., 4.);
// RooRealVar var_mass_nR("var_mass_nR", "n_{R}", 5., 0., 15.);
auto var_mass_alphaL = RooRealConstant::value(1.894);
auto var_mass_nL = RooRealConstant::value(1.120);
auto var_mass_alphaR = RooRealConstant::value(2.446);
auto var_mass_nR = RooRealConstant::value(1.146);
RooCrystalBall sig_cb(suffix("sig_cb"), "Signal Crystal Ball", roo_var_mass, var_mass_x0, var_mass_sigmaLR, var_mass_alphaL, var_mass_nL, var_mass_alphaR, var_mass_nR);
// Exponential for Background
RooRealVar var_mass_bkg_c(suffix("var_mass_bkg_c"), "#lambda", -0.0014, -0.004, -0.000);
RooExponential bkg_exp(suffix("bkg_exp"), "Exp Background", roo_var_mass, var_mass_bkg_c);
RooRealVar var_mass_nsig(suffix("nsig"), "Mass N Signal", 0., hist->GetEntries());
RooRealVar var_mass_nbkg(suffix("nbkg"), "Mass N Background", 0., hist->GetEntries());
TString pdf_name = suffix("sigplusbkg");
RooAddPdf sigplusbkg(pdf_name, "Sig and Bkg PDF", RooArgList(sig_cb, bkg_exp), RooArgList(var_mass_nsig, var_mass_nbkg));
// RooFitResult *fitres = sigplusbkg.fitTo(roohist_B_M, RooFit::Save(), RooFit::PrintLevel(1), RooFit::Range("fitting_range"));
// sigplusbkg.plotOn(roo_frame_mass, RooFit::VisualizeError(*fitres, 1), RooFit::FillColor(kOrange + 1), RooFit::FillStyle(3144));
// sigplusbkg.plotOn(roo_frame_mass, RooFit::LineColor(kRed), RooFit::LineStyle(kSolid), RooFit::Range("fitting_range"), RooFit::Name(pdf_name));
// sigplusbkg.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(bkg_exp)), RooFit::LineColor(kBlue - 7), RooFit::LineStyle(kDashed), RooFit::Range("fitting_range"), RooFit::Name("bkg_exp"));
// sigplusbkg.plotOn(roo_frame_mass, RooFit::Components(RooArgSet(sig_cb)), RooFit::LineColor(kRed - 7), RooFit::LineStyle(kDashed), RooFit::Range("fitting_range"), RooFit::Name("sig_cb"));
// RooPlot *roo_frame_pull = roo_var_mass.frame(RooFit::Title("Pull Distribution"));
// roo_frame_pull->addPlotable(roo_frame_mass->pullHist(hist_name, pdf_name), "P");
TCanvas *c = new TCanvas(suffix("roofit_c"), "roofit_c", 0, 0, 800, 600);
// auto *p2 = new TPad(suffix("p2"), "Pull", 0., 0., 1., 0.3);
// p2->Draw();
// p2->SetTopMargin(0.001);
// p2->SetBottomMargin(0.3);
// p2->SetGrid();
// auto *p1 = new TPad(suffix("p1"), "Fit", 0., 0.32, 1., 1.);
// p1->Draw();
// p1->SetBottomMargin(0.001);
// p1->cd();
roo_frame_mass->Draw();
TLegend *leg1 = new TLegend(0.50, 0.80, 0.87, 0.89);
leg1->SetFillColor(kWhite);
leg1->SetLineColor(kBlack);
// leg1->AddEntry(roo_frame_mass->findObject(pdf_name), "Signal + Background", "LP");
// leg1->AddEntry(roo_frame_mass->findObject("sig_cb"), "Signal", "LP");
// leg1->AddEntry(roo_frame_mass->findObject("bkg_exp"), "Background", "LP");
// leg1->AddEntry((TObject *)0, "", "");
// leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_x0.getTitle().Data(), var_mass_x0.getVal(), var_mass_x0.getError()).Data(), "");
// leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", var_mass_sigmaLR.getTitle().Data(), var_mass_sigmaLR.getVal(), var_mass_sigmaLR.getError()).Data(), "");
// leg1->AddEntry((TObject *)0, TString::Format("%s = %.6f #pm %.6f", var_mass_bkg_c.getTitle().Data(), var_mass_bkg_c.getVal(), var_mass_bkg_c.getError()).Data(), "");
// leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", "S", var_mass_nsig.getVal(), var_mass_nsig.getError()).Data(), "");
// leg1->AddEntry((TObject *)0, TString::Format("%s = %.3f #pm %.3f", "B", var_mass_nbkg.getVal(), var_mass_nbkg.getError()).Data(), "");
leg1->AddEntry((TObject *)0, TString::Format("Entries: %.0f", roohist_B_M.sumEntries()), "");
leg1->Draw();
// p2->cd();
// roo_frame_pull->Draw();
c->Draw();
c->SaveAs(TString::Format("images/data/roofit_hist_%s_bmass.pdf", suffix(FILE_NAME).Data()).Data());
}