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
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')
## phoScale.setVal(0)
## phoRes.setVal(phoEResMC)
## theory.plotOn(plot)
## phoSmear.plotOn(plot, LineColor(kRed))
## theoryXphoSmear.plotOn(plot, LineColor(kBlack))
## c1 = canvases.next('convolution')
## c1.SetGridx()
## c1.SetGridy()
    ## fit the transformed model to the test data
    tmodel.fitTo(data)
    sFitted.append(phoScale.getVal())
    sFittedErr.append(phoScale.getError())

    ## Display data overlaid with fitted and extrapolated models
    canvases.next('test%d' % i)
    frame = mmgMass.frame(Range(60,120))
    frame.SetTitle('')
    frame.GetXaxis().SetTitle(
        'm_{#mu#mu#gamma} (GeV), E^{#gamma} scaled by %g%%' % s
        )
    data.plotOn(frame)
    # m.plotOn(frame)
    tmodel.plotOn(frame, LineStyle(kDashed), LineColor(kRed))
    tmodel.paramOn(frame)
    frame.Draw()
    canvases.canvases[-1].Update()

## Plot fitted vs true
graphs = []    
graph = TGraphErrors(len(sTest))
for i, (x, y, ey) in enumerate(zip(sTest, sFitted, sFittedErr)):
    graph.SetPoint(i, x, y)
    graph.SetPointError(i, 0, ey)

canvases.next('Injected_vs_Fitted')
graph.SetTitle('Tranform Closure Test')
graph.Draw('ap')
graph.GetYaxis().SetTitle('Fitted Scale (%)')
graph.GetXaxis().SetTitle('Injected Scale (%)')