]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - JETAN/AliKMeansClustering.cxx
Compatibility with ROOT trunk
[u/mrichter/AliRoot.git] / JETAN / AliKMeansClustering.cxx
index b161e06b0a6ca220b58f0b750cd929c98a8e88b9..618b6763529451d717246460d67eafcf01ff827a 100755 (executable)
@@ -32,7 +32,7 @@ ClassImp(AliKMeansClustering)
 Double_t AliKMeansClustering::fBeta = 10.;
 
  
-Int_t AliKMeansClustering::SoftKMeans(Int_t k, Int_t n, Double_t* x, Double_t* y, Double_t* mx, Double_t* my , Double_t* rk )
+Int_t AliKMeansClustering::SoftKMeans(Int_t k, Int_t n, const Double_t* x, const Double_t* y, Double_t* mx, Double_t* my , Double_t* rk )
 {
     //
     // The soft K-means algorithm
@@ -221,7 +221,7 @@ Int_t AliKMeansClustering::SoftKMeans2(Int_t k, Int_t n, Double_t* x, Double_t*
       // Sigma
       for (i = 0; i < k; i++) {
        sigma2[i] = 0.;
-       for (j = 1; j < n; j++) {
+       for (j = 0; j < n; j++) {
          sigma2[i] += r[j][i] * d(mx[i], my[i], x[j], y[j]);
        } // Data
        sigma2[i] /= rk[i];
@@ -354,7 +354,7 @@ Int_t AliKMeansClustering::SoftKMeans3(Int_t k, Int_t n, Double_t* x, Double_t*
        sigmax2[i] = 0.;
        sigmay2[i] = 0.;
 
-       for (j = 1; j < n; j++) {
+       for (j = 0; j < n; j++) {
          Double_t dx = TMath::Abs(mx[i]-x[j]);
          if (dx > TMath::Pi()) dx = 2. * TMath::Pi() - dx;
          Double_t dy = TMath::Abs(my[i]-y[j]);
@@ -397,7 +397,7 @@ Double_t AliKMeansClustering::d(Double_t mx, Double_t my, Double_t x, Double_t y
 
 
 
-void AliKMeansClustering::OptimalInit(Int_t k, Int_t n, Double_t* x, Double_t* y, Double_t* mx, Double_t* my)
+void AliKMeansClustering::OptimalInit(Int_t k, Int_t n, const Double_t* x, const Double_t* y, Double_t* mx, Double_t* my)
 {
   //  
   // Optimal initialisation using the k-means++ algorithm
@@ -458,12 +458,12 @@ AliKMeansResult::AliKMeansResult(Int_t k):
 AliKMeansResult::AliKMeansResult(const AliKMeansResult &res):
   TObject(res),
   fK(res.GetK()),
-  fMx(0),
-  fMy(0),
-  fSigma2(0),
-  fRk(0),
-  fTarget(0),
-  fInd(0)
+  fMx(new Double_t[res.GetK()]),
+  fMy(new Double_t[res.GetK()]),
+  fSigma2(new Double_t[res.GetK()]),
+  fRk(new Double_t[res.GetK()]),
+  fTarget(new Double_t[res.GetK()]),
+  fInd(new Int_t[res.GetK()])
 {
   // Copy constructor
   for (Int_t i = 0; i <fK; i++) {
@@ -481,15 +481,30 @@ AliKMeansResult& AliKMeansResult::operator=(const AliKMeansResult& res)
   //
   // Assignment operator
   if (this != &res) {
-    fK = res.GetK();
-    for (Int_t i = 0; i <fK; i++) {
-      fMx[i]     = (res.GetMx())    [i];
-      fMy[i]     = (res.GetMy())    [i];
-      fSigma2[i] = (res.GetSigma2())[i];
-      fRk[i]     = (res.GetRk())    [i];
-      fTarget[i] = (res.GetTarget())[i];
-      fInd[i]    = (res.GetInd())   [i];
+    TObject::operator=(res);
+    if (fK != res.fK) {
+       delete [] fMx;
+       delete [] fMy;
+       delete [] fSigma2;
+       delete [] fRk;
+       delete [] fTarget;
+       delete [] fInd;
+       fK = res.fK;
+       fMx     = new Double_t[fK];
+       fMy     = new Double_t[fK];
+       fSigma2 = new Double_t[fK];
+       fRk     = new Double_t[fK];
+       fTarget = new Double_t[fK];
+       fInd    = new    Int_t[fK];
     }
+    
+    fK = res.fK;
+    memcpy(fMx,     res.fMx,     fK*sizeof(Double_t));
+    memcpy(fMy,     res.fMy,     fK*sizeof(Double_t));
+    memcpy(fSigma2, res.fSigma2, fK*sizeof(Double_t));
+    memcpy(fRk,     res.fRk,     fK*sizeof(Double_t));
+    memcpy(fTarget, res.fTarget, fK*sizeof(Double_t));
+    memcpy(fInd,    res.fInd,    fK*sizeof(Int_t));
   }
   return *this;
 }
@@ -508,17 +523,19 @@ AliKMeansResult::~AliKMeansResult()
 
 void AliKMeansResult::Sort()
 {
-// Sort clusters
-    for (Int_t i = 0; i < fK; i++) {
-       if (fRk[i] > 1.) fRk[i] /= fSigma2[i];
-       else fRk[i] = 0.;
+  // Build target array and sort
+  // Sort clusters
+  for (Int_t i = 0; i < fK; i++) {
+    if (fRk[i] > 2.9) {
+      fTarget[i] = fRk[i] / fSigma2[i];
     }
+    else fTarget[i] = 0.;
+  }
     
-    TMath::Sort(fK, fRk, fInd);
-    
+  TMath::Sort(fK, fTarget, fInd);
 }
 
-void AliKMeansResult::Sort(Int_t n, Double_t* x, Double_t* y)
+void AliKMeansResult::Sort(Int_t n, const Double_t* x, const Double_t* y)
 {
   // Build target array and sort
   for (Int_t i = 0; i < fK; i++)
@@ -526,10 +543,11 @@ void AliKMeansResult::Sort(Int_t n, Double_t* x, Double_t* y)
       Int_t nc = 0;
       for (Int_t j = 0; j < n; j++)
        {
-         if (2. * AliKMeansClustering::d(fMx[i], fMy[i], x[j], y[j])  <  fSigma2[i]) nc++;
+         if (2. * AliKMeansClustering::d(fMx[i], fMy[i], x[j], y[j])  <  2.28 * fSigma2[i]) nc++;
        }
-      if (nc > 1) {
-       fTarget[i] = Double_t(nc) / fSigma2[i];
+
+      if (nc > 2) {
+       fTarget[i] = Double_t(nc) / (2.28 * fSigma2[i]);
       } else {
        fTarget[i] = 0.;
       }
@@ -538,7 +556,7 @@ void AliKMeansResult::Sort(Int_t n, Double_t* x, Double_t* y)
   TMath::Sort(fK, fTarget, fInd);
 }
 
-void AliKMeansResult::CopyResults(AliKMeansResult* res)
+void AliKMeansResult::CopyResults(const AliKMeansResult* res)
 {
   fK = res->GetK();
   for (Int_t i = 0; i <fK; i++) {