xboa
_maus_root_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 json
19 import ROOT
20 import xboa.common as common
21 import xboa.hit.factory
22 from xboa.hit.factory import HitFactoryBase
23 
24 class MausRootHitFactory(HitFactoryBase):
25  """
26  MausRootHitFactory reads hits of a specified type from a root spill
27  """
28  def __init__(self, root_tree, format, entry = 0):
29  """
30  Initialise the hit factory
31  - root_tree: a object of type ROOT.TTree that should contain a branch
32  called "Data" containing a list of MAUS.ROOT.TTrees
33  - format: format of data to be extracted from the ROOT document
34  - entry: entry that will be read to get the ROOT data
35  """
36  if format not in self.file_types():
37  raise KeyError("Did not recognise format "+str(format))
38  self.entry = entry
39  self.hits = []
40  self.format = format[10:] # strip maus_root_
41  self.tree = root_tree
42  self.spill = None
43 
44  def new_spill(self):
45  data = ROOT.MAUS.Data()
46  self.tree.SetBranchAddress("data", data)
47  if self.entry >= self.tree.GetEntries():
48  raise IndexError("Out of entries")
49  self.tree.GetEntry(self.entry)
50  self.spill = data.GetSpill()
51  self.spill_number = self.spill.GetSpillNumber()
52  self._read_maus_spill()
53  self.entry += 1
54 
55  def make_hit(self):
56  """
57  Return the next hit from the given spill document
58  """
59  try:
60  while len(self.hits) == 0:
61  self.new_spill()
62  return self.hits.pop(0)
63  except IndexError:
64  raise xboa.hit.BadEventError("Run out of events")
65 
66  def _read_maus_spill(self):
67  """
68  Read a MAUS spill, converting to MAUS objects
69  """
70  if self.format == 'virtual_hit':
71  self._read_virtual_hits()
72  elif self.format == 'primary':
74 
75  def _read_virtual_hits(self):
76  """
77  Read virtuals from the Spill
78  """
79  for event, mc_event in enumerate(self.spill.GetMCEvents()):
80  for virtual_hit in mc_event.GetVirtualHits():
81  hit = xboa.hit.Hit()
82  hit["x"] = virtual_hit.GetPosition().x()
83  hit["y"] = virtual_hit.GetPosition().y()
84  hit["z"] = virtual_hit.GetPosition().z()
85  hit["px"] = virtual_hit.GetMomentum().x()
86  hit["py"] = virtual_hit.GetMomentum().y()
87  hit["pz"] = virtual_hit.GetMomentum().z()
88  hit["bx"] = virtual_hit.GetBField().x()
89  hit["by"] = virtual_hit.GetBField().y()
90  hit["bz"] = virtual_hit.GetBField().z()
91  hit["ex"] = virtual_hit.GetEField().x()
92  hit["ey"] = virtual_hit.GetEField().y()
93  hit["ez"] = virtual_hit.GetEField().z()
94  hit["pid"] = virtual_hit.GetParticleId()
95  hit["station"] = virtual_hit.GetStationId()
96  hit["particle_number"] = virtual_hit.GetTrackId()
97  hit["t"] = virtual_hit.GetTime()
98  hit["mass"] = virtual_hit.GetMass()
99  hit["charge"] = virtual_hit.GetCharge()
100  hit["proper_time"] = virtual_hit.GetProperTime()
101  hit["path_length"] = virtual_hit.GetPathLength()
102  hit["spill"] = self.spill_number
103  hit["event_number"] = event
104  hit.mass_shell_condition('energy')
105  self.hits.append(hit)
106 
107  def _read_primaries(self):
108  """
109  Read primaries from the Spill
110  """
111  for event, mc_event in enumerate(self.spill.GetMCEvents()):
112  hit = xboa.hit.Hit()
113  primary = mc_event.GetPrimary()
114  hit["x"] = primary.GetPosition().x()
115  hit["y"] = primary.GetPosition().y()
116  hit["z"] = primary.GetPosition().z()
117  hit["px"] = primary.GetMomentum().x()
118  hit["py"] = primary.GetMomentum().y()
119  hit["pz"] = primary.GetMomentum().z()
120  hit["pid"] = primary.GetParticleId()
121  hit["energy"] = primary.GetEnergy()
122  hit["spill"] = self.spill_number
123  hit["event_number"] = event
124  hit["t"] = primary.GetTime()
125  try:
126  hit["mass"] = common.pdg_pid_to_mass[abs(hit["pid"])]
127  except KeyError:
128  hit["mass"] = 0.
129  self.bad_pid(hit["pid"])
130  try:
131  hit["charge"] = common.pdg_pid_to_charge[hit["charge"]]
132  except KeyError:
133  hit["charge"] = 0.
134  self.bad_pid(hit["pid"])
135  hit.mass_shell_condition(self._file_mass_shell[self.format])
136  self.hits.append(hit)
137 
138  @classmethod
139  def file_types(cls):
140  """
141  List of file types that can be read by this class
142  """
143  return ["maus_root_virtual_hit", "maus_root_primary"]
144 
145  _file_mass_shell = {
146  "virtual_hit":"energy",
147  "primary":"p"
148  }
The hit factory module defines a number of factory classes used for generating hit objects...
Definition: __init__.py:1
def _read_maus_spill
Read a MAUS spill, converting to MAUS objects.
MausRootHitFactory reads hits of a specified type from a root spill.
def make_hit
Return the next hit from the given spill document.
common module defines common utility data and functions that are used elsewhere
Definition: __init__.py:1
def file_types
List of file types that can be read by this class.