Index: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc
===================================================================
--- trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 7350)
+++ trunk/MagicSoft/Mars/mbadpixels/MBadPixelsTreat.cc	(revision 7353)
@@ -69,5 +69,5 @@
 #include <TObjString.h>
 
-#include "MArrayD.h" // Used instead of TArrayD because operator[] has no range check
+//#include "MArrayD.h" // Used instead of TArrayD because operator[] has no range check
 
 #include "MLog.h"
@@ -218,9 +218,92 @@
 
     //
-    // Create arrays (FIXME: Check if its possible to create it only once)
+    // Loop over all pixels
     //
-    MArrayD nphot(entries);
-    MArrayD perr(entries);
+    for (UShort_t i=0; i<entries; i++)
+    {
+        //
+        // Check whether pixel with idx i is blind
+        //
+        if (!IsPixelBad(i))
+            continue;
+
+        //
+        // Get the corresponding geometry and pedestal
+        //
+        MSignalPix     &pix  = (*fEvt)[i];
+        const MGeomPix &gpix = (*fGeomCam)[i];
+
+        // Do Not-Use-Central-Pixel
+        const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
+
+        Int_t num = nucp ? 0 : 1;
+
+        Double_t nphot  = nucp ? 0 : pix.GetNumPhotons();
+        Double_t perr   = nucp ? 0 : Pow2(pix.GetErrorPhot());
+
+        //
+	// The values are rescaled to the small pixels area for the right comparison
+        //
+        const Double_t ratio = fGeomCam->GetPixRatio(i);
+
+        nphot *= ratio;
+        perr  *= ratio;
+
+        //
+        // Loop over all its neighbors
+        //
+        const Int_t n = gpix.GetNumNeighbors();
+        for (int j=0; j<n; j++)
+        {
+            const UShort_t nidx = gpix.GetNeighbor(j);
+
+            //
+            // Do not use blind neighbors
+            //
+            if (IsPixelBad(nidx))
+                continue;
+
+            //
+            // Get the geometry for the neighbor
+            //
+            const Double_t nratio = fGeomCam->GetPixRatio(nidx);
+
+            //
+	    //The error is calculated as the quadratic sum of the errors
+	    //
+            const MSignalPix &evtpix = (*fEvt)[nidx];
+
+            nphot += nratio*evtpix.GetNumPhotons();
+            perr  += nratio*Pow2(evtpix.GetErrorPhot());
+
+            num++;
+        }
+
+	// Check if there are enough neighbors to calculate the mean
+        // If not, unmap the pixel. The maximum number of blind neighbors
+        // should be 2
+        if (num<fNumMinNeighbors)
+        {
+            pix.SetPixelUnmapped();
+            continue;
+        }
+
+        //
+        // Now the mean is calculated and the values rescaled back
+        // to the pixel area
+        //
+	nphot /= num*ratio;
+        perr   = TMath::Sqrt(perr/(num*ratio));
  
+        pix.Set(nphot, perr);
+    }
+}
+
+// --------------------------------------------------------------------------
+//
+void MBadPixelsTreat::InterpolatePedestals(MPedPhotCam &pedphot) const
+{
+    const Int_t entries = pedphot.GetSize();
+
     //
     // Loop over all pixels
@@ -237,6 +320,6 @@
         // Get the corresponding geometry and pedestal
         //
-        MSignalPix     &pix  = (*fEvt)[i];
-        const MGeomPix &gpix = (*fGeomCam)[i];
+        const MGeomPix    &gpix = (*fGeomCam)[i];
+        const MPedPhotPix &ppix = pedphot[i];
 
         // Do Not-Use-Central-Pixel
@@ -245,14 +328,14 @@
         Int_t num = nucp ? 0 : 1;
 
-        nphot[i]  = nucp ? 0 : pix.GetNumPhotons();
-        perr[i]   = nucp ? 0 : Pow2(pix.GetErrorPhot());
-
-        //
-	// The values are rescaled to the small pixels area for the right comparison
+        Double_t ped = nucp ? 0 : ppix.GetMean();
+        Double_t rms = nucp ? 0 : Pow2(ppix.GetRms());
+
+        //
+        // The values are rescaled to the small pixels area for the right comparison
         //
         const Double_t ratio = fGeomCam->GetPixRatio(i);
 
-        nphot[i] *= ratio;
-        perr[i]  *= ratio;
+        ped *= ratio;
+        rms *= ratio;
 
         //
@@ -273,97 +356,4 @@
             // Get the geometry for the neighbor
             //
-            const Double_t nratio = fGeomCam->GetPixRatio(nidx);
-
-            //
-	    //The error is calculated as the quadratic sum of the errors
-	    //
-            const MSignalPix &evtpix = (*fEvt)[nidx];
-
-            nphot[i] += nratio*evtpix.GetNumPhotons();
-            perr[i]  += nratio*Pow2(evtpix.GetErrorPhot());
-
-            num++;
-        }
-
-	// Check if there are enough neighbors to calculate the mean
-        // If not, unmap the pixel. The maximum number of blind neighbors
-        // should be 2
-        if (num<fNumMinNeighbors)
-        {
-            pix.SetPixelUnmapped();
-            continue;
-        }
-
-        //
-        // Now the mean is calculated and the values rescaled back
-        // to the pixel area
-        //
-	nphot[i] /= (num*ratio);
-        perr[i]   = TMath::Sqrt(perr[i]/(num*ratio));
- 
-        pix.Set(nphot[i], perr[i]);
-    }
-}
-
-// --------------------------------------------------------------------------
-//
-void MBadPixelsTreat::InterpolatePedestals(MPedPhotCam &pedphot) const
-{
-    const Int_t entries = pedphot.GetSize();
-
-    // Create arrays (FIXME: Check if its possible to create it only once)
-    MArrayD ped(entries);
-    MArrayD rms(entries);
-
-    //
-    // Loop over all pixels
-    //
-    for (UShort_t i=0; i<entries; i++)
-    {
-        //
-        // Check whether pixel with idx i is blind
-        //
-        if (!IsPixelBad(i))
-            continue;
-
-        //
-        // Get the corresponding geometry and pedestal
-        //
-        const MGeomPix    &gpix = (*fGeomCam)[i];
-        const MPedPhotPix &ppix = pedphot[i];
-
-        // Do Not-Use-Central-Pixel
-        const Bool_t nucp = !TESTBIT(fFlags, kUseCentralPixel);
-
-        Int_t num = nucp ? 0 : 1;
-
-        ped[i] = nucp ? 0 : ppix.GetMean();
-        rms[i] = nucp ? 0 : Pow2(ppix.GetRms());
-
-        //
-        // The values are rescaled to the small pixels area for the right comparison
-        //
-        const Double_t ratio = fGeomCam->GetPixRatio(i);
-
-        ped[i] *= ratio;
-        rms[i] *= ratio;
-
-        //
-        // Loop over all its neighbors
-        //
-        const Int_t n = gpix.GetNumNeighbors();
-        for (int j=0; j<n; j++)
-        {
-            const UShort_t nidx = gpix.GetNeighbor(j);
-
-            //
-            // Do not use blind neighbors
-            //
-            if (IsPixelBad(nidx))
-                continue;
-
-            //
-            // Get the geometry for the neighbor
-            //
             const Double_t    nratio = fGeomCam->GetPixRatio(nidx);
             const MPedPhotPix &nppix = pedphot[nidx];
@@ -372,6 +362,6 @@
             //The error is calculated as the quadratic sum of the errors
             //
-            ped[i] += nratio*nppix.GetMean();
-            rms[i] += nratio*Pow2(nppix.GetRms());
+            ped += nratio*nppix.GetMean();
+            rms += nratio*Pow2(nppix.GetRms());
 
             num++;
@@ -381,10 +371,7 @@
         // If not, unmap the pixel. The minimum number of good neighbors
         // should be fNumMinNeighbors
-        if (num < fNumMinNeighbors)
-        {
-            MSignalPix *pix =fEvt->GetPixById(i);
-            if (!pix)
-                pix = fEvt->AddPixel(i, 0, 0);
-            pix->SetPixelUnmapped();
+        if (num<fNumMinNeighbors)
+        {
+            (*fEvt)[i].SetPixelUnmapped();
             continue;
         }
@@ -394,8 +381,8 @@
         // to the pixel area
         //
-        ped[i] /=  (num*ratio);
-        rms[i]  = TMath::Sqrt(rms[i]/(num*ratio));
-
-        pedphot[i].Set(ped[i], rms[i]);
+        ped /= num*ratio;
+        rms  = TMath::Sqrt(rms/(num*ratio));
+
+        pedphot[i].Set(ped, rms);
     }
     pedphot.SetReadyToSave();
@@ -436,13 +423,13 @@
         const MGeomPix &gpix = (*fGeomCam)[i];
 
-        MArrayD time(gpix.GetNumNeighbors());
+        const UInt_t n2 = gpix.GetNumNeighbors();
+
+        Double_t time[n2];
 
         Int_t n0 = 0;
-        for (unsigned int j=0; j<time.GetSize(); j++)
-        {
-            const Int_t nn = gpix.GetNeighbor(j);
-
-            const Double_t t = (*fEvt)[nn].GetArrivalTime();
-            if (t>=0 && !IsPixelBad(nn))
+        for (unsigned int j=0; j<n2; j++)
+        {
+            const Double_t t = (*fEvt)[j].GetArrivalTime();
+            if (t>=0)
                 time[n0++] = t;
         }
