Changeset 7396 for trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc
- Timestamp:
- 11/14/05 09:45:33 (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc
r7178 r7396 17 17 ! 18 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> 19 20 ! 20 21 ! Copyright: MAGIC Software Development, 2000-2005 … … 31 32 #include "MRFEnergyEst.h" 32 33 33 #include <TFile.h>34 #include <TList.h>35 36 #include <TH1.h>37 #include <TH2.h>38 #include <TStyle.h>39 #include <TCanvas.h>40 #include <TMath.h>41 34 #include <TVector.h> 42 35 … … 45 38 #include "MLog.h" 46 39 #include "MLogManip.h" 40 41 #include "MData.h" 42 #include "MDataArray.h" 43 44 #include "MRanForest.h" 45 #include "MParameters.h" 47 46 48 47 #include "MParList.h" 49 48 #include "MTaskList.h" 50 49 #include "MEvtLoop.h" 51 52 #include "MRanTree.h"53 #include "MRanForest.h"54 50 #include "MRanForestGrow.h" 55 51 56 #include "MData.h"57 #include "MParameters.h"58 59 52 ClassImp(MRFEnergyEst); 60 53 61 54 using namespace std; 62 55 63 static const TString gsDefName = "MRFEnergyEst"; 64 static const TString gsDefTitle = "RF for energy estimation"; 65 66 // -------------------------------------------------------------------------- 67 // 68 // Default constructor. Set the name and title of this object 69 // 56 const TString MRFEnergyEst::gsDefName = "MRFEnergyEst"; 57 const TString MRFEnergyEst::gsDefTitle = "RF for energy estimation"; 58 70 59 MRFEnergyEst::MRFEnergyEst(const char *name, const char *title) 71 60 : fDebug(kFALSE), fData(0), fEnergyEst(0), … … 75 64 fName = name ? name : gsDefName.Data(); 76 65 fTitle = title ? title : gsDefTitle.Data(); 66 67 // FIXME: 68 fNumTrees = 100; //100 69 fNumTry = 5; //3 70 fNdSize = 0; //1 0 means: in MRanForest estimated best value will be calculated 77 71 } 78 72 … … 82 76 } 83 77 84 // -------------------------------------------------------------------------- 85 // 86 // Train the RF with the goven MHMatrix. The last column must contain the 87 // True energy. 88 // 89 Int_t MRFEnergyEst::Train(const MHMatrix &matrixtrain, const TArrayD &grid) 78 Int_t MRFEnergyEst::Train(const MHMatrix &matrixtrain, const TArrayD &grid, Int_t ver) 90 79 { 91 80 gLog.Separator("MRFEnergyEst - Train"); … … 105 94 } 106 95 107 const Int_t nbins = grid.GetSize()-1;108 if (nbins<=0)109 {110 *fLog << err << "ERROR - Energy grid not vaild... abort." << endl;111 return kFALSE;112 }113 114 96 // rules (= combination of image par) to be used for energy estimation 115 97 TFile fileRF(fFileName, "recreate"); … … 122 104 const Int_t nobs = 3; // Number of obsolete columns 123 105 106 MDataArray &dcol = *matrixtrain.GetColumns(); 107 124 108 MDataArray usedrules; 125 for(Int_t i=0; i<ncols-nobs; i++) // -3 is important!!! 126 usedrules.AddEntry((*matrixtrain.GetColumns())[i].GetRule()); 127 128 // training of RF for each energy bin 109 for (Int_t i=0; i<ncols-nobs; i++) // -3 is important!!! 110 usedrules.AddEntry(dcol[i].GetRule()); 111 112 MDataArray rules(usedrules); 113 rules.AddEntry(ver<2?"Classification":dcol[ncols-1].GetRule()); 114 115 // prepare matrix for current energy bin 116 TMatrix mat(matrixtrain.GetM()); 117 118 // last column must be removed (true energy col.) 119 mat.ResizeTo(nrows, ncols-nobs+1); 120 121 if (fDebug) 122 gLog.SetNullOutput(kTRUE); 123 124 const Int_t nbins = ver>0 ? 1 : grid.GetSize()-1; 129 125 for (Int_t ie=0; ie<nbins; ie++) 130 126 { 131 TMatrix mat1(nrows, ncols); 132 TMatrix mat0(nrows, ncols); 133 134 // prepare matrix for current energy bin 135 Int_t irow1=0; 136 Int_t irow0=0; 137 138 const TMatrix &m = matrixtrain.GetM(); 139 for (Int_t j=0; j<nrows; j++) 127 switch (ver) 140 128 { 141 const Double_t energy = m(j,ncols-1); 142 143 if (energy>grid[ie] && energy<=grid[ie+1]) 144 TMatrixFRow(mat1, irow1++) = TMatrixFRow_const(m,j); 145 else 146 TMatrixFRow(mat0, irow0++) = TMatrixFRow_const(m,j); 129 case 0: // Replace Energy Grid by classification 130 { 131 Int_t irows=0; 132 for (Int_t j=0; j<nrows; j++) 133 { 134 const Double_t energy = matrixtrain.GetM()(j,ncols-1); 135 const Bool_t inside = energy>grid[ie] && energy<=grid[ie+1]; 136 137 mat(j, ncols-nobs) = inside ? 1 : 0; 138 139 if (inside) 140 irows++; 141 } 142 if (irows==0) 143 *fLog << warn << "WARNING - Skipping"; 144 else 145 *fLog << inf << "Training RF for"; 146 147 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irows << "/" << nrows << endl; 148 149 if (irows==0) 150 continue; 151 } 152 break; 153 154 case 1: // Use Energy as classifier 155 case 2: 156 for (Int_t j=0; j<nrows; j++) 157 mat(j, ncols-nobs) = matrixtrain.GetM()(j,ncols-1); 158 break; 147 159 } 148 160 149 const Bool_t invalid = irow1==0 || irow0==0; 150 151 if (invalid) 152 *fLog << warn << "WARNING - Skipping"; 153 else 154 *fLog << inf << "Training RF for"; 155 156 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irow0 << " " << irow1 << endl; 157 158 if (invalid) 159 continue; 160 161 if (fDebug) 162 gLog.SetNullOutput(kTRUE); 163 164 // last column must be removed (true energy col.) 165 mat1.ResizeTo(irow1, ncols-nobs); 166 mat0.ResizeTo(irow0, ncols-nobs); 167 168 // create MHMatrix as input for RF 169 MHMatrix matrix1(mat1, "MatrixHadrons"); 170 MHMatrix matrix0(mat0, "MatrixGammas"); 171 172 // training of RF 161 MHMatrix matrix(mat, &rules, "MatrixTrain"); 162 163 MParList plist; 173 164 MTaskList tlist; 174 MParList plist;175 165 plist.AddToList(&tlist); 176 plist.AddToList(&matrix0); 177 plist.AddToList(&matrix1); 166 plist.AddToList(&matrix); 167 168 MRanForest rf; 169 rf.SetNumTrees(fNumTrees); 170 rf.SetNumTry(fNumTry); 171 rf.SetNdSize(fNdSize); 172 rf.SetClassify(ver<2 ? 1 : 0); 173 if (ver==1) 174 rf.SetGrid(grid); 175 176 plist.AddToList(&rf); 178 177 179 178 MRanForestGrow rfgrow; 180 rfgrow.SetNumTrees(fNumTrees); // number of trees181 rfgrow.SetNumTry(fNumTry); // number of trials in random split selection182 rfgrow.SetNdSize(fNdSize); // limit for nodesize183 184 179 tlist.AddToList(&rfgrow); 185 180 186 181 MEvtLoop evtloop; 187 evtloop.SetDisplay(fDisplay);188 182 evtloop.SetParList(&plist); 189 183 … … 194 188 gLog.SetNullOutput(kFALSE); 195 189 196 // Calculate bin center 197 const Double_t E = (TMath::Log10(grid[ie])+TMath::Log10(grid[ie+1]))/2; 198 199 // save whole forest 200 MRanForest *forest=(MRanForest*)plist.FindObject("MRanForest"); 201 forest->SetUserVal(E); 202 forest->Write(Form("%.10f", E)); 190 if (ver==0) 191 { 192 // Calculate bin center 193 const Double_t E = (TMath::Log10(grid[ie])+TMath::Log10(grid[ie+1]))/2; 194 195 // save whole forest 196 rf.SetUserVal(E); 197 rf.SetName(Form("%.10f", E)); 198 } 199 200 rf.Write(); 203 201 } 204 202 … … 224 222 while ((o=Next())) 225 223 { 226 MRanForest *forest ;224 MRanForest *forest=0; 227 225 fileRF.GetObject(o->GetName(), forest); 228 226 if (!forest) … … 234 232 } 235 233 234 // Maybe fEForests[0].fRules yould be used instead? 235 236 236 if (fData->Read("rules")<=0) 237 237 { … … 249 249 return kFALSE; 250 250 251 cout << "MDataArray" << endl; 252 251 253 fData = (MDataArray*)plist->FindCreateObj("MDataArray"); 252 254 if (!fData) 253 255 return kFALSE; 254 256 257 cout << "ReadForests" << endl; 258 255 259 if (!ReadForests(*plist)) 256 260 { … … 275 279 } 276 280 277 // --------------------------------------------------------------------------278 //279 //280 281 #include <TGraph.h> 281 282 #include <TF1.h> 282 283 Int_t MRFEnergyEst::Process() 283 284 { 284 static TF1 f1("f1", "gaus");285 286 285 TVector event; 287 286 if (fTestMatrix) … … 290 289 *fData >> event; 291 290 291 // --------------- Single Tree RF ------------------- 292 if (fEForests.GetEntries()==1) 293 { 294 const MRanForest *rf = (MRanForest*)fEForests[0]; 295 fEnergyEst->SetVal(rf->CalcHadroness(event)); 296 fEnergyEst->SetReadyToSave(); 297 298 return kTRUE; 299 } 300 301 // --------------- Multi Tree RF ------------------- 302 static TF1 f1("f1", "gaus"); 303 292 304 Double_t sume = 0; 293 305 Double_t sumh = 0; … … 308 320 const Double_t h = rf->CalcHadroness(event); 309 321 const Double_t e = rf->GetUserVal(); 322 310 323 g.SetPoint(g.GetN(), e, h); 324 311 325 sume += e*h; 312 326 sumh += h; 327 313 328 if (h>maxh) 314 329 { … … 337 352 fEnergyEst->SetVal(pow(10, f1.GetParameter(1))); 338 353 break; 339 340 } 354 } 355 341 356 fEnergyEst->SetReadyToSave(); 342 357
Note:
See TracChangeset
for help on using the changeset viewer.