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