Using TH1F, TH2F and TLine

A class dedicated to plotting histograms using the mplhep package as a backend to format figures. This class adds some additional features to simplify writing simple plotting code, such as bin centering.

class AnalysisG.Plotting.BasePlotting
__precompile__() None

A function which can be overridden and is used to perform preliminary data manipulation or histogram modifications.

__compile__() None
__postcompile__() None
SaveFigure(dirc) None

Whether to compile the given histogram object.

Params str dirc:

The output directory of the plotting.

Variables:
  • OutputDirectory (str) – The directory in which to save the figure. If the directory tree is non-existent, it will automatically be created.

  • Filename (str) – The name given to the output .png file.

  • DPI (int) – The resolution of the figure to save.

  • FontSize (int) – The front size to use for text on the plot.

  • LabelSize (int) – Ajusts the label sizes on the plot.

  • LaTeX (bool) – Whether to use the LaTeX engine of MatplotLib

  • TitleSize (int) – Modify the title font size.

  • LegendSize (int) – Modify the size of the legend being displayed on the plot. This is predominantly relevant for combining TH1F histograms.

  • LegendLoc (int) – Location of the legend within the plot.

  • NEvents (int) – Displays the number of events used to construct the histogram.

  • xScaling (float) – A scaling multiplier in the x-direction of the plot. This is useful when bin labels start to merge together.

  • yScaling (float) – A scaling multiplier in the y-direction of the plot. This is useful when bin labels start to merge together.

  • Alpha (float) – The alpha by which the color should be scaled by.

  • LineWidth (float) – Line width of the histogram bin lines.

  • HistFill (bool) – Whether to fill the histograms with the color assigned or not.

  • Color (str) – The color to assign the histogram.

  • Colors (list[str]) – Expects a list of string indicating the color each histogram should be assigned. If none is given then colors will be automatically assigned to each histogram.

  • Texture (str) – The filling pattern of the histogram, options are; / , \ , | , - , + , x, o, O, ., ‘*’, True, False

  • Marker (str) – Line Marker.

  • Textures (list[str])

  • Markers (list[str])

  • autoscale (bool)

  • xLogarithmic (bool) – Whether to scale the bin content logarithmically.

  • yLogarithmic (bool) – Whether to scale the bin content logarithmically.

  • Title (str) – Main title of the histogram.

  • xTitle (str) – Title to place on the x-Axis.

  • xBinCentering (bool) – Whether to center the bins of the histogram.

  • xBins (int) – The number of bins to construct the histogram with.

  • yTitle (str) – Title to place on the y-Axis.

  • Style (str) – The style to use for plotting the histogram, options are: - “ATLAS” - “ROOT” - “MPL”

  • xMin (Union[float, int]) – The minimum value to start the x-Axis with.

  • xMax (Union[float, int]) – The maximum value to end the x-Axis with.

  • yMin (Union[float, int]) – The minimum value to start the y-Axis with.

  • yMax (Union[float, int]) – The maximum value to end the y-Axis with.

  • xStep (Union[float, int]) – The step size of placing a label on the x-Axis, e.g. 0, 100, 200, …, (n-1)x100.

  • yStep (Union[float, int]) – The step size of placing a label on the y-Axis, e.g. 0, 100, 200, …, (n-1)x100.

  • xData (Union[list, dict]) – The data to plot on the xAxis.

  • yData (Union[list, dict]) – The data to plot on the yAxis.

  • xLabels (Union[list, dict]) – A list of string/values to place on the x-Axis for each bin. The labels will be placed in the same order as given in the list.

  • yLabels (Union[list, dict]) – A list of string/values to place on the y-Axis for each bin. The labels will be placed in the same order as given in the list.

  • ATLASLumi (float) – The luminosity to display on the ATLAS formated histograms.

  • ATLASData (bool) – A boolean switch to distinguish between Simulation and Data.

  • ATLASYear (int) – The year the data/simulation was collected from.

  • ATLASCom (float) – The Center of Mass used for the data/simulation.

AnalysisG.Plotting.TH1F(AnalysisG.Plotting.BasePlotting):

A simple histogram plotting class used to minimize redundant styling code. The class can be further adapted in a custom framework via class inheritance.

__histapply__() None
__makelabelaxis__() None
__fixrange__() None
__aggregate__() None
__precompile__() None
__inherit_compile__(TH1F inpt) None
Parameters:

inpt (TH1F) – Inherit the state of the caller histogram and apply it to the input.

__compile__() None
__postcompile__() None
Variables:
  • Histogram (TH1F) – A single TH1F object used to plot against (useful for underlying distributions).

  • Histograms (list[TH1F]) – Expects TH1F objects from which to construct the combined histogram.

  • Stack (bool) – Whether to combine the histograms as a stack plot.

AnalysisG.Plotting.TH2F(AnalysisG.Plotting.BasePlotting):
__fix_xrange__() None
__fix_yrange__() None
Variables:
  • xBins (int) – Number of bins to use on the x-Axis

  • yBins (int) – Number of bins to use on the y-Axis

  • xUnderFlow (bool) – Whether to reserve the last bin for data not captured on the x-Axis.

  • xOverFlow (bool) – Whether to reserve the last bin for data not captured on the x-Axis.

  • yUnderFlow (bool) – Whether to reserve the last bin for data not captured on the y-Axis.

  • yOverFlow (bool) – Whether to reserve the last bin for data not captured on the y-Axis.

AnalysisG.Plotting.TLine(AnalysisG.Plotting.BasePlotting):
__lineapply__() None
Variables:
  • yDataUp (list[float, int, ...])

  • yDataDown (list[float, int, ...])

Example: A simple TH1F plot

from AnalysisG.Plotting import TH1F

th = TH1F()
th.xBins = 100
th.xMax = 100
th.xMin = 0
th.xData = [i for i in range(100)]
th.Title = "some title"
th.xTitle = "x-Axis"
th.yTitle = "y-Axis"
th.Filename = "some-name"
th.OutputDirectory = "./Some/Path/"
th.SaveFigure()

Example: A TH1F plot with bin centering

from AnalysisG.Plotting import TH1F

th = TH1F()
th.xMin = 0
th.xStep = 20
#th.xMax = 100 <- dont include a maximum
th.xBins = 100 # <- rather define the number of bins
th.xBinCentering = True
th.xData = [i for i in range(100)]
th.Title = "some title"
th.xTitle = "x-Axis"
th.yTitle = "y-Axis"
th.Filename = "some-name"
th.OutputDirectory = "./Some/Path/"
th.SaveFigure()

Example: Combining two or more TH1F plots

from AnalysisG.Plotting import TH1F

# Define the settings to apply to all histograms
th = TH1F()
th.xMin = 0
th.xStep = 20
th.xMax = 100
th.Title = "some title"
th.xTitle = "x-Axis"
th.yTitle = "y-Axis"
th.Filename = "some-name"
th.OutputDirectory = "./Some/Path/"

# Iterate over your data
for i in MyDataDictionary:

    # Create a new TH1F instance
    th_ = TH1F()
    th_.Title = i

    # Populate this instance with some data
    th_.xData = MyDataDictionary[i]

    # Append the instance to the Histograms attribute
    th.Histograms.append(th_)

th.SaveFigure()


# To make the above code shorter, we can create a dictionary
# of commands e.g.
tmp = {"xMin" : 0, "xStep" : 20, ... , "Histograms" : []}

# and then do the same loop over the data, but populate the Histograms
# key in the tmp dictionary

for i in MyDataDictionary:
    tmp2 = {"xData" : MyDataDictionary[i], "Title" : i}
    tmp["Histograms"].append(TH1F(**tmp2))

th = TH1F(**tmp)
th.SaveFigure()

Example: A Simple TH2F Plot

from AnalysisG.Plotting import TH2F

th2 = TH2F()
th2.Title = "Some distribution plot"
th2.xTitle = "x-Title"
th2.yTitle = "y-Title"

th2.xMin = 0
th2.yMin = 0

th2.xMax = 100
th2.yMax = 100

th2.xBins = 100
th2.yBins = 100

th2.xData = [i for i in range(100)]
th2.yData = [i for i in range(100)]
th2.Filename = "Some_File"
th2.OutputDirectory = "./some/path"
th2.SaveFigure()