xboa
_opal_hit_factory.py
Go to the documentation of this file.
1 
2 # This file is a part of xboa
3 #
4 # xboa 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 # xboa 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 xboa in the doc folder. If not, see
16 # <http://www.gnu.org/licenses/>.
17 
18 from xboa.hit.factory import LineFactoryBase
19 
20 class OpalHitFactory(LineFactoryBase):
21  """
22  Factory class for line by line reads of OPAL output files
23  """
24  def __init__(self, pid, probes, ignore_probes, filehandle):
25  super(LineFactoryBase.__init__(self))
26  if pid not in Common.pdg_pid_to_mass or \
27  pid not in Common.pdg_pid_to_charge:
28  raise
29  self.pid = pid
30  self.probes = probes
31  self.ignore_probes = []
32  self.filehandle = filehandle
33 
34  # read beam loss hit
35  # probes is the list of probes discovered - appended to dynamically
36  def make_hit(self):
37  """
38  Read a new hit from filehandle
39  - filehandle = file handle object
40  Returns a new hit object
41 
42  Raises an EOFError if end of file is hit
43  """
44  probes = self.probes
45  pid = self.pid
46  try:
47  line = self.filehandle.next()
48  except StopIteration:
49  raise EOFError("End of file reached")
50  words = line.split()
51  if words[0] in self.ignore_probes:
52  raise BadEventError("Failed to parse event")
53  if not len(words) == 10:
54  raise BadEventError("Failed to parse event")
55  if words[0] not in probes.keys():
56  probes[words[0]] = len(probes.keys())
57  hit_dict = dict( (key, float(words[i+1]) ) for i, key in enumerate(['x', 'y', 'z', 'px', 'py', 'pz', 'event_number', 'station', 't']))
58  hit_dict['pid'] = pid
59  hit_dict['mass'] = Common.pdg_pid_to_mass[abs(pid)]
60  hit_dict['charge'] = Common.pdg_pid_to_charge[pid]
61  hit_dict['station'] = probes[words[0]]+len(probes)*(int(words[8])-1)
62  for item in ['px', 'py', 'pz']:
63  hit_dict[item] *= hit_dict['mass']
64  hit = Hit.new_from_dict(hit_dict)
65  hit.mass_shell_condition('energy')
66  return hit
67 
The hit factory module defines a number of factory classes used for generating hit objects...
Definition: __init__.py:1
def make_hit
Read a new hit from filehandle.
Factory class for line by line reads of OPAL output files.