Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 2444)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 2445)
@@ -1,3 +1,20 @@
                                                  -*-*- END OF LINE -*-*-
+
+  2003/10/30: Thomas Bretz
+
+   * manalysis/MCerPhotEvt.[h,cc]:
+     - fixed a bug in the new Lut-stuff. Sometimes pixels where
+       considered existing, because there where no mark for 
+       empty entries in the Lut.
+     - all loops ignored pixel with index 0 and used it as reference
+       pixel - this is wrong, because pixel 0 must not exist at all.
+       Now all loops are starting with pixel 0.
+
+   * mimage/MImgCleanStd.[h,cc]:
+     - renamed various local variables from 'id' to 'idx'
+     - in clean step 4 we assumed that a pixel with idx2 is existing
+       - this is a dangerous assumption. It is checked now.
+
+
 
   2003/10/29: Thomas Bretz
Index: /trunk/MagicSoft/Mars/manalysis/MCerPhotEvt.cc
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MCerPhotEvt.cc	(revision 2444)
+++ /trunk/MagicSoft/Mars/manalysis/MCerPhotEvt.cc	(revision 2445)
@@ -124,6 +124,5 @@
 Bool_t MCerPhotEvt::IsPixelExisting(Int_t id) const
 {
-    const MCerPhotPix *pix = GetPixById(id);
-    return pix ? kTRUE : kFALSE;
+    return GetPixById(id) ? kTRUE : kFALSE;
 } 
 
@@ -163,5 +162,5 @@
     Float_t minval = FLT_MAX;
 
-    for (UInt_t i=1; i<fNumPixels; i++)
+    for (UInt_t i=0; i<fNumPixels; i++)
     {
         const MCerPhotPix &pix = (*this)[i];
@@ -170,5 +169,5 @@
 
         const UInt_t id = pix.GetPixId();
-        if (id>=n)
+        if (id<0 || id>=n)
             continue;
 
@@ -200,5 +199,5 @@
     Float_t maxval = -FLT_MAX;
 
-    for (UInt_t i=1; i<fNumPixels; i++)
+    for (UInt_t i=0; i<fNumPixels; i++)
     {
         const MCerPhotPix &pix = (*this)[i];
@@ -207,5 +206,5 @@
 
         const UInt_t id = pix.GetPixId();
-        if (id>=n)
+        if (id<0 || id>=n)
             continue;
 
@@ -229,7 +228,7 @@
         return -5.;
 
-    Float_t minval = (*this)[0].GetNumPhotons()/(*this)[0].GetErrorPhot();
-
-    for (UInt_t i=1; i<fNumPixels; i++)
+    Float_t minval = FLT_MAX;
+
+    for (UInt_t i=0; i<fNumPixels; i++)
     {
         const MCerPhotPix &pix = (*this)[i];
@@ -259,5 +258,5 @@
     Float_t maxval = -FLT_MAX;
 
-    for (UInt_t i=1; i<fNumPixels; i++)
+    for (UInt_t i=0; i<fNumPixels; i++)
     {
         const MCerPhotPix &pix = (*this)[i];
@@ -289,5 +288,5 @@
     Float_t minval = FLT_MAX;
 
-    for (UInt_t i=1; i<fNumPixels; i++)
+    for (UInt_t i=0; i<fNumPixels; i++)
     {
         const MCerPhotPix &pix = (*this)[i];
@@ -319,5 +318,5 @@
     Float_t maxval = -FLT_MAX;
 
-    for (UInt_t i=1; i<fNumPixels; i++)
+    for (UInt_t i=0; i<fNumPixels; i++)
     {
         const MCerPhotPix &pix = (*this)[i];
@@ -356,8 +355,16 @@
 // try to search in the array.
 //
-MCerPhotPix *MCerPhotEvt::GetPixById(int idx) const
-{
+MCerPhotPix *MCerPhotEvt::GetPixById(Int_t idx) const
+{
+    if (idx<0)
+        return 0;
+
     if (fLut.GetSize()>0)
-        return idx>=0 && idx<fLut.GetSize() ? (MCerPhotPix*)(fPixels->UncheckedAt(fLut[idx])) : 0;
+    {
+        if (idx>=fLut.GetSize())
+            return 0;
+
+        return fLut[idx]<0 ? 0 : (MCerPhotPix*)(fPixels->UncheckedAt(fLut[idx]));
+    }
 
     TIter Next(fPixels);
Index: /trunk/MagicSoft/Mars/manalysis/MCerPhotEvt.h
===================================================================
--- /trunk/MagicSoft/Mars/manalysis/MCerPhotEvt.h	(revision 2444)
+++ /trunk/MagicSoft/Mars/manalysis/MCerPhotEvt.h	(revision 2445)
@@ -39,5 +39,10 @@
         //
         if (idx>=fLut.GetSize())
+        {
+            const Int_t n = fLut.GetSize();
             fLut.Set(idx+1);
+            for (int i=n; i<idx; i++)
+                fLut[i] = -1;
+        }
 
         fLut[idx] = fNumPixels;
@@ -66,5 +71,5 @@
     void RemoveUnusedPixels();
 
-    MCerPhotPix *GetPixById(int idx) const;// { return idx>=0 && idx<fLut.GetSize() ? (MCerPhotPix*)(fPixels->UncheckedAt(fLut[idx])) : 0; } // Return a pointer to the pixel with the requested id. NULL if it doesn't exist.
+    MCerPhotPix *GetPixById(Int_t idx) const;
 
     void Reset();
Index: /trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc
===================================================================
--- /trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 2444)
+++ /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;
 
