Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 7359)
+++ trunk/MagicSoft/Mars/Changelog	(revision 7360)
@@ -18,4 +18,21 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2005/09/22 Thomas Bretz
+
+   * mbadpixels/MBadPixelsTreat.cc:
+     - implemented a new treatment for the time information developed
+       by S.Ruegamer, this should give much better response than the
+       old very primitive algorithm. This is especially important
+       for bad pixels due to bright stars as Ceta-Tauri
+
+   * mgeom/MGeomCam.[h,cc]:
+     - added new member function SeorNeighbors which sorts the neighboring
+       pixels clockwise
+     - call SortNeighbors in InitGeometry so that we always get a geometry
+       with clockwise sorted pixels.
+
+
+
  2005/09/21 Thomas Bretz
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 7359)
+++ trunk/MagicSoft/Mars/NEWS	(revision 7360)
@@ -14,7 +14,10 @@
      automatic processing
 
-   - callisto: Fixed a bug in the treatment of times in the bad pixel
-     treatment. No neighbors have been taken into account, the new
-     arrival time was always calculated by the pixels 0 to 5.
+   - callisto: Implemented a new treatment for the time information 
+     developed by S.Ruegamer, this should give much better response than
+     the old very primitive algorithm. This is especially important
+     for bad pixels due to bright stars as Ceta-Tauri (btw: the old
+     treatment was buggy! As neighbor pixels to interpolate the 
+     arrival times always the pixels 0 to 5 were taken)
 
    - callisto: The status "unmapped" (for pixel which cannot be interpolated)
Index: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc
===================================================================
--- trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 7359)
+++ 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);
     }
 }
Index: trunk/MagicSoft/Mars/mgeom/MGeomCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCam.cc	(revision 7359)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCam.cc	(revision 7360)
@@ -223,4 +223,50 @@
 // --------------------------------------------------------------------------
 //
+// sort neighbours from angle of -180 degree to -180 degree
+//
+// where right side of main pixel contains negative degrees
+// and left side positive degrees
+//
+// angle measured from top (0 degree) to bottom (180 degree)
+//             ^  -     |     +         //
+//             |      _ | _             //
+//             |     / \|/ \            //
+//  30 degree -+-   |   |   |           //
+//             |   / \ / \ / \          //
+//  90 degree -+- |   | X |   |         //
+//             |   \ / \ / \ /          //
+// 150 degree -+-   |   |   |           //
+//             |     \_/|\_/            //
+//             |        |               //
+//             |  -     |     +         //
+//             ------------------>      //
+//                                      //
+void MGeomCam::SortNeighbors()
+{
+    for (unsigned int i=0; i<fNumPixels; i++)
+    {
+        MGeomPix &gpix = (*this)[i];
+
+        Double_t phi[6];
+        Int_t    idx[7] = { 0, 0, 0, 0, 0, 0, -1 };
+
+        const Int_t n2 = gpix.GetNumNeighbors();
+        for (int j=0; j<n2; j++)
+        {
+            idx[j] = gpix.GetNeighbor(j);
+            phi[j] = (*this)[idx[j]].GetAngle(gpix);
+        }
+
+        Int_t sort[6] = { 6, 6, 6, 6, 6, 6 };
+
+        TMath::Sort(n2, phi, sort, kFALSE);
+
+        gpix.SetNeighbors(idx[sort[0]], idx[sort[1]], idx[sort[2]],
+                          idx[sort[3]], idx[sort[4]], idx[sort[5]]);
+    }
+}
+
+// --------------------------------------------------------------------------
+//
 // Returns the distance between the pixels i and j. -1 if an index
 // doesn't exist. The default for j is 0. Assuming that 0 is the index
Index: trunk/MagicSoft/Mars/mgeom/MGeomCam.h
===================================================================
--- trunk/MagicSoft/Mars/mgeom/MGeomCam.h	(revision 7359)
+++ trunk/MagicSoft/Mars/mgeom/MGeomCam.h	(revision 7360)
@@ -39,4 +39,5 @@
     void CalcNumAreas();
     void InitOuterRing();
+    void SortNeighbors();
 
 public:
@@ -56,4 +57,5 @@
         CalcPixRatio();
         InitOuterRing();
+        SortNeighbors();
     }
 
