60 std::cout <<
"ApOptimizationProcessor::initialize with output file " << outFilename << std::endl;
71 "(vertex.invM_ > " + std::to_string(
lowMass_) +
" && vertex.invM_ < " + std::to_string(
highMass_) +
")";
74 std::cout <<
"ApOptimizationProcessor::initialize: Reading Signal AnaVertex Tuple from file "
82 std::cout <<
"ApOptimizationProcessor::initialize: Reading Signal MC Tuple from file "
88 std::cout <<
"ApOptimizationProcessor::initialize: Reading Background AnaVertex Tuple from file "
94 std::cout <<
"ApOptimizationProcessor::initialize: Initializing Set of Persistent Cuts" << std::endl;
100 std::cout <<
"ApOptimizationProcessor::initialize: Initializing Set of Test Cuts" << std::endl;
112 bkgHistos_ = std::make_shared<ZBiHistos>(
"background");
125 std::cout <<
"ApOptimizationProcessor::initialize: Setting up Test Cut PDFs and Quantiles" << std::endl;
129 std::string cut_name = it->first;
142 if (cut_name ==
"pos_z0" || cut_name ==
"ele_z0" || cut_name ==
"min_y0") {
143 std::cout <<
"ApOptimizationProcessor::initialize: Setting up z0/y0 cut quantiles for cut " << cut_name
148 std::string bin_str =
"(" + std::to_string((
int)
zbins_[0]) +
"," + std::to_string(
zbins_[1]) +
"," +
149 std::to_string(
zbins_[2]) +
"," + std::to_string((
int)bins_and_limits_cut_var[0]) +
"," +
150 std::to_string(bins_and_limits_cut_var[1]) +
"," + std::to_string(bins_and_limits_cut_var[2]) +
")";
152 (cut_var +
":vertex.getZ() >> h_pdf2d_signal_cut_" + cut_name + bin_str).c_str(),
154 TH2F* pdf_signal_cut = (TH2F*)gDirectory->Get((
"h_pdf2d_signal_cut_" + cut_name).c_str());
163 delete[] bins_and_limits_cut_var;
166 double initial_cut_value;
172 double initial_cut_frac =
190 std::string mass_bin_str =
"(" + std::to_string((
int)
massbins_[0]) +
"," + std::to_string(
massbins_[1]) +
"," +
200 auto h_data_vtxz_rad_ = (TH1D*)gDirectory->Get(
"h_data_vtxz_rad");
201 h_data_vtxz_rad_->SetTitle((
";z_{vtx} [mm];Events/" + std::to_string(
z_bin_width_) +
"mm").c_str());
207 std::cout <<
"ApOptimizationProcessor::process: Start of processing events" << std::endl;
209 auto t1 = std::chrono::high_resolution_clock::now();
212 std::string cutname = it->first;
216 for (
int iteration = 0; iteration <
max_iteration_; iteration++) {
219 std::string filter =
"";
220 if (cutname ==
"pos_z0" || cutname ==
"ele_z0" || cutname ==
"min_y0") {
223 filter =
"vertex.getZ() * " + std::to_string(alpha) +
" - abs(" + cutvar +
") < " +
224 std::to_string(alpha) +
" * " + std::to_string(zoffset);
228 filter = cutvar +
" < " + std::to_string(cutvalue);
230 filter = cutvar +
" > " + std::to_string(cutvalue);
234 if (
debug_) std::cout <<
"Applying Test Cut " << filter <<
" at iteration " << iteration << std::endl;
239 (
"vertex.getZ() >> h_bkg_vtxz_cut_" + cutname +
"_" + std::to_string(iteration) +
zbin_str_).c_str(),
241 auto h_bkg_vtxz_cut =
242 (TH1D*)gDirectory->Get((
"h_bkg_vtxz_cut_" + cutname +
"_" + std::to_string(iteration)).c_str());
243 h_bkg_vtxz_cut->SetTitle(
244 (
";z_{vtx} [mm];Background Events/" + std::to_string(
z_bin_width_) +
"mm").c_str());
245 h_bkg_vtxz_cut->Sumw2();
248 std::vector<double> zcuts;
254 zcuts =
findZcut(h_bkg_vtxz_cut,
true, iteration);
257 double Nsig_for_best_zcut = 0.0;
258 double Nbkg_for_best_zcut = 0.0;
259 double best_z_cut = 0.0;
260 double ZBi_for_best_zcut = -99.0;
262 for (
double zcut : zcuts) {
263 if (
debug_) std::cout <<
"Using zcut = " << zcut <<
" mm" << std::endl;
266 std::string zcut_filter = tight_cuts +
" && (vertex.getZ() > " + std::to_string(zcut) +
")";
269 (
"vertex.getZ() >> h_signal_vtxz_cut_" + cutname +
"_" + std::to_string(iteration) +
zbin_str_)
271 zcut_filter.c_str());
272 auto h_signal_vtxz_cut =
273 (TH1D*)gDirectory->Get((
"h_signal_vtxz_cut_" + cutname +
"_" + std::to_string(iteration)).c_str());
274 h_signal_vtxz_cut->SetTitle(
275 (
";z_{vtx} [mm];Signal Events/" + std::to_string(
z_bin_width_) +
"mm").c_str());
276 h_signal_vtxz_cut->Sumw2();
280 (TH1F*)h_signal_vtxz_cut->Clone((
"h_chi_" + cutname +
"_" + std::to_string(iteration)).c_str());
284 double Nbkg = h_bkg_vtxz_cut->Integral(h_bkg_vtxz_cut->FindBin(zcut), h_bkg_vtxz_cut->GetNbinsX());
289 std::cout <<
"Expected Signal: " << Nsig <<
", Expected Background: " << Nbkg <<
", ZBi: " << zbi
292 if (zbi > ZBi_for_best_zcut) {
293 ZBi_for_best_zcut = zbi;
295 Nsig_for_best_zcut = Nsig;
296 Nbkg_for_best_zcut = Nbkg;
300 if (cutname ==
"pos_z0" || cutname ==
"ele_z0" || cutname ==
"min_y0") {
304 processorHistos_->getGraph(
"g_nsig_vs_" + cutname)->AddPoint(iteration, Nsig_for_best_zcut);
305 processorHistos_->getGraph(
"g_nbkg_vs_" + cutname)->AddPoint(iteration, Nbkg_for_best_zcut);
306 processorHistos_->getGraph(
"g_zcut_vs_" + cutname)->AddPoint(iteration, best_z_cut);
307 processorHistos_->getGraph(
"g_zbi_vs_" + cutname)->AddPoint(iteration, ZBi_for_best_zcut);
308 processorHistos_->getGraph(
"g_cut_frac_vs_" + cutname)->AddPoint(iteration, cutfraction);
309 processorHistos_->getGraph(
"g_zoffset_vs_" + cutname)->AddPoint(iteration, zoffset);
310 processorHistos_->getGraph(
"g_alpha_vs_" + cutname)->AddPoint(iteration, alpha);
312 processorHistos_->getGraph(
"g_nsig_vs_" + cutname)->AddPoint(cutvalue, Nsig_for_best_zcut);
313 processorHistos_->getGraph(
"g_nbkg_vs_" + cutname)->AddPoint(cutvalue, Nbkg_for_best_zcut);
314 processorHistos_->getGraph(
"g_zcut_vs_" + cutname)->AddPoint(cutvalue, best_z_cut);
315 processorHistos_->getGraph(
"g_zbi_vs_" + cutname)->AddPoint(cutvalue, ZBi_for_best_zcut);
316 processorHistos_->getGraph(
"g_cut_frac_vs_" + cutname)->AddPoint(cutvalue, cutfraction);
321 auto t2 = std::chrono::high_resolution_clock::now();
324 auto time_in_sec = std::chrono::duration_cast<std::chrono::seconds>(t2 - t1);
326 std::cout <<
"ApOptimizationProcessor::process: End of processing events. Time taken: " << time_in_sec.count()
327 <<
" s" << std::endl;
378 std::vector<double> fit_params;
379 TF1* bkg_tail_fit =
new TF1(fitname.c_str(),
"[0]*exp([2]*(x-[1]))", 0.0, 10.0);
380 bkg_tail_fit->SetParameters(1000.0, 30.0, -0.1);
381 h_bkg->Fit(bkg_tail_fit,
"RQ");
382 fit_params.push_back(bkg_tail_fit->GetParameter(0));
383 fit_params.push_back(bkg_tail_fit->GetParameter(1));
384 fit_params.push_back(bkg_tail_fit->GetParameter(2));
389 std::vector<double> zcuts;
396 std::vector<double> fit_params =
fitZBkgTail((TH1*)h_bkg_vtxz_cut,
"bkg_tail_fit" + std::to_string(iteration));
399 slope = fit_params[2];
402 for (
double ztail_events =
min_ztail_events_; ztail_events <= 20; ztail_events += 0.5) {
405 double zcut = z0 - (log(ztail_events) - log(A)) / slope;
406 zcuts.push_back(zcut);
408 double cumulative_bkg = 0.0;
410 cumulative_bkg += h_bkg_vtxz_cut->GetBinContent(bin);
411 if (cumulative_bkg >= 1.0) {
412 double zcut = h_bkg_vtxz_cut->GetBinLowEdge(bin);
413 zcuts.push_back(zcut);
464 std::string persistent_cuts) {
467 std::string drawstring = cut_var +
">>" + cut_name +
"_pdf_h(" + std::to_string((
int)bins_and_limits[0]) +
"," +
468 std::to_string(bins_and_limits[1]) +
"," + std::to_string(bins_and_limits[2]) +
")";
470 signal_tree_->Draw(drawstring.c_str(), persistent_cuts.c_str());
472 TH1F* sig_h = (TH1F*)gDirectory->Get((cut_name +
"_pdf_h").c_str());
474 sig_h->Scale(1. / sig_h->Integral());
475 sig_h->SetTitle((
"; " + cut_var +
"; normalized units").c_str());
478 delete[] bins_and_limits;
483 bool forward =
testCutsSelector_->getCutRange(cut_name).first < -999 ? false :
true;
553 double initial_cut_frac) {
554 double* quantiles =
new double[n_quantiles];
555 double start_frac = 1.0;
558 std::cout <<
"ApOptimizationProcessor::getQuantileArray: Initial cut fraction = " << initial_cut_frac
560 if (initial_cut_frac > -999.) {
562 start_frac = initial_cut_frac - (n_quantiles / 2) *
step_size_;
564 std::cout <<
"ApOptimizationProcessor::getQuantileArray: Start at fraction = " << start_frac << std::endl;
565 for (
int i = 0; i < n_quantiles; i++) {
573 if (range.first < -999.) {
574 for (
int i = 0; i < n_quantiles; i++) {
575 quantiles[i] = 1 - quantiles[i];
579 if (range.first < -999.) {
580 for (
int i = 0; i < n_quantiles; i++) {
583 }
else if (range.second > 999.) {
584 for (
int i = 0; i < n_quantiles; i++) {
594 double* quantiles =
getQuantileArray(std::make_pair(0, 9999.9), n_quantiles);
595 std::vector<std::pair<double, double>> zoffset_alpha;
597 for (
int q = 0; q < n_quantiles; q++) {
598 TGraphErrors* g_y0_vs_z =
599 processorHistos_->configureGraph((
"g_y0_vs_z_q" + std::to_string(q)).c_str(),
"z_{vtx} [mm]",
"y_{0} [mm]");
603 std::cout <<
"ApOptimizationProcessor::getZoffsetAlpha: Number of z bins = "
604 << int(h_y0_vs_z->GetNbinsX() / nbins) << std::endl;
606 for (
int i = 0; i < int(h_y0_vs_z->GetNbinsX() / nbins); i++) {
607 auto h_proj = h_y0_vs_z->ProjectionY((
"h_y0_vs_z_py_" + std::to_string(i)).c_str(), nbins * i + 1, nbins * (i + 1));
609 if (h_proj->GetEntries() < 50) {
611 std::cout <<
"ApOptimizationProcessor::getZoffsetAlpha: Skipping z slice " << i
612 <<
" with entries = " << h_proj->GetEntries() << std::endl;
617 double zval = ((TAxis*)h_y0_vs_z->GetXaxis())->GetBinCenter(nbins * (i + 0.5));
619 double* quantile_pos =
new double[n_quantiles];
620 h_proj->GetQuantiles(n_quantiles, quantile_pos, quantiles);
621 for (
int q = 0; q < n_quantiles; q++) {
623 TGraphErrors* g =
processorHistos_->getGraph((
"g_y0_vs_z_q" + std::to_string(q)).c_str());
625 g->SetPoint(n, zval, quantile_pos[q]);
626 g->SetPointError(n, 0.0, 0.0);
632 for (
int q = 0; q < n_quantiles; q++) {
634 zoffset_alpha.push_back(std::make_pair(0.0, 0.0));
637 TF1* f1 =
new TF1((
"f1_y0_vs_z_q" + std::to_string(q)).c_str(),
"[0] + [1]*x", -0.5, 15);
638 processorHistos_->getGraph((
"g_y0_vs_z_q" + std::to_string(q)).c_str())->Fit(f1,
"QRS");
640 double zoffset = -f1->GetParameter(0) / f1->GetParameter(1);
641 zoffset_alpha.push_back(std::make_pair(zoffset, f1->GetParameter(1)));
643 std::cout <<
"Quantile " << q <<
" : zoffset = " << zoffset <<
" alpha = " << f1->GetParameter(1)
646 return zoffset_alpha;