ROOT logo
#include"TF1.h"
#include"TH1D.h"
#include"TH2F.h"
#include"TMath.h"
#include"TSystem.h"
#include"TCanvas.h"
#include"TFile.h"
#include"TGraphErrors.h"
#include"AliPIDperfContainer.h"
#include"TRandom.h"

Int_t LoadLib();
void doeffPr(Int_t pos=1,Float_t prob=0.1,Float_t etaminkp=-0.8,Float_t etamaxkp=0.8);
TH2F *GetHistoPrp(Float_t pt=1,Float_t ptM=1.1,Float_t pMinkp=0,Float_t pMinkn=0.,Float_t etaminkp=-0.8,Float_t etamaxkp=0.8);
TH2F *GetHistoPrn(Float_t pt=1,Float_t ptM=1.1,Float_t pMinkn=0,Float_t pMinkp=0.,Float_t etaminkp=-0.8,Float_t etamaxkp=0.8);
void fit(TH1D *h,Float_t *a=NULL,char *opt="",char *opt2="",Float_t pt=1.5);
void AddHisto(TH2F *h1,TH2F *h2,Float_t w);

TObject* fContPid1;
TObject* fContPid2;
const Int_t nBinPid = 14; // pt,eta, ptPip, ptPin, PPip, PPin, TOF3sigmaPip, TOF3sigmaPin, isPhiTrue, nsigmaPip, nsigmaPin
// 0.985 < mass < 1.045 (60) and 0 < centrality < 100 (10)
Int_t binPid[nBinPid] = {1/*ptPhi*/,8/*EtaPr*/,20/*pt+*/,20/*pt-*/,5/*P+*/,1/*P-*/,2/*TOFmatch+*/,2/*TOFmatch-*/,2/*istrue*/,4/*Nsigma+*/,4/*Nsigma-*/,1/*DeltaPhi+*/,1/*DeltaPhi-*/,1/*Psi*/};
Float_t xmin[nBinPid] = {1,-0.8,0.3,0.3,0,0,-0.5,-0.5,-0.5,0,0,-TMath::Pi(),-TMath::Pi(),-TMath::Pi()/2};
Float_t xmax[nBinPid] = {5,0.8,4.3,4.3,1,1,1.5,1.5,1.5,7.5,7.5,TMath::Pi(),TMath::Pi(),TMath::Pi()/2};

TF1 *fsign;
TF1 *fall;
TF1 *fback;

Int_t ifunc=0;

Float_t fitmin = 1.1;
Float_t fitmax = 1.15;

Int_t cmin = 1; // min 1
Int_t cmax = 10; // max 10

Float_t weightS = -1.;

Int_t rebinsize = 1;

Int_t parplotted = 2;

Bool_t isMC = kFALSE; // don't change this (is set automatically)
Bool_t selectTrue = kTRUE; // put it to true to remove background (only for MC)
Bool_t keepTrue = kFALSE; // put it to false to fit only background (only for MC)

Bool_t kGoodMatch = kFALSE; // to check good matching

Bool_t kSigma2vs3 = kFALSE; // to check good matching

Bool_t require5sigma = kFALSE; // don't touch this flag

Bool_t bayesVsigma = kFALSE; // only to do checks

Bool_t kTOFmatch = kFALSE; // for combined PID requires TOF matching

Bool_t kOverAll = kFALSE;
Bool_t kOverAllTOFmatch = kFALSE;
Bool_t kOverAll2Sigma = kFALSE;
Bool_t kPid2Sigma = kFALSE;
Bool_t kPid3Sigma = kFALSE;

TH2F *hmatched;
TH2F *htracked;

Bool_t kLoaded=kFALSE;
Int_t LoadLib(){
  weightS = -1.;

  require5sigma = kFALSE;

  if(! kLoaded){
    gSystem->Load("libVMC.so");
    gSystem->Load("libPhysics.so");
    gSystem->Load("libTree.so");
    gSystem->Load("libMinuit.so");
    gSystem->Load("libSTEERBase.so");
    gSystem->Load("libANALYSIS.so");
    gSystem->Load("libAOD.so");
    gSystem->Load("libESD.so");
    gSystem->Load("libANALYSIS.so");
    gSystem->Load("libANALYSISalice.so");
    gSystem->Load("libCORRFW.so");
    gSystem->Load("libNetx.so");
    gSystem->Load("libPWGPPpid.so");

    TFile *f = new TFile("AnalysisResults.root");
    TList *l = (TList *) f->Get("contLambdaBayes1");
    TList *l2 = (TList *) f->Get("contLambdaBayes2");

    if(!(l && l2)) return 0;

    fContPid1 = (AliPIDperfContainer *) l->FindObject("contPID");
    fContPid2 = (AliPIDperfContainer *) l->FindObject("contPID2");

    hmatched = (TH2F *) l2->FindObject("hMatchPr"); 
    htracked = (TH2F *) l2->FindObject("hTrackingPr"); 
  }
  kLoaded = kTRUE;

  // check if MC
  Float_t x[] = {xmin[0]+0.001,xmin[1]+0.001,xmin[2]+0.001,xmin[3]+0.001,xmin[4]+0.001,xmin[5]+0.001,xmin[6]+0.001,xmin[7]+0.001,1/*trueMC*/,xmin[9],xmin[10],xmin[11],xmin[12],xmin[13]};
  Float_t x2[] = {xmax[0],xmax[1],xmax[2],xmax[3],xmax[4],xmax[5],xmax[6],xmax[7],xmax[8],xmax[9],xmax[10],xmax[11],xmax[12],xmax[13]};

  AliPIDperfContainer *tmp = (AliPIDperfContainer *) fContPid2;
  TH1D *h = tmp->GetQA(0, x, x2)->ProjectionX("checkMC");

  if(h->GetEntries()) isMC = kTRUE;
  else isMC=kFALSE;

  if(!isMC){
    selectTrue = kFALSE;
    keepTrue = kTRUE;
  }
  else{
    printf("MC truth found!!!!!!\nIt is MC!!!!!!");
  }

  fsign = new TF1("fsign","gaus(0) +0*0.5*[0]*TMath::Exp(-[3]*TMath::Abs(x-[1]))",fitmin,fitmax);
  fback = new TF1("fback","pol2*(x-0.938-0.139)*(x > 0.938+0.139)",fitmin,fitmax);
  fall = new TF1("fall","gaus(0) +0*0.5*[0]*TMath::Exp(-[3]*TMath::Abs(x-[1])) + pol2(4)*(x-0.938-0.139)*(x > 0.938+0.139)",fitmin,fitmax);

  fsign->SetLineColor(2);
  fback->SetLineColor(4);

  if(kSigma2vs3){
    kGoodMatch=kFALSE;
    kOverAll = 0;
  }

  if(bayesVsigma){
    kOverAll = 0;
    kGoodMatch=kFALSE;
    kSigma2vs3=kFALSE;
    kTOFmatch=kTRUE;
    weightS = -0.7;
  }

  return 1;
}

void doeffPr(Int_t pos,Float_t prob,Float_t etaminkp,Float_t etamaxkp){
  LoadLib();
  TH1D *hm = hmatched->ProjectionX("matchingPrEff",cmin,cmax);
  TH1D *ht = htracked->ProjectionX("tracking",cmin,cmax);

  hm->GetYaxis()->SetTitle("TOF matching eff.");
  hm->SetTitle("Using probability as weights");

  hm->Sumw2();
  ht->Sumw2();

  hm->Divide(hm,ht,1,1,"B");
 
  Int_t nptbin = binPid[2];
  Float_t minptbin = xmin[2];
  Float_t maxptbin = xmax[2];

  if(pos == 0){
    nptbin = binPid[3];
    minptbin = xmin[3];
    maxptbin = xmax[3];
  }

  if(prob > 0.1999|| kPid3Sigma ||kPid2Sigma){
    kGoodMatch = kFALSE;
    kSigma2vs3 = kFALSE;
//    if(! kOverAll) require5sigma = kTRUE;
    if(!isMC) weightS = -0.95;
  }

  TCanvas *c1 = new TCanvas();
  c1->Divide((nptbin+1)/2,2);
  TH2F *hh,*hh2;
  TH1D *h;
  char name[100];
  Float_t b[50][3];

  Double_t xx[50],yy[50];
  Double_t exx[50],eyy[50];

  for(Int_t i=0;i < nptbin;i++){
    c1->cd(i+1);//->SetLogy();
    Float_t ptmin = minptbin+(maxptbin-minptbin)/nptbin*(i);
    Float_t ptmax = minptbin+(maxptbin-minptbin)/nptbin*(i+1);

    xx[i] = (ptmin+ptmax)/2;
    exx[i] = (-ptmin+ptmax)/2;

    Float_t pp=0.1;
    if(prob < 0.2) pp = 0.;
    if(pos) hh=GetHistoPrp(ptmin,ptmax,pp,0.0,etaminkp,etamaxkp);
    else hh=GetHistoPrn(ptmin,ptmax,pp,0.0);
    sprintf(name,"TOF matched: %f < p_{T} < %f GeV/#it{c}",ptmin,ptmax);
    hh->SetTitle(name);
    sprintf(name,"hNoPid%i",i);
    
    pp=prob;
    if(prob < 0.2) pp = 0.1;
    if(pos) hh2=GetHistoPrp(ptmin,ptmax,pp,0.0,etaminkp,etamaxkp);
    else hh2=GetHistoPrn(ptmin,ptmax,pp,0.0);
    AddHisto(hh,hh2,weightS);

    h = hh->ProjectionX(name,cmin,cmax);
    h->RebinX(rebinsize);
    h->Draw("ERR");
    h->SetMarkerStyle(24);
    b[i][0]=-1;
    Int_t ntrial = 0;
    Float_t chi2 = 10000;
    while(ntrial < 3 && (chi2 > 20 + 1000*selectTrue)){
      fit(h,b[i],"WW","",xx[i]);
      c1->Update();
//       getchar();
      fit(h,b[i],"","",xx[i]);
      ntrial++;
      chi2 = b[i][2];
      printf("chi2 = %f\n",chi2);
      c1->Update();
//       getchar();
      
    }

    yy[i] = fall->GetParameter(parplotted);
    eyy[i] = fall->GetParError(parplotted);
  }

  TGraphErrors *gpar = new TGraphErrors(nptbin,xx,yy,exx,eyy);
  c1->cd(8);
//   gpar->Draw("AP");
  gpar->SetMarkerStyle(20);

  TCanvas *c2 = new TCanvas();
  c2->Divide((nptbin+1)/2,2);
  Float_t b2[50][3];

  for(Int_t i=0;i < nptbin;i++){
    c2->cd(i+1);
    Float_t ptmin = minptbin+(maxptbin-minptbin)/nptbin*(i);
    Float_t ptmax = minptbin+(maxptbin-minptbin)/nptbin*(i+1);

    Float_t pp=prob;
    if(prob < 0.2) pp = 0.1;
    if(pos) hh=GetHistoPrp(ptmin,ptmax,pp,0.0,etaminkp,etamaxkp);
    else hh=GetHistoPrn(ptmin,ptmax,pp,0.0);
    sprintf(name,"P_{TOF} > 0.8: %f < p_{T} < %f GeV/#it{c}",ptmin,ptmax);
    hh->SetTitle(name);
    sprintf(name,"hPid60_%i",i);
    h = hh->ProjectionX(name,cmin,cmax);
    h->RebinX(rebinsize);
    h->Draw("ERR");
    h->SetMarkerStyle(24);
    b2[i][0]=-1;
    Int_t ntrial = 0;
    Float_t chi2 = 10000;
    while(ntrial < 3 && (chi2 > 20 + 1000*selectTrue)){
      fit(h,b2[i],"WW","",xx[i]);
      fit(h,b2[i],"","",xx[i]);
      ntrial++;
      chi2 = b2[i][2];
      printf("chi2 = %f\n",chi2);
    }
    yy[i] = fall->GetParameter(parplotted);
    eyy[i] = fall->GetParError(parplotted);

  }

  TGraphErrors *gpar2 = new TGraphErrors(nptbin,xx,yy,exx,eyy);
  c2->cd(8);
//   gpar2->Draw("AP");
  gpar2->SetMarkerStyle(20);
  
  Double_t xpt[50],expt[50],eff[50],efferr[50];
  for(Int_t i=0;i<nptbin;i++){
    printf("%f +/- %f -  %f +/- %f\n",b[i][0],b[i][1],b2[i][0],b2[i][1]);

    Float_t ptmin = minptbin+(maxptbin-minptbin)/nptbin*(i);
    Float_t ptmax = minptbin+(maxptbin-minptbin)/nptbin*(i+1);

    xpt[i] = (ptmin+ptmax)/2;
    expt[i] = (-ptmin+ptmax)/2;
    eff[i] = b2[i][0]/(b[i][0]-b2[i][0]*weightS);

    //    b[i][0] = b[i][0]-b2[i][0]*weightS;

    //    efferr[i] = TMath::Sqrt(b[i][1]*b[i][1]/b[i][0]/b[i][0] + b2[i][1]*b2[i][1]/b2[i][0]/b2[i][0])*(b2[i][0]+b2[i][1])*(1+weightS*(b2[i][0]-b2[i][1])/b[i][0])/b[i][0];//*(1-eff[i]);//der2*der2*(b[i][1]*b[i][1] - b2[i][1]*b2[i][1]));

    efferr[i] = 1./(b[i][0]-b2[i][0]*weightS)/(b[i][0]-b2[i][0]*weightS)*TMath::Sqrt(b[i][0]*b[i][0]*b2[i][1]*b2[i][1] + b2[i][0]*b2[i][0]*b[i][1]*b[i][1]);

    if(TMath::Abs(efferr[i]) > 1)efferr[i]=1;
  }
  new TCanvas();
  TGraphErrors *geff = new TGraphErrors(nptbin,xpt,eff,expt,efferr);
  geff->Draw("AP");

  char flag[100];
  flag[0] = '\0';

  if(isMC){
    if(selectTrue) sprintf(flag,"true");
    else if(!keepTrue) sprintf(flag,"back");
  }

  char flag2[100];
  flag2[0] = '\0';

  Bool_t kWriteME = kFALSE;

  char etarange[100];
  sprintf(etarange,"_%.1f-%.1f_",etaminkp,etamaxkp);

  if(kGoodMatch)
    sprintf(flag2,"GM");

  if(bayesVsigma)
    sprintf(flag2,"BayesVsSigma");

  if(kSigma2vs3)
    sprintf(flag2,"Sigma2vs3");

  if(kOverAll)
    sprintf(flag2,"OverAll");
  if(kOverAllTOFmatch)
    sprintf(flag2,"OverAllTOF");
  if(kOverAll2Sigma)
    sprintf(flag2,"OverAll2sigma");

  if(kPid3Sigma)
    sprintf(flag2,"pid3sigma");
  if(kPid2Sigma)
    sprintf(flag2,"pid2sigma");

  if(pos){
    if(prob >=0.2) sprintf(name,"protonPos%sP%iEff%i_%i%s%s.root",etarange,Int_t(prob*100),(cmin-1)*10,cmax*10,flag,flag2);
    else{
      sprintf(name,"protonPos%sMatchEff%i_%i%s%s.root",etarange,(cmin-1)*10,cmax*10,flag,flag2);
      if(!(kOverAll || bayesVsigma || kGoodMatch || kSigma2vs3)) kWriteME = kTRUE;
    }
  }
  else{
    if(prob >=0.2) sprintf(name,"protonNeg%sP%iEff%i_%i%s%s.root",etarange,Int_t(prob*100),(cmin-1)*10,cmax*10,flag,flag2);
    else sprintf(name,"protonNeg%sMatchEff%i_%i%s%s.root",etarange,(cmin-1)*10,cmax*10,flag,flag2);
  }

  geff->SetTitle("p efficiency (from (anti)#Lambda);p_{T} (GeV/#it{c};efficiency");
  TFile *fout = new TFile(name,"RECREATE");
  geff->Write();
  if(kWriteME) hm->Write();
  fout->Close();

  if(kWriteME) hm->Draw("SAME");
}

TH2F *GetHistoPrp(Float_t pt,Float_t ptM,Float_t pMinkp,Float_t pMinkn,Float_t etaminkp,Float_t etamaxkp){

  Float_t x[] = {xmin[0]+0.001,etaminkp+0.001,pt+0.001,xmin[3]+0.001,pMinkp+0.001,pMinkn+0.001,(pMinkp>0.09 || kPid3Sigma||kPid2Sigma)+0.001,kTOFmatch+0.001,selectTrue,xmin[9],xmin[10],xmin[11],xmin[12],xmin[13]};
  Float_t x2[] = {xmax[0],etamaxkp-0.001,ptM-0.001,xmax[3],xmax[4],xmax[5],xmax[6],xmax[7],keepTrue,xmax[9],xmax[10],xmax[11],xmax[12],xmax[13]};

  if(kOverAll){
    x[6] = 0.0001;
    x2[9] = 5.9;
    if(pMinkp > 0.19) x2[9] = 4.9;
  }

  if(kOverAllTOFmatch && pMinkp > 0.19){
    x[6] = 1.0001;
    x2[9] = 4.9;
  }

  if(kOverAll2Sigma && pMinkp > 0.09){
    x2[9] = 2;
    x[6] = 1.0001;
  }

  if(kGoodMatch){
    x[6] = 1.0001;
    if(pMinkp > 0)
      x2[9] = 4.9;
      
  }

  if(kTOFmatch){
    x[6] = 1.0001;
  }

  if(kSigma2vs3){
    x[6] = 1.0001;
    x2[9] = 3;
    if(pMinkp > 0)
      x2[9] = 2;
  }

  if(bayesVsigma){
    if(pMinkp > 0){
      x[4] = 0.2001;
      x2[9] = 5;
    }
    else{
      x2[9] = 3;
    }

    
  }

  if(require5sigma) x2[9] = 4.9;
  if(kPid3Sigma && pMinkp>0.09) x2[9] = 2.9;
  if(kPid2Sigma && pMinkp>0.09) x2[9] = 1.9;

  AliPIDperfContainer *tmp = (AliPIDperfContainer *) fContPid1;

  TH2F *h = tmp->GetQA(0, x, x2);

  h->GetXaxis()->SetTitle("M_{#Lambda} (GeV/#it{c}^{2})");
  h->GetYaxis()->SetTitle("centrality [%]");

  return h;
}

TH2F *GetHistoPrn(Float_t pt,Float_t ptM,Float_t pMinkn,Float_t pMinkp,Float_t etaminkp,Float_t etamaxkp){

  Float_t x[] = {xmin[0]+0.001,etaminkp+0.001,xmin[2]+0.001,pt+0.001,pMinkp+0.001,pMinkn+0.001,kTOFmatch+0.001,(pMinkn>0.09 || kPid3Sigma||kPid2Sigma)+0.001,selectTrue,xmin[9],xmin[10],xmin[11],xmin[12],xmin[13]};
  Float_t x2[] = {xmax[0],etamaxkp-0.001,xmax[2],ptM-0.001,xmax[4],xmax[5],xmax[6],xmax[7],keepTrue,xmax[9],xmax[10],xmax[11],xmax[12],xmax[13]};

  if(kOverAll){
    x[7] = 0.0001;
    x2[10] = 5.9;
    if(pMinkn > 0.19) x2[10] = 4.9;
  }

  if(kOverAllTOFmatch && pMinkn > 0.19){
    x[7] = 1.0001;
    x2[10] = 4.9;
  }

  if(kOverAll2Sigma && pMinkn > 0.09){
    x2[10] = 2;
    x[7] = 1.0001;
  }

  if(kGoodMatch){
    x[7] = 1.0001;
    if(pMinkn > 0)
      x2[10] = 4.9;
      
  }

  if(kTOFmatch){
    x[7] = 1.0001;
  }

  if(kSigma2vs3){
    x[7] = 1.0001;
    x2[10] = 3;
    if(pMinkn > 0)
      x2[10] = 2;
  }
 
 if(bayesVsigma){
    if(pMinkn > 0){
      x[5] = 0.2001;
      x2[10] = 5;
    }
    else{
      x2[10] = 3;
    }    
  }

  if(require5sigma) x2[10] = 4.9;
  if(kPid3Sigma && pMinkn>0.09) x2[10] = 2.9;
  if(kPid2Sigma && pMinkn>0.09) x2[10] = 1.9;

  AliPIDperfContainer *tmp = (AliPIDperfContainer *) fContPid2;

  TH2F *h = tmp->GetQA(0, x, x2);

  h->GetXaxis()->SetTitle("M_{#Lambda} (GeV/#it{c}^{2})");
  h->GetYaxis()->SetTitle("centrality [%]");

  return h;
}

void fit(TH1D *h,Float_t *a,char *opt,char *opt2,Float_t pt){
  if(h->Integral(1,h->GetNbinsX()) < 1){
    if(a){
      a[0]=0.01;
      a[1]=1;
    }
    return;
  }


 fall->SetParameter(0,100);
 fall->SetParameter(1,1.115);
 fall->SetParameter(2,2.89748e-03);
 fall->FixParameter(3,1000+350+600/pt);

 fall->SetParLimits(0,0.00001,10000000);
 fall->SetParLimits(1,1.105,1.125);//1.01898 + 2.4e-04*pt-1e-03,1.01898 + 2.4e-04*pt+1e-03);
 fall->SetParLimits(2,0.0005,0.0015);
 //fall->SetParLimits(3,200,350);

 // fall->FixParameter(2,5E-4);

//  fall->FixParameter(1,1.01884 + 2.9891e-04*pt);
//  fall->FixParameter(2,0.0044);
//  fall->FixParameter(3,7.57574e-04 + 3.85408e-04*pt);

 fall->ReleaseParameter(4);
 fall->ReleaseParameter(5);
 fall->ReleaseParameter(6);

 if(selectTrue){
   fall->FixParameter(4,0);
   fall->FixParameter(5,0);
   fall->FixParameter(6,0);
 }

 char namenew[100];
 sprintf(namenew,"%s_%i",h->GetName(),Int_t(gRandom->Rndm()*10000));
 TH1D *h2 = new TH1D(*h);
 h2->SetName(namenew);

 // Float_t entries = h2->GetBinContent(h2->FindBin(0.497));
//  printf("entries under the peak = %f, pt = %f\n",entries,pt);
//  getchar();

 if(pt > 2.5){
   if(pt < 2.4) h2->RebinX(1);
   else if(pt < 3) h2->RebinX(2);
   else h2->RebinX(2);
 }

 h=h2;

 char name[100];
 TF1 *ftmp=fall;

 TF1 *ftmp2=new TF1(*fsign);
 sprintf(name,"fsign%i",ifunc);
 ftmp2->SetName(name);

 TF1 *ftmp3=new TF1(*fback);
 sprintf(name,"ftmp3%i",ifunc);
 ftmp3->SetName(name);

 ifunc++;

 h->Fit(ftmp,opt,opt2,fitmin,fitmax);
 h->Draw("ERR");

 ftmp2->SetParameter(0,ftmp->GetParameter(0));
 ftmp2->SetParameter(1,ftmp->GetParameter(1));
 ftmp2->SetParameter(2,ftmp->GetParameter(2));
 ftmp2->SetParameter(3,ftmp->GetParameter(3));
 ftmp2->Draw("SAME");
 ftmp3->SetParameter(0,ftmp->GetParameter(4));
 ftmp3->SetParameter(1,ftmp->GetParameter(5));
 ftmp3->SetParameter(2,ftmp->GetParameter(6));
 ftmp3->Draw("SAME");

 Float_t mean = ftmp->GetParameter(1);
 Float_t sigma = TMath::Abs(ftmp->GetParameter(2));

 Float_t signI = ftmp2->Integral(mean-10*sigma,mean+10*sigma)/h->GetBinWidth(1);
 Float_t backI = ftmp3->Integral(mean-3*sigma,mean+3*sigma)/h->GetBinWidth(1);

 Float_t errI = TMath::Abs(ftmp->GetParError(0)*ftmp->GetParError(0)/(0.001+ftmp->GetParameter(0))/(0.001+ftmp->GetParameter(0)));

 errI = TMath::Sqrt(errI);

 if(signI < 0) signI = 0;
 if(backI < 1) backI = 1;

 printf("signal(5 sigma) = %f +/- %f(fit) +/- %f(stat)\n",signI,errI*signI,TMath::Sqrt(signI));
 printf("backgr(3sigma) = %f\n",backI);
 printf("significance(3 sigma) = %f\n",signI/sqrt(signI+backI));

 if(a){
   a[0]=signI;
   a[1]=signI*errI*signI*errI + signI;
   a[1] = TMath::Sqrt(a[1]);
   if(ftmp->GetNDF()) a[2] = ftmp->GetChisquare()/ftmp->GetNDF();


    if(selectTrue){
      a[0] = h->Integral(1,h->GetNbinsX());
      a[1] = TMath::Sqrt(a[0]);
    }
 }
}

void AddHisto(TH2F *h1,TH2F *h2,Float_t w){
  Int_t nbinx = h1->GetNbinsX();
  Int_t nbiny = h1->GetNbinsY();

  for(Int_t i=1;i<=nbinx;i++){
    for(Int_t j=1;j<=nbiny;j++){
      Double_t val = h1->GetBinContent(i,j) + h2->GetBinContent(i,j)*w;
      Float_t err = TMath::Min(TMath::Sqrt(val),val);
      h1->SetBinContent(i,j,val);
      h1->SetBinError(i,j,err);
    }
  }
}
 doeffPr.C:1
 doeffPr.C:2
 doeffPr.C:3
 doeffPr.C:4
 doeffPr.C:5
 doeffPr.C:6
 doeffPr.C:7
 doeffPr.C:8
 doeffPr.C:9
 doeffPr.C:10
 doeffPr.C:11
 doeffPr.C:12
 doeffPr.C:13
 doeffPr.C:14
 doeffPr.C:15
 doeffPr.C:16
 doeffPr.C:17
 doeffPr.C:18
 doeffPr.C:19
 doeffPr.C:20
 doeffPr.C:21
 doeffPr.C:22
 doeffPr.C:23
 doeffPr.C:24
 doeffPr.C:25
 doeffPr.C:26
 doeffPr.C:27
 doeffPr.C:28
 doeffPr.C:29
 doeffPr.C:30
 doeffPr.C:31
 doeffPr.C:32
 doeffPr.C:33
 doeffPr.C:34
 doeffPr.C:35
 doeffPr.C:36
 doeffPr.C:37
 doeffPr.C:38
 doeffPr.C:39
 doeffPr.C:40
 doeffPr.C:41
 doeffPr.C:42
 doeffPr.C:43
 doeffPr.C:44
 doeffPr.C:45
 doeffPr.C:46
 doeffPr.C:47
 doeffPr.C:48
 doeffPr.C:49
 doeffPr.C:50
 doeffPr.C:51
 doeffPr.C:52
 doeffPr.C:53
 doeffPr.C:54
 doeffPr.C:55
 doeffPr.C:56
 doeffPr.C:57
 doeffPr.C:58
 doeffPr.C:59
 doeffPr.C:60
 doeffPr.C:61
 doeffPr.C:62
 doeffPr.C:63
 doeffPr.C:64
 doeffPr.C:65
 doeffPr.C:66
 doeffPr.C:67
 doeffPr.C:68
 doeffPr.C:69
 doeffPr.C:70
 doeffPr.C:71
 doeffPr.C:72
 doeffPr.C:73
 doeffPr.C:74
 doeffPr.C:75
 doeffPr.C:76
 doeffPr.C:77
 doeffPr.C:78
 doeffPr.C:79
 doeffPr.C:80
 doeffPr.C:81
 doeffPr.C:82
 doeffPr.C:83
 doeffPr.C:84
 doeffPr.C:85
 doeffPr.C:86
 doeffPr.C:87
 doeffPr.C:88
 doeffPr.C:89
 doeffPr.C:90
 doeffPr.C:91
 doeffPr.C:92
 doeffPr.C:93
 doeffPr.C:94
 doeffPr.C:95
 doeffPr.C:96
 doeffPr.C:97
 doeffPr.C:98
 doeffPr.C:99
 doeffPr.C:100
 doeffPr.C:101
 doeffPr.C:102
 doeffPr.C:103
 doeffPr.C:104
 doeffPr.C:105
 doeffPr.C:106
 doeffPr.C:107
 doeffPr.C:108
 doeffPr.C:109
 doeffPr.C:110
 doeffPr.C:111
 doeffPr.C:112
 doeffPr.C:113
 doeffPr.C:114
 doeffPr.C:115
 doeffPr.C:116
 doeffPr.C:117
 doeffPr.C:118
 doeffPr.C:119
 doeffPr.C:120
 doeffPr.C:121
 doeffPr.C:122
 doeffPr.C:123
 doeffPr.C:124
 doeffPr.C:125
 doeffPr.C:126
 doeffPr.C:127
 doeffPr.C:128
 doeffPr.C:129
 doeffPr.C:130
 doeffPr.C:131
 doeffPr.C:132
 doeffPr.C:133
 doeffPr.C:134
 doeffPr.C:135
 doeffPr.C:136
 doeffPr.C:137
 doeffPr.C:138
 doeffPr.C:139
 doeffPr.C:140
 doeffPr.C:141
 doeffPr.C:142
 doeffPr.C:143
 doeffPr.C:144
 doeffPr.C:145
 doeffPr.C:146
 doeffPr.C:147
 doeffPr.C:148
 doeffPr.C:149
 doeffPr.C:150
 doeffPr.C:151
 doeffPr.C:152
 doeffPr.C:153
 doeffPr.C:154
 doeffPr.C:155
 doeffPr.C:156
 doeffPr.C:157
 doeffPr.C:158
 doeffPr.C:159
 doeffPr.C:160
 doeffPr.C:161
 doeffPr.C:162
 doeffPr.C:163
 doeffPr.C:164
 doeffPr.C:165
 doeffPr.C:166
 doeffPr.C:167
 doeffPr.C:168
 doeffPr.C:169
 doeffPr.C:170
 doeffPr.C:171
 doeffPr.C:172
 doeffPr.C:173
 doeffPr.C:174
 doeffPr.C:175
 doeffPr.C:176
 doeffPr.C:177
 doeffPr.C:178
 doeffPr.C:179
 doeffPr.C:180
 doeffPr.C:181
 doeffPr.C:182
 doeffPr.C:183
 doeffPr.C:184
 doeffPr.C:185
 doeffPr.C:186
 doeffPr.C:187
 doeffPr.C:188
 doeffPr.C:189
 doeffPr.C:190
 doeffPr.C:191
 doeffPr.C:192
 doeffPr.C:193
 doeffPr.C:194
 doeffPr.C:195
 doeffPr.C:196
 doeffPr.C:197
 doeffPr.C:198
 doeffPr.C:199
 doeffPr.C:200
 doeffPr.C:201
 doeffPr.C:202
 doeffPr.C:203
 doeffPr.C:204
 doeffPr.C:205
 doeffPr.C:206
 doeffPr.C:207
 doeffPr.C:208
 doeffPr.C:209
 doeffPr.C:210
 doeffPr.C:211
 doeffPr.C:212
 doeffPr.C:213
 doeffPr.C:214
 doeffPr.C:215
 doeffPr.C:216
 doeffPr.C:217
 doeffPr.C:218
 doeffPr.C:219
 doeffPr.C:220
 doeffPr.C:221
 doeffPr.C:222
 doeffPr.C:223
 doeffPr.C:224
 doeffPr.C:225
 doeffPr.C:226
 doeffPr.C:227
 doeffPr.C:228
 doeffPr.C:229
 doeffPr.C:230
 doeffPr.C:231
 doeffPr.C:232
 doeffPr.C:233
 doeffPr.C:234
 doeffPr.C:235
 doeffPr.C:236
 doeffPr.C:237
 doeffPr.C:238
 doeffPr.C:239
 doeffPr.C:240
 doeffPr.C:241
 doeffPr.C:242
 doeffPr.C:243
 doeffPr.C:244
 doeffPr.C:245
 doeffPr.C:246
 doeffPr.C:247
 doeffPr.C:248
 doeffPr.C:249
 doeffPr.C:250
 doeffPr.C:251
 doeffPr.C:252
 doeffPr.C:253
 doeffPr.C:254
 doeffPr.C:255
 doeffPr.C:256
 doeffPr.C:257
 doeffPr.C:258
 doeffPr.C:259
 doeffPr.C:260
 doeffPr.C:261
 doeffPr.C:262
 doeffPr.C:263
 doeffPr.C:264
 doeffPr.C:265
 doeffPr.C:266
 doeffPr.C:267
 doeffPr.C:268
 doeffPr.C:269
 doeffPr.C:270
 doeffPr.C:271
 doeffPr.C:272
 doeffPr.C:273
 doeffPr.C:274
 doeffPr.C:275
 doeffPr.C:276
 doeffPr.C:277
 doeffPr.C:278
 doeffPr.C:279
 doeffPr.C:280
 doeffPr.C:281
 doeffPr.C:282
 doeffPr.C:283
 doeffPr.C:284
 doeffPr.C:285
 doeffPr.C:286
 doeffPr.C:287
 doeffPr.C:288
 doeffPr.C:289
 doeffPr.C:290
 doeffPr.C:291
 doeffPr.C:292
 doeffPr.C:293
 doeffPr.C:294
 doeffPr.C:295
 doeffPr.C:296
 doeffPr.C:297
 doeffPr.C:298
 doeffPr.C:299
 doeffPr.C:300
 doeffPr.C:301
 doeffPr.C:302
 doeffPr.C:303
 doeffPr.C:304
 doeffPr.C:305
 doeffPr.C:306
 doeffPr.C:307
 doeffPr.C:308
 doeffPr.C:309
 doeffPr.C:310
 doeffPr.C:311
 doeffPr.C:312
 doeffPr.C:313
 doeffPr.C:314
 doeffPr.C:315
 doeffPr.C:316
 doeffPr.C:317
 doeffPr.C:318
 doeffPr.C:319
 doeffPr.C:320
 doeffPr.C:321
 doeffPr.C:322
 doeffPr.C:323
 doeffPr.C:324
 doeffPr.C:325
 doeffPr.C:326
 doeffPr.C:327
 doeffPr.C:328
 doeffPr.C:329
 doeffPr.C:330
 doeffPr.C:331
 doeffPr.C:332
 doeffPr.C:333
 doeffPr.C:334
 doeffPr.C:335
 doeffPr.C:336
 doeffPr.C:337
 doeffPr.C:338
 doeffPr.C:339
 doeffPr.C:340
 doeffPr.C:341
 doeffPr.C:342
 doeffPr.C:343
 doeffPr.C:344
 doeffPr.C:345
 doeffPr.C:346
 doeffPr.C:347
 doeffPr.C:348
 doeffPr.C:349
 doeffPr.C:350
 doeffPr.C:351
 doeffPr.C:352
 doeffPr.C:353
 doeffPr.C:354
 doeffPr.C:355
 doeffPr.C:356
 doeffPr.C:357
 doeffPr.C:358
 doeffPr.C:359
 doeffPr.C:360
 doeffPr.C:361
 doeffPr.C:362
 doeffPr.C:363
 doeffPr.C:364
 doeffPr.C:365
 doeffPr.C:366
 doeffPr.C:367
 doeffPr.C:368
 doeffPr.C:369
 doeffPr.C:370
 doeffPr.C:371
 doeffPr.C:372
 doeffPr.C:373
 doeffPr.C:374
 doeffPr.C:375
 doeffPr.C:376
 doeffPr.C:377
 doeffPr.C:378
 doeffPr.C:379
 doeffPr.C:380
 doeffPr.C:381
 doeffPr.C:382
 doeffPr.C:383
 doeffPr.C:384
 doeffPr.C:385
 doeffPr.C:386
 doeffPr.C:387
 doeffPr.C:388
 doeffPr.C:389
 doeffPr.C:390
 doeffPr.C:391
 doeffPr.C:392
 doeffPr.C:393
 doeffPr.C:394
 doeffPr.C:395
 doeffPr.C:396
 doeffPr.C:397
 doeffPr.C:398
 doeffPr.C:399
 doeffPr.C:400
 doeffPr.C:401
 doeffPr.C:402
 doeffPr.C:403
 doeffPr.C:404
 doeffPr.C:405
 doeffPr.C:406
 doeffPr.C:407
 doeffPr.C:408
 doeffPr.C:409
 doeffPr.C:410
 doeffPr.C:411
 doeffPr.C:412
 doeffPr.C:413
 doeffPr.C:414
 doeffPr.C:415
 doeffPr.C:416
 doeffPr.C:417
 doeffPr.C:418
 doeffPr.C:419
 doeffPr.C:420
 doeffPr.C:421
 doeffPr.C:422
 doeffPr.C:423
 doeffPr.C:424
 doeffPr.C:425
 doeffPr.C:426
 doeffPr.C:427
 doeffPr.C:428
 doeffPr.C:429
 doeffPr.C:430
 doeffPr.C:431
 doeffPr.C:432
 doeffPr.C:433
 doeffPr.C:434
 doeffPr.C:435
 doeffPr.C:436
 doeffPr.C:437
 doeffPr.C:438
 doeffPr.C:439
 doeffPr.C:440
 doeffPr.C:441
 doeffPr.C:442
 doeffPr.C:443
 doeffPr.C:444
 doeffPr.C:445
 doeffPr.C:446
 doeffPr.C:447
 doeffPr.C:448
 doeffPr.C:449
 doeffPr.C:450
 doeffPr.C:451
 doeffPr.C:452
 doeffPr.C:453
 doeffPr.C:454
 doeffPr.C:455
 doeffPr.C:456
 doeffPr.C:457
 doeffPr.C:458
 doeffPr.C:459
 doeffPr.C:460
 doeffPr.C:461
 doeffPr.C:462
 doeffPr.C:463
 doeffPr.C:464
 doeffPr.C:465
 doeffPr.C:466
 doeffPr.C:467
 doeffPr.C:468
 doeffPr.C:469
 doeffPr.C:470
 doeffPr.C:471
 doeffPr.C:472
 doeffPr.C:473
 doeffPr.C:474
 doeffPr.C:475
 doeffPr.C:476
 doeffPr.C:477
 doeffPr.C:478
 doeffPr.C:479
 doeffPr.C:480
 doeffPr.C:481
 doeffPr.C:482
 doeffPr.C:483
 doeffPr.C:484
 doeffPr.C:485
 doeffPr.C:486
 doeffPr.C:487
 doeffPr.C:488
 doeffPr.C:489
 doeffPr.C:490
 doeffPr.C:491
 doeffPr.C:492
 doeffPr.C:493
 doeffPr.C:494
 doeffPr.C:495
 doeffPr.C:496
 doeffPr.C:497
 doeffPr.C:498
 doeffPr.C:499
 doeffPr.C:500
 doeffPr.C:501
 doeffPr.C:502
 doeffPr.C:503
 doeffPr.C:504
 doeffPr.C:505
 doeffPr.C:506
 doeffPr.C:507
 doeffPr.C:508
 doeffPr.C:509
 doeffPr.C:510
 doeffPr.C:511
 doeffPr.C:512
 doeffPr.C:513
 doeffPr.C:514
 doeffPr.C:515
 doeffPr.C:516
 doeffPr.C:517
 doeffPr.C:518
 doeffPr.C:519
 doeffPr.C:520
 doeffPr.C:521
 doeffPr.C:522
 doeffPr.C:523
 doeffPr.C:524
 doeffPr.C:525
 doeffPr.C:526
 doeffPr.C:527
 doeffPr.C:528
 doeffPr.C:529
 doeffPr.C:530
 doeffPr.C:531
 doeffPr.C:532
 doeffPr.C:533
 doeffPr.C:534
 doeffPr.C:535
 doeffPr.C:536
 doeffPr.C:537
 doeffPr.C:538
 doeffPr.C:539
 doeffPr.C:540
 doeffPr.C:541
 doeffPr.C:542
 doeffPr.C:543
 doeffPr.C:544
 doeffPr.C:545
 doeffPr.C:546
 doeffPr.C:547
 doeffPr.C:548
 doeffPr.C:549
 doeffPr.C:550
 doeffPr.C:551
 doeffPr.C:552
 doeffPr.C:553
 doeffPr.C:554
 doeffPr.C:555
 doeffPr.C:556
 doeffPr.C:557
 doeffPr.C:558
 doeffPr.C:559
 doeffPr.C:560
 doeffPr.C:561
 doeffPr.C:562
 doeffPr.C:563
 doeffPr.C:564
 doeffPr.C:565
 doeffPr.C:566
 doeffPr.C:567
 doeffPr.C:568
 doeffPr.C:569
 doeffPr.C:570
 doeffPr.C:571
 doeffPr.C:572
 doeffPr.C:573
 doeffPr.C:574
 doeffPr.C:575
 doeffPr.C:576
 doeffPr.C:577
 doeffPr.C:578
 doeffPr.C:579
 doeffPr.C:580
 doeffPr.C:581
 doeffPr.C:582
 doeffPr.C:583
 doeffPr.C:584
 doeffPr.C:585
 doeffPr.C:586
 doeffPr.C:587
 doeffPr.C:588
 doeffPr.C:589
 doeffPr.C:590
 doeffPr.C:591
 doeffPr.C:592
 doeffPr.C:593
 doeffPr.C:594
 doeffPr.C:595
 doeffPr.C:596
 doeffPr.C:597
 doeffPr.C:598
 doeffPr.C:599
 doeffPr.C:600
 doeffPr.C:601
 doeffPr.C:602
 doeffPr.C:603
 doeffPr.C:604
 doeffPr.C:605
 doeffPr.C:606
 doeffPr.C:607