Changeset 2296 for trunk/MagicSoft/Mars/mranforest/MRanForest.cc
- Timestamp:
- 07/29/03 13:18:57 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mranforest/MRanForest.cc
r2173 r2296 103 103 Int_t ntree=0; 104 104 105 TIter forest(fForest);105 TIter Next(fForest); 106 106 107 107 MRanTree *tree; 108 while ((tree=(MRanTree*) forest.Next()))108 while ((tree=(MRanTree*)Next())) 109 109 { 110 110 fTreeHad[ntree]=tree->TreeHad(event); … … 112 112 ntree++; 113 113 } 114 return hadroness/ Double_t(ntree);114 return hadroness/ntree; 115 115 } 116 116 … … 190 190 Bool_t MRanForest::GrowForest() 191 191 { 192 Int_t ninbag=0; 193 TArrayI datsortinbag; 194 TArrayF classpopw; 195 TArrayI jinbag; 196 TArrayF winbag; 197 198 jinbag.Set(fNumData); 199 winbag.Set(fNumData); 200 classpopw.Set(2); 201 202 TMatrix hadrons=fHadrons->GetM(); 203 TMatrix gammas=fGammas->GetM(); 192 if(!gRandom) 193 { 194 *fLog << err << dbginf << "gRandom not initialized... aborting." << endl; 195 return kFALSE; 196 } 204 197 205 198 fTreeNo++; 206 199 207 200 // initialize running output 208 if(fTreeNo==1) 209 { 210 cout<<endl<<endl<<"1st col.: no. of tree"<<endl; 211 cout<<"2nd col.: error in % (calulated using oob-data -> overestim. of error)"<<endl; 212 } 213 214 jinbag.Reset(); 215 classpopw.Reset(); 216 winbag.Reset(); 201 if (fTreeNo==1) 202 { 203 *fLog << inf << endl; 204 *fLog << underline; // << "1st col 2nd col" << endl; 205 *fLog << "no. of tree error in % (calulated using oob-data -> overestim. of error)" << endl; 206 } 207 208 TArrayF classpopw(2); 209 TArrayI jinbag(fNumData); // Initialization includes filling with 0 210 TArrayF winbag(fNumData); // Initialization includes filling with 0 217 211 218 212 // bootstrap aggregating (bagging) -> sampling with replacement: … … 221 215 // {0,1,...,fNumData-1}, which is the set of the index numbers of 222 216 // all events in the training sample 223 224 for (Int_t n=0;n<fNumData;n++) 225 { 226 if(!gRandom) 227 { 228 *fLog << err << dbginf << "gRandom not initialized... aborting." << endl; 229 return kFALSE; 230 } 231 232 Int_t k=Int_t(fNumData*gRandom->Rndm()); 217 for (Int_t n=0; n<fNumData; n++) 218 { 219 const Int_t k = Int_t(gRandom->Rndm()*fNumData); 233 220 234 221 classpopw[fHadTrue[k]]+=fWeight[k]; … … 241 228 // In bagging procedure ca. 2/3 of all elements in the original 242 229 // training sample are used to build the in-bag data 243 datsortinbag=fDataSort; 244 245 ModifyDataSort(datsortinbag,ninbag,jinbag); 230 TArrayI datsortinbag=fDataSort; 231 Int_t ninbag=0; 232 233 ModifyDataSort(datsortinbag, ninbag, jinbag); 234 235 const TMatrix &hadrons=fHadrons->GetM(); 236 const TMatrix &gammas =fGammas->GetM(); 246 237 247 238 // growing single tree … … 258 249 // determined from oob-data is underestimated, but can still be taken as upper limit. 259 250 260 TVector event(fNumDim); 261 for(Int_t ievt=0;ievt<fNumHad;ievt++) 262 { 263 if(jinbag[ievt]>0)continue; 264 event=TMatrixRow(hadrons,ievt); 265 fHadEst[ievt]+=fRanTree->TreeHad(event); 251 for (Int_t ievt=0;ievt<fNumHad;ievt++) 252 { 253 if (jinbag[ievt]>0) 254 continue; 255 fHadEst[ievt] += fRanTree->TreeHad(hadrons, ievt); 266 256 fNTimesOutBag[ievt]++; 267 257 } 268 for (Int_t ievt=0;ievt<fNumGam;ievt++)269 { 270 if (jinbag[fNumHad+ievt]>0)continue;271 event=TMatrixRow(gammas,ievt);272 fHadEst[fNumHad+ievt] +=fRanTree->TreeHad(event);258 for (Int_t ievt=0;ievt<fNumGam;ievt++) 259 { 260 if (jinbag[fNumHad+ievt]>0) 261 continue; 262 fHadEst[fNumHad+ievt] += fRanTree->TreeHad(gammas, ievt); 273 263 fNTimesOutBag[fNumHad+ievt]++; 274 264 } 275 265 276 266 Int_t n=0; 277 fErr=0;278 for (Int_t ievt=0;ievt<fNumData;ievt++)279 if (fNTimesOutBag[ievt]!=0)267 Double_t ferr=0; 268 for (Int_t ievt=0;ievt<fNumData;ievt++) 269 if (fNTimesOutBag[ievt]!=0) 280 270 { 281 fErr+=TMath::Power(fHadEst[ievt]/fNTimesOutBag[ievt]-fHadTrue[ievt],2.); 271 const Double_t val = fHadEst[ievt]/fNTimesOutBag[ievt]-fHadTrue[ievt]; 272 ferr += val*val; 282 273 n++; 283 274 } 284 275 285 fErr/=Float_t(n); 286 fErr=TMath::Sqrt(fErr); 276 ferr = TMath::Sqrt(ferr/n); 287 277 288 278 // give running output 289 cout << setw(5) << fTreeNo << setw(15) << Form("%.2f",100.*fErr) << endl;279 *fLog << inf << setw(5) << fTreeNo << Form("%15.2f", ferr*100) << endl; 290 280 291 281 // adding tree to forest 292 282 AddTree(); 293 283 294 return (fTreeNo<fNumTrees);284 return fTreeNo<fNumTrees; 295 285 } 296 286 … … 315 305 TArrayI isort(fNumData); 316 306 317 TMatrixhadrons=fHadrons->GetM();318 TMatrixgammas=fGammas->GetM();307 const TMatrix &hadrons=fHadrons->GetM(); 308 const TMatrix &gammas=fGammas->GetM(); 319 309 320 310 for (Int_t j=0;j<fNumHad;j++) … … 346 336 for(Int_t n=0;n<fNumData-1;n++) 347 337 { 348 Int_t n1=isort[n]; 349 Int_t n2=isort[n+1]; 338 const Int_t n1=isort[n]; 339 const Int_t n2=isort[n+1]; 340 350 341 fDataSort[mvar*fNumData+n]=n1; 351 342 if(n==0) fDataRang[mvar*fNumData+n1]=0; … … 359 350 fDataSort[(mvar+1)*fNumData-1]=isort[fNumData-1]; 360 351 } 361 362 return; 363 } 364 365 void MRanForest::ModifyDataSort(TArrayI &datsortinbag,Int_t ninbag,TArrayI &jinbag) 352 } 353 354 void MRanForest::ModifyDataSort(TArrayI &datsortinbag, Int_t ninbag, const TArrayI &jinbag) 366 355 { 367 356 ninbag=0; … … 394 383 } 395 384 } 396 return;397 385 } 398 386
Note:
See TracChangeset
for help on using the changeset viewer.