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
 };
 
Index: trunk/MagicSoft/Mars/mdata/DataLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mdata/DataLinkDef.h	(revision 1487)
+++ trunk/MagicSoft/Mars/mdata/DataLinkDef.h	(revision 1488)
@@ -6,4 +6,5 @@
 
 #pragma link C++ class MData+;
+#pragma link C++ class MDataArray+;
 #pragma link C++ class MDataList+;
 #pragma link C++ class MDataValue+;
Index: trunk/MagicSoft/Mars/mdata/MDataArray.cc
===================================================================
--- trunk/MagicSoft/Mars/mdata/MDataArray.cc	(revision 1488)
+++ trunk/MagicSoft/Mars/mdata/MDataArray.cc	(revision 1488)
@@ -0,0 +1,120 @@
+/* ======================================================================== *\
+!
+! *
+! * This file is part of MARS, the MAGIC Analysis and Reconstruction
+! * Software. It is distributed to you in the hope that it can be a useful
+! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
+! * It is distributed WITHOUT ANY WARRANTY.
+! *
+! * Permission to use, copy, modify and distribute this software and its
+! * documentation for any purpose is hereby granted without fee,
+! * provided that the above copyright notice appear in all copies and
+! * that both that copyright notice and this permission notice appear
+! * in supporting documentation. It is provided "as is" without express
+! * or implied warranty.
+! *
+!
+!
+!   Author(s): Thomas Bretz  08/2002 <mailto:tbretz@uni-sw.gwdg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2002
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+//   MDataArray
+//
+/////////////////////////////////////////////////////////////////////////////
+#include "MDataArray.h"
+
+#include <fstream.h>
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MDataChain.h"
+
+ClassImp(MDataArray);
+
+static const TString gsDefName  = "MDataArray";
+static const TString gsDefTitle = "Array to store MData cntainers";
+
+MDataArray::MDataArray(const char *name, const char *title)
+{
+    fName  = name  ? name  : gsDefName.Data();
+    fTitle = title ? title : gsDefTitle.Data();
+}
+
+void MDataArray::AddEntry(const TString rule)
+{
+    TObject *obj = new MDataChain(rule);
+    obj->SetBit(kCanDelete);
+    fList.Add(obj);
+}
+
+MData &MDataArray::operator[](Int_t i) const
+{
+    return (MData&)*((TObjArray)fList)[i];
+}
+
+Double_t MDataArray::operator()(Int_t i)
+{
+    return ((MData*)fList[i])->GetValue();
+}
+
+Bool_t MDataArray::PreProcess(const MParList *plist)
+{
+    if (fList.GetSize()==0)
+    {
+        *fLog << err << "Error - No Column specified... aborting." << endl;
+        return kFALSE;
+    }
+
+    TIter Next(&fList);
+    MData *data = NULL;
+    while ((data=(MData*)Next()))
+        if (!data->PreProcess(plist))
+            return kFALSE;
+
+    return kTRUE;
+}
+
+void MDataArray::Print(Option_t *opt = "") const
+{
+    Int_t n=0;
+
+    TIter Next(&fList);
+    MData *data = NULL;
+    while ((data=(MData*)Next()))
+    {
+        *fLog << all << " Line " << setw(3) << n++ << ": " << flush;
+        data->Print();
+        *fLog << endl;
+    }
+}
+
+Bool_t MDataArray::AsciiWrite(ostream &out) const
+{
+    ((TObjArray)fList).ForEach(MParContainer, AsciiWrite)(out);
+    return kTRUE;
+}
+
+void MDataArray::StreamPrimitive(ofstream &out) const
+{
+    out << "   MDataArray " << GetUniqueName();
+
+    if (fName!=gsDefName)
+    {
+        out << "(\"" << fName << "\"";
+        if (fTitle!=gsDefTitle)
+            out << ", \"" << fTitle << "\")";
+    }
+    out << ";" << endl;
+
+    TIter Next(&fList);
+    MData *data = NULL;
+    while ((data=(MData*)Next()))
+        out << "   " << GetUniqueName() << ".AddEntry(\"" << data->GetRule() << "\");" << endl;
+}
Index: trunk/MagicSoft/Mars/mdata/MDataArray.h
===================================================================
--- trunk/MagicSoft/Mars/mdata/MDataArray.h	(revision 1488)
+++ trunk/MagicSoft/Mars/mdata/MDataArray.h	(revision 1488)
@@ -0,0 +1,44 @@
+#ifndef MARS_MDataArray
+#define MARS_MDataArray
+
+/////////////////////////////////////////////////////////////////////////////
+//              
+//  MDataArray  
+//
+/////////////////////////////////////////////////////////////////////////////
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+
+#ifndef ROOT_TObjArray
+#include <TObjArray.h>
+#endif
+
+class MData;
+class MParList;
+
+class MDataArray : public MParContainer
+{
+    TObjArray fList;
+
+    void StreamPrimitive(ofstream &out) const;
+
+public:
+    MDataArray(const char *name=NULL, const char *title=NULL);
+
+    void AddEntry(const TString rule);
+
+    MData &operator[](Int_t i) const;
+    Double_t operator()(Int_t i);
+
+    Bool_t PreProcess(const MParList *plist);
+
+    void Print(Option_t *opt = "") const;
+    Bool_t AsciiWrite(ostream &out) const;
+
+    Int_t GetNumEntries() const { return fList.GetEntries(); }
+
+    ClassDef(MDataArray, 1) // An array of MData containers
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mdata/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mdata/Makefile	(revision 1487)
+++ trunk/MagicSoft/Mars/mdata/Makefile	(revision 1488)
@@ -32,4 +32,5 @@
 
 SRCFILES = MData.cc \
+	   MDataArray.cc \
 	   MDataMember.cc \
 	   MDataValue.cc \
Index: trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc
===================================================================
--- trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 1487)
+++ trunk/MagicSoft/Mars/mfileio/MWriteRootFile.cc	(revision 1488)
@@ -49,4 +49,6 @@
 ClassImp(MWriteRootFile);
 
+static const TString gsDefName  = "MWriteRootFile";
+static const TString gsDefTitle = "Task which writes a root-output file";
 // --------------------------------------------------------------------------
 //
@@ -56,6 +58,6 @@
 MWriteRootFile::MWriteRootFile() : fOut(NULL)
 {
-    fName  = "MWriteRootFile";
-    fTitle = "Task which writes a root-output file";
+    fName  = gsDefName;
+    fTitle = gsDefTitle;
 
     fBranches.SetOwner();
@@ -76,6 +78,6 @@
                                const char *title)
 {
-    fName  = name  ? name  : "MWriteRootFile";
-    fTitle = title ? title : "Task which writes a root-output file";
+    fName  = name  ? name  : gsDefName.Data();
+    fTitle = title ? title : gsDefTitle.Data();
 
     //
@@ -409,6 +411,14 @@
     out << fOut->GetOption() << "\", \"";
     out << fOut->GetTitle() << "\", ";
-    out << fOut->GetCompressionLevel() << ", \"";
-    out << fName << "\", \"" << fTitle << "\");" << endl;;
+    out << fOut->GetCompressionLevel();
+
+    if (fName!=gsDefName || fTitle!=gsDefTitle)
+    {
+        out << ", \"" << fName << "\"";
+        if (fTitle!=gsDefTitle)
+            out << ", \"" << fTitle << "\"";
+    }
+    out << ");" << endl;
+
 
     MRootFileBranch *entry;
@@ -416,4 +426,6 @@
     while ((entry=(MRootFileBranch*)Next()))
     {
+        out << "   " << GetUniqueName() << ".AddContainer(";
+
         if  (entry->GetContainer())
         {
@@ -424,6 +436,9 @@
             out << "\"" << entry->GetContName() << "\"";
 
-        out << ", \"" << entry->GetName() << "\", \"";
-        out << entry->GetTitle() << "\");" << endl;
+        out << ", \"" << entry->GetName() << "\"";
+        if ((TString)entry->GetTitle()!="")
+            out << ", \"" << entry->GetTitle() << "\"";
+
+        out <<");" << endl;
     }
 }
