Index: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc
===================================================================
--- trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 7353)
+++ trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 7360)
@@ -67,4 +67,5 @@
 
 #include <TEnv.h>
+#include <TRandom.h>
 #include <TObjString.h>
 
@@ -408,51 +409,75 @@
 void MBadPixelsTreat::InterpolateTimes() const
 {
-    Int_t n = fEvt->GetNumPixels();
-
+    const Double_t tdiffshoweredge = 0.5;
+
+    const Int_t n = fEvt->GetNumPixels();
     for (int i=0; i<n; i++)
     {
-        //
-        // Check whether pixel with idx i is blind
-        //
+        // Check whether pixel with idx i is bad
         if (!IsPixelBad(i))
             continue;
 
-        //
-        // Get the corresponding geometry and pedestal
-        //
+        // Geometry of bad pixel
         const MGeomPix &gpix = (*fGeomCam)[i];
 
-        const UInt_t n2 = gpix.GetNumNeighbors();
-
-        Double_t time[n2];
-
-        Int_t n0 = 0;
-        for (unsigned int j=0; j<n2; j++)
-        {
-            const Double_t t = (*fEvt)[j].GetArrivalTime();
-            if (t>=0)
-                time[n0++] = t;
-        }
-
-        Int_t p0=-1;
-        Int_t p1=-1;
-
-        Double_t min=FLT_MAX;
-        for (int j=0; j<n0; j++)
-            for (int k=0; k<j; k++)
+        // Number of neighbor pixels
+        const Int_t n2 = gpix.GetNumNeighbors();
+
+        // Copy the arrival time of all neighboring bad pixels
+        // to a new array for simplicity
+        Double_t time[6];
+        Int_t cnt = 0;
+        for (Int_t j=0; j<n2; j++)
+        {
+            const Int_t idx = gpix.GetNeighbor(j);
+            if (!IsPixelBad(idx))
+                time[cnt++] = (*fEvt)[idx].GetArrivalTime();
+        }
+
+        // if there are too few neighbours, don't interpolate the pixel
+        //if ((cnt < 3 && n2 > 3) || (cnt < 2 && n2 == 3))
+        if (cnt<fNumMinNeighbors)
+        {
+            (*fEvt)[i].SetPixelUnmapped();
+            continue;
+        }
+
+        Double_t min =  FLT_MAX; // Find minimum arrival time
+        Double_t max = -FLT_MAX; // Find maximum arrival time
+
+        Double_t sum2 = 0; // Sum of arrival times of the pixels
+        Int_t    cnt2 = 0; // Number of pixels summed in sum2
+
+        for (Int_t j=0; j<cnt; j++)
+        {
+            const Double_t tm1 = time[j];           // time of one neighbor pixel
+            const Double_t tm2 = time[(j+1)%cnt];   // time of its neighbor pixel
+
+            // Calculate mean arrival time of pixel probably inside the shower
+            if (TMath::Abs(tm1 - tm2)<tdiffshoweredge)
             {
-                const Double_t diff = TMath::Abs(time[j] - time[k]);
-
-                if (diff>=min && diff<250)
-                    continue;
-
-                p0  = j;
-                p1  = k;
-                min = diff;
+                sum2 += tm1+tm2;
+                cnt2++;
             }
 
-        // FIXME: Request a minimum number of neighbors to take action?
-        if (p0>=0 && p1>=0 && TMath::Abs(time[p0] - time[p1])<250)
-            (*fEvt)[i].SetArrivalTime((time[p0]+time[p1])/2);
+            // Find minimum arrival time
+            if (tm1<min)
+                min = tm1;
+
+            // Find maximum arrival time
+            if (tm1>max)
+                max = tm1;
+        }
+
+        // If less than two nbeighbors belong to a shower the pixel doesn't
+        // belong to the shower, too. Set the arrival time to a uniform
+        // random value, otherwise use the mean value of the pixels belonging
+        // to the shower.
+        if (cnt2<=2)
+            sum2 = gRandom->Uniform(max-min)+min; // FIXME? Set Seed value?
+        else
+            sum2 /= cnt2*2;
+
+        (*fEvt)[i].SetArrivalTime(sum2);
     }
 }
