Logo ROOT  
Reference Guide
RooMomentMorphND.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * *
4 * This code was autogenerated by RooClassFactory *
5 *****************************************************************************/
6
7#include "Riostream.h"
8
9#include "RooMomentMorphND.h"
10#include "RooAbsCategory.h"
11#include "RooRealIntegral.h"
12#include "RooRealConstant.h"
13#include "RooRealVar.h"
14#include "RooFormulaVar.h"
15#include "RooCustomizer.h"
16#include "RooAddPdf.h"
17#include "RooAddition.h"
18#include "RooAbsMoment.h"
19#include "RooMoment.h"
20#include "RooLinearVar.h"
21#include "RooChangeTracker.h"
22#include "RooNumIntConfig.h"
23#include "RooHistPdf.h"
24
25#include "TMath.h"
26#include "TVector.h"
27#include "TMap.h"
28
29#include <map>
30#include <algorithm>
31
32using namespace std;
33
35
36//_____________________________________________________________________________
38 : _cacheMgr(this, 10, true, true), _curNormSet(0), _M(0), _MSqr(0), _setting(RooMomentMorphND::Linear), _useHorizMorph(true)
39{
41}
42
43//_____________________________________________________________________________
44RooMomentMorphND::RooMomentMorphND(const char *name, const char *title, const RooArgList &parList,
45 const RooArgList &obsList, const Grid &referenceGrid, const Setting &setting)
46 : RooAbsPdf(name, title), _cacheMgr(this, 10, true, true), _parList("parList", "List of morph parameters", this),
47 _obsList("obsList", "List of observables", this), _referenceGrid(referenceGrid),
48 _pdfList("pdfList", "List of pdfs", this), _setting(setting), _useHorizMorph(true)
49{
50 // morph parameters
51 initializeParameters(parList);
52
53 // observables
54 initializeObservables(obsList);
55
57
58 // general initialization
59 initialize();
60
62}
63
64//_____________________________________________________________________________
65RooMomentMorphND::RooMomentMorphND(const char *name, const char *title, RooAbsReal &_m, const RooArgList &varList,
66 const RooArgList &pdfList, const TVectorD &mrefpoints, Setting setting)
67 : RooAbsPdf(name, title), _cacheMgr(this, 10, true, true), _parList("parList", "List of morph parameters", this),
68 _obsList("obsList", "List of observables", this), _pdfList("pdfList", "List of pdfs", this), _setting(setting),
69 _useHorizMorph(true)
70{
71 // make reference grid
72 RooBinning grid(mrefpoints.GetNrows() - 1, mrefpoints.GetMatrixArray());
74
75 for (int i = 0; i < mrefpoints.GetNrows(); ++i) {
76 for (int j = 0; j < grid.numBoundaries(); ++j) {
77 if (mrefpoints[i] == grid.array()[j]) {
78 _referenceGrid.addPdf(*(RooAbsPdf *)pdfList.at(i), j);
79 break;
80 }
81 }
82 }
83
85
86 // morph parameters
87 RooArgList parList;
88 parList.add(_m);
89 initializeParameters(parList);
90
91 // observables
92 initializeObservables(varList);
93
94 // general initialization
95 initialize();
96
98}
99
100//_____________________________________________________________________________
101RooMomentMorphND::RooMomentMorphND(const char *name, const char *title, RooAbsReal &_m, const RooArgList &varList,
102 const RooArgList &pdfList, const RooArgList &mrefList, Setting setting)
103 : RooAbsPdf(name, title), _cacheMgr(this, 10, true, true), _parList("parList", "List of morph parameters", this),
104 _obsList("obsList", "List of observables", this), _pdfList("pdfList", "List of pdfs", this), _setting(setting),
105 _useHorizMorph(true)
106{
107 // make reference grid
108 TVectorD mrefpoints(mrefList.getSize());
109 Int_t i = 0;
110 for (auto *mref : mrefList) {
111 if (!dynamic_cast<RooAbsReal*>(mref)) {
112 coutE(InputArguments) << "RooMomentMorphND::ctor(" << GetName() << ") ERROR: mref " << mref->GetName()
113 << " is not of type RooAbsReal" << endl;
114 throw string("RooMomentMorphND::ctor() ERROR mref is not of type RooAbsReal");
115 }
116 if (!dynamic_cast<RooConstVar *>(mref)) {
117 coutW(InputArguments) << "RooMomentMorphND::ctor(" << GetName() << ") WARNING mref point " << i
118 << " is not a constant, taking a snapshot of its value" << endl;
119 }
120 mrefpoints[i] = static_cast<RooAbsReal *>(mref)->getVal();
121 i++;
122 }
123
124 RooBinning grid(mrefpoints.GetNrows() - 1, mrefpoints.GetMatrixArray());
126
127 for (i = 0; i < mrefpoints.GetNrows(); ++i) {
128 for (int j = 0; j < grid.numBoundaries(); ++j) {
129 if (mrefpoints[i] == grid.array()[j]) {
130 _referenceGrid.addPdf(static_cast<RooAbsPdf &>(pdfList[i]), j);
131 break;
132 }
133 }
134 }
135
137
138 // morph parameters
139 RooArgList parList;
140 parList.add(_m);
141 initializeParameters(parList);
142
143 // observables
144 initializeObservables(varList);
145
146 // general initialization
147 initialize();
148
150}
151
152//_____________________________________________________________________________
154 : RooAbsPdf(other, name), _cacheMgr(other._cacheMgr, this), _curNormSet(0),
155 _parList("parList", this, other._parList), _obsList("obsList", this, other._obsList),
156 _referenceGrid(other._referenceGrid), _pdfList("pdfList", this, other._pdfList), _M(0), _MSqr(0),
157 _setting(other._setting), _useHorizMorph(other._useHorizMorph)
158{
159 // general initialization
160 initialize();
161
163}
164
165//_____________________________________________________________________________
167{
168 if (_parItr)
169 delete _parItr;
170 if (_obsItr)
171 delete _obsItr;
172 if (_M)
173 delete _M;
174 if (_MSqr)
175 delete _MSqr;
176
178}
179
180//_____________________________________________________________________________
182{
183 for (auto *par : parList) {
184 if (!dynamic_cast<RooAbsReal *>(par)) {
185 coutE(InputArguments) << "RooMomentMorphND::ctor(" << GetName() << ") ERROR: parameter " << par->GetName()
186 << " is not of type RooAbsReal" << endl;
187 throw string("RooMomentMorphND::initializeParameters() ERROR parameter is not of type RooAbsReal");
188 }
189 _parList.add(*par);
190 }
191}
192
193//_____________________________________________________________________________
195{
196 for (auto *var : obsList){
197 if (!dynamic_cast<RooAbsReal *>(var)) {
198 coutE(InputArguments) << "RooMomentMorphND::ctor(" << GetName() << ") ERROR: variable " << var->GetName()
199 << " is not of type RooAbsReal" << endl;
200 throw string("RooMomentMorphND::initializeObservables() ERROR variable is not of type RooAbsReal");
201 }
202 _obsList.add(*var);
203 }
204}
205
206//_____________________________________________________________________________
207// from http://stackoverflow.com/a/5279601
208template <typename T>
209struct Digits {
210 typename vector<T>::const_iterator begin;
211 typename vector<T>::const_iterator end;
212 typename vector<T>::const_iterator me;
213};
214
215template <typename T>
216inline void cartesian_product(vector<vector<T>> &out, vector<vector<T>> &in)
217{
218 vector<Digits<T>> vd;
219
220 for (typename vector<vector<T>>::const_iterator it = in.begin(); it != in.end(); ++it) {
221 Digits<T> d = {(*it).begin(), (*it).end(), (*it).begin()};
222 vd.push_back(d);
223 }
224
225 while (1) {
226 vector<T> result;
227 for (typename vector<Digits<T>>::const_iterator it = vd.begin(); it != vd.end(); ++it) {
228 result.push_back(*(it->me));
229 }
230 out.push_back(result);
231
232 for (typename vector<Digits<T>>::iterator it = vd.begin();;) {
233 ++(it->me);
234 if (it->me == it->end) {
235 if (it + 1 == vd.end()) {
236 return;
237 } else {
238 it->me = it->begin;
239 ++it;
240 }
241 } else {
242 break;
243 }
244 }
245 }
246}
247
248//_____________________________________________________________________________
250{
251 for (vector<RooAbsBinning *>::iterator itr = _referenceGrid._grid.begin(); itr != _referenceGrid._grid.end();
252 ++itr) {
253 _referenceGrid._nnuis.push_back((*itr)->numBins() + 1);
254 }
255
256 int nPar = _parList.getSize();
257 int nDim = _referenceGrid._grid.size();
258 int nPdf = _referenceGrid._pdfList.getSize();
259 int nRef = _referenceGrid._nref.size();
260 int depth = TMath::Power(2, nPar);
261
262 if (nPar != nDim) {
263 coutE(InputArguments) << "RooMomentMorphND::initialize(" << GetName() << ") ERROR: nPar != nDim"
264 << ": " << nPar << " !=" << nDim << endl;
265 assert(0);
266 }
267
268 if (nPdf != nRef) {
269 coutE(InputArguments) << "RooMomentMorphND::initialize(" << GetName() << ") ERROR: nPdf != nRef"
270 << ": " << nPdf << " !=" << nRef << endl;
271 assert(0);
272 }
273
274 // Transformation matrix for NonLinear settings
275 _M = new TMatrixD(nPdf, nPdf);
276 _MSqr = new TMatrixD(depth, depth);
278 TMatrixD M(nPdf, nPdf);
279
280 vector<vector<double>> dm(nPdf);
281 for (int k = 0; k < nPdf; ++k) {
282 vector<double> dm2;
283 for (int idim = 0; idim < nPar; idim++) {
284 double delta = _referenceGrid._nref[k][idim] - _referenceGrid._nref[0][idim];
285 dm2.push_back(delta);
286 }
287 dm[k] = dm2;
288 }
289
290 vector<vector<int>> powers;
291 for (int idim = 0; idim < nPar; idim++) {
292 vector<int> xtmp;
293 for (int ix = 0; ix < _referenceGrid._nnuis[idim]; ix++) {
294 xtmp.push_back(ix);
295 }
296 powers.push_back(xtmp);
297 }
298
299 vector<vector<int>> output;
300 cartesian_product(output, powers);
301 int nCombs = output.size();
302
303 for (int k = 0; k < nPdf; ++k) {
304 int nperm = 0;
305 for (int i = 0; i < nCombs; i++) {
306 double tmpDm = 1.0;
307 for (int ix = 0; ix < nPar; ix++) {
308 double delta = dm[k][ix];
309 tmpDm *= TMath::Power(delta, static_cast<double>(output[i][ix]));
310 }
311 M(k, nperm) = tmpDm;
312 nperm++;
313 }
314 }
315
316 // M.Print();
317 (*_M) = M.Invert();
318 }
319
320 // Resize transformation vectors
321 _squareVec.resize(TMath::Power(2, nPar));
322 _squareIdx.resize(TMath::Power(2, nPar));
323}
324
325//_____________________________________________________________________________
327 : _grid(other._grid), _pdfList(other._pdfList), _pdfMap(other._pdfMap), _nref(other._nref)
328{
329}
330
331//_____________________________________________________________________________
333{
334}
335
336//_____________________________________________________________________________
337void RooMomentMorphND::Grid::addPdf(const RooAbsPdf &pdf, int bin_x)
338{
339 vector<int> thisBoundaries;
340 vector<double> thisBoundaryCoordinates;
341 thisBoundaries.push_back(bin_x);
342 thisBoundaryCoordinates.push_back(_grid[0]->array()[bin_x]);
343 _pdfList.add(pdf);
344 _pdfMap[thisBoundaries] = _pdfList.getSize() - 1;
345 _nref.push_back(thisBoundaryCoordinates);
346}
347
348//_____________________________________________________________________________
349void RooMomentMorphND::Grid::addPdf(const RooAbsPdf &pdf, int bin_x, int bin_y)
350{
351 vector<int> thisBoundaries;
352 vector<double> thisBoundaryCoordinates;
353 thisBoundaries.push_back(bin_x);
354 thisBoundaryCoordinates.push_back(_grid[0]->array()[bin_x]);
355 thisBoundaries.push_back(bin_y);
356 thisBoundaryCoordinates.push_back(_grid[1]->array()[bin_y]);
357 _pdfList.add(pdf);
358 _pdfMap[thisBoundaries] = _pdfList.getSize() - 1;
359 _nref.push_back(thisBoundaryCoordinates);
360}
361
362//_____________________________________________________________________________
363void RooMomentMorphND::Grid::addPdf(const RooAbsPdf &pdf, int bin_x, int bin_y, int bin_z)
364{
365 vector<int> thisBoundaries;
366 vector<double> thisBoundaryCoordinates;
367 thisBoundaries.push_back(bin_x);
368 thisBoundaryCoordinates.push_back(_grid[0]->array()[bin_x]);
369 thisBoundaries.push_back(bin_y);
370 thisBoundaryCoordinates.push_back(_grid[1]->array()[bin_y]);
371 thisBoundaries.push_back(bin_z);
372 thisBoundaryCoordinates.push_back(_grid[2]->array()[bin_z]);
373 _pdfList.add(pdf);
374 _pdfMap[thisBoundaries] = _pdfList.getSize() - 1;
375 _nref.push_back(thisBoundaryCoordinates);
376}
377
378//_____________________________________________________________________________
379void RooMomentMorphND::Grid::addPdf(const RooAbsPdf &pdf, vector<int> bins)
380{
381 vector<double> thisBoundaryCoordinates;
382 int nBins = bins.size();
383 for (int i = 0; i < nBins; i++) {
384 thisBoundaryCoordinates.push_back(_grid[i]->array()[bins[i]]);
385 }
386 _pdfList.add(pdf);
387 _pdfMap[bins] = _pdfList.getSize() - 1;
388 _nref.push_back(thisBoundaryCoordinates);
389}
390
391//_____________________________________________________________________________
393{
394 CacheElem *cache = static_cast<CacheElem *>(_cacheMgr.getObj(0, (RooArgSet *)0));
395 if (cache) {
396 return cache;
397 }
398
399 int nObs = _obsList.getSize();
400 int nPdf = _referenceGrid._pdfList.getSize();
401
402 RooAbsReal *null = 0;
403 vector<RooAbsReal *> meanrv(nPdf * nObs, null);
404 vector<RooAbsReal *> sigmarv(nPdf * nObs, null);
405 vector<RooAbsReal *> myrms(nObs, null);
406 vector<RooAbsReal *> mypos(nObs, null);
407 vector<RooAbsReal *> slope(nPdf * nObs, null);
408 vector<RooAbsReal *> offsetrv(nPdf * nObs, null);
409 vector<RooAbsReal *> transVar(nPdf * nObs, null);
410 vector<RooAbsReal *> transPdf(nPdf, null);
411
412 RooArgSet ownedComps;
413 RooArgList fracl;
414
415 // fraction parameters
416 RooArgList coefList("coefList"); // fractions multiplied with input pdfs
417 RooArgList coefList2("coefList2"); // fractions multiplied with mean position of observable contribution
418 RooArgList coefList3("coefList3"); // fractions multiplied with rms position of observable contribution
419
420 for (int i = 0; i < 3 * nPdf; ++i) {
421 string fracName = Form("frac_%d", i);
422 RooRealVar *frac = new RooRealVar(fracName.c_str(), fracName.c_str(), /*value=*/1.); // to be set later
423
424 fracl.add(*frac);
425 if (i < nPdf)
426 coefList.add(*(RooRealVar *)(fracl.at(i)));
427 else if (i < 2 * nPdf)
428 coefList2.add(*(RooRealVar *)(fracl.at(i)));
429 else
430 coefList3.add(*(RooRealVar *)(fracl.at(i)));
431 ownedComps.add(*(RooRealVar *)(fracl.at(i)));
432 }
433
434 RooAddPdf *theSumPdf = 0;
435 string sumpdfName = Form("%s_sumpdf", GetName());
436
437 if (_useHorizMorph) {
438 // mean and sigma
439 RooArgList obsList(_obsList);
440 for (int i = 0; i < nPdf; ++i) {
441 for (int j = 0; j < nObs; ++j) {
442 RooAbsMoment *mom = nObs == 1 ? ((RooAbsPdf *)_pdfList.at(i))->sigma((RooRealVar &)*obsList.at(j))
443 : ((RooAbsPdf *)_pdfList.at(i))->sigma((RooRealVar &)*obsList.at(j), obsList);
444
445 mom->setLocalNoDirtyInhibit(true);
446 mom->mean()->setLocalNoDirtyInhibit(true);
447
448 sigmarv[sij(i, j)] = mom;
449 meanrv[sij(i, j)] = mom->mean();
450
451 ownedComps.add(*sigmarv[sij(i, j)]);
452 }
453 }
454
455 // slope and offset (to be set later, depend on nuisance parameters)
456 for (int j = 0; j < nObs; ++j) {
457 RooArgList meanList("meanList");
458 RooArgList rmsList("rmsList");
459 for (int i = 0; i < nPdf; ++i) {
460 meanList.add(*meanrv[sij(i, j)]);
461 rmsList.add(*sigmarv[sij(i, j)]);
462 }
463 string myrmsName = Form("%s_rms_%d", GetName(), j);
464 string myposName = Form("%s_pos_%d", GetName(), j);
465 mypos[j] = new RooAddition(myposName.c_str(), myposName.c_str(), meanList, coefList2);
466 myrms[j] = new RooAddition(myrmsName.c_str(), myrmsName.c_str(), rmsList, coefList3);
467 ownedComps.add(RooArgSet(*myrms[j], *mypos[j]));
468 }
469
470 // construction of unit pdfs
471 RooArgList transPdfList;
472
473 Int_t i = 0;
474 for (auto const *pdf : static_range_cast<RooRealVar *>(_pdfList)) {
475
476 string pdfName = Form("pdf_%d", i);
477 RooCustomizer cust(*pdf, pdfName.c_str());
478
479 Int_t j = 0;
480 for (auto *var : static_range_cast<RooRealVar *>(obsList)) {
481 // slope and offset formulas
482 string slopeName = Form("%s_slope_%d_%d", GetName(), i, j);
483 string offsetName = Form("%s_offset_%d_%d", GetName(), i, j);
484
485 slope[sij(i, j)] =
486 new RooFormulaVar(slopeName.c_str(), "@0/@1", RooArgList(*sigmarv[sij(i, j)], *myrms[j]));
487 offsetrv[sij(i, j)] = new RooFormulaVar(offsetName.c_str(), "@0-(@1*@2)",
488 RooArgList(*meanrv[sij(i, j)], *mypos[j], *slope[sij(i, j)]));
489 ownedComps.add(RooArgSet(*slope[sij(i, j)], *offsetrv[sij(i, j)]));
490
491 // linear transformations, so pdf can be renormalized easily
492 string transVarName = Form("%s_transVar_%d_%d", GetName(), i, j);
493 transVar[sij(i, j)] = new RooLinearVar(transVarName.c_str(), transVarName.c_str(), *var, *slope[sij(i, j)],
494 *offsetrv[sij(i, j)]);
495
496 // *** WVE this is important *** this declares that frac effectively depends on the morphing parameters
497 // This will prevent the likelihood optimizers from erroneously declaring terms constant
498 transVar[sij(i, j)]->addServerList((RooAbsCollection &)_parList);
499
500 ownedComps.add(*transVar[sij(i, j)]);
501 cust.replaceArg(*var, *transVar[sij(i, j)]);
502 ++j;
503 }
504 transPdf[i] = static_cast<RooAbsPdf *>(cust.build());
505 transPdfList.add(*transPdf[i]);
506 ownedComps.add(*transPdf[i]);
507 ++i;
508 }
509
510 // sum pdf
511 theSumPdf = new RooAddPdf(sumpdfName.c_str(), sumpdfName.c_str(), transPdfList, coefList);
512 } else {
513 theSumPdf = new RooAddPdf(sumpdfName.c_str(), sumpdfName.c_str(), _pdfList, coefList);
514 }
515
516 // *** WVE this is important *** this declares that frac effectively depends on the morphing parameters
517 // This will prevent the likelihood optimizers from erroneously declaring terms constant
519 theSumPdf->addOwnedComponents(ownedComps);
520
521 // change tracker for fraction parameters
522 string trackerName = Form("%s_frac_tracker", GetName());
523 RooChangeTracker *tracker = new RooChangeTracker(trackerName.c_str(), trackerName.c_str(), _parList, true);
524
525 // Store it in the cache
526 cache = new CacheElem(*theSumPdf, *tracker, fracl);
527 _cacheMgr.setObj(0, 0, cache, 0);
528
529 cache->calculateFractions(*this, false);
530 return cache;
531}
532
533//_____________________________________________________________________________
535{
536 return RooArgList(*_sumPdf, *_tracker);
537}
538
539//_____________________________________________________________________________
541{
542 delete _sumPdf;
543 delete _tracker;
544}
545
546//_____________________________________________________________________________
547double RooMomentMorphND::getVal(const RooArgSet *set) const
548{
549 // Special version of getVal() overrides RooAbsReal::getVal() to save value of current normalization set
550 _curNormSet = set ? (RooArgSet *)set : (RooArgSet *)&_obsList;
551 return RooAbsPdf::getVal(set);
552}
553
554//_____________________________________________________________________________
556{
557 CacheElem *cache = getCache(nset ? nset : _curNormSet);
558
559 if (cache->_tracker->hasChanged(true)) {
560 cache->calculateFractions(*this, false); // verbose turned off
561 }
562 return cache->_sumPdf;
563}
564
565//_____________________________________________________________________________
567{
569
570 if (cache->_tracker->hasChanged(true)) {
571 cache->calculateFractions(*this, false); // verbose turned off
572 }
573
574 double ret = cache->_sumPdf->getVal(_obsList.nset());
575
576 return ret;
577}
578
579//_____________________________________________________________________________
581{
582 return (RooRealVar *)(_frac.at(i));
583}
584
585//_____________________________________________________________________________
587{
588 return (RooRealVar *)(_frac.at(i));
589}
590
591//_____________________________________________________________________________
592// from http://stackoverflow.com/a/5097100/8747
593template <typename Iterator>
594inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
595{
596 if ((first == last) || (first == k) || (last == k)) {
597 return false;
598 }
599 Iterator itr1 = first;
600 Iterator itr2 = last;
601 ++itr1;
602 if (last == itr1) {
603 return false;
604 }
605 itr1 = last;
606 --itr1;
607 itr1 = k;
608 --itr2;
609 while (first != itr1) {
610 if (*--itr1 < *itr2) {
611 Iterator j = k;
612 while (!(*itr1 < *j)) ++j;
613 iter_swap(itr1, j);
614 ++itr1;
615 ++j;
616 itr2 = k;
617 rotate(itr1, j, last);
618 while (last != j) {
619 ++j;
620 ++itr2;
621 }
622 rotate(k, itr2, last);
623 return true;
624 }
625 }
626 rotate(first, k, last);
627 return false;
628}
629
630//_____________________________________________________________________________
632{
633 int nPdf = self._pdfList.getSize();
634 int nPar = self._parList.getSize();
635
636 double fracLinear(1.);
637 double fracNonLinear(1.);
638
640 // Calculate the delta vector
641 vector<double> dm2;
642 for (int idim = 0; idim < nPar; idim++) {
643 double delta = ((RooRealVar *)self._parList.at(idim))->getVal() - self._referenceGrid._nref[0][idim];
644 dm2.push_back(delta);
645 }
646
647 vector<vector<int>> powers;
648 for (int idim = 0; idim < nPar; idim++) {
649 vector<int> xtmp;
650 for (int ix = 0; ix < self._referenceGrid._nnuis[idim]; ix++) {
651 xtmp.push_back(ix);
652 }
653 powers.push_back(xtmp);
654 }
655
656 vector<vector<int>> output;
657 cartesian_product(output, powers);
658 int nCombs = output.size();
659
660 vector<double> deltavec(nPdf, 1.0);
661
662 int nperm = 0;
663 for (int i = 0; i < nCombs; i++) {
664 double tmpDm = 1.0;
665 for (int ix = 0; ix < nPar; ix++) {
666 double delta = dm2[ix];
667 tmpDm *= TMath::Power(delta, static_cast<double>(output[i][ix]));
668 }
669 deltavec[nperm] = tmpDm;
670 nperm++;
671 }
672
673 double sumposfrac = 0.0;
674 for (int i = 0; i < nPdf; ++i) {
675 double ffrac = 0.0;
676
677 for (int j = 0; j < nPdf; ++j) {
678 ffrac += (*self._M)(j, i) * deltavec[j] * fracNonLinear;
679 }
680
681 if (ffrac >= 0) {
682 sumposfrac += ffrac;
683 }
684
685 // fractions for pdf
686 if (self._setting != NonLinearLinFractions) {
687 ((RooRealVar *)frac(i))->setVal(ffrac);
688 }
689
690 // fractions for rms and mean
691 ((RooRealVar *)frac(nPdf + i))->setVal(ffrac); // need to add up
692 ((RooRealVar *)frac(2 * nPdf + i))->setVal(ffrac); // need to add up
693
694 if (verbose) {
695 cout << "NonLinear fraction " << ffrac << endl;
696 frac(i)->Print();
697 frac(nPdf + i)->Print();
698 frac(2 * nPdf + i)->Print();
699 }
700 }
701
702 if (self._setting == NonLinearPosFractions) {
703 for (int i = 0; i < nPdf; ++i) {
704 if (((RooRealVar *)frac(i))->getVal() < 0)
705 ((RooRealVar *)frac(i))->setVal(0.);
706 ((RooRealVar *)frac(i))->setVal(((RooRealVar *)frac(i))->getVal() / sumposfrac);
707 }
708 }
709 }
710
711 if (self._setting == Linear || self._setting == NonLinearLinFractions) {
712 // loop over parList
713 self._parItr->Reset();
714
715 // zero all fractions
716 // for (int i = 0; i < 3*nPdf; ++i) {
717 for (int i = 0; i < nPdf; ++i) {
718 double initval = 0;
719 ((RooRealVar *)frac(i))->setVal(initval);
720 ((RooRealVar *)frac(nPdf + i))->setVal(initval);
721 ((RooRealVar *)frac(2 * nPdf + i))->setVal(initval);
722 }
723
724 vector<double> mtmp;
725
726 for (int j = 0; j < nPar; j++) {
727 RooRealVar *m = (RooRealVar *)(self._parItr->Next());
728 mtmp.push_back(m->getVal());
729 }
730
731 self.findShape(mtmp); // this sets _squareVec and _squareIdx quantities
732
733 int depth = TMath::Power(2, nPar);
734 vector<double> deltavec(depth, 1.0);
735
736 int nperm = 0;
737
738 vector<int> xtmp;
739 for (int ix = 0; ix < nPar; ix++) {
740 xtmp.push_back(ix);
741 }
742
743 for (int iperm = 1; iperm <= nPar; ++iperm) {
744 do {
745 double dtmp = mtmp[xtmp[0]] - self._squareVec[0][xtmp[0]];
746 for (int itmp = 1; itmp < iperm; ++itmp) {
747 dtmp *= mtmp[xtmp[itmp]] - self._squareVec[0][xtmp[itmp]];
748 }
749 deltavec[nperm + 1] = dtmp;
750 nperm++;
751 } while (next_combination(xtmp.begin(), xtmp.begin() + iperm, xtmp.end()));
752 }
753
754 double origFrac1(0.), origFrac2(0.);
755 for (int i = 0; i < depth; ++i) {
756 double ffrac = 0.;
757 for (int j = 0; j < depth; ++j) {
758 ffrac += (*self._MSqr)(j, i) * deltavec[j] * fracLinear;
759 }
760
761 // set fractions for pdf
762 origFrac1 = ((RooRealVar *)frac(self._squareIdx[i]))->getVal(); // already set in case of smoothlinear
763 ((RooRealVar *)frac(self._squareIdx[i]))->setVal(origFrac1 + ffrac); // need to add up
764
765 // set fractions for rms and mean
766 if (self._setting != NonLinearLinFractions) {
767 origFrac2 =
768 ((RooRealVar *)frac(nPdf + self._squareIdx[i]))->getVal(); // already set in case of smoothlinear
769 ((RooRealVar *)frac(nPdf + self._squareIdx[i]))->setVal(origFrac2 + ffrac); // need to add up
770 ((RooRealVar *)frac(2 * nPdf + self._squareIdx[i]))->setVal(origFrac2 + ffrac); // need to add up
771 }
772
773 if (verbose) {
774 cout << "Linear fraction " << ffrac << endl;
775 frac(self._squareIdx[i])->Print();
776 frac(nPdf + self._squareIdx[i])->Print();
777 frac(2 * nPdf + self._squareIdx[i])->Print();
778 }
779 }
780 }
781}
782
783//_____________________________________________________________________________
784void RooMomentMorphND::findShape(const vector<double> &x) const
785{
786 int nPar = _parList.getSize();
787 int nRef = _referenceGrid._nref.size();
788
789 // Find hypercube enclosing the location to morph to
790 // bool isEnclosed = true;
791 // for (int i = 0; i < nPar; i++) {
792 // if (x[i] < _referenceGrid._grid[i]->lowBound())
793 // isEnclosed = false;
794 // if (x[i] > _referenceGrid._grid[i]->highBound())
795 // isEnclosed = false;
796 // }
797
798 // cout << "isEnclosed = " << isEnclosed << endl;
799
800 int depth = TMath::Power(2, nPar);
801
802 vector<vector<double>> boundaries(nPar);
803 for (int idim = 0; idim < nPar; idim++) {
804 int bin = _referenceGrid._grid[idim]->binNumber(x[idim]);
805 double lo = _referenceGrid._grid[idim]->binLow(bin);
806 double hi = _referenceGrid._grid[idim]->binHigh(bin);
807 boundaries[idim].push_back(lo);
808 boundaries[idim].push_back(hi);
809 }
810
811 vector<vector<double>> output;
812 cartesian_product(output, boundaries);
814
815 for (int isq = 0; isq < depth; isq++) {
816 for (int iref = 0; iref < nRef; iref++) {
817 if (_squareVec[isq] == _referenceGrid._nref[iref]) {
818 _squareIdx[isq] = iref;
819 break;
820 }
821 }
822 }
823
824 // cout << endl;
825
826 // for (int isq = 0; isq < _squareVec.size(); isq++) {
827 // cout << _squareIdx[isq];
828 // cout << " (";
829 // for (int isqq = 0; isqq < _squareVec[isq].size(); isqq++) {
830 // cout << _squareVec[isq][isqq] << ((isqq<_squareVec[isq].size()-1)?",":"");
831 // }
832 // cout << ") ";
833 // }
834
835 // construct transformation matrix for linear extrapolation
836 TMatrixD M(depth, depth);
837
838 vector<int> xtmp;
839 for (int ix = 0; ix < nPar; ix++) {
840 xtmp.push_back(ix);
841 }
842
843 for (int k = 0; k < depth; ++k) {
844 M(k, 0) = 1.0;
845
846 int nperm = 0;
847 vector<double> squareBase = _squareVec[0];
848
849 for (int iperm = 1; iperm <= nPar; ++iperm) {
850 do {
851 double dtmp = _squareVec[k][xtmp[0]] - squareBase[xtmp[0]];
852 for (int itmp = 1; itmp < iperm; ++itmp) {
853 dtmp *= _squareVec[k][xtmp[itmp]] - squareBase[xtmp[itmp]];
854 }
855 M(k, nperm + 1) = dtmp;
856 nperm++;
857 } while (next_combination(xtmp.begin(), xtmp.begin() + iperm, xtmp.end()));
858 }
859 }
860
861 // M.Print();
862 (*_MSqr) = M.Invert();
863}
864
865//_____________________________________________________________________________
867{
868 if (allVars.getSize() == 1) {
869 RooAbsReal *temp = const_cast<RooMomentMorphND *>(this);
870 temp->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
871 int nbins = ((RooRealVar *)allVars.first())->numBins();
872 temp->specialIntegratorConfig(true)->getConfigSection("RooBinIntegrator").setRealValue("numBins", nbins);
873 return true;
874 } else {
875 cout << "Currently BinIntegrator only knows how to deal with 1-d " << endl;
876 return false;
877 }
878 return false;
879}
#define d(i)
Definition: RSha256.hxx:102
bool next_combination(const Iterator first, Iterator k, const Iterator last)
void cartesian_product(vector< vector< T > > &out, vector< vector< T > > &in)
#define coutW(a)
Definition: RooMsgService.h:36
#define coutE(a)
Definition: RooMsgService.h:37
#define TRACE_DESTROY
Definition: RooTrace.h:24
#define TRACE_CREATE
Definition: RooTrace.h:23
#define ClassImp(name)
Definition: Rtypes.h:375
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 Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition: TGX11.cxx:110
#define hi
Definition: THbookFile.cxx:128
TMatrixT< Double_t > TMatrixD
Definition: TMatrixDfwd.h:23
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2447
void setLocalNoDirtyInhibit(bool flag) const
Definition: RooAbsArg.h:723
friend class RooArgSet
Definition: RooAbsArg.h:645
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
Definition: RooAbsArg.cxx:2189
void addServerList(RooAbsCollection &serverList, bool valueProp=true, bool shapeProp=false)
Register a list of RooAbsArg as servers to us by calling addServer() for each arg in the list.
Definition: RooAbsArg.cxx:411
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
Int_t getSize() const
Return the number of elements in the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
bool setRealValue(const char *name, double newVal=0, bool verbose=false)
Set value of a RooAbsRealLValye stored in set with given name to newVal No error messages are printed...
RooAbsArg * first() const
RooAbsMoment represents the first, second, or third order derivative of any RooAbsReal as calculated ...
Definition: RooAbsMoment.h:27
RooAbsReal * mean()
Definition: RooAbsMoment.h:37
const RooArgSet * nset() const
Definition: RooAbsProxy.h:45
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:64
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:94
friend class RooAddPdf
Definition: RooAbsReal.h:558
RooNumIntConfig * specialIntegratorConfig() const
Returns the specialized integrator configuration for this RooAbsReal.
RooAbsMoment * sigma(RooRealVar &obs)
Definition: RooAbsReal.h:363
RooAddPdf is an efficient implementation of a sum of PDFs of the form.
Definition: RooAddPdf.h:32
RooAddition calculates the sum of a set of RooAbsReal terms, or when constructed with two sets,...
Definition: RooAddition.h:27
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:57
Class RooBinning is an implements RooAbsBinning in terms of an array of boundary values,...
Definition: RooBinning.h:27
double * array() const override
Return array of boundary values.
Definition: RooBinning.cxx:217
Int_t numBoundaries() const override
Return the number boundaries.
Definition: RooBinning.h:38
T * getObj(const RooArgSet *nset, Int_t *sterileIndex=0, const TNamed *isetRangeName=0)
Getter function without integration set.
Int_t setObj(const RooArgSet *nset, T *obj, const TNamed *isetRangeName=0)
Setter function without integration set.
bool setLabel(const char *label, bool printError=true) override
Set value by specifying the name of the desired state.
RooChangeTracker is a meta object that tracks value changes in a given set of RooAbsArgs by registeri...
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...
RooConstVar represent a constant real-valued object.
Definition: RooConstVar.h:26
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Definition: RooCustomizer.h:35
void replaceArg(const RooAbsArg &orig, const RooAbsArg &subst)
Replace any occurence of arg 'orig' with arg 'subst'.
RooAbsArg * build(const char *masterCatState, bool verbose=false)
Build a clone of the prototype executing all registered 'replace' rules and 'split' rules for the mas...
A RooFormulaVar is a generic implementation of a real-valued object, which takes a RooArgList of serv...
Definition: RooFormulaVar.h:30
RooLinearVar is the most general form of a derived real-valued object that can be used by RooRealInte...
Definition: RooLinearVar.h:30
RooChangeTracker * _tracker
void calculateFractions(const RooMomentMorphND &self, bool verbose=true) const
RooArgList containedArgs(Action) override
void addBinning(const RooAbsBinning &binning)
std::vector< std::vector< double > > _nref
std::vector< RooAbsBinning * > _grid
void addPdf(const RooAbsPdf &pdf, int bin_x)
std::vector< int > _nnuis
std::vector< int > _squareIdx
Grid _referenceGrid
Do not persist.
CacheElem * getCache(const RooArgSet *nset) const
bool setBinIntegrator(RooArgSet &allVars)
RooObjCacheManager _cacheMgr
! Transient cache manager
RooArgSet * _curNormSet
friend class CacheElem
std::vector< std::vector< double > > _squareVec
void initializeObservables(const RooArgList &obsList)
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
int sij(const int &i, const int &j) const
void findShape(const std::vector< double > &x) const
virtual double getVal(const RooArgSet *set=0) const
TIterator * _obsItr
Do not persist.
RooSetProxy _obsList
RooAbsPdf * sumPdf(const RooArgSet *nset)
void initializeParameters(const RooArgList &parList)
~RooMomentMorphND() override
RooListProxy _parList
RooListProxy _pdfList
const RooArgSet & getConfigSection(const char *name) const
Retrieve configuration information specific to integrator with given name.
RooCategory & method1D()
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
virtual void Reset()=0
virtual TObject * Next()=0
TMatrixT< Element > & Invert(Double_t *det=0)
Invert the matrix and calculate its determinant.
Definition: TMatrixT.cxx:1397
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Int_t GetNrows() const
Definition: TVectorT.h:75
Element * GetMatrixArray()
Definition: TVectorT.h:78
Double_t x[n]
Definition: legend1.C:17
@ InputArguments
Definition: RooGlobalFunc.h:64
std::shared_ptr< std::function< double(double)> > Linear
Definition: NeuralNet.cxx:24
null_t< F > null()
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
Definition: TMath.h:718
Definition: first.py:1
vector< T >::const_iterator begin
vector< T >::const_iterator end
vector< T >::const_iterator me
TMarker m
Definition: textangle.C:8
static void output()