ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * 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)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// RooStudyPackage is a utility class to manage studies that consist of
// repeated applications of generate-and-fit operations on a workspace
//
// END_HTML
//



#include "RooFit.h"
#include "Riostream.h"

#include "RooStudyPackage.h"
#include "RooWorkspace.h"
#include "RooAbsStudy.h"
#include "RooDataSet.h"
#include "RooMsgService.h"
#include "TProof.h"
#include "TTree.h"
#include "TDSet.h"
#include "TFile.h"
#include "TRandom2.h"
#include "RooRandom.h"
#include "TMath.h"
#include "TEnv.h"

using namespace std ;

ClassImp(RooStudyPackage)
  ;



//_____________________________________________________________________________
RooStudyPackage::RooStudyPackage() : _ws(0)
{  
}



//_____________________________________________________________________________
RooStudyPackage::RooStudyPackage(RooWorkspace& w) : _ws(new RooWorkspace(w))
{  
}



//_____________________________________________________________________________
RooStudyPackage::RooStudyPackage(const RooStudyPackage& other) : TNamed(other), _ws(new RooWorkspace(*other._ws))
{      
  list<RooAbsStudy*>::const_iterator iter = other._studies.begin() ;
  for (;iter!=other._studies.end() ; ++iter) {
    _studies.push_back((*iter)->clone()) ;
  }
}



//_____________________________________________________________________________
void RooStudyPackage::addStudy(RooAbsStudy& study) 
{
  _studies.push_back(&study) ;
}



//_____________________________________________________________________________
void RooStudyPackage::driver(Int_t nExperiments)
{
  initialize() ;
  run(nExperiments) ;
  finalize() ;
} 



//_____________________________________________________________________________
void RooStudyPackage::initialize() 
{
  // Make iterator over copy of studies attached to workspace
  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
    (*iter)->attach(*_ws) ;
    (*iter)->initialize() ;
  }

}


//_____________________________________________________________________________
void RooStudyPackage::run(Int_t nExperiments) 
{

  // Run the requested number of experiments
  Int_t prescale = nExperiments>100 ? Int_t(nExperiments/100) : 1 ;
  for (Int_t i=0 ; i<nExperiments ; i++) {
    if (i%prescale==0) {
      coutP(Generation) << "RooStudyPackage::run(" << GetName() << ") processing experiment " << i << "/" << nExperiments << endl ;
    }    
    runOne() ;
  }
}



//_____________________________________________________________________________
void RooStudyPackage::runOne() 
{
  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
    (*iter)->execute() ;
  }    
}




//_____________________________________________________________________________
void RooStudyPackage::finalize() 
{   
  // Finalize all studies
  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {
    (*iter)->finalize() ;
  }
}




//_____________________________________________________________________________
void RooStudyPackage::exportData(TList* olist, Int_t seqno)
{
  for (list<RooAbsStudy*>::iterator iter=_studies.begin() ; iter!=_studies.end() ; iter++) {

    (*iter)->finalize() ;

    RooDataSet* summaryData = (*iter)->summaryData() ;
    if (summaryData) {
      summaryData->SetName(Form("%s_%d",summaryData->GetName(),seqno)) ;
      cout << "registering summary dataset: " ; summaryData->Print() ;
      olist->Add(summaryData) ;
    }

    RooLinkedList* detailedData = (*iter)->detailedData() ;
    if (detailedData && detailedData->GetSize()>0) {

      detailedData->SetName(Form("%s_%d",detailedData->GetName(),seqno)) ;
      cout << "registering detailed dataset " << detailedData->IsA()->GetName() << "::" 
	   << detailedData->GetName() << " with " << detailedData->GetSize() << " elements" << endl ;
      TIterator* diter = detailedData->MakeIterator() ;
      TNamed* dobj ;
      while((dobj=(TNamed*)diter->Next())) {
	dobj->SetName(Form("%s_%d",dobj->GetName(),seqno)) ;
      }
      delete diter ;
      olist->Add(detailedData) ;
      (*iter)->releaseDetailData() ;
    }
  }        
}



//_____________________________________________________________________________
Int_t RooStudyPackage::initRandom()
{
  // Choose random seed for this process
  // in case pass a definite seed to have it deterministic
  // use also worker number
  TRandom2 random(0); 
  //gRandom->SetSeed(0) ;
  Int_t seed = random.Integer(TMath::Limits<Int_t>::Max()) ;

  // get worker number 
  TString  worknumber = gEnv->GetValue("ProofServ.Ordinal","undef");
  int iworker = -1; 
  if (worknumber != "undef") 
     iworker = int( worknumber.Atof()*10 + 0.1);

  if (iworker >= 0)  {
     for (int i = 0; i <= iworker; ++i ) 
        seed = random.Integer( TMath::Limits<Int_t>::Max() ); 
  }

  RooRandom::randomGenerator()->SetSeed(seed) ;
  gRandom->SetSeed(seed) ;

  return seed ;
}



//_____________________________________________________________________________
void RooStudyPackage::processFile(const char* studyName, Int_t nexp) 
{
  // Read in study package
  string name_fin = Form("study_data_%s.root",studyName) ;
  TFile fin(name_fin.c_str()) ;
  RooStudyPackage* pkg = dynamic_cast<RooStudyPackage*>(fin.Get("studypack")) ;
  if (!pkg) {
    cout << "RooStudyPackage::processFile() ERROR input file " << name_fin << " does not contain a RooStudyPackage named 'studypack'" << endl ;
    return ;
  }
      
  // Initialize random seed
  Int_t seqno = pkg->initRandom() ;
  cout << "RooStudyPackage::processFile() Initial random seed for this run is " << seqno << endl ;

  // Run study
  pkg->driver(nexp) ;

  // Save result
  TList res ;
  pkg->exportData(&res,seqno) ;
  TFile fout(Form("study_result_%s_%d.root",studyName,seqno),"RECREATE") ;
  res.Write() ;
  fout.Close() ;    
}
 RooStudyPackage.cxx:1
 RooStudyPackage.cxx:2
 RooStudyPackage.cxx:3
 RooStudyPackage.cxx:4
 RooStudyPackage.cxx:5
 RooStudyPackage.cxx:6
 RooStudyPackage.cxx:7
 RooStudyPackage.cxx:8
 RooStudyPackage.cxx:9
 RooStudyPackage.cxx:10
 RooStudyPackage.cxx:11
 RooStudyPackage.cxx:12
 RooStudyPackage.cxx:13
 RooStudyPackage.cxx:14
 RooStudyPackage.cxx:15
 RooStudyPackage.cxx:16
 RooStudyPackage.cxx:17
 RooStudyPackage.cxx:18
 RooStudyPackage.cxx:19
 RooStudyPackage.cxx:20
 RooStudyPackage.cxx:21
 RooStudyPackage.cxx:22
 RooStudyPackage.cxx:23
 RooStudyPackage.cxx:24
 RooStudyPackage.cxx:25
 RooStudyPackage.cxx:26
 RooStudyPackage.cxx:27
 RooStudyPackage.cxx:28
 RooStudyPackage.cxx:29
 RooStudyPackage.cxx:30
 RooStudyPackage.cxx:31
 RooStudyPackage.cxx:32
 RooStudyPackage.cxx:33
 RooStudyPackage.cxx:34
 RooStudyPackage.cxx:35
 RooStudyPackage.cxx:36
 RooStudyPackage.cxx:37
 RooStudyPackage.cxx:38
 RooStudyPackage.cxx:39
 RooStudyPackage.cxx:40
 RooStudyPackage.cxx:41
 RooStudyPackage.cxx:42
 RooStudyPackage.cxx:43
 RooStudyPackage.cxx:44
 RooStudyPackage.cxx:45
 RooStudyPackage.cxx:46
 RooStudyPackage.cxx:47
 RooStudyPackage.cxx:48
 RooStudyPackage.cxx:49
 RooStudyPackage.cxx:50
 RooStudyPackage.cxx:51
 RooStudyPackage.cxx:52
 RooStudyPackage.cxx:53
 RooStudyPackage.cxx:54
 RooStudyPackage.cxx:55
 RooStudyPackage.cxx:56
 RooStudyPackage.cxx:57
 RooStudyPackage.cxx:58
 RooStudyPackage.cxx:59
 RooStudyPackage.cxx:60
 RooStudyPackage.cxx:61
 RooStudyPackage.cxx:62
 RooStudyPackage.cxx:63
 RooStudyPackage.cxx:64
 RooStudyPackage.cxx:65
 RooStudyPackage.cxx:66
 RooStudyPackage.cxx:67
 RooStudyPackage.cxx:68
 RooStudyPackage.cxx:69
 RooStudyPackage.cxx:70
 RooStudyPackage.cxx:71
 RooStudyPackage.cxx:72
 RooStudyPackage.cxx:73
 RooStudyPackage.cxx:74
 RooStudyPackage.cxx:75
 RooStudyPackage.cxx:76
 RooStudyPackage.cxx:77
 RooStudyPackage.cxx:78
 RooStudyPackage.cxx:79
 RooStudyPackage.cxx:80
 RooStudyPackage.cxx:81
 RooStudyPackage.cxx:82
 RooStudyPackage.cxx:83
 RooStudyPackage.cxx:84
 RooStudyPackage.cxx:85
 RooStudyPackage.cxx:86
 RooStudyPackage.cxx:87
 RooStudyPackage.cxx:88
 RooStudyPackage.cxx:89
 RooStudyPackage.cxx:90
 RooStudyPackage.cxx:91
 RooStudyPackage.cxx:92
 RooStudyPackage.cxx:93
 RooStudyPackage.cxx:94
 RooStudyPackage.cxx:95
 RooStudyPackage.cxx:96
 RooStudyPackage.cxx:97
 RooStudyPackage.cxx:98
 RooStudyPackage.cxx:99
 RooStudyPackage.cxx:100
 RooStudyPackage.cxx:101
 RooStudyPackage.cxx:102
 RooStudyPackage.cxx:103
 RooStudyPackage.cxx:104
 RooStudyPackage.cxx:105
 RooStudyPackage.cxx:106
 RooStudyPackage.cxx:107
 RooStudyPackage.cxx:108
 RooStudyPackage.cxx:109
 RooStudyPackage.cxx:110
 RooStudyPackage.cxx:111
 RooStudyPackage.cxx:112
 RooStudyPackage.cxx:113
 RooStudyPackage.cxx:114
 RooStudyPackage.cxx:115
 RooStudyPackage.cxx:116
 RooStudyPackage.cxx:117
 RooStudyPackage.cxx:118
 RooStudyPackage.cxx:119
 RooStudyPackage.cxx:120
 RooStudyPackage.cxx:121
 RooStudyPackage.cxx:122
 RooStudyPackage.cxx:123
 RooStudyPackage.cxx:124
 RooStudyPackage.cxx:125
 RooStudyPackage.cxx:126
 RooStudyPackage.cxx:127
 RooStudyPackage.cxx:128
 RooStudyPackage.cxx:129
 RooStudyPackage.cxx:130
 RooStudyPackage.cxx:131
 RooStudyPackage.cxx:132
 RooStudyPackage.cxx:133
 RooStudyPackage.cxx:134
 RooStudyPackage.cxx:135
 RooStudyPackage.cxx:136
 RooStudyPackage.cxx:137
 RooStudyPackage.cxx:138
 RooStudyPackage.cxx:139
 RooStudyPackage.cxx:140
 RooStudyPackage.cxx:141
 RooStudyPackage.cxx:142
 RooStudyPackage.cxx:143
 RooStudyPackage.cxx:144
 RooStudyPackage.cxx:145
 RooStudyPackage.cxx:146
 RooStudyPackage.cxx:147
 RooStudyPackage.cxx:148
 RooStudyPackage.cxx:149
 RooStudyPackage.cxx:150
 RooStudyPackage.cxx:151
 RooStudyPackage.cxx:152
 RooStudyPackage.cxx:153
 RooStudyPackage.cxx:154
 RooStudyPackage.cxx:155
 RooStudyPackage.cxx:156
 RooStudyPackage.cxx:157
 RooStudyPackage.cxx:158
 RooStudyPackage.cxx:159
 RooStudyPackage.cxx:160
 RooStudyPackage.cxx:161
 RooStudyPackage.cxx:162
 RooStudyPackage.cxx:163
 RooStudyPackage.cxx:164
 RooStudyPackage.cxx:165
 RooStudyPackage.cxx:166
 RooStudyPackage.cxx:167
 RooStudyPackage.cxx:168
 RooStudyPackage.cxx:169
 RooStudyPackage.cxx:170
 RooStudyPackage.cxx:171
 RooStudyPackage.cxx:172
 RooStudyPackage.cxx:173
 RooStudyPackage.cxx:174
 RooStudyPackage.cxx:175
 RooStudyPackage.cxx:176
 RooStudyPackage.cxx:177
 RooStudyPackage.cxx:178
 RooStudyPackage.cxx:179
 RooStudyPackage.cxx:180
 RooStudyPackage.cxx:181
 RooStudyPackage.cxx:182
 RooStudyPackage.cxx:183
 RooStudyPackage.cxx:184
 RooStudyPackage.cxx:185
 RooStudyPackage.cxx:186
 RooStudyPackage.cxx:187
 RooStudyPackage.cxx:188
 RooStudyPackage.cxx:189
 RooStudyPackage.cxx:190
 RooStudyPackage.cxx:191
 RooStudyPackage.cxx:192
 RooStudyPackage.cxx:193
 RooStudyPackage.cxx:194
 RooStudyPackage.cxx:195
 RooStudyPackage.cxx:196
 RooStudyPackage.cxx:197
 RooStudyPackage.cxx:198
 RooStudyPackage.cxx:199
 RooStudyPackage.cxx:200
 RooStudyPackage.cxx:201
 RooStudyPackage.cxx:202
 RooStudyPackage.cxx:203
 RooStudyPackage.cxx:204
 RooStudyPackage.cxx:205
 RooStudyPackage.cxx:206
 RooStudyPackage.cxx:207
 RooStudyPackage.cxx:208
 RooStudyPackage.cxx:209
 RooStudyPackage.cxx:210
 RooStudyPackage.cxx:211
 RooStudyPackage.cxx:212
 RooStudyPackage.cxx:213
 RooStudyPackage.cxx:214
 RooStudyPackage.cxx:215
 RooStudyPackage.cxx:216
 RooStudyPackage.cxx:217
 RooStudyPackage.cxx:218
 RooStudyPackage.cxx:219
 RooStudyPackage.cxx:220
 RooStudyPackage.cxx:221
 RooStudyPackage.cxx:222
 RooStudyPackage.cxx:223
 RooStudyPackage.cxx:224
 RooStudyPackage.cxx:225
 RooStudyPackage.cxx:226
 RooStudyPackage.cxx:227
 RooStudyPackage.cxx:228
 RooStudyPackage.cxx:229
 RooStudyPackage.cxx:230
 RooStudyPackage.cxx:231
 RooStudyPackage.cxx:232
 RooStudyPackage.cxx:233
 RooStudyPackage.cxx:234