383 """Run the full benchmark analysis over all events.
388 Dictionary of metrics compatible with compare_metrics.py format.
390 n_events = self.
sim_tree.GetEntries()
391 n_reco_events = self.
reco_tree.GetEntries()
392 if n_events != n_reco_events:
393 print(f
"WARNING: sim has {n_events} events, reco has {n_reco_events}")
394 n_events = min(n_events, n_reco_events)
397 h_dp_over_p = ROOT.TH1D(
399 "Momentum resolution;(p_{reco} #minus p_{truth}) / p_{truth};Entries",
404 h_dp_vs_p = ROOT.TH2D(
406 "Momentum resolution vs p_{truth};p_{truth} [GeV/c];(p_{reco} #minus p_{truth}) / p_{truth}",
416 "x resolution at first hit;x_{reco} #minus x_{truth} [cm];Entries",
423 "y resolution at first hit;y_{reco} #minus y_{truth} [cm];Entries",
430 "Slope t_{x} resolution;t_{x,reco} #minus t_{x,truth} (t_{x} = p_{x}/p_{z});Entries",
437 "Slope t_{y} resolution;t_{y,reco} #minus t_{y,truth} (t_{y} = p_{y}/p_{z});Entries",
442 h_hit_residual = ROOT.TH1D(
444 "Unbiased hit residual;drift distance residual [cm];Entries",
449 h_hit_pull = ROOT.TH1D(
451 "Unbiased hit pull;(residual) / #sigma;Entries",
456 h_chi2ndf = ROOT.TH1D(
"h_chi2ndf",
"#chi^{2}/ndf;#chi^{2}/ndf;Entries", 100, 0, 20)
457 h_p_truth = ROOT.TH1D(
"h_p_truth",
"p_{truth} (reconstructible);p [GeV/c];Entries", 50, 0, 120)
458 h_p_matched = ROOT.TH1D(
"h_p_matched",
"p_{truth} (matched);p [GeV/c];Entries", 50, 0, 120)
459 h_p_correct_charge = ROOT.TH1D(
"h_p_correct_charge",
"p_{truth} (correct charge);p [GeV/c];Entries", 50, 0, 120)
460 h_p_wrong_charge = ROOT.TH1D(
"h_p_wrong_charge",
"p_{truth} (wrong charge);p [GeV/c];Entries", 50, 0, 120)
463 n_reconstructible = 0
474 by_multiplicity: dict[int, dict[str, int]] = defaultdict(
lambda: defaultdict(int))
475 vertex_stats: dict[str, Any] = defaultdict(int)
476 vertex_stats[
"h_vtx_dx"] = ROOT.TH1D(
"h_vtx_dx",
"Vertex x;x_{reco} #minus x_{truth} [cm];Entries", 1000, -5, 5)
477 vertex_stats[
"h_vtx_dy"] = ROOT.TH1D(
"h_vtx_dy",
"Vertex y;y_{reco} #minus y_{truth} [cm];Entries", 1000, -5, 5)
478 vertex_stats[
"h_vtx_dz"] = ROOT.TH1D(
479 "h_vtx_dz",
"Vertex z;z_{reco} #minus z_{truth} [cm];Entries", 1000, -200, 200
481 vertex_stats[
"h_doca"] = ROOT.TH1D(
"h_doca",
"Vertex candidate DOCA;DOCA [cm];Entries", 200, 0, 10)
482 vertex_stats[
"h_mass_rel"] = ROOT.TH1D(
483 "h_mass_rel",
"Pair mass;(m_{reco} #minus m_{truth}) / m_{truth};Entries", 1000, -0.5, 0.5
486 for i_event
in range(n_events):
491 for point
in self.
sim_tree.strawtubesPoint:
495 reconstructible_ids: set[int] = set()
496 n_mc_tracks = len(self.
sim_tree.MCTrack)
497 for mc_id
in range(n_mc_tracks):
499 reconstructible_ids.add(mc_id)
502 h_p_truth.Fill(p_truth)
504 n_reconstructible += len(reconstructible_ids)
505 counts_before = (n_matched_mc, n_total_reco, n_matched_reco, n_clone_reco)
509 n_reco = reco_tracks.size()
510 n_total_reco += n_reco
513 matched_mc_this_event: set[int] = set()
515 for i_reco
in range(n_reco):
516 track = reco_tracks[i_reco]
517 info = track_info(track, acts_mode)
518 if not info.converged:
524 chi2 = info.chi2 / ndf
528 mc_id = self.
reco_tree.fitTrack2MC[i_reco]
541 if mc_id
in reconstructible_ids:
542 if mc_id
in matched_mc_this_event:
546 matched_mc_this_event.add(mc_id)
553 if p_truth > 0
and info.mom
is not None and info.pos
is not None:
554 p_reco = info.mom.Mag()
556 dp_over_p = (p_reco - p_truth) / p_truth
557 h_dp_over_p.Fill(dp_over_p)
558 h_dp_vs_p.Fill(p_truth, dp_over_p)
560 h_dx.Fill(info.pos.X() - x_t)
561 h_dy.Fill(info.pos.Y() - y_t)
563 pz_reco = info.mom.Z()
564 if abs(pz_reco) > 1e-10:
565 tx_reco = info.mom.X() / pz_reco
566 ty_reco = info.mom.Y() / pz_reco
567 h_dtx.Fill(tx_reco - tx_t)
568 h_dty.Fill(ty_reco - ty_t)
570 h_p_matched.Fill(p_truth)
573 q_reco = track.charge()
if acts_mode
else track.getFittedState().getCharge()
574 mc_pdg = self.
sim_tree.MCTrack[mc_id].GetPdgCode()
575 mc_particle = self.
PDG.GetParticle(mc_pdg)
577 q_truth = mc_particle.Charge() / 3.0
578 if q_reco * q_truth > 0:
579 n_correct_charge += 1
580 h_p_correct_charge.Fill(p_truth)
583 h_p_wrong_charge.Fill(p_truth)
587 for res_val
in track.GetResiduals():
588 h_hit_residual.Fill(res_val)
589 for pull_val
in track.GetPulls():
590 if pull_val != -999.0:
591 h_hit_pull.Fill(pull_val)
593 n_meas = track.getNumPointsWithMeasurement()
594 for i_hit
in range(n_meas):
596 tp = track.getPointWithMeasurement(i_hit)
597 fitter_info = tp.getFitterInfo()
601 res = fitter_info.getResidual()
603 res_val = res.getState()(0)
604 res_cov = res.getCov()(0, 0)
605 h_hit_residual.Fill(res_val)
607 h_hit_pull.Fill(res_val / math.sqrt(res_cov))
608 except (ROOT.genfit.Exception, AttributeError, IndexError)
as e:
609 logger.debug(
"Skipping hit %d: %s", i_hit, e)
612 n_matched_mc += len(matched_mc_this_event)
614 bin_stats = by_multiplicity[len(reconstructible_ids)]
615 bin_stats[
"n_events"] += 1
616 bin_stats[
"n_reconstructible"] += len(reconstructible_ids)
617 counts_after = (n_matched_mc, n_total_reco, n_matched_reco, n_clone_reco)
618 for name, before, after
in zip(
619 (
"n_matched_mc",
"n_total_reco",
"n_matched_reco",
"n_clone_reco"),
624 bin_stats[name] += after - before
626 vertices_before = (vertex_stats[
"n_truth_vertices"], vertex_stats[
"n_found_vertices"])
628 bin_stats[
"n_truth_vertices"] += vertex_stats[
"n_truth_vertices"] - vertices_before[0]
629 bin_stats[
"n_found_vertices"] += vertex_stats[
"n_found_vertices"] - vertices_before[1]
632 n_ghost_reco = n_total_reco - n_matched_reco
634 efficiency = n_matched_mc / n_reconstructible
if n_reconstructible > 0
else None
637 clone_rate = n_clone_reco / n_matched_reco
if n_matched_reco > 0
else None
640 ghost_rate = n_ghost_reco / n_total_reco
if n_total_reco > 0
else None
644 dp_p_sigma: float |
None =
None
645 dp_p_sigma_unc: float |
None =
None
646 if h_dp_over_p.GetEntries() > 0:
647 dp_p_sigma = h_dp_over_p.GetRMS()
648 dp_p_sigma_unc = h_dp_over_p.GetRMSError()
649 if h_dp_over_p.GetEntries() > 20:
650 fit_result = h_dp_over_p.Fit(
"gaus",
"QS")
651 if fit_result
and int(fit_result) == 0:
652 dp_p_sigma = fit_result.Parameter(2)
653 dp_p_sigma_unc = fit_result.ParError(2)
656 n_charge_total = n_correct_charge + n_wrong_charge
657 charge_id_eff = n_correct_charge / n_charge_total
if n_charge_total > 0
else None
661 hit_pull_underflow = int(h_hit_pull.GetBinContent(0))
662 hit_pull_overflow = int(h_hit_pull.GetBinContent(h_hit_pull.GetNbinsX() + 1))
663 if h_hit_pull.GetEntries() > 50:
664 hit_pull_mean = h_hit_pull.GetMean()
665 hit_pull_mean_unc = h_hit_pull.GetMeanError()
666 hit_pull_sigma = h_hit_pull.GetRMS()
667 hit_pull_sigma_unc = h_hit_pull.GetRMSError()
668 pull_fit = h_hit_pull.Fit(
"gaus",
"QS")
669 if pull_fit
and int(pull_fit) == 0:
670 hit_pull_mean = pull_fit.Parameter(1)
671 hit_pull_mean_unc = pull_fit.ParError(1)
672 hit_pull_sigma = pull_fit.Parameter(2)
673 hit_pull_sigma_unc = pull_fit.ParError(2)
675 hit_pull_mean = math.nan
676 hit_pull_mean_unc = math.nan
677 hit_pull_sigma = math.nan
678 hit_pull_sigma_unc = math.nan
681 "tracking_benchmark": {
682 "n_events": {
"value": int(n_events),
"compare":
"exact"},
683 "n_reconstructible": {
"value": int(n_reconstructible),
"compare":
"exact"},
684 "n_total_reco": {
"value": int(n_total_reco),
"compare":
"exact"},
688 "compare":
"statistical",
693 "compare":
"statistical",
698 "compare":
"statistical",
703 "compare":
"statistical",
706 "value": round(h_dx.GetRMS(), 6),
707 "uncertainty": round(h_dx.GetRMSError(), 6),
708 "compare":
"statistical",
711 "value": round(h_dy.GetRMS(), 6),
712 "uncertainty": round(h_dy.GetRMSError(), 6),
713 "compare":
"statistical",
716 "value": round(h_dtx.GetRMS(), 6),
717 "uncertainty": round(h_dtx.GetRMSError(), 6),
718 "compare":
"statistical",
721 "value": round(h_dty.GetRMS(), 6),
722 "uncertainty": round(h_dty.GetRMSError(), 6),
723 "compare":
"statistical",
725 "hit_residual_rms": {
726 "value": round(h_hit_residual.GetRMS(), 6),
727 "uncertainty": round(h_hit_residual.GetRMSError(), 6),
728 "compare":
"statistical",
731 "value": round(hit_pull_mean, 6),
732 "uncertainty": round(hit_pull_mean_unc, 6),
733 "compare":
"statistical",
736 "value": round(hit_pull_sigma, 6),
737 "uncertainty": round(hit_pull_sigma_unc, 6),
738 "compare":
"statistical",
740 "hit_pull_underflow": {
741 "value": hit_pull_underflow,
744 "hit_pull_overflow": {
745 "value": hit_pull_overflow,
748 "charge_id_efficiency": {
751 "compare":
"statistical",
753 "n_correct_charge": {
"value": int(n_correct_charge),
"compare":
"exact"},
754 "n_wrong_charge": {
"value": int(n_wrong_charge),
"compare":
"exact"},
756 "tracking_benchmark_by_multiplicity": {
757 str(multiplicity): self.
_rates(by_multiplicity[multiplicity])
758 for multiplicity
in sorted(by_multiplicity)
764 "h_dp_over_p": h_dp_over_p,
765 "h_dp_vs_p": h_dp_vs_p,
770 "h_hit_residual": h_hit_residual,
771 "h_hit_pull": h_hit_pull,
772 "h_chi2ndf": h_chi2ndf,
773 "h_p_truth": h_p_truth,
774 "h_p_matched": h_p_matched,
775 "h_p_correct_charge": h_p_correct_charge,
776 "h_p_wrong_charge": h_p_wrong_charge,