xboa
_builtin_hit_factory.py
Go to the documentation of this file.
1 # This file is a part of xboa
2 #
3 # xboa is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # xboa is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with xboa in the doc folder. If not, see
15 # <http://www.gnu.org/licenses/>.
16 
17 import xboa.common as common
18 from xboa.hit.factory import LineFactoryBase
19 
20 class BuiltinHitFactory(LineFactoryBase):
21  """
22  Factory class for line by line reads of output files
23  """
24  def __init__(self, format, filehandle):
25  super(LineFactoryBase, self).__init__()
26  self.filehandle = filehandle
27  self.format = str(format)
28  if self.format not in self.file_formats.keys():
29  raise KeyError("Did not recognise builtin format "+self.format+\
30  ". Should be one of "+str(self.file_formats.keys()))
31 
32  def make_hit(self):
33  """
34  Read a new hit from filehandle according to a predefined format
35  - filehandle = file handle object
36  Returns a new hit object
37  """
38  format = self.format
39  hit = self._read_formatted(self.file_formats[format],
40  self.file_units[format],
41  self.filehandle, '')
42  if(format.find('icool') > -1 ):
43  hit.set('pid', common.icool_pid_to_pdg[hit.get('pid')])
44  if( format.find('mars') > -1 ):
45  hit.set('pid', common.mars_pid_to_pdg[hit.get('pid')])
46  try:
47  hit.set('mass', common.pdg_pid_to_mass[abs(hit.get('pid'))])
48  hit.mass_shell_condition(self.file_mass_shell[format])
49  except KeyError:
50  hit.set('mass', 0.)
51  hit.mass_shell_condition(self.file_mass_shell[format])
52  if hit.get('pid') not in self.bad_pids:
53  print 'Warning - could not resolve PID ', hit.get('pid'), \
54  ' setting mass to 0.'
55  self.bad_pids.append(hit.get('pid'))
56  if 'charge' not in self.file_formats[format]:
57  try:
58  hit.set('charge', common.pdg_pid_to_charge[hit.get('pid')])
59  except KeyError:
60  if hit.get('pid') not in self.bad_pids:
61  print 'Warning - could not resolve PID ',hit.get('pid'),' setting charge to 0.'
62  self.bad_pids.append(hit.get('pid'))
63  return hit
64 
65  @classmethod
66  def file_types(cls):
67  return cls.file_formats
68 
69  #formatting information
70  file_formats = {
71  'icool_for009' : ['eventNumber', 'particleNumber', 'pid', 'status', 'station', 't', 'x', 'y', 'z', 'px', 'py', 'pz', 'bx', 'by', 'bz', 'local_weight',
72  'ex', 'ey', 'ez', '', 'sx', 'sy', 'sz'],
73  'icool_for003' : ['eventNumber', 'particleNumber', 'pid', 'status', 't', 'local_weight', 'x', 'y', 'z', 'px', 'py', 'pz', 'sx', 'sy', 'sz'],
74  'g4beamline_bl_track_file' : ['x','y','z','px','py','pz','t','pid','eventNumber','particleNumber', '','local_weight'],
75  'mars_1' : ['eventNumber','pid','x','y','z','px','py','pz','energy','ct','local_weight']
76  }
77 
78  file_units = {
79  'icool_for009' : {'eventNumber':'', 'particleNumber':'', 'pid':'', 'status':'', 'station':'', 't':'s', 'x':'m', 'y':'m', 'z':'m', 'px':'GeV/c', 'py':'GeV/c',
80  'pz':'GeV/c', 'bx':'T', 'by':'T', 'bz':'T', 'local_weight':'',
81  'ex':'GV/m', 'ey':'GV/m', 'ez':'GV/m', 'sx':'', 'sy':'', 'sz':'', '':''},
82  'icool_for003' : {'eventNumber':'', 'particleNumber':'', 'pid':'', 'status':'', 't':'s', 'local_weight':'', 'x':'m', 'y':'m', 'z':'m', 'px':'GeV/c', 'py':'GeV/c', 'pz':'GeV/c', 'sx':'', 'sy':'', 'sz':''},
83  'g4beamline_bl_track_file' : {'x':'mm','y':'mm','z':'mm','px':'MeV/c','py':'MeV/c','pz':'MeV/c','t':'ns','pid':'','eventNumber':'','station':'','local_weight':'', 'particleNumber':''},
84  'mars_1' : {'eventNumber':'','pid':'','x':'mm','y':'mm','z':'mm','px':'GeV/c','py':'GeV/c','pz':'GeV/c','energy':'GeV','ct':'cm','local_weight':''},
85  }
86 
87  file_mass_shell = {'icool_for009':'energy',
88  'icool_for003':'energy',
89  'g4beamline_bl_track_file':'energy',
90  'mars_1':'energy'}
91 
92 
The hit factory module defines a number of factory classes used for generating hit objects...
Definition: __init__.py:1
Factory class for line by line reads of output files.
common module defines common utility data and functions that are used elsewhere
Definition: __init__.py:1
def make_hit
Read a new hit from filehandle according to a predefined format.