Esempio n. 1
0
void plot_CL_chi2_roofit(char * filename, double min, double max, double initial, double ndof_min, double ndof_max, char * plot, char * var = "chi2")
{

	//gStyle->SetOptStat(0);
	//gStyle->SetOptFit(1);
	//gStyle->SetStatFontSize(0.02);
	TFile * _file0 = TFile::Open(filename);
	TTree * t = (TTree*)_file0->Get("tuple");

	RooRealVar * chi2 = new RooRealVar(var, "#chi^{2}", min, max);
	RooRealVar * ndof = new RooRealVar("ndof", "ndof", initial, ndof_min, ndof_max);
	RooChiSquarePdf * pdf = new RooChiSquarePdf("pdf", "pdf", *chi2, *ndof);

	RooDataSet * data = new RooDataSet("data", "data", RooArgSet(*chi2), RooFit::Import(*t));

	pdf->fitTo(*data);

	char formula[30];
	sprintf(formula, "TMath::Prob(%s,ndof)", var);

	RooFormulaVar * CL_ndof_eff_formula = new RooFormulaVar("CL","CL(#chi^{2})",formula, RooArgList(*chi2, *ndof));
	RooRealVar * CL_ndof_eff = (RooRealVar*) data->addColumn(*CL_ndof_eff_formula);
	CL_ndof_eff->setRange(0, 1);
	RooUniform * uniform = new RooUniform("uniform", "uniform", *CL_ndof_eff);
	uniform->fitTo(*data);

	//RooFormulaVar * CL_ndof_min_formula = new RooFormulaVar("CL","CL(#chi^{2})","TMath::Prob(chi2,39)", RooArgList(*chi2));
	//RooRealVar * CL_ndof_min = (RooRealVar*) data->addColumn(*CL_ndof_min_formula);
	//CL_ndof_min->setRange(0, 1);

	RooPlot * frame0 = chi2->frame(RooFit::Bins(25));
	data->plotOn(frame0);
	pdf->plotOn(frame0);
	pdf->paramOn(frame0, RooFit::Format("NELU", RooFit::AutoPrecision(2)), RooFit::Layout(0.6,0.95,0.75)); 
	data->statOn(frame0, RooFit::Format("NELU", RooFit::AutoPrecision(2)), RooFit::Layout(0.6,0.95,0.95));

	RooPlot * frame1 = CL_ndof_eff->frame(RooFit::Bins(10));
	data->plotOn(frame1);
	uniform->plotOn(frame1);	

        TCanvas * c = new TCanvas("c","c",1200, 600);
        c->Divide(2,1);
        c->cd(1);
	frame0->Draw();
        c->cd(2);
	frame1->Draw();

/*
        char buf[30];
        sprintf(buf, "TMath::Prob(chi2,%f)>>h1", f1->GetParameter(0));
        cout << buf << endl;
        c->Modified();
        c->Update();

        c->cd(2);
        t->Draw("TMath::Prob(chi2,ndof-8)>>h0");
        t->Draw(buf);
        h1->Draw();
        h1->Fit("pol0");
        h0->Draw("same");
        h1->GetXaxis()->SetTitle("CL(#chi^{2})");
        h1->GetYaxis()->SetTitle("Number of toys / 0.1");
        h1->SetMinimum(0);
        h1->SetMaximum(2*t->GetEntries()/nbins);
*/
        c->SaveAs(plot);



}
Esempio n. 2
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();

}
Esempio n. 3
0
void rf106_plotdecoration()
{

  // S e t u p   m o d e l 
  // ---------------------

  // Create observables
  RooRealVar x("x","x",-10,10) ;

  // Create Gaussian
  RooRealVar sigma("sigma","sigma",1,0.1,10) ;
  RooRealVar mean("mean","mean",-3,-10,10) ;
  RooGaussian gauss("gauss","gauss",x,mean,sigma) ;

  // Generate a sample of 1000 events with sigma=3
  RooDataSet* data = gauss.generate(x,1000) ;

  // Fit pdf to data
  gauss.fitTo(*data) ;


  // P l o t   p . d . f   a n d   d a t a 
  // -------------------------------------

  // Overlay projection of gauss on data
  RooPlot* frame = x.frame(Name("xframe"),Title("RooPlot with decorations"),Bins(40)) ;
  data->plotOn(frame) ;
  gauss.plotOn(frame) ;


  // A d d   b o x   w i t h   p d f   p a r a m e t e r s 
  // -----------------------------------------------------

  // Left edge of box starts at 55% of Xaxis)
  gauss.paramOn(frame,Layout(0.55)) ;


  // A d d   b o x   w i t h   d a t a   s t a t i s t i c s
  // -------------------------------------------------------  

  // X size of box is from 55% to 99% of Xaxis range, top of box is at 80% of Yaxis range)
  data->statOn(frame,Layout(0.55,0.99,0.8)) ;


  // A d d   t e x t   a n d   a r r o w 
  // -----------------------------------

  // Add text to frame
  TText* txt = new TText(2,100,"Signal") ;
  txt->SetTextSize(0.04) ;
  txt->SetTextColor(kRed) ;
  frame->addObject(txt) ;

  // Add arrow to frame
  TArrow* arrow = new TArrow(2,100,-1,50,0.01,"|>") ;
  arrow->SetLineColor(kRed) ;
  arrow->SetFillColor(kRed) ;
  arrow->SetLineWidth(3) ;
  frame->addObject(arrow) ;


  // P e r s i s t   f r a m e   w i t h   a l l   d e c o r a t i o n s   i n   R O O T   f i l e
  // ---------------------------------------------------------------------------------------------

  TFile f("rf106_plotdecoration.root","RECREATE") ;
  frame->Write() ;
  f.Close() ;

  // To read back and plot frame with all decorations in clean root session do
  // root> TFile f("rf106_plotdecoration.root") ;
  // root>  xframe->Draw() ;

  new TCanvas("rf106_plotdecoration","rf106_plotdecoration",600,600) ;
  gPad->SetLeftMargin(0.15) ; frame->GetYaxis()->SetTitleOffset(1.6) ; frame->Draw() ;
  
}