/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id: RooWorkspace.h,v 1.3 2007/07/16 21:04:28 wouter Exp $
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/
#ifndef ROO_WORKSPACE
#define ROO_WORKSPACE

#include "RooPrintable.h"
#include "RooArgSet.h"
#include "RooLinkedList.h"
#include "RooCmdArg.h"
#include "RooExpensiveObjectCache.h" 
#include "TUUID.h"
#include <map>
#include <list>
#include <string>

class TClass ;
class RooAbsPdf ;
class RooAbsData ;
class RooRealVar ;
class RooCategory ;
class RooAbsReal ;
class RooAbsCategory ;
class RooFactoryWSTool ;
class RooAbsStudy ;

#include "TNamed.h"
#include "TDirectoryFile.h"

class RooWorkspace : public TNamed {
public:

  RooWorkspace() ;
  RooWorkspace(const char* name, Bool_t doCINTExport) ;
  RooWorkspace(const char* name, const char* title=0) ;
  RooWorkspace(const RooWorkspace& other) ;
  ~RooWorkspace() ;

  void exportToCint(const char* namespaceName=0) ;

  Bool_t importClassCode(const char* pat="*", Bool_t doReplace=kFALSE) ;
  Bool_t importClassCode(TClass* theClass, Bool_t doReplace=kFALSE) ;

  // Import functions for dataset, functions, generic objects
  Bool_t import(const RooAbsArg& arg, 
		const RooCmdArg& arg1=RooCmdArg(),const RooCmdArg& arg2=RooCmdArg(),const RooCmdArg& arg3=RooCmdArg(),
		const RooCmdArg& arg4=RooCmdArg(),const RooCmdArg& arg5=RooCmdArg(),const RooCmdArg& arg6=RooCmdArg(),
		const RooCmdArg& arg7=RooCmdArg(),const RooCmdArg& arg8=RooCmdArg(),const RooCmdArg& arg9=RooCmdArg()) ;
  Bool_t import(const RooArgSet& args, 
		const RooCmdArg& arg1=RooCmdArg(),const RooCmdArg& arg2=RooCmdArg(),const RooCmdArg& arg3=RooCmdArg(),
		const RooCmdArg& arg4=RooCmdArg(),const RooCmdArg& arg5=RooCmdArg(),const RooCmdArg& arg6=RooCmdArg(),
		const RooCmdArg& arg7=RooCmdArg(),const RooCmdArg& arg8=RooCmdArg(),const RooCmdArg& arg9=RooCmdArg()) ;
  Bool_t import(RooAbsData& data, 
		const RooCmdArg& arg1=RooCmdArg(),const RooCmdArg& arg2=RooCmdArg(),const RooCmdArg& arg3=RooCmdArg(),
		const RooCmdArg& arg4=RooCmdArg(),const RooCmdArg& arg5=RooCmdArg(),const RooCmdArg& arg6=RooCmdArg(),
		const RooCmdArg& arg7=RooCmdArg(),const RooCmdArg& arg8=RooCmdArg(),const RooCmdArg& arg9=RooCmdArg()) ;
  Bool_t import(const char *fileSpec, 
		const RooCmdArg& arg1=RooCmdArg(),const RooCmdArg& arg2=RooCmdArg(),const RooCmdArg& arg3=RooCmdArg(),
		const RooCmdArg& arg4=RooCmdArg(),const RooCmdArg& arg5=RooCmdArg(),const RooCmdArg& arg6=RooCmdArg(),
		const RooCmdArg& arg7=RooCmdArg(),const RooCmdArg& arg8=RooCmdArg(),const RooCmdArg& arg9=RooCmdArg()) ;
  Bool_t import(TObject& object, Bool_t replaceExisting=kFALSE) ;
  Bool_t import(TObject& object, const char* aliasName, Bool_t replaceExisting=kFALSE) ;

  // Transaction management interface for multi-step import operations
  Bool_t startTransaction() ;
  Bool_t cancelTransaction() ;
  Bool_t commitTransaction() ;

  // Named set management
  Bool_t defineSet(const char* name, const RooArgSet& aset, Bool_t importMissing=kFALSE) ;
  Bool_t defineSet(const char* name, const char* contentList) ;
  Bool_t extendSet(const char* name, const char* newContents) ;
  Bool_t renameSet(const char* name, const char* newName) ;
  Bool_t removeSet(const char* name) ;
  const RooArgSet* set(const char* name) ;

  // Import, load and save parameter value snapshots
  Bool_t saveSnapshot(const char* name, const char* paramNames) ;
  Bool_t saveSnapshot(const char* name, const RooArgSet& params, Bool_t importValues=kFALSE) ;
  Bool_t loadSnapshot(const char* name) ;  
  const RooArgSet* getSnapshot(const char* name) const ;

  void merge(const RooWorkspace& /*other*/) {} ;

  // Join p.d.f.s and datasets for simultaneous analysis
  //   RooAbsPdf* joinPdf(const char* jointPdfName, const char* indexName, const char* inputMapping) ; 
  //   RooAbsData* joinData(const char* jointDataName, const char* indexName, const char* inputMapping) ; 

  // Accessor functions 
  RooAbsPdf* pdf(const char* name) const ;
  RooAbsReal* function(const char* name) const ;
  RooRealVar* var(const char* name) const ;
  RooCategory* cat(const char* name) const ;
  RooAbsCategory* catfunc(const char* name) const ;
  RooAbsData* data(const char* name) const ;
  RooAbsData* embeddedData(const char* name) const ;
  RooAbsArg* arg(const char* name) const ;
  RooAbsArg* fundArg(const char* name) const ;
  RooArgSet argSet(const char* nameList) const ;
  TIterator* componentIterator() const { return _allOwnedNodes.createIterator() ; }
  const RooArgSet& components() const { return _allOwnedNodes ; }
  TObject* genobj(const char* name) const ;
  TObject* obj(const char* name) const ;

  // Group accessors
  RooArgSet allVars() const;
  RooArgSet allCats() const ;
  RooArgSet allFunctions() const ;
  RooArgSet allCatFunctions() const ;
  RooArgSet allPdfs() const ;
  RooArgSet allResolutionModels() const ;
  std::list<RooAbsData*> allData() const ;
  std::list<RooAbsData*> allEmbeddedData() const ;
  std::list<TObject*> allGenericObjects() const ;

  Bool_t makeDir() ; 
  Bool_t cd(const char* path = 0) ;

  Bool_t writeToFile(const char* fileName, Bool_t recreate=kTRUE) ;


  // Tools management
  RooFactoryWSTool& factory() ;
  RooAbsArg* factory(const char* expr) ;

  // RooStudyManager modules
  Bool_t addStudy(RooAbsStudy& study) ;  
  TIterator* studyIterator() { return _studyMods.MakeIterator() ; }
  void clearStudies() ;

  // Print function
  void Print(Option_t* opts=0) const ;

  static void autoImportClassCode(Bool_t flag) ;
 
  static void addClassDeclImportDir(const char* dir) ;
  static void addClassImplImportDir(const char* dir) ;
  static void setClassFileExportDir(const char* dir=0) ; 

  const TUUID& uuid() const { return _uuid ; }

  RooExpensiveObjectCache& expensiveObjectCache() { return _eocache ; }

  class CodeRepo : public TObject {
  public:
    CodeRepo(RooWorkspace* wspace=0) : _wspace(wspace), _compiledOK(kTRUE) {} ;

    CodeRepo(const CodeRepo& other, RooWorkspace* wspace=0) : TObject(other) ,
          _wspace(wspace?wspace:other._wspace), 
          _c2fmap(other._c2fmap),
          _fmap(other._fmap),
          _ehmap(other._ehmap),
          _compiledOK(other._compiledOK) {} ;

    virtual ~CodeRepo() {} ;

    Bool_t autoImportClass(TClass* tc, Bool_t doReplace=kFALSE) ;
    Bool_t compileClasses() ;

    Bool_t compiledOK() const { return _compiledOK ; }

    std::string listOfClassNames() const ;

       

    class ClassRelInfo {
    public:
      TString _baseName;      
      TString _fileBase ;
    } ;

    class ClassFiles {
    public:
      ClassFiles() : _extracted(kFALSE) {}
      TString _hext ;
      TString _hfile ;
      TString _cxxfile ;
      Bool_t _extracted ; 
    } ;    
    

    class ExtraHeader {     
    public:
      TString _hname ;
      TString _hfile ;
   } ;

  protected:
    RooWorkspace* _wspace ; // owning workspace
    std::map<TString,ClassRelInfo> _c2fmap ; // List of contained classes
    std::map<TString,ClassFiles> _fmap ; // List of contained files
    std::map<TString,ExtraHeader> _ehmap ; // List of extra header files
    Bool_t _compiledOK ; //! Flag indicating that classes compiled OK

    ClassDef(CodeRepo,2) ; // Code repository for RooWorkspace
  } ;


  class WSDir : public TDirectoryFile {    
  public:
    WSDir(const char* name, const char* title, RooWorkspace* wspace) : 
      TDirectoryFile(name,title,"RooWorkspace::WSDir",0), 
      _wspace(wspace) 
      {
      }

    virtual ~WSDir() { Clear("nodelete") ; } ; 


#if ROOT_VERSION_CODE <= 332546
    virtual void Add(TObject*) ;
    virtual void Append(TObject*) ;
#else 
    virtual void Add(TObject*,Bool_t) ; 
    virtual void Append(TObject*,Bool_t) ; 
#endif 

  protected:
    friend class RooWorkspace ;
    void InternalAppend(TObject* obj) ;
    RooWorkspace* _wspace ; //! do not persist

    ClassDef(WSDir,1) ; // TDirectory representation of RooWorkspace
  } ;


 private:

  Bool_t isValidCPPID(const char* name) ;
  void exportObj(TObject* obj) ;
  void unExport() ;

  friend class CodeRepo ;
  static std::list<std::string> _classDeclDirList ;
  static std::list<std::string> _classImplDirList ;
  static std::string            _classFileExportDir ;

  TUUID       _uuid ;  // Unique workspace ID

  static Bool_t _autoClass ; // Automatic import of non-distribution class code
  
  CodeRepo _classes ; // Repository of embedded class code. This data member _must_ be first

  RooArgSet _allOwnedNodes ; // List of owned pdfs and components
  RooLinkedList _dataList ; // List of owned datasets
  RooLinkedList _embeddedDataList ; // List of owned datasets that are embedded in pdfs
  RooLinkedList _views ; // List of model views  
  RooLinkedList _snapshots ; // List of parameter snapshots
  RooLinkedList _genObjects ; // List of generic objects
  RooLinkedList _studyMods ; // List if StudyManager modules
  std::map<std::string,RooArgSet> _namedSets ; // Map of named RooArgSets

  WSDir* _dir ; //! Transient ROOT directory representation of workspace

  RooExpensiveObjectCache _eocache ; // Cache for expensive objects  

  RooFactoryWSTool* _factory ; //! Factory tool associated with workspace

  Bool_t      _doExport ;     //! Export contents of workspace to CINT?
  std::string _exportNSName ; //! Name of CINT namespace to which contents are exported

  Bool_t      _openTrans ;    //! Is there a transaction open?
  RooArgSet   _sandboxNodes ; //! Sandbox for incoming objects in a transaction

  ClassDef(RooWorkspace,8)  // Persistable project container for (composite) pdfs, functions, variables and datasets
  
} ;

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