Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 4715)
+++ trunk/MagicSoft/Mars/Changelog	(revision 4716)
@@ -79,4 +79,5 @@
    * mhist/MHEvent.[h,cc]:
      - added new option for island index
+     - added kEvtCleaningData
 
    * mimage/MImgCleanStd.[h,cc]:
@@ -85,4 +86,5 @@
      - added ReadEnv
      - updated StreamPrimitive
+     - added new cleaning method (probability cleaning)
 
    * mimage/Makefile:
@@ -142,4 +144,14 @@
    * msignal/MArrivalTime.h:
      - removed operator()
+     - added operator[] const
+
+   * manalysis/MCameraData.[h,cc]:
+     - added algorithm for 'Probability cleaning'
+
+   * mbase/MMath.[h,cc]:
+     - added GaussProb
+
+   * mjobs/MSequence.h:
+     - added IsValid
 
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 4715)
+++ trunk/MagicSoft/Mars/NEWS	(revision 4716)
@@ -25,4 +25,10 @@
    - implemented new image parameters displaying the number of islands,
      saturated hi-gain and lo-gain pixels (MImagePar, MHImagePar)
+
+   - event display in executable changed to support also calibrated files
+     (done with MJCalibrateSignal)
+
+   - added a cleaning which takes signal height _and_ arrival time into
+     account: probability cleaning (for more details see MImgCleanStd)
 
 
Index: trunk/MagicSoft/Mars/manalysis/MCameraData.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCameraData.cc	(revision 4715)
+++ trunk/MagicSoft/Mars/manalysis/MCameraData.cc	(revision 4716)
@@ -34,17 +34,20 @@
 #include "MCameraData.h"
 
+#include "MMath.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MGeomCam.h"
+#include "MGeomPix.h"
+
+#include "MPedPhotCam.h"
+#include "MPedPhotPix.h"
+
 #include "MCerPhotEvt.h"
 #include "MCerPhotPix.h"
 
-#include "MGeomCam.h"
-#include "MGeomPix.h"
-
-#include "MLog.h"
-#include "MLogManip.h"
-
-#include "MPedPhotCam.h"
-#include "MPedPhotPix.h"
-
 #include "MSigmabar.h"
+#include "MArrivalTime.h"
 
 ClassImp(MCameraData);
@@ -101,16 +104,14 @@
     const Int_t n = geom.GetNumPixels();
 
-    fData.Set(n);
-    fData.Reset();
-
-    fValidity.Set(n);
-    fValidity.Reset();
-
-    const Int_t entries = evt.GetNumPixels();
-
-    //
-    // check the number of all pixels against the noise level and
-    // set them to 'unused' state if necessary
-    // 
+    // Reset arrays
+    fData.Set(n);
+    fData.Reset();
+
+    fValidity.Set(n);
+    fValidity.Reset();
+
+    const Int_t entries = evt.GetNumPixels();
+
+    // calculate cleaning levels
     for (Int_t i=0; i<entries; i++)
     {
@@ -149,19 +150,18 @@
     const Int_t n = geom.GetNumPixels();
 
-    fData.Set(n);
-    fData.Reset();
-
-    fValidity.Set(n);
-    fValidity.Reset();
-
-    const Int_t entries   = evt.GetNumPixels();
+    // reset arrays
+    fData.Set(n);
+    fData.Reset();
+
+    fValidity.Set(n);
+    fValidity.Reset();
+
+    // check validity of rms with area index 0
     const Float_t anoise0 = cam.GetArea(0).GetRms();
     if (anoise0<=0)
         return;
 
-    //
-    // check the number of all pixels against the noise level and
-    // set them to 'unused' state if necessary
-    // 
+    // calculate cleaning levels
+    const Int_t entries = evt.GetNumPixels();
     for (Int_t i=0; i<entries; i++)
     {
@@ -200,19 +200,17 @@
     const Int_t n = geom.GetNumPixels();
 
-    fData.Set(n);
-    fData.Reset();
-
-    fValidity.Set(n);
-    fValidity.Reset();
-
+    // reset arrays
+    fData.Set(n);
+    fData.Reset();
+
+    fValidity.Set(n);
+    fValidity.Reset();
+
+    // check validity of noise
     if (noise<=0)
         return;
 
-    const Int_t entries = evt.GetNumPixels();
-
-    //
-    // check the number of all pixels against the noise level and
-    // set them to 'unused' state if necessary
-    // 
+    // calculate cleaning levels
+    const Int_t entries = evt.GetNumPixels();
     for (Int_t i=0; i<entries; i++)
     {
@@ -244,19 +242,18 @@
     const Int_t n = geom.GetNumPixels();
 
-    fData.Set(n);
-    fData.Reset();
-
-    fValidity.Set(n);
-    fValidity.Reset();
-
-    const Int_t   entries = evt.GetNumPixels();
-    const Float_t noise0  = cam.GetArea(0).GetRms();
+    // reset arrays
+    fData.Set(n);
+    fData.Reset();
+
+    fValidity.Set(n);
+    fValidity.Reset();
+
+    // check validity of rms with area index 0
+    const Float_t noise0 = cam.GetArea(0).GetRms();
     if (noise0<=0)
         return;
 
-    //
-    // check the number of all pixels against the noise level and
-    // set them to 'unused' state if necessary
-    // 
+    // calculate cleaning levels
+    const Int_t entries = evt.GetNumPixels();
     for (Int_t i=0; i<entries; i++)
     {
@@ -280,4 +277,101 @@
 // --------------------------------------------------------------------------
 //
+// Function to calculate the cleaning level for all pixels in a given event.
+// The level is the probability that the signal is a real signal (taking
+// the signal height and the fluctuation of the background into account)
+// times one minus the probability that the signal is a background
+// fluctuation (calculated from the time spread of arrival times
+// around the pixel with the highest signal)
+//
+// FIXME: Should the check noise<=0 be replaced by MBadPixels?
+//
+void MCameraData::CalcCleaningProbability(const MCerPhotEvt &evt, const MPedPhotCam &pcam,
+                                          const MGeomCam &geom,   const MArrivalTime *tcam)
+{
+    const Int_t n = geom.GetNumPixels();
+
+    // Reset arrays
+    fData.Set(n);
+    fData.Reset();
+
+    fValidity.Set(n);
+    fValidity.Reset();
+
+    // check validity of noise
+    const Float_t anoise0 = pcam.GetArea(0).GetRms();
+    if (anoise0<=0)
+        return;
+
+    const Int_t entries = evt.GetNumPixels();
+
+    // Find pixel with max signal
+    Int_t maxidx = 0;
+    if (tcam)
+    {
+        // Find pixel enty with maximum signal
+        for (Int_t i=0; i<entries; i++)
+        {
+            const Double_t s0 = evt[i].GetNumPhotons()      * geom.GetPixRatio(i);
+            const Double_t s1 = evt[maxidx].GetNumPhotons() * geom.GetPixRatio(maxidx);
+            if (s0>s1)
+                maxidx = i;
+        }
+
+        // Get software index of pixel
+        maxidx = evt[maxidx].GetPixId();
+    }
+
+    const Double_t timemean = tcam ? (*tcam)[maxidx] : 0;
+    const Double_t timerms  = 0.75; //[slices] rms time spread around highest pixel
+
+    // calculate cleaning levels
+    for (Int_t i=0; i<entries; i++)
+    {
+        const MCerPhotPix &spix = evt[i];
+
+        const Int_t   idx = spix.GetPixId();
+        const Float_t rms = pcam[idx].GetRms();
+        if (rms<=0) // fData[idx]=0, fValidity[idx]=0
+            continue;
+
+        fValidity[idx]=1;
+
+        // get signal and arrival time
+        const UInt_t  aidx     = geom[idx].GetAidx();
+        const Float_t ratio    = pcam.GetArea(aidx).GetRms()/anoise0;
+
+        const Double_t signal  = spix.GetNumPhotons() * geom.GetPixRatio(idx) * ratio / rms;
+        const Double_t time    = tcam ? (*tcam)[idx] : 1;
+
+        // if signal<0 the probability is equal 0
+        if (signal<0)
+            continue;
+
+        // Just for convinience for easy readybility
+        const Double_t means   = 0;
+        const Double_t meant   = timemean;
+
+        const Double_t sigmas  = rms;
+        const Double_t sigmat  = timerms;
+
+        // Get probabilities
+        const Double_t psignal = MMath::GaussProb(signal, sigmas, means);
+        const Double_t pbckgnd = MMath::GaussProb(time,   sigmat, meant);
+
+        // Set probability
+        fData[idx]     = psignal*(1-pbckgnd);
+        fValidity[idx] = 1;
+
+        // Make sure, that we don't run into trouble because of
+        // a little numerical uncertanty
+        if (fData[idx]>1)
+            fData[idx]=1;
+        if (fData[idx]<0)
+            fData[idx]=0;
+    }
+}
+
+// --------------------------------------------------------------------------
+//
 // Returns the contents of the pixel.
 //
Index: trunk/MagicSoft/Mars/manalysis/MCameraData.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCameraData.h	(revision 4715)
+++ trunk/MagicSoft/Mars/manalysis/MCameraData.h	(revision 4716)
@@ -19,4 +19,5 @@
 class MCerPhotEvt;
 class MPedPhotCam;
+class MArrivalTime;
 
 class MCameraData : public MParContainer, public MCamEvent
@@ -38,10 +39,10 @@
     void CalcCleaningLevel(const MCerPhotEvt &evt, Double_t noise,
                            const MGeomCam &geom);
-
     void CalcCleaningLevel2(const MCerPhotEvt &evt, const MPedPhotCam &fCam,
-                           const MGeomCam &geom);
-
+                            const MGeomCam &geom);
     void CalcCleaningLevelDemocratic(const MCerPhotEvt &evt, const MPedPhotCam &cam,
                                      const MGeomCam &geom);
+    void CalcCleaningProbability(const MCerPhotEvt &evt, const MPedPhotCam &pcam,
+                                 const MGeomCam &geom,   const MArrivalTime *tcam);
 
     const TArrayD &GetData() const { return fData; }
Index: trunk/MagicSoft/Mars/mbase/MMath.cc
===================================================================
--- trunk/MagicSoft/Mars/mbase/MMath.cc	(revision 4715)
+++ trunk/MagicSoft/Mars/mbase/MMath.cc	(revision 4716)
@@ -47,5 +47,5 @@
     const Double_t f = s+k*k*b;
 
-    return f==0 ? 0 : (s-b)/TMath::Sqrt(f);
+    return f==0 ? 0 : (s-b)/Sqrt(f);
 }
 
@@ -86,8 +86,8 @@
         return -1;
 
-    const Double_t l = s*TMath::Log(s/sum*(alpha+1)/alpha);
-    const Double_t m = b*TMath::Log(b/sum*(alpha+1)      );
+    const Double_t l = s*Log(s/sum*(alpha+1)/alpha);
+    const Double_t m = b*Log(b/sum*(alpha+1)      );
 
-    return l+m<0 ? -1 : TMath::Sqrt((l+m)*2);
+    return l+m<0 ? -1 : Sqrt((l+m)*2);
 }
 
@@ -104,4 +104,15 @@
         return 0;
 
-    return TMath::Sign(sig, s-b);
+    return Sign(sig, s-b);
 }
+
+// --------------------------------------------------------------------------
+//
+// Returns: 2/(sigma*sqrt(2))*integral[0,x](exp(-(x-mu)^2/(2*sigma^2)))
+//
+Double_t MMath::GaussProb(Double_t x, Double_t sigma, Double_t mean)
+{
+    static const Double_t sqrt2 = Sqrt(2.);
+    return Erf((x-mean)/(sigma*sqrt2));
+}
+
Index: trunk/MagicSoft/Mars/mbase/MMath.h
===================================================================
--- trunk/MagicSoft/Mars/mbase/MMath.h	(revision 4715)
+++ trunk/MagicSoft/Mars/mbase/MMath.h	(revision 4716)
@@ -9,4 +9,6 @@
 {
 public:
+    static Double_t GaussProb(Double_t x, Double_t sigma, Double_t mean=0);
+
     static Double_t Significance(Double_t s, Double_t b);
     static Double_t SignificanceSym(Double_t s, Double_t b);
Index: trunk/MagicSoft/Mars/mhist/MHEvent.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHEvent.cc	(revision 4715)
+++ trunk/MagicSoft/Mars/mhist/MHEvent.cc	(revision 4716)
@@ -152,4 +152,8 @@
         fHist->SetYTitle("L");
         break;
+    case kEvtCleaningData:
+        fHist->SetName("CleanData");
+        fHist->SetYTitle("L");
+        break;
      case kEvtIdxMax:
         fHist->SetName("Max Slice Idx");
@@ -220,4 +224,7 @@
             fHist->SetLevels(lvl);
         }
+        break;
+    case kEvtCleaningData:
+        fHist->SetCamContent(*event, 0);
         break;
     case kEvtIdxMax:
Index: trunk/MagicSoft/Mars/mhist/MHEvent.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHEvent.h	(revision 4715)
+++ trunk/MagicSoft/Mars/mhist/MHEvent.h	(revision 4716)
@@ -22,4 +22,5 @@
         kEvtSignalRaw, kEvtSignalDensity, kEvtPedestal, 
         kEvtPedestalRMS, kEvtRelativeSignal, kEvtCleaningLevels,
+        kEvtCleaningData,
         kEvtIdxMax, kEvtArrTime, kEvtTrigPix, kEvtIslandIndex
     };
Index: trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc
===================================================================
--- trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 4715)
+++ trunk/MagicSoft/Mars/mimage/MImgCleanStd.cc	(revision 4716)
@@ -549,8 +549,7 @@
         fData->CalcCleaningLevelDemocratic(*fEvt, *fPed, *fCam);
         break;
-        /*
     case kProbability:
         fData->CalcCleaningProbability(*fEvt, *fPed, *fCam, fTime);
-        break;*/
+        break;
     default:
         break;
Index: trunk/MagicSoft/Mars/mjobs/MSequence.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 4715)
+++ trunk/MagicSoft/Mars/mjobs/MSequence.h	(revision 4716)
@@ -43,4 +43,6 @@
     void Print(Option_t *o="") const;
 
+    Bool_t IsValid() const { return fSequence!=(UInt_t)-1; }
+
     void SetupPedRuns(MDirIter &iter, const char *path=0) const;
     void SetupDatRuns(MDirIter &iter, const char *path=0) const;
Index: trunk/MagicSoft/Mars/msignal/MArrivalTime.h
===================================================================
--- trunk/MagicSoft/Mars/msignal/MArrivalTime.h	(revision 4715)
+++ trunk/MagicSoft/Mars/msignal/MArrivalTime.h	(revision 4716)
@@ -40,5 +40,6 @@
     const TArrayF &GetDataErr() const { return fDataErr; }
 
-    Double_t operator[](int i) { return fData[i]; }
+    Double_t operator[](int i)       { return fData[i]; }
+    Double_t operator[](int i) const { return fData[i]; }
 
     // Do not do such things! It is highly dangerous to use two very similar operators
