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
00027
00028
00029
00030
00031
00032 #include <cmath>
00033 #include <limits>
00034
00035 #ifndef ROOT_TMath
00036 #include "TMath.h"
00037 #endif
00038
00039 #ifndef ROOT_TMVA_PDEFoamDistr
00040 #include "TMVA/PDEFoamDistr.h"
00041 #endif
00042
00043 ClassImp(TMVA::PDEFoamDistr)
00044
00045
00046 TMVA::PDEFoamDistr::PDEFoamDistr()
00047 : TObject(),
00048 fPDEFoam(NULL),
00049 fBst(NULL),
00050 fDensityCalc(kEVENT_DENSITY),
00051 fLogger( new MsgLogger("PDEFoamDistr"))
00052 {}
00053
00054
00055 TMVA::PDEFoamDistr::~PDEFoamDistr()
00056 {
00057 if (fBst) delete fBst;
00058 delete fLogger;
00059 }
00060
00061
00062 TMVA::PDEFoamDistr::PDEFoamDistr(const PDEFoamDistr &distr)
00063 : TObject(),
00064 fPDEFoam (distr.fPDEFoam),
00065 fBst (distr.fBst),
00066 fDensityCalc (kEVENT_DENSITY),
00067 fLogger( new MsgLogger("PDEFoamDistr"))
00068 {
00069
00070 Log() << kFATAL << "COPY CONSTRUCTOR NOT IMPLEMENTED" << Endl;
00071 }
00072
00073
00074 void TMVA::PDEFoamDistr::Initialize()
00075 {
00076
00077
00078
00079 if (!GetPDEFoam())
00080 Log() << kFATAL << "<PDEFoamDistr::Initialize()> Pointer to owner not set!" << Endl;
00081
00082 if (fBst) delete fBst;
00083 fBst = new TMVA::BinarySearchTree();
00084
00085 if (!fBst){
00086 Log() << kFATAL << "<PDEFoamDistr::Initialize> "
00087 << "ERROR: an not create binary tree !" << Endl;
00088 }
00089
00090
00091 fBst->SetPeriode(GetPDEFoam()->GetTotDim());
00092 }
00093
00094
00095 void TMVA::PDEFoamDistr::FillBinarySearchTree( const Event* ev, EFoamType ft, Bool_t NoNegWeights )
00096 {
00097
00098
00099
00100
00101
00102
00103 if((NoNegWeights && ev->GetWeight()<=0) || ev->GetOriginalWeight()==0)
00104 return;
00105
00106 TMVA::Event *event = new TMVA::Event(*ev);
00107
00108
00109 if (ft==kMultiTarget){
00110
00111
00112 std::vector<Float_t> targets = ev->GetTargets();
00113 for (UInt_t i = 0; i < targets.size(); i++)
00114 event->SetVal(i+ev->GetValues().size(), targets.at(i));
00115 event->GetTargets().clear();
00116 }
00117 fBst->Insert(event);
00118
00119 delete event;
00120 }
00121
00122
00123 Double_t TMVA::PDEFoamDistr::Density( Double_t *Xarg, Double_t &event_density )
00124 {
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 if (!GetPDEFoam())
00139 Log() << kFATAL << "<PDEFoamDistr::Density()> Pointer to owner not set!" << Endl;
00140
00141 if (!fBst)
00142 Log() << kFATAL << "<PDEFoamDistr::Density()> Binary tree not found!"<< Endl;
00143
00144
00145 Int_t Dim = GetPDEFoam()->GetTotDim();
00146 Float_t VolFrac = GetPDEFoam()->GetVolumeFraction();
00147
00148
00149
00150 for (Int_t idim=0; idim<Dim; idim++)
00151 Xarg[idim] = GetPDEFoam()->VarTransformInvers(idim, Xarg[idim]);
00152
00153
00154 std::vector<Double_t> lb(Dim);
00155 std::vector<Double_t> ub(Dim);
00156
00157
00158 const Double_t probevolume_inv = std::pow((VolFrac/2), Dim);
00159
00160
00161 for (Int_t idim = 0; idim < Dim; idim++) {
00162 Double_t volsize=(GetPDEFoam()->GetXmax(idim)
00163 - GetPDEFoam()->GetXmin(idim)) / VolFrac;
00164 lb[idim] = Xarg[idim] - volsize;
00165 ub[idim] = Xarg[idim] + volsize;
00166 }
00167
00168 TMVA::Volume volume(&lb, &ub);
00169 std::vector<const TMVA::BinarySearchTreeNode*> nodes;
00170
00171
00172 fBst->SearchVolume(&volume, &nodes);
00173
00174
00175
00176 const UInt_t count = nodes.size();
00177
00178
00179 event_density = count * probevolume_inv;
00180
00181 Double_t weighted_count = 0.;
00182 for (UInt_t j=0; j<nodes.size(); j++)
00183 weighted_count += (nodes.at(j))->GetWeight();
00184
00185 if (FillDiscriminator()){
00186 Double_t N_sig = 0;
00187
00188 for (UInt_t j=0; j<count; j++){
00189 if (nodes.at(j)->IsSignal()) N_sig += nodes.at(j)->GetWeight();
00190 }
00191 return (N_sig/(weighted_count+0.1))*probevolume_inv;
00192 }
00193 else if (FillTarget0()){
00194 Double_t N_tar = 0;
00195
00196 for (UInt_t j=0; j<count; j++) {
00197 N_tar += ((nodes.at(j))->GetTargets()).at(0) * ((nodes.at(j))->GetWeight());
00198 }
00199 return (N_tar/(weighted_count+0.1))*probevolume_inv;
00200 }
00201
00202 return ((weighted_count+0.1)*probevolume_inv);
00203 }
00204
00205
00206 void TMVA::PDEFoamDistr::FillHist(PDEFoamCell* cell, std::vector<TH1F*> &hsig, std::vector<TH1F*> &hbkg, std::vector<TH1F*> &hsig_unw, std::vector<TH1F*> &hbkg_unw)
00207 {
00208
00209
00210
00211 if (!GetPDEFoam())
00212 Log() << kFATAL << "<PDEFoamDistr::FillHist> Pointer to owner not set!" << Endl;
00213
00214
00215 Int_t Dim = GetPDEFoam()->GetTotDim();
00216
00217
00218 if (!cell)
00219 Log() << kFATAL << "<PDEFoamDistr::FillHist> Null pointer for cell given!" << Endl;
00220 if (Int_t(hsig.size()) != Dim || Int_t(hbkg.size()) != Dim ||
00221 Int_t(hsig_unw.size()) != Dim || Int_t(hbkg_unw.size()) != Dim)
00222 Log() << kFATAL << "<PDEFoamDistr::FillHist> Edge histograms have wrong size!" << Endl;
00223
00224
00225 for (Int_t idim=0; idim<Dim; idim++) {
00226 if (!hsig.at(idim) || !hbkg.at(idim) ||
00227 !hsig_unw.at(idim) || !hbkg_unw.at(idim))
00228 Log() << kFATAL << "<PDEFoamDistr::FillHist> Histogram not initialized!" << Endl;
00229 }
00230
00231
00232 PDEFoamVect cellSize(Dim);
00233 PDEFoamVect cellPosi(Dim);
00234 cell->GetHcub(cellPosi, cellSize);
00235
00236
00237 std::vector<Double_t> lb(Dim);
00238 std::vector<Double_t> ub(Dim);
00239 for (Int_t idim = 0; idim < Dim; idim++) {
00240 lb[idim] = GetPDEFoam()->VarTransformInvers(idim, cellPosi[idim] - std::numeric_limits<float>::epsilon());
00241 ub[idim] = GetPDEFoam()->VarTransformInvers(idim, cellPosi[idim] + cellSize[idim] + std::numeric_limits<float>::epsilon());
00242 }
00243
00244
00245 TMVA::Volume volume(&lb, &ub);
00246 std::vector<const TMVA::BinarySearchTreeNode*> nodes;
00247
00248
00249 fBst->SearchVolume(&volume, &nodes);
00250
00251
00252 std::vector<Float_t> xmin(Dim, std::numeric_limits<float>::max());
00253 std::vector<Float_t> xmax(Dim, -std::numeric_limits<float>::max());
00254 for (UInt_t iev=0; iev<nodes.size(); iev++) {
00255 std::vector<Float_t> ev = nodes.at(iev)->GetEventV();
00256 for (Int_t idim=0; idim<Dim; idim++) {
00257 if (ev.at(idim) < xmin.at(idim)) xmin.at(idim) = ev.at(idim);
00258 if (ev.at(idim) > xmax.at(idim)) xmax.at(idim) = ev.at(idim);
00259 }
00260 }
00261
00262
00263 for (Int_t idim=0; idim<Dim; idim++) {
00264 hsig.at(idim)->GetXaxis()->SetLimits(GetPDEFoam()->VarTransform(idim,xmin.at(idim)),
00265 GetPDEFoam()->VarTransform(idim,xmax.at(idim)));
00266 hbkg.at(idim)->GetXaxis()->SetLimits(GetPDEFoam()->VarTransform(idim,xmin.at(idim)),
00267 GetPDEFoam()->VarTransform(idim,xmax.at(idim)));
00268 hsig_unw.at(idim)->GetXaxis()->SetLimits(GetPDEFoam()->VarTransform(idim,xmin.at(idim)),
00269 GetPDEFoam()->VarTransform(idim,xmax.at(idim)));
00270 hbkg_unw.at(idim)->GetXaxis()->SetLimits(GetPDEFoam()->VarTransform(idim,xmin.at(idim)),
00271 GetPDEFoam()->VarTransform(idim,xmax.at(idim)));
00272 hsig.at(idim)->Reset();
00273 hbkg.at(idim)->Reset();
00274 hsig_unw.at(idim)->Reset();
00275 hbkg_unw.at(idim)->Reset();
00276 }
00277
00278
00279 for (UInt_t iev=0; iev<nodes.size(); iev++) {
00280 std::vector<Float_t> ev = nodes.at(iev)->GetEventV();
00281 Float_t wt = nodes.at(iev)->GetWeight();
00282 Bool_t signal = nodes.at(iev)->IsSignal();
00283 for (Int_t idim=0; idim<Dim; idim++) {
00284 if (signal) {
00285 hsig.at(idim)->Fill(GetPDEFoam()->VarTransform(idim,ev.at(idim)), wt);
00286 hsig_unw.at(idim)->Fill(GetPDEFoam()->VarTransform(idim,ev.at(idim)), 1);
00287 } else {
00288 hbkg.at(idim)->Fill(GetPDEFoam()->VarTransform(idim,ev.at(idim)), wt);
00289 hbkg_unw.at(idim)->Fill(GetPDEFoam()->VarTransform(idim,ev.at(idim)), 1);
00290 }
00291 }
00292 }
00293 }