// @(#)root/roostats:$Id$
// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke, Sven Kreiss
/*************************************************************************
 * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOSTATS_ModelConfig
#define ROOSTATS_ModelConfig


#ifndef ROO_ABS_PDF
#include "RooAbsPdf.h"
#endif

#ifndef ROO_ABS_DATA
#include "RooAbsData.h"
#endif

#ifndef ROO_ARG_SET
#include "RooArgSet.h"
#endif

#ifndef ROO_WORKSPACE
#include "RooWorkspace.h"
#endif

#ifndef ROOT_TRef
#include "TRef.h"
#endif

#include <string>

//_________________________________________________
/*
BEGIN_HTML
<p>
ModelConfig is a simple class that holds configuration information specifying how a model
should be used in the context of various RooStats tools.  A single model can be used
in different ways, and this class should carry all that is needed to specify how it should be used.
ModelConfig requires a workspace to be set.
</p>
END_HTML
*/
//


namespace RooStats {

class ModelConfig : public TNamed {

public:

   ModelConfig(RooWorkspace * ws = 0) : 
      TNamed()
   {
      if(ws) SetWS(*ws);
   }
    
   ModelConfig(const char* name, RooWorkspace *ws = 0) : 
      TNamed(name, name)
   {
      if(ws) SetWS(*ws);
   }
    
   ModelConfig(const char* name, const char* title, RooWorkspace *ws = 0) : 
      TNamed(name, title)
   {
      if(ws) SetWS(*ws);
   }

    
   // clone
   virtual ModelConfig * Clone(const char * name = "") const {
      ModelConfig * mc =  new ModelConfig(*this);
      if(strcmp(name,"")==0)
	mc->SetName(this->GetName());
      else
	mc->SetName(name); 
      return mc; 
   }

   // set a workspace that owns all the necessary components for the analysis
   virtual void SetWS(RooWorkspace & ws);
   /// alias for SetWS(...)
   virtual void SetWorkspace(RooWorkspace & ws) { SetWS(ws); }

   // Set the proto DataSet, add to the the workspace if not already there
   virtual void SetProtoData(RooAbsData & data) {      
      ImportDataInWS(data); 
      SetProtoData( data.GetName() );
   }
    
   // Set the Pdf, add to the the workspace if not already there
   virtual void SetPdf(const RooAbsPdf& pdf) {
      ImportPdfInWS(pdf);
      SetPdf( pdf.GetName() );      
   }

   // Set the Prior Pdf, add to the the workspace if not already there
   virtual void SetPriorPdf(const RooAbsPdf& pdf) {
      ImportPdfInWS(pdf);
      SetPriorPdf( pdf.GetName() );      
   }
    
   // specify the parameters of interest in the interval
   virtual void SetParameters(const RooArgSet& set) {
     if (!SetHasOnlyParameters(set,"ModelConfig::SetParameters")) return ;
     fPOIName=std::string(GetName()) + "_POI";
     DefineSetInWS(fPOIName.c_str(), set);
   }

   virtual void SetParametersOfInterest(const RooArgSet& set) {
     if (!SetHasOnlyParameters(set,"ModelConfig::SetParametersOfInterest")) return ;
      SetParameters(set); 
   }
   // specify the parameters of interest 
   // through a list of comma-separated arguments already in the workspace
   virtual void SetParameters(const char *argList) {
      if(!GetWS()) return;
      SetParameters(GetWS()->argSet(argList));
   }
   virtual void SetParametersOfInterest(const char *argList) {
      SetParameters(argList);
   }
    
   // specify the nuisance parameters (e.g. the rest of the parameters)
   virtual void SetNuisanceParameters(const RooArgSet& set) {
     if (!SetHasOnlyParameters(set,"ModelConfig::SetNuisanceParameters")) return ;
      fNuisParamsName=std::string(GetName()) + "_NuisParams";
      DefineSetInWS(fNuisParamsName.c_str(), set);
   }
   // specify the nuisance parameters 
   // through a list of comma-separated arguments already in the workspace
   virtual void SetNuisanceParameters(const char *argList) {
      if(!GetWS()) return;
      SetNuisanceParameters(GetWS()->argSet(argList));
   }

   // specify the constraint parameters 
   virtual void SetConstraintParameters(const RooArgSet& set) {
     if (!SetHasOnlyParameters(set,"ModelConfig::SetConstainedParameters")) return ;
      fConstrParamsName=std::string(GetName()) + "_ConstrainedParams";
      DefineSetInWS(fConstrParamsName.c_str(), set);
   }
   // specify the constraint parameters 
   // through a list of comma-separated arguments already in the workspace
   virtual void SetConstraintParameters(const char *argList) {
      if(!GetWS()) return;
      SetConstraintParameters(GetWS()->argSet(argList));
   }

   // specify the observables
   virtual void SetObservables(const RooArgSet& set) {
     if (!SetHasOnlyParameters(set,"ModelConfig::SetObservables")) return ;
      fObservablesName=std::string(GetName()) + "_Observables";
      DefineSetInWS(fObservablesName.c_str(), set);
   }
   // specify the observables 
   // through a list of comma-separated arguments already in the workspace
   virtual void SetObservables(const char *argList) {
      if(!GetWS()) return;
      SetObservables(GetWS()->argSet(argList));
   }

   // specify the conditional observables
   virtual void SetConditionalObservables(const RooArgSet& set) {
     if (!SetHasOnlyParameters(set,"ModelConfig::SetConditionalObservables")) return ;
      fConditionalObsName=std::string(GetName()) + "_ConditionalObservables";
      DefineSetInWS(fConditionalObsName.c_str(), set);
   }
   // specify the conditional observables
   // through a list of comma-separated arguments already in the workspace
   virtual void SetConditionalObservables(const char *argList) {
      if(!GetWS()) return;
      SetConditionalObservables(GetWS()->argSet(argList));
   }

   // specify the global observables
   virtual void SetGlobalObservables(const RooArgSet& set) {

     if (!SetHasOnlyParameters(set,"ModelConfig::SetGlobalObservables")) return ;

      // make global observables constant
      RooFIter iter = set.fwdIterator();
      RooAbsArg *arg = iter.next();
      while(arg != NULL) {
         arg->setAttribute("Constant", kTRUE);
         arg = iter.next();
      }

      fGlobalObsName=std::string(GetName()) + "_GlobalObservables";
      DefineSetInWS(fGlobalObsName.c_str(), set);
   }
   // specify the global observables 
   // through a list of comma-separated arguments already in the workspace
   virtual void SetGlobalObservables(const char *argList) {
      if(!GetWS()) return;
      SetGlobalObservables(GetWS()->argSet(argList));
   }

   // set parameter values for a particular hypothesis if using a common PDF
   // by saving a snapshot in the workspace
   virtual void SetSnapshot(const RooArgSet& set);
    
   // specify the name of the PDF in the workspace to be used
   virtual void SetPdf(const char* name) {
      if (! GetWS() ) return;

      if(GetWS()->pdf(name))
         fPdfName = name;
      else
         coutE(ObjectHandling) << "pdf "<<name<< " does not exist in workspace"<<std::endl;
   }

   // specify the name of the PDF in the workspace to be used
   virtual void SetPriorPdf(const char* name) {
      if (! GetWS() ) return;

      if(GetWS()->pdf(name))
         fPriorPdfName = name;
      else
         coutE(ObjectHandling) << "pdf "<<name<< " does not exist in workspace"<<std::endl;
   }


   // specify the name of the dataset in the workspace to be used
   virtual void SetProtoData(const char* name){
      if (! GetWS() ) return;

      if(GetWS()->data(name))
         fProtoDataName = name;
      else
         coutE(ObjectHandling) << "dataset "<<name<< " does not exist in workspace"<<std::endl;
   }


   /* getter methods */


   /// get model PDF (return NULL if pdf has not been specified or does not exist)
   RooAbsPdf * GetPdf() const { return (GetWS()) ? GetWS()->pdf(fPdfName.c_str()) : 0;   }

   /// get RooArgSet containing the parameter of interest (return NULL if not existing) 
   const RooArgSet * GetParametersOfInterest() const { return (GetWS()) ? GetWS()->set(fPOIName.c_str()) : 0; }

   /// get RooArgSet containing the nuisance parameters (return NULL if not existing) 
   const RooArgSet * GetNuisanceParameters() const { return (GetWS()) ? GetWS()->set(fNuisParamsName.c_str()) : 0; }

   /// get RooArgSet containing the constraint parameters (return NULL if not existing) 
   const RooArgSet * GetConstraintParameters() const { return (GetWS()) ? GetWS()->set(fConstrParamsName.c_str()) : 0; }

   /// get parameters prior pdf  (return NULL if not existing) 
   RooAbsPdf * GetPriorPdf() const { return (GetWS()) ? GetWS()->pdf(fPriorPdfName.c_str()) : 0; }

   /// get RooArgSet for observables  (return NULL if not existing)
   const RooArgSet * GetObservables() const { return (GetWS()) ? GetWS()->set(fObservablesName.c_str()) : 0; }

   /// get RooArgSet for conditional observables  (return NULL if not existing)
   const RooArgSet * GetConditionalObservables() const { return (GetWS()) ? GetWS()->set(fConditionalObsName.c_str()) : 0; }

   /// get RooArgSet for global observables  (return NULL if not existing)
   const RooArgSet * GetGlobalObservables() const { return (GetWS()) ? GetWS()->set(fGlobalObsName.c_str()) : 0; }

   /// get Proto data set (return NULL if not existing) 
   RooAbsData * GetProtoData()  const {  return (GetWS()) ? GetWS()->data(fProtoDataName.c_str()) : 0; }

   /// get RooArgSet for parameters for a particular hypothesis  (return NULL if not existing) 
   const RooArgSet * GetSnapshot() const;

   void LoadSnapshot() const;
 
   RooWorkspace * GetWS() const;
   /// alias for GetWS()
   RooWorkspace * GetWorkspace() const { return GetWS(); }

   /// guesses Observables and ParametersOfInterest if not already set
   void GuessObsAndNuisance(const RooAbsData& data);

   // overload the print method
   virtual void Print(Option_t* option = "") const;

protected:

   // helper function to check that content of a given set is exclusively parameters
   Bool_t SetHasOnlyParameters(const RooArgSet& set, const char* errorMsgPrefix=0) ;

   // helper functions to define a set in the WS
   void DefineSetInWS(const char* name, const RooArgSet& set);
    
   // internal function to import Pdf in WS
   void ImportPdfInWS(const RooAbsPdf & pdf);
      
   // internal function to import data in WS
   void ImportDataInWS(RooAbsData & data); 
    
   TRef fRefWS;  // WS reference used in the file

   std::string fWSName;  // name of the WS

   std::string fPdfName; // name of  PDF in workspace
   std::string fDataName; // name of data set in workspace
   std::string fPOIName; // name for RooArgSet specifying parameters of interest
    
   std::string fNuisParamsName; // name for RooArgSet specifying nuisance parameters
   std::string fConstrParamsName; // name for RooArgSet specifying constrained parameters
   std::string fPriorPdfName; // name for RooAbsPdf specifying a prior on the parameters
    
   std::string fConditionalObsName; // name for RooArgSet specifying conditional observables
   std::string fGlobalObsName; // name for RooArgSet specifying global observables
   std::string fProtoDataName; // name for RooArgSet specifying dataset that should be used as protodata
    
   std::string fSnapshotName; // name for RooArgSet that specifies a particular hypothesis
    
   std::string fObservablesName; // name for RooArgSet specifying observable parameters. 
    
   ClassDef(ModelConfig,4) // A class that holds configuration information for a model using a workspace as a store
      
};

}   // end namespace RooStats


#endif
 ModelConfig.h:1
 ModelConfig.h:2
 ModelConfig.h:3
 ModelConfig.h:4
 ModelConfig.h:5
 ModelConfig.h:6
 ModelConfig.h:7
 ModelConfig.h:8
 ModelConfig.h:9
 ModelConfig.h:10
 ModelConfig.h:11
 ModelConfig.h:12
 ModelConfig.h:13
 ModelConfig.h:14
 ModelConfig.h:15
 ModelConfig.h:16
 ModelConfig.h:17
 ModelConfig.h:18
 ModelConfig.h:19
 ModelConfig.h:20
 ModelConfig.h:21
 ModelConfig.h:22
 ModelConfig.h:23
 ModelConfig.h:24
 ModelConfig.h:25
 ModelConfig.h:26
 ModelConfig.h:27
 ModelConfig.h:28
 ModelConfig.h:29
 ModelConfig.h:30
 ModelConfig.h:31
 ModelConfig.h:32
 ModelConfig.h:33
 ModelConfig.h:34
 ModelConfig.h:35
 ModelConfig.h:36
 ModelConfig.h:37
 ModelConfig.h:38
 ModelConfig.h:39
 ModelConfig.h:40
 ModelConfig.h:41
 ModelConfig.h:42
 ModelConfig.h:43
 ModelConfig.h:44
 ModelConfig.h:45
 ModelConfig.h:46
 ModelConfig.h:47
 ModelConfig.h:48
 ModelConfig.h:49
 ModelConfig.h:50
 ModelConfig.h:51
 ModelConfig.h:52
 ModelConfig.h:53
 ModelConfig.h:54
 ModelConfig.h:55
 ModelConfig.h:56
 ModelConfig.h:57
 ModelConfig.h:58
 ModelConfig.h:59
 ModelConfig.h:60
 ModelConfig.h:61
 ModelConfig.h:62
 ModelConfig.h:63
 ModelConfig.h:64
 ModelConfig.h:65
 ModelConfig.h:66
 ModelConfig.h:67
 ModelConfig.h:68
 ModelConfig.h:69
 ModelConfig.h:70
 ModelConfig.h:71
 ModelConfig.h:72
 ModelConfig.h:73
 ModelConfig.h:74
 ModelConfig.h:75
 ModelConfig.h:76
 ModelConfig.h:77
 ModelConfig.h:78
 ModelConfig.h:79
 ModelConfig.h:80
 ModelConfig.h:81
 ModelConfig.h:82
 ModelConfig.h:83
 ModelConfig.h:84
 ModelConfig.h:85
 ModelConfig.h:86
 ModelConfig.h:87
 ModelConfig.h:88
 ModelConfig.h:89
 ModelConfig.h:90
 ModelConfig.h:91
 ModelConfig.h:92
 ModelConfig.h:93
 ModelConfig.h:94
 ModelConfig.h:95
 ModelConfig.h:96
 ModelConfig.h:97
 ModelConfig.h:98
 ModelConfig.h:99
 ModelConfig.h:100
 ModelConfig.h:101
 ModelConfig.h:102
 ModelConfig.h:103
 ModelConfig.h:104
 ModelConfig.h:105
 ModelConfig.h:106
 ModelConfig.h:107
 ModelConfig.h:108
 ModelConfig.h:109
 ModelConfig.h:110
 ModelConfig.h:111
 ModelConfig.h:112
 ModelConfig.h:113
 ModelConfig.h:114
 ModelConfig.h:115
 ModelConfig.h:116
 ModelConfig.h:117
 ModelConfig.h:118
 ModelConfig.h:119
 ModelConfig.h:120
 ModelConfig.h:121
 ModelConfig.h:122
 ModelConfig.h:123
 ModelConfig.h:124
 ModelConfig.h:125
 ModelConfig.h:126
 ModelConfig.h:127
 ModelConfig.h:128
 ModelConfig.h:129
 ModelConfig.h:130
 ModelConfig.h:131
 ModelConfig.h:132
 ModelConfig.h:133
 ModelConfig.h:134
 ModelConfig.h:135
 ModelConfig.h:136
 ModelConfig.h:137
 ModelConfig.h:138
 ModelConfig.h:139
 ModelConfig.h:140
 ModelConfig.h:141
 ModelConfig.h:142
 ModelConfig.h:143
 ModelConfig.h:144
 ModelConfig.h:145
 ModelConfig.h:146
 ModelConfig.h:147
 ModelConfig.h:148
 ModelConfig.h:149
 ModelConfig.h:150
 ModelConfig.h:151
 ModelConfig.h:152
 ModelConfig.h:153
 ModelConfig.h:154
 ModelConfig.h:155
 ModelConfig.h:156
 ModelConfig.h:157
 ModelConfig.h:158
 ModelConfig.h:159
 ModelConfig.h:160
 ModelConfig.h:161
 ModelConfig.h:162
 ModelConfig.h:163
 ModelConfig.h:164
 ModelConfig.h:165
 ModelConfig.h:166
 ModelConfig.h:167
 ModelConfig.h:168
 ModelConfig.h:169
 ModelConfig.h:170
 ModelConfig.h:171
 ModelConfig.h:172
 ModelConfig.h:173
 ModelConfig.h:174
 ModelConfig.h:175
 ModelConfig.h:176
 ModelConfig.h:177
 ModelConfig.h:178
 ModelConfig.h:179
 ModelConfig.h:180
 ModelConfig.h:181
 ModelConfig.h:182
 ModelConfig.h:183
 ModelConfig.h:184
 ModelConfig.h:185
 ModelConfig.h:186
 ModelConfig.h:187
 ModelConfig.h:188
 ModelConfig.h:189
 ModelConfig.h:190
 ModelConfig.h:191
 ModelConfig.h:192
 ModelConfig.h:193
 ModelConfig.h:194
 ModelConfig.h:195
 ModelConfig.h:196
 ModelConfig.h:197
 ModelConfig.h:198
 ModelConfig.h:199
 ModelConfig.h:200
 ModelConfig.h:201
 ModelConfig.h:202
 ModelConfig.h:203
 ModelConfig.h:204
 ModelConfig.h:205
 ModelConfig.h:206
 ModelConfig.h:207
 ModelConfig.h:208
 ModelConfig.h:209
 ModelConfig.h:210
 ModelConfig.h:211
 ModelConfig.h:212
 ModelConfig.h:213
 ModelConfig.h:214
 ModelConfig.h:215
 ModelConfig.h:216
 ModelConfig.h:217
 ModelConfig.h:218
 ModelConfig.h:219
 ModelConfig.h:220
 ModelConfig.h:221
 ModelConfig.h:222
 ModelConfig.h:223
 ModelConfig.h:224
 ModelConfig.h:225
 ModelConfig.h:226
 ModelConfig.h:227
 ModelConfig.h:228
 ModelConfig.h:229
 ModelConfig.h:230
 ModelConfig.h:231
 ModelConfig.h:232
 ModelConfig.h:233
 ModelConfig.h:234
 ModelConfig.h:235
 ModelConfig.h:236
 ModelConfig.h:237
 ModelConfig.h:238
 ModelConfig.h:239
 ModelConfig.h:240
 ModelConfig.h:241
 ModelConfig.h:242
 ModelConfig.h:243
 ModelConfig.h:244
 ModelConfig.h:245
 ModelConfig.h:246
 ModelConfig.h:247
 ModelConfig.h:248
 ModelConfig.h:249
 ModelConfig.h:250
 ModelConfig.h:251
 ModelConfig.h:252
 ModelConfig.h:253
 ModelConfig.h:254
 ModelConfig.h:255
 ModelConfig.h:256
 ModelConfig.h:257
 ModelConfig.h:258
 ModelConfig.h:259
 ModelConfig.h:260
 ModelConfig.h:261
 ModelConfig.h:262
 ModelConfig.h:263
 ModelConfig.h:264
 ModelConfig.h:265
 ModelConfig.h:266
 ModelConfig.h:267
 ModelConfig.h:268
 ModelConfig.h:269
 ModelConfig.h:270
 ModelConfig.h:271
 ModelConfig.h:272
 ModelConfig.h:273
 ModelConfig.h:274
 ModelConfig.h:275
 ModelConfig.h:276
 ModelConfig.h:277
 ModelConfig.h:278
 ModelConfig.h:279
 ModelConfig.h:280
 ModelConfig.h:281
 ModelConfig.h:282
 ModelConfig.h:283
 ModelConfig.h:284
 ModelConfig.h:285
 ModelConfig.h:286
 ModelConfig.h:287
 ModelConfig.h:288
 ModelConfig.h:289
 ModelConfig.h:290
 ModelConfig.h:291
 ModelConfig.h:292
 ModelConfig.h:293
 ModelConfig.h:294
 ModelConfig.h:295
 ModelConfig.h:296
 ModelConfig.h:297
 ModelConfig.h:298
 ModelConfig.h:299
 ModelConfig.h:300
 ModelConfig.h:301
 ModelConfig.h:302
 ModelConfig.h:303
 ModelConfig.h:304
 ModelConfig.h:305
 ModelConfig.h:306
 ModelConfig.h:307
 ModelConfig.h:308
 ModelConfig.h:309
 ModelConfig.h:310
 ModelConfig.h:311
 ModelConfig.h:312
 ModelConfig.h:313
 ModelConfig.h:314
 ModelConfig.h:315
 ModelConfig.h:316
 ModelConfig.h:317
 ModelConfig.h:318
 ModelConfig.h:319
 ModelConfig.h:320
 ModelConfig.h:321
 ModelConfig.h:322
 ModelConfig.h:323
 ModelConfig.h:324
 ModelConfig.h:325
 ModelConfig.h:326
 ModelConfig.h:327