~christopher-hunt08/maus/maus_scifi_recon_devel

« back to all changes in this revision

Viewing changes to src/common_py/analysis/hit_types.py

  • Committer: Christopher Hunt
  • Date: 2015-12-14 08:48:38 UTC
  • mfrom: (697.47.179 cctag_corr)
  • Revision ID: christopher.hunt08@imperial.ac.uk-20151214084838-wjl32e152yx3ks1j
MAUS Merg Remerged

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# This file is part of MAUS: http://micewww.pp.rl.ac.uk/projects/maus
 
3
#
 
4
# MAUS is free software: you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation, either version 3 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# MAUS is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with MAUS.  If not, see <http://www.gnu.org/licenses/>.
 
16
#
 
17
 
 
18
"""
 
19
  Defines the AnalysisHit class.
 
20
 
 
21
  A generic hit class that can be used to simplify the analysis of track data.
 
22
 
 
23
  Note that this class is used by the add_hit meth
 
24
"""
 
25
 
 
26
# pylint: disable = W0311, R0902, R0904, R0913, C0103
 
27
 
 
28
import math
 
29
 
 
30
 
 
31
class AnalysisHit() :
 
32
  """
 
33
    A simple, unified class that holds the essential information, when copied
 
34
    from MAUS data types, during various analyses.
 
35
 
 
36
    Some syntax was borrowed from XBoa for simplicity and because I like the
 
37
    interface!
 
38
  """
 
39
  def __init__( self, x = 0.0, y = 0.0, z = 0.0, \
 
40
                      px = 0.0, py = 0.0, pz = 0.0, \
 
41
                      time = 0.0, mass = 105.6583715, p_value = 1.0, pid = 13, \
 
42
                      scifi_track_point = None, virtual_track_point = None ) :
 
43
    """
 
44
      Initialise the object. this can be done in three ways:
 
45
      1. Specify all components by hand
 
46
      2. Build from a sci-fr trackpoint
 
47
      3. Build from a virtual trackpoint
 
48
    """
 
49
    if scifi_track_point is None and virtual_track_point is None :
 
50
      self.__x = x
 
51
      self.__y = y
 
52
      self.__z = z
 
53
      self.__px = px
 
54
      self.__py = py
 
55
      self.__pz = pz
 
56
      self.__time = time
 
57
      self.__mass = mass
 
58
      self.__p_value = p_value
 
59
      self.__pid = pid
 
60
#      self.__reference = reference
 
61
 
 
62
    elif scifi_track_point is not None and virtual_track_point is None :
 
63
      self.__x = scifi_track_point.pos().x()
 
64
      self.__y = scifi_track_point.pos().y()
 
65
      self.__z = scifi_track_point.pos().z()
 
66
      self.__px = scifi_track_point.mom().x()
 
67
      self.__py = scifi_track_point.mom().y()
 
68
      self.__pz = scifi_track_point.mom().z()
 
69
      self.__time = time
 
70
      self.__mass = mass
 
71
      self.__p_value = p_value
 
72
      self.__pid = pid
 
73
#      if reference is None :
 
74
#        self.__reference = str(scifi_track_point.tracker())+"."+\
 
75
#            str(scifi_track_point.station())+"."+str(scifi_track_point.plane())
 
76
#      else :
 
77
#        self.__reference = reference
 
78
      if math.isnan(self.__x) :
 
79
        raise ValueError("NaN Values Received from Scifi Track Point")
 
80
 
 
81
    elif scifi_track_point is None and virtual_track_point is not None :
 
82
      self.__x = virtual_track_point.GetPosition().x()
 
83
      self.__y = virtual_track_point.GetPosition().y()
 
84
      self.__z = virtual_track_point.GetPosition().z()
 
85
      self.__px = virtual_track_point.GetMomentum().x()
 
86
      self.__py = virtual_track_point.GetMomentum().y()
 
87
      self.__pz = virtual_track_point.GetMomentum().z()
 
88
      self.__time = virtual_track_point.GetTime()
 
89
      self.__mass = mass
 
90
      self.__p_value = 1.0
 
91
      self.__pid = virtual_track_point.GetParticleId()
 
92
#      self.__reference = reference
 
93
#      if reference is None :
 
94
#        self.__reference = virtual_track_point.GetStationId()
 
95
#      else :
 
96
#        self.__reference = reference
 
97
 
 
98
    else :
 
99
      print "WTF!"
 
100
      raise ValueError( 'Please supply precisely one of "virtual_track_point"'+\
 
101
                 ' or "scifi_track_point", or specify all values explicitly.' )
 
102
 
 
103
 
 
104
  def __str__( self ) :
 
105
    """
 
106
      Return a string of the parameters.
 
107
      Mostly useful for debuging.
 
108
    """
 
109
    return '(' + str( self.__x ) + ',' +\
 
110
                 str( self.__y ) + ',' +\
 
111
                 str( self.__z ) + '):[' +\
 
112
                 str( self.__px ) + ',' +\
 
113
                 str( self.__py ) + ',' +\
 
114
                 str( self.__pz ) + ']'
 
115
#                 str( self.__reference )
 
116
 
 
117
 
 
118
  def __repr__( self ) :
 
119
    """
 
120
      Return a string of the parameters.
 
121
      Mostly useful for debuging.
 
122
    """
 
123
    return '(' + str( self.__x ) + ',' +\
 
124
                 str( self.__y ) + ',' +\
 
125
                 str( self.__z ) + '):[' +\
 
126
                 str( self.__px ) + ',' +\
 
127
                 str( self.__py ) + ',' +\
 
128
                 str( self.__pz ) + ']'
 
129
#                 str( self.__reference )
 
130
 
 
131
 
 
132
 
 
133
  def set_x( self, val ) :
 
134
    """
 
135
      Set the x position
 
136
    """
 
137
    self.__x = val
 
138
 
 
139
  def set_y( self, val ) :
 
140
    """
 
141
      Set the y position
 
142
    """
 
143
    self.__y = val
 
144
 
 
145
  def set_z( self, val ) :
 
146
    """
 
147
      Set the z position
 
148
    """
 
149
    self.__z = val
 
150
 
 
151
  def set_px( self, val ) :
 
152
    """
 
153
      Set the x momentum
 
154
    """
 
155
    self.__px = val
 
156
 
 
157
  def set_py( self, val ) :
 
158
    """
 
159
      Set the y momentum
 
160
    """
 
161
    self.__py = val
 
162
 
 
163
  def set_pz( self, val ) :
 
164
    """
 
165
      Set the z momentum
 
166
    """
 
167
    self.__pz = val
 
168
 
 
169
  def set_time( self, val ) :
 
170
    """
 
171
      Set the time
 
172
    """
 
173
    self.__time = val
 
174
 
 
175
  def set_mass( self, val ) :
 
176
    """
 
177
      Set the mass
 
178
    """
 
179
    self.__mass = val
 
180
 
 
181
  def set_p_value( self, val ) :
 
182
    """
 
183
      Set the p-value
 
184
    """
 
185
    self.__p_value = val
 
186
 
 
187
  def set_pid( self, val ) :
 
188
    """
 
189
      Set the particle ID
 
190
    """
 
191
    self.__pid = val
 
192
 
 
193
#  def set_reference( self, string ) :
 
194
#    self.__reference = string
 
195
 
 
196
  def get_x( self ) :
 
197
    """
 
198
      Get the x position
 
199
    """
 
200
    return self.__x
 
201
 
 
202
  def get_y( self ) :
 
203
    """
 
204
      Get the y position
 
205
    """
 
206
    return self.__y
 
207
 
 
208
  def get_z( self ) :
 
209
    """
 
210
      Get the z position
 
211
    """
 
212
    return self.__z
 
213
 
 
214
  def get_px( self ) :
 
215
    """
 
216
      Get the x momentum
 
217
    """
 
218
    return self.__px
 
219
 
 
220
  def get_py( self ) :
 
221
    """
 
222
      Get the y momentum
 
223
    """
 
224
    return self.__py
 
225
 
 
226
  def get_pz( self ) :
 
227
    """
 
228
      Get the z momentum
 
229
    """
 
230
    return self.__pz
 
231
 
 
232
  def get_time( self ) :
 
233
    """
 
234
      Get the time
 
235
    """
 
236
    return self.__time
 
237
 
 
238
  def get_p( self ) :
 
239
    """
 
240
      Get the total momentum
 
241
    """
 
242
    if math.isnan(math.sqrt( self.__px**2 + self.__py**2 + self.__pz**2 )) :
 
243
      print "ISNAN!!!", self
 
244
    return math.sqrt( self.__px**2 + self.__py**2 + self.__pz**2 )
 
245
 
 
246
  def get_r( self ) :
 
247
    """
 
248
      Get the position radius
 
249
    """
 
250
    return math.sqrt( self.__x**2 + self.__y**2 )
 
251
 
 
252
  def get_energy( self ) :
 
253
    """
 
254
      Get the total energy
 
255
    """
 
256
    return math.sqrt( self.get_mass()**2 + self.get_p()**2 )
 
257
 
 
258
  def get_pt( self ) :
 
259
    """
 
260
      Get the transverse momentum
 
261
    """
 
262
    return math.sqrt( self.__px**2 + self.__py**2 )
 
263
 
 
264
  def get_mx( self ) :
 
265
    """
 
266
      Get the x gradient
 
267
    """
 
268
    return self.get_px() / self.get_pz()
 
269
 
 
270
  def get_my( self ) :
 
271
    """
 
272
      Get the y gradient
 
273
    """
 
274
    return self.get_py() / self.get_pz()
 
275
 
 
276
  def get_mass( self ) :
 
277
    """
 
278
      Get the mass
 
279
    """
 
280
    return self.__mass
 
281
 
 
282
  def get_p_value( self ) :
 
283
    """
 
284
      Get the p-value
 
285
    """
 
286
    return self.__p_value
 
287
 
 
288
  def get_pid( self ) :
 
289
    """
 
290
      Get the particle ID
 
291
    """
 
292
    return self.__pid
 
293
 
 
294
 
 
295
#  def get_reference( self ) :
 
296
#    return self.__reference
 
297
 
 
298
 
 
299
 
 
300
  def get( self, string ) :
 
301
    """
 
302
      Return the value of the variable described in the string
 
303
    """
 
304
    if not string in AnalysisHit.__hit_get_keys :
 
305
      return None
 
306
    else :
 
307
      return AnalysisHit.__hit_get_variables[ string ]( self )
 
308
 
 
309
 
 
310
  def get_variable_list() :
 
311
    """
 
312
        Return the list of variables that the user can request
 
313
    """
 
314
    return AnalysisHit.__hit_get_keys
 
315
  get_variable_list = staticmethod( get_variable_list )
 
316
 
 
317
 
 
318
 
 
319
  __hit_get_keys = []
 
320
  __hit_get_variables = { 'x':get_x, 'y':get_y, 'z':get_z,
 
321
                              'px':get_px, 'py':get_py, 'pz':get_pz,
 
322
                              'mx':get_mx, 'my':get_my,
 
323
                              't':get_time, 'p':get_p, 'r':get_r,
 
324
                              'E':get_energy, 'pt':get_pt, 'm':get_mass,
 
325
                              'mass':get_mass, 'energy':get_energy,
 
326
                              'pvalue':get_p_value, 'pid':get_pid }
 
327
 
 
328
  for key, val in __hit_get_variables.iteritems() :
 
329
    __hit_get_keys.append( key )