Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooNDKeysPdf.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitModels *
4  * File: $Id: RooNDKeysPdf.h 44368 2012-05-30 15:38:44Z axel $
5  * Authors: *
6  * Max Baak, CERN, mbaak@cern.ch *
7  * *
8  * Copyright (c) 2000-2005, Regents of the University of California *
9  * and Stanford University. All rights reserved. *
10  * *
11  * Redistribution and use in source and binary forms, *
12  * with or without modification, are permitted according to the terms *
13  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
14  *****************************************************************************/
15 #ifndef ROO_NDKEYS_PDF
16 #define ROO_NDKEYS_PDF
17 
18 #include "RooAbsPdf.h"
19 #include "RooRealProxy.h"
20 #include "RooSetProxy.h"
21 #include "RooRealConstant.h"
22 #include "RooDataSet.h"
23 #include "TH1.h"
24 #include "TAxis.h"
25 #include "TVectorD.h"
26 #include "TMatrixD.h"
27 #include "TMatrixDSym.h"
28 #include <map>
29 #include <vector>
30 #include <string>
31 
32 class RooRealVar;
33 class RooArgList;
34 class RooArgSet;
35 class RooChangeTracker;
36 
37 #ifndef __CINT__
38 class VecVecDouble : public std::vector<std::vector<Double_t> > { } ;
39 class VecTVecDouble : public std::vector<TVectorD> { } ;
40 typedef std::pair<Int_t, VecVecDouble::iterator > iiPair;
41 typedef std::vector< iiPair > iiVec;
42 typedef std::pair<Int_t, VecTVecDouble::iterator > itPair;
43 typedef std::vector< itPair > itVec;
44 #else
45 class itPair ;
46 #endif
47 
48 class RooNDKeysPdf : public RooAbsPdf {
49 
50 public:
51 
52  enum Mirror {NoMirror, MirrorLeft, MirrorRight, MirrorBoth,
53  MirrorAsymLeft, MirrorAsymLeftRight,
54  MirrorAsymRight, MirrorLeftAsymRight,
55  MirrorAsymBoth };
56 
57  RooNDKeysPdf() = default;
58 
59  RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const RooDataSet &data,
60  TString options = "ma", Double_t rho = 1, Double_t nSigma = 3, Bool_t rotate = kTRUE,
61  Bool_t sortInput = kTRUE);
62 
63  RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const TH1 &hist, TString options = "ma",
64  Double_t rho = 1, Double_t nSigma = 3, Bool_t rotate = kTRUE, Bool_t sortInput = kTRUE);
65 
66  RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const RooDataSet &data,
67  const TVectorD &rho, TString options = "ma", Double_t nSigma = 3, Bool_t rotate = kTRUE,
68  Bool_t sortInput = kTRUE);
69 
70  RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const RooDataSet &data,
71  const RooArgList &rhoList, TString options = "ma", Double_t nSigma = 3, Bool_t rotate = kTRUE,
72  Bool_t sortInput = kTRUE);
73 
74  RooNDKeysPdf(const char *name, const char *title, const RooArgList &varList, const TH1 &hist,
75  const RooArgList &rhoList, TString options = "ma", Double_t nSigma = 3, Bool_t rotate = kTRUE,
76  Bool_t sortInput = kTRUE);
77 
78  RooNDKeysPdf(const char *name, const char *title, RooAbsReal &x, const RooDataSet &data, Mirror mirror = NoMirror,
79  Double_t rho = 1, Double_t nSigma = 3, Bool_t rotate = kTRUE, Bool_t sortInput = kTRUE);
80 
81  RooNDKeysPdf(const char *name, const char *title, RooAbsReal &x, RooAbsReal &y, const RooDataSet &data,
82  TString options = "ma", Double_t rho = 1.0, Double_t nSigma = 3, Bool_t rotate = kTRUE,
83  Bool_t sortInput = kTRUE);
84 
85  RooNDKeysPdf(const RooNDKeysPdf& other, const char* name=0);
86  virtual ~RooNDKeysPdf();
87 
88  virtual TObject* clone(const char* newname) const { return new RooNDKeysPdf(*this,newname); }
89 
90  Int_t getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* rangeName=0) const ;
91  Double_t analyticalIntegral(Int_t code, const char* rangeName=0) const ;
92 
93  inline void fixShape(Bool_t fix) {
94  createPdf(kFALSE);
95  _fixedShape=fix;
96  }
97 
98  TMatrixD getWeights(const int &k) const;
99 
100  struct BoxInfo {
101  Bool_t filled;
102  Bool_t netFluxZ;
103  Double_t nEventsBW;
104  Double_t nEventsBMSW;
105  std::vector<Double_t> xVarLo, xVarHi;
106  std::vector<Double_t> xVarLoM3s, xVarLoP3s, xVarHiM3s, xVarHiP3s;
107  std::map<Int_t,Bool_t> bpsIdcs;
108  std::vector<Int_t> sIdcs;
109  std::vector<Int_t> bIdcs;
110  std::vector<Int_t> bmsIdcs;
111  } ;
112 
113 protected:
114 
115  RooListProxy _varList ;
116  RooListProxy _rhoList;
117 
118  Double_t evaluate() const;
119 
120  void createPdf(Bool_t firstCall = kTRUE);
121  void setOptions();
122  void initialize();
123  void loadDataSet(Bool_t firstCall) const;
124  void mirrorDataSet() const;
125  void loadWeightSet() const;
126  void calculateShell(BoxInfo *bi) const;
127  void calculatePreNorm(BoxInfo *bi) const;
128  void sortDataIndices(BoxInfo *bi = 0);
129  void calculateBandWidth() const;
130  Double_t gauss(std::vector<Double_t> &x, std::vector<std::vector<Double_t>> &weights) const;
131  void loopRange(std::vector<Double_t> &x, std::map<Int_t, Bool_t> &ibMap) const;
132  void boxInfoInit(BoxInfo *bi, const char *rangeName, Int_t code) const;
133  RooDataSet *createDatasetFromHist(const RooArgList &varList, const TH1 &hist) const;
134  void updateRho() const;
135 
136  std::unique_ptr<RooDataSet> _ownedData{nullptr};
137  const RooDataSet* _data; //! do not persist
138  mutable TString _options;
139  mutable Double_t _widthFactor;
140  mutable Double_t _nSigma;
141 
142  mutable Bool_t _fixedShape{false};
143  mutable Bool_t _mirror{false};
144  mutable Bool_t _debug{false}; //!
145  mutable Bool_t _verbose{false}; //!
146 
147  mutable Int_t _nDim{0};
148  mutable Int_t _nEvents{0};
149  mutable Int_t _nEventsM{0};
150  mutable Double_t _nEventsW{0.};
151  mutable Double_t _d{0.};
152  mutable Double_t _n{0.};
153 
154  // cached info on variable
155 
156  mutable std::vector<std::vector<Double_t> > _dataPts;
157  mutable std::vector<TVectorD> _dataPtsR;
158  mutable std::vector<std::vector<Double_t> > _weights0;
159  mutable std::vector<std::vector<Double_t> > _weights1;
160  mutable std::vector<std::vector<Double_t> >* _weights; //!
161 
162  std::vector<itVec> _sortTVIdcs; //!
163 
164  mutable std::vector<std::string> _varName;
165  mutable std::vector<Double_t> _rho;
166  mutable RooArgSet _dataVars;
167  mutable std::vector<Double_t> _x;
168  mutable std::vector<Double_t> _x0, _x1, _x2;
169  mutable std::vector<Double_t> _mean, _sigma;
170  mutable std::vector<Double_t> _xDatLo, _xDatHi;
171  mutable std::vector<Double_t> _xDatLo3s, _xDatHi3s;
172 
173  mutable Bool_t _netFluxZ{false};
174  mutable Double_t _nEventsBW{0.};
175  mutable Double_t _nEventsBMSW{0.};
176  mutable std::vector<Double_t> _xVarLo, _xVarHi;
177  mutable std::vector<Double_t> _xVarLoM3s, _xVarLoP3s, _xVarHiM3s, _xVarHiP3s;
178  mutable std::map<Int_t,Bool_t> _bpsIdcs;
179  mutable std::map<Int_t, Bool_t> _ibNoSort;
180  mutable std::vector<Int_t> _sIdcs;
181  mutable std::vector<Int_t> _bIdcs;
182  mutable std::vector<Int_t> _bmsIdcs;
183 
184  mutable std::map<std::pair<std::string,int>,BoxInfo*> _rangeBoxInfo ;
185  mutable BoxInfo _fullBoxInfo ;
186 
187  mutable std::vector<Int_t> _idx;
188  mutable Double_t _minWeight{0.};
189  mutable Double_t _maxWeight{0.};
190  mutable std::map<Int_t,Double_t> _wMap;
191 
192  mutable TMatrixDSym* _covMat{nullptr};
193  mutable TMatrixDSym* _corrMat{nullptr};
194  mutable TMatrixD* _rotMat{nullptr};
195  mutable TVectorD* _sigmaR{nullptr};
196  mutable TVectorD* _dx{nullptr};
197  mutable Double_t _sigmaAvgR{0.};
198 
199  mutable Bool_t _rotate;
200  mutable Bool_t _sortInput;
201  mutable Int_t _nAdpt;
202 
203  mutable RooChangeTracker *_tracker{nullptr}; //
204 
205  ClassDef(RooNDKeysPdf, 1) // General N-dimensional non-parametric kernel estimation p.d.f
206 };
207 
208 #endif