fit¶
ROOT required
This module requires PyROOT. See checking ROOT availability.
analysis_helpers.fit
¶
FitModels(_name='FitModels')
¶
This class contains a few RooFit models of general use
Returns:
| Name | Type | Description |
|---|---|---|
class |
An object to control the functions |
Initial call, define what's needed and what's missing
Source code in src/analysis_helpers/fit.py
SimpleWorkspace(obsname='x', xmin=-10, xmax=10, title=None, unit='')
¶
Creates a simple workspace
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obsname
|
str
|
the observable name. Defaults to 'x'. |
'x'
|
xmin
|
float
|
the minimum value of the observable |
-10
|
xmax
|
float
|
the maximum value of the observable |
10
|
title
|
str
|
the title of the observable. Defaults to None. |
None
|
unit
|
str
|
the unit of the observable. Defaults to ''. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
RooWorkspace |
a RooWorkspace with the created observable |
Source code in src/analysis_helpers/fit.py
GetObservable(ws, obsname)
¶
Retrieve the observable from the workspace
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
The workspace |
required |
obsname
|
str
|
observable name |
required |
Returns:
| Type | Description |
|---|---|
|
r.RooRealVar: the observable variable |
Source code in src/analysis_helpers/fit.py
Gaussian(ws, name, obsname)
¶
Add a gaussian PDF to the workspace and returns it
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
name of the gaussian PDF |
required |
obsname
|
str
|
name of the observable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooGaussian |
the Gaussian PDF |
Source code in src/analysis_helpers/fit.py
TwoGaussians(ws, name, obsname)
¶
A PDF made of two gaussians
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
name of the gaussian PDF |
required |
obsname
|
str
|
name of the observable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooAddPdf |
the two Gaussian PDF |
Source code in src/analysis_helpers/fit.py
ThreeGaussians(ws, name, obsname)
¶
A PDF made of three gaussians
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
name of the gaussian PDF |
required |
obsname
|
str
|
name of the observable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooAddPdf |
the three Gaussian PDF |
Source code in src/analysis_helpers/fit.py
Johnson(ws, name, obsname)
¶
A Johnson PDF
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
name of the gaussian PDF |
required |
obsname
|
str
|
name of the observable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooJohnson |
the Johnson PDF |
Source code in src/analysis_helpers/fit.py
JohnsonPlusGaussian(ws, name, obsname)
¶
A PDF made of a Johnson and a Gaussian
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
name of the gaussian PDF |
required |
obsname
|
str
|
name of the observable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooAddPdf |
the Jonhnson + Gaussian PDF |
Source code in src/analysis_helpers/fit.py
DmSignal(ws, name, obsname, sig_type=0)
¶
Define a fit model with different layers of complexity sig_type== 0 : base line, JSU + G1 + G2 1 : simpler, JSU + G1 2 : simplest, JSU 3 : complex, JSU + G1 + G2 + G3 4 : classic, G1 + G2 5 : classic complex, G1 + G2 + G3 6 : Johnson + 1 Gaussian low correlations
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
the name of the signal pdf |
required |
obsname
|
str
|
the observable name |
required |
sig_type
|
int
|
the model choice. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
RooAbsPdf |
The resulting PDF |
Source code in src/analysis_helpers/fit.py
DmBackground(ws, name, obsname, xth=None, bkg_type=0)
¶
Define a fit model with different layers of complexity bkg_type == 0 : RooThresholdDAcp 1 : RooThreshold 2 : RooDstD0BG
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
the name of the signal pdf |
required |
obsname
|
str
|
the observable name |
required |
xth
|
float
|
the threshold value. Defaults to None. |
None
|
bkg_type
|
int
|
the background model choice. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
RooAbsPdf |
the background model |
Source code in src/analysis_helpers/fit.py
DmModel(ws, name, obsname, xth=None, mtype=0, nevs=0)
¶
Define a fit model with different layers of complexity mtype== 0 : base line, JSU + G1 + G2 1 : simpler, JSU + G1 2 : simplest, JSU 3 : complex, JSU + G1 + G2 + G3 4 : classic, G1 + G2 5 : classic complex, G1 + G2 + G3 6 : Johnson + 1 Gaussian low correlations mtype+ 0 : RooThresholdDAcp mtype+10 : RooThreshold mtype+20 : RooDstD0BG
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
the name of the signal pdf |
required |
obsname
|
str
|
the observable name |
required |
xth
|
float
|
the threshold value. Defaults to None. |
None
|
mtype
|
int
|
the model choice. Defaults to 0. |
0
|
nevs
|
int
|
number of events of the extended model. Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
RooAbsPdf |
the signal+background model |
Source code in src/analysis_helpers/fit.py
DmModelBinFlip(ws, name, obsname, nevs, xth=None, mtype=0, ratios=True)
¶
Define a fit model with different layers of complexity mtype== 0 : base line, JSU + G1 + G2 1 : simpler, JSU + G1 2 : simplest, JSU 3 : complex, JSU + G1 + G2 + G3 4 : classic, G1 + G2 5 : classic complex, G1 + G2 + G3 6 : Johnson + 1 Gaussian low correlations mtype+ 0 : RooThresholdDAcp mtype+10 : RooThreshold mtype+20 : RooDstD0BG
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
name
|
str
|
the name of the signal pdf |
required |
obsname
|
str
|
the observable name |
required |
nevs
|
int
|
number of events of the extended model. |
required |
xth
|
float
|
the threshold value. Defaults to None. |
None
|
mtype
|
int
|
the model choice. Defaults to 0. |
0
|
ratios
|
bool
|
whether the model is used to calculate bin-flip ratios. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
RooSimultaneousPdf |
the signal+background model |
Source code in src/analysis_helpers/fit.py
FitUtils(_name='FitUtils')
¶
This class contains a few methods to extend RooFit capabilities
Call: FitUtils(name)
Methods:
| Name | Description |
|---|---|
- getLegend |
{index, color, style}}) |
Initial call, define what's needed and what's missing
Source code in src/analysis_helpers/fit.py
getResiduals(frame, ranges=None)
¶
Returns a RooPlot with the residuals
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
RooFrame
|
a RooFit frame where the residuals will be plotted |
required |
ranges
|
list
|
A list with the defined fit ranges to plot for the RooFrame variable. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
frame, objs
|
a dictionary with the RooPlot with the residuals (frame) and the list of objects to be added to the canvas (objs) |
Source code in src/analysis_helpers/fit.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 | |
plotFitAndResiduals(frame, name='cFit', title='Fit Results', tagLeft=[''], tagRight=[''], xSize=600, ySize=640, Blind=False, semilog=False, ranges=None)
¶
This function plots the fit results with the residuals
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
RooPlot
|
the Roofit frame where the fit results will be plotted |
required |
name
|
str
|
Name of the canvas. Defaults to 'cFit'. |
'cFit'
|
title
|
str
|
Title of the canvas. Defaults to 'Fit Results'. |
'Fit Results'
|
tagLeft
|
list
|
Labels added on the top left of the canvas. Defaults to ['']. |
['']
|
tagRight
|
list
|
Labels added on the top right of the canvas. Defaults to ['']. |
['']
|
xSize
|
int
|
Width of the canvas. Defaults to 600. |
600
|
ySize
|
int
|
Height of the canvas. Defaults to 640. |
640
|
Blind
|
bool
|
Do not plot the y axis labels. Defaults to False. |
False
|
semilog
|
bool
|
Set the y-axis to log scale. Defaults to False. |
False
|
ranges
|
list
|
A list with the defined fit ranges to plot for the RooFrame variable. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
(canvas, extra)
|
A dictionary with the canvas and the list of objects to be added to the canvas (extra) |
Source code in src/analysis_helpers/fit.py
plotFitAndResidualsPad(frame, pad, tagLeft='', tagRight='', semilog=False, Blind=False, ranges=None)
¶
This function plots the fit results with the residuals in two pads
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
RooPlot
|
The Roofit frame where the fit results will be plotted |
required |
pad
|
TPad
|
The pad where the fit results will be plotted |
required |
tagLeft
|
str
|
Labels added on the top left of the canvas. Defaults to ['']. |
''
|
tagRight
|
list
|
Labels added on the top right of the canvas. Defaults to ['']. |
''
|
semilog
|
bool
|
Set the y-axis to log scale. Defaults to False. |
False
|
Blind
|
bool
|
Do not plot the y axis labels. Defaults to False. |
False
|
ranges
|
list
|
A list with the defined fit ranges to plot for the RooFrame variable. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
list
|
the list with the extra objects created. Should be added to the |
Source code in src/analysis_helpers/fit.py
pull_array(hist, model, scale_factor=None, poisson=True)
¶
Calculate pulls of data and model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hist
|
tuple
|
tuple containing (data_counts, data_bins) arrays from histogram |
required |
model
|
array
|
model values to compare against data |
required |
scale_factor
|
float
|
scaling factor for model values. If None, uses bin width. Defaults to None. |
None
|
poisson
|
bool
|
Use Poisson error for the counts. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
the normalised pulls and the bin centers |
Source code in src/analysis_helpers/fit.py
plotFitAndResidualsMPL(frame, axes=None, labels=None)
¶
Plot the fit results with the residuals using matplotlib
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frame
|
RooPlot
|
the Roofit frame where the fit results will be plotted |
required |
axes
|
axes
|
axes where to plot. Defaults to None. |
None
|
labels
|
list
|
labels of the plotted components. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
the figure and the axes |
Source code in src/analysis_helpers/fit.py
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 | |
getLegend(elements, name='cLegend', xlow=0.6, ylow=0.4, xmax=0.95, ymax=0.95)
¶
Draw a legend
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
elements
|
dict
|
A dictionary of the elements to be added to the legend in this format |
required |
name
|
str
|
The name of the r.TPad containing the legend. Defaults to 'cLegend'. |
'cLegend'
|
xlow
|
float
|
Minimum x (NDC) of the legend box. Defaults to 0.6. |
0.6
|
ylow
|
float
|
Minimum y (NDC) of the legend box. Defaults to 0.4. |
0.4
|
xmax
|
float
|
Maximum x (NDC) of the legend box. Defaults to 0.95. |
0.95
|
ymax
|
float
|
Maximum y (NDC) of the legend box. Defaults to 0.95. |
0.95
|
Returns:
| Type | Description |
|---|---|
dict
|
a dictionary with the r.TPad ( |
Source code in src/analysis_helpers/fit.py
getIntegralsAndEvents(model, xmin, xmax, varName, sigName, bkgName, sigEvName, bkgEvName)
¶
Get integrals and number of events for signal and background
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the fit model |
required |
xmin
|
float
|
minimum x value |
required |
xmax
|
float
|
maximum x value |
required |
varName
|
str
|
name of the variable |
required |
sigName
|
str
|
name of the signal component |
required |
bkgName
|
str
|
name of the background component |
required |
sigEvName
|
str
|
name of the signal event variable |
required |
bkgEvName
|
str
|
name of the background event variable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
a dictionary with the integrals and number of events for signal, background and total |
Source code in src/analysis_helpers/fit.py
getIntegral(pdf, x, xmin, xmax, rangeName='intRange')
¶
Get the integral of a pdf over a specified range
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf
|
RooAbsPdf
|
the pdf to integrate |
required |
x
|
RooRealVar
|
the variable to integrate over |
required |
xmin
|
float
|
the minimum x value |
required |
xmax
|
float
|
the maximum x value |
required |
rangeName
|
str
|
the name of the integration range. Defaults to 'intRange'. |
'intRange'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
the value of the integral |
Source code in src/analysis_helpers/fit.py
getSignificance(model, xmin, xmax, varName, sigName, bkgName, sigEvName, bkgEvName)
¶
Get the significance of the signal, defined as $\(\frac{S}{\sigma_B}\)$ where $\(\sigma_B\)$ is the error in the total number of events
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the model |
required |
xmin
|
float
|
the minimum of the variable in the required range |
required |
xmax
|
float
|
the maximum of the variable in the required range |
required |
varName
|
str
|
the variable name |
required |
sigName
|
str
|
the signal PDF name |
required |
bkgName
|
str
|
the background PDF name |
required |
sigEvName
|
str
|
the signal yield variable name |
required |
bkgEvName
|
str
|
the background yield variable name |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooRealVar |
the significance stored in a RooRealVar |
Source code in src/analysis_helpers/fit.py
getPurity(model, xmin, xmax, varName, sigName, bkgName, sigEvName, bkgEvName)
¶
Get the purity of the signal, defined as $\(\frac{S}{S+B}\)$
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the model |
required |
xmin
|
float
|
the minimum of the variable in the required range |
required |
xmax
|
float
|
the maximum of the variable in the required range |
required |
varName
|
str
|
the variable name |
required |
sigName
|
str
|
the signal PDF name |
required |
bkgName
|
str
|
the background PDF name |
required |
sigEvName
|
str
|
the signal yield variable name |
required |
bkgEvName
|
str
|
the background yield variable name |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooRealVar |
the purity stored in a RooRealVar |
Source code in src/analysis_helpers/fit.py
fitToData(model, data, roofit_options=None, outname='', kNLL=True, kChi=False, kMinos=False, kSilent=False)
¶
Fit the model to the data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the model to fit |
required |
data
|
RooDataSet
|
the data to fit |
required |
roofit_options
|
list
|
options for the fit. Defaults to r.RooLinkedList(). |
None
|
outname
|
str
|
the filename where to store the fit results. Defaults to "". |
''
|
kNLL
|
bool
|
whether to use the negative log-likelihood for the fit. Defaults to True. |
True
|
kChi
|
bool
|
whether to use the chi-squared for the fit. Defaults to False. |
False
|
kMinos
|
bool
|
whether to use the MINOS algorithm for the fit. Defaults to False. |
False
|
kSilent
|
bool
|
whether to run the fit in silent mode. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
e
|
exception |
Returns:
| Name | Type | Description |
|---|---|---|
RooFitResult |
the result of the fit |
Source code in src/analysis_helpers/fit.py
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 | |
Migrad(minuit)
¶
Run migrad on the given minuit object
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
minuit
|
RooMinimizer
|
the minuit object to run migrad on |
required |
Returns:
| Type | Description |
|---|---|
RooFitResult
|
the output of migrad |
Source code in src/analysis_helpers/fit.py
Fit(model, data, kChi=False, kExtended=False, kMinos=False, nthreads=1, range=0, poissonError=False, fname=None, kSilent=False)
¶
A function to run fits using RooMinimizer. Likelihood by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the model to fit |
required |
data
|
RooAbsData
|
the data to fit |
required |
kChi
|
bool
|
whether to use chi-squared fit. Defaults to False. |
False
|
kExtended
|
bool
|
whether to use extended maximum likelihood. Defaults to False. |
False
|
kMinos
|
bool
|
whether to run MINOS algorithm. Defaults to False. |
False
|
nthreads
|
int
|
number of CPU threads (currently not used). Defaults to 1. |
1
|
range
|
int
|
fit range (currently not used). Defaults to 0. |
0
|
poissonError
|
bool
|
whether to use Poisson errors. Defaults to False. |
False
|
fname
|
str
|
name for the fit function. Defaults to None. |
None
|
kSilent
|
bool
|
whether to run in silent mode. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
Exception
|
any exception that occurs during fitting |
Returns:
| Name | Type | Description |
|---|---|---|
RooFitResult |
the result of the fit, or None if chi2 fit requested with wrong data type |
Source code in src/analysis_helpers/fit.py
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 | |
AddFitInfo(fname, res)
¶
Add fit information to the fit results file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fname
|
str
|
the file name |
required |
res
|
RooFitResult
|
the fit results object |
required |
Source code in src/analysis_helpers/fit.py
fitToDataAlt(model, data, outname='', kNLL=True, kChi=False, kMinos=False, kExtended=False, nthreads=1, range=None, poissonError=False, constraints=None, kSilent=False, kAsymptotic=False)
¶
Fit the model to the data (alternative implementation with enhanced options)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the model to fit |
required |
data
|
RooDataSet
|
the data to fit |
required |
outname
|
str
|
the filename where to store the fit results. Defaults to "". |
''
|
kNLL
|
bool
|
whether to use the negative log-likelihood for the fit. Defaults to True. |
True
|
kChi
|
bool
|
whether to use the chi-squared for the fit. Defaults to False. |
False
|
kMinos
|
bool
|
whether to use the MINOS algorithm for the fit. Defaults to False. |
False
|
kExtended
|
bool
|
whether to use extended maximum likelihood. Defaults to False. |
False
|
nthreads
|
int
|
number of CPU threads for parallel processing. Defaults to 1. |
1
|
range
|
str
|
fit range specification. Defaults to None. |
None
|
poissonError
|
bool
|
whether to use Poisson error handling. Defaults to False. |
False
|
constraints
|
RooArgSet
|
constraints to apply during fitting. Defaults to empty RooArgSet. |
None
|
kSilent
|
bool
|
whether to run the fit in silent mode. Defaults to False. |
False
|
kAsymptotic
|
bool
|
whether to use asymptotic error calculation. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
Exception
|
any exception that occurs during fitting |
Returns:
| Name | Type | Description |
|---|---|---|
RooFitResult |
the result of the fit |
Source code in src/analysis_helpers/fit.py
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 | |
PlotResiduals2D(model, data, obsNames=[], cname='2Dresiduals', title='residuals2D', tagLeft='', tagRight='', xbins=50, ybins=50)
¶
Plot the fit residuals in a 2D histogram
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
RooAbsPdf
|
the model used in the fit |
required |
data
|
RooAbsData
|
the data used in the fit |
required |
obsNames
|
list
|
the list of observables to project (should be 2). Defaults to []. |
[]
|
cname
|
str
|
name of the r.TCanvas. Defaults to '2Dresiduals'. |
'2Dresiduals'
|
title
|
str
|
title of the r.TCanvas. Defaults to 'residuals2D'. |
'residuals2D'
|
tagLeft
|
str
|
text to be added on the top left of the canvas. Defaults to ''. |
''
|
tagRight
|
str
|
text to be added on the top right of the canvas. Defaults to ''. |
''
|
xbins
|
int
|
number of bins on the x axis. Defaults to 50. |
50
|
ybins
|
int
|
number of bins on the y axis. Defaults to 50. |
50
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
(canvas, extra)
|
a canvas and a list of extra objects to be added to the canvas |
Source code in src/analysis_helpers/fit.py
1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 | |
DataHistFromNumpy(x_arr, observables)
¶
Build a RooDataHist from numpy data (1D or multi-dimensional).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
array
|
Input data. Supported formats: - 1D array with shape (n_samples,) - 2D array with shape (n_samples, n_observables) - 2D array with shape (n_observables, n_samples) (auto-transposed) |
required |
observables
|
(RooRealVar, RooArgSet, RooArgList, list)
|
observables used to define binning/ranges. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooDataHist |
the RooDataHist object |
Source code in src/analysis_helpers/fit.py
DatasetFromNumpy(x_arr, obs)
¶
Build a RooDataSet from a numpy array
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_arr
|
array
|
the numpy array with the data |
required |
obs
|
RooRealVar
|
the observable |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RooDataSet |
the RooDataSet object |
Source code in src/analysis_helpers/fit.py
CombineDatasets(name, title, variables, category, dsets)
¶
Combine datasets for simultaneous fitting
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of the combined dataset |
required |
title
|
str
|
description of the combined dataset |
required |
variables
|
list
|
list of variables to include in the combined dataset |
required |
category
|
RooCategory
|
the RooCategory object for associating the dataset to the model |
required |
dsets
|
dict
|
a dictionary in the form {key: r.RooDataSet/RooDataHist} of the datasets to combine |
required |
Returns:
| Type | Description |
|---|---|
RooDataSet / RooDataHist
|
the combined dataset |
Source code in src/analysis_helpers/fit.py
SaveFitResults(res, oname, verbose=False)
¶
A function to save the fit results to a file in a verbose format
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
RooFitResult
|
the output of the fit |
required |
oname
|
str
|
the name of the output file |
required |
verbose
|
bool
|
True if more detailed, False otherwise. Defaults to False. |
False
|
Source code in src/analysis_helpers/fit.py
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 | |
FixVariables(ws, pdf, fix=True, verbose=True)
¶
Fix or release all the parameters of a PDF in a RooWorkspace
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
pdf
|
RooAbsPdf
|
the pdf to fix/release the parameters of |
required |
fix
|
bool
|
True to fix, False to release. Defaults to True. |
True
|
Source code in src/analysis_helpers/fit.py
GetVarsList(ws, vlist)
¶
Create a list of r.RooRealVariables from a RooWorkspace given a list of strings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
vlist
|
list
|
the list of variables |
required |
Returns:
| Type | Description |
|---|---|
RooArgList
|
the list of r.RooRealVariables |
Source code in src/analysis_helpers/fit.py
GetVarsSet(ws, vlist)
¶
Create a set of r.RooRealVariables from a RooWorkspace given a list of strings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
vlist
|
list
|
the list of variables |
required |
Returns:
| Type | Description |
|---|---|
RooArgSet
|
the set of r.RooRealVariables |
Source code in src/analysis_helpers/fit.py
SetVarsPrintDigits(ws, ndig)
¶
Set the number of digits for the precision with which the variables are printed
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
the workspace |
required |
ndig
|
int
|
the number of digits |
required |
Source code in src/analysis_helpers/fit.py
Sweights(ws, obs, data, model_name, pdfs=[''], nevs=[''], verbose=False)
¶
Calculate s-weights
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
RooFit Workspace |
required |
obs
|
str
|
Name of the observable in the workspace. Defaults to 'x'. |
required |
data
|
str
|
Name of the dataset in the workspace. Defaults to 'data'. |
required |
model_name
|
str
|
Name of the model in the workspace. Defaults to 'model'. |
required |
pdfs
|
list
|
Names of the probability density functions in the workspace to fix for the calculation. Defaults to ['']. |
['']
|
nevs
|
list
|
Names of the yields in the workspace to use for the calculation. Defaults to ['']. |
['']
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
(tree, vars)
|
A dictionary of trees and variables |
Source code in src/analysis_helpers/fit.py
decodeResLine(line)
¶
Given a line from the output of r.RooArgSet.writeToFile(), decode the line and return a dictionary of the variables
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
line
|
str
|
the line written to file |
required |
Returns:
| Type | Description |
|---|---|
dict
|
a dictionary in the format |
|
where |
Source code in src/analysis_helpers/fit.py
resultsToDictionary(resfile)
¶
Decode the output of r.RooArgSet.writeToFile() and return a dictionary of all the variables
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resfile
|
str
|
the name of the file containing the output of r.RooArgSet.writeToFile() |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary in the format |
Source code in src/analysis_helpers/fit.py
check_variable(vinfo, z=2.0, verbose=False)
¶
Check whether a fitted variable is (too) close to the boundaries
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vinfo
|
dict
|
Variable information |
required |
z
|
float
|
Multiplier for the error. Defaults to 2.. |
2.0
|
verbose
|
bool
|
If True, print detailed information. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
A code indicating the status of the variable |
Source code in src/analysis_helpers/fit.py
testVariable(vinfo, verbose=False)
¶
Test whether a variable is in the range of the allowed values
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vinfo
|
dict
|
Variable info dictionary |
required |
verbose
|
bool
|
verbosity. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
a message if the variable is out of range, otherwise '' |
Source code in src/analysis_helpers/fit.py
testVariables(res_dict, verbose=False)
¶
Checks if the variables are within the ranges
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res_dict
|
dict
|
a dictionary with the results of the fit |
required |
verbose
|
bool
|
verbosity. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if all variables are within the ranges, False otherwise |
Source code in src/analysis_helpers/fit.py
IsGoodFit(resfile, verbose=False)
¶
Check if the fit is good
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resfile
|
str
|
the name of the file containing the output of r.RooArgSet.writeToFile() |
required |
verbose
|
bool
|
verbosity. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the fit is good, False otherwise |
Source code in src/analysis_helpers/fit.py
searchForFitProblems(resfile, verbose=False)
¶
Searches for problems in the fit results file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resfile
|
str
|
the fit results file name |
required |
verbose
|
bool
|
verbosity. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
True if the fit is good, False otherwise |
Source code in src/analysis_helpers/fit.py
GetGraphVariables(vv, yoffset=1.0, offsets=None, norm=False)
¶
Obtain a r.TGraphErrors object with the variables in vv
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vv
|
dict
|
Variables in the form of a dictionary with the following structure: {'name':{'val':value,'err':error}} |
required |
yoffset
|
float
|
The offset to draw the graph on y (useful when plotting multiple instances). Defaults to 1.. |
1.0
|
offsets
|
dict
|
Offsets to apply for a fair comparison. Structure {'name':offset}. Defaults to None. |
None
|
norm
|
bool
|
Normalise the graph. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
|
r.TGraphErrors: A r.TGraphErrors object with the variables in vv |
Source code in src/analysis_helpers/fit.py
GetWorkspaceFromFile(ws_name, file_name)
¶
Get workspace from file
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws_name
|
str
|
the name of the workspace |
required |
file_name
|
str
|
the name of the file |
required |
Returns:
| Type | Description |
|---|---|
RooWorkspace
|
the RooWorkspace |
Source code in src/analysis_helpers/fit.py
FitAnalyser(fit_results_files=[], fit_utils=None)
¶
A class to analyse fit results
Source code in src/analysis_helpers/fit.py
fit_results_to_dataframe(fit_results_files=[])
¶
Convert fit results to a pandas dataframe
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fit_results_files
|
list
|
list of fit results files. Defaults to []. |
[]
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
a dataframe with the fit results |
Source code in src/analysis_helpers/fit.py
identify_outliers(variable, threshold=5)
¶
Identify outliers in a given variable based on a threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variable
|
str
|
The variable to check for outliers. |
required |
threshold
|
float
|
The z-score threshold to identify outliers. |
5
|
Returns:
| Type | Description |
|---|---|
|
pd.DataFrame: DataFrame containing the outlier rows. |
Source code in src/analysis_helpers/fit.py
print_file_with_outliers()
¶
Print file paths of fits that contain outliers.
Source code in src/analysis_helpers/fit.py
print_bad_fits()
¶
Print file paths of fits that are not good.
Source code in src/analysis_helpers/fit.py
search_variables_close_to_boundaries(z=2.0, level=1)
¶
Search for variables that are close to their boundaries in the fit results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z
|
float
|
Multiplier for the error to define closeness. Defaults to 2.. |
2.0
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
A dictionary with file paths as keys and lists of problematic variables as values. |
Source code in src/analysis_helpers/fit.py
plot_variable_distributions(selected_vars=None, pull=False, prevent_overlap=True)
¶
Plot distributions of fit variables with optimal square grid layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
selected_vars
|
list
|
List of variables to plot. If None, plots all non-error variables. |
None
|
pull
|
bool
|
Whether to plot pulls instead of raw values. |
False
|
prevent_overlap
|
bool
|
Whether to apply text overlap prevention techniques. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(fig, axes) matplotlib figure and axes objects. |
Source code in src/analysis_helpers/fit.py
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 | |
WS(ws, obj, opts=None)
¶
A RooWorkspace helper for python
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ws
|
RooWorkspace
|
The RooWorkspace to import the object into |
required |
obj
|
TObject
|
The ROOT object to import |
required |
opts
|
list
|
options. Defaults to [r.RooFit.RecycleConflictNodes()]. |
None
|
Raises:
| Type | Description |
|---|---|
TypeError
|
The object is not of the same class as the one in the workspace |
Returns:
| Type | Description |
|---|---|
|
ROOT.TObject: The object added to the workspace |