Skip to content

Commit

Permalink
PWGDQ: fixing the configuration file for the fit (AliceO2Group#983)
Browse files Browse the repository at this point in the history
* Updating configuration file

* Fix clang format

* Removing unnecessary dependencies

* Removing unnecessary dependencies

* MegaLinter fixes (AliceO2Group#2)

Co-authored-by: Lucamicheletti93 <luca.mike93@gmail.com>
Co-authored-by: ALICE Builder <alibuild@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 25, 2022
1 parent 92ce541 commit fbfd890
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 67 deletions.
109 changes: 76 additions & 33 deletions PWGDQ/Macros/DQFitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@
\author Luca Micheletti <luca.micheletti@cern.ch>, CERN
"""
from os.path import exists
from traceback import print_tb

import ROOT
from ROOT import TCanvas, TFile, TH1F, RooRealVar, RooDataSet, RooWorkspace, RooDataHist, RooArgSet
from ROOT import gPad, gROOT
from ROOT import (
TH1F,
RooArgSet,
RooDataHist,
RooDataSet,
RooRealVar,
RooWorkspace,
TCanvas,
TFile,
gPad,
gROOT,
)


class DQFitter:
def __init__(self, fInName, fInputName):
if not exists(fInName):
print("The input file does not exist, exit...")
exit()
self.fPdfDict = {}
self.fFileOut = TFile("FitResults.root", "RECREATE")
self.fFileIn = TFile.Open(fInName)
self.fInput = self.fFileIn.Get(fInputName)
self.fRooWorkspace = RooWorkspace('w','workspace')
self.fParNames = []
self.fFitRangeMin = []
self.fFitRangeMax = []
self.fTrialName = ""
self.fRooMass = RooRealVar("m", "#it{M} (GeV/#it{c}^{2})", 2, 5)
self.fPdfDict = {}
self.fFileOut = TFile("FitResults.root", "RECREATE")
self.fFileIn = TFile.Open(fInName)
self.fInput = self.fFileIn.Get(fInputName)
self.fRooWorkspace = RooWorkspace("w", "workspace")
self.fParNames = []
self.fFitRangeMin = []
self.fFitRangeMax = []
self.fTrialName = ""
self.fRooMass = RooRealVar("m", "#it{M} (GeV/#it{c}^{2})", 2, 5)

def SetFitConfig(self, pdfDict):
'''
"""
Method to set the fit PDFs
'''
"""
self.fPdfDict = pdfDict
self.fFitRangeMin = pdfDict["fitRangeMin"]
self.fFitRangeMax = pdfDict["fitRangeMax"]
Expand All @@ -42,13 +53,17 @@ def SetFitConfig(self, pdfDict):
parName = self.fPdfDict["parName"][i]

if not self.fPdfDict["pdf"][i] == "SUM":
gROOT.ProcessLineSync(".x fit_library/{}Pdf.cxx+".format(self.fPdfDict["pdf"][i]))
gROOT.ProcessLineSync(
".x fit_library/{}Pdf.cxx+".format(self.fPdfDict["pdf"][i])
)
nameFunc = self.fPdfDict["pdf"][i]
nameFunc += "Pdf::{}Pdf(m[2,5]".format(self.fPdfDict["pdf"][i])
pdfList.append(self.fPdfDict["pdf"][i])

for j in range(0, len(parVal)):
nameFunc += ",{}[{},{},{}]".format(parName[j], parVal[j], parLimMin[j], parLimMax[j])
nameFunc += ",{}[{},{},{}]".format(
parName[j], parVal[j], parLimMin[j], parLimMax[j]
)
self.fParNames.append(parName[j])
nameFunc += ")"
print(nameFunc)
Expand All @@ -58,7 +73,9 @@ def SetFitConfig(self, pdfDict):
nameFunc += "::sum("

for j in range(0, len(pdfList)):
nameFunc += "{}[{},{},{}]*{}Pdf".format(parName[j], parVal[j], parLimMin[j], parLimMax[j], pdfList[j])
nameFunc += "{}[{},{},{}]*{}Pdf".format(
parName[j], parVal[j], parLimMin[j], parLimMax[j], pdfList[j]
)
self.fParNames.append(parName[j])
if not j == len(pdfList) - 1:
nameFunc += ","
Expand All @@ -67,24 +84,40 @@ def SetFitConfig(self, pdfDict):
self.fRooWorkspace.factory(nameFunc)

def FitInvMassSpectrum(self, fitRangeMin, fitRangeMax):
'''
"""
Method to perform binned / unbinned fit to a ROOT histogram / tree
'''
"""
trialName = self.fTrialName + "_" + str(fitRangeMin) + "_" + str(fitRangeMax)
self.fRooWorkspace.Print()
pdf = self.fRooWorkspace.pdf("sum")
self.fRooMass.setRange("range", fitRangeMin, fitRangeMax)
fRooPlot = self.fRooMass.frame(ROOT.RooFit.Title(trialName))
if "TTree" in self.fInput.ClassName():
print("Perform unbinned fit")
rooDs = RooDataSet("data", "data", RooArgSet(self.fRooMass), ROOT.RooFit.Import(self.fInput))
rooDs = RooDataSet(
"data",
"data",
RooArgSet(self.fRooMass),
ROOT.RooFit.Import(self.fInput),
)
else:
print("Perform binned fit")
rooDs = RooDataHist("data", "data", RooArgSet(self.fRooMass), ROOT.RooFit.Import(self.fInput))
rooDs = RooDataHist(
"data",
"data",
RooArgSet(self.fRooMass),
ROOT.RooFit.Import(self.fInput),
)
pdf.fitTo(rooDs)

index = 1
histResults = TH1F("fit_results_{}".format(trialName), "fit_results_{}".format(trialName), len(self.fParNames), 0., len(self.fParNames))
histResults = TH1F(
"fit_results_{}".format(trialName),
"fit_results_{}".format(trialName),
len(self.fParNames),
0.0,
len(self.fParNames),
)
for parName in self.fParNames:
histResults.GetXaxis().SetBinLabel(index, parName)
histResults.SetBinContent(index, self.fRooWorkspace.var(parName).getVal())
Expand All @@ -96,7 +129,9 @@ def FitInvMassSpectrum(self, fitRangeMin, fitRangeMax):
pdf.paramOn(fRooPlot, ROOT.RooFit.Layout(0.55))

# Fit plot
canvasFit = TCanvas("fit_plot_{}".format(trialName), "fit_plot_{}".format(trialName), 600, 600)
canvasFit = TCanvas(
"fit_plot_{}".format(trialName), "fit_plot_{}".format(trialName), 600, 600
)
canvasFit.SetLeftMargin(0.15)
gPad.SetLeftMargin(0.15)
fRooPlot.GetYaxis().SetTitleOffset(1.4)
Expand All @@ -105,17 +140,24 @@ def FitInvMassSpectrum(self, fitRangeMin, fitRangeMax):
# Ratio plot
rooHistRatio = fRooPlot.residHist()
rooPlotRatio = self.fRooMass.frame(ROOT.RooFit.Title("Residual Distribution"))
rooPlotRatio.addPlotable(rooHistRatio,"P")
canvasRatio = TCanvas("ratio_plot_{}".format(trialName), "ratio_plot_{}".format(trialName), 600, 600)
rooPlotRatio.addPlotable(rooHistRatio, "P")
canvasRatio = TCanvas(
"ratio_plot_{}".format(trialName),
"ratio_plot_{}".format(trialName),
600,
600,
)
canvasRatio.SetLeftMargin(0.15)
rooPlotRatio.GetYaxis().SetTitleOffset(1.4)
rooPlotRatio.Draw()

# Pull plot
rooHistPull = fRooPlot.pullHist()
rooPlotPull = self.fRooMass.frame(ROOT.RooFit.Title("Pull Distribution"))
rooPlotPull.addPlotable(rooHistPull,"P")
canvasPull = TCanvas("pull_plot_{}".format(trialName), "pull_plot_{}".format(trialName), 600, 600)
rooPlotPull.addPlotable(rooHistPull, "P")
canvasPull = TCanvas(
"pull_plot_{}".format(trialName), "pull_plot_{}".format(trialName), 600, 600
)
canvasPull.SetLeftMargin(0.15)
rooPlotPull.GetYaxis().SetTitleOffset(1.4)
rooPlotPull.Draw()
Expand All @@ -127,11 +169,12 @@ def FitInvMassSpectrum(self, fitRangeMin, fitRangeMax):
canvasPull.Write()
histResults.Write()


def MultiTrial(self):
'''
"""
Method to run multiple fits of the same invariant mass distribution
'''
"""
for iRange in range(0, len(self.fFitRangeMin)):
self.FitInvMassSpectrum(self.fFitRangeMin[iRange], self.fFitRangeMax[iRange])
self.fFileOut.Close()
self.FitInvMassSpectrum(
self.fFitRangeMin[iRange], self.fFitRangeMax[iRange]
)
self.fFileOut.Close()
99 changes: 99 additions & 0 deletions PWGDQ/Macros/configFit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"input": {
"input_file_name": "tutorial.root",
"input_name": "data",
"pdf_dictionary": {
"pdf": [
"CB2",
"VWG",
"SUM"
],
"parVal": [
[
3.096,
1.2e-01,
1.089,
3.393,
2.097,
8.694
],
[
1,
1,
1
],
[
1000,
100000
]
],
"parLimMin": [
[
2.9,
7.0e-02,
1.0889,
3.3929,
2.096,
8.693
],
[
-10,
0,
-100
],
[
0,
0
]
],
"parLimMax": [
[
3.5,
1.9e-01,
1.0891,
3.3931,
2.098,
8.695
],
[
10,
100,
100
],
[
50000,
500000
]
],
"parName": [
[
"mean",
"width",
"a",
"b",
"c",
"d"
],
[
"aa",
"bb",
"cc"
],
[
"sig",
"bkg"
]
],
"fitRangeMin": [
2,
2.1,
2.2
],
"fitRangeMax": [
5,
4.9,
4.8
]
}
}
}
13 changes: 0 additions & 13 deletions PWGDQ/Macros/configFit.yml

This file was deleted.

Loading

0 comments on commit fbfd890

Please sign in to comment.