Changeset 7142 for trunk/MagicSoft/Mars/mranforest
- Timestamp:
- 06/10/05 13:10:09 (19 years ago)
- Location:
- trunk/MagicSoft/Mars/mranforest
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc
r7130 r7142 69 69 // 70 70 MRFEnergyEst::MRFEnergyEst(const char *name, const char *title) 71 : fNumTrees(-1), fNumTry(-1), fNdSize(-1), fD ata(0), fEnergyEst(0),72 f TestMatrix(0)71 : fNumTrees(-1), fNumTry(-1), fNdSize(-1), fDebug(kFALSE), 72 fData(0), fEnergyEst(0), fTestMatrix(0) 73 73 { 74 74 fName = name ? name : gsDefName.Data(); … … 91 91 } 92 92 93 const Int_t ncols = matrixtrain.GetM().GetNcols(); 94 const Int_t nrows = matrixtrain.GetM().GetNrows(); 93 const TMatrix &m = matrixtrain.GetM(); 94 95 const Int_t ncols = m.GetNcols(); 96 const Int_t nrows = m.GetNrows(); 95 97 if (ncols<=0 || nrows <=0) 96 98 { … … 114 116 } 115 117 118 const Int_t nobs = 3; // Number of obsolete columns 119 116 120 MDataArray usedrules; 117 for(Int_t i=0; i<ncols- 3; i++) // -3 is important!!!121 for(Int_t i=0; i<ncols-nobs; i++) // -3 is important!!! 118 122 usedrules.AddEntry((*matrixtrain.GetColumns())[i].GetRule()); 119 120 const TMatrix &m = matrixtrain.GetM();121 123 122 124 // training of RF for each energy bin … … 140 142 } 141 143 142 const Bool_t valid = irow1*irow0>0;143 144 if ( !valid)144 const Bool_t invalid = irow1==0 || irow0==0; 145 146 if (invalid) 145 147 *fLog << warn << "WARNING - Skipping"; 146 148 else 147 149 *fLog << inf << "Training RF for"; 148 150 149 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") "<< endl;150 151 if ( !valid)151 *fLog << " energy bin " << ie << " (" << grid[ie] << ", " << grid[ie+1] << ") " << irow0 << " " << irow1 << endl; 152 153 if (invalid) 152 154 continue; 153 155 154 gLog.SetNullOutput(kTRUE); 156 if (fDebug) 157 gLog.SetNullOutput(kTRUE); 155 158 156 159 // last column must be removed (true energy col.) 157 mat1.ResizeTo(irow1, ncols- 1);158 mat0.ResizeTo(irow0, ncols- 1);160 mat1.ResizeTo(irow1, ncols-nobs); 161 mat0.ResizeTo(irow0, ncols-nobs); 159 162 160 163 // create MHMatrix as input for RF … … 183 186 evtloop.SetParList(&plist); 184 187 185 gLog.SetNullOutput(kFALSE); 188 if (fDebug) 189 gLog.SetNullOutput(kFALSE); 186 190 187 191 if (!evtloop.Eventloop()) 188 192 return kFALSE; 189 193 194 const Double_t E = (log10(grid[ie])+log10(grid[ie+1]))/2; 195 190 196 // save whole forest 191 197 MRanForest *forest=(MRanForest*)plist.FindObject("MRanForest"); 192 const TString title = Form("%f", TMath::Sqrt(grid[ie]*grid[ie+1])); 193 //const TString title = Form("%f", (grid[ie]+grid[ie+1])/2); 198 const TString title = Form("%.10f", E); 194 199 forest->SetTitle(title); 195 200 forest->Write(title); … … 203 208 return kTRUE; 204 209 } 205 /* 206 Int_t MRFEnergyEst::Test(const MHMatrix &matrixtest) 207 { 208 gLog.Separator("MRFEnergyEst - Test"); 209 210 const Int_t ncols = matrixtest.GetM().GetNcols(); 211 const Int_t nrows = matrixtest.GetM().GetNrows(); 212 213 if (ncols<=0 || nrows <=0) 214 { 215 *fLog << err << dbginf << "No. of columns or no. of rows of matrixtrain equal 0 ... aborting." << endl; 216 return kFALSE; 217 } 218 219 if (!ReadForests()) 220 { 221 *fLog << err << dbginf << "Reading RFs failed... aborting." << endl; 222 return kFALSE; 223 } 224 225 const Int_t nbins=fEForests.GetSize(); 226 227 Double_t elow = FLT_MAX; 228 Double_t eup = -FLT_MAX; 229 230 for(Int_t j=0;j<nbins;j++) 231 { 232 elow = TMath::Min(atof(fEForests[j]->GetTitle()), elow); 233 eup = TMath::Max(atof(fEForests[j]->GetTitle()), eup); 234 } 235 236 TH1F hres("hres", "", 1000, -10, 10); 237 TH2F henergy("henergy", "",100, elow, eup,100, elow, eup); 238 239 const TMatrix &m=matrixtest.GetM(); 240 for(Int_t i=0;i<nrows;i++) 241 { 242 Double_t etrue = m(i,ncols-1); 243 Double_t eest = 0; 244 Double_t hsum = 0; 245 246 for(Int_t j=0;j<nbins;j++) 247 { 248 const TVector v = TMatrixFRow_const(m,i); 249 250 const Double_t h = ((MRanForest*)fEForests[j])->CalcHadroness(v); 251 const Double_t e = atof(fEForests[j]->GetTitle()); 252 253 hsum += h; 254 eest += h*e; 255 } 256 eest /= hsum; 257 eest = pow(10., eest); 258 259 //if (etrue>80.) 260 // hres.Fill((eest-etrue)/etrue); 261 262 henergy.Fill(log10(etrue),log10(eest)); 263 } 264 265 if(gStyle) 266 gStyle->SetOptFit(1); 267 268 // show results 269 TCanvas *c=MH::MakeDefCanvas("c","",300,900); 270 271 c->Divide(1,3); 272 c->cd(1); 273 henergy.SetTitle("Estimated vs true energy"); 274 henergy.GetXaxis()->SetTitle("log10(E_{true}[GeV])"); 275 henergy.GetYaxis()->SetTitle("log10(E_{est}[GeV])"); 276 henergy.DrawCopy(); 277 278 c->cd(2); 279 TH1F *hptr=(TH1F*)henergy.ProfileX("_px",1,100,"S"); 280 hptr->SetTitle("Estimated vs true energy - profile"); 281 hptr->GetXaxis()->SetTitle("log10(E_{true}[GeV])"); 282 hptr->GetYaxis()->SetTitle("log10(E_{est}[GeV])"); 283 hptr->DrawCopy(); 284 285 c->cd(3); 286 hres.Fit("gaus"); 287 hres.SetTitle("Energy resolution for E_{true}>80Gev"); 288 hres.GetXaxis()->SetTitle("(E_{estimated}-E_{true})/E_{true})"); 289 hres.GetYaxis()->SetTitle("counts"); 290 hres.DrawCopy(); 291 292 c->GetPad(1)->SetGrid(); 293 c->GetPad(2)->SetGrid(); 294 c->GetPad(3)->SetGrid(); 295 296 return kTRUE; 297 } 298 */ 299 Int_t MRFEnergyEst::ReadForests(MParList *plist) 210 211 Int_t MRFEnergyEst::ReadForests(MParList &plist) 300 212 { 301 213 TFile fileRF(fFileName,"read"); … … 321 233 322 234 fEForests.Add(forest); 323 324 } 325 326 if (plist) 327 { 328 fData = (MDataArray*)plist->FindCreateObj("MDataArray"); 329 fData->Read("rules"); 330 } 331 332 fileRF.Close(); 235 } 236 237 if (fData->Read("rules")<=0) 238 { 239 *fLog << err << "ERROR - Reading 'rules' from file " << fFileName << endl; 240 return kFALSE; 241 } 333 242 334 243 return kTRUE; … … 341 250 return kFALSE; 342 251 343 if (!ReadForests(plist)) 252 fData = (MDataArray*)plist->FindCreateObj("MDataArray"); 253 if (!fData) 254 return kFALSE; 255 256 if (!ReadForests(*plist)) 344 257 { 345 258 *fLog << err << "Reading RFs failed... aborting." << endl; 346 259 return kFALSE; 347 260 } 261 262 *fLog << inf << "RF read from " << fFileName << endl; 348 263 349 264 if (fTestMatrix) 350 265 return kTRUE; 351 266 352 if (!fData) 353 { 354 *fLog << err << "MDataArray not found... aborting." << endl; 355 return kFALSE; 356 } 267 fData->Print(); 357 268 358 269 if (!fData->PreProcess(plist)) … … 387 298 388 299 hsum += h; 389 eest += h*e;390 } 391 392 fEnergyEst->SetVal( eest/hsum);300 eest += e*h; 301 } 302 303 fEnergyEst->SetVal(pow(10, eest/hsum)); 393 304 fEnergyEst->SetReadyToSave(); 394 305 395 306 return kTRUE; 396 307 } 308 309 // -------------------------------------------------------------------------- 310 // 311 // 312 Int_t MRFEnergyEst::ReadEnv(const TEnv &env, TString prefix, Bool_t print) 313 { 314 Bool_t rc = kFALSE; 315 if (IsEnvDefined(env, prefix, "FileName", print)) 316 { 317 rc = kTRUE; 318 SetFileName(GetEnvValue(env, prefix, "FileName", fFileName)); 319 } 320 if (IsEnvDefined(env, prefix, "Debug", print)) 321 { 322 rc = kTRUE; 323 SetDebug(GetEnvValue(env, prefix, "Debug", fDebug)); 324 } 325 return rc; 326 } -
trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h
r7134 r7142 23 23 Int_t fNdSize; // Training parameters 24 24 25 Bool_t fDebug; // Debugging of eventloop while training on/off 26 25 27 TString fFileName; 26 28 TObjArray fEForests; … … 34 36 Int_t Process(); 35 37 36 Int_t ReadForests(MParList *plist=NULL); 38 Int_t ReadForests(MParList &plist); 39 40 Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print); 37 41 38 42 public: … … 41 45 void SetFileName(TString str) { fFileName = str; } 42 46 43 void SetNumTrees(Int_t n=-1) { fNumTrees = n; }44 void SetNdSize(Int_t n=-1) { fNdSize = n; }45 void SetNumTry(Int_t n=-1) { fNumTry = n; }47 void SetNumTrees(Int_t n=-1) { fNumTrees = n; } 48 void SetNdSize(Int_t n=-1) { fNdSize = n; } 49 void SetNumTry(Int_t n=-1) { fNumTry = n; } 46 50 47 51 void SetTestMatrix(MHMatrix *m=0) { fTestMatrix=m; } 48 52 void InitMapping(MHMatrix *m=0) { fTestMatrix=m; } 53 54 void SetDebug(Bool_t b=kTRUE) { fDebug = b; } 49 55 50 56 Int_t Train(const MHMatrix &n, const TArrayD &grid); -
trunk/MagicSoft/Mars/mranforest/MRanTree.cc
r4647 r7142 447 447 // are coded into fBestVar: 448 448 // status of node kt = TMath::Sign(1,fBestVar[kt]) 449 // class of node kt = fBestVar[kt]+2 (class defined by larger450 // node population, actually not used)449 // class of node kt = fBestVar[kt]+2 450 // (class defined by larger node population, actually not used) 451 451 // hadronness assigned to node kt = fBestSplit[kt] 452 452 … … 463 463 } 464 464 465 Double_t MRanTree::TreeHad(const TMatrix Row&event)465 Double_t MRanTree::TreeHad(const TMatrixFRow_const &event) 466 466 { 467 467 Int_t kt=0; … … 504 504 Bool_t MRanTree::AsciiWrite(ostream &out) const 505 505 { 506 TString str; 506 out.width(5); 507 out << fNumNodes << endl; 508 507 509 Int_t k; 508 509 out.width(5);out<<fNumNodes<<endl; 510 511 for (k=0;k<fNumNodes;k++) 512 { 513 str=Form("%f",GetBestSplit(k)); 510 for (k=0; k<fNumNodes; k++) 511 { 512 TString str=Form("%f", GetBestSplit(k)); 514 513 515 514 out.width(5); out << k; … … 521 520 out.width(5); out << GetNodeClass(k); 522 521 } 523 out <<endl;524 525 return k ==fNumNodes;526 } 522 out << endl; 523 524 return kTRUE; 525 } -
trunk/MagicSoft/Mars/mranforest/MRanTree.h
r2307 r7142 16 16 class TMatrix; 17 17 class TMatrixRow; 18 class TMatrixFRow_const; 18 19 class TVector; 19 20 class TRandom; … … 84 85 85 86 Double_t TreeHad(const TVector &event); 86 Double_t TreeHad(const TMatrix Row&event);87 Double_t TreeHad(const TMatrixFRow_const &event); 87 88 Double_t TreeHad(const TMatrix &m, Int_t ievt); 88 89 Double_t TreeHad();
Note:
See TracChangeset
for help on using the changeset viewer.