class  MyFitFun1 {
public:
  double Evaluate(double *xPtr, double *parPtr) {
    double fitVal = 0.0;
    double xVal = *xPtr;


    double mag1   = parPtr[0];
    double center1 = parPtr[1];
    double gamma1  = parPtr[2];
    double bw1 = mag1/(pow(xVal*xVal - center1*center1,2) + center1*center1*gamma1*gamma1);

    double mag2   = parPtr[3];
    double center2 = parPtr[4];
    double gamma2  = parPtr[5];
    double bw2 = mag2/(pow(xVal*xVal - center2*center2,2) + center2*center2*gamma2*gamma2);

    double bg = fabs(parPtr[6]) + parPtr[7]*xVal + parPtr[8]*pow(xVal,2);
    fitVal =  bw1 + 0*bw2 + bg;

    return fitVal;
  }
};

class  MyFitFun2 {
public:
  double Evaluate(double *xPtr, double *parPtr) {
    double fitVal = 0.0;
    double xVal = *xPtr;


    double mag1   = parPtr[0];
    double center1 = parPtr[1];
    double gamma1  = parPtr[2];
    double bw1 = mag1/(pow(xVal*xVal - center1*center1,2) + center1*center1*gamma1*gamma1);

    double mag2   = parPtr[3];
    double center2 = parPtr[4];
    double gamma2  = parPtr[5];
    double bw2 = mag2/(pow(xVal*xVal - center2*center2,2) + center2*center2*gamma2*gamma2);

    double mag3   = fabs(parPtr[6]);
    double center3 = fabs(parPtr[7]);
    double gamma3  = fabs(parPtr[8]);
    double bw3 = mag3/(pow(xVal*xVal - center3*center3,2) + center3*center3*gamma3*gamma3);

    double bg = fabs(parPtr[9]) + parPtr[10]*xVal + 0*parPtr[11]*pow(xVal,2);
    fitVal =  bw1 + 0*bw2 + 0*bw3 + bg;
 
    return fitVal;
  }
};


class  MyFitFun3 {
public:
  double Evaluate(double *xPtr, double *parPtr) {
    double fitVal = 0.0;
    double xVal = *xPtr;


    double mag1   = parPtr[0];
    double center1 = parPtr[1];
    double gamma1  = parPtr[2];
    double bw1 = mag1/(pow(xVal*xVal - center1*center1,2) + center1*center1*gamma1*gamma1);

    double mag2   = parPtr[3];
    double center2 = parPtr[4];
    double gamma2  = parPtr[5];
    double bw2 = mag2/(pow(xVal*xVal - center2*center2,2) + center2*center2*gamma2*gamma2);

    //    double mag3   = fabs(parPtr[6]);
    //double center3 = fabs(parPtr[7]);
    //double gamma3  = fabs(parPtr[8]);
    //double bw3 = mag3/(pow(xVal*xVal - center3*center3,2) + center3*center3*gamma3*gamma3);

    double bg = fabs(parPtr[6]) + parPtr[7]*xVal + 0*parPtr[8]*pow(xVal,2);
    fitVal =  bw1 + bw2 + bg;
 
    return fitVal;
  }
};




void makePics(int iShow, int pdgLim){

  gROOT->Reset();
  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(0);

  TFile *fFF = new TFile("./outFitToRun/ff.root");
  TFile *fA  = new TFile("./C_V1.root");


  //TFile *fT  = new TFile("./AB_V1_mcThrownStage3.root");
  TFile *fT  = new TFile("./C_V1_mcThrownStage3.root");
  TH1D *hT; fT->GetObject("hMassKpKmPi0Course3",hT);

  TFile *fS  = new TFile("./C_V1_mcSeenStage3.root");
  TH1D *hS; fS->GetObject("hMassKpKmPi0Course3",hS);
  hS->Sumw2();
  hT->Sumw2();
  TH1D * hEff = (TH1D *) hT->Clone("hEff");
  hEff->Divide(hS,hT);
  
  if (iShow == 91) hT->Draw();
  if (iShow == 92) hS->Draw();
  if (iShow == 93) hEff->Draw("e");
  //return;

  TH1D *hReal1; fA->GetObject("hMassKpKmPi0Course1",hReal1);
  TH1D *hReal3; fA->GetObject("hMassKpKmPi0Course3",hReal3);



  TH2D *hFitFrac; fFF->GetObject("hFitFrac",hFitFrac);

  //TH1D * hJ0 = (TH1D*)hFitFrac->ProjectionX("hJ0",1,2);
  //TH1D * hJ1 = (TH1D*)hFitFrac->ProjectionX("hJ1",9,17);
  //TH1D * hJ2 = (TH1D*)hFitFrac->ProjectionX("hJ2",6,8);

  TH1D * hJ0 = (TH1D*)hFitFrac->ProjectionX("hJ0",1,2);
  TH1D * hJ1 = (TH1D*)hFitFrac->ProjectionX("hJ1",3,11);
  TH1D * hJ2 = (TH1D*)hFitFrac->ProjectionX("hJ2",14,16);

  //TH1D * hJ2 = (TH1D*)hFitFrac->ProjectionX("hJ2",18,20);
  //TH1D * hJ1 = (TH1D*)hFitFrac->ProjectionX("hJ1",12,14);

  //TH1D * hJ1 = (TH1D*)hFitFrac->ProjectionX("hJ1",2,4);
  //TH1D * hJ1 = (TH1D*)hFitFrac->ProjectionX("hJ1",5,10);

  hReal3->Divide(hReal3,hEff);

  TH1D * hRealJ0 = (TH1D *)hReal3->Clone("hRealJ0");
  TH1D * hRealJ1 = (TH1D *) hReal3->Clone("hRealJ1");
  TH1D * hRealJ2 = (TH1D *) hReal3->Clone("hRealJ2");

  
  hRealJ0->Multiply(hRealJ0,hJ0);
  hRealJ0->SetLineWidth(2);
  hRealJ0->SetLineColor(2);
  hRealJ0->SetFillStyle(1001);
  hRealJ0->SetFillColor(2);

  hRealJ1->Multiply(hRealJ1,hJ1);
  hRealJ1->SetLineWidth(2);
  hRealJ1->SetLineColor(3);
  hRealJ1->SetFillStyle(1001);
  hRealJ1->SetFillColor(3);

  hRealJ2->Multiply(hRealJ2,hJ2);
  hRealJ2->SetLineWidth(2);
  hRealJ2->SetLineColor(4);
  hRealJ2->SetFillStyle(1001);
  hRealJ2->SetFillColor(4);

  MyFitFun1 * fptr1 = new MyFitFun1();
  int nPar = 9;
  TF1 *f1 = new TF1("f1",fptr1,&MyFitFun1::Evaluate,0.0,4.0,nPar,
		    "MyFitFun1","Evaluate");

  MyFitFun2 * fptr2 = new MyFitFun2();
  nPar = 12;

  TF1 *f2 = new TF1("f2",fptr2,&MyFitFun2::Evaluate,0.0,4.0,nPar,
		    "MyFitFun2","Evaluate");

  MyFitFun3 * fptr3 = new MyFitFun3();
  nPar = 9;

  TF1 *f3 = new TF1("f3",fptr3,&MyFitFun3::Evaluate,0.0,4.0,nPar,
		    "MyFitFun3","Evaluate");


  //TF1 *fg1 = new TF1("fg1","gaus+pol1",0.0,3.0);
  //TF1 *fg2a0 = new TF1("fg2a0","gaus+pol1",0.0,3.0);

  f1->SetParameter(0,20);
  f1->SetParameter(1,1.294);
  f1->SetParameter(2,0.055);
  f1->SetParameter(3,30);
  f1->SetParameter(4,1.4139);
  f1->SetParameter(5,0.0501);

  f1->SetParLimits(1,1.275,1.32);
  f1->SetParLimits(4,1.35,1.45);
  f1->SetParLimits(5,0.0501 - 0.020,0.0501 + 0.020);

  f2->SetParameter(0,30);
  //f2->SetParameter(1,1.2819);
  f2->SetParameter(1,1.276);
  f2->SetParameter(2,0.028);
  f2->SetParameter(3,12);
  f2->SetParameter(4,1.4263);
  f2->SetParameter(5,0.0545);
  f2->SetParameter(6,30);

  f2->SetParLimits(1,1.2819-0.02,1.2819+0.02);
  //f2->SetParLimits(2,0.0227-0.011,0.0227+0.011);
  f2->SetParLimits(2,0.0227-0.111,0.0227+0.111);
  f2->SetParLimits(4,1.4263-0.09,1.4263+0.09);
  f2->SetParLimits(5,0.0545-0.026,0.0545+0.026);
  //ParLimit

  f3->SetParameter(0,30);
  f3->SetParameter(1,1.318);
  f3->SetParameter(2,0.11);
  f3->SetParameter(3,30);
  f3->SetParameter(4,1.43);
  f3->SetParameter(5,0.05);

  f3->SetParLimits(1,1.3176,1.3188);
  f3->SetParLimits(2,0.103,0.120);
  f3->SetParLimits(4,1.42,1.44);
  f3->SetParLimits(5,0.02,0.08);



  if (pdgLim == 1){
    f1->SetParLimits(1,1.294-0.004,1.294+0.004);
    f1->SetParLimits(2,0.055-0.005,0.055+0.005);
    f1->SetParLimits(4,1.4139-0.0017,1.4139+0.0017);
    f1->SetParLimits(5,0.048-004,0.048+004);
  
    f2->SetParLimits(1,1.2819-0.0005,1.2819+0.0005);
    f2->SetParLimits(2,0.0227-0.0011,0.0227+0.0011);
    f2->SetParLimits(4,1.4263-0.0009,1.4263+0.0009);
    f2->SetParLimits(5,0.0545-0.0026,0.0545+0.0026);
  }
  //fg1->SetParameter(3,0);
  //fg1->SetParameter(4,0);

  f1->SetLineColor(2);
  f2->SetLineColor(3);

  double fLow = 1.22;
  double fHigh = 1.45;

  TH1D * hRealJ0NoFit = (TH1D *) hRealJ0->Clone("hRealJ0NoFit");

  //hRealJ0->SetBinContent(10,0);
  //hRealJ0->SetBinError(10,0);
  //hRealJ1->SetBinContent(10,0);
  //hRealJ1->SetBinError(10,0);

  hRealJ0->Fit("f1","RB","",fLow,fHigh);
  
  fLow = 1.22;
  fHigh = 1.4;
  //  return;
  hRealJ1->Fit("f2","RB","",fLow,fHigh);

  fLow = 1.22;
  fHigh = 1.5;
  hRealJ2->Fit("f3","RB","",fLow,fHigh);


  hReal3->GetXaxis()->SetRangeUser(1.22,1.45);

  cout<<"----------"<<endl;
  double cJ0 = 1000*f1->GetParameter(1);
  double cJ0Err = 1000*f1->GetParError(1);
  cout<<"cJ0 = "<<cJ0<<" +/- "<<cJ0Err<<endl;

  double wJ0 = 1000*f1->GetParameter(2);
  double wJ0Err = 1000*f1->GetParError(2);
  cout<<"wJ0 = "<<wJ0<<" +/- "<<wJ0Err<<endl;

  cout<<"----------"<<endl;
  double cJ1 = 1000*f2->GetParameter(1);
  double cJ1Err = 1000*f2->GetParError(1);
  cout<<"cJ1 = "<<cJ1<<" +/- "<<cJ1Err<<endl;

  double wJ1 = 1000*f2->GetParameter(2);
  double wJ1Err = 1000*f2->GetParError(2);
  cout<<"wJ1 = "<<wJ1<<" +/- "<<wJ1Err<<endl;

  cout<<"----------"<<endl;
  double cJ2 = 1000*f3->GetParameter(1);
  double cJ2Err = 1000*f3->GetParError(1);
  cout<<"cJ2 = "<<cJ2<<" +/- "<<cJ2Err<<endl;

  double wJ2 = 1000*f3->GetParameter(2);
  double wJ2Err = 1000*f3->GetParError(2);
  cout<<"wJ2 = "<<wJ2<<" +/- "<<wJ2Err<<endl;

  cout<<"----------"<<endl;
  double cJ2b = 1000*f3->GetParameter(4);
  double cJ2bErr = 1000*f3->GetParError(4);
  cout<<"cJ2b = "<<cJ2b<<" +/- "<<cJ2bErr<<endl;

  double wJ2b = 1000*f3->GetParameter(5);
  double wJ2bErr = 1000*f3->GetParError(5);
  cout<<"wJ2b = "<<wJ2b<<" +/- "<<wJ2bErr<<endl;

  hReal3->SetMarkerStyle(21);
  hReal3->GetXaxis()->SetTitle("Mass(K^{+}K^{-}#pi^{0})/GeV");
  hReal3->GetYaxis()->SetTitle("Y/#epsilon");

  hReal1->SetMarkerStyle(20);
  hReal1->GetXaxis()->SetTitle("Mass(K^{+}K^{-}#pi^{0})/GeV");
  hReal1->GetYaxis()->SetTitle("Counts");

  hRealJ0->GetXaxis()->SetTitle("Mass(K^{+}K^{-}#pi^{0})/GeV");
  hRealJ0->GetYaxis()->SetTitle("Counts");

  //hReal3->GetXaxis()->SetRangeUser(1.24,1.31);
  hReal3->SetLineWidth(2);
  hReal3->SetMinimum(0);
  if (iShow == 0) {
    hReal3->Draw("e1");
  }

  hRealJ0->GetYaxis()->SetTitle("");
  if (iShow == 1) {
    hReal3->Draw("e1");
    hRealJ0->Draw("same");
    hRealJ1->Draw("same");
    hRealJ2->Draw("same");

    Double_t xl1=0.63, yl1 = 0.73, xl2=xl1+0.23, yl2=yl1+0.15;
    TLegend *legend = new TLegend(xl1,yl1,xl2,yl2);
    legend->SetFillColor(0);
    legend->SetLineColor(0);
    legend->SetTextSize(0.04);
    legend->AddEntry(hRealJ0,"J=0","f");
    legend->AddEntry(hRealJ1,"J=1","f");
    legend->AddEntry(hRealJ2,"J=2","f");

    legend->Draw();

  }
  if (iShow == 11) {
    hReal3->Draw("e1");
    hRealJ0->Draw("same");
    //hRealJ1->Draw("same");

    Double_t xl1=0.13, yl1 = 0.73+0.06, xl2=xl1+0.23, yl2=yl1+0.06;
    TLegend *legend = new TLegend(xl1,yl1,xl2,yl2);
    legend->SetFillColor(0);
    legend->SetLineColor(0);
    legend->SetTextSize(0.04);
    legend->AddEntry(hRealJ0,"J=0^{-}","f");
    //legend->AddEntry(hRealJ1,"J=1","f");

    legend->Draw();

  }

  Double_t xl1=0.12, yl1 = 0.8, xl2=xl1+0.23, yl2=yl1+0.06;
  TLegend *legend = new TLegend(xl1,yl1,xl2,yl2);
  legend->SetFillColor(0);
  legend->SetLineColor(0);
  legend->SetTextSize(0.04);
 
  char  tString1[120];
  char  tString2[120];
  char  tString3[120];
  char  tString4[120];

  char  tString5[120];
  char  tString6[120];
  char  tString7[120];
  char  tString8[120];

  bool noFit = false;
  hRealJ0->SetMaximum(80000);
  hRealJ1->SetMaximum(80000);
  if (iShow == 2) {
    if (noFit == true) {
      hRealJ0NoFit->SetMinimum(0);
      hRealJ0NoFit->Draw("e");
      legend->AddEntry(hRealJ0NoFit,"J=0","f");
      legend->Draw();
    }else{
      hRealJ0->SetMinimum(0);
      hRealJ0->Draw("e");
      legend->AddEntry(hRealJ0,"J=0","f");
      legend->Draw();
      sprintf(tString1,"            center = %6.2f +/- %4.2f",cJ0,cJ0Err);
      sprintf(tString2,"PDG: #eta(1295) = 1294      +/- 4");
      sprintf(tString3,"            width  = %6.2f +/- %4.2f",wJ0,wJ0Err);
      sprintf(tString4,"PDG: #eta(1295) =  55      +/- 5");
      TLatex *t1 = new TLatex();
      TLatex *t2 = new TLatex();
      TLatex *t3 = new TLatex();
      TLatex *t4 = new TLatex();
      t1->SetNDC();
      t1->SetTextSize(0.05);
      t2->SetNDC();
      t2->SetTextSize(0.05);
      t3->SetNDC();
      t3->SetTextSize(0.05);
      t4->SetNDC();
      t4->SetTextSize(0.05);
      double xLocationFraction = 0.4;
      double yLocationFraction = 0.85;
      t1->DrawLatex(xLocationFraction,yLocationFraction,tString1);
      t2->DrawLatex(xLocationFraction,yLocationFraction-0.05,tString2);
      
      t3->DrawLatex(xLocationFraction,yLocationFraction-0.15,tString3);
      t4->DrawLatex(xLocationFraction,yLocationFraction-0.2,tString4);
    }
  }
  if (iShow == 3) {
    hRealJ1->SetMinimum(0);
    hRealJ1->Draw("e");
    legend->AddEntry(hRealJ1,"J=1","f");
    legend->Draw();

    sprintf(tString1,"            center = %6.2f +/- %4.2f",cJ1,cJ1Err);
    sprintf(tString2,"PDG: f_{1}(1285) = 1281.9   +/-  0.5");
    sprintf(tString3,"            width  = %6.2f +/- %4.2f",wJ1,wJ1Err);
    sprintf(tString4,"PDG: f_{1}(1285) =  22.7   +/-  1.1");
    TLatex *t1 = new TLatex();
    TLatex *t2 = new TLatex();
    TLatex *t3 = new TLatex();
    TLatex *t4 = new TLatex();
    t1->SetNDC();
    t1->SetTextSize(0.05);
    t2->SetNDC();
    t2->SetTextSize(0.05);
    t3->SetNDC();
    t3->SetTextSize(0.05);
    t4->SetNDC();
    t4->SetTextSize(0.05);
    double xLocationFraction = 0.3;
    double yLocationFraction = 0.85;
    t1->DrawLatex(xLocationFraction,yLocationFraction,tString1);
    t2->DrawLatex(xLocationFraction,yLocationFraction-0.05,tString2);

    t3->DrawLatex(xLocationFraction,yLocationFraction-0.15,tString3);
    t4->DrawLatex(xLocationFraction,yLocationFraction-0.2,tString4);

  }

  if (iShow == 4) {
    hRealJ2->SetMinimum(0);
    hRealJ2->SetMaximum(50000);
    hRealJ2->Draw("e");
    legend->AddEntry(hRealJ2,"J=2","f");
    legend->Draw();

    sprintf(tString1,"            center  = %6.2f +/- %4.2f",cJ2,cJ2Err);
    sprintf(tString2,"PDG: a_{2}(1320) = 1318.2   +/-  0.6");
    sprintf(tString3,"            width   = %6.2f +/- %4.2f",wJ2,wJ2Err);
    sprintf(tString4,"PDG: a_{2}(1320) = 105.0^{+1.7}_{-1.9}");
    TLatex *t1 = new TLatex();
    TLatex *t2 = new TLatex();
    TLatex *t3 = new TLatex();
    TLatex *t4 = new TLatex();
    t1->SetNDC();
    t1->SetTextSize(0.05);
    t2->SetNDC();
    t2->SetTextSize(0.05);
    t3->SetNDC();
    t3->SetTextSize(0.05);
    t4->SetNDC();
    t4->SetTextSize(0.05);
    double xLocationFraction = 0.1;
    double yLocationFraction = 0.55;
    t1->DrawLatex(xLocationFraction,yLocationFraction,tString1);
    t2->DrawLatex(xLocationFraction,yLocationFraction-0.05,tString2);

    t3->DrawLatex(xLocationFraction,yLocationFraction-0.15,tString3);
    t4->DrawLatex(xLocationFraction,yLocationFraction-0.2,tString4);

    /////////////
    sprintf(tString5,"            center  = %6.2f +/- %4.2f",cJ2b,cJ2bErr);
    sprintf(tString6,"PDG: f_{2}(1430)  = 1430   +/- ?");
    sprintf(tString7,"            width   = %6.2f +/- %4.2f",wJ2b,wJ2bErr);
    sprintf(tString8,"PDG: f_{2}(1430)  =  ?");
    TLatex *t5 = new TLatex();
    TLatex *t6 = new TLatex();
    TLatex *t7 = new TLatex();
    TLatex *t8 = new TLatex();
    t5->SetNDC();
    t5->SetTextSize(0.05);
    t6->SetNDC();
    t6->SetTextSize(0.05);
    t7->SetNDC();
    t7->SetTextSize(0.05);
    t8->SetNDC();
    t8->SetTextSize(0.05);
    xLocationFraction = 0.4;
    yLocationFraction = 0.85;
    t5->DrawLatex(xLocationFraction,yLocationFraction,tString5);
    t6->DrawLatex(xLocationFraction,yLocationFraction-0.05,tString6);

    t7->DrawLatex(xLocationFraction,yLocationFraction-0.15,tString7);
    t8->DrawLatex(xLocationFraction,yLocationFraction-0.2,tString8);

  }

}
