// @(#)root/roostats:$Id$
// Author: Sven Kreiss and Kyle Cranmer    June 2010
/*************************************************************************
 * 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.             *
 *************************************************************************/

#include "RooStats/ToyMCStudy.h"

#include "RooStats/ToyMCSampler.h"


#ifndef ROO_MSG_SERVICE
#include "RooMsgService.h"
#endif

#include "RooRandom.h"
#include "TRandom2.h"
#include "TMath.h"

#include "TEnv.h"



ClassImp(RooStats::ToyMCStudy);

ClassImp(RooStats::ToyMCPayload);

using namespace std;


namespace RooStats {

// _____________________________________________________________________________
Bool_t ToyMCStudy::initialize(void) {
   coutP(Generation) << "initialize" << endl;

   if(!fToyMCSampler) {
      coutE(InputArguments) << "Need an instance of ToyMCSampler to run." << endl;
      return kFALSE;
   }else{
      coutI(InputArguments) << "Using given ToyMCSampler." << endl;
   }


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

      // generate a seed using 
      std::cout << "Current global seed is " << fRandomSeed << std::endl;
      TRandom2 r(fRandomSeed );
      // get a seed using the iworker-value
      unsigned int seed = r.Integer(TMath::Limits<unsigned int>::Max() ); 
      for (int i = 0; i< iworker; ++i) 
         seed = r.Integer(TMath::Limits<unsigned int>::Max() );

      // initialize worker using seed from ToyMCSampler 
      RooRandom::randomGenerator()->SetSeed(seed);
   }

   coutI(InputArguments) << "Worker " << iworker << " seed is: " << RooRandom::randomGenerator()->GetSeed() << endl;
  
   return kFALSE;
}

// _____________________________________________________________________________
Bool_t ToyMCStudy::execute(void) {

   coutP(Generation) << "ToyMCStudy::execute - run with seed " <<   RooRandom::randomGenerator()->Integer(TMath::Limits<unsigned int>::Max() ) << std::endl;
   RooDataSet* sd = fToyMCSampler->GetSamplingDistributionsSingleWorker(fParamPoint);
   ToyMCPayload *sdw = new ToyMCPayload(sd);
   storeDetailedOutput(*sdw);

   return kFALSE;
}

// _____________________________________________________________________________
Bool_t ToyMCStudy::finalize(void) {
   coutP(Generation) << "ToyMCStudy::finalize" << endl;

   if(fToyMCSampler) delete fToyMCSampler;
   fToyMCSampler = NULL;

   return kFALSE;
}


RooDataSet* ToyMCStudy::merge() {

   RooDataSet* samplingOutput = NULL;

   if(!detailedData()) {
      coutE(Generation) << "ToyMCStudy::merge No detailed output present." << endl;
      return NULL;
   }

   RooLinkedListIter iter = detailedData()->iterator();
   TObject *o = NULL;
   int i = 0;
   while((o = iter.Next())) {
      ToyMCPayload *oneWorker = dynamic_cast< ToyMCPayload* >(o);
      if(!oneWorker) {
         coutW(Generation) << "Merging Results problem: not correct type" << endl;
         continue;
      }
      
      if( !samplingOutput ) samplingOutput = new RooDataSet(*oneWorker->GetSamplingDistributions());

      else samplingOutput->append( *oneWorker->GetSamplingDistributions() );

      i++;
      //delete oneWorker;
   }
   coutP(Generation) << "Merged data from nworkers # " << i << "- merged data size is " << samplingOutput->numEntries() << std::endl;


   return samplingOutput;
}


} // end namespace RooStats
 ToyMCStudy.cxx:1
 ToyMCStudy.cxx:2
 ToyMCStudy.cxx:3
 ToyMCStudy.cxx:4
 ToyMCStudy.cxx:5
 ToyMCStudy.cxx:6
 ToyMCStudy.cxx:7
 ToyMCStudy.cxx:8
 ToyMCStudy.cxx:9
 ToyMCStudy.cxx:10
 ToyMCStudy.cxx:11
 ToyMCStudy.cxx:12
 ToyMCStudy.cxx:13
 ToyMCStudy.cxx:14
 ToyMCStudy.cxx:15
 ToyMCStudy.cxx:16
 ToyMCStudy.cxx:17
 ToyMCStudy.cxx:18
 ToyMCStudy.cxx:19
 ToyMCStudy.cxx:20
 ToyMCStudy.cxx:21
 ToyMCStudy.cxx:22
 ToyMCStudy.cxx:23
 ToyMCStudy.cxx:24
 ToyMCStudy.cxx:25
 ToyMCStudy.cxx:26
 ToyMCStudy.cxx:27
 ToyMCStudy.cxx:28
 ToyMCStudy.cxx:29
 ToyMCStudy.cxx:30
 ToyMCStudy.cxx:31
 ToyMCStudy.cxx:32
 ToyMCStudy.cxx:33
 ToyMCStudy.cxx:34
 ToyMCStudy.cxx:35
 ToyMCStudy.cxx:36
 ToyMCStudy.cxx:37
 ToyMCStudy.cxx:38
 ToyMCStudy.cxx:39
 ToyMCStudy.cxx:40
 ToyMCStudy.cxx:41
 ToyMCStudy.cxx:42
 ToyMCStudy.cxx:43
 ToyMCStudy.cxx:44
 ToyMCStudy.cxx:45
 ToyMCStudy.cxx:46
 ToyMCStudy.cxx:47
 ToyMCStudy.cxx:48
 ToyMCStudy.cxx:49
 ToyMCStudy.cxx:50
 ToyMCStudy.cxx:51
 ToyMCStudy.cxx:52
 ToyMCStudy.cxx:53
 ToyMCStudy.cxx:54
 ToyMCStudy.cxx:55
 ToyMCStudy.cxx:56
 ToyMCStudy.cxx:57
 ToyMCStudy.cxx:58
 ToyMCStudy.cxx:59
 ToyMCStudy.cxx:60
 ToyMCStudy.cxx:61
 ToyMCStudy.cxx:62
 ToyMCStudy.cxx:63
 ToyMCStudy.cxx:64
 ToyMCStudy.cxx:65
 ToyMCStudy.cxx:66
 ToyMCStudy.cxx:67
 ToyMCStudy.cxx:68
 ToyMCStudy.cxx:69
 ToyMCStudy.cxx:70
 ToyMCStudy.cxx:71
 ToyMCStudy.cxx:72
 ToyMCStudy.cxx:73
 ToyMCStudy.cxx:74
 ToyMCStudy.cxx:75
 ToyMCStudy.cxx:76
 ToyMCStudy.cxx:77
 ToyMCStudy.cxx:78
 ToyMCStudy.cxx:79
 ToyMCStudy.cxx:80
 ToyMCStudy.cxx:81
 ToyMCStudy.cxx:82
 ToyMCStudy.cxx:83
 ToyMCStudy.cxx:84
 ToyMCStudy.cxx:85
 ToyMCStudy.cxx:86
 ToyMCStudy.cxx:87
 ToyMCStudy.cxx:88
 ToyMCStudy.cxx:89
 ToyMCStudy.cxx:90
 ToyMCStudy.cxx:91
 ToyMCStudy.cxx:92
 ToyMCStudy.cxx:93
 ToyMCStudy.cxx:94
 ToyMCStudy.cxx:95
 ToyMCStudy.cxx:96
 ToyMCStudy.cxx:97
 ToyMCStudy.cxx:98
 ToyMCStudy.cxx:99
 ToyMCStudy.cxx:100
 ToyMCStudy.cxx:101
 ToyMCStudy.cxx:102
 ToyMCStudy.cxx:103
 ToyMCStudy.cxx:104
 ToyMCStudy.cxx:105
 ToyMCStudy.cxx:106
 ToyMCStudy.cxx:107
 ToyMCStudy.cxx:108
 ToyMCStudy.cxx:109
 ToyMCStudy.cxx:110
 ToyMCStudy.cxx:111
 ToyMCStudy.cxx:112
 ToyMCStudy.cxx:113
 ToyMCStudy.cxx:114
 ToyMCStudy.cxx:115
 ToyMCStudy.cxx:116
 ToyMCStudy.cxx:117
 ToyMCStudy.cxx:118
 ToyMCStudy.cxx:119
 ToyMCStudy.cxx:120
 ToyMCStudy.cxx:121
 ToyMCStudy.cxx:122
 ToyMCStudy.cxx:123
 ToyMCStudy.cxx:124
 ToyMCStudy.cxx:125
 ToyMCStudy.cxx:126