File indexing completed on 2025-01-18 10:11:03
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #ifndef ROOT_TMVA_PDEFoam
0029 #define ROOT_TMVA_PDEFoam
0030
0031 #include <iosfwd>
0032 #include <cassert>
0033 #include <vector>
0034 #include <map>
0035
0036 #include "TH2D.h"
0037 #include "TObjArray.h"
0038 #include "TObjString.h"
0039 #include "TVectorT.h"
0040 #include "TString.h"
0041 #include "TMVA/VariableInfo.h"
0042 #include "TMVA/Timer.h"
0043 #include "TObject.h"
0044 #include "TRandom3.h"
0045
0046 namespace TMVA {
0047 class PDEFoamCell;
0048 class PDEFoamVect;
0049 class PDEFoamDensityBase;
0050 class PDEFoamKernelBase;
0051 class PDEFoam;
0052
0053
0054 enum EDTSeparation { kFoam, kGiniIndex, kMisClassificationError,
0055 kCrossEntropy, kGiniIndexWithLaplace, kSdivSqrtSplusB };
0056
0057
0058 enum EFoamType { kSeparate, kDiscr, kMonoTarget, kMultiTarget, kMultiClass };
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069 enum ECellValue { kValue, kValueError, kValueDensity, kMeanValue,
0070 kRms, kRmsOvMean, kCellVolume };
0071 }
0072
0073 #include "TMVA/PDEFoamDensityBase.h"
0074 #include "TMVA/PDEFoamVect.h"
0075 #include "TMVA/PDEFoamCell.h"
0076
0077 namespace TMVA {
0078
0079 class PDEFoam : public TObject {
0080 protected:
0081
0082
0083 TString fName;
0084 Int_t fDim;
0085 Int_t fNCells;
0086
0087 Int_t fNBin;
0088 Int_t fNSampl;
0089 Int_t fEvPerBin;
0090
0091 Int_t *fMaskDiv;
0092 Int_t *fInhiDiv;
0093
0094 Int_t fNoAct;
0095 Int_t fLastCe;
0096 PDEFoamCell **fCells;
0097
0098 TObjArray *fHistEdg;
0099 Double_t *fRvec;
0100
0101 TRandom3 *fPseRan;
0102
0103 Double_t *fAlpha;
0104
0105 EFoamType fFoamType;
0106 Double_t *fXmin;
0107 Double_t *fXmax;
0108 UInt_t fNElements;
0109 UInt_t fNmin;
0110 UInt_t fMaxDepth;
0111 Float_t fVolFrac;
0112 Bool_t fFillFoamWithOrigWeights;
0113 EDTSeparation fDTSeparation;
0114 Bool_t fPeekMax;
0115 PDEFoamDensityBase *fDistr;
0116 Timer *fTimer;
0117 TObjArray *fVariableNames;
0118 mutable MsgLogger* fLogger;
0119
0120
0121
0122
0123
0124 protected:
0125
0126
0127 void OutputGrow(Bool_t finished = false );
0128
0129
0130
0131
0132 void InitCells();
0133 Int_t CellFill(Int_t, PDEFoamCell*);
0134 virtual void Explore(PDEFoamCell *Cell);
0135 void Varedu(Double_t [5], Int_t&, Double_t&,Double_t&);
0136 void MakeAlpha();
0137 void Grow();
0138 Long_t PeekMax();
0139 Int_t Divide(PDEFoamCell *);
0140 Double_t Eval(Double_t *xRand, Double_t &event_density);
0141
0142
0143
0144
0145 Double_t GetCellElement(const PDEFoamCell *cell, UInt_t i) const;
0146 void SetCellElement(PDEFoamCell *cell, UInt_t i, Double_t value);
0147
0148
0149 virtual Bool_t CellValueIsUndefined( PDEFoamCell* );
0150
0151
0152 PDEFoamCell* FindCell(const std::vector<Float_t>&) const ;
0153 std::vector<TMVA::PDEFoamCell*> FindCells(const std::vector<Float_t>&) const;
0154 std::vector<TMVA::PDEFoamCell*> FindCells(const std::map<Int_t,Float_t>&) const;
0155 void FindCells(const std::map<Int_t, Float_t>&, PDEFoamCell*, std::vector<PDEFoamCell*> &) const;
0156
0157
0158 PDEFoamDensityBase* GetDistr() const { assert(fDistr); return fDistr; }
0159
0160
0161 template<typename T> T Sqr(T x) const { return x*x; }
0162
0163 PDEFoam(const PDEFoam&);
0164
0165
0166 public:
0167 PDEFoam();
0168 PDEFoam(const TString&);
0169 virtual ~PDEFoam();
0170
0171
0172
0173 void Initialize() {}
0174 void FillBinarySearchTree( const Event* ev );
0175 void Create();
0176
0177
0178 virtual void FillFoamCells(const Event* ev, Float_t wt);
0179
0180
0181 void ResetCellElements();
0182
0183
0184 virtual void Finalize() {}
0185
0186
0187
0188 void SetDim(Int_t kDim);
0189 void SetnCells(Long_t nCells){fNCells =nCells;}
0190 void SetnSampl(Long_t nSampl){fNSampl =nSampl;}
0191 void SetnBin(Int_t nBin){fNBin = nBin;}
0192 void SetEvPerBin(Int_t EvPerBin){fEvPerBin =EvPerBin;}
0193 void SetInhiDiv(Int_t, Int_t );
0194 void SetDensity(PDEFoamDensityBase *dens) { fDistr = dens; }
0195
0196
0197 Int_t GetTotDim() const {return fDim; }
0198 TString GetFoamName() const {return fName; }
0199 UInt_t GetNActiveCells() const {return fNoAct;}
0200 UInt_t GetNInActiveCells() const {return GetNCells()-GetNActiveCells();}
0201 UInt_t GetNCells() const {return fNCells;}
0202 PDEFoamCell* GetRootCell() const {return fCells[0];}
0203
0204
0205 void SetNmin(UInt_t val) { fNmin=val; }
0206 UInt_t GetNmin() { return fNmin; }
0207 void SetMaxDepth(UInt_t maxdepth) { fMaxDepth = maxdepth; }
0208 UInt_t GetMaxDepth() const { return fMaxDepth; }
0209
0210
0211 void SetXmin(Int_t idim, Double_t wmin);
0212 void SetXmax(Int_t idim, Double_t wmax);
0213 Double_t GetXmin(Int_t idim) const {return fXmin[idim];}
0214 Double_t GetXmax(Int_t idim) const {return fXmax[idim];}
0215
0216
0217 void AddVariableName(const char *s) { AddVariableName(new TObjString(s)); }
0218 void AddVariableName(TObjString *s) { fVariableNames->Add(s); }
0219 TObjString* GetVariableName(Int_t idx) {return dynamic_cast<TObjString*>(fVariableNames->At(idx));}
0220
0221
0222
0223 void DeleteBinarySearchTree();
0224
0225
0226
0227
0228 Float_t VarTransform(Int_t idim, Float_t x) const;
0229 std::vector<Float_t> VarTransform(const std::vector<Float_t> &invec) const;
0230 Float_t VarTransformInvers(Int_t idim, Float_t x) const;
0231 std::vector<Float_t> VarTransformInvers(const std::vector<Float_t> &invec) const;
0232
0233
0234
0235 void CheckAll(Int_t);
0236 void PrintCell(Long_t iCell=0);
0237 void PrintCells();
0238
0239
0240 MsgLogger& Log() const { return *fLogger; }
0241
0242
0243
0244
0245 virtual TH2D* Project2(Int_t idim1, Int_t idim2, ECellValue cell_value=kValue,
0246 PDEFoamKernelBase *kernel=nullptr, UInt_t nbin=50);
0247
0248
0249 TH1D* Draw1Dim(ECellValue cell_value, Int_t nbin, PDEFoamKernelBase *kernel=nullptr);
0250
0251
0252 void RootPlot2dim( const TString& filename, TString opt,
0253 Bool_t createCanvas = kTRUE, Bool_t colors = kTRUE );
0254
0255
0256
0257
0258 virtual Float_t GetCellValue( const std::vector<Float_t>& xvec, ECellValue cv, PDEFoamKernelBase* );
0259
0260
0261 virtual std::vector<Float_t> GetCellValue( const std::map<Int_t,Float_t>& xvec, ECellValue cv );
0262
0263
0264 virtual Float_t GetCellValue( const PDEFoamCell* cell, ECellValue cv );
0265
0266
0267 friend class PDEFoamKernelBase;
0268 friend class PDEFoamKernelTrivial;
0269 friend class PDEFoamKernelLinN;
0270 friend class PDEFoamKernelGauss;
0271
0272
0273 ClassDef(PDEFoam,7)
0274 };
0275
0276 }
0277
0278
0279
0280
0281 inline Float_t TMVA::PDEFoam::VarTransform(Int_t idim, Float_t x) const
0282 {
0283
0284 return (x-fXmin[idim])/(fXmax[idim]-fXmin[idim]);
0285 }
0286
0287
0288 inline std::vector<Float_t> TMVA::PDEFoam::VarTransform(const std::vector<Float_t> &invec) const
0289 {
0290
0291 std::vector<Float_t> outvec;
0292 for(UInt_t i=0; i<invec.size(); i++)
0293 outvec.push_back(VarTransform(i, invec.at(i)));
0294 return outvec;
0295 }
0296
0297
0298 inline Float_t TMVA::PDEFoam::VarTransformInvers(Int_t idim, Float_t x) const
0299 {
0300
0301 return x*(fXmax[idim]-fXmin[idim]) + fXmin[idim];
0302 }
0303
0304
0305 inline std::vector<Float_t> TMVA::PDEFoam::VarTransformInvers(const std::vector<Float_t> &invec) const
0306 {
0307
0308 std::vector<Float_t> outvec;
0309 for(UInt_t i=0; i<invec.size(); i++)
0310 outvec.push_back(VarTransformInvers(i, invec.at(i)));
0311 return outvec;
0312 }
0313
0314 #endif