Belle II Software  release-05-01-25
ParticleGunPlots.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import sys
5 import math
6 from basf2 import create_path, register_module, process, logging, \
7  LogLevel, Module, statistics
8 logging.log_level = LogLevel.WARNING
9 
10 # Load the required libraries
11 from ROOT import TH1D, TH2D, TCanvas, Belle2, PyConfig
12 
13 # reenable GUI thread for our canvas
14 PyConfig.StartGuiThread = True
15 
16 # Create some histograms to be filled
17 h_nTracks = TH1D('nTracks', 'Number of Tracks per Event', 100, 0, 50)
18 h_pdg = TH1D('pid', 'PDG codes', 500, -250, 250)
19 h_momentum = TH1D('momentum', 'Momentum p; p/GeV', 200, 0, 10)
20 h_pt = TH1D('pt', 'Transverse Momentum p_{t};p_{t}/GeV', 200, 0, 10)
21 h_phi = TH1D('phi', 'azimuthal angle #phi; #phi in degree', 200, -180, 180)
22 h_theta = TH1D('theta', 'polar angle #theta; #theta in degree', 200, 0, 180)
23 h_costheta = TH1D('costheta', 'cos(#theta); cos(#theta)', 200, -1, 1)
24 h_xyvertex = TH2D('xyvertex', 'vertex in xy;x/#mum;y/#mum',
25  500, -500, 500, 500, -500, 500)
26 h_zxvertex = TH2D('zxvertex', 'vertex in zx;z/#mum;x/#mum',
27  500, -500, 500, 500, -500, 500)
28 h_zyvertex = TH2D('zyvertex', 'vertex in zy;z/#mum;y/#mum',
29  500, -500, 500, 500, -500, 500)
30 
31 
32 class ShowMCParticles(Module):
33  """Simple module to collect some information about MCParticles"""
34 
35  def event(self):
36  """Fill the histograms with the values of the MCParticle collection"""
37  mcParticles = Belle2.PyStoreArray('MCParticles')
38  h_nTracks.Fill(mcParticles.getEntries())
39  for mc in mcParticles:
40  p = mc.getMomentum()
41  h_momentum.Fill(p.Mag())
42  h_pt.Fill(p.Perp())
43  h_phi.Fill(p.Phi() / math.pi * 180)
44  h_theta.Fill(p.Theta() / math.pi * 180)
45  h_costheta.Fill(math.cos(p.Theta()))
46  h_pdg.Fill(mc.getPDG())
47  h_xyvertex.Fill(mc.getProductionVertex().X() * 1e4,
48  mc.getProductionVertex().Y() * 1e4)
49  h_zxvertex.Fill(mc.getProductionVertex().Z() * 1e4,
50  mc.getProductionVertex().X() * 1e4)
51  h_zyvertex.Fill(mc.getProductionVertex().Z() * 1e4,
52  mc.getProductionVertex().Y() * 1e4)
53 
54 # Create Modules
55 eventinfosetter = register_module('EventInfoSetter')
56 progress = register_module('Progress')
57 particlegun = register_module('ParticleGun')
58 vertexsmear = register_module('SmearPrimaryVertex')
59 showMCPart = ShowMCParticles()
60 
61 # Specify number of events to generate
62 eventinfosetter.param({'evtNumList': [10000], 'runList': [1]})
63 
64 # Set parameters for particlegun
65 particlegun.param({
66  # Generate 5 tracks
67  'nTracks': 5,
68  # But vary the number of tracks according to Poisson distribution
69  'varyNTracks': True,
70  # Generate pi+, pi-, e+ and e-
71  'pdgCodes': [211, -211, 11, -11],
72  # having a normal distributed transversal momentum
73  'momentumGeneration': 'normalPt',
74  # with a mean of 5 GeV and a width of 1 GeV
75  'momentumParams': [5.0, 1.0],
76  # use normal distribution for phi
77  'phiGeneration': 'normal',
78  # with a mean of 90 degree and w width of 30 degree
79  'phiParams': [90, 30],
80  # and generate theta uniform in cos(theta)
81  'thetaGeneration': 'uniformCos',
82  # between 17 and 150 degree
83  'thetaParams': [17, 150],
84  # finally, vertex generation, fixed, we use smearing module
85  'vertexGeneration': 'fixed',
86  'xVertexParams': [0],
87  'yVertexParams': [0],
88  'zVertexParams': [0],
89  # all tracks should originate from the same vertex
90  'independentVertices': False,
91 })
92 
93 
94 # The beamspot size is determined by using the beamsize for LER and HER from
95 # the TDR, multiply both zx-distributions including their crossing angle and
96 # fit a rotated 2D gaussian to the resulting shape.
97 # Parameters used (angle is the angle between beam and z axis):
98 # HER: sigma_x = 10.2µm, sigma_z = 6mm, angle= 41.5mrad
99 # LER: sigma_x = 7.76µm, sigma_z = 5mm, angle=-41.5mrad
100 vertexsmear.param({
101  # Beamspot size in x
102  'sigma_pvx': 6.18e-4, # µm
103  # Beamspot size in y
104  'sigma_pvy': 59e-7, # nm
105  # Beamspot size in z
106  'sigma_pvz': 154e-4, # µm
107  # Angle between beamspot and z axis, rotation around y
108  'angle_pv_zx': -1.11e-2,
109 })
110 
111 # create processing path
112 main = create_path()
113 main.add_module(eventinfosetter)
114 main.add_module(progress)
115 main.add_module(particlegun)
116 main.add_module(vertexsmear)
117 main.add_module(showMCPart)
118 
119 # generate events
120 process(main)
121 
122 # show call statistics
123 print(statistics)
124 
125 # Create a Canvas to show histograms
126 c = TCanvas('Canvas', 'Canvas', 1920, 768)
127 c.Divide(5, 2, 1e-5, 1e-5)
128 
129 # Draw all histograms
130 histograms = [h_nTracks, h_pdg, h_momentum, h_pt, h_theta, h_costheta, h_phi]
131 vertexhists = [h_xyvertex, h_zxvertex, h_zyvertex]
132 for (i, h) in enumerate(histograms):
133  c.cd(i + 1)
134  h.SetMinimum(0)
135  h.Draw()
136 for (j, h) in enumerate(vertexhists, i + 2):
137  c.cd(j)
138  h.Draw('colz')
139 c.Update()
140 
141 # Wait for enter to be pressed
142 print("Press Enter to exit ...")
143 sys.stdin.readline()
ParticleGunPlots.ShowMCParticles
Definition: ParticleGunPlots.py:32
ParticleGunPlots.ShowMCParticles.event
def event(self)
Definition: ParticleGunPlots.py:35
Belle2::PyStoreArray
a (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:58