Index: trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc
===================================================================
--- trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 7224)
+++ trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 7358)
@@ -377,7 +377,11 @@
     for (UInt_t idx=0; idx<npixevt; idx++)
     {
+        // The default for pixels is "used" set by
+        // MParContainer::Reset before processing
         if (data[idx]>fCleanLvl1)
             continue;
 
+        // Setting a pixel to unused if it is unmapped would overwrite
+        // the unmapped-status. Therefor this pixels are excluded.
         MSignalPix &pix = (*fEvt)[idx];
         if (!pix.IsPixelUnmapped())
@@ -401,4 +405,5 @@
     for (UInt_t idx=0; idx<npixevt; idx++)
     {
+        // Exclude all unused (this includes all unmapped) pixels
         MSignalPix &pix = (*fEvt)[idx];
         if (!pix.IsPixelUsed())
@@ -416,6 +421,7 @@
             const Int_t idx2 = gpix.GetNeighbor(j);
 
-	    // when you find an used neighbor, break the loop
-            if (fEvt->IsPixelUsed(idx2))
+            // when you find an used neighbor (this excludes unused
+            // and unmapped pixels) break the loop
+            if ((*fEvt)[idx2].IsPixelUsed())
             {
                 hasNeighbor = kTRUE;
@@ -424,18 +430,26 @@
         }
 
-        if (hasNeighbor == kFALSE)
-        {
-            if (!fKeepSinglePixels)
-                pix.SetPixelUnused();
-            size += pix.GetNumPhotons();
-            n++;
-        }
-    }
-
+        // If the pixel has at least one core-neighbor
+        // go on with the next pixel
+        if (hasNeighbor)
+            continue;
+
+        // If the pixel has no neighbors and the single pixels
+        // should not be kept turn the used- into an unused-status
+        if (!fKeepSinglePixels)
+            pix.SetPixelUnused();
+
+        // count size and number of single core-pixels
+        size += pix.GetNumPhotons();
+        n++;
+    }
+
+    // Now turn the used-status into the core-status
+    // (FIXME: A more intelligent handling of used/core in clean step1/2
+    //         would make this loop obsolete!)
     for (UInt_t idx=0; idx<npixevt; idx++)
     {
         MSignalPix &pix = (*fEvt)[idx];
-	if (pix.IsPixelUsed())
-            pix.SetPixelCore();
+        pix.SetPixelCore(pix.IsPixelUsed());
     }
 
@@ -458,5 +472,7 @@
         const Int_t idx2 = gpix.GetNeighbor(j);
 
-	if (!fEvt->IsPixelCore(idx2))
+        // Check if the neighbor pixel is a core pixel. (Rem: Unampped
+        // pixels are never assigned the core-pixel status)
+	if (!(*fEvt)[idx2].IsPixelCore())
             continue;
 
@@ -475,5 +491,5 @@
 //   If a value<2 for fCleanRings is used, no CleanStep4 is done.
 //
-void MImgCleanStd::CleanStep4(UShort_t r, Int_t idx/*MSignalPix &pix*/)
+void MImgCleanStd::CleanStep4(UShort_t r, Int_t idx)
 {
     MSignalPix &pix = (*fEvt)[idx];
@@ -490,5 +506,5 @@
     // and tell to which ring it belongs to.
     //
-    MGeomPix  &gpix  = (*fCam)[idx];
+    MGeomPix  &gpix = (*fCam)[idx];
 
     const Int_t nnmax = gpix.GetNumNeighbors();
@@ -498,6 +514,6 @@
         const Int_t idx2 = gpix.GetNeighbor(j);
 
-        MSignalPix *npix = fEvt->GetPixById(idx2);
-        if (!npix || !npix->IsPixelUsed() || npix->GetRing()>r-1 ) 
+        const MSignalPix &npix = (*fEvt)[idx2];
+        if (!npix.IsPixelUsed() || npix.GetRing()>r-1 )
             continue;
 
