Public Member Functions | Public Attributes | List of all members
Canvas.Canvas Class Reference

Public Member Functions

def __init__ (self, verbose=False)
 
def set_defaults (self)
 
def draw (self, input_histograms, labels, axis_labels, save_as='plot.eps', draw_option="E1X0", draw_options=False, log_x=False, log_y=False, y_range=[], z_range=[], show_grid=True, show_legend=True, legend_coordinates=None, auto_legend_height=True, auto_legend_width=True, auto_legend_position=True, colours=[i for i in range(20)], markers=[i for i in range(20)], lines=[i for i in range(20)], nova_validation=True, latex_label=False, l_x=0.25, l_y=0.94, suffixes=[".png"])
 
def set_text_style (self, histogram)
 
def set_style (self, histogram, iter, colours=[], lines=[], markers=[])
 
def label_axes (self, histogram, axis_labels)
 

Public Attributes

 canvas
 
 verbose
 
 TEXT_SIZE
 
 TEXT_OFFSET
 
 LEGEND_TEXT_SIZE
 
 LOG_SPREAD_FRACTION
 
 MARGIN_LEFT
 
 MARGIN_RIGHT
 
 MARGIN_TOP
 
 MARGIN_BOTTOM
 
 LATEX_FONT_SIZE
 
 FONT
 
 line_options
 
 fill_options
 
 point_options
 

Detailed Description

Definition at line 5 of file Canvas.py.

Constructor & Destructor Documentation

def Canvas.Canvas.__init__ (   self,
  verbose = False 
)

Definition at line 7 of file Canvas.py.

7  def __init__(self, verbose=False):
8  ROOT.gROOT.SetBatch(1)
9  ROOT.gErrorIgnoreLevel = ROOT.kError
10  ROOT.gROOT.SetStyle("novaStyle")
11  ROOT.gROOT.ForceStyle()
12  ROOT.gStyle.SetOptTitle(0)
13  ROOT.gErrorIgnoreLevel = 1001
14  ROOT.gStyle.SetEndErrorSize(5)
15 
16  self.canvas = ROOT.TCanvas("canvas","variable", 800, 600)
17  self.canvas.Clear()
18 
19  ROOT.gPad.SetTicks(1,1)
20 
21  self.verbose = verbose
22  self.set_defaults()
23 
def __init__(self, verbose=False)
Definition: Canvas.py:7
def set_defaults(self)
Definition: Canvas.py:24

Member Function Documentation

def Canvas.Canvas.draw (   self,
  input_histograms,
  labels,
  axis_labels,
  save_as = 'plot.eps',
  draw_option = "E1X0",
  draw_options = False,
  log_x = False,
  log_y = False,
  y_range = [],
  z_range = [],
  show_grid = True,
  show_legend = True,
  legend_coordinates = None,
  auto_legend_height = True,
  auto_legend_width = True,
  auto_legend_position = True,
  colours = [i for i in range(20)],
  markers = [i for i in range(20)],
  lines = [i for i in range(20)],
  nova_validation = True,
  latex_label = False,
  l_x = 0.25,
  l_y = 0.94,
  suffixes = [".png"] 
)

Definition at line 63 of file Canvas.py.

References Draw(), Canvas.Canvas.fill_options, Canvas.Canvas.label_axes(), Canvas.Canvas.LATEX_FONT_SIZE, Canvas.Canvas.LEGEND_TEXT_SIZE, Canvas.Canvas.line_options, cet::sqlite.max(), min(), Canvas.Canvas.point_options, Canvas.Canvas.set_defaults(), Canvas.Canvas.set_style(), Canvas.Canvas.set_text_style(), Controller.Controller.verbose, Component.Component.verbose, Canvas.Canvas.verbose, samweb_client.http_client_requests.RequestsHTTPClient.verbose, samweb_client.http_client.SAMWebHTTPClient.verbose, evg::MCTruthToDk2NuHackItr.verbose, and samweb_client.client.SAMWebClient.verbose.

63  suffixes = [".png"]):
64 
65  if legend_coordinates == None:
66  legend_coordinates = [0.6, 0.78, 0.85, 0.93]
67 
68  if self.verbose:
69  print "canvs: -- Canvas draw"
70  print "canvs: saving as: ",save_as
71 
72  self.canvas.Clear()
73  self.canvas.Update()
74  self.canvas.Flush()
75 
76  # checks
77  if colours: assert len(colours) >= len(input_histograms)
78  if markers: assert len(markers) >= len(input_histograms)
79  if lines: assert len(lines) >= len(input_histograms)
80  assert len(labels) == len(input_histograms)
81 
82  if log_x: self.canvas.SetLogx(1)
83  else: self.canvas.SetLogx(0)
84 
85  if log_y: self.canvas.SetLogy(1)
86  else: self.canvas.SetLogy(0)
87 
88  if show_grid: self.canvas.SetGrid(1)
89  else: self.canvas.SetGrid(0,0)
90 
91  if auto_legend_height:
92  # adjust y1 based on height
93  legend_coordinates[1] = (legend_coordinates[3] - (len(input_histograms)*self.LEGEND_TEXT_SIZE) - ((len(input_histograms)-1)*0.01))
94  legend_flip = False
95  if auto_legend_position and type(input_histograms[0]) in [ROOT.TH1D, ROOT.TH1F]:
96  if tools.findMaxAsymmetry(input_histograms) > 0:
97  legend_coordinates[0] -= 0.4
98  legend_coordinates[2] -= 0.4
99  legend_flip = True
100  if auto_legend_width:
101  # adjust x1(2) based on length
102  width = max([len(label) for label in labels]) * 0.02
103  if legend_flip:
104  legend_coordinates[2] = legend_coordinates[0] + width
105  else:
106  legend_coordinates[0] = legend_coordinates[2] - width
107 
108  Legend = ROOT.TLegend(legend_coordinates[0],legend_coordinates[1],legend_coordinates[2],legend_coordinates[3])
109  Legend.SetBorderSize(0)
110  Legend.SetTextFont(42)
111  Legend.SetFillStyle(0)
112 
113  histograms = []
114  for histogram in input_histograms:
115  histograms.append(histogram.Clone(histogram.GetName()+"_to_draw"))
116 
117  if len(y_range) == 0:
118  if type(histograms[0]) in [ROOT.TH1D, ROOT.TH1F]:
119  y_min, y_max = tools.findMinMax(histograms)
120  if log_y:
121  y_min = max(y_min,0.01)
122  y_max = y_max*5.
123  else:
124  y_min = min(y_min,0)
125  y_max = y_max*1.2
126  else:
127  y_min, y_max = False, False
128  y_range = [y_min, y_max]
129 
130  for i_hist, histogram in enumerate(histograms):
131  if self.verbose:
132  print "canvs: Histogram[%i]: %s"%(i_hist,histogram.GetName()),histogram
133  print "canvs: - label: %s" %(labels[i_hist])
134  if type(histogram) in [ROOT.TH1D, ROOT.TH1F]:
135  print "canvs: - Entries: %i" %histogram.GetEntries()
136  print "canvs: - Sum of weights: %.2f"%histogram.GetSumOfWeights()
137 
138  if draw_options: this_draw_option = draw_options[i_hist]
139  else: this_draw_option = draw_option
140 
141  self.set_text_style(histogram)
142  self.set_style(histogram, i_hist, colours=colours, markers=markers, lines=lines)
143  self.label_axes(histogram, axis_labels)
144 
145  mode = False
146  if this_draw_option in self.line_options: mode = "L"
147  elif this_draw_option in self.fill_options: mode = "F"
148  elif this_draw_option in self.point_options: mode = "P"
149  else: mode = "PL"
150  Legend.AddEntry(histogram, labels[i_hist], mode)
151 
152  if (i_hist == 0):
153  if (y_range[1] != False):
154  histogram.GetYaxis().SetRangeUser(y_range[0], y_range[1])
155  if (z_range != []):
156  histogram.GetZaxis().SetRangeUser(z_range[0], z_range[1])
157  if type(histogram) in [ROOT.TGraph]: this_draw_option = "A" + this_draw_option
158  histogram.Draw(this_draw_option)
159  else:
160  this_draw_option += " same"
161 
162  if self.verbose:
163  print "canvs: - draw option: %s"%this_draw_option
164 
165  histogram.Draw(this_draw_option)
166 
167  if show_legend: Legend.Draw("same")
168  if type(histograms[0]) in [ROOT.TH1D, ROOT.TH1F]: histograms[0].Draw("axis same")
169 
170  if nova_validation:
171  if self.verbose: print "canvs: Adding NOvA validation label"
172  validation = ROOT.TLatex(.9, 0.95, "NO#nuA Validation")
173  validation.SetNDC()
174  validation.SetTextFont(42)
175  validation.SetTextSize(self.LATEX_FONT_SIZE)
176  validation.SetTextColor(ROOT.kGray+2)
177  validation.SetTextAlign(32)
178  validation.Draw()
179 
180  if latex_label:
181  latex_labels = latex_label.split(";")
182  if self.verbose: print "canvs: Adding %i latex label(s) at %.2f %.2f:"%(len(latex_labels),l_x,l_y),latex_labels
183  latex = ROOT.TLatex()
184  latex.SetNDC()
185  latex.SetTextFont(42)
186  latex.SetTextSize(self.LATEX_FONT_SIZE)
187  offset = 0
188  for latex_label in latex_labels:
189  latex.DrawLatex(l_x,l_y-offset, latex_label)
190  offset+=0.06
191 
192  if self.verbose:
193  print "canvs: Done, saving"
194  print "canvs: Saving as: %s"%save_as.split("/")[-1]
195 
196  self.canvas.Update()
197  for suffix in suffixes:
198  self.canvas.SaveAs(save_as+suffix)
199  self.canvas.Clear()
200 
201  # restore default settings
202  self.set_defaults()
203 
tree Draw("slc.nhit")
def label_axes(self, histogram, axis_labels)
Definition: Canvas.py:242
def set_style(self, histogram, iter, colours=[], lines=[], markers=[])
Definition: Canvas.py:233
LEGEND_TEXT_SIZE
Definition: Canvas.py:27
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
def set_defaults(self)
Definition: Canvas.py:24
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
def set_text_style(self, histogram)
Definition: Canvas.py:204
def Canvas.Canvas.label_axes (   self,
  histogram,
  axis_labels 
)

Definition at line 242 of file Canvas.py.

References SetTitle().

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

242  def label_axes(self, histogram, axis_labels):
243  labels = axis_labels.split(";")
244  histogram.GetXaxis().SetTitle(labels[0])
245  histogram.GetYaxis().SetTitle(labels[1])
246 
247 
248 
def label_axes(self, histogram, axis_labels)
Definition: Canvas.py:242
gargamelle SetTitle("Gargamelle #nu_{e} CC data")
def Canvas.Canvas.set_defaults (   self)

Definition at line 24 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

24  def set_defaults(self):
25  self.TEXT_SIZE = 0.05 # default 0.07, for large plots 0.06
26  self.TEXT_OFFSET = 1.7
27  self.LEGEND_TEXT_SIZE = 0.08
28  self.LOG_SPREAD_FRACTION = 0.3
29  self.MARGIN_LEFT = 0.16
30  self.MARGIN_RIGHT = 0.1
31  self.MARGIN_TOP = 0.07
32  self.MARGIN_BOTTOM = 0.16
33  self.LATEX_FONT_SIZE = 0.04
34  self.FONT = 42
35 
36  self.canvas.SetLeftMargin( self.MARGIN_LEFT)
37  self.canvas.SetRightMargin( self.MARGIN_RIGHT)
38  self.canvas.SetTopMargin( self.MARGIN_TOP)
39  self.canvas.SetBottomMargin(self.MARGIN_BOTTOM)
40 
41  self.line_options = ["H9","C9","hist H9","hist C9","L9","hist L9","hist9","hist][9","LX","hist ][","hist ][9","histL9","histL"]
42  self.fill_options = ["E2","2", "hist F", "hist F9"]
43  self.point_options = ["P","P9"]
44 
45 
LOG_SPREAD_FRACTION
Definition: Canvas.py:28
LEGEND_TEXT_SIZE
Definition: Canvas.py:27
def set_defaults(self)
Definition: Canvas.py:24
def Canvas.Canvas.set_style (   self,
  histogram,
  iter,
  colours = [],
  lines = [],
  markers = [] 
)

Definition at line 233 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

233  def set_style(self, histogram, iter, colours=[], lines=[], markers=[]):
234  histogram.SetLineWidth(3)
235  histogram.SetLineStyle(lines[iter])
236  histogram.SetLineColor(colours[iter])
237 
238  histogram.SetMarkerStyle(markers[iter])
239  histogram.SetMarkerSize(1.5)
240  histogram.SetMarkerColor(colours[iter])
241 
def set_style(self, histogram, iter, colours=[], lines=[], markers=[])
Definition: Canvas.py:233
def Canvas.Canvas.set_text_style (   self,
  histogram 
)

Definition at line 204 of file Canvas.py.

References Canvas.Canvas.FONT, Canvas.Canvas.TEXT_OFFSET, and Canvas.Canvas.TEXT_SIZE.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

204  def set_text_style(self, histogram):
205  histogram.GetXaxis().SetTitleSize(self.TEXT_SIZE)
206  histogram.GetXaxis().SetTitleOffset(self.TEXT_OFFSET)
207  histogram.GetXaxis().SetTitleFont(self.FONT)
208  histogram.GetYaxis().SetTitleSize(self.TEXT_SIZE)
209  histogram.GetYaxis().SetTitleOffset(self.TEXT_OFFSET)
210  histogram.GetYaxis().SetTitleFont(self.FONT)
211 
212  try:
213  histogram.GetZaxis().SetTitleSize(self.TEXT_SIZE)
214  histogram.GetZaxis().SetTitleFont(self.FONT)
215  except:
216  pass
217 
218  histogram.GetXaxis().SetLabelSize(self.TEXT_SIZE)
219  histogram.GetXaxis().SetLabelFont(self.FONT)
220  histogram.GetYaxis().SetLabelSize(self.TEXT_SIZE)
221  histogram.GetYaxis().SetLabelFont(self.FONT)
222  try:
223  histogram.GetZaxis().SetLabelSize(self.TEXT_SIZE)
224  histogram.GetZaxis().SetLabelFont(self.FONT)
225  histogram.GetZaxis().CenterTitle()
226  except:
227  pass
228 
229  # centre titles
230  histogram.GetXaxis().CenterTitle()
231  histogram.GetYaxis().CenterTitle()
232 
def set_text_style(self, histogram)
Definition: Canvas.py:204

Member Data Documentation

Canvas.Canvas.canvas

Definition at line 16 of file Canvas.py.

Referenced by generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.fill_options

Definition at line 42 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.FONT
Canvas.Canvas.LATEX_FONT_SIZE

Definition at line 33 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.LEGEND_TEXT_SIZE

Definition at line 27 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.line_options

Definition at line 41 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.LOG_SPREAD_FRACTION

Definition at line 28 of file Canvas.py.

Canvas.Canvas.MARGIN_BOTTOM

Definition at line 32 of file Canvas.py.

Referenced by generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.MARGIN_LEFT

Definition at line 29 of file Canvas.py.

Referenced by generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.MARGIN_RIGHT

Definition at line 30 of file Canvas.py.

Referenced by generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.MARGIN_TOP

Definition at line 31 of file Canvas.py.

Referenced by generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.point_options

Definition at line 43 of file Canvas.py.

Referenced by Canvas.Canvas.draw(), and generic.tools.ImageMaker.ImageMaker.draw().

Canvas.Canvas.TEXT_OFFSET
Canvas.Canvas.TEXT_SIZE

The documentation for this class was generated from the following file: