Python
MCTrackInfo.py
Go to the documentation of this file.
1 import ROOT,os,sys,getopt
2 import rootUtils as ut
3 import shipunit as u
4 import RecoSettings
5 ########################################################################
6 ## For a single event stores HNL, its decay products and several other tracks (if requested)
7 # from ShipMCTracks collection of a MCtrack branch (see $FAIRSHIP/shipdata/ShipMCTrack.h)\n
8 #
9 #
10 class MCTrackInfo(object):
11  """ More description here"""
12  def __init__(self, tree, debug=0):
13  ## root tree to be read.
14  self.__tree = tree
15  ## debug level [0,3].
16  self.__debug = debug
17  ## z of the last full acceptance plane (to be used in #checkEllipticAcc), must be set later.
18  self.__zAcc = 3380*u.cm
19  ## event weight. Read in #readEvent as weight of HNL.
20  self.__weight = 0
21  ## HNL decay vertex coordinates (TVector3). Assigned in #readEvent()
22  self.__HNLdecayCoord = None
23  ## MCTrID (index of the track in MCtrack list) for \b HNL \b daughter products.
24  self.__decayProdTrID = []
25  ## Production vertices of several stored products (\b not \b only HNL daughter) as {MCTrID : TVector3}.
26  self.__productVertex = {}
27  ## Momentum of several stored products (\b not \b only HNL daughter) as {MCTrID : TVector3}. Correspond to $__productVertex.
28  self.__productMoment = {}
29  ## Charge of several stored products (\b not \b only HNL daughter) as {MCTrID : value}. Correspond to $__productVertex.
30  self.__productCharge = {}
31  ## Basic information on the particle (pdg, mother pdg,... to be added - z decay!)
32  self.__productInfo = {}
33 
34 
35 
36 
37 
38  ########################################################################
39  ##\brief clears all dictionaries and lists. To be called inside #readEvent.
40  def __clean(self):
41  self.__weight = 0
42  self.__HNLdecayCoord = None
43  self.__decayProdTrID = []
44  self.__productVertex.clear()
45  self.__productMoment.clear()
46  self.__productCharge.clear()
47  self.__productInfo.clear()
48 
49  ########################################################################
50  ##\brief set z of the last full acceptance plane (to be used in #checkEllipticAcc), depends on #RecoSettings. trackMinNofStations.
51  # \param z - the value [cm].
52  def setAccPlaneZ(self, z):
53  self.__zAcc = z
54 
55 
56  ########################################################################
57  ##\brief prints HNL decay products
58  def PrintHNL(self):
59  print type(self).__name__, " : "
60  print "\tdecay vertex: ", " ".join("{:10.4f}".format(self.__HNLdecayCoord(ii)) for ii in range(0,3)),
61  print "\n\tdecay products (trID, pdg, charge, momentum):"
62  for tid in self.__decayProdTrID :
63  print "\t", "{0:6}{1:10}{2:6.0f}".format(tid, self.__productInfo[tid]['pdg'], self.__productCharge[tid]),
64  print "\t", " ".join("{:10.4f}".format(self.__productMoment[tid](ii)) for ii in range(0,3))
65 
66 
67 
68  ########################################################################
69  ## \brief returns event weight.
70  # \return value of #__weight
71  def getEventWeight(self):
72  return self.__weight
73 
74 
75  ########################################################################
76  ## \brief returns \b HNL \b decay \b vertex coordinates (TVector3 #__HNLdecayCoord).
77  # \return TVector3 #__HNLdecayCoord
78  def getHNLdecayVertex(self):
79  return self.__HNLdecayCoord
80 
81 
82  ########################################################################
83  ## \brief returns list of MCtrIDs (index in MCTrack collection) fot \b HNL \b decay \b products.
84  # \return list of MCtrIDs (index in MCTrack collection) fot \b HNL \b decay \b products.
85  def getHNLdecayTrIDs(self):
86  trID = []
87  for tid in self.__decayProdTrID:
88  trID.append(tid)
89  return trID
90 
91 
92  ########################################################################
93  ## \brief returns list of all MCtrIDs read by #readEvent and #readTrack - \b not \b only HNL decay products.
94  # \return list of MCtrIDs (index in MCTrack collection) fot all tracks read by #readEvent and #readTrack.
95  def getTrIDs(self):
96  trID = []
97  for tid in self.__productMoment:
98  trID.append(tid)
99  return trID
100 
101 
102  ########################################################################
103  ## \brief returns charge of a particle with the given trID (index in MCTrack collection). May return None.
104  # Tries to get charge from #__productCharge;
105  # if it is not available, tries to add the track calling #readTrack. If fails, returns None.
106  # \param trID - index of the MCTrack
107  # \return charge of the track or None
108  def getCharge(self, trID):
109  if ( not trID in self.__productCharge ): self.readTrack(trID)
110  return self.__productCharge[trID]
111 
112 
113 
114  ########################################################################
115  ## \brief returns momentum (TVector3) for a particle with the given trID (index in MCTrack collection). May return None.
116  # Tries to get momentum from #__productMoment;
117  # if it is not available, tries to add the track calling #readTrack. If fails, returns None.
118  # \param trID - index of the MCTrack
119  # \return momentum of the track (TVector3) or None
120  def getMomentum(self, trID):
121  if ( not trID in self.__productMoment ): self.readTrack(trID)
122  return self.__productMoment[trID]
123 
124 
125 
126 
127  ########################################################################
128  ## \brief checks if HNL decay vertex (and both HNL daughter tracks, if tight cut) are in elliptic acceptance at #__zAcc.
129  # For tight cut each HNL daughter is propagated to #__zAcc (see #getTrackPropagation)
130  # and checked if it in the acceptance (see #RecoSettings .checkEllipticAcc). Also checks HNL decay vertex.
131  # \param tight - if true, not only HNL vertex but also propagated tracks are checked.
132  # \return True if the vertex (and both tracks, if tight cut) are in the acceptance.
133  def checkEllipticAcc(self, tight=True):
135  if ( not ok ) : return False
136  if ( tight ) :
137  for tid in self.__decayProdTrID :
138  v3 = self.getTrackPropagation(tid, self.__zAcc)
139  ok = ( ok and RecoSettings.checkEllipticAcc(v3) )
140  if ( not ok ) : break
141  return ok
142 
143 
144 
145 
146  ########################################################################
147  ## \brief for Tracker Performance studies. Returns -1, 0, 1 depending on vertex topology.
148  # defines direction in y: 0 if both momY the same direction, -1 if (negative up, positive down).
149  # \return 0 if both momY the same direction, -1 if (negative up, positive down).
150  def checkVertexUpDown(self):
151  theCase = 0 # undefined
152  if ( (not self.__productVertex) or (len(self.__decayProdTrID)<2) ): return theCase
153  tid1 = self.__decayProdTrID[0]
154  tid2 = self.__decayProdTrID[1]
155  if ( self.__productMoment[tid1].Y()*self.__productMoment[tid2].Y() >=0 ): return theCase
156 
157  theCase = self.__productCharge[tid1]*self.__productMoment[tid1].Y()/abs(self.__productMoment[tid1].Y())
158 
159  return theCase
160 
161 
162  ########################################################################
163  ## \brief for Tracker Performance studies. Returns TVector3.
164  # Linear propagation of a given track to a plane (0,0,z) perendicular to Z-axis.
165  # \param trID - track index (MCtrID).
166  # \param z - coordinate of the plane.
167  # \return coordinates of the point of track crossing the given plalne (TVector3).
168  def getTrackPropagation(self, trID, z):
169  if ( not trID in self.__productMoment ): self.readTrack(trID)
170  xy=[0.,0.]
171  for ii in [0,1]:
172  xy[ii] = self.__productVertex[trID][ii]+self.__productMoment[trID][ii]/self.__productMoment[trID][2]*(z-self.__productVertex[trID][2])
173  vec3 = ROOT.TVector3(xy[0],xy[1],z)
174  return vec3
175 
176 
177 
178 
179  ########################################################################
180  ## \brief reads one additional track with trID from MCTrack collection. Returns 1 if success, 0 otherwise.
181  # fills #__productVertex, #__productMoment, #__productCharge dictionaries.
182  # \param trID - track index (MCtrID).
183  # \return 1 if successful, 0 if trID is not found and the dictionaries are not modified.
184  def readTrack(self, trID):
185  x = None
186  try:
187  x=self.__tree.MCTrack[trID]
188  except:
189  pass
190  if (not b) : return 0
191 
192  vec3 = ROOT.TVector3(x.GetPx(),x.GetPy(),x.GetPz())
193  self.__productMoment[trID] = vec3
194  vec3 = ROOT.TVector3(x.GetStartX(),x.GetStartY(),x.GetStartZ())
195  self.__productVertex[trID] = vec3
196  self.__productCharge[trID] = RecoSettings.chargePDG(x.GetPdgCode())
197  self.__productInfo[trID] ={}
198  self.__productInfo[trID]['pdg'] = x.GetPdgCode()
199  mid = x.GetMotherId()
200  if(mid>=0) : self.__productInfo[trID]['mpdg'] = self.__tree.MCTrack[mid].GetPdgCode()
201  else : self.__productInfo[trID]['mpdg'] = 0
202  if( self.__debug>0 ):
203  print "additional MC track for trID", trID
204  print "\tpdgid/charge/mother: ", x.GetPdgCode(), self.__productCharge[trID], mid, "(pdf=",self.__productInfo[trID]['mpdg'], ")"
205  print "\torigin: ", self.__productVertex[trID].X(), self.__productVertex[trID].Y(), self.__productVertex[trID].Z()
206  print "\tmomentum: ", self.__productMoment[trID].X(), self.__productMoment[trID].Y(), self.__productMoment[trID].Z()
207  return 1
208 
209 
210 
211 
212 
213  ########################################################################
214  ## \brief reads \b HNL \b decay \b products from MCTrack collection (\b two at the moment!). Returns number of daughters.
215  # Fills #__HNLdecayCoord and #__productVertex, #__productMoment, #__productCharge dictionaries for \b HNL \b daughters.
216  # \return number of daughters (loop stops at daughter==2 at the moment!).
217  def readEvent(self):
218  self.__clean()
219  if (self.__debug>0 ):
220  print "sTree.MCTrack", self.__tree.MCTrack, "size:", len(self.__tree.MCTrack)
221  daughter = 0
222  tid = 0
223  #/home/kkuzn/SHIP/ShipSoft/tmp/FairShip/shipdata/ShipMCTrack.h
224  for x in self.__tree.MCTrack:
225  if daughter>1:
226  break
227  mid = x.GetMotherId()
228  Z = x.GetStartZ()
229  if( Z>5000 ): Z=5000
230  if mid==0: # #NB# here I suppose HNL is record #1 always!
231  self.__weight = x.GetWeight()
232  if self.__debug>0: print "HNL\t\t", tid,"\t", x.GetStartX(), x.GetStartY(), x.GetStartZ(), "\t\t", x.GetPx(), x.GetPy(), x.GetPz()
233  elif( mid==1 ): # HNL decay products
234  self.__decayProdTrID.append(tid)
235  vec3 = ROOT.TVector3(x.GetPx(),x.GetPy(),x.GetPz())
236  self.__productMoment[tid] = vec3
237  vec3 = ROOT.TVector3(x.GetStartX(),x.GetStartY(),x.GetStartZ())
238  self.__productVertex[tid] = vec3
239  self.__productCharge[tid] = RecoSettings.chargePDG(x.GetPdgCode())
240  self.__productInfo[tid] ={}
241  self.__productInfo[tid]['pdg'] = x.GetPdgCode()
242  self.__productInfo[tid]['mpdg'] = self.__tree.MCTrack[mid].GetPdgCode()
243  if( self.__debug>0 ): print x.GetPdgCode(), "\t\t", tid,"\t", x.GetStartX(), x.GetStartY(), x.GetStartZ(), "\t\t", x.GetPx(), x.GetPy(), x.GetPz()
244  if daughter<1:
245  self.__HNLdecayCoord = ROOT.TVector3(x.GetStartX(), x.GetStartY(), x.GetStartZ())
246  else:
247  if self.__HNLdecayCoord.Z()!=x.GetStartZ():
248  self.__HNLdecayCoord.SetXYZ(0,0,-8899)
249  daughter+=1
250  tid+=1
251 
252  return daughter
253 
def setAccPlaneZ
set z of the last full acceptance plane (to be used in checkEllipticAcc), depends on RecoSettings...
Definition: MCTrackInfo.py:52
__HNLdecayCoord
HNL decay vertex coordinates (TVector3).
Definition: MCTrackInfo.py:22
def readEvent
reads HNL decay products from MCTrack collection (two at the moment!).
Definition: MCTrackInfo.py:217
__productCharge
Charge of several stored products (not only HNL daughter) as {MCTrID : value}.
Definition: MCTrackInfo.py:30
__decayProdTrID
MCTrID (index of the track in MCtrack list) for HNL daughter products.
Definition: MCTrackInfo.py:24
def getCharge
returns charge of a particle with the given trID (index in MCTrack collection).
Definition: MCTrackInfo.py:108
def getTrIDs
returns list of all MCtrIDs read by readEvent and readTrack - not only HNL decay products.
Definition: MCTrackInfo.py:95
__zAcc
z of the last full acceptance plane (to be used in checkEllipticAcc), must be set later...
Definition: MCTrackInfo.py:18
def getHNLdecayTrIDs
returns list of MCtrIDs (index in MCTrack collection) fot HNL decay products.
Definition: MCTrackInfo.py:85
__productInfo
Basic information on the particle (pdg, mother pdg,...
Definition: MCTrackInfo.py:32
__weight
event weight.
Definition: MCTrackInfo.py:20
def getMomentum
returns momentum (TVector3) for a particle with the given trID (index in MCTrack collection).
Definition: MCTrackInfo.py:120
def getTrackPropagation
for Tracker Performance studies.
Definition: MCTrackInfo.py:168
def checkEllipticAcc
checks if HNL decay vertex (and both HNL daughter tracks, if tight cut) are in elliptic acceptance at...
Definition: MCTrackInfo.py:133
def __clean
clears all dictionaries and lists.
Definition: MCTrackInfo.py:40
__productMoment
Momentum of several stored products (not only HNL daughter) as {MCTrID : TVector3}.
Definition: MCTrackInfo.py:28
def getHNLdecayVertex
returns HNL decay vertex coordinates (TVector3 __HNLdecayCoord).
Definition: MCTrackInfo.py:78
def getEventWeight
returns event weight.
Definition: MCTrackInfo.py:71
def PrintHNL
prints HNL decay products
Definition: MCTrackInfo.py:58
def readTrack
reads one additional track with trID from MCTrack collection.
Definition: MCTrackInfo.py:184
__debug
debug level [0,3].
Definition: MCTrackInfo.py:16
def checkVertexUpDown
for Tracker Performance studies.
Definition: MCTrackInfo.py:150
__tree
root tree to be read.
Definition: MCTrackInfo.py:14
For a single event stores HNL, its decay products and several other tracks (if requested) from ShipMC...
Definition: MCTrackInfo.py:10
__productVertex
Production vertices of several stored products (not only HNL daughter) as {MCTrID : TVector3}...
Definition: MCTrackInfo.py:26
def checkEllipticAcc
Definition: RecoSettings.py:21