Belle II Software  release-08-01-10
genPixelLikelihoods.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 """
13 genPixelLikelihoods.py
14 
15 A sample script that demonstrates how to collect data using the TOPPixelLikelihood
16 data object.
17 
18 Example usage:
19  basf2 genPixelLikelihoods.py -n100 -- --particle 321 --output kaon.awkd
20 
21 Note: data writing uses AwkwardArrays (tested with v0.12.6).
22 """
23 
24 import basf2 as b2
25 from ROOT import Belle2
26 from tracking import add_tracking_reconstruction
27 from svd import add_svd_reconstruction, add_svd_simulation
28 from pxd import add_pxd_reconstruction, add_pxd_simulation
29 
30 from argparse import ArgumentParser
31 import numpy as np
32 import awkward as awk
33 
34 
35 # command line options
36 ap = ArgumentParser('')
37 ap.add_argument('--output', '-o', default='output.awkd', help='output filename')
38 ap.add_argument('--particle', type=int, default=13, help='pdg code of the particles to generate')
39 ap.add_argument('--momentum_lower', type=float, default=2., help='lower bound for momentum')
40 ap.add_argument('--momentum_upper', type=float, default=2., help='upper bound for momentum')
41 ap.add_argument('--phi_lower', type=float, default=0., help='lower bound for phi')
42 ap.add_argument('--phi_upper', type=float, default=360., help='upper bound for phi')
43 ap.add_argument('--theta_lower', type=float, default=32.6, help='lower bound for theta')
44 ap.add_argument('--theta_upper', type=float, default=122., help='upper bound for theta')
45 ap.add_argument('--xVertex', type=float, default=0., help='x-coordinate of initial position')
46 ap.add_argument('--yVertex', type=float, default=0., help='y-coordinate of initial position')
47 ap.add_argument('--zVertex', type=float, default=0., help='z-coordinate of initial position')
48 
49 opts = ap.parse_args()
50 
51 
52 class WriteData(b2.Module):
53  """
54  Data collector module. Gathers TOP Cherenkov photon data for each event.
55  On terminate(), writes gathered data to given output file.
56 
57  Attributes:
58  data: A list containing AwkwardArrays with each event's data.
59  """
60 
61  def initialize(self):
62  """Inits WriteData object."""
63 
64  self.datadata = []
65 
66  def event(self):
67  """Collects event data about TOP Cherenkov photons."""
68  mcps = Belle2.PyStoreArray("MCParticles")
69  mcp = sorted(mcps, key=lambda x: x.getEnergy(), reverse=True)[0]
70  mcp_pdg = abs(mcp.getPDG())
71  track = mcp.getRelated('Tracks')
72  if not track:
73  print('no related track')
74  return
75  logl = track.getRelated('TOPLikelihoods')
76  if not logl:
77  print('no related loglihoods')
78  return
79  pixlogl = track.getRelated('TOPPixelLikelihoods')
80  if not pixlogl:
81  print('no related pixel loglihoods')
82  return
83  digits = Belle2.PyStoreArray("TOPDigits")
84  if not digits:
85  print('no digits')
86  return
87 
88  moduleID = 0
89  photon_detections = []
90  for d in digits:
91  if moduleID == 0:
92  moduleID = d.getModuleID()
93  simhit = d.getRelated("TOPSimHits")
94  if not simhit:
95  continue
96  photon = simhit.getRelated("TOPSimPhotons")
97  if not photon:
98  continue
99 
100  chanID = d.getPixelID() - 1 # pixelID is 1-based
101  time = photon.getDetectionTime()
102  point = photon.getEmissionPoint()
103  emitTime = photon.getEmissionTime()
104  photon_detections.append([
105  chanID, time,
106  point.X(), point.Y(), point.Z(), emitTime
107  ])
108 
109  self.datadata.append(
110  awk.fromiter([
111  np.array(pixlogl.getPixelLogL_e()),
112  np.array(pixlogl.getPixelLogL_mu()),
113  np.array(pixlogl.getPixelLogL_pi()),
114  np.array(pixlogl.getPixelLogL_K()),
115  np.array(pixlogl.getPixelLogL_p()),
116  np.array(pixlogl.getPixelSigPhot_e()),
117  np.array(pixlogl.getPixelSigPhot_mu()),
118  np.array(pixlogl.getPixelSigPhot_pi()),
119  np.array(pixlogl.getPixelSigPhot_K()),
120  np.array(pixlogl.getPixelSigPhot_p()),
121  logl.getLogL_e(),
122  logl.getLogL_mu(),
123  logl.getLogL_pi(),
124  logl.getLogL_K(),
125  logl.getLogL_p(),
126  photon_detections,
127  mcp_pdg
128  ])
129  )
130 
131  def terminate(self):
132  """Writes data to given output file."""
133  awk.save(opts.output, self.datadata, mode="w")
134 
135 
136 # Suppress messages and warnings during processing:
137 b2.set_log_level(b2.LogLevel.ERROR)
138 
139 # Create path
140 main = b2.create_path()
141 
142 # Set number of events to generate
143 eventinfosetter = b2.register_module('EventInfoSetter')
144 eventinfosetter.param({'evtNumList': [10000]})
145 main.add_module(eventinfosetter)
146 
147 # Gearbox: access to database (xml files)
148 gearbox = b2.register_module('Gearbox')
149 main.add_module(gearbox)
150 
151 # Geometry
152 geometry = b2.register_module('Geometry')
153 main.add_module(geometry)
154 
155 # Particle gun: generate multiple tracks
156 particlegun = b2.register_module('ParticleGun')
157 particlegun.param('pdgCodes', [opts.particle])
158 particlegun.param('nTracks', 1)
159 particlegun.param('varyNTracks', False)
160 particlegun.param('independentVertices', False)
161 particlegun.param('momentumGeneration', 'uniform')
162 particlegun.param('momentumParams', [opts.momentum_lower, opts.momentum_upper])
163 particlegun.param('thetaGeneration', 'uniform')
164 particlegun.param('thetaParams', [opts.theta_lower, opts.theta_upper])
165 particlegun.param('phiGeneration', 'uniform')
166 particlegun.param('phiParams', [opts.phi_lower, opts.phi_upper])
167 particlegun.param('vertexGeneration', 'fixed')
168 particlegun.param('xVertexParams', [opts.xVertex])
169 particlegun.param('yVertexParams', [opts.yVertex])
170 particlegun.param('zVertexParams', [opts.zVertex])
171 main.add_module(particlegun)
172 
173 # Simulation
174 simulation = b2.register_module('FullSim')
175 main.add_module(simulation)
176 
177 add_svd_simulation(main)
178 add_pxd_simulation(main)
179 
180 # PXD digitization & clustering
181 add_pxd_reconstruction(main)
182 
183 # SVD digitization & clustering
184 add_svd_reconstruction(main)
185 
186 # CDC digitization
187 cdcDigitizer = b2.register_module('CDCDigitizer')
188 main.add_module(cdcDigitizer)
189 
190 # TOP digitization
191 topdigi = b2.register_module('TOPDigitizer')
192 main.add_module(topdigi)
193 
194 add_tracking_reconstruction(main)
195 
196 # Track extrapolation
197 ext = b2.register_module('Ext')
198 main.add_module(ext)
199 
200 # TOP reconstruction
201 top_cm = b2.register_module('TOPChannelMasker')
202 main.add_module(top_cm)
203 
204 topreco = b2.register_module('TOPReconstructor')
205 main.add_module(topreco)
206 
207 pdfdebug = b2.register_module("TOPPDFDebugger")
208 pdfdebug.pdfOption = 'fine' # other options: 'optimal', 'rough'
209 main.add_module(pdfdebug)
210 
211 data_getter = WriteData()
212 main.add_module(data_getter)
213 
214 # Show progress of processing
215 progress = b2.register_module('Progress')
216 main.add_module(progress)
217 
218 # Process events
219 b2.process(main)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
data
A list containing arrays with each event's data.