00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #ifndef ROOT_TMVA_PDEFoam
00027 #define ROOT_TMVA_PDEFoam
00028 
00029 #include <iosfwd>
00030 #include <cassert>
00031 
00032 #ifndef ROOT_TH2D
00033 #include "TH2D.h"
00034 #endif
00035 #ifndef ROOT_TObjArray
00036 #include "TObjArray.h"
00037 #endif
00038 #ifndef ROOT_TObjString
00039 #include "TObjString.h"
00040 #endif
00041 #ifndef ROOT_TVectorT
00042 #include "TVectorT.h"
00043 #endif
00044 #ifndef ROOT_TString
00045 #include "TString.h"
00046 #endif
00047 #ifndef ROOT_TMVA_VariableInfo
00048 #include "TMVA/VariableInfo.h"
00049 #endif
00050 #ifndef ROOT_TMVA_Timer
00051 #include "TMVA/Timer.h"
00052 #endif
00053 #ifndef ROOT_TObject
00054 #include "TObject.h"
00055 #endif
00056 #ifndef ROOT_TRandom3
00057 #include "TRandom3.h"
00058 #endif
00059 
00060 namespace TMVA {
00061    class PDEFoamCell;
00062    class PDEFoamVect;
00063    class PDEFoamDistr;
00064    class PDEFoam;
00065 
00066    enum EFoamType { kSeparate, kDiscr, kMonoTarget, kMultiTarget };
00067 }
00068 
00069 #ifndef ROOT_TMVA_PDEFoamDistr
00070 #include "TMVA/PDEFoamDistr.h"
00071 #endif
00072 #ifndef ROOT_TMVA_PDEFoamVect
00073 #include "TMVA/PDEFoamVect.h"
00074 #endif
00075 #ifndef ROOT_TMVA_PDEFoamCell
00076 #include "TMVA/PDEFoamCell.h"
00077 #endif
00078 
00079 namespace TMVA {
00080    enum EKernel { kNone=0, kGaus=1, kLinN=2 };
00081    enum ETargetSelection { kMean=0, kMpv=1 };
00082    enum ECellType { kAll, kActive, kInActive };
00083 
00084    
00085    
00086    
00087    
00088    
00089    
00090    
00091    
00092    
00093    
00094    
00095    enum ECellValue { kNev, kDiscriminator, kDiscriminatorError, kTarget0,
00096                      kTarget0Error, kMeanValue, kRms, kRmsOvMean, kDensity };
00097    
00098    enum EDTSeparation { kFoam, kGiniIndex, kMisClassificationError, 
00099                         kCrossEntropy };
00100 }
00101 
00102 namespace TMVA {
00103 
00104    std::ostream& operator<< ( std::ostream& os, const PDEFoam& pdefoam );
00105    std::istream& operator>> ( std::istream& istr,     PDEFoam& pdefoam );
00106 
00107    class PDEFoam : public TObject {
00108    protected:
00109       
00110       
00111       TString fName;             
00112       Int_t   fDim;              
00113       Int_t   fNCells;           
00114       
00115       Int_t   fNBin;             
00116       Int_t   fNSampl;           
00117       Int_t   fEvPerBin;         
00118       
00119       Int_t  *fMaskDiv;          
00120       Int_t  *fInhiDiv;          
00121       
00122       Int_t   fNoAct;            
00123       Int_t   fLastCe;           
00124       PDEFoamCell **fCells;      
00125       
00126       TObjArray *fHistEdg;       
00127       Double_t *fRvec;           
00128       
00129       TRandom3        *fPseRan;  
00130       
00131       Double_t *fAlpha;          
00132       
00133       EFoamType fFoamType;     
00134       Double_t *fXmin;         
00135       Double_t *fXmax;         
00136       UInt_t fNElements;       
00137       UInt_t fNmin;            
00138       UInt_t fMaxDepth;        
00139       Float_t fVolFrac;        
00140       Bool_t fFillFoamWithOrigWeights; 
00141       EDTSeparation fDTSeparation; 
00142       Bool_t fPeekMax;         
00143       PDEFoamDistr *fDistr;    
00144       Timer *fTimer;           
00145       TObjArray *fVariableNames;
00146       mutable MsgLogger* fLogger;                     
00147 
00148 
00149       
00150 
00151    private:
00152       
00153       template<typename T> T Sqr(T x) const { return x*x; }
00154       PDEFoamDistr* GetDistr() const { assert(fDistr); return fDistr; }
00155 
00156    protected:
00157       
00158 
00159       void OutputGrow(Bool_t finished = false ); 
00160 
00161       
00162 
00163       Float_t WeightGaus(PDEFoamCell*, std::vector<Float_t>&, UInt_t dim=0);
00164 
00165       Double_t WeightLinNeighbors( std::vector<Float_t> &txvec, ECellValue cv, 
00166                                    Int_t dim1=-1, Int_t dim2=-1, 
00167                                    Bool_t TreatEmptyCells=kFALSE );
00168       
00169       
00170 
00171       
00172       void InitCells();                   
00173       Int_t CellFill(Int_t, PDEFoamCell*);
00174       void Explore(PDEFoamCell *Cell);    
00175       void DTExplore(PDEFoamCell *Cell);  
00176       void Varedu(Double_t [], Int_t&, Double_t&,Double_t&); 
00177       void MakeAlpha();             
00178       void Grow();                  
00179       Long_t PeekMax();             
00180       Long_t PeekLast();            
00181       Int_t  Divide(PDEFoamCell *); 
00182       Double_t Eval(Double_t *xRand, Double_t &event_density); 
00183       Float_t GetSeparation(Float_t s, Float_t b); 
00184 
00185       
00186 
00187       
00188       Double_t GetCellElement(PDEFoamCell *cell, UInt_t i);  
00189       void SetCellElement(PDEFoamCell *cell, UInt_t i, Double_t value); 
00190 
00191       
00192       Double_t GetCellValue(PDEFoamCell*, ECellValue);
00193 
00194       
00195       Bool_t   CellValueIsUndefined( PDEFoamCell* );
00196 
00197       
00198       PDEFoamCell* FindCell(std::vector<Float_t>&); 
00199       std::vector<TMVA::PDEFoamCell*> FindCells(std::vector<Float_t>&); 
00200 
00201       
00202       void FindCellsRecursive(std::vector<Float_t>&, PDEFoamCell*, 
00203                               std::vector<PDEFoamCell*> &);
00204       
00205       
00206       std::vector<Float_t> GetCellTargets( std::vector<Float_t> &tvals, ETargetSelection ts );
00207       
00208       Double_t GetBuildUpCellEvents(PDEFoamCell* cell);
00209       
00210       PDEFoam(const PDEFoam&);    
00211 
00212       
00213    public:
00214       PDEFoam();                  
00215       PDEFoam(const TString&);    
00216       virtual ~PDEFoam();         
00217 
00218       
00219 
00220       void Init();                    
00221       void FillBinarySearchTree( const Event* ev, Bool_t NoNegWeights=kFALSE );
00222       void Create();              
00223 
00224       
00225       void FillFoamCells(const Event* ev, Bool_t NoNegWeights=kFALSE);
00226 
00227       
00228       
00229       void CalcCellDiscr();
00230       void CalcCellTarget();
00231 
00232       
00233       void ResetCellElements(Bool_t allcells = false);
00234 
00235       
00236 
00237       void SetDim(Int_t kDim); 
00238       void SetnCells(Long_t nCells){fNCells =nCells;}  
00239       void SetnSampl(Long_t nSampl){fNSampl =nSampl;}  
00240       void SetnBin(Int_t nBin){fNBin = nBin;}          
00241       void SetEvPerBin(Int_t EvPerBin){fEvPerBin =EvPerBin;} 
00242       void SetInhiDiv(Int_t, Int_t ); 
00243       void SetNElements(UInt_t numb){fNElements = numb;} 
00244       void SetVolumeFraction(Float_t vfr){fVolFrac = vfr;} 
00245       void SetFoamType(EFoamType ft);   
00246       void SetFillFoamWithOrigWeights(Bool_t new_val){fFillFoamWithOrigWeights=new_val;}
00247       void SetDTSeparation(EDTSeparation new_val){fDTSeparation=new_val;}
00248       void SetPeekMax(Bool_t new_val){ fPeekMax = new_val; }
00249 
00250       
00251       Int_t    GetTotDim()    const {return fDim;  } 
00252       TString  GetFoamName()  const {return fName; } 
00253       UInt_t   GetNElements() const {return fNElements; } 
00254       Float_t  GetVolumeFraction() const {return fVolFrac;} 
00255       EFoamType GetFoamType()      const {return fFoamType;}; 
00256       UInt_t   GetNActiveCells()   const {return fNoAct;}; 
00257       UInt_t   GetNInActiveCells() const {return GetNCells()-GetNActiveCells();}; 
00258       UInt_t   GetNCells()         const {return fNCells;};   
00259       PDEFoamCell* GetRootCell()   const {return fCells[0];}; 
00260 
00261       
00262       void     SetNmin(UInt_t val)     { fNmin=val;      }
00263       UInt_t   GetNmin()               { return fNmin;   }
00264       Bool_t   GetFillFoamWithOrigWeights() const { return fFillFoamWithOrigWeights; }
00265       void     SetMaxDepth(UInt_t maxdepth) { fMaxDepth = maxdepth; }
00266       UInt_t   GetMaxDepth() const { return fMaxDepth; }
00267 
00268       
00269       void SetXmin(Int_t idim, Double_t wmin);
00270       void SetXmax(Int_t idim, Double_t wmax);
00271       Double_t GetXmin(Int_t idim) const {return fXmin[idim];}
00272       Double_t GetXmax(Int_t idim) const {return fXmax[idim];}
00273 
00274       
00275       void AddVariableName(const char *s) { AddVariableName(new TObjString(s)); }
00276       void AddVariableName(TObjString *s) { fVariableNames->Add(s); }
00277       TObjString* GetVariableName(Int_t idx) {return dynamic_cast<TObjString*>(fVariableNames->At(idx));}
00278 
00279       
00280       
00281       void DeleteBinarySearchTree();
00282 
00283       
00284       
00285 
00286       Float_t VarTransform(Int_t idim, Float_t x) const; 
00287       std::vector<Float_t> VarTransform(std::vector<Float_t> &invec) const;
00288       Float_t VarTransformInvers(Int_t idim, Float_t x) const; 
00289       std::vector<Float_t> VarTransformInvers(std::vector<Float_t> &invec) const;
00290 
00291       
00292 
00293       void     CheckAll(Int_t);  
00294       void     PrintCell(Long_t iCell=0); 
00295       void     PrintCells();     
00296       void     CheckCells(Bool_t remove_empty_cells=false);   
00297       void     RemoveEmptyCell(Int_t iCell); 
00298       void     PrintCellElements();          
00299 
00300       
00301       MsgLogger& Log() const { return *fLogger; }
00302 
00303       
00304 
00305       friend std::ostream& operator<< ( std::ostream& os, const PDEFoam& pdefoam );
00306       friend std::istream& operator>> ( std::istream& istr,     PDEFoam& pdefoam );
00307 
00308       void ReadStream(istream &);         
00309       void PrintStream(ostream  &) const; 
00310       void ReadXML( void* parent );       
00311       void AddXMLTo( void* parent );      
00312 
00313       
00314 
00315       
00316       TH2D* Project2(Int_t idim1, Int_t idim2, const char *opt="cell_value", 
00317                      const char *ker="kNone", UInt_t maxbins=50);
00318 
00319       
00320       Double_t GetProjectionCellValue( PDEFoamCell* cell,
00321                                        Int_t idim1, Int_t idim2, ECellValue cv );
00322 
00323       
00324       TH1D* Draw1Dim(const char *opt, Int_t nbin);
00325 
00326       
00327       void RootPlot2dim( const TString& filename, TString opt,
00328                          Bool_t CreateCanvas = kTRUE, Bool_t colors = kTRUE,
00329                          Bool_t log_colors = kFALSE  );
00330 
00331       
00332 
00333       
00334       Double_t GetCellValue(std::vector<Float_t>&, ECellValue);
00335 
00336       
00337       Double_t GetCellDiscr(std::vector<Float_t> &xvec, EKernel kernel=kNone);
00338       Double_t GetCellDensity(std::vector<Float_t> &xvec, EKernel kernel=kNone);
00339 
00340       
00341       Double_t GetAverageNeighborsValue(std::vector<Float_t> &txvec, ECellValue cv);
00342 
00343       
00344       Double_t GetCellRegValue0(std::vector<Float_t>&, EKernel kernel=kNone);
00345 
00346       
00347       std::vector<Float_t> GetProjectedRegValue(std::vector<Float_t> &vals, EKernel kernel=kNone, ETargetSelection ts=kMean);
00348 
00349       
00350       ClassDef(PDEFoam,5) 
00351    }; 
00352 
00353 }  
00354 
00355 
00356 
00357 
00358 inline Float_t TMVA::PDEFoam::VarTransform(Int_t idim, Float_t x) const
00359 {
00360    
00361    return (x-fXmin[idim])/(fXmax[idim]-fXmin[idim]);
00362 }
00363 
00364 
00365 inline std::vector<Float_t> TMVA::PDEFoam::VarTransform(std::vector<Float_t> &invec) const
00366 {
00367    
00368    std::vector<Float_t> outvec;
00369    for(UInt_t i=0; i<invec.size(); i++)
00370       outvec.push_back(VarTransform(i, invec.at(i)));
00371    return outvec;
00372 }
00373 
00374 
00375 inline Float_t TMVA::PDEFoam::VarTransformInvers(Int_t idim, Float_t x) const
00376 {
00377    
00378    return x*(fXmax[idim]-fXmin[idim]) + fXmin[idim];
00379 }
00380 
00381 
00382 inline std::vector<Float_t> TMVA::PDEFoam::VarTransformInvers(std::vector<Float_t> &invec) const
00383 {
00384    
00385    std::vector<Float_t> outvec;
00386    for(UInt_t i=0; i<invec.size(); i++)
00387       outvec.push_back(VarTransformInvers(i, invec.at(i)));
00388    return outvec;
00389 }
00390 
00391 #endif