45 using namespace RooFit;
104 RooWorkspace* RooStats::HistFactory::MakeModelAndMeasurementFast( RooStats::HistFactory::Measurement& measurement ) {
107 RooWorkspace* ws = NULL;
108 TFile* outFile = NULL;
109 FILE* tableFile=NULL;
113 std::cout <<
"Making Model and Measurements (Fast) for measurement: " << measurement.GetName() << std::endl;
115 double lumiError = measurement.GetLumi()*measurement.GetLumiRelErr();
117 std::cout <<
"using lumi = " << measurement.GetLumi() <<
" and lumiError = " << lumiError
118 <<
" including bins between " << measurement.GetBinLow() <<
" and " << measurement.GetBinHigh() << std::endl;
119 std::cout <<
"fixing the following parameters:" << std::endl;
121 for(std::vector<std::string>::iterator itr=measurement.GetConstantParams().begin(); itr!=measurement.GetConstantParams().end(); ++itr){
122 std::cout <<
" " << *itr << std::endl;
125 std::string rowTitle = measurement.GetName();
127 std::vector<RooWorkspace*> channel_workspaces;
128 std::vector<std::string> channel_names;
131 std::string prefix = measurement.GetOutputFilePrefix();
137 size_t pos = prefix.rfind(
"/");
138 if (pos != std::string::npos) {
139 std::string outputDir = prefix.substr(0,pos);
140 std::cout <<
"Checking if output directory : " << outputDir <<
" - exists" << std::endl;
141 if (gSystem->OpenDirectory( outputDir.c_str() ) == 0 ) {
142 std::cout <<
"Output directory : " << outputDir <<
" - does not exist, try to create" << std::endl;
143 int success = gSystem->MakeDirectory( outputDir.c_str() );
145 std::string fullOutputDir = std::string(gSystem->pwd()) + std::string(
"/") + outputDir;
146 std::cout <<
"Error: Failed to make output directory: " << fullOutputDir << std::endl;
153 std::string outputFileName = measurement.GetOutputFilePrefix() +
"_" + measurement.GetName() +
".root";
154 std::cout <<
"Creating the output file: " << outputFileName << std::endl;
155 outFile =
new TFile(outputFileName.c_str(),
"recreate");
159 std::string tableFileName = measurement.GetOutputFilePrefix() +
"_results.table";
160 std::cout <<
"Creating the table file: " << tableFileName << std::endl;
161 tableFile = fopen( tableFileName.c_str(),
"a");
163 std::cout <<
"Creating the HistoToWorkspaceFactoryFast factory" << std::endl;
164 HistoToWorkspaceFactoryFast factory( measurement );
168 std::cout <<
"Setting preprocess functions" << std::endl;
169 factory.SetFunctionsToPreprocess( measurement.GetPreprocessFunctions() );
172 fprintf(tableFile,
" %s &", rowTitle.c_str() );
175 for(
unsigned int chanItr = 0; chanItr < measurement.GetChannels().size(); ++chanItr ) {
177 HistFactory::Channel& channel = measurement.GetChannels().at( chanItr );
178 if( ! channel.CheckHistograms() ) {
179 std::cout <<
"MakeModelAndMeasurementsFast: Channel: " << channel.GetName()
180 <<
" has uninitialized histogram pointers" << std::endl;
185 std::string ch_name = channel.GetName();
186 std::cout <<
"Starting to process channel: " << ch_name << std::endl;
187 channel_names.push_back(ch_name);
188 RooWorkspace* ws_single = factory.MakeSingleChannelModel( measurement, channel );
189 channel_workspaces.push_back(ws_single);
192 std::string ChannelFileName = measurement.GetOutputFilePrefix() +
"_"
193 + ch_name +
"_" + rowTitle +
"_model.root";
194 ws_single->writeToFile( ChannelFileName.c_str() );
198 RooStats::HistFactory::Measurement meas_chan( measurement );
199 meas_chan.GetChannels().clear();
200 meas_chan.GetChannels().push_back( channel );
201 std::cout <<
"Opening File to hold channel: " << ChannelFileName << std::endl;
202 TFile* chanFile = TFile::Open( ChannelFileName.c_str(),
"UPDATE" );
203 std::cout <<
"About to write channel measurement to file" << std::endl;
204 meas_chan.writeToFile( chanFile );
205 std::cout <<
"Successfully wrote channel to file" << std::endl;
209 RooRealVar* poi =
dynamic_cast<RooRealVar*
>( ws_single->var( (measurement.GetPOI()).c_str() ) );
212 if(! measurement.GetExportOnly()){
214 std::cout <<
"Can't do fit for: " << measurement.GetName()
215 <<
", no parameter of interest" << std::endl;
217 if(ws_single->data(
"obsData")) {
218 FitModelAndPlot(measurement.GetName(), measurement.GetOutputFilePrefix(), ws_single,
219 ch_name,
"obsData", outFile, tableFile);
221 FitModelAndPlot(measurement.GetName(), measurement.GetOutputFilePrefix(), ws_single,
222 ch_name,
"asimovData", outFile, tableFile);
227 fprintf(tableFile,
" & " );
237 ws = factory.MakeCombinedModel(channel_names, channel_workspaces);
240 HistoToWorkspaceFactoryFast::ConfigureWorkspaceForMeasurement(
"simPdf", ws, measurement );
243 RooRealVar* poi =
dynamic_cast<RooRealVar*
>( ws->var( (measurement.GetPOI()).c_str() ) );
245 std::string CombinedFileName = measurement.GetOutputFilePrefix() +
"_combined_"
246 + rowTitle +
"_model.root";
247 std::cout <<
"Writing combined workspace to file: " << CombinedFileName << std::endl;
248 ws->writeToFile( CombinedFileName.c_str() );
249 std::cout <<
"Writing combined measurement to file: " << CombinedFileName << std::endl;
250 TFile* combFile = TFile::Open( CombinedFileName.c_str(),
"UPDATE" );
251 if( combFile == NULL ) {
252 std::cout <<
"Error: Failed to open file " << CombinedFileName << std::endl;
255 measurement.writeToFile( combFile );
259 if(! measurement.GetExportOnly()){
261 std::cout <<
"Can't do fit for: " << measurement.GetName()
262 <<
", no parameter of interest" << std::endl;
265 if(ws->data(
"obsData")){
266 FitModelAndPlot(measurement.GetName(), measurement.GetOutputFilePrefix(), ws,
"combined",
267 "obsData", outFile, tableFile);
270 FitModelAndPlot(measurement.GetName(), measurement.GetOutputFilePrefix(), ws,
"combined",
271 "asimovData", outFile, tableFile);
276 fprintf(tableFile,
" \\\\ \n");
285 if( tableFile ) fclose(tableFile);
286 if(outFile) outFile->Close();
296 void RooStats::HistFactory::FitModelAndPlot(
const std::string& MeasurementName,
297 const std::string& FileNamePrefix,
298 RooWorkspace * combined, std::string channel,
299 std::string data_name,
300 TFile* outFile, FILE* tableFile ) {
302 if( outFile == NULL ) {
303 std::cout <<
"Error: Output File in FitModelAndPlot is NULL" << std::endl;
307 if( tableFile == NULL ) {
308 std::cout <<
"Error: tableFile in FitModelAndPlot is NULL" << std::endl;
312 if( combined == NULL ) {
313 std::cout <<
"Error: Supplied workspace in FitModelAndPlot is NULL" << std::endl;
317 ModelConfig* combined_config = (ModelConfig *) combined->obj(
"ModelConfig");
318 if(!combined_config){
319 std::cout <<
"Error: no ModelConfig found in Measurement: "
320 << MeasurementName << std::endl;
324 RooAbsData* simData = combined->data(data_name.c_str());
326 std::cout <<
"Error: Failed to get dataset: " << data_name
327 <<
" in measurement: " << MeasurementName << std::endl;
331 const RooArgSet* POIs = combined_config->GetParametersOfInterest();
333 std::cout <<
"Not Fitting Model for measurement: " << MeasurementName
334 <<
", no poi found" << std::endl;
339 RooAbsPdf* model = combined_config->GetPdf();
341 std::cout <<
"Error: Failed to find pdf in ModelConfig: " << combined_config->GetName()
347 RooArgSet PoiPlusNuisance;
348 if( combined_config->GetNuisanceParameters() ) {
349 PoiPlusNuisance.add( *combined_config->GetNuisanceParameters() );
351 PoiPlusNuisance.add( *combined_config->GetParametersOfInterest() );
352 combined->saveSnapshot(
"InitialValues", PoiPlusNuisance);
356 std::cout <<
"\n\n---------------" << std::endl;
357 std::cout <<
"---------------- Doing "<< channel <<
" Fit" << std::endl;
358 std::cout <<
"---------------\n\n" << std::endl;
359 model->fitTo(*simData, Minos(kTRUE), PrintLevel(1));
363 if( POIs->getSize()==0 ) {
364 std::cout <<
"WARNING: No POIs found in measurement: " << MeasurementName << std::endl;
369 RooRealVar* poi = NULL;
370 TIterator* params_itr = POIs->createIterator();
371 TObject* poi_obj=NULL;
372 while( (poi_obj=params_itr->Next()) ) {
374 poi =
dynamic_cast<RooRealVar*
>(poi_obj);
375 std::cout <<
"printing results for " << poi->GetName()
376 <<
" at " << poi->getVal()<<
" high "
377 << poi->getErrorLo() <<
" low "
378 << poi->getErrorHi() << std::endl;
383 poi =
dynamic_cast<RooRealVar*
>(POIs->first());
386 fprintf(tableFile,
" %.4f / %.4f ", poi->getErrorLo(), poi->getErrorHi());
389 RooAbsReal* nll = model->createNLL(*simData);
390 RooAbsReal* profile = nll->createProfile(*poi);
391 if( profile==NULL ) {
392 std::cout <<
"Error: Failed to make ProfileLikelihood for: " << poi->GetName()
393 <<
" using model: " << model->GetName()
394 <<
" and data: " << simData->GetName()
399 RooPlot* frame = poi->frame();
400 if( frame == NULL ) {
401 std::cout <<
"Error: Failed to create RooPlot frame for: " << poi->GetName() << std::endl;
406 FormatFrameForLikelihood(frame);
407 TCanvas* ProfileLikelihoodCanvas =
new TCanvas( channel.c_str(),
"",800,600);
408 nll->plotOn(frame, ShiftToZero(), LineColor(kRed), LineStyle(kDashed));
409 profile->plotOn(frame);
410 frame->SetMinimum(0);
411 frame->SetMaximum(2.);
413 std::string ProfilePlotName = FileNamePrefix+
"_"+channel+
"_"+MeasurementName+
"_profileLR.eps";
414 ProfileLikelihoodCanvas->SaveAs( ProfilePlotName.c_str() );
415 delete ProfileLikelihoodCanvas;
423 TDirectory* channel_dir = outFile->mkdir(channel.c_str());
424 if( channel_dir == NULL ) {
425 std::cout <<
"Error: Failed to make channel directory: " << channel << std::endl;
428 TDirectory* summary_dir = channel_dir->mkdir(
"Summary");
429 if( summary_dir == NULL ) {
430 std::cout <<
"Error: Failed to make Summary directory for channel: "
431 << channel << std::endl;
437 RooCurve* curve=frame->getCurve();
438 Int_t curve_N=curve->GetN();
439 Double_t* curve_x=curve->GetX();
441 Double_t * x_arr =
new Double_t[curve_N];
442 Double_t * y_arr_nll =
new Double_t[curve_N];
444 for(
int i=0; i<curve_N; i++){
448 y_arr_nll[i]=nll->getVal();
453 TGraph* g =
new TGraph(curve_N, x_arr, y_arr_nll);
454 g->SetName( (FileNamePrefix +
"_nll").c_str() );
461 combined->loadSnapshot(
"InitialValues");
466 void RooStats::HistFactory::FitModel(RooWorkspace * combined, std::string data_name ) {
468 std::cout <<
"In Fit Model" << std::endl;
469 ModelConfig * combined_config = (ModelConfig *) combined->obj(
"ModelConfig");
470 if(!combined_config){
471 std::cout <<
"no model config " <<
"ModelConfig" <<
" exiting" << std::endl;
475 RooAbsData* simData = combined->data(data_name.c_str());
477 std::cout <<
"no data " << data_name <<
" exiting" << std::endl;
481 const RooArgSet * POIs=combined_config->GetParametersOfInterest();
483 std::cout <<
"no poi " << data_name <<
" exiting" << std::endl;
487 RooAbsPdf* model=combined_config->GetPdf();
488 model->fitTo(*simData, Minos(kTRUE), PrintLevel(1));
493 void RooStats::HistFactory::FormatFrameForLikelihood(RooPlot* frame, std::string ,
496 gStyle->SetCanvasBorderMode(0);
497 gStyle->SetPadBorderMode(0);
504 RooAbsRealLValue* var = frame->getPlotVar();
505 double xmin = var->getMin();
506 double xmax = var->getMax();
510 frame->GetXaxis()->SetTitle(var->GetTitle());
511 frame->GetYaxis()->SetTitle(YTitle.c_str());
512 frame->SetMaximum(2.);
513 frame->SetMinimum(0.);
514 TLine * line =
new TLine(xmin,.5,xmax,.5);
515 line->SetLineColor(kGreen);
516 TLine * line90 =
new TLine(xmin,2.71/2.,xmax,2.71/2.);
517 line90->SetLineColor(kGreen);
518 TLine * line95 =
new TLine(xmin,3.84/2.,xmax,3.84/2.);
519 line95->SetLineColor(kGreen);
520 frame->addObject(line);
521 frame->addObject(line90);
522 frame->addObject(line95);