Belle II Software development
SavedMCParticles.py
1#!/usr/bin/env python3
2
3
10
11import basf2 as b2
12from ROOT import Belle2
13
14b2.logging.log_level = b2.LogLevel.WARNING
15
16
17class CheckMCParticles(b2.Module):
18
19 """
20 Counts MCParticles that generate TrueHits.
21 """
22
23 def __init__(self):
24 """Initialize the module"""
25
26 super().__init__()
27
29
31
32 self.nMCParticles = 0
33
34 self.nSecondaries = 0
35
36 self.processesPXD = []
37
38 self.processesSVD = []
39
40 def initialize(self):
41 """ Does nothing """
42
43 def beginRun(self):
44 """ Does nothing """
45
46 def event(self):
47 """
48 Count the number of MCParticles related to a VXD TrueHit
49 """
50
51 mc_particles = Belle2.PyStoreArray('MCParticles')
52 self.nMCParticles += mc_particles.getEntries()
53 for particle in mc_particles:
54 if not particle.hasStatus(Belle2.MCParticle.c_PrimaryParticle):
55 self.nSecondaries += 1
56 if (len(particle.getRelationsTo('PXDTrueHits')) > 0):
57 self.nSecondariesPXD += 1
58 self.processesPXD\
59 .append(particle.getSecondaryPhysicsProcess())
60 if (len(particle.getRelationsTo('SVDTrueHits')) > 0):
61 self.nSecondariesSVD += 1
62 self.processesSVD\
63 .append(particle.getSecondaryPhysicsProcess())
64
65 def terminate(self):
66 """ Write results """
67 b2.B2INFO(f'Found {self.nSecondaries} secondary MC Particles out of total {self.nMCParticles}.')
68 b2.B2INFO(f'Of these, found {self.nSecondariesPXD} secondaries in PXD and {self.nSecondariesSVD} in SVD.')
69 b2.B2INFO(f'Secondary processes for PXD: {str(self.processesPXD)}; for SVD: {str(self.processesSVD)}')
70
71
72# Particle gun module
73particlegun = b2.register_module('ParticleGun')
74# Create Event information
75eventinfosetter = b2.register_module('EventInfoSetter')
76# Show progress of processing
77progress = b2.register_module('Progress')
78# Load parameters
79gearbox = b2.register_module('Gearbox')
80# Create geometry
81geometry = b2.register_module('Geometry')
82# Run simulation
83simulation = b2.register_module('FullSim')
84# simulation.param('StoreAllSecondaries', True)
85# PXD digitization module
86printParticles = CheckMCParticles()
87printParticles.set_log_level(b2.LogLevel.INFO)
88
89# Specify number of events to generate
90eventinfosetter.param({'evtNumList': [100], 'runList': [1]})
91
92# Set parameters for particlegun
93particlegun.param({
94 'nTracks': 1,
95 'varyNTracks': True,
96 'pdgCodes': [211, -211, 11, -11],
97 'momentumGeneration': 'normalPt',
98 'momentumParams': [2, 1],
99 'phiGeneration': 'normal',
100 'phiParams': [0, 360],
101 'thetaGeneration': 'uniformCos',
102 'thetaParams': [17, 150],
103 'vertexGeneration': 'normal',
104 'xVertexParams': [0, 1],
105 'yVertexParams': [0, 1],
106 'zVertexParams': [0, 1],
107 'independentVertices': False,
108})
109
110# Select subdetectors to be built
111geometry.param('components', ['MagneticField', 'PXD', 'SVD'])
112
113# create processing path
114main = b2.create_path()
115main.add_module(eventinfosetter)
116main.add_module(progress)
117main.add_module(particlegun)
118main.add_module(gearbox)
119main.add_module(geometry)
120main.add_module(simulation)
121main.add_module(printParticles)
122
123# generate events
124b2.process(main)
125
126# show call statistics
127print(b2.statistics)
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72
nSecondariesSVD
Number of secondaries that generated an SVD TrueHit.
processesPXD
List of processes that generated secondaries in PXD.
nSecondariesPXD
Number of secondaries that generated a PXDTrueHit.
processesSVD
List of processes that generated secondaries in SVD.
nMCParticles
Total number of MCParticles.
nSecondaries
Total number of secondary MCParticles.