Пример #1
0
double upper_limit_Profile_Likelihood(Model* model,double confidence){
  cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl;
  cout<<"Calculating upper limit with the Profile Likelihood method"<<endl;
  cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl;
  
  
  //      //define paramsOfInterest
     RooArgSet *paramsOfInterest=new RooArgSet("paramsOfInterest");
     paramsOfInterest->addClone(*model->get_POI()); 	//clone because we need s for complete likelihood
     
     //get the calculator
     ProfileLikelihoodCalculator plc(*model->get_data(),*model->get_complete_likelihood(), *paramsOfInterest);
     
     //      //get the confidence interval
     LikelihoodInterval* lrint = (LikelihoodInterval*) plc.GetInterval();
     lrint->SetConfidenceLevel(1-(1-confidence)*2);
     double ul=lrint->UpperLimit(*model->get_POI());
     cout<<confidence<<"% upper limit is "<<ul<<endl;
  //    TCanvas *c1=new TCanvas;
//      LikelihoodIntervalPlot* lrplot1 = new LikelihoodIntervalPlot(lrint);
//      lrplot1->Draw(); 
//      c1->SaveAs("pll_plot6.png");
     
     
     return ul;
}
Пример #2
0
void central_interval_Profile_Likelihood(Model* model,double confidence){
  cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl;
  cout<<"Calculating central confidence interval with the Profile Likelihood method"<<endl;
  cout<<"///////////////////////////////////////////////////////////////////////////////////////////"<<endl;
  
  
  //      //define paramsOfInterest
     RooArgSet *paramsOfInterest=new RooArgSet("paramsOfInterest");
     paramsOfInterest->addClone(*model->get_POI()); 	//clone because we need s for complete likelihood
     
     //get the calculator
     ProfileLikelihoodCalculator plc(*model->get_data(),*model->get_complete_likelihood(), *paramsOfInterest);
     
     //      //get the confidence interval
     LikelihoodInterval* lrint = (LikelihoodInterval*) plc.GetInterval();
     lrint->SetConfidenceLevel(confidence);
     double ll=lrint->LowerLimit(*model->get_POI());
     double ul=lrint->UpperLimit(*model->get_POI());
     cout<<confidence<<"% inter val is "<<ll<<" , "<<ul<<endl;

}
Пример #3
0
void StandardProfileLikelihoodDemo(const char* infile = "",
                                   const char* workspaceName = "combined",
                                   const char* modelConfigName = "ModelConfig",
                                   const char* dataName = "obsData"){

      double confLevel = optPL.confLevel;
      double nScanPoints = optPL.nScanPoints;
      bool plotAsTF1 = optPL.plotAsTF1;
      double poiXMin = optPL.poiMinPlot;
      double poiXMax = optPL.poiMaxPlot;
      bool doHypoTest = optPL.doHypoTest;
      double nullParamValue = optPL.nullValue;

   // -------------------------------------------------------
   // First part is just to access a user-defined file
   // or create the standard example file if it doesn't exist
      const char* filename = "";
      if (!strcmp(infile,"")) {
         filename = "results/example_combined_GaussExample_model.root";
         bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
         // if file does not exists generate with histfactory
         if (!fileExist) {
#ifdef _WIN32
            cout << "HistFactory file cannot be generated on Windows - exit" << endl;
            return;
#endif
            // Normally this would be run on the command line
            cout <<"will run standard hist2workspace example"<<endl;
            gROOT->ProcessLine(".! prepareHistFactory .");
            gROOT->ProcessLine(".! hist2workspace config/example.xml");
            cout <<"\n\n---------------------"<<endl;
            cout <<"Done creating example input"<<endl;
            cout <<"---------------------\n\n"<<endl;
         }

      }
      else
         filename = infile;

      // Try to open the file
      TFile *file = TFile::Open(filename);

      // if input file was specified byt not found, quit
      if(!file ){
         cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
         return;
      }

   // -------------------------------------------------------
   // Tutorial starts here
   // -------------------------------------------------------

   // get the workspace out of the file
   RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
   if(!w){
      cout <<"workspace not found" << endl;
      return;
   }

   // get the modelConfig out of the file
   ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);

   // get the modelConfig out of the file
   RooAbsData* data = w->data(dataName);

   // make sure ingredients are found
   if(!data || !mc){
      w->Print();
      cout << "data or ModelConfig was not found" <<endl;
      return;
   }

   // ---------------------------------------------
   // create and use the ProfileLikelihoodCalculator
   // to find and plot the 95% confidence interval
   // on the parameter of interest as specified
   // in the model config
   ProfileLikelihoodCalculator pl(*data,*mc);
   pl.SetConfidenceLevel(confLevel); // 95% interval
   LikelihoodInterval* interval = pl.GetInterval();

   // print out the interval on the first Parameter of Interest
   RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
   cout << "\n>>>> RESULT : " << confLevel*100 << "% interval on " <<firstPOI->GetName()<<" is : ["<<
      interval->LowerLimit(*firstPOI) << ", "<<
      interval->UpperLimit(*firstPOI) <<"]\n "<<endl;

   // make a plot

   cout << "making a plot of the profile likelihood function ....(if it is taking a lot of time use less points or the TF1 drawing option)\n";
   LikelihoodIntervalPlot plot(interval);
   plot.SetNPoints(nScanPoints);  // do not use too many points, it could become very slow for some models
   if (poiXMin < poiXMax) plot.SetRange(poiXMin, poiXMax);
   TString opt;
   if (plotAsTF1) opt += TString("tf1");
   plot.Draw(opt);  // use option TF1 if too slow (plot.Draw("tf1")


   // if requested perform also an hypothesis test for the significance
   if (doHypoTest) {
      RooArgSet nullparams("nullparams");
      nullparams.addClone(*firstPOI);
      nullparams.setRealValue(firstPOI->GetName(), nullParamValue);
      pl.SetNullParameters(nullparams);
      std::cout << "Perform Test of Hypothesis : null Hypothesis is " << firstPOI->GetName() << nullParamValue << std::endl;
      auto result = pl.GetHypoTest();
      std::cout << "\n>>>> Hypotheis Test Result \n";
      result->Print();
   }


}
Пример #4
0
//
// calculation of the limit: assumes that wspace is set up and observations
//   contained in data
//
MyLimit computeLimit (RooWorkspace* wspace, RooDataSet* data, StatMethod method, bool draw) {

  // let's time this challenging example
  TStopwatch t;

  //
  // get nominal signal
  //
  RooRealVar exp_sig(*wspace->var("s"));
  double exp_sig_val = exp_sig.getVal();
  std::cout << "exp_sig = " << exp_sig_val << std::endl;
  
  /////////////////////////////////////////////////////
  // Now the statistical tests
  // model config
  std::cout << wspace->pdf("model") << " "
	    << wspace->pdf("prior") << " "
	    << wspace->set("poi") << " "
	    << wspace->set("nuis") << std::endl;
  ModelConfig modelConfig("RA4abcd");
  modelConfig.SetWorkspace(*wspace);
  modelConfig.SetPdf(*wspace->pdf("model"));
  modelConfig.SetPriorPdf(*wspace->pdf("prior"));
  modelConfig.SetParametersOfInterest(*wspace->set("poi"));
  modelConfig.SetNuisanceParameters(*wspace->set("nuis"));


  //////////////////////////////////////////////////
  // If you want to see the covariance matrix uncomment
  // wspace->pdf("model")->fitTo(*data);

  // use ProfileLikelihood
  if ( method == ProfileLikelihoodMethod ) {
    ProfileLikelihoodCalculator plc(*data, modelConfig);
    plc.SetConfidenceLevel(0.95);
    RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
    RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
    LikelihoodInterval* plInt = plc.GetInterval();
    RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
    plInt->LowerLimit( *wspace->var("s") ); // get ugly print out of the way. Fix.
    // RooMsgService::instance().setGlobalKillBelow(RooFit::DEBUG);
    if ( draw ) {
      TCanvas* c = new TCanvas("ProfileLikelihood");
      LikelihoodIntervalPlot* lrplot = new LikelihoodIntervalPlot(plInt);
      lrplot->Draw();
    }
//     RooMsgService::instance().setGlobalKillBelow(msglevel);
    double lowLim = plInt->LowerLimit(*wspace->var("s"));
    double uppLim = plInt->UpperLimit(*wspace->var("s"));
//     double exp_sig_val = wspace->var("s")->getVal();
//     double exp_sig_val = exp_sig.getVal();
    cout << "Profile Likelihood interval on s = [" << 
      lowLim << ", " <<
      uppLim << "]" << " " << exp_sig_val << endl; 
//     MyLimit result(plInt->IsInInterval(exp_sig),
    MyLimit result(exp_sig_val>lowLim&&exp_sig_val<uppLim,lowLim,uppLim);
    // std::cout << "isIn " << result << std::endl;
    delete plInt;
//     delete modelConfig;
    return result;
  }

  // use FeldmaCousins (takes ~20 min)  
  if ( method == FeldmanCousinsMethod ) {
    FeldmanCousins fc(*data, modelConfig);
    fc.SetConfidenceLevel(0.95);
    //number counting: dataset always has 1 entry with N events observed
    fc.FluctuateNumDataEntries(false); 
    fc.UseAdaptiveSampling(true);
    fc.SetNBins(100);
    PointSetInterval* fcInt = NULL;
    fcInt = (PointSetInterval*) fc.GetInterval(); // fix cast
    double lowLim = fcInt->LowerLimit(*wspace->var("s"));
    double uppLim = fcInt->UpperLimit(*wspace->var("s"));
//     double exp_sig_val = wspace->var("s")->getVal();
    cout << "Feldman Cousins interval on s = [" << lowLim << " " << uppLim << endl;
    // std::cout << "isIn " << result << std::endl;
    MyLimit result(exp_sig_val>lowLim&&exp_sig_val<uppLim,
		   fcInt->LowerLimit(*wspace->var("s")),fcInt->UpperLimit(*wspace->var("s")));
    delete fcInt;
    return result;
  }


  // use BayesianCalculator (only 1-d parameter of interest, slow for this problem)  
  if ( method == BayesianMethod ) {
    BayesianCalculator bc(*data, modelConfig);
    bc.SetConfidenceLevel(0.95);
    bc.SetLeftSideTailFraction(0.5);
    SimpleInterval* bInt = NULL;
    if( wspace->set("poi")->getSize() == 1)   {
      bInt = bc.GetInterval();
      if ( draw ) {
	TCanvas* c = new TCanvas("Bayesian");
	// the plot takes a long time and print lots of error
	// using a scan it is better
	bc.SetScanOfPosterior(50);
	RooPlot* bplot = bc.GetPosteriorPlot();
	bplot->Draw();
      }
      cout << "Bayesian interval on s = [" << 
	bInt->LowerLimit( ) << ", " <<
	bInt->UpperLimit( ) << "]" << endl;
      // std::cout << "isIn " << result << std::endl;
      MyLimit result(bInt->IsInInterval(exp_sig),
		     bInt->LowerLimit(),bInt->UpperLimit());
      delete bInt;
      return result;
    } else {
    cout << "Bayesian Calc. only supports on parameter of interest" << endl;
    return MyLimit();
    }
  }


  // use MCMCCalculator  (takes about 1 min)
  // Want an efficient proposal function, so derive it from covariance
  // matrix of fit
  if ( method == MCMCMethod ) {
    RooFitResult* fit = wspace->pdf("model")->fitTo(*data,Save());
    ProposalHelper ph;
    ph.SetVariables((RooArgSet&)fit->floatParsFinal());
    ph.SetCovMatrix(fit->covarianceMatrix());
    ph.SetUpdateProposalParameters(kTRUE); // auto-create mean vars and add mappings
    ph.SetCacheSize(100);
    ProposalFunction* pf = ph.GetProposalFunction();
    
    MCMCCalculator mc(*data, modelConfig);
    mc.SetConfidenceLevel(0.95);
    mc.SetProposalFunction(*pf);
    mc.SetNumBurnInSteps(100); // first N steps to be ignored as burn-in
    mc.SetNumIters(100000);
    mc.SetLeftSideTailFraction(0.5); // make a central interval
    MCMCInterval* mcInt = NULL;
    mcInt = mc.GetInterval();
    MCMCIntervalPlot mcPlot(*mcInt); 
    mcPlot.Draw();
    cout << "MCMC interval on s = [" << 
      mcInt->LowerLimit(*wspace->var("s") ) << ", " <<
      mcInt->UpperLimit(*wspace->var("s") ) << "]" << endl;
    // std::cout << "isIn " << result << std::endl;
    MyLimit result(mcInt->IsInInterval(exp_sig),
		   mcInt->LowerLimit(*wspace->var("s")),mcInt->UpperLimit(*wspace->var("s")));
    delete mcInt;
    return result;
  }
  

  t.Print();

//   delete modelConfig;
  return MyLimit();

}
Пример #5
0
void IntervalExamples()
{

   // Time this macro
   TStopwatch t;
   t.Start();


   // set RooFit random seed for reproducible results
   RooRandom::randomGenerator()->SetSeed(3001);

   // make a simple model via the workspace factory
   RooWorkspace* wspace = new RooWorkspace();
   wspace->factory("Gaussian::normal(x[-10,10],mu[-1,1],sigma[1])");
   wspace->defineSet("poi","mu");
   wspace->defineSet("obs","x");

   // specify components of model for statistical tools
   ModelConfig* modelConfig = new ModelConfig("Example G(x|mu,1)");
   modelConfig->SetWorkspace(*wspace);
   modelConfig->SetPdf( *wspace->pdf("normal") );
   modelConfig->SetParametersOfInterest( *wspace->set("poi") );
   modelConfig->SetObservables( *wspace->set("obs") );

   // create a toy dataset
   RooDataSet* data = wspace->pdf("normal")->generate(*wspace->set("obs"),100);
   data->Print();

   // for convenience later on
   RooRealVar* x = wspace->var("x");
   RooRealVar* mu = wspace->var("mu");

   // set confidence level
   double confidenceLevel = 0.95;

   // example use profile likelihood calculator
   ProfileLikelihoodCalculator plc(*data, *modelConfig);
   plc.SetConfidenceLevel( confidenceLevel);
   LikelihoodInterval* plInt = plc.GetInterval();

   // example use of Feldman-Cousins
   FeldmanCousins fc(*data, *modelConfig);
   fc.SetConfidenceLevel( confidenceLevel);
   fc.SetNBins(100); // number of points to test per parameter
   fc.UseAdaptiveSampling(true); // make it go faster

   // Here, we consider only ensembles with 100 events
   // The PDF could be extended and this could be removed
   fc.FluctuateNumDataEntries(false);

   // Proof
   //  ProofConfig pc(*wspace, 4, "workers=4", kFALSE);    // proof-lite
   //ProofConfig pc(w, 8, "localhost");    // proof cluster at "localhost"
   //  ToyMCSampler* toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler();
   //  toymcsampler->SetProofConfig(&pc);     // enable proof

   PointSetInterval* interval = (PointSetInterval*) fc.GetInterval();


   // example use of BayesianCalculator
   // now we also need to specify a prior in the ModelConfig
   wspace->factory("Uniform::prior(mu)");
   modelConfig->SetPriorPdf(*wspace->pdf("prior"));

   // example usage of BayesianCalculator
   BayesianCalculator bc(*data, *modelConfig);
   bc.SetConfidenceLevel( confidenceLevel);
   SimpleInterval* bcInt = bc.GetInterval();

   // example use of MCMCInterval
   MCMCCalculator mc(*data, *modelConfig);
   mc.SetConfidenceLevel( confidenceLevel);
   // special options
   mc.SetNumBins(200);        // bins used internally for representing posterior
   mc.SetNumBurnInSteps(500); // first N steps to be ignored as burn-in
   mc.SetNumIters(100000);    // how long to run chain
   mc.SetLeftSideTailFraction(0.5); // for central interval
   MCMCInterval* mcInt = mc.GetInterval();

   // for this example we know the expected intervals
   double expectedLL = data->mean(*x)
      + ROOT::Math::normal_quantile(  (1-confidenceLevel)/2,1)
      / sqrt(data->numEntries());
   double expectedUL = data->mean(*x)
      + ROOT::Math::normal_quantile_c((1-confidenceLevel)/2,1)
      / sqrt(data->numEntries()) ;

   // Use the intervals
   std::cout << "expected interval is [" <<
      expectedLL << ", " <<
      expectedUL << "]" << endl;

   cout << "plc interval is [" <<
      plInt->LowerLimit(*mu) << ", " <<
      plInt->UpperLimit(*mu) << "]" << endl;

   std::cout << "fc interval is ["<<
      interval->LowerLimit(*mu) << " , "  <<
      interval->UpperLimit(*mu) << "]" << endl;

   cout << "bc interval is [" <<
      bcInt->LowerLimit() << ", " <<
      bcInt->UpperLimit() << "]" << endl;

   cout << "mc interval is [" <<
      mcInt->LowerLimit(*mu) << ", " <<
      mcInt->UpperLimit(*mu) << "]" << endl;

   mu->setVal(0);
   cout << "is mu=0 in the interval? " <<
      plInt->IsInInterval(RooArgSet(*mu)) << endl;


   // make a reasonable style
   gStyle->SetCanvasColor(0);
   gStyle->SetCanvasBorderMode(0);
   gStyle->SetPadBorderMode(0);
   gStyle->SetPadColor(0);
   gStyle->SetCanvasColor(0);
   gStyle->SetTitleFillColor(0);
   gStyle->SetFillColor(0);
   gStyle->SetFrameFillColor(0);
   gStyle->SetStatColor(0);


   // some plots
   TCanvas* canvas = new TCanvas("canvas");
   canvas->Divide(2,2);

   // plot the data
   canvas->cd(1);
   RooPlot* frame = x->frame();
   data->plotOn(frame);
   data->statOn(frame);
   frame->Draw();

   // plot the profile likelihood
   canvas->cd(2);
   LikelihoodIntervalPlot plot(plInt);
   plot.Draw();

   // plot the MCMC interval
   canvas->cd(3);
   MCMCIntervalPlot* mcPlot = new MCMCIntervalPlot(*mcInt);
   mcPlot->SetLineColor(kGreen);
   mcPlot->SetLineWidth(2);
   mcPlot->Draw();

   canvas->cd(4);
   RooPlot * bcPlot = bc.GetPosteriorPlot();
   bcPlot->Draw();

   canvas->Update();

   t.Stop();
   t.Print();

}
Пример #6
0
void exercise_3() {
  //Open the rootfile and get the workspace from the exercise_0
  TFile fIn("exercise_0.root");
  fIn.cd();
  RooWorkspace *w = (RooWorkspace*)fIn.Get("w");

  //You can set constant parameters that are known
  //If you leave them floating, the fit procedure will determine their uncertainty
  w->var("mean")->setConstant(kFALSE); //don't fix the mean, it's what we want to know the interval for!
  w->var("sigma")->setConstant(kTRUE);
  w->var("tau")->setConstant(kTRUE);
  w->var("Nsig")->setConstant(kTRUE);
  w->var("Nbkg")->setConstant(kTRUE);

  //Set the RooModelConfig and let it know what the content of the workspace is about
  ModelConfig model;
  model.SetWorkspace(*w);
  model.SetPdf("PDFtot");

  //Let the model know what is the parameter of interest
  RooRealVar* mean = w->var("mean");
  mean->setRange(120., 130.);   //this is mostly for plotting reasons
  RooArgSet poi(*mean);

  // set confidence level
  double confidenceLevel = 0.68;

  //Build the profile likelihood calculator
  ProfileLikelihoodCalculator plc; 
  plc.SetData(*(w->data("PDFtotData"))); 
  plc.SetModel(model);
  plc.SetParameters(poi);
  plc.SetConfidenceLevel(confidenceLevel);

  //Get the interval
  LikelihoodInterval* plInt = plc.GetInterval();

  //Now let's do the same for the Bayesian Calculator
  //Now we also need to specify a prior in the ModelConfig
  //To be quicker, we'll use the PDF factory facility of RooWorkspace
  //NB!! For simplicity, we are using a flat prior, but this doesn't mean it's the best choice!
  w->factory("Uniform::prior(mean)");
  model.SetPriorPdf(*w->pdf("prior"));

  //Construct the bayesian calculator
  BayesianCalculator bc(*(w->data("PDFtotData")), model);
  bc.SetConfidenceLevel(confidenceLevel);
  bc.SetParameters(poi);
  SimpleInterval* bcInt = bc.GetInterval();

  // Let's make a plot
  TCanvas dataCanvas("dataCanvas");
  dataCanvas.Divide(2,1);
  dataCanvas.cd(1);

  LikelihoodIntervalPlot plotInt((LikelihoodInterval*)plInt);
  plotInt.SetTitle("Profile Likelihood Ratio and Posterior for mH");
  plotInt.SetMaximum(3.);
  plotInt.Draw();

  dataCanvas.cd(2);
  RooPlot *bcPlot = bc.GetPosteriorPlot();
  bcPlot->Draw();

  dataCanvas.SaveAs("exercise_3.gif");

  //Now print the interval for mH for the two methods
  cout << "PLC interval is [" << plInt->LowerLimit(*mean) << ", " << 
    plInt->UpperLimit(*mean) << "]" << endl;

  cout << "Bayesian interval is [" << bcInt->LowerLimit() << ", " << 
    bcInt->UpperLimit() << "]" << endl;

}
void StandardTestStatDistributionDemo(const char* infile = "",
                                      const char* workspaceName = "combined",
                                      const char* modelConfigName = "ModelConfig",
                                      const char* dataName = "obsData"){


  // the number of toy MC used to generate the distribution
  int nToyMC = 1000;
  // The parameter below is needed for asymptotic distribution to be chi-square,
  // but set to false if your model is not numerically stable if mu<0
  bool allowNegativeMu=true;


  /////////////////////////////////////////////////////////////
  // First part is just to access a user-defined file
  // or create the standard example file if it doesn't exist
  ////////////////////////////////////////////////////////////
   const char* filename = "";
   if (!strcmp(infile,"")) {
      filename = "results/example_combined_GaussExample_model.root";
      bool fileExist = !gSystem->AccessPathName(filename); // note opposite return code
      // if file does not exists generate with histfactory
      if (!fileExist) {
#ifdef _WIN32
         cout << "HistFactory file cannot be generated on Windows - exit" << endl;
         return;
#endif
         // Normally this would be run on the command line
         cout <<"will run standard hist2workspace example"<<endl;
         gROOT->ProcessLine(".! prepareHistFactory .");
         gROOT->ProcessLine(".! hist2workspace config/example.xml");
         cout <<"\n\n---------------------"<<endl;
         cout <<"Done creating example input"<<endl;
         cout <<"---------------------\n\n"<<endl;
      }

   }
   else
      filename = infile;

   // Try to open the file
   TFile *file = TFile::Open(filename);

   // if input file was specified byt not found, quit
   if(!file ){
      cout <<"StandardRooStatsDemoMacro: Input file " << filename << " is not found" << endl;
      return;
   }


  /////////////////////////////////////////////////////////////
  // Now get the data and workspace
  ////////////////////////////////////////////////////////////

  // get the workspace out of the file
  RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
  if(!w){
    cout <<"workspace not found" << endl;
    return;
  }

  // get the modelConfig out of the file
  ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);

  // get the modelConfig out of the file
  RooAbsData* data = w->data(dataName);

  // make sure ingredients are found
  if(!data || !mc){
    w->Print();
    cout << "data or ModelConfig was not found" <<endl;
    return;
  }

  mc->Print();
  /////////////////////////////////////////////////////////////
  // Now find the upper limit based on the asymptotic results
  ////////////////////////////////////////////////////////////
  RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
  ProfileLikelihoodCalculator plc(*data,*mc);
  LikelihoodInterval* interval = plc.GetInterval();
  double plcUpperLimit = interval->UpperLimit(*firstPOI);
  delete interval;
  cout << "\n\n--------------------------------------"<<endl;
  cout <<"Will generate sampling distribution at " << firstPOI->GetName() << " = " << plcUpperLimit <<endl;
  int nPOI = mc->GetParametersOfInterest()->getSize();
  if(nPOI>1){
    cout <<"not sure what to do with other parameters of interest, but here are their values"<<endl;
    mc->GetParametersOfInterest()->Print("v");
  }

  /////////////////////////////////////////////
  // create thte test stat sampler
  ProfileLikelihoodTestStat ts(*mc->GetPdf());

  // to avoid effects from boundary and simplify asymptotic comparison, set min=-max
  if(allowNegativeMu)
    firstPOI->setMin(-1*firstPOI->getMax());

  // temporary RooArgSet
  RooArgSet poi;
  poi.add(*mc->GetParametersOfInterest());

  // create and configure the ToyMCSampler
  ToyMCSampler sampler(ts,nToyMC);
  sampler.SetPdf(*mc->GetPdf());
  sampler.SetObservables(*mc->GetObservables());
  sampler.SetGlobalObservables(*mc->GetGlobalObservables());
  if(!mc->GetPdf()->canBeExtended() && (data->numEntries()==1)){
    cout << "tell it to use 1 event"<<endl;
    sampler.SetNEventsPerToy(1);
  }
  firstPOI->setVal(plcUpperLimit); // set POI value for generation
  sampler.SetParametersForTestStat(*mc->GetParametersOfInterest()); // set POI value for evaluation

  if (useProof) {
     ProofConfig pc(*w, nworkers, "",false);
     sampler.SetProofConfig(&pc); // enable proof
  }

  firstPOI->setVal(plcUpperLimit);
  RooArgSet allParameters;
  allParameters.add(*mc->GetParametersOfInterest());
  allParameters.add(*mc->GetNuisanceParameters());
  allParameters.Print("v");

  SamplingDistribution* sampDist = sampler.GetSamplingDistribution(allParameters);
  SamplingDistPlot plot;
  plot.AddSamplingDistribution(sampDist);
  plot.GetTH1F(sampDist)->GetYaxis()->SetTitle(Form("f(-log #lambda(#mu=%.2f) | #mu=%.2f)",plcUpperLimit,plcUpperLimit));
  plot.SetAxisTitle(Form("-log #lambda(#mu=%.2f)",plcUpperLimit));

  TCanvas* c1 = new TCanvas("c1");
  c1->SetLogy();
  plot.Draw();
  double min = plot.GetTH1F(sampDist)->GetXaxis()->GetXmin();
  double max = plot.GetTH1F(sampDist)->GetXaxis()->GetXmax();

  TF1* f = new TF1("f",Form("2*ROOT::Math::chisquared_pdf(2*x,%d,0)",nPOI),min,max);
  f->Draw("same");
  c1->SaveAs("standard_test_stat_distribution.pdf");

}
Пример #8
0
void CountingModelCross_2(  int nobs_4m = 27,           // number of observed events
			   double b_4m = 0.09,           // number of background events
			   double sigmab_4m = 0.16,     // relative uncertainty in b 1
			   
			   int nobs_2e2m = 30,           // number of observed events
			   double b_2e2m = 0.47,           // number of background events
			   double sigmab_2e2m = 0.26,    // relative uncertainty in b 2
			   
			   
			   int nobs_4e = 8,           // number of observed events
			   double b_4e = 0.44,           // number of background events
			   double sigmab_4e = 0.35    // relative uncertainty in b 2
			   )
  
{
  RooWorkspace w("w");
  
  // make Poisson model * Gaussian constraint
  w.factory("prod:s_4m(cross[34.7,0.,10000000],BR_4m[0.5,0,1],Acc_4m[0.5,0.,1.],Lumi[2.229,0.,10000])");  
  w.factory("prod:s_2e2m(cross[34.7,0.,10000000],BR_2e2m[0.5,0,1],Acc_2e2m[0.5,0.,1.],Lumi[2.229,0.,10000])");  
  w.factory("prod:s_4e(cross[34.7,0.,10000000],BR_4e[0.5,0,1],Acc_4e[0.5,0.,1.],Lumi[2.229,0.,10000])");  

  w.factory("sum:nexp_4m(s_4m,b_4m[1,0,10])");
  w.factory("sum:nexp_2e2m(s_2e2m,b_2e2m[1,0,10])");
  w.factory("sum:nexp_4e(s_4e,b_4e[1,0,10])");
  
  w.var("b_4m")->setVal(b_4m);
  w.var("b_2e2m")->setVal(b_2e2m);
  w.var("b_4e")->setVal(b_4e);

  // Poisson of (n | s+b)
  w.factory("Poisson:pdf_4m(nobs_4m[0,50],nexp_4m)");
  w.factory("Poisson:pdf_2e2m(nobs_2e2m[0,50],nexp_2e2m)");
  w.factory("Poisson:pdf_4e(nobs_4e[0,50],nexp_4e)");

  w.factory("Gaussian:constraint_4m(b0_4m[0,10],b_4m,sigmab_4m[1])");
  w.factory("Gaussian:constraint_2e2m(b0_2e2m[0,10],b_2e2m,sigmab_2e2m[1])");
  w.factory("Gaussian:constraint_4e(b0_4e[0,10],b_4e,sigmab_4e[1])");

  w.factory("Gaussian:constraintAcc_4m(Acc0_4m[0,1],Acc_4m,sigmaAcc_4m[1])");
  w.factory("Gaussian:constraintAcc_2e2m(Acc0_2e2m[0,1],Acc_2e2m,sigmaAcc_2e2m[1])");
  w.factory("Gaussian:constraintAcc_4e(Acc0_4e[0,1],Acc_4e,sigmaAcc_4e[1])");


  w.factory("PROD:model_4m(pdf_4m,constraint_4m,constraintAcc_4m)");
  w.factory("PROD:model_2e2m(pdf_2e2m,constraint_2e2m,constraintAcc_2e2m)");
  w.factory("PROD:model_4e(pdf_4e,constraint_4e,constraintAcc_4e)");

  w.factory("PROD:model(model_4m,model_2e2m,model_4e)");

  w.var("b0_4m")->setVal(b_4m);
  w.var("b0_2e2m")->setVal(b_2e2m);
  w.var("b0_4e")->setVal(b_4e);

  w.var("b0_4m")->setConstant(true); // needed for being treated as global observables
  w.var("b0_2e2m")->setConstant(true); // needed for being treated as global observables
  w.var("b0_4e")->setConstant(true); // needed for being treated as global observables


  // w.var("Acc_4m")->setVal(0.844);
  // w.var("Acc_2e2m")->setVal(0.694);
  // w.var("Acc_4e")->setVal(0.60);

  Float_t Acc_4m = 0.844*0.5388;
  Float_t Acc_2e2m = 0.694*0.5388;
  Float_t Acc_4e = 0.60*0.5388;   

  // Float_t Acc_4m = 0.844;
  // Float_t Acc_2e2m = 0.694;
  // Float_t Acc_4e = 0.60;

  Float_t  sigmaAcc_4m = 0.0097 ;
  Float_t  sigmaAcc_2e2m = 0.0097 ;
  Float_t  sigmaAcc_4e = 0.0097 ;

  Float_t BRele = 0.03363;
  Float_t BRmu  = 0.03366;  

  Float_t BR_4m = BRmu*BRmu;
  Float_t BR_2e2m = 2*BRmu*BRele;
  Float_t BR_4e = BRele*BRele;

  Float_t BR = BR_4m+BR_2e2m+BR_4e;

   cout<<"BR sum"<<BR<<" BRmean "<<(BR_4m + BR_4e + BR_2e2m)/3.<<" BR_4m "<<BR_4m<<" BR_2e2m "<<BR_2e2m<<" BR_4e "<<BR_4e<<endl;

  w.var("BR_4m")->setVal(BR_4m);
  w.var("BR_2e2m")->setVal(BR_2e2m);
  w.var("BR_4e")->setVal(BR_4e);

  w.var("BR_4m")->setConstant(true); // needed for being treated as global observables
  w.var("BR_2e2m")->setConstant(true); // needed for being treated as global observables
  w.var("BR_4e")->setConstant(true); // needed for being treated as global observables

  w.var("Lumi")->setVal(2.560);
  w.var("Lumi")->setConstant(true); 

  w.var("sigmab_4m")->setVal(sigmab_4m*b_4m);  
  w.var("sigmab_2e2m")->setVal(sigmab_2e2m*b_2e2m);  
  w.var("sigmab_4e")->setVal(sigmab_4e*b_4e);  

  w.var("sigmaAcc_4m")->setVal(sigmaAcc_4m*Acc_4m);  
  w.var("sigmaAcc_2e2m")->setVal(sigmaAcc_2e2m*Acc_2e2m);  
  w.var("sigmaAcc_4e")->setVal(sigmaAcc_4e*Acc_4e);  
  
  ModelConfig mc("ModelConfig",&w);
  
   mc.SetPdf(*w.pdf("model"));
   mc.SetParametersOfInterest(*w.var("cross"));
  
   mc.SetObservables(RooArgSet(*w.var("nobs_4m"),*w.var("nobs_2e2m"),*w.var("nobs_4e")));  
  // mc.SetNuisanceParameters(RooArgSet(*w.var("b_4m"),*w.var("b_2e2m"),*w.var("b_4e")));

   //mc.SetObservables(RooArgSet(*w.var("nobs_4m"),*w.var("nobs_2e2m")));  
   //   mc.SetNuisanceParameters(RooArgSet(*w.var("b_4m"),*w.var("b_2e2m"),*w.var("Acc_4m"),*w.var("Acc_2e2m")));
   mc.SetNuisanceParameters(RooArgSet(*w.var("b_4m"),*w.var("b_2e2m"),*w.var("b_4e"),*w.var("Acc_4m"),*w.var("Acc_2e2m"),*w.var("Acc_4e")));

   mc.SetParametersOfInterest(*w.var("cross"));

  // these are needed for the hypothesis tests
  mc.SetSnapshot(*w.var("cross"));
  //  mc.SetGlobalObservables(RooArgSet(*w.var("b0_4m"),*w.var("b0_2e2m"),*w.var("b0_4e")));


  mc.Print();
  // import model in the workspace 

  w.import(mc);

  // make data set with the namber of observed events
  RooDataSet data("data","", RooArgSet(*w.var("nobs_4m"),*w.var("nobs_2e2m"),*w.var("nobs_4e")));
  //RooDataSet data("data","", RooArgSet(*w.var("nobs_4m"),*w.var("nobs_2e2m")));


  w.var("nobs_4m")->setVal(nobs_4m);
  data.add(*w.var("nobs_4m") );

  w.var("nobs_2e2m")->setVal(nobs_2e2m);
  data.add(*w.var("nobs_2e2m") );

  w.var("nobs_4e")->setVal(nobs_4e);
  data.add(*w.var("nobs_4e") );

  // import data set in workspace and save it in a file

  ProfileLikelihoodCalculator pl(data, mc);
  pl.SetConfidenceLevel(0.68); // 95% interval
  LikelihoodInterval* interval = pl.GetInterval();

  // print out the iterval on the first Parameter of Interest
  RooRealVar* firstPOI = (RooRealVar*) mc.GetParametersOfInterest()->first();
  cout << "\n65% interval on " <<firstPOI->GetName()<<" is : ["<<
    interval->LowerLimit(*firstPOI) << ", "<<
    interval->UpperLimit(*firstPOI) <<"] "<<endl;

  std::cout<<"Cross "<<mc.GetParametersOfInterest()->getRealValue("cross")*BR<<std::endl;

  w.import(data);

  w.Print();

  TString fileName = "CountingModel.root"; 

  // write workspace in the file (recreate file if already existing)
  w.writeToFile(fileName, true);

}
Пример #9
0
Int_t Tprime::RunCls( std::string channel, // ejets, mujets, combined
                      std::string mode,    // observed, expected
                      double peak,         // resonance mass
                      std::string suffix,  // suffix for output file names
                      Int_t ntoys,         // number of pseudoexperiments for expected limit
                      std::string method,  // method asymcls or cls standard
                      Int_t npoints,       // points to scan
                      Double_t poimin,     // scan upper range
                      Double_t poimax,     // scan upper range
                      std::string inputdir ) { // input dir name
    //
    // CLs calculation
    //

    //RooStats::ProfileLikelihoodTestStat::setReuseNLL(kTRUE) ; // activate use of setData()
    //RooStats::ToyMCSampler::setUseMultiGen(kTRUE) ;// activate use of prepareMultiGen()

    std::string legend = "[tprime::RunCls()]: ";

    // print out inputs
    std::cout << legend << std::endl;
    std::cout << legend << "Input parameters specified. Some of them are not used and defaults are entered" << std::endl;
    std::cout << legend << "------------------------------" << std::endl;
    std::cout << legend << "channel: " << channel << std::endl;
    std::cout << legend << "mode: " << mode << std::endl;
    std::cout << legend << "input directory: " << inputdir << std::endl;
    std::cout << legend << "resonance peak mass: " << peak << std::endl;
    std::cout << legend << "suffix: ->" << suffix << "<-" << std::endl;
    std::cout << legend << "number of pseudo-experiments: "<< ntoys << std::endl;
    std::cout << legend << std::endl;

    // compose the workspace file name
    char buf[1024];
    sprintf(buf, "%sresults_%04.0f/tprime_%s_tprimeCrossSection_model.root", inputdir.c_str(), peak, channel.c_str());
    std::string _file = buf;
    std::cout << legend << "guessed name of the file with the workspace: >" << _file << "<" << std::endl;

    if (suffix.find("merge_")==std::string::npos) {

        //load workspace
        LoadWorkspace(_file, channel);

        // fix POI range if needed
        std::cout << std::endl << legend << "fixing POI range to [0-10]" << std::endl;
        pWs->var("xsec")->setRange(poimin,poimax);

    }

    // timer
    TStopwatch t;
    t.Start();


    // FIXME: experimental: try adaptive range

    if (suffix.find("merge_")==std::string::npos) {
        GetPlrInterval(0.95);
        Double_t upper_limit = pPlrInt->UpperLimit( *pWs->var("xsec") );
        // FIXME: this only works for limits ~1!!! May become 0 - dangerous!
        poimax = ((double)(int)(4.0 * upper_limit*100.0))/100.0; // round to ~1% precision to avoid splitting points in batch
    }

    int calcType = 1; // calculator type, 0-freq, 1-hybrid, 2-asymototic cls
    if( method.find("asymcls") != std::string::npos ) {
        calcType = 2;
    }
    std::string suff = suffix;
    if(suffix.find('asymcls') != std::string::npos) {
        suff = "";
    }

    std::vector<Double_t> lim =
        StandardHypoTestInvDemo( _file.c_str(), //filename
                                 channel.c_str(),//pWs, //ws name const char
                                 "ModelConfig",
                                 "BModel",
                                 "obsData",
                                 calcType,
                                 3, // test statistic, 0-lep, 1-tevatron, 2-PL, 3-PL 1-sided
                                 true, // useCls
                                 npoints, // npoints in the scan
                                 poimin, // poimin: use default is poimin >= poimax
                                 poimax, // poimax
                                 ntoys,// ntoys
                                 false, //bool //use number counting
                                 0,//nuispriorname
                                 suff.c_str(), //suffix name
                                 peak
                               );

    std::string _outfile = "tprime_limit_cls_" + channel + "_" + mode + "_" + suffix + ".ascii";
    PrintToFile(_outfile, lim,
                "# mass observed_limit  expected_m2    expected_m1   expected_med    expected_p1    expected_p2");

    t.Stop();
    t.Print();

    return 0;
}
Пример #10
0
Int_t Tprime::RunPlr( std::string channel, // ejets, mujets, combined
                      std::string mode,    // observed, expected
                      double peak,         // resonance mass
                      std::string suffix,  // suffix for output file names
                      Int_t ntoys,         // number of pseudoexperiments for expected limit
                      std::string inputdir ) { // input dir name
    //
    // Profile likelihood ratio calculation
    //

    //RooStats::ProfileLikelihoodTestStat::setReuseNLL(kTRUE) ; // activate use of setData()
    //RooStats::ToyMCSampler::setUseMultiGen(kTRUE) ;// activate use of prepareMultiGen()

    std::string legend = "[tprime::RunPlr()]: ";

    // print out inputs
    std::cout << legend << std::endl;
    std::cout << legend << "Input parameters specified. Some of them are not used and defaults are entered" << std::endl;
    std::cout << legend << "------------------------------" << std::endl;
    std::cout << legend << "channel: " << channel << std::endl;
    std::cout << legend << "mode: " << mode << std::endl;
    std::cout << legend << "input directory: " << inputdir << std::endl;
    std::cout << legend << "resonance peak mass: " << peak << std::endl;
    std::cout << legend << "suffix: ->" << suffix << "<-" << std::endl;
    std::cout << legend << "number of pseudo-experiments: "<< ntoys << std::endl;
    std::cout << legend << std::endl;

    // compose the workspace file name
    char buf[1024];
    sprintf(buf, "%sresults_%04.0f/tprime_%s_tprimeCrossSection_model.root", inputdir.c_str(), peak, channel.c_str());
    std::string _file = buf;
    std::cout << legend << "guessed name of the file with the workspace: >" << _file << "<" << std::endl;

    //load workspace
    LoadWorkspace(_file, channel);

    // fix POI range if needed
    std::cout << std::endl << legend << "fixing POI range to [0-10]" << std::endl;
    pWs->var("xsec")->setRange(0,10);

    // timer
    TStopwatch t;
    t.Start();

    int pe_counter = 0;
    while (pe_counter < ntoys) {

        // set data
        if (mode.find("observed") != std::string::npos ) {

            std::cout << legend << std::endl;
            std::cout << legend << "calculating an observed limit..." << std::endl;
            std::cout << legend << std::endl;

            GetWorkspaceData("obsData");
            ntoys = 1;

            //RooStats::ModelConfig * _mc = GetModaelConfig();
            //_mc->GetPdf()->fitTo(*data, Save());

        }
        else if (mode.find("expected") != std::string::npos ) { // expected interval

            std::cout << legend << std::endl;
            std::cout << legend << "this is pseudoexperiment " << pe_counter+1 << " of " << ntoys << std::endl;
            std::cout << legend << "for the expected limit estimate" << std::endl;
            std::cout << legend << std::endl;

            GetPseudoData();
            //GetWorkspaceData("obsData");

        } // end of expected interval

        GetPlrInterval(0.95);

        // query intervals
        RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
        RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
        RooStats::ModelConfig * _mc = GetModelConfig();
        RooRealVar * _poi = (RooRealVar *)_mc->GetParametersOfInterest()->first();
        Double_t upper_limit = pPlrInt->UpperLimit( *_poi );
        RooMsgService::instance().setGlobalKillBelow(msglevel);

        std::cout << legend << "Profile Likelihood upper limit = " << upper_limit << std::endl;

        std::string _outfile = "tprime_limit_plr_" + channel + "_" + mode + "_" + suffix + ".ascii";
        std::vector<Double_t> results;
        results.push_back(peak);
        results.push_back(upper_limit);
        PrintToFile( _outfile, results, "#          peak    upper_limit" );

        ++pe_counter;
    } // end of while toy loop

    // stop timer and print time used
    t.Stop();
    t.Print();

    // make plot
    TCanvas c1("c1");
    LikelihoodIntervalPlot * pPlrPlot = new LikelihoodIntervalPlot(pPlrInt);
    pPlrPlot->Draw();
    std::string plot_file_name = "tprime_limit_" + channel + "_" + mode + "_" + suffix + ".pdf";
    c1.SaveAs(plot_file_name.c_str());
    delete pPlrPlot;

    return 0;
}