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

    double smearVal = 0.00926;

    double mag1   = fabs(parPtr[0]);
    double center1 = parPtr[1];
    double gamma1  = parPtr[2];
    double bw1 = TMath::Voigt(xVal-center1,smearVal,gamma1)*mag1;

    double mag2   = fabs(parPtr[3]);
    double center2 = parPtr[4];
    double gamma2  = parPtr[5];
    double bw2 = TMath::Voigt(xVal-center2,smearVal,gamma2)*mag2;

    double mag3   = fabs(parPtr[6]);
    double center3 = parPtr[7];
    double gamma3  = parPtr[8];
    double bw3 = TMath::Voigt(xVal-center3,smearVal,gamma3)*mag3;

    double bg = fabs(parPtr[9]) + parPtr[10]*xVal;// + parPtr[8]*pow(xVal,2);

    fitVal =  bw1 + bw2 + bw3 + bg;

    return fitVal;
  }
};

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

    double smearVal = 0.00926;

    double mag1   = fabs(parPtr[0]);
    double center1 = parPtr[1];
    double gamma1  = parPtr[2];
    double bw1 = TMath::Voigt(xVal-center1,smearVal,gamma1)*mag1;

    double mag2   = fabs(parPtr[3]);
    double center2 = parPtr[4];
    double gamma2  = parPtr[5];
    double bw2 = TMath::Voigt(xVal-center2,smearVal,gamma2)*mag2;

    double mag3   = fabs(parPtr[6]);
    double center3 = parPtr[7];
    double gamma3  = parPtr[8];
    double bw3 = TMath::Voigt(xVal-center3,smearVal,gamma3)*mag3;

    double mag4   = fabs(parPtr[9]);
    double center4 = parPtr[10];
    double gamma4  = parPtr[11];
    double bw4 = TMath::Voigt(xVal-center4,smearVal,gamma4)*mag4;

    double mag5   = fabs(parPtr[12]);
    double center5 = parPtr[13];
    double gamma5  = parPtr[14];
    double bw5 = TMath::Voigt(xVal-center5,smearVal,gamma5)*mag5;

    double bg = fabs(parPtr[15]) + parPtr[16]*xVal + parPtr[17]*pow(xVal,2);

    fitVal =  bw1 + bw2 + bw3 + bw4 + bw5 + 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 bg = fabs(parPtr[6]) + parPtr[7]*xVal + 0*parPtr[8]*pow(xVal,2);
    fitVal =  bw1 + bw2 + bg;
 
    return fitVal;
  }
};




void makePicsB1B2(int iShow, int pdgLim){

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



  TFile *fFF = new TFile("../outFit/ff.root");

  TFile *fA  = new TFile("/data/dugger/mike/ana/stage3KpKmPi0/Q_E1_V1.root");
  TFile *fT  = new TFile("/data/dugger/mike/ana/stage3KpKmPi0/Q_E1_V1_mcThrownStage3.root");
  TFile *fS  = new TFile("/data/dugger/mike/ana/stage3KpKmPi0/Q_E1_V1_mcSeenStage3.root");

  TH1D *hT; fT->GetObject("hMassKpKmPi0Course3",hT);


  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();

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

  TH1D *h1 = (TH1D *) hReal3->Clone("h1");
  TH1D *h2 = (TH1D *) hReal3->Clone("h2");
  TH1D *h3 = (TH1D *) hReal3->Clone("h3");
  TH1D *h4 = (TH1D *) hReal3->Clone("h4");
  TH1D *h5 = (TH1D *) hReal3->Clone("h5");
  TH1D *h6 = (TH1D *) hReal3->Clone("h6");
  TH1D *h7 = (TH1D *) hReal3->Clone("h7");
  TH1D *h8 = (TH1D *) hReal3->Clone("h8");
  TH1D *h9 = (TH1D *) hReal3->Clone("h9");

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

  int j0BinLow  = 15;
  int j0BinHigh = 15;

  int j1BinLow  = 17;
  int j1BinHigh = 17;

  TH1D * hJ0a = (TH1D*)hFitFrac->ProjectionX("hJ0a",j0BinLow,j0BinHigh);
  TH1D * hJ0b = (TH1D*)hFitFrac->ProjectionX("hJ0b",j0BinLow,j0BinHigh);
  TH1D * hJ0  = (TH1D*)hJ0b->Clone("hJ0");
  for (int iTmp = 1; iTmp<=13; iTmp++){
    double valTmp = hJ0a->GetBinContent(iTmp);
    double errTmp = hJ0a->GetBinError(iTmp);

    hJ0->SetBinContent(iTmp,valTmp); 
    hJ0->SetBinError(iTmp,errTmp);
  }

 
  TH1D * hJ1a = (TH1D*)hFitFrac->ProjectionX("hJ1a",j1BinLow,j1BinHigh);
  TH1D * hJ1b = (TH1D*)hFitFrac->ProjectionX("hJ1b",j1BinLow,j1BinHigh);
  TH1D * hJ1  = (TH1D*)hJ1b->Clone("hJ1");
  for (int iTmp = 1; iTmp<=13; iTmp++){
    double valTmp = hJ1a->GetBinContent(iTmp);
    double errTmp = hJ1a->GetBinError(iTmp);

    hJ1->SetBinContent(iTmp,valTmp);
    hJ1->SetBinError(iTmp,errTmp);
  }

  TH1D * hProj1 = (TH1D*)hFitFrac->ProjectionX("hProj1",1,1);
  TH1D * hProj2 = (TH1D*)hFitFrac->ProjectionX("hProj2",2,2);
  TH1D * hProj3 = (TH1D*)hFitFrac->ProjectionX("hProj3",3,3);
  TH1D * hProj4 = (TH1D*)hFitFrac->ProjectionX("hProj4",4,4);
  TH1D * hProj5 = (TH1D*)hFitFrac->ProjectionX("hProj5",5,5);
  TH1D * hProj6 = (TH1D*)hFitFrac->ProjectionX("hProj6",6,6);
  TH1D * hProj7 = (TH1D*)hFitFrac->ProjectionX("hProj7",7,7);
  TH1D * hProj8 = (TH1D*)hFitFrac->ProjectionX("hProj8",8,8);
  TH1D * hProj9 = (TH1D*)hFitFrac->ProjectionX("hProj9",9,9);


  TH1D * hJ2 = (TH1D*)hFitFrac->ProjectionX("hJ2",1,1);
  //TH1D * hJ2 = (TH1D*)hJ0->Clone("hJ2");
  //hJ2->Add(hJ1);

  hReal3->Divide(hReal3,hEff);

  h1->Divide(hEff);
  h2->Divide(hEff);
  h3->Divide(hEff);
  h4->Divide(hEff);
  h5->Divide(hEff);
  h6->Divide(hEff);
  h7->Divide(hEff);
  h8->Divide(hEff);
  h9->Divide(hEff);

  h1->Multiply(hProj1);
  h2->Multiply(hProj2);
  h3->Multiply(hProj3);
  h4->Multiply(hProj4);
  h5->Multiply(hProj5);
  h6->Multiply(hProj6);
  h7->Multiply(hProj7);
  h8->Multiply(hProj8);
  h9->Multiply(hProj9);

  if (iShow == 99){
    TFile *outFile = new TFile("./hOut.root","RECREATE");
    h1->Write();
    h2->Write();
    h3->Write();
    h4->Write();
    h5->Write();
    h6->Write();
    h7->Write();
    h8->Write();
    h9->Write();
    outFile->Close();
    cout<<"Wrote histograms to hOut.root"<<endl;
    return;
  } 


  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);

  //hRealJ0->SetBinError(19,0);
  //hRealJ0->SetBinError(21,0);
  hRealJ0->SetBinError(28,0);


  //hRealJ1->SetBinError(19,0);
  //hRealJ1->SetBinError(21,0);
  hRealJ1->SetBinError(28,0);


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

  MyFitFun2 * fptr2 = new MyFitFun2();
  nPar = 18;
  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");


  f1->FixParameter(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->SetParameter(6,50);
  f1->SetParameter(7,1.52);
  f1->SetParameter(8,0.0501);

  f1->SetParLimits(1,1.275,1.32);
  f1->SetParLimits(4,1.38,1.45);
  f1->SetParLimits(5,0.02,0.1);

  f1->SetParLimits(7,1.5,1.75);
  f1->SetParLimits(8,0.02,0.15);

  //polynomial
  //f1->FixParameter(9,0.0);
  //f1->FixParameter(10,0.0);



  f2->SetParameter(0,50);
  f2->SetParameter(1,1.2819);
  f2->SetParLimits(1,1.2819-0.01,1.2819+0.01);
  f2->SetParameter(2,0.0227);
  f2->SetParLimits(2,0.0227-0.041,0.0227+0.041);

  //f_1(1285)
  f2->SetParameter(0,26); 
  f2->SetParameter(1,1.2819);
  f2->SetParLimits(1,1.2819-0.0005,1.2819+0.0005);
  if (pdgLim == 0) f2->SetParLimits(1,1.2819-0.01,1.2819+0.01);
  f2->SetParameter(2,0.0227);
  f2->SetParLimits(2,0.0227-0.0011-0.01,0.0227+0.0011+0.01);
  if (pdgLim == 0) f2->SetParLimits(2,0.0227-0.01,0.0227+0.05);

  //f_1(1420)
  f2->SetParameter(3,300); 
  f2->SetParameter(4,1.4263);
  f2->SetParLimits(4,1.4263-0.0009,1.4263+0.0009);
  if (pdgLim == 0) f2->SetParLimits(4,1.4263-0.01,1.4263+0.01);
  f2->SetParameter(5,0.0545);
  f2->SetParLimits(5,0.0545-0.0026,0.0545+0.0026+0.01);
  if (pdgLim == 0) f2->SetParLimits(5,0.0545-0.0026,0.0545+0.0026+0.01);

  //h_1(1415)
  f2->SetParameter(6,100); 
  f2->SetParameter(7,1.409);
  f2->SetParLimits(7,1.400,1.409+0.008);
  if (pdgLim == 0) f2->SetParLimits(7,1.416-0.04,1.416+0.02);
  f2->SetParameter(8,0.078);
  f2->SetParLimits(8,0.078-0.011,0.078+0.011);
  if (pdgLim == 0)  f2->SetParLimits(8,0.078-0.05,0.078+0.05);

  //rho 
  f2->SetParameter(9,100);
  f2->SetParameter(10,1.465);
  f2->SetParLimits(10,1.465 - 0.035,1.465+0.035);
  f2->SetParameter(11,0.4);
  f2->SetParLimits(11,0.4 - 0.06,0.4+0.04);

  //f_1(1510)
  f2->SetParameter(12,5000);
  f2->SetParameter(13,1.518);
  f2->SetParLimits(13,1.518-0.005,1.518+0.005);
  f2->SetParameter(14,0.073);
  f2->SetParLimits(14,0.073-0.025,0.073+0.025);

  //f2->SetParameter(13,1.5);
  //f2->SetParameter(14,0.073);

  //polynomial
  f2->FixParameter(15,0.0);
  f2->FixParameter(16,0.0);
  f2->FixParameter(17,0.0);


  f3->SetParameter(0,30);
  f3->SetParameter(1,1.318);
  f3->SetParameter(2,0.11);
  f3->SetParameter(3,30);
  f3->SetParameter(4,1.51);
  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);


  f1->SetLineColor(2);
  f2->SetLineColor(3);
  if (iShow == 2) f1->SetLineColor(1);  
  if (iShow == 3) f2->SetLineColor(1);  

  double fLow = 1.22;
  double fHigh = 1.5;

  //fLow = 1.25;
  fHigh = 1.49;

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

  TH1D * hF2a = (TH1D *) hRealJ0->Clone("hF2a");
  TH1D * hF2b = (TH1D *) hRealJ0->Clone("hF2b");
  TH1D * hF2c = (TH1D *) hRealJ0->Clone("hF2c");
  TH1D * hF2d = (TH1D *) hRealJ0->Clone("hF2d");
  TH1D * hF2e = (TH1D *) hRealJ0->Clone("hF2e");

  hF2a->SetFillStyle(0);
  hF2b->SetFillStyle(0);
  hF2c->SetFillStyle(0);
  hF2d->SetFillStyle(0);
  hF2e->SetFillStyle(0);

  TH1D * hF1a = (TH1D *) hRealJ0->Clone("hF1a");
  TH1D * hF1b = (TH1D *) hRealJ0->Clone("hF1b");
  TH1D * hF1c = (TH1D *) hRealJ0->Clone("hF1c");

  hF1a->SetFillStyle(0);
  hF1b->SetFillStyle(0);
  hF1c->SetFillStyle(0);

  hRealJ0->Fit("f1","RB","",fLow,fHigh);

  hRealJ1->Fit("f2","RB","",fLow,fHigh);




  hRealJ1->GetXaxis()->SetRangeUser(fLow,fHigh);

  //hRealJ2->Fit("f3","RB","",fLow,fHigh);

  hReal3->GetXaxis()->SetRangeUser(fLow,fHigh);

  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;

  double cJ0b = 1000*f1->GetParameter(4);
  double cJ0bErr = 1000*f1->GetParError(4);
  cout<<"cJ0b = "<<cJ0b<<" +/- "<<cJ0bErr<<endl;

  double wJ0b = 1000*f1->GetParameter(5);
  double wJ0bErr = 1000*f1->GetParError(5);
  cout<<"wJ0b = "<<wJ0b<<" +/- "<<wJ0bErr<<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;

  double cJ1b = 1000*f2->GetParameter(4);
  double cJ1bErr = 1000*f2->GetParError(4);
  cout<<"cJ1b = "<<cJ1b<<" +/- "<<cJ1bErr<<endl;

  double wJ1b = 1000*f2->GetParameter(5);
  double wJ1bErr = 1000*f2->GetParError(5);
  cout<<"wJ1b = "<<wJ1b<<" +/- "<<wJ1bErr<<endl;

  double cJ1c = 1000*f2->GetParameter(7);
  double cJ1cErr = 1000*f2->GetParError(7);
  cout<<"cJ1c = "<<cJ1c<<" +/- "<<cJ1cErr<<endl;

  double wJ1c = 1000*f2->GetParameter(8);
  double wJ1cErr = 1000*f2->GetParError(8);
  cout<<"wJ1c = "<<wJ1c<<" +/- "<<wJ1cErr<<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;


  double rF1a = f1->GetParameter(0); f1->SetParameter(0,0);
  double rF1b = f1->GetParameter(3); f1->SetParameter(3,0);
  double rF1c = f1->GetParameter(6); f1->SetParameter(6,0);

  f1->SetParameter(0,rF1a); hF1a->Eval(f1);  f1->SetParameter(0,0);
  f1->SetParameter(3,rF1b); hF1b->Eval(f1);  f1->SetParameter(3,0);
  f1->SetParameter(6,rF1c); hF1c->Eval(f1);  f1->SetParameter(6,0);

  hF1a->SetLineWidth(2); hF1a->SetLineColor(2);
  hF1b->SetLineWidth(2); hF1b->SetLineColor(4);
  hF1c->SetLineWidth(2); hF1c->SetLineColor(7);

  hF1a->SetLineWidth(2); hF1a->SetLineColor(2);
  hF1b->SetLineWidth(2); hF1b->SetLineColor(4);
  hF1c->SetLineWidth(2); hF1c->SetLineColor(7);

  double rF2a = f2->GetParameter(0); f2->SetParameter(0,0);
  double rF2b = f2->GetParameter(3); f2->SetParameter(3,0);
  double rF2c = f2->GetParameter(6); f2->SetParameter(6,0);
  double rF2d = f2->GetParameter(9); f2->SetParameter(9,0);
  double rF2e = f2->GetParameter(12); f2->SetParameter(12,0);


  f2->SetParameter(0,rF2a); hF2a->Eval(f2);  f2->SetParameter(0,0);
  f2->SetParameter(3,rF2b); hF2b->Eval(f2);  f2->SetParameter(3,0);
  f2->SetParameter(6,rF2c); hF2c->Eval(f2);  f2->SetParameter(6,0);
  f2->SetParameter(9,rF2d); hF2d->Eval(f2);  f2->SetParameter(9,0);
  f2->SetParameter(12,rF2e); hF2e->Eval(f2);  f2->SetParameter(12,0);

  hF2a->SetLineWidth(2); hF2a->SetLineColor(2);
  hF2b->SetLineWidth(2); hF2b->SetLineColor(7);
  hF2c->SetLineWidth(2); hF2c->SetLineColor(4);
  hF2d->SetLineWidth(2); hF2d->SetLineColor(11);
  hF2e->SetLineWidth(2); hF2e->SetLineColor(8);

  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");

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

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

  //hReal3->SetMaximum(100000);
  //hRealJ0->SetMaximum(100000);
  //hRealJ1->SetMaximum(100000);


  hRealJ0->GetYaxis()->SetTitle("");
  if (iShow == 1) {

    //TF1 *fun1 = hRealJ0->GetFunction("f1");
    //hRealJ0->GetListOfFunctions()->Remove(fun1);

    //TF1 *fun2 = hRealJ1->GetFunction("f2");
    //hRealJ1->GetListOfFunctions()->Remove(fun2);

    hReal3->Draw("e1");
    hRealJ0->Draw("samee");
    hRealJ1->Draw("same");
    hRealJ2->Draw("same");

    Double_t xl1=0.15, yl1 = 0.63, xl2=xl1+0.15, yl2=yl1+0.2;
    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,"Background","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.1, 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.75, yl1 = 0.8, xl2=xl1+0.1, 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];

  char  tString9[120];
  char  tString10[120];
  char  tString11[120];
  char  tString12[120];

  bool noFit = false;
  //hRealJ0->SetMaximum(80000);
  //hRealJ1->SetMaximum(80000);
  if (iShow == 2) {
    if (noFit == true) {
      hRealJ0NoFit->SetMinimum(0);
      hRealJ0NoFit->Draw("e1");
      legend->AddEntry(hRealJ0NoFit,"J=0","p");
      legend->Draw();
    }else{

      hRealJ0->SetLineColor(1);
      hRealJ0->SetMarkerStyle(21);
      hRealJ0->SetMarkerColor(1);

      hRealJ0->SetMinimum(0);

      hRealJ0->GetXaxis()->SetRangeUser(fLow,fHigh);
      hRealJ0->Draw("e1");

      
      hF1a->Draw("chistsame");
      hF1b->Draw("chistsame");
      hF1c->Draw("chistsame");
      
      legend->AddEntry(hRealJ0,"J=0","p");
      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");

      sprintf(tString5,"            center = %6.2f +/- %4.2f",cJ0b,cJ0bErr);
      sprintf(tString6,"PDG: #eta(1405) = 1408.8     +/- 2");
      sprintf(tString7,"            width  = %6.2f +/- %4.2f",wJ0b,wJ0bErr);
      sprintf(tString8,"PDG: #eta(1405) =  50.1      +/- 2.6");

      TLatex *t1 = new TLatex();
      TLatex *t2 = new TLatex();
      TLatex *t3 = new TLatex();
      TLatex *t4 = new TLatex();

      TLatex *t5 = new TLatex();
      TLatex *t6 = new TLatex();
      TLatex *t7 = new TLatex();
      TLatex *t8 = new TLatex();

      double tSize = 0.04;

      t1->SetNDC();
      t1->SetTextSize(tSize);
      t2->SetNDC();
      t2->SetTextSize(tSize);
      t3->SetNDC();
      t3->SetTextSize(tSize);
      t4->SetNDC();
      t4->SetTextSize(tSize);

      t5->SetNDC();
      t5->SetTextSize(tSize);
      t6->SetNDC();
      t6->SetTextSize(tSize);
      t7->SetNDC();
      t7->SetTextSize(tSize);
      t8->SetNDC();
      t8->SetTextSize(tSize);

      t5->SetTextColor(4);
      t6->SetTextColor(4);
      t7->SetTextColor(4);
      t8->SetTextColor(4);

      double xLocationFraction = 0.12;
      double yLocationFraction = 0.85;
      //t1->DrawLatex(xLocationFraction,yLocationFraction,tString1); //bump1
      //t2->DrawLatex(xLocationFraction,yLocationFraction-0.05,tString2);//bump1
      
      //t3->DrawLatex(xLocationFraction,yLocationFraction-0.15,tString3);//bump1
      //t4->DrawLatex(xLocationFraction,yLocationFraction-0.20,tString4);//bump1

      //t5->DrawLatex(xLocationFraction,yLocationFraction-0.30,tString5);
      //t6->DrawLatex(xLocationFraction,yLocationFraction-0.35,tString6);

      //t7->DrawLatex(xLocationFraction,yLocationFraction-0.45,tString7);
      //t8->DrawLatex(xLocationFraction,yLocationFraction-0.50,tString8);

      t5->DrawLatex(xLocationFraction,yLocationFraction,tString5);//bump2
      t6->DrawLatex(xLocationFraction,yLocationFraction-0.05,tString6);//bump2
      
      t7->DrawLatex(xLocationFraction,yLocationFraction-0.15,tString7);//bump2
      t8->DrawLatex(xLocationFraction,yLocationFraction-0.20,tString8);//bump2
    }
  }
  if (iShow == 3) {
    //hRealJ1->SetMaximum(100000);
    hRealJ1->SetMinimum(0);
    hRealJ1->SetLineColor(1);
    hRealJ1->SetMarkerStyle(21);
    hRealJ1->SetMarkerColor(1);
    hRealJ1->Draw("e1");

    hF2a->Draw("chistsame");
    hF2b->Draw("chistsame");
    hF2c->Draw("chistsame");
    hF2d->Draw("chistsame");
    hF2e->Draw("chistsame");

    legend->AddEntry(hRealJ1,"J=1","p");
    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");

    sprintf(tString5,"            center = %6.2f +/- %4.2f",cJ1b,cJ1bErr);
    sprintf(tString6,"PDG: f_{1}(1420) = 1426.3   +/-  0.9");
    sprintf(tString7,"            width  = %6.2f +/- %4.2f",wJ1b,wJ1bErr);
    sprintf(tString8,"PDG: f_{1}(1420) =  54.5   +/-  2.6");

    sprintf(tString9,"            center = %6.2f +/- %4.2f",cJ1c,cJ1cErr);
    sprintf(tString10,"PDG: h_{1}(1415) = 1409   + 9 - 8");
    sprintf(tString11,"            width  = %6.2f +/- %4.2f",wJ1c,wJ1cErr);
    sprintf(tString12,"PDG: h_{1}(1415) =  78   +/-  11");

    TLatex *t1 = new TLatex();
    TLatex *t2 = new TLatex();
    TLatex *t3 = new TLatex();
    TLatex *t4 = new TLatex();

    TLatex *t5 = new TLatex();
    TLatex *t6 = new TLatex();
    TLatex *t7 = new TLatex();
    TLatex *t8 = new TLatex();

    TLatex *t9 = new TLatex();
    TLatex *t10 = new TLatex();
    TLatex *t11 = new TLatex();
    TLatex *t12 = new TLatex();

    double tSize = 0.035;

    t1->SetNDC();
    t1->SetTextSize(tSize);
    t2->SetNDC();
    t2->SetTextSize(tSize);
    t3->SetNDC();
    t3->SetTextSize(tSize);
    t4->SetNDC();
    t4->SetTextSize(tSize);

    t5->SetNDC();
    t5->SetTextSize(tSize);
    t6->SetNDC();
    t6->SetTextSize(tSize);
    t7->SetNDC();
    t7->SetTextSize(tSize);
    t8->SetNDC();
    t8->SetTextSize(tSize);

    t9->SetNDC();
    t9->SetTextSize(tSize);
    t10->SetNDC();
    t10->SetTextSize(tSize);
    t11->SetNDC();
    t11->SetTextSize(tSize);
    t12->SetNDC();
    t12->SetTextSize(tSize);

    t1->SetTextColor(2); t2->SetTextColor(2);
    t3->SetTextColor(2); t4->SetTextColor(2);

    t5->SetTextColor(7); t6->SetTextColor(7);
    t7->SetTextColor(7); t8->SetTextColor(7);

    t9->SetTextColor(4); t10->SetTextColor(4);
    t11->SetTextColor(4); t12->SetTextColor(4);

    double xLocationFraction = 0.12;
    double yLocationFraction = 0.85;
    t1->DrawLatex(xLocationFraction,yLocationFraction,tString1);
    t2->DrawLatex(xLocationFraction,yLocationFraction-0.04,tString2);

    t3->DrawLatex(xLocationFraction,yLocationFraction-0.1,tString3);
    t4->DrawLatex(xLocationFraction,yLocationFraction-0.14,tString4);

    xLocationFraction = 0.51;
    yLocationFraction = 0.75;

    t5->DrawLatex(xLocationFraction,yLocationFraction,tString5);
    t6->DrawLatex(xLocationFraction,yLocationFraction-0.04,tString6);

    t7->DrawLatex(xLocationFraction,yLocationFraction-0.1,tString7);
    t8->DrawLatex(xLocationFraction,yLocationFraction-0.14,tString8);

    xLocationFraction = 0.12;
    yLocationFraction = 0.62;

    t9->DrawLatex(xLocationFraction,yLocationFraction,tString9);
    t10->DrawLatex(xLocationFraction,yLocationFraction-0.04,tString10);

    t11->DrawLatex(xLocationFraction,yLocationFraction-0.1,tString11);
    t12->DrawLatex(xLocationFraction,yLocationFraction-0.14,tString12);

  }

  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);

  }

}
