ROOT
master
Reference Guide
Loading...
Searching...
No Matches
hist002_TH1_fillrandom_userfunc.C
Go to the documentation of this file.
1
/// \file
2
/// \ingroup tutorial_hist
3
/// \notebook
4
/// Fill a 1D histogram from a user-defined parametric function.
5
///
6
/// \macro_image
7
/// \macro_code
8
///
9
/// \date November 2024
10
/// \author Rene Brun, Giacomo Parolini
11
12
void
hist002_TH1_fillrandom_userfunc
()
13
{
14
// Create a user-defined formula.
15
// A function (any dimension) or a formula may reference an already defined formula
16
TFormula
form1
(
"form1"
,
"abs(sin(x)/x)"
);
17
18
// Create a 1D function using the formula defined above and the predefined "gaus" formula.
19
double
rangeMin
= 0.0;
20
double
rangeMax
= 10.0;
21
TF1
sqroot
(
"sqroot"
,
"x*gaus(0) + [3]*form1"
,
rangeMin
,
rangeMax
);
22
sqroot
.SetLineColor(4);
23
sqroot
.SetLineWidth(6);
24
// Set parameters to the functions "gaus" and "form1".
25
double
gausScale
= 10.0;
// [0]
26
double
gausMean
= 4.0;
// [1]
27
double
gausVar
= 1.0;
// [2]
28
double
form1Scale
= 20.0;
// [3]
29
sqroot
.SetParameters(
gausScale
,
gausMean
,
gausVar
,
form1Scale
);
30
31
// Create a one dimensional histogram and fill it following the distribution in function sqroot.
32
int
nBins = 200;
33
TH1D
h1d
(
"h1d"
,
"Test random numbers"
, nBins,
rangeMin
,
rangeMax
);
34
35
// Use our user-defined function to fill the histogram with random values sampled from it.
36
h1d
.FillRandom(
"sqroot"
, 10000);
37
38
// Open a ROOT file and save the formula, function and histogram
39
auto
myFile
= std::unique_ptr<TFile>(
TFile::Open
(
"fillrandom_userfunc.root"
,
"RECREATE"
));
40
myFile
->WriteObject(&
form1
,
form1
.GetName());
41
myFile
->WriteObject(&
sqroot
,
sqroot
.GetName());
42
myFile
->WriteObject(&
h1d
,
h1d
.GetName());
43
}
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
TF1
1-Dim function class
Definition
TF1.h:233
TFile::Open
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
TFormula
The Formula class.
Definition
TFormula.h:89
TH1D
1-D histogram with a double per channel (see TH1 documentation)
Definition
TH1.h:693
hist002_TH1_fillrandom_userfunc
Definition
hist002_TH1_fillrandom_userfunc.py:1
tutorials
hist
hist002_TH1_fillrandom_userfunc.C
ROOT master - Reference Guide Generated on Thu Feb 13 2025 15:59:18 (GVA Time) using Doxygen 1.10.0