2024-02-26 16:50:51 +01:00
# include <string>
# include <iostream>
# include <cmath>
# include <algorithm>
# include <filesystem>
# include <string_view>
2024-03-12 16:56:43 +01:00
# include <ctime>
# include <fstream>
2024-02-26 16:50:51 +01:00
2024-02-21 16:34:42 +01:00
# 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"
2024-02-26 16:50:51 +01:00
# include "constants.h"
# include "basic_analysis.h"
# include "hlt1_decision_analysis.h"
# include "bdt_classification.h"
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Bu To Hp Mu Mu %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2024-02-21 16:34:42 +01:00
2024-02-27 15:43:32 +01:00
int new_analysis_bu2hpmumu ( )
2024-02-23 14:46:29 +01:00
{
2024-02-26 16:50:51 +01:00
const char * analysis_name = " BuToHpMuMu " ;
2024-03-10 20:24:41 +01:00
const char * data_tree_name = " SpruceRD_BuToHpMuMu " ;
const char * sim_tree_name = " BuToHpMuMu_noPID_mapped " ;
2024-02-26 16:50:51 +01:00
const char * end_state_mass_literal = " m(#pi^{+}_{(#rightarrow K^{+})}#mu^{+}#mu^{-}) " ;
2024-03-06 16:17:59 +01:00
const bool retrain_bdt = false ;
2024-02-23 14:46:29 +01:00
2024-02-27 15:43:32 +01:00
TChain * data_chain = new TChain ( TString : : Format ( " %s/DecayTree " , data_tree_name ) ) ;
2024-03-10 20:24:41 +01:00
data_chain - > Add ( " /auto/data/pfeiffer/inclusive_detached_dilepton/data_samples/Collision23_Beam6800GeV-VeloClosed-MagDown-Excl-UT_RealData_Sprucing23r1_90000000_RD.root " ) ;
2024-02-23 14:46:29 +01:00
2024-03-10 20:24:41 +01:00
FourVect * l14v_data = FourVect : : Init ( data_chain , " muminus " ) ;
FourVect * l24v_data = FourVect : : Init ( data_chain , " muplus " ) ;
FourVect * hp4v_data = FourVect : : Init ( data_chain , " Kplus " ) ;
2024-02-23 14:46:29 +01:00
2024-02-27 15:43:32 +01:00
TChain * sim_chain = new TChain ( TString : : Format ( " %s/DecayTree " , sim_tree_name ) ) ;
2024-03-10 20:24:41 +01:00
sim_chain - > Add ( " /auto/data/pfeiffer/inclusive_detached_dilepton/MC/BuToHpMuMu_mapped_mc.root " ) ;
2024-02-22 16:53:48 +01:00
2024-03-10 20:24:41 +01:00
FourVect * l14v_sim = FourVect : : Init ( sim_chain , " muminus " ) ;
FourVect * l24v_sim = FourVect : : Init ( sim_chain , " muplus " ) ;
FourVect * hp4v_sim = FourVect : : Init ( sim_chain , " Kplus " ) ;
2024-03-06 16:17:59 +01:00
Double_t B_Mass_jpsi_var , B_Mass_psi2s_var , B_Mass_sim_var ;
TString B_Mass_jpsi_var_name = " B_Mass_jpsi_var " ;
TString B_Mass_psi2s_var_name = " B_Mass_psi2s_var " ;
TString B_Mass_sim_var_name = " B_Mass_sim_var " ;
TTree * tree_B_Mass_jpsi = new TTree ( " tree_B_Mass_jpsi " , TString : : Format ( " B Mass, J/#psi Mode (%s) " , analysis_name ) ) ;
TTree * tree_B_Mass_psi2s = new TTree ( " tree_B_Mass_psi2s " , TString : : Format ( " B Mass, #psi(2S) Mode (%s) " , analysis_name ) ) ;
TTree * tree_B_Mass_sim = new TTree ( " tree_B_Mass_sim " , TString : : Format ( " B Mass, Simualted (%s_noPID) " , analysis_name ) ) ;
tree_B_Mass_jpsi - > Branch ( B_Mass_jpsi_var_name , & B_Mass_jpsi_var ) ;
tree_B_Mass_psi2s - > Branch ( B_Mass_psi2s_var_name , & B_Mass_psi2s_var ) ;
tree_B_Mass_sim - > Branch ( B_Mass_sim_var_name , & B_Mass_sim_var ) ;
TH1D * h1_B_Mass_unf = new TH1D ( " h1_B_Mass_unf " , TString : : Format ( " B Mass (%s), Unfiltered " , data_tree_name ) , B_MASS_HIST_BINS , B_MASS_VAR_MIN , B_MASS_VAR_MAX ) ;
TH1D * h1_B_Mass_bdtf = new TH1D ( " h1_B_Mass_bdtf " , TString : : Format ( " B Mass (%s), BDT Filter " , data_tree_name ) , B_MASS_HIST_BINS , B_MASS_VAR_MIN , B_MASS_VAR_MAX ) ;
2024-02-22 16:53:48 +01:00
2024-02-26 16:50:51 +01:00
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. ) ;
TH1D * h1_bdt_probs = new TH1D ( " h1_bdt_probs " , " BDT Probabilities " , 100 , - 2 , 2 ) ;
2024-02-21 16:34:42 +01:00
2024-03-06 16:17:59 +01:00
h1_B_Mass_unf - > GetXaxis ( ) - > SetTitle ( end_state_mass_literal ) ;
h1_B_Mass_bdtf - > GetXaxis ( ) - > SetTitle ( end_state_mass_literal ) ;
2024-02-26 16:50:51 +01:00
h2_Hlt1_flags_B_Mass - > GetXaxis ( ) - > SetTitle ( end_state_mass_literal ) ;
h2_Hlt1_flags_excl_B_Mass - > GetXaxis ( ) - > SetTitle ( end_state_mass_literal ) ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
ConnectHlt1Decisions ( data_chain , h2_Hlt1_flags_B_Mass , h2_Hlt1_flags_excl_B_Mass ) ;
auto hlt1_decision_histos = CreateHlt1DecisionHistos ( analysis_name ) ;
std : : map < std : : string , int > exclusive_hits { } ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
std : : vector < TV * > vars {
TV : : Float ( " B_PT " , " B_PT " ) ,
TV : : Float ( " B_BPVFDCHI2 " , " B_BPVFDCHI2 " ) ,
TV : : Float ( " B_BPVDIRA " , " B_BPVDIRA " ) ,
TV : : Float ( " Jpsi_BPVIPCHI2 " , " Jpsi_BPVIPCHI2 " ) ,
TV : : Float ( " Jpsi_PT " , " Jpsi_PT " ) ,
2024-03-10 20:24:41 +01:00
TV : : Float ( " Kplus_BPVIPCHI2 " , " Kplus_BPVIPCHI2 " ) ,
TV : : Float ( " Kplus_PT " , " Kplus_PT " ) ,
TV : : Double ( " Kplus_PROBNN_K " , " Kplus_PROBNN_K " ) ,
TV : : Float ( " muminus_BPVIPCHI2 " , " muminus_BPVIPCHI2 " ) ,
TV : : Float ( " muplus_BPVIPCHI2 " , " muplus_BPVIPCHI2 " ) ,
2024-02-26 16:50:51 +01:00
} ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
TTree * sig_tree = new TTree ( " TreeS " , " tree containing signal data " ) ;
TTree * bkg_tree = new TTree ( " TreeB " , " tree containing background data " ) ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
ConnectVarsToData ( vars , data_chain , sim_chain , sig_tree , bkg_tree ) ;
2024-02-21 16:34:42 +01:00
2024-02-29 15:54:28 +01:00
unsigned int data_entries = data_chain - > GetEntries ( ) ;
unsigned int sim_entries = sim_chain - > GetEntries ( ) ;
2024-02-28 15:50:50 +01:00
std : : cout < < " # Got " < < data_entries < < " data and " < < sim_entries < < " simulated events. " < < std : : endl ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
unsigned int bkg_events = 0 ;
2024-02-28 15:50:50 +01:00
unsigned int sig_events = 0 ;
2024-02-22 16:53:48 +01:00
2024-02-26 16:50:51 +01:00
if ( retrain_bdt )
2024-02-22 16:53:48 +01:00
{
2024-02-26 16:50:51 +01:00
std : : cout < < " # Starting with BDT retrain. " < < std : : endl ;
for ( unsigned int i = 0 ; i < data_entries ; i + + )
2024-02-22 16:53:48 +01:00
{
2024-02-26 16:50:51 +01:00
data_chain - > GetEntry ( i ) ;
2024-03-10 20:24:41 +01:00
TLorentzVector dimuon = l14v_data - > LorentzVector ( ) + l24v_data - > LorentzVector ( ) ;
Double_t reconstructed_B_Mass = ( hp4v_data - > LorentzVector ( ) + dimuon ) . M ( ) ;
2024-02-22 16:53:48 +01:00
2024-02-26 16:50:51 +01:00
if ( std : : all_of ( vars . begin ( ) , vars . end ( ) , [ ] ( TV * v )
{ return v - > IsDataFinite ( ) ; } ) )
2024-02-22 16:53:48 +01:00
{
2024-03-10 20:24:41 +01:00
if ( reconstructed_B_Mass > 5500. & & ( ( TMath : : Abs ( dimuon . M ( ) - JPSI_MASS ) < 100. ) | | ( TMath : : Abs ( dimuon . M ( ) - PSI2S_MASS ) < 100. ) ) )
2024-02-26 16:50:51 +01:00
{
bkg_tree - > Fill ( ) ;
bkg_events + + ;
}
2024-02-22 16:53:48 +01:00
}
2024-02-26 16:50:51 +01:00
PrintProgress ( TString : : Format ( " %s BKG Collection " , analysis_name ) , data_entries , 10000 , i ) ;
2024-02-22 16:53:48 +01:00
}
2024-02-28 15:50:50 +01:00
std : : cout < < " # Added " < < bkg_events < < " background events. " < < std : : endl ;
2024-02-22 16:53:48 +01:00
}
2024-02-28 15:50:50 +01:00
else
{
2024-02-26 16:50:51 +01:00
std : : cout < < " # Starting without BDT retrain. " < < std : : endl ;
2024-02-28 15:50:50 +01:00
bkg_events = data_entries ;
2024-02-22 16:53:48 +01:00
}
2024-02-26 16:50:51 +01:00
for ( unsigned int i = 0 ; i < sim_entries ; i + + )
2024-02-22 16:53:48 +01:00
{
2024-02-26 16:50:51 +01:00
sim_chain - > GetEntry ( i ) ;
2024-03-10 20:24:41 +01:00
Double_t reconstructed_B_Mass = ( hp4v_sim - > LorentzVector ( ) + l14v_sim - > LorentzVector ( ) + l24v_sim - > LorentzVector ( ) ) . M ( ) ;
if ( sig_events < bkg_events )
2024-02-23 14:46:29 +01:00
{
2024-03-10 20:24:41 +01:00
if ( retrain_bdt & & std : : all_of ( vars . begin ( ) , vars . end ( ) , [ ] ( TV * v )
{ return v - > IsMCFinite ( ) ; } ) )
2024-02-23 14:46:29 +01:00
{
2024-03-10 20:24:41 +01:00
sig_tree - > Fill ( ) ;
sig_events + + ;
2024-02-23 14:46:29 +01:00
}
2024-02-28 15:50:50 +01:00
}
2024-03-10 20:24:41 +01:00
B_Mass_sim_var = reconstructed_B_Mass ;
tree_B_Mass_sim - > Fill ( ) ;
2024-02-26 16:50:51 +01:00
PrintProgress ( TString : : Format ( " %s SIG Collection " , analysis_name ) , sim_entries , 10000 , i ) ;
2024-02-22 16:53:48 +01:00
}
2024-02-26 16:50:51 +01:00
if ( retrain_bdt )
2024-02-22 16:53:48 +01:00
{
2024-03-10 20:24:41 +01:00
std : : cout < < " # Added " < < sig_events < < " signal events. " < < std : : endl ;
TrainBDT ( vars , analysis_name , sig_tree , bkg_tree ) ;
2024-02-26 16:50:51 +01:00
std : : cout < < " # Finished BDT retrain. " < < std : : endl ;
2024-02-22 16:53:48 +01:00
}
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
std : : cout < < " # Starting evaluation of data. " < < std : : endl ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
Float_t * train_vars = new Float_t [ vars . size ( ) ] ;
2024-03-10 20:24:41 +01:00
auto reader = SetupReader ( vars , train_vars , analysis_name ) ;
2024-02-21 16:34:42 +01:00
2024-03-10 20:24:41 +01:00
const double mva_cut_value = - 0.05 ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
for ( unsigned int i = 0 ; i < data_entries ; i + + )
{
data_chain - > GetEntry ( i ) ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
TLorentzVector dimuon = l14v_data - > LorentzVector ( ) + l24v_data - > LorentzVector ( ) ;
2024-03-10 20:24:41 +01:00
Double_t reconstructed_B_Mass = ( hp4v_data - > LorentzVector ( ) + dimuon ) . M ( ) ;
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
if ( std : : all_of ( vars . begin ( ) , vars . end ( ) , [ ] ( TV * v )
{ return v - > IsDataFinite ( ) ; } ) )
2024-02-21 16:34:42 +01:00
{
2024-02-26 16:50:51 +01:00
for ( size_t j = 0 ; j < vars . size ( ) ; j + + )
{
if ( vars [ j ] - > IsDouble ( ) )
{
train_vars [ j ] = vars [ j ] - > GetDataDouble ( ) ;
}
else if ( vars [ j ] - > IsFloat ( ) )
{
train_vars [ j ] = vars [ j ] - > GetDataFloat ( ) ;
}
}
2024-02-21 16:34:42 +01:00
}
if ( TMath : : Abs ( dimuon . M ( ) - JPSI_MASS ) < 100. )
{
2024-02-22 16:53:48 +01:00
CheckHlt1Decisioins ( h2_Hlt1_flags_B_Mass , h2_Hlt1_flags_excl_B_Mass , exclusive_hits , reconstructed_B_Mass ) ;
FillHlt1DecisionHistos ( hlt1_decision_histos , reconstructed_B_Mass ) ;
}
2024-02-21 16:34:42 +01:00
2024-02-26 16:50:51 +01:00
double mva_response = reader - > EvaluateMVA ( " BDT " ) ;
h1_bdt_probs - > Fill ( mva_response ) ;
2024-03-06 16:17:59 +01:00
h1_B_Mass_unf - > Fill ( reconstructed_B_Mass ) ;
2024-02-26 16:50:51 +01:00
if ( mva_response > mva_cut_value )
2024-02-22 16:53:48 +01:00
{
2024-03-06 16:17:59 +01:00
h1_B_Mass_bdtf - > Fill ( reconstructed_B_Mass ) ;
2024-02-22 16:53:48 +01:00
if ( TMath : : Abs ( dimuon . M ( ) - JPSI_MASS ) < 100. )
2024-02-21 16:34:42 +01:00
{
2024-03-06 16:17:59 +01:00
B_Mass_jpsi_var = reconstructed_B_Mass ;
tree_B_Mass_jpsi - > Fill ( ) ;
2024-02-21 16:34:42 +01:00
}
2024-02-22 16:53:48 +01:00
else if ( TMath : : Abs ( dimuon . M ( ) - PSI2S_MASS ) < 100. )
2024-02-21 16:34:42 +01:00
{
2024-03-06 16:17:59 +01:00
B_Mass_psi2s_var = reconstructed_B_Mass ;
tree_B_Mass_psi2s - > Fill ( ) ;
2024-02-21 16:34:42 +01:00
}
}
2024-02-26 16:50:51 +01:00
PrintProgress ( TString : : Format ( " %s BDT Evaluation " , analysis_name ) , data_entries , 10000 , i ) ;
2024-02-22 16:53:48 +01:00
}
std : : cout < < " # Exclusive Hits " < < std : : endl ;
for ( const auto & [ line , hits ] : exclusive_hits )
{
std : : cout < < line < < " : " < < hits < < std : : endl ;
}
2024-03-06 16:17:59 +01:00
DrawInDefaultCanvas ( h2_Hlt1_flags_B_Mass , analysis_name , 0.16 , " COLZ " ) ;
DrawInDefaultCanvas ( h2_Hlt1_flags_excl_B_Mass , analysis_name , 0.16 , " COLZ " ) ;
DrawInDefaultCanvas ( h1_B_Mass_unf , analysis_name , 0.1 ) ;
DrawInDefaultCanvas ( h1_B_Mass_bdtf , analysis_name , 0.1 ) ;
2024-03-10 14:21:17 +01:00
DrawInDefaultCanvasStacked ( { h1_B_Mass_unf , h1_B_Mass_bdtf } , { kRed , kBlue } , { 0 , 3003 } , analysis_name ) ;
2024-02-22 16:53:48 +01:00
2024-03-06 16:17:59 +01:00
auto roofit_hist_sim = CreateRooDataSetAndFitCB ( tree_B_Mass_sim , B_Mass_sim_var_name , end_state_mass_literal , false , false , ShapeParamters { } ) ;
auto roofit_hist_jpsi_fitsum = CreateRooDataSetAndFitCB ( tree_B_Mass_jpsi , B_Mass_jpsi_var_name , end_state_mass_literal , true , true , roofit_hist_sim . shape_parameters ) ;
2024-03-10 20:24:41 +01:00
auto roofit_hist_psi2s_fitsum = CreateRooDataSetAndFitCB ( tree_B_Mass_psi2s , B_Mass_psi2s_var_name , end_state_mass_literal , true , true , roofit_hist_sim . shape_parameters , true ) ;
2024-02-27 15:43:32 +01:00
DrawInDefaultCanvas ( roofit_hist_jpsi_fitsum , analysis_name ) ;
DrawInDefaultCanvas ( roofit_hist_psi2s_fitsum , analysis_name ) ;
DrawInDefaultCanvas ( roofit_hist_sim , analysis_name ) ;
2024-02-22 16:53:48 +01:00
2024-02-28 15:50:50 +01:00
// DrawHlt1DecisionHistos(analysis_name, hlt1_decision_histos);
2024-02-22 16:53:48 +01:00
2024-02-26 16:50:51 +01:00
DrawBDTProbs ( h1_bdt_probs , mva_cut_value , analysis_name ) ;
2024-03-12 16:56:43 +01:00
auto signal_ratio = DivWithErr ( roofit_hist_psi2s_fitsum . signal_yield . first , roofit_hist_psi2s_fitsum . signal_yield . second , roofit_hist_jpsi_fitsum . signal_yield . first , roofit_hist_jpsi_fitsum . signal_yield . second ) ;
std : : time_t t = std : : time ( nullptr ) ;
std : : tm tm = * std : : localtime ( & t ) ;
ofstream res_file ;
res_file . open ( TString : : Format ( " %s_results.txt " , analysis_name ) . Data ( ) , ios : : out | ios : : trunc ) ;
res_file < < " #### " < < std : : put_time ( & tm , " %c " ) < < " #### " < < std : : endl ;
res_file < < " J/Psi Mode: " < < roofit_hist_jpsi_fitsum . signal_yield . first < < " +- " < < roofit_hist_jpsi_fitsum . signal_yield . second < < std : : endl ;
res_file < < " Psi(2S) Mode: " < < roofit_hist_psi2s_fitsum . signal_yield . first < < " +- " < < roofit_hist_psi2s_fitsum . signal_yield . second < < std : : endl ;
res_file < < " Mode Yield Ratio: " < < signal_ratio . first < < " +- " < < signal_ratio . second < < std : : endl ;
res_file < < " Rel Br Frac MuMu: " < < ( BRF_JPSI_MUMU_VAL / BRF_PSI2S_MUMU_VAL ) < < std : : endl ;
res_file < < " Rel Br Frac: " < < signal_ratio . first * ( BRF_JPSI_MUMU_VAL / BRF_PSI2S_MUMU_VAL ) < < std : : endl ;
res_file . close ( ) ;
2024-02-22 16:53:48 +01:00
return 0 ;
}