xboa
_maus_tracking.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 """
18 \namespace xboa::tracking::_maus_tracking
19 
20 Should be imported directly from the xboa::tracking namespace
21 """
22 
23 import json
24 try:
25  import maus_cpp.globals
26  import maus_cpp.simulation
27 except ImportError:
28  pass
29 
30 from xboa import common
31 from xboa.hit import Hit
32 
33 from _tracking_base import TrackingBase
34 
35 class MAUSTracking(TrackingBase):
36  """
37  Provides an interface to MAUS tracking routines for use by xboa.algorithms
38  """
39  def __init__(self, datacards=None, seed=0):
40  """
41  Ensure MAUS is initialised, ready for tracking
42  - datacards json document containing datacards that will be used for
43  initialise MAUS. If datacards is None, TrackingMAUS will not
44  attempt to initialise MAUS and will not check that MAUS is
45  initialised.
46  - seed initial random seed; each time a particle is fired, the seed
47  increments by 1
48  Raises an ImportError if maus is not installed or maus environment is
49  not sourced
50  """
51  common.has_maus()
52  TrackingBase.__init__(self)
53  if type(datacards) == type({}) or type(datacards) == type([]):
54  datacards = json.dumps(datacards)
55  if datacards != None:
56  if maus_cpp.globals.has_instance():
57  maus_cpp.globals.death()
58  maus_cpp.globals.birth(datacards)
59  self.random_seed = seed
60  self.spill = 0
61 
62  def track_one(self, hit):
63  """
64  Track a hit and return a list of output hits
65  - hit initial Hit - particle to be tracked (of Hit type)
66 
67  Track a hit and return a list of output hits. The output hits should
68  correspond to e.g. particle crossings over cell ends, depending on the
69  usage of the Tracking object; see algorithm documentation.
70 
71  Spill number will increment by one for each call to track_one or
72  track_many
73  """
74  return self.track_many([hit])[0]
75 
76 
77  def track_many(self, list_of_hits):
78  """
79  Track many hits and return a list of list of output hits
80  - list_of_hits list of Hits - initial particles to be tracked
81 
82  Track many hits and return a list containing a list of output hits,
83  one for each track. This provides a hook for tracking codes that have
84  significant set up and tear down times, or which simulate collective
85  effects that need to be taken into account by the algorithm
86 
87  Spill number will increment by one for each call to track_one or
88  track_many
89  """
90  primary_list = [self._hit_to_primary(hit) for hit in list_of_hits]
91  primary_str = json.dumps(primary_list)
92  mc_events_str = maus_cpp.simulation.track_particles(primary_str)
93  mc_events = json.loads(mc_events_str)
94  hit_list_of_lists = [self._mc_event_to_hit_list(event, i) \
95  for i, event in enumerate(mc_events)]
96  self.spill += 1
97  return hit_list_of_lists
98 
99  def _mc_event_to_hit_list(self, mc_event, event_number):
100  """
101  Make a list of data from a MAUS mc event
102  """
103  hit_list = [self._primary_to_xboa_hit(mc_event["primary"],
104  event_number, 1)]
105  if "virtual_hits" in mc_event:
106  hit_list += [self._virtual_hit_to_xboa_hit(vhit, event_number) \
107  for vhit in mc_event["virtual_hits"]]
108  return hit_list
109 
110  def _virtual_hit_to_xboa_hit(self, virtual_hit, event):
111  """
112  Fill hit data from a MAUS virtual hit
113  - virtual_hit json object corresponding to the hit
114  - event integer event number
115  """
116  hit = Hit()
117  pid = virtual_hit["particle_id"]
118  for key in 'x', 'y', 'z':
119  hit[key] = virtual_hit['position'][key]
120  hit["p"+key] = virtual_hit['momentum'][key]
121  hit["pid"] = pid
122  hit["t"] = virtual_hit["time"]
123  hit["mass"] = common.pdg_pid_to_mass[abs(pid)]
124  hit["charge"] = common.pdg_pid_to_charge[pid]
125  hit["particle_number"] = virtual_hit["track_id"]
126  hit["event_number"] = event
127  hit["spill"] = self.spill
128  hit["station"] = virtual_hit["station_id"]
129  hit.mass_shell_condition("energy")
130  return hit
131 
132  def _primary_to_xboa_hit(self, primary, event, particle):
133  """
134  Fill hit data from a MAUS primary
135  """
136  hit = Hit()
137  pid = primary["particle_id"]
138  for key in 'x', 'y', 'z':
139  hit[key] = primary['position'][key]
140  hit["p"+key] = primary['momentum'][key]
141  hit["pid"] = pid
142  hit["energy"] = primary["energy"]
143  hit["t"] = primary["time"]
144  hit["mass"] = common.pdg_pid_to_mass[abs(pid)]
145  hit["charge"] = common.pdg_pid_to_charge[pid]
146  hit["particle_number"] = particle
147  hit["event_number"] = event
148  hit["spill"] = self.spill
149  hit["station"] = -1
150  return hit
151 
152  def _hit_to_primary(self, hit):
153  """
154  Fill primary data from a hit
155  """
156  primary = {'primary':
157  {
158  'position':{'x':hit["x"], 'y':hit["y"], 'z':hit["z"]},
159  'momentum':{'x':hit["px"], 'y':hit["py"], 'z':hit["pz"]},
160  'particle_id':hit["pid"],
161  'energy':hit["energy"],
162  'time':hit["t"],
163  'random_seed':self.random_seed
164  }
165  }
166  self.random_seed += 1
167  return primary
168 
169 
def track_one
Track a hit and return a list of output hits.
def track_many
Track many hits and return a list of list of output hits.
Provides an interface to MAUS tracking routines for use by xboa.algorithms.
def _mc_event_to_hit_list
Make a list of data from a MAUS mc event.
def _virtual_hit_to_xboa_hit
Fill hit data from a MAUS virtual hit.
Implemented within this module:
Definition: __init__.py:1
def _primary_to_xboa_hit
Fill hit data from a MAUS primary.
def _hit_to_primary
Fill primary data from a hit.