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