Belle II Software development
examplePython.py
1#!/usr/bin/env python3
2
3
10
11"""
12<header>
13<contact>arul.prakash@physik.uni-muenchen.de</contact>
14<description>
15This file contains example for generating validation plots using python
16</description>
17</header>
18"""
19
20import basf2
21import array
22import numpy as np
23from ROOT import (
24 TEfficiency,
25 TF1,
26 TFile,
27 TGraphErrors,
28 TH1F,
29 TH2F,
30 TNamed,
31 TNtuple,
32 gRandom,
33)
34
35basf2.set_random_seed(1337)
36
37tfile = TFile("examplePython.root", "RECREATE")
38tntuple = TNtuple("ntuple_test", "ntuple test", "x:y:z:k")
39
40prefix = "examplePython"
41
42array_of_values = array.array("f", [23.4, 4.4, 5.12, -23.0])
43tntuple.Fill(array_of_values)
44
45tntuple.SetAlias(
46 "Description",
47 r"This is a description test. "
48 r"Lorem ipsum sit dolor amet. We also support $\LaTeX$! "
49 "\n <br> \n For example, here is the infamous "
50 "Einstein-Pythagoras-theorem: \n "
51 r"$$a^2 + b^2 = \frac{E}{m}$$ "
52 "\n Of course, you can also "
53 r"do other things, like $\theta = 90^\circ$ or $D^- "
54 r"\rightarrow D^0 \pi^- \pi^+$. "
55 "\n Sometimes it is necessary to escape commands "
56 "with a double backslash, "
57 r"because e.g. \\theta will be interpreted as [tab]heta.",
58)
59tntuple.SetAlias("Check", "This is the check text.")
60tntuple.SetAlias("Contact", "Name of the contact person.")
61tntuple.SetAlias("MetaOptions", "some_meta_options")
62
63# Overwrite the former TNtuple if one was there
64tntuple.Write()
65
66# Gauss Histogram
67gausH = TH1F("gaus_histogram", prefix + " Gaus Histogram", 100, -3, 3)
68gausH.FillRandom("gaus", 5000)
69
70gausH.GetListOfFunctions().Add(TNamed("Description", "xlog"))
71gausH.GetListOfFunctions().Add(TNamed("Check", "Gaus Histogram Check"))
72gausH.GetListOfFunctions().Add(TNamed("Contact", "Gaus Histogram Contact"))
73gausH.GetListOfFunctions().Add(TNamed("MetaOptions", "logx, nostats"))
74
75gausH.Write()
76
77gausH = TH1F("exp_histogram", prefix + " Exp Histogram", 100, 0, 10)
78
79exp_fn = TF1("exp_fn", "exp(-x)", 0, 10)
80gausH.FillRandom("exp_fn", 5000)
81
82gausH.GetListOfFunctions().Add(TNamed("Description", "Expert Validation Plot"))
83gausH.GetListOfFunctions().Add(TNamed("Check", "Exp Histogram Check"))
84gausH.GetListOfFunctions().Add(TNamed("Contact", "Exp Histogram Contact"))
85gausH.GetListOfFunctions().Add(TNamed("MetaOptions", "logy, nostats, C"))
86
87gausH.Write()
88
89# Example for a 2D histogram
90hist2d = TH2F("example_2d_histogram", "example_2d_title", 60, -3, 3, 60, -3, 3)
91
92mean = (0, 0)
93cov = [[1, 0], [0, 1]]
94for i in range(10000):
95 x, y = np.random.multivariate_normal(mean, cov)
96 hist2d.Fill(x, y)
97
98hist2d.GetListOfFunctions().Add(TNamed("Description", "Some 2D Histogram"))
99hist2d.GetListOfFunctions().Add(TNamed("Check", "Check For Something"))
100hist2d.GetListOfFunctions().Add(TNamed("Contact", "Contact Someone"))
101hist2d.GetListOfFunctions().Add(TNamed("MetaOptions", "contz"))
102
103hist2d.Write()
104
105# add TEfficiency plot
106gaus_passed = TH1F("gaus_passed", "gaus_passed", 50, -0.5, 49.5)
107gaus_total = TH1F("gaus_total", "gaus_total", 50, -0.5, 49.5)
108
109for i in range(5000):
110 # shifted to more passed at larger positions
111 ratio = float(i) / 5000.0
112 passed = gRandom.Uniform(ratio * 0.45, 1.0)
113 pos = gRandom.Uniform(ratio * 30.0, 49.5)
114 gaus_total.Fill(pos)
115# fixme
116if passed > 0.5:
117 gaus_passed.Fill(pos)
118
119teff = TEfficiency(gaus_passed, gaus_total)
120teff.SetName("TEfficiency")
121teff.GetListOfFunctions().Add(
122 TNamed("Description", "Efficiency plot of something")
123)
124teff.GetListOfFunctions().Add(TNamed("Check", "Check For Something"))
125teff.GetListOfFunctions().Add(TNamed("Contact", "Contact Someone"))
126teff.GetListOfFunctions().Add(TNamed("MetaOptions", ""))
127
128teff.Write()
129
130# add TGraphErrors plot
131graph_err = TGraphErrors()
132
133graph_err.Set(50)
134for i in range(50):
135 # shifted to more passed at larger positions
136 ratio = float(i) / 50.0
137 passed = gRandom.Uniform(ratio * 0.45, 1.0)
138
139# fixme
140graph_err.SetPoint(i, i + 1.0, passed)
141
142graph_err.SetName("TGraphErrors")
143graph_err.GetListOfFunctions().Add(
144 TNamed("Description", "TGraphErrors plot of something")
145)
146graph_err.GetListOfFunctions().Add(TNamed("Check", "Check For Something"))
147graph_err.GetListOfFunctions().Add(TNamed("Contact", "Contact Someone"))
148graph_err.GetListOfFunctions().Add(TNamed("MetaOptions", ""))
149
150graph_err.Write()
151
152tfile.Close()