readExclusion() { TFile * file = new TFile("theExclusion_M115_EGMLOOSE006.root"); HypoTestInverterResult * r = dynamic_cast<HypoTestInverterResult*>( file->Get("result_r") ); double upperLimit = r->UpperLimit(); double ulError = r->UpperLimitEstimatedError(); std::cout << "The computed upper limit is: " << upperLimit << " +/- " << ulError << std::endl; const int nEntries = r->ArraySize(); TCanvas *c0 = new TCanvas("c0","coucou",600,600); c0->SetFillColor(0); TString plotTitle = "CL Scan for workspace"; HypoTestInverterPlot *plot = new HypoTestInverterPlot("HTI_Result_Plot",plotTitle,r); plot->Draw("CLb 2CL"); // plot all and Clb c0->Print("limit_plot.gif"); const int nEntries = r->ArraySize(); cout << "N entries = " << nEntries << endl; TCanvas * c2 = new TCanvas(); c2->Divide( 2, TMath::Ceil(nEntries/2)); for (int i=0; i<nEntries; i++) { c2->cd(i+1); SamplingDistPlot * pl = plot->MakeTestStatPlot(i); pl->SetLogYaxis(true); pl->Draw(); } }
readExclusion_M130(){ TString massPointString = IntToString(massPoint); TFile * file = new TFile("theExclusion_M"+massPointString+"_"+nomPlot+".root"); HypoTestInverterResult * r = dynamic_cast<HypoTestInverterResult*>( file->Get("result_r") ); double upperLimit = r->UpperLimit(); double ulError = r->UpperLimitEstimatedError(); std::cout << "The computed upper limit is: " << upperLimit << " +/- " << ulError << std::endl; const int nEntries = r->ArraySize(); TCanvas *c0 = new TCanvas("c0","coucou",600,600); c0->SetFillColor(0); TString plotTitle = "CL Scan for workspace"; HypoTestInverterPlot *plot = new HypoTestInverterPlot("HTI_Result_Plot",plotTitle,r); plot->Draw("CLb 2CL");//("CLb 2CL"); // plot all and Clb c0->Print("limit_plot_M"+massPointString+".gif"); const int nEntries = r->ArraySize(); cout << "N entries = " << nEntries << endl; TCanvas * c2 = new TCanvas(); c2->Divide( 2, TMath::Ceil(nEntries/2)); for (int i=0; i<nEntries; i++) { c2->cd(i+1); SamplingDistPlot * pl = plot->MakeTestStatPlot(i); pl->SetLogYaxis(true); pl->Draw(); } std::cout << "The computed upper limit is: " << upperLimit << " +/- " << ulError << std::endl; std::cout << "The expected limi is : " << r->GetExpectedUpperLimit(0) << endl; float expected = r->GetExpectedUpperLimit(0); float observed = r->UpperLimit(); float observedError = r->UpperLimitEstimatedError(); float expected1sP = r->GetExpectedUpperLimit(1); float expected2sP = r->GetExpectedUpperLimit(2); float expected1sM = r->GetExpectedUpperLimit(-1); float expected2sM = r->GetExpectedUpperLimit(-2); cout << "ZZZZ graph->SetPoint(" <<number << "," << massPoint<< "," << expected << ");" << endl; cout << "ZZZZ grae->SetPoint(" <<number << "," << massPoint<< "," << expected << ");" << endl; cout << "ZZZZ grae->SetPointError(" << number << ",0,0," << (expected-expected1sM) << "," << (expected1sP-expected) << ");" << endl; cout << "ZZZZ grae2->SetPoint(" <<number << "," << massPoint<< "," << expected << ");" << endl; cout << "ZZZZ grae2->SetPointError(" << number << ",0,0," << (expected-expected2sM) << "," << (expected2sP-expected) << ");" << endl; cout << "ZZZZ gre->SetPoint(" <<number << "," << massPoint<< "," << observed << ");" << endl; cout << "ZZZZ gre->SetPointError(" <<number << ",0," << observedError <<");" << endl; }
void RooStats::HypoTestInvTool::AnalyzeResult( HypoTestInverterResult * r, int calculatorType, int testStatType, bool useCLs, int npoints, const char * fileNameBase ){ // analyze result produced by the inverter, optionally save it in a file double lowerLimit = 0; double llError = 0; #if defined ROOT_SVN_VERSION && ROOT_SVN_VERSION >= 44126 if (r->IsTwoSided()) { lowerLimit = r->LowerLimit(); llError = r->LowerLimitEstimatedError(); } #else lowerLimit = r->LowerLimit(); llError = r->LowerLimitEstimatedError(); #endif double upperLimit = r->UpperLimit(); double ulError = r->UpperLimitEstimatedError(); //std::cout << "DEBUG : [ " << lowerLimit << " , " << upperLimit << " ] " << std::endl; if (lowerLimit < upperLimit*(1.- 1.E-4) && lowerLimit != 0) std::cout << "The computed lower limit is: " << lowerLimit << " +/- " << llError << std::endl; std::cout << "The computed upper limit is: " << upperLimit << " +/- " << ulError << std::endl; // compute expected limit std::cout << "Expected upper limits, using the B (alternate) model : " << std::endl; std::cout << " expected limit (median) " << r->GetExpectedUpperLimit(0) << std::endl; std::cout << " expected limit (-1 sig) " << r->GetExpectedUpperLimit(-1) << std::endl; std::cout << " expected limit (+1 sig) " << r->GetExpectedUpperLimit(1) << std::endl; std::cout << " expected limit (-2 sig) " << r->GetExpectedUpperLimit(-2) << std::endl; std::cout << " expected limit (+2 sig) " << r->GetExpectedUpperLimit(2) << std::endl; printf("\n\n owen: (-2s,-1s,m,1s,2s) %5.2f %5.2f %5.2f %5.2f %5.2f\n\n", r->GetExpectedUpperLimit(-2), r->GetExpectedUpperLimit(-1), r->GetExpectedUpperLimit(0), r->GetExpectedUpperLimit(1), r->GetExpectedUpperLimit(2) ) ; // write result in a file if (r != NULL && mWriteResult) { // write to a file the results const char * calcType = (calculatorType == 0) ? "Freq" : (calculatorType == 1) ? "Hybr" : "Asym"; const char * limitType = (useCLs) ? "CLs" : "Cls+b"; const char * scanType = (npoints < 0) ? "auto" : "grid"; if (mResultFileName.IsNull()) { mResultFileName = TString::Format("%s_%s_%s_ts%d_",calcType,limitType,scanType,testStatType); //strip the / from the filename if (mMassValue.size()>0) { mResultFileName += mMassValue.c_str(); mResultFileName += "_"; } TString name = fileNameBase; name.Replace(0, name.Last('/')+1, ""); mResultFileName += name; } TFile * fileOut = new TFile(mResultFileName,"RECREATE"); r->Write(); fileOut->Close(); } // plot the result ( p values vs scan points) std::string typeName = ""; if (calculatorType == 0 ) typeName = "Frequentist"; if (calculatorType == 1 ) typeName = "Hybrid"; else if (calculatorType == 2 || calculatorType == 3) { typeName = "Asymptotic"; mPlotHypoTestResult = false; } const char * resultName = r->GetName(); TString plotTitle = TString::Format("%s CL Scan for workspace %s",typeName.c_str(),resultName); HypoTestInverterPlot *plot = new HypoTestInverterPlot("HTI_Result_Plot",plotTitle,r); // plot in a new canvas with style TString c1Name = TString::Format("%s_Scan",typeName.c_str()); TCanvas * c1 = new TCanvas(c1Name); c1->SetLogy(false); plot->Draw("CLb 2CL"); // plot all and Clb // if (useCLs) // plot->Draw("CLb 2CL"); // plot all and Clb // else // plot->Draw(""); // plot all and Clb const int nEntries = r->ArraySize(); // plot test statistics distributions for the two hypothesis if (mPlotHypoTestResult) { TCanvas * c2 = new TCanvas(); if (nEntries > 1) { int ny = TMath::CeilNint(TMath::Sqrt(nEntries)); int nx = TMath::CeilNint(double(nEntries)/ny); c2->Divide( nx,ny); } for (int i=0; i<nEntries; i++) { if (nEntries > 1) c2->cd(i+1); SamplingDistPlot * pl = plot->MakeTestStatPlot(i); pl->SetLogYaxis(true); pl->Draw(); } } }
void RA2bHypoTestInvDemo(const char * fileName =0, const char * wsName = "combined", const char * modelSBName = "ModelConfig", const char * modelBName = "", const char * dataName = "obsData", int calculatorType = 0, int testStatType = 3, bool useCls = true , int npoints = 5, double poimin = 0, double poimax = 5, int ntoys=1000, int mgl = -1, int mlsp = -1, const char * outFileName = "test") { /* Other Parameter to pass in tutorial apart from standard for filename, ws, modelconfig and data type = 0 Freq calculator type = 1 Hybrid testStatType = 0 LEP = 1 Tevatron = 2 Profile Likelihood = 3 Profile Likelihood one sided (i.e. = 0 if mu < mu_hat) useCLs scan for CLs (otherwise for CLs+b) npoints: number of points to scan , for autoscan set npoints = -1 poimin,poimax: min/max value to scan in case of fixed scans (if min >= max, try to find automatically) ntoys: number of toys to use extra options are available as global paramters of the macro. They are: plotHypoTestResult plot result of tests at each point (TS distributions) useProof = true; writeResult = true; nworkers = 4; */ if (fileName==0) { fileName = "results/example_combined_GaussExample_model.root"; std::cout << "Use standard file generated with HistFactory :" << fileName << std::endl; } TFile * file = new TFile(fileName); RooWorkspace * w = dynamic_cast<RooWorkspace*>( file->Get(wsName) ); HypoTestInverterResult * r = 0; std::cout << w << "\t" << fileName << std::endl; if (w != NULL) { r = RunInverter(w, modelSBName, modelBName, dataName, calculatorType, testStatType, npoints, poimin, poimax, ntoys, useCls ); if (!r) { std::cerr << "Error running the HypoTestInverter - Exit " << std::endl; return; } } else { // case workspace is not present look for the inverter result std::cout << "Reading an HypoTestInverterResult with name " << wsName << " from file " << fileName << std::endl; r = dynamic_cast<HypoTestInverterResult*>( file->Get(wsName) ); // if (!r) { std::cerr << "File " << fileName << " does not contain a workspace or an HypoTestInverterResult - Exit " << std::endl; file->ls(); return; } } printf("\n\n") ; HypoTestResult* htr = r->GetResult(0) ; printf(" Data value for test stat : %7.3f\n", htr->GetTestStatisticData() ) ; printf(" CLsplusb : %9.4f\n", r->CLsplusb(0) ) ; printf(" CLb : %9.4f\n", r->CLb(0) ) ; printf(" CLs : %9.4f\n", r->CLs(0) ) ; printf("\n\n") ; cout << flush ; double upperLimit = r->UpperLimit(); double ulError = r->UpperLimitEstimatedError(); std::cout << "The computed upper limit is: " << upperLimit << " +/- " << ulError << std::endl; const int nEntries = r->ArraySize(); const char * typeName = (calculatorType == 0) ? "Frequentist" : "Hybrid"; const char * resultName = (w) ? w->GetName() : r->GetName(); TString plotTitle = TString::Format("%s CL Scan for workspace %s",typeName,resultName); HypoTestInverterPlot *plot = new HypoTestInverterPlot("HTI_Result_Plot",plotTitle,r); TCanvas* c1 = new TCanvas() ; plot->Draw("CLb 2CL"); // plot all and Clb c1->Update() ; c1->SaveAs("cls-canv1.png") ; c1->SaveAs("cls-canv1.pdf") ; if (plotHypoTestResult) { TCanvas * c2 = new TCanvas(); c2->Divide( 2, TMath::Ceil(nEntries/2)); for (int i=0; i<nEntries; i++) { c2->cd(i+1); SamplingDistPlot * pl = plot->MakeTestStatPlot(i); pl->SetLogYaxis(true); pl->Draw(); } c2->Update() ; c2->SaveAs("cls-canv2.png") ; c2->SaveAs("cls-canv2.pdf") ; } std::cout << " expected limit (median) " << r->GetExpectedUpperLimit(0) << std::endl; std::cout << " expected limit (-1 sig) " << r->GetExpectedUpperLimit(-1) << std::endl; std::cout << " expected limit (+1 sig) " << r->GetExpectedUpperLimit(1) << std::endl; // save 2d histograms bin to file TH2F *result = new TH2F("result","result",22,100,1200,23,50,1200); TH2F *exp_res = new TH2F("exp_res","exp_res",22,100,1200,23,50,1200); TH2F *exp_res_minus = new TH2F("exp_res_minus","exp_res_minus",22,100,1200,23,50,1200); TH2F *exp_res_plus = new TH2F("exp_res_plus","exp_res_plus",22,100,1200,23,50,1200); result->Fill(mgl,mlsp,upperLimit); exp_res->Fill(mgl,mlsp,r->GetExpectedUpperLimit(0)); exp_res_minus->Fill(mgl,mlsp,r->GetExpectedUpperLimit(-1)); exp_res_plus->Fill(mgl,mlsp,r->GetExpectedUpperLimit(1)); TFile *f = new TFile(outFileName,"RECREATE"); f->cd(); result->Write(); exp_res->Write(); exp_res_minus->Write(); exp_res_plus->Write(); f->Close(); if (w != NULL && writeResult) { // write to a file the results const char * calcType = (calculatorType == 0) ? "Freq" : "Hybr"; const char * limitType = (useCls) ? "CLs" : "Cls+b"; const char * scanType = (npoints < 0) ? "auto" : "grid"; TString resultFileName = TString::Format("%s_%s_%s_ts%d_",calcType,limitType,scanType,testStatType); resultFileName += fileName; TFile * fileOut = new TFile(resultFileName,"RECREATE"); r->Write(); fileOut->Close(); } }
void RooStats::HypoTestInvTool::AnalyzeResult( HypoTestInverterResult * r, int calculatorType, int testStatType, bool useCLs, int npoints, const char * fileNameBase ){ // analyize result produced by the inverter, optionally save it in a file double upperLimit = r->UpperLimit(); double ulError = r->UpperLimitEstimatedError(); std::cout << "The computed upper limit is: " << upperLimit << " +/- " << ulError << std::endl; // compute expected limit std::cout << " expected limit (median) " << r->GetExpectedUpperLimit(0) << std::endl; std::cout << " expected limit (-1 sig) " << r->GetExpectedUpperLimit(-1) << std::endl; std::cout << " expected limit (+1 sig) " << r->GetExpectedUpperLimit(1) << std::endl; std::cout << " expected limit (-2 sig) " << r->GetExpectedUpperLimit(-2) << std::endl; std::cout << " expected limit (+2 sig) " << r->GetExpectedUpperLimit(2) << std::endl; // write result in a file if (r != NULL && mWriteResult) { // write to a file the results const char * calcType = (calculatorType == 0) ? "Freq" : (calculatorType == 1) ? "Hybr" : "Asym"; const char * limitType = (useCLs) ? "CLs" : "Cls+b"; const char * scanType = (npoints < 0) ? "auto" : "grid"; TString resultFileName = TString::Format("%s_%s_%s_ts%d_",calcType,limitType,scanType,testStatType); //strip the / from the filename if (mMassValue.size()>0) { resultFileName += mMassValue.c_str(); resultFileName += "_"; } TString name = fileNameBase; name.Replace(0, name.Last('/')+1, ""); resultFileName += name; TFile * fileOut = new TFile(resultFileName,"RECREATE"); r->Write(); fileOut->Close(); } // plot the result ( p values vs scan points) std::string typeName = ""; if (calculatorType == 0 ) typeName = "Frequentist"; if (calculatorType == 1 ) typeName = "Hybrid"; else if (calculatorType == 2 ) { typeName = "Asymptotic"; mPlotHypoTestResult = false; } const char * resultName = r->GetName(); TString plotTitle = TString::Format("%s CL Scan for workspace %s",typeName.c_str(),resultName); HypoTestInverterPlot *plot = new HypoTestInverterPlot("HTI_Result_Plot",plotTitle,r); plot->Draw("CLb 2CL"); // plot all and Clb const int nEntries = r->ArraySize(); // plot test statistics distributions for the two hypothesis if (mPlotHypoTestResult) { TCanvas * c2 = new TCanvas(); if (nEntries > 1) { int ny = TMath::CeilNint( sqrt(nEntries) ); int nx = TMath::CeilNint(double(nEntries)/ny); c2->Divide( nx,ny); } for (int i=0; i<nEntries; i++) { if (nEntries > 1) c2->cd(i+1); SamplingDistPlot * pl = plot->MakeTestStatPlot(i); pl->SetLogYaxis(true); pl->Draw(); } } }
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"); }