Belle II Software  release-08-01-10
PXDHitErrorsTTree.py
1 #!/usr/bin/env python3
2 
3 
10 
11 from ROOT import EventData
12 import math
13 import basf2 as b2
14 
15 # Some ROOT tools
16 import ROOT
17 from ROOT import Belle2
18 
19 from ROOT import gROOT, AddressOf
20 
21 # Define a ROOT struct to hold output data in the TTree.
22 gROOT.ProcessLine('struct EventData {\
23  int vxd_id;\
24  int layer;\
25  int ladder;\
26  int sensor;\
27  int truehit_index;\
28  int cluster_index;\
29  float truehit_u;\
30  float truehit_v;\
31  float truehit_time;\
32  float truehit_charge;\
33  float theta_u;\
34  float theta_v;\
35  float cluster_u;\
36  float cluster_v;\
37  float cluster_uError;\
38  float cluster_vError;\
39  float cluster_rho;\
40  float cluster_charge;\
41  float cluster_seed;\
42  float cluster_size;\
43  float cluster_uSize;\
44  float cluster_vSize;\
45  float cluster_uPull;\
46  float cluster_vPull;\
47 };'
48  )
49 
50 
51 class PXDHitErrorsTTree(b2.Module):
52 
53  """
54  A simple module to check the reconstruction of PXDTrueHits.
55  This module writes its output to a ROOT tree.
56  """
57 
58  def __init__(self):
59  """Initialize the module"""
60 
61  super().__init__()
62 
63  self.filefile = ROOT.TFile('PXDHitErrorOutput.root', 'recreate')
64 
65  self.treetree = ROOT.TTree('tree', 'Event data of PXD simulation')
66 
67  self.datadata = EventData()
68  # Declare tree branches
69  for key in EventData.__dict__:
70  if '__' not in key:
71  formstring = '/F'
72  if isinstance(self.datadata.__getattribute__(key), int):
73  formstring = '/I'
74  self.treetree.Branch(
75  key,
76  AddressOf(
77  self.datadata,
78  key),
79  key +
80  formstring)
81 
82  def beginRun(self):
83  """ Does nothing """
84 
85  def event(self):
86  """Find clusters with a truehit and print some stats."""
87 
88  clusters = Belle2.PyStoreArray('PXDClusters')
89 
90  # Start with clusters and use the relation to get the corresponding
91  # digits and truehits.
92  for cluster in clusters:
93  cluster_truehits = cluster.getRelationsTo('PXDTrueHits')
94 
95  # Here we ask only for clusters with exactly one TrueHit.
96  if len(cluster_truehits) != 1:
97  continue
98 
99  for truehit in cluster_truehits:
100  # Now let's store some data
101  # Sesnor identification
102  vxd_id = truehit.getSensorID()
103  self.data.vxd_id = vxd_id.getID()
104  self.data.layer = vxd_id.getLayerNumber()
105  self.data.ladder = vxd_id.getLadderNumber()
106  self.data.sensor = vxd_id.getSensorNumber()
107  self.data.truehit_index = truehit.getArrayIndex()
108  self.data.cluster_index = cluster.getArrayIndex()
109 
110  # Get sensor geometry information
111  sensor_info = Belle2.VXD.GeoCache.get(vxd_id)
112  thickness = sensor_info.getThickness()
113 
114  # TrueHit information
115  self.data.truehit_u = truehit.getU()
116  self.data.truehit_v = truehit.getV()
117  self.data.truehit_time = truehit.getGlobalTime()
118  self.data.truehit_charge = truehit.getEnergyDep()
119  self.data.theta_u = math.atan2(
120  truehit.getExitU() - truehit.getEntryU(), thickness)
121  self.data.theta_v = math.atan2(
122  truehit.getExitV() - truehit.getEntryV(), thickness)
123  # Cluster information
124  self.data.cluster_u = cluster.getU()
125  self.data.cluster_v = cluster.getV()
126  self.data.cluster_uError = cluster.getUSigma()
127  self.data.cluster_vError = cluster.getVSigma()
128  self.data.cluster_rho = cluster.getRho()
129  self.data.cluster_charge = cluster.getCharge()
130  self.data.cluster_seed = cluster.getSeedCharge()
131  self.data.cluster_size = cluster.getSize()
132  self.data.cluster_uSize = cluster.getUSize()
133  self.data.cluster_vSize = cluster.getVSize()
134  self.data.cluster_uPull = (
135  cluster.getU() - truehit.getU()) / cluster.getUSigma()
136  self.data.cluster_vPull = (
137  cluster.getV() - truehit.getV()) / cluster.getVSigma()
138  self.file.cd()
139  self.tree.Fill()
140 
141  def terminate(self):
142  """ Close the output file."""
143 
144  self.filefile.cd()
145  self.filefile.Write()
146  self.filefile.Close()
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
static const SensorInfoBase & get(Belle2::VxdID id)
Return a reference to the SensorInfo of a given SensorID.
Definition: GeoCache.h:139
data
Instance of EventData class.