Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooNDKeysPdf.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitModels *
4 * File: $Id: RooNDKeysPdf.cxx 31258 2009-11-17 22:41:06Z wouter $
5 * Authors: *
6 * Max Baak, CERN, mbaak@cern.ch *
7 * *
8 * Redistribution and use in source and binary forms, *
9 * with or without modification, are permitted according to the terms *
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
11 *****************************************************************************/
12
13/** \class RooNDKeysPdf
14 \ingroup Roofit
15
16Generic N-dimensional implementation of a kernel estimation p.d.f. This p.d.f. models the distribution
17of an arbitrary input dataset as a superposition of Gaussian kernels, one for each data point,
18each contributing 1/N to the total integral of the p.d.f.
19If the 'adaptive mode' is enabled, the width of the Gaussian is adaptively calculated from the
20local density of events, i.e. narrow for regions with high event density to preserve details and
21wide for regions with log event density to promote smoothness. The details of the general algorithm
22are described in the following paper:
23Cranmer KS, Kernel Estimation in High-Energy Physics.
24 Computer Physics Communications 136:198-207,2001 - e-Print Archive: hep ex/0011057
25For multi-dimensional datasets, the kernels are modeled by multidimensional Gaussians. The kernels are
26constructed such that they reflect the correlation coefficients between the observables
27in the input dataset.
28**/
29
30#include <iostream>
31#include <algorithm>
32#include <string>
33
34#include "TMath.h"
35#include "TMatrixDSymEigen.h"
36#include "RooNDKeysPdf.h"
37#include "RooRealVar.h"
38#include "RooRandom.h"
39#include "RooHist.h"
40#include "RooMsgService.h"
41#include "RooChangeTracker.h"
42
43#include "TError.h"
44
45using std::string, std::vector, std::pair, std::map;
46
47
48////////////////////////////////////////////////////////////////////////////////
49/// Construct N-dimensional kernel estimation p.d.f. in observables 'varList'
50/// from dataset 'data'. Options can be
51///
52/// - 'a' = Use adaptive kernels (width varies with local event density)
53/// - 'm' = Mirror data points over observable boundaries. Improves modeling
54/// behavior at edges for distributions that are not close to zero
55/// at edge
56/// - 'd' = Debug flag
57/// - 'v' = Verbose flag
58///
59/// The parameter rho (default = 1) provides an overall scale factor that can
60/// be applied to the bandwidth calculated for each kernel. The nSigma parameter
61/// determines the size of the box that is used to search for contributing kernels
62/// around a given point in observable space. The nSigma parameters is used
63/// in case of non-adaptive bandwidths and for the 1st non-adaptive pass for
64/// the calculation of adaptive keys p.d.f.s.
65///
66/// The optional weight arguments allows to specify an observable or function
67/// expression in observables that specifies the weight of each event.
68
69RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const RooDataSet &data,
70 TString options, double rho, double nSigma, bool rotate, bool sortInput)
71 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
72 _rhoList("rhoList", "List of rho parameters", this), _options(options), _widthFactor(rho),
73 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
74{
76
77 createPdf(true, data);
78}
79
80////////////////////////////////////////////////////////////////////////////////
81/// Constructor
82
83RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const TH1 &hist,
84 TString options, double rho, double nSigma, bool rotate, bool sortInput)
85 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
86 _rhoList("rhoList", "List of rho parameters", this),
87 _options(options), _widthFactor(rho), _nSigma(nSigma), _rotate(rotate),
88 _sortInput(sortInput), _nAdpt(1)
89{
91
92 createPdf(true, *std::unique_ptr<RooDataSet>{createDatasetFromHist(varList, hist)});
93}
94
95////////////////////////////////////////////////////////////////////////////////
96/// Constructor
97
98RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const RooDataSet &data,
99 const TVectorD &rho, TString options, double nSigma, bool rotate, bool sortInput)
100 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
101 _rhoList("rhoList", "List of rho parameters", this), _options(options), _widthFactor(-1.0),
102 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
103{
104 _varList.addTyped<RooAbsReal>(varList);
105
106 // copy rho widths
107 if(int( _varList.size()) != rho.GetNrows() ) {
108 coutE(InputArguments)
109 << "ERROR: RooNDKeysPdf::RooNDKeysPdf() : The vector-size of rho is different from that of varList."
110 << "Unable to create the PDF." << std::endl;
111 R__ASSERT(int(_varList.size()) == rho.GetNrows());
112 }
113
114 // negative width factor will serve as a switch in initialize()
115 // negative value means that a vector has been provided as input,
116 // and that _rho has already been set ...
117 _rho.resize( rho.GetNrows() );
118 for (Int_t j = 0; j < rho.GetNrows(); j++) {
119 _rho[j] = rho[j]; /*cout<<"RooNDKeysPdf ctor, _rho["<<j<<"]="<<_rho[j]<< std::endl;*/
120 }
121
122 createPdf(true, data); // calls initialize ...
123}
124
125////////////////////////////////////////////////////////////////////////////////
126/// Backward compatibility constructor for (1-dim) RooKeysPdf. If you are a new user,
127/// please use the first constructor form.
128
129RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const RooDataSet &data,
130 const RooArgList &rhoList, TString options, double nSigma, bool rotate, bool sortInput)
131 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
132 _rhoList("rhoList", "List of rho parameters", this), _options(options), _widthFactor(-1.0),
133 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
134{
135 _varList.addTyped<RooAbsReal>(varList);
136
137 _rho.resize(rhoList.size(), 1.0);
138
139 for (unsigned int i=0; i < rhoList.size(); ++i) {
140 const auto rho = rhoList.at(i);
141 if (!dynamic_cast<const RooAbsReal *>(rho)) {
142 coutE(InputArguments) << "RooNDKeysPdf::ctor(" << GetName() << ") ERROR: parameter " << rho->GetName()
143 << " is not of type RooRealVar" << std::endl;
144 assert(0);
145 }
146 _rhoList.add(*rho);
147 _rho[i] = (dynamic_cast<RooAbsReal *>(rho))->getVal();
148 }
149
150 // copy rho widths
151 if ((_varList.size() != _rhoList.size())) {
152 coutE(InputArguments) << "ERROR: RooNDKeysPdf::RooNDKeysPdf() : The size of rhoList is different from varList."
153 << "Unable to create the PDF." << std::endl;
154 assert(_varList.size() == _rhoList.size());
155 }
156
157 // keep track of changes in rho parameters
158 _tracker = new RooChangeTracker("tracker", "track rho parameters", _rhoList, true); // check for value updates
159 (void)_tracker->hasChanged(true); // first evaluation always true for new parameters (?)
160
161 createPdf(true, data);
162}
163
164////////////////////////////////////////////////////////////////////////////////
165/// Constructor
166
167RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const TH1 &hist,
168 const RooArgList &rhoList, TString options, double nSigma, bool rotate, bool sortInput)
169 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
170 _rhoList("rhoList", "List of rho parameters", this),
171 _options(options), _widthFactor(-1), _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput),
172 _nAdpt(1)
173{
174 _varList.addTyped<RooAbsReal>(varList);
175
176 // copy rho widths
177 _rho.resize(rhoList.size(), 1.0);
178
179 for (unsigned int i=0; i < rhoList.size(); ++i) {
180 const auto rho = rhoList.at(i);
181 if (!dynamic_cast<RooAbsReal *>(rho)) {
182 coutE(InputArguments) << "RooNDKeysPdf::ctor(" << GetName() << ") ERROR: parameter " << rho->GetName()
183 << " is not of type RooRealVar" << std::endl;
184 assert(0);
185 }
186 _rhoList.add(*rho);
187 _rho[i] = (dynamic_cast<RooAbsReal *>(rho))->getVal();
188 }
189
190 if ((_varList.size() != _rhoList.size())) {
191 coutE(InputArguments) << "ERROR: RooNDKeysPdf::RooNDKeysPdf() : The size of rhoList is different from varList."
192 << "Unable to create the PDF." << std::endl;
193 assert(_varList.size() == _rhoList.size());
194 }
195
196 // keep track of changes in rho parameters
197 _tracker = new RooChangeTracker("tracker", "track rho parameters", _rhoList, true); // check for value updates
198 (void)_tracker->hasChanged(true); // first evaluation always true for new parameters (?)
199
200 createPdf(true, *std::unique_ptr<RooDataSet>{createDatasetFromHist(varList, hist)});
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Constructor
205
206RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, RooAbsReal &x, const RooDataSet &data, Mirror mirror,
207 double rho, double nSigma, bool rotate, bool sortInput)
208 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
209 _rhoList("rhoList", "List of rho parameters", this), _options("a"), _widthFactor(rho),
210 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
211{
212 _varList.add(x);
213
214 if (mirror != NoMirror) {
215 if (mirror != MirrorBoth) {
216 coutW(InputArguments) << "RooNDKeysPdf::RooNDKeysPdf() : Warning : asymmetric mirror(s) no longer supported."
217 << std::endl;
218 }
219 _options = "m";
220 }
221
222 createPdf(true, data);
223}
224
225////////////////////////////////////////////////////////////////////////////////
226/// Backward compatibility constructor for Roo2DKeysPdf. If you are a new user,
227/// please use the first constructor form.
228
229RooNDKeysPdf::RooNDKeysPdf(const char *name, const char *title, RooAbsReal &x, RooAbsReal &y, const RooDataSet &data,
230 TString options, double rho, double nSigma, bool rotate, bool sortInput)
231 : RooAbsPdf(name, title), _varList("varList", "List of variables", this),
232 _rhoList("rhoList", "List of rho parameters", this), _options(options), _widthFactor(rho),
233 _nSigma(nSigma), _rotate(rotate), _sortInput(sortInput), _nAdpt(1)
234{
236
237 createPdf(true, data);
238}
239
240////////////////////////////////////////////////////////////////////////////////
241/// Constructor
242
244 : RooAbsPdf(other, name),
245 _varList("varList", this, other._varList),
246 _rhoList("rhoList", this, other._rhoList),
247 _options(other._options),
248 _widthFactor(other._widthFactor),
249 _nSigma(other._nSigma),
250 _fixedShape(other._fixedShape),
251 _mirror(other._mirror),
252 _debug(other._debug),
253 _verbose(other._verbose),
254 _nDim(other._nDim),
255 _nEvents(other._nEvents),
256 _nEventsM(other._nEventsM),
257 _nEventsW(other._nEventsW),
258 _d(other._d),
259 _n(other._n),
260 _dataPts(other._dataPts),
261 _dataPtsR(other._dataPtsR),
262 _weights0(other._weights0),
263 _weights1(other._weights1),
264 _weights(_options.Contains("a") ? &_weights1 : &_weights0),
265 _sortTVIdcs(other._sortTVIdcs),
266 _rho(other._rho),
267 _x(other._x),
268 _x0(other._x0),
269 _x1(other._x1),
270 _x2(other._x2),
271 _mean(other._mean),
272 _sigma(other._sigma),
273 _xDatLo(other._xDatLo),
274 _xDatHi(other._xDatHi),
275 _xDatLo3s(other._xDatLo3s),
276 _xDatHi3s(other._xDatHi3s),
277 _netFluxZ(other._netFluxZ),
278 _nEventsBW(other._nEventsBW),
279 _nEventsBMSW(other._nEventsBMSW),
280 _xVarLo(other._xVarLo),
281 _xVarHi(other._xVarHi),
282 _xVarLoM3s(other._xVarLoM3s),
283 _xVarLoP3s(other._xVarLoP3s),
284 _xVarHiM3s(other._xVarHiM3s),
285 _xVarHiP3s(other._xVarHiP3s),
286 _bpsIdcs(other._bpsIdcs),
287 _ibNoSort(other._ibNoSort),
288 _sIdcs(other._sIdcs),
289 _bIdcs(other._bIdcs),
290 _bmsIdcs(other._bmsIdcs),
291 _rangeBoxInfo(other._rangeBoxInfo),
292 _fullBoxInfo(other._fullBoxInfo),
293 _idx(other._idx),
294 _minWeight(other._minWeight),
295 _maxWeight(other._maxWeight),
296 _wMap(other._wMap),
297 _covMat(new TMatrixDSym(*other._covMat)),
298 _corrMat(new TMatrixDSym(*other._corrMat)),
299 _rotMat(new TMatrixD(*other._rotMat)),
300 _sigmaR(new TVectorD(*other._sigmaR)),
301 _dx(new TVectorD(*other._dx)),
302 _sigmaAvgR(other._sigmaAvgR),
303 _rotate(other._rotate),
304 _sortInput(other._sortInput),
305 _nAdpt(other._nAdpt),
306 _tracker(other._tracker != nullptr ? new RooChangeTracker(*other._tracker) : nullptr)
307{
308}
309
310////////////////////////////////////////////////////////////////////////////////
311
313{
314 if (_covMat) delete _covMat;
315 if (_corrMat) delete _corrMat;
316 if (_rotMat) delete _rotMat;
317 if (_sigmaR) delete _sigmaR;
318 if (_dx) delete _dx;
319 if (_tracker)
320 delete _tracker;
321
322 // delete all the boxinfos map
323 while ( !_rangeBoxInfo.empty() ) {
324 map<pair<string,int>,BoxInfo*>::iterator iter = _rangeBoxInfo.begin();
325 BoxInfo* box= (*iter).second;
326 _rangeBoxInfo.erase(iter);
327 delete box;
328 }
329}
330
331////////////////////////////////////////////////////////////////////////////////
332/// evaluation order of constructor.
333
334void RooNDKeysPdf::createPdf(bool firstCall, RooDataSet const& data)
335{
336 if (firstCall) {
337 // set options
338 setOptions();
339 // initialization
341 }
342
343
344 // copy dataset, calculate sigma_i's, determine min and max event weight
345 loadDataSet(firstCall, data);
346
347 // mirror dataset around dataset boundaries -- does not depend on event weights
348 if (_mirror) mirrorDataSet();
349
350 // store indices and weights of events with high enough weights
352
353 // store indices of events in variable boundaries and box shell.
354//calculateShell(&_fullBoxInfo);
355 // calculate normalization needed in analyticalIntegral()
356//calculatePreNorm(&_fullBoxInfo);
357
358 // lookup table for determining which events contribute to a certain coordinate
359 const_cast<RooNDKeysPdf*>(this)->sortDataIndices();
360
361 // determine static and/or adaptive bandwidth
363}
364
365////////////////////////////////////////////////////////////////////////////////
366/// set the configuration
367
369{
371
372 if( _options.Contains("a") ) { _weights = &_weights1; }
373 else { _weights = &_weights0; }
374 if( _options.Contains("m") ) _mirror = true;
375 else _mirror = false;
376 if( _options.Contains("d") ) _debug = true;
377 else _debug = false;
378 if( _options.Contains("v") ) { _debug = true; _verbose = true; }
379 else { _debug = false; _verbose = false; }
380
381 cxcoutD(InputArguments) << "RooNDKeysPdf::setOptions() options = " << _options
382 << "\n\tbandWidthType = " << _options.Contains("a")
383 << "\n\tmirror = " << _mirror
384 << "\n\tdebug = " << _debug
385 << "\n\tverbose = " << _verbose
386 << std::endl;
387
388 if (_nSigma<2.0) {
389 coutW(InputArguments) << "RooNDKeysPdf::setOptions() : Warning : nSigma = " << _nSigma << " < 2.0. "
390 << "Calculated normalization could be too large."
391 << std::endl;
392 }
393
394 // number of adaptive width iterations. Default is 1.
395 if (_options.Contains("a")) {
396 if (!sscanf(_options.Data(), "%d%*s", &_nAdpt)) {
397 _nAdpt = 1;
398 }
399 }
400}
401
402////////////////////////////////////////////////////////////////////////////////
403/// initialization
404
406{
407 _nDim = _varList.size();
408 _nEvents = (Int_t)data.numEntries();
410 _fixedShape= false;
411
412 _netFluxZ = false;
413 _nEventsBW = 0;
414 _nEventsBMSW = 0;
415
416 if(_nDim==0) {
417 coutE(InputArguments) << "ERROR: RooNDKeysPdf::initialize() : The observable list is empty. "
418 << "Unable to begin generating the PDF." << std::endl;
419 R__ASSERT (_nDim!=0);
420 }
421
422 if(_nEvents==0) {
423 coutE(InputArguments) << "ERROR: RooNDKeysPdf::initialize() : The input data set is empty. "
424 << "Unable to begin generating the PDF." << std::endl;
425 R__ASSERT (_nEvents!=0);
426 }
427
428 _d = static_cast<double>(_nDim);
429
430 std::vector<double> dummy(_nDim,0.);
431 _dataPts.resize(_nEvents,dummy);
432 _weights0.resize(_nEvents,dummy);
433
434 //rdh _rho.resize(_nDim,_widthFactor);
435
436 if (_widthFactor>0) { _rho.resize(_nDim,_widthFactor); }
437 // else: _rho has been provided as external input
438
439 _x.resize(_nDim,0.);
440 _x0.resize(_nDim,0.);
441 _x1.resize(_nDim,0.);
442 _x2.resize(_nDim,0.);
443
444 _mean.resize(_nDim,0.);
445 _sigma.resize(_nDim,0.);
446
447 _xDatLo.resize(_nDim,0.);
448 _xDatHi.resize(_nDim,0.);
449 _xDatLo3s.resize(_nDim,0.);
450 _xDatHi3s.resize(_nDim,0.);
451
452 boxInfoInit(&_fullBoxInfo,nullptr,0xFFFF);
453
454 _minWeight=0;
455 _maxWeight=0;
456 _wMap.clear();
457
458 _covMat = nullptr;
459 _corrMat= nullptr;
460 _rotMat = nullptr;
461 _sigmaR = nullptr;
462 _dx = new TVectorD(_nDim); _dx->Zero();
463 _dataPtsR.resize(_nEvents,*_dx);
464
465 for(unsigned int j=0; j < _varList.size(); ++j) {
466 auto& var = static_cast<const RooRealVar&>(_varList[j]);
467 _xDatLo[j] = var.getMin();
468 _xDatHi[j] = var.getMax();
469 }
470}
471
472////////////////////////////////////////////////////////////////////////////////
473/// copy the dataset and calculate some useful variables
474
475void RooNDKeysPdf::loadDataSet(bool firstCall, RooDataSet const& data)
476{
477 // first some initialization
478 _nEventsW=0.;
479
480 TMatrixD mat(_nDim,_nDim);
481 if (!_covMat) _covMat = new TMatrixDSym(_nDim);
483 if (!_rotMat) _rotMat = new TMatrixD(_nDim,_nDim);
484 if (!_sigmaR) _sigmaR = new TVectorD(_nDim);
485
486 mat.Zero();
487 _covMat->Zero();
488 _corrMat->Zero();
489 _rotMat->Zero();
490 _sigmaR->Zero();
491
492 const RooArgSet* values= data.get();
493 vector<RooRealVar*> dVars(_nDim);
494 for (Int_t j=0; j<_nDim; j++) {
495 dVars[j] = static_cast<RooRealVar*>(values->find(_varList[j].GetName()));
496 _x0[j]=_x1[j]=_x2[j]=0.;
497 }
498
499 _idx.clear();
500 for (Int_t i=0; i<_nEvents; i++) {
501
502 data.get(i); // fills dVars
503 _idx.push_back(i);
504 std::vector<double>& point = _dataPts[i];
505 TVectorD& pointV = _dataPtsR[i];
506
507 double myweight = data.weight(); // default is one?
508 if ( std::abs(myweight)>_maxWeight ) { _maxWeight = std::abs(myweight); }
509 _nEventsW += myweight;
510
511 for (Int_t j=0; j<_nDim; j++) {
512 for (Int_t k=0; k<_nDim; k++) {
513 mat(j,k) += dVars[j]->getVal() * dVars[k]->getVal() * myweight;
514 }
515 // only need to do once
516 if (firstCall)
517 point[j] = pointV[j] = dVars[j]->getVal();
518
519 _x0[j] += 1. * myweight;
520 _x1[j] += point[j] * myweight ;
521 _x2[j] += point[j] * point[j] * myweight ;
522 if (_x2[j]!=_x2[j]) exit(3);
523
524 // only need to do once
525 if (firstCall) {
526 if (point[j]<_xDatLo[j]) { _xDatLo[j]=point[j]; }
527 if (point[j]>_xDatHi[j]) { _xDatHi[j]=point[j]; }
528 }
529 }
530 }
531
532 _n = std::pow(4./(_nEventsW*(_d+2.)), 1./(_d+4.)) ;
533 // = (4/[n(dim(R) + 2)])^1/(dim(R)+4); dim(R) = 2
534 _minWeight = (0.5 - std::erf(_nSigma/sqrt(2.))/2.) * _maxWeight;
535
536 for (Int_t j=0; j<_nDim; j++) {
537 _mean[j] = _x1[j]/_x0[j];
538 _sigma[j] = sqrt(_x2[j]/_x0[j]-_mean[j]*_mean[j]);
539 }
540
541 for (Int_t j=0; j<_nDim; j++) {
542 for (Int_t k=0; k<_nDim; k++) {
543 (*_covMat)(j,k) = mat(j,k)/_x0[j] - _mean[j]*_mean[k];
544 }
545 }
546
547 for (Int_t j=0; j<_nDim; j++) {
548 for (Int_t k=0; k<_nDim; k++)
549 (*_corrMat)(j,k) = (*_covMat)(j,k)/(_sigma[j]*_sigma[k]) ;
550 }
551
552 // use raw sigmas (without rho) for sigmaAvgR
553 TMatrixDSymEigen evCalculator(*_covMat);
554 TVectorD sigmaRraw = evCalculator.GetEigenValues();
555 for (Int_t j=0; j<_nDim; j++) { sigmaRraw[j] = sqrt(sigmaRraw[j]); }
556
557 _sigmaAvgR=1.;
558 for (Int_t j=0; j<_nDim; j++) { _sigmaAvgR *= sigmaRraw[j]; }
559 _sigmaAvgR = std::pow(_sigmaAvgR, 1./_d) ;
560
561 // find decorrelation matrix and eigenvalues (R)
562 if (_nDim > 1 && _rotate) {
563 // new: rotation matrix now independent of rho evaluation
564 *_rotMat = evCalculator.GetEigenVectors();
565 *_rotMat = _rotMat->T(); // transpose
566 } else {
567 TMatrixD haar(_nDim, _nDim);
568 TMatrixD unit(TMatrixD::kUnit, haar);
569 *_rotMat = unit;
570 }
571
572 // update sigmas (rho dependent)
573 updateRho();
574
575 //// rho no longer used after this.
576 //// Now set rho = 1 because sigmaR now contains rho
577 // for (Int_t j=0; j<_nDim; j++) { _rho[j] = 1.; } // reset: important!
578
579 if (_verbose) {
580 //_covMat->Print();
581 _rotMat->Print();
582 _corrMat->Print();
583 _sigmaR->Print();
584 }
585
586 if (_nDim > 1 && _rotate) {
587 // apply rotation
588 for (Int_t i = 0; i < _nEvents; i++) {
589 TVectorD &pointR = _dataPtsR[i];
590 pointR *= *_rotMat;
591 }
592 }
593
594 coutI(Contents) << "RooNDKeysPdf::loadDataSet(" << this << ")"
595 << "\n Number of events in dataset: " << _nEvents
596 << "\n Weighted number of events in dataset: " << _nEventsW << std::endl;
597}
598
599////////////////////////////////////////////////////////////////////////////////
600/// determine mirror dataset.
601/// mirror points are added around the physical boundaries of the dataset
602/// Two steps:
603/// 1. For each entry, determine if it should be mirrored (the mirror configuration).
604/// 2. For each mirror configuration, make the mirror points.
605
607{
608 for (Int_t j=0; j<_nDim; j++) {
609 _xDatLo3s[j] = _xDatLo[j] + _nSigma * (_n * _sigma[j]);
610 _xDatHi3s[j] = _xDatHi[j] - _nSigma * (_n * _sigma[j]);
611
612 // std::cout<<"xDatLo3s["<<j<<"]="<<_xDatLo3s[j]<< std::endl;
613 // std::cout<<"xDatHi3s["<<j<<"]="<<_xDatHi3s[j]<< std::endl;
614 }
615
616 vector<double> dummy(_nDim,0.);
617
618 // 1.
619 for (Int_t i=0; i<_nEvents; i++) {
620 vector<double>& x = _dataPts[i];
621
622 Int_t size = 1;
623 vector<vector<double> > mpoints(size,dummy);
624 vector<vector<Int_t> > mjdcs(size);
625
626 // create all mirror configurations for event i
627 for (Int_t j=0; j<_nDim; j++) {
628
629 vector<Int_t>& mjdxK = mjdcs[0];
630 vector<double>& mpointK = mpoints[0];
631
632 // single mirror *at physical boundaries*
633 if ((x[j]>_xDatLo[j] && x[j]<_xDatLo3s[j]) && x[j]<(_xDatLo[j]+_xDatHi[j])/2.) {
634 mpointK[j] = 2.*_xDatLo[j]-x[j];
635 mjdxK.push_back(j);
636 } else if ((x[j]<_xDatHi[j] && x[j]>_xDatHi3s[j]) && x[j]>(_xDatLo[j]+_xDatHi[j])/2.) {
637 mpointK[j] = 2.*_xDatHi[j]-x[j];
638 mjdxK.push_back(j);
639 }
640 }
641
642 vector<Int_t>& mjdx0 = mjdcs[0];
643 // no mirror point(s) for this event
644 if (size==1 && mjdx0.empty()) continue;
645
646 // 2.
647 // generate all mirror points for event i
648 vector<Int_t>& mjdx = mjdcs[0];
649 vector<double>& mpoint = mpoints[0];
650
651 // number of mirror points for this mirror configuration
652 Int_t eMir = 1 << mjdx.size();
653 vector<vector<double> > epoints(eMir,x);
654
655 for (Int_t m=0; m<Int_t(mjdx.size()); m++) {
656 Int_t size1 = 1 << m;
657 Int_t size2 = 1 << (m+1);
658 // copy all previous mirror points
659 for (Int_t l=size1; l<size2; ++l) {
660 epoints[l] = epoints[l-size1];
661 // fill high mirror points
662 vector<double>& epoint = epoints[l];
663 epoint[mjdx[Int_t(mjdx.size()-1)-m]] = mpoint[mjdx[Int_t(mjdx.size()-1)-m]];
664 }
665 }
666
667 // remove duplicate mirror points
668 // note that: first epoint == x
669 epoints.erase(epoints.begin());
670
671 // add mirror points of event i to total dataset
672 TVectorD pointR(_nDim);
673
674 for (Int_t m=0; m<Int_t(epoints.size()); m++) {
675 _idx.push_back(i);
676 _dataPts.push_back(epoints[m]);
677 //_weights0.push_back(_weights0[i]);
678 for (Int_t j=0; j<_nDim; j++) { pointR[j] = (epoints[m])[j]; }
679 if (_nDim > 1 && _rotate) {
680 pointR *= *_rotMat;
681 }
682 _dataPtsR.push_back(pointR);
683 }
684
685 epoints.clear();
686 mpoints.clear();
687 mjdcs.clear();
688 } // end of event loop
689
690 _nEventsM = Int_t(_dataPts.size());
691}
692
693////////////////////////////////////////////////////////////////////////////////
694
696{
697 _wMap.clear();
698
699 for (Int_t i=0; i<_nEventsM; i++) {
700 data.get(_idx[i]);
701 double myweight = data.weight();
702 //if ( std::abs(myweight)>_minWeight ) {
703 _wMap[i] = myweight;
704 //}
705 }
706
707 coutI(Contents) << "RooNDKeysPdf::loadWeightSet(" << this << ") : Number of weighted events : " << _wMap.size() << std::endl;
708}
709
710////////////////////////////////////////////////////////////////////////////////
711/// determine points in +/- nSigma shell around the box determined by the variable
712/// ranges. These points are needed in the normalization, to determine probability
713/// leakage in and out of the box.
714
716{
717 for (Int_t j=0; j<_nDim; j++) {
718 if (bi->xVarLo[j]==_xDatLo[j] && bi->xVarHi[j]==_xDatHi[j]) {
719 bi->netFluxZ = bi->netFluxZ && true;
720 } else { bi->netFluxZ = false; }
721
722 bi->xVarLoM3s[j] = bi->xVarLo[j] - _nSigma * (_n * _sigma[j]);
723 bi->xVarLoP3s[j] = bi->xVarLo[j] + _nSigma * (_n * _sigma[j]);
724 bi->xVarHiM3s[j] = bi->xVarHi[j] - _nSigma * (_n * _sigma[j]);
725 bi->xVarHiP3s[j] = bi->xVarHi[j] + _nSigma * (_n * _sigma[j]);
726
727 //cout<<"bi->xVarLoM3s["<<j<<"]="<<bi->xVarLoM3s[j]<< std::endl;
728 //cout<<"bi->xVarLoP3s["<<j<<"]="<<bi->xVarLoP3s[j]<< std::endl;
729 //cout<<"bi->xVarHiM3s["<<j<<"]="<<bi->xVarHiM3s[j]<< std::endl;
730 //cout<<"bi->xVarHiM3s["<<j<<"]="<<bi->xVarHiM3s[j]<< std::endl;
731 }
732
733 //for (Int_t i=0; i<_nEventsM; i++) {
734 for (const auto& wMapItr : _wMap) {
735 Int_t i = wMapItr.first;
736
737 const vector<double>& x = _dataPts[i];
738 bool inVarRange(true);
739 bool inVarRangePlusShell(true);
740
741 for (Int_t j=0; j<_nDim; j++) {
742
743 if (x[j]>bi->xVarLo[j] && x[j]<bi->xVarHi[j]) {
744 inVarRange = inVarRange && true;
745 } else { inVarRange = inVarRange && false; }
746
747 if (x[j]>bi->xVarLoM3s[j] && x[j]<bi->xVarHiP3s[j]) {
748 inVarRangePlusShell = inVarRangePlusShell && true;
749 } else { inVarRangePlusShell = inVarRangePlusShell && false; }
750 }
751
752 // event in range?
753 if (inVarRange) {
754 bi->bIdcs.push_back(i);
755 }
756
757 // event in shell?
758 if (inVarRangePlusShell) {
759 bi->bpsIdcs[i] = true;
760 bool inShell(false);
761 for (Int_t j=0; j<_nDim; j++) {
762 if ((x[j]>bi->xVarLoM3s[j] && x[j]<bi->xVarLoP3s[j]) && x[j]<(bi->xVarLo[j]+bi->xVarHi[j])/2.) {
763 inShell = true;
764 } else if ((x[j]>bi->xVarHiM3s[j] && x[j]<bi->xVarHiP3s[j]) && x[j]>(bi->xVarLo[j]+bi->xVarHi[j])/2.) {
765 inShell = true;
766 }
767 }
768 if (inShell) bi->sIdcs.push_back(i); // needed for normalization
769 else {
770 bi->bmsIdcs.push_back(i); // idem
771 }
772 }
773 }
774
775 coutI(Contents) << "RooNDKeysPdf::calculateShell() : "
776 << "\n Events in shell " << bi->sIdcs.size()
777 << "\n Events in box " << bi->bIdcs.size()
778 << "\n Events in box and shell " << bi->bpsIdcs.size()
779 << std::endl;
780}
781
782////////////////////////////////////////////////////////////////////////////////
783///bi->nEventsBMSW=0.;
784///bi->nEventsBW=0.;
785
787{
788 // box minus shell
789 for (Int_t i=0; i<Int_t(bi->bmsIdcs.size()); i++)
790 bi->nEventsBMSW += _wMap.at(bi->bmsIdcs[i]);
791
792 // box
793 for (Int_t i=0; i<Int_t(bi->bIdcs.size()); i++)
794 bi->nEventsBW += _wMap.at(bi->bIdcs[i]);
795
796 cxcoutD(Eval) << "RooNDKeysPdf::calculatePreNorm() : "
797 << "\n nEventsBMSW " << bi->nEventsBMSW
798 << "\n nEventsBW " << bi->nEventsBW
799 << std::endl;
800}
801
802////////////////////////////////////////////////////////////////////////////////
803/// sort entries, as needed for loopRange()
804
806{
807 // will loop over all events by default
808 if (!_sortInput) {
809 _ibNoSort.clear();
810 for (unsigned int i = 0; i < _dataPtsR.size(); ++i) {
811 _ibNoSort[i] = true;
812 }
813 return;
814 }
815
816 itVec itrVecR;
817 vector<TVectorD>::iterator dpRItr = _dataPtsR.begin();
818 for (Int_t i = 0; dpRItr != _dataPtsR.end(); ++dpRItr, ++i) {
819 if (bi) {
820 if (bi->bpsIdcs.find(i) != bi->bpsIdcs.end()) {
821 // if (_wMap.find(i)!=_wMap.end())
822 itrVecR.push_back(itPair(i, dpRItr));
823 }
824 } else {
825 itrVecR.push_back(itPair(i, dpRItr));
826 }
827 }
828
829 _sortTVIdcs.resize(_nDim);
830 for (Int_t j=0; j<_nDim; j++) {
831 _sortTVIdcs[j].clear();
832 sort(itrVecR.begin(), itrVecR.end(), [=](const itPair& a, const itPair& b) {
833 return (*a.second)[j] < (*b.second)[j];
834 });
835 _sortTVIdcs[j] = itrVecR;
836 }
837
838 for (Int_t j=0; j<_nDim; j++) {
839 cxcoutD(Eval) << "RooNDKeysPdf::sortDataIndices() : Number of sorted events : " << _sortTVIdcs[j].size() << std::endl;
840 }
841}
842
843////////////////////////////////////////////////////////////////////////////////
844
846{
847 cxcoutD(Eval) << "RooNDKeysPdf::calculateBandWidth()" << std::endl;
848
849 const bool adaptive = _options.Contains("a");
850 if (_weights != &_weights1 || _weights != &_weights0) {
851 _weights = adaptive ? &_weights1 : &_weights0;
852 }
853
854 // non-adaptive bandwidth
855 // (default, and needed to calculate adaptive bandwidth)
856
857 if(!adaptive) {
858 cxcoutD(Eval) << "RooNDKeysPdf::calculateBandWidth() Using static bandwidth." << std::endl;
859 }
860
861 // fixed width approximation
862 for (Int_t i=0; i<_nEvents; i++) {
863 vector<double>& weight = _weights0[i];
864 for (Int_t j = 0; j < _nDim; j++) {
865 weight[j] = _n * (*_sigmaR)[j];
866 // std::cout<<"j: "<<j<<", _n: "<<_n<<", sigmaR="<<(*_sigmaR)[j]<<", weight="<<weight[j]<< std::endl;
867 }
868 }
869
870 // adaptive width
871 if (adaptive) {
872 cxcoutD(Eval) << "RooNDKeysPdf::calculateBandWidth() Using adaptive bandwidth." << std::endl;
873
874 double sqrt12 = sqrt(12.);
875 double sqrtSigmaAvgR = sqrt(_sigmaAvgR);
876
877 vector<double> dummy(_nDim, 0.);
878 _weights1.resize(_nEvents, dummy);
879
880 std::vector<std::vector<double>> *weights_prev(nullptr);
881 std::vector<std::vector<double>> *weights_new(nullptr);
882
883 // std::cout << "Number of adaptive iterations: " << _nAdpt << std::endl;
884
885 for (Int_t k = 1; k <= _nAdpt; ++k) {
886
887 // std::cout << " Cycle: " << k << std::endl;
888
889 // if multiple adaptive iterations, need to swap weight sets
890 if (k % 2) {
891 weights_prev = &_weights0;
892 weights_new = &_weights1;
893 } else {
894 weights_prev = &_weights1;
895 weights_new = &_weights0;
896 }
897
898 for (Int_t i = 0; i < _nEvents; ++i) {
899 vector<double> &x = _dataPts[i];
900 double f = std::pow(gauss(x, *weights_prev) / _nEventsW, -1. / (2. * _d));
901
902 vector<double> &weight = (*weights_new)[i];
903 for (Int_t j = 0; j < _nDim; j++) {
904 double norm = (_n * (*_sigmaR)[j]) / sqrtSigmaAvgR;
905 weight[j] = norm * f / sqrt12; // note additional factor of sqrt(12) compared with HEP-EX/0011057
906 }
907 }
908 }
909 // this is the latest updated weights set
910 _weights = weights_new;
911 }
912}
913
914////////////////////////////////////////////////////////////////////////////////
915/// loop over all closest point to x, as determined by loopRange()
916
917double RooNDKeysPdf::gauss(vector<double>& x, vector<vector<double> >& weights) const
918{
919 if(_nEvents==0) return 0.;
920
921 const double sqrt2pi = std::sqrt(TMath::TwoPi());
922
923 double z=0.;
924 std::vector<int> indices;
925
926 // determine input loop range for event x
927 if (_sortInput) {
928 if (_sortTVIdcs.empty())
929 const_cast<RooNDKeysPdf*>(this)->sortDataIndices();
930
931 loopRange(x, indices);
932 } else {
933 indices.reserve(_ibNoSort.size());
934 for (const auto& ibMapItr : _ibNoSort) {
935 indices.push_back(ibMapItr.first);
936 }
937 }
938
939 for (const auto& i : indices) {
940 double g(1.);
941
942 if (i >= (Int_t)_idx.size()) {
943 continue;
944 } //---> 1.myline
945
946 const vector<double> &point = _dataPts[i];
947 const vector<double> &weight = weights[_idx[i]];
948
949 for (Int_t j = 0; j < _nDim; j++) {
950 (*_dx)[j] = x[j] - point[j];
951 }
952
953 if (_nDim > 1 && _rotate) {
954 *_dx *= *_rotMat; // rotate to decorrelated frame!
955 }
956
957 for (Int_t j = 0; j < _nDim; j++) {
958 double r = (*_dx)[j]; // x[j] - point[j];
959 double c = 1. / (2. * weight[j] * weight[j]);
960
961 // std::cout << "j = " << j << " x[j] = " << point[j] << " w = " << weight[j] << std::endl;
962
963 g *= exp(-c * r * r);
964 g *= 1. / (sqrt2pi * weight[j]);
965 }
966 z += (g * _wMap.at(_idx[i]));
967 }
968 return z;
969}
970
971////////////////////////////////////////////////////////////////////////////////
972/// determine closest points to x, to loop over in evaluate()
973
974void RooNDKeysPdf::loopRange(vector<double>& x, std::vector<Int_t>& ibMap) const
975{
976 ibMap.clear();
977 TVectorD xRm(_nDim);
978 TVectorD xRp(_nDim);
979
980 for (Int_t j = 0; j < _nDim; j++) {
981 xRm[j] = xRp[j] = x[j];
982 }
983
984 if (_nDim > 1 && _rotate) {
985 xRm *= *_rotMat;
986 xRp *= *_rotMat;
987 }
988 for (Int_t j = 0; j < _nDim; j++) {
989 xRm[j] -= _nSigma * (_n * (*_sigmaR)[j]);
990 xRp[j] += _nSigma * (_n * (*_sigmaR)[j]);
991 }
992
993 std::vector<TVectorD> xvecRm(1,xRm);
994 std::vector<TVectorD> xvecRp(1,xRp);
995
996 // To remember the indices that were selected in the previous dimensions
997 std::vector<Int_t> ibMapRT;
998
999 for (Int_t j=0; j<_nDim; j++) {
1000 ibMap.clear();
1001 auto comp = [=](const itPair& a, const itPair& b) {
1002 return (*a.second)[j] < (*b.second)[j];
1003 };
1004
1005 // Figure out which elements are to be selected in the current dimension
1006 auto lo = std::lower_bound(_sortTVIdcs[j].begin(), _sortTVIdcs[j].end(), itPair(0, xvecRm.begin()), comp);
1007 auto hi = std::upper_bound(_sortTVIdcs[j].begin(), _sortTVIdcs[j].end(), itPair(0, xvecRp.begin()), comp);
1008 auto it = lo;
1009
1010 if (j==0) {
1011 // In the first dimension, we can skip straight to filling the remembered
1012 // indices for the previous dimension, or in the 1-d case, just fill the
1013 // output map.
1014 auto& m = _nDim==1 ? ibMap : ibMapRT;
1015 m.reserve(std::distance(lo, hi));
1016 for (it=lo; it!=hi; ++it) {
1017 m.push_back(it->first);
1018 }
1019 // Sort by indices so we can use binary search
1020 std::sort(m.begin(), m.end());
1021 continue;
1022 }
1023
1024 // Iterate over elements in current dimension and add them to the list if
1025 // they were also selected in the previous dimension.
1026 for (it=lo; it!=hi; ++it) {
1027 // Look up in ibMapRT with binary search
1028 auto found = std::lower_bound(ibMapRT.begin(), ibMapRT.end(), it->first);
1029 if (found != ibMapRT.end() && !(it->first < *found)) {
1030 ibMap.push_back(it->first);
1031 }
1032 }
1033 // Sort by indices so we can use binary search
1034 std::sort(ibMap.begin(), ibMap.end());
1035
1036 ibMapRT.clear();
1037 if (j!=_nDim-1) { ibMapRT = std::move(ibMap); }
1038 }
1039}
1040
1041////////////////////////////////////////////////////////////////////////////////
1042
1043void RooNDKeysPdf::boxInfoInit(BoxInfo* bi, const char* rangeName, Int_t /*code*/) const
1044{
1045 vector<bool> doInt(_nDim,true);
1046
1047 bi->filled = false;
1048
1049 bi->xVarLo.resize(_nDim,0.);
1050 bi->xVarHi.resize(_nDim,0.);
1051 bi->xVarLoM3s.resize(_nDim,0.);
1052 bi->xVarLoP3s.resize(_nDim,0.);
1053 bi->xVarHiM3s.resize(_nDim,0.);
1054 bi->xVarHiP3s.resize(_nDim,0.);
1055
1056 bi->netFluxZ = true;
1057 bi->bpsIdcs.clear();
1058 bi->bIdcs.clear();
1059 bi->sIdcs.clear();
1060 bi->bmsIdcs.clear();
1061
1062 bi->nEventsBMSW=0.;
1063 bi->nEventsBW=0.;
1064
1065 for (unsigned int j=0; j < _varList.size(); ++j) {
1066 auto var = static_cast<const RooAbsRealLValue*>(_varList.at(j));
1067 if (doInt[j]) {
1068 bi->xVarLo[j] = var->getMin(rangeName);
1069 bi->xVarHi[j] = var->getMax(rangeName);
1070 } else {
1071 bi->xVarLo[j] = var->getVal() ;
1072 bi->xVarHi[j] = var->getVal() ;
1073 }
1074 }
1075}
1076
1077////////////////////////////////////////////////////////////////////////////////
1078
1080{
1081 if ( (_tracker && _tracker->hasChanged(true)) || (_weights != &_weights0 && _weights != &_weights1) ) {
1082 updateRho(); // update internal rho parameters
1083 // redetermine static and/or adaptive bandwidth
1084 const_cast<RooNDKeysPdf*>(this)->calculateBandWidth();
1085 }
1086
1087 const RooArgSet *nset = _varList.nset();
1088 for (unsigned int j=0; j < _varList.size(); ++j) {
1089 auto var = static_cast<const RooAbsReal*>(_varList.at(j));
1090 _x[j] = var->getVal(nset);
1091 }
1092
1093 double val = gauss(_x,*_weights);
1094 //cout<<"returning "<<val<< std::endl;
1095
1096 if (val >= 1E-20) {
1097 return val;
1098 } else {
1099 return (1E-20);
1100 }
1101}
1102
1103////////////////////////////////////////////////////////////////////////////////
1104
1105Int_t RooNDKeysPdf::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* rangeName) const
1106{
1107 if (rangeName) return 0 ;
1108
1109 Int_t code=0;
1110 if (matchArgs(allVars,analVars,RooArgSet(_varList))) { code=1; }
1111
1112 return code;
1113
1114}
1115
1116////////////////////////////////////////////////////////////////////////////////
1117
1118double RooNDKeysPdf::analyticalIntegral(Int_t code, const char* rangeName) const
1119{
1121
1122 cxcoutD(Eval) << "Calling RooNDKeysPdf::analyticalIntegral(" << GetName() << ") with code " << code
1123 << " and rangeName " << (rangeName?rangeName:"<none>") << std::endl;
1124
1125 // determine which observables need to be integrated over ...
1126 Int_t nComb = 1 << (_nDim);
1127 R__ASSERT(code>=1 && code<nComb) ;
1128
1129 vector<bool> doInt(_nDim,true);
1130
1131 // get BoxInfo
1132 BoxInfo* bi(nullptr);
1133
1134 if (rangeName) {
1135 string rangeNameStr(rangeName) ;
1136 bi = _rangeBoxInfo[make_pair(rangeNameStr,code)] ;
1137 if (!bi) {
1138 bi = new BoxInfo ;
1139 _rangeBoxInfo[make_pair(rangeNameStr,code)] = bi ;
1140 boxInfoInit(bi,rangeName,code);
1141 }
1142 } else {
1143 bi = &_fullBoxInfo;
1144 }
1145
1146 // have boundaries changed?
1147 bool newBounds(false);
1148 for (unsigned int j=0; j < _varList.size(); ++j) {
1149 auto var = static_cast<const RooAbsRealLValue*>(_varList.at(j));
1150 if ((var->getMin(rangeName)-bi->xVarLo[j]!=0) ||
1151 (var->getMax(rangeName)-bi->xVarHi[j]!=0)) {
1152 newBounds = true;
1153 }
1154 }
1155
1156 // reset
1157 if (newBounds) {
1158 cxcoutD(Eval) << "RooNDKeysPdf::analyticalIntegral() : Found new boundaries ... " << (rangeName?rangeName:"<none>") << std::endl;
1159 boxInfoInit(bi,rangeName,code);
1160 }
1161
1162 // recalculates netFluxZero and nEventsIR
1163 if (!bi->filled || newBounds) {
1164 // Fill box info with contents
1165 calculateShell(bi);
1166 calculatePreNorm(bi);
1167 bi->filled = true;
1168 const_cast<RooNDKeysPdf*>(this)->sortDataIndices(bi);
1169 }
1170
1171 // first guess
1172 double norm=bi->nEventsBW;
1173
1174 if (_mirror && bi->netFluxZ) {
1175 // KEYS expression is self-normalized
1176 cxcoutD(Eval) << "RooNDKeysPdf::analyticalIntegral() : Using mirrored normalization : " << bi->nEventsBW << std::endl;
1177 return bi->nEventsBW;
1178 }
1179 // calculate leakage in and out of variable range box
1180 else
1181 {
1182 norm = bi->nEventsBMSW;
1183 if (norm<0.) norm=0.;
1184
1185 for (Int_t sIdx : bi->sIdcs) {
1186 double prob=1.;
1187 const vector<double>& x = _dataPts[sIdx];
1188 const vector<double>& weight = (*_weights)[_idx[sIdx]];
1189
1190 vector<double> chi(_nDim,100.);
1191
1192 for (Int_t j=0; j<_nDim; j++) {
1193 if(!doInt[j]) continue;
1194
1195 if ((x[j] > bi->xVarLoM3s[j] && x[j] < bi->xVarLoP3s[j]) && x[j] < (bi->xVarLo[j] + bi->xVarHi[j]) / 2.) {
1196 chi[j] = (x[j] - bi->xVarLo[j]) / weight[j];
1197 } else if ((x[j] > bi->xVarHiM3s[j] && x[j] < bi->xVarHiP3s[j]) && x[j] > (bi->xVarLo[j] + bi->xVarHi[j]) / 2.) {
1198 chi[j] = (bi->xVarHi[j] - x[j]) / weight[j];
1199 }
1200
1201 if (chi[j] > 0) { // inVarRange
1202 prob *= (0.5 + std::erf(std::abs(chi[j]) / sqrt(2.)) / 2.);
1203 } else { // outside Var range
1204 prob *= (0.5 - std::erf(std::abs(chi[j]) / sqrt(2.)) / 2.);
1205 }
1206 }
1207
1208 norm += prob * _wMap.at(_idx[sIdx]);
1209 }
1210
1211 cxcoutD(Eval) << "RooNDKeysPdf::analyticalIntegral() : Final normalization : " << norm << " " << bi->nEventsBW << std::endl;
1212 return norm;
1213 }
1214}
1215
1216
1217////////////////////////////////////////////////////////////////////////////////
1218
1220{
1221 RooArgSet varSet{varList};
1222 std::vector<RooRealVar *> varVec;
1223
1224 for (const auto var : varList) {
1225 if (!dynamic_cast<RooRealVar *>(var)) {
1226 coutE(InputArguments) << "RooNDKeysPdf::createDatasetFromHist(" << GetName() << ") WARNING: variable "
1227 << var->GetName() << " is not of type RooRealVar. Skip." << std::endl;
1228 continue;
1229 }
1230 varVec.push_back(static_cast<RooRealVar *>(var)); // used for setting the variables.
1231 }
1232
1233 /// determine histogram dimensionality
1234 unsigned int histndim(0);
1235 std::string classname = hist.ClassName();
1236 if (classname.find("TH1") == 0) {
1237 histndim = 1;
1238 } else if (classname.find("TH2") == 0) {
1239 histndim = 2;
1240 } else if (classname.find("TH3") == 0) {
1241 histndim = 3;
1242 }
1243 assert(histndim == varVec.size());
1244
1245 if (histndim > 3 || histndim <= 0) {
1246 coutE(InputArguments) << "RooNDKeysPdf::createDatasetFromHist(" << GetName()
1247 << ") ERROR: input histogram dimension not between [1-3]: " << histndim << std::endl;
1248 assert(0);
1249 }
1250
1251 /// dataset creation
1252 RooDataSet *dataFromHist = new RooDataSet("datasetFromHist", "datasetFromHist", varSet, RooFit::WeightVar());
1253
1254 /// dataset filling
1255 for (int i = 1; i <= hist.GetXaxis()->GetNbins(); ++i) {
1256 // 1 or more dimension
1257
1258 double xval = hist.GetXaxis()->GetBinCenter(i);
1259 varVec[0]->setVal(xval);
1260
1261 if (varVec.size() == 1) {
1262 double fval = hist.GetBinContent(i);
1263 dataFromHist->add(varSet, fval);
1264 } else { // 2 or more dimensions
1265
1266 for (int j = 1; j <= hist.GetYaxis()->GetNbins(); ++j) {
1267 double yval = hist.GetYaxis()->GetBinCenter(j);
1268 varVec[1]->setVal(yval);
1269
1270 if (varVec.size() == 2) {
1271 double fval = hist.GetBinContent(i, j);
1272 dataFromHist->add(varSet, fval);
1273 } else { // 3 dimensions
1274
1275 for (int k = 1; k <= hist.GetZaxis()->GetNbins(); ++k) {
1276 double zval = hist.GetZaxis()->GetBinCenter(k);
1277 varVec[2]->setVal(zval);
1278
1279 double fval = hist.GetBinContent(i, j, k);
1280 dataFromHist->add(varSet, fval);
1281 }
1282 }
1283 }
1284 }
1285 }
1286
1287 return dataFromHist;
1288}
1289
1290
1291////////////////////////////////////////////////////////////////////////////////
1292/// Return evaluated weights
1294{
1296
1297 TMatrixD mref(_nEvents, _nDim + 1);
1298
1299 cxcoutD(Eval) << "RooNDKeysPdf::getWeights() Return evaluated weights." << std::endl;
1300
1301 for (Int_t i = 0; i < _nEvents; ++i) {
1302 const vector<double>& x = _dataPts[i];
1303 for (Int_t j = 0; j < _nDim; j++) {
1304 mref(i, j) = x[j];
1305 }
1306
1307 const vector<double>& weight = (*_weights)[i];
1308 mref(i, _nDim) = weight[k];
1309 }
1310
1311 return mref;
1312}
1313
1314 ////////////////////////////////////////////////////////////////////////////////
1316{
1317 for (unsigned int j = 0; j < _rhoList.size(); ++j) {
1318 auto rho = static_cast<const RooAbsReal*>(_rhoList.at(j));
1319 _rho[j] = rho->getVal();
1320 }
1321
1322 if (_nDim > 1 && _rotate) {
1323 TMatrixDSym covMatRho(_nDim); // covariance matrix times rho parameters
1324 for (Int_t j = 0; j < _nDim; j++) {
1325 for (Int_t k = 0; k < _nDim; k++) {
1326 covMatRho(j, k) = (*_covMat)(j, k) * _rho[j] * _rho[k];
1327 }
1328 }
1329 // find decorrelation matrix and eigenvalues (R)
1330 TMatrixDSymEigen evCalculatorRho(covMatRho);
1331 *_sigmaR = evCalculatorRho.GetEigenValues();
1332 for (Int_t j = 0; j < _nDim; j++) {
1333 (*_sigmaR)[j] = sqrt((*_sigmaR)[j]);
1334 }
1335 } else {
1336 for (Int_t j = 0; j < _nDim; j++) {
1337 (*_sigmaR)[j] = (_sigma[j] * _rho[j]);
1338 } // * rho
1339 }
1340}
ValidateOptions const & _options
#define b(i)
Definition RSha256.hxx:100
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
#define a(i)
Definition RSha256.hxx:99
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define coutI(a)
#define cxcoutD(a)
#define coutW(a)
#define coutE(a)
std::vector< itPair > itVec
std::pair< Int_t, VecTVecDouble::iterator > itPair
int Int_t
Signed integer 4 bytes (int)
Definition RtypesCore.h:60
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:139
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
char name[80]
Definition TGX11.cxx:142
#define hi
TMatrixTSym< Double_t > TMatrixDSym
TMatrixT< Double_t > TMatrixD
Definition TMatrixDfwd.h:23
TVectorT< Double_t > TVectorD
Definition TVectorDfwd.h:23
friend class RooDataSet
Definition RooAbsArg.h:579
Storage_t::size_type size() const
bool addTyped(const RooAbsCollection &list, bool silent=false)
Adds elements of a given RooAbsCollection to the container if they match the specified type.
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:32
const RooArgSet * nset() const
Definition RooAbsProxy.h:52
Abstract base class for objects that represent a real value that may appear on the left hand side of ...
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
bool matchArgs(const RooArgSet &allDeps, RooArgSet &analDeps, const RooArgProxy &a, const Proxies &... proxies) const
Definition RooAbsReal.h:426
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:24
Meta object that tracks value changes in a given set of RooAbsArgs by registering itself as value cli...
bool hasChanged(bool clearState)
Returns true if state has changed since last call with clearState=true.
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
Container class to hold unbinned data.
Definition RooDataSet.h:32
Generic N-dimensional implementation of a kernel estimation p.d.f.
BoxInfo _fullBoxInfo
RooListProxy _rhoList
TVectorD * _dx
std::map< Int_t, bool > _ibNoSort
std::vector< std::vector< double > > _weights0
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Implements the actual analytical integral(s) advertised by getAnalyticalIntegral.
~RooNDKeysPdf() override
void calculatePreNorm(BoxInfo *bi) const
bi->nEventsBMSW=0.; bi->nEventsBW=0.;
std::vector< double > _xDatHi
std::vector< std::vector< double > > * _weights
! Weights to be used. Points either to _weights0 or _weights1
TVectorD * _sigmaR
void createPdf(bool firstCall, RooDataSet const &data)
evaluation order of constructor.
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
std::vector< double > _xDatLo
std::vector< double > _x
void initialize(RooDataSet const &data)
initialization
std::map< Int_t, double > _wMap
double _maxWeight
void sortDataIndices(BoxInfo *bi=nullptr)
sort entries, as needed for loopRange()
double _nEventsBMSW
void loadDataSet(bool firstCall, RooDataSet const &data)
copy the dataset and calculate some useful variables
std::vector< TVectorD > _dataPtsR
void updateRho() const
std::vector< double > _mean
void loopRange(std::vector< double > &x, std::vector< Int_t > &ibMap) const
determine closest points to x, to loop over in evaluate()
void calculateShell(BoxInfo *bi) const
determine points in +/- nSigma shell around the box determined by the variable ranges.
void calculateBandWidth()
std::vector< double > _xDatLo3s
std::vector< double > _x1
double _widthFactor
std::vector< std::vector< double > > _dataPts
double _sigmaAvgR
std::vector< double > _x0
std::vector< double > _x2
double gauss(std::vector< double > &x, std::vector< std::vector< double > > &weights) const
loop over all closest point to x, as determined by loopRange()
std::vector< double > _rho
RooListProxy _varList
TMatrixDSym * _corrMat
void loadWeightSet(RooDataSet const &data)
std::vector< itVec > _sortTVIdcs
!
void boxInfoInit(BoxInfo *bi, const char *rangeName, Int_t code) const
std::map< std::pair< std::string, int >, BoxInfo * > _rangeBoxInfo
std::vector< Int_t > _idx
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Interface function getAnalyticalIntergral advertises the analytical integrals that are supported.
std::vector< std::vector< double > > _weights1
std::vector< double > _xDatHi3s
TString _options
TMatrixD getWeights(const int &k) const
Return evaluated weights.
std::vector< double > _sigma
void mirrorDataSet()
determine mirror dataset.
void setOptions()
set the configuration
RooDataSet * createDatasetFromHist(const RooArgList &varList, const TH1 &hist) const
RooNDKeysPdf()=default
void checkInitWeights() const
double _nEventsBW
TMatrixD * _rotMat
TMatrixDSym * _covMat
double _minWeight
RooChangeTracker * _tracker
Variable that can be changed from the outside.
Definition RooRealVar.h:37
virtual Double_t GetBinCenter(Int_t bin) const
Return center of bin.
Definition TAxis.cxx:489
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:125
TAxis * GetZaxis()
Definition TH1.h:602
TAxis * GetXaxis()
Definition TH1.h:600
TAxis * GetYaxis()
Definition TH1.h:601
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition TH1.cxx:5239
TMatrixDSymEigen.
virtual TMatrixTBase< Element > & Zero()
Set matrix elements to zero.
void Print(Option_t *name="") const override
Print the matrix as a table of elements.
TMatrixT< Element > & T()
Definition TMatrixT.h:168
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition TObject.cxx:226
Basic string class.
Definition TString.h:137
void ToLower()
Change string to lower-case.
Definition TString.cxx:1190
const char * Data() const
Definition TString.h:385
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition TString.h:642
TVectorT< Element > & Zero()
Set vector elements to zero.
Definition TVectorT.cxx:453
Int_t GetNrows() const
Definition TVectorT.h:75
void Print(Option_t *option="") const override
Print the vector as a list of elements.
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
RooCmdArg WeightVar(const char *name="weight", bool reinterpretAsWeight=false)
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
constexpr Double_t TwoPi()
Definition TMath.h:47
TMarker m
Definition textangle.C:8
TLine l
Definition textangle.C:4