Ejemplo n.º 1
0
def DoRooFit(histo, title):
    can = makeCMSCanvas(str(random.random()),"Fit result ",900,700)
    
    #Varible
    if "ele" in title:
      x1 = RooRealVar("x1","m_{e^{+}e^{-}}",80,100)
    if "mu" in title:
      x1 = RooRealVar("x1","m_{#mu^{+}#mu^{-}}",80,100)

    #Define CB function
    m = RooRealVar("mean_{CB}","mean of gaussian",60,120)
    s = RooRealVar("#sigma_{CB}","width of gaussian",0,3)
    a = RooRealVar("#alpha_{CB}","mean of gaussian",0,100)
    n = RooRealVar("n_{CB}","width of gaussian",0,5)
    CB = RooCBShape("CB","CB PDF",x1, m, s, a, n)
    
    m.setConstant(kFALSE)
    s.setConstant(kFALSE)
    a.setConstant(kFALSE)
    n.setConstant(kFALSE)
    
    
    #Define Gaussian function
    mean1 = RooRealVar("mean_{G}","mean of gaussian",-60,60)
    sigma1 = RooRealVar("#sigma_{G}","width of gaussian",0,10)
    gauss1 = RooGaussian("gauss1","gaussian PDF",x1,mean1,sigma1)
    
    mean1.setConstant(kFALSE)
    sigma1.setConstant(kFALSE)
    
    #Starting values of the parameters
    mean1.setVal(1.0)
    sigma1.setVal(1.0)
    m.setVal(90.0)
    s.setVal(1.0)
    a.setVal(10.0)
    n.setVal(2.0)

    # Construct CB (x) gauss
    x1.setBins(10000, "cache")
    CBxG = RooFFTConvPdf("CBxG", "CB (X) gauss", x1, CB, gauss1)
    
    can.cd()
    d = RooDataHist("d","d",RooArgList(x1),RooFit.Import(histo))
    CBxG.fitTo(d, RooLinkedList())
   
    # Plot PDF and toy data overlaid
    xframe2 = x1.frame(RooFit.Name("xframe"),RooFit.Title("")) # RooPlot
    d.plotOn(xframe2, RooLinkedList() )
    CBxG.paramOn(xframe2, RooFit.Layout(0.65,0.99,0.9))
    xframe2.getAttText().SetTextSize(0.03)
    CBxG.plotOn(xframe2)
    xframe2.Draw()
    can.SaveAs("DataVsMC/FitResults/"+title+"_Roofit.pdf")
    can.SaveAs("DataVsMC/FitResults/"+title+"_Roofit.png")
    
    return;
Ejemplo n.º 2
0
def rooFit208():

    print ">>> setup component pdfs..."
    t = RooRealVar("t", "t", -10, 30)
    ml = RooRealVar("ml", "mean landau", 5., -20, 20)
    sl = RooRealVar("sl", "sigma landau", 1, 0.1, 10)
    landau = RooLandau("lx", "lx", t, ml, sl)
    mg = RooRealVar("mg", "mg", 0)
    sg = RooRealVar("sg", "sg", 2, 0.1, 10)
    gauss = RooGaussian("gauss", "gauss", t, mg, sg)

    print ">>> construct convolution pdf..."
    # Set #bins to be used for FFT sampling to 10000
    t.setBins(10000, "cache")

    # Construct landau (x) gauss
    convolution = RooFFTConvPdf("lxg", "landau (X) gauss", t, landau, gauss)

    print ">>> sample, fit and plot convoluted pdf..."
    data = convolution.generate(RooArgSet(t), 10000)  # RooDataSet
    convolution.fitTo(data)

    print "\n>>> plot everything..."
    frame1 = t.frame(Title("landau #otimes gauss convolution"))  # RooPlot
    data.plotOn(frame1, Binning(50), Name("data"))
    convolution.plotOn(frame1, Name("lxg"))
    gauss.plotOn(frame1, LineStyle(kDashed), LineColor(kRed), Name("gauss"))
    landau.plotOn(frame1, LineStyle(kDashed), LineColor(kGreen),
                  Name("landau"))

    print "\n>>> draw pfds and fits on canvas..."
    canvas = TCanvas("canvas", "canvas", 100, 100, 800, 600)
    legend = TLegend(0.6, 0.8, 0.8, 0.6)
    legend.SetTextSize(0.032)
    legend.SetBorderSize(0)
    legend.SetFillStyle(0)
    gPad.SetLeftMargin(0.15)
    gPad.SetRightMargin(0.02)
    frame1.GetYaxis().SetLabelOffset(0.008)
    frame1.GetYaxis().SetTitleOffset(1.5)
    frame1.GetYaxis().SetTitleSize(0.045)
    frame1.GetXaxis().SetTitleSize(0.045)
    frame1.Draw()
    legend.AddEntry("data", "data", 'LEP')
    legend.AddEntry("lxg", "convolution", 'L')
    legend.AddEntry("landau", "landau", 'L')
    legend.AddEntry("gauss", "gauss", 'L')
    legend.Draw()
    canvas.SaveAs("rooFit208.png")
def fitLandauGaus(hist, full = False):

    ## c1 = ROOT.TCanvas()
    ## c1.Divide(2)
    ## c1.cd(1)
    ## hist.Draw()
    neg_landau = False
    if hist.GetMean() < 0.:
        neg_landau = True
    if neg_landau:
        hist = turnHisto(hist)

    hist.Rebin(2)
    hist.SetTitle('')
    hist.SetName('hSignal')
    ## c1.cd(2)
    ## hist.Draw('hist')
    ## c1.SaveAs('foobar.pdf')

    ### #if neg_landau:
    ### #    func = ROOT.TF1('my_landau','[0] * TMath::Landau(-x,[1],[2])', hist.GetXaxis().GetXmin(), hist.GetXaxis().GetXmax())
    ### #    func.SetParameters(1, hist.GetMean(), hist.GetRMS() )
    ### #else:
    ### func = ROOT.TF1('my_landau','[0] * TMath::Landau(x,[1],[2])', hist.GetXaxis().GetXmin(), hist.GetXaxis().GetXmax())
    ### func.SetParameters(1, hist.GetMean(), hist.GetRMS() )

    ### hist.Fit('my_landau','q')
    ### fit_res = []
    ### fit_res.append(func.GetParameter(0) if not neg_landau else     func.GetParameter(0))
    ### fit_res.append(func.GetParameter(1) if not neg_landau else -1.*func.GetParameter(1))
    ### fit_res.append(func.GetParameter(2) if not neg_landau else     func.GetParameter(2))
    ### return hist, fit_res

## ROOFIT VERSION

    xmin = hist.GetXaxis().GetXmin()
    xmax = hist.GetXaxis().GetXmax()
    mean = hist.GetMean()
    mp = hist.GetXaxis().GetBinCenter(hist.GetMaximumBin())
    rms = hist.GetRMS()
    flandau = ROOT.TF1('flandau','landau',mp-20,mp+40)
    flandau.SetLineWidth(1)
    flandau.SetLineColor(ROOT.kBlue)
    hist2 = hist.Clone(hist.GetName()+'_2')
    hist2.Scale(1./hist2.GetBinContent(hist2.GetMaximumBin()))
    hist2.Fit(flandau,'Q','',mp-20,mp+40)

    flandau2 = flandau.Clone('flandau2')
    flandau2.SetRange(0,500)
    flandau2.SetLineStyle(2)

    for i in range(flandau.GetNpar()):
        flandau2.SetParLimits(i,flandau.GetParameter(i),flandau.GetParameter(i))
    hist2.Fit(flandau2,'Q+')#,'same',mp-20,mp+40)
    for i in range(flandau.GetNpar()):
        hist2.GetFunction('flandau2').SetParameter(i,flandau.GetParameter(i))

    for i in range(flandau.GetNpar()):
        print flandau.GetParameter(i),flandau2.GetParameter(i)

    x   = RooRealVar('x', 'signal / adc', 0,500)
    x.setRange("signal",mp - 40, mp+90)
    x.setRange("draw",0,500)
    ral = RooArgList(x)
    dh  = RooDataHist('dh', 'dh', ral, RooFit.Import(hist))
    
    
    if full: 
        ml     = RooRealVar('ml', 'mean landau' , mp, mp-20., mp+30)
        sl     = RooRealVar('sl', 'sigma landau', 10, 1., 25.)
    else:
        ml     = RooRealVar('ml', 'mean landau' , mean, mean-40., mean)
        sl     = RooRealVar('sl', 'sigma landau', 10., 6., 14.)
    landau = RooLandau ('lx', 'lx', x, ml, sl)
    
    mean = 0
    if full: 
        mg     = RooRealVar ('mg', 'mean gaus' , 0,0,0)
        sg     = RooRealVar ('sg', 'sigma gaus', flandau.GetParameter(2), 0.1, 30.)
    else:
        mg     = RooRealVar ('mg', 'mean gaus' , 0,0,0) #mean, mean-30.,  mean+30.)
        sg     = RooRealVar ('sg', 'sigma gaus', 2., 0.1, 20.)
    gaus   = RooGaussian('gx', 'gx', x, mg, sg)
    
    x.setBins(1000,'cache')
    
    ## Construct landau (x) gauss
    lxg = RooFFTConvPdf('lxg','landau (x) gaus', x, landau, gaus)
    lxg.fitTo(dh,RooFit.Range("signal"))
    #,RooFit.Normalization(ROOT.RooAbsReal.NumEvent,1))
    a = lxg.getParameters(dh)

    print 'fit par0                                     %+6.1f'%flandau.GetParameter(0)
    print 'fit par1                                     %+6.1f'%flandau.GetParameter(1)
    print 'fit par2                                     %+6.1f'%flandau.GetParameter(2)
    print 'mp                                           %+6.1f'%mp
    print 'rms                                          %+6.1f'%rms
    print 'lxg.getParameters(dh).getRealValue(\'ml\'):  %+6.1f'% a.getRealValue('ml')
    print 'lxg.getParameters(dh).getRealValue(\'sl\'):  %+6.1f'% a.getRealValue('sl')
    print 'lxg.getParameters(dh).getRealValue(\'sg\'):  %+6.1f'% a.getRealValue('sg')

    frame = x.frame(RooFit.Title('landau (x) gauss convolution'),RooFit.Range("draw"))
    #,RooFit.Normalization(ROOT.RooAbsReal.NumEvent,1))
    dh.plotOn(frame,RooFit.Range("draw"))
    #,RooFit.Normalization(1./dh.numEntries(),ROOT.RooAbsReal.Raw))
    lxg.plotOn(frame,RooFit.LineColor(ROOT.kRed),RooFit.Range("draw"))
    #,RooFit.Normalization(1,ROOT.RooAbsReal.Raw))
    #lxg.plotOn(frame,RooFit.LineColor(ROOT.kBlue),RooFit.Range("signal"),RooFit.Components('lx,gx'))
    
    # c = ROOT.TCanvas('lg_convolution','landau (x) gaus', 600, 600)
    # c.Divide(2)
    # c.cd(1)
    # hist.Draw()
    # c.cd(2)
    # ROOT.gPad.SetLeftMargin(0.15)
    # frame.GetYaxis().SetTitleOffset(1.4)
    # frame.Draw()
    # c.SaveAs('histograms/outputhisto'+hist.GetName().split('pz')[1]+'.pdf')
    return dh, copy.deepcopy(a), copy.deepcopy(frame),copy.deepcopy(hist2)