Project

General

Profile

Color test » root_style.py

Rogers, Chris, 05 March 2018 14:41

 
1
import random
2
import numpy
3
import ROOT
4
import os
5

    
6
ROOT_OBJECTS = []
7

    
8
def set_root_verbosity(verbose_level):
9
    ROOT.gErrorIgnoreLevel = 6000
10

    
11
def setup_gstyle():
12
    stops = [0.0000, 0.1250, 0.2500, 0.3750, 0.5000, 0.6250, 0.7500, 0.8750, 1.0000]
13
    red   = [0.2082, 0.0592, 0.0780, 0.0232, 0.1802, 0.5301, 0.8186, 0.9956, 0.9764]
14
    green = [0.1664, 0.3599, 0.5041, 0.6419, 0.7178, 0.7492, 0.7328, 0.7862, 0.9832]
15
    blue  = [0.5293, 0.8684, 0.8385, 0.7914, 0.6425, 0.4662, 0.3499, 0.1968, 0.0539]
16
    s = numpy.array(stops)
17
    r = numpy.array(red)
18
    g = numpy.array(green)
19
    b = numpy.array(blue)
20

    
21
    ncontours = 255
22
    npoints = len(s)
23
    ROOT.TColor.CreateGradientColorTable(npoints, s, r, g, b, ncontours)
24
    ROOT.gStyle.SetNumberContours(ncontours)
25

    
26
    # axes and labels
27
    ROOT.gStyle.SetPadBottomMargin(0.15)
28
    ROOT.gStyle.SetPadLeftMargin(0.15)
29
    ROOT.gStyle.SetPadRightMargin(0.15)
30
    for axis in "X", "Y":
31
        ROOT.gStyle.SetNdivisions(505, axis)
32
        ROOT.gStyle.SetLabelSize(0.05, axis)
33
        ROOT.gStyle.SetTitleSize(0.06, axis)
34
        ROOT.gStyle.SetTitleOffset(1.10, axis)
35

    
36
def get_frame_fill():
37
    level = 0.9
38
    return ROOT.TColor.GetColor(0.2082*level, 0.1664*level, 0.5293*level)
39

    
40
def text_box():
41
    text_box = ROOT.TPaveText(0.6, 0.8, 0.85, 0.899, "NDC")
42
    text_box.SetBorderSize(0)
43
    text_box.SetFillColor(0)
44
    text_box.SetTextSize(0.04)
45
    text_box.SetTextAlign(12)
46
    text_box.AddText("MICE preliminary")
47
    text_box.Draw()
48
    ROOT_OBJECTS.append(text_box)
49
    text_box = ROOT.TPaveText(0.6, 0.7, 0.85, 0.8, "NDC")
50
    text_box.SetBorderSize(0)
51
    text_box.SetFillColor(0)
52
    text_box.SetTextSize(0.03)
53
    text_box.SetTextAlign(12)
54
    text_box.AddText("ISIS Cycle 2017/03")
55
    text_box.AddText("Run 7469 MAUS v1.2.3")
56
    text_box.Draw()
57
    ROOT_OBJECTS.append(text_box)
58

    
59
def test_hist_2d():
60
    canvas = ROOT.TCanvas("test 2d", "test 2d")
61
    hist = ROOT.TH2D("test 2d", ";x axis [units];y axis [units]", 50, -1, 1, 70, -1, 1.8)
62
    hist.SetStats(False)
63
    for i in range(100000):
64
        x, y = random.gauss(0, 0.3), random.gauss(0, 0.3)
65
        hist.Fill(x, y)
66
    canvas.Draw()
67
    # if you want to emphasise the tails, leave the frame white
68
    hist.Draw("COLZ")
69
    text_box()
70
    canvas.Update()
71
    canvas.Print("test_hist_2d_no_fill.eps")
72
    canvas.Print("test_hist_2d_no_fill.png")
73

    
74
    # if you want to de-emphasise the tails, use a frame fill
75
    canvas.SetFrameFillColor(get_frame_fill())
76

    
77
    hist.Draw("COLZ")
78
    text_box()
79
    canvas.Update()
80
    canvas.Print("test_hist_2d_fill.eps")
81
    canvas.Print("test_hist_2d_fill.png")
82
    # these two lines force python to keep the ROOT stuff in memory
83
    ROOT_OBJECTS.append(canvas)
84
    ROOT_OBJECTS.append(hist)
85

    
86
def test_hist_1d():
87
    canvas = ROOT.TCanvas("test 1d", "test 1d")
88
    hist_data = ROOT.TH1D("test 1d data", ";x axis [units];y axis [units]", 100, -1, 1)
89
    hist_mc = ROOT.TH1D("test 1d mc", ";x axis [units];y axis [units]", 100, -1, 1)
90
    hist_data.SetStats(False)
91
    hist_mc.SetStats(False)
92
    for i in range(100000):
93
        x_data = random.gauss(0, 0.3)
94
        hist_data.Fill(x_data)
95
        x_mc = random.gauss(0.01, 0.27)
96
        hist_mc.Fill(x_mc)
97
    canvas.Draw()
98
    hist_mc.SetFillColor(ROOT.kOrange-2)
99
    hist_mc.Draw()
100

    
101
    hist_data.SetMarkerStyle(20)
102
    hist_data.Draw("e1 p same")
103
    text_box()
104
    canvas.Update()
105
    canvas.Print("test_hist_1d.eps")
106
    canvas.Print("test_hist_1d.png")
107
    # these two lines force python to keep the ROOT stuff in memory
108
    ROOT_OBJECTS.append(canvas)
109
    ROOT_OBJECTS.append(hist_data)
110
    ROOT_OBJECTS.append(hist_mc)
111

    
112
    # if you are comparing MC graph with data graph, then following style is recommended:
113
    hist_mc.SetMarkerStyle(26)
114
    hist_mc.SetMarkerColor(ROOT.kRed)
115
    hist_mc.SetLineColor(ROOT.kRed)
116
    hist_mc.Draw("e1 p")
117

    
118
    hist_data.SetMarkerStyle(20)
119
    hist_data.Draw("e1 p same")
120
    text_box()
121
    canvas.Update()
122
    canvas.Print("test_graph_1d.eps")
123
    canvas.Print("test_graph_1d.png")
124

    
125
if __name__ == "__main__":
126
    test_dir = "style_test"
127
    try:
128
        os.mkdir(test_dir)
129
    except OSError:
130
        pass
131
    os.chdir(test_dir)
132
    setup_gstyle()
133
    test_hist_2d()
134
    test_hist_1d()
135
    raw_input("Finished - output in "+test_dir)
136

    
(1-1/7)