376 std::cout <<
"[SimpZBiOptimizationProcessor]::process()" << std::endl;
379 std::cout <<
"step_size_ " <<
step_size_ << std::endl;
385 for (
int iteration = 0; iteration <
max_iteration_ + 1; iteration++) {
386 double cutSignal = (double)iteration *
step_size_ * 100.0;
387 cutSignal =
round(cutSignal);
388 if (
debug_) std::cout <<
"## ITERATION " << iteration <<
" ##" << std::endl;
392 if (
debug_) std::cout <<
"[SimpZBiOptimization]::Resetting Histograms" << std::endl;
402 std::string cutname = it->first;
405 std::cout <<
"Saving persistent cut: " << cutname <<
" " << cutvalue << std::endl;
408 processorHistos_->Fill2DHisto(
"persistent_cuts_hh", (
double)cutSignal, (
double)cutid, cutvalue);
413 for (
int e = 0; e <
signalMTT_->GetEntries(); e++) {
425 bkgHistos_->writeHistos(
outFile_,
"background_pct_sig_cut_" + std::to_string(cutSignal));
430 if (iteration == 0) {
433 std::cout <<
"[SimpZBiOptimization]::Integrating initial Signal distributions" << std::endl;
435 std::string cutname = it->first;
439 std::cout <<
"Initial Integral for " << var <<
" is " <<
initialIntegrals_[var] << std::endl;
444 if (
debug_) std::cout <<
"Looping over Signal Test Cuts" << std::endl;
446 std::string cutname = it->first;
449 double cutvalue =
signalHistos_->cutFractionOfSignalVariable(cutvar, isCutGT, iteration * cutFraction,
453 std::cout <<
"Test Cut " << cutname <<
" " <<
testCutsSelector_->getCut(cutname) <<
" cuts "
454 << cutSignal <<
"% of signal distribution in this variable " << std::endl;
459 if (
debug_) std::cout <<
"Filling Background Variables for each Test Cut" << std::endl;
460 for (
int e = 0; e <
bkgMTT_->GetEntries(); e++) {
470 std::string cutname = it->first;
485 if (
debug_) std::cout <<
"Build Signal truth z vs recon z" << std::endl;
486 for (
int e = 0; e <
signalMTT_->GetEntries(); e++) {
494 std::string cutname = it->first;
499 testCutHistos_->Fill2DHisto(
"unc_vtx_z_vs_true_vtx_z_" + cutname +
"_hh",
510 std::string iteration_subdir =
"testCuts_pct_sig_cut_" + std::to_string(cutSignal);
511 TDirectory* dir{
nullptr};
512 std::cout << iteration_subdir.c_str() << std::endl;
513 if (!iteration_subdir.empty()) {
514 dir =
outFile_->mkdir(iteration_subdir.c_str(),
"",
true);
516 double best_zbi = -9999.9;
517 double best_zcut = -9999.9;
518 double best_nsig = -9999.9;
519 double best_nbkg = -9999.9;
520 std::string best_cutname;
521 double best_cutvalue;
524 if (
debug_) std::cout <<
"Calculate ZBi for each Test Cut " << std::endl;
526 std::string cutname = it->first;
530 std::cout <<
"Calculating ZBi for Test Cut " << cutname << std::endl;
531 std::cout <<
"Test Cut ID: " << cutid <<
" | Test Cut Value: " << cutvalue << std::endl;
535 if (
debug_) std::cout <<
"Build Background Model" << std::endl;
537 TF1* bkg_model = (TF1*)
testCutHistos_->fitExponentialTail(
"background_zVtx_" + cutname, start_fit);
539 if (
debug_) std::cout <<
"END Build Background Model" << std::endl;
543 (TH2F*)
testCutHistos_->get2dHisto(
"testCutHistos_unc_vtx_z_vs_true_vtx_z_" + cutname +
"_hh");
549 TGraph* zcutscan_zbi_g =
new TGraph();
550 zcutscan_zbi_g->SetName((
"zcut_vs_zbi_" + cutname +
"_g").c_str());
551 zcutscan_zbi_g->SetTitle((
"zcut_vs_zbi_" + cutname +
"_g;zcut [mm];zbi").c_str());
552 zcutscan_zbi_g->SetMarkerStyle(8);
553 zcutscan_zbi_g->SetMarkerSize(2.0);
554 zcutscan_zbi_g->SetMarkerColor(2);
556 TGraph* zcutscan_nsig_g =
new TGraph();
557 zcutscan_nsig_g->SetName((
"zcut_vs_nsig_" + cutname +
"_g").c_str());
558 zcutscan_nsig_g->SetTitle((
"zcut_vs_nsig_" + cutname +
"_g;zcut [mm];nsig").c_str());
559 zcutscan_nsig_g->SetMarkerStyle(23);
560 zcutscan_nsig_g->SetMarkerSize(2.0);
561 zcutscan_nsig_g->SetMarkerColor(57);
563 TGraph* zcutscan_nbkg_g =
new TGraph();
564 zcutscan_nbkg_g->SetName((
"zcut_vs_nbkg_" + cutname +
"_g").c_str());
565 zcutscan_nbkg_g->SetTitle((
"zcut_vs_nbkg_" + cutname +
"_g;zcut [mm];nbkg").c_str());
566 zcutscan_nbkg_g->SetMarkerStyle(45);
567 zcutscan_nbkg_g->SetMarkerSize(2.0);
568 zcutscan_nbkg_g->SetMarkerColor(49);
570 TGraph* nbkg_zbi_g =
new TGraph();
571 nbkg_zbi_g->SetName((
"nbkg_vs_zbi_" + cutname +
"_g").c_str());
572 nbkg_zbi_g->SetTitle((
"nbkg_vs_zbi_" + cutname +
"_g;nbkg;zbi").c_str());
574 TGraph* nsig_zbi_g =
new TGraph();
575 nsig_zbi_g->SetName((
"nsig_vs_zbi_" + cutname +
"_g").c_str());
576 nsig_zbi_g->SetTitle((
"nsig_vs_zbi_" + cutname +
"_g;nsig;zbi").c_str());
578 TH2F* nsig_zcut_hh =
new TH2F((
"nsig_v_zcut_zbi_" + cutname +
"_hh").c_str(),
579 (
"nsig_v_zcut_zbi_" + cutname +
"_hh; zcut [mm]; Nbkg").c_str(), 200, -50.3,
580 149.7, 3000, 0.0, 300.0);
582 TH2F* nbkg_zcut_hh =
new TH2F((
"nbkg_v_zcut_zbi_" + cutname +
"_hh").c_str(),
583 (
"nbkg_v_zcut_zbi_" + cutname +
"_hh; zcut [mm]; Nbkg").c_str(), 200, -50.3,
584 149.7, 3000, 0.0, 300.0);
588 double zcut_step = 0.1;
589 TH1F* bkg_zVtx_h = (TH1F*)
testCutHistos_->get1dHisto(
"testCutHistos_background_zVtx_" + cutname +
"_h");
590 double max_zcut = bkg_model->GetXmin();
592 bkg_zVtx_h->GetBinLowEdge(bkg_zVtx_h->FindLastBinAbove(0.0)) + bkg_zVtx_h->GetBinWidth(1);
594 double testIntegral = bkg_model->Integral(max_zcut, endIntegral);
596 std::cout <<
"Background between " << max_zcut <<
"and end of histo is " << testIntegral << std::endl;
598 max_zcut = max_zcut + zcut_step;
599 testIntegral = bkg_model->Integral(max_zcut, endIntegral);
601 max_zcut = max_zcut - zcut_step;
602 testIntegral = bkg_model->Integral(max_zcut, endIntegral);
607 std::cout <<
"Maximum Zcut: " << max_zcut <<
" gives " << testIntegral <<
" background events"
611 double min_zcut = bkg_model->GetXmin();
613 std::cout <<
"Minimum Zcut position: " << min_zcut << std::endl;
616 if (
debug_) std::cout <<
"Get signal vtx z selection efficiency before Zcut" << std::endl;
617 TH1F* true_vtx_NoZ_h = (TH1F*)vtx_z_hh->ProjectionY((cutname +
"_" +
"true_vtx_z_projy").c_str(), 1,
618 vtx_z_hh->GetXaxis()->GetNbins(),
"");
622 TH1F* signalSelNoZ_h =
623 (TH1F*)
signalSimZ_h_->Clone((
"testCutHistos_signal_SelNoZ_" + cutname +
"_h").c_str());
624 for (
int i = 0; i < 201; i++) {
625 signalSelNoZ_h->SetBinContent(i, true_vtx_NoZ_h->GetBinContent(i));
627 TEfficiency* effCalcNoZ_h =
new TEfficiency(*signalSelNoZ_h, *
signalSimZ_h_);
629 outFile_->cd((
"testCuts_pct_sig_cut_" + std::to_string(cutSignal)).c_str());
630 signalSelNoZ_h->Write();
631 effCalcNoZ_h->Write();
635 double best_scan_zbi = -999.9;
636 double best_scan_zcut;
637 double best_scan_nsig;
638 double best_scan_nbkg;
639 if (
debug_) std::cout <<
"Scanning zcut position" << std::endl;
640 for (
double zcut = min_zcut; zcut < (max_zcut + zcut_step); zcut = zcut + zcut_step) {
641 double Nbkg = bkg_model->Integral(zcut, endIntegral);
646 TH1F* true_vtx_z_h = (TH1F*)vtx_z_hh->ProjectionY(
647 (std::to_string(zcut) +
"_" + cutname +
"_" +
"true_vtx_z_projy").c_str(),
648 vtx_z_hh->GetXaxis()->FindBin(zcut) + 1, vtx_z_hh->GetXaxis()->GetNbins(),
"");
653 (TH1F*)
signalSimZ_h_->Clone((
"testCutHistos_signal_SelZ_" + cutname +
"_h").c_str());
654 for (
int i = 0; i < 201; i++) {
655 signalSelZ_h->SetBinContent(i, true_vtx_z_h->GetBinContent(i));
660 TEfficiency* effCalc_h =
new TEfficiency(*signalSelZ_h, *
signalSimZ_h_);
666 double eps2 = std::pow(10,
logEps2_);
667 double eps = std::sqrt(eps2);
687 double Nsig = nSigRho + nSigPhi;
706 double n_on = Nsig + Nbkg;
710 std::cout <<
"ZBi before rounding: " << ZBi << std::endl;
724 if (ZBi > best_scan_zbi) {
726 best_scan_zcut = zcut;
727 best_scan_nsig = Nsig;
728 best_scan_nbkg = Nbkg;
732 zcutscan_zbi_g->SetPoint(zcutscan_zbi_g->GetN(), zcut, ZBi);
733 zcutscan_nbkg_g->SetPoint(zcutscan_nbkg_g->GetN(), zcut, Nbkg);
734 zcutscan_nsig_g->SetPoint(zcutscan_nsig_g->GetN(), zcut, Nsig);
735 nbkg_zbi_g->SetPoint(nbkg_zbi_g->GetN(), Nbkg, ZBi);
736 nsig_zbi_g->SetPoint(nsig_zbi_g->GetN(), Nsig, ZBi);
738 nsig_zcut_hh->Fill(zcut, Nsig, ZBi);
739 nbkg_zcut_hh->Fill(zcut, Nbkg, ZBi);
743 writeGraph(
outFile_,
"testCuts_pct_sig_cut_" + std::to_string(cutSignal), zcutscan_zbi_g);
744 writeGraph(
outFile_,
"testCuts_pct_sig_cut_" + std::to_string(cutSignal), zcutscan_nbkg_g);
745 writeGraph(
outFile_,
"testCuts_pct_sig_cut_" + std::to_string(cutSignal), zcutscan_nsig_g);
748 delete zcutscan_zbi_g;
749 delete zcutscan_nsig_g;
750 delete zcutscan_nbkg_g;
757 processorHistos_->Fill2DHisto(
"test_cuts_values_hh", (
double)cutSignal, (
double)cutid, cutvalue);
760 processorHistos_->Fill2DHisto(
"test_cuts_ZBi_hh", (
double)cutSignal, (
double)cutid, best_scan_zbi);
763 processorHistos_->Fill2DHisto(
"test_cuts_zcut_hh", (
double)cutSignal, (
double)cutid, best_scan_zcut);
766 processorHistos_->Fill2DHisto(
"test_cuts_nsig_hh", (
double)cutSignal, (
double)cutid, best_scan_nsig);
769 processorHistos_->Fill2DHisto(
"test_cuts_nbkg_hh", (
double)cutSignal, (
double)cutid, best_scan_nbkg);
773 if (best_scan_zbi > best_zbi) {
774 best_zbi = best_scan_zbi;
775 best_cutname = cutname;
776 best_cutvalue = cutvalue;
777 best_zcut = best_scan_zcut;
778 best_nsig = best_scan_nsig;
779 best_nbkg = best_scan_nbkg;
785 std::cout <<
"Iteration " << iteration <<
" Best Test Cut is " << best_cutname <<
" " << best_cutvalue
786 <<
" with ZBi=" << best_zbi << std::endl;
787 std::cout <<
"Update persistent cuts list with this best test cut..." << std::endl;
788 std::cout <<
"[Persistent Cuts] Before update:" << std::endl;
793 processorHistos_->Fill2DHisto(
"best_test_cut_ZBi_hh", (
double)cutSignal, best_zbi,
795 processorHistos_->Fill1DHisto(
"best_test_cut_ZBi_h", (
double)cutSignal, best_zbi);
796 processorHistos_->Fill1DHisto(
"best_test_cut_zcut_h", (
double)cutSignal, best_zcut);
797 processorHistos_->Fill1DHisto(
"best_test_cut_nsig_h", (
double)cutSignal, best_nsig);
798 processorHistos_->Fill1DHisto(
"best_test_cut_nbkg_h", (
double)cutSignal, best_nbkg);
804 std::cout <<
"[Persistent Cuts] After update:" << std::endl;
810 bkgHistos_->writeHistos(
outFile_,
"background_pct_sig_cut_" + std::to_string(cutSignal));