forked from SusyRa2b/Statistics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HybridToyMCSampler.cxx
433 lines (362 loc) · 14.9 KB
/
HybridToyMCSampler.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// @(#)root/roostats:$Id: HybridToyMCSampler.cxx 42273 2011-11-28 14:28:37Z moneta $
// Author: Lucas Winstrom and Sven Kreiss June 2010
// Author: Kyle Cranmer, Lorenzo Moneta, Gregory Schott, Wouter Verkerke
/*************************************************************************
* 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 "HybridToyMCSampler.h"
#ifndef ROO_MSG_SERVICE
#include "RooMsgService.h"
#endif
#ifndef ROO_DATA_HIST
#include "RooDataHist.h"
#endif
#ifndef ROO_REAL_VAR
#include "RooRealVar.h"
#endif
#include "RooStats/RooStatsUtils.h"
#include "TCanvas.h"
#include "RooPlot.h"
#include "RooRandom.h"
#include "RooStudyManager.h"
#include "RooStats/ToyMCStudy.h"
#include "RooSimultaneous.h"
#include "RooProfileLL.h"
#include "RooMinuit.h"
#include "TMath.h"
#include "TTree.h"
#include "RooMinimizer.h"
#include "Math/MinimizerOptions.h"
using namespace RooFit;
ClassImp(RooStats::HybridToyMCSampler)
namespace RooStats {
class NuisanceParametersSamplerAndFitter {
// Helper for HybridToyMCSampler. Handles all of the nuisance parameter related
// functions. Once instantiated, it gives a new nuisance parameter point
// at each call to nextPoint(...).
public:
NuisanceParametersSamplerAndFitter(RooAbsPdf *prior=NULL, RooAbsPdf *fullPdf=NULL, RooAbsData* data=NULL , const RooArgSet *poi=NULL , const RooArgSet *parameters=NULL , Int_t nToys=1000, Bool_t asimov=kFALSE) :
fPrior(prior),
fPdf((RooAbsPdf*)fullPdf->cloneTree()),
fData(data),
fPOI((RooArgSet*)poi->snapshot()),
fParams(parameters),
fNToys(nToys),
fExpected(asimov),
fPoints(NULL),
fIndex(0)
{
if(prior) Refresh();
}
virtual ~NuisanceParametersSamplerAndFitter() {
if(fPoints) delete fPoints;
delete fPdf;
delete fPOI;
}
void NextPoint(RooArgSet& nuisPoint, Double_t& weight) {
// Assigns new nuisance parameter point to members of nuisPoint.
// nuisPoint can be more objects than just the nuisance
// parameters.
// check whether to get new set of nuisanceParPoints
if (fIndex >= fNToys) {
Refresh();
fIndex = 0;
}
//New Code for Hybrid Hybrid Calculator
if(fData)
{
RooArgSet * allVariables = fPdf->getParameters(*fData);
RemoveConstantParameters(allVariables);
*allVariables=*fPoints->get(fIndex++);
RooArgSet nuisancePlusPoi;
RooLinkedListIter it = fParams->iterator();
RooAbsArg* tmpPar = NULL;
RooRealVar* tmpParA = NULL;
while((tmpPar = (RooRealVar*)it.Next())){
tmpParA = dynamic_cast<RooRealVar*>( allVariables->find(tmpPar->GetName()) );
if (tmpParA) nuisancePlusPoi.add(*tmpParA);
//cout << "Adding variable " << tmpParA->GetName() << " to nuisancePlusPoi" << endl;
}
it=fPOI->iterator();
while((tmpPar = (RooRealVar*)it.Next())){
tmpParA = dynamic_cast<RooRealVar*>( allVariables->find(tmpPar->GetName()) );
if (tmpParA) nuisancePlusPoi.add(*tmpParA);
//cout << "Adding variable " << tmpParA->GetName() << " to nuisancePlusPoi" << endl;
}
nuisancePlusPoi=nuisPoint;
nuisancePlusPoi=*fPoints->get(fIndex++);
it = nuisancePlusPoi.iterator();
while((tmpParA = (RooRealVar*)it.Next())){
tmpParA->setConstant();
//cout << "Setting variable " << tmpParA->GetName() << " constant and to value " << tmpParA->getVal() << endl;
}
RooAbsCollection* allVarSnap = allVariables->snapshot();
//RooArgSet * allNuisance = fPrior->getVariables();
//RemoveConstantParameters(allNuisance);
//RooArgSet nuisancePlusPoi(*allNuisance);
//it = nuisancePlusPoi.iterator();
//while((tmpParA = (RooRealVar*)it.Next())){
// tmpParA->setConstant(kFALSE);
//}
RooAbsReal* nll = fPdf->createNLL(*fData);
int status =0;
MinNLL(nll,status);
it.Reset();
while((tmpParA = (RooRealVar*)it.Next())){
tmpParA->setConstant();
//cout << "Setting variable " << tmpParA->GetName() << " not constant and to value " << tmpParA->getVal() << endl;
}
//nuisancePlusPoi.Print("v");
//RooProfileLL* profile = (RooProfileLL*) nll->createProfile(nuisancePlusPoi);
//profile->setEvalErrorLoggingMode(RooAbsReal::CountErrors);
//profile->minuit()->setWarnLevel(-1);
//profile->getVal(); // this will do fit and set nuisance parameters to profiled values
// add nuisance parameters to parameter point
//cout << "========= First Values ==============" << endl;
//allVarSnap->Print("v");
//cout << "=====================================" << endl;
//cout << "========= Second Values =============" << endl;
//allVariables->Print("v");
//cout << "=====================================" << endl;
nuisPoint=*allVariables;
weight = fPoints->weight();
delete nll;
delete allVariables;
delete allVarSnap;
if(status != 0) NextPoint(nuisPoint, weight);
}
//End New Code
else{
// get value
nuisPoint = *fPoints->get(fIndex++);
weight = fPoints->weight();
}
// check whether result will have any influence
if(fPoints->weight() == 0.0) {
oocoutI((TObject*)NULL,Generation) << "Weight 0 encountered. Skipping." << endl;
NextPoint(nuisPoint, weight);
}
}
protected:
void Refresh() {
// Creates the initial set of nuisance parameter points. It also refills the
// set with new parameter points if called repeatedly. This helps with
// adaptive sampling as the required number of nuisance parameter points
// might increase during the run.
if (!fPrior || !fParams) return;
if (fPoints) delete fPoints;
if (fExpected) {
// UNDER CONSTRUCTION
oocoutI((TObject*)NULL,InputArguments) << "Using expected nuisance parameters." << endl;
int nBins = fNToys;
// From FeldmanCousins.cxx:
// set nbins for the POI
TIter it2 = fParams->createIterator();
RooRealVar *myarg2;
while ((myarg2 = dynamic_cast<RooRealVar*>(it2.Next()))) {
myarg2->setBins(nBins);
}
fPoints = fPrior->generate(
*fParams,
AllBinned(),
ExpectedData(),
NumEvents(1) // for Asimov set, this is only a scale factor
);
if(fPoints->numEntries() != fNToys) {
fNToys = fPoints->numEntries();
oocoutI((TObject*)NULL,InputArguments) <<
"Adjusted number of toys to number of bins of nuisance parameters: " << fNToys << endl;
}
/*
// check
TCanvas *c1 = new TCanvas;
RooPlot *p = dynamic_cast<RooRealVar*>(fParams->first())->frame();
fPoints->plotOn(p);
p->Draw();
for(int x=0; x < fPoints->numEntries(); x++) {
fPoints->get(x)->Print("v");
cout << fPoints->weight() << endl;
}
*/
}else{
oocoutI((TObject*)NULL,InputArguments) << "Using randomized nuisance parameters." << endl;
fPoints = fPrior->generate(*fParams, fNToys);
}
}
void MinNLL(RooAbsReal* NLL,int& status) {
//find minimum of NLL using RooMinimizer
TString fMinimizer;
Int_t fStrategy;
Double_t fTolerance;
Int_t fPrintLevel;
fMinimizer=::ROOT::Math::MinimizerOptions::DefaultMinimizerType().c_str();
fStrategy=::ROOT::Math::MinimizerOptions::DefaultStrategy();
// avoid default tolerance to be too small (1. is default in RooMinimizer)
fTolerance=TMath::Max(1.,::ROOT::Math::MinimizerOptions::DefaultTolerance());
fPrintLevel=::ROOT::Math::MinimizerOptions::DefaultPrintLevel();
RooMinimizer minim(*NLL);
minim.setStrategy(fStrategy);
minim.setEps(fTolerance);
//LM: RooMinimizer.setPrintLevel has +1 offset - so subtruct here -1 + an extra -1
int level = (fPrintLevel == 0) ? -1 : fPrintLevel -2;
minim.setPrintLevel(level);
minim.setEps(fTolerance);
// this cayses a memory leak
minim.optimizeConst(true);
for (int tries = 0, maxtries = 4; tries <= maxtries; ++tries) {
// status = minim.minimize(fMinimizer, ROOT::Math::MinimizerOptions::DefaultMinimizerAlgo().c_str());
status = minim.minimize(fMinimizer, "Minimize");
if (status == 0) {
break;
} else {
if (tries > 1) {
printf(" ----> Doing a re-scan first\n");
minim.minimize(fMinimizer,"Scan");
}
if (tries > 2) {
printf(" ----> trying with strategy = 1\n");
minim.setStrategy(1);
}
}
}
}
private:
RooAbsPdf *fPrior; // prior for nuisance parameters
RooAbsPdf *fPdf; // full Pdf parameters
RooAbsData *fData;
const RooArgSet *fPOI;
const RooArgSet *fParams; // nuisance parameters
Int_t fNToys;
Bool_t fExpected;
RooAbsData *fPoints; // generated nuisance parameter points
Int_t fIndex; // current index in fPoints array
};
HybridToyMCSampler::~HybridToyMCSampler() {
if(fNuisanceParametersSamplerAndFitter) delete fNuisanceParametersSamplerAndFitter;
}
//SamplingDistribution* HybridToyMCSampler::GetSamplingDistribution(RooArgSet& paramPointIn) {
// // Use for serial and parallel runs.
// //cout << "The paramPointIn is"<<endl;
// //paramPointIn.Print("v");
//
// //Modification to get a new variable definition for each parameter point for sampling.
//
// RooArgSet* allVars = NULL;
// RooArgSet* allVarsSnap = NULL;
// RooArgSet* paramPointInSnap = NULL;
// bool doTest(false);
// if(doTest && fFitToData)
// {
// //paramPointIn.Print("v");
// allVars = fPdf->getVariables();
// allVarsSnap = (RooArgSet*) allVars->snapshot();
// *allVars=paramPointIn;
// RooLinkedListIter it = fNullPOI->iterator();
// RooAbsArg* tmpPar = NULL;
// RooRealVar* tmpParA = NULL;
// while((tmpPar = (RooRealVar*)it.Next())){
// tmpParA = dynamic_cast<RooRealVar*>( allVars->find(tmpPar->GetName()) );
// if (tmpParA) tmpParA->setConstant();
// //cout << "Setting variable " << tmpParA->GetName() << " constant and to value " << tmpParA->getVal() << endl;
// }
// fPdf->fitTo(*fFitToData);
// it.Reset();
// while((tmpPar = (RooRealVar*)it.Next())){
// tmpParA = dynamic_cast<RooRealVar*>( allVars->find(tmpPar->GetName()) );
// if (tmpParA) tmpParA->setConstant(kFALSE);
// //cout << "Setting variable " << tmpParA->GetName() << " not constant with value " << tmpParA->getVal() << endl;
// }
// paramPointInSnap = (RooArgSet*) paramPointIn.snapshot();
// paramPointIn = *allVars ;
// //paramPointIn.Print("v");
// }
// SamplingDistribution *result = ToyMCSampler::GetSamplingDistribution(paramPointIn) ;
//
// if(allVarsSnap)
// {
// *allVars = *allVarsSnap;
// delete allVarsSnap;
// }
// if(paramPointInSnap)
// {
// paramPointIn = *paramPointInSnap;
// }
//
// return result;
//}
RooAbsData* HybridToyMCSampler::GenerateToyData(RooArgSet& paramPoint, double& weight) const {
// This method generates a toy data set for the given parameter point taking
// global observables into account.
// The values of the generated global observables remain in the pdf's variables.
// They have to have those values for the subsequent evaluation of the
// test statistics.
if(!fObservables) {
ooccoutE((TObject*)NULL,InputArguments) << "Observables not set." << endl;
return NULL;
}
if(fImportanceDensity) {
oocoutW((TObject*)NULL,InputArguments) << "HybridToyMCSampler: importance density given but ignored for generating toys." << endl;
}
// assign input paramPoint
RooArgSet* allVars = fPdf->getVariables();
*allVars = paramPoint;
//create nuisance parameter points
//if(fPriorNuisance)
// {
// RooArgSet* tempNuisanceVars = fPriorNuisance->getVariables();
// if(fNuisancePars) delete fNuisancePars;
// fNuisancePars = tempNuisanceVars;
// }
//fNuisancePars->Print();
if(!fNuisanceParametersSamplerAndFitter && fPriorNuisance && fNuisancePars)
fNuisanceParametersSamplerAndFitter = new NuisanceParametersSamplerAndFitter(fPriorNuisance, fPdf, fFitToData, fNullPOI, fNuisancePars, fNToys, fExpectedNuisancePar);
// generate global observables
RooArgSet observables(*fObservables);
if(fGlobalObservables && fGlobalObservables->getSize()) {
observables.remove(*fGlobalObservables);
GenerateGlobalObservables();
}
// save values to restore later.
// but this must remain after(!) generating global observables
const RooArgSet* saveVars = (const RooArgSet*)allVars->snapshot();
if(fNuisanceParametersSamplerAndFitter) { // use nuisance parameters?
// Construct a set of nuisance parameters that has the parameters
// in the input paramPoint removed. Therefore, no parameter in
// paramPoint is randomized.
// Therefore when a parameter is given (should be held fixed),
// but is also in the list of nuisance parameters, the parameter
// will be held fixed. This is useful for debugging to hold single
// parameters fixed although under "normal" circumstances it is
// randomized.
//RooArgSet nuisanceVars(*allVars);
//RemoveConstantParameters(&nuisanceVars);
//RooArgSet nonNuisanceVars(nuisanceVars);
//nonNuisanceVars.remove(*fNuisancePars,kFALSE,kTRUE);
//nuisanceVars.remove(nonNuisanceVars,kFALSE,kTRUE);
//RooArgSet* nuisanceSnap = (RooArgSet*)nuisanceVars.snapshot();
//RooArgSet* nonNuisanceSnap = (RooArgSet*)nonNuisanceVars.snapshot();
RooArgSet allVarsMinusParamPoint(*allVars);
allVarsMinusParamPoint.remove(paramPoint, kFALSE, kTRUE); // match by name
//allVarsMinusParamPoint.Print("v");
fNuisanceParametersSamplerAndFitter->NextPoint(allVarsMinusParamPoint, weight);
//allVarsMinusParamPoint.Print("v");
//nonNuisanceSnap->Print("v");
//nonNuisanceVars.Print("v");
//nuisanceSnap->Print("v");
//nuisanceVars.Print("v");
}else{
weight = -1.0;
}
RooAbsData *data = Generate(*fPdf, observables);
// We generated the data with the randomized nuisance parameter (if hybrid)
// but now we are setting the nuisance parameters back to where they were.
*allVars = *saveVars;
delete allVars;
delete saveVars;
return data;
}
} // end namespace RooStats