def __init__( self, ParNames, AnaFiles = [ ], RefFile = '' ) : assert ParNames and hasattr( ParNames, '__iter__' ) and hasattr( ParNames, '__len__' ) and len(ParNames) > 0, 'P2VV'\ 'P2VV - ERROR: fitResultsAnalysis: no parameter names specified' self._parNames = ParNames from P2VV.Parameterizations.FullPDFs import PdfConfiguration self._refPars = PdfConfiguration() self._anaPars = PdfConfiguration() self._refParVals = [ None ] * len(self._parNames) self._anaParVals = { } self._parHists = { } self._nFiles = 0 self.readRefFile(RefFile) self.readAnaFiles(AnaFiles)
massPdfPars = sWeightMassPdf.getParameters(preDataSet) if model == 'ipatia2' : # set values of constant Ipatia 2 parameters from P2VV.Utilities.General import getSplitPar for splitCat in sWeightMassPdf.indexCat() : sWeightMassPdf.indexCat().setIndex( splitCat.getVal() ) KKState = observables['KKMassCat'].getLabel() for it, parName in enumerate(constSplitPars) : par = getSplitPar( parName, KKState, massPdfPars ) par.setVal( constSplitVals[KKState][it] ) if parFileIn or parFileOut : # create PDF configuration object from P2VV.Parameterizations.FullPDFs import PdfConfiguration as PdfConfig pdfConfig = PdfConfig() print 120 * '=' if parFileIn : # read parameters from file pdfConfig.readParametersFromFile( filePath = parFileIn ) pdfConfig.setParametersInPdf(sWeightMassPdf) print 'P2VV - INFO: createBs2JpsiKKDataSet: observables in mass PDF:' massPdfObs.Print('v') print 'P2VV - INFO: createBs2JpsiKKDataSet: parameters in mass PDF:' massPdfPars.Print('v') # determine mass parameters with a fit print 'P2VV - INFO: createBs2JpsiKKDataSet: fitting mass PDF' simMassFitResult = sWeightMassPdf.fitTo( preDataSet, Save = True, SumW2Error = False, NumCPU = args.numCPU, Optimize = 1, Timer = True , Minimizer = 'Minuit2', Strategy = 1, Offset = True, Hesse = runHesse, Minos = minosPars )
pdf = SimultaneousPdf( protoPdf.GetName() + '_simul', MasterPdf = protoPdf , SplitCategories = splitCats, SplitParameters = splitPars ) # set values of split parameters for par, vals in tResPars.iteritems() : for cat, val in vals.iteritems() : ws[ par + '_' + cat ].setVal(val) for par, vals in SWaveAmps.iteritems() : for cat, val in vals.iteritems() : ws[ par + '_' + cat ].setVal(val) else : pdf = protoPdf if parFileIn : # read parameter values from file from P2VV.Parameterizations.FullPDFs import PdfConfiguration pdfConfig = PdfConfiguration() pdfConfig.readParametersFromFile( filePath = parFileIn ) pdfConfig.setParametersInPdf(pdf) ########################################################################################################################################### ## compute angular efficiency moments ## ######################################## # print PDF, data, variables and parameters print '\nData set:' data.Print() if normPdf : print '\nPDF:' pdf.Print()
if valPos < 0 : print 'plotNLL: ERROR: file "%s" is not an NLL file for parameter "%s"' % ( filePath, testParName ) if parValsNLL : minNLL = min(nllVals) parValsNLLArr = array( 'd', parValsNLL ) nllValsArr = array( 'd', [ val - minNLL for val in nllVals ] ) # get fit values parValsFit = [ ] nllValsFit = [ ] from P2VV.Parameterizations.FullPDFs import PdfConfiguration for filePath in fitFilePaths : pdfConfig = PdfConfiguration() fitStatus = pdfConfig.readParametersFromFile( filePath, Verbose = False ) if fitStatus[0] != 0 : print 'plotNLL: WARNING: fit status is "%d" for fit "%s"' % ( fitStatus[0], filePath ) assert testParName in pdfConfig.parameters(), 'plotNLL: ERROR: "%s" not in parameters of file "%s"' % ( testParName, filePath ) parVal = pdfConfig.parameters()[testParName][0] pos = 0 for it in range( len(parValsFit) ) : if parVal > parValsFit[it] : break pos += 1 parValsFit.insert( pos, parVal ) nllValsFit.insert( pos, fitStatus[1] ) print parValsFit
class fitResultsAnalysis(object) : def __init__( self, ParNames, AnaFiles = [ ], RefFile = '' ) : assert ParNames and hasattr( ParNames, '__iter__' ) and hasattr( ParNames, '__len__' ) and len(ParNames) > 0, 'P2VV'\ 'P2VV - ERROR: fitResultsAnalysis: no parameter names specified' self._parNames = ParNames from P2VV.Parameterizations.FullPDFs import PdfConfiguration self._refPars = PdfConfiguration() self._anaPars = PdfConfiguration() self._refParVals = [ None ] * len(self._parNames) self._anaParVals = { } self._parHists = { } self._nFiles = 0 self.readRefFile(RefFile) self.readAnaFiles(AnaFiles) def readRefFile( self, file ) : self._refParVals = [ None ] * len(self._parNames) if file : self._refPars.readParametersFromFile( filePath = file ) parVals = self._refPars.parameters() for parIt, name in enumerate(self._parNames) : assert name in parVals\ , 'P2VV - ERROR: fitResultsAnalysis.readRefFile(): parameter "%s" not found in file "%s"' % ( name, file ) self._refParVals[parIt] = ( parVals[name][0], parVals[name][1] ) def readAnaFiles( self, files ) : self._anaParVals = dict( [ ( name, [ ] ) for name in self._parNames ] ) self._nFiles = 0 if files : fileStats = { } for file in files : fitStatus = self._anaPars.readParametersFromFile( filePath = file, Verbose = False ) if fitStatus[0] : print 'P2VV - WARNING: fitResultsAnalysis.readAnaFiles(): fit status %d for file "%s"' % ( fitStatus[0], file ) if not fitStatus[0] in fileStats : fileStats[ fitStatus[0] ] = 1 else : fileStats[ fitStatus[0] ] += 1 parVals = self._anaPars.parameters() for name, vals in self._anaParVals.iteritems() : assert name in parVals\ , 'P2VV - ERROR: fitResultsAnalysis.readAnaFiles(): parameter "%s" not found in file "%s"' % ( name, file ) vals.append( parVals[name][ : 2 ] ) self._nFiles = sum( stats for stats in fileStats.itervalues() ) print 'P2VV - INFO: fitResultsAnalysis.readAnaFiles(): read %d parameter files for analysis (fit status: %s)'\ % ( self._nFiles, ', '.join( '%d: %d' % ( stat, count ) for stat, count in fileStats.iteritems() ) ) def processResults( self, **kwargs ) : if not self._anaParVals or self._nFiles == 0 : print 'P2VV - WARNING: fitResultsAnalysis.processResults(): no parameter values available for analysis' return histsFile = kwargs.pop( 'HistsFile', '' ).split('.')[0] anaParSetts = kwargs.pop( 'ParSettings', { } ) toyMode = kwargs.pop( 'ToyMode', False ) self._nParVals = [ [ 0, 0, 0 ] for name in self._parNames ] self._maxPulls = [ 0. for name in self._parNames ] self._parVals = [ [ ] for name in self._parNames ] self._parSums = [ [ 0., 0. ] for name in self._parNames ] self._errSums = [ 0. for name in self._parNames ] self._pullVals = [ [ ] for name in self._parNames ] self._pullSums = [ [ 0., 0., 0., 0., 0., 0. ] for name in self._parNames ] if histsFile : from P2VV.Load import LHCbStyle from ROOT import TH1D, TLine, TCanvas, kFullDotLarge, kBlue dCanv = TCanvas('dummy') dCanv.Print( histsFile + '_pars.pdf[' ) dCanv.Print( histsFile + '_pulls.pdf[' ) def drawHist( parName, parHist, fileName, doFit = False, refVal = None ) : print 'P2VV - INFO: fitResultsAnalysis.processResults(): drawing "%s" histogram' % parName parSetts = anaParSetts[parName] if parName in anaParSetts else { } self._parHists[parName] = parHist parHist.GetXaxis().SetNdivisions( 7, 5, 0 ) parHist.SetTitleOffset( 1.15, 'x' ) parHist.SetTitleOffset( 1.05, 'y' ) parHist.SetXTitle( parSetts['name'] if 'name' in parSetts else parName ) parHist.SetYTitle('Entries / %.2g' % parHist.GetBinWidth(1) ) parHist.SetMarkerStyle(kFullDotLarge) parHist.SetMarkerSize(0.7) parHist.SetLineWidth(2) if doFit : parHist.Fit('gaus') fitFunc = parHist.GetFunction('gaus') fitFunc.SetNpx(1000) fitFunc.SetLineWidth(3) fitFunc.SetLineColor(kBlue) histMax = -1. for bin in range( 1, parHist.GetNbinsX() + 1 ) : val = parHist.GetBinContent(bin) + parHist.GetBinErrorUp(bin) if val > histMax : histMax = val assert histMax > 0. histMax *= 1.05 parHist.SetMinimum(0.) parHist.SetMaximum(histMax) canv = TCanvas(parName) canv.SetLeftMargin(0.19) canv.SetRightMargin(0.05) canv.SetBottomMargin(0.21) canv.SetTopMargin(0.05) parHist.Draw('E1') if refVal != None : line = TLine() line.DrawLine( refVal, 0., refVal, histMax ) canv.Print(fileName) for parIt, name in enumerate(self._parNames) : isPhase = name in anaParSetts and 'isPhase' in anaParSetts[name] and anaParSetts[name]['isPhase'] from math import floor, pi for vals in self._anaParVals[name] : self._nParVals[parIt][0] += 1 val = vals[0] if self._refParVals[parIt] != None : diff = val - self._refParVals[parIt][0] if isPhase and abs(diff) >= pi : nShift = floor( 0.5 * ( diff / pi + 1. ) ) val -= nShift * 2. * pi diff -= nShift * 2. * pi pull = diff / ( vals[1] if toyMode else self._refParVals[parIt][1] ) self._nParVals[parIt][ 2 if pull < 0. else 1 ] += 1 if abs(pull) > self._maxPulls[parIt] : self._maxPulls[parIt] = abs(pull) self._pullVals[parIt].append(pull) self._pullSums[parIt][ 1 if pull < 0. else 0 ] += pull self._pullSums[parIt][ 3 if pull < 0. else 2 ] += pull**2 self._pullSums[parIt][ 5 if pull < 0. else 4 ] += pull**4 self._parVals[parIt].append(val) self._parSums[parIt][0] += val self._parSums[parIt][1] += val**2 self._errSums[parIt] += vals[1] if name in self._parHists and self._parHists[name] : self._parHists[name].Delete() if histsFile : parMinMax = anaParSetts[name]['parMinMax'] if name in anaParSetts and 'parMinMax' in anaParSetts[name] else None parHistBins = anaParSetts[name]['parHistBins'] if name in anaParSetts and 'parHistBins' in anaParSetts[name]\ else int( float(self._nParVals[parIt][0]) / 250. ) if self._nParVals[parIt][0] > 1000 else 10 plotShift = anaParSetts[name]['plotShift'] if name in anaParSetts and 'plotShift' in anaParSetts[name] else 0. parHistMin = parMinMax[0] if parMinMax else min( self._parVals[parIt] ) + plotShift parHistMax = parMinMax[1] if parMinMax else max( self._parVals[parIt] ) + plotShift parHistRange = parHistMax - parHistMin if not parMinMax and parHistRange > 0. : parHistMin = parHistMin - 0.01 * parHistRange parHistMax = parHistMax + 0.01 * parHistRange parHist = TH1D( name + '_par', name, parHistBins, parHistMin, parHistMax ) for val in self._parVals[parIt] : parHist.Fill( val + plotShift ) doParFit = name in anaParSetts and 'doParFit' in anaParSetts[name] and anaParSetts[name]['doParFit'] drawHist( name, parHist, histsFile + '_pars.pdf', doParFit , self._refParVals[parIt][0] + plotShift if self._refParVals[parIt] != None else None ) pullMinMax = anaParSetts[name]['pullMinMax'] if name in anaParSetts and 'pullMinMax' in anaParSetts[name] else None pullHistBins = anaParSetts[name]['pullHistBins'] if name in anaParSetts and 'pullHistBins' in anaParSetts[name]\ else int( float(self._nParVals[parIt][0]) / 250. ) if self._nParVals[parIt][0] > 1000 else 10 pullHistMin = pullMinMax[0] if pullMinMax else min( self._pullVals[parIt] ) pullHistMax = pullMinMax[1] if pullMinMax else max( self._pullVals[parIt] ) pullHistRange = pullHistMax - pullHistMin if not pullMinMax and pullHistRange > 0. : pullHistMin = pullHistMin - 0.01 * pullHistRange pullHistMax = pullHistMax + 0.01 * pullHistRange pullHist = TH1D( name + '_pull', name, pullHistBins, pullHistMin, pullHistMax ) for val in self._pullVals[parIt] : pullHist.Fill(val) doPullFit = name in anaParSetts and 'doPullFit' in anaParSetts[name] and anaParSetts[name]['doPullFit'] drawHist(name, pullHist, histsFile + '_pulls.pdf', doPullFit, 0. ) if histsFile : dCanv.Print( histsFile + '_pars.pdf]' ) dCanv.Print( histsFile + '_pulls.pdf]' ) for valCounts in self._nParVals : assert valCounts[0] == self._nFiles and ( valCounts[1] + valCounts[2] == self._nFiles or self._refParVals[0] == None ) from math import sqrt, log10, ceil nameLen = min( 30, max( len(name) for name in self._parNames ) ) sepStr = ' ' + '-' * ( nameLen + ( 167 if self._refParVals[0] != None else 32 ) ) print 'P2VV - INFO: fitResultsAnalysis.processResults(): parameter statistics for %d files:' % self._nFiles print sepStr print ( ' {0:<%ds} {1:<22s}' % nameLen ).format( 'name', 'measured' ), if self._refParVals[0] != None : print ' {0:<18s} {1:<20s} {2:<19s} {3:<19s} {4:<19s} {5:<6s} {6:<6s} {7:<8s}'\ .format( 'reference', 'pull mean', 'mean error', 'pull RMS', 'width error', '+RMS', '-RMS', 'max pull' ) else : print print sepStr for name, nParVals, maxPull, parSums, errSums, pullSums, refVal\ in zip( self._parNames, self._nParVals, self._maxPulls, self._parSums, self._errSums, self._pullSums, self._refParVals ) : meanVal = parSums[0] / float( nParVals[0] ) meanSqVal = parSums[1] / float( nParVals[0] ) stdDev = sqrt( meanSqVal - meanVal**2 ) precDev = max( 0, 3 - int( ceil( log10(stdDev) ) ) ) prec = max( 0, 3 - int( ceil( log10( refVal[1] ) ) ) ) if refVal != None else precDev print ( ' {0:<%ds} {1:<+8.%df} {2:<11.%df}' % ( nameLen, prec, precDev ) ).format( name, meanVal, stdDev ), if refVal != None : refErr = errSums / float( nParVals[0] ) if toyMode else refVal[1] pullMean = ( pullSums[0] + pullSums[1] ) / float( nParVals[0] ) pullVar = ( pullSums[2] + pullSums[3] ) / float( nParVals[0] ) pullVarVar = ( pullSums[4] + pullSums[5] ) / float( nParVals[0] ) meanErr = sqrt( ( pullVar - pullMean**2 ) / float( nParVals[0] ) ) width = sqrt(pullVar) widthErr = 0.5 / sqrt(pullVar) * sqrt( ( pullVarVar - pullVar**2 ) / float( nParVals[0] ) ) widthPlus = sqrt( pullSums[2] / float( nParVals[1] ) ) if nParVals[1] > 0 else 0. widthMin = sqrt( pullSums[3] / float( nParVals[2] ) ) if nParVals[2] > 0 else 0. print ( ' {0:<+8.%df} {1:<7.%df} {2:<+6.3f} / {3:<+11.%df} {4:<5.3f} / {5:<11.%df} {6:<5.3f} / {7:<11.%df} {8:<5.3f} / {9:<11.%df} {10:<6.3f} {11:<6.3f} {12:<8.3f}'\ % ( prec, prec, precDev, precDev, precDev, precDev ) )\ .format( refVal[0], refErr, pullMean, pullMean * refErr, meanErr, meanErr * refErr , width, width * refErr, widthErr, widthErr * refErr, widthPlus, widthMin, maxPull ) else : print print sepStr
print 13 * ' ' + 'split parameters:' for it, pars in enumerate(splitPars) : print 13 * ' ' + '%2d: pars: [ %s ]' % ( it, ', '.join( par.GetName() for par in pars[0] ) ) print 13 * ' ' + ' cats: [ %s ]' % ', '.join( cat.GetName() for cat in pars[1] ) from P2VV.RooFitWrappers import SimultaneousPdf simMassPdf = SimultaneousPdf( massPdf.GetName() + '_simul' , MasterPdf = massPdf , SplitCategories = [ pars[1] for pars in splitPars ] , SplitParameters = [ pars[0] for pars in splitPars ] ) massPdfObs = simMassPdf.getObservables(dataSet) massPdfPars = simMassPdf.getParameters(dataSet) # create PDF configuration object from P2VV.Parameterizations.FullPDFs import PdfConfiguration as PdfConfig pdfConfig = PdfConfig() # read parameters from file print 120 * '=' pdfConfig.readParametersFromFile( filePath = args.parFilePathIn ) pdfConfig.setParametersInPdf(simMassPdf) print 'P2VV - INFO: splitDataSetSigBkg: observables in mass PDF:' massPdfObs.Print('v') print 'P2VV - INFO: splitDataSetSigBkg: parameters in mass PDF:' massPdfPars.Print('v') ########################################################################################################################################### ## split data into signal and background ## ###########################################