//////---------- channel: 0==muon dijet, 1== electron dijet
/////                     2==muon  boosted,   3== electron boosted
void makeATGCLimitDataCards(int channel) {

//   const Int_t bins = 8; 
//   const Float_t dm_min = 200.; 
//   const Float_t dm_max = 600.;

  Int_t bins = 7; 
  Float_t dm_min = 100.; 
  Float_t dm_max = 275.;
  if(channel>1) { bins = 15; dm_min = 200.; dm_max = 800; }

  Int_t binsmc = (int)(10*(dm_max - dm_min)); // units of 0.1GeV, set up for smearing post-binning!

  domu = true;
  if(channel==1 || channel==3) domu = false;

 
  TString outfile = (domu?TString("mu"):TString("el"))+ 
    (channel<2?TString("dijet"):TString("boosted"));
  TFile* outputForLimit = TFile::Open(outfile+".root", "recreate");


  TString cutsDijet("(W_pt<200.) && (dijetPt>70.) && (abs(JetPFCor_Eta[0])<2.4) && (abs(JetPFCor_Eta[1])<2.4) && (abs(JetPFCor_Eta[0]-JetPFCor_Eta[1])<1.5) &&(abs(JetPFCor_dphiMET[0])>0.4) &&(W_mt>30.) &&(JetPFCor_Pt[0]>40.) &&(JetPFCor_Pt[1]>35.) &&(JetPFCor_Pt[2]<30.) &&(JetPFCor_bDiscriminatorCSV[0]<0.244) &&(JetPFCor_bDiscriminatorCSV[1]<0.244) && (Mass2j_PFCor>70. && Mass2j_PFCor<100.)");



  // Do not put jet pt in the cut string here, since it is going to be smeared
  TString cutsMerged("(vbf_event==0) && (W_pt>200.) &&(abs(GroomedJet_CA8_eta[0])<2.4)&&(ggdboostedWevt==1) && (GroomedJet_CA8_deltaphi_METca8jet[0]>2.0) && (GroomedJet_CA8_deltaR_lca8jet[0]>1.57) && (numPFCorJetBTags<1) && (GroomedJet_CA8_tau2tau1[0]<0.55) && (GroomedJet_CA8_mass_pr[0]>70. && GroomedJet_CA8_mass_pr[0]<100.)");

  // && (GroomedJet_CA8_deltaR_lca8jet[1]<-900 || GroomedJet_CA8_deltaR_lca8jet[1]>7.0)

  TString        lepton_cut = "(event_met_pfmet >30) && (W_electron_pt>35.)";
  if(channel==0) lepton_cut = "(event_met_pfmet >25) &&(abs(W_muon_eta)<2.1) && (W_muon_pt>25.)";
  if(channel==1) lepton_cut = "(event_met_pfmet >30) && (W_electron_pt>30.)";
  if(channel==2) lepton_cut = "(event_met_pfmet >50) &&(abs(W_muon_eta)<2.1) && (W_muon_pt>30.)";
  if(channel==3) lepton_cut = "(event_met_pfmet >70) && (W_electron_pt>35.)";

  TString And = " && ";

  TString jet_cut = cutsDijet;
  TString jetptcut,mcjetptcut;

  if(channel>1) jet_cut = cutsMerged;

  char* observable = "dijetPt";
  char* mcobservable = "dijetPt";
  char* xtitle = "p_{T}^{jj} [GeV]"; 
  double jetptcutval;
  if(channel>1) {
    double jetthresh = 80;

    /*observable = 
      "(GroomedJet_CA8_pt[0]>jetthresh)+\
      (GroomedJet_CA8_pt[1]>jetthresh)+\
      (GroomedJet_CA8_pt[2]>jetthresh)+\
      (GroomedJet_CA8_pt[3]>jetthresh)+\
      (GroomedJet_CA8_pt[4]>jetthresh)+\
      (GroomedJet_CA8_pt[5]>jetthresh)";*/

    mcobservable = "GroomedJet_CA8_pt_smeared[0]";
    //mcobservable = "GroomedJet_CA8_pt[0]";
    observable = "GroomedJet_CA8_pt[0]";
    jetptcutval = 200.;
    jetptcut   = Form("(%s > %f)",  observable,jetptcutval);
    mcjetptcut = Form("(%s > %f)",mcobservable,jetptcutval);
    xtitle = "p_{T}^{j} [GeV]";
  }


  TCut mccut( TString("(effwt*puwt)*(")+ lepton_cut+And+jet_cut+And+mcjetptcut + TString(")") );
  TCut datacut( TString("(") + lepton_cut+And+jet_cut+And+jetptcut + TString(")") );
  //TCut the_cut( TString("(effwt*puwt)*(")+ lepton_cut+And+jet_cut+And+jetptcut + TString(")") );

  // for combining ttbar files
  TString mttstr
    ("sqrt((W_top_E+W_atop_E)^2 -(W_top_px+W_atop_px)^2 -(W_top_py+W_atop_py)^2 -(W_top_pz+W_atop_pz)^2)");

  // weight default sample by 1 for mtt<700, by half above for adding the high mtt samples
  TCut mttwt(TString("(")+mttstr+TString("<700)?1.0:0.5"));

  InstantiateTrees();


  //th1data  = new TH1D("th1data",  "th1data",  bins, ptbins_boosted); // bins, dm_min, dm_max);
  th1data  = new TH1D("th1data",  "th1data",  bins, dm_min, dm_max);
  th1data->Sumw2();
  th1data->SetMarkerStyle(20);
  th1data->SetMarkerSize(1.25);
  th1data->SetLineWidth(2);
  th1data->SetMinimum(0.0);

  TString drawstr = TString(observable)+TString(">>th1data");

  cout <<
    TString("treedata->Draw(\"")+drawstr+TString("\", \"")+
    TString((const char*)datacut)+TString("\", \"goff\")") << endl;

  treedata->Draw(drawstr, datacut, "goff");

  // ------- Get WW/WZ ------- 
  th1wwlo = new TH1D("th1wwlo", "th1wwlo", bins, dm_min, dm_max);
  th1wwhi = new TH1D("th1wwhi", "th1wwhi", bins, dm_min, dm_max);
  th1ww   = new TH1D("th1ww", "th1ww", bins, dm_min, dm_max);
  th1wzlo = new TH1D("th1wzlo", "th1wzlo", bins, dm_min, dm_max);
  th1wzhi = new TH1D("th1wzhi", "th1wzhi", bins, dm_min, dm_max);
  th1wz   = new TH1D("th1wz", "th1wz", bins, dm_min, dm_max);
  //th1wz = new TH1D("th1wz", "th1wz", bins, ptbins_boosted);
  th1wwlo->Sumw2();
  th1wwhi->Sumw2();
  th1wzlo->Sumw2();
  th1wzhi->Sumw2();
  th1wz->Sumw2();

  fillMChisto(th1wwlo,treewwlo,mcobservable,mccut);
  fillMChisto(th1wwhi,treewwhi,mcobservable,mccut);
  fillMChisto(th1wzlo,treewzlo,mcobservable,mccut);
  fillMChisto(th1wzhi,treewzhi,mcobservable,mccut);

  // ------- Get ttbar ------- 
  th1Top   = new TH1D("th1Top", "th1Top", bins, dm_min, dm_max);
  th1toplo = new TH1D("th1toplo", "th1toplo", bins, dm_min, dm_max);
  th1topmd = new TH1D("th1topmd", "th1topmd", bins, dm_min, dm_max);
  th1tophi = new TH1D("th1tophi", "th1tophi", bins, dm_min, dm_max);

  //th1Top = new TH1D("th1Top", "th1Top", bins, ptbins_boosted);
  th1Top->Sumw2();
  th1toplo->Sumw2();
  th1topmd->Sumw2();
  th1tophi->Sumw2();

  fillMChisto(th1toplo,treettblo,mcobservable,mttwt*mccut);
  fillMChisto(th1topmd,treettbmd,mcobservable,mccut);
  fillMChisto(th1tophi,treettbhi,mcobservable,mccut);

    // ------- Get WJets ------- 
  th1wjlo  = new TH1D("th1wjlo",  "th1wjlo",  bins, dm_min, dm_max);
  th1wjhi  = new TH1D("th1wjhi",  "th1wjhi",  bins, dm_min, dm_max);
  th1wjets = new TH1D("th1wjets", "th1wjets", bins, dm_min, dm_max);
  th1wjlo->Sumw2();
  th1wjhi->Sumw2();

  fillMChisto(th1wjlo,treewjlo,mcobservable,mccut);
  fillMChisto(th1wjhi,treewjhi,mcobservable,mccut);

  //th1wjets  = new TH1D("th1wjets",  "th1wjets", bins, ptbins_boosted);

  // ------- Get QCD ------- 
  //th1qcd = new TH1D("th1qcd", "th1qcd", bins, dm_min, dm_max);
  //th1qcd = new TH1D("th1qcd", "th1qcd", bins, ptbins_boosted);
  //th1qcd->Sumw2();
  //treeqcd->Draw(TString(observable)+TString(">>th1qcd"), mccut, "goff");

  // ------- Get Z+Jets ------- 
  //th1zjets = new TH1D("th1zjets", "th1zjets", bins, dm_min, dm_max);
  //th1zjets->Sumw2();
  //treezj->Draw(TString(observable)+TString(">>th1zjets"), mccut, "goff");


  // ------- Get Single top ------- 
  th1stops = new TH1D("th1stops", "th1stops", bins, dm_min, dm_max);
  th1stopt = new TH1D("th1stopt", "th1stopt", bins, dm_min, dm_max);
  th1stoptw = new TH1D("th1stoptw", "th1stoptw", bins, dm_min, dm_max);
  //th1stops = new TH1D("th1stops", "th1stops", bins, ptbins_boosted);
  //th1stopt = new TH1D("th1stopt", "th1stopt", bins, ptbins_boosted);
  //th1stoptw = new TH1D("th1stoptw", "th1stoptw", bins, ptbins_boosted);
  th1stops->Sumw2();
  th1stopt->Sumw2();
  th1stoptw->Sumw2();
  
  fillMChisto(th1stops,treests,mcobservable,mccut);
  fillMChisto(th1stopt,treestt,mcobservable,mccut);
  fillMChisto(th1stoptw,treestw,mcobservable,mccut);
 
  th1stopps = new TH1D("th1stopps", "th1stopps", bins, dm_min, dm_max);
  th1stoppt = new TH1D("th1stoppt", "th1stoppt", bins, dm_min, dm_max);
  th1stopptw = new TH1D("th1stopptw", "th1stopptw", bins, dm_min, dm_max);
  //th1stopps = new TH1D("th1stopps", "th1stopps", bins, ptbins_boosted);
  //th1stoppt = new TH1D("th1stoppt", "th1stoppt", bins, ptbins_boosted);
  //th1stopptw = new TH1D("th1stopptw", "th1stopptw", bins, ptbins_boosted);
  th1stopps->Sumw2();
  th1stoppt->Sumw2();
  th1stopptw->Sumw2();

  fillMChisto(th1stopps,tree64,mcobservable,mccut);
  fillMChisto(th1stoppt,tree65,mcobservable,mccut);
  fillMChisto(th1stopptw,tree66,mcobservable,mccut);

  // ---- Scale the histos ---- 
  ScaleHistos(channel);

#if 0    
  // ---- Make smooth diboson shape ----------
  TH1D* th1wvclone = (TH1D *)th1wv->Clone("th1wvclone");
  float tmin = 200.0;
  if(channel>1) tmin = 300.0;
  gaus2 = new TF1("gaus2","gaus", tmin, 1000000000.);
  th1wvclone->Fit(gaus2,"I0","");
#endif
    
  // ---- Empty histograms for display/plotting ---- 
  SetupEmptyHistogram(bins, dm_min, dm_max, xtitle);
  //SetupEmptyHistogram(bins, ptbins_boosted, xtitle);
  
  // ---- Sum all backgrounds ----------
  TH1D* th1wv_no_overflow = (TH1D *)th1wv->Clone("th1wv_no_overflow");
  SumAllBackgrounds();

#if 1
  // ---- Get signal histogram ----------
  TCut wwsigratio= GetSigRatioFunction(mcobservable);
  TCut wzsigratio= GetSigRatioFunction(mcobservable,"wz");
  
  TCut wwsigcut = mccut*wwsigratio;
  TCut wzsigcut = mccut*wzsigratio;
  
  wwatgc4Display = new TH1D("wwatgc4Display","wwatgc4Display",bins,dm_min,dm_max);
  wzatgc4Display = new TH1D("wzatgc4Display","wzatgc4Display",bins,dm_min,dm_max);
  //wwatgc4Display = new TH1D("wwatgc4Display","wwatgc4Display",bins,ptbins_boosted);
  
  drawstr = TString(mcobservable)+TString(">>wwatgc4Display");

  cout <<
    TString("wwtree->Draw(\"")+drawstr+TString("\", \"")+
    TString((const char*)wwsigcut)+TString("\", \"goff\")") << endl;

  TH1D *th1wwlosc = new TH1D("sigwwlo","sigwwlo",bins,dm_min,dm_max);
  TH1D *th1wwhisc = new TH1D("sigwwhi","sigwwhi",bins,dm_min,dm_max);
  TH1D *th1wzlosc = new TH1D("sigwzlo","sigwzlo",bins,dm_min,dm_max);
  TH1D *th1wzhisc = new TH1D("sigwzhi","sigwzhi",bins,dm_min,dm_max);

  fillMChisto(th1wwlosc,treewwlo,mcobservable,wwsigcut,false);
  fillMChisto(th1wwhisc,treewwhi,mcobservable,wwsigcut,false);
  fillMChisto(th1wzlosc,treewzlo,mcobservable,wzsigcut,false);
  fillMChisto(th1wzhisc,treewzhi,mcobservable,wzsigcut,false);

  th1wwlosc->Scale(WW_scale_lo);
  th1wwhisc->Scale(WW_scale_hi);

  th1wzlosc->Scale(WZ_scale_lo);
  th1wzhisc->Scale(WZ_scale_hi);

  // stitch the two histograms together into one, post-smearing.
  for (int ibin=1;ibin<=wwatgc4Display->GetNbinsX();ibin++) {
    if (wwatgc4Display->GetBinLowEdge(ibin)<stitchwwgev) {
      wwatgc4Display->SetBinContent(ibin,th1wwlosc->GetBinContent(ibin));
      wwatgc4Display->SetBinError  (ibin,th1wwlosc->GetBinError  (ibin));
    } else {
      wwatgc4Display->SetBinContent(ibin,th1wwhisc->GetBinContent(ibin));
      wwatgc4Display->SetBinError  (ibin,th1wwhisc->GetBinError  (ibin));
    }
  }

  wwatgc4Display->Scale(WW_scale_NLO * (domu ? intLUMIinvpb_mu : intLUMIinvpb_el));

  // stitch the two WZ histograms together into one, post-smearing.
  for (int ibin=0;ibin<=wzatgc4Display->GetNbinsX()+1;ibin++) {
    if (wzatgc4Display->GetBinLowEdge(ibin)<stitchwzgev) {
      wzatgc4Display->SetBinContent(ibin,th1wzlosc->GetBinContent(ibin));
      wzatgc4Display->SetBinError  (ibin,th1wzlosc->GetBinError  (ibin));
    } else {
      wzatgc4Display->SetBinContent(ibin,th1wzhisc->GetBinContent(ibin));
      wzatgc4Display->SetBinError  (ibin,th1wzhisc->GetBinError  (ibin));
    }
  }

  wzatgc4Display->Scale(WZ_scale_NLO * (domu ? intLUMIinvpb_mu : intLUMIinvpb_el));

  cout << "wwatgc4Display nentries = " << wwatgc4Display->GetEntries() << endl;

  // ----- need to subtract the diboson contribution 
  wwatgc4Display->SetLineWidth(2);
  wwatgc4Display->SetLineColor(1);
  wwatgc4Display->SetFillColor(0);

  // ----- need to subtract the diboson contribution 
  wzatgc4Display->SetLineWidth(2);
  wzatgc4Display->SetLineColor(1);
  wzatgc4Display->SetFillColor(0);
  wzatgc4Display->SetLineStyle(2);

  //-------- Add overflow bin ----------------
  AddOverflowBin(wwatgc4Display);
  AddOverflowBin(wzatgc4Display);
  
#endif

  // ---- Compose the stack ----------
  THStack* hs = new THStack("hs","MC contribution");
  //hs->Add(th1zjets); 
 //hs->Add(th1qcd);
  hs->Add(th1Top);
  hs->Add(th1wjets);
  hs->Add(th1wv);  // add WW in with backgrounds for display only
  hs->Add(wwatgc4Display);
  hs->Add(wzatgc4Display);



  // ---- Stack for shape systematics Up ----------
  double bkgd_norm_fracerror = domu ? mu_bkgd_norm_error : el_bkgd_norm_error;
  double sig_norm_fracerror = domu ? mu_sig_norm_error : el_sig_norm_error;

  cout << "Background normalization fractional systematic = " << bkgd_norm_fracerror << endl;
  cout << "Signal     normalization fractional systematic = " << sig_norm_fracerror << endl;

  //TF1* formScaleUp = new TF1("formScaleUp", "1.0+0.4*log(x/5)", dm_min, dm_max);
  //TF1* formScaleDn = new TF1("formScaleDn", "1.0-0.2*log(x/5)", dm_min, dm_max);
  TF1* formScaleUp = new TF1("formScaleUp", Form("1.0+%f",bkgd_norm_fracerror), dm_min, dm_max);
  TF1* formScaleDn = new TF1("formScaleDn", Form("1.0-%f",bkgd_norm_fracerror), dm_min, dm_max);

  systUp = (TH1D*) th1wjets->Clone("systUp");
  systUp->Multiply(formScaleUp);
  //systUp->Add(th1zjets);
  //systUp->Add(th1qcd);
  systUp->Add(th1Top);
  systUp->SetFillColor(0);
  systUp->SetLineStyle(2);
  systUp->SetLineColor(2);
  systUp->SetLineWidth(3);

  // ---- Stack for shape systematics Down ----------
  systDown = (TH1D*) th1wjets->Clone("systDown");
  systDown->Multiply(formScaleDn);
  //systDown->Add(th1zjets);
  //systDown->Add(th1qcd);
  systDown->Add(th1Top);
  systDown->SetFillColor(0);
  systDown->SetLineWidth(3);
  systDown->SetLineStyle(2);
  systDown->SetLineColor(2);

  /////////////////////////////////////////
  ///////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////
  ///////////////////////////////////////////////////////////
  

  // ------- Setup the canvas ------- 
  gStyle->SetOptStat(0);
  gStyle->SetOptTitle(0);
  // gStyle->SetPadTopMargin(0.1);
  gStyle->SetPadLeftMargin(0.15);
  // gStyle->SetPadRightMargin(0.2);
  gStyle->SetPadBottomMargin(0.3);
  // gStyle->SetErrorX(0.5);

  TCanvas* c1 = new TCanvas("dijetPt", "", 10,10, 500, 500);

  TPad *d1, *d2;
  c1->Divide(1,2,0,0);
  d1 = (TPad*)c1->GetPad(1);
  d1->SetPad(0.01,0.30,0.95,0.99);
  d2 = (TPad*)c1->GetPad(2);
  d2->SetPad(0.01,0.02,0.95,0.30);
  d1->cd();

  gPad->SetBottomMargin(0.005);

  gPad->SetTopMargin(0.1);
  gPad->SetRightMargin(0.04);
  // gPad->SetLeftMargin(0.14);


  // Draw it all
  double ymax= 5000000.;
  double ymin= 7.0;
  if(channel>1) { 
    ymax= 3000.;
    ymin= 0.10;
  }

  th1totempty->GetYaxis()->SetRangeUser(ymin, ymax);
  th1data->GetYaxis()->SetRangeUser(ymin, ymax);
  th1totempty->Draw();
  hs->Draw("samehist");
  for (int i=1;i<=th1tot->GetNbinsX();i++)
    {
      double val = th1tot->GetBinContent(i); // / (ptbins_boosted[i]-ptbins_boosted[i-1]);
      double err = fabs(th1tot->GetBinError(i)); // / (ptbins_boosted[i]-ptbins_boosted[i-1]);
      TBox *b = new TBox(th1tot->GetBinLowEdge(i),
			 val-err,th1tot->GetBinLowEdge(i+1),val+err);
      b->SetLineColor(1);
      b->SetFillColor(1);
      b->SetFillStyle(3001);
      b->SetLineStyle(3001);	 
      b->Draw();
    }
  //data2draw->Draw("esame");

#ifndef BLINDED
  th1data->Draw("esame");
#endif

  cmspre(); 
  // Set up the legend
  TLegend* Leg = GetLegend(channel);   
  Leg->Draw();  
  gPad->SetLogy();
  gPad->RedrawAxis();

  d2->cd();
  gPad->SetTopMargin(0.02);
  gPad->SetRightMargin(0.04);
  gPad->SetFrameBorderSize(0);
  gPad->SetBottomMargin(0.45);
  gPad->SetTickx();

  th1emptyclone->Draw();
#ifndef BLINDED
  hhratio->Draw("esame");
  //hhratioUp->Draw("hist lsame");
  //hhratioDown->Draw("hist lsame");
#endif
  TLine *line; line = new TLine(dm_min,1.0,dm_max,1.0);
  line->SetLineStyle(1);
  line->SetLineWidth(1);
  line->SetLineColor(1);
  line->Draw();

  //gPad->WaitPrimitive();
  c1->Modified();
  c1->Update();

  c1->Print(TString("OutDir/")+outfile
#ifdef BLINDED
	    +TString("_fatjetPt_blinded.png")
#else
	    +TString("_fatjetPt_unblinded.png")
#endif
	    );

  c1->SaveAs(TString("OutDir/") + outfile
#ifdef BLINDED
	     +TString("_fatjetPt_blinded.pdf")
#else
	     +TString("_fatjetPt_unblinded.pdf")
#endif
	     );
  c1->SaveAs(TString("OutDir/") + outfile
#ifdef BLINDED
	     +TString("_fatjetPt_blinded.root")
#else
	     +TString("_fatjetPt_unblinded.root")
#endif
	     );
   

  ///// -------------------------------//////

  if(saveDataCards_) {
    outputForLimit->cd();
    th1data->SetName("data_obs");     th1data->Write("data_obs");
    th1tot->SetName("background");    th1tot->Write("background");
    th1ww->SetName("ww");             th1ww->Write("ww");
    th1ww->SetName("wz");             th1wz->Write("wz");
    th1wv->SetName("diboson");        th1wv->Write("diboson");

    th1wv_no_overflow->Write("th1wv_no_overflow");
    char* tempname = "background_backshapeUp";
    if(channel==0) tempname = "background_mudijet_backshapeUp";
    if(channel==1) tempname = "background_eldijet_backshapeUp";
    if(channel==2) tempname = "background_muboosted_backshapeUp";
    if(channel==3) tempname = "background_elboosted_backshapeUp";
    systUp->SetName(tempname);
    systUp->Write(tempname);
    tempname = "background_backshapeDown";
    if(channel==0) tempname = "background_mudijet_backshapeDown";
    if(channel==1) tempname = "background_eldijet_backshapeDown";
    if(channel==2) tempname = "background_muboosted_backshapeDown";
    if(channel==3) tempname = "background_elboosted_backshapeDown";
    systDown->SetName(tempname);
    systDown->Write(tempname);

    outputForLimit->Close();

  } ///// close if saveDataCards_

    //delete th1wvclone;
}
void NuclearModificationY(){

  gROOT->SetStyle("Plain");
  gStyle->SetOptTitle(0);
  gStyle->SetOptStat(0);
/*
  gStyle->SetPadRightMargin(0.010);//###0.020
  gStyle->SetPadLeftMargin(0.165);
  gStyle->SetPadTopMargin(0.075);
  gStyle->SetPadBottomMargin(0.145);
*/

    
  TFile*filePPReference=new TFile(Form("../../../fonll/output%sY.root",particle.Data()));  
  //TFile*filePPReference=new TFile(Form("../../fonll/output%sY.root",particle.Data()));  
  TGraphAsymmErrors*gaeBplusReference=(TGraphAsymmErrors*)filePPReference->Get(Form("gaeSigmaDecay%s",particle.Data()));
  gaeBplusReference->SetName(Form("gae%sReference",particle.Data()));
  
  TFile*filepPb=new TFile(Form("../Results%s_y/Sigma%s.root",particle.Data(),particle.Data()));
  TH1F*hSigmapPbStat=(TH1F*)filepPb->Get("hPtSigma");  
  
  double scalingfactor=1e-6;
  double yvalue,xvalue,yerrorhigh,yerrorlow;
  
  for (int i=0;i<nbins;i++){
    hSigmapPbStat->SetBinContent(i+1,scalingfactor*hSigmapPbStat->GetBinContent(i+1));
    hSigmapPbStat->SetBinError(i+1,scalingfactor*hSigmapPbStat->GetBinError(i+1));
    
    yvalue=-1.;
    xvalue=-1.;
    yerrorhigh=-1.;
    yerrorlow=-1.;
    
    gaeBplusReference->GetPoint(i,xvalue,yvalue);
    yerrorhigh=gaeBplusReference->GetEYhigh()[i];
    yerrorlow=gaeBplusReference->GetEYlow()[i];
  
    gaeBplusReference->SetPoint(i,xvalue,yvalue*scalingfactor);
    gaeBplusReference->SetPointEYhigh(i,yerrorhigh*scalingfactor);
    gaeBplusReference->SetPointEYlow(i,yerrorlow*scalingfactor);

  } 

  
  
  for (int i=0;i<nbins;i++){
    hSigmapPbStat->SetBinContent(i+1,(1./tagandprobcorrection[i])*(hSigmapPbStat->GetBinContent(i+1)));
    hSigmapPbStat->SetBinError(i+1,(1./tagandprobcorrection[i])*(hSigmapPbStat->GetBinError(i+1)));
  } 

  
  Double_t yRefPP[nbins];                        //value y reference
  Double_t xRefPP[nbins];                        //value x reference
  Double_t yPPsystFONLLhigh[nbins];              //y err syst FONLL high
  Double_t yPPsystFONLLlow[nbins];               //y err syst FONLL low
  Double_t yPercPPsystFONLLhigh[nbins];          //y percentuale err syst FONLL high
  Double_t yPercPPsystFONLLlow[nbins];           //y percentuale err syst FONLL low
    
  Double_t ySigmapPb[nbins];                     //value y pPb 
  Double_t xSigmapPb[nbins];                     //value x pPb
  Double_t ySigmapPbStat[nbins];                 //y err stat pPb
  Double_t yPercSigmapPbStat[nbins];             //y err stat pPb
  
  Double_t yFONLL[nbins];                        //1
  Double_t yRpA[nbins];                          //value y RpA 
  Double_t yRpAStat[nbins];                      //y err stat RpA 
  Double_t yRpAsystFONLLhigh[nbins];             //y err syst FONLL RpA high
  Double_t yRpAsystFONLLlow[nbins];              //y err syst FONLL RpA lzow
  Double_t yPercRpAsystFONLLhigh[nbins];         //y percentuale err syst FONLL RpA high
  Double_t yPercRpAsystFONLLlow[nbins];          //y percentuale err syst FONLL RpA low
    
  Double_t ySigmapPbSystTotHigh[nbins];              //y percentuale err syst pPb TOT
  Double_t ySigmapPbSystTotLow[nbins];              //y percentuale err syst pPb TOT

  Double_t yPercRpPbSystTotHigh[nbins];          //y percentuale err syst RpPb TOT
  Double_t yPercRpPbSystTotLow[nbins];          //y percentuale err syst RpPb TOT
  
  Double_t yRpPbSystTotHigh[nbins];              //y percentuale err syst RpPb TOT
  Double_t yRpPbSystTotLow[nbins];              //y percentuale err syst RpPb TOT

  Double_t yRFB[nbinsRFB];
  Double_t yRFBStat[nbinsRFB];
  Double_t yRFBSystTotHigh[nbinsRFB];
  Double_t yRFBSystTotLow[nbinsRFB];

  double x,y;

  for (Int_t i=0;i<nbins;i++) {
    gaeBplusReference->GetPoint(i,xRefPP[i],yRefPP[i]);
    yPPsystFONLLhigh[i]=gaeBplusReference->GetEYhigh()[i];
    yPPsystFONLLlow[i]=gaeBplusReference->GetEYlow()[i];
    yPercPPsystFONLLhigh[i]=yPPsystFONLLhigh[i]/yRefPP[i];
    yPercPPsystFONLLlow[i]=yPPsystFONLLlow[i]/yRefPP[i];
  }
  
  for(Int_t i=0;i<nbins;i++) {
    ySigmapPb[i]=hSigmapPbStat->GetBinContent(i+1);
    ySigmapPbStat[i]=hSigmapPbStat->GetBinError(i+1);
    yPercSigmapPbStat[i]=ySigmapPbStat[i]/ySigmapPb[i];
    ySigmapPbSystTotHigh[i]=yPercSigmapPbSystTotHigh[i]*ySigmapPb[i];
    ySigmapPbSystTotLow[i]=yPercSigmapPbSystTotLow[i]*ySigmapPb[i];
  }
  
  for(Int_t i=0;i<nbins;i++) {
    yRpA[i]=ySigmapPb[i]/yRefPP[i];
    yRpAStat[i]=ySigmapPbStat[i]/yRefPP[i];
    yFONLL[i]=yRpA[i];
    yPercRpAsystFONLLhigh[i]=(yPercPPsystFONLLlow[i]/(1-yPercPPsystFONLLlow[i]));
    yPercRpAsystFONLLlow[i]=(yPercPPsystFONLLhigh[i]/(1+yPercPPsystFONLLhigh[i]));
    yRpAsystFONLLhigh[i]=yPercRpAsystFONLLhigh[i]*yRpA[i];
    yRpAsystFONLLlow[i]=yPercRpAsystFONLLlow[i]*yRpA[i];
    yRpPbSystTotHigh[i]=yPercSigmapPbSystTotHigh[i]*yRpA[i];
    yRpPbSystTotLow[i]=yPercSigmapPbSystTotLow[i]*yRpA[i];

        std::cout << i << " , " << xbins[i] << " , " << ySigmapPb[i] << " , sta: " << ySigmapPbStat[i] << " , syslow: " << ySigmapPbSystTotLow[i] << " ,syshigh: " << ySigmapPbSystTotHigh[i] << std::endl;
std::cout << "FONLL: " << yRefPP[i] << " - " << yPPsystFONLLlow[i] << " + " << yPPsystFONLLhigh[i] << std::endl;
        std::cout << i << " ####### " << xbins[i] << " , " << yRpA[i] << " , sta: " << yRpAStat[i] << " , syslow: " << yRpPbSystTotLow[i] << " ,syshigh: " << yRpPbSystTotHigh[i] << " ,FONLLlow: " << yRpAsystFONLLlow[i] << " , FONLLhigh: " << yRpAsystFONLLhigh[i] << std::endl;
  }

  //RFB stuff
  //central value
  yRFB[0] =  ySigmapPb[3]/ySigmapPb[2];
  yRFB[1] =  ySigmapPb[4]/ySigmapPb[1];
  //Stat.
  yRFBStat[0] =  yRFB[0]*sqrt(pow(ySigmapPbStat[2]/ySigmapPb[2],2)+pow(ySigmapPbStat[3]/ySigmapPb[3],2));
  yRFBStat[1] =  yRFB[1]*sqrt(pow(ySigmapPbStat[4]/ySigmapPb[4],2)+pow(ySigmapPbStat[1]/ySigmapPb[1],2));
  //Syst.

  yRFBSystTotHigh[0] = yRFB[0]*yRFBSystTotHighRel[0];
  yRFBSystTotLow[0] = yRFB[0]*yRFBSystTotLowRel[0];
  yRFBSystTotHigh[1] = yRFB[1]*yRFBSystTotHighRel[1];
  yRFBSystTotLow[1] = yRFB[1]*yRFBSystTotLowRel[1];
    
  TGraphAsymmErrors *gSigmasyst = new TGraphAsymmErrors(nbins,xbins,ySigmapPb,exl2,exl2,ySigmapPbSystTotLow,ySigmapPbSystTotHigh);
  //###TGraphAsymmErrors *gSigmasyst = new TGraphAsymmErrors(nbins,xbins,ySigmapPb,exl,exl,ySigmapPbSystTotLow,ySigmapPbSystTotHigh);

  gSigmasyst->SetTitle("Sigma syst uncertainty from pPb");
  gSigmasyst->SetMarkerColor(1);
  gSigmasyst->SetLineColor(1);
  gSigmasyst->SetLineWidth(3);   
  gSigmasyst->SetMarkerStyle(21);
  gSigmasyst->SetMarkerColor(1);
  gSigmasyst->SetFillColor(kYellow-7);//5
  gSigmasyst->SetFillStyle(1001);

  TGraphAsymmErrors*gSigmasyst2=(TGraphAsymmErrors*)gSigmasyst->Clone();
  gSigmasyst2->SetMarkerColor(1);
  gSigmasyst2->SetMarkerStyle(25);  
  gSigmasyst2->SetFillColor(0);
  gSigmasyst2->SetFillStyle(0);
  gSigmasyst2->SetLineColor(1);//5
  gSigmasyst2->SetLineStyle(1);
  gSigmasyst2->SetLineWidth(3);


  //###TGraphAsymmErrors *gSigmastat = new TGraphAsymmErrors(nbins,xbins,ySigmapPb,exl,exl,ySigmapPbStat,ySigmapPbStat);
  TGraphAsymmErrors *gSigmastat = new TGraphAsymmErrors(nbins,xbins,ySigmapPb,exl0,exl0,ySigmapPbStat,ySigmapPbStat);


  gSigmastat->SetTitle("Sigma stat uncertainty from pPb");
  gSigmastat->SetMarkerColor(1);
  gSigmastat->SetLineColor(1);
  gSigmastat->SetLineWidth(3);   
  gSigmastat->SetMarkerStyle(21);
  gSigmastat->SetMarkerColor(1);
  
  gSigmastat->SetFillColor(0);
  gSigmastat->SetFillStyle(0);


  //###TCanvas *canvasSigma=new TCanvas("canvasSigma","canvasSigma",600,500);   
  TCanvas *canvasSigma=new TCanvas("canvasSigma","canvasSigma",500,500);   
  canvasSigma->cd();
  canvasSigma->Range(-1.989924,-0.2917772,25.49622,2.212202);
  canvasSigma->SetFillColor(0);
  canvasSigma->SetBorderMode(0);
  canvasSigma->SetBorderSize(2);
  canvasSigma->SetLeftMargin(0.200);
  canvasSigma->SetRightMargin(0.025);
  canvasSigma->SetTopMargin(0.080);
  canvasSigma->SetBottomMargin(0.150);
  canvasSigma->SetFrameBorderMode(0);
  
  TH2F* hempty=new TH2F("hempty","",10,-3.5,2.5,10,0,650.);  
  hempty->GetXaxis()->SetTitle("y_{CM}");
  hempty->GetXaxis()->CenterTitle();
  hempty->GetYaxis()->CenterTitle();
  //###hempty->GetYaxis()->SetTitle("d#sigma / dy_{CM}(#mub GeV^{-1}c)");
  hempty->GetYaxis()->SetTitle("d#sigma / dy_{CM}(#mub)");
  hempty->GetXaxis()->SetTitleOffset(0.90);
  hempty->GetYaxis()->SetTitleOffset(1.34);
  hempty->GetXaxis()->SetTitleSize(0.070);//###0.045
  hempty->GetYaxis()->SetTitleSize(0.070);//###0.045
  hempty->GetXaxis()->SetTitleFont(42);
  hempty->GetYaxis()->SetTitleFont(42);
  hempty->GetXaxis()->SetLabelFont(42);
  hempty->GetYaxis()->SetLabelFont(42);
  hempty->GetXaxis()->SetLabelSize(0.060);//###0.037
  hempty->GetYaxis()->SetLabelSize(0.060);//###0.037  
  hempty->SetMaximum(2);
  hempty->SetMinimum(0.);
  hempty->Draw();
    
  // Histogram style
  TH1D* hBplusReference = new TH1D("hBplusReference","",nbins,xhbins);
  TH1D* hBplusReferenceEYhigh = new TH1D("hBplusReferenceEYhigh","",nbins,xhbins);
  TH1D* hBplusReferenceEYlow = new TH1D("hBplusReferenceEYlow","",nbins,xhbins);

  for (int i=0;i<nbins;i++){
        double xgae,ygae;
        gaeBplusReference->GetPoint(i,xgae,ygae);
        hBplusReference->SetBinContent(i+1,ygae);
        hBplusReferenceEYhigh->SetBinContent(i+1,ygae+gaeBplusReference->GetEYhigh()[i]);
        hBplusReferenceEYlow->SetBinContent(i+1,ygae-gaeBplusReference->GetEYlow()[i]);
}

  //hBplusReference->SetMarkerColor(1);
  //hBplusReference->SetMarkerStyle(25);  
  //hBplusReference->SetFillColor(kYellow-7);//5
  //hBplusReference->SetFillStyle(1001);
  hBplusReference->SetLineColor(kAzure-3);
  hBplusReference->SetLineWidth(3);
  hBplusReferenceEYhigh->SetLineColor(kAzure-3);
  hBplusReferenceEYhigh->SetLineWidth(3);
  hBplusReferenceEYhigh->SetLineStyle(2);
  hBplusReferenceEYlow->SetLineColor(kAzure-3);
  hBplusReferenceEYlow->SetLineWidth(3);
  hBplusReferenceEYlow->SetLineStyle(2);

for (int i=0;i<nbins;i++){
        double xgae,ygae;
        gaeBplusReference->GetPoint(i,xgae,ygae);
        hBplusReference->SetBinContent(i+1,ygae);
        hBplusReferenceEYhigh->SetBinContent(i+1,ygae+gaeBplusReference->GetEYhigh()[i]);
        hBplusReferenceEYlow->SetBinContent(i+1,ygae-gaeBplusReference->GetEYlow()[i]);
        std::cout << "### FONLL (" << i << ") : " << ygae << " + " << gaeBplusReference->GetEYhigh()[i] << " - " << gaeBplusReference->GetEYlow()[i] << std::endl;
}

  for (int i=0;i<nbins;i++){
        double xgae,ygae;
        gSigmastat->GetPoint(i,xgae,ygae);
        std::cout << "### cross section (" << i << ") : " << ygae << " stat: " << gSigmastat->GetEYhigh()[i] << " syst: " << gSigmasyst->GetEYhigh()[i]     << std::endl;
  }
/*
  hBplusReference->Draw("][,same");

  hBplusReferenceEYhigh->Draw("][,same");
  hBplusReferenceEYlow->Draw("][,same");
*/
/*  
  gaeBplusReference->SetMarkerColor(1);
  gaeBplusReference->SetMarkerStyle(25);  
  gaeBplusReference->SetFillColor(kYellow-7);//5
  gaeBplusReference->SetFillStyle(1001);
  gaeBplusReference->SetLineColor(kAzure-3);//5
  gaeBplusReference->SetLineStyle(1);
  gaeBplusReference->SetLineWidth(0);
  gaeBplusReference->Draw("2psame");

  TGraphAsymmErrors*gaeBplusReference2=(TGraphAsymmErrors*)gaeBplusReference->Clone(); 
  gaeBplusReference2->SetMarkerColor(1);
  gaeBplusReference2->SetMarkerStyle(25);  
  gaeBplusReference2->SetFillColor(0);
  gaeBplusReference2->SetFillStyle(0);
  gaeBplusReference2->SetLineColor(kAzure-3);//5
  gaeBplusReference2->SetLineStyle(1);
  gaeBplusReference2->SetLineWidth(1);
  gaeBplusReference2->Draw("2psame");
*/
  //gSigmasyst->SetFillColor(0);
  //gSigmasyst->SetFillStyle(0);
  gSigmasyst->Draw("2esame");//2same
  gSigmasyst2->Draw("2esame");

  hBplusReference->Draw("][,same");

  hBplusReferenceEYhigh->Draw("][,same");
  hBplusReferenceEYlow->Draw("][,same");

  gSigmastat->SetMarkerColor(1);
  gSigmastat->SetLineColor(1);
  gSigmastat->SetLineWidth(3);   
  gSigmastat->SetMarkerStyle(21);
  gSigmastat->SetMarkerColor(1);
  gSigmastat->SetFillColor(0);
  gSigmastat->Draw("epsame");

  //###TLegend *legendSigma=new TLegend(0.233871,0.6701903,0.5322581,0.7526427,"");
  //TLegend *legendSigma=new TLegend(0.40,0.60,0.70,0.78,"");
  TLegend *legendSigma=new TLegend(0.23,0.56,0.57,0.77,"");

  legendSigma->SetBorderSize(0);
  legendSigma->SetLineColor(0);
  legendSigma->SetFillColor(0);
  legendSigma->SetFillStyle(1001);
  legendSigma->SetTextFont(42);
  legendSigma->SetTextSize(0.055);//###0.045
/*
  TBox *c = new TBox(0.1,1-commonErrorN,4,1+commonErrorP);
  c->SetLineColor(5);
  c->SetFillColor(5);
  c->Draw();
*/  
  gSigmastat->SetMarkerSize(1);

  TLegendEntry *ent_SigmapPb=legendSigma->AddEntry(gSigmastat,"pPb data","p");
  ent_SigmapPb->SetTextFont(42);
  ent_SigmapPb->SetMarkerColor(1);
  ent_SigmapPb->SetMarkerStyle(25);
  ent_SigmapPb->SetLineColor(1);
  ent_SigmapPb->SetLineWidth(2);
  ent_SigmapPb->SetLineStyle(0);

  TLegendEntry *ent_SigmapPbsyst=legendSigma->AddEntry(gSigmasyst,"Syst. pPb data","f");
  ent_SigmapPbsyst->SetTextFont(42);
  ent_SigmapPbsyst->SetMarkerColor(1);
  ent_SigmapPbsyst->SetMarkerStyle(25);
  ent_SigmapPbsyst->SetLineColor(1);
  ent_SigmapPbsyst->SetLineWidth(2);
  ent_SigmapPbsyst->SetLineStyle(0);
/*
///
  TLegendEntry *ent_SigmapPb=legendSigma->AddEntry(gSigmasyst,"pPb","pf");
  ent_SigmapPb->SetTextFont(42);
  ent_SigmapPb->SetLineColor(1);
  ent_SigmapPb->SetMarkerColor(1);
  ent_SigmapPb->SetMarkerStyle(25);
  //ent_SigmapPb->SetFillColor(kYellow-7);//5
  //ent_SigmapPb->SetFillStyle(1001);
  ent_SigmapPb->SetLineColor(1);
  ent_SigmapPb->SetLineWidth(0);
  ent_SigmapPb->SetLineStyle(0);
////
*/
/*
  TLegendEntry *ent_Sigmapp=legendSigma->AddEntry(gaeBplusReference,"FONLL pp ref.","fp");
  ent_Sigmapp->SetTextFont(42);
  ent_Sigmapp->SetLineColor(kAzure-3);//5
  ent_Sigmapp->SetLineStyle(1);
  ent_Sigmapp->SetMarkerColor(1);
  ent_Sigmapp->SetMarkerStyle(25);
*/

  TLegendEntry *ent_Sigmapp=legendSigma->AddEntry(hBplusReference,"Scaled FONLL pp ref.","l");
  ent_Sigmapp->SetTextFont(42);
  ent_Sigmapp->SetLineColor(kAzure-3);//5
  ent_Sigmapp->SetLineStyle(1);
  ent_Sigmapp->SetMarkerColor(1);
  ent_Sigmapp->SetMarkerStyle(21);

  TLegendEntry *ent_Sigmapperr=legendSigma->AddEntry(hBplusReferenceEYhigh,"FONLL pp ref. uncert.","l");
  ent_Sigmapperr->SetTextFont(42);
  ent_Sigmapperr->SetLineColor(kAzure-3);//5
  ent_Sigmapperr->SetLineStyle(2);
  ent_Sigmapperr->SetMarkerColor(1);
  ent_Sigmapperr->SetMarkerStyle(21);

  legendSigma->Draw("psame");
/*
  TBox *d = new TBox(0.1,1-commonErrorN,4,1+commonErrorP);
  d->SetLineColor(1);
  d->SetFillColor(0);
  d->Draw();
*/
  //###TLatex * tlatex1=new TLatex(0.23,0.84,"CMS");
  TLatex * tlatex1=new TLatex(0.24,0.85,"CMS");

  tlatex1->SetNDC();
  tlatex1->SetTextColor(1);
  tlatex1->SetTextFont(62);//###42
  tlatex1->SetTextSize(0.07);//###0.045
  tlatex1->Draw();
/*
  TLatex * tlatex12=new TLatex(0.24,0.80,"Preliminary");
  tlatex12->SetNDC();
  tlatex12->SetTextColor(1);
  tlatex12->SetTextFont(52);//###42
  tlatex12->SetTextSize(0.05);//###0.045
  tlatex12->Draw();
*/  

  TString mypar="B^{+}";
  
  //###TLatex * tlatexlumi=new TLatex(0.671371,0.7801268,"L = 34.8 nb^{-1}");
  TLatex * tlatexlumi=new TLatex(0.40,0.94,"34.6 nb^{-1} (pPb 5.02 TeV)");
  tlatexlumi->SetNDC();
  tlatexlumi->SetTextColor(1);
  tlatexlumi->SetTextFont(42);
  tlatexlumi->SetTextSize(0.06);//###0.045
  tlatexlumi->Draw();

  //###double xpos=0.8528226;
  //###double ypos=0.6849894;
  double xpos=0.85;
  double ypos=0.75;
  
  TLatex * tlatex3=new TLatex(xpos,ypos,mypar.Data());
   tlatex3->SetNDC();
  tlatex3->SetTextColor(1);
  tlatex3->SetTextFont(42);
  tlatex3->SetTextSize(0.07);//###0.06
  tlatex3->Draw();
  
  //###TLatex * tlatex4=new TLatex(0.51,0.84,"10 < p_{T}^{B} < 60 GeV/c");
  TLatex * tlatex4=new TLatex(0.51,0.84,"10 < p_{T} < 60 GeV/c");
  tlatex4->SetNDC();
  tlatex4->SetTextColor(1);
  tlatex4->SetTextFont(42);
  tlatex4->SetTextSize(0.06);
  tlatex4->Draw();
 
  TLatex * tlatex5=new TLatex(0.62,0.20,Form("Global uncert. %2.1f%%",commonErrorP*100));
  tlatex5->SetNDC();
  tlatex5->SetTextColor(1);
  tlatex5->SetTextFont(42);
  tlatex5->SetTextSize(0.04);
  tlatex5->Draw();


  canvasSigma->SaveAs(Form("../Results%s_y/canvasSigma%s.pdf",particle.Data(),particle.Data()));  
  canvasSigma->SaveAs(Form("../Results%s_y/canvasSigma%s.png",particle.Data(),particle.Data()));  

  canvasSigma->SaveAs(Form("../Results%s_y/canvasSigma%s.eps",particle.Data(),particle.Data()));  
  
  TGraphAsymmErrors *gRpAstat = new TGraphAsymmErrors(nbins,xbins,yRpA,exl,exl,yRpAStat,yRpAStat);
  gRpAstat->SetTitle("RpA stat uncertainty from pPb");
  gRpAstat->SetMarkerStyle(21);
  gRpAstat->SetMarkerColor(1);
  gRpAstat->SetLineColor(1);
  gRpAstat->SetLineWidth(3);  
  gRpAstat->SetFillColor(0);
  
  TGraphAsymmErrors *gRpAsyst = new TGraphAsymmErrors(nbins,xbins,yRpA,exl,exl,yRpPbSystTotLow,yRpPbSystTotHigh);
  gRpAsyst->SetTitle("RpA syst uncertainty from pPb");
  gRpAsyst->SetFillColor(kYellow-7);//###0
  //gRpAsyst->SetMarkerSize(0);
  gRpAsyst->SetLineColor(1);
  gRpAsyst->SetLineWidth(3);
  gRpAsyst->SetFillStyle(1001);
  gRpAsyst->SetMarkerStyle(21);
  gRpAsyst->SetMarkerColor(1);
  TGraphAsymmErrors*gRpAsyst2=(TGraphAsymmErrors*)gRpAsyst->Clone();
  gRpAsyst2->SetMarkerColor(1);
  gRpAsyst2->SetMarkerStyle(25);  
  gRpAsyst2->SetFillColor(0);
  gRpAsyst2->SetFillStyle(0);
  gRpAsyst2->SetLineColor(1);//5
  gRpAsyst2->SetLineStyle(1);
  gRpAsyst2->SetLineWidth(3);


   
  TGraphAsymmErrors *gRpAsystFONLL = new TGraphAsymmErrors(nbins,xbins,yFONLL,exl,exl,yRpAsystFONLLlow,yRpAsystFONLLhigh);
  gRpAsystFONLL->SetTitle("RpA syst uncertainty from FONLL reference");
  gRpAsystFONLL->SetFillColor(0);//5,kYellow-7
  gRpAsystFONLL->SetLineColor(kAzure-3);//5
  gRpAsystFONLL->SetMarkerColor(4);//kAzure-3);
  gRpAsystFONLL->SetLineStyle(2);//###1
  gRpAsystFONLL->SetLineWidth(3);


  TGraphAsymmErrors*gRpAsystFONLL2=(TGraphAsymmErrors*)gRpAsystFONLL->Clone(); 
  gRpAsystFONLL2->SetMarkerColor(1);
  gRpAsystFONLL2->SetMarkerStyle(25);  
  gRpAsystFONLL2->SetFillColor(0);
  gRpAsystFONLL2->SetFillStyle(0);
  gRpAsystFONLL2->SetLineColor(kAzure-3);//5
  gRpAsystFONLL2->SetLineStyle(2);//###1
  gRpAsystFONLL2->SetLineStyle(3);
  gRpAsystFONLL2->SetLineWidth(3);

//###
  TBox *gRpAsystFONLL_box0 = new TBox(xbins[0]-exl[0],yFONLL[0]-yRpAsystFONLLlow[0],xbins[0]+exl[0],yFONLL[0]+yRpAsystFONLLhigh[0]);
  gRpAsystFONLL_box0->SetLineColor(kAzure-3);
  gRpAsystFONLL_box0->SetLineWidth(3);
  gRpAsystFONLL_box0->SetLineStyle(2);
  gRpAsystFONLL_box0->SetFillColor(0);
  gRpAsystFONLL_box0->SetFillStyle(0);

  TBox *gRpAsystFONLL_box1 = new TBox(xbins[1]-exl[1],yFONLL[1]-yRpAsystFONLLlow[1],xbins[1]+exl[1],yFONLL[1]+yRpAsystFONLLhigh[1]);
  gRpAsystFONLL_box1->SetLineColor(kAzure-3);
  gRpAsystFONLL_box1->SetLineWidth(3);
  gRpAsystFONLL_box1->SetLineStyle(2);
  gRpAsystFONLL_box1->SetFillColor(0);
  gRpAsystFONLL_box1->SetFillStyle(0);

  TBox *gRpAsystFONLL_box2 = new TBox(xbins[2]-exl[2],yFONLL[2]-yRpAsystFONLLlow[2],xbins[2]+exl[2],yFONLL[2]+yRpAsystFONLLhigh[2]);
  gRpAsystFONLL_box2->SetLineColor(kAzure-3);
  gRpAsystFONLL_box2->SetLineWidth(3);
  gRpAsystFONLL_box2->SetLineStyle(2);
  gRpAsystFONLL_box2->SetFillColor(0);
  gRpAsystFONLL_box2->SetFillStyle(0);

  TBox *gRpAsystFONLL_box3 = new TBox(xbins[3]-exl[3],yFONLL[3]-yRpAsystFONLLlow[3],xbins[3]+exl[3],yFONLL[3]+yRpAsystFONLLhigh[3]);
  gRpAsystFONLL_box3->SetLineColor(kAzure-3);
  gRpAsystFONLL_box3->SetLineWidth(3);
  gRpAsystFONLL_box3->SetLineStyle(2);
  gRpAsystFONLL_box3->SetFillColor(0);
  gRpAsystFONLL_box3->SetFillStyle(0);

  TBox *gRpAsystFONLL_box4 = new TBox(xbins[4]-exl[4],yFONLL[4]-yRpAsystFONLLlow[4],xbins[4]+exl[4],yFONLL[4]+yRpAsystFONLLhigh[4]);
  gRpAsystFONLL_box4->SetLineColor(kAzure-3);
  gRpAsystFONLL_box4->SetLineWidth(3);
  gRpAsystFONLL_box4->SetLineStyle(2);
  gRpAsystFONLL_box4->SetFillColor(0);
  gRpAsystFONLL_box4->SetFillStyle(0);

  // Histogram style
  TH1D* hRpAsystFONLL = new TH1D("hRpAsystFONLL","",nbins,xhbins);
  TH1D* hRpAsystFONLLEYhigh = new TH1D("hRpAsystFONLLEYhigh","",nbins,xhbins);
  TH1D* hRpAsystFONLLEYlow = new TH1D("hRpAsystFONLLEYlow","",nbins,xhbins);
  
  for (int i=0;i<nbins;i++){
    hRpAsystFONLL->SetBinContent(i+1,yFONLL[i]);
    hRpAsystFONLLEYhigh->SetBinContent(i+1,yFONLL[i]+yRpAsystFONLLhigh[i]);
    hRpAsystFONLLEYlow->SetBinContent(i+1,yFONLL[i]-yRpAsystFONLLlow[i]);
    }
 
    hRpAsystFONLLEYhigh->SetLineColor(kAzure-3);
    hRpAsystFONLLEYhigh->SetLineWidth(3);
    hRpAsystFONLLEYhigh->SetLineStyle(2);
    hRpAsystFONLLEYhigh->SetFillColor(0);
    hRpAsystFONLLEYhigh->SetFillStyle(0);

    hRpAsystFONLLEYlow->SetLineColor(kAzure-3);
    hRpAsystFONLLEYlow->SetLineWidth(3);
    hRpAsystFONLLEYlow->SetLineStyle(2);
    hRpAsystFONLLEYlow->SetFillColor(0);
    hRpAsystFONLLEYlow->SetFillStyle(0);
    hRpAsystFONLLEYlow->Draw("][,same");


  //###TCanvas *canvasRpA=new TCanvas("canvasRpA","canvasRpA",600,500);   
  TCanvas *canvasRpA=new TCanvas("canvasRpA","canvasRpA",500,500);   
  
  canvasRpA->Range(-1.989924,-0.2917772,25.49622,2.212202);
  canvasRpA->SetFillColor(0);
  canvasRpA->SetBorderMode(0);
  canvasRpA->SetBorderSize(2);
  canvasRpA->SetLeftMargin(0.200);
  canvasRpA->SetRightMargin(0.025);
  canvasRpA->SetTopMargin(0.080);
  canvasRpA->SetBottomMargin(0.150);
  canvasRpA->SetFrameBorderMode(0);
  canvasRpA->SetFrameBorderMode(0);
  
  //###TLegend *legendRpA=new TLegend(0.23,0.58,0.57,0.77,"");
  TLegend *legendRpA=new TLegend(0.23,0.56,0.57,0.77,"");

  legendRpA->SetBorderSize(0);
  legendRpA->SetLineColor(0);
  legendRpA->SetFillColor(0);
  legendRpA->SetFillStyle(1001);
  legendRpA->SetTextFont(42);
  legendRpA->SetTextSize(0.055);//###0.04

  hempty=new TH2F("hempty","",4,-3.5,2.7,40.,0.,4.0);  
  hempty->GetXaxis()->SetTitle("y_{CM}");
  /*
  if(particle=="Bplus") hempty->GetYaxis()->SetTitle("R^{FONLL}_{pA}  (B^{+})");
  if(particle=="Bzero") hempty->GetYaxis()->SetTitle("R^{FONLL}_{pA}  (B^{0})");
  if(particle=="Bs") hempty->GetYaxis()->SetTitle("R^{FONLL}_{pA}  (B_{s})");
  */
  hempty->GetYaxis()->SetTitle("R^{FONLL}_{pA}");

  hempty->GetXaxis()->CenterTitle();
  hempty->GetYaxis()->CenterTitle();
  hempty->GetXaxis()->SetTitleOffset(0.90);
  hempty->GetYaxis()->SetTitleOffset(1.34);
  hempty->GetXaxis()->SetTitleSize(0.070);//###0.045
  hempty->GetYaxis()->SetTitleSize(0.070);//###0.045
  hempty->GetXaxis()->SetTitleFont(42);
  hempty->GetYaxis()->SetTitleFont(42);
  hempty->GetXaxis()->SetLabelFont(42);
  hempty->GetYaxis()->SetLabelFont(42);
  hempty->GetXaxis()->SetLabelSize(0.060);//###0.040
  hempty->GetYaxis()->SetLabelSize(0.060);//###0.040  
  hempty->SetMaximum(4.0);
  hempty->SetMinimum(0.);
  hempty->Draw();
  
  TLine *l = new TLine(0,1,70,1);
  l->SetLineStyle(2);
  
  legendRpA->Draw();
  gRpAstat->SetMarkerStyle(21);
  gRpAstat->SetLineColor(1);
  gRpAstat->SetMarkerColor(1);

for (int i=0;i<nbins;i++){
         double xgae,ygae;
         gRpAstat->GetPoint(i,xgae,ygae);
         std::cout << "####### RpA (" << i << ") : " << ygae << " stat: " << gRpAstat->GetEYhigh()[i] << " syst: " << gRpAsyst->GetEYhigh()[i] << std::endl;
         std::cout << "########### FONLL (" << i << ") : " << " + " << gRpAsystFONLL->GetEYhigh()[i] << " - " << gRpAsystFONLL->GetEYlow()[i] << std::    endl;
}

  //###gRpAsystFONLL->Draw("e2same");//###2same
  //###gRpAsystFONLL2->Draw("2esame");//###2same
/*
  gRpAsystFONLL_box0->Draw("same");
  gRpAsystFONLL_box1->Draw("same");
  gRpAsystFONLL_box2->Draw("same");
  gRpAsystFONLL_box3->Draw("same");
  gRpAsystFONLL_box4->Draw("same");
*/
  hRpAsystFONLLEYlow->Draw("][,same");
  hRpAsystFONLLEYhigh->Draw("][,same");
 
  gRpAsyst->Draw("2esame");
  gRpAsyst2->Draw("2esame");
  gRpAstat->Draw("psame");
  
  std::cout << "%%%%%%% RpA commonError " << " - " << commonErrorN << " + " << commonErrorP << std::endl;
  TBox *b = new TBox(-3.49,1-commonErrorN,-3.1,1+commonErrorP);
  b->SetLineColor(1);
  b->SetLineWidth(1);
  b->SetLineStyle(1);
  b->SetFillColor(kGray);
  b->Draw();
  
  TBox *b2=(TBox*)b->Clone();
  b2->SetFillStyle(0);
  b2->SetLineStyle(1);
  b2->SetLineWidth(1);
  b2->Draw();

  TGraphAsymmErrors*gRpAstat2=(TGraphAsymmErrors*)gRpAstat->Clone();
  gRpAstat2->SetTitle("RpA stat uncertainty from pPb");
  gRpAstat2->SetMarkerStyle(21);
  gRpAstat2->SetMarkerColor(1);
  gRpAstat2->SetLineColor(1);
  gRpAstat2->SetLineWidth(3);  
  gRpAstat2->SetFillColor(0);
  gRpAstat2->SetMarkerSize(1.5);
  
  TLegendEntry *ent_RpAstat=legendRpA->AddEntry(gRpAstat2,"R^{FONLL}_{pA}","p");
  ent_RpAstat->SetTextFont(42);
  ent_RpAstat->SetMarkerColor(1);
  ent_RpAstat->SetMarkerStyle(25);
  ent_RpAstat->SetMarkerSize(30);//###3
  //ent_RpAstat->SetFillColor(kYellow-7);//5
  //ent_RpAstat->SetFillStyle(1001);
  ent_RpAstat->SetLineColor(1);
  ent_RpAstat->SetLineWidth(3);
  ent_RpAstat->SetLineStyle(0);
 
   //TLegendEntry *ent_RpAsyst=legendRpA->AddEntry(gRpAsyst,"R^{FONLL}_{pA}","pf");
  TLegendEntry *ent_RpAsyst=legendRpA->AddEntry(gRpAsyst,"Syst. pPb data","f");
  ent_RpAsyst->SetTextFont(42);
  ent_RpAsyst->SetMarkerColor(1);
  ent_RpAsyst->SetMarkerStyle(25);
  //ent_RpAsyst->SetFillColor(kYellow-7);//5
  //ent_RpAsyst->SetFillStyle(1001);
  ent_RpAsyst->SetLineColor(1);
  ent_RpAsyst->SetLineWidth(3);
  ent_RpAsyst->SetLineStyle(0);
 
  TLegendEntry *ent_RpAsystFONLL=legendRpA->AddEntry(gRpAsystFONLL,"Syst. FONLL pp ref.","f");
  ent_RpAsystFONLL->SetTextFont(42);
  ent_RpAsystFONLL->SetLineColor(kAzure-3);//###5
  ent_RpAsystFONLL->SetLineStyle(1);//###0
  ent_RpAsystFONLL->SetLineWidth(3);
  ent_RpAsystFONLL->SetMarkerColor(kYellow-7);//5

   //###TLegendEntry *ent_RpAsystData=legendRpA->AddEntry(b,"Syst. int. lumi + BR","f");
  TLegendEntry *ent_RpAsystData=legendRpA->AddEntry(b,"Syst. int. lumi + B","f");
  ent_RpAsystData->SetTextFont(42);
  ent_RpAsystData->SetLineColor(2);
  ent_RpAsystData->SetMarkerColor(2);
  
  tlatex1->Draw();
  //###tlatex12->Draw();


  tlatexlumi->Draw();
  tlatex3->Draw();
  tlatex4->Draw();


  canvasRpA->SaveAs(Form("../Results%s_y/canvasRpA%s.pdf",particle.Data(),particle.Data()));  
  canvasRpA->SaveAs(Form("../Results%s_y/canvasRpA%s.png",particle.Data(),particle.Data()));  

  canvasRpA->SaveAs(Form("../Results%s_y/canvasRpA%s.eps",particle.Data(),particle.Data()));  
  canvasRpA->SaveAs(Form("../Results%s_y/canvasRpA%s.ps",particle.Data(),particle.Data()));  


/*
  TCanvas *canvasRFB=new TCanvas("canvasRFB","canvasRFB",500,500);   
  canvasRFB->cd();
  canvasRFB->Range(-1.989924,-0.2917772,25.49622,2.212202);
  canvasRFB->SetFillColor(0);
  canvasRFB->SetBorderMode(0);
  canvasRFB->SetBorderSize(2);
  canvasRFB->SetLeftMargin(0.1451613);
  canvasRFB->SetRightMargin(0.05443548);
  canvasRFB->SetTopMargin(0.08474576);
  canvasRFB->SetBottomMargin(0.1165254);
  canvasRFB->SetFrameBorderMode(0);
  canvasRFB->SetFrameBorderMode(0);
 
  TLegend *legendRFB=new TLegend(0.2560484,0.6490486,0.5483871,0.7716702,"");
  legendRFB->SetBorderSize(0);
  legendRFB->SetLineColor(0);
  legendRFB->SetFillColor(0);
  legendRFB->SetFillStyle(1001);
  legendRFB->SetTextFont(42);
  legendRFB->SetTextSize(0.045);

  TH2F* hempty=new TH2F("hempty","",4,-0.1,2.,10.,0.,2.5);  
  hempty->GetXaxis()->SetTitle("|y_{CM}|");
  hempty->GetYaxis()->SetTitle("R_{FB}");
  hempty->GetXaxis()->SetTitleOffset(1.1);
  hempty->GetYaxis()->SetTitleOffset(1.3);
  hempty->GetXaxis()->SetTitleSize(0.045);
  hempty->GetYaxis()->SetTitleSize(0.045);
  hempty->GetXaxis()->SetTitleFont(42);
  hempty->GetYaxis()->SetTitleFont(42);
  hempty->GetXaxis()->SetLabelFont(42);
  hempty->GetYaxis()->SetLabelFont(42);
  hempty->GetXaxis()->SetLabelSize(0.04);
  hempty->GetYaxis()->SetLabelSize(0.04);  
  hempty->GetXaxis()->CenterTitle();
  hempty->GetYaxis()->CenterTitle();
  hempty->SetMaximum(3);
  hempty->SetMinimum(0.);
  hempty->Draw();
  legendRFB->Draw();

  TGraphAsymmErrors *gRFBsyst = new TGraphAsymmErrors(nbinsRFB,xbinsRFB,yRFB,exlRFB,exlRFB,yRFBSystTotLow,yRFBSystTotHigh);
  gRFBsyst->SetTitle("gRFBsyst");
   gRFBsyst->SetName("gRFBsyst");
  gRFBsyst->SetMarkerColor(4);
  gRFBsyst->SetLineColor(4);
  gRFBsyst->SetLineWidth(2);   
  gRFBsyst->SetMarkerStyle(0);
  gRFBsyst->SetMarkerColor(1);


  


  TGraphAsymmErrors *gRFBstat = new TGraphAsymmErrors(nbinsRFB,xbinsRFB,yRFB,exlRFB,exlRFB,yRFBStat, yRFBStat);
  gRFBstat->SetTitle("gRFBstat");
  gRFBstat->SetName("gRFBstat");
  gRFBstat->SetMarkerColor(1);
  gRFBstat->SetLineColor(1);
  gRFBstat->SetLineWidth(2);   
  gRFBstat->SetMarkerStyle(21);
  gRFBstat->SetMarkerColor(1);
  gRFBstat->SetFillColor(0);
  gRFBstat->SetFillStyle(0);

  gRFBsyst->SetFillColor(0);
  gRFBsyst->SetFillStyle(0);
  
  gRFBsyst->Draw("2same");
  gRFBstat->Draw("psame");



  TLegendEntry *ent_RFB=legendRFB->AddEntry(gRFBstat,"R_{FB} stat.","pl");
  ent_RFB->SetTextFont(42);
  ent_RFB->SetLineColor(2);
  ent_RFB->SetMarkerColor(2);

  TLegendEntry *ent_RFB=legendRFB->AddEntry(gRFBsyst,"R_{FB} syst.","l");
  ent_RFB->SetTextFont(42);
  ent_RFB->SetLineColor(4);
  // ent_RFB->SetMarkerColor(2);

  TLatex * tlatex1=new TLatex(0.1612903,0.8625793,"CMS                 pPb #sqrt{s_{NN}}= 5.02 TeV");
  tlatex1->SetNDC();
  tlatex1->SetTextColor(1);
  tlatex1->SetTextFont(42);
  tlatex1->SetTextSize(0.045);
  tlatex1->Draw();

    
  TLatex * tlatexlumi=new TLatex(0.671371,0.7801268,"L = 34.8 nb^{-1}");
  tlatexlumi->SetNDC();
  tlatexlumi->SetTextColor(1);
  tlatexlumi->SetTextFont(42);
  tlatexlumi->SetTextSize(0.045);
  tlatexlumi->Draw();

  
  //double xpos1=0.6528226;
  // double ypos1=0.6849894;
  TLatex * tlatex3=new TLatex(xpos,ypos,mypar.Data());
   tlatex3->SetNDC();
  tlatex3->SetTextColor(1);
  tlatex3->SetTextFont(42);
  tlatex3->SetTextSize(0.06);
  tlatex3->Draw();
  canvasRFB->SaveAs(Form("../Results%s_y/CanvasRDFB.pdf",particle.Data(),particle.Data()));

  */


//  l->Draw();  
//  canvasRpA->SaveAs(Form("../Results%sY/canvasRpA%s.pdf",particle.Data(),particle.Data()));  
//  canvasRpA->SaveAs(Form("../Results%s_y/canvasRpA%s.pdf",particle.Data(),particle.Data()));  
  
//  TFile *fout=new TFile(Form("../Results%sY/fileRpA%s.root",particle.Data(),particle.Data()),"recreate");  
  TFile *fout=new TFile(Form("../Results%s_y/fileRpA%s.root",particle.Data(),particle.Data()),"recreate");  
  fout->cd();
  gSigmasyst->SetName("gSigmasyst");
  gSigmasyst->Write();
  gaeBplusReference->Write();
  hSigmapPbStat->Write();






}