Belle II Software  release-05-01-25
SVDValidationTTreeStrip.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 """
5 <header>
6  <contact>G. Caria, gcaria@student.unimelb.edu.au</contact>
7  <description>
8  This module is used for the SVD validation.
9  It gets information about digits, saving
10  in a ttree in a ROOT file.
11  </description>
12 </header>
13 """
14 import sys
15 import math
16 import xml.etree.ElementTree as ET
17 
18 from basf2 import *
19 
20 # Some ROOT tools
21 import ROOT
22 from ROOT import Belle2 # make Belle2 namespace available
23 from ROOT import gROOT, AddressOf
24 from ROOT import PyConfig
25 from ROOT import TVector3
26 
27 # Define a ROOT struct to hold output data in the TTree
28 gROOT.ProcessLine('struct EventDataStrip {\
29  int sensor_id;\
30  int layer;\
31  int ladder;\
32  int sensor;\
33  int sensor_type;\
34  int strip_dir;\
35  float strip_charge;\
36  int strip_noise;\
37  };')
38 
39 from ROOT import EventDataStrip
40 
41 
43  '''class to create the strip ttree'''
44 
45  def __init__(self):
46  """Initialize the module"""
47 
48  super(SVDValidationTTreeStrip, self).__init__()
49  self.file = ROOT.TFile('../SVDValidationTTreeStrip.root', 'recreate')
50  '''Output ROOT file'''
51  self.tree = ROOT.TTree('tree', 'Event data of SVD validation events')
52  '''TTrees for output data'''
53  self.data = EventDataStrip()
54  '''Instance of the EventDataStrip class'''
55 
56  # Declare tree branches
57  for key in EventDataStrip.__dict__:
58  if '__' not in key:
59  formstring = '/F'
60  if isinstance(self.data.__getattribute__(key), int):
61  formstring = '/I'
62  self.tree.Branch(key, AddressOf(self.data, key), key + formstring)
63 
64  def event(self):
65  """Find digit with a cluster and save needed information"""
66  # Before starting iterating on clusters gets noise value from xml file
67  # so that it doesn't have to be repeated for every digit
68  tree = ET.parse(Belle2.FileSystem.findFile('svd/data/SVD-Components.xml'))
69  root = tree.getroot()
70  for sensor in root.findall('Sensor'):
71  if sensor.get('type') == 'Layer3':
72  if len(sensor.findall('Active')) == 1:
73  active = sensor.find('Active')
74  NoiseULayer3 = int(active.find('ElectronicNoiseU').text)
75  NoiseVLayer3 = int(active.find('ElectronicNoiseV').text)
76  for sensorbase in root.iter('SensorBase'):
77  if sensorbase.get('type') == 'Barrel':
78  active = sensorbase.find('Active')
79  NoiseUBarrel = int(active.find('ElectronicNoiseU').text)
80  NoiseVBarrel = int(active.find('ElectronicNoiseV').text)
81  elif sensorbase.get('type') == 'Slanted':
82  active = sensorbase.find('Active')
83  NoiseUSlanted = int(active.find('ElectronicNoiseU').text)
84  NoiseVSlanted = int(active.find('ElectronicNoiseV').text)
85 
86  # Start with clusters and use the relation to get the corresponding
87  # digits
88  clusters = Belle2.PyStoreArray('SVDClusters')
89  for cluster in clusters:
90 
91  cls_strip_ids = []
92 
93  cluster_truehits = cluster.getRelationsTo('SVDTrueHits')
94  # We want only clusters with exactly one associated TrueHit
95  if len(cluster_truehits) != 1:
96  continue
97  digits = cluster.getRelationsTo('SVDRecoDigits')
98 
99  # get all the strip IDs of this cluster
100  for digit in digits:
101  if digit.getCellID() not in cls_strip_ids:
102  cls_strip_ids.append(digit.getCellID())
103 
104  # get the strip charge as the highest charge among its digits
105  for strip_id in cls_strip_ids:
106  # print("strip_id is : " + str(strip_id))
107  strip_charge = 0
108  for digit in digits:
109  if strip_id != digit.getCellID():
110  continue
111 
112  # print("this digit's charge: " + str(digit.getCharge()))
113 
114  if(digit.getCharge() > strip_charge):
115  strip_charge = digit.getCharge()
116 
117  # Sensor identification
118  sensorID = cluster.getSensorID()
119  self.data.sensor_id = int(sensorID)
120  sensorNum = sensorID.getSensorNumber()
121  self.data.sensor = sensorNum
122  layerNum = sensorID.getLayerNumber()
123  self.data.layer = layerNum
124  if (layerNum == 3):
125  sensorType = 1
126  else:
127  if (sensorNum == 1):
128  sensorType = 0
129  else:
130  sensorType = 1
131  self.data.sensor_type = sensorType
132  ladderNum = sensorID.getLadderNumber()
133  self.data.ladder = ladderNum
134  self.data.strip_charge = strip_charge
135  # Find what noise value should be used
136  # depending on layer and sensor number
137  if digit.isUStrip():
138  strip_dir = 0
139  if (layerNum == 3):
140  noise = NoiseULayer3
141  else:
142  if (sensorNum == 1):
143  noise = NoiseUSlanted
144  else:
145  noise = NoiseUBarrel
146  else:
147  strip_dir = 1
148  if (layerNum == 3):
149  noise = NoiseVLayer3
150  else:
151  if (sensorNum == 1):
152  noise = NoiseVSlanted
153  else:
154  noise = NoiseVBarrel
155  self.data.strip_dir = strip_dir
156  self.data.strip_noise = noise
157  # Fill tree
158  self.file.cd()
159  self.tree.Fill()
160 
161  def terminate(self):
162  """Close the output file. """
163  self.file.cd()
164  self.file.Write()
165  self.file.Close()
SVDValidationTTreeStrip.SVDValidationTTreeStrip.file
file
Definition: SVDValidationTTreeStrip.py:49
SVDValidationTTreeStrip.SVDValidationTTreeStrip
Definition: SVDValidationTTreeStrip.py:42
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
SVDValidationTTreeStrip.SVDValidationTTreeStrip.event
def event(self)
Definition: SVDValidationTTreeStrip.py:64
SVDValidationTTreeStrip.SVDValidationTTreeStrip.__init__
def __init__(self)
Definition: SVDValidationTTreeStrip.py:45
Belle2::FileSystem::findFile
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:147
SVDValidationTTreeStrip.SVDValidationTTreeStrip.tree
tree
Definition: SVDValidationTTreeStrip.py:51
SVDValidationTTreeStrip.SVDValidationTTreeStrip.data
data
Definition: SVDValidationTTreeStrip.py:53
SVDValidationTTreeStrip.SVDValidationTTreeStrip.terminate
def terminate(self)
Definition: SVDValidationTTreeStrip.py:161