Index: trunk/MagicSoft/Mars/manalysis/MHillasSrc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MHillasSrc.cc	(revision 1487)
+++ trunk/MagicSoft/Mars/manalysis/MHillasSrc.cc	(revision 1488)
@@ -116,6 +116,4 @@
     }
 
-    fDist = dist;
-                                                 // [mm]
     //
     // Calculate Alpha and Cosda = cos(d,a)
@@ -123,8 +121,9 @@
     // a head-tail information
     //
-    const Double_t arg = (sy-tand*sx) / (fDist*sqrt(tand*tand+1));
+    const Double_t arg = (sy-tand*sx) / (dist*sqrt(tand*tand+1));
 
     fAlpha         = asin(arg)*kRad2Deg;        // [deg]
-    fCosDeltaAlpha = fHeadTail/fDist;           // [1]
+    fCosDeltaAlpha = fHeadTail/dist;            // [1]
+    fDist          = dist;                      // [mm]
 
     SetReadyToSave();
Index: trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc	(revision 1487)
+++ trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc	(revision 1488)
@@ -26,15 +26,23 @@
 /////////////////////////////////////////////////////////////////////////////
 //
-// MMultiDimDistCalc
-//
-// Calculated a multidimensional distance. It calculates the distance to
-// all vectors in a given matrix describing Gammas and another one
-// describing Hadrons (non gammas). The shortest distances are avaraged.
-// How many distances are used for avaraging can be specified in the
-// constructor.
+//  MMultiDimDistCalc
+//
+//  Calculated a multidimensional distance. It calculates the distance to
+//  all vectors in a given matrix describing Gammas and another one
+//  describing Hadrons (non gammas). The shortest distances are avaraged.
+//  How many distances are used for avaraging can be specified in the
+//  constructor.
+//
+//  * If you want to use the kernel function to calculate the distance use:
+//      MMultiDimDistCalc::SetUseKernelMethod();
+//  * To use only the n next neighbors for your calculation use:
+//      MMultiDimDistCalc::SetUseNumRows(n);
+//  * To use all reference events set the number to 0 <default>
 //
 ////////////////////////////////////////////////////////////////////////////
 #include "MMultiDimDistCalc.h"
 
+#include <fstream.h>
+
 #include "MHMatrix.h" // must be before MLogManip.h
 
@@ -49,4 +57,6 @@
 ClassImp(MMultiDimDistCalc);
 
+static const TString gsDefName  = "MMultiDimDistCalc";
+static const TString gsDefTitle = "Composite Probabilities Loop 1/2";
 // --------------------------------------------------------------------------
 //
@@ -54,12 +64,12 @@
 // avaraging in CalcDist
 //
-MMultiDimDistCalc::MMultiDimDistCalc(Int_t num, const char *name, const char *title)
-    : fNum(num)
+MMultiDimDistCalc::MMultiDimDistCalc(const char *name, const char *title)
+    : fNum(0), fUseKernel(kFALSE)
 {
     //
     //   set the name and title of this object
     //
-    fName  = name  ? name  : "MMultiDimDistCalc";
-    fTitle = title ? title : "Composite Probabilities Loop 1/2";
+    fName  = name  ? name  : gsDefName.Data();
+    fTitle = title ? title : gsDefTitle.Data();
 
     fData = new TList;
@@ -152,7 +162,21 @@
         event(n++) = data->GetValue();
 
-    Double_t dg = fMGammas->CalcDist(event, fNum);
-    Double_t dh = fMHadrons->CalcDist(event, fNum);
-
+    Double_t numg = fNum;
+    Double_t numh = fNum;
+    if (fNum==0)
+    {
+        numg = fMGammas->GetM().GetNrows();
+        numh = fMHadrons->GetM().GetNrows();
+    }
+    if (fUseKernel)
+    {
+        numg = -numg;
+        numh = -numh;
+    }
+
+    Double_t dg = fMGammas->CalcDist(event, numg);
+    Double_t dh = fMHadrons->CalcDist(event, numh);
+
+    //fHadroness->SetHadroness(dg/(dg+dh));
     fHadroness->SetHadroness(exp(-dh/dg));
 
@@ -160,2 +184,19 @@
 }
 
+void MMultiDimDistCalc::StreamPrimitive(ofstream &out) const
+{
+    out << "   MMultiDimDist " << GetUniqueName();
+
+    if (fName!=gsDefName || fTitle!=gsDefTitle)
+    {
+        out << "(\"" << fName << "\"";
+        if (fTitle!=gsDefTitle)
+            out << ", \"" << fTitle << "\")";
+    }
+    out << ";" << endl;
+
+    if (fNum!=0)
+        out << "   " << GetUniqueName() << ".SetUseNumRows(" << fNum << ");" << endl;
+    if (fUseKernel)
+        out << "   " << GetUniqueName() << ".SetUseKernelMethod();" << endl;
+}
Index: trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.h	(revision 1487)
+++ trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.h	(revision 1488)
@@ -13,5 +13,6 @@
 {
 private:
-    Int_t fNum;             // number of distances used for an avarage
+    Int_t  fNum;            // number of distances used for an avarage
+    Bool_t fUseKernel;      // Flag whether kernel method should be used
 
     MHMatrix   *fMGammas;   //! Gammas describing matrix
@@ -22,12 +23,17 @@
     TList *fData;           //! Used to store the MDataChains to get the event values
 
+    void StreamPrimitive(ofstream &out) const;
+
 public:
-    MMultiDimDistCalc(Int_t num, const char *name=NULL, const char *title=NULL);
+    MMultiDimDistCalc(const char *name=NULL, const char *title=NULL);
     ~MMultiDimDistCalc();
+
+    void SetUseNumRows(UShort_t n=0) { fNum = n; }
+    void SetUseKernelMethod(Bool_t k=kTRUE) { fUseKernel = k; }
 
     Bool_t PreProcess(MParList *plist);
     Bool_t Process();
 
-    ClassDef(MMultiDimDistCalc, 1) // Task to calculate multidimensional distances
+    ClassDef(MMultiDimDistCalc, 0) // Task to calculate multidimensional distances
 };
 
