# Filling histograms with random numbers from a function

    //
    // To see the output of this macro, click  here
    // Here we create a Canvas named "c1" with all default parameters
    //
    gROOT.Reset(c1)
    TCanvas c1(c1,"The FillRandom example",700,900)
    TPad pad1(pad1,"The pad with the function",0.05,0.50,0.90,0.45,46)
    TPad pad2(pad2,"The pad with the histogram",0.05,0.05,0.90,0.40,46)
    pad1.Draw
    pad2.Draw
    pad1.cd
    //
    // A function (any dimension) or a formula may reference
    // an already defined formula
    //
    TFormula form1(form1,abs(sin(x)/x))
    TF1 sqroot(sqroot,"x*gaus + [3]*form1",0,10)
    sqroot.SetParameters(10,4,1,20)
    pad1.SetGridx
    pad1.SetGridy
    sqroot.SetLineColor(4)
    sqroot.SetLineWidth(6)
    sqroot.Draw
    TPaveLabel lfunction(5,39,9.8,46,"The sqroot function")
    lfunction.SetFillColor(25)
    lfunction.Draw
    //
    // Create a one dimensional histogram (one float per bin)
    // and fill it following the distribution in function sqroot.
    //
    pad2.cd
    TH1F h1f(h1f,"Test random numbers",100,0,10)
    h1f.FillRandom(sqroot,10000)
    h1f.Draw
    //
    // Open a ROOT file and save the formula, function and histogram
    //
    gSystem.Exec("/bin/rm fillrandom.root")
    TFile fill(fillrandom.root,NEW)
    form1.Write
    sqroot.Write
    h1f.Write
    fill.Close

[Home page] [Class index] [Top of the page]

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.