## phoEResShape2.plotOn(plot, LineColor(kRed))
## plot.Draw()
## llabels.draw()
## plots.append(plot)

## Mass smearing due to photon resolution
phoSmearData = reducedData['mmgMassPhoSmear']
phoSmearData.reduce(RooArgSet(mmgMassShifted))
phoSmearData.merge(data.reduce(RooArgSet(mmgMass, mmMass)))
phoSmearData.Print()
mmgMass.setBins(3)
mmMass.setBins(5)
mmgMassShifted.setBins(40)
phoSmearDataBinned = phoSmearData.binnedClone()
# phoSmear.fitTo(reducedData['mmgMassPhoSmear'], Range(-5, 5), NumCPU(3))
phoSmear.fitTo(phoSmearDataBinned,
               ConditionalObservables(RooArgSet(mmMass, mmgMass)))
canvases.next('mmgMassPhoSmear')#.SetLogy()
mmgMassShifted.SetTitle('m(#mu#mu + reco #gamma) - m(#mu#mu + gen #gamma)')
plot = mmgMassShifted.frame(Range(-5,5)) #Range(-5,5)) #Range(0, 500))
mmgMassShifted.SetTitle(mmgMassShiftedTitle)
plot.SetTitle('"Resolution:" Modeling of Mass Smearing Due to Photon Energy Resolution')
reducedData['mmgMassPhoSmear'].plotOn(plot)
phoSmear.plotOn(plot)
phoSmear.paramOn(plot)
plot.Draw()
llabels.draw()
plots.append(plot)

## ## Plot theory, smearing and smeared theory
## plot = mmgMassShifted.frame(Range(-5, 5))
## plot.SetTitle('Model = Theory * Resolution')
                             'Entry$ % 4 == 0']
        #data = dataset.get(variable=mmgMass, cuts=cutsMod)
        weight.SetTitle('1')
        data = dataset.get(tree=dataTree, variable=mmgMass, weight=weight,
                           cuts = cuts)

        ## Import the data in the workspace
        data.SetName('data_%s' % name)
        w.Import(data)
        ## Also build the PDF's
        ## m = w.factory('KeysPdf::model%d(mmgMass, data%d, NoMirror, 2)' % (i, i))
        ## dataCollection.append(data)
        ## models.append(m)

        ## fit the transformed model to the test data
        tmodel.fitTo(data)
        sFitted.append(phoScale.getVal())
        sFittedErr.append(phoScale.getError())

        labels.append('#Deltas = %.2f #pm %.2f %%' %
                      (phoScale.getVal(), phoScale.getError()))
        latexlabels = Latex(labels, position = (0.25, 0.8))

        ## Scan -log(L) vs photon scale
        sframe = phoScale.frame(Bins(100),
                                Range(sFitted[-1] - 5*sFittedErr[-1],
                                      sFitted[-1] + 5*sFittedErr[-1]))
        nll = RooNLLVar('nll', 'nll', tmodel, data)
        nll.plotOn(sframe, ShiftToZero())
        canvases.next(name + '_nll')
        phoScale.SetTitle('#Deltas')
## plot = phoERes.frame(Range(-1,1))
## reducedData['phoERes'].plotOn(plot)
## phoEResShape.plotOn(plot)
## phoEResShape2.plotOn(plot, LineColor(kRed))
## plot.Draw()
## llabels.draw()
## plots.append(plot)

## Mass smearing due to photon resolution
canvases.next("mmgMassPhoSmear")  # .SetLogy()
mmgMassShifted.SetTitle("m(#mu#mu + reco #gamma) - m(#mu#mu + gen #gamma)")
plot = mmgMassShifted.frame(Range(-5, 5))  # Range(-5,5)) #Range(0, 500))
mmgMassShifted.SetTitle(mmgMassShiftedTitle)
plot.SetTitle('"Resolution:" Modeling of Mass Smearing Due to Photon Energy Resolution')
reducedData["mmgMassPhoSmear"].plotOn(plot)
phoSmear.fitTo(reducedData["mmgMassPhoSmear"], Range(-5, 5), NumCPU(3))
phoSmear.plotOn(plot)
phoSmear.paramOn(plot)
phoSmear2.fitTo(reducedData["mmgMassPhoSmear"], Range(-5, 5), NumCPU(3))
phoSmear2.plotOn(plot, LineColor(kRed), LineStyle(kDashed))
plot.Draw()
llabels.draw()
plots.append(plot)

## Plot theory, smearing and smeared theory
plot = mmgMassShifted.frame(Range(-5, 5))
plot.SetTitle("Model = Theory * Resolution")
phoScale.setVal(0)
phoRes.setVal(phoEResMC)
theory.plotOn(plot)
phoSmear.plotOn(plot, LineColor(kRed))
hist = k.createHistogram('x', 10000)
dh = DataHist('dh', 'dh', ArgList(x), hist)
h = HistPdf('h', 'h', ArgList(t), ArgList(x), dh, 2)

## 5. Plot results
f1 = x.frame()
f1.SetTitle('Training Data')
data1.plotOn(f1)
g.plotOn(f1)
k.plotOn(f1, LineColor(kRed), LineStyle(kDashed))
h.plotOn(f1, LineColor(kBlack), LineStyle(kDashed))
canvases.next('Training_Data')
f1.Draw()

## 6. Fit h1 to data2 to simulate a "measurement" of m2, s2.
h.fitTo(data2)

f2 = x.frame()
f2.SetTitle('Test Data')
data2.plotOn(f2)
h.plotOn(f2)
h.paramOn(f2)
canvases.next('Test_Data')
f2.Draw()

for c in canvases.canvases:
    c.Update()

if __name__ == '__main__':
    import user