PyLamarr
Pythonizations for the ultra-fast simulation option for the LHCb experiment
 
Loading...
Searching...
No Matches
adapters.py
1import PyLamarr
2from particle import Particle
3
4
5@PyLamarr.persistent_table((
6 'event_id', 'eventNumber', 'runNumber', 'timeStamp', 'weight'
7))
8def EventHeader():
9 """
10 Event Header. Additional parameters are assumed to go into the metadata tree.
11 """
12 return "SELECT datasource_id, evt_number, run_number, 0, 0 FROM DataSources;"
13
14
15@PyLamarr.persistent_table((
16 "event_id",
17 "mcparticle_id",
18 "PDG",
19 "generatorStatus",
20 "simulatorStatus",
21 "charge",
22 "time",
23 "mass",
24 "vertex_x",
25 "vertex_y",
26 "vertex_z",
27 "endpoint_x",
28 "endpoint_y",
29 "endpoint_z",
30 "momentum_x",
31 "momentum_y",
32 "momentum_z",
33# "momentumAtEndpoint_x",
34# "momentumAtEndpoint_y",
35# "momentumAtEndpoint_z",
36# "spin_x",
37# "spin_y",
38# "spin_z",
39# "colorFlow_x",
40# "colorFlow_y",
41# "colorFlow_z",
42))
43def MCParticle():
44 """
45 The Monte Carlo particle - based on the lcio::MCParticle.
46 """
47 return """
48 SELECT
49 gev.datasource_id AS event_id,
50 mcp.mcparticle_id AS mcparticle_id,
51 mcp.pid AS pdg,
52 gp.status AS generator_status,
53 mcp.is_signal AS simulator_status,
54 propagation_charge(mcp.pid) AS charge,
55 ov.t AS time,
56 mcp.m AS mass,
57 ov.x AS vertex_x,
58 ov.y AS vertex_y,
59 ov.z AS vertex_z,
60 end.x AS endpoint_x,
61 end.y AS endpoint_y,
62 end.z AS endpoint_z,
63 mcp.px AS momentum_x,
64 mcp.py AS momentum_y,
65 mcp.pz AS momentum_z
66 FROM MCParticles AS mcp
67 LEFT JOIN GenParticles AS gp
68 ON mcp.genparticle_id == gp.genparticle_id
69 LEFT JOIN GenVertices AS ov
70 ON gp.production_vertex == ov.genvertex_id
71 LEFT JOIN GenVertices AS end
72 ON gp.end_vertex == end.genvertex_id
73 LEFT JOIN GenEvents AS gev
74 ON gp.genevent_id == gev.genevent_id
75 """
76
77
78@PyLamarr.persistent_table(('event_id', 'daughter', 'parent'))
79def MCParticle__parents__MCParticle():
80 """
81 Zip table from parent to daughter
82 """
83 return """
84 SELECT
85 gev.datasource_id AS event_id,
86 mcp.mcparticle_id AS daughter,
87 parent.mcparticle_id AS parent
88 FROM MCParticles AS mcp
89 LEFT JOIN GenParticles AS daughter_gp
90 ON mcp.genparticle_id == daughter_gp.genparticle_id
91 INNER JOIN GenParticles AS parent_gp
92 ON parent_gp.end_vertex == daughter_gp.production_vertex
93 LEFT JOIN MCParticles AS parent
94 ON parent.genparticle_id == parent_gp.genparticle_id
95 LEFT JOIN GenParticles AS gp
96 ON mcp.genparticle_id == gp.genparticle_id
97 LEFT JOIN GenEvents AS gev
98 ON gp.genevent_id == gev.genevent_id
99 """
100
101
102@PyLamarr.persistent_table(('parent', 'daughter'))
103def MCParticle__daughters__MCParticle():
104 """
105 Zip table from daughter to parent
106 """
107 return """
108 SELECT
109 mcp.mcparticle_id AS parent,
110 daughter.mcparticle_id AS daughter
111 FROM MCParticles AS mcp
112 LEFT JOIN GenParticles AS parent_gp
113 ON mcp.genparticle_id == parent_gp.genparticle_id
114 INNER JOIN GenParticles AS daughter_gp
115 ON daughter_gp.end_vertex == parent_gp.production_vertex
116 LEFT JOIN MCParticles AS daughter
117 ON daughter.genparticle_id == daughter_gp.genparticle_id
118 """
119
120
121@PyLamarr.persistent_table((
122 'event_id', 'particle_id', 'type', 'PDG', 'algorithmType', 'likelihood'
123 ))
124def ParticleID():
125 """
126 Particle Identification algorithms.
127 - particle_id: index of the Particle table
128 - algorithmType:
129 - 0: boolean (e.g. isMuon)
130 - 1: efficiency (e.g. isMuonEfficiency)
131 - 2: log-likelihood (e.g. RichDLL or Global DLL)
132 - 3: classifier (e.g. ProbNN)
133 - type (unit digit is reserved for future additional versions):
134 - 0: isMuonEff
135 - 10: isMuon
136 - 20: RichDLL
137 - 30: MuonLL
138 - 100: CombinedDLL wrt pion hypothesis
139 - 110: ProbNN
140 """
141 join_clause = " JOIN MCParticle AS mcp ON mcp.mcparticle_id=t.mcparticle_id"
142 return [stmt + join_clause
143 for stmt in [
144 "SELECT mcp.event_id, t.mcparticle_id, 0, 13, 0, ismuoneff FROM tmp_is_muon AS t",
145 "SELECT mcp.event_id, t.mcparticle_id, 10, 13, 1, is_muon FROM tmp_is_muon AS t",
146 "SELECT mcp.event_id, t.mcparticle_id, 20, 11, 2, RichDLLe FROM pid AS t",
147 "SELECT mcp.event_id, t.mcparticle_id, 20, 13, 2, RichDLLmu FROM pid AS t",
148 "SELECT mcp.event_id, t.mcparticle_id, 20, 211, 2, 0 FROM pid AS t",
149 "SELECT mcp.event_id, t.mcparticle_id, 20, 321, 2, RichDLLK FROM pid AS t",
150 "SELECT mcp.event_id, t.mcparticle_id, 20, 2212, 2, RichDLLp FROM pid AS t",
151 "SELECT mcp.event_id, t.mcparticle_id, 30, 13, 2, MuonMuLL FROM pid AS t",
152 "SELECT mcp.event_id, t.mcparticle_id, 30, 2212, 2, MuonBkgLL FROM pid AS t",
153 "SELECT mcp.event_id, t.mcparticle_id, 100, 11, 2, PIDe FROM pid AS t",
154 "SELECT mcp.event_id, t.mcparticle_id, 100, 13, 2, PIDmu FROM pid AS t",
155 "SELECT mcp.event_id, t.mcparticle_id, 100, 211, 2, 0 FROM pid AS t",
156 "SELECT mcp.event_id, t.mcparticle_id, 100, 321, 2, PIDK FROM pid AS t",
157 "SELECT mcp.event_id, t.mcparticle_id, 100, 2212, 2, PIDp FROM pid AS t",
158 "SELECT mcp.event_id, t.mcparticle_id, 110, 11, 2, ProbNNe FROM pid AS t",
159 "SELECT mcp.event_id, t.mcparticle_id, 110, 13, 2, ProbNNmu FROM pid AS t",
160 "SELECT mcp.event_id, t.mcparticle_id, 110, 211, 2, ProbNNpi FROM pid AS t",
161 "SELECT mcp.event_id, t.mcparticle_id, 110, 321, 2, ProbNNk FROM pid AS t",
162 "SELECT mcp.event_id, t.mcparticle_id, 110, 2212, 2, ProbNNp FROM pid AS t",
163 ]]
164
165
166@PyLamarr.persistent_table((
167 'event_id', 'track_id', 'mcparticle_id', 'type', 'chi2', 'ndf', 'ghostProb',
168 # 'dEdx', 'dEdxError', 'radiusOfInnermostHit',
169 ))
170def Track():
171 """
172 Reconstructed track, intended as a collection of track states.
173 """
174 return """
175 SELECT
176 mcp.event_id AS event_id,
177 reco.mcparticle_id AS track_id,
178 reco.mcparticle_id AS mcparticle_id,
179 reco.track_type AS track_type,
180 res.chi2PerDoF * floor(res.nDoF_f), floor(res.nDoF_f), res.ghostProb
181 FROM tmp_particles_recoed_as AS reco
182 LEFT JOIN tmp_resolution_out AS res ON reco.mcparticle_id == res.mcparticle_id
183 LEFT JOIN MCParticle AS mcp ON mcp.mcparticle_id == res.mcparticle_id
184 WHERE reco.track_type != 0
185 """
186
187
188@PyLamarr.persistent_table((
189 'event_id',
190 'track_id',
191 'location',
192 # 'D0', # transverse impact parameter
193 # 'phi', # azimuthal angle
194 # 'omega', # signed curvature of the track
195 # 'Z0', # longitudinal impact parameter
196 # 'tanLambda', #lambda is the dip anglie of the track in r-z
197 # 'time', # time of the track when reaching this location
198 'referencePoint_x',
199 'referencePoint_y',
200 'referencePoint_z',
201 'slope_x',
202 'slope_y',
203 'momentum',
204 'qOverP',
205 'covMatrix_00',
206 'covMatrix_01',
207 'covMatrix_02',
208 'covMatrix_03',
209 'covMatrix_04',
210 'covMatrix_11',
211 'covMatrix_12',
212 'covMatrix_13',
213 'covMatrix_14',
214 'covMatrix_22',
215 'covMatrix_23',
216 'covMatrix_24',
217 'covMatrix_33',
218 'covMatrix_34',
219 'covMatrix_44',
220 ))
221def LHCbTrackState():
222 """
223 TrackState encodes the knowledge of a track in a specific location.
224 Location:
225 - 0: AtOther
226 - 1: AtIP
227 - 2: AtFirstHit
228 - 3: AtLastHist
229 - 4: AtCalorimeter
230 - 5: AtVertex
231 - 5: LastLocation
232
233 For LHCb, AtIP (1) is used to indicate ClosestToBeam.
234
235 Given the different variables used to describe a track state in LHCb
236 and in 4pi experiments, we redefine the TrackState
237 """
238 return """
239 SELECT
240 gev.datasource_id AS event_id,
241 mcp.mcparticle_id,
242 1 AS location,
243 ctb.x + res.dx AS referencePoint_x,
244 ctb.y + res.dy AS referencePoint_y,
245 ctb.z + res.dz AS referencePoint_z,
246 (mcp.px / mcp.pz) + res.dtx AS slope_x,
247 (mcp.py / mcp.pz) + res.dty AS slope_y,
248 norm2(mcp.px, mcp.py, mcp.pz) + res.dp AS momentum,
249 propagation_charge(mcp.pid) / (norm2(mcp.px, mcp.py, mcp.pz) + res.dp) AS qOverP,
250 cov.cov00,
251 cov.cov01,
252 cov.cov02,
253 cov.cov03,
254 cov.cov04,
255 cov.cov11,
256 cov.cov12,
257 cov.cov13,
258 cov.cov14,
259 cov.cov22,
260 cov.cov23,
261 cov.cov24,
262 cov.cov33,
263 cov.cov34,
264 cov.cov44
265 FROM tmp_particles_recoed_as AS reco
266 LEFT JOIN MCParticles AS mcp
267 ON mcp.mcparticle_id == reco.mcparticle_id
268 LEFT JOIN tmp_resolution_out AS res
269 ON res.mcparticle_id == reco.mcparticle_id
270 LEFT JOIN covariance AS cov
271 ON cov.mcparticle_id == reco.mcparticle_id
272 LEFT JOIN tmp_closest_to_beam AS ctb
273 ON ctb.mcparticle_id == reco.mcparticle_id
274 LEFT JOIN GenEvents AS gev
275 ON gev.genevent_id == mcp.genevent_id
276 WHERE reco.track_type != 0
277
278 """
279
280
281@PyLamarr.persistent_table((
282 'event_id',
283 'vertex_id',
284 'is_primary',
285 'time',
286# 'chi2',
287# 'probability',
288 'position_x',
289 'position_y',
290 'position_z',
291 ))
292def MCVertex():
293 return """
294 SELECT
295 gev.datasource_id AS event_id,
296 v.mcvertex_id AS vertex_id,
297 true AS is_primary,
298 v.t AS time,
299 v.x AS position_x,
300 v.y AS position_y,
301 v.z AS position_z
302 FROM MCVertices AS v
303 JOIN GenEvents AS gev
304 ON gev.genevent_id == v.genevent_id
305 """
306
307@PyLamarr.persistent_table((
308 'event_id',
309 'vertex_id',
310 'is_primary',
311 'time',
312# 'chi2',
313# 'probability',
314 'position_x',
315 'position_y',
316 'position_z',
317 'covMatrix_xx',
318 'covMatrix_yx',
319 'covMatrix_zx',
320 'covMatrix_yy',
321 'covMatrix_zy',
322 'covMatrix_zz',
323 ))
324def Vertex():
325 return """
326 SELECT
327 gev.datasource_id AS event_id,
328 v.mcvertex_id AS vertex_id,
329 true AS is_primary,
330 v.t AS time,
331 v.x AS position_x,
332 v.y AS position_y,
333 v.z AS position_z,
334 v.sigma_x*v.sigma_x AS cov_xx,
335 0 AS cov_yx,
336 0 AS cov_zx,
337 v.sigma_y*v.sigma_y AS cov_yy,
338 0 AS cov_zy,
339 v.sigma_z*v.sigma_z AS cov_zz
340 FROM Vertices AS v
341 JOIN GenEvents AS gev
342 ON gev.genevent_id == v.genevent_id
343 """
344
345@PyLamarr.persistent_table((
346 'event_id',
347 'particle_id',
348 'mcparticle_id',
349 'PDG',
350 'type',
351 'energy',
352 'momentum_x',
353 'momentum_y',
354 'momentum_z',
355 'referencePoint_x',
356 'referencePoint_y',
357 'referencePoint_z',
358 'charge',
359 'mass',
360 #'goodnessOfPID',
361 #'covMatrix_pxpx',
362 #'covMatrix_pypx',
363 #'covMatrix_pzpx',
364 #'covMatrix_pepx',
365 #'covMatrix_pypy',
366 #'covMatrix_pzpy',
367 #'covMatrix_pepy',
368 #'covMatrix_pzpz',
369 #'covMatrix_pepz',
370 #'covMatrix_pepe',
371 #'startVertex',
372 ))
373def ReconstructedParticle():
374 return [f"""
375 SELECT
376 gev.datasource_id AS event_id,
377 NULL AS particle_id,
378 mcp.mcparticle_id AS mcparticle_id,
379 {pdg}*propagation_charge(mcp.pid) AS PDG,
380 trk.type AS type,
381 POW(
382 POW({Particle.from_pdgid(pdg).mass},2) +
383 POW(norm2(mcp.px, mcp.py, mcp.pz) + res.dp, 2),
384 0.5) AS energy,
385 slopes_to_cartesian(0,
386 abs(norm2(mcp.px, mcp.py, mcp.pz) + res.dp),
387 mcp.px/mcp.pz + res.dp,
388 mcp.py/mcp.pz + res.dp
389 ) AS momentum_x,
390 slopes_to_cartesian(1,
391 abs(norm2(mcp.px, mcp.py, mcp.pz) + res.dp),
392 mcp.px/mcp.pz + res.dp,
393 mcp.py/mcp.pz + res.dp
394 ) AS momentum_y,
395 slopes_to_cartesian(2,
396 abs(norm2(mcp.px, mcp.py, mcp.pz) + res.dp),
397 mcp.px/mcp.pz + res.dp,
398 mcp.py/mcp.pz + res.dp
399 ) AS momentum_z,
400 ctb.x + res.dx AS referencePoint_x,
401 ctb.y + res.dy AS referencePoint_y,
402 ctb.z + res.dz AS referencePoint_z,
403 propagation_charge(mcp.pid) AS charge,
404 {Particle.from_pdgid(pdg).mass} AS mass
405 FROM Track AS trk
406 LEFT JOIN MCParticles AS mcp
407 ON mcp.mcparticle_id == trk.mcparticle_id
408 LEFT JOIN tmp_closest_to_beam AS ctb
409 ON ctb.mcparticle_id == trk.mcparticle_id
410 LEFT JOIN tmp_resolution_out AS res
411 ON res.mcparticle_id == trk.mcparticle_id
412 LEFT JOIN GenEvents AS gev
413 ON gev.genevent_id == mcp.genevent_id
414
415 """ for pdg in (-11, -13, 211, 321, 2212)]
416
417
418CleanUp = PyLamarr.custom_query([
419 "DROP TABLE IF EXISTS tmp_particles_recoed_as",
420 "DROP TABLE IF EXISTS tmp_is_muon",
421 "DROP TABLE IF EXISTS tmp_closest_to_beam",
422 "DROP TABLE IF EXISTS pid",
423 "DROP TABLE IF EXISTS ismuoneff",
424 "DROP TABLE IF EXISTS covariance",
425 ]);
426