import ROOT
from array import array
import numpy as np
def makeTH1(trnd):
hh = ROOT.TH1D("hh", "hh", 25, -10, 10)
hh.Fill(np.array([trnd.Gaus(0, 3) for _ in range(100)]))
return hh
def makeTTree(trnd):
tree = ROOT.TTree("tree", "tree")
px = array("d", [0])
py = array("d", [0])
tree.Branch("x", px, "x/D")
tree.Branch("y", py, "y/D")
for i in range(100):
px[0] = trnd.Gaus(0, 3)
py[0] = trnd.Uniform() * 30 - 15
tree.Fill()
return tree
trnd = ROOT.TRandom3()
hh = makeTH1(trnd)
x = ROOT.RooRealVar("x", "x", -10, 10)
dh = ROOT.RooDataHist("dh", "dh", [x], Import=hh)
frame = x.frame(Title="Imported ROOT.TH1 with Poisson error bars")
dh.plotOn(frame)
mean = ROOT.RooRealVar("mean", "mean", 0, -10, 10)
sigma = ROOT.RooRealVar("sigma", "sigma", 3, 0.1, 10)
gauss = ROOT.RooGaussian("gauss", "gauss", x, mean, sigma)
gauss.fitTo(dh, PrintLevel=-1)
gauss.plotOn(frame)
frame2 = x.frame(Title="Imported ROOT.TH1 with internal errors")
dh.plotOn(frame2, DataError="SumW2")
gauss.plotOn(frame2)
tree = makeTTree(trnd)
y = ROOT.RooRealVar("y", "y", -10, 10)
ds = ROOT.RooDataSet("ds", "ds", {x, y}, Import=tree)
def write_dataset(ds, filename):
outstream = ROOT.std.ofstream(filename)
ds.write(outstream)
outstream.close()
write_dataset(ds, "rf102_testData.txt")
print("\n-----------------------\nReading data from ASCII")
dataReadBack = ROOT.RooDataSet.read(
"rf102_testData.txt",
[x, y],
"D",
)
dataReadBack.Print("V")
print("\nOriginal data, line 20:")
print("\nRead-back data, line 20:")
dataReadBack.get(20).
Print(
"V")
ds.Print()
frame3 = y.frame(Title="Unbinned data shown in default frame binning")
ds.plotOn(frame3)
frame4 = y.frame(Title="Unbinned data shown with custom binning")
ds.plotOn(frame4, Binning=20)
frame5 = y.frame(Title="Unbinned data read back from ASCII file")
ds.plotOn(frame5, Binning=20)
dataReadBack.plotOn(frame5, Binning=20, MarkerColor="r", MarkerStyle=5)
c = ROOT.TCanvas("rf102_dataimport", "rf102_dataimport", 800, 800)
c.Divide(3, 2)
c.cd(1)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()
c.cd(2)
ROOT.gPad.SetLeftMargin(0.15)
frame2.GetYaxis().SetTitleOffset(1.4)
frame2.Draw()
c.cd(4)
ROOT.gPad.SetLeftMargin(0.15)
frame3.GetYaxis().SetTitleOffset(1.4)
frame3.Draw()
c.cd(5)
ROOT.gPad.SetLeftMargin(0.15)
frame4.GetYaxis().SetTitleOffset(1.4)
frame4.Draw()
c.cd(6)
ROOT.gPad.SetLeftMargin(0.15)
frame4.GetYaxis().SetTitleOffset(1.4)
frame5.Draw()
c.SaveAs("rf102_dataimport.png")
void Print(GNN_Data &d, std::string txt="")
[#1] INFO:Fitting -- RooAbsPdf::fitTo(gauss_over_gauss_Int[x]) fixing normalization set for coefficient determination to observables in data
[#1] INFO:Fitting -- using CPU computation library compiled with -mavx2
[#1] INFO:Fitting -- RooAddition::defaultErrorLevel(nll_gauss_over_gauss_Int[x]_dh) Summation contains a RooNLLVar, using its error level
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #0 because y cannot accommodate the value 14.424
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #3 because y cannot accommodate the value -12.0022
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #5 because y cannot accommodate the value 13.8261
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #6 because y cannot accommodate the value -14.9925
[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping ...
[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds) Ignored 36 out-of-range events
[#1] INFO:DataHandling -- RooDataSet::read: reading file rf102_testData.txt
[#1] INFO:DataHandling -- RooDataSet::read: read 64 events (ignored 0 out of range events)
DataStore dataset (rf102_testData.txt)
Contains 64 entries
Observables:
1) x = 0.0174204 L(-10 - 10) "x"
2) y = 9.46654 L(-10 - 10) "y"
3) blindState = Normal(idx = 0)
"Blinding State"
1) RooRealVar:: x = -0.79919
2) RooRealVar:: y = 0.0106407
1) RooRealVar:: x = -0.79919
2) RooRealVar:: y = 0.0106407
3) RooCategory:: blindState = Normal(idx = 0)
RooDataSet::ds[x,y] = 64 entries
-----------------------
Reading data from ASCII
Original data, line 20:
Read-back data, line 20: