Index: trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc
===================================================================
--- trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 2440)
+++ trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 2445)
@@ -329,4 +329,5 @@
     // set them to 'unused' state if necessary
     // 
+
     for (Int_t i=0; i<entries; i++ )
     {
@@ -373,8 +374,8 @@
         MCerPhotPix &pix = (*fEvt)[i];
 
-        const Int_t id = pix.GetPixId();
+        const Int_t idx = pix.GetPixId();
 
         const Float_t  entry = pix.GetNumPhotons();
-        const Double_t ratio = fCam->GetPixRatio(id);
+        const Double_t ratio = fCam->GetPixRatio(idx);
 
         // COBB: '<=' to skip entry=noise=0
@@ -422,5 +423,10 @@
     // (MCerPhotEvt::IsPixelUsed) all the time.
     //
+    // We allocate the array ourself because the TArrays always do
+    // range check which slows down the access to the array
+    // by 25-50%
+    //
     Byte_t *ispixused = new Byte_t[fCam->GetNumPixels()];
+    memset(ispixused, 0, sizeof(Byte_t)*fCam->GetNumPixels());
 
     for (Int_t i=0; i<entries; i++)
@@ -428,5 +434,5 @@
         const MCerPhotPix &pix = (*fEvt)[i];
         ispixused[pix.GetPixId()] = pix.IsPixelUsed() ? 1 : 0 ;
-    }
+   }
 
     for (Int_t i=0; i<entries; i++)
@@ -436,12 +442,12 @@
 
 	// get pixel id of this entry
-        const Int_t id = pix.GetPixId();
+        const Int_t idx = pix.GetPixId();
 
 	// check if pixel is in use, if not goto next pixel in list
-	if (ispixused[id] == 0)
+	if (ispixused[idx] == 0)
             continue;
  
         // check for 'used' neighbors of this pixel
-        const MGeomPix &gpix  = (*fCam)[id];
+        const MGeomPix &gpix  = (*fCam)[idx];
         const Int_t     nnmax = gpix.GetNumNeighbors();
 
@@ -451,11 +457,11 @@
         for (Int_t j=0; j<nnmax; j++)
         {
-            const Int_t id2 = gpix.GetNeighbor(j);
+            const Int_t idx2 = gpix.GetNeighbor(j);
 
 	    // when you find an used neighbor, break the loop
-            if (ispixused[id2] == 1)
+            if (ispixused[idx2] == 1)
             {
-		hasNeighbor = kTRUE;
-		break;
+                hasNeighbor = kTRUE;
+                break;
             }
         }
@@ -478,4 +484,5 @@
     }
 } 
+
 
 // --------------------------------------------------------------------------
@@ -518,5 +525,5 @@
     // get pixel id of this entry
     //
-    const Int_t id = pix.GetPixId();
+    const Int_t idx = pix.GetPixId();
 
     //
@@ -524,5 +531,5 @@
     //
     const Float_t  entry = pix.GetNumPhotons();
-    const Double_t ratio = fCam->GetPixRatio(id);
+    const Double_t ratio = fCam->GetPixRatio(idx);
 
     return (entry * ratio <= fCleanLvl2 * fInnerNoise);
@@ -531,5 +538,5 @@
 void MImgCleanStd::CleanStep3b(MCerPhotPix &pix)
 {
-    const Int_t id = pix.GetPixId();
+    const Int_t idx = pix.GetPixId();
 
     //
@@ -537,16 +544,16 @@
     // if it is a core pixel set pixel state to: used.
     //
-    MGeomPix   &gpix  = (*fCam)[id];
+    MGeomPix   &gpix  = (*fCam)[idx];
     const Int_t nnmax = gpix.GetNumNeighbors();
 
     for (Int_t j=0; j<nnmax; j++)
     {
-        const Int_t id2 = gpix.GetNeighbor(j);
-
-	if (!fEvt->GetPixById(id2) || !fEvt->IsPixelCore(id2)) 
-	  continue;
+        const Int_t idx2 = gpix.GetNeighbor(j);
+
+	if (!fEvt->GetPixById(idx2) || !fEvt->IsPixelCore(idx2))
+            continue;
 
         pix.SetPixelUsed();
-	   break;
+       	break;
     }
 }
@@ -568,6 +575,6 @@
     // and tell to which ring it belongs to.
     //
-    const Int_t id = pix.GetPixId();
-    MGeomPix  &gpix  = (*fCam)[id];
+    const Int_t idx = pix.GetPixId();
+    MGeomPix  &gpix  = (*fCam)[idx];
 
     const Int_t nnmax = gpix.GetNumNeighbors();
@@ -575,14 +582,9 @@
     for (Int_t j=0; j<nnmax; j++)
     {
-        const Int_t id2 = gpix.GetNeighbor(j);
-
-        MCerPhotPix &npix = *fEvt->GetPixById(id2);
-
-	// FIXME!
-	// Needed check to read CT1 data without having a Segmentation fault
-	if (!fEvt->GetPixById(id2)) 
-	    continue; 
-
-        if (!npix.IsPixelUsed() || npix.GetRing()>r-1 )
+        const Int_t idx2 = gpix.GetNeighbor(j);
+
+        MCerPhotPix *npix = fEvt->GetPixById(idx2);
+
+        if (!npix || !npix->IsPixelUsed() || npix->GetRing()>r-1 )
             continue;
 
