#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);
std::cout << "Current global seed is " << fRandomSeed << std::endl;
TRandom2 r(fRandomSeed );
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() );
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++;
}
coutP(Generation) << "Merged data from nworkers # " << i << "- merged data size is " << samplingOutput->numEntries() << std::endl;
return samplingOutput;
}
}