FairShip
Loading...
Searching...
No Matches
evd_fillEnergy.py
Go to the documentation of this file.
1# SPDX-License-Identifier: LGPL-3.0-or-later
2# SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP Collaboration
3
4import eveGlobal
5import ROOT
6
7
8def collect_hits(lsOfGlobals, checked_muons):
9 MUON = 13
10 sTree = lsOfGlobals.FindObject("cbmsim")
11
12 fPos = ROOT.TVector3()
13 fMom = ROOT.TVector3()
14
15 muon_to_follow = -1
16 for index, track in enumerate(sTree.MCTrack):
17 if abs(track.GetPdgCode()) == MUON and index not in checked_muons:
18 muon_to_follow = index
19 checked_muons.add(muon_to_follow)
20 break
21
22 if muon_to_follow == -1:
23 return {}
24
25 fT = sTree.MCTrack[muon_to_follow]
26 fT.GetStartVertex(fPos)
27 hitlist = {}
28 hitlist[fPos.Z()] = [fPos.X(), fPos.Y(), fT.GetP()]
29 # loop over all sensitive volumes to find hits
30 for P in ["vetoPoint", "strawtubesPoint", "ShipRpcPoint", "TargetPoint"]:
31 if not sTree.GetBranch(P):
32 continue
33 c = eval("sTree." + P)
34 for p in c:
35 if p.GetTrackID() == muon_to_follow:
36 p.Momentum(fMom)
37 if hasattr(p, "LastPoint"):
38 lp = p.LastPoint()
39 hitlist[lp.z()] = [lp.x(), lp.y(), p.LastMom().Mag()]
40 hitlist[2.0 * p.GetZ() - lp.z()] = [2.0 * p.GetX() - lp.x(), 2.0 * p.GetY() - lp.y(), fMom.Mag()]
41 else:
42 hitlist[p.GetZ()] = [p.GetX(), p.GetY(), fMom.Mag()]
43 return hitlist
44
45
46def trajectory_init(lsOfGlobals, name: str = "SHiP MuonTraj"):
47 traj = lsOfGlobals.FindObject(name)
48 if not traj:
49 traj = ROOT.TGraph()
50 traj.SetName(name)
51 traj.SetLineWidth(2)
52 lsOfGlobals.Add(traj)
53 traj.Set(0)
54 return traj
55
56
57def execute() -> None:
58 N_MUONS = 2
59 canvas = ROOT.gROOT.FindObject("Root Canvas EnergyLoss")
60 if not canvas:
61 print("add particle flower not started!")
62 return
63 lsOfGlobals = ROOT.gROOT.GetListOfGlobals()
64 c1 = canvas.cd(1)
65 c1.Clear()
66
67 # get zmin, zmax from graphic
68 SHiPDisplay = eveGlobal.SHiPDisplay
69 v = ROOT.gEve.GetViewers().FindChild("Bar Embedded Viewer side")
70 vw = v.GetGLViewer()
71 cam = vw.CurrentCamera()
72 ed = v.GetEditorObject()
73 co = ed.GetCameraOverlay()
74 ax = co.GetAttAxis()
75 fr = vw.GetFrame()
76 test = ROOT.TGLVertex3(0.0, 0.0, 0.0)
77 vtest = cam.ViewportToWorld(test)
78 zmin = vtest.Z()
79 test = ROOT.TGLVertex3(fr.GetWidth(), 0.0, 0.0)
80 vtest = cam.ViewportToWorld(test)
81 zmax = vtest.Z()
82
83 checked_muons = set()
84 all_muons_hitlist = [collect_hits(lsOfGlobals, checked_muons) for _ in range(N_MUONS)]
85 all_muons_hitlist = list(filter(lambda x: x, all_muons_hitlist))
86 trajectories = [
87 trajectory_init(lsOfGlobals, "SHiP MuonTraj_" + str(index)) for index in range(len(all_muons_hitlist))
88 ]
89
90 emin, emax = 1e9, -1e9
91 for trajectory, hitlist in zip(trajectories, all_muons_hitlist):
92 for index, z in enumerate(sorted(hitlist.keys())):
93 E = hitlist[z][2]
94 trajectory.SetPoint(index, z, E)
95 emax = max(emax, E)
96 emin = min(emin, E)
97
98 emin, emax = emin * 0.9, emax * 1.1
99 print("zmin/max", zmin, zmax)
100 hist = c1.DrawFrame(zmin, emin, zmax, emax)
101 hist.SetYTitle("p (GeV/c)")
102 hist.SetXTitle("z cm")
103 xaxis = hist.GetXaxis()
104 xaxis.SetNdivisions(ax.GetNdivisions())
105 for trajectory in trajectories:
106 trajectory.Draw()
107 txt = ROOT.TLatex()
108 txt.DrawLatexNDC(0.6, 0.8, "event index:" + str(SHiPDisplay.n))
109 c1.Update()
110
111
112if __name__ == "__main__":
113 execute()
def trajectory_init(lsOfGlobals, str name="SHiP MuonTraj")
def collect_hits(lsOfGlobals, checked_muons)