示例#1
0
def _calculate_confidence_band(root_obj: TF1,
                               ci: float,
                               steps: int = 1000) -> dict:
    """
    Calculate confidence bands from a fit of a
    TF1 to a histogram.

    Parameters
    ----------
    root_obj : TH1
        ROOT TF1 pdf used for the fit
    ci : float
        Confidence interval
    steps : int
        Number of points in band

    Returns
    -------
    dict
        Dictionary with x, y, xerr, yerr points
    """
    assert(isinstance(root_obj, TF1))
    band = TGraphErrors(steps)
    lowb, highb = root_obj.GetXaxis().GetXmin(), root_obj.GetXaxis().GetXmax()
    for i in range(steps):
        x = (lowb + (
             (highb - lowb) / steps) * i)
        y = 0
        band.SetPoint(i, x, y)
        (TVirtualFitter.GetFitter()).GetConfidenceIntervals(band, ci)

    return _convert_tgrapherrors(band)
示例#2
0
def TF2_Fit_NKG(h2,N0,r0,s0,x0,y0,x_min,x_max,y_min,y_max):
    
    fitf2= TF2('fitf2',func_nkg_show,x_min,x_max,y_min,y_max,5)
    fitf2.SetParameter(0,N0) #        Setting starting values (Ne)
    fitf2.SetParameter(1,r0) #            ,,    ,,     (r_M)
    fitf2.SetParameter(2,s0) #            ,,    ,,     (s)
    fitf2.SetParameter(3,x0) #            ,,    ,,     (x_core)
    fitf2.SetParameter(4,y0) #            ,,    ,,     (y_core)
    
    #//fitf2->SetParLimits(1,10,300) ;        //Setting r_M limits
    fitf2.SetParLimits(1,r0,r0)#        //Setting r_M limits
    #//fitf2->SetParLimits(2,1,2.5) ;        //Setting Age limits (keeping fixed)
    fitf2.SetParLimits(2,s0,s0)#        //Setting Age limits (Keeping fixed)
    h2.Fit(fitf2,"Q0")
    #h2SetXTitle("X (m)") ;
    #h2->GetXaxis()->CenterTitle() ;
    #h2->SetYTitle("Y (m)") ;
    #h2->GetYaxis()->CenterTitle() ;
    #//h2->Draw("HIST") ;
    #//fitf2->DrawCopy("surf1 same");
    #gStyle->SetOptFit(1111) ;
    Ne_fit=fitf2.GetParameter(0)
    rM_fit=fitf2.GetParameter(1)
    s_fit=fitf2.GetParameter(2)
    x_core_fit=fitf2.GetParameter(3)
    y_core_fit=fitf2.GetParameter(4)
    x_core_fit_err=fitf2.GetParError(3)
    y_core_fit_err=fitf2.GetParError(4)
    
    
    
    fitter=TVirtualFitter.GetFitter()
    
  
    
    corMatrix=np.zeros([5,5])
    
    sig_i=0
    sig_j=0
    cov_ij=0
    
    for i in np.arange(4):

        sig_i=np.sqrt(fitter.GetCovarianceMatrixElement(i,i))
        for j in np.arange(4):
            sig_j=np.sqrt(fitter.GetCovarianceMatrixElement(j,j))
            cov_ij=fitter.GetCovarianceMatrixElement(i,j)
            corMatrix[i][j]=cov_ij/(sig_i*sig_j)
            if(sig_i==0 or sig_j==0):
                corMatrix[i][j]=0
       

    corCoef_xy=corMatrix[1][2] #Correlation coeff. between the x & y core position
    return Ne_fit,rM_fit,s_fit,x_core_fit,y_core_fit,x_core_fit_err,y_core_fit_err, corCoef_xy
示例#3
0
    def fit(self, graph, xstart, xend, options=''):
        """computes fit and stores calculated parameters with errors in parameter dict. 
        Also extracts covariance matrix and calculates correlation matrix

        Arguments:
        graph   -- an instance of ROOTs TGraph with data to fit
        xstart  -- start value for x interval  
        xend    -- end value for x interval
        options -- fitting options (see ROOT documentation)
        """
        graph.Fit(self._function, 'Q' + options, '', xstart,
                  xend)  # Q = quit mode, no print out on console
        # get fitted params
        self.virtualFitter = TVirtualFitter.GetFitter()
        if self.virtualFitter:
            for i in self.params:
                self.params[i]['value'] = self.virtualFitter.GetParameter(i)
                self.params[i]['error'] = self.virtualFitter.GetParError(i)
            # get cov. and corr. matrix
            freeparams = dict(
            )  # map cov. matrix index to params index, only not fixed params are in cov. matrix
            freecount = 0
            fixedcount = 0
            for i, param in self.params.items():
                if param['fixed']:
                    fixedcount += 1
                else:
                    freecount += 1
                    freeparams[i - fixedcount] = i
            self._covMatrix = [[
                self.virtualFitter.GetCovarianceMatrixElement(col, row)
                for row in range(freecount)
            ] for col in range(freecount)]
            self._corrMatrix = [[
                self._covMatrix[col][row] /
                (self.params[freeparams[col]]['error'] *
                 self.params[freeparams[row]]['error'])
                for row in range(freecount)
            ] for col in range(freecount)]
    logger.info("Do fitting")
    logger.info(
        "Argument \"setinitialgaussianmean\" set to %u, however not used at the moment",
        setinitialgaussianmean)

    if "Lb" not in case and "Lc" not in case and "D0" not in case and "Ds" not in case:
        logger.warning(
            "Can only do the fit for Lc, D0 or Ds, however found case %s",
            case)
        return -1, -1

    histo.GetXaxis().SetTitle("Invariant Mass L_{c}^{+}(GeV/c^{2})")
    histo.Rebin(rebin)
    histo.SetStats(0)
    TVirtualFitter.SetDefaultFitter("Minuit")

    # Prepare what can be prepared
    npar_sig = 3
    npar_bkg = 3 if bkgfunc == "Pol2" else 2
    par_names_sig = ["IntSig", "Mean", "Sigma"]
    par_names_bkg = ["IntBkg", "BkgCoeff1", "BkgCoeff2"] if bkgfunc == "Pol2" \
                                                         else ["IntBkg", "BkgCoeff1"]

    fitOption = "L,E" if dolikelihood else ""

    logger.info("Initial parameters for signal fit are")
    print(f"mean = {masspeak}\nsigma = {sigma_sig}")

    logger.debug("fit background (just side bands)")
    nSigma4SideBands = 4.
示例#5
0
    def fit(self, fitPairs=None, quiet=True):
        """
        Fit efficiency curve to values given by 'fitPairs' which should be a list
        of energy<->efficiency pairs. (See hdtv.util.Pairs())

        'energies' and 'efficiencies' may be a list of ufloats
        """
        # TODO: Unify this with the energy calibration fitter
        if fitPairs is not None:
            #self.fitInput = fitPairs
            self._fitInput = fitPairs

        E = array.array("d")
        delta_E = array.array("d")
        eff = array.array("d")
        delta_eff = array.array("d")
        EN = array.array("d")
        effN = array.array("d")

        #        map(energies.append(self.fitInput[0]), self.fitInput)
        #        map(efficiencies.append(self.fitInput[1]), self.fitInput)
        hasXerrors = False
        # Convert energies to array needed by ROOT
        try:
            list(map(lambda x: E.append(x[0].nominal_value), self._fitInput))
            list(map(lambda x: delta_E.append(x[0].std_dev), self._fitInput))
            list(map(lambda x: EN.append(0.0), self._fitInput))
            hasXerrors = True
        except AttributeError:  # energies does not seem to be ufloat list
            list(map(lambda x: E.append(x[0]), self._fitInput))
            list(map(lambda x: delta_E.append(0.0), self._fitInput))

        # Convert efficiencies to array needed by ROOT
        try:
            list(map(lambda x: eff.append(x[1].nominal_value), self._fitInput))
            list(map(lambda x: delta_eff.append(x[1].std_dev), self._fitInput))
            list(map(lambda x: effN.append(0.0), self._fitInput))
        except AttributeError:  # energies does not seem to be ufloat list
            list(map(lambda x: eff.append(x[1]), self._fitInput))
            list(map(lambda x: delta_eff.append(0.0), self._fitInput))

        # if fit has errors we first fit without errors to get good initial values
        # if hasXerrors == True:
        # print "Fit parameter without errors included:"
        #self.TGraphWithoutErrors = TGraphErrors(len(E), E, eff, EN, effN)
        #fitWithoutErrors = self.TGraphWithoutErrors.Fit(self.id, "SF")

        hdtv.ui.msg("Fit parameter with errors included:")

        # Preliminary normalization
        #        if self._doNorm:
        #            self.norm = 1 / max(efficiencies)
        #            for i in range(len(eff)):
        #                eff[i] *= self.norm
        #                delta_eff[i] *= self.norm

        #self.TF1.SetRange(0, max(E) * 1.1)
        # self.TF1.SetParameter(0, 1) # Unset normalization for fitting

        self.TGraph = TGraphErrors(len(E), E, eff, delta_E, delta_eff)

        # Do the fit
        fitopts = "0"  # Do not plot

        if hasXerrors:
            # We must use the iterative fitter (minuit) to take x errors
            # into account.
            fitopts += "F"
            hdtv.ui.info(
                "switching to non-linear fitter (minuit) for x error weighting"
            )
        if quiet:
            fitopts += "Q"

        fitopts += "S"  # Additional fitinfo returned needed for ROOT5.26 workaround below
        fitreturn = self.TGraph.Fit(self.id, fitopts)

        try:
            # Workaround for checking the fitstatus in ROOT 5.26 (TFitResultPtr
            # does not cast properly to int)
            fitstatus = fitreturn.Get().Status()
        except AttributeError:  # This is for ROOT <= 5.24, where fit returns an int
            fitstatus = int(fitreturn)

        if fitstatus != 0:
            # raise RuntimeError, "Fit failed"
            hdtv.ui.msg("Fit failed")

#         # Final normalization
#         if self._doNorm:
#             self.normalize()

# Get parameter
        for i in range(self._numPars):
            self.parameter[i] = self.TF1.GetParameter(i)

        # Get covariance matrix
        tvf = TVirtualFitter.GetFitter()
        ##        cov = tvf.GetCovarianceMatrix()
        for i in range(0, self._numPars):
            for j in range(0, self._numPars):
                self.fCov[i][j] = tvf.GetCovarianceMatrixElement(i, j)
##                 self.fCov[i][j] = cov[i * self._numPars + j]

        return self.parameter
示例#6
0
def doFit(FunctionType,hMassNEW,g,fFitXmin,fFitXmax,fNbins,xbins,fLabel):
  print ""
  print ""
  print ""
  print ""
  
  if( FunctionType==-2 ):
    nPar=2
    BKGfit = TF1("BKGfit%i"%FunctionType," [0] / ( TMath::Power(x/13000,[1]) )",fFitXmin,fFitXmax)
 
     # signal region: 
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,1.98680e-09)
      BKGfit.SetParameter(1,8.66744e+00)
    elif fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.13062e-07)
      BKGfit.SetParameter(1,7.85359e+00)  
    elif fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,7.09589e-09)
      BKGfit.SetParameter(1,8.54541e+00)
    elif fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.50873e-07)
      BKGfit.SetParameter(1,8.03595e+00)
    elif fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,1.82729e-09)
      BKGfit.SetParameter(1,8.92196e+00)
    elif fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,6.82046e-08)
      BKGfit.SetParameter(1,8.08889e+00)
      # sideband region: 
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,1.98680e-09)
      BKGfit.SetParameter(1,8.66744e+00)
    elif fLabel.find("W") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,1.13062e-07)
      BKGfit.SetParameter(1,7.85359e+00)  
    elif fLabel.find("Z") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,7.09589e-09)
      BKGfit.SetParameter(1,8.54541e+00)
    elif fLabel.find("Z") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,1.50873e-07)
      BKGfit.SetParameter(1,8.03595e+00)
      
  if( FunctionType==0 ):
    print "Fitting three parameter default function!"
    nPar=3
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]) )",fFitXmin,fFitXmax)
   
    
    # signal region:
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,7.68629e-09 )       
      BKGfit.SetParameter(1,8.35270e+00 )
      BKGfit.SetParameter(2, 0.)
      #BKGfit.SetParameter(0, 9.01237e-12)
      #BKGfit.SetParameter(1,-1.58982e+01)
      #BKGfit.SetParameter(2, 1.02766e+01)
      BKGfit.SetParLimits(2,7,20)
    elif fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,3.75544e-07)
      BKGfit.SetParameter(1,3.37623e+00)
      BKGfit.SetParameter(2,7.49005e+00)
    elif fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,7.09589e-09)
      BKGfit.SetParameter(1,8.54541e+00)
      BKGfit.SetParameter(2, 8.87103e+00)
    elif fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.50874e-07)
      BKGfit.SetParameter(1,8.03595e+00)
      BKGfit.SetParameter(2,7.37063e+00)
    elif fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,1.06871e-08)
      BKGfit.SetParameter(1,1.03718e+00)
      BKGfit.SetParameter(2,8.38073e+00)
      BKGfit.SetParLimits(2, 8.2e+00 ,8.4e+00 )
      BKGfit.SetParLimits(1, 1.03718e+00 , 1.03718e+00)
    elif fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,6.82046e-08)
      BKGfit.SetParameter(2,8.08889e+00)
      BKGfit.SetParameter(1,0.00000000 )
    
    # sideband region:
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0, 9.01237e-12)
      BKGfit.SetParameter(1,-1.58982e+01)
      BKGfit.SetParameter(2, 1.02766e+01)
    elif fLabel.find("WW") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,3.75544e-07)
      BKGfit.SetParameter(1,3.37623e+00)
      BKGfit.SetParameter(2,7.49005e+00) 
    elif fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,1.82729e-09)
      BKGfit.SetParameter(1,8.92196e+00)
      BKGfit.SetParameter(2,9.48734e+00)
    elif fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,6.82046e-08)
      BKGfit.SetParameter(2,8.08889e+00)
      BKGfit.SetParameter(1,0.00000000 )
    
          
  elif( FunctionType==1 ):
    print "Fitting four parameter default function!"
    nPar=4
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",fFitXmin,fFitXmax)
    
    
    # signal region: 
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,   2.45156e-02) #6.48238e-13 )
      BKGfit.SetParameter(1,  2.60131e+01 ) #1.11517e+00 )
      BKGfit.SetParameter(2,  1.17317e+00 ) #1.02840e+01 )
      BKGfit.SetParameter(3, -8.19261e-01 ) #8.45161e+03 )
      #BKGfit.FixParameter(3,0 )
      #BKGfit.SetParLimits(1,  2.60131e+01, 2.60131e+01)
      #BKGfit.SetParLimits(2,0,10)
      #BKGfit.SetParLimits(3,-8.19261e-01,-8.19261e-01)
      #BKGfit.SetParLimits(0,3.01257e-12,9.01257e-12)
    elif fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,3.72696e-07)
      BKGfit.SetParameter(1,3.35574e+00)
      BKGfit.SetParameter(2,7.49238e+00)
      BKGfit.SetParameter(3,-4.10905e-07)
    elif fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,7.09589e-09)
      BKGfit.SetParameter(1,8.54541e+00)
      BKGfit.SetParameter(2, 8.87103e+00)
      BKGfit.SetParameter(3,-8.14899e-02)
    elif fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.50873e-07)
      BKGfit.SetParameter(1,8.03595e+00)
      BKGfit.SetParameter(2,7.96865e+00)  
      BKGfit.SetParameter(3,9.66574e-02)
    elif fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0, 5.90181e-02)# 1.82729e-09)
      BKGfit.SetParameter(1, 2.12431e+01)# 8.92196e+00)
      BKGfit.SetParameter(2,-1.00178e+00)# 4.76973e+00)
      BKGfit.SetParameter(3,-1.54769e+00)#-7.21037e-01)
      #BKGfit.SetParLimits(3, -1.54769e+00,-1.54769e+00)
      #BKGfit.SetParLimits(2,-1.00178e+00,-1.00178e+00 )
      #BKGfit.SetParLimits(1,2.12431e+01,2.12431e+01 )
      #BKGfit.SetParLimits(0,  5.90181e-02, 5.90181e-02)
    elif fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,6.82046e-08)
      BKGfit.SetParameter(2,8.08889e+00)
      BKGfit.SetParameter(1,0.00000000 )
      BKGfit.SetParameter(3,0.00000000 )
      
    # sideband region:
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0, 9.01257e-12)
      BKGfit.SetParameter(1,-1.09759e+01)
      BKGfit.SetParameter(2, 8.55014e+00)
      BKGfit.SetParameter(3,-1 )# -2.54954e-01)
      BKGfit.SetParLimits(3,-10,0)
    elif fLabel.find("WW") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,3.72696e-07)
      BKGfit.SetParameter(1,3.35574e+00)
      BKGfit.SetParameter(2,7.49238e+00)
      BKGfit.SetParameter(3,-4.10905e-07)
    elif fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1 :
      BKGfit.SetParameter(0,1.82729e-09)
      BKGfit.SetParameter(1,8.92196e+00)
      BKGfit.SetParameter(2, 4.76973e+00)
      BKGfit.SetParameter(3,-7.21037e-01)
    elif fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,6.82046e-08)
      BKGfit.SetParameter(2,8.08889e+00)
      BKGfit.SetParameter(1,0.00000000 )
      BKGfit.SetParameter(3,0.00000000 )
      
  elif( FunctionType==3 ):
    print "Fitting four parameter alternate function!"
    nPar=4
    BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]) )",fFitXmin,fFitXmax)
    
      
    
    # signal region:
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,2.21926e-13) 
      BKGfit.SetParameter(1,2.25629e+00) 
      BKGfit.SetParameter(2,1.15338e+01) 
      BKGfit.SetParameter(3,2.11319e+02) 
      # BKGfit.SetParLimits(0,0.21926e-13,4.21926e-13) #WWHP
    elif fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.44775e-05 ) 
      BKGfit.SetParameter(1,2.05225e+01 ) 
      BKGfit.SetParameter(2,6.50590e+00 ) 
      BKGfit.SetParameter(3,1.86664e+00 ) 
    elif fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,1.19345e-05) 
      BKGfit.SetParameter(1,3.73975e+01) 
      BKGfit.SetParameter(2,6.58579e+00) 
      BKGfit.SetParameter(3,2.48983e+00) 
    elif fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.36722e-06)
      BKGfit.SetParameter(1,6.63254e+00)
      BKGfit.SetParameter(2,7.37687e+00)
      BKGfit.SetParameter(3,3.28215e-01)
    elif fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0, 2.45629e-09 )
      BKGfit.SetParameter(1,-4.58690e+00 )
      BKGfit.SetParameter(2, 8.80612e+00 )
      BKGfit.SetParameter(3, 2.05830e+00 )
      #BKGfit.SetParLimits(2, 8.80612e+00, 8.80612e+00)
      #BKGfit.SetParLimits(3, 2.05830e+00, 2.05830e+00)
    elif fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0, 1.74162e-06)
      BKGfit.SetParameter(1,-8.47400e-01)
      BKGfit.SetParameter(2, 7.05095e+00)
      BKGfit.SetParameter(3, 1.79446e+02)
      
    #sideband region:
    if fLabel.find("W") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,2.21926e-13) 
      BKGfit.SetParameter(1,2.25629e+00) 
      BKGfit.SetParameter(2,1.15338e+01) 
      BKGfit.SetParameter(3,2.11319e+02) 
    elif fLabel.find("W") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0,1.44775e-05 ) 
      BKGfit.SetParameter(1,2.05225e+01 ) 
      BKGfit.SetParameter(2,6.50590e+00 ) 
      BKGfit.SetParameter(3,1.86664e+00 ) 
    elif fLabel.find("Z") != -1 and fLabel.find("HP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0, 1.82729e-09)
      BKGfit.SetParameter(1, 8.92196e+00)
      BKGfit.SetParameter(2, 4.76973e+00)
      BKGfit.SetParameter(3,-7.21037e-01)
    elif fLabel.find("Z") != -1 and fLabel.find("LP") != -1 and fLabel.find("SB")!=-1:
      BKGfit.SetParameter(0, 1.74162e-06)
      BKGfit.SetParameter(1,-8.47400e-01)
      BKGfit.SetParameter(2, 7.05095e+00)
      BKGfit.SetParameter(3, 1.79446e+02)
          
  stopProgram=1;
  for loop in range (0,10):
    r = hMassNEW.Fit("BKGfit%i"%FunctionType,"BISR","",fFitXmin,fFitXmax)
    fitStatus = int(r)
    print "fit status : %d" % fitStatus
    if(fitStatus==0):
      stopProgram=0
      # r.Print("V")
      break
  c1 = TCanvas("c3","fit",600,400)
  hMassNEW.Draw();
  suffix ="WHP";
  if fLabel.find('W')!=-1:
      suffix ="W"
  if fLabel.find('Z')!=-1:
      suffix ="Z"
  if fLabel.find('Z')!=-1:
      suffix ="Z"
  if fLabel.find('WW')!=-1:
      suffix ="WW"
  if fLabel.find('WZ')!=-1:
      suffix ="WZ"
  if fLabel.find('ZZ')!=-1:
      suffix ="WZ"
      
  if fLabel.find('HP')!=-1:
      suffix +="HP"
  if fLabel.find('LP')!=-1:
      suffix +="LP"
  c1.SaveAs("ftest_2016/test_"+suffix+".pdf");
  #if(stopProgram==1):
   # print "######################"
   # print"######################"
   # print "ERROR : Fit %i failed!!!!" %FunctionType
   # print "######################"
   # print "######################"
   # sys.exit()
  
  
  #Create a histogram to hold the confidence intervals
  # histoCI=TH1D("histoCI","", fNbins,xbins)
  # # histoCI=TH1D()
  # histoCI.SetTitle("Fitted histogram with .95 conf. band")
  # (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
  # #Now the "hint" histogram has the fitted function values as the
  # #bin contents and the confidence intervals as bin errors
  # histoCI.SetStats(kFALSE)
  # histoCI.SetFillColor(kRed+2)
  # histoCI.SetLineColor(kRed+2)
  # histoCI.SetFillStyle(3354)


  histoCI = TGraphErrors(fNbins)
  histoCI.SetTitle("Fitted line with .95 conf. band")
  for i in range (0, fNbins):
    histoCI.SetPoint(i, g.GetX()[i], 0)
  #Compute the confidence intervals at the x points of the created graph
  (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
  # //Now the "grint" graph contains function values as its y-coordinates
 #  //and confidence intervals as the errors on these coordinates
 #  //Draw the graph, the function and the confidence intervals
  histoCI.SetLineColor(0)
  histoCI.SetLineWidth(-802)
  histoCI.SetFillStyle(3002)
  histoCI.SetFillColor(2)  
  
  NumberOfVarBins = 0
  NumberOfObservations_VarBin = 0
  chi2_VarBin = 0.
  chi2_VarBin_notNorm = 0.
  hist_fit_residual_vsMass =  TH1D("hist_fit_residual_vsMass","hist_fit_residual_vsMass",fNbins,xbins)
  
  for bin in range (1,hMassNEW.GetNbinsX()):
    if( hMassNEW.GetXaxis().GetBinCenter(bin)>=fFitXmin and hMassNEW.GetXaxis().GetBinCenter(bin)<=fFitXmax ):
       NumberOfVarBins += 1
       data = hMassNEW.GetBinContent(bin)
       # data = g.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin) , hMassNEW.GetXaxis().GetBinUpEdge(bin) )
       err_data_low = g.GetErrorYlow(bin-1) 
       err_data_high= g.GetErrorYhigh(bin-1)
       fit = BKGfit.Integral(hMassNEW.GetXaxis().GetBinCenter(bin) , hMassNEW.GetXaxis().GetBinCenter(bin) )
       fit = fit / ( hMassNEW.GetBinWidth(bin) )
       # fit = BKGfit.Eval(hMassNEW.GetBinCenter(bin)) #yields same results
       if(fit > data):
         err_tot = err_data_high
       else:
         err_tot = err_data_low
       if err_tot!=0:  
            fit_residual = (data - fit) / err_tot
       else:
           print "Warning: err_tot = 0 !"
           fit_residual = 0
       err_fit_residual = 1
       
       if (hMassNEW.GetBinContent(bin)>0):
         NumberOfObservations_VarBin+=1
         print "(data-fit)**2 = "+str(pow( (data - fit) , 2 ))+" err_tot : "+str(err_tot)
         print chi2_VarBin
         chi2_VarBin += pow( (data - fit) , 2 ) / pow( err_tot , 2 )
         chi2_VarBin_notNorm += pow( (data - fit) , 2 )

       hist_fit_residual_vsMass.SetBinContent(bin,fit_residual)
       hist_fit_residual_vsMass.SetBinError(bin,err_fit_residual)
  
  ndf_VarBin = NumberOfObservations_VarBin - nPar -1 #ndof    
  return [chi2_VarBin_notNorm,ndf_VarBin,chi2_VarBin,BKGfit,hist_fit_residual_vsMass,nPar,histoCI]   
        gRho00VsPromptFrac[iPt].SetPoint(iPoint, promptFrac, rho00)
        gRho00VsPromptFrac[iPt].SetPointError(iPoint, uncPromptFrac,
                                              uncPromptFrac, uncRho00,
                                              uncRho00)

        gRho00VsNonPromptFrac[iPt].SetPoint(iPoint, nonpromptFrac, rho00)
        gRho00VsNonPromptFrac[iPt].SetPointError(iPoint, uncNonpromptFrac,
                                                 uncNonpromptFrac, uncRho00,
                                                 uncRho00)

    cRho00VsPromptFrac.cd(iPt + 1).DrawFrame(
        0., 0., 1., 0.6, ';#it{f}_{prompt};#it{#rho}_{00}')
    lineNoPol.Draw('same')
    gRho00VsPromptFrac[iPt].Fit(f'fRho00VsPromptFrac_pt{ptMin}-{ptMax}', '0')
    TVirtualFitter.GetFitter().GetConfidenceIntervals(ciRho00VsPromptFrac[iPt],
                                                      0.68)
    ciRho00VsPromptFrac[iPt].DrawCopy('same')
    fRho00VsPromptFrac[iPt].Draw('same')
    gRho00VsPromptFrac[iPt].Draw('PZ')
    lat.DrawLatex(0.5, 0.4, f'{ptMin} < #it{{p}}_{{T}} < {ptMax} GeV/#it{{c}}')
    lat.DrawLatex(
        0.5, 0.3,
        f'#chi^{{2}}/ndf = {fRho00VsPromptFrac[iPt].GetChisquare():0.2f}/{fRho00VsPromptFrac[iPt].GetNDF()}'
    )

    cRho00VsNonPromptFrac.cd(iPt + 1).DrawFrame(
        0., 0., 1., 0.6, ';#it{f}_{non-prompt};#it{#rho}_{00}')
    lineNoPol.Draw('same')
    gRho00VsNonPromptFrac[iPt].Fit(f'fRho00VsNonPromptFrac_pt{ptMin}-{ptMax}',
                                   '0')
    TVirtualFitter.GetFitter().GetConfidenceIntervals(
示例#8
0
    def fit(self):
        """
        Some comments:
            -> The object 'back_fit' is fitted using only the side-bands
            -> The object 'tot_fit' is fitted to the entire extracted inv-mass distribution
            -> The object 'back_refit' is not used for fitting but only stores the background fit
               parameters from fitted 'tot_fit'
            -> The object 'sig_fit' is not used for fitting but only stores the signal fit
               parameters from fitted 'tot_fit'
        """

        self.logger.info("Do fitting")

        TVirtualFitter.SetDefaultFitter("Minuit")

        # Prepare what can be prepared
        npar_sig = self.sig_fit_func.GetNpar()
        # Need to do it that way since background functions have a few dummy parameters
        npar_bkg = self.tot_fit_func.GetNpar() - npar_sig

        self.logger.info("Initial parameters for signal fit are")
        print(f"mean = {self.mean}\nsigma = {self.sigma}")

        self.logger.debug("fit background (just side bands)")
        self.histo_to_fit.Fit(self.bkg_sideband_fit_func, ("R,%s,+,0" % (self.fit_options)))

        # Prepare a function to store the signal parameters which will finally be extracted
        # from the total fit. So this is just a helper for now
        minForSig = self.mean - self.nsigma_sideband * self.sigma
        maxForSig = self.mean + self.nsigma_sideband * self.sigma
        binForMinSig = self.histo_to_fit.FindBin(minForSig)
        binForMaxSig = self.histo_to_fit.FindBin(maxForSig)
        sum_tot = 0.
        sumback = 0.
        for ibin in range(binForMinSig, binForMaxSig + 1):
            sum_tot += self.histo_to_fit.GetBinContent(ibin)
            sumback += self.bkg_sideband_fit_func.Eval(self.histo_to_fit.GetBinCenter(ibin))
        integsig = Double((sum_tot - sumback) * (self.histo_to_fit.GetBinWidth(1)))

        self.sig_fit_func.SetParameter(0, integsig)
        self.sig_fit_func.SetParameter(1, self.mean)
        self.sig_fit_func.SetParameter(2, self.sigma)

        self.logger.info("fit all (signal + background)")
        self.tot_fit_func.SetLineColor(4)
        parmin = Double()
        parmax = Double()
        for ipar in range(0, npar_sig):
            self.tot_fit_func.SetParameter(ipar + npar_bkg, self.sig_fit_func.GetParameter(ipar))
            self.sig_fit_func.GetParLimits(ipar, parmin, parmax)
            self.tot_fit_func.SetParLimits(ipar + npar_bkg, parmin, parmax)
        for ipar in range(0, npar_bkg):
            self.tot_fit_func.SetParameter(ipar, self.bkg_sideband_fit_func.GetParameter(ipar))
            self.bkg_fit_func.SetParameter(ipar, self.bkg_sideband_fit_func.GetParameter(ipar))
        if self.fix_mean:
            # Mass peak would be fixed to what user sets
            self.tot_fit_func.FixParameter(npar_bkg + 1, self.mean)
        if self.fix_sigma is True:
            # Sigma would be fixed to what the fit to MC gives
            self.tot_fit_func.FixParameter(npar_bkg + 2,
                                           self.tot_fit_func.GetParameter(npar_bkg + 2))
        self.histo_to_fit.Fit(self.tot_fit_func, ("R,%s,+,0" % (self.fit_options)))

        for ipar in range(0, npar_bkg):
            self.bkg_tot_fit_func.SetParameter(ipar, self.tot_fit_func.GetParameter(ipar))
            self.bkg_tot_fit_func.SetParError(ipar, self.tot_fit_func.GetParameter(ipar))
        for ipar in range(npar_bkg, (npar_bkg + npar_sig)):
            self.sig_fit_func.SetParameter(ipar - npar_bkg, self.tot_fit_func.GetParameter(ipar))
            self.sig_fit_func.SetParError(ipar - npar_bkg, self.tot_fit_func.GetParError(ipar))

        self.derive_yields()

        # Check the signal fit
        error = self.update_check_signal_fit()
        if error != "":
            self.logger.error("Signal fit probably bad for following reasons:\n%s", error)

        self.fitted = True
        self.fit_success = (error == "")
        return self.fit_success
示例#9
0
def doFit(FunctionType, hMassNEW, g, fFitXmin, fFitXmax, fNbins, xbins,
          fLabel):
    print ""
    print ""
    print ""
    print ""

    if (FunctionType == -2):
        nPar = 2
        BKGfit = TF1("BKGfit%i" % FunctionType,
                     " [0] / ( TMath::Power(x/13000,[1]) )", fFitXmin,
                     fFitXmax)
        if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 8.10795e-06)
            BKGfit.SetParameter(1, 7.30736e+00)
        elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.13062e-07)
            BKGfit.SetParameter(1, 7.85359e+00)
        elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 7.09589e-09)
            BKGfit.SetParameter(1, 8.54541e+00)
        elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.50873e-07)
            BKGfit.SetParameter(1, 8.03595e+00)
        elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 7.09589e-09)
            BKGfit.SetParameter(1, 8.54541e+00)
        elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.50873e-07)
            BKGfit.SetParameter(1, 8.03595e+00)

    if (FunctionType == 0):
        print "Fitting three parameter default function!"
        nPar = 3
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]) )",
            fFitXmin, fFitXmax)
        if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 1.21219e-04)
            BKGfit.SetParameter(1, 6.16942e+00)
            BKGfit.SetParameter(2, 6.66576e+00)
        elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.47860e-03)
            BKGfit.SetParameter(1, 7.17033e+00)
            BKGfit.SetParameter(2, 6.00169e+00)
        elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 5.79282e-05)
            BKGfit.SetParameter(1, 4.53461e+00)
            BKGfit.SetParameter(2, 7.03951e+00)
        elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 9.62968e-04)
            BKGfit.SetParameter(1, 6.99994e+00)
            BKGfit.SetParameter(2, 6.09152e+00)
        elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 9.62968e-04)
            BKGfit.SetParameter(1, 6.99994e+00)
            BKGfit.SetParameter(2, 6.09152e+00)
        elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 9.62968e-04)
            BKGfit.SetParameter(1, 6.99994e+00)
            BKGfit.SetParameter(2, 6.09152e+00)

    elif (FunctionType == 1):
        print "Fitting four parameter default function!"
        nPar = 4
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",
            fFitXmin, fFitXmax)
        if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 1.14515e-04)
            BKGfit.SetParameter(1, 5.88071e+00)
            BKGfit.SetParameter(2, 6.65185e+00)
            BKGfit.SetParameter(3, -1.09416e-02)
        elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 2.17738e-03)
            BKGfit.SetParameter(1, 6.84330e+00)
            BKGfit.SetParameter(2, 6.43714e+00)
            BKGfit.SetParameter(3, 6.55647e-02)

        elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 5.79282e-05)
            BKGfit.SetParameter(1, 4.53461e+00)
            BKGfit.SetParameter(2, 7.03951e+00)
            BKGfit.SetParameter(3, 2.28311e-02)
        elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 8.67191e-03)
            BKGfit.SetParameter(1, 8.18650e+00)
            BKGfit.SetParameter(2, 5.25282e+00)
            BKGfit.SetParameter(3, -1.79962e-01)
        elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 3.47742e-05)
            BKGfit.SetParameter(1, 4.11131e+00)
            BKGfit.SetParameter(2, 6.97258e+00)
            BKGfit.SetParameter(3, 0.0000)
        elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 9.62968e-04)
            BKGfit.SetParameter(1, 6.99994e+00)
            BKGfit.SetParameter(2, 6.09152e+00)
            BKGfit.SetParameter(3, 0.0000)

    elif (FunctionType == 3):
        print "Fitting five parameter default function!"
        nPar = 5
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)+[4]*TMath::Power(log(x/13000),2)) )",
            fFitXmin, fFitXmax)
        if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 1.06734e-04)
            BKGfit.SetParameter(1, 7.17665e+00)
            BKGfit.SetParameter(2, 7.68365e+00)
            BKGfit.SetParameter(3, 7.09362e-01)
            BKGfit.SetParameter(4, 1.35015e-01)
        elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.20333e-04)
            BKGfit.SetParameter(1, 7.69352e+00)
            BKGfit.SetParameter(2, 7.79226e+00)
            BKGfit.SetParameter(3, 8.15310e-01)
            BKGfit.SetParameter(4, 1.55179e-01)
        elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 4.36219e-05)
            BKGfit.SetParameter(1, 3.19544e+00)
            BKGfit.SetParameter(2, 7.08912e+00)
            BKGfit.SetParameter(3, -7.33087e-01)
            BKGfit.SetParameter(4, -1.01751e-02)
            BKGfit.SetParLimits(4, -1.01751e-02, -1.01751e-02)
        elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 9.62968e-04)
            BKGfit.SetParameter(1, 6.99994e+00)
            BKGfit.SetParameter(2, 6.09152e+00)
        elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 3.47742e-05)
            BKGfit.SetParameter(1, 4.11131e+00)
            BKGfit.SetParameter(2, 6.97258e+00)
            BKGfit.SetParameter(3, 0.0000)
            BKGfit.SetParameter(4, 0.0000)
        elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 9.62968e-04)
            BKGfit.SetParameter(1, 6.99994e+00)
            BKGfit.SetParameter(2, 6.09152e+00)
            BKGfit.SetParameter(3, 0.0000)
            BKGfit.SetParameter(4, 0.0000)

    elif (FunctionType == 4):
        print "Fitting four parameter alternate function!"
        nPar = 4
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]) )",
            fFitXmin, fFitXmax)
        if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 8.10795e-06)
            BKGfit.SetParameter(1, 4.67052e+00)
            BKGfit.SetParameter(2, 7.30736e+00)
            BKGfit.SetParameter(3, 8.09331e+00)

        elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 5.14674e-03)
            BKGfit.SetParameter(1, 8.54997e+00)
            BKGfit.SetParameter(2, 5.98285e+00)
            BKGfit.SetParameter(3, 1.89857e-01)

        elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 3.64825e-05)
            BKGfit.SetParameter(1, 2.78348e+00)
            BKGfit.SetParameter(2, 7.17321e+00)
            BKGfit.SetParameter(3, -9.83737e-01)
            BKGfit.SetParLimits(0, -0.1, 1)
            BKGfit.SetParLimits(1, 0, 5)
            BKGfit.SetParLimits(2, 0, 10)
            BKGfit.SetParLimits(3, -1, 1)

        elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.06334e-03)
            BKGfit.SetParameter(1, 2.96443e+00)
            BKGfit.SetParameter(2, 6.36638e+00)
            BKGfit.SetParameter(3, -1.72927e+00)
        elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 3.47742e-05)
            BKGfit.SetParameter(1, 4.11131e+00)
            BKGfit.SetParameter(2, 6.97258e+00)
            BKGfit.SetParameter(3, 0.0000)
        elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 2.10360e+01)  # 3.61006e+01)
            BKGfit.SetParameter(1, -3.67723e+00)  #-3.14135e+00)
            BKGfit.SetParameter(2, 2.66588e+00)  # 2.31349e+00 )
            BKGfit.SetParameter(3, 1.31511e+02)  # 1.32204e+02)
            #BKGfit.SetParameter(3,0.0000)

    elif (FunctionType == 5):
        print "Fitting five parameter alternate function!"
        nPar = 5
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]+[4]*log(x/13000)) )",
            fFitXmin, fFitXmax)
        if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 4.99818e-07)
            BKGfit.SetParameter(1, -8.15352e+00)
            BKGfit.SetParameter(2, 9.17744e+00)
            BKGfit.SetParameter(3, 2.49453e+00)
            BKGfit.SetParameter(4, 2.98004e-01)

        elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 1.47860e-03)
            BKGfit.SetParameter(1, 7.17031e+00)
            BKGfit.SetParameter(2, 6.00169e+00)
            BKGfit.SetParameter(3, 5.03605e+00)
            BKGfit.SetParameter(4, -4.77303e-01)

        elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 4.36216e-05)
            BKGfit.SetParameter(1, 3.19548e+00)
            BKGfit.SetParameter(2, 7.08912e+00)
            BKGfit.SetParameter(3, -7.32868e-01)
            BKGfit.SetParameter(4, -1.01744e-02)
            BKGfit.SetParLimits(0, -0.1, 1)
            BKGfit.SetParLimits(1, 0, 5)
            BKGfit.SetParLimits(2, 0, 10)
            BKGfit.SetParLimits(3, -1, 1)
            BKGfit.SetParLimits(4, -1, 1)
        elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(0, 2.93921e-03)
            BKGfit.SetParameter(1, 6.35371e+00)
            BKGfit.SetParameter(2, 5.86362e+00)
            BKGfit.SetParameter(3, -1.98677e-01)
            BKGfit.SetParameter(4, -8.50249e-02)
        elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
            BKGfit.SetParameter(0, 3.47742e-05)  #3.47742e-05)
            BKGfit.SetParameter(1, 4.11131e+00)  #4.11131e+00)
            BKGfit.SetParameter(2, 6.97258e+00)  #6.97258e+00)
            BKGfit.SetParameter(3, 0.0)  #0.0000)
            BKGfit.SetParameter(4, 0.0)  #0.0000)
            #BKGfit.SetParLimits(4,-5,5)
            #BKGfit.SetParLimits(3,-1.794,10)
        elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
            BKGfit.SetParameter(
                0, 1.70901e-04
            )  # 8.96766e-04)                           1  p0           1.70900e-04
            BKGfit.SetParameter(
                1, 3.00147e+01
            )  # 6.70628e+00)                           2  p1           3.00147e+01
            BKGfit.SetParameter(
                2, 1.06146e+01
            )  # 6.11149e+00)                           3  p2           1.06146e+01
            BKGfit.SetParameter(
                3, 1.62392e+00
            )  #1.23384e+00 )# -7.82019e-02)            4  p3           1.62392e+00
            BKGfit.SetParameter(
                4, 1.29753e+00
            )  # 0.0000)                                5  p4           1.29753e+00

    elif (FunctionType == 3):
        print "Fitting four parameter alternate function!"
        nPar = 4
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]) )",
            fFitXmin, fFitXmax)

    stopProgram = 1
    for loop in range(0, 10):
        r = hMassNEW.Fit("BKGfit%i" % FunctionType, "ISRBL", "", fFitXmin,
                         fFitXmax)
        fitStatus = int(r)
        print "Inetgral of first bin:"
        print BKGfit.Integral(fFitXmin, 1118)
        print hMassNEW.GetBinContent(hMassNEW.GetXaxis().FindBin(1110))
        print "fit status : %d" % fitStatus
        if (fitStatus == 0):
            stopProgram = 0
            # r.Print("V")
            #break

    #if(stopProgram==1):
    #print "######################"
    #print"######################"
    #print "ERROR : Fit %i failed!!!!" %FunctionType
    #print "######################"
    #print "######################"
    #sys.exit()

    #Create a histogram to hold the confidence intervals
    # histoCI=TH1D("histoCI","", fNbins,xbins)
    # # histoCI=TH1D()
    # histoCI.SetTitle("Fitted histogram with .95 conf. band")
    # (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
    # #Now the "hint" histogram has the fitted function values as the
    # #bin contents and the confidence intervals as bin errors
    # histoCI.SetStats(kFALSE)
    # histoCI.SetFillColor(kRed+2)
    # histoCI.SetLineColor(kRed+2)
    # histoCI.SetFillStyle(3354)

    histoCI = TGraphErrors(fNbins)
    histoCI.SetTitle("Fitted line with .95 conf. band")
    for i in range(0, fNbins):
        histoCI.SetPoint(i, g.GetX()[i], 0)
    #Compute the confidence intervals at the x points of the created graph
    (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
    # //Now the "grint" graph contains function values as its y-coordinates
    #  //and confidence intervals as the errors on these coordinates
    #  //Draw the graph, the function and the confidence intervals
    histoCI.SetLineColor(0)
    histoCI.SetLineWidth(-802)
    histoCI.SetFillStyle(3002)
    histoCI.SetFillColor(2)

    NumberOfVarBins = 0
    NumberOfObservations_VarBin = 0
    chi2_VarBin = 0.
    chi2_VarBin_notNorm = 0.
    hist_fit_residual_vsMass = TH1D("hist_fit_residual_vsMass",
                                    "hist_fit_residual_vsMass", fNbins, xbins)

    for bin in range(1, hMassNEW.GetNbinsX()):
        if (hMassNEW.GetXaxis().GetBinLowEdge(bin) >= fFitXmin
                and hMassNEW.GetXaxis().GetBinUpEdge(bin) <= fFitXmax):
            #print "computing chi2"
            #print "bin: " + str(bin) + " content " + str(hMassNEW.GetBinContent(bin))
            NumberOfVarBins += 1
            data = hMassNEW.GetBinContent(bin)
            # data = g.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin) , hMassNEW.GetXaxis().GetBinUpEdge(bin) )
            err_data_low = g.GetErrorYlow(bin - 1)
            err_data_high = g.GetErrorYhigh(bin - 1)
            fit = BKGfit.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin),
                                  hMassNEW.GetXaxis().GetBinUpEdge(bin))
            fit = fit / (hMassNEW.GetBinWidth(bin))
            # fit = BKGfit.Eval(hMassNEW.GetBinCenter(bin)) #yields same results
            if (fit > data):
                err_tot = err_data_high
            else:
                err_tot = err_data_low
            if err_tot != 0:
                fit_residual = (data - fit) / err_tot
            else:
                fit_residual = 0
            err_fit_residual = 1

            if (hMassNEW.GetBinContent(bin) > 0):
                NumberOfObservations_VarBin += 1
                chi2_VarBin += pow((data - fit), 2) / pow(err_tot, 2)
                chi2_VarBin_notNorm += pow((data - fit), 2)

            hist_fit_residual_vsMass.SetBinContent(bin, fit_residual)
            hist_fit_residual_vsMass.SetBinError(bin, err_fit_residual)

    ndf_VarBin = NumberOfObservations_VarBin - nPar - 1  #ndof
    return [
        chi2_VarBin_notNorm, ndf_VarBin, chi2_VarBin, BKGfit,
        hist_fit_residual_vsMass, nPar, histoCI
    ]
示例#10
0
    gMassPValue.append(TGraph(0))
    gMassNsigma.append(TGraph(0))
    SetObjectStyle(gMassPValue[iPt], color=kRed + 1)
    SetObjectStyle(gMassNsigma[iPt], color=kRed + 1)

    hMassToFit = hMassSel[iPt].Clone()
    for iBin in range(hMassToFit.GetNbinsX()):  #remove signal region
        if mParticle - 0.02 < hMassToFit.GetBinCenter(iBin +
                                                      1) < mParticle + 0.02:
            hMassToFit.SetBinContent(iBin + 1, 0)
    hMassToFit.Fit(fMass[iPt], 'RE0')
    hConfIntBkg.append(
        TH1F(f'hConfIntMassKKMCPt{ptMin}_{ptMax}', '',
             hMassSel[iPt].GetNbinsX(), minMass, maxMass))
    TVirtualFitter.GetFitter().GetConfidenceIntervals(hConfIntBkg[iPt], 0.683)
    SetObjectStyle(hConfIntBkg[iPt],
                   color=kBlack,
                   fillalpha=0.3,
                   markerstyle=0)

    if iPt == 0:
        legFit.AddEntry(hMassSel[iPt], 'ML selected', 'lp')
        legFit.AddEntry(hConfIntBkg[iPt], f'{args.fitfunc} fit', 'fl')
        legFit.AddEntry(line, f'{particleTitle} mass', 'l')

    cMassFit.append(
        TCanvas(f'cMassFitPt{ptMin*10:.0f}_{ptMax*10:.0f}', '', 1200, 400))
    cMassFit[iPt].Divide(3, 1)
    cMassFit[iPt].cd(1).DrawFrame(minMass, 0., maxMass,
                                  hMassSel[iPt].GetMaximum() * 1.5,
示例#11
0
        f'{ptMin:.0f} < #it{{p}}_{{T}} < {ptMax:.0f} GeV/#it{{c}}')
    hMassKKBkgSubData[iPt].GetYaxis().SetTitle(f'Normalised Counts-{subLabel}')

    # fit distribution with voigt function
    fMassKKMC.append(TF1(f'fMassKKMCPt{iPt}', VoigtFunc, 0.98, 1.08, 4))
    fMassKKMC[iPt].SetLineColor(kRed + 1)
    fMassKKMC[iPt].SetParameters(hMassKKBkgSubMC[iPt].GetBinWidth(1), massPhi,
                                 0.0015, widthPhi)
    fMassKKMC[iPt].FixParameter(0, hMassKKBkgSubMC[iPt].GetBinWidth(1))
    fMassKKMC[iPt].SetParLimits(1, massPhi * 0.5, massPhi * 2)
    fMassKKMC[iPt].SetParLimits(2, 0., 0.1)
    fMassKKMC[iPt].FixParameter(3, widthPhi)
    resMC = hMassKKBkgSubMC[iPt].Fit(fMassKKMC[iPt], 'ER0SQ')
    hConfIntMassKKMC.append(
        TH1F(f'hConfIntMassKKMC_{iPt}', '', 500, 0.98, 1.08))
    TVirtualFitter.GetFitter().GetConfidenceIntervals(hConfIntMassKKMC[iPt],
                                                      0.68)
    SetObjectStyle(hConfIntMassKKMC[iPt],
                   color=kRed + 1,
                   alpha=0.3,
                   markerstyle=0)

    fMassKKData.append(TF1(f'fMassKKDataPt{iPt}', VoigtFunc, 0.98, 1.08, 4))
    fMassKKData[iPt].SetLineColor(kAzure + 4)
    fMassKKData[iPt].SetParameters(hMassKKBkgSubData[iPt].GetBinWidth(1),
                                   massPhi, 0.0015, widthPhi)
    fMassKKData[iPt].FixParameter(0, hMassKKBkgSubData[iPt].GetBinWidth(1))
    fMassKKData[iPt].SetParLimits(1, massPhi * 0.5, massPhi * 2)
    fMassKKData[iPt].SetParLimits(2, 0., 0.1)
    fMassKKData[iPt].FixParameter(3, widthPhi)
    resData = hMassKKBkgSubData[iPt].Fit(fMassKKData[iPt], 'ER0SQ')
    hConfIntMassKKData.append(
示例#12
0
e1 = round(e1,4)

# value of the first parameter
p2 = fit_x.GetParameter(1)
p2 = round(p2,4)

# error of the first parameter
e2 = fit_x.GetParError(1)
e2 = round(e2,4)

print "Chi2 = " + str(chi2)
print "P1 = " + str(p1)
print "E1 = " + str(e1)


TVirtualFitter.GetFitter().GetConfidenceIntervals(grint)
grint.SetLineColor(1)
grint.SetFillColorAlpha(16,0.65)

grint.GetHistogram().SetTitle(" ");
grint.GetHistogram().GetXaxis().SetTitle("Top P_{T} (GeV)")



if(THEORY == "PWHGP8" ):
    grint.GetHistogram().GetYaxis().SetTitle("DATA/Powheg+Pythia8")
elif(THEORY == "Kidonakis"):
    grint.GetHistogram().GetYaxis().SetTitle("DATA/aN^3LO")
elif(THEORY == "Mitov"):
    grint.GetHistogram().GetYaxis().SetTitle("DATA/NNLO")
elif(THEORY == "Lipka"):
示例#13
0
def doFit(FunctionType,hMassNEW,hMass,g,fFitXmin,fFitXmax,fNbins,xbins):
  
  if( FunctionType==-2 ):    
      nPar=2
      BKGfit = TF1("BKGfit%i"%FunctionType," [0] / ( TMath::Power(x/13000,[1]) )",fFitXmin,fFitXmax)
      # BKGfit.SetParameter(0,8.63367e-11)
   #    BKGfit.SetParameter(1,9.39072e+00)

  if( FunctionType==0 ):
    print "Fitting three parameter default function!"
    nPar=3
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]) )",fFitXmin,fFitXmax)
    # SIGfit = TF1("SIGfit%i"%FunctionType,"(TMath::Gaus(x,[3]))",fFitXmin,fFitXmax)
    # BKGfit.SetParameter(0,8.63367e-11)
    # BKGfit.SetParameter(1,9.39072e+00)
    # BKGfit.SetParameter(0,1.03079e-08)
    # BKGfit.SetParameter(1,7.86584e+00)
    # BKGfit.SetParameter(2,1.42945e+01)



  elif( FunctionType==1 ):
    print "Fitting four parameter default function!"
    nPar=4
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",fFitXmin,fFitXmax)
    BKGfit.SetParameter(0,8.63367e-11)
    BKGfit.SetParameter(1,9.39072e+00)
    BKGfit.SetParameter(0,4.07887e-18)
    BKGfit.SetParLimits(0,4.15001e-18-4.07887e-18,4.15001e-18+4.07887e-18)
    BKGfit.SetParameter(1,7.86584e+00)
    BKGfit.SetParameter(2,1.42945e+01)
    BKGfit.SetParameter(3,-5.1)
    
  elif( FunctionType==2 ):
    print "Fitting five parameter  function!"
    nPar=5
    # BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)+[4]*TMath::Power(log(x/13000),2)) )",fFitXmin,fFitXmax)
    BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1])*(1+[4]*x/13000) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",fFitXmin,fFitXmax)
    BKGfit.SetParameter(0,4.07887e-18)
    BKGfit.SetParLimits(0,4.15001e-18-4.07887e-18,4.15001e-18+4.07887e-18)
    BKGfit.SetParameter(1,7.86584e+00)
    BKGfit.SetParameter(2,1.42945e+01)
    BKGfit.SetParameter(3,-5.1000699000)

  elif( FunctionType==3 ):
    print "Fitting four parameter alternate function!"
    nPar=4
    BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]) )",fFitXmin,fFitXmax)
    # BKGfit.SetParameter(0,3.61696e-06) #VVHP
    # BKGfit.SetParameter(1,-1.29729e+00 )
    # BKGfit.SetParameter(2,5.93603e+00 )
    # BKGfit.SetParameter(3,3.02948e+02 )

    
  
  
  stopProgram=1;
  for loop in range (0,10):
    r = hMassNEW.Fit("BKGfit%i"%FunctionType,"ISR","",fFitXmin,fFitXmax)          
    fitStatus = int(r)
    print "fit status : %d" % fitStatus
    if(fitStatus==0):
      stopProgram=0
      # r.Print("V")
      break
 
  if(stopProgram==1):
    print "######################" 
    print"######################" 
    print "ERROR : Fit %i failed!!!!" %FunctionType
    print "######################" 
    print "######################"
    sys.exit()
  
  NumberOfVarBins = 0
  NumberOfObservations_VarBin = 0
  chi2_VarBin = 0.
  chi2_VarBin_ALL = 0.
  chi2_VarBin_notNorm = 0.
  chi2_VarBin_notNorm_ALL = 0.
  
  chi2_VarBin_notNorm_over5 = 0.
  chi2_VarBin_over5 = 0.
  NumberOfObservations_VarBin_over5 = 0.
  
  #Create a histogram to hold the confidence intervals
  # histoCI=TH1D("histoCI","", fNbins,xbins)
  # # histoCI=TH1D()
  # histoCI.SetTitle("Fitted histogram with .95 conf. band")
  # (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
  # #Now the "hint" histogram has the fitted function values as the
  # #bin contents and the confidence intervals as bin errors
  # histoCI.SetStats(kFALSE)
  # histoCI.SetFillColor(kRed+2)
  # histoCI.SetLineColor(kRed+2)
  # histoCI.SetFillStyle(3354)


  histoCI = TGraphErrors(fNbins)
  histoCI.SetTitle("Fitted line with .95 conf. band")
  for i in range (0, fNbins):
    histoCI.SetPoint(i, g.GetX()[i], 0)
  #Compute the confidence intervals at the x points of the created graph
  (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
  # //Now the "grint" graph contains function values as its y-coordinates
 #  //and confidence intervals as the errors on these coordinates
 #  //Draw the graph, the function and the confidence intervals
  histoCI.SetLineColor(0)
  histoCI.SetLineWidth(-802)
  histoCI.SetFillStyle(3002)
  histoCI.SetFillColor(2)  
  
  
  
  
  
  hist_fit_residual_vsMass =  TH1D("hist_fit_residual_vsMass","hist_fit_residual_vsMass",fNbins,xbins)
  
  for bin in range (1,hMassNEW.GetNbinsX()):
    if( hMassNEW.GetXaxis().GetBinLowEdge(bin)>=fFitXmin and hMassNEW.GetXaxis().GetBinUpEdge(bin)<=fFitXmax ):
       NumberOfVarBins += 1
       data = hMassNEW.GetBinContent(bin)
       err_data_low = g.GetErrorYlow(bin-1) 
       err_data_high= g.GetErrorYhigh(bin-1)
       fit = BKGfit.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin) , hMassNEW.GetXaxis().GetBinUpEdge(bin) )
       fit = fit / ( hMassNEW.GetBinWidth(bin) )
       # fit = BKGfit.Eval(hMassNEW.GetBinCenter(bin)) #yields same results
       if(fit > data):
         err_tot = err_data_high
       else:
         err_tot = err_data_low
       # if(err_tot==0):continue
       fit_residual = (data - fit) / err_tot
       err_fit_residual = 1
       
       chi2_VarBin_notNorm_ALL += pow( (data - fit) , 2 ) #For alternate method from Yongjie, not skipping emty bins
       chi2_VarBin_ALL += pow( (data - fit) , 2 ) / pow( err_tot , 2 )
       
       if (hMassNEW.GetBinContent(bin)>0.0000000000001):
         NumberOfObservations_VarBin+=1
         chi2_VarBin += pow( (data - fit) , 2 ) / pow( err_tot , 2 )	 
         chi2_VarBin_notNorm += pow( (data - fit) , 2 ) 	 #For dijet method, skipping emty bins
         print "Mass = %f , Bin = %i ,  data = %f,  fit = %f,  rss = %f ,  +=rss= %f" %(hMassNEW.GetBinCenter(bin),bin,data,fit,(pow( (data - fit) , 2 )),chi2_VarBin_notNorm)
       if (hMassNEW.GetBinContent(bin)*hMassNEW.GetBinWidth(bin)>=5):
         NumberOfObservations_VarBin_over5+=1
         chi2_VarBin_over5 += pow( (data - fit) , 2 ) / pow( err_tot , 2 )	 
         chi2_VarBin_notNorm_over5+= pow( (data - fit) , 2 ) 	 #For F-test, skipping bins with less than 5 entries
         
       hist_fit_residual_vsMass.SetBinContent(bin,fit_residual)
       hist_fit_residual_vsMass.SetBinError(bin,err_fit_residual)
  
  ndf_VarBin = NumberOfObservations_VarBin - nPar -1 #ndof for >0 bins
  ndf_VarBin_over5 = NumberOfObservations_VarBin_over5 - nPar -1 #ndof for >5 bins
    
  return [chi2_VarBin_notNorm,ndf_VarBin,chi2_VarBin,BKGfit,hist_fit_residual_vsMass,nPar,chi2_VarBin_ALL,chi2_VarBin_notNorm_ALL,histoCI,ndf_VarBin_over5,chi2_VarBin_notNorm_over5]   
示例#14
0
def doFit(FunctionType,hMassNEW,g,fFitXmin,fFitXmax,fNbins,xbins,fLabel):
  print ""
  print ""
  
  if( FunctionType==-2 ):
    nPar=2
    BKGfit = TF1("BKGfit%i"%FunctionType," [0] / ( TMath::Power(x/13000,[1]) )",fFitXmin,fFitXmax)
    BKGfit.SetParameter(0, 1.76820e-08 )#4.92405e-09   ) #WWHP
    BKGfit.SetParameter(1, 8.25223e+00 )#7.90584e+00   ) #WWHP
    if fLabel.find("qV") != -1:
      BKGfit.SetParameter(0,  3.72282e-12 )
      BKGfit.SetParameter(1, -5.30842e+00 )
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1: 
        BKGfit.SetParameter(0,8.22440e-09) #WWLP
        BKGfit.SetParameter(1,8.48110e+00)
 
 
    if fLabel.find("WW") != -1 and fLabel.find("LP") != -1: 
        BKGfit.SetParameter(0,3.54934e-07   ) #WWLP

    
  if( FunctionType==0 ):
    print "Fitting three parameter default function!"
    nPar=3
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]) )",fFitXmin,fFitXmax)
    if fLabel.find("qV") != -1:
      BKGfit.SetParameter(0,  3.72282e-12 )
      BKGfit.SetParameter(1, -5.30842e+00 )
      BKGfit.SetParameter(2,  1.82771e+01 )
    if fLabel.find("qW")!=-1 or fLabel.find("qZ")!=-1:
        BKGfit.SetParameter(0,5.83605e-09   ) #WWHP
        BKGfit.SetParameter(1,4.83899e-01   ) #WWHP
        BKGfit.SetParameter(2,7.85446e+00   ) #WWHP
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:
        BKGfit.SetParameter(0,3.09869e-05)
        BKGfit.SetParameter(1,2.11957e+01)
        BKGfit.SetParameter(2,10)
       
        BKGfit.SetParLimits(2,10.2,20)
       
    if fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
        BKGfit.SetParameter(0, 1.91544e-07 ) #WWLP
        BKGfit.SetParameter(1,-7.48572e-01 ) #WWHP
        BKGfit.SetParameter(2, 8.07181e+00 ) #WWHP
      
    if fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
        print "ZZHP three paramter default function"
        BKGfit.SetParameter(0, 1.78211e-08 ) #ZZHP
        BKGfit.SetParameter(1, 8.31381e+00 ) #WWHP
        BKGfit.SetParameter(2, 0. ) #3.36325e+00   )
        
        #BKGfit.SetParLimits(0, 1.78211e-08, 1.78211e-08)
        #BKGfit.SetParLimits(1, 8.31381e+00 ,8.31381e+00)
        #BKGfit.SetParLimits(2, -0.000001, 0.000001   ) 
    if fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
        BKGfit.SetParameter(0,5.83605e-09   ) #WWHP
        BKGfit.SetParameter(1,4.83899e-01   ) #WWHP
        BKGfit.SetParameter(2,7.85446e+00   ) #WWHP
    if fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
        print "WZHP"
        BKGfit.SetParameter(0, 1.35257e-08)
        BKGfit.SetParameter(1, 8.74474e+00)
        #BKGfit.SetParLimits(0,0,1e-07)
        #BKGfit.SetParLimits(1,5,8)
        BKGfit.SetParLimits(2,8.8,12)
        #BKGfit.SetParameter(0,0)#1.76820e-08)#5.83605e-09   ) #WWHP
        #BKGfit.SetParameter(1,0)#8.25223e+00)#4.83899e-01   ) #WWHP
        #BKGfit.SetParameter(2,0)#7.85446e+00   ) #WWHP
    if fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
        BKGfit.SetParameter(0,5.83605e-09   ) #WWHP
        BKGfit.SetParameter(1,4.83899e-01   ) #WWHP
        BKGfit.SetParameter(2,7.85446e+00   ) #WWHP

  elif( FunctionType==1 ):
    print "Fitting four parameter default function!"
    nPar=4
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",fFitXmin,fFitXmax)
    if fLabel.find("qV") != -1:
      BKGfit.SetParameter(0,  3.72282e-12 )
      BKGfit.SetParameter(1, -5.30842e+00 )
      BKGfit.SetParameter(2,  1.82771e+01 )
      BKGfit.SetParameter(3,  2.27782e+00 )
    if fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
      #BKGfit.SetParLimits(0,  1.57838e+07,1.57838e+08  )
      BKGfit.SetParameter(0,  2.93331e-14   )
      BKGfit.SetParameter(1, -1.50024e+01   ) 
      BKGfit.SetParameter(2,  1.69784e+01  )
      BKGfit.SetParameter(3, 1.41238e+00   )
      BKGfit.SetParLimits(3,0,5)
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,  5.08045e-10 )                                 
      BKGfit.SetParameter(1,  8.10729e+00 )                                 
      BKGfit.SetParameter(2,  1.28397e+01 )                         
      BKGfit.SetParameter(3,  1.20048e+00 )
      BKGfit.SetParLimits(3,-10,0)
      BKGfit.SetParLimits(2,10,20)
    if fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0, 1.34232e-02 )#WWLP
      BKGfit.SetParameter(1, 1.35564e+01 ) #WWLP
      BKGfit.SetParameter(2, 1.29328e+00 )#WWLP
      BKGfit.SetParameter(3,-1.12300e+00 )#WWLP
    if fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0, 1.40401e-06)#1.45686e-06)#WWLP
      BKGfit.SetParameter(1, 2.78968e+00)#2.86174e+00) #WWLP
      BKGfit.SetParameter(2, 7.70104e+00)#7.68425e+00) #WWLP
      BKGfit.SetParameter(3, -1) #WWLP  
      BKGfit.SetParLimits(3,-10,-0.005)
    if fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
      # BKGfit.SetParLimits(0, 9.66361e-08,9.66361e-06 )
      BKGfit.SetParameter(0, 9.11215e-09)
      BKGfit.SetParameter(1, 2.02554e+01)
      BKGfit.SetParameter(2, 1.36639e+01)
      BKGfit.SetParameter(3, 0.) #1.77601e+00)
      #BKGfit.SetParLimits(3,0.,0.)
    if fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
        #BKGfit.SetParLimits(0, 1)#9.66361e-08,9.66361e-04 )
      BKGfit.SetParameter(0, 6.27404e-07 )#6.92267e-04 )                            
      BKGfit.SetParameter(1, 2.69667e+00 )#1.18029e+01 )                                                                   
      BKGfit.SetParameter(2, 7.73807e+00 )# 3.50726e+00)                             
      BKGfit.SetParameter(3, 1         )#-6.96555e-01 ) 
      BKGfit.SetParLimits(3,0,2)
      
  
  elif( FunctionType==2 ):
    print "Fitting five parameter default function!"
    nPar=5
    BKGfit = TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)+[4]*TMath::Power(log(x/13000),2)) )",fFitXmin,fFitXmax)
    if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0, 5.30325e-05)
      # BKGfit.SetParameter(1, 4.87226e+00)
  #     BKGfit.SetParameter(2, 6.71810e+00)
  #     BKGfit.SetParameter(3,0.001)
    elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0, 1.69726e-03)
      BKGfit.SetParameter(1, 7.40452e+00)  
      BKGfit.SetParameter(2, 5.93207e+00)
      BKGfit.SetParameter(3,-7.75527e-03)
    elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,3.47742e-05)
      BKGfit.SetParameter(1,4.11131e+00)
      BKGfit.SetParameter(2,6.97258e+00)
      BKGfit.SetParameter(3,0.0000)
      BKGfit.SetParameter(4,0.0000)
    elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.06977e-01 )                          
      BKGfit.SetParameter(1, 9.45383e+00)                          
      BKGfit.SetParameter(2, 3.83836e+00)                          
      BKGfit.SetParameter(3,-5.80838e-01)                          
      BKGfit.SetParameter(4,-3.49361e-02)                          
      
    elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,3.47742e-05)
      BKGfit.SetParameter(1,4.11131e+00)
      BKGfit.SetParameter(2,6.97258e+00)
      BKGfit.SetParameter(3,0.0000)
      BKGfit.SetParameter(4,0.0000)
    elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,9.62968e-04)
      BKGfit.SetParameter(1,6.99994e+00)
      BKGfit.SetParameter(2,6.09152e+00)
      BKGfit.SetParameter(3,0.0000)
      BKGfit.SetParameter(4,0.0000)
  
  
  elif( FunctionType==3 ):
    print "Fitting four parameter alternate function!"
    nPar=4
    BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]) )",fFitXmin,fFitXmax)
    if fLabel.find("q") != -1:
        BKGfit.SetParameter(0, 3.45939e+02 )
        BKGfit.SetParameter(1,-4.45967e+00 )
        BKGfit.SetParameter(2, 1.31413e+00 )
        BKGfit.SetParameter(3, 1.91782e+02  )
    if fLabel.find("qV")!=-1 and fLabel.find("HP")!=-1:
        BKGfit.SetParameter(0, 3.45939e+02 )
        BKGfit.SetParameter(1,-4.45967e+00 )
        BKGfit.SetParameter(2, 1.31413e+00 )
        BKGfit.SetParameter(3, 0.0)
    if fLabel.find("WW") != -1 and fLabel.find("HP") != -1:     
      BKGfit.SetParameter(0,  7.16992e-05 )                     
      BKGfit.SetParameter(1, -3.00473e+00 )                     
      #BKGfit.SetParameter(2,  5.35092e+00 )                     
      #BKGfit.SetParameter(3,  9.42630e+01 )
      BKGfit.SetParLimits(2,5,10)
      BKGfit.SetParLimits(3,-15,1)
      
    if fLabel.find("WW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0, 4.48531e-07 )# 1.47497e+08) #WWLP
      BKGfit.SetParameter(1, -7.96655e-01)#5.01221e+01 ) #WWLP
      BKGfit.SetParameter(2, 7.43952e+00 )#-1.21037e+01) #WWLP
      BKGfit.SetParameter(3,0.)#-3.10164e+00) #WWLP
    if fLabel.find("ZZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0, 4.48531e-07   ) #ZZLP
      BKGfit.SetParameter(1, -7.96655e-01 ) #WWLP
      BKGfit.SetParameter(2, 7.43952e+00  ) #WWLP
      BKGfit.SetParameter(3,0.   ) #ZZLP
    if fLabel.find("ZZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,  9.66361e-07) #ZZHP
      BKGfit.SetParameter(1, -1.15120e+01)
      BKGfit.SetParameter(2,  6.63440e+00)
      BKGfit.SetParameter(3,  1.04241e+01)
      
    if fLabel.find("WZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,  1.31274e-02) 
      BKGfit.SetParameter(1, -2.42314e+00 )
      BKGfit.SetParameter(2,  4.70224e+00 )
      BKGfit.SetParameter(3,  5.41163e+02 )
      
    if fLabel.find("WZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,4.48531e-07 ) #WWLP
      BKGfit.SetParameter(1,-7.96655e-01) #WWLP
      BKGfit.SetParameter(2,7.43952e+00 ) #WWLP
      BKGfit.SetParameter(3, 0.0) #WWLP
  
  
  elif( FunctionType==4 ):
    print "Fitting five parameter alternate function!"
    nPar=5
    BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]+[4]*log(x/13000)) )",fFitXmin,fFitXmax)
    if fLabel.find("qW") != -1 and fLabel.find("HP") != -1:
     BKGfit.SetParameter(0, 7.93174e-05)
     BKGfit.SetParameter(1,-9.05071e+00)
     BKGfit.SetParameter(2, 5.02176e+00)
     BKGfit.SetParameter(3, 5.03605e+00)
     BKGfit.SetParameter(4,-4.77303e-01)
     BKGfit.SetParameter(5, 7.93174e-05)

    elif fLabel.find("qW") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,1.47860e-03)
      BKGfit.SetParameter(1,7.17031e+00)  
      BKGfit.SetParameter(2,6.00169e+00)
      
    elif fLabel.find("qZ") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,3.47742e-05)
      BKGfit.SetParameter(1,4.11131e+00)
      BKGfit.SetParameter(2,6.97258e+00)
      BKGfit.SetParameter(3,0.0000)
      BKGfit.SetParameter(4,0.0000)
    elif fLabel.find("qZ") != -1 and fLabel.find("LP") != -1:
      BKGfit.SetParameter(0,  3.08932e-04)                 
      BKGfit.SetParameter(1, -4.02134e+00)                 
      BKGfit.SetParameter(2,  7.00824e+00)                 
      BKGfit.SetParameter(3,  5.03328e+00)                 
      BKGfit.SetParameter(4,-4.77303e-01)
     
      
    elif fLabel.find("qV") != -1 and fLabel.find("HP") != -1:
      BKGfit.SetParameter(0,3.47742e-05)
      BKGfit.SetParameter(1,4.11131e+00)
      BKGfit.SetParameter(2,6.97258e+00)
      BKGfit.SetParameter(3,0.0000)
      BKGfit.SetParameter(4,0.0000)
    elif fLabel.find("qV") != -1 and fLabel.find("LP") != -1:
      #BKGfit.SetParameter(0, 8.96766e-04)
      #BKGfit.SetParameter(1, 6.70628e+00)
      #BKGfit.SetParameter(2, 6.11149e+00)
      BKGfit.SetParameter(0,3.47742e-05)
      BKGfit.SetParameter(1,4.11131e+00)
      BKGfit.SetParameter(2,6.97258e+00)
      BKGfit.SetParameter(3, 0.0)
      BKGfit.SetParameter(4,0.0)
  
  stopProgram=1;
  for loop in range (0,30):
    r = hMassNEW.Fit("BKGfit%i"%FunctionType,"ISRB","",fFitXmin,fFitXmax)  
    # r = hMassNEW.Fit("BKGfit%i"%FunctionType,"LFISR","",fFitXmin,fFitXmax)
    fitStatus = int(r)
    print "fit status : %d" % fitStatus
    #if(fitStatus==0 and loop > 3):
      #stopProgram=0
      ## r.Print("V")
      #break
 
  #if(stopProgram==1):
    #print "######################" 
    #print"######################" 
    #print "ERROR : Fit %i failed!!!!" %FunctionType
    #print "######################" 
    #print "######################"
    #sys.exit()
  
  
  #Create a histogram to hold the confidence intervals
  # histoCI=TH1D("histoCI","", fNbins,xbins)
  # # histoCI=TH1D()
  # histoCI.SetTitle("Fitted histogram with .95 conf. band")
  # (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
  # #Now the "hint" histogram has the fitted function values as the
  # #bin contents and the confidence intervals as bin errors
  # histoCI.SetStats(kFALSE)
  # histoCI.SetFillColor(kRed+2)
  # histoCI.SetLineColor(kRed+2)
  # histoCI.SetFillStyle(3354)


  histoCI = TGraphErrors(fNbins)
  histoCI.SetTitle("Fitted line with .95 conf. band")
  for i in range (0, fNbins):
    histoCI.SetPoint(i, g.GetX()[i], 0)
  #Compute the confidence intervals at the x points of the created graph
  (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
  # //Now the "grint" graph contains function values as its y-coordinates
 #  //and confidence intervals as the errors on these coordinates
 #  //Draw the graph, the function and the confidence intervals
  histoCI.SetLineColor(0)
  histoCI.SetLineWidth(-802)
  histoCI.SetFillStyle(3002)
  histoCI.SetFillColor(2)  
  
  NumberOfVarBins = 0
  NumberOfObservations_VarBin = 0
  chi2_VarBin = 0.
  chi2_VarBin_notNorm = 0.
  hist_fit_residual_vsMass =  TH1D("hist_fit_residual_vsMass","hist_fit_residual_vsMass",fNbins,xbins)
  
  for bin in range (1,hMassNEW.GetNbinsX()):
    if( hMassNEW.GetXaxis().GetBinCenter(bin)>=fFitXmin and hMassNEW.GetXaxis().GetBinCenter(bin)<=fFitXmax ):
       NumberOfVarBins += 1
       data = hMassNEW.GetBinContent(bin)
       # data = g.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin) , hMassNEW.GetXaxis().GetBinUpEdge(bin) )
       err_data_low = g.GetErrorYlow(bin-1) 
       err_data_high= g.GetErrorYhigh(bin-1)
       fit = BKGfit.Integral(hMassNEW.GetXaxis().GetBinCenter(bin) , hMassNEW.GetXaxis().GetBinCenter(bin) )
       fit = fit / ( hMassNEW.GetBinWidth(bin) )
       # fit = BKGfit.Eval(hMassNEW.GetBinCenter(bin)) #yields same results
       if(fit > data):
         err_tot = err_data_high
       else:
         err_tot = err_data_low
       fit_residual = (data - fit) / err_tot
       err_fit_residual = 1
       
       if (hMassNEW.GetBinContent(bin)>0):
         NumberOfObservations_VarBin+=1
         chi2_VarBin += pow( (data - fit) , 2 ) / pow( err_tot , 2 )	 
         chi2_VarBin_notNorm += pow( (data - fit) , 2 )

       hist_fit_residual_vsMass.SetBinContent(bin,fit_residual)
       hist_fit_residual_vsMass.SetBinError(bin,err_fit_residual)
  
  ndf_VarBin = NumberOfObservations_VarBin - nPar -1 #ndof    
  return [chi2_VarBin_notNorm,ndf_VarBin,chi2_VarBin,BKGfit,hist_fit_residual_vsMass,nPar,histoCI]   
示例#15
0
ay = array('f', (5.0935, 2.1777, 0.2089, -2.3949, -2.4457, -3.0430, -2.2731, -2.0706, -1.6231, -2.5605, -0.7703, -0.3055, 1.6817, 1.8728, 3.6586, 3.2353, 4.2520, 5.2550, 3.8766, 4.2890 ) ) 
ey=np.array(len(data_x)*[0.5],dtype=np.float)
ex=np.array(len(data_y)*[0],dtype=np.float)
# ... and pass to TGraphErros object
gr=TGraphErrors(nPoints,data_x,data_y,ex,ey)
gr.SetTitle("TGraphErrors mit Fit")
gr.Draw("AP");

Pol=["pol1","pol2","pol3","pol4","pol5","pol6","pol7"]

# Polifit
for i in range(len(polynom)):
  gr.Fit(polynom[i],"V")
  c1.Update()
  gr.Draw('AP')
  fitrp = TVirtualFitter.GetFitter()
  nPar = fitrp.GetNumberTotalParameters()
  covmat = TMatrixD(nPar, nPar,fitrp.GetCovarianceMatrix())
  print('The Covariance Matrix is: ')
  covmat.Print()
  cormat = TMatrixD(covmat)
  for i in range(nPar):
    for j in range(nPar):
      cormat[i][j] = cormat[i][j] / (np.sqrt(covmat[i][i]) * np.sqrt(covmat[j][j]))
  print('The Correlation Matrix is: ')
  cormat.Print()
  raw_input('Press <ret> to continue -> ')

#Legendre Poly
for i in range(len(legendre)):
  gr.Fit(legendre[i],"V")
示例#16
0
def doFit(FunctionType, hMassNEW, g, fFitXmin, fFitXmax, fNbins, xbins):
    print ""
    print ""

    if (FunctionType == -2):
        nPar = 2
        BKGfit = TF1("BKGfit%i" % FunctionType,
                     " [0] / ( TMath::Power(x/13000,[1]) )", fFitXmin,
                     fFitXmax)
        # BKGfit.SetParameter(0,43557e-16 )

    if (FunctionType == 0):
        print "Fitting three parameter default function!"
        nPar = 3
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]) )",
            fFitXmin, fFitXmax)
        # BKGfit.SetParameter(0,3.43557e-16 )
    #  BKGfit.SetParameter(1,-4.20539e+01)
    #  BKGfit.SetParameter(2,1.29180e+01)

    elif (FunctionType == 1):
        print "Fitting four parameter default function!"
        nPar = 4
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",
            fFitXmin, fFitXmax)
        # BKGfit.SetParameter(0,2.89006e-05    )
        # BKGfit.SetParameter(1,1.44811e+01      )
        # BKGfit.SetParameter(2,6.08355)

    elif (FunctionType == 2):
        print "Fitting five parameter  function!"
        nPar = 5
        # BKGfit =  TF1("BKGfit%i"%FunctionType,"( [0]*TMath::Power(1-x/13000,[1]) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)+[4]*TMath::Power(log(x/13000),2)) )",fFitXmin,fFitXmax)
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000,[1])*(1+[4]*x/13000) ) / ( TMath::Power(x/13000,[2]+[3]*log(x/13000)) )",
            fFitXmin, fFitXmax)
        # BKGfit.SetParameter(0,2.89006e-05    )
        # BKGfit.SetParameter(1,1.44811e+01      )
        # BKGfit.SetParameter(2,6.08355)

    elif (FunctionType == 3):
        print "Fitting four parameter alternate function!"
        nPar = 4
        BKGfit = TF1(
            "BKGfit%i" % FunctionType,
            "( [0]*TMath::Power(1-x/13000 + [3]*TMath::Power(x/13000,2),[1]) ) / ( TMath::Power(x/13000,[2]) )",
            fFitXmin, fFitXmax)
        BKGfit.SetParameter(0, 5.87345e-09)  #VVHP
        BKGfit.SetParameter(1, 7.86080)
        BKGfit.SetParameter(2, 1.29180e+01)
        BKGfit.SetParameter(3, .4)

    stopProgram = 1
    for loop in range(0, 10):
        r = hMassNEW.Fit("BKGfit%i" % FunctionType, "ISR", "", fFitXmin,
                         fFitXmax)
        fitStatus = int(r)
        print "fit status : %d" % fitStatus
        if (fitStatus == 0):
            stopProgram = 0
            # r.Print("V")
            break

    if (stopProgram == 1):
        print "######################"
        print "######################"
        print "ERROR : Fit %i failed!!!!" % FunctionType
        print "######################"
        print "######################"
        sys.exit()

    #Create a histogram to hold the confidence intervals
    # histoCI=TH1D("histoCI","", fNbins,xbins)
    # # histoCI=TH1D()
    # histoCI.SetTitle("Fitted histogram with .95 conf. band")
    # (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
    # #Now the "hint" histogram has the fitted function values as the
    # #bin contents and the confidence intervals as bin errors
    # histoCI.SetStats(kFALSE)
    # histoCI.SetFillColor(kRed+2)
    # histoCI.SetLineColor(kRed+2)
    # histoCI.SetFillStyle(3354)

    histoCI = TGraphErrors(fNbins)
    histoCI.SetTitle("Fitted line with .95 conf. band")
    for i in range(0, fNbins):
        histoCI.SetPoint(i, g.GetX()[i], 0)
    #Compute the confidence intervals at the x points of the created graph
    (TVirtualFitter.GetFitter()).GetConfidenceIntervals(histoCI)
    # //Now the "grint" graph contains function values as its y-coordinates
    #  //and confidence intervals as the errors on these coordinates
    #  //Draw the graph, the function and the confidence intervals
    histoCI.SetLineColor(0)
    histoCI.SetLineWidth(-802)
    histoCI.SetFillStyle(3002)
    histoCI.SetFillColor(2)

    NumberOfVarBins = 0
    NumberOfObservations_VarBin = 0
    chi2_VarBin = 0.
    chi2_VarBin_notNorm = 0.
    hist_fit_residual_vsMass = TH1D("hist_fit_residual_vsMass",
                                    "hist_fit_residual_vsMass", fNbins, xbins)

    for bin in range(1, hMassNEW.GetNbinsX()):
        if (hMassNEW.GetXaxis().GetBinLowEdge(bin) >= fFitXmin
                and hMassNEW.GetXaxis().GetBinUpEdge(bin) <= fFitXmax):
            NumberOfVarBins += 1
            data = hMassNEW.GetBinContent(bin)
            # data = g.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin) , hMassNEW.GetXaxis().GetBinUpEdge(bin) )
            err_data_low = g.GetErrorYlow(bin - 1)
            err_data_high = g.GetErrorYhigh(bin - 1)
            fit = BKGfit.Integral(hMassNEW.GetXaxis().GetBinLowEdge(bin),
                                  hMassNEW.GetXaxis().GetBinUpEdge(bin))
            fit = fit / (hMassNEW.GetBinWidth(bin))
            # fit = BKGfit.Eval(hMassNEW.GetBinCenter(bin)) #yields same results
            if (fit > data):
                err_tot = err_data_high
            else:
                err_tot = err_data_low
            fit_residual = (data - fit) / err_tot
            err_fit_residual = 1

            if (hMassNEW.GetBinContent(bin) > 0):
                NumberOfObservations_VarBin += 1
                chi2_VarBin += pow((data - fit), 2) / pow(err_tot, 2)
                chi2_VarBin_notNorm += pow((data - fit), 2)

            hist_fit_residual_vsMass.SetBinContent(bin, fit_residual)
            hist_fit_residual_vsMass.SetBinError(bin, err_fit_residual)

    ndf_VarBin = NumberOfObservations_VarBin - nPar - 1  #ndof
    return [
        chi2_VarBin_notNorm, ndf_VarBin, chi2_VarBin, BKGfit,
        hist_fit_residual_vsMass, nPar, histoCI
    ]
示例#17
0
def RunMinuit(sess, nll, feed_dict = None, float_tfpars = None, call_limit = 50000, useGradient = True, 
              gradient = None, printout = 50, tmpFile = "tmp_result.txt", 
              runHesse = False, runMinos = False, 
              options = None, run_metadata = None ) :
  """
    Perform MINUIT minimisation of the negative likelihood. 
      sess         : TF session
      nll          : graph for negitive likelihood to be minimised
      feed_dict    : 
      float_tfpars : list of floating parameter to be used in the fit
      call_limit   : call limit for MINUIT
      gradient     : external gradient graph. If None and useGradient is not False, will be 
                     calculated internally
      useGradient  : flag to control the use of analytic gradient while fitting: 
                     None or False   : gradient is not used
                     True or "CHECK" : analytic gradient will be checked with finite elements, 
                                      and will be used is they match
                     "FORCE"         : analytic gradient will be used regardless. 
      printout     : Printout frequency 
      tmpFile      : Name of the file with temporary results (updated every time printout is called)
      runHesse     ; Run HESSE after minimisation
      runMinos     : Run MINOS after minimisation
      options      : additional options to pass to TF session run
      run_metadata : metadata to pass to TF session run
  """

  global cacheable_tensors

  if float_tfpars is None :
    tfpars = tf.trainable_variables()                      # Create TF variables
    float_tfpars = [ p for p in tfpars if p.floating() ]   # List of floating parameters

  if useGradient and gradient is None : 
    gradient = tf.gradients(nll, float_tfpars)            # Get analytic gradient

  cached_data = {}

  fetch_list = []
  for i in cacheable_tensors : 
    if i not in cached_data : fetch_list += [ i ]
  if feed_dict : 
    feeds = dict(feed_dict)
  else : 
    feeds = None
  for i in cacheable_tensors : 
    if i in cached_data : feeds[i] = cached_data[i]

  fetch_data = sess.run(fetch_list, feed_dict = feeds ) # Calculate log likelihood

  for i,d in zip(fetch_list, fetch_data) : 
    cached_data[i] = d

  if feed_dict : 
    feeds = dict(feed_dict)
  else : 
    feeds = None
  for i in cacheable_tensors : 
    if i in cached_data : feeds[i] = cached_data[i]

  def fcn(npar, gin, f, par, istatus) :                  # MINUIT fit function 
    for i in range(len(float_tfpars)) : float_tfpars[i].update(sess, par[i])

    f[0] = sess.run(nll, feed_dict = feeds, options = options, run_metadata = run_metadata ) # Calculate log likelihood

    if istatus == 2 :            # If gradient calculation is needed
      dnll = sess.run(gradient, feed_dict = feeds, options = options, run_metadata = run_metadata )  # Calculate analytic gradient
      for i in range(len(float_tfpars)) : gin[i] = dnll[i] # Pass gradient to MINUIT
    fcn.n += 1
    if fcn.n % printout == 0 : 
      print "  Iteration ", fcn.n, ", Flag=", istatus, " NLL=", f[0], ", pars=", sess.run(float_tfpars)
      tmp_results = { 'loglh' : f[0], "status" : -1 }
      for n,p in enumerate(float_tfpars) : 
        tmp_results[p.par_name] = (p.prev_value, 0.)
      #WriteFitResults(tmp_results, tmpFile)

  fcn.n = 0
  minuit = TVirtualFitter.Fitter(0, len(float_tfpars))        # Create MINUIT instance
  minuit.Clear()
  minuit.SetFCN(fcn)
  arglist = array.array('d', 10*[0])    # Auxiliary array for MINUIT parameters

  for n,p in enumerate(float_tfpars) :  # Declare fit parameters in MINUIT
#    print "passing parameter %s to Minuit" % p.par_name
    step_size = p.step_size
    lower_limit = p.lower_limit
    upper_limit = p.upper_limit
    if not step_size : step_size = 1e-6
    if not lower_limit : lower_limit = 0.
    if not upper_limit : upper_limit = 0.
    minuit.SetParameter(n, p.par_name, p.init_value, step_size, lower_limit, upper_limit)

  arglist[0] = 0.5
  minuit.ExecuteCommand("SET ERR", arglist, 1)  # Set error definition for neg. likelihood fit
  if useGradient == True or useGradient == "CHECK" : 
    minuit.ExecuteCommand("SET GRA", arglist, 0)  # Ask analytic gradient
  elif useGradient == "FORCE" : 
    arglist[0] = 1
    minuit.ExecuteCommand("SET GRA", arglist, 1)  # Ask analytic gradient
  arglist[0] = call_limit                       # Set call limit
  minuit.ExecuteCommand("MIGRAD", arglist, 1)   # Perform minimisation

  minuit.ExecuteCommand("SET NOG", arglist, 0)  # Ask no analytic gradient

  if runHesse : 
    minuit.ExecuteCommand("HESSE", arglist, 1)

  if runMinos : 
    minuit.ExecuteCommand("MINOS", arglist, 1)

  results = {}                                  # Get fit results and update parameters
  for n,p in enumerate(float_tfpars) : 
    p.update(sess, minuit.GetParameter(n) )
    p.fitted_value = minuit.GetParameter(n)
    p.error = minuit.GetParError(n)
    if runMinos : 
      eplus  = array.array("d", [0.])
      eminus = array.array("d", [0.])
      eparab = array.array("d", [0.])
      globcc = array.array("d", [0.])
      minuit.GetErrors(n, eplus, eminus, eparab, globcc)
      p.positive_error = eplus[0]
      p.negative_error = eminus[0]
      results[p.par_name] = ( p.fitted_value, p.error, p.positive_error, p.negative_error )
    else : 
      results[p.par_name] = ( p.fitted_value, p.error)

  # Get status of minimisation and NLL at the minimum
  maxlh = array.array("d", [0.])
  edm = array.array("d", [0.])
  errdef = array.array("d", [0.])
  nvpar = array.array("i", [0])
  nparx = array.array("i", [0])
  fitstatus = minuit.GetStats(maxlh, edm, errdef, nvpar, nparx)

  # return fit results
  results["edm"] = edm[0]
  results["loglh"] = maxlh[0]
  results["status"] = fitstatus
  results["iterations"] = fcn.n
  return results