#include "TKDInterpolator.h" #include "TLinearFitter.h" #include "TVector.h" #include "TTree.h" #include "TH2.h" #include "TObjArray.h" #include "TObjString.h" #include "TBox.h" #include "TGraph.h" #include "TMarker.h" ClassImp(TKDInterpolator) ///////////////////////////////////////////////////////////////////// // Memory setup of protected data memebers // fRefPoints : evaluation point of PDF for each terminal node of underlying KD Tree. // | 1st terminal node (fNDim point coordinates) | 2nd terminal node (fNDim point coordinates) | ... // // fRefValues : evaluation value/error of PDF for each terminal node of underlying KD Tree. // | 1st terminal node (value) | 2nd terminal node (value) | ... | 1st terminal node (error) | 2nd terminal node (error) | ... ///////////////////////////////////////////////////////////////////// //_________________________________________________________________ TKDInterpolator::TKDInterpolator() : TKDTreeIF() ,fNTNodes(0) ,fRefPoints(0x0) ,fRefValues(0x0) ,fDepth(-1) ,fTmpPoint(0x0) ,fKDhelper(0x0) ,fFitter(0x0) { } //_________________________________________________________________ TKDInterpolator::TKDInterpolator(Int_t npoints, Int_t ndim, UInt_t bsize, Float_t **data) : TKDTreeIF(npoints, ndim, bsize, data) ,fNTNodes(GetNTerminalNodes()) ,fRefPoints(0x0) ,fRefValues(0x0) ,fDepth(-1) ,fTmpPoint(0x0) ,fKDhelper(0x0) ,fFitter(0x0) { Build(); } //_________________________________________________________________ TKDInterpolator::TKDInterpolator(TTree *t, const Char_t *var, const Char_t *cut, UInt_t bsize) : TKDTreeIF() ,fNTNodes(0) ,fRefPoints(0x0) ,fRefValues(0x0) ,fDepth(-1) ,fTmpPoint(0x0) ,fKDhelper(0x0) ,fFitter(0x0) { // Alocate data from a tree. The variables which have to be analysed are // defined in the "var" parameter as a colon separated list. The format should // be identical to that used by TTree::Draw(). // // fNpoints = t->GetEntriesFast(); TObjArray *vars = TString(var).Tokenize(":"); fNDim = vars->GetEntriesFast(); if(fNDim > 6/*kDimMax*/) Warning("TKDInterpolator(TTree*, UInt_t, const Char_t)", Form("Variable number exceed maximum dimension %d. Results are unpredictable.", 6/*kDimMax*/)); fBucketSize = bsize; printf("Allocating %d points in %d dimensions.\n", fNpoints, fNDim); Float_t *mem = new Float_t[fNDim*fNpoints]; fData = new Float_t*[fNDim]; for(int idim=0; idimDraw(((TObjString*)(*vars)[idim])->GetName(), cut, "goff"))){ Warning("TKDInterpolator(TTree*, UInt_t, const Char_t)", Form("Can not access data for %s", ((TObjString*)(*vars)[idim])->GetName() )); continue; } v = t->GetV1(); for(int ip=0; ipClearPoints(); do{ if(!fKDhelper->FindNearestNeighbors(point, kNN, index, dist)){ Error("Eval()", Form("Failed retriving %d neighbours for point:", kNN)); for(int idim=0; idimAddPoint(fTmpPoint, TMath::Log(fRefValues[index[in]]), 1.); } kNN_old = kNN; kNN += 4; } while(fFitter->Eval()); // calculate evaluation TVectorD par(kNN); fFitter->GetParameters(par); Double_t result = par[0]; Int_t ipar = 0; for(int idim=0; idim> depth != 1) continue; nnodes++; } //printf("depth %d nodes %d\n", depth, nnodes); //TH2 *h2 = new TH2S("hframe", "", 100, fRange[2*ax1], fRange[2*ax1+1], 100, fRange[2*ax2], fRange[2*ax2+1]); TH2 *h2 = new TH2S("hframe", "", 100, 0., 1., 100, 0., 1.); h2->Draw(); const Float_t border = 0.;//1.E-4; TBox **node_array = new TBox*[nnodes], *node; Float_t *bounds = 0x0; nnodes = 0; for(int inode = 0; inode <= 2*fNnodes; inode++){ if(depth == -1){ if(!IsTerminal(inode)) continue; } else if((inode+1) >> depth != 1) continue; node = node_array[nnodes++]; bounds = GetBoundary(inode); node = new TBox(bounds[2*ax1]+border, bounds[2*ax2]+border, bounds[2*ax1+1]-border, bounds[2*ax2+1]-border); node->SetFillStyle(0); node->SetFillColor(51+inode); node->Draw(); } if(depth != -1) return; // Draw reference points TGraph *ref = new TGraph(GetNTerminalNodes()); ref->SetMarkerStyle(2); ref->SetMarkerColor(2); Float_t val, error; for(int inode = 0; inode < GetNTerminalNodes(); inode++) ref->SetPoint(inode, fRefPoints[ax1][inode], fRefPoints[ax2][inode]); ref->Draw("p"); return; } //_________________________________________________________________ void TKDInterpolator::DrawNode(Int_t tnode, Int_t ax1, Int_t ax2) { // Draw node "node" and the data points within. if(tnode < 0 || tnode >= GetNTerminalNodes()){ Warning("DrawNode()", Form("Terminal node %d outside defined range.", tnode)); return; } //TH2 *h2 = new TH2S("hframe", "", 1, fRange[2*ax1], fRange[2*ax1+1], 1, fRange[2*ax2], fRange[2*ax2+1]); TH2 *h2 = new TH2S("hframe", "", 1, 0., 1., 1, 0., 1.); h2->Draw(); Int_t inode = tnode; tnode += fNnodes; // select zone of interest in the indexes array Int_t *index = GetPointsIndexes(tnode); Int_t nPoints = (tnode == 2*fNnodes) ? fNpoints%fBucketSize : fBucketSize; printf("true index %d points %d\n", tnode, nPoints); // draw data points TGraph *g = new TGraph(nPoints); g->SetMarkerStyle(3); g->SetMarkerSize(.8); for(int ip = 0; ipSetPoint(ip, fData[ax1][index[ip]], fData[ax2][index[ip]]); g->Draw("p"); // draw estimation point TMarker *m=new TMarker(fRefPoints[ax1][inode], fRefPoints[ax2][inode], 2); m->SetMarkerColor(2); m->Draw(); // draw node contour Float_t *bounds = GetBoundary(tnode); TBox *n = new TBox(bounds[2*ax1], bounds[2*ax2], bounds[2*ax1+1], bounds[2*ax2+1]); n->SetFillStyle(0); n->Draw(); return; }