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