37 ToyMCImportanceSampler::~ToyMCImportanceSampler() {
38 for(
unsigned int i=0; i < fImportanceSnapshots.size(); i++ )
if(fImportanceSnapshots[i])
delete fImportanceSnapshots[i];
39 for(
unsigned int i=0; i < fNullSnapshots.size(); i++ )
if(fNullSnapshots[i])
delete fNullSnapshots[i];
44 void ToyMCImportanceSampler::ClearCache(
void) {
45 ToyMCSampler::ClearCache();
47 for(
unsigned int i=0; i < fImpNLLs.size(); i++ )
if(fImpNLLs[i]) {
delete fImpNLLs[i]; fImpNLLs[i] = NULL; }
48 for(
unsigned int i=0; i < fNullNLLs.size(); i++ )
if(fNullNLLs[i]) {
delete fNullNLLs[i]; fNullNLLs[i] = NULL; }
54 if( fNToys == 0 )
return NULL;
57 Int_t allToys = fNToys;
60 RooCategory densityLabel(
"densityLabel",
"densityLabel" );
62 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ )
69 for(
int i = -1; i < (int)fImportanceDensities.size(); i++ ) {
73 SetDensityToGenerateFromByIndex( 0,
true );
75 oocoutP((
TObject*)0,
Generation) << endl << endl <<
" GENERATING IMP DENS/SNAP "<<i+1<<
" OUT OF "<<fImportanceDensities.size()<<endl<<endl;
76 SetDensityToGenerateFromByIndex( i,
false );
79 RooRealVar reweight(
"reweight",
"reweight", 1.0 );
83 fNToys =
TMath::CeilNint(
double(allToys)/(fImportanceDensities.size()+1) );
87 fNToys =
TMath::CeilNint(
double(allToys) *
pow(
double(2) , i+1 ) / (
pow(
double(2),
int(fImportanceDensities.size()+1) )-1) );
89 int largestNToys =
TMath::CeilNint( allToys *
pow(
double(2),
int(fImportanceDensities.size()) ) / (
pow(
double(2),
int(fImportanceDensities.size()+1) )-1) );
90 reweight.
setVal( ((
double)largestNToys) / fNToys );
97 RooDataSet* result = ToyMCSampler::GetSamplingDistributionsSingleWorker( paramPoint );
108 RooRealVar weightVar (
"weight",
"weight", 1.0 );
109 columns.add( weightVar );
116 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 );
180 vector<double>& impNLLs,
183 if( fNullDensities.size() > 1 ) {
185 for(
unsigned int i=0; i < fNullDensities.size(); i++) {
186 ooccoutI((
TObject*)NULL,
InputArguments) <<
" null density["<<i<<
"]: " << fNullDensities[i] <<
" \t null snapshot["<<i<<
"]: " << fNullSnapshots[i] << endl;
192 if( fNullDensities.size() == 0 && fPdf ) {
193 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;
198 if(fNullSnapshots[0])
delete fNullSnapshots[0];
199 fNullSnapshots.clear();
202 vector<double> weights;
203 weights.push_back( weight );
205 vector<double> nullNLLs;
206 nullNLLs.push_back( nullNLL );
208 RooAbsData *
d = GenerateToyData( weights, impNLLs, nullNLLs );
210 nullNLL = nullNLLs[0];
217 vector<double>& weights
219 if( fNullDensities.size() != weights.size() ) {
220 ooccoutI((
TObject*)NULL,
InputArguments) <<
"weights.size() != nullDesnities.size(). You need to provide a vector with the correct size." << endl;
224 vector<double> impNLLs;
225 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ ) impNLLs.push_back( 0.0 );
226 vector<double> nullNLLs;
227 for(
unsigned int i=0; i < fNullDensities.size(); i++ ) nullNLLs.push_back( 0.0 );
229 RooAbsData *
d = GenerateToyData( weights, impNLLs, nullNLLs );
241 vector<double>& weights,
242 vector<double>& impNLLVals,
243 vector<double>& nullNLLVals
255 if( fNullDensities.size() == 0 ) {
261 if( fNullNLLs.size() == 0 && fNullDensities.size() > 0 ) {
262 for(
unsigned int i = 0; i < fNullDensities.size(); i++ ) fNullNLLs.push_back( NULL );
264 if( fImpNLLs.size() == 0 && fImportanceDensities.size() > 0 ) {
265 for(
unsigned int i = 0; i < fImportanceDensities.size(); i++ ) fImpNLLs.push_back( NULL );
268 if( fNullDensities.size() != fNullNLLs.size() ) {
269 oocoutE((
TObject*)NULL,
InputArguments) <<
"ToyMCImportanceSampler: Something wrong. NullNLLs must be of same size as null densities." << endl;
273 if( (!fGenerateFromNull && fIndexGenDensity >= fImportanceDensities.size()) ||
274 (fGenerateFromNull && fIndexGenDensity >= fNullDensities.size())
284 RooArgSet paramPoint( *fNullSnapshots[0] );
290 RooArgSet* allVars = fPdf->getVariables();
291 *allVars = paramPoint;
295 if(!fNuisanceParametersSampler && fPriorNuisance && fNuisancePars)
300 if(fGlobalObservables && fGlobalObservables->getSize()) {
301 observables.
remove(*fGlobalObservables);
303 GenerateGlobalObservables(*fNullDensities[0]);
308 if( !fGenerateFromNull ) {
309 RooArgSet* allVarsImpDens = fImportanceDensities[fIndexGenDensity]->getVariables();
310 allVars->
add(*allVarsImpDens);
311 delete allVarsImpDens;
315 double globalWeight = 1.0;
316 if(fNuisanceParametersSampler) {
325 RooArgSet allVarsMinusParamPoint(*allVars);
329 fNuisanceParametersSampler->NextPoint(allVarsMinusParamPoint, globalWeight);
332 for(
unsigned int i=0; i < weights.size(); i++ ) weights[i] = globalWeight;
335 if( fGenerateFromNull ) {
337 *allVars = *fNullSnapshots[fIndexGenDensity];
338 data = Generate(*fNullDensities[fIndexGenDensity], observables);
343 if(fImportanceSnapshots[fIndexGenDensity]) *allVars = *fImportanceSnapshots[fIndexGenDensity];
344 data = Generate(*fImportanceDensities[fIndexGenDensity], observables);
359 for(
unsigned int i=0; i < fNullDensities.size(); i++ ) {
363 *allVars = *fNullSnapshots[i];
364 if( !fNullNLLs[i] ) {
365 RooArgSet* allParams = fNullDensities[i]->getParameters(*data);
370 fNullNLLs[i]->setData( *data,
kFALSE );
372 nullNLLVals[i] = fNullNLLs[i]->getVal();
374 if( !fReuseNLL ) {
delete fNullNLLs[i]; fNullNLLs[i] = NULL; }
380 vector<double> minNLLVals;
381 for(
unsigned int i=0; i < nullNLLVals.size(); i++ ) minNLLVals.push_back( nullNLLVals[i] );
383 for(
unsigned int i=0; i < fImportanceDensities.size(); i++ ) {
387 if( fImportanceSnapshots[i] ) *allVars = *fImportanceSnapshots[i];
389 RooArgSet* allParams = fImportanceDensities[i]->getParameters(*data);
394 fImpNLLs[i]->setData( *data,
kFALSE );
396 impNLLVals[i] = fImpNLLs[i]->getVal();
398 if( !fReuseNLL ) {
delete fImpNLLs[i]; fImpNLLs[i] = NULL; }
400 for(
unsigned int j=0; j < nullNLLVals.size(); j++ ) {
401 if( impNLLVals[i] < minNLLVals[j] ) minNLLVals[j] = impNLLVals[i];
402 ooccoutD((
TObject*)0,
InputArguments) <<
"minNLLVals["<<j<<
"]: " << minNLLVals[j] <<
" nullNLLVals["<<j<<
"]: " << nullNLLVals[j] <<
" impNLLVals["<<i<<
"]: " << impNLLVals[i] << endl;
409 for(
unsigned int j=0; j < nullNLLVals.size(); j++ ) {
410 if ( fApplyVeto && fGenerateFromNull && minNLLVals[j] != nullNLLVals[j] ) weights[j] = 0.0;
411 else if( fApplyVeto && !fGenerateFromNull && minNLLVals[j] != impNLLVals[fIndexGenDensity] ) weights[j] = 0.0;
412 else if( !fGenerateFromNull ) {
416 weights[j] *=
exp(minNLLVals[j] - nullNLLVals[j]);
424 *allVars = *saveVars;
434 int ToyMCImportanceSampler::CreateImpDensitiesForOnePOIAdaptively(
RooAbsPdf& pdf,
const RooArgSet& allPOI,
RooRealVar& poi,
double nStdDevOverlap,
double poiValueForBackground ) {
436 double impMaxMu = poi.
getVal();
451 return CreateNImpDensitiesForOnePOI( pdf, allPOI, poi, n-1, poiValueForBackground);
460 double impMaxMu = poi.
getVal();
463 if( impMaxMu > poiValueForBackground && n > 0 ) {
464 for(
int i=1; i <=
n; i++ ) {
465 poi.
setVal( poiValueForBackground + (
double)i/(n)*(impMaxMu - poiValueForBackground) );
471 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.