Belle II Software  release-08-01-10
BHWidePlots.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import sys
13 import math
14 import basf2
15 
16 # reenable GUI thread for our canvas
17 from ROOT import PyConfig
18 PyConfig.StartGuiThread = True # noqa
19 
20 # Load the required libraries
21 import ROOT
22 from ROOT import Belle2
23 
24 # Set the global log level
25 basf2.logging.log_level = basf2.LogLevel.WARNING
26 
27 # Create some histograms to be filled
28 h_nTracks = ROOT.TH1D('nTracks', 'Number of Tracks per Event;#', 20, 0, 20)
29 h_pdg = ROOT.TH1D('pid', 'Particle code of particles', 100, -50, 50)
30 h_momentum = ROOT.TH1D('momentum', 'Momentum of particles', 200, 0, 8)
31 h_pt = ROOT.TH1D('pt', 'Transverse Momentum of particles', 200, 0, 6)
32 h_phi = ROOT.TH1D('phi', 'Azimuth angle of particles', 200, -180, 180)
33 h_theta = ROOT.TH1D('theta', 'Polar angle of particles', 200, 0, 180)
34 h_costheta = ROOT.TH1D('costheta', 'Cosinus of the polar angle of particles',
35  200, -1, 1)
36 h_vertex = ROOT.TH2D(
37  'xyvertex',
38  'XY Vertex of particles',
39  200,
40  -10,
41  10,
42  200,
43  -10,
44  10,
45 )
46 
47 
48 class ShowMCParticles(basf2.Module):
49 
50  """Simple module to collect some information about MCParticles"""
51 
52  def event(self):
53  """Fill the histograms with the values of the MCParticle collection"""
54 
55  mcParticles = Belle2.PyStoreArray('MCParticles')
56  nTracks = mcParticles.getEntries()
57  h_nTracks.Fill(nTracks)
58  for i in range(nTracks):
59  mc = mcParticles[i]
60  if mc.hasStatus(Belle2.MCParticle.c_PrimaryParticle):
61  p = mc.getMomentum()
62  h_momentum.Fill(p.Mag())
63  h_pt.Fill(p.Perp())
64  h_phi.Fill(p.Phi() / math.pi * 180)
65  h_theta.Fill(p.Theta() / math.pi * 180)
66  h_costheta.Fill(math.cos(p.Theta()))
67  h_pdg.Fill(mc.getPDG())
68  h_vertex.Fill(mc.getProductionVertex().X(),
69  mc.getProductionVertex().Y())
70 
71 
72 # Create the main path and add the modules
73 main = basf2.create_path()
74 
75 # event info setter
76 main.add_module("EventInfoSetter", expList=0, runList=1, evtNumList=100)
77 
78 # Register the BHWideInput module
79 bhwide = basf2.register_module('BHWideInput')
80 
81 # Set the min and max values for the theta scattering angle of the Positrons
82 bhwide.param('ScatteringAngleRangePositron', [5.7, 174.3])
83 
84 # Set the min and max values for the theta scattering angle of the Electrons
85 bhwide.param('ScatteringAngleRangeElectron', [5.7, 174.3])
86 
87 # Set the logging level for the BHWide module to INFO in order to see the total
88 # cross section
89 bhwide.set_log_level(basf2.LogLevel.INFO)
90 
91 # Register the Progress module and the Python histogram module
92 showMCPart = ShowMCParticles()
93 
94 main.add_module("Progress")
95 main.add_module(bhwide)
96 main.add_module(showMCPart)
97 
98 # generate events
99 basf2.process(main)
100 
101 # show call statistics
102 print(basf2.statistics)
103 
104 # Create a Canvas to show histograms
105 c = ROOT.TCanvas('Canvas', 'Canvas', 1536, 768)
106 c.Divide(4, 2, 1e-5, 1e-5)
107 
108 # Draw all histograms
109 histograms = [
110  h_nTracks,
111  h_pdg,
112  h_momentum,
113  h_pt,
114  h_theta,
115  h_costheta,
116  h_phi,
117 ]
118 for (i, h) in enumerate(histograms):
119  c.cd(i + 1)
120  h.SetMinimum(0)
121  h.Draw()
122 c.cd(i + 2)
123 h_vertex.Draw('colz')
124 c.Update()
125 
126 # Wait for enter to be pressed
127 sys.stdin.readline()
A (simplified) python wrapper for StoreArray.
Definition: PyStoreArray.h:72