StandardFeldmanCousinsDemo.C

Go to the documentation of this file.
00001 // Standard demo of the Feldman-Cousins calculator
00002 /*
00003 StandardFeldmanCousinsDemo 
00004 
00005 Author: Kyle Cranmer
00006 date: Dec. 2010
00007 
00008 This is a standard demo that can be used with any ROOT file 
00009 prepared in the standard way.  You specify:
00010  - name for input ROOT file
00011  - name of workspace inside ROOT file that holds model and data
00012  - name of ModelConfig that specifies details for calculator tools
00013  - name of dataset 
00014 
00015 With default parameters the macro will attempt to run the 
00016 standard hist2workspace example and read the ROOT file
00017 that it produces.
00018 
00019 The actual heart of the demo is only about 10 lines long.
00020 
00021 The FeldmanCousins tools is a classical frequentist calculation
00022 based on the Neyman Construction.  The test statistic can be
00023 generalized for nuisance parameters by using the profile likeihood ratio.
00024 But unlike the ProfileLikelihoodCalculator, this tool explicitly
00025 builds the sampling distribution of the test statistic via toy Monte Carlo.
00026 */
00027 
00028 #include "TFile.h"
00029 #include "TROOT.h"
00030 #include "TH1F.h"
00031 
00032 #include "RooWorkspace.h"
00033 #include "RooAbsData.h"
00034 
00035 #include "RooStats/ModelConfig.h"
00036 #include "RooStats/FeldmanCousins.h"
00037 #include "RooStats/ToyMCSampler.h"
00038 #include "RooStats/PointSetInterval.h"
00039 #include "RooStats/ConfidenceBelt.h"
00040 
00041 
00042 using namespace RooFit;
00043 using namespace RooStats;
00044 
00045 void StandardFeldmanCousinsDemo(const char* infile = "",
00046                       const char* workspaceName = "proto",
00047                       const char* modelConfigName = "ModelConfig",
00048                       const char* dataName = "expData"){
00049 
00050   /////////////////////////////////////////////////////////////
00051   // First part is just to access a user-defined file 
00052   // or create the standard example file if it doesn't exist
00053   ////////////////////////////////////////////////////////////
00054   const char* filename = "";
00055   if (!strcmp(infile,""))
00056     filename = "results/example_channel1_GammaExample_model.root";
00057   else
00058     filename = infile;
00059   // Check if example input file exists
00060   TFile *file = TFile::Open(filename);
00061 
00062   // if input file was specified byt not found, quit
00063   if(!file && strcmp(infile,"")){
00064     cout <<"file not found" << endl;
00065     return;
00066   } 
00067 
00068   // if default file not found, try to create it
00069   if(!file ){
00070     // Normally this would be run on the command line
00071     cout <<"will run standard hist2workspace example"<<endl;
00072     gROOT->ProcessLine(".! prepareHistFactory .");
00073     gROOT->ProcessLine(".! hist2workspace config/example.xml");
00074     cout <<"\n\n---------------------"<<endl;
00075     cout <<"Done creating example input"<<endl;
00076     cout <<"---------------------\n\n"<<endl;
00077   }
00078 
00079   // now try to access the file again
00080   file = TFile::Open(filename);
00081   if(!file){
00082     // if it is still not there, then we can't continue
00083     cout << "Not able to run hist2workspace to create example input" <<endl;
00084     return;
00085   }
00086 
00087   
00088   /////////////////////////////////////////////////////////////
00089   // Tutorial starts here
00090   ////////////////////////////////////////////////////////////
00091 
00092   // get the workspace out of the file
00093   RooWorkspace* w = (RooWorkspace*) file->Get(workspaceName);
00094   if(!w){
00095     cout <<"workspace not found" << endl;
00096     return;
00097   }
00098 
00099   // get the modelConfig out of the file
00100   ModelConfig* mc = (ModelConfig*) w->obj(modelConfigName);
00101 
00102   // get the modelConfig out of the file
00103   RooAbsData* data = w->data(dataName);
00104 
00105   // make sure ingredients are found
00106   if(!data || !mc){
00107     w->Print();
00108     cout << "data or ModelConfig was not found" <<endl;
00109     return;
00110   }
00111 
00112   /////////////////////////////////////////////
00113   // create and use the FeldmanCousins tool
00114   // to find and plot the 95% confidence interval
00115   // on the parameter of interest as specified
00116   // in the model config
00117   FeldmanCousins fc(*data,*mc);
00118   fc.SetConfidenceLevel(0.95); // 95% interval
00119   //fc.AdditionalNToysFactor(0.1); // to speed up the result 
00120   fc.UseAdaptiveSampling(true); // speed it up a bit
00121   fc.SetNBins(10); // set how many points per parameter of interest to scan
00122   fc.CreateConfBelt(true); // save the information in the belt for plotting
00123 
00124   // Since this tool needs to throw toy MC the PDF needs to be
00125   // extended or the tool needs to know how many entries in a dataset
00126   // per pseudo experiment.  
00127   // In the 'number counting form' where the entries in the dataset
00128   // are counts, and not values of discriminating variables, the
00129   // datasets typically only have one entry and the PDF is not
00130   // extended.  
00131   if(!mc->GetPdf()->canBeExtended()){
00132     if(data->numEntries()==1)     
00133       fc.FluctuateNumDataEntries(false);
00134     else
00135       cout <<"Not sure what to do about this model" <<endl;
00136   }
00137 
00138   // We can use PROOF to speed things along in parallel
00139   //  ProofConfig pc(*w, 1, "workers=4", kFALSE);
00140   //  ToyMCSampler*  toymcsampler = (ToyMCSampler*) fc.GetTestStatSampler();
00141   //  toymcsampler->SetProofConfig(&pc);        // enable proof
00142 
00143 
00144   // Now get the interval
00145   PointSetInterval* interval = fc.GetInterval();
00146   ConfidenceBelt* belt = fc.GetConfidenceBelt();
00147  
00148   // print out the iterval on the first Parameter of Interest
00149   RooRealVar* firstPOI = (RooRealVar*) mc->GetParametersOfInterest()->first();
00150   cout << "\n95% interval on " <<firstPOI->GetName()<<" is : ["<<
00151     interval->LowerLimit(*firstPOI) << ", "<<
00152     interval->UpperLimit(*firstPOI) <<"] "<<endl;
00153 
00154   //////////////////////////////////////////////
00155   // No nice plots yet, so plot the belt by hand
00156   
00157   // Ask the calculator which points were scanned
00158   RooDataSet* parameterScan = (RooDataSet*) fc.GetPointsToScan();
00159   RooArgSet* tmpPoint;
00160 
00161   // make a histogram of parameter vs. threshold
00162   TH1F* histOfThresholds = new TH1F("histOfThresholds","",
00163                                     parameterScan->numEntries(),
00164                                     firstPOI->getMin(),
00165                                     firstPOI->getMax());
00166 
00167   // loop through the points that were tested and ask confidence belt
00168   // what the upper/lower thresholds were.
00169   // For FeldmanCousins, the lower cut off is always 0
00170   for(Int_t i=0; i<parameterScan->numEntries(); ++i){
00171     tmpPoint = (RooArgSet*) parameterScan->get(i)->clone("temp");
00172     double arMax = belt->GetAcceptanceRegionMax(*tmpPoint);
00173     double arMin = belt->GetAcceptanceRegionMax(*tmpPoint);
00174     double poiVal = tmpPoint->getRealValue(firstPOI->GetName()) ;
00175     histOfThresholds->Fill(poiVal,arMax);
00176   }
00177   histOfThresholds->SetMinimum(0);
00178   histOfThresholds->Draw();
00179 
00180 }

Generated on Tue Jul 5 15:45:10 2011 for ROOT_528-00b_version by  doxygen 1.5.1