diff --git a/tutorials/hist/hist000_TH1_first.py b/tutorials/hist/hist000_TH1_first.py new file mode 100644 index 0000000000000..3088689f5dc21 --- /dev/null +++ b/tutorials/hist/hist000_TH1_first.py @@ -0,0 +1,33 @@ +## \file +## \ingroup tutorial_hist +## Hello World example for TH1 +## +## Shows how to create, fill and write a histogram to a ROOT file. +## +## \macro_code +## \macro_output +## +## \date November 2024 +## \author Giacomo Parolini (CERN) + +import ROOT + +# Open the file to write the histogram to +with ROOT.TFile.Open("outfile.root", "RECREATE") as outFile: + # Create the histogram object + # There are several constructors you can use (see TH1). In this example we use the + # simplest one, accepting a number of bins and a range. + histogram = ROOT.TH1D("histogram", "My first ROOT histogram", nbinsx = 30, xlow = 0.0, xup = 10.0) + + # Fill the histogram. In this simple example we use a fake set of data. + # The 'D' in TH1D stands for 'double', so we fill the histogram with doubles. + # In general you should prefer TH1D over TH1F unless you have a very specific reason + # to do otherwise. + values = [1, 2, 3, 3, 3, 4, 3, 2, 1, 0] + for val in values: + histogram.Fill(val) + + # Write the histogram to `outFile`. + outFile.WriteObject(histogram, histogram.GetName()) + + # When `with` block exits, `outFile` will close itself and write its contents to disk. diff --git a/tutorials/hist/hist001_TH1_fillrandom.py b/tutorials/hist/hist001_TH1_fillrandom.py new file mode 100644 index 0000000000000..c7b0da4f16ae4 --- /dev/null +++ b/tutorials/hist/hist001_TH1_fillrandom.py @@ -0,0 +1,22 @@ +## \file +## \ingroup tutorial_hist +## \notebook +## Fill a 1D histogram with random values using predefined functions +## +## \macro_image +## \macro_code +## +## \date November 2024 +## \author Giacomo Parolini +import ROOT + +# Create a one dimensional histogram and fill it with a gaussian distribution +h1d = ROOT.TH1D("h1d", "Test random numbers", nbinsx = 200, xlow = 0.0, xup = 10.0) + +# "gaus" is a predefined ROOT function. Here we are filling the histogram with +# 10000 values sampled from that distribution. +h1d.FillRandom("gaus", 10000) + +# Open a ROOT file and save the histogram +with ROOT.TFile.Open("fillrandom.root", "RECREATE") as myfile: + myfile.WriteObject(h1d, h1d.GetName()) diff --git a/tutorials/hist/hist002_TH1_fillrandom_userfunc.py b/tutorials/hist/hist002_TH1_fillrandom_userfunc.py new file mode 100644 index 0000000000000..163bc8dcfb693 --- /dev/null +++ b/tutorials/hist/hist002_TH1_fillrandom_userfunc.py @@ -0,0 +1,40 @@ +## \file +## \ingroup tutorial_hist +## \notebook +## Fill a 1D histogram from a user-defined parametric function. +## +## \macro_image +## \macro_code +## +## \date November 2024 +## \author Rene Brun, Giacomo Parolini +import ROOT + +# Create a user-defined formula. +# A function (any dimension) or a formula may reference an already defined formula +form1 = ROOT.TFormula("form1", "abs(sin(x)/x)") + +# Create a 1D function using the formula defined above and the predefined "gaus" formula. +rangeMin = 0.0 +rangeMax = 10.0 +sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax) +sqroot.SetLineColor(4) +sqroot.SetLineWidth(6) +# Set parameters to the functions "gaus" and "form1". +gausScale = 10.0 # [0] +gausMean = 4.0 # [1] +gausVar = 1.0 # [2] +form1Scale = 20.0 # [3] +sqroot.SetParameters(gausScale, gausMean, gausVar, form1Scale) + +# Create a one dimensional histogram and fill it following the distribution in function sqroot. +h1d = ROOT.TH1D("h1d", "Test random numbers", 200, rangeMin, rangeMax) + +# Use our user-defined function to fill the histogram with random values sampled from it. +h1d.FillRandom("sqroot", 10000) + +# Open a ROOT file and save the formula, function and histogram +with ROOT.TFile.Open("fillrandom.root", "RECREATE") as myFile: + myFile.WriteObject(form1, form1.GetName()) + myFile.WriteObject(sqroot, sqroot.GetName()) + myFile.WriteObject(h1d, h1d.GetName()) diff --git a/tutorials/hist/hist003_TH1_draw.py b/tutorials/hist/hist003_TH1_draw.py new file mode 100644 index 0000000000000..5d276450363de --- /dev/null +++ b/tutorials/hist/hist003_TH1_draw.py @@ -0,0 +1,68 @@ +## \file +## \ingroup tutorial_hist +## \notebook +## Draw a 1D histogram to a canvas. +## +## \note When using graphics inside a ROOT macro the objects must be created with `new`. +## +## \macro_image +## \macro_code +## +## \date November 2024 +## \author Rene Brun, Giacomo Parolini +import ROOT + +# Create and fill the histogram. +# See hist002_TH1_fillrandom_userfunc.C for more information about this section. +form1 = ROOT.TFormula("form1", "abs(sin(x)/x)") +rangeMin = 0.0 +rangeMax = 10.0 +sqroot = ROOT.TF1("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax) +sqroot.SetLineColor(4) +sqroot.SetLineWidth(6) +sqroot.SetParameters(10.0, 4.0, 1.0, 20.0) + +nBins = 200 +h1d = ROOT.TH1D("h1d", "Test random numbers", nBins, rangeMin, rangeMax) + +h1d.FillRandom("sqroot", 10000) + +# Create a canvas and draw the histogram +topX = 200 +topY = 10 +width = 700 +height = 900 +c1 = ROOT.TCanvas("c1", "The FillRandom example", topX, topY, width, height) + +# Split the canvas into two sections to plot both the function and the histogram +# The TPad's constructor accepts the relative coordinates (0 to 1) of the pad's boundaries +pad1 = ROOT.TPad("pad1", "The pad with the function", 0.05, 0.50, 0.95, 0.95) +pad2 = ROOT.TPad("pad2", "The pad with the histogram", 0.05, 0.05, 0.95, 0.45) + +# Draw the two pads +pad1.Draw() +pad2.Draw() + +# Select pad1 to draw the next objects into +pad1.cd() +pad1.SetGridx() +pad1.SetGridy() +pad1.GetFrame().SetBorderMode(-1) +pad1.GetFrame().SetBorderSize(5) + +# Draw the function in pad1 +sqroot.Draw() +# Add a label to the function. +# TPaveLabel's constructor accepts the pixel coordinates and the label string. +lfunction = ROOT.TPaveLabel(5, 39, 9.8, 46, "The sqroot function") +lfunction.Draw() +c1.Update() + +# Select pad2 to draw the next objects into +pad2.cd() +pad2.GetFrame().SetBorderMode(-1) +pad2.GetFrame().SetBorderSize(5) + +h1d.SetFillColor(45) +h1d.Draw() +c1.Update()