Belle II Software  release-05-01-25
PXDValidationTTreeSimHit.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 from ROOT import gROOT, AddressOf
12 
13 # Define a ROOT struct to hold output data in the TTree.
14 gROOT.ProcessLine('struct EventDataSimHit {\
15  unsigned long exp;\
16  unsigned long run;\
17  unsigned long evt;\
18  int vxd_id;\
19  int layer;\
20  int ladder;\
21  int sensor;\
22  int simhit_index;\
23  int truehit_index;\
24  int pixel_type;\
25  int simhit_PDGcode;\
26  float simhit_PosInX;\
27  float simhit_PosInY;\
28  float simhit_PosInZ;\
29  float simhit_PosOutX;\
30  float simhit_PosOutY;\
31  float simhit_PosOutZ;\
32  float simhit_Length;\
33  float simhit_EnergyDep;\
34  float simhit_GlobalTime;\
35 };'
36  )
37 
38 from ROOT import EventDataSimHit
39 
40 
42 
43  """
44  A simple module to check the simulation of PXDTrueHits with Geant4 steps.
45  This module writes its output to a ROOT tree.
46  Primary goal is supporting of validation plots
47  """
48 
49  def __init__(self):
50  """Initialize the module"""
51 
52  super(PXDValidationTTreeSimHit, self).__init__()
53 
54  self.file = ROOT.TFile('PXDValidationTTreeSimHitOutput.root',
55  'recreate')
56 
57  self.tree = ROOT.TTree('tree', 'Event data of PXD simulation')
58 
59  self.data = EventDataSimHit()
60  # Declare tree branches
61  for key in EventDataSimHit.__dict__:
62  if '__' not in key:
63  formstring = '/F'
64  if isinstance(self.data.__getattribute__(key), int):
65  formstring = '/I'
66  self.tree.Branch(key, AddressOf(self.data, key), key + formstring)
67 
68  def beginRun(self):
69  """ Does nothing """
70 
71  def event(self):
72  """Find simhits with a truehit and save needed information."""
73 
74  # Start with truehits and use the relation to get the corresponding
75  # simhits.
76  pxd_truehits = Belle2.PyStoreArray('PXDTrueHits')
77  for truehit in pxd_truehits:
78  pxd_simhits = truehit.getRelationsTo('PXDSimHits')
79  for simhit in pxd_simhits:
80  # Now let's store some data
81  # Event identification
82  self.data.exp = Belle2.PyStoreObj(
83  'EventMetaData').obj().getExperiment()
84  self.data.run = Belle2.PyStoreObj(
85  'EventMetaData').obj().getRun()
86  self.data.evt = Belle2.PyStoreObj(
87  'EventMetaData').obj().getEvent()
88  # Sesnor identification
89  vxd_id = simhit.getSensorID()
90  self.data.vxd_id = vxd_id.getID()
91  self.data.layer = vxd_id.getLayerNumber()
92  self.data.ladder = vxd_id.getLadderNumber()
93  self.data.sensor = vxd_id.getSensorNumber()
94  sensor_info = Belle2.VXD.GeoCache.get(vxd_id)
95  VPitch = sensor_info.getVPitch(simhit.getPosIn().Y())
96  self.data.pixel_type = (vxd_id.getLayerNumber() - 1) * 2
97  if vxd_id.getLayerNumber() == 1:
98  if VPitch > 0.0058:
99  self.data.pixel_type += 1
100  if vxd_id.getLayerNumber() == 2:
101  if VPitch > 0.0080:
102  self.data.pixel_type += 1
103  # Hit identification
104  self.data.simhit_index = simhit.getArrayIndex()
105  self.data.truehit_index = truehit.getArrayIndex()
106  # SimHit information
107  self.data.simhit_PDGcode = simhit.getPDGcode()
108  self.data.simhit_PosInX = simhit.getPosIn().X()
109  self.data.simhit_PosInY = simhit.getPosIn().Y()
110  self.data.simhit_PosInZ = simhit.getPosIn().Z()
111  self.data.simhit_PosOutX = simhit.getPosOut().X()
112  self.data.simhit_PosOutY = simhit.getPosOut().Y()
113  self.data.simhit_PosOutZ = simhit.getPosOut().Z()
114  self.data.simhit_Length = (simhit.getPosOut() - simhit.getPosIn()).Mag()
115  self.data.simhit_EnergyDep = simhit.getElectrons()
116  self.data.simhit_GlobalTime = simhit.getGlobalTime()
117  # Fill tree
118  self.file.cd()
119  self.tree.Fill()
120 
121  def terminate(self):
122  """ Close the output file."""
123 
124  self.file.cd()
125  self.file.Write()
126  self.file.Close()
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.__init__
def __init__(self)
Definition: PXDValidationTTreeSimHit.py:49
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
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.beginRun
def beginRun(self)
Definition: PXDValidationTTreeSimHit.py:68
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.data
data
Instance of EventData class.
Definition: PXDValidationTTreeSimHit.py:59
Belle2::PyStoreObj
a (simplified) python wrapper for StoreObjPtr.
Definition: PyStoreObj.h:69
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit
Definition: PXDValidationTTreeSimHit.py:41
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.file
file
Output ROOT file.
Definition: PXDValidationTTreeSimHit.py:54
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.event
def event(self)
Definition: PXDValidationTTreeSimHit.py:71
Belle2::getRun
static ExpRun getRun(map< ExpRun, pair< double, double >> runs, double t)
Get exp number + run number from time.
Definition: Splitter.cc:262
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.tree
tree
TTree for output data.
Definition: PXDValidationTTreeSimHit.py:57
PXDValidationTTreeSimHit.PXDValidationTTreeSimHit.terminate
def terminate(self)
Definition: PXDValidationTTreeSimHit.py:121
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58