Changeset 2296 for trunk/MagicSoft/Mars/mranforest/MRanTree.cc
- Timestamp:
- 07/29/03 13:18:57 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mranforest/MRanTree.cc
r2173 r2296 76 76 } 77 77 78 void MRanTree::GrowTree( TMatrix &mhad,TMatrix &mgam,Int_t numdata, Int_t numdim,TArrayI &hadtrue,78 void MRanTree::GrowTree(const TMatrix &mhad, const TMatrix &mgam,Int_t numdata, Int_t numdim,TArrayI &hadtrue, 79 79 TArrayI &datasort,TArrayI &datarang,TArrayF &tclasspop,TArrayI &jinbag, 80 80 TArrayF &winbag,TArrayF &weight) … … 88 88 for (Int_t n=0;n<numdata;n++) 89 89 if(jinbag[n]==1) ninbag++; 90 91 // weighted class populations after split92 TArrayF wl(2); // left node93 TArrayF wc(2);94 TArrayF wr(2); // right node95 TArrayI nc(2);96 90 97 91 TArrayI bestsplit(nrnodes); … … 106 100 TArrayI idmove(numdata); 107 101 108 idmove.Reset();109 110 102 fBestVar.Set(nrnodes); 111 103 fTreeMap1.Set(nrnodes); … … 123 115 BuildTree(datasort,datarang,hadtrue,numdim,numdata,bestsplit, 124 116 bestsplitnext,nodepop,nodestart,tclasspop,nrnodes, 125 idmove,ncase,parent,jinbag,iv,winbag, wr,wc,wl,ninbag);117 idmove,ncase,parent,jinbag,iv,winbag,ninbag); 126 118 127 119 // post processing, determine cut (or split) values fBestSplit 128 120 Int_t nhad=mhad.GetNrows(); 129 121 130 for(Int_t k=0; k<nrnodes;k++)131 { 132 Int_t bsp=bestsplit[k];133 Int_t bspn=bestsplitnext[k];134 Int_t msp=fBestVar[k]; 135 136 if (GetNodeStatus(k)!=-1)137 {138 fBestSplit[k] = bsp<nhad ? mhad(bsp,msp):mgam(bsp-nhad,msp); 139 fBestSplit[k] += bspn<nhad ? mhad(bspn,msp):mgam(bspn-nhad,msp);140 fBestSplit[k] /=2.;141 }122 for(Int_t k=0; k<nrnodes; k++) 123 { 124 if (GetNodeStatus(k)==-1) 125 continue; 126 127 const Int_t &bsp =bestsplit[k]; 128 const Int_t &bspn=bestsplitnext[k]; 129 const Int_t &msp =fBestVar[k]; 130 131 fBestSplit[k] = bsp<nhad ? mhad(bsp, msp):mgam(bsp-nhad, msp); 132 fBestSplit[k] += bspn<nhad ? mhad(bspn,msp):mgam(bspn-nhad,msp); 133 fBestSplit[k] /= 2; 142 134 } 143 135 … … 152 144 Int_t numdata,Int_t ndstart,Int_t ndend,TArrayF &tclasspop, 153 145 Int_t &msplit,Float_t &decsplit,Int_t &nbest,TArrayI &ncase, 154 TArrayI &jinbag,TArrayI &iv,TArrayF &winbag,TArrayF &wr, 155 TArrayF &wc,TArrayF &wl,Int_t kbuild) 156 { 146 TArrayI &jinbag,TArrayI &iv,TArrayF &winbag,Int_t kbuild) 147 { 148 if(!gRandom) 149 { 150 *fLog << err << dbginf << "gRandom not initialized... aborting." << endl; 151 return kFALSE; 152 } 153 154 // weighted class populations after split 155 TArrayF wc(2); 156 TArrayF wr(2); // right node 157 157 158 // For the best split, msplit is the index of the variable (e.g Hillas par., zenith angle ,...) 158 159 // split on. decsplit is the decreae in impurity measured by Gini-index. … … 160 161 // and nsplitnext is the case number of the next larger value of msplit. 161 162 162 Int_t mvar,nc,nbestvar=0,jstat,k;163 Float_t crit ,crit0,critmax,critvar=0;163 Int_t nc,nbestvar=0,k; 164 Float_t crit; 164 165 Float_t rrn, rrd, rln, rld, u; 165 166 … … 171 172 for (Int_t j=0;j<2;j++) 172 173 { 173 pno+=tclasspop[j]*tclasspop[j]; 174 pdo+=tclasspop[j]; 175 } 176 crit0=pno/pdo; 177 jstat=0; 174 pno+=tclasspop[j]*tclasspop[j]; 175 pdo+=tclasspop[j]; 176 } 177 178 const Double_t crit0=pno/pdo; 179 Int_t jstat=0; 178 180 179 181 // start main loop through variables to find best split, 180 182 // (Gini-index as criterium crit) 181 183 182 critmax=-1.0e20; // FIXME: Replace by a constant from limits.h184 Double_t critmax=-1.0e20; // FIXME: Replace by a constant from limits.h 183 185 184 186 // random split selection, number of trials = fNumTry 185 if(!gRandom)186 {187 *fLog << err << dbginf << "gRandom not initialized... aborting." << endl;188 return kFALSE;189 }190 187 for(Int_t mt=0;mt<fNumTry;mt++) 191 188 { 192 mvar=Int_t(mdim*gRandom->Rndm());189 Int_t mvar=Int_t(gRandom->Rndm()*mdim); 193 190 194 191 // Gini index = rrn/rrd+rln/rld … … 197 194 rln=0; 198 195 rld=0; 199 wl.Reset(); 200 201 for (Int_t j=0;j<2;j++) 202 { 203 wr[j]=tclasspop[j]; 204 } 205 206 critvar=-1.0e20; 196 197 TArrayF wl(2); // left node 198 wr = tclasspop; 199 200 Double_t critvar=-1.0e20; 207 201 208 202 for(Int_t nsp=ndstart;nsp<=ndend-1;nsp++) … … 223 217 if (datarang[mvar*numdata+nc]<datarang[mvar*numdata+datasort[mvar*numdata+nsp+1]]) 224 218 { 225 if (TMath::Min(rrd,rld)>1.0e-5)219 if (TMath::Min(rrd,rld)>1.0e-5) 226 220 { 227 221 crit=(rln/rld)+(rrn/rrd); … … 309 303 TArrayI &nodepop,TArrayI &nodestart,TArrayF &tclasspop, 310 304 Int_t nrnodes,TArrayI &idmove,TArrayI &ncase,TArrayI &parent, 311 TArrayI &jinbag,TArrayI &iv,TArrayF &winbag,TArrayF &wr,TArrayF &wc, 312 TArrayF &wl,Int_t ninbag) 305 TArrayI &jinbag,TArrayI &iv,TArrayF &winbag,Int_t ninbag) 313 306 { 314 307 // Buildtree consists of repeated calls to two void functions, FindBestSplit and MoveData. … … 326 319 // returns. 327 320 328 Int_t msplit, nbest,ndendl,nc,jstat,ndend,ndstart;321 Int_t msplit, nbest, ndendl; 329 322 Float_t decsplit=0; 330 Float_t popt1,popt2,pp; 331 TArrayF classpop; 332 TArrayI nodestatus; 333 334 nodestatus.Set(nrnodes); 335 classpop.Set(2*nrnodes); 336 337 nodestatus.Reset(); 323 TArrayF classpop(2*nrnodes); 324 TArrayI nodestatus(nrnodes); 325 338 326 nodestart.Reset(); 339 327 nodepop.Reset(); 340 classpop.Reset();341 342 328 343 329 for (Int_t j=0;j<2;j++) … … 357 343 // initialize for next call to FindBestSplit 358 344 359 ndstart=nodestart[kbuild];360 ndend=ndstart+nodepop[kbuild]-1;345 const Int_t ndstart=nodestart[kbuild]; 346 const Int_t ndend=ndstart+nodepop[kbuild]-1; 361 347 for (Int_t j=0;j<2;j++) 362 348 tclasspop[j]=classpop[j*nrnodes+kbuild]; 363 349 364 jstat=FindBestSplit(datasort,datarang,hadtrue,mdim,numdata, 365 ndstart,ndend,tclasspop,msplit,decsplit, 366 nbest,ncase,jinbag,iv,winbag,wr,wc,wl, 367 kbuild); 350 const Int_t jstat=FindBestSplit(datasort,datarang,hadtrue,mdim,numdata, 351 ndstart,ndend,tclasspop,msplit,decsplit, 352 nbest,ncase,jinbag,iv,winbag,kbuild); 368 353 369 354 if(jstat==1) { … … 392 377 for (Int_t n=ndstart;n<=ndendl;n++) 393 378 { 394 nc=ncase[n];395 Int_t j=hadtrue[nc];379 const Int_t nc=ncase[n]; 380 const Int_t j=hadtrue[nc]; 396 381 classpop[j*nrnodes+ncur+1]+=winbag[nc]; 397 382 } … … 399 384 for (Int_t n=ndendl+1;n<=ndend;n++) 400 385 { 401 nc=ncase[n];402 Int_t j=hadtrue[nc];386 const Int_t nc=ncase[n]; 387 const Int_t j=hadtrue[nc]; 403 388 classpop[j*nrnodes+ncur+2]+=winbag[nc]; 404 389 } … … 410 395 if (nodepop[ncur+1]<=fNdSize) nodestatus[ncur+1]=-1; 411 396 if (nodepop[ncur+2]<=fNdSize) nodestatus[ncur+2]=-1; 412 popt1=0; 413 popt2=0; 397 398 Double_t popt1=0; 399 Double_t popt2=0; 414 400 for (Int_t j=0;j<2;j++) 415 401 { 416 popt1+=classpop[j*nrnodes+ncur+1];417 popt2+=classpop[j*nrnodes+ncur+2];402 popt1+=classpop[j*nrnodes+ncur+1]; 403 popt2+=classpop[j*nrnodes+ncur+2]; 418 404 } 419 405 420 406 for (Int_t j=0;j<2;j++) 421 407 { 422 if (classpop[j*nrnodes+ncur+1]==popt1) nodestatus[ncur+1]=-1;423 if (classpop[j*nrnodes+ncur+2]==popt2) nodestatus[ncur+2]=-1;408 if (classpop[j*nrnodes+ncur+1]==popt1) nodestatus[ncur+1]=-1; 409 if (classpop[j*nrnodes+ncur+2]==popt2) nodestatus[ncur+2]=-1; 424 410 } 425 411 … … 446 432 { 447 433 fNumEndNodes++; 448 pp=0;434 Double_t pp=0; 449 435 for (Int_t j=0;j<2;j++) 450 436 { … … 482 468 483 469 const Int_t m=fBestVar[kt]; 484 485 470 kt = event(m)<=fBestSplit[kt] ? fTreeMap1[kt] : fTreeMap2[kt]; 486 471 } 487 472 488 473 return fBestSplit[kt]; 474 } 475 476 Double_t MRanTree::TreeHad(const TMatrixRow &event) 477 { 478 Int_t kt=0; 479 // to optimize on storage space node status and node class 480 // are coded into fBestVar: 481 // status of node kt = TMath::Sign(1,fBestVar[kt]) 482 // class of node kt = fBestVar[kt]+2 (class defined by larger 483 // node population, actually not used) 484 // hadronness assigned to node kt = fBestSplit[kt] 485 486 for (Int_t k=0;k<fNumNodes;k++) 487 { 488 if (fBestVar[kt]<0) 489 break; 490 491 const Int_t m=fBestVar[kt]; 492 kt = event(m)<=fBestSplit[kt] ? fTreeMap1[kt] : fTreeMap2[kt]; 493 } 494 495 return fBestSplit[kt]; 496 } 497 498 Double_t MRanTree::TreeHad(const TMatrix &m, Int_t ievt) 499 { 500 return TreeHad(TMatrixRow(m, ievt)); 489 501 } 490 502
Note:
See TracChangeset
for help on using the changeset viewer.