]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliSymMatrix.cxx
ESD tags for muons modified, since they revealed to be
[u/mrichter/AliRoot.git] / STEER / AliSymMatrix.cxx
index 50bed502751f0880837d37a58519c54b5983ded3..184c511f5b6ea02880e780f541e126c04b751d74 100644 (file)
@@ -1,10 +1,24 @@
+/**********************************************************************************************/
+/* Fast symmetric matrix with dynamically expandable size.                                    */
+/* Only part can be used for matrix operations. It is defined as:                             */ 
+/* fNCols: rows built by constructor (GetSizeBooked)                                          */ 
+/* fNRows: number of rows added dynamically (automatically added on assignment to row)        */ 
+/*         GetNRowAdded                                                                       */ 
+/* fNRowIndex: total size (fNCols+fNRows), GetSize                                            */ 
+/* fRowLwb   : actual size to used for given operation, by default = total size, GetSizeUsed  */ 
+/*                                                                                            */ 
+/* Author: ruben.shahoyan@cern.ch                                                             */
+/*                                                                                            */ 
+/**********************************************************************************************/
 #include <stdlib.h>
 #include <stdio.h>
 #include <iostream>
+#include <float.h>
 //
-#include "TClass.h"
-#include "TMath.h"
+#include <TClass.h>
+#include <TMath.h>
 #include "AliSymMatrix.h"
+#include "AliLog.h"
 //
 
 using namespace std;
@@ -28,7 +42,7 @@ AliSymMatrix::AliSymMatrix(Int_t size)
 {
   //
   fNrows = 0;
-  fNrowIndex = fNcols = size;
+  fNrowIndex = fNcols = fRowLwb = size;
   fElems     = new Double_t[fNcols*(fNcols+1)/2];
   fSymmetric = kTRUE;
   Reset();
@@ -42,15 +56,16 @@ AliSymMatrix::AliSymMatrix(const AliSymMatrix &src)
 {
   fNrowIndex = fNcols = src.GetSize();
   fNrows = 0;
+  fRowLwb = src.GetSizeUsed();
   if (fNcols) {
     int nmainel = fNcols*(fNcols+1)/2;
     fElems     = new Double_t[nmainel];
     nmainel = src.fNcols*(src.fNcols+1)/2;
     memcpy(fElems,src.fElems,nmainel*sizeof(Double_t));
-    if (src.fNrows) { // transfer extra rows to main matrix
+    if (src.GetSizeAdded()) { // transfer extra rows to main matrix
       Double_t *pnt = fElems + nmainel;
-      int ncl = src.fNcols + 1;
-      for (int ir=0;ir<src.fNrows;ir++) {
+      int ncl = src.GetSizeBooked() + 1;
+      for (int ir=0;ir<src.GetSizeAdded();ir++) {
        memcpy(pnt,src.fElemsAdd[ir],ncl*sizeof(Double_t));
        pnt += ncl;
        ncl++; 
@@ -76,22 +91,23 @@ AliSymMatrix&  AliSymMatrix::operator=(const AliSymMatrix& src)
   //
   if (this != &src) {
     TObject::operator=(src);
-    if (fNcols!=src.fNcols && fNrows!=src.fNrows) {
+    if (GetSizeBooked()!=src.GetSizeBooked() && GetSizeAdded()!=src.GetSizeAdded()) {
       // recreate the matrix
       if (fElems) delete[] fElems;
-      for (int i=0;i<fNrows;i++) delete[] fElemsAdd[i]; 
+      for (int i=0;i<GetSizeAdded();i++) delete[] fElemsAdd[i]; 
       delete[] fElemsAdd;
       //
       fNrowIndex = src.GetSize(); 
       fNcols = src.GetSize();
       fNrows = 0;
-      fElems     = new Double_t[fNcols*(fNcols+1)/2];
-      int nmainel = src.fNcols*(src.fNcols+1);
+      fRowLwb = src.GetSizeUsed();
+      fElems     = new Double_t[GetSize()*(GetSize()+1)/2];
+      int nmainel = src.GetSizeBooked()*(src.GetSizeBooked()+1);
       memcpy(fElems,src.fElems,nmainel*sizeof(Double_t));
-      if (src.fNrows) { // transfer extra rows to main matrix
+      if (src.GetSizeAdded()) { // transfer extra rows to main matrix
        Double_t *pnt = fElems + nmainel*sizeof(Double_t);
-       int ncl = src.fNcols + 1;
-       for (int ir=0;ir<src.fNrows;ir++) {
+       int ncl = src.GetSizeBooked() + 1;
+       for (int ir=0;ir<src.GetSizeAdded();ir++) {
          ncl += ir; 
          memcpy(pnt,src.fElemsAdd[ir],ncl*sizeof(Double_t));
          pnt += ncl*sizeof(Double_t);
@@ -100,9 +116,9 @@ AliSymMatrix&  AliSymMatrix::operator=(const AliSymMatrix& src)
       //
     }
     else {
-      memcpy(fElems,src.fElems,fNcols*(fNcols+1)/2*sizeof(Double_t));
-      int ncl = fNcols + 1;
-      for (int ir=0;ir<fNrows;ir++) { // dynamic rows
+      memcpy(fElems,src.fElems,GetSizeBooked()*(GetSizeBooked()+1)/2*sizeof(Double_t));
+      int ncl = GetSizeBooked() + 1;
+      for (int ir=0;ir<GetSizeAdded();ir++) { // dynamic rows
        ncl += ir; 
        memcpy(fElemsAdd[ir],src.fElemsAdd[ir],ncl*sizeof(Double_t));
       }
@@ -112,19 +128,29 @@ AliSymMatrix&  AliSymMatrix::operator=(const AliSymMatrix& src)
   return *this;
 }
 
+//___________________________________________________________
+AliSymMatrix& AliSymMatrix::operator+=(const AliSymMatrix& src)
+{
+  //
+  if (GetSizeUsed() != src.GetSizeUsed()) {
+    AliError("Matrix sizes are different");
+    return *this;
+  }
+  for (int i=0;i<GetSizeUsed();i++) for (int j=i;j<GetSizeUsed();j++) (*this)(j,i) += src(j,i);
+  return *this;
+}
+
 //___________________________________________________________
 void AliSymMatrix::Clear(Option_t*)
 {
   if (fElems) {delete[] fElems; fElems = 0;}
   //  
   if (fElemsAdd) {
-    for (int i=0;i<fNrows;i++) delete[] fElemsAdd[i]; 
+    for (int i=0;i<GetSizeAdded();i++) delete[] fElemsAdd[i]; 
     delete[] fElemsAdd;
     fElemsAdd = 0;
   }
-  fNrowIndex = 0;
-  fNcols = 0;
-  fNrows = 0;
+  fNrowIndex = fNcols = fNrows = fRowLwb = 0;
   //
 }
 
@@ -133,18 +159,18 @@ Float_t AliSymMatrix::GetDensity() const
 {
   // get fraction of non-zero elements
   Int_t nel = 0;
-  for (int i=GetSize();i--;) for (int j=i+1;j--;) if (GetEl(i,j)!=0) nel++;
-  return 2.*nel/( (GetSize()+1)*GetSize() );
+  for (int i=GetSizeUsed();i--;) for (int j=i+1;j--;) if (TMath::Abs(GetEl(i,j))>DBL_MIN) nel++;
+  return 2.*nel/( (GetSizeUsed()+1)*GetSizeUsed() );
 }
 
 //___________________________________________________________
 void AliSymMatrix::Print(Option_t* option) const
 {
-  printf("Symmetric Matrix: Size = %d (%d rows added dynamically)\n",GetSize(),fNrows);
+  printf("Symmetric Matrix: Size = %d (%d rows added dynamically), %d used\n",GetSize(),GetSizeAdded(),GetSizeUsed());
   TString opt = option; opt.ToLower();
   if (opt.IsNull()) return;
   opt = "%"; opt += 1+int(TMath::Log10(double(GetSize()))); opt+="d|";
-  for (Int_t i=0;i<fNrowIndex;i++) {
+  for (Int_t i=0;i<GetSizeUsed();i++) {
     printf(opt,i);
     for (Int_t j=0;j<=i;j++) printf("%+.3e|",GetEl(i,j));
     printf("\n");
@@ -156,9 +182,9 @@ void AliSymMatrix::MultiplyByVec(Double_t *vecIn,Double_t *vecOut) const
 {
   // fill vecOut by matrix*vecIn
   // vector should be of the same size as the matrix
-  for (int i=fNrowIndex;i--;) {
+  for (int i=GetSizeUsed();i--;) {
     vecOut[i] = 0.0;
-    for (int j=fNrowIndex;j--;) vecOut[i] += vecIn[j]*GetEl(i,j);
+    for (int j=GetSizeUsed();j--;) vecOut[i] += vecIn[j]*GetEl(i,j);
   }
   //
 }
@@ -174,7 +200,7 @@ AliSymMatrix* AliSymMatrix::DecomposeChol()
   // In opposite to function from the book, the matrix is modified:
   // lower triangle and diagonal are refilled.
   //
-  if (!fgBuffer || fgBuffer->GetSize()!=GetSize()) {
+  if (!fgBuffer || fgBuffer->GetSizeUsed()!=GetSizeUsed()) {
     delete fgBuffer; 
     try {
       fgBuffer = new AliSymMatrix(*this);
@@ -188,9 +214,9 @@ AliSymMatrix* AliSymMatrix::DecomposeChol()
   //
   AliSymMatrix& mchol = *fgBuffer;
   //
-  for (int i=0;i<fNrowIndex;i++) {
+  for (int i=0;i<GetSizeUsed();i++) {
     Double_t *rowi = mchol.GetRow(i);
-    for (int j=i;j<fNrowIndex;j++) {
+    for (int j=i;j<GetSizeUsed();j++) {
       Double_t *rowj = mchol.GetRow(j);
       double sum = rowj[i];
       for (int k=i-1;k>=0;k--) if (rowi[k]&&rowj[k]) sum -= rowi[k]*rowj[k];
@@ -233,9 +259,9 @@ void AliSymMatrix::InvertChol(AliSymMatrix* pmchol)
   AliSymMatrix& mchol = *pmchol;
   //
   // Invert decomposed triangular L matrix (Lower triangle is filled)
-  for (int i=0;i<fNrowIndex;i++) { 
+  for (int i=0;i<GetSizeUsed();i++) { 
     mchol(i,i) =  1.0/mchol(i,i);
-    for (int j=i+1;j<fNrowIndex;j++) { 
+    for (int j=i+1;j<GetSizeUsed();j++) { 
       Double_t *rowj = mchol.GetRow(j);
       sum = 0.0; 
       for (int k=i;k<j;k++) if (rowj[k]) { 
@@ -246,10 +272,10 @@ void AliSymMatrix::InvertChol(AliSymMatrix* pmchol)
   }
   //
   // take product of the inverted Choleski L matrix with its transposed
-  for (int i=fNrowIndex;i--;) {
+  for (int i=GetSizeUsed();i--;) {
     for (int j=i+1;j--;) {
       sum = 0;
-      for (int k=i;k<fNrowIndex;k++) {
+      for (int k=i;k<GetSizeUsed();k++) {
        double &mik = mchol(i,k); 
        if (mik) {
          double &mjk = mchol(j,k);
@@ -284,14 +310,14 @@ Bool_t AliSymMatrix::SolveChol(Double_t *b, Bool_t invert)
   }
   AliSymMatrix& mchol = *pmchol;
   //
-  for (i=0;i<fNrowIndex;i++) {
+  for (i=0;i<GetSizeUsed();i++) {
     Double_t *rowi = mchol.GetRow(i);
     for (sum=b[i],k=i-1;k>=0;k--) if (rowi[k]&&b[k]) sum -= rowi[k]*b[k];
     b[i]=sum/rowi[i];
   }
   //
-  for (i=fNrowIndex-1;i>=0;i--) {
-    for (sum=b[i],k=i+1;k<fNrowIndex;k++) if (b[k]) {
+  for (i=GetSizeUsed()-1;i>=0;i--) {
+    for (sum=b[i],k=i+1;k<GetSizeUsed();k++) if (b[k]) {
       double &mki=mchol(k,i); if (mki) sum -= mki*b[k];
     }
     b[i]=sum/mchol(i,i);
@@ -312,7 +338,7 @@ Bool_t AliSymMatrix::SolveChol(TVectorD &b, Bool_t invert)
 //___________________________________________________________
 Bool_t AliSymMatrix::SolveChol(Double_t *brhs, Double_t *bsol,Bool_t invert) 
 {
-  memcpy(bsol,brhs,GetSize()*sizeof(Double_t));
+  memcpy(bsol,brhs,GetSizeUsed()*sizeof(Double_t));
   return SolveChol(bsol,invert);  
 }
 
@@ -335,6 +361,7 @@ void AliSymMatrix::AddRows(int nrows)
     memset(pnew[fNrows],0,ncl*sizeof(Double_t));
     fNrows++;
     fNrowIndex++;
+    fRowLwb++;
   }
   delete[] fElemsAdd;
   fElemsAdd = pnew;
@@ -349,11 +376,11 @@ void AliSymMatrix::Reset()
     delete[] fElems;
     for (int i=0;i<fNrows;i++) delete[] fElemsAdd[i]; 
     delete[] fElemsAdd; fElemsAdd = 0;
-    fNcols = fNrowIndex;
-    fElems = new Double_t[fNcols*(fNcols+1)/2];
+    fNcols = fRowLwb = fNrowIndex;
+    fElems = new Double_t[GetSize()*(GetSize()+1)/2];
     fNrows = 0;
   }
-  if (fElems) memset(fElems,0,fNcols*(fNcols+1)/2*sizeof(Double_t));
+  if (fElems) memset(fElems,0,GetSize()*(GetSize()+1)/2*sizeof(Double_t));
   //
 }
 
@@ -392,11 +419,11 @@ void AliSymMatrix::AddToRow(Int_t r, Double_t *valc,Int_t *indc,Int_t n)
 //___________________________________________________________
 Double_t* AliSymMatrix::GetRow(Int_t r)
 {
-  if (r>=fNrowIndex) {
-    int nn = fNrowIndex;
-    AddRows(r-fNrowIndex+1); 
+  if (r>=GetSize()) {
+    int nn = GetSize();
+    AddRows(r-GetSize()+1); 
     printf("create %d of %d\n",r, nn);
-    return &((fElemsAdd[r-fNcols])[0]);
+    return &((fElemsAdd[r-GetSizeBooked()])[0]);
   }
   else return &fElems[GetIndex(r,0)];
 }
@@ -413,8 +440,8 @@ int AliSymMatrix::SolveSpmInv(double *vecB, Bool_t stabilize)
   Int_t nRank = 0;
   int iPivot;
   double vPivot = 0.;
-  double eps = 0.00000000000001;
-  int nGlo = GetSize();
+  double eps = 1e-14;
+  int nGlo = GetSizeUsed();
   bool   *bUnUsed = new bool[nGlo];
   double *rowMax,*colMax=0;
   rowMax  = new double[nGlo];
@@ -424,7 +451,7 @@ int AliSymMatrix::SolveSpmInv(double *vecB, Bool_t stabilize)
     for (Int_t i=nGlo; i--;) rowMax[i] = colMax[i] = 0.0;
     for (Int_t i=nGlo; i--;) for (Int_t j=i+1;j--;) { 
        double vl = TMath::Abs(Query(i,j));
-       if (vl==0) continue;
+       if (vl<DBL_MIN) continue;
        if (vl > rowMax[i]) rowMax[i] = vl; // Max elemt of row i
        if (vl > colMax[j]) colMax[j] = vl; // Max elemt of column j
        if (i==j) continue;
@@ -433,15 +460,15 @@ int AliSymMatrix::SolveSpmInv(double *vecB, Bool_t stabilize)
       }
     //
     for (Int_t i=nGlo; i--;) {
-      if (0.0 != rowMax[i]) rowMax[i] = 1./rowMax[i]; // Max elemt of row i
-      if (0.0 != colMax[i]) colMax[i] = 1./colMax[i]; // Max elemt of column i
+      if (TMath::Abs(rowMax[i])>DBL_MIN) rowMax[i] = 1./rowMax[i]; // Max elemt of row i
+      if (TMath::Abs(colMax[i])>DBL_MIN) colMax[i] = 1./colMax[i]; // Max elemt of column i
     }
     //
   }
   //
   for (Int_t i=nGlo; i--;) bUnUsed[i] = true;
   //  
-  if (!fgBuffer || fgBuffer->GetSize()!=GetSize()) {
+  if (!fgBuffer || fgBuffer->GetSizeUsed()!=GetSizeUsed()) {
     delete fgBuffer; 
     try {
       fgBuffer = new AliSymMatrix(*this);
@@ -456,11 +483,11 @@ int AliSymMatrix::SolveSpmInv(double *vecB, Bool_t stabilize)
   if (stabilize) for (int i=0;i<nGlo; i++) { // Small loop for matrix equilibration (gives a better conditioning) 
       for (int j=0;j<=i; j++) {
        double vl = Query(i,j);
-       if (vl!=0) SetEl(i,j, TMath::Sqrt(rowMax[i])*vl*TMath::Sqrt(colMax[j]) ); // Equilibrate the V matrix
+       if (TMath::Abs(vl)>DBL_MIN) SetEl(i,j, TMath::Sqrt(rowMax[i])*vl*TMath::Sqrt(colMax[j]) ); // Equilibrate the V matrix
       }
       for (int j=i+1;j<nGlo;j++) {
        double vl = Query(j,i);
-       if (vl!=0) fgBuffer->SetEl(j,i,TMath::Sqrt(rowMax[i])*vl*TMath::Sqrt(colMax[j]) ); // Equilibrate the V matrix
+       if (TMath::Abs(vl)>DBL_MIN) fgBuffer->SetEl(j,i,TMath::Sqrt(rowMax[i])*vl*TMath::Sqrt(colMax[j]) ); // Equilibrate the V matrix
       }
     }
   //