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