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