Index: trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 1325)
+++ trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 1337)
@@ -21,4 +21,9 @@
 #pragma link C++ class MMcPedestalNSBAdd+;
 
+#pragma link C++ class MHadroness+;
+
+#pragma link C++ class MCompProbCalc+;
+#pragma link C++ class MMultiDimDistCalc+;
+
 #pragma link C++ class MSrcPosCam+;
 #pragma link C++ class MHillas+;
Index: trunk/MagicSoft/Mars/manalysis/MCompProbCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCompProbCalc.cc	(revision 1337)
+++ trunk/MagicSoft/Mars/manalysis/MCompProbCalc.cc	(revision 1337)
@@ -0,0 +1,164 @@
+/* ======================================================================== *\
+!
+! *
+! * 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): Abelardo Moralejo <mailto:moralejo@pd.infn.it>
+!   Author(s): Thomas Bretz, 5/2002 <mailto:moralejo@pd.infn.it>
+!
+!   Copyright: MAGIC Software Development, 2000-2002
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// MCompProbCalc
+//
+// FIXME: Use A huge matrix to evaluate variable bins instead of a
+// histogram
+//
+////////////////////////////////////////////////////////////////////////////
+#include "MCompProbCalc.h"
+
+#include <TH1.h>
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+#include "MDataChain.h"
+
+#include "MHCompProb.h"
+#include "MHadroness.h"
+
+ClassImp(MCompProbCalc);
+
+// --------------------------------------------------------------------------
+//
+// Default constructor
+//
+MCompProbCalc::MCompProbCalc(const char *name, const char *title)
+{
+    //
+    //   set the name and title of this object
+    //
+    fName  = name  ? name  : "MCompProbCalc";
+    fTitle = title ? title : "Composite Probabilities Loop 1/2";
+
+    fData = new TList;
+    fData->SetOwner();
+}
+
+// --------------------------------------------------------------------------
+//
+// Destructor
+//
+MCompProbCalc::~MCompProbCalc()
+{
+    delete fData;
+}
+
+// --------------------------------------------------------------------------
+//
+// Needs:
+//  - MHCompProb
+//  - all data values which were used to build the MHCompProb
+//  - MHadroness
+//
+Bool_t MCompProbCalc::PreProcess(MParList *plist)
+{
+    MHCompProb *p = (MHCompProb*)plist->FindObject("MHCompProb");
+    if (!p)
+    {
+        *fLog << err << dbginf << "MHCompProb not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fHistVar = p->GetHistVar();
+    if (fHistVar->GetSize()==0)
+    {
+        *fLog << err << dbginf << "No Entries in MHCompProb::fHistVar... aborting." << endl;
+        return kFALSE;
+    }
+
+    const TList *rules = p->GetRules();
+    if (rules->GetSize()==0)
+    {
+        *fLog << err << dbginf << "No Entries in MHCompProb::fRules... aborting." << endl;
+        return kFALSE;
+    }
+
+    if (fHistVar->GetSize() != rules->GetSize())
+    {
+        *fLog << err << dbginf << "Number of rules doesn't match number of var.bin histograms.. aborting." << endl;
+        return kFALSE;
+    }
+
+    TIter Next(rules);
+    TObject *data=NULL;
+    while ((data=Next()))
+    {
+        MDataChain *chain = new MDataChain(data->GetName());
+        if (!chain->PreProcess(plist))
+        {
+            delete chain;
+            return kFALSE;
+        }
+        fData->Add(chain);
+    }
+
+    fHadroness = (MHadroness*)plist->FindCreateObj("MHadroness");
+    if (!fHadroness)
+        return kFALSE;
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Calculate the hadroness (composite propability)
+//  - For each data member search the bin corresponding to the data value.
+//  - The value describes with which probability this value corresponds to
+//    a hadron
+//  - For all data members multiply the probabilities.
+//  - For normalization take the n-th root of the result.
+//  - This is the hadroness stored in the MHadroness container
+//
+Bool_t MCompProbCalc::Process()
+{
+    Double_t p = 1;
+
+    TIter NextH(fHistVar);
+    TIter NextD(fData);
+
+    TH1D *hist=NULL;
+
+    Int_t n = 0;
+
+    while ((hist=(TH1D*)NextH()))
+    {
+        MData *data = (MData*)NextD();
+
+        Int_t ibin = hist->FindBin(data->GetValue());
+
+        p *= hist->GetBinContent(ibin) / hist->GetEntries();
+        n++;
+    }
+
+    fHadroness->SetHadroness(pow(p, 1./n));
+    return kTRUE;
+}
+
Index: trunk/MagicSoft/Mars/manalysis/MCompProbCalc.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCompProbCalc.h	(revision 1337)
+++ trunk/MagicSoft/Mars/manalysis/MCompProbCalc.h	(revision 1337)
@@ -0,0 +1,33 @@
+#ifndef MARS_MCompProbCalc
+#define MARS_MCompProbCalc
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MMcEvt;
+class MParList;
+class MHadroness;
+
+class MCompProbCalc : public MTask
+{
+private:
+    MHadroness *fHadroness; //! Output container (Hadroness)
+
+    TList *fData;           //! List of MDataChains to be used
+    TList *fHistVar;        //! List of variable bin size histograms
+
+    void Fill(TList &list);
+    void SetBinningHistVar();
+
+public:
+    MCompProbCalc(const char *name=NULL, const char *title=NULL);
+    ~MCompProbCalc();
+
+    Bool_t PreProcess(MParList *plist);
+    Bool_t Process();
+
+    ClassDef(MCompProbCalc, 1) // Task to calculate composite probabilities
+};
+
+#endif
Index: trunk/MagicSoft/Mars/manalysis/MHadroness.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MHadroness.cc	(revision 1337)
+++ trunk/MagicSoft/Mars/manalysis/MHadroness.cc	(revision 1337)
@@ -0,0 +1,69 @@
+/* ======================================================================== *\
+!
+! *
+! * 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 5/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2002
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// MHadroness
+//
+// The Value must be in the range [0,1]
+// It should be the value used for gamma/hadron seperation.
+// For quality histograms look at MHHadroness
+//
+/////////////////////////////////////////////////////////////////////////////
+#include "MHadroness.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+ClassImp(MHadroness);
+
+// --------------------------------------------------------------------------
+//
+// Default constructor.
+//
+MHadroness::MHadroness(const char *name, const char *title)
+    : fHadroness(-1)
+{
+    fName  = name  ? name  : "MHadroness";
+    fTitle = title ? title : "Storage container for hadroness value";
+}
+
+// --------------------------------------------------------------------------
+//
+// Reset hadroness, -1 indicates: invalid value
+//
+void MHadroness::Reset()
+{
+    fHadroness = -1;
+}
+
+// --------------------------------------------------------------------------
+//
+// Print the hillas Parameters to *fLog
+//
+void MHadroness::Print(Option_t *) const
+{
+    *fLog << all << GetDescriptor() << ": Hadroness = " << fHadroness << endl;
+}
+
Index: trunk/MagicSoft/Mars/manalysis/MHadroness.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MHadroness.h	(revision 1337)
+++ trunk/MagicSoft/Mars/manalysis/MHadroness.h	(revision 1337)
@@ -0,0 +1,26 @@
+#ifndef MARS_MHadroness
+#define MARS_MHadroness
+
+#ifndef MARS_MParContainer
+#include "MParContainer.h"
+#endif
+
+class MHadroness : public MParContainer
+{
+private:
+    Double_t fHadroness; // [0,1] Hadroness
+
+public:
+    MHadroness(const char *name=NULL, const char *title=NULL);
+
+    void Reset();
+
+    virtual void Print(Option_t *opt=NULL) const;
+
+    Double_t GetHadroness() const { return fHadroness; }
+    void     SetHadroness(Double_t h) { fHadroness = h; }
+
+    ClassDef(MHadroness, 1) // Storage Container for the hadroness
+};
+
+#endif
Index: trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc	(revision 1337)
+++ trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.cc	(revision 1337)
@@ -0,0 +1,161 @@
+/* ======================================================================== *\
+!
+! *
+! * 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, 5/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
+!   Author(s): Rudy Bock, 5/2002 <mailto:rkb@mppmu.mpg.de>
+!
+!   Copyright: MAGIC Software Development, 2000-2002
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// 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.
+//
+////////////////////////////////////////////////////////////////////////////
+#include "MMultiDimDistCalc.h"
+
+#include "MHMatrix.h" // must be before MLogManip.h
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MParList.h"
+#include "MDataChain.h"
+
+#include "MHadroness.h"
+
+ClassImp(MMultiDimDistCalc);
+
+// --------------------------------------------------------------------------
+//
+// Setup histograms and the number of distances which are used for
+// avaraging in CalcDist
+//
+MMultiDimDistCalc::MMultiDimDistCalc(Int_t num, const char *name, const char *title)
+    : fNum(num)
+{
+    //
+    //   set the name and title of this object
+    //
+    fName  = name  ? name  : "MMultiDimDistCalc";
+    fTitle = title ? title : "Composite Probabilities Loop 1/2";
+
+    fData = new TList;
+    fData->SetOwner();
+}
+
+// --------------------------------------------------------------------------
+//
+// Delete the data chains
+//
+MMultiDimDistCalc::~MMultiDimDistCalc()
+{
+    delete fData;
+}
+
+// --------------------------------------------------------------------------
+//
+// Needs:
+//  - MatrixGammas  [MHMatrix]
+//  - MatrixHadrons {MHMatrix]
+//  - MHadroness
+//  - all data containers used to build the matrixes
+//
+// The matrix object can be filles using MFillH. And must be of the same
+// number of columns (with the same meaning).
+//
+Bool_t MMultiDimDistCalc::PreProcess(MParList *plist)
+{
+    fMGammas = (MHMatrix*)plist->FindObject("MatrixGammas", "MHMatrix");
+    if (!fMGammas)
+    {
+        *fLog << err << dbginf << "MatrixGammas [MHMatrix] not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    fMHadrons = (MHMatrix*)plist->FindObject("MatrixHadrons", "MHMatrix");
+    if (!fMHadrons)
+    {
+        *fLog << err << dbginf << "MatrixHadrons [MHMatrix] not found... aborting." << endl;
+        return kFALSE;
+    }
+
+    if (fMGammas->GetM().GetNcols() != fMHadrons->GetM().GetNcols())
+    {
+        *fLog << err << dbginf << "Error matrices have different numbers of columns... aborting." << endl;
+        return kFALSE;
+    }
+
+    TIter Next(fMGammas->GetRules());
+    TObject *data=NULL;
+    while ((data=Next()))
+    {
+        MDataChain *chain = new MDataChain(data->GetName());
+        if (!chain->PreProcess(plist))
+        {
+            delete chain;
+            return kFALSE;
+        }
+        fData->Add(chain);
+    }
+
+    fHadroness = (MHadroness*)plist->FindCreateObj("MHadroness");
+    if (!fHadroness)
+        return kFALSE;
+
+    return kTRUE;
+}
+
+// --------------------------------------------------------------------------
+//
+// Evaluates the avarage over the fNum shortest distances in a
+// multidimensional space to a matrix (set of vectors) describing
+// gammas and hadrons.
+// The hadroness of the event is defines as the avarage distance to the
+// set of gammas (dg) divided by the avarage distance to the set of
+// hadrons (dh). Because this value is in teh range [0, inf] it is
+// transformed into [0,1] by:
+//  H = exp(-dh/dg);
+//
+Bool_t MMultiDimDistCalc::Process()
+{
+    const Double_t ncols = fMGammas->GetM().GetNcols();
+
+    TVector event(ncols);
+
+    Int_t n=0;
+    TIter Next(fData);
+    MData *data = NULL;
+    while ((data=(MData*)Next()))
+        event(n++) = data->GetValue();
+
+    Double_t dg = fMGammas->CalcDist(event, fNum);
+    Double_t dh = fMHadrons->CalcDist(event, fNum);
+
+    fHadroness->SetHadroness(exp(-dh/dg));
+
+    return kTRUE;
+}
+
Index: trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.h	(revision 1337)
+++ trunk/MagicSoft/Mars/manalysis/MMultiDimDistCalc.h	(revision 1337)
@@ -0,0 +1,34 @@
+#ifndef MARS_MMultiDimDistCalc
+#define MARS_MMultiDimDistCalc
+
+#ifndef MARS_MTask
+#include "MTask.h"
+#endif
+
+class MHMatrix;
+class MParList;
+class MHadroness;
+
+class MMultiDimDistCalc : public MTask
+{
+private:
+    Int_t fNum;             // number of distances used for an avarage
+
+    MHMatrix   *fMGammas;   //! Gammas describing matrix
+    MHMatrix   *fMHadrons;  //! Hadrons (non gammas) describing matrix
+
+    MHadroness *fHadroness; //! Output container for calculated hadroness
+
+    TList *fData;           //! Used to store the MDataChains to get the event values
+
+public:
+    MMultiDimDistCalc(Int_t num, const char *name=NULL, const char *title=NULL);
+    ~MMultiDimDistCalc();
+
+    Bool_t PreProcess(MParList *plist);
+    Bool_t Process();
+
+    ClassDef(MMultiDimDistCalc, 1) // Task to calculate multidimensional distances
+};
+
+#endif
Index: trunk/MagicSoft/Mars/manalysis/Makefile
===================================================================
--- trunk/MagicSoft/Mars/manalysis/Makefile	(revision 1325)
+++ trunk/MagicSoft/Mars/manalysis/Makefile	(revision 1337)
@@ -22,5 +22,5 @@
 #  connect the include files defined in the config.mk file
 #
-INCLUDES = -I. -I../mbase -I../mgui -I../mmc -I../mraw
+INCLUDES = -I. -I../mbase -I../mgui -I../mmc -I../mraw -I../mdata -I../mhist
 
 #------------------------------------------------------------------------------
@@ -37,4 +37,7 @@
            MEnergyEstimate.cc \
            MSrcPosCam.cc \
+           MHadroness.cc \
+           MCompProbCalc.cc \
+           MMultiDimDistCalc.cc \
            MHillas.cc \
            MHillasSrc.cc \
