ModelConfig.cxx

Go to the documentation of this file.
00001 // @(#)root/roostats:$Id: ModelConfig.cxx 37522 2010-12-10 16:31:00Z moneta $
00002 // Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke, Sven Kreiss
00003 /*************************************************************************
00004  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00005  * All rights reserved.                                                  *
00006  *                                                                       *
00007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00009  *************************************************************************/
00010 
00011 #include "RooStats/ModelConfig.h"
00012 
00013 #include "TROOT.h"
00014 
00015 #ifndef ROO_MSG_SERVICE
00016 #include "RooMsgService.h"
00017 #endif
00018 
00019 #ifndef RooStats_RooStatsUtils
00020 #include "RooStats/RooStatsUtils.h"
00021 #endif
00022 
00023 #include <sstream>
00024 
00025 
00026 ClassImp(RooStats::ModelConfig)
00027 
00028 
00029 namespace RooStats {
00030 
00031 void ModelConfig::GuessObsAndNuisance(const RooAbsData& data) {
00032    // Makes sensible guesses of observables, parameters of interest
00033    // and nuisance parameters.
00034    //
00035    // Defaults:
00036    //  observables: determined from data,
00037    //  global observables = explicit obs  -  obs from data
00038    //  parameters of interest: empty,
00039    //  nuisance parameters: all parameters except parameters of interest
00040   //
00041   // We use NULL to mean not set, so we don't want to fill
00042   // with empty RooArgSets
00043 
00044    // observables
00045   if (!GetObservables()) {
00046     SetObservables(*GetPdf()->getObservables(data));
00047     //const RooArgSet* temp = data.get();
00048     //     SetObservables(*(RooArgSet*)temp->snapshot());
00049     //     delete temp;
00050    }
00051    if (!GetGlobalObservables()) {
00052       RooArgSet co(*GetObservables());
00053       co.remove(*GetPdf()->getObservables(data));
00054       RemoveConstantParameters(&co);
00055       if(co.getSize()>0)
00056         SetGlobalObservables(co);
00057 
00058       // TODO BUG This does not work as observables with the same name are already in the workspace.
00059       /*
00060       RooArgSet o(*GetObservables());
00061       o.remove(co);
00062       SetObservables(o);
00063       */
00064    }
00065 
00066    // parameters
00067    //   if (!GetParametersOfInterest()) {
00068    //      SetParametersOfInterest(RooArgSet());
00069    //   }
00070    if (!GetNuisanceParameters()) {
00071       RooArgSet p(*GetPdf()->getParameters(data));
00072       p.remove(*GetParametersOfInterest());
00073       RemoveConstantParameters(&p);
00074       if(p.getSize()>0)
00075         SetNuisanceParameters(p);
00076    }
00077 
00078    Print();
00079 }
00080 
00081 void ModelConfig::Print(Option_t*) const {
00082    // print contents
00083    ccoutI(InputArguments) << endl << "=== Using the following for " << GetName() << " ===" << endl;
00084 
00085    // necessary so that GetObservables()->Print("") gets piped to the
00086    // ccoutI(InputArguments) stream
00087    ostream& oldstream = RooPrintable::defaultPrintStream(&ccoutI(InputArguments));
00088 
00089    // args
00090    if(GetObservables()){
00091       ccoutI(InputArguments) << "Observables:             ";
00092       GetObservables()->Print("");
00093    }
00094    if(GetParametersOfInterest()) {
00095       ccoutI(InputArguments) << "Parameters of Interest:  ";
00096       GetParametersOfInterest()->Print("");
00097    }
00098    if(GetNuisanceParameters()){
00099       ccoutI(InputArguments) << "Nuisance Parameters:     ";
00100       GetNuisanceParameters()->Print("");
00101    }
00102    if(GetGlobalObservables()){
00103       ccoutI(InputArguments) << "Global Observables:      ";
00104       GetGlobalObservables()->Print("");
00105    }
00106    if(GetConstraintParameters()){
00107       ccoutI(InputArguments) << "Constraint Parameters:   ";
00108       GetConstraintParameters()->Print("");
00109    }
00110    if(GetConditionalObservables()){
00111       ccoutI(InputArguments) << "Conditional Observables: ";
00112       GetConditionalObservables()->Print("");
00113    }
00114    if(GetProtoData()){
00115       ccoutI(InputArguments) << "Proto Data:              ";
00116       GetProtoData()->Print("");
00117    }
00118 
00119    // pdfs
00120    if(GetPdf()) {
00121       ccoutI(InputArguments) << "PDF:                     ";
00122       GetPdf()->Print("");
00123    }
00124    if(GetPriorPdf()) {
00125       ccoutI(InputArguments) << "Prior PDF:               ";
00126       GetPriorPdf()->Print("");
00127    }
00128 
00129    // snapshot
00130    if(GetSnapshot()) {
00131       ccoutI(InputArguments) << "Snapshot:                " << endl;
00132       GetSnapshot()->Print("v");
00133    }
00134 
00135    ccoutI(InputArguments) << endl;
00136    RooPrintable::defaultPrintStream(&oldstream);
00137 }
00138 
00139 
00140 void ModelConfig::SetWS(RooWorkspace & ws) {
00141    // set a workspace that owns all the necessary components for the analysis
00142    if( !fRefWS.GetObject() ) {
00143       fRefWS = &ws; 
00144       fWSName = ws.GetName();
00145    }   
00146    else{
00147       RooFit::MsgLevel level = RooMsgService::instance().globalKillBelow();
00148       RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR) ;
00149       GetWS()->merge(ws);
00150       RooMsgService::instance().setGlobalKillBelow(level) ;
00151    }
00152 }
00153 
00154 RooWorkspace * ModelConfig::GetWS() const {
00155    // get from TRef
00156    RooWorkspace *ws = dynamic_cast<RooWorkspace *>(fRefWS.GetObject() );
00157    if(!ws) {
00158       coutE(ObjectHandling) << "workspace not set" << endl;
00159       return NULL;
00160    }
00161    return ws;
00162 }
00163 
00164 void ModelConfig::SetSnapshot(const RooArgSet& set) {
00165    // save snaphot in the workspace 
00166    // and use values passed with the set
00167    if ( !GetWS() ) return;
00168 
00169    fSnapshotName = GetName();
00170    if (fSnapshotName.size()  > 0) fSnapshotName += "_";
00171    fSnapshotName += set.GetName();
00172    if (fSnapshotName.size()  > 0) fSnapshotName += "_";
00173    fSnapshotName += "snapshot";
00174    GetWS()->saveSnapshot(fSnapshotName.c_str(), set, true);  // import also the given parameter values
00175    DefineSetInWS(fSnapshotName.c_str(), set);
00176 }    
00177 
00178 const RooArgSet * ModelConfig::GetSnapshot() const{
00179    // Load the snapshot from ws and return the corresponding set with the snapshot values.
00180    // User must delete returned RooArgSet.
00181    if ( !GetWS() ) return 0;
00182    if (!fSnapshotName.length()) return 0;
00183    if (!(GetWS()->loadSnapshot(fSnapshotName.c_str())) ) return 0;
00184 
00185    return dynamic_cast<const RooArgSet*>(GetWS()->set(fSnapshotName.c_str() )->snapshot());
00186 }
00187 
00188 void ModelConfig::LoadSnapshot() const{
00189    // load the snapshot from ws if it exists
00190    if ( !GetWS() ) return;
00191 
00192    // kill output
00193    RooFit::MsgLevel level = RooMsgService::instance().globalKillBelow();
00194    RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR);
00195    GetWS()->loadSnapshot(fSnapshotName.c_str());
00196    RooMsgService::instance().setGlobalKillBelow(level);
00197 }
00198 
00199 void ModelConfig::DefineSetInWS(const char* name, const RooArgSet& set) {
00200    // helper functions to avoid code duplication
00201    if ( !GetWS() ) return;
00202 
00203    if ( ! GetWS()->set(name) ) {
00204       RooFit::MsgLevel level = RooMsgService::instance().globalKillBelow();
00205       RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR) ;
00206       // use option to import missing constituents
00207       // TODO IS THIS A BUG? if content with same name exist they will not be imported ?
00208       // See ModelConfig::GuessObsAndNuissance(...) for example of the problem.
00209 
00210       GetWS()->defineSet(name, set,true);
00211 
00212       RooMsgService::instance().setGlobalKillBelow(level) ;
00213    }
00214 }
00215    
00216 void ModelConfig::ImportPdfInWS(const RooAbsPdf & pdf) { 
00217    // internal function to import Pdf in WS 
00218    if ( !GetWS() ) return;
00219 
00220    if (! GetWS()->pdf( pdf.GetName() ) ){
00221       RooFit::MsgLevel level = RooMsgService::instance().globalKillBelow();
00222       RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR) ;
00223       GetWS()->import(pdf, RooFit::RecycleConflictNodes());
00224       RooMsgService::instance().setGlobalKillBelow(level) ;
00225    }
00226 }
00227    
00228 void ModelConfig::ImportDataInWS(RooAbsData & data) { 
00229    // internal function to import data in WS
00230    if ( !GetWS() ) return;
00231 
00232    if (! GetWS()->data( data.GetName() ) ){
00233       RooFit::MsgLevel level = RooMsgService::instance().globalKillBelow();
00234       RooMsgService::instance().setGlobalKillBelow(RooFit::ERROR) ;
00235       GetWS()->import(data);
00236       RooMsgService::instance().setGlobalKillBelow(level) ;
00237    }
00238 }
00239 
00240 
00241 } // end namespace RooStats

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