382 def compute_metrics(self) -> dict[str, Any]:
383 """Run the full benchmark analysis over all events.
384
385 Returns
386 -------
387 dict
388 Dictionary of metrics compatible with compare_metrics.py format.
389 """
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)
395
396
397 h_dp_over_p = ROOT.TH1D(
398 "h_dp_over_p",
399 "Momentum resolution;(p_{reco} #minus p_{truth}) / p_{truth};Entries",
400 100,
401 -0.5,
402 0.5,
403 )
404 h_dp_vs_p = ROOT.TH2D(
405 "h_dp_vs_p",
406 "Momentum resolution vs p_{truth};p_{truth} [GeV/c];(p_{reco} #minus p_{truth}) / p_{truth}",
407 50,
408 0,
409 120,
410 100,
411 -0.5,
412 0.5,
413 )
414 h_dx = ROOT.TH1D(
415 "h_dx",
416 "x resolution at first hit;x_{reco} #minus x_{truth} [cm];Entries",
417 100,
418 -5.0,
419 5.0,
420 )
421 h_dy = ROOT.TH1D(
422 "h_dy",
423 "y resolution at first hit;y_{reco} #minus y_{truth} [cm];Entries",
424 100,
425 -5.0,
426 5.0,
427 )
428 h_dtx = ROOT.TH1D(
429 "h_dtx",
430 "Slope t_{x} resolution;t_{x,reco} #minus t_{x,truth} (t_{x} = p_{x}/p_{z});Entries",
431 100,
432 -0.01,
433 0.01,
434 )
435 h_dty = ROOT.TH1D(
436 "h_dty",
437 "Slope t_{y} resolution;t_{y,reco} #minus t_{y,truth} (t_{y} = p_{y}/p_{z});Entries",
438 100,
439 -0.01,
440 0.01,
441 )
442 h_hit_residual = ROOT.TH1D(
443 "h_hit_residual",
444 "Unbiased hit residual;drift distance residual [cm];Entries",
445 100,
446 -0.2,
447 0.2,
448 )
449 h_hit_pull = ROOT.TH1D(
450 "h_hit_pull",
451 "Unbiased hit pull;(residual) / #sigma;Entries",
452 100,
453 -5.0,
454 5.0,
455 )
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)
461
462
463 n_reconstructible = 0
464 n_matched_mc = 0
465 n_total_reco = 0
466 n_matched_reco = 0
467 n_clone_reco = 0
468 n_correct_charge = 0
469 n_wrong_charge = 0
470
471 acts_mode = uses_acts(self.reco_tree)
472
473
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
480 )
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
484 )
485
486 for i_event in range(n_events):
487 self.sim_tree.GetEvent(i_event)
488 self.reco_tree.GetEvent(i_event)
489 self._straw_index = self._build_straw_index()
490 self._straw_crossers = defaultdict(set)
491 for point in self.sim_tree.strawtubesPoint:
492 self._straw_crossers[point.GetDetectorID()].add(point.GetTrackID())
493
494
495 reconstructible_ids: set[int] = set()
496 n_mc_tracks = len(self.sim_tree.MCTrack)
497 for mc_id in range(n_mc_tracks):
498 if self._is_reconstructible(mc_id):
499 reconstructible_ids.add(mc_id)
500 p_truth, _, _, _ = self._get_ptruth_first(mc_id)
501 if p_truth > 0:
502 h_p_truth.Fill(p_truth)
503
504 n_reconstructible += len(reconstructible_ids)
505 counts_before = (n_matched_mc, n_total_reco, n_matched_reco, n_clone_reco)
506
507
508 reco_tracks = get_tracks(self.reco_tree)
509 n_reco = reco_tracks.size()
510 n_total_reco += n_reco
511
512
513 matched_mc_this_event: set[int] = set()
514
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:
519 continue
520
521 ndf = info.ndf
522 if ndf <= 0:
523 continue
524 chi2 = info.chi2 / ndf
525 h_chi2ndf.Fill(chi2)
526
527
528 mc_id = self.reco_tree.fitTrack2MC[i_reco]
529
530
531 frac, dominant_id = self._fracMCsame(i_reco)
532 if self.straw_purity:
533 mc_id = dominant_id
534
535 if frac < self.purity_cut:
536
537 continue
538
539 n_matched_reco += 1
540
541 if mc_id in reconstructible_ids:
542 if mc_id in matched_mc_this_event:
543
544 n_clone_reco += 1
545 continue
546 matched_mc_this_event.add(mc_id)
547
548
549 p_truth, _, _, _ = self._get_ptruth_first(mc_id)
550 x_t, y_t, _ = self._get_truth_pos_first(mc_id)
551 tx_t, ty_t = self._get_truth_slopes(mc_id)
552
553 if p_truth > 0 and info.mom is not None and info.pos is not None:
554 p_reco = info.mom.Mag()
555
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)
559
560 h_dx.Fill(info.pos.X() - x_t)
561 h_dy.Fill(info.pos.Y() - y_t)
562
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)
569
570 h_p_matched.Fill(p_truth)
571
572
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)
576 if mc_particle:
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)
581 else:
582 n_wrong_charge += 1
583 h_p_wrong_charge.Fill(p_truth)
584
585
586 if acts_mode:
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)
592 else:
593 n_meas = track.getNumPointsWithMeasurement()
594 for i_hit in range(n_meas):
595 try:
596 tp = track.getPointWithMeasurement(i_hit)
597 fitter_info = tp.getFitterInfo()
598
599 if not fitter_info:
600 continue
601 res = fitter_info.getResidual()
602
603 res_val = res.getState()(0)
604 res_cov = res.getCov()(0, 0)
605 h_hit_residual.Fill(res_val)
606 if res_cov > 0:
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)
610 continue
611
612 n_matched_mc += len(matched_mc_this_event)
613
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"),
620 counts_before,
621 counts_after,
622 strict=True,
623 ):
624 bin_stats[name] += after - before
625
626 vertices_before = (vertex_stats["n_truth_vertices"], vertex_stats["n_found_vertices"])
627 self._evaluate_vertices(reconstructible_ids, vertex_stats)
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]
630
631
632 n_ghost_reco = n_total_reco - n_matched_reco
633
634 efficiency = n_matched_mc / n_reconstructible if n_reconstructible > 0 else None
635 efficiency_unc = wilson_interval(n_matched_mc, n_reconstructible)
636
637 clone_rate = n_clone_reco / n_matched_reco if n_matched_reco > 0 else None
638 clone_rate_unc = wilson_interval(n_clone_reco, n_matched_reco)
639
640 ghost_rate = n_ghost_reco / n_total_reco if n_total_reco > 0 else None
641 ghost_rate_unc = wilson_interval(n_ghost_reco, n_total_reco)
642
643
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)
654
655
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
658 charge_id_eff_unc = wilson_interval(n_correct_charge, n_charge_total)
659
660
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)
674 else:
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
679
680 self.metrics = {
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"},
685 "efficiency": {
686 "value": _round_or_none(efficiency),
687 "uncertainty": _round_or_none(efficiency_unc),
688 "compare": "statistical",
689 },
690 "clone_rate": {
691 "value": _round_or_none(clone_rate),
692 "uncertainty": _round_or_none(clone_rate_unc),
693 "compare": "statistical",
694 },
695 "ghost_rate": {
696 "value": _round_or_none(ghost_rate),
697 "uncertainty": _round_or_none(ghost_rate_unc),
698 "compare": "statistical",
699 },
700 "dp_over_p_sigma": {
701 "value": _round_or_none(dp_p_sigma),
702 "uncertainty": _round_or_none(dp_p_sigma_unc),
703 "compare": "statistical",
704 },
705 "dx_rms": {
706 "value": round(h_dx.GetRMS(), 6),
707 "uncertainty": round(h_dx.GetRMSError(), 6),
708 "compare": "statistical",
709 },
710 "dy_rms": {
711 "value": round(h_dy.GetRMS(), 6),
712 "uncertainty": round(h_dy.GetRMSError(), 6),
713 "compare": "statistical",
714 },
715 "dtx_rms": {
716 "value": round(h_dtx.GetRMS(), 6),
717 "uncertainty": round(h_dtx.GetRMSError(), 6),
718 "compare": "statistical",
719 },
720 "dty_rms": {
721 "value": round(h_dty.GetRMS(), 6),
722 "uncertainty": round(h_dty.GetRMSError(), 6),
723 "compare": "statistical",
724 },
725 "hit_residual_rms": {
726 "value": round(h_hit_residual.GetRMS(), 6),
727 "uncertainty": round(h_hit_residual.GetRMSError(), 6),
728 "compare": "statistical",
729 },
730 "hit_pull_mean": {
731 "value": round(hit_pull_mean, 6),
732 "uncertainty": round(hit_pull_mean_unc, 6),
733 "compare": "statistical",
734 },
735 "hit_pull_sigma": {
736 "value": round(hit_pull_sigma, 6),
737 "uncertainty": round(hit_pull_sigma_unc, 6),
738 "compare": "statistical",
739 },
740 "hit_pull_underflow": {
741 "value": hit_pull_underflow,
742 "compare": "exact",
743 },
744 "hit_pull_overflow": {
745 "value": hit_pull_overflow,
746 "compare": "exact",
747 },
748 "charge_id_efficiency": {
749 "value": _round_or_none(charge_id_eff),
750 "uncertainty": _round_or_none(charge_id_eff_unc),
751 "compare": "statistical",
752 },
753 "n_correct_charge": {"value": int(n_correct_charge), "compare": "exact"},
754 "n_wrong_charge": {"value": int(n_wrong_charge), "compare": "exact"},
755 },
756 "tracking_benchmark_by_multiplicity": {
757 str(multiplicity): self._rates(by_multiplicity[multiplicity])
758 for multiplicity in sorted(by_multiplicity)
759 },
760 "vertexing_benchmark": self._vertex_metrics(vertex_stats),
761 }
762
763 self._histos = {
764 "h_dp_over_p": h_dp_over_p,
765 "h_dp_vs_p": h_dp_vs_p,
766 "h_dx": h_dx,
767 "h_dy": h_dy,
768 "h_dtx": h_dtx,
769 "h_dty": h_dty,
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,
777 }
778
779 return self.metrics
780