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