]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliMillePede2.cxx
EMD DA updated
[u/mrichter/AliRoot.git] / STEER / AliMillePede2.cxx
index 62a3f94f38f38221d3c64985ff772df20af77c81..37f8bf8d182a24a6e9f1797f55107eea216a8c34 100644 (file)
@@ -12,6 +12,7 @@
 #include <TStopwatch.h>
 #include <TFile.h>
 #include <TMath.h>
+#include <TVectorD.h>
 #include "AliMatrixSq.h"
 #include "AliSymMatrix.h"
 #include "AliRectMatrix.h"
@@ -329,12 +330,12 @@ void AliMillePede2::SetLocalEquation(double *dergb, double *derlc, double lMeas,
   fRecord->AddResidual(lMeas);
   //
   // Retrieve local param interesting indices
-  for (int i=0;i<fNLocPar;i++) if (derlc[i]!=0.0) {fRecord->AddIndexValue(i,derlc[i]); derlc[i] = 0.0;}
+  for (int i=0;i<fNLocPar;i++) if (!IsZero(derlc[i])) {fRecord->AddIndexValue(i,derlc[i]); derlc[i] = 0.0;}
   //
   fRecord->AddWeight( 1.0/lSigma/lSigma );
   //
   // Idem for global parameters
-  for (int i=0;i<fNGloPar;i++) if (dergb[i]!=0.0) {
+  for (int i=0;i<fNGloPar;i++) if (!IsZero(dergb[i])) {
     fRecord->AddIndexValue(i,dergb[i]); dergb[i] = 0.0;
     fRecord->MarkGroup(fParamGrID[i]);
   }
@@ -357,12 +358,12 @@ void AliMillePede2::SetLocalEquation(int *indgb, double *dergb, int ngb, int *in
   fRecord->AddResidual(lMeas);
   //
   // Retrieve local param interesting indices
-  for (int i=0;i<nlc;i++) if (derlc[i]!=0.0) {fRecord->AddIndexValue(indlc[i],derlc[i]); derlc[i]=0.; indlc[i]=0;}
+  for (int i=0;i<nlc;i++) if (!IsZero(derlc[i])) {fRecord->AddIndexValue(indlc[i],derlc[i]); derlc[i]=0.; indlc[i]=0;}
   //
   fRecord->AddWeight( 1./lSigma/lSigma );
   //
   // Idem for global parameters
-  for (int i=0;i<ngb;i++) if (dergb[i]!=0.0) {fRecord->AddIndexValue(indgb[i],dergb[i]); dergb[i]=0.; indgb[i]=0;} 
+  for (int i=0;i<ngb;i++) if (!IsZero(dergb[i])) {fRecord->AddIndexValue(indgb[i],dergb[i]); dergb[i]=0.; indgb[i]=0;} 
   //
 }
 
@@ -376,9 +377,9 @@ void AliMillePede2::SetGlobalConstraint(double *dergb, double val, double sigma)
   fRecord->Reset();
   fRecord->AddResidual(val);
   fRecord->AddWeight(sigma);
-  for (int i=0; i<fNGloPar; i++) if (dergb[i]!=0)  fRecord->AddIndexValue(i,dergb[i]);
+  for (int i=0; i<fNGloPar; i++) if (!IsZero(dergb[i]))  fRecord->AddIndexValue(i,dergb[i]);
   fNGloConstraints++;
-  if (sigma==0) fNLagrangeConstraints++;
+  if (IsZero(sigma)) fNLagrangeConstraints++;
   //  printf("NewConstraint:\n"); fRecord->Print(); //RRR
   SaveRecordConstraint();
   //
@@ -392,9 +393,9 @@ void AliMillePede2::SetGlobalConstraint(const int *indgb, double *dergb, int ngb
   fRecord->Reset();
   fRecord->AddResidual(val);
   fRecord->AddWeight(sigma);   // dummy
-  for (int i=0; i<ngb; i++) if (dergb[i]!=0)  fRecord->AddIndexValue(indgb[i],dergb[i]);
+  for (int i=0; i<ngb; i++) if (!IsZero(dergb[i]))  fRecord->AddIndexValue(indgb[i],dergb[i]);
   fNGloConstraints++;
-  if (sigma==0) fNLagrangeConstraints++;
+  if (IsZero(sigma)) fNLagrangeConstraints++;
   SaveRecordConstraint();
   //
 }
@@ -449,6 +450,8 @@ Int_t AliMillePede2::LocalFit(double *localParams)
   }
   double vl;
   //
+  Int_t maxLocUsed = 0;
+  //
   for (int ip=nPoints;ip--;) {  // Transfer the measurement records to matrices
     double  resid  = fRecord->GetValue( refLoc[ip]-1 );
     double  weight = fRecord->GetValue( refGlo[ip]-1 );    
@@ -467,11 +470,14 @@ Int_t AliMillePede2::LocalFit(double *localParams)
     // Symmetric matrix, don't bother j>i coeffs
     for (int i=nrefLoc[ip];i--;) {         // Fill local matrix and vector
       fVecBLoc[ indLoc[i] ] += weight*resid*derLoc[i];
+      if (indLoc[i]>maxLocUsed) maxLocUsed = indLoc[i];  
       for (int j=i+1;j--;) matCLoc(indLoc[i] ,indLoc[j]) += weight*derLoc[i]*derLoc[j];
-    }   
+    }
     //
   } // end of the transfer of the measurement record to matrices
   //
+  matCLoc.SetSizeUsed(++maxLocUsed);   // data with B=0 may use less than declared nLocals 
+  //
   // first try to solve by faster Cholesky decomposition, then by Gaussian elimination
   if (!matCLoc.SolveChol(fVecBLoc,kTRUE)) {
     AliInfo("Failed to solve locals by Cholesky, traying Gaussian Elimination");
@@ -484,7 +490,7 @@ Int_t AliMillePede2::LocalFit(double *localParams)
   //
   // If requested, store the track params and errors
   //  printf("locfit: "); for (int i=0;i<fNLocPar;i++) printf("%+e |",fVecBLoc[i]); printf("\n");
-  if (localParams) for (int i=fNLocPar; i--;) {
+  if (localParams) for (int i=maxLocUsed; i--;) {
       localParams[2*i]   = fVecBLoc[i];
       localParams[2*i+1] = TMath::Sqrt(TMath::Abs(matCLoc.QueryDiag(i)));
     }
@@ -515,6 +521,7 @@ Int_t AliMillePede2::LocalFit(double *localParams)
     if ( (absres >= fResCutInit && fIter ==1 ) ||
         (absres >= fResCut     && fIter > 1)) {
       if (fLocFitAdd) fNLocFitsRejected++;      
+      //      printf("reject res %5ld %+e\n",fCurrRecDataID,resid);
       return 0;
     }
     // 
@@ -522,11 +529,12 @@ Int_t AliMillePede2::LocalFit(double *localParams)
     nEq++;                        // number of equations                       
   } // end of Calculate residuals
   //
-  int nDoF = nEq-fNLocPar;
+  int nDoF = nEq-maxLocUsed;
   lChi2 = (nDoF>0) ? lChi2/nDoF : 0;  // Chi^2/dof  
   //
   if (fNStdDev != 0 && nDoF>0 && lChi2 > Chi2DoFLim(fNStdDev,nDoF)*fChi2CutFactor) { // check final chi2
     if (fLocFitAdd) fNLocFitsRejected++;      
+    //    printf("reject chi2 %5ld: %+e\n",fCurrRecDataID, lChi2);
     return 0;
   }
   //
@@ -570,7 +578,7 @@ Int_t AliMillePede2::LocalFit(double *localParams)
       for (int jg=ig+1;jg--;) {        // matCGlo is symmetric by construction  
        int jIDg = indGlo[jg];
        if ( fSigmaPar[jIDg] <= 0.) continue;                                    // fixed parameter RRRCheck
-       if ( (vl = weight*derGlo[ig]*derGlo[jg])!=0 ) {
+       if ( !IsZero(vl = weight*derGlo[ig]*derGlo[jg]) ) {
          fFillIndex[nfill]   = jIDg;
          fFillValue[nfill++] = fLocFitAdd ? vl:-vl;
        }
@@ -581,7 +589,7 @@ Int_t AliMillePede2::LocalFit(double *localParams)
       int iCIDg = fGlo2CGlo[iIDg];  // compressed Index of index          
       if (iCIDg == -1) {
        Double_t *rowGL = matCGloLoc(nGloInFit);
-       for (int k=fNLocPar;k--;) rowGL[k] = 0.0;  // reset the row
+       for (int k=maxLocUsed;k--;) rowGL[k] = 0.0;  // reset the row
        iCIDg = fGlo2CGlo[iIDg] = nGloInFit;
        fCGlo2Glo[nGloInFit++] = iIDg;
       }
@@ -603,8 +611,8 @@ Int_t AliMillePede2::LocalFit(double *localParams)
     //
     vl = 0;
     Double_t *rowGLIDg =  matCGloLoc(iCIDg);
-    for (int kl=0;kl<fNLocPar;kl++) if (rowGLIDg[kl]) vl += rowGLIDg[kl]*fVecBLoc[kl];
-    if  (vl!=0) fVecBGlo[iIDg] -= fLocFitAdd ? vl : -vl;
+    for (int kl=0;kl<maxLocUsed;kl++) if (rowGLIDg[kl]) vl += rowGLIDg[kl]*fVecBLoc[kl];
+    if  (!IsZero(vl)) fVecBGlo[iIDg] -= fLocFitAdd ? vl : -vl;
     //
     int nfill = 0;
     for (int jCIDg=0;jCIDg<=iCIDg; jCIDg++) {  
@@ -612,17 +620,17 @@ Int_t AliMillePede2::LocalFit(double *localParams)
       //
       vl = 0;
       Double_t *rowGLJDg =  matCGloLoc(jCIDg);
-      for (int kl=0;kl<fNLocPar;kl++) {
+      for (int kl=0;kl<maxLocUsed;kl++) {
        // diag terms
-       if ( (vll=rowGLIDg[kl]*rowGLJDg[kl])!=0 ) vl += matCLoc.QueryDiag(kl)*vll;
+       if ( (!IsZero(vll=rowGLIDg[kl]*rowGLJDg[kl])) ) vl += matCLoc.QueryDiag(kl)*vll;
        //
        // off-diag terms
        for (int ll=0;ll<kl;ll++) {
-         if ( (vll=rowGLIDg[kl]*rowGLJDg[ll])!=0 ) vl += matCLoc(kl,ll)*vll;
-         if ( (vll=rowGLIDg[ll]*rowGLJDg[kl])!=0 ) vl += matCLoc(kl,ll)*vll;
+         if ( !IsZero(vll=rowGLIDg[kl]*rowGLJDg[ll]) ) vl += matCLoc(kl,ll)*vll;
+         if ( !IsZero(vll=rowGLIDg[ll]*rowGLJDg[kl]) ) vl += matCLoc(kl,ll)*vll;
        }
       }
-      if (vl!=0) {
+      if (!IsZero(vl)) {
        fFillIndex[nfill]   = jIDg;
        fFillValue[nfill++] = fLocFitAdd ? -vl : vl;
       }
@@ -656,7 +664,7 @@ Int_t AliMillePede2::GlobalFit(Double_t *par, Double_t *error, Double_t *pull)
     res = GlobalFitIteration();
     if (!res) break;
     //
-    if (fChi2CutFactor != fChi2CutRef) {    
+    if (!IsZero(fChi2CutFactor-fChi2CutRef)) {    
       fChi2CutFactor = TMath::Sqrt(fChi2CutFactor);
       if (fChi2CutFactor < 1.2*fChi2CutRef) {
        fChi2CutFactor = fChi2CutRef;
@@ -711,7 +719,7 @@ Int_t AliMillePede2::GlobalFitIteration()
   fNLagrangeConstraints = 0;
   for (int i=0; i<fNGloConstraints; i++) {
     ReadRecordConstraint(i);
-    if ( fRecord->GetValue(1)==0 ) fNLagrangeConstraints++; // exact constraint (no error) -> Lagrange multiplier 
+    if ( IsZero(fRecord->GetValue(1)) ) fNLagrangeConstraints++; // exact constraint (no error) -> Lagrange multiplier 
   }
   //
   // if needed, readjust the size of the global vector (for matrices this is done automatically)
@@ -736,7 +744,7 @@ Int_t AliMillePede2::GlobalFitIteration()
   for (Long_t i=0;i<ndr;i++) {
     ReadRecordData(i);
     LocalFit();
-    if ( (i%int(0.1*ndr)) == 0) printf("%.1f%% of local fits done\n", double(100.*i)/ndr);
+    if ( (i%int(0.2*ndr)) == 0) printf("%.1f%% of local fits done\n", double(100.*i)/ndr);
   }
   swt.Stop();
   printf("%ld local fits done: ", ndr);
@@ -792,6 +800,7 @@ Int_t AliMillePede2::GlobalFitIteration()
   // add large number to diagonal of fixed params  
   //
   for (int i=fNGloPar;i--;) { // // Reset row and column of fixed params and add 1/sig^2 to free ones
+    //    printf("#%3d : Nproc : %5d   grp: %d\n",i,fProcPnt[i],fParamGrID[i]);
     if (fProcPnt[i]<1) {
       fNGloFix++; 
       fVecBGlo[i] = 0.;
@@ -846,7 +855,7 @@ Int_t AliMillePede2::GlobalFitIteration()
        for (int ic=0;ic<=ir;ic++) { // matrix is symmetric
          int jID = indV[ic];
          double vl = der[ir]*der[ic]*sig2i;
-         if (vl!=0) matCGlo(iID,jID) += vl;
+         if (!IsZero(vl)) matCGlo(iID,jID) += vl;
        }
        fVecBGlo[iID] += val*der[ir]*sig2i;
       }