PyROOT Helper

Convenient wrapper for plotting Python things in ROOT. Mostly uses numpy data and aims to have a Pyton like feeling. No external dependencies except numpy and PyROOT

sls_detector_tools.root_helper.getHist(h, edge='center')[source]

Returns the bin edges and values for a ROOT histogram.

Parameters:
  • h (TH1) – Histogram that we need the values from
  • edge (str, optional) – Which edge that should be used. Defaults to center. Possible values low, center high and both
Returns:

  • x (np.array) – Bin edges as spefied by edge
  • y (np.array) – Values of the bins

Raises:

ValueError – If edge is something different from low, center, high or both

sls_detector_tools.root_helper.hist(data=None, xmin=None, xmax=None, bins=100, title='Histogram', add=False, c=None, h0=None, color=None, line_color=None, fill_style=1001)[source]

Plot a histogram using TH1D also can add a new histogram as an overlay on the same canvas. In that case the canvas and one histogram on it needs to be specified

Parameters:
  • data (array_like) – An array or list with the hits that should be added to the histogram
  • xmin (double, optional) – Lower edge of the lowest bin
  • xmax (double, optional) – High edge of the highest bin
  • bins (int, optional) – Defaults to 100
  • title (std, optional) – Title of the histogram
  • add (bool, optional) – If True add the new histogram to an old canvas
  • c (TCanvas, optional) – Canvas to add the histogram to
  • h0 (TH1D, optional) – Base histogram plotted on the old canvas
  • color (int, optional) – ROOT color for the histogram, if not specified the value is taken from style.hist_color
  • line_color (int optional) – ROOT color for the line of the histogram is not specified taken from style.hist_line_color
  • fill_style (int optional) – Style to fill the histogram, default 1001
Returns:

  • c (TCanvas) – Canvas on which the Histogram is drawn c=None
  • h (TH1D) – The histogram that was just drawn.

sls_detector_tools.root_helper.plot(x, y, options='ALP', title='A TGraph', wx=900, wy=600, draw=True, error_graph=False, ex=None, ey=None)[source]

Plot a TGraph

Parameters:
  • x (array_like) – Values for the x axis
  • y (array_like) – Values for the y axis
  • options (str, optional) – Draw options for the TGraph default is ALP -> (Axis, Line, Points)
  • title (str, optional) – Title of the TGraph, defaults to A TGraph
  • wx (int, optional) – Width of the plot in pixels
  • wy (int, optional) – Height of the plot in pixels
  • draw (bool, optional) – Defaults to True, draws the plot
  • error_graph (bool, optional) – Use a TGraphErrors instead of a normal TGraph
  • ex (array_like, optional) – Errors in x values
  • ey (array_like, optional) – Errors in y values, if not spefified it is assumed to be np.sqrt(y)
Returns:

  • c (TCanvas) – Canvas on which the TGraph is drawn if draw is set to false c=None
  • g (TGraph) – The graph.

Raises:

ValueError – If the size of x and y is not the same

class sls_detector_tools.root_helper.style[source]

Bases: object

Class used to configure the apparence of PyROOT draw objects

hist_color = 864
hist_line_color = 1
hist_line_width = 1
line_color = 862
line_width = 2
marker_color = 862
marker_style = 21
sls_detector_tools.root_helper.th2(x, y, xmin=0, xmax=0, ymin=0, ymax=0, bins=False)[source]

Plot a TH2

Parameters:
  • x (array_like) – input
  • y (array_like) – input
Returns:

  • c (TCanvas) – Canvas on which the Histogram is drawn
  • h (TH2D) – The histogram that was just drawn.