forked from zhang8473/twobody
/
dimuon.C
875 lines (703 loc) · 26.2 KB
/
dimuon.C
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <limits>
#include "TFile.h"
#include "TCanvas.h"
#include "TRandom3.h"
#include "TUnixSystem.h"
#include "TStopwatch.h"
#include "TMath.h"
#include "TLatex.h"
#include "TStyle.h"
#include "TF1.h"
#include "TFile.h"
#include "TTree.h"
#include "RooGlobalFunc.h"
#include "RooRandom.h"
#include "RooWorkspace.h"
#include "RooAbsPdf.h"
#include "RooCategory.h"
#include "RooSimultaneous.h"
#include "RooDataSet.h"
#include "RooRealVar.h"
#include "RooFitResult.h"
#include "RooPlot.h"
#include "RooHistPdf.h"
#include "Roo1DTable.h"
#include "RooStats/ModelConfig.h"
#include "RooStats/SimpleInterval.h"
#include "RooStats/BayesianCalculator.h"
#include "RooStats/MCMCCalculator.h"
#include "RooStats/MCMCInterval.h"
#include "RooStats/MCMCIntervalPlot.h"
#include "RooStats/SequentialProposal.h"
#include "RooStats/ProposalHelper.h"
#include "RooStats/ProfileLikelihoodCalculator.h"
#include "RooStats/LikelihoodInterval.h"
#include "RooStats/LikelihoodIntervalPlot.h"
#include "RooStats/HypoTestResult.h"
using namespace RooFit;
using namespace RooStats;
using namespace std;
class TwoBody{
//
// The class combines multiple channel analyses. A workspace is created
// with the combined model, data, model config. The class can also call
// interval calculation routines
//
public:
TwoBody();
~TwoBody();
void DimuonRatioLimit( Float_t peak,
std::string mode = "observed", // obsereved, expected, mass limit (extra k-factor uncertainty)
std::string suffix = "", // suffix for output file names
Int_t nruns = 1, // number of pseudoexperiments for expected limit
Int_t mcmc_iter = 100000, // number of MCMC iterations
Int_t mcmc_burnin = 100, // number of MCMC burn in steps to be discarded
std::string inputdir = "",// directory with workspace files
Double_t masswindow_width=-1.,
UInt_t minEvents=1000000 );
Int_t AddWorkspace(std::string filename,
std::string ws_name,
std::string channel_name,
std::string shared_vars);
ModelConfig prepareDimuonRatioModel( std::string inputdir );
double printMcmcUpperLimit( double peak, ModelConfig &_mc , std::string filename = "");
MCMCInterval * GetMcmcInterval(ModelConfig mc,
double conf_level,
int n_iter,
int n_burn,
double left_side_tail_fraction,
int n_bins);
MCMCInterval * GetMcmcInterval_OldWay(ModelConfig mc,
double conf_level,
int n_iter,
int n_burn,
double left_side_tail_fraction,
int n_bins);
LikelihoodInterval * GetPlrInterval( double conf_level, ModelConfig &_mc );
private:
Int_t CreateDimuonToyMc();
Double_t GetRandom( std::string pdf, std::string var );
Int_t FixVariables( std::set<std::string> par );
Double_t GetPoiUpperSimple(std::string channel, Double_t peak);
Double_t GetPoiUpper(std::string channel, Double_t peak, ModelConfig &_mc);
std::map<std::string, double> GetDataRange( RooAbsData * _data, double peak, int goal, Double_t window_width=0.2 );
RooAbsData * SetObservableRange( double peak, Double_t window_width=0.2, UInt_t minEvents=600 );
std::ofstream logfile;
RooAbsData * data, * realdata;
RooAbsPdf * model;
RooArgSet * poi;
RooArgSet * nuis;
RooAbsPdf * prior;
RooWorkspace * ws;
// roostats calculators results
MCMCInterval * mcInt;
bool bMcmcConverged;
TRandom3 r;
};
double TwoBody::printMcmcUpperLimit( double peak, ModelConfig &_mc, std::string filename ){
//
// print out the upper limit on the first Parameter of Interest
//
std::string _legend = "[TwoBody::printMcmcUpperLimit]: ";
char buf[1024];
double _limit = numeric_limits<double>::max();
if (mcInt){
//mc.SetWorkspace(*ws);
RooRealVar * firstPOI = (RooRealVar*) _mc.GetParametersOfInterest()->first();
_limit = mcInt->UpperLimit(*firstPOI);
std::cout << "\n95% upper limit on " << firstPOI->GetName() << " is : " <<
_limit << endl;
if (bMcmcConverged){
sprintf(buf, "%7.1f %7.6f", peak, _limit);
}
else{
sprintf(buf, "# %7.1f %7.6f # MCMC did not converge", peak, _limit);
}
}
else{
sprintf(buf, "# MCMC did not converge");
}
if (filename.size()!=0){
std::ofstream aFile;
// append to file if exists
aFile.open(filename.c_str(), std::ios_base::app);
aFile << buf << std::endl;
// close outfile here so it is safe even if subsequent iterations crash
aFile.close();
}
return _limit;
}
std::map<std::string, double>
TwoBody::GetDataRange( RooAbsData * _data,
double peak,
int goal, Double_t window_width ){
//
// Estimate the reduced observable range so either
// - ~goal events are used or
// - minimal range (so not to kill signal efficiency)
// for individual channel dataset
//
std::string legend = "[TwoBody::GetDataRange]: ";
double _total = _data->sumEntries();
// get data in a vector
std::vector<double> v_data;
for (int i=0; i!=_total; ++i){
RooRealVar * _var = (RooRealVar *)(_data->get(i)->first());
v_data.push_back(_var->getVal());
}
int iTotal = v_data.size();
// sort data vector
std::sort(v_data.begin(), v_data.end());
int iGoal = goal; // we want that many events in the range
double sig_low = max(peak*(1-window_width),v_data[0]); // signal box that must be in the range
double sig_high = min(peak*(1+window_width),v_data[iTotal-1]); // signal box that must be in the range
std::cout << legend << "data vector size: " << iTotal << "; want [" <<sig_low<<","<<sig_high<<"]"<<std::endl;
// find highest point in the signal box
int iPeak = iTotal;
while( iPeak != 0 ){
--iPeak;
if ( v_data[iPeak] < sig_high ) break;
}
int iSigHigh = iPeak;
if (v_data[iSigHigh]>sig_high) iSigHigh = -1; // all data higher than the signal box
if (v_data[iSigHigh]<sig_low) iSigHigh = iTotal; // all data lower than the signal box
// find lowest point in the signal box
iPeak = 0;
while( iPeak != (iTotal-1) ){
if ( v_data[iPeak] > sig_low ) break;
++iPeak;
}
int iSigLow = iPeak;
std::cout << legend << "all data lower than the signal box: " << iSigLow << std::endl;
if (v_data[iSigLow]<sig_low) iSigLow = iTotal; // all data lower than the signal box
if (v_data[iSigLow]>sig_high) iSigLow = -1; // all data higher than the signal box
// now expand the range starting from signal box if necessary
int iSignal = iSigHigh-iSigLow+1;
if ( iSigHigh==iTotal || iSigLow==-1 ) iSignal = 0;
while (iSignal<iGoal){
int iLow = std::max(iSigLow-1,0);
int iHigh = std::min(iSigHigh+1,iTotal-1);
double dLow = 0.0;
if ( iLow >= 0 ) dLow = std::max(0.0, sig_low - v_data[iLow]);
double dHigh = 0.0;
if ( iHigh < iTotal ) dHigh = std::max(0.0, v_data[iHigh] - sig_high);
if ( dLow < dHigh || iHigh <= iSigHigh ){
iSigLow = iLow;
sig_high += sig_low - v_data[iSigLow];
if (sig_high>v_data[iTotal-1]) sig_high=v_data[iTotal-1];
sig_low = v_data[iSigLow];
}
else{
iSigHigh = iHigh;
sig_low -= v_data[iSigHigh] - sig_high;
if (sig_low<0) sig_low=v_data[0];
sig_high = v_data[iSigHigh];
}
++iSignal;
}
std::cout << legend << "signal box index range: [" << iSigLow << ", " << iSigHigh << "]" << std::endl;
std::cout << legend << "signal box range: [" << sig_low << ", " << sig_high << "]" << std::endl;
std::cout << legend << "events in signal box: " << iSignal << std::endl;
// events above the signal box
int iAbove = iTotal - iSigHigh - 1;
if ( iAbove < 0 ) iAbove = 0;
if ( iAbove > iTotal ) iAbove= iTotal;
// events below the signal box
int iBelow = iSigLow;
if ( iBelow < 0 ) iBelow = 0;
std::cout << legend << "events below signal box: " << iBelow << std::endl;
std::cout << legend << "events above signal box: " << iAbove << std::endl;
// prepare return map
std::map<std::string, double> _mres;
_mres["low"] = sig_low;
_mres["high"] = sig_high;
return _mres;
}
RooAbsData * TwoBody::SetObservableRange( double peak, Double_t window_width, UInt_t minEvents ){
//
// Reduce the observable range so ~400 events are used
// for the full combined dataset
//
std::string legend = "[TwoBody::SetObservableRange]: ";
std::map<std::string, double> _range;
_range = GetDataRange( data, peak, minEvents, window_width );
char buf[256];
// Should not change the range!
//ws->var("mass")->setRange(_range["low"], _range["high"]);
//ws->var("mass")->Print();
// replace data
//int iTotal = (int)data->sumEntries();
sprintf(buf, "mass>%f && mass<%f", _range["low"], _range["high"]);
RooAbsData * _data = data->reduce( RooFit::Cut(buf) );
// correct the nbkg constraint accordingly
//double _nbkg = ws->var("nbkg_est_dimuon")->getVal();
//ws->var("nbkg_est_dimuon")->setVal(_nbkg*_data->sumEntries()/(double)(iTotal));
//ws->var("nbkg_est_dimuon")->Print();
data->Print();
_data->Print();
delete data;
return _data;
}
TwoBody::TwoBody(){
std::string legend = "[TwoBody::TwoBody]: ";
ws = new RooWorkspace("ws");
//mc.SetName("mc");
//mc.SetTitle("model_config");
data = 0;
model = 0;
poi = 0;
nuis = 0;
prior = 0;
mcInt = 0;
bMcmcConverged = false;
// set random seed
r.SetSeed();
UInt_t _seed = r.GetSeed();
UInt_t _pid = gSystem->GetPid();
std::cout << legend << "Random seed: " << _seed << std::endl;
std::cout << legend << "PID: " << _pid << std::endl;
_seed = 31*_seed+_pid;
std::cout << legend << "New random seed (31*seed+pid): " << _seed << std::endl;
r.SetSeed(_seed);
// set RooFit random seed (it has a private copy)
RooRandom::randomGenerator()->SetSeed(_seed);
// open log file
logfile.open("twobody.log");
}
TwoBody::~TwoBody(){
std::string _legend = "[TwoBody::~TwoBody]: ";
logfile.close();
delete ws;
delete data;
delete model;
delete poi;
delete nuis;
delete prior;
delete mcInt;
}
Int_t TwoBody::FixVariables( std::set<std::string> par ){
//
// Set all RooRealVars except <par> to be constants
//
Int_t _fixed = 0;
RooArgSet _vars = ws->allVars();
TIterator * iter = _vars.createIterator();
for(TObject * _obj = iter->Next(); _obj; _obj = iter->Next() ){
std::string _name = _obj->GetName();
if (par.find(_name) == par.end()){
RooRealVar * _var = (RooRealVar *)( _vars.find(_name.c_str()) );
_var->setConstant(kTRUE);
++_fixed;
}
}
delete iter;
return _fixed;
}
ModelConfig TwoBody::prepareDimuonRatioModel( std::string inputdir ){
//
// prepare workspace and ModelConfig for the dimuon xsec ratio limit
//
std::string _legend = "[TwoBody::prepareDimuonRatioModel]: ";
std::string _infile = inputdir+"ws_dimuon_ratio.root";
AddWorkspace(_infile.c_str(),
"myWS",
"dimuon",
"peak,mass,ratio");
ws->pdf("model_dimuon")->SetName("model");
//ws->Print();
// set all vars to const except <par>
std::set<std::string> par;
par.insert("mass");
par.insert("ratio");
par.insert("beta_nsig_dimuon");
// par.insert("beta_nbkg_dimuon");
//par.insert("beta_mass_dimuon");
FixVariables(par);
// POI
RooArgSet sPoi( *(ws->var("ratio")) );
// nuisance
RooArgSet sNuis( *(ws->var("beta_nsig_dimuon"))
//*(ws->var("beta_nbkg_dimuon")),
//*(ws->var("beta_mass_dimuon"))
);
// observables
RooArgSet sObs( *(ws->var("mass")) );
// prior
// ModelConfig
ModelConfig _mc("mc",ws);
_mc.SetPdf(*(ws->pdf("model")));
_mc.SetParametersOfInterest( sPoi );
_mc.SetPriorPdf( *(ws->pdf("prior_dimuon")) );
_mc.SetNuisanceParameters( sNuis );
_mc.SetObservables( sObs );
return _mc;
}
LikelihoodInterval *TwoBody::GetPlrInterval( double conf_level , ModelConfig &_mc){
//
// Profile likelihood ratio interval calculations
//
// cerr<<"Print ModelConfig: "<<endl;
//_mc.Print();
//data->Print();
ProfileLikelihoodCalculator plc(*data, _mc);
plc.SetConfidenceLevel(conf_level);
return plc.GetInterval();
}
double TwoBody::GetPoiUpperSimple(std::string channel, Double_t peak){
//
// Estimate a good value for the upper boundary of the range of POI
// using just data in a window corresponding to the signal region
//
std::string legend = "[TwoBody::GetPoiUpperSimple]: ";
char buf[128];
// special handling needed for single-channel workspaces
bool b_single_channel;
if (channel.size()==0) b_single_channel = true; else b_single_channel = false;
double _range = 1.0;
// get data yield under the signal peak
ws->var("peak")->setVal(peak);
if (b_single_channel) sprintf(buf,"width");
else sprintf(buf,"width_%s",channel.c_str());
double _width = 0;//ws->function(buf)->getVal();
if (b_single_channel) sprintf(buf,"sigma");
else sprintf(buf,"sigma_%s",channel.c_str());
double _sigma = ws->function(buf)->getVal();
double c_min = peak - sqrt(_width*_width + _sigma*_sigma);
double c_max = peak + sqrt(_width*_width + _sigma*_sigma);
sprintf(buf, "mass>=%f && mass<=%f", c_min, c_max);
double n_count = data->sumEntries( buf );
// ad-hoc fix when there are no events in window
if (n_count < 0.3) n_count = 0.3;
std::cout << legend << "event yield in data in the window: "
<< n_count << std::endl;
// compute the corresponding POI range
if (b_single_channel) sprintf(buf,"nsig_scale");
else sprintf(buf,"nsig_scale_%s",channel.c_str());
double _nsig_scale = ws->var(buf)->getVal();
if (b_single_channel) sprintf(buf,"nz");
else sprintf(buf,"nz_%s",channel.c_str());
double _nz = ws->var(buf)->getVal();
if (b_single_channel) sprintf(buf,"eff");
else sprintf(buf,"eff_%s",channel.c_str());
double _eff = ws->function(buf)->getVal();
double n_excess = 3.0 * sqrt(n_count)/0.68; // let signal excess be ~ uncertainty on BG
_range = n_excess / _nsig_scale / _nz / _eff;
return _range;
}
Double_t TwoBody::GetPoiUpper(std::string channel, Double_t peak, ModelConfig &_mc){
//
// Estimate a good value for the upper boundary of the range of POI
//
std::string legend = "[TwoBody::GetPoiUpper]: ";
double _range = -1.0;
std::cout << legend << "doing a rough estimate of the POI range" << std::endl;
// adding auto-channel option (multi) while
// preserving backwards compatibility
// if single channel
_range = GetPoiUpperSimple(channel, peak);
std::cout << legend << "crude estimate for poi range (x3): "
<< 3.0*_range << std::endl;
std::cout << legend
<< "this will be used if the profile likelihood ratio estimate fails"
<< std::endl;
std::cout << legend
<< "will try to estimate POI range better with profile likelihood ratio limit now"
<< std::endl;
Double_t result = 0.1;
// estimate limit with profile likelihood ratio and
// set the range to 3 times the limit
// query intervals
RooFit::MsgLevel msglevel = RooMsgService::instance().globalKillBelow();
RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
RooRealVar * _poi = (RooRealVar *)_mc.GetParametersOfInterest()->first();
double upper_limit = GetPlrInterval(0.95, _mc)->UpperLimit( *_poi );
RooMsgService::instance().setGlobalKillBelow(msglevel);
// safety in case upper limit == 0
if (upper_limit<std::numeric_limits<double>::min()){
upper_limit = _range;
}
result = 3.0*upper_limit;
return result;
}
Double_t TwoBody::GetRandom( std::string pdf, std::string var ){
//
// generates a random number using a pdf in the workspace
//
// generate a dataset with one entry
if (ws!=NULL) {
RooRealVar * _par = ws->var(var.c_str());
if (_par!=NULL) {
RooAbsPdf * _pdf=ws->pdf(pdf.c_str());
/*
RooPlot* xframe = _par->frame(Title("p.d.f")) ;
_pdf->plotOn(xframe);
TCanvas* c = new TCanvas("test","rf101_basics",800,400) ;
gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.6) ; xframe->Draw() ;
c->SaveAs("syst_nbkg.pdf");
*/
if (_pdf!=NULL) return _pdf->generate(*_par, 1)->get(0)->getRealValue(var.c_str(),0);
else {
std::cerr<<"Cannot find RooPdf:"<<pdf<<std::endl;
}
}
else std::cerr<<"Cannot find RooVar:"<<var<<std::endl;
}
else std::cerr<<"[BUG]workspace is deleted??"<<var<<std::endl;
return 0;
}
Int_t TwoBody::AddWorkspace(std::string filename,
std::string ws_name,
std::string channel_name,
std::string shared_vars){
//
// Load a single channel model and data from a workspace
//
std::string _legend = "[TwoBody::AddWorkspace]: ";
// load workspace from a file
TFile _file(filename.c_str(), "read");
RooWorkspace * _ws = (RooWorkspace *)_file.Get( ws_name.c_str() );
// get the single channel model PDF
RooAbsPdf * _model = _ws->pdf("model"),
* _prior = _ws->pdf("prior");
// import the channel model PDF into the combined workspace
// add prefix channel_name to all nodes except shared_vars
ws->import( RooArgSet(*_model,*_prior),
RooFit::RenameAllNodes( channel_name.c_str() ),
RooFit::RenameAllVariablesExcept(channel_name.c_str(), shared_vars.c_str()) );
delete data;
data = new RooDataSet( *(RooDataSet *) _ws->data("data") );
data->changeObservableName("vertex_m","mass");
//realdata = new RooDataSet( *(RooDataSet *) data );
//new RooDataSet( *(RooDataSet *) ws->data("data") );
//realdata->SetName("RealData");
//realdata->changeObservableName("vertex_m","mass");
ws->import( *data );
ws->Print();
// for (int i=0; i!=100; ++i){
// RooRealVar * _var = (RooRealVar *)(data->get(i)->first());
// cerr<<_var->getVal()<<",";
//}
_file.Close();
return 0;
}
Int_t TwoBody::CreateDimuonToyMc( void ){
//
// generate a toy di-muon dataset with systematics
// set mData accordingly
//
// generate expected number of events from its uncertainty
//RooDataSet * _ds = ws->pdf("syst_nbkg_dimuon")->generate(*ws->var("beta_nbkg_dimuon"), 1);
//Double_t _ntoy = ((RooRealVar *)(_ds->get(0)->first()))->getVal() * (ws->var("nbkg_est_dimuon")->getVal());
//delete _ds;
Double_t _beta = GetRandom("syst_nbkg_dimuon", "beta_nbkg_dimuon");
// Double_t _kappa = ws->var("nbkg_kappa_dimuon")->getVal();
Double_t _nbkg_est = ws->var("nbkg_est_dimuon")->getVal();
//Double_t _ntoy = pow(_kappa,_beta) * _nbkg_est;
Double_t _ntoy = _beta * _nbkg_est;
Int_t _n = r.Poisson(_ntoy);
// all nuisance parameters:
// beta_nsig_dimuon,
// beta_nbkg_dimuon,
// lumi_nuis
// create dataset
RooRealVar * _mass = ws->var("mass");
RooArgSet _vars(*_mass);
RooAbsPdf * _pdf = ws->pdf("bkgpdf_dimuon");
RooAbsPdf::GenSpec * _spec = _pdf->prepareMultiGen(_vars,
Name("toys"),
NumEvents(_n),
Extended(kFALSE),
Verbose(kTRUE));
//RooPlot* xframe = _mass->frame(Title("Gaussian p.d.f.")) ;
//realdata->plotOn(xframe,LineColor(kRed),MarkerColor(kRed));
delete data;
data = _pdf->generate(*_spec); // class member
delete _spec;
//data->plotOn(xframe);
//TCanvas* c = new TCanvas("test","rf101_basics",800,400) ;
//gPad->SetLeftMargin(0.15) ; xframe->GetYaxis()->SetTitleOffset(1.6) ; xframe->Draw() ;
//c->SaveAs("test.pdf");
Int_t n_generated_entries = (Int_t)(data->sumEntries());
// debug
std::cout << "!!!!!!!!!!!!!! _beta = " << _beta << std::endl;
//std::cout << "!!!!!!!!!!!!!! _kappa = " << _kappa << std::endl;
std::cout << "!!!!!!!!!!!!!! _nbkg_est = " << _nbkg_est << std::endl;
std::cout << "!!!!!!!!!!!!!! _ntoy = " << _ntoy << std::endl;
std::cout << "!!!!!!!!!!!!!! _n = " << _n << std::endl;
std::cout << "!!!!!!!!!!!!!! n_generated_entries = " << n_generated_entries << std::endl;
return n_generated_entries;
}
MCMCInterval * TwoBody::GetMcmcInterval(ModelConfig _mc,
double conf_level,
int n_iter,
int n_burn,
double left_side_tail_fraction,
int n_bins){
// use MCMCCalculator (takes about 1 min)
// Want an efficient proposal function, so derive it from covariance
// matrix of fit
std::string legend = "[TwoBody::GetMcmcInterval]: ";
// FIXME: testing: this proposal function seems fairly robust
SequentialProposal sp(0.5);
MCMCCalculator mcmc( *data, _mc );
mcmc.SetConfidenceLevel(conf_level);
mcmc.SetNumIters(n_iter); // Metropolis-Hastings algorithm iterations
mcmc.SetProposalFunction(sp);
mcmc.SetNumBurnInSteps(n_burn); // first N steps to be ignored as burn-in
mcmc.SetLeftSideTailFraction(left_side_tail_fraction);
mcmc.SetNumBins(n_bins);
// FIXME: testing good initial values - don't seem to do anything different
//ws->var("ratio")->setVal(0.01);
//ws->var("beta_nsig_dielectron")->setRange(-3.0, 3.0);
//ws->var("beta_nbkg_dielectron")->setRange(-3.0, 3.0);
//ws->var("beta_mass_dielectron")->setRange(-3.0, 3.0);
//mcInt = mcmc.GetInterval();
try {
delete mcInt;
mcInt = mcmc.GetInterval();
} catch ( std::length_error &ex) {
mcInt = 0;
}
cerr<<legend<<endl;
// check if limit makes sense
bMcmcConverged = false; // default
if (mcInt){
RooRealVar * p_first_poi = (RooRealVar*) _mc.GetParametersOfInterest()->first();
double poi_limit = mcInt->UpperLimit(*p_first_poi);
double u_poi_min = p_first_poi->getMin();
double u_poi_max = p_first_poi->getMax();
double u_poi_gap = (u_poi_max-poi_limit)/(u_poi_max-u_poi_min);
std::cout << legend << "POI upper limit: " << poi_limit << std::endl;
std::cout << legend << "POI range: [" << u_poi_min << ", " << u_poi_max << "]" << std::endl;
std::cout << legend << "POI upper gap (fraction of range): " << u_poi_gap << std::endl;
if (u_poi_gap<0.2){
std::cout << legend
<< "POI limit too close to the upper boundary, MCMC probably failed!!!" << std::endl;
std::cout << legend
<< "returning interval and setting fail flag" << std::endl;
bMcmcConverged = false;
}
else{
bMcmcConverged = true;
}
}
else std::cout << "No interval found!" << std::endl;
return mcInt;
}
MCMCInterval * TwoBody::GetMcmcInterval_OldWay(ModelConfig mc,
double conf_level,
int n_iter,
int n_burn,
double left_side_tail_fraction,
int n_bins){
// use MCMCCalculator (takes about 1 min)
// Want an efficient proposal function, so derive it from covariance
// matrix of fit
RooFitResult* fit = ws->pdf("model")->fitTo(*data,Save());
ProposalHelper ph;
ph.SetVariables((RooArgSet&)fit->floatParsFinal());
ph.SetCovMatrix(fit->covarianceMatrix());
ph.SetUpdateProposalParameters(kTRUE); // auto-create mean vars and add mappings
ph.SetCacheSize(100);
ProposalFunction* pf = ph.GetProposalFunction();
MCMCCalculator mcmc( *data, mc );
mcmc.SetConfidenceLevel(conf_level);
mcmc.SetNumIters(n_iter); // Metropolis-Hastings algorithm iterations
mcmc.SetProposalFunction(*pf);
mcmc.SetNumBurnInSteps(n_burn); // first N steps to be ignored as burn-in
mcmc.SetLeftSideTailFraction(left_side_tail_fraction);
mcmc.SetNumBins(n_bins);
//mcInt = mcmc.GetInterval();
try {
mcInt = mcmc.GetInterval();
} catch ( std::length_error &ex) {
mcInt = 0;
}
//std::cout << "!!!!!!!!!!!!!! interval" << std::endl;
if (mcInt == 0) std::cout << "No interval found!" << std::endl;
return mcInt;
}
void TwoBody::DimuonRatioLimit( Float_t peak,
std::string mode, // obsereved, expected, mass limit (extra k-factor uncertainty)
std::string suffix, // suffix for output file names
Int_t ntoys, // number of pseudoexperiments for expected limit
Int_t mcmc_iter,
Int_t mcmc_burnin,
std::string inputdir, // directory with workspace files
Double_t masswindow_width,
UInt_t minEvents ){
//
// limit on ratio = xsec(Z')/xsec(Z) in dimuon channel
//
std::string _legend = "[TwoBody::DimuonRatioLimit]: ";
ModelConfig _mc = prepareDimuonRatioModel(inputdir);
// set model parameters and data
ws->var("peak")->setVal(peak);
int pe_counter = 0;
std::vector<Double_t> _limits;
while (pe_counter < ntoys){
if ( mode.find("expected") != std::string::npos ){
std::cout << _legend << std::endl;
std::cout << _legend << "this is pseudoexperiment " << pe_counter+1 << " of " << ntoys << std::endl;
std::cout << _legend << "for the expected limit estimate" << std::endl;
std::cout << _legend << std::endl;
// prepare PE data
CreateDimuonToyMc();
if (masswindow_width>0) data=SetObservableRange(peak,masswindow_width,minEvents);
}
else { // "regular" observed limit
std::cout << _legend << std::endl;
std::cout << _legend << "calculating an observed limit..." << std::endl;
std::cout << _legend << "I will do it " << ntoys << " times, so one can average. " << pe_counter+1 << " of " << ntoys << std::endl;
std::cout << _legend << std::endl;
if (!pe_counter&&masswindow_width>0) data=SetObservableRange(peak,masswindow_width,minEvents);
//ntoys = 1;
}
// change POI range
_mc.SetWorkspace(*ws);
double poiUpper = GetPoiUpper("dimuon", peak, _mc);
std::cout << _legend << "setting POI range to [0; " << poiUpper << "]" << std::endl;
ws->var("ratio")->setRange(0.0, poiUpper);
// FIXME: try to restrict the range of observable
mcInt = GetMcmcInterval(_mc,
0.95,
mcmc_iter,
mcmc_burnin,
0.0,
100);
std::string _outfile = "dimuon_ratio_mcmc_limit_" + suffix + ".ascii";
printMcmcUpperLimit( peak, _mc, _outfile);
++pe_counter;
} // end of while
return;
}
Double_t limit( std::string channel, // dimuon, dielectron, mumuee, etc
std::string mode, // observed, expected, mass limit (extra k-factor uncertainty)
Float_t peak, // resonance mass
std::string suffix, // suffix for output file names
Int_t ntoys, // number of pseudoexperiments for expected limit
Int_t mcmc_iter, // number of MCMC iterations
Int_t mcmc_burnin, // number of MCMC burn in steps to be discarded
std::string inputdir,// directory with workspace files
Double_t masswindow_width,
UInt_t minEvents ){
// time it
TStopwatch t;
t.Start();
RooMsgService::instance().setGlobalKillBelow(RooFit::WARNING);
Double_t limit = -1.0;
TwoBody manager;
//dimuon single channel ratio limit
if (channel.find("dimuon") != std::string::npos ){
manager.DimuonRatioLimit(peak, mode, suffix,
ntoys, mcmc_iter, mcmc_burnin,
inputdir,masswindow_width,minEvents);
}
t.Print();
return limit;
}
void twobody(void){} // dummy
void dimuon(void){} // dummy