Belle II Software  release-08-01-10
SVDValidationTTreeRecoDigit.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 <header>
14  <description>
15  This module is used for the SVD validation.
16  It gets information about RecoDigits, saving
17  in a ttree in a ROOT file.
18  </description>
19  <noexecute>SVD validation helper class</noexecute>
20 </header>
21 """
22 
23 import basf2 as b2
24 
25 # Some ROOT tools
26 import ROOT
27 from ROOT import Belle2
28 from ROOT import gROOT, addressof
29 
30 # Define a ROOT struct to hold output data in the TTree
31 gROOT.ProcessLine('struct EventDataRecoDigit {\
32  int sensor_id;\
33  int layer;\
34  int ladder;\
35  int sensor;\
36  int sensor_type;\
37  int strip_dir;\
38  float recodigit_charge;\
39  float recodigit_time;\
40  float truehit_time;\
41 };')
42 
43 from ROOT import EventDataRecoDigit # noqa
44 
45 
46 class SVDValidationTTreeRecoDigit(b2.Module):
47  '''class to create reco digitis ttree'''
48 
49  def __init__(self):
50  """Initialize the module"""
51 
52  super(SVDValidationTTreeRecoDigit, self).__init__()
53 
54  self.filefile = ROOT.TFile('../SVDValidationTTreeRecoDigit.root', 'recreate')
55 
56  self.treetree = ROOT.TTree('tree', 'Event data of SVD validation events')
57 
58  self.datadata = EventDataRecoDigit()
59 
60  # Declare tree branches
61  for key in EventDataRecoDigit.__dict__:
62  if '__' not in key:
63  formstring = '/F'
64  if isinstance(self.datadata.__getattribute__(key), int):
65  formstring = '/I'
66  self.treetree.Branch(key, addressof(self.datadata, key), key + formstring)
67 
68  def event(self):
69  """Take digits from SVDRecoDigits with at least one truehit and save needed information"""
70  digits = Belle2.PyStoreArray('SVDRecoDigits')
71  shaperDigits = Belle2.PyStoreArray('SVDShaperDigits')
72  for digit in digits:
73  # get the true hit from the related SVDShaperDigit
74  # it works because there is a 1-to-1 correspondence between
75  # ShaperDigits and RecoDigits
76  digit_truehits = shaperDigits[digit.getArrayIndex()].getRelationsTo('SVDTrueHits')
77 
78  if len(digit_truehits) == 0:
79  continue
80 
81  # find the trueHit with highest energy deposit (the "best" match)
82  energy = 0
83  bestTrueHitIndex = 0
84 
85  for i, trueHit in enumerate(digit_truehits):
86  if trueHit.getEnergyDep() > energy:
87  energy = trueHit.getEnergyDep()
88  bestTrueHitIndex = i
89  bestTrueHit = digit_truehits[bestTrueHitIndex]
90 
91  # Sensor identification
92  sensorID = digit.getSensorID()
93  self.datadata.sensor_id = int(sensorID)
94  sensorNum = sensorID.getSensorNumber()
95  self.datadata.sensor = sensorNum
96  layerNum = sensorID.getLayerNumber()
97  self.datadata.layer = layerNum
98  if (layerNum == 3):
99  sensorType = 1 # Barrel
100  else:
101  if (sensorNum == 1):
102  sensorType = 0
103  else:
104  sensorType = 1
105  self.datadata.sensor_type = sensorType
106  ladderNum = sensorID.getLadderNumber()
107  self.datadata.ladder = ladderNum
108  if digit.isUStrip():
109  self.datadata.strip_dir = 0
110  else:
111  self.datadata.strip_dir = 1
112  self.datadata.recodigit_charge = digit.getCharge()
113  self.datadata.recodigit_time = digit.getTime()
114  self.datadata.truehit_time = bestTrueHit.getGlobalTime()
115  # Fill tree
116  self.filefile.cd()
117  self.treetree.Fill()
118 
119  def terminate(self):
120  """Close the output file. """
121  self.filefile.cd()
122  self.filefile.Write()
123  self.filefile.Close()
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72