14 #ifndef ROO_MSG_SERVICE 30 ToyMCImportanceSampler::~ToyMCImportanceSampler() {
31 for(
unsigned int i=0; i < fImportanceSnapshots.size(); i++ )
if(fImportanceSnapshots[i])
delete fImportanceSnapshots[i];
32 for(
unsigned int i=0; i < fNullSnapshots.size(); i++ )
if(fNullSnapshots[i])
delete fNullSnapshots[i];
34 void ToyMCImportanceSampler::ClearCache(
void) {
35 ToyMCSampler::ClearCache();
37 for(
unsigned int i=0; i < fImpNLLs.size(); i++ )
if(fImpNLLs[i]) {
delete fImpNLLs[i]; fImpNLLs[i] =
NULL; }
38 for(
unsigned int i=0; i < fNullNLLs.size(); i++ )
if(fNullNLLs[i]) {
delete fNullNLLs[i]; fNullNLLs[i] =
NULL; }
50 if( fNToys == 0 )
return NULL;
53 Int_t allToys = fNToys;
56 RooCategory densityLabel(
"densityLabel",
"densityLabel" );
58 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ )
65 for(
int i = -1; i < (int)fImportanceDensities.size(); i++ ) {
69 SetDensityToGenerateFromByIndex( 0,
true );
71 oocoutP((
TObject*)0,
Generation) << endl << endl <<
" GENERATING IMP DENS/SNAP "<<i+1<<
" OUT OF "<<fImportanceDensities.size()<<endl<<endl;
72 SetDensityToGenerateFromByIndex( i,
false );
75 RooRealVar reweight(
"reweight",
"reweight", 1.0 );
79 fNToys =
TMath::CeilNint(
double(allToys)/(fImportanceDensities.size()+1) );
83 fNToys =
TMath::CeilNint(
double(allToys) *
pow(
double(2) , i+1 ) / (
pow(
double(2),
int(fImportanceDensities.size()+1) )-1) );
85 int largestNToys =
TMath::CeilNint( allToys *
pow(
double(2),
int(fImportanceDensities.size()) ) / (
pow(
double(2),
int(fImportanceDensities.size()+1) )-1) );
86 reweight.
setVal( ((
double)largestNToys) / fNToys );
93 RooDataSet*
result = ToyMCSampler::GetSamplingDistributionsSingleWorker( paramPoint );
104 RooRealVar weightVar (
"weight",
"weight", 1.0 );
105 columns.add( weightVar );
112 for(
int j=0; j < result->
numEntries(); j++ ) {
139 if( fNullDensities.size() > 1 ) {
141 for(
unsigned int i=0; i < fNullDensities.size(); i++) {
142 ooccoutI((
TObject*)NULL,
InputArguments) <<
" null density["<<i<<
"]: " << fNullDensities[i] <<
" \t null snapshot["<<i<<
"]: " << fNullSnapshots[i] << endl;
148 if( fNullDensities.size() == 0 && fPdf ) {
149 ooccoutI((
TObject*)
NULL,
InputArguments) <<
"No explicit null densities specified. Going to add one based on the given paramPoint and the global fPdf. ... but cannot do that inside const function." << endl;
155 if( fNullSnapshots[0] != ¶mPoint ) {
157 if(fNullSnapshots[0])
delete fNullSnapshots[0];
158 fNullSnapshots.clear();
162 vector<double> weights;
163 weights.push_back( weight );
165 vector<double> impNLLs;
166 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ ) impNLLs.push_back( 0.0 );
167 vector<double> nullNLLs;
168 for(
unsigned int i=0; i < fNullDensities.size(); i++ ) nullNLLs.push_back( 0.0 );
170 RooAbsData *d = GenerateToyData( weights, impNLLs, nullNLLs );
177 vector<double>& impNLLs,
180 if( fNullDensities.size() > 1 ) {
182 for(
unsigned int i=0; i < fNullDensities.size(); i++) {
183 ooccoutI((
TObject*)NULL,
InputArguments) <<
" null density["<<i<<
"]: " << fNullDensities[i] <<
" \t null snapshot["<<i<<
"]: " << fNullSnapshots[i] << endl;
189 if( fNullDensities.size() == 0 && fPdf ) {
190 ooccoutI((
TObject*)
NULL,
InputArguments) <<
"No explicit null densities specified. Going to add one based on the given paramPoint and the global fPdf. ... but cannot do that inside const function." << endl;
195 if(fNullSnapshots[0])
delete fNullSnapshots[0];
196 fNullSnapshots.clear();
199 vector<double> weights;
200 weights.push_back( weight );
202 vector<double> nullNLLs;
203 nullNLLs.push_back( nullNLL );
205 RooAbsData *d = GenerateToyData( weights, impNLLs, nullNLLs );
207 nullNLL = nullNLLs[0];
212 vector<double>& weights
214 if( fNullDensities.size() != weights.size() ) {
219 vector<double> impNLLs;
220 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ ) impNLLs.push_back( 0.0 );
221 vector<double> nullNLLs;
222 for(
unsigned int i=0; i < fNullDensities.size(); i++ ) nullNLLs.push_back( 0.0 );
224 RooAbsData *d = GenerateToyData( weights, impNLLs, nullNLLs );
228 vector<double>& weights,
229 vector<double>& impNLLVals,
230 vector<double>& nullNLLVals
246 if( fNullDensities.size() == 0 ) {
252 if( fNullNLLs.size() == 0 && fNullDensities.size() > 0 ) {
253 for(
unsigned int i = 0; i < fNullDensities.size(); i++ ) fNullNLLs.push_back(
NULL );
255 if( fImpNLLs.size() == 0 && fImportanceDensities.size() > 0 ) {
256 for(
unsigned int i = 0; i < fImportanceDensities.size(); i++ ) fImpNLLs.push_back(
NULL );
259 if( fNullDensities.size() != fNullNLLs.size() ) {
264 if( (!fGenerateFromNull && fIndexGenDensity >= fImportanceDensities.size()) ||
265 (fGenerateFromNull && fIndexGenDensity >= fNullDensities.size())
275 RooArgSet paramPoint( *fNullSnapshots[0] );
281 RooArgSet* allVars = fPdf->getVariables();
282 *allVars = paramPoint;
286 if(!fNuisanceParametersSampler && fPriorNuisance && fNuisancePars)
291 if(fGlobalObservables && fGlobalObservables->getSize()) {
292 observables.
remove(*fGlobalObservables);
294 GenerateGlobalObservables(*fNullDensities[0]);
299 if( !fGenerateFromNull ) {
300 RooArgSet* allVarsImpDens = fImportanceDensities[fIndexGenDensity]->getVariables();
301 allVars->
add(*allVarsImpDens);
302 delete allVarsImpDens;
306 double globalWeight = 1.0;
307 if(fNuisanceParametersSampler) {
316 RooArgSet allVarsMinusParamPoint(*allVars);
320 fNuisanceParametersSampler->NextPoint(allVarsMinusParamPoint, globalWeight);
323 for(
unsigned int i=0; i < weights.size(); i++ ) weights[i] = globalWeight;
326 if( fGenerateFromNull ) {
328 *allVars = *fNullSnapshots[fIndexGenDensity];
329 data = Generate(*fNullDensities[fIndexGenDensity], observables);
334 if(fImportanceSnapshots[fIndexGenDensity]) *allVars = *fImportanceSnapshots[fIndexGenDensity];
335 data = Generate(*fImportanceDensities[fIndexGenDensity], observables);
350 for(
unsigned int i=0; i < fNullDensities.size(); i++ ) {
354 *allVars = *fNullSnapshots[i];
355 if( !fNullNLLs[i] ) {
356 RooArgSet* allParams = fNullDensities[i]->getParameters(*data);
361 fNullNLLs[i]->setData( *data,
kFALSE );
363 nullNLLVals[i] = fNullNLLs[i]->getVal();
365 if( !fReuseNLL ) {
delete fNullNLLs[i]; fNullNLLs[i] =
NULL; }
371 vector<double> minNLLVals;
372 for(
unsigned int i=0; i < nullNLLVals.size(); i++ ) minNLLVals.push_back( nullNLLVals[i] );
374 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ ) {
378 if( fImportanceSnapshots[i] ) *allVars = *fImportanceSnapshots[i];
380 RooArgSet* allParams = fImportanceDensities[i]->getParameters(*data);
385 fImpNLLs[i]->setData( *data,
kFALSE );
387 impNLLVals[i] = fImpNLLs[i]->getVal();
389 if( !fReuseNLL ) {
delete fImpNLLs[i]; fImpNLLs[i] =
NULL; }
391 for(
unsigned int j=0; j < nullNLLVals.size(); j++ ) {
392 if( impNLLVals[i] < minNLLVals[j] ) minNLLVals[j] = impNLLVals[i];
393 ooccoutD((
TObject*)0,
InputArguments) <<
"minNLLVals["<<j<<
"]: " << minNLLVals[j] <<
" nullNLLVals["<<j<<
"]: " << nullNLLVals[j] <<
" impNLLVals["<<i<<
"]: " << impNLLVals[i] << endl;
400 for(
unsigned int j=0; j < nullNLLVals.size(); j++ ) {
401 if ( fApplyVeto && fGenerateFromNull && minNLLVals[j] != nullNLLVals[j] ) weights[j] = 0.0;
402 else if( fApplyVeto && !fGenerateFromNull && minNLLVals[j] != impNLLVals[fIndexGenDensity] ) weights[j] = 0.0;
403 else if( !fGenerateFromNull ) {
407 weights[j] *=
exp(minNLLVals[j] - nullNLLVals[j]);
415 *allVars = *saveVars;
427 int ToyMCImportanceSampler::CreateImpDensitiesForOnePOIAdaptively(
RooAbsPdf& pdf,
const RooArgSet& allPOI,
RooRealVar& poi,
double nStdDevOverlap,
double poiValueForBackground ) {
429 double impMaxMu = poi.
getVal();
444 return CreateNImpDensitiesForOnePOI( pdf, allPOI, poi, n-1, poiValueForBackground);
451 double impMaxMu = poi.
getVal();
454 if( impMaxMu > poiValueForBackground && n > 0 ) {
455 for(
int i=1; i <=
n; i++ ) {
456 poi.
setVal( poiValueForBackground + (
double)i/(n)*(impMaxMu - poiValueForBackground) );
462 AddImportanceDensity( &pdf, &allPOI );
virtual const char * GetName() const
Returns name of object.
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
RooCmdArg CloneData(Bool_t flag)
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set value by specifying the index code of the desired state.
Double_t getVal(const RooArgSet *set=0) const
ToyMCImportanceSampler is an extension of the ToyMCSampler for Importance Sampling.
virtual RooAbsArg * addColumn(RooAbsArg &var, Bool_t adjustRange=kTRUE)
Add a column with the values of the given (function) argument to this dataset.
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
double pow(double, double)
virtual void Print(Option_t *options=0) const
Print TNamed name and title.
virtual Double_t weight() const
Return event weight of current event.
RooRealVar represents a fundamental (non-derived) real valued object.
virtual void setVal(Double_t value)
Set value of variable to 'value'.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents: An owning collection is returned containing clones o...
RooAbsData is the common abstract base class for binned and unbinned datasets.
RooDataSet is a container class to hold unbinned data.
RooCategory represents a fundamental (non-derived) discrete value object.
virtual const RooArgSet * get(Int_t index) const
Return RooArgSet with coordinates of event 'index'.
virtual void add(const RooArgSet &row, Double_t weight=1.0, Double_t weightError=0)
Add a data point, with its coordinates specified in the 'data' argset, to the data set...
Namespace for the RooStats classes.
Mother of all ROOT objects.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Bool_t defineType(const char *label)
Define a state with given name, the lowest available positive integer is assigned as index...
RooCmdArg ConditionalObservables(const RooArgSet &set)
Double_t getError() const
Int_t CeilNint(Double_t x)
RooCmdArg Constrain(const RooArgSet ¶ms)
virtual Int_t numEntries() const
virtual const char * GetTitle() const
Returns title of object.