Changeset 1489 for trunk/MagicSoft/Mars/mhist/MHMatrix.cc
- Timestamp:
- 08/08/02 11:58:59 (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Mars/mhist/MHMatrix.cc
r1355 r1489 45 45 #include "MHMatrix.h" 46 46 47 #include <fstream.h> 48 47 49 #include <TList.h> 48 50 #include <TArrayD.h> … … 54 56 #include "MParList.h" 55 57 56 #include "MDataChain.h" 58 #include "MData.h" 59 #include "MDataArray.h" 57 60 58 61 ClassImp(MHMatrix); 59 62 60 // -------------------------------------------------------------------------- 61 // 62 // Default Constructor 63 static const TString gsDefName = "MHMatrix"; 64 static const TString gsDefTitle = "Multidimensional Matrix"; 65 66 // -------------------------------------------------------------------------- 67 // 68 // Default Constructor 63 69 // 64 70 MHMatrix::MHMatrix(const char *name, const char *title) 65 : fNumRow(0) 66 { 67 fName = name ? name : "MHMatrix"; 68 fTitle = title ? title : "Multidimensional Matrix"; 69 70 fData = new TList; 71 fData->SetOwner(); 72 73 fRules = new TList; 74 fRules->SetOwner(); 75 } 76 77 // -------------------------------------------------------------------------- 78 // 79 // Destructor 71 : fNumRow(0), fData(NULL) 72 { 73 fName = name ? name : gsDefName.Data(); 74 fTitle = title ? title : gsDefTitle.Data(); 75 } 76 77 // -------------------------------------------------------------------------- 78 // 79 // Constructor. Initializes the columns of the matrix with the entries 80 // from a MDataArray 81 // 82 MHMatrix::MHMatrix(MDataArray *mat, const char *name, const char *title) 83 : fNumRow(0), fData(mat) 84 { 85 fName = name ? name : gsDefName.Data(); 86 fTitle = title ? title : gsDefTitle.Data(); 87 } 88 89 // -------------------------------------------------------------------------- 90 // 91 // Destructor. Does not deleted a user given MDataArray, except IsOwner 92 // was called. 80 93 // 81 94 MHMatrix::~MHMatrix() 82 95 { 83 delete fData;84 delete fRules;96 if (TestBit(kIsOwner) && fData) 97 delete fData; 85 98 } 86 99 … … 99 112 } 100 113 101 MDataChain &chain = *new MDataChain(rule); 102 if (!chain.IsValid()) 103 { 104 *fLog << err << "Error - Rule cannot be translated... ignored." << endl; 105 delete &chain; 114 if (!fData) 115 { 116 fData = new MDataArray; 117 SetBit(kIsOwner); 118 } 119 120 fData->AddEntry(rule); 121 } 122 123 void MHMatrix::AddColumns(MDataArray *matrix) 124 { 125 if (fM.IsValid()) 126 { 127 *fLog << warn << "Warning - matrix is already in use. Can't add new columns... skipped." << endl; 106 128 return; 107 129 } 108 fData->Add(&chain); 109 110 TNamed *name = new TNamed(rule, rule); // Fimxe, in 3.02/07 the title can't be "", why? 111 fRules->Add(name); 112 } 113 114 void MHMatrix::AddColumns(const MHMatrix *matrix) 115 { 116 TIter Next(matrix->fRules); 117 TObject *rule=NULL; 118 while ((rule=Next())) 119 AddColumn(rule->GetName()); 130 131 if (fData) 132 *fLog << warn << "Warning - columns already added... replacing." << endl; 133 134 if (fData && TestBit(kIsOwner)) 135 { 136 delete fData; 137 ResetBit(kIsOwner); 138 } 139 140 fData = matrix; 120 141 } 121 142 … … 127 148 Bool_t MHMatrix::SetupFill(const MParList *plist) 128 149 { 129 if ( fData->GetSize()==0)130 { 131 *fLog << err << "Error - No Column specified... aborting." << endl;150 if (!fData) 151 { 152 *fLog << err << "Error - No Columns initialized... aborting." << endl; 132 153 return kFALSE; 133 154 } 134 155 135 TIter Next(fData); 136 MData *data = NULL; 137 while ((data=(MData*)Next())) 138 if (!data->PreProcess(plist)) 139 return kFALSE; 140 141 return kTRUE; 156 return fData->PreProcess(plist); 142 157 } 143 158 … … 155 170 if (!fM.IsValid()) 156 171 { 157 fM.ResizeTo(1, fData->Get Size());172 fM.ResizeTo(1, fData->GetNumEntries()); 158 173 return; 159 174 } … … 161 176 TMatrix m(fM); 162 177 163 fM.ResizeTo(fM.GetNrows()*2, fData->Get Size());178 fM.ResizeTo(fM.GetNrows()*2, fData->GetNumEntries()); 164 179 165 180 for (int x=0; x<m.GetNcols(); x++) … … 176 191 AddRow(); 177 192 178 int col=0; 179 TIter Next(fData); 180 MData *data = NULL; 181 while ((data=(MData*)Next())) 182 fM(fNumRow-1, col++) = data->GetValue(); 193 for (int col=0; col<fData->GetNumEntries(); col++) 194 fM(fNumRow-1, col) = (*fData)(col); 183 195 184 196 return kTRUE; … … 195 207 // It's not a fatal error so we don't need to stop PostProcessing... 196 208 // 197 if (fData->Get Size()<1|| fNumRow<1)209 if (fData->GetNumEntries()==0 || fNumRow<1) 198 210 return kTRUE; 199 211 200 212 TMatrix m(fM); 201 213 202 fM.ResizeTo(fNumRow, fData->Get Size());214 fM.ResizeTo(fNumRow, fData->GetNumEntries()); 203 215 204 216 for (int x=0; x<fM.GetNcols(); x++) … … 287 299 void MHMatrix::Print(Option_t *) const 288 300 { 289 int n=0; 290 291 TIter Next(fData->GetSize() ? fData : fRules); 292 MData *data = NULL; 293 while ((data=(MData*)Next())) 294 { 295 *fLog << all << " Column " << setw(3) << n++ << ": " << flush; 296 data->Print(); 297 *fLog << endl; 298 } 301 if (fData) 302 fData->Print(); 303 else 304 *fLog << all << "Sorry, no column information available." << endl; 299 305 300 306 fM.Print(); … … 336 342 } 337 343 344 // -------------------------------------------------------------------------- 345 // 346 // Calculated the distance of vector evt from the reference sample 347 // represented by the covariance metrix m. 348 // - If n<0 the kernel method is applied and 349 // sum(epx(-d)/n is returned. 350 // - For n>=0 the n nearest neighbors are summed and 351 // sqrt(sum(d))/n is returned. 352 // - if n==0 all distances are summed 353 // 338 354 Double_t MHMatrix::CalcDist(const TMatrix &m, const TVector &evt, Int_t num) const 339 355 { 356 if (num==0) // may later be used for another method 357 { 358 TVector d = evt; 359 d *= m; 360 return sqrt(d*evt); 361 } 362 340 363 const Int_t rows = fM.GetNrows(); 341 364 const Int_t cols = fM.GetNcols(); … … 358 381 359 382 dists[i] = d2*d; // square of distance 383 384 // 385 // This corrects for numerical uncertanties in cases of very 386 // small distances... 387 // 388 if (dists[i]<0) 389 dists[i]=0; 360 390 } 361 391 … … 363 393 TMath::Sort(dists.GetSize(), dists.GetArray(), idx.GetArray(), kFALSE); 364 394 365 const Int_t n = num<rows ? num : rows; 366 367 Double_t res = 0; 368 for (int i=0; i<n; i++) 369 res += dists[idx[i]]; 370 371 return sqrt(res)/n; 372 } 373 395 const Int_t n = abs(num)<rows ? abs(num) : rows; 396 397 if (num<0) 398 { 399 // 400 // Kernel function sum 401 // 402 const Double_t h = 1; 403 404 Double_t res = 0; 405 for (int i=0; i<n; i++) 406 res += exp(-dists[idx[i]]/h); 407 408 return log(res/n); 409 } 410 else 411 { 412 // 413 // Nearest Neighbor sum 414 // 415 Double_t res = 0; 416 for (int i=0; i<n; i++) 417 res += dists[idx[i]]; 418 419 return sqrt(res/n); 420 } 421 } 422 423 // -------------------------------------------------------------------------- 424 // 425 // Calls calc dist. In the case of the first call the covariance matrix 426 // fM2 is calculated. 427 // - If n<0 it is divided by (nrows-1)/h while h is the kernel factor. 428 // 374 429 Double_t MHMatrix::CalcDist(const TVector &evt, Int_t num) 375 430 { … … 379 434 fM2.ResizeTo(m); 380 435 fM2 = m; 436 fM2 *= fM.GetNrows()-1; 381 437 delete &m; 382 438 } … … 391 447 fM.ResizeTo(m); 392 448 fM = m; 393 394 TIter Next(fRules); 395 TObject *obj = NULL; 396 while ((obj=Next())) 397 fData->Add(new MDataChain(obj->GetName())); 398 } 449 } 450 451 void MHMatrix::StreamPrimitive(ofstream &out) const 452 { 453 Bool_t data = fData && !TestBit(kIsOwner); 454 455 if (data) 456 { 457 fData->SavePrimitive(out); 458 out << endl; 459 } 460 461 out << " MHMatrix " << GetUniqueName(); 462 463 if (data || fName!=gsDefName || fTitle!=gsDefTitle) 464 { 465 out << "("; 466 if (data) 467 out << "&" << fData->GetUniqueName(); 468 if (fName!=gsDefName || fTitle!=gsDefTitle) 469 { 470 if (data) 471 out << ", "; 472 out << "\"" << fName << "\""; 473 if (fTitle!=gsDefTitle) 474 out << ", \"" << fTitle << "\""; 475 } 476 } 477 out << ");" << endl; 478 479 if (fData && TestBit(kIsOwner)) 480 for (int i=0; i<fData->GetNumEntries(); i++) 481 out << " " << GetUniqueName() << ".AddColumn(\"" << (*fData)[i].GetRule() << "\");" << endl; 482 }
Note:
See TracChangeset
for help on using the changeset viewer.