Belle II Software  release-06-02-00
SVDValidationTTreeRecoDigit.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 <header>
14  <contact> SVD Software Group, svd-software@belle2.org </contact>
15  <description>
16  This module is used for the SVD validation.
17  It gets information about ShaperDigits and RecoDigits, saving
18  in a ttree in a ROOT file.
19  </description>
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 a 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  digit_truehits = shaperDigits[digit.getArrayIndex()].getRelationsTo('SVDTrueHits')
75  # We want only digits with exactly one associated TrueHit
76  if len(digit_truehits) != 1:
77  continue
78  for truehit in digit_truehits:
79  # Sensor identification
80  sensorID = digit.getSensorID()
81  self.datadata.sensor_id = int(sensorID)
82  sensorNum = sensorID.getSensorNumber()
83  self.datadata.sensor = sensorNum
84  layerNum = sensorID.getLayerNumber()
85  self.datadata.layer = layerNum
86  if (layerNum == 3):
87  sensorType = 1 # Barrel
88  else:
89  if (sensorNum == 1):
90  sensorType = 0
91  else:
92  sensorType = 1
93  self.datadata.sensor_type = sensorType
94  ladderNum = sensorID.getLadderNumber()
95  self.datadata.ladder = ladderNum
96  if digit.isUStrip():
97  self.datadata.strip_dir = 0
98  else:
99  self.datadata.strip_dir = 1
100  self.datadata.recodigit_charge = digit.getCharge()
101  self.datadata.recodigit_time = digit.getTime()
102  self.datadata.truehit_time = truehit.getGlobalTime()
103  # Fill tree
104  self.filefile.cd()
105  self.treetree.Fill()
106 
107  def terminate(self):
108  """Close the output file. """
109  self.filefile.cd()
110  self.filefile.Write()
111  self.filefile.Close()
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:56