Belle II Software development
validationTestPlots.py
1#!/usr/bin/env python3
2
3
10
11"""
12<header>
13<output>validationTestPlots.root, validationTestPlotsExpertOnly.root</output>
14<contact>Kilian Lieret, Kilian.Lieret@campus.lmu.de</contact>
15
16<description>
17This file will generate various output into a root-file to have
18a fast turn-around during development
19</description>
20</header>
21"""
22
23
24import basf2
25import array
26import numpy as np
27import ROOT
28from datetime import datetime
29from validationtest import add_properties
30
31
32if __name__ == "__main__":
33 # make sure we are able to always create the same plots
34 basf2.set_random_seed(1337)
35
36 tfile = ROOT.TFile("validationTestPlots.root", "RECREATE")
37
38 # NTuples
39 # ======================================
40
41 tntuple = ROOT.TNtuple("ntuple_test", "ntuple test", "x:y:z:k")
42
43 array_of_values = array.array("f", [23.4, 4.4, 5.12, -23.0])
44 tntuple.Fill(array_of_values)
45
46 tntuple.SetAlias(
47 "Description",
48 r"This is a description test. "
49 r"Lorem ipsum sit dolor amet. We also support $\LaTeX$! "
50 "\n <br> \n For example, here is the infamous "
51 "Einstein-Pythagoras-theorem: \n "
52 r"$$a^2 + b^2 = \frac{E}{m}$$ "
53 "\n Of course, you can also do other things, "
54 r"like $\theta = 90^\circ$ or $D^- \rightarrow D^0 \pi^- \pi^+$. "
55 "\n Sometimes it is necessary to escape commands with a double backslash, "
56 r"because e.g. \\theta will be interpreted as [tab]heta.",
57 )
58 tntuple.SetAlias("Check", "This is the check text.")
59 tntuple.SetAlias("Contact", "Name of the contact person.")
60 tntuple.SetAlias("MetaOptions", "shifter, some_meta_options")
61
62 # Overwrite the former TNtuple if one was there
63 tntuple.Write()
64
65 # Gauss Histogram
66 # ======================================
67
68 gaus_h = ROOT.TH1F("gaus_histogram", " Gaus Histogram", 100, -3, 3)
69 gaus_h.FillRandom("gaus", 500)
70
71 add_properties(
72 gaus_h,
73 {
74 "Description": "xlog",
75 "Check": "Gaus Histogram Check",
76 "Contact": "Gaus Histogram Contact",
77 "MetaOptions": "shifter, logx, nostats",
78 },
79 )
80
81 gaus_h.Write()
82
83 # Warnings
84 # ======================================
85
86 warnings_h = ROOT.TH1F(
87 "warnings_histogram",
88 "Histogram with missing check and description",
89 100,
90 -3,
91 3,
92 )
93 warnings_h.FillRandom("gaus", 500)
94
95 add_properties(
96 warnings_h,
97 {
98 "Description": "",
99 "Check": "",
100 "Contact": "Kilian Lieret, Kilian.Lieret@campus.lmu.de",
101 "MetaOptions": "shifter, logx, nostats",
102 },
103 )
104
105 warnings_h.Write()
106
107 # Exp histograms
108 # ======================================
109
110 exp_h = ROOT.TH1F("exp_histogram", " Expert Histogram", 100, 0, 10)
111
112 exp_fn = ROOT.TF1("exp_fn", "exp(-x)", 0, 10)
113 exp_h.FillRandom("exp_fn", 500)
114
115 add_properties(
116 exp_h,
117 {
118 "Description": "Expert Validation Plot",
119 "Check": "Expert Histogram Check. Should only appear if expert button"
120 "was checked.",
121 "Contact": "Expert Histogram Contact",
122 "MetaOptions": "logy, nostats, C",
123 },
124 )
125
126 exp_h.Write()
127
128 # Gaus changing histogram
129 # ======================================
130
131 # set new random seed, to have changed a changed diagram for every
132 # execution
133 basf2.set_random_seed(datetime.now().microsecond)
134
135 gaus_changing = ROOT.TH1F(
136 "gaus_changing_histogram", "Gaus Changing Histogram", 100, -5, 5
137 )
138 mean = ROOT.gRandom.Uniform(-1.0, 1.0)
139
140 for i in range(500):
141 v = ROOT.gRandom.Gaus(mean, 1.0)
142 gaus_changing.Fill(v)
143
144 add_properties(
145 gaus_changing,
146 {
147 "Description": r"xlog ylog with stats. I can haz $\LaTeX$?",
148 "Check": "Gaus Changing Histogram Check",
149 "Contact": "Gaus Changing Histogram Contact",
150 "MetaOptions": "shifter, logx, logy",
151 },
152 )
153
154 gaus_changing.Write()
155
156 # 2D histogram
157 # ======================================
158 hist2d = ROOT.TH2F(
159 "example_2d_histogram", "example_2d_title", 60, -3, 3, 60, -3, 3
160 )
161
162 mean = (0, 0)
163 cov = [[1, 0], [0, 1]]
164 for _ in range(500):
165 x, y = np.random.multivariate_normal(mean, cov)
166 hist2d.Fill(x, y)
167
168 add_properties(
169 hist2d,
170 {
171 "Description": "Some 2D Histogram",
172 "Check": "Check For Something",
173 "Contact": "Contact Someone",
174 "MetaOptions": "shifter, contz",
175 },
176 )
177
178 hist2d.Write()
179
180 # TEfficiency plot
181 # ======================================
182
183 gaus_passed = ROOT.TH1F("gaus_passed", "gaus_passed", 50, -0.5, 49.5)
184 gaus_total = ROOT.TH1F("gaus_total", "gaus_total", 50, -0.5, 49.5)
185
186 for i in range(500):
187 # shifted to more passed at larger positions
188 ratio = float(i) / 500.0
189 passed = ROOT.gRandom.Uniform(ratio * 0.45, 1.0)
190 pos = ROOT.gRandom.Uniform(ratio * 30.0, 49.5)
191
192 gaus_total.Fill(pos)
193 if passed > 0.5:
194 gaus_passed.Fill(pos)
195
196 teff = ROOT.TEfficiency(gaus_passed, gaus_total)
197 teff.SetName("TEfficiency")
198 teff.SetTitle("Tefficiency")
199
200 add_properties(
201 teff,
202 {
203 "Description": "Efficiency plot of something",
204 "Check": "Check For Something",
205 "Contact": "Contact Someone",
206 "MetaOptions": "shifter",
207 },
208 )
209
210 teff.Write()
211
212 # TGraphErrors
213 # ======================================
214
215 graph_err = ROOT.TGraphErrors()
216
217 graph_err.Set(50)
218 for i in range(50):
219 # shifted to more passed at larger positions
220 ratio = float(i) / 50.0
221 passed = ROOT.gRandom.Uniform(ratio * 0.45, 1.0)
222
223 graph_err.SetPoint(i, i + 1.0, passed)
224
225 graph_err.SetName("TGraphErrors")
226 graph_err.SetTitle("TGraphErrors")
227 add_properties(
228 graph_err,
229 {
230 "Description": "TGraphErrors plot of something",
231 "Check": "Check For Something",
232 "Contact": "Contact Someone",
233 "MetaOptions": "shifter",
234 },
235 )
236
237 graph_err.Write()
238
239 # Html content
240 # ======================================
241
242 # generate some user-defined HTML
243 html_content = ROOT.TNamed(
244 "This is a bold HTML tag", "<p><b>THIS IS USER'S HTML</b></p>"
245 )
246
247 html_content.Write()
248
249 tfile.Close()
250
251 # Expert only
252 # ======================================
253
254 tfile = ROOT.TFile("validationTestPlotsExpertOnly.root", "RECREATE")
255 gaus_h = ROOT.TH1F(
256 "gaus_histogram_exprt", " Gaus Histogram Expert", 100, -3, 3
257 )
258 gaus_h.FillRandom("gaus", 500)
259 gaus_h.Write()
260 tfile.Close()