536def myEventLoop(n: int) -> None:
537 rc = sTree.GetEntry(n)
538
539 measCut = measCutFK
540 if sTree.GetBranch("FitTracks_PR"):
541 sTree.FitTracks = sTree.FitTracks_PR
542 measCut = measCutPR
543 if sTree.GetBranch("fitTrack2MC_PR"):
544 sTree.fitTrack2MC = sTree.fitTrack2MC_PR
545 if sTree.GetBranch("Particles_PR"):
546 sTree.Particles = sTree.Particles_PR
547 if not checkHNLorigin(sTree):
548 return
549 if not len(sTree.MCTrack) > 1:
550 wg = 1.0
551 else:
552 wg = sTree.MCTrack[1].GetWeight()
553 if not wg > 0.0:
554 wg = 1.0
555
556
557 hitlist = {}
558 for ahit in sTree.strawtubesPoint:
559 detID = ahit.GetDetectorID()
560 top = ROOT.TVector3()
561 bot = ROOT.TVector3()
562 modules["strawtubes"].StrawEndPoints(detID, bot, top)
563 dw = ahit.dist2Wire()
564 if detID < 50000000:
565 if abs(top.y()) == abs(bot.y()):
566 h["disty"].Fill(dw)
567 if abs(top.y()) > abs(bot.y()):
568 h["distu"].Fill(dw)
569 if abs(top.y()) < abs(bot.y()):
570 h["distv"].Fill(dw)
571
572 trID = ahit.GetTrackID()
573 if not trID < 0:
574 if trID in hitlist:
575 hitlist[trID] += 1
576 else:
577 hitlist[trID] = 1
578 for tr in hitlist:
579 h["meanhits"].Fill(hitlist[tr])
580 key = -1
581 fittedTracks = {}
582 for atrack in sTree.FitTracks:
583 key += 1
584
585 if not checkFiducialVolume(sTree, key, dy):
586 continue
587 fitStatus = atrack.getFitStatus()
588 nmeas = fitStatus.getNdf()
589 h["meas"].Fill(nmeas)
590 if not fitStatus.isFitConverged():
591 continue
592 h["meas2"].Fill(nmeas)
593 if nmeas < measCut:
594 continue
595 fittedTracks[key] = atrack
596
597 rchi2 = fitStatus.getChi2()
598 prob = ROOT.TMath.Prob(rchi2, int(nmeas))
599 h["prob"].Fill(prob)
600 chi2 = rchi2 / nmeas
601 fittedState = atrack.getFittedState()
602 h["chi2"].Fill(chi2, wg)
603 h["measVSchi2"].Fill(atrack.getNumPoints(), chi2)
604 P = fittedState.getMomMag()
605 Px, Py, Pz = fittedState.getMom().x(), fittedState.getMom().y(), fittedState.getMom().z()
606 cov = fittedState.get6DCov()
607 if len(sTree.fitTrack2MC) - 1 < key:
608 continue
609 mcPartKey = sTree.fitTrack2MC[key]
610 if mcPartKey < 0 or mcPartKey >= len(sTree.MCTrack):
611 continue
612 mcPart = sTree.MCTrack[mcPartKey]
613 mcPart.GetP()
614 mcPart.GetPz()
615
616 Ptruth, Ptruthx, Ptruthy, Ptruthz = getPtruthFirst(sTree, mcPartKey)
617 delPOverP = (Ptruth - P) / Ptruth
618 h["delPOverP"].Fill(Ptruth, delPOverP)
619 delPOverPz = (1.0 / Ptruthz - 1.0 / Pz) * Ptruthz
620 h["pullPOverPx"].Fill(Ptruth, (Ptruthx - Px) / ROOT.TMath.Sqrt(cov[3][3]))
621 h["pullPOverPy"].Fill(Ptruth, (Ptruthy - Py) / ROOT.TMath.Sqrt(cov[4][4]))
622 h["pullPOverPz"].Fill(Ptruth, (Ptruthz - Pz) / ROOT.TMath.Sqrt(cov[5][5]))
623 h["delPOverPz"].Fill(Ptruthz, delPOverPz)
624 if chi2 > chi2CutOff:
625 continue
626 h["delPOverP2"].Fill(Ptruth, delPOverP)
627 h["delPOverP2z"].Fill(Ptruth, delPOverPz)
628
629 trackDir = fittedState.getDir()
630 trackPos = fittedState.getPos()
631 vx = ROOT.TVector3()
632 mcPart.GetStartVertex(vx)
633 t = 0
634 for i in range(3):
635 t += trackDir(i) * (vx(i) - trackPos(i))
636 dist = 0
637 for i in range(3):
638 dist += (vx(i) - trackPos(i) - t * trackDir(i)) ** 2
639 dist = ROOT.TMath.Sqrt(dist)
640 h["IP"].Fill(dist)
641
642
643 for HNL in sTree.Particles:
644 t1, t2 = HNL.GetDaughter(0), HNL.GetDaughter(1)
645
646 if not checkFiducialVolume(sTree, t1, dy) or not checkFiducialVolume(sTree, t2, dy):
647 continue
648 checkMeasurements = True
649
650 for tr in [t1, t2]:
651 fitStatus = sTree.FitTracks[tr].getFitStatus()
652 nmeas = fitStatus.getNdf()
653 if nmeas < measCut:
654 checkMeasurements = False
655 if not checkMeasurements:
656 continue
657
658 if not match2HNL(HNL):
659 continue
660 HNLPos = ROOT.TLorentzVector()
661 HNL.ProductionVertex(HNLPos)
662 HNLMom = ROOT.TLorentzVector()
663 HNL.Momentum(HNLMom)
664 doca = HNL.GetDoca()
665
666
667
668
669 if not isInFiducial(HNLPos.X(), HNLPos.Y(), HNLPos.Z()):
670 continue
671 h["Doca"].Fill(doca)
672 if doca > docaCut:
673 continue
674 tr = ROOT.TVector3(0, 0, ShipGeo.target.z0)
675
676
677 """
678 for pi0 in pi0Reco.findPi0(sTree,HNLPos):
679 rc = h['pi0Mass'].Fill(pi0.M())
680 if abs(pi0.M()-0.135)>0.02: continue
681
682 HNLwithPi0 = HNLMom + pi0
683 dist = ImpactParameter(tr,HNLPos,HNLwithPi0)
684 mass = HNLwithPi0.M()
685 h['IP0_pi0'].Fill(dist)
686 h['IP0/mass_pi0'].Fill(mass,dist)
687 h['HNL_pi0'].Fill(mass)
688 """
689
690 dist = ImpactParameter(tr, HNLPos, HNLMom)
691 mass = HNLMom.M()
692 h["IP0"].Fill(dist)
693 h["IP0/mass"].Fill(mass, dist)
694 h["HNL"].Fill(mass)
695 h["HNLw"].Fill(mass, wg)
696
697 vetoDets["SBT"] = veto.SBT_decision()
698 vetoDets["UBT"] = veto.UBT_decision()
699 vetoDets["TRA"] = veto.Track_decision()
700 h["nrtracks"].Fill(vetoDets["TRA"][2])
701 h["nrSBT"].Fill(vetoDets["SBT"][2])
702
703 mcTrackIdx = sTree.fitTrack2MC[t1]
704 if mcTrackIdx < 0 or mcTrackIdx >= len(sTree.MCTrack):
705 continue
706 mctrack = sTree.MCTrack[mcTrackIdx]
707 h["Vzresol"].Fill((mctrack.GetStartZ() - HNLPos.Z()) / u.cm)
708 h["Vxresol"].Fill((mctrack.GetStartX() - HNLPos.X()) / u.cm)
709 h["Vyresol"].Fill((mctrack.GetStartY() - HNLPos.Y()) / u.cm)
710 PosDir, newPosDir, CovMat, _scalFac = {}, {}, {}, {}
711
712 newPos = ROOT.TVector3(HNLPos.X(), HNLPos.Y(), HNLPos.Z())
713 st1, st2 = sTree.FitTracks[t1].getFittedState(), sTree.FitTracks[t2].getFittedState()
714 PosDir[t1] = {"position": st1.getPos(), "direction": st1.getDir(), "momentum": st1.getMom()}
715 PosDir[t2] = {"position": st2.getPos(), "direction": st2.getDir(), "momentum": st2.getMom()}
716 CovMat[t1] = st1.get6DCov()
717 CovMat[t2] = st2.get6DCov()
718 rep1, rep2 = ROOT.genfit.RKTrackRep(st1.getPDG()), ROOT.genfit.RKTrackRep(st2.getPDG())
719 state1, state2 = ROOT.genfit.StateOnPlane(rep1), ROOT.genfit.StateOnPlane(rep2)
720 rep1.setPosMom(state1, st1.getPos(), st1.getMom())
721 rep2.setPosMom(state2, st2.getPos(), st2.getMom())
722 try:
723 rep1.extrapolateToPoint(state1, newPos, False)
724 rep2.extrapolateToPoint(state2, newPos, False)
725 mom1, mom2 = rep1.getMom(state1), rep2.getMom(state2)
726 except Exception:
727 mom1, mom2 = st1.getMom(), st2.getMom()
728 newPosDir[t1] = {"position": rep1.getPos(state1), "direction": rep1.getDir(state1), "momentum": mom1}
729 newPosDir[t2] = {"position": rep2.getPos(state2), "direction": rep2.getDir(state2), "momentum": mom2}
730 oa = mom1.Dot(mom2) / (mom1.Mag() * mom2.Mag())
731 h["oa"].Fill(oa)
732
733 covX = HNL.GetCovV()
734 dist = HNL.GetDoca()
735 h["Vzpull"].Fill((mctrack.GetStartZ() - HNLPos.Z()) / ROOT.TMath.Sqrt(covX[5]))
736 h["Vxpull"].Fill((mctrack.GetStartX() - HNLPos.X()) / ROOT.TMath.Sqrt(covX[0]))
737 h["Vypull"].Fill((mctrack.GetStartY() - HNLPos.Y()) / ROOT.TMath.Sqrt(covX[3]))
738
739
740 if hasattr(ShipGeo, "TimeDet"):
741 for fT in sTree.FitTracks:
743 if rc:
744 for aPoint in sTree.TimeDetPoint:
745 h["extrapTimeDetX"].Fill(pos.X() - aPoint.GetX())
746 h["extrapTimeDetY"].Fill(pos.Y() - aPoint.GetY())
747
748
749