Ejemplo 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"));


}
Ejemplo n.º 2
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;

}