#Longo-Sestili formula
  #http://arxiv.org/pdf/hep-ex/0001020.pdf
fit = TF1("fit","[0]*(pow([2]*x,[1]-1)*[2]*exp(-[2]*(x))/TMath::Gamma([1]))")
fit.SetParName(0,"A") #normalization factor
fit.SetParName(1,"#alpha")
fit.SetParName(2,"#beta")

gStyle.SetOptStat("emr");

c1 = TCanvas("c1","c1",1000,1000)
c1.Divide(3,2)
c1.cd(1)
draw_1histogram(ma.h_ptGen, "p_{T}^{gen} [GeV]","")
c1.cd(2)
draw_1histogram(ma.h_pdgGen, "PDG code","")
c1.cd(4)
ma.h_cellEnergy.Rebin(2)
draw_1histogram(ma.h_cellEnergy, "Total cell energy [GeV]","")
if (ma.h_cellEnergy.GetEntries()>50):
    ma.h_cellEnergy.Fit("gaus")
c1.cd(5)
draw_2histograms(ma.h_longProfile_particle, ma.h_longProfile, "Longitudinal distance/X0", "Energy [GeV]", "Particle dir.", "Hits in 1st layer")
fit.SetParameters(100, 8.15/1.15,1.0/1.15);
ma.h_longProfile.Fit("fit")
gPad.SetLogy(1)
c1.cd(6)
draw_2histograms(ma.h_radialProfile_particle, ma.h_radialProfile, "Radial distance/X0", "Energy [GeV]", "Particle dir.", "Hits in 1st layer")

raw_input("Press ENTER to exit")
#c1.SaveAs("plots_"+PARTICLE+str(ENERGY)+".gif")
energy = calo_init.energies[0]
filename = calo_init.filenamesIn[0]
if len(calo_init.filenamesIn) > 1:
    print "WARNING: analysis of the first input file, ignoring the rest"

ma = CellAnalysis(energy, SF)
ma.loop(filename, calo_init.verbose)
print "Mean cell energy: ", ma.h_cellEnergy.GetMean()
print "Mean cell Id: ", ma.h_cellId.GetMean()
print "Cell Id underflow: ",ma.h_cellId.GetBinContent(0)
print "Cell Id overflow: ",ma.h_cellId.GetBinContent(ma.h_cellId.GetNbinsX()+1)

c1 = TCanvas("c1","c1",1000,1000)
c1.Divide(2,2)
c1.cd(1)
draw_2histograms(ma.h_cellEnergy, ma.h_cellEnergy_check,"Energy per cell","", "FCCSW cells", "Hits in cells")
ma.h_cellEnergy.Rebin(2)
ma.h_cellEnergy_check.Rebin(2)
ma.h_cellEnergy.Fit("gaus")
c1.cd(2)
draw_2histograms(ma.h_ene_r, ma.h_ene_r_check,"rCell [mm]","Energy per bin [GeV]", "FCCSW cells", "Hits in cells")
gPad.SetLogy(1)
print "Original: r bins: ", ma.h_ene_r_check.GetNbinsX(), " underflow ", ma.h_ene_r_check.GetBinContent(0), " overflow ", ma.h_ene_r_check.GetBinContent( ma.h_ene_r_check.GetNbinsX()+1 ), " integral ", ma.h_ene_r_check.Integral()
print "New: r bins: ", ma.h_ene_r.GetNbinsX(), " underflow ", ma.h_ene_r.GetBinContent(0), " overflow ", ma.h_ene_r.GetBinContent( ma.h_ene_r.GetNbinsX()+1 ), " integral ", ma.h_ene_r.Integral()
c1.cd(3)
draw_2histograms(ma.h_ene_phi, ma.h_ene_phi_check,"#phi","Energy per bin [GeV]", "FCCSW cells", "Hits in cells")
gPad.SetLogy(1)
print "Original: phi bins: ", ma.h_ene_phi_check.GetNbinsX(), " underflow ", ma.h_ene_phi_check.GetBinContent(0), " overflow ", ma.h_ene_phi_check.GetBinContent( ma.h_ene_phi_check.GetNbinsX()+1 ), " integral ", ma.h_ene_phi_check.Integral()
print "New: phi bins: ", ma.h_ene_phi.GetNbinsX(), " underflow ", ma.h_ene_phi.GetBinContent(0), " overflow ", ma.h_ene_phi.GetBinContent( ma.h_ene_phi.GetNbinsX()+1 ), " integral ", ma.h_ene_phi.Integral()
c1.cd(4)
draw_2histograms(ma.h_ene_eta, ma.h_ene_eta_check,"#eta","Energy per bin [GeV]", "FCCSW cells", "Hits in cells")
filename = calo_init.filenamesIn[0]
if len(calo_init.filenamesIn) > 1:
    print "WARNING: analysis of the first input file, ignoring the rest"

ma = CellAnalysis(energy, SF)
ma.loop(filename, calo_init.verbose)
print "Mean cell energy: ", ma.h_cellEnergy.GetMean()
print "Mean cell Id: ", ma.h_cellId.GetMean()
print "Cell Id underflow: ", ma.h_cellId.GetBinContent(0)
print "Cell Id overflow: ", ma.h_cellId.GetBinContent(ma.h_cellId.GetNbinsX() +
                                                      1)

c1 = TCanvas("c1", "c1", 1000, 1000)
c1.Divide(2, 2)
c1.cd(1)
draw_2histograms(ma.h_cellEnergy, ma.h_cellEnergy_check, "Energy per cell", "",
                 "FCCSW cells", "Hits in cells")
ma.h_cellEnergy.Rebin(2)
ma.h_cellEnergy_check.Rebin(2)
ma.h_cellEnergy.Fit("gaus")
c1.cd(2)
draw_2histograms(ma.h_ene_r, ma.h_ene_r_check, "rCell [mm]",
                 "Energy per bin [GeV]", "FCCSW cells", "Hits in cells")
gPad.SetLogy(1)
print "Original: r bins: ", ma.h_ene_r_check.GetNbinsX(
), " underflow ", ma.h_ene_r_check.GetBinContent(
    0), " overflow ", ma.h_ene_r_check.GetBinContent(
        ma.h_ene_r_check.GetNbinsX() +
        1), " integral ", ma.h_ene_r_check.Integral()
print "New: r bins: ", ma.h_ene_r.GetNbinsX(
), " underflow ", ma.h_ene_r.GetBinContent(
    0), " overflow ", ma.h_ene_r.GetBinContent(
gStyle.SetOptStat("emr");

c1 = TCanvas("c1","c1",1000,1000)
c1.Divide(3,2)
c1.cd(1)
draw_1histogram(ma.h_ptGen, "p_{T}^{gen} [GeV]","")
c1.cd(2)
draw_1histogram(ma.h_pdgGen, "PDG code","")
c1.cd(4)
ma.h_cellEnergy.Rebin(2)
draw_1histogram(ma.h_cellEnergy, "Total cell energy [GeV]","")
if (ma.h_cellEnergy.GetEntries()>50):
    ma.h_cellEnergy.Fit("gaus")
c1.cd(5)
draw_2histograms(ma.h_longProfile_particle, ma.h_longProfile, "Longitudinal distance/X0", "Energy [GeV]", "Particle dir.", "Hits in 1st layer")
fit.SetParameters(100, 8.15/1.15,1.0/1.15);
ma.h_longProfile.Fit("fit")
gPad.SetLogy(1)
c1.cd(6)
draw_2histograms(ma.h_radialProfile_particle, ma.h_radialProfile, "Radial distance/X0", "Energy [GeV]", "Particle dir.", "Hits in 1st layer")

fileOut = TFile("profiles_e"+str(int(energy))+"GeV_eta1.root","RECREATE")
fileOut.cd()
ma.h_ptGen.Write()
ma.h_pdgGen.Write()
ma.h_cellEnergy.Write()
ma.h_longProfile_particle.Write()
ma.h_longProfile.Write()
ma.h_radialProfile_particle.Write()
ma.h_radialProfile.Write()