Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[hist] Add TH1::FillRandom overload that takes TF1 directly #17108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hist/hist/inc/TH1.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ class TH1 : public TNamed, public TAttLine, public TAttFill, public TAttMarker {
virtual Int_t Fill(const char *name, Double_t w);
virtual void FillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stride=1);
virtual void FillN(Int_t, const Double_t *, const Double_t *, const Double_t *, Int_t) {}
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom * rng = nullptr);
virtual void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom * rng = nullptr);
void FillRandom(const char *fname, Int_t ntimes=5000, TRandom * rng = nullptr);
virtual void FillRandom(TH1 *h, Int_t ntimes=5000, TRandom * rng = nullptr);
virtual Int_t FindBin(Double_t x, Double_t y=0, Double_t z=0);
virtual Int_t FindFixBin(Double_t x, Double_t y=0, Double_t z=0) const;
Expand Down
3 changes: 2 additions & 1 deletion hist/hist/inc/TH2.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class TH2 : public TH1 {
virtual Int_t Fill(const char *namex, const char *namey, Double_t w);
void FillN(Int_t, const Double_t *, const Double_t *, Int_t) override {} //MayNotUse
void FillN(Int_t ntimes, const Double_t *x, const Double_t *y, const Double_t *w, Int_t stride=1) override;
void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng = nullptr) override;
using TH1::FillRandom;
void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng = nullptr) override;
void FillRandom(TH1 *h, Int_t ntimes=5000, TRandom *rng = nullptr) override;
virtual void FitSlicesX(TF1 *f1 = nullptr, Int_t firstybin=0, Int_t lastybin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray* arr = nullptr);
virtual void FitSlicesY(TF1 *f1 = nullptr, Int_t firstxbin=0, Int_t lastxbin=-1, Int_t cut=0, Option_t *option="QNR", TObjArray* arr = nullptr);
Expand Down
3 changes: 2 additions & 1 deletion hist/hist/inc/TH3.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class TH3 : public TH1, public TAtt3D {
virtual Int_t Fill(Double_t x, const char *namey, Double_t z, Double_t w);
virtual Int_t Fill(Double_t x, Double_t y, const char *namez, Double_t w);

void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng = nullptr) override;
using TH1::FillRandom;
void FillRandom(TF1 *f1, Int_t ntimes=5000, TRandom *rng = nullptr) override;
void FillRandom(TH1 *h, Int_t ntimes=5000, TRandom *rng = nullptr) override;
virtual void FitSlicesZ(TF1 *f1 = nullptr, Int_t binminx = 1, Int_t binmaxx = 0, Int_t binminy = 1, Int_t binmaxy = 0,
Int_t cut = 0, Option_t *option = "QNR"); // *MENU*
Expand Down
10 changes: 8 additions & 2 deletions hist/hist/src/TH1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3522,12 +3522,18 @@ void TH1::DoFillN(Int_t ntimes, const Double_t *x, const Double_t *w, Int_t stri

void TH1::FillRandom(const char *fname, Int_t ntimes, TRandom * rng)
{
Int_t bin, binx, ibin, loop;
Double_t r1, x;
// - Search for fname in the list of ROOT defined functions
TF1 *f1 = (TF1*)gROOT->GetFunction(fname);
if (!f1) { Error("FillRandom", "Unknown function: %s",fname); return; }

FillRandom(f1, ntimes, rng);
}

void TH1::FillRandom(TF1 *f1, Int_t ntimes, TRandom * rng)
{
Int_t bin, binx, ibin, loop;
Double_t r1, x;

// - Allocate temporary space to store the integral and compute integral

TAxis * xAxis = &fXaxis;
Expand Down
7 changes: 2 additions & 5 deletions hist/hist/src/TH2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -711,15 +711,12 @@ void TH2::FillN(Int_t ntimes, const Double_t *x, const Double_t *y, const Double
///
/// One can also call TF2::GetRandom2 to get a random variate from a function.

void TH2::FillRandom(const char *fname, Int_t ntimes, TRandom * rng)
void TH2::FillRandom(TF1 *fobj, Int_t ntimes, TRandom * rng)
{
Int_t bin, binx, biny, ibin, loop;
Double_t r1, x, y;
//*-*- Search for fname in the list of ROOT defined functions
TObject *fobj = gROOT->GetFunction(fname);
if (!fobj) { Error("FillRandom", "Unknown function: %s",fname); return; }
TF2 * f1 = dynamic_cast<TF2*>(fobj);
if (!f1) { Error("FillRandom", "Function: %s is not a TF2, is a %s",fname,fobj->IsA()->GetName()); return; }
if (!f1) { Error("FillRandom", "Function: %s is not a TF2, is a %s",fobj->GetName(),fobj->IsA()->GetName()); return; }


TAxis & xAxis = fXaxis;
Expand Down
7 changes: 2 additions & 5 deletions hist/hist/src/TH3.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -830,15 +830,12 @@ Int_t TH3::Fill(Double_t x, Double_t y, const char *namez, Double_t w)
///
/// One can also call TF1::GetRandom to get a random variate from a function.

void TH3::FillRandom(const char *fname, Int_t ntimes, TRandom * rng)
void TH3::FillRandom(TF1 *fobj, Int_t ntimes, TRandom * rng)
{
Int_t bin, binx, biny, binz, ibin, loop;
Double_t r1, x, y,z, xv[3];
// Search for fname in the list of ROOT defined functions
TObject *fobj = gROOT->GetFunction(fname);
if (!fobj) { Error("FillRandom", "Unknown function: %s",fname); return; }
TF3 *f1 = dynamic_cast<TF3*>( fobj );
if (!f1) { Error("FillRandom", "Function: %s is not a TF3, is a %s",fname,fobj->IsA()->GetName()); return; }
if (!f1) { Error("FillRandom", "Function: %s is not a TF3, is a %s",fobj->GetName(),fobj->IsA()->GetName()); return; }

TAxis & xAxis = fXaxis;
TAxis & yAxis = fYaxis;
Expand Down
6 changes: 3 additions & 3 deletions tutorials/fit/combinedFit.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ def __call__(self, par):

fB = ROOT.TF1("fB", "expo", 0, 100)
fB.SetParameters(1, -0.05)
hB.FillRandom("fB")
hB.FillRandom(fB)

fS = ROOT.TF1("fS", "gaus", 0, 100)
fS.SetParameters(1, 30, 5)

hSB.FillRandom("fB", 2000)
hSB.FillRandom("fS", 1000)
hSB.FillRandom(fB, 2000)
hSB.FillRandom(fS, 1000)

# perform now global fit

Expand Down
4 changes: 2 additions & 2 deletions tutorials/fit/fitNormSum.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
w = ROOT.TStopwatch()
w.Start()
h_sum = ROOT.TH1D("h_ExpCB", "Exponential Bkg + CrystalBall function", nBins, -5.0, 5.0)
h_sum.FillRandom("fsum", nEvents)
h_sum.FillRandom(f_sum, nEvents)
print("Time to generate {0} events: ".format(nEvents))
w.Print()

Expand All @@ -74,7 +74,7 @@
ROOT.Math.MinimizerOptions.SetDefaultMinimizer("Minuit2")
c1 = ROOT.TCanvas("Fit", "Fit", 800, 1000)
# do a least-square fit of the spectrum
result = h_sum.Fit("fsum", "SQ")
result = h_sum.Fit(f_sum, "SQ")
result.Print()
h_sum.Draw()
print("Time to fit using ROOT TF1Normsum: ")
Expand Down
2 changes: 1 addition & 1 deletion tutorials/hist/fillrandom.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
pad2.GetFrame().SetBorderSize(5)
h1f = ROOT.TH1F("h1f","Test random numbers",200,0,10)
h1f.SetFillColor(45)
h1f.FillRandom("sqroot",10000)
h1f.FillRandom(sqroot,10000)
h1f.Draw()
c1.Update()

Expand Down
4 changes: 2 additions & 2 deletions tutorials/hist/ratioplot1.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
f1 = ROOT.TF1("f1", "exp(- x/[0] )")
f1.SetParameter(0,3)

h1.FillRandom("f1",1900)
h2.FillRandom("f1", 2000)
h1.FillRandom(f1, 1900)
h2.FillRandom(f1, 2000)
h1.Sumw2()
h2.Scale(1.9/2.)

Expand Down
4 changes: 2 additions & 2 deletions tutorials/tmva/TMVA_CNN_Classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def MakeImagesTree(n, nh, nw):
f2.SetParameter(1, ROOT.gRandom.Uniform(3, 7))
f2.SetParameter(3, ROOT.gRandom.Uniform(3, 7))

h1.FillRandom("f1", nRndmEvts)
h2.FillRandom("f2", nRndmEvts)
h1.FillRandom(f1, nRndmEvts)
h2.FillRandom(f2, nRndmEvts)

for k in range(nh):
for l in range(nw):
Expand Down
4 changes: 2 additions & 2 deletions tutorials/tmva/TMVA_RNN_Classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def MakeTimeData(n, ntime, ndim):
f1.SetParameters(1, mean1[j], sigma1[j])
f2.SetParameters(1, mean2[j], sigma2[j])

h1.FillRandom("f1", 1000)
h2.FillRandom("f2", 1000)
h1.FillRandom(f1, 1000)
h2.FillRandom(f2, 1000)

for k in range(ntime):
# std::cout << j*10+k << " ";
Expand Down
Loading