Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist001_TH1_fillrandom.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_hist
3## \notebook
4## Fill a 1D histogram with random values using predefined functions
5##
6## \macro_image
7## \macro_code
8##
9## \date November 2024
10## \author Giacomo Parolini
11import ROOT
12
13# Create a one dimensional histogram and fill it with a gaussian distribution
14h1d = ROOT.TH1D("h1d", "Test random numbers", nbinsx = 200, xlow = 0.0, xup = 10.0)
15
16# "gaus" is a predefined ROOT function. Here we are filling the histogram with
17# 10000 values sampled from that distribution.
18h1d.FillRandom("gaus", 10000)
19
20# Open a ROOT file and save the histogram
21with ROOT.TFile.Open("fillrandom_py.root", "RECREATE") as myfile:
22 myfile.WriteObject(h1d, h1d.GetName())