//____________________________________ 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")); }
//____________________________________ 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")); }
//____________________________________ 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; }