Changeset 7413 for trunk/MagicSoft/Mars/mranforest
- Timestamp:
- 11/21/05 11:09:12 (19 years ago)
- Location:
- trunk/MagicSoft/Mars/mranforest
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mranforest/MHRanForestGini.cc
r2296 r7413 43 43 #include "MRanTree.h" 44 44 #include "MRanForest.h" 45 #include "MDataArray.h" 45 46 46 47 #include "MLog.h" … … 57 58 // 58 59 MHRanForestGini::MHRanForestGini(Int_t nbins, const char *name, const char *title) 60 : fRules(0.01, 0.01, 0.99, 0.99) 59 61 { 60 62 // … … 64 66 fTitle = title ? title : "Measure of importance of Random Forest-input parameters"; 65 67 66 fGraphGini = new TGraph; 67 fGraphGini->SetTitle("Importance of RF-input parameters measured by mean Gini decrease"); 68 fGraphGini->SetMarkerStyle(kFullDotSmall); 69 } 70 71 // -------------------------------------------------------------------------- 72 // 73 // Delete the histograms. 74 // 75 MHRanForestGini::~MHRanForestGini() 76 { 77 delete fGraphGini; 68 fGraphGini.SetNameTitle("Gini", "Importance of RF-input parameters measured by mean Gini decrease"); 69 fGraphGini.SetMarkerStyle(kFullDotMedium); 70 71 fGraphError.SetNameTitle("ResErr", "Resolution/Error versus train step"); 72 fGraphError.SetMarkerStyle(kFullDotMedium); 73 74 fGraphNodes.SetNameTitle("Nodes", "Number of nodes versus train step"); 75 fGraphNodes.SetMarkerStyle(kFullDotMedium); 76 77 fRules.SetTextAlign(13); 78 fRules.SetTextSize(0.05); 78 79 } 79 80 … … 104 105 Bool_t MHRanForestGini::Fill(const MParContainer *par, const Stat_t w) 105 106 { 107 MRanTree *t = fRanForest->GetCurTree(); 108 106 109 for (Int_t i=0;i<fRanForest->GetNumDim();i++) 107 fGini[i]+=fRanForest->GetCurTree()->GetGiniDec(i); 110 fGini[i] += t->GetGiniDec(i); 111 112 fGraphError.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetError()); 113 fGraphNodes.SetPoint(fGraphError.GetN(), GetNumExecutions(), t->GetNumEndNodes()); 108 114 109 115 return kTRUE; … … 117 123 const Int_t n = fGini.GetSize(); 118 124 119 fGraphGini->Set(n); 120 121 Stat_t max=0; 122 Stat_t min=0; 125 fGraphGini.Set(n); 126 123 127 for (Int_t i=0; i<n; i++) 124 128 { 125 fGini[i] /= fRanForest->GetNumTrees() ;126 fG ini[i] /= fRanForest->GetNumData();127 128 const Stat_t ip = i+1; 129 const Stat_t ig = fGini[i];130 131 if (ig>max) max=ig;132 if (ig<min) min=ig;133 134 fGraphGini->SetPoint(i,ip,ig);135 }136 137 // This is used in root>3.04/? so that SetMaximum/Minimum can succeed138 fGraphGini->GetHistogram();139 140 f GraphGini->SetMaximum(1.05*max);141 fGraphGini->SetMinimum(0.95*min);129 fGini[i] /= fRanForest->GetNumTrees()*fRanForest->GetNumData(); 130 fGraphGini.SetPoint(i, i+1, fGini[i]); 131 } 132 133 fRules.AddText(""); 134 const MDataArray &arr = *fRanForest->GetRules(); 135 int i; 136 for (i=0; i<arr.GetNumEntries(); i++) 137 { 138 TString s; 139 s += i+1; 140 s += ") "; 141 s += arr.GetRule(i); 142 fRules.AddText(s); 143 } 144 for (; i<20; i++) 145 fRules.AddText(""); 142 146 143 147 return kTRUE; … … 150 154 void MHRanForestGini::Draw(Option_t *) 151 155 { 152 if (fGraphGini ->GetN()==0)156 if (fGraphGini.GetN()==0) 153 157 return; 154 158 … … 158 162 AppendPad(""); 159 163 160 fGraphGini->Draw("ALP"); 161 pad->Modified(); 162 pad->Update(); 163 164 TH1 *h = fGraphGini->GetHistogram(); 165 if (!h) 166 return; 167 168 //h->GetXaxis()->SetRangeUser(0, fGini.GetSize()+1); 169 h->SetXTitle("No.of RF-input parameter"); 170 h->SetYTitle("Mean decrease in Gini-index [a.u.]"); 171 172 pad->Modified(); 173 pad->Update(); 174 } 164 pad->Divide(2,2); 165 166 pad->cd(1); 167 gPad->SetBorderMode(0); 168 gPad->SetGridx(); 169 gPad->SetGridy(); 170 fGraphGini.Draw("ALP"); 171 172 TH1 *h = fGraphGini.GetHistogram(); 173 if (h) 174 { 175 h->SetXTitle("No.of RF-input parameter"); 176 h->SetYTitle("Mean decrease in Gini-index [au]"); 177 h->GetXaxis()->SetNdivisions(10); 178 } 179 180 pad->cd(2); 181 gPad->SetBorderMode(0); 182 fGraphError.Draw("ALP"); 183 h = fGraphError.GetHistogram(); 184 if (h) 185 { 186 h->SetXTitle("Train step/Tree number"); 187 h->SetYTitle("Error/Resolution"); 188 } 189 190 pad->cd(3); 191 gPad->SetBorderMode(0); 192 fGraphNodes.Draw("ALP"); 193 h = fGraphNodes.GetHistogram(); 194 if (h) 195 { 196 h->SetXTitle("Train step/Tree number"); 197 h->SetYTitle("Number of end nodes"); 198 } 199 200 pad->cd(4); 201 gPad->SetBorderMode(0); 202 fRules.Draw(); 203 } -
trunk/MagicSoft/Mars/mranforest/MHRanForestGini.h
r2173 r7413 9 9 #include <TArrayF.h> 10 10 #endif 11 #ifndef ROOT_TGraph 12 #include <TGraph.h> 13 #endif 14 #ifndef ROOT_TPaveText 15 #include <TPaveText.h> 16 #endif 11 17 12 class TH1D;13 class TGraph;14 18 class MParList; 15 19 class MRanForest; … … 22 26 23 27 TArrayF fGini; //! 24 TGraph *fGraphGini; //-> 28 29 TGraph fGraphGini; 30 TGraph fGraphError; 31 TGraph fGraphNodes; 32 33 TPaveText fRules; 25 34 26 35 public: 27 36 MHRanForestGini(Int_t nbins=100, const char *name=NULL, const char *title=NULL); 28 ~MHRanForestGini();29 30 TGraph *GetGraphGini() const { return fGraphGini; }31 37 32 38 Bool_t SetupFill(const MParList *plist); -
trunk/MagicSoft/Mars/mranforest/MRanForest.cc
r7409 r7413 166 166 if(grid[i]>=grid[i+1]) 167 167 { 168 *fLog<< inf<<"Grid points must be in increasing order! Ignoring grid."<<endl;168 *fLog<<warn<<"Grid points must be in increasing order! Ignoring grid."<<endl; 169 169 return; 170 170 } … … 225 225 fClass[j] = int(fHadTrue[j]+0.5); 226 226 } 227 228 return;229 227 } 230 228 … … 272 270 Bool_t MRanForest::AddTree(MRanTree *rantree=NULL) 273 271 { 274 fRanTree = rantree ? rantree :fRanTree;272 fRanTree = rantree ? rantree : fRanTree; 275 273 276 274 if (!fRanTree) return kFALSE; … … 287 285 // access matrix, copy last column (target) preliminarily 288 286 // into fHadTrue 289 TMatrix mat_tmp = mat->GetM(); 290 int dim = mat_tmp.GetNcols(); 291 int numdata = mat_tmp.GetNrows(); 292 293 fMatrix=new TMatrix(mat_tmp); 287 fMatrix = new TMatrix(mat->GetM()); 288 289 int dim = fMatrix->GetNcols()-1; 290 int numdata = fMatrix->GetNrows(); 294 291 295 292 fHadTrue.Set(numdata); … … 297 294 298 295 for (Int_t j=0;j<numdata;j++) 299 fHadTrue[j] = (*fMatrix)(j,dim -1);296 fHadTrue[j] = (*fMatrix)(j,dim); 300 297 301 298 // remove last col 302 fMatrix->ResizeTo(numdata,dim-1); 303 dim=fMatrix->GetNcols(); 299 fMatrix->ResizeTo(numdata,dim); 304 300 305 301 //------------------------------------------------------------------- … … 308 304 fClass.Reset(0); 309 305 310 if(fClassify) PrepareClasses(); 306 if (fClassify) 307 PrepareClasses(); 311 308 312 309 //------------------------------------------------------------------- 313 310 // allocating and initializing arrays 314 fHadEst.Set(numdata); fHadEst.Reset(0); 315 fNTimesOutBag.Set(numdata); fNTimesOutBag.Reset(0); 316 fDataSort.Set(dim*numdata); fDataSort.Reset(0); 317 fDataRang.Set(dim*numdata); fDataRang.Reset(0); 311 fHadEst.Set(numdata); 312 fHadEst.Reset(0); 313 314 fNTimesOutBag.Set(numdata); 315 fNTimesOutBag.Reset(0); 316 317 fDataSort.Set(dim*numdata); 318 fDataSort.Reset(0); 319 320 fDataRang.Set(dim*numdata); 321 fDataRang.Reset(0); 318 322 319 323 if(fWeight.GetSize()!=numdata) … … 326 330 //------------------------------------------------------------------- 327 331 // setup rules to be used for classification/regression 328 MDataArray *allrules=(MDataArray*)mat->GetColumns();332 const MDataArray *allrules=(MDataArray*)mat->GetColumns(); 329 333 if(allrules==NULL) 330 334 { … … 333 337 } 334 338 335 fRules=new MDataArray(); fRules->Reset(); 336 TString target_rule; 337 338 for(Int_t i=0;i<dim+1;i++) 339 { 340 MData &data=(*allrules)[i]; 341 if(i<dim) 342 fRules->AddEntry(data.GetRule()); 343 else 344 target_rule=data.GetRule(); 345 } 346 347 *fLog << inf <<endl; 348 *fLog << inf <<"Setting up RF for training on target:"<<endl<<" "<<target_rule.Data()<<endl; 349 *fLog << inf <<"Following rules are used as input to RF:"<<endl; 350 351 for(Int_t i=0;i<dim;i++) 352 { 353 MData &data=(*fRules)[i]; 354 *fLog<<inf<<" "<<i<<") "<<data.GetRule()<<endl<<flush; 355 } 356 357 *fLog << inf <<endl; 339 fRules = new MDataArray(); 340 fRules->Reset(); 341 342 const TString target_rule = (*allrules)[dim]; 343 for (Int_t i=0;i<dim;i++) 344 fRules->AddEntry((*allrules)[i].GetRule()); 345 346 *fLog << inf << endl; 347 *fLog << "Setting up RF for training on target:" << endl; 348 *fLog << " " << target_rule.Data() << endl; 349 *fLog << "Following rules are used as input to RF:" << endl; 350 for (Int_t i=0;i<dim;i++) 351 *fLog << " " << i << ") " << (*fRules)[i].GetRule() << endl; 352 353 *fLog << endl; 358 354 359 355 //------------------------------------------------------------------- 360 356 // prepare (sort) data for fast optimization algorithm 361 if(!CreateDataSort()) return kFALSE; 357 if (!CreateDataSort()) 358 return kFALSE; 362 359 363 360 //------------------------------------------------------------------- 364 361 // access and init tree container 365 362 fRanTree = (MRanTree*)plist->FindCreateObj("MRanTree"); 366 367 363 if(!fRanTree) 368 364 { … … 371 367 } 372 368 369 const Int_t tryest = TMath::Nint(TMath::Sqrt(dim)+0.5); 370 371 *fLog << inf << endl; 372 *fLog << "Following input for the tree growing are used:"<<endl; 373 *fLog << " Number of Trees : "<<fNumTrees<<endl; 374 *fLog << " Number of Trials: "<<(fNumTry==0?tryest:fNumTry)<<(fNumTry==0?" (auto)":"")<<endl; 375 *fLog << " Final Node size : "<<fNdSize<<endl; 376 *fLog << " Using Grid: "<<(fGrid.GetSize()>0?"Yes":"No")<<endl; 377 *fLog << " Number of Events: "<<numdata<<endl; 378 *fLog << " Number of Params: "<<dim<<endl; 379 380 if(fNumTry==0) 381 { 382 fNumTry=tryest; 383 *fLog << inf << endl; 384 *fLog << "Set no. of trials to the recommended value of round("<< TMath::Sqrt(dim) <<") = "; 385 *fLog << fNumTry << endl; 386 387 388 } 389 fRanTree->SetNumTry(fNumTry); 373 390 fRanTree->SetClassify(fClassify); 374 391 fRanTree->SetNdSize(fNdSize); 375 392 376 if(fNumTry==0)377 {378 double ddim = double(dim);379 380 fNumTry=int(sqrt(ddim)+0.5);381 *fLog<<inf<<endl;382 *fLog<<inf<<"Set no. of trials to the recommended value of round("<<sqrt(ddim)<<") = ";383 *fLog<<inf<<fNumTry<<endl;384 385 }386 fRanTree->SetNumTry(fNumTry);387 388 *fLog<<inf<<endl;389 *fLog<<inf<<"Following settings for the tree growing are used:"<<endl;390 *fLog<<inf<<" Number of Trees : "<<fNumTrees<<endl;391 *fLog<<inf<<" Number of Trials: "<<fNumTry<<endl;392 *fLog<<inf<<" Final Node size : "<<fNdSize<<endl;393 394 393 fTreeNo=0; 395 394 … … 415 414 if (fTreeNo==1) 416 415 { 417 *fLog << inf << endl; 418 *fLog << underline; 416 *fLog << inf << endl << underline; 419 417 420 418 if(calcResolution) … … 435 433 TArrayF winbag(numdata); // Initialization includes filling with 0 436 434 437 float square=0; float mean=0; 435 float square=0; 436 float mean=0; 438 437 439 438 for (Int_t n=0; n<numdata; n++) … … 483 482 for (Int_t ievt=0;ievt<numdata;ievt++) 484 483 { 485 if (jinbag[ievt]>0) continue; 484 if (jinbag[ievt]>0) 485 continue; 486 486 487 487 fHadEst[ievt] +=fRanTree->TreeHad((*fMatrix), ievt); … … 491 491 492 492 Int_t n=0; 493 doubleferr=0;493 Float_t ferr=0; 494 494 495 495 for (Int_t ievt=0;ievt<numdata;ievt++) … … 508 508 //------------------------------------------------------------------- 509 509 // give running output 510 *fLog << inf << setw(5) << fTreeNo; 511 *fLog << inf << setw(18) << fRanTree->GetNumEndNodes(); 512 *fLog << inf << Form("%18.2f", ferr*100.); 513 *fLog << inf << endl; 510 *fLog << setw(5) << fTreeNo; 511 *fLog << setw(18) << fRanTree->GetNumEndNodes(); 512 *fLog << Form("%18.2f", ferr*100.); 513 *fLog << endl; 514 515 fRanTree->SetError(ferr); 514 516 515 517 // adding tree to forest -
trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc
r6893 r7413 16 16 ! 17 17 ! 18 ! Author(s): Thomas Hengstebeck 3/2003 <mailto:hengsteb@alwa02.physik.uni-siegen.de> 19 ! 20 ! Copyright: MAGIC Software Development, 2000-2003 18 ! Author(s): Thomas Hengstebeck 2/2005 <mailto:hengsteb@physik.hu-berlin.de> 19 ! Author(s): Thomas Bretz 8/2005 <mailto:tbretz@astro.uni-wuerzburg.de> 20 ! 21 ! Copyright: MAGIC Software Development, 2000-2005 21 22 ! 22 23 ! … … 27 28 // MRanForestCalc 28 29 // 29 // Calculates the hadroness of an event. It calculates a mean value of all30 // classifications by the trees in a previously grown random forest.31 //32 // To use only n trees for your calculation use:33 // MRanForestCalc::SetUseNumTrees(n);34 30 // 35 31 //////////////////////////////////////////////////////////////////////////// … … 38 34 #include <TVector.h> 39 35 40 #include "MHMatrix.h" // must be before MLogManip.h 41 #include "MDataArray.h" 36 #include "MHMatrix.h" 42 37 43 38 #include "MLog.h" 44 39 #include "MLogManip.h" 45 40 41 #include "MData.h" 42 #include "MDataArray.h" 43 44 #include "MRanForest.h" 45 #include "MParameters.h" 46 46 47 #include "MParList.h" 47 48 #include "MRanTree.h" 49 #include "MRanForest.h" 50 51 #include "MParameters.h" 52 48 #include "MTaskList.h" 53 49 #include "MEvtLoop.h" 54 #include "M TaskList.h"50 #include "MRanForestGrow.h" 55 51 #include "MFillH.h" 56 #include "MStatusDisplay.h"57 #include "MRanForestGrow.h"58 #include "MRanForestFill.h"59 60 #include "MWriteRootFile.h"61 #include "MReadTree.h"62 52 63 53 ClassImp(MRanForestCalc); … … 65 55 using namespace std; 66 56 67 static const TString gsDefName = "MRanForestCalc"; 68 static const TString gsDefTitle = "Tree Classification Loop 1/2"; 69 70 // -------------------------------------------------------------------------- 71 // 72 // Setup histograms and the number of distances which are used for 73 // avaraging in CalcDist 74 // 57 const TString MRanForestCalc::gsDefName = "MRanForestCalc"; 58 const TString MRanForestCalc::gsDefTitle = "RF for energy estimation"; 59 60 const TString MRanForestCalc::gsNameOutput = "RanForestOut"; 61 75 62 MRanForestCalc::MRanForestCalc(const char *name, const char *title) 76 : fNum(100), fHadronnessName("MHadronness"), fData(NULL), fRanForest(0), fRanTree(0) 77 { 78 // 79 // set the name and title of this object 80 // 63 : fDebug(kFALSE), fData(0), fRFOut(0), 64 fNumTrees(-1), fNumTry(-1), fNdSize(-1), fNumObsoleteVariables(1), 65 fTestMatrix(0), fEstimationMode(kMean) 66 { 81 67 fName = name ? name : gsDefName.Data(); 82 68 fTitle = title ? title : gsDefTitle.Data(); 83 } 84 85 // -------------------------------------------------------------------------- 86 // 87 // Delete the data chains 88 // 69 70 // FIXME: 71 fNumTrees = 100; //100 72 fNumTry = 0; //3 0 means: in MRanForest estimated best value will be calculated 73 fNdSize = 1; //1 74 } 75 89 76 MRanForestCalc::~MRanForestCalc() 90 77 { 91 if (!IsOwner()) 92 return; 93 94 delete fRanForest; 95 delete fRanTree; 96 } 97 98 // -------------------------------------------------------------------------- 99 // 100 // Needs: 101 // - MatrixGammas [MHMatrix] 102 // - MatrixHadrons [MHMatrix] 103 // - MHadronness 104 // - all data containers used to build the matrixes 105 // 106 // The matrix object can be filles using MFillH. And must be of the same 107 // number of columns (with the same meaning). 108 // 78 fEForests.Delete(); 79 } 80 81 Int_t MRanForestCalc::Train(const MHMatrix &matrixtrain, const TArrayD &grid, Int_t ver) 82 { 83 gLog.Separator("MRanForestCalc - Train"); 84 85 if (!matrixtrain.GetColumns()) 86 { 87 *fLog << err << "ERROR - MHMatrix does not contain rules... abort." << endl; 88 return kFALSE; 89 } 90 91 const Int_t ncols = matrixtrain.GetM().GetNcols(); 92 const Int_t nrows = matrixtrain.GetM().GetNrows(); 93 if (ncols<=0 || nrows <=0) 94 { 95 *fLog << err << "ERROR - No. of columns or no. of rows of matrixtrain equal 0 ... abort." << endl; 96 return kFALSE; 97 } 98 99 // rules (= combination of image par) to be used for energy estimation 100 TFile fileRF(fFileName, "recreate"); 101 if (!fileRF.IsOpen()) 102 { 103 *fLog << err << "ERROR - File to store RFs could not be opened... abort." << endl; 104 return kFALSE; 105 } 106 107 const Int_t nobs = fNumObsoleteVariables; // Number of obsolete columns 108 109 const MDataArray &dcol = *matrixtrain.GetColumns(); 110 111 MDataArray usedrules; 112 for (Int_t i=0; i<ncols; i++) 113 if (i<ncols-nobs) // -3 is important!!! 114 usedrules.AddEntry(dcol[i].GetRule()); 115 else 116 *fLog << inf << "Skipping " << dcol[i].GetRule() << " for training" << endl; 117 118 MDataArray rules(usedrules); 119 rules.AddEntry(ver<2?"Classification":dcol[ncols-1].GetRule()); 120 121 // prepare matrix for current energy bin 122 TMatrix mat(matrixtrain.GetM()); 123 124 // last column must be removed (true energy col.) 125 mat.ResizeTo(nrows, ncols-nobs+1); 126 127 if (fDebug) 128 gLog.SetNullOutput(kTRUE); 129 130 const Int_t nbins = ver>0 ? 1 : grid.GetSize()-1; 131 for (Int_t ie=0; ie<nbins; ie++) 132 { 133 switch (ver) 134 { 135 case 0: // Replace Energy Grid by classification 136 { 137 Int_t irows=0; 138 for (Int_t j=0; j<nrows; j++) 139 { 140 const Double_t energy = matrixtrain.GetM()(j,ncols-1); 141 const Bool_t inside = energy>grid[ie] && energy<=grid[ie+1]; 142 143 mat(j, ncols-nobs) = inside ? 1 : 0; 144 145 if (inside) 146 irows++; 147 } 148 if (irows==0) 149 *fLog << warn << "WARNING - Skipping"; 150 else 151 *fLog << inf << "Training RF for"; 152 153 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irows << "/" << nrows << endl; 154 155 if (irows==0) 156 continue; 157 } 158 break; 159 160 case 1: // Use Energy as classifier 161 case 2: 162 for (Int_t j=0; j<nrows; j++) 163 mat(j, ncols-nobs) = matrixtrain.GetM()(j,ncols-1); 164 break; 165 } 166 167 MHMatrix matrix(mat, &rules, "MatrixTrain"); 168 169 MParList plist; 170 MTaskList tlist; 171 plist.AddToList(&tlist); 172 plist.AddToList(&matrix); 173 174 MRanForest rf; 175 rf.SetNumTrees(fNumTrees); 176 rf.SetNumTry(fNumTry); 177 rf.SetNdSize(fNdSize); 178 rf.SetClassify(ver<2 ? 1 : 0); 179 if (ver==1) 180 rf.SetGrid(grid); 181 182 plist.AddToList(&rf); 183 184 MRanForestGrow rfgrow; 185 tlist.AddToList(&rfgrow); 186 187 MFillH fillh("MHRanForestGini"); 188 tlist.AddToList(&fillh); 189 190 MEvtLoop evtloop; 191 evtloop.SetParList(&plist); 192 evtloop.SetDisplay(fDisplay); 193 evtloop.SetLogStream(fLog); 194 195 if (!evtloop.Eventloop()) 196 return kFALSE; 197 198 if (fDebug) 199 gLog.SetNullOutput(kFALSE); 200 201 if (ver==0) 202 { 203 // Calculate bin center 204 const Double_t E = (TMath::Log10(grid[ie])+TMath::Log10(grid[ie+1]))/2; 205 206 // save whole forest 207 rf.SetUserVal(E); 208 rf.SetName(Form("%.10f", E)); 209 } 210 211 rf.Write(); 212 } 213 214 // save rules 215 usedrules.Write("rules"); 216 217 return kTRUE; 218 } 219 220 Int_t MRanForestCalc::ReadForests(MParList &plist) 221 { 222 TFile fileRF(fFileName, "read"); 223 if (!fileRF.IsOpen()) 224 { 225 *fLog << err << dbginf << "File containing RFs could not be opened... aborting." << endl; 226 return kFALSE; 227 } 228 229 fEForests.Delete(); 230 231 TIter Next(fileRF.GetListOfKeys()); 232 TObject *o=0; 233 while ((o=Next())) 234 { 235 MRanForest *forest=0; 236 fileRF.GetObject(o->GetName(), forest); 237 if (!forest) 238 continue; 239 240 forest->SetUserVal(atof(o->GetName())); 241 242 fEForests.Add(forest); 243 } 244 245 // Maybe fEForests[0].fRules yould be used instead? 246 247 if (fData->Read("rules")<=0) 248 { 249 *fLog << err << "ERROR - Reading 'rules' from file " << fFileName << endl; 250 return kFALSE; 251 } 252 253 return kTRUE; 254 } 255 109 256 Int_t MRanForestCalc::PreProcess(MParList *plist) 110 257 { 111 if (!fRanForest) 112 { 113 fRanForest = (MRanForest*)plist->FindObject("MRanForest"); 114 if (!fRanForest) 258 fRFOut = (MParameterD*)plist->FindCreateObj("MParameterD", fNameOutput); 259 if (!fRFOut) 260 return kFALSE; 261 262 fData = (MDataArray*)plist->FindCreateObj("MDataArray"); 263 if (!fData) 264 return kFALSE; 265 266 if (!ReadForests(*plist)) 267 { 268 *fLog << err << "Reading RFs failed... aborting." << endl; 269 return kFALSE; 270 } 271 272 *fLog << inf << "RF read from " << fFileName << endl; 273 274 if (fTestMatrix) 275 return kTRUE; 276 277 fData->Print(); 278 279 if (!fData->PreProcess(plist)) 280 { 281 *fLog << err << "PreProcessing of the MDataArray failed... aborting." << endl; 282 return kFALSE; 283 } 284 285 return kTRUE; 286 } 287 288 #include <TGraph.h> 289 #include <TF1.h> 290 Int_t MRanForestCalc::Process() 291 { 292 TVector event; 293 if (fTestMatrix) 294 *fTestMatrix >> event; 295 else 296 *fData >> event; 297 298 // --------------- Single Tree RF ------------------- 299 if (fEForests.GetEntries()==1) 300 { 301 MRanForest *rf = (MRanForest*)fEForests[0]; 302 fRFOut->SetVal(rf->CalcHadroness(event)); 303 fRFOut->SetReadyToSave(); 304 305 return kTRUE; 306 } 307 308 // --------------- Multi Tree RF ------------------- 309 static TF1 f1("f1", "gaus"); 310 311 Double_t sume = 0; 312 Double_t sumh = 0; 313 Double_t maxh = 0; 314 Double_t maxe = 0; 315 316 Double_t max = -1e10; 317 Double_t min = 1e10; 318 319 TIter Next(&fEForests); 320 MRanForest *rf = 0; 321 322 TGraph g; 323 while ((rf=(MRanForest*)Next())) 324 { 325 const Double_t h = rf->CalcHadroness(event); 326 const Double_t e = rf->GetUserVal(); 327 328 g.SetPoint(g.GetN(), e, h); 329 330 sume += e*h; 331 sumh += h; 332 333 if (h>maxh) 115 334 { 116 *fLog << err << dbginf << "MRanForest not found... aborting." << endl;117 return kFALSE;335 maxh = h; 336 maxe = e; 118 337 } 119 } 120 121 if (!fRanTree) 122 { 123 fRanTree = (MRanTree*)plist->FindObject("MRanTree"); 124 if (!fRanTree) 125 { 126 *fLog << err << dbginf << "MRanTree not found... aborting." << endl; 127 return kFALSE; 128 } 129 } 130 131 fData = fRanTree->GetRules(); 132 133 if (!fData) 134 { 135 *fLog << err << dbginf << "Error matrix doesn't contain columns... aborting." << endl; 136 return kFALSE; 137 } 138 139 if (!fData->PreProcess(plist)) 140 { 141 *fLog << err << dbginf << "PreProcessing of the MDataArray failed for the columns failed... aborting." << endl; 142 return kFALSE; 143 } 144 145 fHadroness = (MParameterD*)plist->FindCreateObj("MParameterD", fHadronnessName); 146 if (!fHadroness) 147 return kFALSE; 338 if (e>max) 339 max = e; 340 if (e<min) 341 min = e; 342 } 343 344 switch (fEstimationMode) 345 { 346 case kMean: 347 fRFOut->SetVal(pow(10, sume/sumh)); 348 break; 349 case kMaximum: 350 fRFOut->SetVal(pow(10, maxe)); 351 break; 352 case kFit: 353 f1.SetParameter(0, maxh); 354 f1.SetParameter(1, maxe); 355 f1.SetParameter(2, 0.125); 356 g.Fit(&f1, "Q0N"); 357 fRFOut->SetVal(pow(10, f1.GetParameter(1))); 358 break; 359 } 360 361 fRFOut->SetReadyToSave(); 148 362 149 363 return kTRUE; … … 153 367 // 154 368 // 155 Int_t MRanForestCalc::Process()156 {157 // first copy the data from the data array to a vector event158 TVector event;159 *fData >> event;160 161 Double_t hadroness=fRanForest->CalcHadroness(event);162 fHadroness->SetVal(hadroness);163 164 return kTRUE;165 }166 167 Bool_t MRanForestCalc::Grow(MHMatrix *matrixg,MHMatrix *matrixh,Int_t ntree,168 Int_t numtry,Int_t ndsize,const char* treefile,169 const char* treename,const char* contname,170 const char* hgininame)171 {172 173 treename = treename ? treename : "Tree";174 contname = contname ? contname : "MRanTree";175 hgininame = hgininame ? hgininame : "MHRanForestGini";176 177 if (!matrixg->IsValid())178 {179 *fLog << err << dbginf << " MRanForestCalc::Grow - ERROR: matrixg not valid." << endl;180 return kFALSE;181 }182 if(!matrixh->IsValid())183 {184 *fLog << err << dbginf << " MRanForestCalc::Grow - ERROR: matrixh not valid." << endl;185 return kFALSE;186 }187 188 MEvtLoop run(GetName());189 MTaskList tlist;190 MParList plist;191 plist.AddToList(&tlist);192 plist.AddToList(matrixg);193 plist.AddToList(matrixh);194 195 // creating training task and setting parameters196 MRanForestGrow rfgrow;197 rfgrow.SetNumTrees(ntree); // number of trees198 rfgrow.SetNumTry(numtry); // number of trials in random split selection199 rfgrow.SetNdSize(ndsize); // limit for nodesize200 tlist.AddToList(&rfgrow);201 202 if(treefile){203 MWriteRootFile rfwrite(treefile);204 rfwrite.AddContainer(contname,treename);205 tlist.AddToList(&rfwrite);206 }207 208 MFillH fillh(hgininame);209 tlist.AddToList(&fillh);210 211 run.SetParList(&plist);212 213 // Execute tree growing214 if (!run.Eventloop())215 {216 *fLog << err << dbginf << "Evtloop in MRanForestCalc::Grow failed." << endl;217 return kFALSE;218 }219 tlist.PrintStatistics(0, kTRUE);220 221 if (TestBit(kEnableGraphicalOutput))222 plist.FindObject(hgininame)->DrawClone();223 224 return kTRUE;225 }226 227 Bool_t MRanForestCalc::Fill(Int_t ntree,const char* treefile,const char* treename)228 {229 treefile = treefile ? treefile : "RF.root";230 treename = treename ? treename : "Tree";231 232 MParList plist;233 234 MTaskList tlist;235 plist.AddToList(&tlist);236 237 MReadTree read(treename,treefile);238 read.DisableAutoScheme();239 240 MRanForestFill rffill;241 rffill.SetNumTrees(ntree);242 243 tlist.AddToList(&read);244 tlist.AddToList(&rffill);245 246 MEvtLoop run(GetName());247 run.SetParList(&plist);248 249 //250 // Execute tree reading251 //252 if (!run.Eventloop())253 {254 *fLog << err << dbginf << "Evtloop in MRanForestCalc::Fill failed." << endl;255 return kFALSE;256 }257 tlist.PrintStatistics(0, kTRUE);258 259 return kTRUE;260 }261 262 369 Int_t MRanForestCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print) 263 370 { 264 if (!IsEnvDefined(env, prefix, "File", print)) 265 return kFALSE; 266 267 TString fname = GetEnvValue(env, prefix, "File", "RF.root"); 268 TString tname = GetEnvValue(env, prefix, "Tree", "Tree"); 269 const Int_t num = GetEnvValue(env, prefix, "NumTrees", 100); 270 const Bool_t debug = GetEnvValue(env, prefix, "Debug", kFALSE); 271 272 fname.ReplaceAll("\015", ""); 273 tname.ReplaceAll("\015", ""); 274 275 *fLog << inf << dbginf << "Reading " << num << " trees " << tname << " from file " << fname << endl; 276 277 gLog.SetNullOutput(!debug); 278 MEvtLoop evtloop; 279 MParList plist; 280 evtloop.SetParList(&plist); 281 MLog l; 282 l.SetNullOutput(!debug); 283 evtloop.SetLogStream(&l); 284 gLog.SetNullOutput(debug); 285 286 if (IsOwner()) 287 { 288 delete fRanForest; 289 delete fRanTree; 290 } 291 fRanForest = new MRanForest; 292 fRanTree = new MRanTree; 293 SetOwner(); 294 295 plist.AddToList(fRanForest); 296 plist.AddToList(fRanTree); 297 298 MTaskList tlist; 299 plist.AddToList(&tlist); 300 301 MReadTree read(tname, fname); 302 read.DisableAutoScheme(); 303 304 MRanForestFill rffill; 305 rffill.SetNumTrees(num); 306 307 tlist.AddToList(&read); 308 tlist.AddToList(&rffill); 309 310 if (!evtloop.Eventloop()) 311 { 312 *fLog << err << "ERROR - Reading " << tname << " from file " << fname << endl; 313 return kERROR; 314 } 315 316 return kTRUE; 317 } 371 Bool_t rc = kFALSE; 372 if (IsEnvDefined(env, prefix, "FileName", print)) 373 { 374 rc = kTRUE; 375 SetFileName(GetEnvValue(env, prefix, "FileName", fFileName)); 376 } 377 if (IsEnvDefined(env, prefix, "Debug", print)) 378 { 379 rc = kTRUE; 380 SetDebug(GetEnvValue(env, prefix, "Debug", fDebug)); 381 } 382 if (IsEnvDefined(env, prefix, "NameOutput", print)) 383 { 384 rc = kTRUE; 385 SetNameOutput(GetEnvValue(env, prefix, "NameOutput", fNameOutput)); 386 } 387 if (IsEnvDefined(env, prefix, "EstimationMode", print)) 388 { 389 TString txt = GetEnvValue(env, prefix, "EstimationMode", ""); 390 txt = txt.Strip(TString::kBoth); 391 txt.ToLower(); 392 if (txt==(TString)"mean") 393 fEstimationMode = kMean; 394 if (txt==(TString)"maximum") 395 fEstimationMode = kMaximum; 396 if (txt==(TString)"fit") 397 fEstimationMode = kFit; 398 rc = kTRUE; 399 } 400 return rc; 401 } -
trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h
r6893 r7413 6 6 #endif 7 7 8 #ifndef MARS_MHMatrix9 #include "MHMatrix.h"8 #ifndef ROOT_TObjArray 9 #include <TObjArray.h> 10 10 #endif 11 11 12 class MParList; 12 #ifndef ROOT_TArrayD 13 #include <TArrayD.h> 14 #endif 15 16 class MDataArray; 13 17 class MParameterD; 14 class MDataArray; 15 class MRanTree; 16 class MRanForest; 18 class MHMatrix; 17 19 18 20 class MRanForestCalc : public MTask 19 21 { 22 public: 23 enum EstimationMode_t 24 { 25 kMean, 26 kMaximum, 27 kFit 28 }; 29 20 30 private: 21 Int_t fNum; // number of trees used to compute hadronness 31 static const TString gsDefName; //! Default Name 32 static const TString gsDefTitle; //! Default Title 33 static const TString gsNameOutput; //! Default Output name 22 34 23 TString fHadronnessName; // Name of container storing hadronness35 Bool_t fDebug; // Debugging of eventloop while training on/off 24 36 25 MParameterD *fHadroness; //! Output container for calculated hadroness 37 TString fFileName; // File name to forest 38 TObjArray fEForests; // List of forests 39 40 TString fNameOutput; // Name of output container 41 26 42 MDataArray *fData; //! Used to store the MDataChains to get the event values 27 MRanForest *fRanForest; 28 MRanTree *fRanTree; 43 MParameterD *fRFOut; //! Used to store result 29 44 45 Int_t fNumTrees; //! Training parameters 46 Int_t fNumTry; //! Training parameters 47 Int_t fNdSize; //! Training parameters 48 49 Int_t fNumObsoleteVariables; 50 51 MHMatrix *fTestMatrix; //! Test Matrix used in Process (together with MMatrixLoop) 52 53 EstimationMode_t fEstimationMode; 54 55 private: 56 // MTask 30 57 Int_t PreProcess(MParList *plist); 31 58 Int_t Process(); 32 59 33 enum { kIsOwner = BIT(14) }; 60 // MRanForestCalc 61 Int_t ReadForests(MParList &plist); 34 62 35 Bool_t IsOwner() const { return TestBit(kIsOwner); } 36 void SetOwner() { SetBit(kIsOwner); } 63 // MParContainer 64 Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print); 65 66 // Train Interface 67 Int_t Train(const MHMatrix &n, const TArrayD &grid, Int_t ver=2); 37 68 38 69 public: … … 40 71 ~MRanForestCalc(); 41 72 42 Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print); 73 // Setter for estimation 74 void SetFileName(TString filename) { fFileName = filename; } 75 void SetEstimationMode(EstimationMode_t op) { fEstimationMode = op; } 76 void SetNameOutput(TString name=gsNameOutput) { fNameOutput = name; } 43 77 44 void SetHadronnessName(const TString name) { fHadronnessName = name; } 45 TString GetHadronnessName() const { return fHadronnessName; } 78 // Setter for training 79 void SetNumTrees(UShort_t n=100) { fNumTrees = n; } 80 void SetNdSize(UShort_t n=5) { fNdSize = n; } 81 void SetNumTry(UShort_t n=0) { fNumTry = n; } 82 void SetDebug(Bool_t b=kTRUE) { fDebug = b; } 46 83 47 void Set UseNumTrees(UShort_t n=100) { fNum= n; }84 void SetNumObsoleteVariables(Int_t n=1) { fNumObsoleteVariables = n; } 48 85 49 Bool_t Grow(MHMatrix *matrixg,MHMatrix *matrixh,Int_t ntree, 50 Int_t numtry,Int_t ndsize,const char* treefile=0, 51 const char* treename=0,const char* contname=0, 52 const char* hgininame=0); 86 // Train Interface 87 Int_t TrainMultiRF(const MHMatrix &n, const TArrayD &grid) 88 { 89 return Train(n, grid, 0); 90 } 91 Int_t TrainSingleRF(const MHMatrix &n, const TArrayD &grid=TArrayD()) 92 { 93 return Train(n, grid, grid.GetSize()==0 ? 2 : 1); 94 } 53 95 54 Bool_t Fill(Int_t ntree,const char* treefile=0,const char* treename=0); 96 // Test Interface 97 void SetTestMatrix(MHMatrix *m=0) { fTestMatrix=m; } 98 void InitMapping(MHMatrix *m=0) { fTestMatrix=m; } 55 99 56 57 ClassDef(MRanForestCalc, 0) // Task 100 ClassDef(MRanForestCalc, 0) // Task to calculate RF output and for RF training 58 101 }; 59 102 -
trunk/MagicSoft/Mars/mranforest/MRanTree.h
r7396 r7413 28 28 Int_t fNumNodes; 29 29 Int_t fNumEndNodes; 30 Float_t fError; 30 31 31 32 TArrayI fBestVar; … … 70 71 void SetNdSize(Int_t n); 71 72 void SetNumTry(Int_t n); 73 void SetError(Float_t f) { fError = f; } 72 74 73 75 Int_t GetNdSize() const { return fNdSize; } … … 75 77 Int_t GetNumNodes() const { return fNumNodes; } 76 78 Int_t GetNumEndNodes() const { return fNumEndNodes; } 79 Float_t GetError() const { return fError; } 77 80 78 81 Int_t GetBestVar(Int_t i) const { return fBestVar.At(i); } -
trunk/MagicSoft/Mars/mranforest/Makefile
r7396 r7413 25 25 MRanForest.cc \ 26 26 MRanForestGrow.cc \ 27 MRanForestCalc.cc \ 27 28 MHRanForest.cc \ 28 MHRanForestGini.cc \ 29 MRFEnergyEst.cc 29 MHRanForestGini.cc 30 30 31 31 ############################################################ -
trunk/MagicSoft/Mars/mranforest/RanForestLinkDef.h
r7396 r7413 8 8 #pragma link C++ class MRanForest+; 9 9 #pragma link C++ class MRanForestGrow+; 10 #pragma link C++ class MRanForestCalc+; 10 11 11 12 #pragma link C++ class MHRanForest+; 12 13 #pragma link C++ class MHRanForestGini+; 13 14 14 #pragma link C++ class MRFEnergyEst+;15 16 15 #endif
Note:
See TracChangeset
for help on using the changeset viewer.