diff --git a/tutorials/roofit/rf102_dataimport.C b/tutorials/roofit/rf102_dataimport.C index 1c1929c0a214d..bc24567fddb12 100644 --- a/tutorials/roofit/rf102_dataimport.C +++ b/tutorials/roofit/rf102_dataimport.C @@ -19,22 +19,25 @@ #include "TTree.h" #include "TH1D.h" #include "TRandom.h" -using namespace RooFit; -TH1 *makeTH1(); -TTree *makeTTree(); +TH1 *makeTH1(TRandom &trnd); +TTree *makeTTree(TRandom &trnd); void rf102_dataimport() { + using namespace RooFit; + // --------------------------------------------------- // I m p o r t i n g R O O T h i s t o g r a m s // =================================================== + TRandom3 trnd{}; + // I m p o r t T H 1 i n t o a R o o D a t a H i s t // --------------------------------------------------------- // Create a ROOT TH1 histogram - TH1 *hh = makeTH1(); + TH1 *hh = makeTH1(trnd); // Declare observable x RooRealVar x("x", "x", -10, 10); @@ -80,7 +83,7 @@ void rf102_dataimport() // I m p o r t T T r e e i n t o a R o o D a t a S e t // ----------------------------------------------------------- - TTree *tree = makeTTree(); + TTree *tree = makeTTree(trnd); // Define 2nd observable y RooRealVar y("y", "y", -10, 10); @@ -166,17 +169,17 @@ void rf102_dataimport() } // Create ROOT TH1 filled with a Gaussian distribution -TH1 *makeTH1() +TH1 *makeTH1(TRandom &trnd) { TH1D *hh = new TH1D("hh", "hh", 25, -10, 10); for (int i = 0; i < 100; i++) { - hh->Fill(gRandom->Gaus(0, 3)); + hh->Fill(trnd.Gaus(0, 3)); } return hh; } // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y -TTree *makeTTree() +TTree *makeTTree(TRandom &trnd) { TTree *tree = new TTree("tree", "tree"); double *px = new double; @@ -184,8 +187,8 @@ TTree *makeTTree() tree->Branch("x", px, "x/D"); tree->Branch("y", py, "y/D"); for (int i = 0; i < 100; i++) { - *px = gRandom->Gaus(0, 3); - *py = gRandom->Uniform() * 30 - 15; + *px = trnd.Gaus(0, 3); + *py = trnd.Uniform() * 30 - 15; tree->Fill(); } return tree; diff --git a/tutorials/roofit/rf102_dataimport.py b/tutorials/roofit/rf102_dataimport.py index 8670d535f3295..30cf8591a0a53 100644 --- a/tutorials/roofit/rf102_dataimport.py +++ b/tutorials/roofit/rf102_dataimport.py @@ -15,17 +15,17 @@ from array import array -def makeTH1(): +def makeTH1(trnd): # Create ROOT ROOT.TH1 filled with a Gaussian distribution hh = ROOT.TH1D("hh", "hh", 25, -10, 10) for i in range(100): - hh.Fill(ROOT.gRandom.Gaus(0, 3)) + hh.Fill(trnd.Gaus(0, 3)) return hh -def makeTTree(): +def makeTTree(trnd): # Create ROOT ROOT.TTree filled with a Gaussian distribution in x and a # uniform distribution in y @@ -35,11 +35,12 @@ def makeTTree(): tree.Branch("x", px, "x/D") tree.Branch("y", py, "y/D") for i in range(100): - px[0] = ROOT.gRandom.Gaus(0, 3) - py[0] = ROOT.gRandom.Uniform() * 30 - 15 + px[0] = trnd.Gaus(0, 3) + py[0] = trnd.Uniform() * 30 - 15 tree.Fill() return tree +trnd = ROOT.TRandom3() ############################ # Importing ROOT histograms @@ -47,7 +48,7 @@ def makeTTree(): # Import ROOT TH1 into a RooDataHist # --------------------------------------------------------- # Create a ROOT TH1 histogram -hh = makeTH1() +hh = makeTH1(trnd) # Declare observable x x = ROOT.RooRealVar("x", "x", -10, 10) @@ -90,7 +91,7 @@ def makeTTree(): # ----------------------------------------------------------- # Import ROOT TTree into a RooDataSet -tree = makeTTree() +tree = makeTTree(trnd) # Define 2nd observable y y = ROOT.RooRealVar("y", "y", -10, 10) diff --git a/tutorials/roofit/rf303_conditional.C b/tutorials/roofit/rf303_conditional.C index 5effeacb03a9a..e6d3186413fb9 100644 --- a/tutorials/roofit/rf303_conditional.C +++ b/tutorials/roofit/rf303_conditional.C @@ -91,6 +91,8 @@ void rf303_conditional() RooDataSet *makeFakeDataXY() { + TRandom3 trnd{}; + RooRealVar x("x", "x", -10, 10); RooRealVar y("y", "y", -10, 10); RooArgSet coord(x, y); @@ -98,8 +100,8 @@ RooDataSet *makeFakeDataXY() RooDataSet *d = new RooDataSet("d", "d", RooArgSet(x, y)); for (int i = 0; i < 10000; i++) { - double tmpy = gRandom->Gaus(0, 10); - double tmpx = gRandom->Gaus(0.5 * tmpy, 1); + double tmpy = trnd.Gaus(0, 10); + double tmpx = trnd.Gaus(0.5 * tmpy, 1); if (fabs(tmpy) < 10 && fabs(tmpx) < 10) { x.setVal(tmpx); y.setVal(tmpy); diff --git a/tutorials/roofit/rf303_conditional.py b/tutorials/roofit/rf303_conditional.py index 69bf1fd1a9392..e3b4c2f93d9e2 100644 --- a/tutorials/roofit/rf303_conditional.py +++ b/tutorials/roofit/rf303_conditional.py @@ -17,6 +17,9 @@ def makeFakeDataXY(): + + trnd = ROOT.TRandom3() + x = ROOT.RooRealVar("x", "x", -10, 10) y = ROOT.RooRealVar("y", "y", -10, 10) coord = {x, y} @@ -24,8 +27,8 @@ def makeFakeDataXY(): d = ROOT.RooDataSet("d", "d", coord) for i in range(10000): - tmpy = ROOT.gRandom.Gaus(0, 10) - tmpx = ROOT.gRandom.Gaus(0.5 * tmpy, 1) + tmpy = trnd.Gaus(0, 10) + tmpx = trnd.Gaus(0.5 * tmpy, 1) if (abs(tmpy) < 10) and (abs(tmpx) < 10): x.setVal(tmpx) y.setVal(tmpy) diff --git a/tutorials/roofit/rf401_importttreethx.C b/tutorials/roofit/rf401_importttreethx.C index 826d334caf789..0fc3eebde4b50 100644 --- a/tutorials/roofit/rf401_importttreethx.C +++ b/tutorials/roofit/rf401_importttreethx.C @@ -26,18 +26,20 @@ using namespace RooFit; -TH1 *makeTH1(const char *name, double mean, double sigma); -TTree *makeTTree(); +TH1 *makeTH1(TRandom &trnd, const char *name, double mean, double sigma); +TTree *makeTTree(TRandom &trnd); void rf401_importttreethx() { + TRandom3 trnd{}; + // I m p o r t m u l t i p l e T H 1 i n t o a R o o D a t a H i s t // -------------------------------------------------------------------------- // Create thee ROOT TH1 histograms - TH1 *hh_1 = makeTH1("hh1", 0, 3); - TH1 *hh_2 = makeTH1("hh2", -3, 1); - TH1 *hh_3 = makeTH1("hh3", +3, 4); + TH1 *hh_1 = makeTH1(trnd, "hh1", 0, 3); + TH1 *hh_2 = makeTH1(trnd, "hh2", -3, 1); + TH1 *hh_3 = makeTH1(trnd, "hh3", +3, 4); // Declare observable x RooRealVar x("x", "x", -10, 10); @@ -61,7 +63,7 @@ void rf401_importttreethx() // I m p o r t i n g a T T r e e i n t o a R o o D a t a S e t w i t h c u t s // ----------------------------------------------------------------------------------------- - TTree *tree = makeTTree(); + TTree *tree = makeTTree(trnd); // Define observables y,z RooRealVar y("y", "y", -10, 10); @@ -108,18 +110,18 @@ void rf401_importttreethx() dsABC.Print(); } -TH1 *makeTH1(const char *name, double mean, double sigma) +TH1 *makeTH1(TRandom &trnd, const char *name, double mean, double sigma) { // Create ROOT TH1 filled with a Gaussian distribution TH1D *hh = new TH1D(name, name, 100, -10, 10); for (int i = 0; i < 1000; i++) { - hh->Fill(gRandom->Gaus(mean, sigma)); + hh->Fill(trnd.Gaus(mean, sigma)); } return hh; } -TTree *makeTTree() +TTree *makeTTree(TRandom &trnd) { // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y @@ -133,9 +135,9 @@ TTree *makeTTree() tree->Branch("z", pz, "z/D"); tree->Branch("i", pi, "i/I"); for (int i = 0; i < 100; i++) { - *px = gRandom->Gaus(0, 3); - *py = gRandom->Uniform() * 30 - 15; - *pz = gRandom->Gaus(0, 5); + *px = trnd.Gaus(0, 3); + *py = trnd.Uniform() * 30 - 15; + *pz = trnd.Gaus(0, 5); *pi = i % 3; tree->Fill(); } diff --git a/tutorials/roofit/rf401_importttreethx.py b/tutorials/roofit/rf401_importttreethx.py index dccdc53c27ea2..0c9bfdb3d3deb 100644 --- a/tutorials/roofit/rf401_importttreethx.py +++ b/tutorials/roofit/rf401_importttreethx.py @@ -16,17 +16,17 @@ from array import array -def makeTH1(name, mean, sigma): +def makeTH1(trnd, name, mean, sigma): """Create ROOT TH1 filled with a Gaussian distribution.""" hh = ROOT.TH1D(name, name, 100, -10, 10) for i in range(1000): - hh.Fill(ROOT.gRandom.Gaus(mean, sigma)) + hh.Fill(trnd.Gaus(mean, sigma)) return hh -def makeTTree(): +def makeTTree(trnd): """Create ROOT ROOT.TTree filled with a Gaussian distribution in x and a uniform distribution in y.""" tree = ROOT.TTree("tree", "tree") @@ -39,22 +39,23 @@ def makeTTree(): tree.Branch("z", pz, "z/D") tree.Branch("i", pi, "i/I") for i in range(100): - px[0] = ROOT.gRandom.Gaus(0, 3) - py[0] = ROOT.gRandom.Uniform() * 30 - 15 - pz[0] = ROOT.gRandom.Gaus(0, 5) + px[0] = trnd.Gaus(0, 3) + py[0] = trnd.Uniform() * 30 - 15 + pz[0] = trnd.Gaus(0, 5) pi[0] = i % 3 tree.Fill() return tree +trnd = ROOT.TRandom3() # Import multiple TH1 into a RooDataHist # ---------------------------------------------------------- # Create thee ROOT ROOT.TH1 histograms -hh_1 = makeTH1("hh1", 0, 3) -hh_2 = makeTH1("hh2", -3, 1) -hh_3 = makeTH1("hh3", +3, 4) +hh_1 = makeTH1(trnd, "hh1", 0, 3) +hh_2 = makeTH1(trnd, "hh2", -3, 1) +hh_3 = makeTH1(trnd, "hh3", +3, 4) # Declare observable x x = ROOT.RooRealVar("x", "x", -10, 10) @@ -76,7 +77,7 @@ def makeTTree(): # Importing a ROOT TTree into a RooDataSet with cuts # -------------------------------------------------------------------- -tree = makeTTree() +tree = makeTTree(trnd) # Define observables y,z y = ROOT.RooRealVar("y", "y", -10, 10) diff --git a/tutorials/roofit/rf609_xychi2fit.C b/tutorials/roofit/rf609_xychi2fit.C index 52c6bbf971a8d..baf39741e81ed 100644 --- a/tutorials/roofit/rf609_xychi2fit.C +++ b/tutorials/roofit/rf609_xychi2fit.C @@ -37,7 +37,10 @@ void rf609_xychi2fit() RooRealVar y("y", "y", -10, 200); RooDataSet dxy("dxy", "dxy", {x, y}, StoreError({x, y})); + TRandom3 trnd{}; + // Fill an example dataset with X,err(X),Y,err(Y) values + RooArgSet vars{x, y}; for (int i = 0; i <= 10; i++) { // Set X value and error @@ -45,10 +48,10 @@ void rf609_xychi2fit() x.setError(i < 5 ? 0.5 / 1. : 1.0 / 1.); // Set Y value and error - y = x.getVal() * x.getVal() + 4 * fabs(gRandom->Gaus()); + y = x.getVal() * x.getVal() + 4 * std::abs(trnd.Gaus()); y.setError(sqrt(y.getVal())); - dxy.add({x, y}); + dxy.add(vars); } // P e r f o r m c h i 2 f i t t o X + / - d x a n d Y + / - d Y v a l u e s diff --git a/tutorials/roofit/rf609_xychi2fit.py b/tutorials/roofit/rf609_xychi2fit.py index 954d3a181bda9..201d3c33dcf38 100644 --- a/tutorials/roofit/rf609_xychi2fit.py +++ b/tutorials/roofit/rf609_xychi2fit.py @@ -14,6 +14,7 @@ import ROOT import math +trnd = ROOT.TRandom3() # Create dataset with X and Y values # ------------------------------------------------------------------- @@ -35,7 +36,7 @@ x.setError((0.5 / 1.0) if (i < 5) else (1.0 / 1.0)) # Set Y value and error - y.setVal(x.getVal() * x.getVal() + 4 * abs(ROOT.gRandom.Gaus())) + y.setVal(x.getVal() * x.getVal() + 4 * abs(trnd.Gaus())) y.setError(math.sqrt(y.getVal())) dxy.add({x, y})