Exemplo n.º 1
0
//____________________________________
void DoSPlot(RooWorkspace* ws){
  std::cout << "Calculate sWeights" << std::endl;

  // get what we need out of the workspace to do the fit
  RooAbsPdf* model = ws->pdf("model");
  RooRealVar* zYield = ws->var("zYield");
  RooRealVar* qcdYield = ws->var("qcdYield");
  RooDataSet* data = (RooDataSet*) ws->data("data");

  // fit the model to the data.
  model->fitTo(*data, Extended() );

  // The sPlot technique requires that we fix the parameters
  // of the model that are not yields after doing the fit.
  RooRealVar* sigmaZ = ws->var("sigmaZ");
  RooRealVar* qcdMassDecayConst = ws->var("qcdMassDecayConst");  
  sigmaZ->setConstant();
  qcdMassDecayConst->setConstant();


  RooMsgService::instance().setSilentMode(true);


  // Now we use the SPlot class to add SWeights to our data set
  // based on our model and our yield variables
  RooStats::SPlot* sData = new RooStats::SPlot("sData","An SPlot",
		            *data, model, RooArgList(*zYield,*qcdYield) );


  // Check that our weights have the desired properties

  std::cout << "Check SWeights:" << std::endl;


  std::cout << std::endl <<  "Yield of Z is " 
	    << zYield->getVal() << ".  From sWeights it is "
	    << sData->GetYieldFromSWeight("zYield") << std::endl;


  std::cout << "Yield of QCD is " 
	    << qcdYield->getVal() << ".  From sWeights it is "
	    << sData->GetYieldFromSWeight("qcdYield") << std::endl
	    << std::endl;

  for(Int_t i=0; i < 10; i++)
    {
      std::cout << "z Weight   " << sData->GetSWeight(i,"zYield") 
		<< "   qcd Weight   " << sData->GetSWeight(i,"qcdYield") 
		<< "  Total Weight   " << sData->GetSumOfEventSWeight(i) 
		<< std::endl;
    }

  std::cout << std::endl;

  // import this new dataset with sWeights
 std::cout << "import new dataset with sWeights" << std::endl;
 ws->import(*data, Rename("dataWithSWeights"));


}
Exemplo n.º 2
0
int main() {
    const double PDGLbMass=5619.51;
    const double RareUpperBlind=PDGLbMass+125.0;
    const double RareLowerBlind=PDGLbMass-125.0;
    const double Rare2UpperBlind=PDGLbMass+175.0;
    const double Rare2LowerBlind=PDGLbMass-175.0;
    bool blind=true;
    bool remakecc=false;
    bool remakerare=false;
    bool remakerare2=false;
    bool sweight=false;
    TRandom3* MultiCandRand=new TRandom3(224); //dont change seed
    TH1I* ControlChannelMultiCands= new TH1I("ControlChannelMultiCands","ControlChannelMultiCands",10,0,10);
    TH1I* RareChannelMultiCands= new TH1I("RareChannelMultiCands","RareChannelMultiCands",10,0,10);
    TH1I* Rare2ChannelMultiCands= new TH1I("RareChannel2MultiCands","RareChannel2MultiCands",10,0,10);

    //_______________________________________ MAKE CONTROL CHANNEL ROODATASET______________________________________
    if(remakecc) {
        std::string ControlDataPath=std::getenv("BUKETAPDATABDTRESPROOT");
        DataFile CC12A(ControlDataPath,Data,Twel,MagAll,buketap,"BDTApplied_SampleA");
        DataFile CC12B(ControlDataPath,Data,Twel,MagAll,buketap,"BDTApplied_SampleB");
        DataFile CC11A(ControlDataPath,Data,Elev,MagAll,buketap,"BDTApplied_SampleA");
        DataFile CC11B(ControlDataPath,Data,Elev,MagAll,buketap,"BDTApplied_SampleB");

        TreeReader* CC12Reader= new TreeReader("DecayTree");
        CC12Reader->AddFile(CC12A);
        CC12Reader->AddFile(CC12B);
        CC12Reader->Initialize(ControlCuts::BranchesToKeep,"contains");
        TFile* Minimal12File= new TFile("MinimalCC12.root","RECREATE");
        TTree* Minimal12Tree=CC12Reader->CopyTree(ControlCuts::AllCuts12,-1,"DecayTree");
        TTree* MinimalFlagged12Tree=HandyFunctions::GetSingleTree(MultiCandRand,Minimal12Tree,ControlChannelMultiCands,NULL);
        MinimalFlagged12Tree->Write();
        Minimal12File->Close();

        TreeReader* CC11Reader= new TreeReader("DecayTree");
        CC11Reader->AddFile(CC11A);
        CC11Reader->AddFile(CC11B);
        CC11Reader->Initialize(ControlCuts::BranchesToKeep,"contains");
        TFile* Minimal11File= new TFile("MinimalCC11.root","RECREATE");
        TTree* Minimal11Tree=CC11Reader->CopyTree(ControlCuts::AllCuts11,-1,"DecayTree");
        TTree* MinimalFlagged11Tree=HandyFunctions::GetSingleTree(MultiCandRand,Minimal11Tree,ControlChannelMultiCands,NULL);
        MinimalFlagged11Tree->Write();
        Minimal11File->Close();

        TreeReader* FlaggedReader= new TreeReader("DecayTree");
        FlaggedReader->AddFile("MinimalCC12.root");
        FlaggedReader->AddFile("MinimalCC11.root");
        FlaggedReader->Initialize();
        TFile* CCSingleFile= new TFile("CCSingleFile.root","RECREATE");
        TTree* SingleTree=FlaggedReader->CopyTree("isSingle==1",-1,"DecayTree");
        SingleTree->Write();
        CCSingleFile->Close();
    }

    //_____________________________________Make Rare Mode One Data__________________________________________________
    if(remakerare) {
        std::string RareDataPath=std::getenv("LBPKETAPPIPIGDATABDTRESPROOT");
        DataFile Rare12A(RareDataPath,Data,Twel,MagAll,lbpketappipig,"BDTApplied_SampleA_NB");
        DataFile Rare12B(RareDataPath,Data,Twel,MagAll,lbpketappipig,"BDTApplied_SampleB_NB");
        DataFile Rare11A(RareDataPath,Data,Elev,MagAll,lbpketappipig,"BDTApplied_SampleA_NB");
        DataFile Rare11B(RareDataPath,Data,Elev,MagAll,lbpketappipig,"BDTApplied_SampleB_NB");

        TreeReader* Rare12Reader= new TreeReader("DecayTree");
        Rare12Reader->AddFile(Rare12A);
        Rare12Reader->AddFile(Rare12B);
        Rare12Reader->Initialize(pipigCuts::BranchesToKeep,"contains");
        TFile* Minimal12File= new TFile("MinimalRare12.root","RECREATE");
        TTree* Minimal12Tree=Rare12Reader->CopyTree(pipigCuts::AllCuts12,-1,"DecayTree");
        TTree* MinimalFlagged12Tree=HandyFunctions::GetSingleTree(MultiCandRand,Minimal12Tree,RareChannelMultiCands,NULL);
        MinimalFlagged12Tree->Write();
        Minimal12File->Close();

        TreeReader* Rare11Reader= new TreeReader("DecayTree");
        Rare11Reader->AddFile(Rare11A);
        Rare11Reader->AddFile(Rare11B);
        Rare11Reader->Initialize(pipigCuts::BranchesToKeep,"contains");
        TFile* Minimal11File= new TFile("MinimalRare11.root","RECREATE");
        TTree* Minimal11Tree=Rare11Reader->CopyTree(pipigCuts::AllCuts11,-1,"DecayTree");
        TTree* MinimalFlagged11Tree=HandyFunctions::GetSingleTree(MultiCandRand,Minimal11Tree,RareChannelMultiCands,NULL);
        MinimalFlagged11Tree->Write();
        Minimal11File->Close();

        TreeReader* FlaggedReader= new TreeReader("DecayTree");
        FlaggedReader->AddFile("MinimalRare12.root");
        FlaggedReader->AddFile("MinimalRare11.root");
        FlaggedReader->Initialize();
        TFile* RareSingleFile= new TFile("RareSingleFile.root","RECREATE");
        TTree* SingleTree=FlaggedReader->CopyTree("isSingle==1",-1,"DecayTree");
        SingleTree->Write();
        RareSingleFile->Close();

    }
    //___________________________________ Make Rare mode Two Data________________________________________________
    if(remakerare2) {
        std::string Rare2DataPath=std::getenv("LBPKETAPETADATABDTRESPROOT");
        DataFile Rare212A(Rare2DataPath,Data,Twel,MagAll,lbpketapeta,"BDTApplied_SampleA_NB");
        DataFile Rare212B(Rare2DataPath,Data,Twel,MagAll,lbpketapeta,"BDTApplied_SampleB_NB");
        DataFile Rare211A(Rare2DataPath,Data,Elev,MagAll,lbpketapeta,"BDTApplied_SampleA_NB");
        DataFile Rare211B(Rare2DataPath,Data,Elev,MagAll,lbpketapeta,"BDTApplied_SampleB_NB");

        TreeReader* Rare212Reader= new TreeReader("DecayTree");
        Rare212Reader->AddFile(Rare212A);
        Rare212Reader->AddFile(Rare212B);
        Rare212Reader->Initialize(etapetaCuts::BranchesToKeep,"contains");
        TFile* Minimal12File= new TFile("MinimalRare212.root","RECREATE");
        TTree* Minimal12Tree=Rare212Reader->CopyTree(etapetaCuts::AllCuts12,-1,"DecayTree");
        TTree* MinimalFlagged12Tree=HandyFunctions::GetSingleTree(MultiCandRand,Minimal12Tree,Rare2ChannelMultiCands,NULL);
        MinimalFlagged12Tree->Write();
        Minimal12File->Close();

        TreeReader* Rare211Reader= new TreeReader("DecayTree");
        Rare211Reader->AddFile(Rare211A);
        Rare211Reader->AddFile(Rare211B);
        Rare211Reader->Initialize(etapetaCuts::BranchesToKeep,"contains");
        TFile* Minimal11File= new TFile("MinimalRare211.root","RECREATE");
        TTree* Minimal11Tree=Rare211Reader->CopyTree(etapetaCuts::AllCuts11,-1,"DecayTree");
        TTree* MinimalFlagged11Tree=HandyFunctions::GetSingleTree(MultiCandRand,Minimal11Tree,Rare2ChannelMultiCands,NULL);
        MinimalFlagged11Tree->Write();
        Minimal11File->Close();

        TreeReader* FlaggedReader= new TreeReader("DecayTree");
        FlaggedReader->AddFile("MinimalRare212.root");
        FlaggedReader->AddFile("MinimalRare211.root");
        FlaggedReader->Initialize();
        TFile* Rare2SingleFile= new TFile("Rare2SingleFile.root","RECREATE");
        TTree* SingleTree=FlaggedReader->CopyTree("isSingle==1",-1,"DecayTree");
        SingleTree->Write();
        Rare2SingleFile->Close();
    }
    //____________________________________ Shared Parameters_______________________________________________________

    RooRealVar DataMCRatio("DataMCRatio","DataMCRatio",1.0,0.5,1.5);
    RooRealVar CCMean("CCMean","CCMean",5279.0,5250.0,5310.0);
    RooFormulaVar LambdabMean("LambdabMean","LambdabMean","@0+339.72",RooArgList(CCMean));
    //RooRealVar LambdabMean("LambdabMean","LambdabMean",5619.0,5615.0,5630.0);
    // RooFormulaVar CCMean("CCMean","CCMean","@0 - 339.72",RooArgList(LambdabMean));

    std::vector<RooRealVar*> FreeParameters;
    FreeParameters.push_back(&DataMCRatio);
    //  FreeParameters.push_back(&LambdabMean);

    //____________________________________ Control Channel PDF______________________________________________________

    TFile* CCInputFile = new TFile("CCSingleFile.root");
    TTree* CCInputTree=(TTree*)CCInputFile->Get("DecayTree");
    RooRealVar CCBMass("Bu_DTF_MF","Bu_DTF_MF",5000.0,5550.0);
    RooRealVar CCEtaMass("eta_prime_MM","eta_prime_MM",880.0,1040.0);
    RooArgSet CCArgs(CCBMass,CCEtaMass);
    RooDataSet* CCData=new RooDataSet("CCData","CCData",CCArgs,Import(*CCInputTree));


    //++++++++++++++++++++++++++++++Signal PDF+++++++++++++++++++++++++++++++++


    TFile *CCMCFitResultFile = new TFile("CCFitResult.root");
    RooFitResult* CCMCFitResult=NULL;
    try {
        CCMCFitResult=SafeGetFitResult(CCMCFitResultFile,"CCFitResult");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    CCMCFitResult->Print("v");
    RooRealVar* CCMCSigma=NULL;
    RooRealVar* CCMCLAlphaFix=NULL;
    RooRealVar* CCMCRAlphaFix=NULL;
    RooRealVar* CCMCLNFix=NULL;
    RooRealVar* CCMCRNFix=NULL;

    try {
        CCMCSigma=SafeGetVar(CCMCFitResult,"CCSigma");
        CCMCLAlphaFix=SafeGetVar(CCMCFitResult,"CCLAlpha");
        CCMCRAlphaFix=SafeGetVar(CCMCFitResult,"CCRAlpha");
        CCMCLNFix=SafeGetVar(CCMCFitResult,"CCLN");
        CCMCRNFix=SafeGetVar(CCMCFitResult,"CCRN");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    RooRealVar CCMCSigmaFix("CCMCSigmaFix","CCMCSigmaFix",CCMCSigma->getVal());
    CCMCSigmaFix.setConstant(kTRUE);
    RooFormulaVar CCSigma("CCSigma","CCSigma","@0*@1",RooArgList(DataMCRatio,CCMCSigmaFix));

    RooRealVar CCLAlpha("CCLAlpha","CCLAlpha",CCMCLAlphaFix->getVal());

    RooRealVar CCRAlpha("CCRAlpha","CCRAlpha",CCMCRAlphaFix->getVal());

    RooRealVar CCLN("CCLN","CCLN",CCMCLNFix->getVal());

    RooRealVar CCRN("CCRN","CCRN",CCMCRNFix->getVal());

    RooCBShape CCDCBLeft("CCDCBLeft","CCDCBLeft",CCBMass,CCMean,CCSigma,CCLAlpha,CCLN);

    RooCBShape CCDCBRight("CCDCBRight","CCDCBRight",CCBMass,CCMean,CCSigma,CCRAlpha,CCRN);

    RooRealVar CCFitFraction("CCFitFraction","CCFitFraction",0.5,0.0,1.0);
    FreeParameters.push_back(&CCFitFraction);
    RooAddPdf CCDCB("CCDCB","CCDCB",RooArgList(CCDCBRight,CCDCBLeft),CCFitFraction);

    RooRealVar CCEtamean("CCEtamean","CCEtamean",958.0,955.0,960.0);
    RooRealVar CCEtasigma("CCEtasigma","CCEtasigma",9.16,9.0,15.0);

    RooRealVar* CCMCEtaLAlpha=NULL;
    RooRealVar* CCMCEtaRAlpha=NULL;
    RooRealVar* CCMCEtaLN=NULL;
    RooRealVar* CCMCEtaRN=NULL;

    try {
        CCMCEtaLAlpha=SafeGetVar(CCMCFitResult,"CCEtaLAlpha");
        CCMCEtaRAlpha=SafeGetVar(CCMCFitResult,"CCEtaRAlpha");
        CCMCEtaLN=SafeGetVar(CCMCFitResult,"CCEtaLN");
        CCMCEtaRN=SafeGetVar(CCMCFitResult,"CCEtaRN");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    RooRealVar CCEtaLAlpha("CCEtaLAlpha","CCEtaLAlpha",CCMCEtaLAlpha->getVal());

    RooRealVar CCEtaRAlpha("CCEtaRAlpha","CCEtaRAlpha",CCMCEtaRAlpha->getVal());

    RooRealVar CCEtaLN("CCEtaLN","CCEtaLN",CCMCEtaLN->getVal());

    RooRealVar CCEtaRN("CCEtaRN","CCEtaRN",CCMCEtaRN->getVal());

    RooCBShape CCEtaDCBLeft("CCEtaCBLeft","CCEtaCBLeft",CCEtaMass,CCEtamean,CCEtasigma,CCEtaLAlpha,CCEtaLN);

    RooCBShape CCEtaDCBRight("CCEtaCBRight","CCEtaCBRight",CCEtaMass,CCEtamean,CCEtasigma,CCEtaRAlpha,CCEtaRN);

    RooRealVar CCEtaFitFraction("CCEtaFitFraction","CCEtaFitFraction",0.22,0.1,1.0);
    FreeParameters.push_back(&CCEtaFitFraction);

    RooAddPdf CCEtaDCB("CCEteaDCB","CCEtaDCB",RooArgList(CCEtaDCBRight,CCEtaDCBLeft),CCEtaFitFraction);

    RooProdPdf CCSignalPdf("CCSignalPdf","CCSignalPdf",RooArgSet(CCEtaDCB,CCDCB));
    RooRealVar CCSignalYield("CCSignalYield","CCSignalYield",10000.0,1000.0,20000.0);

    RooExtendPdf CCExtendedSignalPdf("CCExtendedSignalPdf","CCExtendedSignalPdf",CCSignalPdf,CCSignalYield);

    //+++++++++++++++++++++++++++++++++ COMB NO ETAP PDF++++++++++++++++++++++++++++++++++++++++

    RooRealVar CCNoEtaBSlope("CCNoEtaBSlope","CCNoEtaBSlope",-0.6,-0.8,-0.4);
    FreeParameters.push_back(&CCNoEtaBSlope);

    RooRealVar CCNoEtaBCurve("CCNoEtaBCurve","CCNoEtaBCurve",-0.1,-0.30,-0.05);
    FreeParameters.push_back(&CCNoEtaBCurve);

    RooChebychev CCNoEtaBLine("CCNoEtaBLine","CCNoEtaBLine",CCBMass,RooArgList(CCNoEtaBSlope,CCNoEtaBCurve));

    RooRealVar CCNoEtaEtaP1("CCNoEtaEtaP1","CCNoEtaEtaP1",-0.1,-1.0,0.0);
    FreeParameters.push_back(&CCNoEtaEtaP1);

    RooRealVar CCNoEtaEtaP2("CCNoEtaEtaP2","CCNoEtaEtaP2",-0.1,-0.8,0.0);
    FreeParameters.push_back(&CCNoEtaEtaP2);

    RooChebychev CCNoEtaQuad("CCNoEtaQuad","CCNoEtaQuad",CCEtaMass,RooArgList(CCNoEtaEtaP1,CCNoEtaEtaP2));

    RooProdPdf CCNoEtaBkgPdf("CCNoEtaBkgPdf","CCNoEtaBkgPdf",RooArgSet(CCNoEtaQuad,CCNoEtaBLine));
    RooRealVar CCNoEtaBkgYield("CCNoEtaBkgYield","CCNoEtaBkgYield",2400.0,500.0,15000.0);

    RooExtendPdf ExtCCNoEtaBkgPdf("ExtCCNoEtaBkgPdf","ExtCCNoEtaBkgPdf",CCNoEtaBkgPdf,CCNoEtaBkgYield);


    //++++++++++++++++++++++++++++++++++ COMB TRUE ETAP PDF++++++++++++++++++++++++++++++++++

    RooRealVar CCTrueEtaBSlope("CCTrueEtaBSlope","CCTrueEtaBSlope",-0.1,-1.0,0.0);
    FreeParameters.push_back(&CCTrueEtaBSlope);

    RooChebychev CCTrueEtaBLine("CCTrueEtaBLine","CCTrueEtaBLine",CCBMass,RooArgList(CCTrueEtaBSlope));

    RooRealVar CCTrueEtaEtaLAlpha("CCTrueEtaEtaLAlpha","CCTrueEtaEtaLAlpha",CCMCEtaLAlpha->getVal());
    RooRealVar CCTrueEtaEtaRAlpha("CCTrueEtaEtaRAlpha","CCTrueEtaEtaRAlpha",CCMCEtaRAlpha->getVal());
    RooRealVar CCTrueEtaEtaLN("CCTrueEtaEtaLN","CCTrueEtaEtaLN",CCMCEtaLN->getVal());
    RooRealVar CCTrueEtaEtaRN("CCTrueEtaEtaRN","CCTrueEtaEtaRN",CCMCEtaRN->getVal());

    //RooCBShape CCTrueEtaEtaCBLeft("CCTrueEtaEtaCBLeft","CCTrueEtaEtaCBLeft",CCEtaMass,CCEtamean,CCEtasigma,CCTrueEtaEtaLAlpha,CCTrueEtaEtaLN);

    RooCBShape CCTrueEtaEtaCBRight("CCTrueEtaEtaCBRight","CCTrueEtaEtaCBRight",CCEtaMass,CCEtamean,CCEtasigma,CCTrueEtaEtaRAlpha,CCTrueEtaEtaRN);

    //  RooRealVar CCTrueEtaEtaFitFraction("CCTrueEtaEtaFitFraction","CCTrueEtaEtaFitFraction",0.4,0.0,1.0);
    //RooAddPdf CCTrueEtaEtaDCB("CCTrueEtaEtaDCB","CCTrueEtaEtaDCB",RooArgList(CCTrueEtaEtaCBRight,CCTrueEtaEtaCBLeft),CCTrueEtaEtaFitFraction);

    RooProdPdf CCTrueEtaEtaBkgPdf("CCTrueEtaEtaBkgPdf","CCTrueEtaEtaBkgPdf",RooArgSet(CCTrueEtaEtaCBRight,CCTrueEtaBLine));

    RooRealVar CCTrueEtaBkgYield("CCTrueEtaBkgYield","CCTrueEtaBkgYield",800.0,20.0,10000.0);

    RooExtendPdf ExtCCTrueEtaBkgPdf("ExtCCTrueEtaBkgPdf","ExtCCTrueEtaBkgPdf",CCTrueEtaEtaBkgPdf,CCTrueEtaBkgYield);

    RooAddPdf CCTotalPdf("CCTotalPdf","CCTotalPdf",RooArgList(ExtCCTrueEtaBkgPdf,ExtCCNoEtaBkgPdf,CCExtendedSignalPdf));



    //_________________________________Rare1 Fit_____________________________

    TFile * RareInputFile = new TFile("RareSingleFile.root");
    TTree* RareInputTree=NULL;
    try {
        RareInputTree=SafeGetTree(RareInputFile,"DecayTree");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    RooRealVar LbMass("Lambda_b0_DTF_MF","Lambda_b0_DTF_MF",5200.0,6000.0);
    RooRealVar RareLambda_b0_PE("Lambda_b0_PE","Lambda_b0_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareLambda_b0_PX("Lambda_b0_PX","Lambda_b0_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareLambda_b0_PY("Lambda_b0_PY","Lambda_b0_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareLambda_b0_PZ("Lambda_b0_PZ","Lambda_b0_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareProton_PE("Proton_PE","Proton_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareProton_PX("Proton_PX","Proton_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareProton_PY("Proton_PY","Proton_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareProton_PZ("Proton_PZ","Proton_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareKaon_PE("Kaon_PE","Kaon_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareKaon_PX("Kaon_PX","Kaon_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareKaon_PY("Kaon_PY","Kaon_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar RareKaon_PZ("Kaon_PZ","Kaon_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rareeta_prime_PE("eta_prime_PE","eta_prime_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rareeta_prime_PX("eta_prime_PX","eta_prime_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rareeta_prime_PY("eta_prime_PY","eta_prime_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rareeta_prime_PZ("eta_prime_PZ","eta_prime_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooArgSet RareArgs(LbMass,RareLambda_b0_PE,RareLambda_b0_PX,RareLambda_b0_PY,RareLambda_b0_PZ,RareProton_PE,RareProton_PX,RareProton_PY,RareProton_PZ);
    RareArgs.add(RareKaon_PE);
    RareArgs.add(RareKaon_PX);
    RareArgs.add(RareKaon_PY);
    RareArgs.add(RareKaon_PZ);
    RareArgs.add(Rareeta_prime_PE);
    RareArgs.add(Rareeta_prime_PX);
    RareArgs.add(Rareeta_prime_PY);
    RareArgs.add(Rareeta_prime_PZ);
    RooDataSet* RareData= new RooDataSet("RareData","RareData",RareArgs,Import(*RareInputTree));


    TFile * RareFitResultFile = new TFile("RareFitResult.root");
    RooFitResult* RareMCFitResult=NULL;
    try {
        RareMCFitResult=SafeGetFitResult(RareFitResultFile,"RareFitResult");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    RareMCFitResult->Print("v");
    RooRealVar* RareMCRAlpha=NULL;
    RooRealVar* RareMCFitFraction=NULL;
    RooRealVar* RareMCLAlpha=NULL;
    RooRealVar* RareMCLN=NULL;
    RooRealVar* RareMCRN=NULL;
    RooRealVar* RareMCSigma=NULL;
    try {
        RareMCRAlpha=SafeGetVar(RareMCFitResult,"RareAlpha");
        RareMCFitFraction=SafeGetVar(RareMCFitResult,"RareFitFraction");
        RareMCLAlpha=SafeGetVar(RareMCFitResult,"RareLAlpha");
        RareMCLN=SafeGetVar(RareMCFitResult,"RareLN");
        RareMCRN=SafeGetVar(RareMCFitResult,"RareRN");
        RareMCSigma=SafeGetVar(RareMCFitResult,"RareSigma");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }

    //++++++++++++++++++++++++++++++Rare Signal PDF++++++++++++++++++++++++++++
    RooRealVar RareMCSigmaFix("RareMCSigmaFix","RareMCSigmaFix",RareMCSigma->getVal());
    RareMCSigmaFix.setConstant();

    RooFormulaVar RareSigma("RareSigma","RareSigma","@0*@1",RooArgSet(RareMCSigmaFix,DataMCRatio));
    RooRealVar RareLAlpha("RareLAlpha","RareLAlpha",RareMCLAlpha->getVal());
    RooRealVar RareRAlpha("RareRAlpha","RareRAlpha",RareMCRAlpha->getVal());
    RooRealVar RareRN("RareRN","RareRN",RareMCRN->getVal());
    RooRealVar RareLN("RareLN","RareLN",RareMCLN->getVal());
    RooRealVar RareFitFraction("RareFitFraction","RareFitFraction",RareMCFitFraction->getVal());

    RooCBShape RareDCBLeft("DCBLeft","DCBLeft",LbMass,LambdabMean,RareSigma,RareLAlpha,RareLN);

    RooCBShape RareDCBRight("DCBRight","DCBRight",LbMass,LambdabMean,RareSigma,RareRAlpha,RareRN);

    RooAddPdf RareDCB("RareDCB","RareDCB",RooArgList(RareDCBLeft,RareDCBRight),RareFitFraction);

    RooRealVar RareYield("RareYield","RareYield",40.0,-100.0,1000.0);
    RooExtendPdf RareSignalPdf("RareSignalPdf","RareSignalPdf",RareDCB,RareYield);
    //++++++++++++++++++++++++++++++Rare Combinatorial Bkg+++++++++++++++++++++++++++++++++++
    RooRealVar Rarep1("Rarep1","Rarep1",-1.1,-5.0,-0.1);
    FreeParameters.push_back(&Rarep1);

    RooRealVar Rarep2("Rarep2","Rarep2",0.5,0.001,10.0);
    FreeParameters.push_back(&Rarep2);

    RooChebychev RarePoly("RarePoly","RarePoly",LbMass,RooArgList(Rarep1,Rarep2));

    RooRealVar RareBkgYield("RareBkgYield","RareBkgYield",2000.0,0.0,10000.0);
    RooExtendPdf RareBkgPdf("RareBkgPdf","RareBkgPdf",RarePoly,RareBkgYield);

    //++++++++++++++++++++++++++++Rare PKPhi Bkg+++++++++++++++++++++++++++++++++
    TFile* PKPhiMCFitresultFile= new TFile("PkPhiFitResultFile.root");
    RooFitResult* PKPhiMCFitResult=NULL;
    try {
        PKPhiMCFitResult=SafeGetFitResult(PKPhiMCFitresultFile,"PkPhiFitResult");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    PKPhiMCFitResult->Print("v");
    RooRealVar* PkPhiMean=NULL;
    RooRealVar* PkPhiSigma=NULL;
    RooRealVar* PkPhiLAlpha=NULL;
    RooRealVar* PkPhiRAlpha=NULL;
    RooRealVar* PkPhiLN=NULL;
    RooRealVar* PkPhiRN=NULL;
    try {
        PkPhiMean=SafeGetVar(PKPhiMCFitResult,"PkPhiMean");
        PkPhiSigma=SafeGetVar(PKPhiMCFitResult,"PkPhiSigma");
        PkPhiLAlpha=SafeGetVar(PKPhiMCFitResult,"PkPhiLAlpha");
        PkPhiLN=SafeGetVar(PKPhiMCFitResult,"PkPhiLN");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }

    RooRealVar RarePkPhiMean("RarePkPhiMean","RarePkPhiMean",PkPhiMean->getVal());
    RooRealVar RarePkPhiSigma("RarePkPhiSigma","RarePkPhiSigma",PkPhiSigma->getVal());
    RooRealVar RarePkPhiLN("RarePkPhiLN","RarePkPhiLN",PkPhiLN->getVal());
    RooRealVar RarePkPhiLAlpha("RarePkPhiLAlpha","RarePkPhiLAlpha",PkPhiLAlpha->getVal());

    RooCBShape RarePkPhiModel("RarePkPhiModel","RarePkPhiModel",LbMass,RarePkPhiMean,RarePkPhiSigma,RarePkPhiLAlpha,RarePkPhiLN);

    RooRealVar RarePkPhiYield("RarePkPhiYield","RarePkPhiYield",50.0,1.0,150.0);
    RooExtendPdf RarePkPhiPdf("RarePkPhiPdf","RarePkPhiPdf",RarePkPhiModel,RarePkPhiYield);

    RooAddPdf RarePdf("RarePdf","RarePdf",RooArgList(RarePkPhiPdf,RareBkgPdf,RareSignalPdf));

    /*RarePdf.fitTo(*RareData,Extended(kTRUE));
    RooPlot* RareFrame=LbMass.frame(Bins(35),Range(5200.0,6100.0));
    TCanvas RareCanvas;
    RareData->plotOn(RareFrame);
    RarePdf.plotOn(RareFrame);
    RareFrame->Draw();
    RareCanvas.SaveAs("RareCanvas.pdf");*/

    //________________________________ Fit Rare 2_______________________________
    TFile* Rare2InputFile = new TFile("Rare2SingleFile.root");
    TTree* Rare2InputTree=NULL;
    try {
        Rare2InputTree=SafeGetTree(Rare2InputFile,"DecayTree");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }
    RooRealVar Rare2Lambda_b0_PE("Lambda_b0_PE","Lambda_b0_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Lambda_b0_PX("Lambda_b0_PX","Lambda_b0_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Lambda_b0_PY("Lambda_b0_PY","Lambda_b0_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Lambda_b0_PZ("Lambda_b0_PZ","Lambda_b0_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Proton_PE("Proton_PE","Proton_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Proton_PX("Proton_PX","Proton_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Proton_PY("Proton_PY","Proton_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Proton_PZ("Proton_PZ","Proton_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Kaon_PE("Kaon_PE","Kaon_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Kaon_PX("Kaon_PX","Kaon_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Kaon_PY("Kaon_PY","Kaon_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2Kaon_PZ("Kaon_PZ","Kaon_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2eta_prime_PE("eta_prime_PE","eta_prime_PE",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2eta_prime_PX("eta_prime_PX","eta_prime_PX",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2eta_prime_PY("eta_prime_PY","eta_prime_PY",-RooNumber::infinity(),RooNumber::infinity());
    RooRealVar Rare2eta_prime_PZ("eta_prime_PZ","eta_prime_PZ",-RooNumber::infinity(),RooNumber::infinity());
    RooArgSet Rare2Args(LbMass,Rare2Lambda_b0_PE,Rare2Lambda_b0_PX,Rare2Lambda_b0_PY,Rare2Lambda_b0_PZ,Rare2Proton_PE,Rare2Proton_PX,Rare2Proton_PY,Rare2Proton_PZ);
    Rare2Args.add(Rare2Kaon_PE);
    Rare2Args.add(Rare2Kaon_PX);
    Rare2Args.add(Rare2Kaon_PY);
    Rare2Args.add(Rare2Kaon_PZ);
    Rare2Args.add(Rare2eta_prime_PE);
    Rare2Args.add(Rare2eta_prime_PX);
    Rare2Args.add(Rare2eta_prime_PY);
    Rare2Args.add(Rare2eta_prime_PZ);

    RooDataSet* Rare2Data=new RooDataSet("Rare2Data","Rare2Data",Rare2Args,Import(*Rare2InputTree));

    TFile* Rare2MCFitResultFile= new TFile("RareTwoFitResult.root");
    RooFitResult* Rare2MCFitResult=NULL;
    try {
        Rare2MCFitResult=SafeGetFitResult(Rare2MCFitResultFile,"RareTwoFitResult");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }

    //++++++++++++++++++++++++++++++Signal PDF++++++++++++++++++++++++++++++++
    RooRealVar* Rare2MCFitFraction=NULL;
    RooRealVar* Rare2MCLAlpha=NULL;
    RooRealVar* Rare2MCLN=NULL;
    RooRealVar* Rare2MCRN=NULL;
    RooRealVar* Rare2MCRAlpha=NULL;
    RooRealVar* Rare2MCSigma=NULL;

    try {
        Rare2MCFitFraction=SafeGetVar(Rare2MCFitResult,"Rare2FitFraction");
        Rare2MCLAlpha=SafeGetVar(Rare2MCFitResult,"Rare2LAlpha");
        Rare2MCLN=SafeGetVar(Rare2MCFitResult,"Rare2LN");
        Rare2MCRN=SafeGetVar(Rare2MCFitResult,"Rare2RN");
        Rare2MCRAlpha=SafeGetVar(Rare2MCFitResult,"Rare2RAlpha");
        Rare2MCSigma=SafeGetVar(Rare2MCFitResult,"Rare2Sigma");
    } catch(std::exception &e) {
        std::cout<<e.what()<<std::endl;
        return 1;
    }

    RooRealVar Rare2SigmaFix("Rare2SigmaFix","Rare2SigmaFix",Rare2MCSigma->getVal());
    RooFormulaVar Rare2Sigma("Rare2Sigma","Rare2Sigma","@0*@1",RooArgList(Rare2SigmaFix,DataMCRatio));

    RooRealVar Rare2LN("Rare2LN","Rare2LN",Rare2MCLN->getVal());
    RooRealVar Rare2RN("Rare2RN","Rare2RN",Rare2MCRN->getVal());
    RooRealVar Rare2RAlpha("Rare2RAlpha","Rare2RAlpha",Rare2MCRAlpha->getVal());
    RooRealVar Rare2LAlpha("Rare2LAlpha","Rare2LAlpha",Rare2MCLAlpha->getVal());
    RooRealVar Rare2FitFraction("Rare2FitFraction","Rare2FitFraction",Rare2MCFitFraction->getVal());

    RooCBShape Rare2CBLeft("Rare2CBLeft","Rare2CBLeft",LbMass,LambdabMean,Rare2Sigma,Rare2LAlpha,Rare2LN);

    RooCBShape Rare2CBRight("Rare2CBRight","Rare2CBRight",LbMass,LambdabMean,Rare2Sigma,Rare2RAlpha,Rare2RN);

    RooAddPdf Rare2DCB("Rare2DCB","Rare2DCB",RooArgList(Rare2CBLeft,Rare2CBRight),Rare2FitFraction);

    RooRealVar Rare2SignalYield("Rare2SignalYield","Rare2SignalYield",100.0,-100.0,1000.0);
    RooExtendPdf Rare2SignalPdf("Rare2SignalPdf","Rare2SignalPdf",Rare2DCB,Rare2SignalYield);

    //++++++++++++++++++++++++++++++Rare 2 Bkg pdf++++++++++++++++++++++++++++++
    RooRealVar Rare2K("K","K",-0.003,-0.1,0.0);
    RooExponential Rare2BkgExp("Rare2BkgExp","Rare2BkgExp",LbMass,Rare2K);
    RooRealVar Rare2BkgYield("Rare2BkgYield","Rare2BkgYield",2000.0,0.0,100000.0);
    RooExtendPdf Rare2BkgPdf("Rare2BkgPdf","Rare2BkgPdf",Rare2BkgExp,Rare2BkgYield);

    RooAddPdf Rare2Pdf("Rare2Pdf","Rare2Pdf",RooArgList(Rare2SignalPdf,Rare2BkgPdf));

    //___________________________________ SImultaneous Part ________________________________________________________

    RooCategory Channel("Channel","Channel");
    Channel.defineType("Rare");
    Channel.defineType("Rare2");
    Channel.defineType("Control");

    RooDataSet* AllData = new RooDataSet("AllData","AllData",RooArgSet(LbMass,CCBMass,CCEtaMass),Index(Channel),Import("Rare",*RareData),Import("Rare2",*Rare2Data),Import("Control",*CCData));

    RooSimultaneous SimPdf("SimPdf","SimPdf",Channel);
    SimPdf.addPdf(Rare2Pdf,"Rare2");
    SimPdf.addPdf(RarePdf,"Rare");
    SimPdf.addPdf(CCTotalPdf,"Control");

    RooDataSet* BlindedData=NULL;
    RooFitResult* SimResult=NULL;
    if(blind) {
        LbMass.setRange("RLSB",5200.0,RareLowerBlind);
        LbMass.setRange("RUSB",RareUpperBlind,6000.0);
        LbMass.setRange("R2LSB",5200.0,Rare2LowerBlind);
        LbMass.setRange("R2USB",Rare2UpperBlind,6000.0);
        LbMass.setRange("RDLSB",5200.0,5493.33333);
        LbMass.setRange("RDUSB",RareUpperBlind,6000.0);

        std::string CutString="Lambda_b0_DTF_MF<"+std::to_string(RareLowerBlind)+"||Lambda_b0_DTF_MF>"+std::to_string(Rare2UpperBlind);
        BlindedData=(RooDataSet*)AllData->reduce(CutString.data());
        RooMsgService::instance().setSilentMode(kTRUE);
        RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL) ;
    }

    RooArgSet MinosPars(Rare2SignalYield,RareYield,CCSignalYield);
    SimResult = SimPdf.fitTo(*AllData,Save(kTRUE),Extended(kTRUE),Minos(MinosPars));
    SimPdf.Print("v");
    RooPlot* NominalLikelihoodRareYield=RareYield.frame(Title("Likelihood scan of RareYield"),Range(50,150));
    RooAbsReal* nll = SimPdf.createNLL(*AllData,NumCPU(2),Extended(kTRUE));
    TCanvas C;
    nll->plotOn(NominalLikelihoodRareYield,ShiftToZero());
    NominalLikelihoodRareYield->Draw();
    C.SaveAs("NominalLikelihood.pdf");
    //  HandyFunctions::SetlhcbStyle();
    //_______________________________ Plot B+ Canvas_____________________________________________________________
    RooPlot* CCBFrame= CCBMass.frame(Bins(50),Range(5000.0,5500.0),Title("Control Channel B+ Mass Projection"));
    AllData->plotOn(CCBFrame,Cut("Channel==Channel::Control"));
    SimPdf.plotOn(CCBFrame,Slice(Channel,"Control"),Components("ExtCCTrueEtaBkgPdf"),LineColor(kMagenta),LineStyle(kDashed),ProjWData(Channel,*AllData));
    SimPdf.plotOn(CCBFrame,Slice(Channel,"Control"),Components("ExtCCNoEtaBkgPdf"),LineColor(kGreen),LineStyle(kDashed),ProjWData(Channel,*AllData));
    SimPdf.plotOn(CCBFrame,Slice(Channel,"Control"),Components("CCExtendedSignalPdf"),LineColor(kRed),LineStyle(kDashed),ProjWData(Channel,*AllData));
    SimPdf.plotOn(CCBFrame,Slice(Channel,"Control"),ProjWData(Channel,*AllData));
    TCanvas* CCBCanvas=HandyFunctions::DecoratePlot(CCBFrame);
    /*(TCanvas* CCBCanvas= new TCanvas("CCBCanvas","CCBCanvas",1600,900);
    CCBFrame->Draw();
    CCBCanvas->SaveAs("CCBCanvas.eps");*/

    //________________________________Plot Eta Canvas_____________________________________________________________
    RooPlot* CCEtaFrame=CCEtaMass.frame(Bins(50),Range(880.0,1040.0),Title("Control Channel Eta' Projection"));
    AllData->plotOn(CCEtaFrame,Cut("Channel==Channel::Control"));
    SimPdf.plotOn(CCEtaFrame,Slice(Channel,"Control"),Components("ExtCCTrueEtaBkgPdf"),LineColor(kMagenta),LineStyle(kDashed),ProjWData(Channel,*AllData));
    SimPdf.plotOn(CCEtaFrame,Slice(Channel,"Control"),Components("ExtCCNoEtaBkgPdf"),LineColor(kGreen),LineStyle(kDashed),ProjWData(Channel,*AllData));
    SimPdf.plotOn(CCEtaFrame,Slice(Channel,"Control"),Components("CCExtendedSignalPdf"),LineColor(kRed),LineStyle(kDashed),ProjWData(Channel,*AllData));
    SimPdf.plotOn(CCEtaFrame,Slice(Channel,"Control"),ProjWData(Channel,*AllData));
    TCanvas* CCEtaCanvas=HandyFunctions::DecoratePlot(CCEtaFrame);
    /*TCanvas* CCEtaCanvas=new TCanvas("CCEtaCanvas","CCEtaCanvas",1600,900);
    CCEtaFrame->Draw();
    CCEtaCanvas->Print("CCEtaCanvas.eps");*/


    //________________________________Plot Rare Canvas_____________________________________________________________
    RooPlot* RareFrame=LbMass.frame(Bins(30),Range(5200.0,6000.0),Title("#Lambda_{b} -> p K (#eta' -> #pi #pi #gamma) ;M(#Lambda_{b})"));
    if(blind) {
        double sidebandsN = RareData ->sumEntries("1","RLSB,RUSB");
        double LowsidebandsN = RareData ->sumEntries("1","RLSB");
        RareData->plotOn(RareFrame,CutRange("RDLSB,RDUSB"),Name("RareDataPlotted"));
        RarePdf.plotOn(RareFrame,Range("RLSB,RUSB"),Components("RarePkPhiPdf"),Name("RarePkPhiBkg"),LineColor(kMagenta),LineStyle(kDashed),Normalization(sidebandsN,RooAbsReal::NumEvent));
        RarePdf.plotOn(RareFrame,Range("RLSB,RUSB"),Components("RareBkgPdf"),Name("RareCombBkg"),LineColor(kGreen),LineStyle(kDashed),Normalization(sidebandsN,RooAbsReal::NumEvent));
        RarePdf.plotOn(RareFrame,Range("RLSB,RUSB"),Normalization(sidebandsN,RooAbsReal::NumEvent));
    } else {
        AllData->plotOn(RareFrame,Cut("Channel==Channel::Rare"));
        SimPdf.plotOn(RareFrame,Slice(Channel,"Rare"),Components("RarePkPhiPdf"),LineColor(kMagenta),LineStyle(kDashed),ProjWData(Channel,*AllData));
        SimPdf.plotOn(RareFrame,Slice(Channel,"Rare"),Components("RareBkgPdf"),LineColor(kGreen),LineStyle(kDashed),ProjWData(Channel,*AllData));
        SimPdf.plotOn(RareFrame,Slice(Channel,"Rare"),Components("RareSignalPdf"),LineColor(kRed),LineStyle(kDashed),ProjWData(Channel,*AllData));
        SimPdf.plotOn(RareFrame,Slice(Channel,"Rare"),ProjWData(Channel,*AllData));
    }
    int i=RareFrame->numItems();
    std::cout<<"_________________________________________"<<std::endl;
    for(int j=0; j<i; j++) {
        std::cout<<RareFrame->nameOf(j)<<std::endl;
    }
    TLegend * RareLegend = new TLegend(0.66,0.7,0.9,0.9);
    RareLegend->AddEntry(RareFrame->findObject("RarePkPhiBkg"),"#Lambda_{b} -> p K #phi bkg","l");
    RareLegend->AddEntry(RareFrame->findObject("RareCombBkg"),"Combinatorial Background","l");
    std::cout<<"++++++++++++++++++++++++++++++++++++++++++++"<<std::endl;
    RooHist* Pulls=RareFrame->pullHist(0,"RarePdf_Norm[Lambda_b0_DTF_MF]");
    TCanvas* RareCanvas=HandyFunctions::DecoratePlot(RareFrame,"DecoratedRareCanvas");
    RareCanvas->Print("RareCanvas.pdf");
    TCanvas * PullCanvas=new TCanvas("RarePullCanvas","RarePullCanvas",1200,1000);
    Pulls->Draw("AP");
    PullCanvas->SaveAs("RarePulls.pdf");
    TCanvas *RareNoPullCanvas = new TCanvas("RareFitCanvas","RareFitCanvas",1600,900);
    RareFrame->Draw();
    RareLegend->Draw();
    RareNoPullCanvas->Print("RareNoPullCanvasPrinted.eps");


    //_______________________________Plot Rare2 Canvas_____________________________________________________________
    RooPlot* Rare2Frame=LbMass.frame(Bins(25),Range(5200.0,6000.),Title("#Lambda_{b} -> p K (#eta' -> #pi #pi #eta);M(#Lambda_{b})"));
    if(blind) {
        double Rare2sidebandsN= Rare2Data->sumEntries("1","R2LSB,R2USB");
        Rare2Data->plotOn(Rare2Frame,CutRange("R2LSB,R2USB"));
        Rare2Pdf.plotOn(Rare2Frame,Range("R2LSB,R2USB"),Normalization(Rare2sidebandsN,RooAbsReal::NumEvent));
    } else {
        AllData->plotOn(Rare2Frame,Cut("Channel==Channel::Rare2"));
        SimPdf.plotOn(Rare2Frame,Slice(Channel,"Rare2"),Components("Rare2BkgPdf"),LineColor(kGreen),LineStyle(kDashed),ProjWData(Channel,*AllData));
        SimPdf.plotOn(Rare2Frame,Slice(Channel,"Rare2"),Components("Rare2SignalPdf"),LineColor(kRed),LineStyle(kDashed),ProjWData(Channel,*AllData));
        SimPdf.plotOn(Rare2Frame,Slice(Channel,"Rare2"),ProjWData(Channel,*AllData));
    }
    TCanvas* NoPullRare2= new TCanvas("NoPullRare2","NoPullRare2",1800,1000);
    Rare2Frame->Draw();
    NoPullRare2->SaveAs("PiPiEtaFit.eps");
    TCanvas* Rare2Canvas=HandyFunctions::DecoratePlot(Rare2Frame,"DecoratedRare2Canvas");
    Rare2Canvas->SaveAs("Rare2Canvas.pdf");

    if(blind) {
        RooArgSet NotBlind(CCEtamean,CCSignalYield,DataMCRatio,CCNoEtaBkgYield,CCTrueEtaBkgYield,CCMean,RareBkgYield,Rare2BkgYield,RarePkPhiYield);
        cout << "Fit complete" << endl;
        cout << "covQual:" << SimResult->covQual() << endl;
        cout << "EDM:" << SimResult->edm() << endl;
        cout << "FCN at min:" << SimResult->minNll() << endl;
        NotBlind.Print("s");
    }


    TFile* DataFitResults= new TFile("DataFitResults.root","RECREATE");
    RareNoPullCanvas->Write();
    CCBCanvas->Write();
    CCEtaCanvas->Write();
    RareCanvas->Write();
    Rare2Canvas->Write();
    SimResult->Write("DataFitResult");
    DataFitResults->Close();

    RooWorkspace* Wkspc= new RooWorkspace("w","workspace");
    Wkspc->import(SimPdf);
    //  Wkspc->writeToFile("BigFitWorkspace.root");
    Wkspc->writeToFile("TestBigFitWorkspace.root");

    for(auto Var : FreeParameters) {
        Var->setConstant();
    }

    RooStats::SPlot* sDataMass;
    if(sweight) {
        sDataMass = new RooStats::SPlot("sData","An SPlot",*RareData,&RarePdf,RooArgList(RarePkPhiYield,RareYield,RareBkgYield));
        std::cout<<" Check SWeights: "<<std::endl;
        std::cout<<" BkgYield= "<<RareBkgYield.getVal()<<std::endl;
        std::cout<<" Bkg yield from sweights = "<<sDataMass->GetYieldFromSWeight("RareBkgYield")<<std::endl;
    }




}
Exemplo n.º 3
0
//____________________________________
void DoSPlot(RooWorkspace* ws){
  std::cout << "Calculate sWeights" << std::endl;

  RooAbsPdf* model = ws->pdf("model");
  RooRealVar* nsig = ws->var("nsig");
  RooRealVar* nBbkg = ws->var("nBbkg");
  RooRealVar* nbkg = ws->var("nbkg");
  RooRealVar* nbkg2 = ws->var("nbkg2");
  RooDataSet* data = (RooDataSet*) ws->data("data");

  // fit the model to the data.
  model->fitTo(*data, Extended() );

  RooMsgService::instance().setSilentMode(true);

  // Now we use the SPlot class to add SWeights to our data set
  // based on our model and our yield variables
  RooStats::SPlot* sData = new RooStats::SPlot("sData","An SPlot",
					       *data, model, RooArgList(*nsig,*nBbkg,*nbkg,*nbkg2) );


  // Check that our weights have the desired properties

  std::cout << "Check SWeights:" << std::endl;

  std::cout << std::endl <<  "Yield of sig is " 
	    << nsig->getVal() << ".  From sWeights it is "
	    << sData->GetYieldFromSWeight("nsig") << std::endl;

  std::cout << std::endl <<  "Yield of Bbkg is " 
	    << nBbkg->getVal() << ".  From sWeights it is "
	    << sData->GetYieldFromSWeight("nBbkg") << std::endl;

  std::cout << std::endl <<  "Yield of bkg is " 
	    << nbkg->getVal() << ".  From sWeights it is "
	    << sData->GetYieldFromSWeight("nbkg") << std::endl;

  std::cout << std::endl <<  "Yield of bkg2 is " 
	    << nbkg2->getVal() << ".  From sWeights it is "
	    << sData->GetYieldFromSWeight("nbkg2") << std::endl;

  cout << endl;   cout << endl;   cout << endl;
  float sum20=0;
  float sum50=0;
  float sum100=0;
  float sum200=0;
  float sum300=0;
  float sum600=0;
  float sum900=0;
  float sum1200=0;
  float total=0;

  // saving weights into a file
  ofstream myfile;
  myfile.open ("weights.txt");
  // plot the weight event by event with the Sum of events values as cross-check
  for(Int_t i=0; i < data->numEntries(); i++) {
      //myfile << sData->GetSWeight(i,"nsig") << " " << sData->GetSWeight(i,"nBbkg") << " " << sData->GetSWeight(i,"nbkg") << " " << sData->GetSWeight(i,"nbkg2") << endl;  
      //myfile << sData->GetSWeight(i,"nsig") <<endl;
    myfile << (unsigned int) data->get(i)->getRealValue("run")
             << " " << (unsigned int) data->get(i)->getRealValue("event")
	   << " " << (float) data->get(i)->getRealValue("FourMu_Mass")
             << " " << sData->GetSWeight(i,"nsig")
             << endl;
     // std::cout << "nsig Weight   " << sData->GetSWeight(i,"nsig") 
     //		<< "   nBbkg Weight   " << sData->GetSWeight(i,"nBbkg")
     //		<< "   nbkg Weight   " << sData->GetSWeight(i,"nbkg")
     //		<< "   nbkg2 Weight  " << sData->GetSWeight(i,"nbkg2")
//		<< "   Total Weight   " << sData->GetSumOfEventSWeight(i) 
//		<< std::endl;
      total+=sData->GetSWeight(i,"nsig");         
      if(i<20) sum20+=sData->GetSWeight(i,"nsig");
      if(i<50) sum50+=sData->GetSWeight(i,"nsig");
      if(i<100) sum100+=sData->GetSWeight(i,"nsig");
      if(i<200) sum200+=sData->GetSWeight(i,"nsig");
      if(i<300) sum300+=sData->GetSWeight(i,"nsig");
      if(i<600) sum600+=sData->GetSWeight(i,"nsig");
      if(i<900) sum900+=sData->GetSWeight(i,"nsig");
      if(i<1200) sum1200+=sData->GetSWeight(i,"nsig");

    }
  myfile.close();

  std::cout << std::endl;

  std::cout<<"Sum of the sWeights is: "<<total<<std::endl;
  std::cout<<"Sum of the first 20 sWeights is: "<<sum20<<std::endl;
  std::cout<<"Sum of the first 50 sWeights is: "<<sum50<<std::endl;
  std::cout<<"Sum of the first 100 sWeights is: "<<sum100<<std::endl;
  std::cout<<"Sum of the first 200 sWeights is: "<<sum200<<std::endl;
  std::cout<<"Sum of the first 300 sWeights is: "<<sum300<<std::endl;
  std::cout<<"Sum of the first 600 sWeights is: "<<sum600<<std::endl;
  std::cout<<"Sum of the first 900 sWeights is: "<<sum900<<std::endl;
  std::cout<<"Sum of the first 1200 sWeights is: "<<sum1200<<std::endl;
  std::cout<<"Total # of events: "<<data->numEntries()<<std::endl;

  // import this new dataset with sWeights
  std::cout << "import new dataset with sWeights" << std::endl;
  ws->import(*data, Rename("dataWithSWeights"));

}
Exemplo n.º 4
0
int main(){

  /*Toy Model with following True parameters
    Guess 20 events in Lb->pketa' (1)
    and 15 events in Lb->pketa'->pipieta (2)
    Lb Efficiencies=0.0002637
    B+ Efficiency=0.000411 (3)
    B(eta'->pi pi g)=0.291
    B(eta'->pi pi eta)=0.16
    fd/fL= 2.5
   */
  
  int GenRare=40;
  int GenRare2=30;
  int GenControl=11000;
  
  RooRealVar Mode2EfficiencyRatio("Mode2EfficiencyRatio","Mode2EfficiencyRatio",1.558589);
  RooRealVar ModeEfficiencyRatio("ModeEfficiencyRatio","ModeEfficiencyRatio",1.558589);
  RooRealVar SubBRRatio("SubBRRatio","SubBRRatio",0.645);
  RooRealVar fdFl("fdFl","fdFl",2.5);
  RooRealVar YieldRatio("YieldRatio","YieldRatio",0.1,0.000001,1.0);
  RooRealVar YieldRatio2("YieldRatio2","YieldRatio2",0.1,0.000001,1.0);


  //RooFormulaVar ObservableBFRatio("ObservableBFRatio","ObservableBFRatio","((@0*@1)+(@2*@3))*@4*@5",RooArgSet(YieldRatio,ModeEfficiencyRatio,YieldRatio2,Mode2EfficiencyRatio,SubBRRatio,fdFl));
  RooRealVar ObservableBFRatio("ObservableBFRatio","ObservableBFRatio",0.5,0.00000001,10.0);
  
  //  RooRealVar ControlYield("ControlYield","ControlYield",10000.0,0.0,20000.0);
  //RooRealVar RareYield("RareYield","RareYield",30.0,0.0,1100.0);

  
  //RooFormulaVar RareArg("RareArg","RareArg","@1*@2",RooArgSet(ControlYield,YieldRatio));
  RooRealVar ControlMean("ControlMean","ControlMean",5279.0,5200.0,5350.0);
  RooRealVar SigmaCorrection("SigmaCorrection","SigmaCorrection",1.1,0.9,1.8);
  RooRealVar LambdaMass("LambdaMass","LambdaMass",5000,6200.0);
  //  RooRealVar RareMean("RareMean","RareMean",5619.0,5600.0,5650.0);
  RooFormulaVar RareMean("RareMean","RareMean","@0+339.72",RooArgSet(ControlMean));
  RooRealVar MCRareSigma("MCRareSigma","MCRareSigma",25.0);
  //  RooRealVar RareSigma("RareSigma","RareSigma",29.0,20.0,40.0);
  RooFormulaVar RareSigma("RareSigma","RareSigma","@0*@1",RooArgSet(MCRareSigma,SigmaCorrection));
  RooGaussian RareMode("RareMode","RareMode",LambdaMass,RareMean,RareSigma);

  RooRealVar K("K","K",-0.003,-0.010,-0.001);
  RooExponential RareBkg("RareBkg","RareBkg",LambdaMass,K);
  
  RooRealVar RareBkgYield("RareBkgYield","RareBkgYield",1000.0,0.0,2000.0);
  RooExtendPdf ExtRareBkg("ExtRareBkg","ExtRareBkg",RareBkg,RareBkgYield);
  
  //  RooFormulaVar RareYield("RareYield","RareYield","@0*@1",RooArgSet(ControlYield,YieldRatio));
  RooRealVar RareYield("RareYield","RareYield",1.0,0.0,600.0);
  RooExtendPdf ExtRareSig("ExtRareSig","ExtRareSig",RareMode,RareYield);

  RooAddPdf ExtRare("ExtRare","ExtRare",RooArgSet(ExtRareSig,ExtRareBkg));

  RooDataSet* RareSigData=RareMode.generate(RooArgSet(LambdaMass),GenRare);
  RooDataSet* RareBkgData=RareBkg.generate(RooArgSet(LambdaMass),500);
  RooDataSet* RareData= new RooDataSet(*RareSigData,"RareData");
  RareData->append(*RareBkgData);
  
  RooRealVar MCRare2Sigma("MCRare2Sigma","MCRare2Sigma",17.0);
  //RooRealVar Rare2Sigma("Rare2Sigma","Rare2Sigma",20.0,10.0,30.0);
  RooFormulaVar Rare2Sigma("Rare2Sigma","Rare2Sigma","@0*@1",RooArgSet(MCRare2Sigma,SigmaCorrection));
  RooGaussian RareMode2("RareMode2","RareMode2",LambdaMass,RareMean,Rare2Sigma);
  RooDataSet* RareData2=RareMode2.generate(RooArgSet(LambdaMass),GenRare2);
  
  
  //RooFormulaVar RareYield2("RareYield2","RareYield2","@0*@1",RooArgSet(ControlYield,YieldRatio2));
  //RooFormulaVar RareYield2("RareYield2","RareYield2","(ControlYield*(1/Mode2EfficiencyRatio))*((ObservableBFRatio/(SubBRRatio*fdFl))-(YieldRatio*ModeEfficiencyRatio))",RooArgSet(ControlYield,Mode2EfficiencyRatio,ObservableBFRatio,SubBRRatio,fdFl,YieldRatio,ModeEfficiencyRatio));
  RooRealVar RareYield2("RareYield2","RareYield2",1.0,0.0,100.0);
  RooExtendPdf ExtRare2("ExtRare2","ExtRare2",RareMode2,RareYield2);
  
  RooRealVar BMass("BMass","BMass",5000.0,5500.0);					
  
  //  RooFormulaVar ControlMean("ControlMean","ControlMean","@0-339.72",RooArgSet(RareMean));
  RooRealVar MCControlSigma("MCControlSigma","MCControlSigma",17.0);
  RooFormulaVar ControlSigma("ControlSigma","ControlSigma","@0*@1",RooArgSet(MCControlSigma,SigmaCorrection));
  //RooRealVar ControlSigma("ControlSigma","ControlSigma",20.0,10.0,40.0);
  RooGaussian ControlMode("ControlMode","ControlMode",BMass,ControlMean,ControlSigma);
  RooFormulaVar ControlYield("ControlYield","ControlYield","(1/ObservableBFRatio)*((ModeEfficiencyRatio*RareYield)+(Mode2EfficiencyRatio*RareYield2))*SubBRRatio*fdFl",RooArgSet(ObservableBFRatio,ModeEfficiencyRatio,RareYield,Mode2EfficiencyRatio,RareYield2,SubBRRatio,fdFl));
  RooExtendPdf ExtControl("ExtControl","ExtControl",ControlMode,ControlYield);

    
  RooDataSet* ControlData=ControlMode.generate(RooArgSet(BMass),GenControl);
  
  RooCategory Mode("Mode","Mode");
  Mode.defineType("Rare");
  Mode.defineType("Rare2");
  Mode.defineType("Control");

  RooDataSet CombData("CombData","CombData",RooArgSet(BMass,LambdaMass),Index(Mode),Import("Rare2",*RareData2),Import("Rare",*RareData),Import("Control",*ControlData));

  RooSimultaneous SimPdf("SimPdf","SimPdf",Mode);
  SimPdf.addPdf(ExtRare,"Rare");
  SimPdf.addPdf(ExtRare2,"Rare2");
  SimPdf.addPdf(ExtControl,"Control");

  RooFitResult* SimResult=SimPdf.fitTo(CombData,Save(kTRUE),Minos(kTRUE));
  /*  double FreeYield=-1*SimResult->minNll();
  std::cout<<"With free yield = "<<SimResult->minNll()<<std::endl;
  RareYield.setVal(0);
  RareYield.setConstant(kTRUE);
  RooFitResult* Rare1Fixed=SimPdf.fitTo(CombData,Save(kTRUE),Minos(kTRUE));
  double NullYield=-1*Rare1Fixed->minNll();
  std::cout<<"With not free yield = "<<Rare1Fixed->minNll()<<std::endl;

  double DeltaLogLikelihood=NullYield-FreeYield;
  std::cout<<"DeltaNll= "<<DeltaLogLikelihood<<std::endl;
  double Significance=TMath::Sqrt(-2*DeltaLogLikelihood); 
  std::cout<<"Significance= "<<Significance<<std::endl;
  SimPdf.fitTo(CombData,Save(kTRUE),Minos(kTRUE));*/

  
  RooPlot* BFrame= BMass.frame(Bins(50),Title("B Mass"),Range(5200.0,5400.0));
  ControlData->plotOn(BFrame);
  ControlMode.plotOn(BFrame);

  RooPlot* LambdaFrame = LambdaMass.frame(Bins(50),Title("Lambda mass"),Range(5200.0,6100.0));
  RareData->plotOn(LambdaFrame);
  ExtRare.plotOn(LambdaFrame);

  RooPlot* LambdaFrame2 = LambdaMass.frame(Bins(50),Title("Lambda mass"),Range(5200.0,6100.0));
  RareData2->plotOn(LambdaFrame2);
  RareMode2.plotOn(LambdaFrame2);
  
  TCanvas BCanvas;
  BFrame->Draw();
  BCanvas.SaveAs("BCanvas.pdf");

  TCanvas LambdaCanvas;
  LambdaFrame->Draw();
  LambdaCanvas.SaveAs("LambdaCanvas.pdf");

  TCanvas LambdaCanvas2;
  LambdaFrame2->Draw();
  LambdaCanvas2.SaveAs("LambdaCanvas2.pdf");
  

  SimResult->Print("v");

  // S imPdf.graphVizTree("model.dot");
  std::cout<<"Real Ratio = "<<GenRare/(double)GenControl<<std::endl;
  ObservableBFRatio.Print("v");
  

  std::cout<<"Lb BF = "<<ObservableBFRatio.getVal()*7.06E-5<<" + "<<ObservableBFRatio.getErrorHi()*7.06E-5<<" - "<<ObservableBFRatio.getErrorLo()*7.06E-5<<std::endl;
  

  //________________________________________________ATTEMPT TO SWEIGHT____________________________________________
  
  RooStats::SPlot* sDataMass = new RooStats::SPlot("sData","An SPlot",*RareData,&ExtRare,RooArgList(RareYield,RareBkgYield));
  std::cout << std::endl <<  "Yield of signal is " << RareYield.getVal() << ".  From sWeights it is " << sDataMass->GetYieldFromSWeight("RareYield") << std::endl;
  std::cout << "Yield of background is " << RareBkgYield.getVal() << ".  From sWeights it is " << sDataMass->GetYieldFromSWeight("RareBkgYield") << std::endl << std::endl;

  RooAbsReal* nll = SimPdf.createNLL(CombData);
  RooMinuit(*nll).migrad();
  
  RooPlot* LLFrame=ObservableBFRatio.frame(Title("Some Title"),Range(0.005,0.03));
  nll->plotOn(LLFrame,ShiftToZero());
  LLFrame->GetYaxis()->SetRangeUser(0.0,1000.0);

  TCanvas LLCanvas;
  LLFrame->Draw();
  LLCanvas.SaveAs("LLCanvas.pdf");
}
Exemplo n.º 5
0
//____________________________________
RooStats::SPlot*  DoSPlot(RooWorkspace* ws){
  //Users will have to edit this functioon to account for changes made for their own model
  //This will mainly involve matching the variable names to those defined in AddModel and AddData

  std::cout << "Calculate sWeights" << std::endl;

  // get what we need out of the workspace to do the fit
  RooAbsPdf* model = ws->pdf(pdfName);
  RooDataSet* data = (RooDataSet*) ws->data(dataSetName);

  // fit the model to the data.
  model->fitTo(*data, Extended() );

  // The sPlot technique requires that we fix the parameters
  // of the model that are not yields after doing the fit.
  ws->var(TString("SigMean"))->setConstant();
  ws->var(TString("SigWidth"))->setConstant();  
  ws->var(TString("l0"))->setConstant();
  ws->var(TString("l1"))->setConstant();
  ws->var(TString("l2"))->setConstant();
  //The only 2 free parameters will now be the signal and background yields
  //Note, if we have more than 2 types of event we would need to get additional yeilds here  
  RooRealVar* s_Yield = ws->var(TString("SigYield"));
  RooRealVar* b_Yield = ws->var(TString("BckYield"));


  RooMsgService::instance().setSilentMode(true);

  // Now we use the SPlot class to add SWeights to our data set
  // based on our model and our yield variables
  RooStats::SPlot* sData = new RooStats::SPlot(pdfName+"SW","An SPlot",
		            *data, model, RooArgList(*s_Yield,*b_Yield) );

  //Check that our weights have the desired properties

  std::cout << "Check SWeights:" << std::endl;


  std::cout << std::endl <<  "Yield of Signal is " 
  	    << s_Yield->getVal() << ".  From sWeights it is "
  	    << sData->GetYieldFromSWeight(TString("SigYield")) << std::endl;


  std::cout << "Yield of Background is " 
  	    << b_Yield->getVal() << ".  From sWeights it is "
  	    << sData->GetYieldFromSWeight(TString("BckYield")) << std::endl
  	    << std::endl;

  for(Int_t i=0; i < 10; i++)
    {
      std::cout << "signal Weight   " << sData->GetSWeight(i,TString("SigYield")) 
  		<< " background Weight   " << sData->GetSWeight(i,TString("BckYield")) 
  		<< "  Total Weight   " << sData->GetSumOfEventSWeight(i) 
  		<< std::endl;
    }

  std::cout << std::endl;

  //Now plot the fit resluts for checking 
  //Users will have to edit this for the variables they are using
  RooRealVar* vML = ws->var("Mmiss");
  RooAbsPdf* b_Model = ws->pdf("BackPDF"); 
  RooPlot* frame = vML->frame() ; 
  data->plotOn(frame, DataError(RooAbsData::SumW2) ) ; //plot the data
  model->plotOn(frame,LineStyle(kDashed), LineColor(kRed)) ; //model = signal + back fit result   
  model->plotOn(frame,Components(*b_Model),LineStyle(kDashed),LineColor(kGreen)) ; //just the back fit result  
  model->paramOn(frame,
		 FillColor(kRed),
		 Label("Global Fit parameters:"),
		 Layout(0.1, 0.4, 0.9),
		 Format("NEU",AutoPrecision(3)),
		 ShowConstants());
  frame->SetTitle("M#Lambda distribution fit");
  frame->Draw() ;
  canvas->Modified();
  canvas->Update();
  cin.get();//wait here until users ready to continue
  //Write fit plot canvas to output file
  TDirectory* savedir=gDirectory;
  outPlots->cd();
  canvas->SetName(pdfName);
  canvas->Write();
  savedir->cd();

  //return the finished RooStats::sPlot object 
  return sData;

}