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