Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rf619_discrete_profiling.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_roofit_main
3## \notebook -js
4## Basic functionality: demonstrate fitting multiple models using RooMultiPdf and selecting the best one via Discrete
5## Profiling method.
6##
7## \macro_image
8## \macro_code
9## \macro_output
10##
11## \date July 2025
12## \author Galin Bistrev
13
14import ROOT
15
16x = ROOT.RooRealVar("x", "Observable", 0, 50)
17
18
19lambda1 = ROOT.RooRealVar("lambda1", "slope1", -0.025, -0.1, -0.02)
20expo1 = ROOT.RooExponential("expo1", "Exponential 1", x, lambda1)
21
22c0 = ROOT.RooRealVar("c0", "Cheby coeff 0", -1.0, -1.0, 1.0)
23c1 = ROOT.RooRealVar("c1", "Cheby coeff 1", 0.4, 0.05, 0.5)
24chebCoeffs = ROOT.RooArgList(c0, c1)
25cheb = ROOT.RooChebychev("cheb", "Chebyshev PDF", x, chebCoeffs)
26
27pdfIndex0 = ROOT.RooCategory("pdfIndex0", "pdf index 0")
28multiPdf0 = ROOT.RooMultiPdf("multiPdf0", "multiPdf0", pdfIndex0, ROOT.RooArgList(expo1, cheb))
29
30
31lambdaExtra = ROOT.RooRealVar("lambdaExtra", "extra slope", -0.05, -1.0, -0.01)
32expoExtra = ROOT.RooExponential("expoExtra", "extra exponential", x, lambdaExtra)
33
34
35mean = ROOT.RooRealVar("mean", "shared mean", 25, 0, 50)
36sigmaG = ROOT.RooRealVar("sigmaG", "Gaussian width", 2.0, 0.0, 5.0)
37sigmaL = ROOT.RooRealVar("sigmaL", "Landau width", 3.0, 1.0, 8.0)
38
39gauss1 = ROOT.RooGaussian("gauss1", "Gaussian", x, mean, sigmaG)
40landau1 = ROOT.RooLandau("landau1", "Landau", x, mean, sigmaL)
41
42pdfIndex1 = ROOT.RooCategory("pdfIndex1", "pdf index 1")
43multiPdf1 = ROOT.RooMultiPdf("multiPdf1", "multiPdf1", pdfIndex1, ROOT.RooArgList(gauss1, landau1))
44
45
46sigmaExtra = ROOT.RooRealVar("sigmaExtra", "extra Gaussian width", 3.0, 1.0, 6.0)
47gaussExtra = ROOT.RooGaussian("gaussExtra", "extra Gaussian", x, mean, sigmaExtra)
48
49
50frac0 = ROOT.RooRealVar("frac0", "fraction for cat0", 0.7, 0.0, 1.0)
51addPdf0 = ROOT.RooAddPdf("addPdf0", "multiPdf0 + extra expo", ROOT.RooArgList(multiPdf0, gaussExtra), frac0)
52
53frac1 = ROOT.RooRealVar("frac1", "fraction for cat1", 0.5, 0.0, 1.0)
54addPdf1 = ROOT.RooAddPdf("addPdf1", "multiPdf1 + extra gauss", ROOT.RooArgList(multiPdf1, expoExtra), frac1)
55catIndex = ROOT.RooCategory("catIndex", "Category")
56catIndex.defineType("cat0", 0)
57catIndex.defineType("cat1", 1)
58
59simPdf = ROOT.RooSimultaneous("simPdf", "simultaneous model", catIndex)
60simPdf.addPdf(addPdf0, "cat0")
61simPdf.addPdf(addPdf1, "cat1")
62
63
64data0 = addPdf0.generate(ROOT.RooArgSet(x), 800)
65data1 = addPdf1.generate(ROOT.RooArgSet(x), 1000)
66
67
68frame0 = x.frame()
69data0.plotOn(frame0)
70addPdf0.plotOn(frame0)
73
75frame0.GetXaxis().SetTitle("Observable")
76frame0.GetYaxis().SetTitle("Events")
77
78leg0 = ROOT.TLegend(0.6, 0.7, 0.9, 0.9)
79leg0.AddEntry(frame0.getObject(0), "Data", "lep")
80leg0.AddEntry(frame0.getObject(1), "Expo", "l")
81leg0.AddEntry(frame0.getObject(2), "Poly", "l")
82
83
84frame1 = x.frame()
85data1.plotOn(frame1)
86addPdf1.plotOn(frame1)
89
91frame1.GetXaxis().SetTitle("Observable")
92frame1.GetYaxis().SetTitle("Events")
93
94leg1 = ROOT.TLegend(0.6, 0.7, 0.9, 0.9)
95leg1.AddEntry(frame1.getObject(0), "Data", "lep")
96leg1.AddEntry(frame1.getObject(1), "Gauss", "l")
97leg1.AddEntry(frame1.getObject(2), "Landau", "l")
98
99
100combined_data = ROOT.RooDataSet("data", "combined", ROOT.RooArgSet(x, catIndex))
101
102combined_vars = ROOT.RooArgSet(x, catIndex)
103
104for i in range(data0.numEntries()):
105 x.setVal(data0.get(i).getRealValue("x"))
106 catIndex.setLabel("cat0")
107 combined_data.add(combined_vars)
108
109for i in range(data1.numEntries()):
110 x.setVal(data1.get(i).getRealValue("x"))
111 catIndex.setLabel("cat1")
112 combined_data.add(combined_vars)
113
114
115nll = simPdf.createNLL(combined_data)
116minim = ROOT.RooMinimizer(nll)
118minim.setEps(1e-7)
120
121
122nMeanPoints = 40
123meanMin = 17
124meanMax = 33
125
126combosToPlot = [[i, j] for i in range(pdfIndex0.numTypes()) for j in range(pdfIndex1.numTypes())]
127
129markers = [20, 21, 22, 23, 33]
130
131graphs = []
132for idx in range(len(combosToPlot)):
133 g = ROOT.TGraph(nMeanPoints)
134 g.SetLineColor(colors[idx % 5])
135 g.SetMarkerColor(colors[idx % 5])
136 g.SetMarkerStyle(markers[idx % 5])
137 g.SetTitle(f"Combo [{combosToPlot[idx][0]},{combosToPlot[idx][1]}]")
139
140profileGraph = ROOT.TGraph(nMeanPoints)
145profileGraph.SetTitle("Profile")
146graphs.append(profileGraph)
147
148
149for i in range(nMeanPoints):
150 meanVal = meanMin + i * (meanMax - meanMin) / (nMeanPoints - 1)
151 mean.setVal(meanVal)
152
153 for comboIdx, combo in enumerate(combosToPlot):
154 pdfIndex0.setIndex(combo[0])
155 pdfIndex1.setIndex(combo[1])
158 mean.setConstant(True)
159 minim.minimize("Minuit2", "Migrad")
160 graphs[comboIdx].SetPoint(i, meanVal, nll.getVal())
163 mean.setConstant(False)
164
165 mean.setConstant(True)
166 minim.minimize("Minuit2", "Migrad")
167 profileGraph.SetPoint(i, meanVal, nll.getVal())
168
169
170c = ROOT.TCanvas("c_rf619", "NLL vs Mean for Different Discrete Combinations", 1200, 400)
171c.Divide(3, 1)
172
173c.cd(1)
175frame0.GetYaxis().SetTitleOffset(1.4)
177leg0.Draw()
178
179c.cd(2)
181frame1.GetYaxis().SetTitleOffset(1.4)
183leg1.Draw()
184
185c.cd(3)
187mg = ROOT.TMultiGraph()
188for g in graphs:
189 mg.Add(g, "PL")
190mg.Draw("APL")
191mg.GetXaxis().SetTitle("Mean")
192mg.GetYaxis().SetTitle("NLL")
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len