void ZeeGammaMassFitSystematicStudy(string workspaceFile, const Int_t seed = 1234, 
                                    Int_t Option = 0, Int_t NToys = 1) {


  //--------------------------------------------------------------------------------------------------------------
  // Settings 
  //==============================================================================================================    
  TRandom3 *randomnumber = new TRandom3(seed);
//   RooRealVar m("m","mass",60,130);

  RooCategory sample("sample","");
  sample.defineType("Pass",1);
  sample.defineType("Fail",2);

  //--------------------------------------------------------------------------------------------------------------
  //Load Workspace
  //==============================================================================================================    
  TFile *f = new TFile (workspaceFile.c_str(), "READ");
  RooWorkspace *w = (RooWorkspace*)f->Get("MassFitWorkspace");

  //--------------------------------------------------------------------------------------------------------------
  //Setup output tree
  //==============================================================================================================    
  TFile *outputfile = new TFile (Form("EffToyResults_Option%d_Seed%d.root",Option, seed), "RECREATE");
  float varEff = 0;
  float varEffErrL = 0;
  float varEffErrH = 0;
  TTree *outTree = new TTree("eff","eff");
  outTree->Branch("eff",&varEff, "eff/F");
  outTree->Branch("efferrl",&varEffErrL, "efferrl/F");
  outTree->Branch("efferrh",&varEffErrH, "efferrh/F");
  
  //--------------------------------------------------------------------------------------------------------------
  //Load Model
  //==============================================================================================================    
  RooSimultaneous *totalPdf = (RooSimultaneous*)w->pdf("totalPdf");
  RooRealVar *m_default = (RooRealVar*)w->var("m");
  m_default->setRange("signalRange",85, 95);
  
  //get default models
  RooAddPdf *modelPass_default = (RooAddPdf*)w->pdf("modelPass");
  RooAddPdf *modelFail_default = (RooAddPdf*)w->pdf("modelFail");

  //get variables
  RooRealVar *Nsig = (RooRealVar*)w->var("Nsig");
  RooRealVar *eff = (RooRealVar*)w->var("eff");
  RooRealVar *NbkgFail = (RooRealVar*)w->var("NbkgFail");

  RooFormulaVar NsigPass("NsigPass","eff*Nsig",RooArgList(*eff,*Nsig));	 
  RooFormulaVar NsigFail("NsigFail","(1.0-eff)*Nsig",RooArgList(*eff,*Nsig));

  //get number of expected events
  Double_t npass = 100;
  Double_t nfail = 169;

  //*************************************************************************************
  //make alternative model
  //*************************************************************************************
  RooRealVar *tFail_default = (RooRealVar*)w->var("tFail");
  RooRealVar *fracFail_default = (RooRealVar*)w->var("fracFail");
 

  RooRealVar *meanFail_default = (RooRealVar*)w->var("meanFail");
  RooRealVar *sigmaFail_default = (RooRealVar*)w->var("sigmaFail");
  RooHistPdf *bkgFailTemplate_default = (RooHistPdf*)w->pdf("bkgHistPdfFail");
  RooFFTConvPdf *sigFail_default = (RooFFTConvPdf*)w->pdf("signalFail");
  RooFFTConvPdf *bkgFail_default = (RooFFTConvPdf*)w->pdf("bkgConvPdfFail");
  RooExtendPdf *esignalFail_default = (RooExtendPdf *)w->pdf("esignalFail");
  RooExtendPdf *ebackgroundFail_default = (RooExtendPdf *)w->pdf("ebackgroundFail");
  RooExponential *bkgexpFail_default = (RooExponential*)w->pdf("bkgexpFail");
  RooAddPdf *backgroundFail_default = (RooAddPdf*)w->pdf("backgroundFail");
  RooGaussian *bkggausFail_default = (RooGaussian*)w->pdf("bkggausFail");

  //shifted mean
  RooRealVar *meanFail_shifted = new RooRealVar("meanFail_shifted","meanFail_shifted", 0, -5, 5);
  meanFail_shifted->setVal(meanFail_default->getVal());
  if (Option == 1) meanFail_shifted->setVal(meanFail_default->getVal()-1.0);
  else if (Option == 2) meanFail_shifted->setVal(meanFail_default->getVal()+1.0);  
  else if (Option == 11) meanFail_shifted->setVal(meanFail_default->getVal()-2.0);
  else if (Option == 12) meanFail_shifted->setVal(meanFail_default->getVal()+2.0);  

  RooRealVar *sigmaFail_shifted = new RooRealVar("sigmaFail_shifted","sigmaFail_shifted", 0, -5, 5);
  sigmaFail_shifted->setVal(sigmaFail_default->getVal());
  if (Option == 3) sigmaFail_shifted->setVal(sigmaFail_default->getVal()*1.2);
  else if (Option == 4) sigmaFail_shifted->setVal(sigmaFail_default->getVal()*0.8);

  CMCBkgTemplateConvGaussianPlusExp *bkgFailModel = new CMCBkgTemplateConvGaussianPlusExp(*m_default,bkgFailTemplate_default,false,meanFail_shifted,sigmaFail_shifted, "shifted");
  bkgFailModel->t->setVal(tFail_default->getVal());
  bkgFailModel->frac->setVal(fracFail_default->getVal());

  cout << "mean : " << meanFail_default->getVal() << " - " << meanFail_shifted->getVal() << endl;
  cout << "sigma : " << sigmaFail_default->getVal() << " - " << sigmaFail_shifted->getVal() << endl;
  cout << "t: " << tFail_default->getVal() << " - " << bkgFailModel->t->getVal() << endl;
  cout << "frac: " << fracFail_default->getVal() << " - " << bkgFailModel->frac->getVal() << endl;
  
  cout << "eff: " << eff->getVal() << " : " << NsigPass.getVal() << " / " << (NsigPass.getVal() + NsigFail.getVal()) << endl;
  cout << "NbkgFail: " << NbkgFail->getVal() << endl;


  //make alternative fail model
  RooAddPdf *modelFail=0;
  RooExtendPdf *esignalFail=0, *ebackgroundFail=0;
  ebackgroundFail = new RooExtendPdf("ebackgroundFail_shifted","ebackgroundFail_shifted",*(bkgFailModel->model),*NbkgFail,"signalRange");
  modelFail       = new RooAddPdf("modelFail","Model for FAIL sample", RooArgList(*esignalFail_default,*ebackgroundFail));



  cout << "*************************************\n";
  ebackgroundFail->Print();
  cout << "*************************************\n";
  ebackgroundFail_default->Print();
  cout << "*************************************\n";
  modelFail->Print();
  cout << "*************************************\n";
  modelFail_default->Print();
  cout << "*************************************\n";

  TCanvas *cv = new TCanvas("cv","cv",800,600);

  RooPlot *mframeFail_default = m_default->frame(Bins(Int_t(130-60)/2));
  modelFail_default->plotOn(mframeFail_default);
  modelFail_default->plotOn(mframeFail_default,Components("ebackgroundFail"),LineStyle(kDashed),LineColor(kRed));
  modelFail_default->plotOn(mframeFail_default,Components("bkgexpFail"),LineStyle(kDashed),LineColor(kGreen+2));
  mframeFail_default->GetYaxis()->SetTitle("");
  mframeFail_default->GetYaxis()->SetTitleOffset(1.2);
  mframeFail_default->GetXaxis()->SetTitle("m_{ee#gamma} [GeV/c^{2}]");
  mframeFail_default->GetXaxis()->SetTitleOffset(1.05);
  mframeFail_default->SetTitle("");
  mframeFail_default->Draw();

  cv->SaveAs("DefaultModel.gif");

  RooPlot *mframeFail = m_default->frame(Bins(Int_t(130-60)/2));
  modelFail->plotOn(mframeFail);
  modelFail->plotOn(mframeFail,Components("ebackgroundFail_shifted"),LineStyle(kDashed),LineColor(kRed));
  modelFail->plotOn(mframeFail,Components("bkgexpFail_shifted"),LineStyle(kDashed),LineColor(kGreen+2));
  mframeFail->GetYaxis()->SetTitle("");
  mframeFail->GetYaxis()->SetTitleOffset(1.2);
  mframeFail->GetXaxis()->SetTitle("m_{ee#gamma} [GeV/c^{2}]");
  mframeFail->GetXaxis()->SetTitleOffset(1.05);
  mframeFail->SetTitle("");
  mframeFail->Draw();
  cv->SaveAs(Form("ShiftedModel_%d.gif",Option));


  //*************************************************************************************
  //Do Toys
  //*************************************************************************************
  for(uint t=0; t < NToys; ++t) {

    RooDataSet *pseudoData_pass    = modelPass_default->generate(*m_default, randomnumber->Poisson(npass));
    RooDataSet *pseudoData_fail  = 0;
    pseudoData_fail    = modelFail->generate(*m_default, randomnumber->Poisson(nfail));
    RooDataSet *pseudoDataCombined = new RooDataSet("pseudoDataCombined","pseudoDataCombined",RooArgList(*m_default),
                                                    RooFit::Index(sample),
                                                    RooFit::Import("Pass",*pseudoData_pass),
                                                    RooFit::Import("Fail",*pseudoData_fail));

    pseudoDataCombined->write(Form("toy%d.txt",t));

    RooFitResult *fitResult=0;
    fitResult = totalPdf->fitTo(*pseudoDataCombined,
                                RooFit::Extended(),
                                RooFit::Strategy(2),
                                //RooFit::Minos(RooArgSet(eff)),
                                RooFit::Save());

    cout << "\n\n";
    cout << "Eff Fit: " << eff->getVal() << " -" << fabs(eff->getErrorLo()) << " +" << eff->getErrorHi() << endl;

    //Fill Tree
    varEff = eff->getVal();
    varEffErrL = fabs(eff->getErrorLo());
    varEffErrH = eff->getErrorHi();
    outTree->Fill();


//   //*************************************************************************************
//   //Plot Toys
//   //*************************************************************************************
//   TCanvas *cv = new TCanvas("cv","cv",800,600);
//   char pname[50];
//   char binlabelx[100];
//   char binlabely[100];
//   char yield[50];
//   char effstr[100];
//   char nsigstr[100];
//   char nbkgstr[100];
//   char chi2str[100];

//   //
//   // Plot passing probes
//   //

//   RooPlot *mframeFail_default = m.frame(Bins(Int_t(130-60)/2));
//   modelFail_default->plotOn(mframeFail_default);
//   modelFail_default->plotOn(mframeFail_default,Components("ebackgroundFail"),LineStyle(kDashed),LineColor(kRed));
//   modelFail_default->plotOn(mframeFail_default,Components("bkgexpFail"),LineStyle(kDashed),LineColor(kGreen+2));
//   mframeFail_default->Draw();
//   cv->SaveAs("DefaultModel.gif");





//   RooPlot *mframeFail = m.frame(Bins(Int_t(130-60)/2));
//   modelFail->plotOn(mframeFail);
//   modelFail->plotOn(mframeFail,Components("ebackgroundFail_shifted"),LineStyle(kDashed),LineColor(kRed));
//   modelFail->plotOn(mframeFail,Components("bkgexpFail_shifted"),LineStyle(kDashed),LineColor(kGreen+2));

//   sprintf(yield,"%u Events",(Int_t)passTree->GetEntries());
//   sprintf(nsigstr,"N_{sig} = %.1f #pm %.1f",NsigPass.getVal(),NsigPass.getPropagatedError(*fitResult));
//     plotPass.AddTextBox(yield,0.21,0.76,0.51,0.80,0,kBlack,-1);    
//   plotPass.AddTextBox(effstr,0.70,0.85,0.94,0.90,0,kBlack,-1);
//     plotPass.AddTextBox(0.70,0.73,0.94,0.83,0,kBlack,-1,1,nsigstr);//,chi2str);

//   mframeFail->Draw();
//   cv->SaveAs(Form("ShiftedModel_%d.gif",Option));


 
//   //
//   // Plot failing probes
//   //
//   sprintf(pname,"fail%s_%i",name.Data(),ibin);
//   sprintf(yield,"%u Events",(Int_t)failTree->GetEntries());
//   sprintf(nsigstr,"N_{sig} = %.1f #pm %.1f",NsigFail.getVal(),NsigFail.getPropagatedError(*fitResult));
//   sprintf(nbkgstr,"N_{bkg} = %.1f #pm %.1f",NbkgFail.getVal(),NbkgFail.getPropagatedError(*fitResult));
//   sprintf(chi2str,"#chi^{2}/DOF = %.3f",mframePass->chiSquare(nflfail));
//   CPlot plotFail(pname,mframeFail,"Failing probes","tag-probe mass [GeV/c^{2}]","Events / 2.0 GeV/c^{2}");
//   plotFail.AddTextBox(binlabelx,0.21,0.85,0.51,0.90,0,kBlack,-1);
//   if((name.CompareTo("etapt")==0) || (name.CompareTo("etaphi")==0)) {
//     plotFail.AddTextBox(binlabely,0.21,0.80,0.51,0.85,0,kBlack,-1);    
//     plotFail.AddTextBox(yield,0.21,0.76,0.51,0.80,0,kBlack,-1);    
//   } else {
//     plotFail.AddTextBox(yield,0.21,0.81,0.51,0.85,0,kBlack,-1);
//   }
//   plotFail.AddTextBox(effstr,0.70,0.85,0.94,0.90,0,kBlack,-1);  
//   plotFail.AddTextBox(0.70,0.68,0.94,0.83,0,kBlack,-1,2,nsigstr,nbkgstr);//,chi2str);
//   plotFail.Draw(cfail,kTRUE,format);  




  } //for loop over all toys
  



  //*************************************************************************************
  //Save To File
  //*************************************************************************************
  outputfile->WriteTObject(outTree, outTree->GetName(), "WriteDelete");

}
Ejemplo n.º 2
0
Archivo: gen.C Proyecto: dcraik/lhcb
void gen(Int_t iToy, Int_t qSqBin){
    TString namestr("toy/toy_"); namestr+=iToy; namestr+="_"; namestr+=qSqBin; namestr+=".dat";

    Double_t bkgMin(5400), bkgMax(5970);

    TString bkgStr="Bplus_M > "; bkgStr+=bkgMin; bkgStr+=" && Bplus_M < "; bkgStr+=bkgMax;

    TFile * file(0);
    TTree * DecayTree(0);
    RooRealVar * cosTheta(0);
    RooRealVar * Bplus_M(0);
    RooDataSet * data(0);
   
    TString fname("~/lhcb/rd/Kll/tuples/fromPatrick/Kmm_Q"); fname+=qSqBin; fname+="_reduced.root";
    file = TFile::Open(fname);
    DecayTree = dynamic_cast<TTree*>(file->Get("finalTree_KMuMu"));
    cosTheta = new RooRealVar("cosThetaL", "cosThetaL", -1., 1.);
    Bplus_M  = new RooRealVar("Bplus_M", "Bplus_M", 5170., 5970.);
    data = new RooDataSet("data", "", DecayTree, RooArgList(*cosTheta,*Bplus_M));

    RooDataSet* data2 = dynamic_cast<RooDataSet*>(data->reduce(bkgStr));
    RooKeysPdf bkg("bkg","bkg",*cosTheta,*data2);

    Double_t g0, g1, g2, g3, g4, g5, g6, va, vb, ignore;
    Int_t nsig, nbkg;

    std::ifstream fin;

    //load efficiency parameters
    fin.open("../efficiencies/effParams_PIDonly_fixB_50.txt");
    Int_t row(0);
    fin >> row;
    while(row!=qSqBin) {
	    fin.ignore(256,'\n');
	    fin >> row;
    }
    fin >> ignore >> ignore >> va >> ignore >> vb >> ignore;
    std::cout << va << "\t" << vb << std::endl;

    fin.close();

    //load moments parameters
    fin.open("../MoM/moments.dat");
    fin >> row;
    while(row!=qSqBin) {
	    fin.ignore(256,'\n');
	    fin >> row;
    }
    fin >> g0 >> g1 >> g2 >> g3 >> g4 >> g5 >> g6;
    std::cout << g0 << "\t" << g1 << "\t" << g2 << "\t" << g3 << "\t" << g4 << "\t" << g5 << "\t" << g6 << std::endl;

    fin.close();

    //load sig/bkg yields
    TString yieldsname("../massFit/yields/"); yieldsname+= qSqBin; yieldsname+= ".dat";
    fin.open(yieldsname);
    fin >> nsig >> ignore >> nbkg;
    std::cout << nsig << "\t" << nbkg << std::endl;

    fin.close();

    RooRealVar G000("G000", "G000",  g0);
    RooRealVar G001("G001", "G001",  g1);
    RooRealVar G002("G002", "G002",  g2);
    RooRealVar G003("G003", "G003",  g3);
    RooRealVar G004("G004", "G004",  g4);
    RooRealVar G005("G005", "G005",  g5);
    RooRealVar G006("G006", "G006",  g6);
    RooRealVar    a("a",    "a",     va);
    RooRealVar    b("b",    "b",     vb);
    RooRealVar    n("n",    "n",    1.0);

    RooB2Kll sig("sig", "sig", *cosTheta, G000, G001, G002, G003, G004, G005, G006, a, b, n);

    TString vetoHistName("efficiencyHist_");
    vetoHistName+=qSqBin;
    TFile * fileK1 = TFile::Open("../efficiencies/Dveto_200.root");
    TH1D  * histK1  = dynamic_cast<TH1D*>(fileK1->Get(vetoHistName));
    RooDataHist * dataK1 = new RooDataHist("dataK1", "", RooArgSet(*cosTheta), histK1);
    RooHistPdf DVeto("DVeto","", RooArgSet(*cosTheta), *dataK1, 2);

    TFile * fileK2 = TFile::Open("../efficiencies/Psiveto_200.root");
    TH1D  * histK2  = dynamic_cast<TH1D*>(fileK2->Get(vetoHistName));
    RooDataHist * dataK2 = new RooDataHist("dataK2", "", RooArgSet(*cosTheta), histK2);
    RooHistPdf PsiVeto("PsiVeto","", RooArgSet(*cosTheta), *dataK2, 2);

    RooProdPdf sigeff("sigeff","sigeff", RooArgList(sig,DVeto,PsiVeto));

//    RooAddPdf pdf("pdf","pdf",RooArgList(sig,bkg), RooArgList(nsig,nbkg));
//    RooAddPdf pdfeff("pdfeff","pdfeff",RooArgList(sigeff,bkg), RooArgList(nsig,nbkg));

    RooRandom::randomGenerator()->SetSeed(iToy);

    RooDataSet * genData  = sigeff.generate(RooArgSet(*cosTheta), nsig);
//    RooDataSet * genData2 = bkg.generate(RooArgSet(*cosTheta), nsig);
//    genData->append(*genData2);
    genData->write(namestr);

    file->Close();
    fileK1->Close();
    fileK2->Close();

    delete genData;
//    delete genData2;

//    RooFitResult * fitresult = pdfeff.fitTo(*data1,Save(kTRUE), Minos(kFALSE), NumCPU(4), SumW2Error(kTRUE));
//    
//    cout << i << "\t" << a.getVal() << "\t" << b.getVal() << "\t" << fitresult->minNll() << "\t" << fitresult->status() << "\t" 
//         << G000.getVal() << "\t" << G001.getVal() << "\t" << G002.getVal() << "\t" << G003.getVal() << "\t" << G004.getVal() << endl;
//    
//    RooPlot * frame = cosTheta->frame(50);
//    data1->plotOn(frame);
//    pdfeff.plotOn(frame);
//    pdfeff.plotOn(frame, RooFit::Components("sigeff"),RooFit::LineStyle(kDashed));
//    pdfeff.plotOn(frame, RooFit::Components("bkg"),RooFit::LineStyle(kDashed),RooFit::LineColor(kMagenta));
//   
//    //std::cout << sig.getNorm(RooArgSet(*cosTheta)) << "\t" << nsig.getVal() << std::endl;
//    //std::cout << bkg.getNorm(RooArgSet(*cosTheta)) << "\t" << nbkg.getVal() << std::endl;
//    
//    //std::cout << sig.createIntegral(RooArgSet(*cosTheta),RooFit::NormSet(*cosTheta))->getVal() << "\t" << nsig.getVal() << std::endl;
//    //std::cout << bkg.createIntegral(RooArgSet(*cosTheta),RooFit::NormSet(*cosTheta))->getVal() << "\t" << nbkg.getVal() << std::endl;
//
//    gROOT->ProcessLine(".x ~/lhcb/lhcbStyle.C");
//    TCanvas c("fit","fit", 800, 800);
//    frame->Draw();
//    c.SaveAs("bkgFits/fit"+TString(namestr)+".png");
//    c.SaveAs("bkgFits/fit"+TString(namestr)+".pdf");
}