Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist002_TH1_fillrandom_userfunc.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Fill a 1D histogram from a user-defined parametric function.

…␕

{
// Create a user-defined formula.
// A function (any dimension) or a formula may reference an already defined formula
TFormula form1("form1", "abs(sin(x)/x)");
// Create a 1D function using the formula defined above and the predefined "gaus" formula.
double rangeMin = 0.0;
double rangeMax = 10.0;
TF1 sqroot("sqroot", "x*gaus(0) + [3]*form1", rangeMin, rangeMax);
sqroot.SetLineColor(4);
sqroot.SetLineWidth(6);
// Set parameters to the functions "gaus" and "form1".
double gausScale = 10.0; // [0]
double gausMean = 4.0; // [1]
double gausVar = 1.0; // [2]
double form1Scale = 20.0; // [3]
// Create a one dimensional histogram and fill it following the distribution in function sqroot.
int nBins = 200;
TH1D h1d("h1d", "Test random numbers", nBins, 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
auto myFile = std::unique_ptr<TFile>(TFile::Open("fillrandom_userfunc.root", "RECREATE"));
myFile->WriteObject(&form1, form1.GetName());
myFile->WriteObject(&sqroot, sqroot.GetName());
myFile->WriteObject(&h1d, h1d.GetName());
}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
1-Dim function class
Definition TF1.h:233
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4130
The Formula class.
Definition TFormula.h:89
1-D histogram with a double per channel (see TH1 documentation)
Definition TH1.h:693
Date
November 2024
Author
Rene Brun, Giacomo Parolini

Definition in file hist002_TH1_fillrandom_userfunc.C.