Index: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.cc	(revision 5762)
+++ trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.cc	(revision 5777)
@@ -56,5 +56,7 @@
 
 #include <TEnv.h>
-#include <TArrayD.h>
+
+#include "MArrayI.h"
+#include "MArrayD.h"
 
 #include "MLog.h"
@@ -86,5 +88,5 @@
 //
 MBadPixelsCalc::MBadPixelsCalc(const char *name, const char *title)
-    : fPedestalLevel(3), fNamePedPhotCam("MPedPhotCam")
+    : fPedestalLevel(3), fPedestalLevelVariance(-1), fNamePedPhotCam("MPedPhotCam")
 {
     fName  = name  ? name  : gsDefName.Data();
@@ -118,5 +120,9 @@
     }
 
-    *fLog << inf << "Name of MPedPhotCam used: " << fNamePedPhotCam << endl;
+    *fLog << inf << "Name of MPedPhotCam to get 'pedestal rms' from used: " << fNamePedPhotCam << endl;
+    if (fPedestalLevel)
+        *fLog << "Checking mean 'pedestal rms' against absolute value with level " << fPedestalLevel << endl;
+    if (fPedestalLevelVariance)
+        *fLog << "Checking mean 'pedestal rms' against its variance with level " << fPedestalLevelVariance << endl;
 
     return kTRUE;
@@ -167,6 +173,6 @@
     const Int_t na = fGeomCam->GetNumAreas();
 
-    TArrayD meanrms(na);
-    TArrayI npix(na);
+    MArrayD meanrms(na);
+    MArrayI npix(na);
 
     for (Int_t i=0; i<entries; i++)
@@ -196,5 +202,6 @@
     }
 
-    TArrayD meanrms2(na);
+    MArrayD meanrms2(na);
+    MArrayD varrms2(na);
     for (Int_t i=0; i<entries; i++)
     {
@@ -208,8 +215,11 @@
 
         meanrms2[aidx] += rms;
+        varrms2 [aidx] += rms*rms;
         npix[aidx]++;
     }
 
     //if no pixel has a minimum signal, return
+    MArrayD lolim1(na), lolim2(na); // Precalcualtion of limits
+    MArrayD uplim1(na), uplim2(na); // for speeed reasons
     for (int i=0; i<na; i++)
     {
@@ -221,4 +231,19 @@
 
         meanrms2[i] /= npix[i];
+
+        if (fPedestalLevel>0)
+        {
+            lolim1[i]  = meanrms2[i]/fPedestalLevel;
+            uplim1[i]  = meanrms2[i]*fPedestalLevel;
+        }
+
+        if (fPedestalLevelVariance>0)
+        {
+            varrms2[i] /= npix[i];
+            varrms2[i]  = TMath::Sqrt(varrms2[i]-meanrms2[i]*meanrms2[i]);
+
+            lolim2[i]   = meanrms2[i]-fPedestalLevelVariance*varrms2[i];
+            uplim2[i]   = meanrms2[i]+fPedestalLevelVariance*varrms2[i];
+        }
     }
 
@@ -231,5 +256,6 @@
         const Byte_t  aidx = (*fGeomCam)[i].GetAidx();
 
-        if (rms>meanrms2[aidx]/fPedestalLevel && rms<=meanrms2[aidx]*fPedestalLevel)
+        if ((fPedestalLevel<0         || (rms>lolim1[aidx] && rms<=uplim1[aidx])) &&
+            (fPedestalLevelVariance<0 || (rms>lolim2[aidx] && rms<=uplim2[aidx])))
             continue;
 
@@ -237,4 +263,5 @@
         bads++;
     }
+
 
     // Check if the number of pixels to blind is > 60% of total number of pixels
@@ -255,8 +282,8 @@
 Int_t MBadPixelsCalc::Process()
 {
-    if (fPedestalLevel>0)
+    if (fPedestalLevel>0 || fPedestalLevelVariance)
     {
         CheckPedestalRms();
-        fPedPhotCam->ReCalc(*fGeomCam, fBadPixels);
+        //fPedPhotCam->ReCalc(*fGeomCam, fBadPixels);
     }
 
@@ -267,5 +294,6 @@
 //
 // Read the setup from a TEnv, eg:
-//   MBadPixelsCalc.PedestalLevel: 3.0
+//   MBadPixelsCalc.PedestalLevel:         3.0
+//   MBadPixelsCalc.PedestalLevelVariance: 3.0
 //
 Int_t MBadPixelsCalc::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
@@ -277,4 +305,10 @@
         SetPedestalLevel(GetEnvValue(env, prefix, "PedestalLevel", fPedestalLevel));
     }
+
+    if (IsEnvDefined(env, prefix, "PedestalLevelVariance", print))
+    {
+        rc = kTRUE;
+        SetPedestalLevelVariance(GetEnvValue(env, prefix, "PedestalLevelVariance", fPedestalLevelVariance));
+    }
     return rc;
 }
Index: trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.h	(revision 5762)
+++ trunk/MagicSoft/Mars/mbadpixels/MBadPixelsCalc.h	(revision 5777)
@@ -18,4 +18,6 @@
 
     Float_t fPedestalLevel;
+    Float_t fPedestalLevelVariance;
+
     TString fNamePedPhotCam; // name of the 'MPedPhotCam' container
    
@@ -30,6 +32,7 @@
     MBadPixelsCalc(const char *name=NULL, const char *title=NULL);
 
-    void SetPedestalLevel(Float_t f) { fPedestalLevel=f; }
-    void SetNamePedPhotCam(const char *name)    { fNamePedPhotCam = name; }
+    void SetPedestalLevel(Float_t f)         { fPedestalLevel=f; }
+    void SetPedestalLevelVariance(Float_t f) { fPedestalLevelVariance=f; }
+    void SetNamePedPhotCam(const char *name) { fNamePedPhotCam = name; }
 
     ClassDef(MBadPixelsCalc, 1) // Task to find bad pixels (star, broken pixels, etc)
