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