xboa
_user_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 import xboa.common as common
19 import xboa.hit
20 from xboa.hit.factory import LineFactoryBase
21 
22 class UserHitFactory(LineFactoryBase):
23  """
24  Factory class for line by line reads of output files using a user-defined
25  format
26  """
27  def __init__(self, format_list, format_units_dict, filehandle, mass_shell_condition):
28  """
29  Initialise the factory
30  - format_list = ordered list of strings. Each string should be a valid
31  Hit.set_variable()
32  - format_units_dict = dict of formats mapping from format_list elements
33  to units. Variables that are dimensionless or in natural units should
34  have an empty string to denote natural units. If a variable is not
35  listed at all, a KeyError will be raised
36  - file_handle = file handle made using e.g. open() command
37  - mass_shell_condition = string containing from Hit.mass_shell_variables
38  that determines how the mass shell condition will be calculated (or
39  set to '' to ignore)
40  Returns a new factory class
41  """
42  super(LineFactoryBase, self).__init__()
43  self.filehandle = filehandle
44  self.format_list = format_list
45  self.format_units_dict = format_units_dict
46  for format in format_list:
47  err_str = "Format key '"+str(format)+"' should be one of: "
48  for var in xboa.hit.Hit.set_variables():
49  err_str += var+", "
50  if format not in xboa.hit.Hit.set_variables():
51  raise KeyError(err_str)
52  if xboa.hit.Hit._default_var_types[format] == float and format != '':
53  if format not in self.format_units_dict:
54  raise KeyError("Missing unit for variable "+format)
55  unit = self.format_units_dict[format]
56  if unit not in common.units:
57  raise KeyError("Could not parse unit "+str(unit)+\
58  " for variable "+str(format))
59  self.mass_shell_condition = mass_shell_condition
60 
61  def make_hit(self):
62  """
63  Read the next line in the file handle and return a new hit object
64  """
65  hit = self._read_formatted(self.format_list,
66  self.format_units_dict,
67  self.filehandle,
69  if 'mass' not in self.format_list:
70  hit.set('mass', common.pdg_pid_to_mass[abs(hit['pid'])])
71  hit.mass_shell_condition(self.mass_shell_condition)
72  if 'charge' not in self.format_list:
73  hit.set('charge', common.pdg_pid_to_charge[hit['pid']])
74  return hit
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 using a user-defined format. ...
common module defines common utility data and functions that are used elsewhere
Definition: __init__.py:1
Implemented within this module:
Definition: __init__.py:1
def make_hit
Read the next line in the file handle and return a new hit object.