Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
ToyMCStudy.cxx
Go to the documentation of this file.
1 // @(#)root/roostats:$Id$
2 // Author: Sven Kreiss and Kyle Cranmer June 2010
3 /*************************************************************************
4  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers. *
5  * All rights reserved. *
6  * *
7  * For the licensing terms see $ROOTSYS/LICENSE. *
8  * For the list of contributors see $ROOTSYS/README/CREDITS. *
9  *************************************************************************/
10 
11 /** \class RooStats::ToyMCStudy
12  \ingroup Roostats
13 
14 ToyMCStudy is an implementation of RooAbsStudy for toy Monte Carlo sampling.
15 This class is automatically used by ToyMCSampler when given a ProofConfig.
16 This is also its intended use case.
17 */
18 
19 #include "RooStats/ToyMCStudy.h"
20 
21 #include "RooStats/ToyMCSampler.h"
22 
23 
24 #include "RooMsgService.h"
25 
26 #include "RooRandom.h"
27 #include "TRandom2.h"
28 #include "TMath.h"
29 
30 #include "TEnv.h"
31 
32 ClassImp(RooStats::ToyMCStudy);
33 
34 ClassImp(RooStats::ToyMCPayload);
35 
36 using namespace std;
37 
38 
39 namespace RooStats {
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 
43 Bool_t ToyMCStudy::initialize(void) {
44  coutP(Generation) << "initialize" << endl;
45 
46  if(!fToyMCSampler) {
47  coutE(InputArguments) << "Need an instance of ToyMCSampler to run." << endl;
48  return kFALSE;
49  }else{
50  coutI(InputArguments) << "Using given ToyMCSampler." << endl;
51  }
52 
53 
54  TString worknumber = gEnv->GetValue("ProofServ.Ordinal","undef");
55  int iworker = -1;
56  if (worknumber != "undef") {
57  iworker = int( worknumber.Atof()*10 + 0.1);
58 
59  // generate a seed using
60  std::cout << "Current global seed is " << fRandomSeed << std::endl;
61  TRandom2 r(fRandomSeed );
62  // get a seed using the iworker-value
63  unsigned int seed = r.Integer(TMath::Limits<unsigned int>::Max() );
64  for (int i = 0; i< iworker; ++i)
65  seed = r.Integer(TMath::Limits<unsigned int>::Max() );
66 
67  // initialize worker using seed from ToyMCSampler
68  RooRandom::randomGenerator()->SetSeed(seed);
69  }
70 
71  coutI(InputArguments) << "Worker " << iworker << " seed is: " << RooRandom::randomGenerator()->GetSeed() << endl;
72 
73  return kFALSE;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 
78 Bool_t ToyMCStudy::execute(void) {
79 
80  coutP(Generation) << "ToyMCStudy::execute - run with seed " << RooRandom::randomGenerator()->Integer(TMath::Limits<unsigned int>::Max() ) << std::endl;
81  RooDataSet* sd = fToyMCSampler->GetSamplingDistributionsSingleWorker(fParamPoint);
82  ToyMCPayload *sdw = new ToyMCPayload(sd);
83  storeDetailedOutput(*sdw);
84 
85  return kFALSE;
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 
90 Bool_t ToyMCStudy::finalize(void) {
91  coutP(Generation) << "ToyMCStudy::finalize" << endl;
92 
93  if(fToyMCSampler) delete fToyMCSampler;
94  fToyMCSampler = NULL;
95 
96  return kFALSE;
97 }
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 
101 RooDataSet* ToyMCStudy::merge() {
102 
103  RooDataSet* samplingOutput = NULL;
104 
105  if(!detailedData()) {
106  coutE(Generation) << "ToyMCStudy::merge No detailed output present." << endl;
107  return NULL;
108  }
109 
110  RooLinkedListIter iter = detailedData()->iterator();
111  TObject *o = NULL;
112  int i = 0;
113  while((o = iter.Next())) {
114  ToyMCPayload *oneWorker = dynamic_cast< ToyMCPayload* >(o);
115  if(!oneWorker) {
116  coutW(Generation) << "Merging Results problem: not correct type" << endl;
117  continue;
118  }
119 
120  if( !samplingOutput ) samplingOutput = new RooDataSet(*oneWorker->GetSamplingDistributions());
121 
122  else samplingOutput->append( *oneWorker->GetSamplingDistributions() );
123 
124  i++;
125  //delete oneWorker;
126  }
127  coutP(Generation) << "Merged data from nworkers # " << i << "- merged data size is " << samplingOutput->numEntries() << std::endl;
128 
129 
130  return samplingOutput;
131 }
132 
133 
134 } // end namespace RooStats