Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 7419)
+++ trunk/MagicSoft/Mars/Changelog	(revision 7420)
@@ -30,4 +30,32 @@
    * mfbase/MFEventSelector.cc:
      - also reset fSelRatio if totalnumber of events from file.
+
+   * mranforest/MHRanForestGini.[h,cc]:
+     - added a TPaveText containing text information to output
+
+   * mranforest/MRanForest.[h,cc]:
+     - replaced type of fClassify by Bool_t
+     - improved handling of allocated memory (still to be checked
+       further)
+
+   * mranforest/MRanForestCalc.[h,cc]:
+     - added another training type
+     - added some comments
+
+   * mranforest/MRanTree.[h,cc]:
+     - replaced type of fClassify by Bool_t
+
+   * mjtrain/MJTrainDisp.cc, mjtrain/MJTrainEnegry.cc:
+     - renamed TrainSingleRF to TrainRegression
+
+   * mjtrain/MJTrainDisp.cc:
+     - replaced training variable (hypot(dist,dca)) by dist
+
+   * mjtrain/MJTrainRanForest.cc:
+     - added some comment
+
+   * mjtrain/MJTrainSeparation.cc:
+     - updated output
+     - made it work properly
 
 
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 7420)
@@ -115,6 +115,6 @@
     MHMatrix train("Train");
     train.AddColumns(fRules);
-    //train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
-    train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
+    train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
+    //train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
 
     // ----------------------- Fill Matrix RF ----------------------
@@ -147,5 +147,5 @@
         return;
     */
-    if (!rf.TrainSingleRF(train))                  // regression (best choice)
+    if (!rf.TrainRegression(train))                  // regression (best choice)
         return kFALSE;
 
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 7420)
@@ -141,5 +141,5 @@
         return;
     */
-    if (!rf.TrainSingleRF(train))                  // regression (best choice)
+    if (!rf.TrainRegression(train))                  // regression (best choice)
         return kFALSE;
 
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 7420)
@@ -26,4 +26,6 @@
 //
 // MJTrainRanForest
+//
+// Base class for classes training a random forest
 //
 /////////////////////////////////////////////////////////////////////////////
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 7420)
@@ -30,4 +30,8 @@
 #include "MJTrainSeparation.h"
 
+#include <TH2.h>
+#include <TGraph.h>
+#include <TVirtualPad.h>
+
 #include "MHMatrix.h"
 
@@ -36,7 +40,9 @@
 
 // tools
+#include "MMath.h"
 #include "MDataSet.h"
 #include "MTFillMatrix.h"
 #include "MChisqEval.h"
+#include "MStatusDisplay.h"
 
 // eventloop
@@ -63,4 +69,5 @@
 // filter
 #include "MF.h"
+#include "MFEventSelector.h"
 #include "MFilterList.h"
 
@@ -68,4 +75,64 @@
 
 using namespace std;
+
+void MJTrainSeparation::DisplayResult(MH3 &h31, MH3 &h32)
+{
+    TH2 &g = (TH2&)h32.GetHist();
+    TH2 &h = (TH2&)h31.GetHist();
+
+    h.SetMarkerColor(kRed);
+    g.SetMarkerColor(kGreen);
+
+    const Int_t nx = h.GetNbinsX();
+    const Int_t ny = h.GetNbinsY();
+
+    gROOT->SetSelectedPad(NULL);
+
+    TGraph gr1;
+    TGraph gr2;
+    for (int x=0; x<nx; x++)
+    {
+        TH1 *hx = h.ProjectionY("H_py", x+1, x+1);
+        TH1 *gx = g.ProjectionY("G_py", x+1, x+1);
+
+        Double_t max1 = -1;
+        Double_t max2 = -1;
+        Int_t maxy1 = 0;
+        Int_t maxy2 = 0;
+        for (int y=0; y<ny; y++)
+        {
+            const Float_t s = gx->Integral(1, y+1);
+            const Float_t b = hx->Integral(1, y+1);
+            const Float_t sig1 = MMath::SignificanceLiMa(s+b, b);
+            const Float_t sig2 = s<1 ? 0 : MMath::SignificanceLiMa(s+b, b)*TMath::Log10(s);
+            if (sig1>max1)
+            {
+                maxy1 = y;
+                max1 = sig1;
+            }
+            if (sig2>max2)
+            {
+                maxy2 = y;
+                max2 = sig2;
+            }
+        }
+
+        gr1.SetPoint(x, h.GetXaxis()->GetBinCenter(x+1), h.GetYaxis()->GetBinCenter(maxy1+1));
+        gr2.SetPoint(x, h.GetXaxis()->GetBinCenter(x+1), h.GetYaxis()->GetBinCenter(maxy2+1));
+
+        delete hx;
+        delete gx;
+    }
+
+    fDisplay->AddTab("OptCut");
+    gPad->SetLogx();
+    h.DrawCopy();
+    g.DrawCopy("same");
+    gr1.SetMarkerStyle(kFullDotMedium);
+    gr1.DrawClone("LP")->SetBit(kCanDelete);
+    gr2.SetLineColor(kBlue);
+    gr2.SetMarkerStyle(kFullDotMedium);
+    gr2.DrawClone("LP")->SetBit(kCanDelete);
+}
 
 Bool_t MJTrainSeparation::Train(const char *out)
@@ -83,25 +150,18 @@
 
     // --------------------- Setup files --------------------
-    MReadMarsFile read("Events");
+    MReadMarsFile read1("Events");
     MReadMarsFile read2("Events");
     MReadMarsFile read3("Events");
-    read.DisableAutoScheme();
+    MReadMarsFile read4("Events");
+    read1.DisableAutoScheme();
     read2.DisableAutoScheme();
     read3.DisableAutoScheme();
-
-    fDataSetTrain.AddFilesOn(read);
+    read4.DisableAutoScheme();
+
+    fDataSetTrain.AddFilesOn(read1);
     fDataSetTrain.AddFilesOff(read3);
 
     fDataSetTest.AddFilesOff(read2);
-    fDataSetTest.AddFilesOn(read2);
-
-    read2.VetoBranch("MMcTrig");
-    read2.VetoBranch("MTime");
-    read2.VetoBranch("MMcRunHeader");
-    read2.VetoBranch("MMcTrigHeader");
-    read2.VetoBranch("MMcFadcHeader");
-    read2.VetoBranch("MMcCorsikaRunHeader");
-    read2.VetoBranch("MMcConfigRunHeader");
-
+    fDataSetTest.AddFilesOn(read4);
 
     // ----------------------- Setup RF ----------------------
@@ -116,4 +176,5 @@
     MParList plistx;
     plistx.AddToList(&had);
+    plistx.AddToList(this);
 
     MTFillMatrix fill;
@@ -127,5 +188,5 @@
     fill.SetName("FillGammas");
     fill.SetDestMatrix1(&train, fNumTrainOn);
-    fill.SetReader(&read);
+    fill.SetReader(&read1);
     if (!fill.Process(plistx))
         return kFALSE;
@@ -160,6 +221,7 @@
         return kFALSE;
 
+    //fDisplay = rf.GetDisplay();
+
     // --------------------- Display result ----------------------
-
     gLog.Separator("Test");
 
@@ -172,6 +234,7 @@
     plist.AddToList(&mcevt);
 
+    // ----- Setup histograms -----
     MBinning binsy(100, 0 , 1,      "BinningMH3Y", "lin");
-    MBinning binsx(100, 10, 100000, "BinningMH3X", "log");
+    MBinning binsx( 50, 10, 100000, "BinningMH3X", "log");
 
     plist.AddToList(&binsx);
@@ -183,20 +246,15 @@
     h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
 
-    MF f("MRawRunHeader.fRunType>255");
-    MFilterList list;
-    list.SetInverted();
-    list.AddToList(&f);
-
     MHHadronness hist;
 
-    MFillH fillh(&hist, "", "FillHadronness");
-    MFillH fillh1(&h31, "", "FillGammas");
-    MFillH fillh2(&h32, "", "FillBackground");
-    fillh1.SetNameTab("Gammas");
-    fillh2.SetNameTab("Background");
-
-    fillh1.SetFilter(&f);
-    fillh2.SetFilter(&list);
-
+    // ----- Setup tasks -----
+    MFillH fillh0(&hist, "", "FillHadronness");
+    MFillH fillh1(&h31);
+    MFillH fillh2(&h32);
+    fillh1.SetNameTab("Background");
+    fillh2.SetNameTab("Gammas");
+    fillh0.SetBit(MFillH::kDoNotDisplay);
+
+    // ----- Setup filter -----
     MFilterList precuts;
     precuts.AddToList(fPreCuts);
@@ -204,14 +262,22 @@
 
     MContinue c0(&precuts);
+    c0.SetName("PreCuts");
     c0.SetInverted();
 
+    MFEventSelector sel;
+    sel.SetNumSelectEvts(fNumTestOn);
+
+    MContinue c1(&sel);
+    c1.SetInverted();
+
+    // ----- Setup tasklist -----
     tlist.AddToList(&read2);
     tlist.AddToList(&c0);
+    tlist.AddToList(&c1);
     tlist.AddToList(&rf);
-    tlist.AddToList(&fillh);
-    tlist.AddToList(&list);
+    tlist.AddToList(&fillh0);
     tlist.AddToList(&fillh1);
-    tlist.AddToList(&fillh2);
-
+
+    // ----- Run eventloop on gammas -----
     MEvtLoop loop;
     loop.SetDisplay(fDisplay);
@@ -222,4 +288,16 @@
         return kFALSE;
 
+    // ----- Setup and run eventloop on background -----
+    sel.SetNumSelectEvts(fNumTestOff);
+    fillh0.ResetBit(MFillH::kDoNotDisplay);
+
+    tlist.Replace(&read4);
+    tlist.Replace(&fillh2);
+
+    if (!loop.Eventloop())
+        return kFALSE;
+
+    DisplayResult(h31, h32);
+
     if (!WriteDisplay(out))
         return kFALSE;
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.h
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.h	(revision 7419)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.h	(revision 7420)
@@ -10,4 +10,6 @@
 #endif
 
+class MH3;
+
 class MJTrainSeparation : public MJTrainRanForest
 {
@@ -19,8 +21,15 @@
     UInt_t fNumTrainOff;
 
+    UInt_t fNumTestOn;
+    UInt_t fNumTestOff;
+
+    void DisplayResult(MH3 &h31, MH3 &h32);
+
 public:
-    MJTrainSeparation() { }
+    MJTrainSeparation() :
+        fNumTrainOn((UInt_t)-1), fNumTrainOff((UInt_t)-1), fNumTestOn((UInt_t)-1), fNumTestOff((UInt_t)-1)
+    { }
 
-    void SetDataSetTrain(const MDataSet &ds, UInt_t non, UInt_t noff)
+    void SetDataSetTrain(const MDataSet &ds, UInt_t non=(UInt_t)-1, UInt_t noff=(UInt_t)-1)
     {
         ds.Copy(fDataSetTrain);
@@ -28,7 +37,9 @@
         fNumTrainOff = noff;
     }
-    void SetDataSetTest(const MDataSet &ds)
+    void SetDataSetTest(const MDataSet &ds, UInt_t non=(UInt_t)-1, UInt_t noff=(UInt_t)-1)
     {
         ds.Copy(fDataSetTest);
+        fNumTestOn = non;
+        fNumTestOff = noff;
     }
 
Index: trunk/MagicSoft/Mars/mranforest/MRanForest.cc
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanForest.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mranforest/MRanForest.cc	(revision 7420)
@@ -62,5 +62,7 @@
 // Default constructor.
 //
-MRanForest::MRanForest(const char *name, const char *title) : fClassify(1), fNumTrees(100), fNumTry(0), fNdSize(1), fRanTree(NULL), fUserVal(-1)
+MRanForest::MRanForest(const char *name, const char *title)
+    : fClassify(kTRUE), fNumTrees(100), fNumTry(0), fNdSize(1),
+    fRanTree(NULL), fRules(NULL), fMatrix(NULL), fUserVal(-1)
 {
     fName  = name  ? name  : "MRanForest";
@@ -125,4 +127,8 @@
 {
     delete fForest;
+    if (fMatrix)
+        delete fMatrix;
+    if (fRules)
+        delete fRules;
 }
 
@@ -152,9 +158,5 @@
 void MRanForest::SetWeights(const TArrayF &weights)
 {
-    const int n=weights.GetSize();
-    fWeight.Set(n);
     fWeight=weights;
-
-    return;
 }
 
@@ -175,6 +177,4 @@
     //for(int i=0;i<n;i++)
     //    *fLog<<inf<<" "<<i<<") "<<fGrid[i]<<endl;
-
-    return;
 }
 
@@ -227,23 +227,4 @@
 }
 
-/*
-Bool_t MRanForest::PreProcess(MParList *plist)
-{
-    if (!fRules)
-    {
-        *fLog << err << dbginf << "MDataArray with rules not initialized... aborting." << endl;
-        return kFALSE;
-    }
-
-    if (!fRules->PreProcess(plist))
-    {
-        *fLog << err << dbginf << "PreProcessing of MDataArray failed... aborting." << endl;
-        return kFALSE;
-    }
-
-    return kTRUE;
-}
-*/
-
 Double_t MRanForest::CalcHadroness()
 {
@@ -285,4 +266,6 @@
     // access matrix, copy last column (target) preliminarily
     // into fHadTrue
+    if (fMatrix)
+        delete fMatrix;
     fMatrix = new TMatrix(mat->GetM());
 
@@ -337,4 +320,6 @@
     }
 
+    if (fRules)
+        delete fRules;
     fRules = new MDataArray();
     fRules->Reset();
Index: trunk/MagicSoft/Mars/mranforest/MRanForest.h
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanForest.h	(revision 7419)
+++ trunk/MagicSoft/Mars/mranforest/MRanForest.h	(revision 7420)
@@ -30,5 +30,5 @@
 {
 private:
-    Int_t fClassify;
+    Bool_t fClassify;
 
     Int_t fNumTrees;       // Number of trees
@@ -83,5 +83,5 @@
     void SetNdSize(Int_t n);
 
-    void SetClassify(Int_t n){ fClassify=n; }
+    void SetClassify(Bool_t n){ fClassify=n; }
     void PrepareClasses();
 
Index: trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc	(revision 7420)
@@ -79,4 +79,15 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// ver=0: One yes/no-classification forest is trained for each bin.
+//        the yes/no classification is done using the grid
+// ver=1: One classification forest is trained. The last column contains a
+//        value which is turned into a classifier by rf itself using the grid
+// ver=2: One classification forest is trained. The last column already contains
+//        the classifier
+// ver=3: A regression forest is trained. The last column contains the
+//        classifier
+//
 Int_t MRanForestCalc::Train(const MHMatrix &matrixtrain, const TArrayD &grid, Int_t ver)
 {
@@ -117,5 +128,5 @@
 
     MDataArray rules(usedrules);
-    rules.AddEntry(ver<2?"Classification":dcol[ncols-1].GetRule());
+    rules.AddEntry(ver<3?"Classification":dcol[ncols-1].GetRule());
 
     // prepare matrix for current energy bin
@@ -133,5 +144,5 @@
         switch (ver)
         {
-        case 0: // Replace Energy Grid by classification
+        case 0: // Replace last column by a classification
             {
                 Int_t irows=0;
@@ -158,6 +169,7 @@
             break;
 
-        case 1: // Use Energy as classifier
+        case 1: // Use last column as classifier or for regression
         case 2:
+        case 3:
             for (Int_t j=0; j<nrows; j++)
                 mat(j, ncols-nobs) = matrixtrain.GetM()(j,ncols-1);
@@ -176,5 +188,5 @@
         rf.SetNumTry(fNumTry);
         rf.SetNdSize(fNdSize);
-        rf.SetClassify(ver<2 ? 1 : 0);
+        rf.SetClassify(ver<3 ? kTRUE : kFALSE);
         if (ver==1)
             rf.SetGrid(grid);
Index: trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h	(revision 7419)
+++ trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h	(revision 7420)
@@ -65,5 +65,5 @@
 
     // Train Interface
-    Int_t Train(const MHMatrix &n, const TArrayD &grid, Int_t ver=2);
+    Int_t Train(const MHMatrix &n, const TArrayD &grid, Int_t ver);
 
 public:
@@ -87,9 +87,17 @@
     Int_t TrainMultiRF(const MHMatrix &n, const TArrayD &grid)
     {
+        // One yes/no-classification forest is trained for each bin
         return Train(n, grid, 0);
     }
     Int_t TrainSingleRF(const MHMatrix &n, const TArrayD &grid=TArrayD())
     {
+        // w/o Grid: Last Column contains classifier
+        // w/  Grid: Last Column will be converted by grid into classifier
         return Train(n, grid, grid.GetSize()==0 ? 2 : 1);
+    }
+    Int_t TrainRegression(const MHMatrix &n)
+    {
+        // Use last column for regression
+        return Train(n, TArrayD(), 3);
     }
 
Index: trunk/MagicSoft/Mars/mranforest/MRanTree.cc
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanTree.cc	(revision 7419)
+++ trunk/MagicSoft/Mars/mranforest/MRanTree.cc	(revision 7420)
@@ -49,5 +49,5 @@
 // Default constructor.
 //
-MRanTree::MRanTree(const char *name, const char *title):fClassify(1),fNdSize(0), fNumTry(3)
+MRanTree::MRanTree(const char *name, const char *title):fClassify(kTRUE),fNdSize(0), fNumTry(3)
 {
 
Index: trunk/MagicSoft/Mars/mranforest/MRanTree.h
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanTree.h	(revision 7419)
+++ trunk/MagicSoft/Mars/mranforest/MRanTree.h	(revision 7420)
@@ -22,5 +22,6 @@
 {
 private:
-    Int_t fClassify;
+    Bool_t fClassify;
+
     Int_t fNdSize;
     Int_t fNumTry;
@@ -88,5 +89,5 @@
     Float_t GetGiniDec(Int_t i)  const { return fGiniDec.At(i); }
 
-    void SetClassify(Int_t n){ fClassify=n; }
+    void SetClassify(Bool_t n){ fClassify=n; }
 
     // functions used in tree growing process
