Belle II Software  release-05-01-25
SVDValidationTTreeSimhit.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 truehits and clusters, saving
10  in a ttree in a ROOT file.
11  </description>
12 </header>
13 """
14 import sys
15 import math
16 
17 from basf2 import *
18 
19 # Some ROOT tools
20 import ROOT
21 from ROOT import Belle2 # make Belle2 namespace available
22 from ROOT import gROOT, AddressOf
23 from ROOT import PyConfig
24 from ROOT import TVector3
25 
26 # Define a ROOT struct to hold output data in the TTree
27 gROOT.ProcessLine('struct EventDataSimhit {\
28  int sensor_id;\
29  int layer;\
30  int ladder;\
31  int sensor;\
32  int sensor_type;\
33  float simhit_length;\
34  float simhit_energy;\
35  float simhit_dEdx;\
36  };')
37 
38 from ROOT import EventDataSimhit
39 
40 
42  '''class to create sim hit ttree'''
43 
44  def __init__(self):
45  """Initialize the module"""
46 
47  super(SVDValidationTTreeSimhit, self).__init__()
48  self.file = ROOT.TFile('../SVDValidationTTreeSimhit.root', 'recreate')
49  '''Output ROOT file'''
50  self.tree = ROOT.TTree('tree', 'Event data of SVD validation events')
51  '''TTrees for output data'''
52  self.data = EventDataSimhit()
53  '''Instance of the EventDataSimhit class'''
54 
55  # Declare tree branches
56  for key in EventDataSimhit.__dict__:
57  if '__' not in key:
58  formstring = '/F'
59  if isinstance(self.data.__getattribute__(key), int):
60  formstring = '/I'
61  self.tree.Branch(key, AddressOf(self.data, key), key + formstring)
62 
63  def event(self):
64  """Find simhits with a truehit and save needed information"""
65 
66  # Start with truehits and use the relation to get the corresponding
67  # simhits
68  svd_truehits = Belle2.PyStoreArray('SVDTrueHits')
69  for truehit in svd_truehits:
70  simhits = truehit.getRelationsTo('SVDSimHits')
71  for simhit in simhits:
72  # Sensor identification
73  sensorID = simhit.getSensorID()
74  self.data.sensor_id = int(sensorID)
75  sensorNum = sensorID.getSensorNumber()
76  self.data.sensor = sensorNum
77  layerNum = sensorID.getLayerNumber()
78  self.data.layer = layerNum
79  ladderNum = sensorID.getLadderNumber()
80  self.data.ladder = ladderNum
81  if (layerNum == 3):
82  sensorType = 1
83  else:
84  if (sensorNum == 1):
85  sensorType = 0
86  else:
87  sensorType = 1
88  self.data.sensor_type = sensorType
89  # We are interested only in the primary particles, not
90  # delta electrons, etc.
91  particle = simhit.getRelatedFrom('MCParticles')
92  if not particle.hasStatus(Belle2.MCParticle.c_PrimaryParticle):
93  continue
94 
95  length = (simhit.getPosOut() - simhit.getPosIn()).Mag()
96  # The deposited energy is the number of electrons multiplied
97  # by the energy required to create an electron-hole pair
98  energy = simhit.getElectrons() * Belle2.Const.ehEnergy
99  self.data.simhit_length = length
100  self.data.simhit_energy = energy
101  self.data.simhit_dEdx = energy / length
102  # A reasonable cut to see a nice Landau distribution
103  if self.data.simhit_dEdx > 0.015:
104  continue
105  # Fill tree
106  self.file.cd()
107  self.tree.Fill()
108 
109  def terminate(self):
110  """Close the output file. """
111  self.file.cd()
112  self.file.Write()
113  self.file.Close()
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit.terminate
def terminate(self)
Definition: SVDValidationTTreeSimhit.py:109
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit.event
def event(self)
Definition: SVDValidationTTreeSimhit.py:63
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit.__init__
def __init__(self)
Definition: SVDValidationTTreeSimhit.py:44
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit.tree
tree
Definition: SVDValidationTTreeSimhit.py:50
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit.file
file
Definition: SVDValidationTTreeSimhit.py:48
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit
Definition: SVDValidationTTreeSimhit.py:41
SVDValidationTTreeSimhit.SVDValidationTTreeSimhit.data
data
Definition: SVDValidationTTreeSimhit.py:52