FairShip
Loading...
Searching...
No Matches
shipVeto.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
4# utility to simulate response of the veto systems
5from array import array
6
7import ROOT
8from shipTrackAccess import get_tracks, track_info, uses_acts
9
10
11class Task:
12 "initialize and give response of the veto systems"
13
14 def __init__(self, t) -> None:
15 self.SBTefficiency = 0.99 # Surrounding Background tagger: 99% efficiency picked up from TP
16 self.UBTefficiency = 0.999 # Upstream background tagger, per triggered tile
17 self.random = ROOT.TRandom3()
18 self.random.SetSeed(13)
19 self.detList = self.detMap()
20 self.sTree = t
22
23 def detMap(self):
24 fGeo = ROOT.gGeoManager
25 detList = {}
26 for v in fGeo.GetListOfVolumes():
27 nm = v.GetName()
28 i = fGeo.FindVolumeFast(nm).GetNumber()
29 detList[i] = nm
30 return detList
31
32 def SBT_plastic_decision(self, mcParticle=None) -> tuple[bool, float, int]:
33 return self.SBT_decision(mcParticle, detector="plastic")
34
35 def SBT_liquid_decision(self, mcParticle=None) -> tuple[bool, float, int]:
36 return self.SBT_decision(mcParticle, detector="liquid")
37
38 def SBT_decision(self, mcParticle=None, detector="liquid") -> tuple[bool, float, int]:
39 # if mcParticle >0, only count hits with this particle
40 # if mcParticle <0, do not count hits with this particle
41 hitSegments = 0
42 index = -1
43 fdetector = detector == "liquid"
44 for aDigi in self.sTree.Digi_SBTHits:
45 index += 1
46 detID = aDigi.GetDetectorID()
47 if fdetector and detID > 999999:
48 continue
49 if not fdetector and not detID > 999999:
50 continue
51 if mcParticle:
52 contributors = list(self.sTree.digiSBT2MC[index])
53 if mcParticle > 0:
54 # only count hits from this particle: skip if it is absent
55 if mcParticle not in contributors:
56 continue
57 elif mcParticle < 0:
58 # do not count hits from this particle: skip if it is present
59 if abs(mcParticle) in contributors:
60 continue
61 aDigi.GetXYZ()
62 aDigi.GetEloss()
63 if aDigi.isValid():
64 hitSegments += 1 # threshold of 45 MeV per segment
65 w = (1 - self.SBTefficiency) ** hitSegments
66 veto = self.random.Rndm() > w
67 # print 'SBT :',hitSegments
68 return veto, w, hitSegments
69
70 def UBT_decision(self, mcParticle=None) -> tuple[bool, float, int]:
71 # Count the triggered tiles: one digitized hit per fired tile, above
72 # the ADC trigger threshold.
73 # if mcParticle >0, only count tiles with a point of this particle
74 # if mcParticle <0, do not count tiles with a point of this particle
75 nHits = 0
76 if not self.sTree.GetBranch("Digi_UpstreamTaggerHits"):
77 # Not digitized: a geometry from before the scintillator UBT
78 if not self._warned_no_ubt_digis:
79 print("Warning: no Digi_UpstreamTaggerHits, the UBT does not veto")
80 self._warned_no_ubt_digis = True
81 return False, 1.0, 0
82 for index, aDigi in enumerate(self.sTree.Digi_UpstreamTaggerHits):
83 if not aDigi.IsTriggered():
84 continue
85 if mcParticle:
86 contributors = {self.sTree.UpstreamTaggerPoint[i].GetTrackID() for i in self.sTree.digiUBT2MC[index]}
87 if mcParticle > 0 and mcParticle not in contributors:
88 continue
89 if mcParticle < 0 and abs(mcParticle) in contributors:
90 continue
91 nHits += 1
92 w = (1 - self.UBTefficiency) ** nHits
93 veto = self.random.Rndm() > w
94 return veto, w, nHits
95
96 def Track_decision(self, mcParticle=None) -> tuple[bool, int | float, int]:
97 nMultCon = 0
98 k = -1
99 acts_tracks = uses_acts(self.sTree)
100 for aTrack in get_tracks(self.sTree):
101 k += 1
102 if mcParticle:
103 if mcParticle > 0 and mcParticle != self.sTree.fitTrack2MC[k]:
104 continue
105 if mcParticle < 0 and abs(mcParticle) == self.sTree.fitTrack2MC[k]:
106 continue
107 info = track_info(aTrack, acts_tracks)
108 if not info.converged:
109 continue
110 if info.ndf < 25:
111 continue
112 nMultCon += 1
113 w = 1.0
114 veto = nMultCon > 2
115 if veto:
116 w = 0.0
117 return veto, w, nMultCon
118
120 hnl = self.sTree.Particles[n]
121 aPoint = ROOT.TVector3(hnl.Vx(), hnl.Vy(), hnl.Vz())
122 distmin = self.fiducialCheck(aPoint)
123 return distmin
124
125 def fiducialCheck(self, aPoint):
126 nav = ROOT.gGeoManager.GetCurrentNavigator()
127 phi = 0.0
128 nSteps = 36
129 delPhi = 2.0 * ROOT.TMath.Pi() / nSteps
130 distmin = 1e10
131 nav.SetCurrentPoint(aPoint.x(), aPoint.y(), aPoint.z())
132 cNode = "outside"
133 aNode = nav.FindNode()
134 if aNode:
135 cNode = aNode.GetName()
136 if not cNode.startswith("decay_medium"):
137 distmin = 0.0
138 else:
139 for n in range(nSteps):
140 # set direction
141 xDir = ROOT.TMath.Sin(phi)
142 yDir = ROOT.TMath.Cos(phi)
143 nav.SetCurrentPoint(aPoint.x(), aPoint.y(), aPoint.z())
144 cNode = nav.FindNode().GetName()
145 nav.SetCurrentDirection(xDir, yDir, 0.0)
146 nav.FindNextBoundaryAndStep()
147 x, y = nav.GetCurrentPoint()[0], nav.GetCurrentPoint()[1]
148 if cNode != nav.GetCurrentNode().GetName():
149 dist = ROOT.TMath.Sqrt((aPoint.x() - x) ** 2 + (aPoint.y() - y) ** 2)
150 if dist < distmin:
151 distmin = dist
152 phi += delPhi
153 # distance to Tr1_x1
154 nav.cd("/Tr1_1")
155 shape = nav.GetCurrentNode().GetVolume().GetShape()
156 origin = array("d", [0, 0, shape.GetDZ()])
157 master = array("d", [0, 0, 0])
158 nav.LocalToMaster(origin, master)
159 dist = master[2] - aPoint.z()
160 if dist < distmin:
161 distmin = dist
162 # distance to straw veto:
163 nav.cd("/Veto_5")
164 shape = nav.GetCurrentNode().GetVolume().GetShape()
165 origin = array("d", [0, 0, shape.GetDZ()])
166 master = array("d", [0, 0, 0])
167 nav.LocalToMaster(origin, master)
168 dist = aPoint.z() - master[2]
169 if dist < distmin:
170 distmin = dist
171 return distmin
172
173
174# usage
175# import shipVeto
176# veto = shipVeto.Task(sTree)
177# veto,w = veto.SBT_decision()
178# or for plastic veto,w = veto.SBT_decision(detector='plastic')
179# if veto: continue # reject event
180# or
181# continue using weight w
bool _warned_no_ubt_digis
Definition shipVeto.py:21
tuple[bool, float, int] SBT_plastic_decision(self, mcParticle=None)
Definition shipVeto.py:32
tuple[bool, float, int] SBT_decision(self, mcParticle=None, detector="liquid")
Definition shipVeto.py:38
tuple[bool, float, int] UBT_decision(self, mcParticle=None)
Definition shipVeto.py:70
fiducialCheckSignal(self, n)
Definition shipVeto.py:119
detMap(self)
Definition shipVeto.py:23
tuple[bool, int|float, int] Track_decision(self, mcParticle=None)
Definition shipVeto.py:96
float UBTefficiency
Definition shipVeto.py:16
fiducialCheck(self, aPoint)
Definition shipVeto.py:125
None __init__(self, t)
Definition shipVeto.py:14
float SBTefficiency
Definition shipVeto.py:15
tuple[bool, float, int] SBT_liquid_decision(self, mcParticle=None)
Definition shipVeto.py:35