예제 #1
0
void Purity_1d_fit(int type = 0){
  TChain* tree = new TChain("TEvent");
  if(!type) tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_0-1_full.root");
  else      tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_data.root");

  RooCategory b0f("b0f","b0f");
  b0f.defineType("signal",1);
  b0f.defineType("fsr",10);
  b0f.defineType("bad_pi0",5);
  b0f.defineType("rho",3);
  b0f.defineType("comb",-1);

  RooArgSet argset;

  const double deMin = -0.15;
  const double deMax = 0.3;

  RooRealVar mbc("mbc","M_{bc}",mbc_min,mbc_max,"GeV"); argset.add(mbc);
  RooRealVar de("de","#DeltaE",deMin,deMax,"GeV"); argset.add(de);
  de.setRange("Signal",de_min,de_max);
  RooRealVar md("md","md",DMass-md_cut,DMass+md_cut,"GeV"); argset.add(md);
  RooRealVar mk("mk","mk",KMass-mk_cut,KMass+mk_cut,"GeV"); argset.add(mk);
  RooRealVar mpi0("mpi0","mpi0",Pi0Mass-mpi0_cut,Pi0Mass+mpi0_cut,"GeV"); argset.add(mpi0);
  RooRealVar bdtgs("bdtgs","bdtgs",bdtgs_cut,1.); argset.add(bdtgs);
  RooRealVar atckpi_max("atckpi_max","atckpi_max",0.,atckpi_cut); argset.add(atckpi_max);

  if(!type) argset.add(b0f);

  RooDataSet ds("ds","ds",tree,argset,"mbc>0||mbc<=0");
//  RooDataSet* ds0 = ds.reduce(RooArgSet(de));
  
  stringstream out;
  if(!type){
    out.str("");
    out << "de<" << de_max << " && de>" << de_min;
    Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
    sigtable->Print();
    sigtable->Print("v");

    Roo1DTable* fulltable = ds.table(b0f);
    fulltable->Print();
    fulltable->Print("v");
  }

//  RooDataHist* dh = ds0->binnedClone();

//  ds0->Print();

  ////////////////
  // Signal PDF //
  ////////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar de0("de0","de0",m_de0,-0.1,0.1); if(cSig) de0.setConstant(kTRUE);
  RooRealVar s1("s1","s1",m_s1,0.,0.5); if(cSig) s1.setConstant(kTRUE);
  RooGaussian g1("g1","g1",de,de0,s1);

  RooRealVar deCBl("deCBl","deCBl",m_deCBl,-0.1,0.1); if(cSig) deCBl.setConstant(kTRUE);
  RooRealVar sCBl("sCBl","sCBl",m_sCBl,0.,0.5); if(cSig) sCBl.setConstant(kTRUE);
  RooRealVar nl("nl","nl",m_nl,0.,100.); if(cSig) nl.setConstant(kTRUE);
  RooRealVar alphal("alphal","alphal",m_alphal,-10.,10.); if(cSig) alphal.setConstant(kTRUE);

  RooRealVar deCBr("deCBr","deCBr",m_deCBr,-0.1,0.1); if(cSig) deCBr.setConstant(kTRUE);
  RooRealVar sCBr("sCBr","sCBr",m_sCBr,0.,0.5); if(cSig) sCBr.setConstant(kTRUE);
  RooRealVar nr("nr","nr",m_nr,0.,100.); if(cSig) nr.setConstant(kTRUE);
  RooRealVar alphar("alphar","alphar",m_alphar,-10.,10.); if(cSig) alphar.setConstant(kTRUE);

  RooCBShape CBl("CBl","CBl",de,deCBl,sCBl,alphal,nl);
  RooCBShape CBr("CBr","CBr",de,deCBr,sCBr,alphar,nr);

  RooRealVar fCBl("fCBl","fCBl",m_fCBl,0.,1.); if(cSig) fCBl.setConstant(kTRUE);
  RooRealVar fCBr("fCBr","fCBr",m_fCBr,0.,1.); if(cSig) fCBr.setConstant(kTRUE);

  RooAddPdf pdf_sig("pdf_sig","pdf_sig",RooArgList(CBl,CBr,g1),RooArgSet(fCBl,fCBr));

  //////////////
  // Comb PDF //
  //////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar c1("c1","c1",mc_c1_1d,-10.,10.); if(cComb) c1.setConstant(kTRUE);
  RooRealVar c2("c2","c2",mc_c2_1d,-10.,10.); if(cComb) c2.setConstant(kTRUE);
  RooChebychev pdf_comb("pdf_comb","pdf_comb",de,RooArgSet(c1,c2));

  /////////////
  // Rho PDF //
  /////////////
  ////////////
  // de pdf //
  ////////////
if(de_rho_param == 0){
  RooRealVar exppar("exppar","exppar",mr_exppar,-40.,-25.);// if(cRho) exppar.setConstant(kTRUE);
  RooExponential pdf_rho("pdf_rho","pdf_rho",de,exppar);
  }
  
  RooRealVar de0r("de0r","de0r",mr_de0r,-0.2,0.12); if(cRho) de0r.setConstant(kTRUE);
  
  if(de_rho_param == 1){
   RooRealVar slopel("slopel","slopel",mr_slopel,-1000,-500.); if(cRho) slopel.setConstant(kTRUE);
   RooRealVar sloper("sloper","sloper",mr_sloper,-10000,0.); if(cRho) sloper.setConstant(kTRUE);
   RooRealVar steep("steep","steep",mr_steep,7.,9.); if(cRho) steep.setConstant(kTRUE);
   RooRealVar p5("p5","p5",mr_p5,0.01,1000.); if(cRho) p5.setConstant(kTRUE);
   RooRhoDeltaEPdf pdf_rho("pdf_rho","pdf_rho",de,de0r,slopel,sloper,steep,p5);
  }
  
  if(de_rho_param == -1){
   RooRealVar x0("x0","x0",mr_x0_1d,-0.2,0.12); if(cRho) x0.setConstant(kTRUE);
   RooRealVar p1("p1","p1",mr_p1_1d,-1000.,100.); if(cRho) p1.setConstant(kTRUE);
   RooRealVar p2("p2","p2",mr_p2_1d,0.,100.); if(cRho) p2.setConstant(kTRUE);
   RooGenericPdf pdf_rho("pdf_rho","1+@0*@1-@2*TMath::Log(1+TMath::Exp(@2*(@0-@1)/@3))",RooArgSet(de,x0,p1,p2));
  }
  //////////////////
  // Complete PDF //
  //////////////////
  RooRealVar Nsig("Nsig","Nsig",700,100.,1500.);// fsig.setConstant(kTRUE);
  RooRealVar Nrho("Nrho","Nrho",400,100,1500.);// frho.setConstant(kTRUE);
  RooRealVar Ncmb("Ncmb","Ncmb",1000,100,100000);// frho.setConstant(kTRUE);
  RooAddPdf pdf("pdf","pdf",RooArgList(pdf_sig,pdf_rho,pdf_comb),RooArgList(Nsig,Nrho,Ncmb));

  pdf.fitTo(ds,Verbose(),Timer(true));

   RooAbsReal* intSig  = pdf_sig.createIntegral(RooArgSet(de),NormSet(RooArgSet(de)),Range("Signal"));
   RooAbsReal* intRho  = pdf_rho.createIntegral(RooArgSet(de),NormSet(RooArgSet(de)),Range("Signal"));
   RooAbsReal* intCmb  = pdf_comb.createIntegral(RooArgSet(de),NormSet(RooArgSet(de)),Range("Signal"));
   const double nsig = intSig->getVal()*Nsig.getVal();
   const double nsig_err = intSig->getVal()*Nsig.getError();
   const double nsig_err_npq = TMath::Sqrt(nsig*(Nsig.getVal()-nsig)/Nsig.getVal());
   const double nsig_err_total = TMath::Sqrt(nsig_err*nsig_err+nsig_err_npq*nsig_err_npq);
   const double nrho = intRho->getVal()*Nrho.getVal();
   const double nrho_err = intRho->getVal()*Nrho.getError();
   const double nrho_err_npq = TMath::Sqrt(nrho*(Nrho.getVal()-nrho)/Nrho.getVal());
   const double nrho_err_total = TMath::Sqrt(nrho_err*nrho_err+nrho_err_npq*nrho_err_npq);
   const double ncmb = intCmb->getVal()*Ncmb.getVal();
   const double ncmb_err = intCmb->getVal()*Ncmb.getError();
   const double ncmb_err_npq = TMath::Sqrt(ncmb*(Ncmb.getVal()-ncmb)/Ncmb.getVal());
   const double ncmb_err_total = TMath::Sqrt(ncmb_err*ncmb_err+ncmb_err_npq*ncmb_err_npq);
   const double purity = nsig/(nsig+nrho+ncmb);
   const double purity_err = nsig_err_total/(nsig+nrho+ncmb);
   cout << "Nsig = " << nsig <<" +- " << nsig_err << endl;
   cout << "Nrho = " << nrho <<" +- " << nrho_err << endl;
   cout << "Ncmb = " << ncmb <<" +- " << ncmb_err << endl;
   
  /////////////
  //  Plots  //
  /////////////
  // de //
  RooPlot* deFrame = de.frame();
  ds.plotOn(deFrame,DataError(RooAbsData::SumW2),MarkerSize(1));
  pdf.plotOn(deFrame,Components(pdf_sig),LineStyle(kDashed));
  pdf.plotOn(deFrame,Components(pdf_rho),LineStyle(kDashed));
  pdf.plotOn(deFrame,Components(pdf_comb),LineStyle(kDashed));
  pdf.plotOn(deFrame,LineWidth(2));

  RooHist* hdepull = deFrame->pullHist();
  RooPlot* dePull = de.frame(Title("#Delta E pull distribution"));
  dePull->addPlotable(hdepull,"P");
  dePull->GetYaxis()->SetRangeUser(-5,5);

  TCanvas* cm = new TCanvas("Delta E","Delta E",600,700);
  cm->cd();

  TPad *pad3 = new TPad("pad3","pad3",0.01,0.20,0.99,0.99);
  TPad *pad4 = new TPad("pad4","pad4",0.01,0.01,0.99,0.20);
  pad3->Draw();
  pad4->Draw();

  pad3->cd();
  pad3->SetLeftMargin(0.15);
  pad3->SetFillColor(0);

  deFrame->GetXaxis()->SetTitleSize(0.05);
  deFrame->GetXaxis()->SetTitleOffset(0.85);
  deFrame->GetXaxis()->SetLabelSize(0.04);
  deFrame->GetYaxis()->SetTitleOffset(1.6);
  deFrame->Draw();

  stringstream out1;
  TPaveText *pt = new TPaveText(0.6,0.75,0.98,0.9,"brNDC");
  pt->SetFillColor(0);
  pt->SetTextAlign(12);
  out1.str("");
  out1 << "#chi^{2}/n.d.f = " << deFrame->chiSquare();
  pt->AddText(out1.str().c_str());
  out1.str("");
  out1 << "S: " << (int)(nsig+0.5) << " #pm " << (int)(nsig_err_total+0.5);
  pt->AddText(out1.str().c_str());
  out1.str("");
  out1 << "Purity: " << std::fixed << std::setprecision(2) << purity*100. << " #pm " << purity_err*100;
  pt->AddText(out1.str().c_str());
  pt->Draw();

  TLine *de_line_RIGHT = new TLine(de_max,0,de_max,50);
  de_line_RIGHT->SetLineColor(kRed);
  de_line_RIGHT->SetLineStyle(1);
  de_line_RIGHT->SetLineWidth((Width_t)2.);
  de_line_RIGHT->Draw();
  TLine *de_line_LEFT = new TLine(de_min,0,de_min,50);
  de_line_LEFT->SetLineColor(kRed);
  de_line_LEFT->SetLineStyle(1);
  de_line_LEFT->SetLineWidth((Width_t)2.);
  de_line_LEFT->Draw();

  pad4->cd(); pad4->SetLeftMargin(0.15); pad4->SetFillColor(0);
  dePull->SetMarkerSize(0.05); dePull->Draw();
  TLine *de_lineUP = new TLine(deMin,3,deMax,3);
  de_lineUP->SetLineColor(kBlue);
  de_lineUP->SetLineStyle(2);
  de_lineUP->Draw();
  TLine *de_line = new TLine(deMin,0,deMax,0);
  de_line->SetLineColor(kBlue);
  de_line->SetLineStyle(1);
  de_line->SetLineWidth((Width_t)2.);
  de_line->Draw();
  TLine *de_lineDOWN = new TLine(deMin,-3,deMax,-3);
  de_lineDOWN->SetLineColor(kBlue);
  de_lineDOWN->SetLineStyle(2);
  de_lineDOWN->Draw();

  cm->Update();
  
  if(!type){
    out.str("");
    out << "de<" << de_max << " && de>" << de_min;
    Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
    sigtable->Print();
    sigtable->Print("v");
    
    Roo1DTable* fulltable = ds.table(b0f);
    fulltable->Print();
    fulltable->Print("v");
  }
  
  cout << "Nsig = " << nsig <<" +- " << nsig_err << " +- " << nsig_err_npq << " (" << nsig_err_total << ")" << endl;
  cout << "Nrho = " << nrho <<" +- " << nrho_err << " +- " << nrho_err_npq << " (" << nrho_err_total << ")" << endl;
  cout << "Ncmb = " << ncmb <<" +- " << ncmb_err << " +- " << ncmb_err_npq << " (" << ncmb_err_total << ")" << endl;
  cout << "Pury = " << purity << " +- " << purity_err << endl;
}
void Purity_2d_fit_etagg_v2(bool data = false) {
    TChain* tree = new TChain("TEvent");
    if(!data) tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_0-1_full.root");
    else      tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_data.root");

    const int _mode = 2;
    const int _h0mode = 10;
    const int _b0f = -1;
//  gROOT->ProcessLine(".L pdfs/RooRhoDeltaEPdf.cxx+");

    RooCategory b0f("b0f","b0f");
    b0f.defineType("signal",1);
    b0f.defineType("fsr",10);
    b0f.defineType("bad_pi0",5);
    b0f.defineType("peak1",3);
    b0f.defineType("peak2",4);
    b0f.defineType("peak3",11);
    b0f.defineType("peak4",20);
    b0f.defineType("comb",-1);

    RooArgSet argset;

    const double mbcMin = 5.20;
    const double mbcMax = 5.29;
    double deMin = -0.15;
    if(keysflag) deMin = -0.3;
    const double deMax = 0.3;
    const double elliscaleDe  = TMath::Sqrt(4./TMath::Pi());
    const double elliscaleMbc = TMath::Sqrt(4./TMath::Pi());

    RooRealVar mbc_center("mbc_center","mbc_center",0.5*(mbc_min+mbc_max),mbc_min,mbc_max);
    mbc_center.setConstant(kTRUE);
    RooRealVar mbc_center_eq("mbc_center_eq","mbc_center_eq",mr_argedge_3-0.5*(mbc_max-mbc_min)*elliscaleMbc,mbc_min,mbc_max);
    mbc_center_eq.setConstant(kTRUE);
    RooRealVar de_center("de_center","de_center",0.5*(de_min+de_max),de_min,de_max);
    de_center.setConstant(kTRUE);
    RooRealVar mbc_radius("mbc_radius","mbc_radius",0.5*(mbc_max-mbc_min)*elliscaleMbc,0,0.5*(mbcMax-mbcMin));
    mbc_radius.setConstant(kTRUE);
    RooRealVar de_radius("de_radius","de_radius",0.5*(de_max-de_min)*elliscaleDe,0.,0.5*(deMax-deMin));
    de_radius.setConstant(kTRUE);
    RooRealVar mbc_radius1("mbc_radius1","mbc_radius1",0.5*(mbc_max-mbc_min),0,0.5*(mbcMax-mbcMin));
    mbc_radius1.setConstant(kTRUE);
    RooRealVar de_radius1("de_radius1","de_radius1",0.5*(de_max-de_min),0.,0.5*(deMax-deMin));
    de_radius1.setConstant(kTRUE);

    cout << 0.5*(mbc_min+mbc_max) << " " << 0.5*(mbc_max-mbc_min) << endl;
    cout << 0.5*(de_min+de_max) << " " << 0.5*(de_max-de_min) << endl;

    mbc_center.Print();
    mbc_center_eq.Print();

    const double BDTG_MIN = bdtg_cut_etagg;
    const double BDTG_MAX = 1;
    RooCategory mode("mode","mode");
    mode.defineType("eta",2);
    RooCategory h0mode("h0mode","h0mode");
    h0mode.defineType("gg",10);
    argset.add(mode);
    argset.add(h0mode);

    RooRealVar mbc("mbc","M_{bc}",0.5*(mbc_min+mbc_max),mbcMin,mbcMax,"GeV");
    argset.add(mbc);
    mbc.setRange("Signal",mbc_min,mbc_max);
    mbc.setRange("mbcSignal",mbc_min,mbc_max);
    mbc.setRange("deSignal",mbcMin,mbcMax);

    RooRealVar de("de","#DeltaE",deMin,deMax,"GeV");
    argset.add(de);
    de.setRange("Signal",de_min,de_max);
    de.setRange("mbcSignal",deMin,deMax);
    de.setRange("deSignal",de_min,de_max);

    RooRealVar md("md","md",DMass-md_cut,DMass+md_cut,"GeV");
    argset.add(md);
    RooRealVar mk("mk","mk",KMass-mk_cut,KMass+mk_cut,"GeV");
    argset.add(mk);
    RooRealVar mh0("mh0","mh0",EtaMass-meta_cut,EtaMass+meta_cut,"GeV");
    argset.add(mh0);
    RooRealVar bdtg("bdtg","bdtg",BDTG_MIN,1.);
    argset.add(bdtg);
    RooRealVar atckpi_max("atckpi_max","atckpi_max",0.,atckpi_cut);
    argset.add(atckpi_max);

    if(!data) argset.add(b0f);
    RooDataSet ds("ds","ds",tree,argset,"mbc>0||mbc<=0");
//  RooDataSet* ds0 = ds.reduce(RooArgSet(de,mbc));

    stringstream out;
    if(!data) {
        out.str("");
        out << "de<" << de_max << " && de>" << de_min;
        out << " && mbc>" << mbc_min << " && mbc<" << mbc_max;
        Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
        sigtable->Print();
        sigtable->Print("v");

        Roo1DTable* fulltable = ds.table(b0f);
        fulltable->Print();
        fulltable->Print("v");
    }

//  RooDataHist* dh = ds0->binnedClone();

    ds.Print();

    ////////////////
    // Signal PDF //
    ////////////////
    ////////////
    // de pdf //
    ////////////
    if(!(_mode!=1 && _h0mode == 20 && _b0f == -1)) {
        RooRealVar de0("de0","de0",get_de0(_mode,_h0mode,_b0f),-0.2,0.1);
        if(cSIG) de0.setConstant(kTRUE);
        RooRealVar s1("s1","s1",get_s1(_mode,_h0mode,_b0f),0.,0.5);
        if(cSIG) s1.setConstant(kTRUE);
        RooGaussian g1("g1","g1",de,de0,s1);

        RooRealVar deCBl("deCBl","deCBl",get_deCBl(_mode,_h0mode,_b0f),-0.2,0.1);
        if(cSIG) deCBl.setConstant(kTRUE);
        RooRealVar sCBl("sCBl","sCBl",get_sCBl(_mode,_h0mode,_b0f),0.,0.5);
        if(cSIG) sCBl.setConstant(kTRUE);
        RooRealVar nl("nl","nl",get_nl(_mode,_h0mode,_b0f),0.,100.);
        if(cSIG) nl.setConstant(kTRUE);
//  RooRealVar nl("nl","nl",1.,0.,100.); nl.setConstant(kTRUE);
        RooRealVar alphal("alphal","alphal",get_alphal(_mode,_h0mode,_b0f),-10.,10.);
        if(cSIG) alphal.setConstant(kTRUE);

        RooRealVar deCBr("deCBr","deCBr",get_deCBr(_mode,_h0mode,_b0f),-0.2,0.1);
        if(cSIG) deCBr.setConstant(kTRUE);
        RooRealVar sCBr("sCBr","sCBr",get_sCBr(_mode,_h0mode,_b0f),0.,0.5);
        if(cSIG) sCBr.setConstant(kTRUE);
        RooRealVar nr("nr","nr",get_nr(_mode,_h0mode,_b0f),0.,100.);
        if(cSIG) nr.setConstant(kTRUE);
        RooRealVar alphar("alphar","alphar",get_alphar(_mode,_h0mode,_b0f),-10.,10.);
        if(cSIG) alphar.setConstant(kTRUE);

        RooCBShape CBl("CBl","CBl",de,deCBl,sCBl,alphal,nl);
        RooCBShape CBr("CBr","CBr",de,deCBr,sCBr,alphar,nr);

        RooRealVar fCBl("fCBl","fCBl",get_fCBl(_mode,_h0mode,_b0f),0.,1.);
        if(cSIG) fCBl.setConstant(kTRUE);
        RooRealVar fCBr("fCBr","fCBr",get_fCBr(_mode,_h0mode,_b0f),0.,1.);
        if(cSIG) fCBr.setConstant(kTRUE);

        RooAddPdf pdf_de_sig("pdf_de_sig","pdf_de_sig",RooArgList(CBl,CBr,g1),RooArgSet(fCBl,fCBr));
    }

    if((_mode==3&&_h0mode == 20 || _mode==2) && _b0f == -1) {
        RooRealVar de0_201("de0_201","de0_201",get_de0(_mode,_h0mode,1),-0.1,0.1);
        de0_201.setConstant(kTRUE);
        RooRealVar s1_201("s1_201","s1_201",get_s1(_mode,_h0mode,1),0.,0.5);
        s1_201.setConstant(kTRUE);
        RooGaussian g1_201("g1_201","g1_201",de,de0_201,s1_201);

        RooRealVar deCBl_201("deCBl_201","deCBl_201",get_deCBl(_mode,_h0mode,1),-0.1,0.1);
        deCBl_201.setConstant(kTRUE);
        RooRealVar sCBl_201("sCBl_201","sCBl_201",get_sCBl(_mode,_h0mode,1),0.,0.5);
        sCBl_201.setConstant(kTRUE);
        RooRealVar nl_201("nl_201","nl_201",get_nl(_mode,_h0mode,1),0.,100.);
        nl_201.setConstant(kTRUE);
        RooRealVar alphal_201("alphal_201","alphal_201",get_alphal(_mode,_h0mode,1),-10.,10.);
        alphal_201.setConstant(kTRUE);
        RooRealVar deCBr_201("deCBr_201","deCBr_201",get_deCBr(_mode,_h0mode,1),-0.1,0.1);
        deCBr_201.setConstant(kTRUE);
        RooRealVar sCBr_201("sCBr_201","sCBr_201",get_sCBr(_mode,_h0mode,1),0.,0.5);
        sCBr_201.setConstant(kTRUE);
        RooRealVar nr_201("nr_201","nr_201",get_nr(_mode,_h0mode,1),0.,100.);
        nr_201.setConstant(kTRUE);
        RooRealVar alphar_201("alphar_201","alphar_201",get_alphar(_mode,_h0mode,1),-10.,10.);
        alphar_201.setConstant(kTRUE);
        RooCBShape CBl_201("CBl_201","CBl_201",de,deCBl_201,sCBl_201,alphal_201,nl_201);
        RooCBShape CBr_201("CBr_201","CBr_201",de,deCBr_201,sCBr_201,alphar_201,nr_201);

        RooRealVar fCBl_201("fCBl_201","fCBl_201",get_fCBl(_mode,_h0mode,1),0.,1.);
        fCBl_201.setConstant(kTRUE);
        RooRealVar fCBr_201("fCBr_201","fCBr_201",get_fCBr(_mode,_h0mode,1),0.,1.);
        fCBr_201.setConstant(kTRUE);

        RooAddPdf pdf_de_201("pdf_de_201","pdf_de_201",RooArgList(CBl_201,CBr_201,g1_201),RooArgSet(fCBl_201,fCBr_201));

        RooRealVar de0_205("de0_205","de0_205",get_de0(_mode,_h0mode,5),-0.1,0.1);
        de0_205.setConstant(kTRUE);
        RooRealVar s1_205("s1_205","s1_205",get_s1(_mode,_h0mode,5),0.,0.5);
        s1_205.setConstant(kTRUE);
        RooGaussian g1_205("g1_205","g1_205",de,de0_205,s1_205);

        RooRealVar deCBl_205("deCBl_205","deCBl_205",get_deCBl(_mode,_h0mode,5),-0.1,0.1);
        deCBl_205.setConstant(kTRUE);
        RooRealVar sCBl_205("sCBl_205","sCBl_205",get_sCBl(_mode,_h0mode,5),0.,0.5);
        sCBl_205.setConstant(kTRUE);
        RooRealVar nl_205("nl_205","nl_205",get_nl(_mode,_h0mode,5),0.,100.);
        nl_205.setConstant(kTRUE);
        RooRealVar alphal_205("alphal_205","alphal_205",get_alphal(_mode,_h0mode,5),-10.,10.);
        alphal_205.setConstant(kTRUE);
        RooCBShape CBl_205("CBl_205","CBl_205",de,deCBl_205,sCBl_205,alphal_205,nl_205);

        RooRealVar fCBl_205("fCBl_205","fCBl_205",get_fCBl(_mode,_h0mode,5),0.,1.);
        fCBl_205.setConstant(kTRUE);

        RooAddPdf pdf_de_205("pdf_de_205","pdf_de_205",RooArgList(CBl_205,g1_205),RooArgSet(fCBl_205));

        RooRealVar f_201("f_201","f_201",get_f201(_mode,_h0mode),0.,1.);
        if(cSIG) f_201.setConstant(kTRUE);
        RooAddPdf pdf_de_sig("pdf_de_sig","pdf_de_sig",RooArgList(pdf_de_201,pdf_de_205),RooArgSet(f_201));
    }

    /////////////
    // mbc pdf //
    /////////////
    const bool cond_flag = false;//(b0f == 5);
    if(cond_flag) {
        RooRealVar cond("cond","cond",mr_cond_3,-0.1.,1.);
        if(cRHO) cond.setConstant(kTRUE);
        RooRealVar condr("condr","condr",mr_condr_3,-1000.,1.);
        if(cRHO) condr.setConstant(kTRUE);

        RooRealVar _sl("_sl","_sl",get_sl(_mode,_h0mode,_b0f),0.,0.5);
        if(cSIG) _sl.setConstant(kTRUE);
        RooRealVar _sr("_sr","_sr",get_sr(_mode,_h0mode,_b0f),0.0005,0.5);
        if(cSIG) _sr.setConstant(kTRUE);
        RooFormulaVar sl("sl","sl","@0+@1*@2",RooArgList(_sl,cond,de));
        RooFormulaVar sr("sr","sr","@0+@1*@2",RooArgList(_sr,condr,de));

        RooRealVar _sll("_sll","_sll",get_sll(_mode,_h0mode,_b0f),0.,0.5);
        if(cSIG) _sll.setConstant(kTRUE);
        RooRealVar _srr("_srr","_srr",get_srr(_mode,_h0mode,_b0f),0.0005,0.5);
        if(cSIG) _srr.setConstant(kTRUE);
        RooFormulaVar sll("sll","sll","@0+@1*@2",RooArgList(_sll,cond,de));
        RooFormulaVar srr("srr","srr","@0+@1*@2",RooArgList(_srr,condr,de));
    } else {
예제 #3
0
void PurityFit(const int _mode){
  TChain* tree = new TChain("TEvent");
//  tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_0-1.root");
  tree->Add("/home/vitaly/B0toDh0/TMVA/FIL1_b2dh_uds_2_12.root");
  tree->Add("/home/vitaly/B0toDh0/TMVA/FIL1_b2dh_charm_2_12.root");
  tree->Add("/home/vitaly/B0toDh0/TMVA/FIL1_b2dh_charged_2_12.root");
  tree->Add("/home/vitaly/B0toDh0/TMVA/FIL1_b2dh_mixed_2_12.root");

  gROOT->ProcessLine(".L pdfs/RooRhoDeltaEPdf.cxx+");

  RooCategory b0f("b0f","b0f");
  b0f.defineType("signal",1);
  b0f.defineType("fsr",10);
  b0f.defineType("bad_pi0",5);
  b0f.defineType("rho2",2);
  b0f.defineType("rho3",3);
  b0f.defineType("rho4",4);
  b0f.defineType("rho11",11);
  b0f.defineType("comb",-1);

  RooCategory mode("mode","mode");
  RooCategory h0mode("h0mode","h0mode");

  double BDTG_MIN = 0;
  double BDTG_MAX = 1;
  bool gg_flag = true;
  double Mbc_min;
  double Mbc_max;
  double dE_min;
  double dE_max;
  int m_mode,m_h0mode;
  double mh0_min, mh0_max;
  string label;
  switch(_mode){
  case 1:
    label = string("#pi^{0}");
    BDTG_MIN = bdt_cut_pi0;
    mode.defineType("pi0",1);
    h0mode.defineType("gg",10);
    Mbc_min = mbc_min_pi0;
    Mbc_max = mbc_max_pi0;
    dE_min  = de_min_pi0;
    dE_max  = de_max_pi0;
    m_mode = 1;
    m_h0mode = 10;
    mh0_min = mpi0_min;
    mh0_max = mpi0_max;
    break;
  case 2:
    label = string("#eta#rightarrow#gamma#gamma");
    BDTG_MIN = bdtg_cut_etagg;
    mode.defineType("eta",2);
    h0mode.defineType("gg",10);
    Mbc_min = mbc_min;
    Mbc_max = mbc_max;
    dE_min  = de_min;
    dE_max  = de_max;
    m_mode = 2;
    m_h0mode = 10;
    mh0_min = EtaGGMass-metagg_cut;
    mh0_max = EtaGGMass+metagg_cut;
    break;
  case 3:
    label = string("#eta#rightarrow#pi^{+}#pi^{-}#pi^{0}");
    BDTG_MIN = bdtg_cut_etappp;
    gg_flag = false;
    mode.defineType("eta",2);
    h0mode.defineType("ppp",20);
    Mbc_min = mbc_min;
    Mbc_max = mbc_max;
    dE_min  = de_min_etappp;
    dE_max  = de_max_etappp;
    m_mode = 2;
    m_h0mode = 20;
    mh0_min = EtaMass-metappp_cut;
    mh0_max = EtaMass+metappp_cut;
    break;
  case 4:
    label = string("#omega");
    BDTG_MIN = bdtg_cut_omega;
    gg_flag = false;
    mode.defineType("omega",3);
    h0mode.defineType("ppp",20);
    Mbc_min = mbc_min_omega;
    Mbc_max = mbc_max_omega;
    dE_min  = de_min_omega;
    dE_max  = de_max_omega;
    m_mode = 3;
    m_h0mode = 20;
    mh0_min = OmegaMass-momega_cut;
    mh0_max = OmegaMass+momega_cut;
    break;
  default:
    return;
  }

  RooArgSet argset;
  argset.add(mode);
  argset.add(h0mode);
  argset.add(b0f);

  RooCategory flv("flv_mc","flv_mc");
  flv.defineType("B0",1);
  flv.defineType("anti-B0",-1);
  argset.add(flv);

  RooCategory bin("bin","bin");
  bin.defineType("1",1); bin.defineType("-1",-1);
  bin.defineType("2",2); bin.defineType("-2",-2);
  bin.defineType("3",3); bin.defineType("-3",-3);
  bin.defineType("4",4); bin.defineType("-4",-4);
  bin.defineType("5",5); bin.defineType("-5",-5);
  bin.defineType("6",6); bin.defineType("-6",-6);
  bin.defineType("7",7); bin.defineType("-7",-7);
  bin.defineType("8",8); bin.defineType("-8",-8);
  argset.add(bin);

  RooSuperCategory binflv("binflv","binflv",RooArgSet(bin,flv));

  const double mbcMin = 5.20;
  const double mbcMax = 5.2885;
  const double deMin = -0.15;
  const double deMax = 0.3;
  const double elliscaleDe  = TMath::Sqrt(4./TMath::Pi());
  const double elliscaleMbc = TMath::Sqrt(4./TMath::Pi());

  RooRealVar mbc_center("mbc_center","mbc_center",0.5*(Mbc_min+Mbc_max),Mbc_min,Mbc_max); mbc_center.setConstant(kTRUE);
  RooRealVar mbc_center_eq("mbc_center_eq","mbc_center_eq",mr_argedge_3-0.5*(Mbc_max-Mbc_min)*elliscaleMbc,Mbc_min,Mbc_max); mbc_center_eq.setConstant(kTRUE);
  RooRealVar de_center("de_center","de_center",0.5*(dE_min+dE_max),dE_min,dE_max); de_center.setConstant(kTRUE);
  RooRealVar mbc_radius("mbc_radius","mbc_radius",0.5*(Mbc_max-Mbc_min)*elliscaleMbc,0,0.5*(mbcMax-mbcMin)); mbc_radius.setConstant(kTRUE);
  RooRealVar de_radius("de_radius","de_radius",0.5*(dE_max-dE_min)*elliscaleDe,0.,0.5*(deMax-deMin)); de_radius.setConstant(kTRUE);
  RooRealVar mbc_radius1("mbc_radius1","mbc_radius1",0.5*(Mbc_max-Mbc_min),0,0.5*(mbcMax-mbcMin)); mbc_radius1.setConstant(kTRUE);
  RooRealVar de_radius1("de_radius1","de_radius1",0.5*(dE_max-dE_min),0.,0.5*(deMax-deMin)); de_radius1.setConstant(kTRUE);

  cout << 0.5*(Mbc_min+Mbc_max) << " " << 0.5*(Mbc_max-Mbc_min) << endl;
  cout << 0.5*(dE_min+dE_max) << " " << 0.5*(dE_max-dE_min) << endl;

  mbc_center.Print();
  mbc_center_eq.Print();

  RooRealVar mbc("mbc","M_{bc}",0.5*(Mbc_min+Mbc_max),mbcMin,mbcMax,"GeV"); argset.add(mbc);
  mbc.setRange("Signal",Mbc_min,Mbc_max);
  mbc.setRange("mbcSignal",Mbc_min,Mbc_max);
  mbc.setRange("deSignal",mbcMin,mbcMax);

  RooRealVar de("de","#DeltaE",deMin,deMax,"GeV"); argset.add(de);
  de.setRange("Signal",dE_min,dE_max);
  de.setRange("mbcSignal",deMin,deMax);
  de.setRange("deSignal",dE_min,dE_max);

//   de.setRange("Ellips",dE_min,dE_max);
//   RooFormulaVar mbclo("mbclo","@1-@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4+0.00001)",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
//   RooFormulaVar mbchi("mbchi","@1+@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4+0.00001)",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
//   mbc.setRange("Ellips",mbclo,mbchi);

  RooRealVar md("md","md",DMass-md_cut,DMass+md_cut,"GeV"); argset.add(md);
  RooRealVar mk("mk","mk",KMass-mk_cut,KMass+mk_cut,"GeV"); argset.add(mk);
  RooRealVar mh0("mh0","mh0",mh0_min,mh0_max,"GeV"); argset.add(mh0);
  RooRealVar mpi0("mpi0","mpi0",mpi0_min,mpi0_max,"GeV"); if(_mode!=2) argset.add(mpi0);
  RooRealVar bdt("bdt","bdt",BDTG_MIN,BDTG_MAX); argset.add(bdt);

  argset.add(b0f);
  RooDataSet ds_sig("ds_sig","ds_sig",tree,argset,"mbc>0||mbc<=0 && (b0f == 1 || b0f == 5 || b0f == 10)");
  RooDataSet ds_bkg("ds_bkg","ds_bkg",tree,argset,"mbc>0||mbc<=0 && !(b0f == 1 || b0f == 5 || b0f == 10)");
  RooDataHist dh("dh","dh");
  dh.add(ds_sig,"",1./0.563);
  dh.add(ds_bkg,"",1./0.949);

  stringstream out;
  out.str("");
  out << "de<" << dE_max << " && de>" << dE_min;
  out << " && mbc>" << Mbc_min << " && mbc<" << Mbc_max;
  Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
  sigtable->Print();
  sigtable->Print("v");

  Roo1DTable* fulltable = ds.table(b0f);
  fulltable->Print();
  fulltable->Print("v");

//  RooDataHist* dh = ds0->binnedClone();

  ds.Print();
  int _b0f = -1;
  ////////////////
  // Signal PDF //
  ////////////////
  ////////////
  // de pdf //
  ////////////
  if(gg_flag){
    RooRealVar  de0("de0","de0",get_de0(m_mode,m_h0mode,_b0f),-0.2,0.1); if(cSig) de0.setConstant(kTRUE);
    RooRealVar  s1("s1","s1",get_s1(m_mode,m_h0mode,_b0f),0.,0.5);       if(cSig) s1.setConstant(kTRUE);
    RooGaussian g1("g1","g1",de,de0,s1);

    RooRealVar deCBl("deCBl","deCBl",get_deCBl(m_mode,m_h0mode,_b0f),-0.2,0.1); if(cSig) deCBl.setConstant(kTRUE);
    RooRealVar sCBl("sCBl","sCBl",get_sCBl(m_mode,m_h0mode,_b0f),0.,0.5);       if(cSig) sCBl.setConstant(kTRUE);
    RooRealVar alphal("alphal","alphal", get_alphal(m_mode,m_h0mode,_b0f), 0.,10.);                          if(cSIG) alphal.setConstant(kTRUE);
    RooRealVar nl("nl","nl",2.,0.,100.); nl.setConstant(kTRUE);

    RooRealVar deCBr("deCBr","deCBr",get_deCBr(m_mode,m_h0mode,_b0f),-0.2,0.1); if(cSig) deCBr.setConstant(kTRUE);
    RooRealVar sCBr("sCBr","sCBr",get_sCBr(m_mode,m_h0mode,_b0f),0.,0.5);       if(cSig) sCBr.setConstant(kTRUE);
    RooRealVar alphar("alphar","alphar",get_alphar(m_mode,m_h0mode,_b0f),-10.,0.);                          if(cSig) alphar.setConstant(kTRUE);
    RooRealVar nr("nr","nr",2,0.,100.); nr.setConstant(kTRUE);

    RooCBShape CBl("CBl","CBl",de,deCBl,sCBl,alphal,nl);
    RooCBShape CBr("CBr","CBr",de,deCBr,sCBr,alphar,nr);

    RooRealVar fCBl("fCBl","fCBl",get_fCBl(m_mode,m_h0mode,_b0f),0.,1.); if(cSig) fCBl.setConstant(kTRUE);
    RooRealVar fCBr("fCBr","fCBr",get_fCBr(m_mode,m_h0mode,_b0f),0.,1.); if(cSig) fCBr.setConstant(kTRUE);

    RooAddPdf pdf_de_sig("pdf_de_sig","pdf_de_sig",RooArgList(CBl,CBr,g1),RooArgSet(fCBl,fCBr));
  } else{
    RooRealVar de0_201("de0_201","de0_201",get_de0(m_mode,m_h0mode,1),-0.1,0.1); if(cSig) de0_201.setConstant(kTRUE);
    RooRealVar  s1_201("s1_201","s1_201",get_s1(m_mode,m_h0mode,1),0.,0.5); if(cSig) s1_201.setConstant(kTRUE);
    RooGaussian g1_201("g1_201","g1_201",de,de0_201,s1_201);

    RooRealVar deCBl_201("deCBl_201","deCBl_201",get_deCBl(m_mode,m_h0mode,1),-0.1,0.1);     if(cSig) deCBl_201.setConstant(kTRUE);
    RooRealVar sCBl_201("sCBl_201","sCBl_201",get_sCBl(m_mode,m_h0mode,1),0.,0.5);           if(cSig) sCBl_201.setConstant(kTRUE);
    RooRealVar nl_201("nl_201","nl_201",2.,0.,100.); nl_201.setConstant(kTRUE);
    RooRealVar alphal_201("alphal_201","alphal_201",get_alphal(m_mode,m_h0mode,1),-10.,10.); if(cSig) alphal_201.setConstant(kTRUE);
    RooRealVar deCBr_201("deCBr_201","deCBr_201",get_deCBr(m_mode,m_h0mode,1),-0.1,0.1);     if(cSig) deCBr_201.setConstant(kTRUE);
    RooRealVar sCBr_201("sCBr_201","sCBr_201",get_sCBr(m_mode,m_h0mode,1),0.,0.5);           if(cSig) sCBr_201.setConstant(kTRUE);
    RooRealVar nr_201("nr_201","nr_201",2.,0.,100.); nr_201.setConstant(kTRUE);
    RooRealVar alphar_201("alphar_201","alphar_201",get_alphar(m_mode,m_h0mode,1),-10.,10.); if(cSig) alphar_201.setConstant(kTRUE);

    RooCBShape CBl_201("CBl_201","CBl_201",de,deCBl_201,sCBl_201,alphal_201,nl_201);
    RooCBShape CBr_201("CBr_201","CBr_201",de,deCBr_201,sCBr_201,alphar_201,nr_201);

    RooRealVar fCBl_201("fCBl_201","fCBl_201",get_fCBl(m_mode,m_h0mode,1),0.,1.); if(cSig) fCBl_201.setConstant(kTRUE);
    if(_mode == 3){
      fCBl_201.setVal(0.);
      fCBl_201.setConstant(kTRUE);
      alphal_201.setConstant(kTRUE);
    }
    RooRealVar fCBr_201("fCBr_201","fCBr_201",get_fCBr(m_mode,m_h0mode,1),0.,1.); if(cSig) fCBr_201.setConstant(kTRUE);

    RooAddPdf pdf_de1("pdf_de1","pdf_de1",RooArgList(CBl_201,CBr_201,g1_201),RooArgSet(fCBl_201,fCBr_201));

    RooRealVar  de0_205("de0_205","de0_205",get_de0(m_mode,m_h0mode,5),-0.2,0.1); if(cSig) de0_205.setConstant(kTRUE);
    RooRealVar  s1_205("s1_205","s1_205",get_s1(m_mode,m_h0mode,5),0.,0.5);       if(cSig) s1_205.setConstant(kTRUE);
    RooGaussian g1_205("g1_205","g1_205",de,de0_205,s1_205);

    RooRealVar deCBl_205("deCBl_205","deCBl_205",get_deCBl(m_mode,m_h0mode,5),-0.1,0.1); if(cSig) deCBl_205.setConstant(kTRUE);
    RooRealVar sCBl_205("sCBl_205","sCBl_205",get_sCBl(m_mode,m_h0mode,5),0.,0.5);       if(cSig) sCBl_205.setConstant(kTRUE);
    RooRealVar nl_205("nl_205","nl_205",2,0.,100.); nl_205.setConstant(kTRUE);
    RooRealVar alphal_205("alphal_205","alphal_205",get_alphal(m_mode,m_h0mode,5),-10.,10.);  if(cSig) alphal_205.setConstant(kTRUE);
    RooCBShape CBl_205("CBl_205","CBl_205",de,deCBl_205,sCBl_205,alphal_205,nl_205);

    RooRealVar fCBl_205("fCBl_205","fCBl_205",get_fCBl(m_mode,m_h0mode,5),0.,1.); if(cSig) fCBl_205.setConstant(kTRUE);

    RooAddPdf pdf_de5("pdf_de5","pdf_de5",RooArgList(CBl_205,g1_205),RooArgSet(fCBl_205));
  }

  /////////////
  // mbc pdf //
  /////////////
  if(gg_flag){
    RooRealVar a_s("a_s","a_s",get_a_s(_mode)); if(cSig) a_s.setConstant(kTRUE);
    RooRealVar b_s("b_s","b_s",get_b_s(_mode)); if(cSig) b_s.setConstant(kTRUE);
    RooRealVar c_s("c_s","c_s",get_c_s(_mode),0.0015,0.0035);// if(cSig) c_s.setConstant(kTRUE);
    RooFormulaVar S("S","S","@1+@2*@0+@3*@0*@0",RooArgList(de,c_s,b_s,a_s));

    RooRealVar alpha("alpha","alpha",0.139,0.01,2.); alpha.setConstant(kTRUE);

    RooRealVar a_mbc0("a_mbc0","a_mbc0",get_a_mbc0(_mode)); if(cSig) a_mbc0.setConstant(kTRUE);
    RooRealVar b_mbc0("b_mbc0","b_mbc0",get_b_mbc0(_mode)); if(cSig) b_mbc0.setConstant(kTRUE);
    RooRealVar c_mbc0("c_mbc0","c_mbc0",get_c_mbc0(_mode),5.277,5.285);// if(cSig) c_mbc0.setConstant(kTRUE);
    RooFormulaVar MBC0("MBC0","MBC0","@1+@2*@0+@3*@0*@0",RooArgList(de,c_mbc0,b_mbc0,a_mbc0));
    RooNovosibirsk pdf_mbc_sig("pdf_mbc_sig","pdf_mbc_sig",mbc,MBC0,S,alpha);
  } else{
    RooRealVar alpha("alpha","alpha",0.139,0.01,2.); alpha.setConstant(kTRUE);
    RooRealVar c0("c0","c0",get_c0(_mode)); if(cSig) c0.setConstant(kTRUE);
    RooRealVar c1("c1","c1",get_c1(_mode)); if(cSig) c1.setConstant(kTRUE);
    RooRealVar c2("c2","c2",get_c2(_mode)); if(cSig) c2.setConstant(kTRUE);
    RooRealVar mbc0("mbc0","mbc0",5.284,5.277,5.29);// if(cSig) mbc0.setConstant(kTRUE);
    RooFormulaVar MBC("MBC","MBC","@0+@1*TMath::Erf((@2-@3))/@4",RooArgList(mbc0,c0,c1,de,c2));

    RooRealVar a_s1("a_s1","a_s1",get_a_s(_mode),0.15,0.45); if(cSig) a_s1.setConstant(kTRUE);
    RooRealVar b_s1("b_s1","b_s1",get_b_s(_mode),-0.05,0.05); if(cSig) b_s1.setConstant(kTRUE);
    RooRealVar c_s1("c_s1","c_s1",get_c_s(_mode),0.0015,0.0035);// if(cSig) c_s1.setConstant(kTRUE);
    RooFormulaVar S1("S1","S1","@1+@2*@0+@3*@0*@0",RooArgList(de,c_s1,b_s1,a_s1));
    RooNovosibirsk pdf_mbc1("pdf_mbc1","pdf_mbc1",mbc,MBC,S1,alpha);

    RooRealVar a_s5("a_s5","a_s5",get_a5_s(_mode)); if(cSig) a_s5.setConstant(kTRUE);
    RooRealVar b_s5("b_s5","b_s5",get_b5_s(_mode)); if(cSig) b_s5.setConstant(kTRUE);
    RooRealVar c_s5("c_s5","c_s5",get_c5_s(_mode),0.0015,0.0055); if(cSig) c_s5.setConstant(kTRUE);
    RooFormulaVar S5("S5","S5","@1+@2*@0+@3*@0*@0",RooArgList(de,c_s5,b_s5,a_s5));

    RooRealVar a_mbc0("a_mbc0","a_mbc0",get_a5_mbc0(_mode)); if(cSig) a_mbc0.setConstant(kTRUE);
    RooRealVar b_mbc0("b_mbc0","b_mbc0",get_b5_mbc0(_mode)); if(cSig) b_mbc0.setConstant(kTRUE);
    RooRealVar c_mbc0("c_mbc0","c_mbc0",get_c5_mbc0(_mode),5.27,5.29); if(cSig) c_mbc0.setConstant(kTRUE);
    RooFormulaVar MBC0("MBC0","MBC0","@1+@2*@0+@3*@0*@0",RooArgList(de,c_mbc0,b_mbc0,a_mbc0));
    RooNovosibirsk pdf_mbc5("pdf_mbc5","pdf_mbc5",mbc,MBC0,S5,alpha);
  }

  /////////
  // pdf //
  /////////
  if(gg_flag){
    RooProdPdf pdf_sig("pdf_sig","pdf_sig",pdf_de_sig,Conditional(pdf_mbc_sig,mbc));
  } else{
    RooRealVar f_201("f_201","f_201",get_f201(m_mode,m_h0mode),0.,1.); if(cSig) f_201.setConstant(kTRUE);
    RooProdPdf pdf1_sig("pdf1_sig","pdf1_sig",pdf_de1,Conditional(pdf_mbc1,mbc));
    RooProdPdf pdf5_sig("pdf5_sig","pdf5_sig",pdf_de5,Conditional(pdf_mbc5,mbc));
    RooAddPdf  pdf_sig("pdf_sig","pdf_sig",RooArgList(pdf1_sig,pdf5_sig),RooArgSet(f_201));
  }

  //////////////
  // Comb PDF //
  //////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar c10("c10","c10",get_cmb_c10(_mode),-10,50.); if(cComb) c10.setConstant(kTRUE);
  RooRealVar c11("c11","c11",get_cmb_c11(_mode),-50,0.);  if(cComb) c11.setConstant(kTRUE);
  RooFormulaVar c1_cmb("c1_cmb","@0+@1*@2",RooArgSet(c10,c11,mbc));
  RooRealVar c2_cmb("c2_cmb","c2_cmb",get_cmb_c20(_mode),-0.1,1);     if(cComb) c2_cmb.setConstant(kTRUE);
  RooChebychev pdf_de_comb_bb("pdf_de_comb_bb","pdf_de_comb_bb",de,RooArgSet(c1_cmb,c2_cmb));

  RooRealVar C1("C1","C1",get_cmb_c1(_mode),-10,50.); if(cComb) C1.setConstant(kTRUE);
  RooRealVar C2("C2","C2",get_cmb_c2(_mode),-0.1,1);  if(cComb) C2.setConstant(kTRUE);
  RooChebychev pdf_de_comb_qq("pdf_de_comb_qq","pdf_de_comb_qq",de,RooArgSet(C1,C2));
  /////////////
  // mbc pdf //
  /////////////
  RooRealVar argedge("argedge","argedge",5.288,5.285,5.29); //argedge.setConstant(kTRUE);
  RooRealVar argpar_cmb_bb("argpar_cmb_bb","argpar_cmb_bb",get_argpar_bb(_mode),-300,-10.); if(cComb) argpar_cmb_bb.setConstant(kTRUE);
  RooArgusBG pdf_mbc_comb_ar("pdf_mbc_comb_ar","Argus PDF",mbc,argedge,argpar_cmb_bb);

  RooRealVar mbc0_cmb_bb("mbc0_cmb_bb","mbc0_cmb_bb",get_mbc0_cmb_bb(_mode),5.25,5.29,"GeV");// if(cComb) mbc0_cmb_bb.setConstant(kTRUE);
  RooRealVar mbcWidth_cmb_bb("mbcWidth","mbcWidth",get_mbcw_cmb_bb(_mode),0.,0.1,"GeV"); if(cComb) mbcWidth_cmb_bb.setConstant(kTRUE);
  RooGaussian mbcGaus_cmb_bb("mbcGaus","mbcGaus",mbc,mbc0_cmb_bb,mbcWidth_cmb_bb);

  RooRealVar f_g("f_g","f_g",get_f_g_cmb_bb(_mode),0.4,0.7);if(_mode == 2 || !gg_flag){ f_g.setConstant(kTRUE);}
  RooAddPdf pdf_mbc_cmb_bb("pdf_mbc_cmb_bb","pdf_mbc_cmb_bb",RooArgList(mbcGaus_cmb_bb,pdf_mbc_comb_ar),RooArgSet(f_g));

  RooRealVar argpar_cmb_qq("argpar_cmb_qq","argpar_cmb_qq",get_argpar_qq(_mode),-300,-10.); if(cComb) argpar_cmb_qq.setConstant(kTRUE);
  RooArgusBG pdf_mbc_cmb_qq("pdf_mbc_cmb_qq","pdf_mbc_cmb_qq",mbc,argedge,argpar_cmb_qq);
  
  /////////
  // pdf //
  /////////
  RooRealVar f_bb("f_bb","f_bb",0.3,0.,1.);
  RooProdPdf pdf_cmb_bb("pdf_cmb_bb","pdf_cmb_bb",pdf_mbc_cmb_bb,Conditional(pdf_de_comb_bb,de));
  RooProdPdf pdf_cmb_qq("pdf_cmb_qq","pdf_cmb_qq",pdf_mbc_cmb_qq,Conditional(pdf_de_comb_qq,de));
  RooAddPdf pdf_comb("pdf_comb","pdf_comb",RooArgSet(pdf_cmb_bb,pdf_cmb_qq),RooArgList(f_bb));

  /////////////////////
  // Peaking bkg PDF //
  /////////////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar de0r("de0r","de0r",get_de0r(_mode),-0.2,0.12);         if(cPeak) de0r.setConstant(kTRUE);
  RooRealVar slopel("slopel","slopel",get_slopel(_mode),-1.e5,0.);  if(cPeak) slopel.setConstant(kTRUE);
  RooRealVar sloper("sloper","sloper",get_sloper(_mode),-10000,0.); if(cPeak) sloper.setConstant(kTRUE);
  RooRealVar steep("steep","steep",get_steep(_mode),0.,1000.);      if(cPeak) steep.setConstant(kTRUE);
  RooRealVar p5("p5","p5",get_p5(_mode),0.01,1000.);                if(cPeak) p5.setConstant(kTRUE);
  RooRhoDeltaEPdf pdf_de_peak("pdf_de_peak","pdf_de_peak",de,de0r,slopel,sloper,steep,p5);
//  RooGenericPdf pdf_de_peak("pdf_de_peak","1+(@0-@1)*@2+@4*TMath::Log(1+@5*TMath::Exp((@3-@2)*(@0-@1)/@4)) > 0 ? 1+(@0-@1)*@2+@4*TMath::Log(1+@5*TMath::Exp((@3-@2)*(@0-@1)/@4)) : 0.001",RooArgSet(de,de0r,slopel,sloper,steep,p5));
  /////////////
  // mbc pdf //
  /////////////
  if(gg_flag){
    RooRealVar b_peak_s("b_peak_s","b_peak_s",get_peak_b_s(_mode),-0.1,0.1); if(cPeak) b_peak_s.setConstant(kTRUE);
    RooRealVar k_peak_s("k_peak_s","k_peak_s",get_peak_k_s(_mode),-0.1,0.1); if(cPeak) k_peak_s.setConstant(kTRUE);
    RooFormulaVar S_peak("S_peak","S_peak","@0+@1*@2",RooArgList(b_peak_s,de,k_peak_s));
    RooRealVar alpha_peak("alpha_peak","alpha_peak",0.139,0.01,2.); alpha_peak.setConstant(kTRUE);
    RooRealVar b_peak_mbc0("b_peak_mbc0","b_peak_mbc0",get_peak_b_mbc0(_mode),5.25,5.29); if(cPeak) b_peak_mbc0.setConstant(kTRUE);
    RooRealVar k_peak_mbc0("k_peak_mbc0","k_peak_mbc0",get_peak_k_mbc0(_mode),-0.1,0.1);  if(cPeak) k_peak_mbc0.setConstant(kTRUE);
    RooFormulaVar MBC0_peak("MBC0_peak","MBC0_peak","@0+@1*@2",RooArgList(b_peak_mbc0,de,k_peak_mbc0));
    RooNovosibirsk pdf_mbc_peak("pdf_mbc_peak","pdf_mbc_peak",mbc,MBC0_peak,S_peak,alpha_peak);
  } else{
//    RooRealVar argedge("argedge","argedge",5.288,5.285,5.29); //argedge.setConstant(kTRUE);
    RooRealVar argpar_peak_bb("argpar_peak_bb","argpar_peak_bb",get_argpar_bb(_mode),-300,-10.); if(cPeak) argpar_peak_bb.setConstant(kTRUE);
    RooArgusBG pdf_mbc_peak_ar("pdf_mbc_peak_ar","Argus PDF",mbc,argedge,argpar_peak_bb);

    RooRealVar mbc0_peak("mbc0_peak","mbc0_peak",get_peak_b_mbc0(_mode),5.25,5.291,"GeV"); if(cPeak) mbc0_peak.setConstant(kTRUE);
    RooRealVar mbcWidth_peak("mbcWidth_peak","mbcWidth_peak",get_peak_b_s(_mode),0.,0.1,"GeV"); if(cPeak) mbcWidth_peak.setConstant(kTRUE);
    RooGaussian mbcGaus_peak("mbcGaus_peak","mbcGaus_peak",mbc,mbc0_peak,mbcWidth_peak);
    RooRealVar f_g_peak("f_g_peak","f_g_peak",get_f_g_cmb_bb(_mode),0.,1.); if(cPeak) f_g_peak.setConstant(kTRUE);

    RooAddPdf pdf_mbc_peak("pdf_mbc_peak","pdf_mbc_peak",RooArgList(mbcGaus_peak,pdf_mbc_peak_ar),RooArgSet(f_g_peak));
  }
  /////////
  // pdf //
  /////////
  RooProdPdf pdf_peak("pdf_peak","pdf_peak",pdf_de_peak,Conditional(pdf_mbc_peak,mbc));

  //////////////////
  // Complete PDF //
  //////////////////
  RooRealVar Nsig("Nsig","Nsig",1150,0.,10000.);
//  RooRealVar Npbg("Npbg","Npbg",100,0,100000.);
  RooRealVar Ncmb("Ncmb","Ncmb",2288,0,100000);
  switch(_mode){
  case 1:
    RooRealVar Npbg("Npbg","Npbg",100,0,100000.);
    break;
  case 2:
    RooConstVar f_p_f_bbc("f_p_f_bbc","f_p_f_bbc",0.0051);
    RooFormulaVar Npbg("Npbg","Npbg","@0*@1*@2",RooArgList(Ncmb,f_bb,f_p_f_bbc));
    break;
  case 3:
    RooConstVar f_p_f_bbc("f_p_f_bbc","f_p_f_bbc",0.0081);
    RooFormulaVar Npbg("Npbg","Npbg","@0*@1*@2",RooArgList(Ncmb,f_bb,f_p_f_bbc));
    break;
  case 4:
    RooConstVar f_p_f_bbc("f_p_f_bbc","f_p_f_bbc",0.0031);
    RooFormulaVar Npbg("Npbg","Npbg","@0*@1*@2",RooArgList(Ncmb,f_bb,f_p_f_bbc));
    break;
  default:
    return -1;
  }

  RooAddPdf pdf("pdf","pdf",RooArgList(pdf_sig,pdf_peak,pdf_comb),RooArgList(Nsig,Npbg,Ncmb));

  RooArgSet* params = pdf.getParameters(RooArgSet(de,mbc));
//  RooArgset* initParams = (RooArgSet*) params->snapshot();

  pdf.fitTo(ds,Verbose(),Timer(true));

  params->printLatex(OutputFile("PurityFit.tex"));

   RooAbsReal* intSig  = pdf_sig.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Signal"));
   RooAbsReal* intRho  = pdf_peak->createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Signal"));
   RooAbsReal* intCmb  = pdf_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Signal"));
   const double nsig = intSig->getVal()*Nsig.getVal();
   const double nsig_err = intSig->getVal()*Nsig.getError();
   const double nsig_err_npq = TMath::Sqrt(nsig*(Nsig.getVal()-nsig)/Nsig.getVal());
   const double nsig_err_total = TMath::Sqrt(nsig_err*nsig_err+nsig_err_npq*nsig_err_npq);
   const double nrho = intRho->getVal()*Npbg.getVal();
   const double nrho_err = _mode == 1 ? intRho->getVal()*Npbg.getError() : intRho->getVal()*f_bb.getError()*Ncmb.getVal()*f_p_f_bbc.getVal();
   const double nrho_err_npq = TMath::Sqrt(nrho*(Npbg.getVal()-nrho)/Npbg.getVal());
   const double nrho_err_total = TMath::Sqrt(nrho_err*nrho_err+nrho_err_npq*nrho_err_npq);
   const double ncmb = intCmb->getVal()*Ncmb.getVal();
   const double ncmb_err = intCmb->getVal()*Ncmb.getError();
   const double ncmb_err_npq = TMath::Sqrt(ncmb*(Ncmb.getVal()-ncmb)/Ncmb.getVal());
   const double ncmb_err_total = TMath::Sqrt(ncmb_err*ncmb_err+ncmb_err_npq*ncmb_err_npq);
   const double purity = nsig/(nsig+nrho+ncmb);
   const double purity_err = nsig_err_total/(nsig+nrho+ncmb);

   de.setRange("Ellips",dE_min,dE_max);
   RooFormulaVar mbclo("mbclo","@1-@2*TMath::Sqrt(TMath::Abs(1-(@0-@3)/@4*(@0-@3)/@4)+0.0000001)",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
   RooFormulaVar mbchi("mbchi","@1+@2*TMath::Sqrt(TMath::Abs(1-(@0-@3)/@4*(@0-@3)/@4)+0.0000001)",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
   mbc.setRange("Ellips",mbclo,mbchi);

   de.setRange("Elli",dE_min,dE_max);
   RooFormulaVar mbclo1("mbclo1","@1-@2*TMath::Sqrt(TMath::Abs(1-(@0-@3)/@4*(@0-@3)/@4)+0.0000001)",RooArgSet(de,mbc_center,mbc_radius1,de_center,de_radius1));
   RooFormulaVar mbchi1("mbchi1","@1+@2*TMath::Sqrt(TMath::Abs(1-(@0-@3)/@4*(@0-@3)/@4)+0.0000001)",RooArgSet(de,mbc_center,mbc_radius1,de_center,de_radius1));
   mbc.setRange("Elli",mbclo1,mbchi1);

   RooAbsReal* intSigEl = pdf_sig.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Ellips"));
   RooAbsReal* intRhoEl = pdf_peak->createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Ellips"));
   RooAbsReal* intCmbEl = pdf_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Ellips"));
   const double nsigEl = intSigEl->getVal()*Nsig.getVal();
   const double nsig_errEl = intSigEl->getVal()*Nsig.getError();
   const double nsig_errEl_npq = TMath::Sqrt(fabs(nsigEl*(Nsig.getVal()-nsigEl)/Nsig.getVal()));
   const double nsig_errEl_total = TMath::Sqrt(fabs(nsig_errEl*nsig_errEl+nsig_errEl_npq*nsig_errEl_npq));
   const double nrhoEl = intRhoEl->getVal()*Npbg.getVal();
   const double nrho_errEl = _mode == 1 ? intRhoEl->getVal()*Npbg.getError() : intRhoEl->getVal()*f_bb.getError()*Ncmb.getVal()*f_p_f_bbc.getVal();
   const double nrho_errEl_npq = TMath::Sqrt(fabs(nrhoEl*(Npbg.getVal()-nrhoEl)/Npbg.getVal()));
   const double nrho_errEl_total = TMath::Sqrt(fabs(nrho_errEl*nrho_errEl+nrho_errEl_npq*nrho_errEl_npq));
   const double ncmbEl = intCmbEl->getVal()*Ncmb.getVal();
   const double ncmb_errEl = intCmbEl->getVal()*Ncmb.getError();
   const double ncmb_errEl_npq = TMath::Sqrt(fabs(ncmbEl*(Ncmb.getVal()-ncmbEl)/Ncmb.getVal()));
   const double ncmb_errEl_total = TMath::Sqrt(ncmb_errEl*ncmb_errEl+ncmb_errEl_npq*ncmb_errEl_npq);
   const double purityEl = nsigEl/(nsigEl+nrhoEl+ncmbEl);
   const double purity_errEl = nsig_errEl_total/(nsigEl+nrhoEl+ncmbEl);


   RooAbsReal* intSigEl1 = pdf_sig.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Elli"));
   const double intElli = intSigEl1->getVal();
   RooAbsReal* intRhoEl1 = pdf_peak->createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Elli"));
   RooAbsReal* intCmbEl1 = pdf_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Elli"));
   const double nsigEl1 = intSigEl1->getVal()*Nsig.getVal();
   const double nsig_errEl1 = intSigEl1->getVal()*Nsig.getError();
   const double nsig_errEl1_npq = TMath::Sqrt(fabs(nsigEl1*(Nsig.getVal()-nsigEl1)/Nsig.getVal()));
   const double nsig_errEl1_total = TMath::Sqrt(fabs(nsig_errEl1*nsig_errEl1+nsig_errEl1_npq*nsig_errEl1_npq));
   const double nrhoEl1 = intRhoEl1->getVal()*Npbg.getVal();
   const double nrho_errEl1 = _mode == 1 ? intRhoEl1->getVal()*Npbg.getError() : intRhoEl1->getVal()*f_bb.getError()*Ncmb.getVal()*f_p_f_bbc.getVal();
   const double nrho_errEl1_npq = TMath::Sqrt(fabs(nrhoEl1*(Npbg.getVal()-nrhoEl1)/Npbg.getVal()));
   const double nrho_errEl1_total = TMath::Sqrt(fabs(nrho_errEl1*nrho_errEl1+nrho_errEl1_npq*nrho_errEl1_npq));
   const double ncmbEl1 = intCmbEl1->getVal()*Ncmb.getVal();
   const double ncmb_errEl1 = intCmbEl1->getVal()*Ncmb.getError();
   const double ncmb_errEl1_npq = TMath::Sqrt(ncmbEl1*(Ncmb.getVal()-ncmbEl1)/Ncmb.getVal());
   const double ncmb_errEl1_total = TMath::Sqrt(ncmb_errEl1*ncmb_errEl1+ncmb_errEl1_npq*ncmb_errEl1_npq);
   const double purityEl1 = nsigEl1/(nsigEl1+nrhoEl1+ncmbEl1);
   const double purity_errEl1 = nsig_errEl1_total/(nsigEl1+nrhoEl1+ncmbEl1);

  /////////////
  //  Plots  //
  /////////////
  // de //
  RooPlot* deFrame = de.frame();
  ds.plotOn(deFrame,DataError(RooAbsData::SumW2),MarkerSize(1),CutRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(pdf_sig),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(pdf_peak),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(pdf_cmb_bb),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(pdf_cmb_qq),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,LineWidth(2),ProjectionRange("mbcSignal"));

  RooHist* hdepull = deFrame->pullHist();
  RooPlot* dePull = de.frame(Title("#Delta E pull distribution"));
  dePull->addPlotable(hdepull,"P");
  dePull->GetYaxis()->SetRangeUser(-5,5);

  TCanvas* cm = new TCanvas("#Delta E, Signal","#Delta E, Signal",600,700);
  cm->cd();

  TPad *pad3 = new TPad("pad3","pad3",0.01,0.20,0.99,0.99);
  TPad *pad4 = new TPad("pad4","pad4",0.01,0.01,0.99,0.20);
  pad3->Draw();
  pad4->Draw();

  pad3->cd();
  pad3->SetLeftMargin(0.15);
  pad3->SetFillColor(0);

  deFrame->GetXaxis()->SetTitleSize(0.05);
  deFrame->GetXaxis()->SetTitleOffset(0.85);
  deFrame->GetXaxis()->SetLabelSize(0.04);
  deFrame->GetYaxis()->SetTitleOffset(1.6);
  deFrame->Draw();

  out.str("");
  out << "(de-" << de_center.getVal() << ")/" << de_radius1.getVal() << "*(de-" << de_center.getVal() << ")/" << de_radius1.getVal() << "+(mbc-"<<mbc_center.getVal()<<")/" << mbc_radius1.getVal() << "*(mbc-" << mbc_center.getVal() << ")/" << mbc_radius1.getVal() << "<1";
  Roo1DTable* ellitable1 = ds.table(b0f,out.str().c_str());

  out.str("");
  out << "(de-" << de_center.getVal() << ")/" << de_radius.getVal() << "*(de-" << de_center.getVal() << ")/" << de_radius.getVal() << "+(mbc-"<<mbc_center.getVal()<<")/" << mbc_radius.getVal() << "*(mbc-" << mbc_center.getVal() << ")/" << mbc_radius.getVal() << "<1";
  Roo1DTable* ellitable = ds.table(b0f,out.str().c_str());

  const int NSIGNAL_ELLI   = ellitable1->get("signal") + ellitable1->get("fsr") + ellitable1->get("bad_pi0");
//  const int NSIGNAL_ELLIPS = ellitable->get("signal")  + ellitable->get("fsr")  + ellitable->get("bad_pi0");

  stringstream out1;
  TPaveText *pt = new TPaveText(0.5,0.6,0.98,0.9,"brNDC");
  pt->SetFillColor(0);
  pt->SetTextAlign(12);
  out1.str("");
  out1 << "#chi^{2}/n.d.f = " << deFrame->chiSquare();
  pt->AddText(out1.str().c_str());
  out1.str("");
  out1 << "S: " << (int)(nsigEl1+0.5) << " #pm " << (int)(nsig_errEl1_total+0.5) << " (" << NSIGNAL_ELLI << ")";
//  out1 << "S: " << (int)(nsig+0.5) << " #pm " << (int)(nsig_err_total+0.5);
  pt->AddText(out1.str().c_str());
//  out1.str("");
//  out1 << "S_{2}: " << (int)(nsigEl+0.5) << " #pm " << (int)(nsig_errEl_total+0.5) << " (" << NSIGNAL_ELLIPS << ")";
//  pt->AddText(out1.str().c_str());
  out1.str("");
//  out1 << "Purity: " << std::fixed << std::setprecision(2) << purity*100. << " #pm " << purity_err*100;
  out1 << "P: " << std::fixed << std::setprecision(2) << purityEl1*100. << " #pm " << purity_errEl1*100;
  pt->AddText(out1.str().c_str());
  pt->AddText(label.c_str());
  pt->Draw();

  TLine *de_line_RIGHT;
  de_line_RIGHT = new TLine(dE_max,0,dE_max,120);
  de_line_RIGHT->SetLineColor(kRed);
  de_line_RIGHT->SetLineStyle(1);
  de_line_RIGHT->SetLineWidth((Width_t)2.);
  de_line_RIGHT->Draw();
  TLine *de_line_LEFT;
  de_line_LEFT = new TLine(dE_min,0,dE_min,120);
  de_line_LEFT->SetLineColor(kRed);
  de_line_LEFT->SetLineStyle(1);
  de_line_LEFT->SetLineWidth((Width_t)2.);
  de_line_LEFT->Draw();

  pad4->cd(); pad4->SetLeftMargin(0.15); pad4->SetFillColor(0);
  dePull->SetMarkerSize(0.05); dePull->Draw();
  TLine *de_lineUP = new TLine(deMin,3,deMax,3);
  de_lineUP->SetLineColor(kBlue);
  de_lineUP->SetLineStyle(2);
  de_lineUP->Draw();
  TLine *de_line = new TLine(deMin,0,deMax,0);
  de_line->SetLineColor(kBlue);
  de_line->SetLineStyle(1);
  de_line->SetLineWidth((Width_t)2.);
  de_line->Draw();
  TLine *de_lineDOWN = new TLine(deMin,-3,deMax,-3);
  de_lineDOWN->SetLineColor(kBlue);
  de_lineDOWN->SetLineStyle(2);
  de_lineDOWN->Draw();

  cm->Update();

  // mbc //
  RooPlot* mbcFrame = mbc.frame();
  ds.plotOn(mbcFrame,DataError(RooAbsData::SumW2),MarkerSize(1),CutRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_cmb_bb),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_cmb_qq),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_sig),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_peak),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,LineWidth(2),ProjectionRange("deSignal"));

  RooHist* hmbcpull = mbcFrame->pullHist();
  RooPlot* mbcPull  = mbc.frame(Title("#Delta E pull distribution"));
  mbcPull->addPlotable(hmbcpull,"P");
  mbcPull->GetYaxis()->SetRangeUser(-5,5);

  TCanvas* cmmbc = new TCanvas("M_{bc}, Signal","M_{bc}, Signal",600,700);
  cmmbc->cd();

  TPad *pad1 = new TPad("pad1","pad1",0.01,0.20,0.99,0.99);
  TPad *pad2 = new TPad("pad2","pad2",0.01,0.01,0.99,0.20);
  pad1->Draw();
  pad2->Draw();

  pad1->cd();
  pad1->SetLeftMargin(0.15);
  pad1->SetFillColor(0);

  mbcFrame->GetXaxis()->SetTitleSize(0.05);
  mbcFrame->GetXaxis()->SetTitleOffset(0.85);
  mbcFrame->GetXaxis()->SetLabelSize(0.04);
  mbcFrame->GetYaxis()->SetTitleOffset(1.6);
  mbcFrame->Draw();

  TPaveText *ptmbc = new TPaveText(0.2,0.6,0.7,0.9,"brNDC");
  ptmbc->SetFillColor(0);
  ptmbc->SetTextAlign(12);
  out1.str("");
  out1 << "#chi^{2}/n.d.f = " << mbcFrame->chiSquare();
  ptmbc->AddText(out1.str().c_str());
  out1.str("");
  out1 << "S: " << (int)(nsigEl1+0.5) << " #pm " << (int)(nsig_errEl1_total+0.5) << " (" << NSIGNAL_ELLI << ")";
  ptmbc->AddText(out1.str().c_str());
//  out1.str("");
//  out1 << "S_{2}: " << (int)(nsigEl+0.5) << " #pm " << (int)(nsig_errEl_total+0.5) << " (" << NSIGNAL_ELLIPS << ")";
//  ptmbc->AddText(out1.str().c_str());
  out1.str("");
  out1 << "P: " << std::fixed << std::setprecision(2) << purityEl1*100. << " #pm " << purity_errEl1*100;
  ptmbc->AddText(out1.str().c_str());
  ptmbc->AddText(label.c_str());
  ptmbc->Draw();

  TLine *mbc_line_RIGHT;
  mbc_line_RIGHT = new TLine(Mbc_max,0,Mbc_max,40);
  mbc_line_RIGHT->SetLineColor(kRed);
  mbc_line_RIGHT->SetLineStyle(1);
  mbc_line_RIGHT->SetLineWidth((Width_t)2.);
  mbc_line_RIGHT->Draw();
  TLine *mbc_line_LEFT;
  mbc_line_LEFT = new TLine(Mbc_min,0,Mbc_min,40);
  mbc_line_LEFT->SetLineColor(kRed);
  mbc_line_LEFT->SetLineStyle(1);
  mbc_line_LEFT->SetLineWidth((Width_t)2.);
  mbc_line_LEFT->Draw();

  pad2->cd();
  pad2->SetLeftMargin(0.15);
  pad2->SetFillColor(0);
  mbcPull->SetMarkerSize(0.05);
  mbcPull->Draw();
  TLine *mbc_lineUP = new TLine(mbcMin,3,mbcMax,3);
  mbc_lineUP->SetLineColor(kBlue);
  mbc_lineUP->SetLineStyle(2);
  mbc_lineUP->Draw();
  TLine *mbc_line = new TLine(mbcMin,0,mbcMax,0);
  mbc_line->SetLineColor(kBlue);
  mbc_line->SetLineStyle(1);
  mbc_line->SetLineWidth((Width_t)2.);
  mbc_line->Draw();
  TLine *mbc_lineDOWN = new TLine(mbcMin,-3,mbcMax,-3);
  mbc_lineDOWN->SetLineColor(kBlue);
  mbc_lineDOWN->SetLineStyle(2);
  mbc_lineDOWN->Draw();

  cmmbc->Update();

  double DEMIN = -0.15;
  if(keysflag) DEMIN = -0.3;
  TH2D* hh_pdf = pdf.createHistogram("hh_data",de,Binning(50,DEMIN,0.1),YVar(mbc,Binning(50,5.26,5.30)));
  hh_pdf->SetLineColor(kBlue);
  TCanvas* hhc = new TCanvas("hhc","hhc",600,600);
  hhc->cd();
  hh_pdf->Draw("SURF");

  // Show signal ranges
  TEllipse* elli = new TEllipse(de_center.getVal(),mbc_center.getVal(),de_radius.getVal(),mbc_radius.getVal());
  elli->SetFillColor(0);
  elli->SetFillStyle(0);
  elli->SetLineColor(kBlue);
  elli->SetLineWidth(2);
  TEllipse* elli1 = new TEllipse(de_center.getVal(),mbc_center.getVal(),de_radius1.getVal(),mbc_radius1.getVal());
  elli1->SetFillColor(0);
  elli1->SetFillStyle(0);
//  elli1->SetLineColor(kBlue);
  elli1->SetLineColor(kRed);
  elli1->SetLineWidth(2);
  TLine* l1 = new TLine(dE_min,Mbc_min,dE_max,Mbc_min);
  l1->SetLineColor(kRed);
  l1->SetLineStyle(1);
  l1->SetLineWidth(2);
  TLine* l2 = new TLine(dE_min,Mbc_max,dE_max,Mbc_max);
  l2->SetLineColor(kRed);
  l2->SetLineStyle(1);
  l2->SetLineWidth(2);
  TLine* l3 = new TLine(dE_min,Mbc_min,dE_min,Mbc_max);
  l3->SetLineColor(kRed);
  l3->SetLineStyle(1);
  l3->SetLineWidth(2);
  TLine* l4 = new TLine(dE_max,Mbc_min,dE_max,Mbc_max);
  l4->SetLineColor(kRed);
  l4->SetLineStyle(1);
  l4->SetLineWidth(2);

  TCanvas* ellican = new TCanvas("ellican","ellican",400,400);
  ellican->cd();
  out.str("");
  out << "bdtg>" << BDTG_MIN << " && de>-0.15 && de<0.20 && mbc>5.265 && b0f != 1 && b0f != 5 && b0f != 10 && b0f != 0";
  tree->Draw("mbc:de",out.str().c_str());
  tree->SetMarkerStyle(6);
  tree->SetMarkerColor(kBlue);
  out.str("");
  out << "bdtg>" << BDTG_MIN << " && de>-0.15 && de<0.20 && mbc>5.265 && (b0f == 1 || b0f == 5 || b0f == 10)";
  tree->Draw("mbc:de",out.str().c_str(),"same");
//  elli->Draw();
  elli1->Draw();
//  ellican->Pad().GetXaxis()->SetTitle("#DeltaE (GeV)");
//  l1->Draw(); l2->Draw(); l3->Draw(); l4->Draw();

//  TCanvas* sigcan = new TCanvas("sigcan","sigcan",400,400);
//  sigcan->cd();
//  out <<
//  tree->Draw("mbc:de","bdtg>0.98 && de>-0.15 && de<0.20 && mbc>5.265 && (b0f == 1 || b0f == 5 || b0f == 10)");
//  elli->Draw(); elli1->Draw(); l1->Draw(); l2->Draw(); l3->Draw(); l4->Draw();

//  TCanvas* backcan = new TCanvas("backcan","backcan",400,400);
//  backcan->cd();
//  tree->Draw("mbc:de","bdtg>0.98 && de>-0.15 && de<0.20 && mbc>5.265 && !(b0f == 1 || b0f == 5 || b0f == 10)");
//  elli->Draw(); elli1->Draw(); l1->Draw(); l2->Draw(); l3->Draw(); l4->Draw();

  cout << "Rectangle:" << endl;
  out.str("");
  out << "de<" << dE_max << " && de>" << dE_min;
  out << " && mbc>" << Mbc_min << " && mbc<" << Mbc_max;
  Roo1DTable* recttable = ds.table(b0f,out.str().c_str());
  recttable->Print();
  recttable->Print("v");

  cout << "Ellips:" << endl;
//  out.str("");
//  out << "(de-" << de_center.getVal() << ")/" << de_radius.getVal() << "*(de-" << de_center.getVal() << ")/" << de_radius.getVal() << "+(mbc-"<<mbc_center.getVal()<<")/" << mbc_radius.getVal() << "*(mbc-" << mbc_center.getVal() << ")/" << mbc_radius.getVal() << "<1";
//  cout << out.str() << endl;
//  Roo1DTable* ellitable = ds.table(b0f,out.str().c_str());
  ellitable->Print();
  ellitable->Print("v");

  cout << "Elli:" << endl;
//  out.str("");
//  out << "(de-" << de_center.getVal() << ")/" << de_radius1.getVal() << "*(de-" << de_center.getVal() << ")/" << de_radius1.getVal() << "+(mbc-"<<mbc_center.getVal()<<")/" << mbc_radius1.getVal() << "*(mbc-" << mbc_center.getVal() << ")/" << mbc_radius1.getVal() << "<1";
//  cout << out.str() << endl;
//  Roo1DTable* ellitable1 = ds.table(b0f,out.str().c_str());
  ellitable1->Print();
  ellitable1->Print("v");

  Roo1DTable* fulltable = ds.table(b0f);
  fulltable->Print();
  fulltable->Print("v");
  const int NSigTotal = fulltable->get("signal") + fulltable->get("fsr") + fulltable->get("bad_pi0");
  const double TruePur = ((double)NSIGNAL_ELLI)/(NSIGNAL_ELLI+ellitable1->get("comb")+ellitable1->get("rho2")+ellitable1->get("rho3")+ellitable1->get("rho4")+ellitable1->get("rho11"));

  cout << "Rectangle:" << endl;
  cout << "Nsig = " << nsig <<" +- " << nsig_err << " +- " << nsig_err_npq << " (" << nsig_err_total << ")" << endl;
  cout << "Npbg = " << nrho <<" +- " << nrho_err << " +- " << nrho_err_npq << " (" << nrho_err_total << ")" << endl;
  cout << "Ncmb = " << ncmb <<" +- " << ncmb_err << " +- " << ncmb_err_npq << " (" << ncmb_err_total << ")" << endl;
  cout << "Pury = " << purity << " +- " << purity_err << endl;

  cout << "Ellips:" << endl;
  cout << "Nsig = " << nsigEl <<" +- " << nsig_errEl << " +- " << nsig_errEl_npq << " (" << nsig_errEl_total << ")" << endl;
  cout << "Npbg = " << nrhoEl <<" +- " << nrho_errEl << " +- " << nrho_errEl_npq << " (" << nrho_errEl_total << ")" << endl;
  cout << "Ncmb = " << ncmbEl <<" +- " << ncmb_errEl << " +- " << ncmb_errEl_npq << " (" << ncmb_errEl_total << ")" << endl;
  cout << "Pury = " << purityEl << " +- " << purity_errEl << endl;

  cout << "Elli:" << endl;
  cout << "Nsig = " << nsigEl1 <<" +- " << nsig_errEl1 << " +- " << nsig_errEl1_npq << " (" << nsig_errEl1_total << ")" << endl;
  cout << "Npbg = " << nrhoEl1 <<" +- " << nrho_errEl1 << " +- " << nrho_errEl1_npq << " (" << nrho_errEl1_total << ")" << endl;
  cout << "Ncmb = " << ncmbEl1 <<" +- " << ncmb_errEl1 << " +- " << ncmb_errEl1_npq << " (" << ncmb_errEl1_total << ")" << endl;
  cout << "Pury = " << purityEl1 << " +- " << purity_errEl1 << endl;

  cout << "Elli signal integral: " << intElli << endl;
  cout << "Nsig (full range): " << Nsig.getVal() << " +- " << Nsig.getError() << " (" << NSigTotal << ")" << endl;
  cout << "True purity: " << TruePur << endl;
}
예제 #4
0
void rf405_realtocatfuncs()
{

  // D e f i n e   p d f   i n   x ,   s a m p l e   d a t a s e t   i n   x 
  // ------------------------------------------------------------------------


  // Define a dummy PDF in x 
  RooRealVar x("x","x",0,10) ;
  RooArgusBG a("a","argus(x)",x,RooConst(10),RooConst(-1)) ;

  // Generate a dummy dataset 
  RooDataSet *data = a.generate(x,10000) ;



  // C r e a t e   a   t h r e s h o l d   r e a l - > c a t   f u n c t i o n
  // --------------------------------------------------------------------------

  // A RooThresholdCategory is a category function that maps regions in a real-valued 
  // input observable observables to state names. At construction time a 'default'
  // state name must be specified to which all values of x are mapped that are not
  // otherwise assigned
  RooThresholdCategory xRegion("xRegion","region of x",x,"Background") ;

  // Specify thresholds and state assignments one-by-one. 
  // Each statement specifies that all values _below_ the given value 
  // (and above any lower specified threshold) are mapped to the
  // category state with the given name
  //
  // Background | SideBand | Signal | SideBand | Background
  //           4.23       5.23     8.23       9.23 
  xRegion.addThreshold(4.23,"Background") ;
  xRegion.addThreshold(5.23,"SideBand") ;
  xRegion.addThreshold(8.23,"Signal") ;
  xRegion.addThreshold(9.23,"SideBand") ; 



  // U s e   t h r e s h o l d   f u n c t i o n   t o   p l o t   d a t a   r e g i o n s
  // -------------------------------------------------------------------------------------

  // Add values of threshold function to dataset so that it can be used as observable
  data->addColumn(xRegion) ;

  // Make plot of data in x
  RooPlot* xframe = x.frame(Title("Demo of threshold and binning mapping functions")) ;
  data->plotOn(xframe) ;

  // Use calculated category to select sideband data
  data->plotOn(xframe,Cut("xRegion==xRegion::SideBand"),MarkerColor(kRed),LineColor(kRed)) ;



  // C r e a t e   a   b i n n i n g    r e a l - > c a t   f u n c t i o n
  // ----------------------------------------------------------------------

  // A RooBinningCategory is a category function that maps bins of a (named) binning definition 
  // in a real-valued input observable observables to state names. The state names are automatically
  // constructed from the variable name, the binning name and the bin number. If no binning name
  // is specified the default binning is mapped

  x.setBins(10,"coarse") ;
  RooBinningCategory xBins("xBins","coarse bins in x",x,"coarse") ;



  // U s e   b i n n i n g   f u n c t i o n   f o r   t a b u l a t i o n   a n d   p l o t t i n g
  // -----------------------------------------------------------------------------------------------

  // Print table of xBins state multiplicity. Note that xBins does not need to be an observable in data
  // it can be a function of observables in data as well
  Roo1DTable* xbtable = data->table(xBins) ;
  xbtable->Print("v") ;

  // Add values of xBins function to dataset so that it can be used as observable
  RooCategory* xb = (RooCategory*) data->addColumn(xBins) ;

  // Define range "alt" as including bins 1,3,5,7,9 
  xb->setRange("alt","x_coarse_bin1,x_coarse_bin3,x_coarse_bin5,x_coarse_bin7,x_coarse_bin9") ;
  
  // Construct subset of data matching range "alt" but only for the first 5000 events and plot it on the frame
  RooDataSet* dataSel = (RooDataSet*) data->reduce(CutRange("alt"),EventRange(0,5000)) ;
  dataSel->plotOn(xframe,MarkerColor(kGreen),LineColor(kGreen)) ;



  new TCanvas("rf405_realtocatfuncs","rf405_realtocatfuncs",600,600) ;
  xframe->SetMinimum(0.01) ;
  gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.4) ; xframe->Draw() ;


}
예제 #5
0
void Purity_2d_fit(int type = 0){
  TChain* tree = new TChain("TEvent");
//  if(!type) tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_3-5.root");
//  else      tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_data.root");
  tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_0-1_full.root");

  gROOT->ProcessLine(".L pdfs/RooRhoDeltaEPdf.cxx+");

  RooCategory b0f("b0f","b0f");
  b0f.defineType("signal",1);
  b0f.defineType("fsr",10);
  b0f.defineType("bad_pi0",5);
  b0f.defineType("rho",3);
  b0f.defineType("comb",-1);

  RooArgSet argset;

  const double mbcMin = 5.20;
  const double mbcMax = 5.29;
  double deMin = -0.15;
  if(keysflag) deMin = -0.3;
  const double deMax = 0.3;
  const double elliscaleDe  = TMath::Sqrt(4./TMath::Pi());
  const double elliscaleMbc = TMath::Sqrt(4./TMath::Pi());

  RooRealVar mbc_center("mbc_center","mbc_center",0.5*(mbc_min+mbc_max),mbc_min,mbc_max); mbc_center.setConstant(kTRUE);
  RooRealVar mbc_center_eq("mbc_center_eq","mbc_center_eq",mr_argedge_3-0.5*(mbc_max-mbc_min)*elliscaleMbc,mbc_min,mbc_max); mbc_center_eq.setConstant(kTRUE);
  RooRealVar de_center("de_center","de_center",0.5*(de_min+de_max),de_min,de_max); de_center.setConstant(kTRUE);
  RooRealVar mbc_radius("mbc_radius","mbc_radius",0.5*(mbc_max-mbc_min)*elliscaleMbc,0,0.5*(mbcMax-mbcMin)); mbc_radius.setConstant(kTRUE);
  RooRealVar de_radius("de_radius","de_radius",0.5*(de_max-de_min)*elliscaleDe,0.,0.5*(deMax-deMin)); de_radius.setConstant(kTRUE);
  RooRealVar mbc_radius1("mbc_radius1","mbc_radius1",0.5*(mbc_max-mbc_min),0,0.5*(mbcMax-mbcMin)); mbc_radius1.setConstant(kTRUE);
  RooRealVar de_radius1("de_radius1","de_radius1",0.5*(de_max-de_min),0.,0.5*(deMax-deMin)); de_radius1.setConstant(kTRUE);

  cout << 0.5*(mbc_min+mbc_max) << " " << 0.5*(mbc_max-mbc_min) << endl;
  cout << 0.5*(de_min+de_max) << " " << 0.5*(de_max-de_min) << endl;

  mbc_center.Print();
  mbc_center_eq.Print();

  RooRealVar mbc("mbc","M_{bc}",0.5*(mbc_min+mbc_max),mbcMin,mbcMax,"GeV"); argset.add(mbc);
  mbc.setRange("Signal",mbc_min,mbc_max);
  mbc.setRange("mbcSignal",mbc_min,mbc_max);
  mbc.setRange("deSignal",mbcMin,mbcMax);

  RooRealVar de("de","#DeltaE",deMin,deMax,"GeV"); argset.add(de);
  de.setRange("Signal",de_min,de_max);
  de.setRange("mbcSignal",deMin,deMax);
  de.setRange("deSignal",de_min,de_max);
  
//   de.setRange("Ellips",de_min,de_max);
//   RooFormulaVar mbclo("mbclo","@1-@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4+0.00001)",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
//   RooFormulaVar mbchi("mbchi","@1+@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4+0.00001)",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
//   mbc.setRange("Ellips",mbclo,mbchi);
  
  RooRealVar md("md","md",DMass-md_cut,DMass+md_cut,"GeV"); argset.add(md);
  RooRealVar mk("mk","mk",KMass-mk_cut,KMass+mk_cut,"GeV"); argset.add(mk);
  RooRealVar mpi0("mpi0","mpi0",Pi0Mass-mpi0_cut,Pi0Mass+mpi0_cut,"GeV"); argset.add(mpi0);
  RooRealVar bdtg("bdtg","bdtg",bdtg_cut_pi0,1.); argset.add(bdtg);
  RooRealVar atckpi_max("atckpi_max","atckpi_max",0.,atckpi_cut); argset.add(atckpi_max);

  if(!type) argset.add(b0f);
  RooDataSet ds("ds","ds",tree,argset,"mbc>0||mbc<=0");
//  RooDataSet* ds0 = ds.reduce(RooArgSet(de,mbc));

  stringstream out;
  if(!type){
    out.str("");
    out << "de<" << de_max << " && de>" << de_min;
    out << " && mbc>" << mbc_min << " && mbc<" << mbc_max;
    Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
    sigtable->Print();
    sigtable->Print("v");

    Roo1DTable* fulltable = ds.table(b0f);
    fulltable->Print();
    fulltable->Print("v");
  }

//  RooDataHist* dh = ds0->binnedClone();

  ds.Print();

  ////////////////
  // Signal PDF //
  ////////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar de0("de0","de0",m_de0,-0.1,0.1); if(cSig && false) de0.setConstant(kTRUE);
  RooRealVar s1("s1","s1",m_s1,0.,0.5);       if(cSig) s1.setConstant(kTRUE);
  RooGaussian g1("g1","g1",de,de0,s1);

  RooRealVar deCBl("deCBl","deCBl",m_deCBl,-0.1,0.1);     if(cSig) deCBl.setConstant(kTRUE);
  RooRealVar sCBl("sCBl","sCBl",m_sCBl,0.,0.5);           if(cSig) sCBl.setConstant(kTRUE);
  RooRealVar nl("nl","nl",m_nl,0.,100.);                  if(cSig) nl.setConstant(kTRUE);
  RooRealVar alphal("alphal","alphal",m_alphal,-10.,10.); if(cSig) alphal.setConstant(kTRUE);

  RooRealVar deCBr("deCBr","deCBr",m_deCBr,-0.1,0.1);     if(cSig) deCBr.setConstant(kTRUE);
  RooRealVar sCBr("sCBr","sCBr",m_sCBr,0.,0.5);           if(cSig) sCBr.setConstant(kTRUE);
  RooRealVar nr("nr","nr",m_nr,0.,100.);                  if(cSig) nr.setConstant(kTRUE);
  RooRealVar alphar("alphar","alphar",m_alphar,-10.,10.); if(cSig) alphar.setConstant(kTRUE);

  RooCBShape CBl("CBl","CBl",de,deCBl,sCBl,alphal,nl);
  RooCBShape CBr("CBr","CBr",de,deCBr,sCBr,alphar,nr);

  RooRealVar fCBl("fCBl","fCBl",m_fCBl,0.,1.); if(cSig) fCBl.setConstant(kTRUE);
  RooRealVar fCBr("fCBr","fCBr",m_fCBr,0.,1.); if(cSig) fCBr.setConstant(kTRUE);

  RooAddPdf pdf_de_sig("pdf_de_sig","pdf_de_sig",RooArgList(CBl,CBr,g1),RooArgSet(fCBl,fCBr));

  /////////////
  // mbc pdf //
  /////////////
  RooRealVar mbc0("mbc0","mbc0",m_mbc0,5.26,5.30); if(cSig && false) mbc0.setConstant(kTRUE);
  RooRealVar sl("sl","sl",m_sl,0.,0.5); if(cSig) sl.setConstant(kTRUE);
  RooRealVar sr("sr","sr",m_sr,0.,0.5); if(cSig) sr.setConstant(kTRUE);
  RooBifurGauss bg("bg","bg",mbc,mbc0,sl,sr);
//  RooGaussian pdf_mbc_sig("pdf_mbc_sig","pdf_mbc_sig",mbc,mbc0,sl);

  RooRealVar mbc00("mbc00","mbc00",m_mbc00,5.26,5.30); if(cSig) mbc00.setConstant(kTRUE);
  RooRealVar sll("sll","sll",m_sll,0.,0.5); if(cSig) sll.setConstant(kTRUE);
  RooRealVar srr("srr","srr",m_srr,0.,0.5); if(cSig) srr.setConstant(kTRUE);
  RooBifurGauss bgg("bgg","bgg",mbc,mbc00,sll,srr);

  RooRealVar fmbc("fmbc","fmbc",m_fmbc,0.,1.); if(cSig) fmbc.setConstant(kTRUE);
  RooAddPdf pdf_mbc_sig("pdf_mbc_sig","pdf_mbc_sig",RooArgList(bg,bgg),RooArgSet(fmbc));
  pdf_mbc_sig.Print();

  /////////
  // pdf //
  /////////
  RooProdPdf pdf_sig("pdf_sig","pdf_sig",RooArgList(pdf_de_sig,pdf_mbc_sig));

  //////////////
  // Comb PDF //
  //////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar c1("c1","c1",mc_c1,-10.,10.); if(cComb && false) c1.setConstant(kTRUE);
  RooRealVar c2("c2","c2",mc_c2,-10.,10.); if(cComb && false) c2.setConstant(kTRUE);
  RooChebychev pdf_de_comb("pdf_de_comb","pdf_de_comb",de,RooArgSet(c1,c2));

  /////////////
  // mbc pdf //
  /////////////
  RooRealVar argpar("argpar","argus shape parameter",mc_argpar,-100.,-1.); if(cComb) argpar.setConstant(kTRUE);
  RooRealVar argedge("argedge","argedge",mc_argedge,5.285,5.292); //argedge.setConstant(kTRUE);
  RooArgusBG pdf_mbc_comb("pdf_mbc_comb","Argus PDF",mbc,argedge,argpar);

  /////////
  // pdf //
  /////////
  RooProdPdf pdf_comb("pdf_comb","pdf_comb",RooArgList(pdf_de_comb,pdf_mbc_comb));
  
  /////////////
  // Rho PDF //
  /////////////
  if(keysflag){
//    TFile* file = TFile::Open("rho_pdf.root");
//    RooHistPdf* pdf_rho = (RooHistPdf*) file->Get("pdf");

    TFile *ifile1 = TFile::Open("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_0-2.root");
    TTree *tree1 = (TTree*)ifile1->Get("TEvent");
    RooArgSet argset1;
    RooCategory b0f("b0f","b0f");
    b0f.defineType("rho",3);
    argset1.add(b0f);
    argset1.add(mbc);
    argset1.add(de);
    argset1.add(md);
    argset1.add(mk);
    argset1.add(mpi0);
    argset1.add(bdtgs);
    argset1.add(atckpi_max);
    RooDataSet ds1("ds1","ds1",tree1,argset1,"mbc>0||mbc<=0");
    RooNDKeysPdf* pdf_rho1 = new RooNDKeysPdf("pdf_rho1","pdf_rho1",RooArgList(de,mbc),ds1,"am",2);
    de.setBins(250);
    mbc.setBins(250);
    RooDataHist* hist = new RooDataHist("hist","hist",RooArgSet(de,mbc));
    pdf_rho1->fillDataHist(hist,new RooArgSet(de,mbc),1);
    RooHistPdf* pdf_rho = new RooHistPdf("pdf_rho","pdf_rho",RooArgSet(de,mbc),*hist);
    de.setBins(100);
    mbc.setBins(100);
  }
  else{
  ////////////
  // de pdf //
  ////////////
  if(de_rho_param == 0){
  RooRealVar exppar("exppar","exppar",mr_exppar,-40.,-25.);// if(cRho) exppar.setConstant(kTRUE);
  RooExponential pdf_de_rho("pdf_de_rho","pdf_de_rho",de,exppar);
  }
  
  RooRealVar de0r("de0r","de0r",mr_de0r,-0.2,0.12); if(cRho) de0r.setConstant(kTRUE);
  
  if(de_rho_param == 1){
   RooRealVar slopel("slopel","slopel",mr_slopel,-1000,-500.); if(cRho) slopel.setConstant(kTRUE);
   RooRealVar sloper("sloper","sloper",mr_sloper,-10000,0.); if(cRho) sloper.setConstant(kTRUE);
   RooRealVar steep("steep","steep",mr_steep,7.,9.); if(cRho) steep.setConstant(kTRUE);
   RooRealVar p5("p5","p5",mr_p5,0.01,1000.); if(cRho) p5.setConstant(kTRUE);
   RooRhoDeltaEPdf pdf_de_rho("pdf_de_rho","pdf_de_rho",de,de0r,slopel,sloper,steep,p5);
  }
  
  if(de_rho_param == -1){
   RooRealVar x0("x0","x0",mr_x0,-0.2,0.12); if(cRho) x0.setConstant(kTRUE);
   RooRealVar p1("p1","p1",mr_p1,-1000.,100.); if(cRho) p1.setConstant(kTRUE);
   RooRealVar p2("p2","p2",mr_p2,0.,100.); if(cRho) p2.setConstant(kTRUE);
   RooGenericPdf pdf_de_rho("pdf_de_rho","1+@0*@1-@2*TMath::Log(1+TMath::Exp(@2*(@0-@1)/@3))",RooArgSet(de,x0,p1,p2));
  }
  
  if(de_rho_param == 2){
     RooRealVar slopel("slopel","slopel",mr_slopel_2,-10,100.); if(cRho) slopel.setConstant(kTRUE);
     RooRealVar steep("steep","steep",mr_steep_2,0.,10000.); if(cRho) steep.setConstant(kTRUE);
     RooRealVar exppar("exppar","exppar",mr_exppar_2,1.,100.); if(cRho) exppar.setConstant(kTRUE);
     RooRealVar x0("x0","x0",mr_x0_2,-0.2,0.12); if(cRho) x0.setConstant(kTRUE);
     RooGenericPdf pdf_de_rho("pdf_de_rho","pdf_de_rho","1+@2*(@0-@1)+@3*TMath::Log(1+TMath::Exp(-@2*(@0-@1)+TMath::Exp(-@4*(@0-@1))))",RooArgSet(de,x0,slopel,steep,exppar));
   }
   
  /////////////
  // mbc pdf //
  /////////////
  if(mbc_rho_param == 0){
  RooRealVar rmbc0("rmbc0","rmbc0",mr_mbc0_0,5.26,5.30); if(cRho) rmbc0.setConstant(kTRUE);
  RooRealVar cond("cond","cond",mr_cond_0,-1000.,1.); if(cRho || true) cond.setConstant(kTRUE);
  RooRealVar condr("condr","condr",mr_condr_0,-1000.,1.); if(cRho || true) condr.setConstant(kTRUE);
  RooRealVar rsl("rsl","rsl",mr_sl_0,0.,0.5); if(cRho) rsl.setConstant(kTRUE);
  RooRealVar rsr("rsr","rsr",mr_sr_0,0.,0.5); if(cRho) rsr.setConstant(kTRUE);
  RooFormulaVar _sl("_sl","_sl","@0+@1*@2",RooArgList(rsl,cond,de));
  RooFormulaVar _sr("_sr","_sr","@0+@1*@2",RooArgList(rsr,condr,de));
  RooBifurGauss pdf_mbc_rho("pdf_mbc_rho","pdf_mbc_rho",mbc,rmbc0,_sl,_sr);
  }

  if(mbc_rho_param == 1){
   RooRealVar cond("cond","cond",mr_cond_1,-1000.,1.); if(cRho || true) cond.setConstant(kTRUE);
   RooRealVar rmbc0("rmbc0","rmbc0",mr_mbc0_1,5.26,5.30); if(cRho) rmbc0.setConstant(kTRUE);
   RooRealVar rsl("rsl","rsl",mr_sl_1,0.,0.5); if(cRho) rsl.setConstant(kTRUE);
   RooFormulaVar _rsl("_rsl","_rsl","@0+@1*(@2-@3)",RooArgList(rsl,cond,de,de0r));
   RooRealVar rsr("rsr","rsr",mr_sr_1,0.,0.5); if(cRho) rsr.setConstant(kTRUE);
   RooBifurGauss rbg("rbg","rbg",mbc,rmbc0,_rsl,rsr);

   RooRealVar rmbc00("rmbc00","rmbc00",mr_mbc00_1,5.26,5.30); if(cRho) rmbc00.setConstant(kTRUE);
   RooRealVar rsll("rsll","rsll",mr_sll_1,0.,0.5); if(cRho) rsll.setConstant(kTRUE);
   RooRealVar rsrr("rsrr","rsrr",mr_srr_1,0.,0.5); if(cRho) rsrr.setConstant(kTRUE);
   RooFormulaVar _rsll("_rsll","_rsll","@0+@1*(@2-@3)",RooArgList(rsll,cond,de,de0r));
   RooFormulaVar _rsrr("_rsrr","_rsrr","@0+@1*(@2-@3)",RooArgList(rsrr,cond,de,de0r));
   RooBifurGauss rbgg("rbgg","rbgg",mbc,rmbc00,_rsll,_rsrr);
 
   RooRealVar rfmbc("rfmbc","rfmbc",mr_fmbc_1,0.,1.); if(cRho) rfmbc.setConstant(kTRUE);
   RooAddPdf pdf_mbc_rho("pdf_mbc_rho","pdf_mbc_rho",RooArgList(rbg,rbgg),RooArgSet(rfmbc));
  }

  if(mbc_rho_param == 2){
   RooRealVar mbc1("mbc1","mbc1",mr_mbc1_2,5.26,5.30); if(cRho)
   mbc1.setConstant(kTRUE);
   RooRealVar ss("ss","ss",mr_ss_2,0.,0.5); if(cRho) ss.setConstant(kTRUE);
   RooRealVar alpha("alpha","alpha",mr_alpha_2,-3,0.); if(cRho) alpha.setConstant(kTRUE);
   RooRealVar n("n","n",mr_n_2,0.1,10.); n.setConstant(kTRUE);
   RooCBShape cb("cb","cb",mbc,mbc1,ss,alpha,n);

   RooRealVar rmbc0("rmbc0","rmbc0",mr_mbc0_2,5.26,5.30); if(cRho) rmbc0.setConstant(kTRUE);
   RooRealVar rsr("rsr","rsr",mr_sr_2,0.,0.5); if(cRho) rsr.setConstant(kTRUE);
   RooGaussian gauss("gauss","gauss",mbc,rmbc0,rsr);
   RooRealVar fcb("fcb","fcb",mr_fcb_2,0.,1.); if(cRho) fcb.setConstant(kTRUE);
   RooAddPdf pdf_mbc_rho("pdf_mbc_rho","pdf_mbc_rho",RooArgList(cb,gauss),RooArgSet(fcb));
  }
  
  if(mbc_rho_param == 3){
   RooRealVar rargpar("rargpar","argus shape parameter",mr_argpar_3,-100.,0.); if(cRho) rargpar.setConstant(kTRUE);
   RooRealVar rargedge("rargedge","rargedge",mr_argedge_3,5.285,5.292); if(cRho) rargedge.setConstant(kTRUE);
   RooArgusBG rargus("rargus","Argus PDF",mbc,rargedge,rargpar);

   RooRealVar rmbc0("rmbc0","rmbc0",mr_mbc0_3,5.26,5.30); if(cRho) rmbc0.setConstant(kTRUE);
   RooRealVar rcond("rcond","rcond",mr_cond_3,-0.1.,1.); if(cRho) rcond.setConstant(kTRUE);
   RooRealVar rcondr("rcondr","rcondr",mr_condr_3,-1000.,1.); if(cRho) rcondr.setConstant(kTRUE);
   RooRealVar rsl("rsl","rsl",mr_sl_3,0.,0.5); if(cRho) rsl.setConstant(kTRUE);
   RooRealVar rsr("rsr","rsr",mr_sr_3,0.,0.5); if(cRho) rsr.setConstant(kTRUE);
   RooFormulaVar _rsl("_rsl","_rsl","@0+@1*@2",RooArgList(rsl,rcond,de));
   RooFormulaVar _rsr("_rsr","_rsr","@0+@1*@2",RooArgList(rsr,rcondr,de));
   RooBifurGauss rbg("rbg","rbg",mbc,rmbc0,_rsl,_rsr);
   RooProdPdf pdf_mbc_rho("pdf_mbc_rho","pdf_mbc_rho",RooArgList(rbg,rargus));
  }

  /////////
  // pdf //
  /////////
  RooProdPdf *pdf_rho = new RooProdPdf("pdf_rho","pdf_rho",pdf_de_rho,Conditional(pdf_mbc_rho,mbc));
//  RooProdPdf pdf_rho("pdf_rho","pdf_rho",pdf_de_rho,pdf_mbc_rho);
  }
void Purity_2d_fit_etagg_corr(bool data = false){
  TChain* tree = new TChain("TEvent");
  if(!data) tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_gen_0-1_full.root");
  else      tree->Add("/home/vitaly/B0toDh0/TMVA/FIL_b2dh_data.root");

  const int _mode = 2;
  const int _h0mode = 10;
  const int _b0f = -1;
//  gROOT->ProcessLine(".L pdfs/RooRhoDeltaEPdf.cxx+");

  RooCategory b0f("b0f","b0f");
  b0f.defineType("signal",1);
  b0f.defineType("fsr",10);
  b0f.defineType("bad_pi0",5);
//  b0f.defineType("peak1",3);
//  b0f.defineType("peak2",4);
//  b0f.defineType("peak3",11);
//  b0f.defineType("peak4",20);
  b0f.defineType("comb",-1);

//  RooCategory d0f("d0f","d0f");
//  d0f.defineType("signal",1);
  RooArgSet argset;
  argset.add(b0f);

  const double mbcMin = 5.2;
  const double mbcMax = 5.29;
  double deMin = -0.15;
//  if(keysflag) deMin = -0.3;
  const double deMax = 0.3;
  const double elliscaleDe  = TMath::Sqrt(4./TMath::Pi());
  const double elliscaleMbc = TMath::Sqrt(4./TMath::Pi());

  const double DE_MIN = de_min;
  const double DE_MAX = de_max;
//  RooConstVar DELO("DELO","DELO",DE_MIN);
  RooConstVar DELO("DELO","DELO",DE_MIN);

  RooRealVar mbc_center("mbc_center","mbc_center",0.5*(mbc_min+mbc_max),mbc_min,mbc_max); mbc_center.setConstant(kTRUE);
  RooRealVar mbc_center_eq("mbc_center_eq","mbc_center_eq",mr_argedge_3-0.5*(mbc_max-mbc_min)*elliscaleMbc,mbc_min,mbc_max); mbc_center_eq.setConstant(kTRUE);
  RooRealVar de_center("de_center","de_center",0.5*(DE_MIN+DE_MAX),DE_MIN,DE_MAX); de_center.setConstant(kTRUE);
  RooRealVar mbc_radius("mbc_radius","mbc_radius",0.5*(mbc_max-mbc_min)*elliscaleMbc,0,0.5*(mbcMax-mbcMin)); mbc_radius.setConstant(kTRUE);
  RooRealVar de_radius("de_radius","de_radius",0.5*(DE_MAX-DE_MIN)*elliscaleDe,0.,0.5*(deMax-deMin)); de_radius.setConstant(kTRUE);
  RooRealVar mbc_radius1("mbc_radius1","mbc_radius1",0.5*(mbc_max-mbc_min),0,0.5*(mbcMax-mbcMin)); mbc_radius1.setConstant(kTRUE);
  RooRealVar de_radius1("de_radius1","de_radius1",0.5*(DE_MAX-DE_MIN),0.,0.5*(deMax-deMin)); de_radius1.setConstant(kTRUE);

  cout << 0.5*(mbc_min+mbc_max) << " " << 0.5*(mbc_max-mbc_min) << endl;
  cout << 0.5*(DE_MIN+DE_MAX) << " " << 0.5*(DE_MAX-DE_MIN) << endl;

  mbc_center.Print();
  mbc_center_eq.Print();

  const double BDTG_MIN = bdtg_cut_etagg;
  const double BDTG_MAX = 1;
  RooCategory mode("mode","mode");
  mode.defineType("eta",2);
  RooCategory h0mode("h0mode","h0mode");
  h0mode.defineType("gg",10);
  argset.add(mode);
  argset.add(h0mode);

  RooRealVar mbc("mbc","M_{bc}",0.5*(mbc_min+mbc_max),mbcMin,mbcMax,"GeV");
  argset.add(mbc);
  mbc.setRange("Signal",mbc_min,mbc_max);
  mbc.setRange("mbcSignal",mbc_min,mbc_max);
  mbc.setRange("deSignal",mbcMin,mbcMax);

  RooRealVar de("de","#DeltaE",deMin,deMax,"GeV");
  argset.add(de);
  de.setRange("Signal",DE_MIN,DE_MAX);
  de.setRange("mbcSignal",deMin,deMax);
  de.setRange("deSignal",DE_MIN,DE_MAX);
  
  RooRealVar md("md","md",DMass-md_cut,DMass+md_cut,"GeV");
  argset.add(md);
  RooRealVar mk("mk","mk",KMass-mk_cut,KMass+mk_cut,"GeV");
  argset.add(mk);
  RooRealVar mh0("mh0","mh0",EtaMass-meta_cut,EtaMass+meta_cut,"GeV");
  argset.add(mh0);
  RooRealVar bdtg("bdtg","bdtg",BDTG_MIN,1.);
  argset.add(bdtg);
  RooRealVar atckpi_max("atckpi_max","atckpi_max",0.,atckpi_cut);
  argset.add(atckpi_max);

  argset.add(b0f);
  RooDataSet ds("ds","ds",tree,argset,"mbc>0||mbc<=0");
//  RooDataSet* ds0 = ds.reduce(RooArgSet(de,mbc));

  stringstream out;
  out.str("");
  out << "de<" << DE_MAX << " && de>" << DE_MIN;
  out << " && mbc>" << mbc_min << " && mbc<" << mbc_max;
  Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
  sigtable->Print();
  sigtable->Print("v");

  Roo1DTable* fulltable = ds.table(b0f);
  fulltable->Print();
  fulltable->Print("v");

//  RooDataHist* dh = ds0->binnedClone();

  ds.Print();

  if(!data){
    out.str("");
    out << "de<" << DE_MAX << " && de>" << DE_MIN;
    out << " && mbc>" << mbc_min << " && mbc<" << mbc_max;
    Roo1DTable* sigtable = ds.table(b0f,out.str().c_str());
    sigtable->Print();
    sigtable->Print("v");

    Roo1DTable* fulltable = ds.table(b0f);
    fulltable->Print();
    fulltable->Print("v");
  }

  ////////////////
  // Signal PDF //
  ////////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar de0("de0","de0",get_de0(_mode,_h0mode,_b0f),-0.2,0.1); if(cSIG) de0.setConstant(kTRUE);
  RooRealVar s1("s1","s1",get_s1(_mode,_h0mode,_b0f),0.,0.5);       if(cSIG) s1.setConstant(kTRUE);
  RooGaussian g1("g1","g1",de,de0,s1);

  RooRealVar deCBl("deCBl","deCBl",get_deCBl(_mode,_h0mode,_b0f),-0.2,0.1);     if(cSIG) deCBl.setConstant(kTRUE);
  RooRealVar sCBl("sCBl","sCBl",get_sCBl(_mode,_h0mode,_b0f),0.,0.5);           if(cSIG) sCBl.setConstant(kTRUE);
  RooRealVar nl("nl","nl",get_nl(_mode,_h0mode,_b0f),0.,100.);                  if(cSIG) nl.setConstant(kTRUE);
  RooRealVar alphal("alphal","alphal",get_alphal(_mode,_h0mode,_b0f),-10.,10.); if(cSIG) alphal.setConstant(kTRUE);

  RooRealVar deCBr("deCBr","deCBr",get_deCBr(_mode,_h0mode,_b0f),-0.2,0.1);     if(cSIG) deCBr.setConstant(kTRUE);
  RooRealVar sCBr("sCBr","sCBr",get_sCBr(_mode,_h0mode,_b0f),0.,0.5);           if(cSIG) sCBr.setConstant(kTRUE);
  RooRealVar nr("nr","nr",get_nr(_mode,_h0mode,_b0f),0.,100.);                  if(cSIG) nr.setConstant(kTRUE);
  RooRealVar alphar("alphar","alphar",get_alphar(_mode,_h0mode,_b0f),-10.,10.); if(cSIG) alphar.setConstant(kTRUE);

  RooCBShape CBl("CBl","CBl",de,deCBl,sCBl,alphal,nl);
  RooCBShape CBr("CBr","CBr",de,deCBr,sCBr,alphar,nr);

  RooRealVar fCBl("fCBl","fCBl",get_fCBl(_mode,_h0mode,_b0f),0.,1.); if(cSIG) fCBl.setConstant(kTRUE);
  RooRealVar fCBr("fCBr","fCBr",get_fCBr(_mode,_h0mode,_b0f),0.,1.); if(cSIG) fCBr.setConstant(kTRUE);

  RooAddPdf pdf_de_sig("pdf_de_sig","pdf_de_sig",RooArgList(CBl,CBr,g1),RooArgSet(fCBl,fCBr));

  /////////////
  // mbc pdf //
  /////////////
  RooRealVar c1_mbc0("c1_mbc0","c1_mbc0",m_mbc0_c1_eta10,-0.1,0.); c1_mbc0.setConstant(kTRUE);
  RooRealVar c2_mbc0("c2_mbc0","c2_mbc0",m_mbc0_c2_eta10,0.,1.);   c2_mbc0.setConstant(kTRUE);
  RooRealVar c3_mbc0("c3_mbc0","c3_mbc0",m_mbc0_c3_eta10,0.,10.);  c3_mbc0.setConstant(kTRUE);
  RooRealVar mbc0("mbc0","mbc0",m_mbc0_c0_eta10,5.26,5.30); if(cSIG) mbc0.setConstant(kTRUE);
//  RooFormulaVar _mbc0("_mbc0","_mbc0","abs(@0+@1*@2+@1*@1*@3+@1*@1*@1*@4) > abs(@0+@5*@2+@5*@5*@3+@5*@5*@5*@4) ? (@0+@5*@2+@5*@5*@3+@5*@5*@5*@4) : @0+@1*@2+@1*@1*@3+@1*@1*@1*@4",RooArgList(mbc0,de,c1_mbc0,c2_mbc0,c3_mbc0,DELO));
  RooFormulaVar _mbc0("_mbc0","_mbc0","abs(@1*@2+@1*@1*@3+@1*@1*@1*@4) > abs(@5*@2+@5*@5*@3+@5*@5*@5*@4) ? @0 : @0+@1*@2+@1*@1*@3+@1*@1*@1*@4",RooArgList(mbc0,de,c1_mbc0,c2_mbc0,c3_mbc0,DELO));

  RooRealVar c1_alpha("c1_alpha","c1_alpha",m_alpha_c1_eta10,-2.,0.); c1_alpha.setConstant(kTRUE);
  RooRealVar c2_alpha("c2_alpha","c2_alpha",m_alpha_c2_eta10,10.,100.);c2_alpha.setConstant(kTRUE);
  RooRealVar c3_alpha("c3_alpha","c3_alpha",m_alpha_c3_eta10,10.,100.);c3_alpha.setConstant(kTRUE);
  RooRealVar alpha("alpha","alpha",m_alpha_c0_eta10,0.,0.1); if(cSIG) alpha.setConstant(kTRUE);
  RooFormulaVar _alpha("_alpha","_alpha","abs(@0+@1*@2+@1*@1*@3+@1*@1*@1*@4) > abs(@0+@5*@2+@5*@5*@3+@5*@5*@5*@4) ? (@0+@5*@2+@5*@5*@3+@5*@5*@5*@4) : (@0+@1*@2+@1*@1*@3+@1*@1*@1*@4)",RooArgList(alpha,de,c1_alpha,c2_alpha,c3_alpha,DELO));

  RooRealVar c1_width("c1_width","c1_width",m_width_c1_eta10,-2.,0.);  c1_width.setConstant(kTRUE);
  RooRealVar c2_width("c2_width","c2_width",m_width_c2_eta10,10.,100.);c2_width.setConstant(kTRUE);
  RooRealVar width("width","width",m_width_c0_eta10,0.,0.1); if(cSIG) width.setConstant(kTRUE);
  RooFormulaVar _width("_width","_width","@0+@1*@2+@1*@1*@3",RooArgList(width,de,c1_width,c2_width));

  RooNovosibirsk pdf_mbc_sig("pdf_mbc_sig","pdf_mbc_sig",mbc,_mbc0,_width,_alpha);

  /////////
  // pdf //
  /////////
  RooProdPdf pdf_sig("pdf_sig","pdf_sig",pdf_de_sig,Conditional(pdf_mbc_sig,mbc));

  //////////////
  // Comb PDF //
  //////////////
  ////////////
  // de pdf //
  ////////////
  RooRealVar c10("c10","c10",m_c10_eta10,-10.,10.);c10.setConstant(kTRUE);
  RooRealVar c11("c11","c11",m_c11_eta10,-10.,10); c11.setConstant(kTRUE);
  RooRealVar c12("c12","c12",m_c12_eta10,-10.,10); c12.setConstant(kTRUE);
  RooFormulaVar _c1("_c1","@0+@1*@3+@2*@3*@3",RooArgSet(c10,c11,c12,mbc));
  RooRealVar c20("c20","c20",m_c20_eta10,-10.,10.);c20.setConstant(kTRUE);
  RooRealVar c21("c21","c21",m_c21_eta10,-10.,10); c21.setConstant(kTRUE);
  RooFormulaVar _c2("_c2","@0+@1*@2",RooArgSet(c20,c21,mbc));

  RooRealVar d1("d1","d1",0.,-100000,100000);
  RooRealVar d2("d2","d2",0.,-100000,100000);
  RooChebychev pdf_de_comb("pdf_de_comb","pdf_de_comb",de,RooArgSet(d1,d2));

  /////////////
  // mbc pdf //
  /////////////
  RooRealVar argpar("argpar","argus shape parameter",m_argpar_eta10,-100.,-1.); argpar.setConstant(kTRUE);
  RooRealVar argedge("argedge","argedge",m_argedge_eta10,5.285,5.292);          argedge.setConstant(kTRUE);
  RooArgusBG pdf_mbc_comb("pdf_mbc_comb","Argus PDF",mbc,argedge,argpar);

  //////////////
  // pdf comb //
  //////////////
//  RooProdPdf pdf_comb("pdf_comb","pdf_comb",pdf_mbc_comb,Conditional(pdf_de_comb,de));
  RooProdPdf pdf_comb("pdf_comb","pdf_comb",RooArgSet(pdf_mbc_comb,pdf_de_comb,de));
  
  /////////////////
  // BB comb PDF //
  /////////////////
  /////////////
  // mbc pdf //
  /////////////
//  RooRealVar edge("edge","edge",5.29,5.28,5.30,"GeV");// edge.setConstant(kTRUE);
//  RooRealVar mbctau("mbctau","mbctau",-97.,-300,0.,"GeV");
//  RooRealVar pow1("pow1","pow1",6.8,0.1,10);
//  RooRealVar pow2("pow2","pow2",0.12,0.1,10);

  RooRealVar edge("edge","edge",5.29,5.28,5.30,"GeV");       edge.setConstant(kTRUE);
  RooRealVar mbctau("mbctau","mbctau",m_mbctau_bbcomb_eta10,-100,0.,"GeV"); mbctau.setConstant(kTRUE);
  RooRealVar pow1("pow1","pow1",m_pow1_bbcomb_eta10,0.1,10);                pow1.setConstant(kTRUE);
  RooRealVar pow2("pow2","pow2",m_pow2_bbcomb_eta10,0.1,10);                pow2.setConstant(kTRUE);

  RooGenericPdf pdf_mbc_bb_comb("pdf_mbc_bb_comb","pow(@1-@0,@2)*exp(@3*pow(@1-@0,@4))",RooArgList(mbc,edge,pow1,mbctau,pow2));

  ////////////
  // de pdf //
  ////////////
  RooRealVar c3("c3","c3",m_c0_bb_eta10,-100000.,10.); c3.setConstant(kTRUE);
  RooRealVar c31("c31","c31",m_c1_bb_eta10,-10.,10.); c31.setConstant(kTRUE);
  RooFormulaVar _c3("_c3","@0+@1*@2",RooArgSet(c3,c31,mbc));
  RooExponential pdf_de_bb_comb("pdf_de_bb_comb","pdf_de_bb_comb",de,_c3);

  RooProdPdf pdf_bb_comb("pdf_bb_comb","pdf_bb_comb",pdf_mbc_bb_comb,Conditional(pdf_de_bb_comb,de));

  //////////////////
  // Complete PDF //
  //////////////////
  const bool OneDfit = false;
  RooRealVar Nsig("Nsig","Nsig",600,0.,10000.);
  RooRealVar Ncmb("Ncmb","Ncmb",10000,0,100000);
  RooRealVar NBBcmb("NBBcmb","NBBcmb",0,0,100000.);
  RooAddPdf pdf("pdf","pdf",RooArgList(pdf_sig,pdf_comb,pdf_bb_comb),RooArgList(Nsig,Ncmb,NBBcmb));
//  RooAddPdf pdf("pdf","pdf",RooArgList(pdf_sig,pdf_bb_comb),RooArgList(Nsig,NBBcmb));
//  RooAddPdf pdf("pdf","pdf",RooArgList(pdf_sig,pdf_comb),RooArgList(Nsig,Ncmb));
//  RooFormulaVar NCmb("NCmb","@0+@1",RooArgList(NBBcmb,Ncmb));

//  RooArgSet* params = pdf.getParameters(RooArgSet(de,mbc));
//  RooArgset* initParams = (RooArgSet*) params->snapshot();
  
  if(!OneDfit) pdf.fitTo(ds,Verbose(),Timer(true));
  else{
    RooAddPdf pdf_de("pdf_de","pdf_de",RooArgList(pdf_de_sig,pdf_de_comb),RooArgList(Nsig,Ncmb));
    NBBcmb.setVal(0);
    NBBcmb.setConstant(kTRUE);
    pdf_de.fitTo(ds,Verbose(),Timer(true));
  }

//return;
//  params->printLatex(OutputFile("PurityEtaGGFit.tex"));

   RooAbsReal* intSig  = pdf_sig.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Signal"));
   RooAbsReal* intRho  = pdf_bb_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Signal"));
   RooAbsReal* intCmb  = pdf_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Signal"));
   const double nsig = intSig->getVal()*Nsig.getVal();
   const double nsig_err = intSig->getVal()*Nsig.getError();
   const double nsig_err_npq = TMath::Sqrt(nsig*(Nsig.getVal()-nsig)/Nsig.getVal());
   const double nsig_err_total = TMath::Sqrt(nsig_err*nsig_err+nsig_err_npq*nsig_err_npq);
   const double nrho = intRho->getVal()*NBBcmb.getVal();
   const double nrho_err = intRho->getVal()*NBBcmb.getError();
   const double nrho_err_npq = TMath::Sqrt(nrho*(NBBcmb.getVal()-nrho)/NBBcmb.getVal());
   const double nrho_err_total = TMath::Sqrt(nrho_err*nrho_err+nrho_err_npq*nrho_err_npq);
   const double ncmb = intCmb->getVal()*Ncmb.getVal();
   const double ncmb_err = intCmb->getVal()*Ncmb.getError();
   const double ncmb_err_npq = TMath::Sqrt(ncmb*(Ncmb.getVal()-ncmb)/Ncmb.getVal());
   const double ncmb_err_total = TMath::Sqrt(ncmb_err*ncmb_err+ncmb_err_npq*ncmb_err_npq);
   const double purity = nsig/(nsig+nrho+ncmb);
   const double purity_err = nsig_err_total/(nsig+nrho+ncmb);

   de.setRange("Ellips",DE_MIN,DE_MAX);
   RooFormulaVar mbclo("mbclo","(1-(@0-@3)/@4*(@0-@3)/@4) > 0 ? @1-@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4) : 0",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
   RooFormulaVar mbchi("mbchi","(1-(@0-@3)/@4*(@0-@3)/@4) > 0 ? @1+@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4) : 0",RooArgSet(de,mbc_center,mbc_radius,de_center,de_radius));
   mbc.setRange("Ellips",mbclo,mbchi);
   
   de.setRange("Elli",DE_MIN,DE_MAX);
   RooFormulaVar mbclo1("mbclo1","(1-(@0-@3)/@4*(@0-@3)/@4) > 0 ? @1-@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4) : 0",RooArgSet(de,mbc_center,mbc_radius1,de_center,de_radius1));
   RooFormulaVar mbchi1("mbchi1","(1-(@0-@3)/@4*(@0-@3)/@4) > 0 ? @1+@2*TMath::Sqrt(1-(@0-@3)/@4*(@0-@3)/@4) : 0",RooArgSet(de,mbc_center,mbc_radius1,de_center,de_radius1));
   mbc.setRange("Elli",mbclo1,mbchi1);

   RooAbsReal* intSigEl = pdf_sig.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Ellips"));
   RooAbsReal* intRhoEl = pdf_bb_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Ellips"));
   RooAbsReal* intCmbEl = pdf_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Ellips"));
   const double nsigEl = intSigEl->getVal()*Nsig.getVal();
   const double nsig_errEl = intSigEl->getVal()*Nsig.getError();
   const double nsig_errEl_npq = TMath::Sqrt(nsigEl*(Nsig.getVal()-nsigEl)/Nsig.getVal());
   const double nsig_errEl_total = TMath::Sqrt(nsig_errEl*nsig_errEl+nsig_errEl_npq*nsig_errEl_npq);
   const double nrhoEl = intRhoEl->getVal()*NBBcmb.getVal();
   const double nrho_errEl = intRhoEl->getVal()*NBBcmb.getError();
   const double nrho_errEl_npq = TMath::Sqrt(nrhoEl*(NBBcmb.getVal()-nrhoEl)/NBBcmb.getVal());
   const double nrho_errEl_total = TMath::Sqrt(nrho_errEl*nrho_errEl+nrho_errEl_npq*nrho_errEl_npq);
   const double ncmbEl = intCmbEl->getVal()*Ncmb.getVal();
   const double ncmb_errEl = intCmbEl->getVal()*Ncmb.getError();
   const double ncmb_errEl_npq = TMath::Sqrt(ncmbEl*(Ncmb.getVal()-ncmbEl)/Ncmb.getVal());
   const double ncmb_errEl_total = TMath::Sqrt(ncmb_errEl*ncmb_errEl+ncmb_errEl_npq*ncmb_errEl_npq);
   const double purityEl = nsigEl/(nsigEl+nrhoEl+ncmbEl);
   const double purity_errEl = nsig_errEl_total/(nsigEl+nrhoEl+ncmbEl);

   RooAbsReal* intSigEl1 = pdf_sig.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Elli"));
   RooAbsReal* intRhoEl1 = pdf_bb_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Elli"));
   RooAbsReal* intCmbEl1 = pdf_comb.createIntegral(RooArgSet(de,mbc),NormSet(RooArgSet(de,mbc)),Range("Elli"));
   const double nsigEl1 = intSigEl1->getVal()*Nsig.getVal();
   const double nsig_errEl1 = intSigEl1->getVal()*Nsig.getError();
   const double nsig_errEl1_npq = TMath::Sqrt(nsigEl1*(Nsig.getVal()-nsigEl1)/Nsig.getVal());
   const double nsig_errEl1_total = TMath::Sqrt(nsig_errEl1*nsig_errEl1+nsig_errEl1_npq*nsig_errEl1_npq);
   const double nrhoEl1 = intRhoEl1->getVal()*NBBcmb.getVal();
   const double nrho_errEl1 = intRhoEl1->getVal()*NBBcmb.getError();
   const double nrho_errEl1_npq = TMath::Sqrt(nrhoEl1*(NBBcmb.getVal()-nrhoEl1)/NBBcmb.getVal());
   const double nrho_errEl1_total = TMath::Sqrt(nrho_errEl1*nrho_errEl1+nrho_errEl1_npq*nrho_errEl1_npq);
   const double ncmbEl1 = intCmbEl1->getVal()*Ncmb.getVal();
   const double ncmb_errEl1 = intCmbEl1->getVal()*Ncmb.getError();
   const double ncmb_errEl1_npq = TMath::Sqrt(ncmbEl1*(Ncmb.getVal()-ncmbEl1)/Ncmb.getVal());
   const double ncmb_errEl1_total = TMath::Sqrt(ncmb_errEl1*ncmb_errEl1+ncmb_errEl1_npq*ncmb_errEl1_npq);
   const double purityEl1 = nsigEl1/(nsigEl1+nrhoEl1+ncmbEl1);
   const double purity_errEl1 = nsig_errEl1_total/(nsigEl1+nrhoEl1+ncmbEl1);

  /////////////
  //  Plots  //
  /////////////
  // de //
  RooPlot* deFrame = de.frame();
  ds.plotOn(deFrame,DataError(RooAbsData::SumW2),MarkerSize(1),CutRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(pdf_sig),LineStyle(kDashed),ProjectionRange("mbcSignal"));
//  pdf.plotOn(deFrame,Components(pdf_back),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(pdf_bb_comb),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,Components(RooArgSet(pdf_comb)),LineStyle(kDashed),ProjectionRange("mbcSignal"));
  pdf.plotOn(deFrame,LineWidth(2),ProjectionRange("mbcSignal"));

  RooHist* hdepull = deFrame->pullHist();
  RooPlot* dePull = de.frame(Title("#Delta E pull distribution"));
  dePull->addPlotable(hdepull,"P");
  dePull->GetYaxis()->SetRangeUser(-5,5);

  TCanvas* cm = new TCanvas("#Delta E","#Delta E",600,700);
  cm->cd();

  TPad *pad3 = new TPad("pad3","pad3",0.01,0.20,0.99,0.99);
  TPad *pad4 = new TPad("pad4","pad4",0.01,0.01,0.99,0.20);
  pad3->Draw();
  pad4->Draw();

  pad3->cd();
  pad3->SetLeftMargin(0.15);
  pad3->SetFillColor(0);

  deFrame->GetXaxis()->SetTitleSize(0.05);
  deFrame->GetXaxis()->SetTitleOffset(0.85);
  deFrame->GetXaxis()->SetLabelSize(0.04);
  deFrame->GetYaxis()->SetTitleOffset(1.6);
  deFrame->Draw();

  stringstream out1;
  TPaveText *pt = new TPaveText(0.6,0.75,0.98,0.9,"brNDC");
  pt->SetFillColor(0);
  pt->SetTextAlign(12);
  out1.str("");
  out1 << "#chi^{2}/n.d.f = " << deFrame->chiSquare();
  pt->AddText(out1.str().c_str());
  out1.str("");
  if(!data) out1 << "S: " << (int)(nsig+0.5) << " #pm " << (int)(nsig_err_total+0.5);
  else      out1 << "S: " << (int)(nsigEl+0.5) << " #pm " << (int)(nsig_errEl_total+0.5);
  pt->AddText(out1.str().c_str());
  out1.str("");
  if(!data) out1 << "Purity: " << std::fixed << std::setprecision(2) << purity*100. << " #pm " << purity_err*100;
  else out1 << "Purity: " << std::fixed << std::setprecision(2) << purityEl*100. << " #pm " << purity_errEl*100;
  pt->AddText(out1.str().c_str());
  pt->Draw();

  TLine *de_line_RIGHT;
  if(!data) de_line_RIGHT = new TLine(DE_MAX,0,DE_MAX,120);
  else      de_line_RIGHT = new TLine(DE_MAX,0,DE_MAX,30);
  de_line_RIGHT->SetLineColor(kRed);
  de_line_RIGHT->SetLineStyle(1);
  de_line_RIGHT->SetLineWidth((Width_t)2.);
  de_line_RIGHT->Draw();
  TLine *de_line_LEFT;
  if(!data) de_line_LEFT = new TLine(DE_MIN,0,DE_MIN,120);
  else      de_line_LEFT = new TLine(DE_MIN,0,DE_MIN,30);
  de_line_LEFT->SetLineColor(kRed);
  de_line_LEFT->SetLineStyle(1);
  de_line_LEFT->SetLineWidth((Width_t)2.);
  de_line_LEFT->Draw();

  pad4->cd(); pad4->SetLeftMargin(0.15); pad4->SetFillColor(0);
  dePull->SetMarkerSize(0.05); dePull->Draw();
  TLine *de_lineUP = new TLine(deMin,3,deMax,3);
  de_lineUP->SetLineColor(kBlue);
  de_lineUP->SetLineStyle(2);
  de_lineUP->Draw();
  TLine *de_line = new TLine(deMin,0,deMax,0);
  de_line->SetLineColor(kBlue);
  de_line->SetLineStyle(1);
  de_line->SetLineWidth((Width_t)2.);
  de_line->Draw();
  TLine *de_lineDOWN = new TLine(deMin,-3,deMax,-3);
  de_lineDOWN->SetLineColor(kBlue);
  de_lineDOWN->SetLineStyle(2);
  de_lineDOWN->Draw();

  cm->Update();
  
  // mbc //
  RooPlot* mbcFrame = mbc.frame();
  ds.plotOn(mbcFrame,DataError(RooAbsData::SumW2),MarkerSize(1),CutRange("deSignal"));
//  pdf.plotOn(mbcFrame,Components(pdf_back),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_comb),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_bb_comb),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,Components(pdf_sig),LineStyle(kDashed),ProjectionRange("deSignal"));
  pdf.plotOn(mbcFrame,LineWidth(2),ProjectionRange("deSignal"));

  RooHist* hmbcpull = mbcFrame->pullHist();
  RooPlot* mbcPull = mbc.frame(Title("#Delta E pull distribution"));
  mbcPull->addPlotable(hmbcpull,"P");
  mbcPull->GetYaxis()->SetRangeUser(-5,5);

  TCanvas* cmmbc = new TCanvas("M_{bc}","M_{bc}",600,700);
  cmmbc->cd();

  TPad *pad1 = new TPad("pad1","pad1",0.01,0.20,0.99,0.99);
  TPad *pad2 = new TPad("pad2","pad2",0.01,0.01,0.99,0.20);
  pad1->Draw();
  pad2->Draw();

  pad1->cd();
  pad1->SetLeftMargin(0.15);
  pad1->SetFillColor(0);

  mbcFrame->GetXaxis()->SetTitleSize(0.05);
  mbcFrame->GetXaxis()->SetTitleOffset(0.85);
  mbcFrame->GetXaxis()->SetLabelSize(0.04);
  mbcFrame->GetYaxis()->SetTitleOffset(1.6);
  mbcFrame->Draw();

  TPaveText *ptmbc = new TPaveText(0.2,0.75,0.58,0.9,"brNDC");
  ptmbc->SetFillColor(0);
  ptmbc->SetTextAlign(12);
  out1.str("");
  out1 << "#chi^{2}/n.d.f = " << mbcFrame->chiSquare();
  ptmbc->AddText(out1.str().c_str());
  out1.str("");
  if(!data) out1 << "S: " << (int)(nsig+0.5) << " #pm " << (int)(nsig_err_total+0.5);
  else      out1 << "S: " << (int)(nsigEl+0.5) << " #pm " << (int)(nsig_errEl_total+0.5);
  ptmbc->AddText(out1.str().c_str());
  out1.str("");
  if(!data) out1 << "Purity: " << std::fixed << std::setprecision(2) << purity*100. << " #pm " << purity_err*100;
  else out1 << "Purity: " << std::fixed << std::setprecision(2) << purityEl*100. << " #pm " << purity_errEl*100;
  ptmbc->AddText(out1.str().c_str());
  ptmbc->Draw();

  TLine *mbc_line_RIGHT;
  if(!data) mbc_line_RIGHT = new TLine(mbc_max,0,mbc_max,70);
  else      mbc_line_RIGHT = new TLine(mbc_max,0,mbc_max,40);
  mbc_line_RIGHT->SetLineColor(kRed);
  mbc_line_RIGHT->SetLineStyle(1);
  mbc_line_RIGHT->SetLineWidth((Width_t)2.);
  mbc_line_RIGHT->Draw();
  TLine *mbc_line_LEFT;
  if(!data) mbc_line_LEFT = new TLine(mbc_min,0,mbc_min,70);
  else      mbc_line_LEFT = new TLine(mbc_min,0,mbc_min,40);
  mbc_line_LEFT->SetLineColor(kRed);
  mbc_line_LEFT->SetLineStyle(1);
  mbc_line_LEFT->SetLineWidth((Width_t)2.);
  mbc_line_LEFT->Draw();
  
  pad2->cd();
  pad2->SetLeftMargin(0.15);
  pad2->SetFillColor(0);
  mbcPull->SetMarkerSize(0.05);
  mbcPull->Draw();
  TLine *mbc_lineUP = new TLine(mbcMin,3,mbcMax,3);
  mbc_lineUP->SetLineColor(kBlue);
  mbc_lineUP->SetLineStyle(2);
  mbc_lineUP->Draw();
  TLine *mbc_line = new TLine(mbcMin,0,mbcMax,0);
  mbc_line->SetLineColor(kBlue);
  mbc_line->SetLineStyle(1);
  mbc_line->SetLineWidth((Width_t)2.);
  mbc_line->Draw();
  TLine *mbc_lineDOWN = new TLine(mbcMin,-3,mbcMax,-3);
  mbc_lineDOWN->SetLineColor(kBlue);
  mbc_lineDOWN->SetLineStyle(2);
  mbc_lineDOWN->Draw();

  cmmbc->Update();
  
  double DEMIN = -0.15;
//  if(keysflag) DEMIN = -0.3;
  TH2D* hh_pdf = pdf.createHistogram("hh_data",de,Binning(50,DEMIN,0.1),YVar(mbc,Binning(50,5.26,5.30)));
  hh_pdf->SetLineColor(kBlue);
  TCanvas* hhc = new TCanvas("hhc","hhc",600,600);
  hhc->cd();
  hh_pdf->Draw("SURF");

  // Show signal ranges
  TEllipse* elli = new TEllipse(de_center.getVal(),mbc_center.getVal(),de_radius.getVal(),mbc_radius.getVal());
  elli->SetFillColor(0);
  elli->SetFillStyle(0);
  elli->SetLineColor(kBlue);
  elli->SetLineWidth(2);
  TEllipse* elli1 = new TEllipse(de_center.getVal(),mbc_center.getVal(),de_radius1.getVal(),mbc_radius1.getVal());
  elli1->SetFillColor(0);
  elli1->SetFillStyle(0);
  elli1->SetLineColor(kBlue);
  elli1->SetLineWidth(2);
  TLine* l1 = new TLine(DE_MIN,mbc_min,DE_MAX,mbc_min);
  l1->SetLineColor(kRed);
  l1->SetLineStyle(1);
  l1->SetLineWidth(2);
  TLine* l2 = new TLine(DE_MIN,mbc_max,DE_MAX,mbc_max);
  l2->SetLineColor(kRed);
  l2->SetLineStyle(1);
  l2->SetLineWidth(2);
  TLine* l3 = new TLine(DE_MIN,mbc_min,DE_MIN,mbc_max);
  l3->SetLineColor(kRed);
  l3->SetLineStyle(1);
  l3->SetLineWidth(2);
  TLine* l4 = new TLine(DE_MAX,mbc_min,DE_MAX,mbc_max);
  l4->SetLineColor(kRed);
  l4->SetLineStyle(1);
  l4->SetLineWidth(2);

  TCanvas* ellican = new TCanvas("ellican","ellican",400,400);
  ellican->cd();
  out.str("");
  out << "bdtg>" << BDTG_MIN << " && bdtg<" << BDTG_MAX << " && de>-0.15 && de<0.20 && mbc>5.265";
  tree->Draw("mbc:de",out.str().c_str());
  elli->Draw(); elli1->Draw(); l1->Draw(); l2->Draw(); l3->Draw(); l4->Draw();
  
  if(!data){
    TCanvas* sigcan = new TCanvas("sigcan","sigcan",400,400);
    sigcan->cd();
    out.str("");
    out << "bdtg>" << BDTG_MIN << " && bdtg<" << BDTG_MAX << " && de>-0.15 && de<0.20 && mbc>5.265 && (b0f == 1 || b0f == 5 || b0f == 10)";
    tree->Draw("mbc:de",out.str().c_str());
    elli->Draw(); elli1->Draw(); l1->Draw(); l2->Draw(); l3->Draw(); l4->Draw();
  
    TCanvas* backcan = new TCanvas("backcan","backcan",400,400);
    backcan->cd();
    out.str("");
    out << "bdtg>" << BDTG_MIN << " && bdtg<" << BDTG_MAX << " && de>-0.15 && de<0.20 && mbc>5.265 && !(b0f == 1 || b0f == 5 || b0f == 10 || b0f == 0)";
    tree->Draw("mbc:de",out.str().c_str());
    elli->Draw(); elli1->Draw(); l1->Draw(); l2->Draw(); l3->Draw(); l4->Draw();
    
    cout << "Rectangle:" << endl;
    out.str("");
    out << "de<" << DE_MAX << " && de>" << DE_MIN;
    out << " && mbc>" << mbc_min << " && mbc<" << mbc_max;
    Roo1DTable* recttable = ds.table(b0f,out.str().c_str());
    recttable->Print();
    recttable->Print("v");

    cout << "Ellips:" << endl;
    out.str("");
    out << "(de-" << de_center.getVal() << ")/" << de_radius.getVal() << "*(de-" << de_center.getVal() << ")/" << de_radius.getVal() << "+(mbc-"<<mbc_center.getVal()<<")/" << mbc_radius.getVal() << "*(mbc-" << mbc_center.getVal() << ")/" << mbc_radius.getVal() << "<1";
    cout << out.str() << endl;
    Roo1DTable* ellitable = ds.table(b0f,out.str().c_str());
    ellitable->Print();
    ellitable->Print("v");
     
    cout << "Elli:" << endl;
    out.str("");
    out << "(de-" << de_center.getVal() << ")/" << de_radius1.getVal() << "*(de-" << de_center.getVal() << ")/" << de_radius1.getVal() << "+(mbc-"<<mbc_center.getVal()<<")/" << mbc_radius1.getVal() << "*(mbc-" << mbc_center.getVal() << ")/" << mbc_radius1.getVal() << "<1";
    cout << out.str() << endl;
    Roo1DTable* ellitable1 = ds.table(b0f,out.str().c_str());
    ellitable1->Print();
    ellitable1->Print("v");

    Roo1DTable* fulltable = ds.table(b0f);
    fulltable->Print();
    fulltable->Print("v");
  }

  cout << "Rectangle:" << endl;
  cout << "Nsig    = " << nsig <<" +- " << nsig_err << " +- " << nsig_err_npq << " (" << nsig_err_total << ")" << endl;
  cout << "NBBcomb = " << nrho <<" +- " << nrho_err << " +- " << nrho_err_npq << " (" << nrho_err_total << ")" << endl;
  cout << "Ncmb    = " << ncmb <<" +- " << ncmb_err << " +- " << ncmb_err_npq << " (" << ncmb_err_total << ")" << endl;
  cout << "Pury    = " << purity << " +- " << purity_err << endl;

  cout << "Ellips:" << endl;
  cout << "Nsig    = " << nsigEl <<" +- " << nsig_errEl << " +- " << nsig_errEl_npq << " (" << nsig_errEl_total << ")" << endl;
  cout << "NBBcomb = " << nrhoEl <<" +- " << nrho_errEl << " +- " << nrho_errEl_npq << " (" << nrho_errEl_total << ")" << endl;
  cout << "Ncmb    = " << ncmbEl <<" +- " << ncmb_errEl << " +- " << ncmb_errEl_npq << " (" << ncmb_errEl_total << ")" << endl;
  cout << "Pury    = " << purityEl << " +- " << purity_errEl << endl;
  
  cout << "Elli:" << endl;
  cout << "Nsig    = " << nsigEl1 <<" +- " << nsig_errEl1 << " +- " << nsig_errEl1_npq << " (" << nsig_errEl1_total << ")" << endl;
  cout << "NBBcomb = " << nrhoEl1 <<" +- " << nrho_errEl1 << " +- " << nrho_errEl1_npq << " (" << nrho_errEl1_total << ")" << endl;
  cout << "Ncmb    = " << ncmbEl1 <<" +- " << ncmb_errEl1 << " +- " << ncmb_errEl1_npq << " (" << ncmb_errEl1_total << ")" << endl;
  cout << "Pury    = " << purityEl1 << " +- " << purity_errEl1 << endl;

  // de full //
  RooPlot* deFrameF = de.frame();
  ds.plotOn(deFrameF,DataError(RooAbsData::SumW2),MarkerSize(1),MarkerColor(kGreen));
  pdf.plotOn(deFrameF,Components(pdf_sig),LineStyle(kDashed));
  pdf.plotOn(deFrameF,Components(RooArgSet(pdf_comb,pdf_bb_comb)),LineStyle(kDashed));
  pdf.plotOn(deFrameF,LineWidth(2));
  
  RooHist* hdepullF = deFrameF->pullHist();
  RooPlot* dePullF = de.frame(Title("#Delta E pull"));
  dePullF->addPlotable(hdepullF,"P");
  dePullF->GetYaxis()->SetRangeUser(-5,5);

  TCanvas* cmf = new TCanvas("#Delta E full","#Delta E full",600,700);
  cmf->cd();

  TPad *pad5 = new TPad("pad5","pad5",0.01,0.20,0.99,0.99);
  TPad *pad6 = new TPad("pad6","pad6",0.01,0.01,0.99,0.20);
  pad5->Draw();
  pad6->Draw();

  pad5->cd();
  pad5->SetLeftMargin(0.15);
  pad5->SetFillColor(0);

  deFrameF->GetXaxis()->SetTitleSize(0.05);
  deFrameF->GetXaxis()->SetTitleOffset(0.85);
  deFrameF->GetXaxis()->SetLabelSize(0.04);
  deFrameF->GetYaxis()->SetTitleOffset(1.6);
  deFrameF->Draw();

  TPaveText *ptf = new TPaveText(0.6,0.75,0.98,0.9,"brNDC");
  ptf->SetFillColor(0);
  ptf->SetTextAlign(12);
  out1.str("");
  out1 << "#chi^{2}/n.d.f = " << deFrameF->chiSquare();
  ptf->AddText(out1.str().c_str());
//  out1.str("");
//  if(!data) out1 << "S: " << (int)(nsig+0.5) << " #pm " << (int)(nsig_err_total+0.5);
//  else      out1 << "S: " << (int)(nsigEl+0.5) << " #pm " << (int)(nsig_errEl_total+0.5);
//  ptf->AddText(out1.str().c_str());
//  out1.str("");
//  if(!data) out1 << "Purity: " << std::fixed << std::setprecision(2) << purity*100. << " #pm " << purity_err*100;
//  else out1 << "Purity: " << std::fixed << std::setprecision(2) << purityEl*100. << " #pm " << purity_errEl*100;
//  ptf->AddText(out1.str().c_str());
  ptf->Draw();

//  TLine *de_line_RIGHT;
//  if(!data) de_line_RIGHT = new TLine(DE_MAX,0,DE_MAX,120);
//  else      de_line_RIGHT = new TLine(DE_MAX,0,DE_MAX,30);
//  de_line_RIGHT->SetLineColor(kRed);
//  de_line_RIGHT->SetLineStyle(1);
//  de_line_RIGHT->SetLineWidth((Width_t)2.);
//  de_line_RIGHT->Draw();
  TLine *de_line_LEFT;
//  if(!data) de_line_LEFT = new TLine(DE_MIN,0,DE_MIN,120);
//  else      de_line_LEFT = new TLine(DE_MIN,0,DE_MIN,30);
//  de_line_LEFT->SetLineColor(kRed);
//  de_line_LEFT->SetLineStyle(1);
//  de_line_LEFT->SetLineWidth((Width_t)2.);
  de_line_LEFT->Draw();

  pad6->cd(); pad6->SetLeftMargin(0.15); pad6->SetFillColor(0);
  dePullF->SetMarkerSize(0.05); dePullF->Draw();
//  TLine *de_lineUP = new TLine(deMin,3,deMax,3);
//  de_lineUP->SetLineColor(kBlue);
//  de_lineUP->SetLineStyle(2);
  de_lineUP->Draw();
//  TLine *de_line = new TLine(deMin,0,deMax,0);
//  de_line->SetLineColor(kBlue);
//  de_line->SetLineStyle(1);
//  de_line->SetLineWidth((Width_t)2.);
  de_line->Draw();
//  TLine *de_lineDOWN = new TLine(deMin,-3,deMax,-3);
//  de_lineDOWN->SetLineColor(kBlue);
//  de_lineDOWN->SetLineStyle(2);
  de_lineDOWN->Draw();

  cmf->Update();

}