Double_t accFun(Double_t *x,Double_t *parPtr) {
  Double_t accVal,eTot;
  eTot   = *x;
  accVal = 0.0;
  double lVal   = parPtr[0];
  double uVal   = parPtr[1];
  double yMin   = parPtr[2];
  double yMax   = parPtr[3];

  double yCross = lVal + uVal;
  
  if (eTot > yMin){
    accVal = 1 - 2*lVal/eTot; 
    if (eTot > yCross){
      accVal = -1 + 2*uVal/eTot; 
    }
  }
  return accVal;
}


void plotAcc(int iShow){
  gROOT->Reset();
  gROOT->SetStyle("Plain");
  gStyle->SetOptStat(0);

  TFile *fMC = new TFile("./mcOut.root");
  TH1D *hNum; fMC->GetObject("hBeamEnergyPSHit",hNum);
  TH1D *hDen; fMC->GetObject("hBeamEnergy",hDen);
  TH1D *hDiv = (TH1D*) hNum->Clone("hDiv");
  hDiv->Sumw2();
  hDiv->Divide(hNum,hDen);
  hDiv->SetLineWidth(2);
  hDiv->SetLineColor(7);

  //TF1 *f1 = new TF1("f1",accFun,6,100,4);
  TF1 *f1 = new TF1("f1",accFun,5.9,100,4);
  //TF1 *f2 = new TF1("f2",accFun,6.*5/9,100,4);
  TF1 *f2 = new TF1("f2",accFun,3.2,100,4);
  //TF1 *f3 = new TF1("f3",accFun,6.*2/9,100,4);
  TF1 *f3 = new TF1("f3",accFun,1.5,100,4);

  TF1 *f4 = new TF1("f3",accFun,1.5,100,4);

  f4->SetParameter(0,1.66); //Min e1
  f4->SetParameter(1,5); //Max e1
  f4->SetParameter(2,1.66*2); //Min eTot
  f4->SetParameter(3,18);//Max eTot
  f4->SetLineColor(1);
  f4->Draw("same");


  TH1D *hAcc18a = new TH1D("hAcc18a","",5000,0.0,18.0);
  hAcc18a->SetMinimum(0);
  hAcc18a->SetMaximum(0.7);
  hAcc18a->GetXaxis()->SetTitle("E_{#gamma}");
  hAcc18a->GetYaxis()->SetTitle("Acceptance");
  hAcc18a->Draw("");


  f1->SetParameter(0,3); //Min e1
  f1->SetParameter(1,9); //Max e1
  f1->SetParameter(2,6); //Min eTot
  f1->SetParameter(3,18);//Max eTot
  f1->SetLineColor(1);
  f1->Draw("same");

  f2->SetParameter(0,3*5./9); //Min e1
  f2->SetParameter(1,9*5./9); //Max e1
  f2->SetParameter(2,6*5./9); //Min eTot
  f2->SetParameter(3,18*5./9);//Max eTot
  f2->SetLineColor(2);
  f2->Draw("same");

  f3->SetParameter(0,3*2.5/9); //Min e1
  f3->SetParameter(1,9*2.5/9); //Max e1
  f3->SetParameter(2,6*2.5/9); //Min eTot
  f3->SetParameter(3,18*2.5/9);//Max eTot
  f3->SetLineColor(4);
  f3->Draw("same");


  hDiv->Draw("same");

  //Make a legend with fill type                                                                                                                                        
  Double_t xl1=0.53, yl1 = 0.7;
  Double_t yl2=yl1 + 0.18;
  Double_t xl2=xl1 + 0.15;
  TLegend *legend = new TLegend(xl1,yl1,xl2,yl2);
  legend->SetFillColor(0);
  legend->SetLineColor(0);
  legend->SetTextSize(0.035);
  legend->AddEntry(hDiv,"E_{e} = 18 GeV, B = 1.8T: MC","l");
  legend->AddEntry(f1,"E_{e} = 18 GeV, B = 1.8T","l");
  legend->AddEntry(f2,"E_{e} = 10 GeV, B = 1.0T","l");
  legend->AddEntry(f3,"E_{e} =  5 GeV, B = 0.5T","l");

  //legend->AddEntry(f2,"10 GeV, 1.0T","l");
  //legend->AddEntry(f3," 5 GeV, 0.5T","l");
  legend->Draw();

  if (iShow == 2) {
    hAcc18a->Draw("");
    f1->Draw("same");
    f4->Draw("same");
  }


}
