Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 7866)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 7867)
@@ -28,4 +28,11 @@
    * msignal/MExtractedSignalCam.cc:
      - some update to returning the hi-/lo-gain ratio
+
+   * mbase/MMath.[h,cc]:
+     - new functions MedianDev
+
+   * mhist/MHCamera.[h,cc]:
+     - new function GetMedian
+     - new function GetMedianDev
 
 
Index: /trunk/MagicSoft/Mars/NEWS
===================================================================
--- /trunk/MagicSoft/Mars/NEWS	(revision 7866)
+++ /trunk/MagicSoft/Mars/NEWS	(revision 7867)
@@ -8,4 +8,8 @@
    - general: Fixed some warnings thrown if more warnings are switched
      on in the compiler
+
+   - general: Added a function to MMath which returns the Median of
+     the distribution abs(y[i]-median), which is somehow the median
+     counterpart of the rms.
 
    - showplot:
Index: /trunk/MagicSoft/Mars/mbase/MMath.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MMath.cc	(revision 7866)
+++ /trunk/MagicSoft/Mars/mbase/MMath.cc	(revision 7867)
@@ -161,4 +161,85 @@
 }
 
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+template <class Size, class Element>
+Double_t MMath::MedianDevImp(Size n, const Element *a)
+{
+    // Get median of distribution
+    const Double_t med = TMath::Median(n, a);
+
+    // Allocate space for distribution
+    Double_t *arr = new Double_t[n];
+
+    // Create the abs(a[i]-med) distribution
+    for (int i=0; i<n; i++)
+        arr[i] = TMath::Abs(a[i]-med);
+
+    // FIXME: GausProb() is a workaround. It should be taken into account in Median!
+    const Double_t rc = TMath::Median(n, arr);
+
+    // delete space
+    delete arr;
+
+    // return result
+    return rc;
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+Double_t MMath::MedianDev(Long64_t n, const Short_t *a)
+{
+    return MedianDevImp(n, a);
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+Double_t MMath::MedianDev(Long64_t n, const Int_t *a)
+{
+    return MedianDevImp(n, a);
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+Double_t MMath::MedianDev(Long64_t n, const Float_t *a)
+{
+    return MedianDevImp(n, a);
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+Double_t MMath::MedianDev(Long64_t n, const Double_t *a)
+{
+    return MedianDevImp(n, a);
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+Double_t MMath::MedianDev(Long64_t n, const Long_t *a)
+{
+    return MedianDevImp(n, a);
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value of the distribution of abs(a[i]-Median)
+//
+Double_t MMath::MedianDev(Long64_t n, const Long64_t *a)
+{
+    return MedianDevImp(n, a);
+}
+
 // --------------------------------------------------------------------------
 //
Index: /trunk/MagicSoft/Mars/mbase/MMath.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MMath.h	(revision 7866)
+++ /trunk/MagicSoft/Mars/mbase/MMath.h	(revision 7867)
@@ -18,4 +18,12 @@
 
     Double_t GaussProb(Double_t x, Double_t sigma=1, Double_t mean=0);
+
+    template <class Size, class Element> Double_t MedianDevImp(Size n, const Element *a);
+    Double_t  MedianDev(Long64_t n, const Short_t  *a);
+    Double_t  MedianDev(Long64_t n, const Int_t    *a);
+    Double_t  MedianDev(Long64_t n, const Float_t  *a);
+    Double_t  MedianDev(Long64_t n, const Double_t *a);
+    Double_t  MedianDev(Long64_t n, const Long_t   *a);
+    Double_t  MedianDev(Long64_t n, const Long64_t *a);
 
     Double_t Significance(Double_t s, Double_t b);
Index: /trunk/MagicSoft/Mars/mhist/MHCamera.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHCamera.cc	(revision 7866)
+++ /trunk/MagicSoft/Mars/mhist/MHCamera.cc	(revision 7867)
@@ -84,4 +84,5 @@
 
 #include "MArrayD.h"
+#include "MMath.h"    // MMath::GaussProb
 
 #define kItemsLegend 48 // see SetPalette(1,0)
@@ -312,4 +313,44 @@
     for (Int_t idx=0; idx<fNcells-2; idx++)
         arr[idx] ? SetUsed(idx) : ResetUsed(idx);
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value on the y-axis (profile option is correctly taken
+// into account)
+//
+Stat_t MHCamera::GetMedian() const
+{
+    // Just for speed reasons
+    if (!TestBit(kProfile))
+        return TMath::Median(GetSize()-2, GetArray()+1);
+
+    // Copy profiled data into new array (FIXME: Should we take errors into account?)
+    TArrayD arr(fNcells-2);
+    for (int i=1; i<fNcells-1; i++)
+        arr[i-1] = GetBinContent(i);
+
+    // return Median of the profile data
+    return TMath::Median(arr.GetSize(), arr.GetArray());
+}
+
+// ------------------------------------------------------------------------
+//
+// Return the median value (divided by MMath::GausProb(1.0)) of the
+// distribution of abs(y[i]-Median). This is my Median equivalent of the RMS
+//
+Stat_t MHCamera::GetMedianDev() const
+{
+    // Just for speed reasons
+    if (!TestBit(kProfile))
+        return MMath::MedianDev(GetSize()-2, GetArray()+1);
+
+    // Copy profiled data into new array (FIXME: Should we take errors into account?)
+    TArrayD arr(fNcells-2);
+    for (int i=1; i<fNcells-1; i++)
+        arr[i-1] = GetBinContent(i);
+
+    // return MedianDev of the profile data
+    return MMath::MedianDev(arr.GetSize(), arr.GetArray())/MMath::GaussProb(1.0);
 }
 
Index: /trunk/MagicSoft/Mars/mhist/MHCamera.h
===================================================================
--- /trunk/MagicSoft/Mars/mhist/MHCamera.h	(revision 7866)
+++ /trunk/MagicSoft/Mars/mhist/MHCamera.h	(revision 7867)
@@ -240,4 +240,7 @@
     Stat_t   GetMean(Int_t=0) const { return GetMeanSectors(TArrayI(), TArrayI(), kFALSE); }
     Stat_t   GetRMS(Int_t=0)  const { return GetRmsSectors(TArrayI(), TArrayI(), kFALSE); }
+
+    Stat_t   GetMedian() const;
+    Stat_t   GetMedianDev() const;
 
     Stat_t   GetMeanSector(Int_t sector, Int_t aidx, Bool_t all=kFALSE) const
