Belle II Software  release-08-01-10
SetSensitiveThreshold.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import basf2 as b2
13 
14 # Import xml parser
15 import xml.etree.ElementTree as xml
16 
17 # Load the required libraries
18 
19 b2.logging.log_level = b2.LogLevel.WARNING
20 
21 
22 class SetVXDSensitiveThreshold(b2.Module):
23 
24  """A utility module to manipulate the threshold on deposited energy
25  in PXD and SVD SensitiveDetector.
26  """
27 
28  def __init__(self, threshold):
29  """Initialize the module"""
30 
31  super(SetVXDSensitiveThreshold, self).__init__()
32 
33  self.thresholdthreshold = threshold
34 
35  self.old_pxd_thresholdold_pxd_threshold = 0.0
36 
37  self.old_svd_thresholdold_svd_threshold = 0.0
38 
39  self.pathpath = 'data/{comp}/{COMP}.xml'
40 
41  self.xmlpathxmlpath = 'Content/SensitiveThreshold'
42 
43  def set_threshold(self, component, value):
44  """ Set the threshold value in PXD or SVD file."""
45 
46  filename = self.pathpath.format(comp=component.lower(),
47  COMP=component.upper())
48  tree = xml.parse(filename)
49  for node in tree.getroot().findall(self.xmlpathxmlpath):
50  if component.lower() == 'pxd':
51  self.old_pxd_thresholdold_pxd_threshold = float(node.text)
52  else:
53  self.old_svd_thresholdold_svd_threshold = float(node.text)
54  node.text = '{val}'.format(val=value)
55 
56  file = open(filename, 'w')
57  tree.write(file, encoding='UTF-8', xml_declaration=True)
58  file.close()
59 
60  def initialize(self):
61  """ Set the required threshold value """
62 
63  self.set_thresholdset_threshold('PXD', self.thresholdthreshold)
64  self.set_thresholdset_threshold('SVD', self.thresholdthreshold)
65 
66  def beginRun(self):
67  """ Do nothing. """
68 
69  def event(self):
70  """Do nothing."""
71 
72  def terminate(self):
73  """ Set the previuos threshold value """
74 
75  self.set_threshold('PXD', self.old_pxd_threshold)
76  self.set_threshold('SVD', self.old_svd_threshold)
77 
78 
79 # Particle gun module
80 particlegun = b2.register_module('ParticleGun')
81 # Create Event information
82 eventinfosetter = b2.register_module('EventInfoSetter')
83 # Show progress of processing
84 progress = b2.register_module('Progress')
85 # Manipulate the energy threshold in VXD SensitiveDetectors.
86 set_thr = SetVXDSensitiveThreshold(-0.01)
87 # Load parameters
88 gearbox = b2.register_module('Gearbox')
89 # Create geometry
90 geometry = b2.register_module('Geometry')
91 # Run simulation
92 simulation = b2.register_module('FullSim')
93 # PXD digitization module
94 pxddigi = b2.register_module('PXDDigitizer')
95 # PXD clustering module
96 pxdclust = b2.register_module('PXDClusterizer')
97 # SVD digitization module
98 svddigi = b2.register_module('SVDDigitizer')
99 # SVD clustering module
100 svdclust = b2.register_module('SVDClusterizer')
101 # Simpleoutput
102 output = b2.register_module('RootOutput')
103 
104 # Specify number of events to generate
105 eventinfosetter.param({'evtNumList': [10], 'runList': [1]})
106 
107 # Set parameters for particlegun
108 particlegun.param({ # Generate 5 tracks on average
109  # the number of tracks is a Poisson variate
110  # Generate pi+, pi-, e+ and e-
111  # with a normal distributed transversal momentum
112  # with a center of 5 GeV and a width of 1 GeV
113  # a normal distributed phi angle,
114  # center of 180 degree and a width of 30 degree
115  # Generate theta angles uniform in cos theta
116  # between 17 and 150 degree
117  # normal distributed vertex generation
118  # around the origin with a sigma of 2cm in the xy plane
119  # and no deviation in z
120  # all tracks sharing the same vertex per event
121  'nTracks': 1,
122  'varyNTracks': True,
123  'pdgCodes': [211, -211, 11, -11],
124  'momentumGeneration': 'normalPt',
125  'momentumParams': [5, 1],
126  'phiGeneration': 'normal',
127  'phiParams': [180, 30],
128  'thetaGeneration': 'uniformCosinus',
129  'thetaParams': [17, 150],
130  'vertexGeneration': 'normal',
131  'xVertexParams': [0, 2],
132  'yVertexParams': [0, 2],
133  'zVertexParams': [0, 0],
134  'independentVertices': False,
135  })
136 
137 # Select subdetectors to be built
138 geometry.param('Components', ['MagneticField', 'PXD', 'SVD'])
139 
140 # Set the desired SensitiveDetector threshold for the VXD.
141 # -1 eV means no threshold.
142 # set_thr.param('threshold',-1.0)
143 
144 # create processing path
145 main = b2.create_path()
146 main.add_module(eventinfosetter)
147 main.add_module(progress)
148 main.add_module(particlegun)
149 main.add_module(set_thr) # MUST precede gearbox!
150 main.add_module(gearbox)
151 main.add_module(geometry)
152 main.add_module(simulation)
153 main.add_module(pxddigi)
154 main.add_module(pxdclust)
155 main.add_module(svddigi)
156 main.add_module(svdclust)
157 main.add_module(output)
158 
159 # generate events
160 b2.process(main)
161 
162 # show call statistics
163 print(b2.statistics)
old_svd_threshold
The current svd threshold value.
old_pxd_threshold
The current pxd threshold value.