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