forked from UVaCompPhys/plotting_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_example2.py
More file actions
44 lines (36 loc) · 1.34 KB
/
Copy pathpython_example2.py
File metadata and controls
44 lines (36 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python3
import ROOT as r, sys, math
r.gROOT.SetBatch(True)
r.TH1.AddDirectory(False)
def H(n,t):
h = r.TH2F(n,t,100,50,150,100,50,150)
h.SetDirectory(0)
return h
def python_example2(samples=10000):
rng = r.TRandom3()
h1 = H("h1","2D Gaussian;x;y")
for _ in range(samples):
x = rng.Gaus(100,6); y = rng.Gaus(100,6)
if 50<=x<=150 and 50<=y<=150: h1.Fill(x,y)
h2 = H("h2","2D Gaussian + uniform;x;y"); h2.Add(h1)
for _ in range(samples//3):
h2.Fill(rng.Uniform(50,150), rng.Uniform(50,150))
h3 = H("h3","2D Gaussian + 1/r^2;x;y"); h3.Add(h1)
for _ in range(samples*10):
u = rng.Uniform(0,1-1.0/11.0); rrad = 1.0/(1.0-u); th = rng.Uniform(0,2*math.pi)
x = 100 + 10*rrad*math.cos(th); y = 100 + 10*rrad*math.sin(th)
if 50<=x<=150 and 50<=y<=150: h3.Fill(x,y)
h4 = H("h4","Mixture of 2D Gaussians;x;y"); h4.Add(h1)
for _ in range(samples//2):
x = rng.Gaus(100,20); y = rng.Gaus(100,20)
if 50<=x<=150 and 50<=y<=150: h4.Fill(x,y)
c = r.TCanvas("c2d_py","Canvas2D_py",900,900)
c.Divide(2,2)
c.cd(1); h1.Draw("COLZ")
c.cd(2); h2.Draw("COLZ")
c.cd(3); h3.Draw("COLZ")
c.cd(4); h4.Draw("COLZ")
c.SaveAs("canvas2d_py.png")
if __name__=="__main__":
n = int(sys.argv[1]) if len(sys.argv)>1 else 10000
python_example2(n)