399 std::cout <<
"[SimpZBiOptimizationProcessor]::process()" << std::endl;
402 std::cout <<
"step_size_ " <<
step_size_ << std::endl;
409 for(
int iteration = 0; iteration <
max_iteration_+1; iteration ++){
410 double cutSignal = (double)iteration*
step_size_*100.0;
411 cutSignal =
round(cutSignal);
412 if(
debug_) std::cout <<
"## ITERATION " << iteration <<
" ##" << std::endl;
416 if(
debug_) std::cout <<
"[SimpZBiOptimization]::Resetting Histograms" << std::endl;
426 std::string cutname = it->first;
429 std::cout <<
"Saving persistent cut: " << cutname <<
" " << cutvalue << std::endl;
433 (double)cutid,cutvalue);
451 bkgHistos_->writeHistos(
outFile_,
"background_pct_sig_cut_"+std::to_string(cutSignal));
459 std::cout <<
"[SimpZBiOptimization]::Integrating initial Signal distributions" << std::endl;
461 std::string cutname = it->first;
465 std::cout <<
"Initial Integral for " << var <<
" is " <<
initialIntegrals_[var] << std::endl;
470 if(
debug_) std::cout <<
"Looping over Signal Test Cuts" << std::endl;
472 std::string cutname = it->first;
475 double cutvalue =
signalHistos_->cutFractionOfSignalVariable(cutvar, isCutGT,
480 " cuts " << cutSignal <<
"% of signal distribution in this variable " << std::endl;
485 if(
debug_) std::cout <<
"Filling Background Variables for each Test Cut" << std::endl;
497 std::string cutname = it->first;
513 if(
debug_) std::cout <<
"Build Signal truth z vs recon z" << std::endl;
523 std::string cutname = it->first;
529 testCutHistos_->Fill2DHisto(
"unc_vtx_z_vs_true_vtx_z_"+cutname+
"_hh",
539 std::string iteration_subdir =
"testCuts_pct_sig_cut_"+std::to_string(cutSignal);
540 TDirectory* dir{
nullptr};
541 std::cout<<iteration_subdir.c_str()<<std::endl;
542 if (!iteration_subdir.empty()) {
543 dir =
outFile_->mkdir(iteration_subdir.c_str(),
"",
true);
545 double best_zbi = -9999.9;
546 double best_zcut = -9999.9;
547 double best_nsig = -9999.9;
548 double best_nbkg = -9999.9;
549 std::string best_cutname;
550 double best_cutvalue;
553 if(
debug_) std::cout <<
"Calculate ZBi for each Test Cut " << std::endl;
555 std::string cutname = it->first;
559 std::cout <<
"Calculating ZBi for Test Cut " << cutname << std::endl;
560 std::cout <<
"Test Cut ID: " << cutid <<
" | Test Cut Value: " << cutvalue << std::endl;
564 if(
debug_) std::cout <<
"Build Background Model" << std::endl;
566 TF1* bkg_model = (TF1*)
testCutHistos_->fitExponentialTail(
"background_zVtx_"+cutname, start_fit);
568 if(
debug_) std::cout <<
"END Build Background Model" << std::endl;
571 TH2F* vtx_z_hh = (TH2F*)
testCutHistos_->get2dHisto(
"testCutHistos_unc_vtx_z_vs_true_vtx_z_"+cutname+
"_hh");
577 TGraph* zcutscan_zbi_g =
new TGraph();
578 zcutscan_zbi_g->SetName((
"zcut_vs_zbi_"+cutname+
"_g").c_str());
579 zcutscan_zbi_g->SetTitle((
"zcut_vs_zbi_"+cutname+
"_g;zcut [mm];zbi").c_str());
580 zcutscan_zbi_g->SetMarkerStyle(8);
581 zcutscan_zbi_g->SetMarkerSize(2.0);
582 zcutscan_zbi_g->SetMarkerColor(2);
584 TGraph* zcutscan_nsig_g =
new TGraph();
585 zcutscan_nsig_g->SetName((
"zcut_vs_nsig_"+cutname+
"_g").c_str());
586 zcutscan_nsig_g->SetTitle((
"zcut_vs_nsig_"+cutname+
"_g;zcut [mm];nsig").c_str());
587 zcutscan_nsig_g->SetMarkerStyle(23);
588 zcutscan_nsig_g->SetMarkerSize(2.0);
589 zcutscan_nsig_g->SetMarkerColor(57);
591 TGraph* zcutscan_nbkg_g =
new TGraph();
592 zcutscan_nbkg_g->SetName((
"zcut_vs_nbkg_"+cutname+
"_g").c_str());
593 zcutscan_nbkg_g->SetTitle((
"zcut_vs_nbkg_"+cutname+
"_g;zcut [mm];nbkg").c_str());
594 zcutscan_nbkg_g->SetMarkerStyle(45);
595 zcutscan_nbkg_g->SetMarkerSize(2.0);
596 zcutscan_nbkg_g->SetMarkerColor(49);
598 TGraph* nbkg_zbi_g =
new TGraph();
599 nbkg_zbi_g->SetName((
"nbkg_vs_zbi_"+cutname+
"_g").c_str());
600 nbkg_zbi_g->SetTitle((
"nbkg_vs_zbi_"+cutname+
"_g;nbkg;zbi").c_str());
602 TGraph* nsig_zbi_g =
new TGraph();
603 nsig_zbi_g->SetName((
"nsig_vs_zbi_"+cutname+
"_g").c_str());
604 nsig_zbi_g->SetTitle((
"nsig_vs_zbi_"+cutname+
"_g;nsig;zbi").c_str());
606 TH2F* nsig_zcut_hh =
new TH2F((
"nsig_v_zcut_zbi_"+cutname+
"_hh").c_str(),
607 (
"nsig_v_zcut_zbi_"+cutname+
"_hh; zcut [mm]; Nbkg").c_str(),
608 200,-50.3,149.7,3000,0.0,300.0);
610 TH2F* nbkg_zcut_hh =
new TH2F((
"nbkg_v_zcut_zbi_"+cutname+
"_hh").c_str(),
611 (
"nbkg_v_zcut_zbi_"+cutname+
"_hh; zcut [mm]; Nbkg").c_str(),
612 200,-50.3,149.7,3000,0.0,300.0);
617 double zcut_step = 0.1;
618 TH1F* bkg_zVtx_h = (TH1F*)
testCutHistos_->get1dHisto(
"testCutHistos_background_zVtx_"+cutname+
"_h");
619 double max_zcut = bkg_model->GetXmin();
620 double endIntegral = bkg_zVtx_h->GetBinLowEdge(bkg_zVtx_h->FindLastBinAbove(0.0)) + bkg_zVtx_h->GetBinWidth(1);
622 double testIntegral = bkg_model->Integral(max_zcut, endIntegral);
623 if(
debug_) std::cout <<
"Background between " << max_zcut <<
"and end of histo is " << testIntegral << std::endl;
625 max_zcut = max_zcut+zcut_step;
626 testIntegral = bkg_model->Integral(max_zcut, endIntegral);
628 max_zcut = max_zcut-zcut_step;
629 testIntegral = bkg_model->Integral(max_zcut, endIntegral);
633 if(
debug_) std::cout <<
"Maximum Zcut: " << max_zcut <<
" gives " << testIntegral <<
" background events" << std::endl;
636 double min_zcut = bkg_model->GetXmin();
639 std::cout <<
"Minimum Zcut position: " << min_zcut << std::endl;
642 if(
debug_) std::cout <<
"Get signal vtx z selection efficiency before Zcut" << std::endl;
643 TH1F* true_vtx_NoZ_h = (TH1F*)vtx_z_hh->ProjectionY((cutname+
"_"+
"true_vtx_z_projy").c_str(),1,vtx_z_hh->GetXaxis()->GetNbins(),
"");
647 TH1F* signalSelNoZ_h =
648 (TH1F*)
signalSimZ_h_->Clone((
"testCutHistos_signal_SelNoZ_"+cutname+
"_h").c_str());
649 for(
int i=0; i<201; i++){
650 signalSelNoZ_h->SetBinContent(i,true_vtx_NoZ_h->GetBinContent(i));
652 TEfficiency* effCalcNoZ_h =
new TEfficiency(*signalSelNoZ_h, *
signalSimZ_h_);
654 outFile_->cd((
"testCuts_pct_sig_cut_"+std::to_string(cutSignal)).c_str());
655 signalSelNoZ_h->Write();
656 effCalcNoZ_h->Write();
660 double best_scan_zbi = -999.9;
661 double best_scan_zcut;
662 double best_scan_nsig;
663 double best_scan_nbkg;
664 if(
debug_) std::cout <<
"Scanning zcut position" << std::endl;
665 for(
double zcut = min_zcut; zcut < (max_zcut+zcut_step); zcut = zcut+zcut_step){
666 double Nbkg = bkg_model->Integral(zcut,endIntegral);
671 TH1F* true_vtx_z_h = (TH1F*)vtx_z_hh->ProjectionY((std::to_string(zcut)+
"_"+cutname+
"_"+
"true_vtx_z_projy").c_str(),vtx_z_hh->GetXaxis()->FindBin(zcut)+1,vtx_z_hh->GetXaxis()->GetNbins(),
"");
676 (TH1F*)
signalSimZ_h_->Clone((
"testCutHistos_signal_SelZ_"+cutname+
"_h").c_str());
677 for(
int i=0; i<201; i++){
678 signalSelZ_h->SetBinContent(i,true_vtx_z_h->GetBinContent(i));
683 TEfficiency* effCalc_h =
new TEfficiency(*signalSelZ_h, *
signalSimZ_h_);
689 double eps2 = std::pow(10,
logEps2_);
690 double eps = std::sqrt(eps2);
710 double Nsig = nSigRho + nSigPhi;
730 double n_on = Nsig + Nbkg;
734 std::cout <<
"ZBi before rounding: " << ZBi << std::endl;
748 if(ZBi > best_scan_zbi){
750 best_scan_zcut = zcut;
751 best_scan_nsig = Nsig;
752 best_scan_nbkg = Nbkg;
756 zcutscan_zbi_g->SetPoint(zcutscan_zbi_g->GetN(),zcut, ZBi);
757 zcutscan_nbkg_g->SetPoint(zcutscan_nbkg_g->GetN(),zcut, Nbkg);
758 zcutscan_nsig_g->SetPoint(zcutscan_nsig_g->GetN(),zcut, Nsig);
759 nbkg_zbi_g->SetPoint(nbkg_zbi_g->GetN(),Nbkg, ZBi);
760 nsig_zbi_g->SetPoint(nsig_zbi_g->GetN(),Nsig, ZBi);
762 nsig_zcut_hh->Fill(zcut,Nsig,ZBi);
763 nbkg_zcut_hh->Fill(zcut,Nbkg,ZBi);
775 delete zcutscan_zbi_g;
776 delete zcutscan_nsig_g;
777 delete zcutscan_nbkg_g;
784 processorHistos_->Fill2DHisto(
"test_cuts_values_hh",(
double)cutSignal, (
double)cutid,cutvalue);
787 processorHistos_->Fill2DHisto(
"test_cuts_ZBi_hh",(
double)cutSignal, (
double)cutid,best_scan_zbi);
790 processorHistos_->Fill2DHisto(
"test_cuts_zcut_hh",(
double)cutSignal, (
double)cutid,best_scan_zcut);
793 processorHistos_->Fill2DHisto(
"test_cuts_nsig_hh",(
double)cutSignal, (
double)cutid,best_scan_nsig);
796 processorHistos_->Fill2DHisto(
"test_cuts_nbkg_hh",(
double)cutSignal, (
double)cutid,best_scan_nbkg);
800 if(best_scan_zbi > best_zbi){
801 best_zbi = best_scan_zbi;
802 best_cutname = cutname;
803 best_cutvalue = cutvalue;
804 best_zcut = best_scan_zcut;
805 best_nsig = best_scan_nsig;
806 best_nbkg = best_scan_nbkg;
812 std::cout <<
"Iteration " << iteration <<
" Best Test Cut is " << best_cutname
813 <<
" " << best_cutvalue <<
" with ZBi=" << best_zbi << std::endl;
814 std::cout <<
"Update persistent cuts list with this best test cut..." << std::endl;
815 std::cout <<
"[Persistent Cuts] Before update:" << std::endl;
820 processorHistos_->Fill2DHisto(
"best_test_cut_ZBi_hh",(
double)cutSignal, best_zbi,
822 processorHistos_->Fill1DHisto(
"best_test_cut_ZBi_h",(
double)cutSignal, best_zbi);
823 processorHistos_->Fill1DHisto(
"best_test_cut_zcut_h",(
double)cutSignal, best_zcut);
824 processorHistos_->Fill1DHisto(
"best_test_cut_nsig_h",(
double)cutSignal, best_nsig);
825 processorHistos_->Fill1DHisto(
"best_test_cut_nbkg_h",(
double)cutSignal, best_nbkg);
832 std::cout <<
"[Persistent Cuts] After update:" << std::endl;
838 bkgHistos_->writeHistos(
outFile_,
"background_pct_sig_cut_"+std::to_string(cutSignal));