Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 8642)
+++ trunk/MagicSoft/Mars/Changelog	(revision 8643)
@@ -18,4 +18,34 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+
+ 2007/07/19 Thomas Bretz
+
+   * mjobs/MJCut.cc:
+     - added a sanity check for one off-position in on/off-mode
+
+   * mjoptim/MJOptimize.[h,cc]:
+     - moved the functions and data-members to set cuts etc to new
+       base class
+     - added setting of pre- and post-tasks to reading the data
+
+   * mjoptim/Makefile, mjoptim/OptimLinkDef.h:
+     - added new MJOptimizeBase
+
+   * mjtrain/MJTrainDisp.cc, mjtrain/MJTrainEnergy.cc,
+     mjtrain/MJTrainSeparation.cc:
+     - changed to use fDebug and WriteDisplay from the new base class
+
+   * mjtrain/MJTrainRanForest.[h,cc]:
+     - moved all data members and setting of pre-/posttasks/-cuts
+       to new base class
+
+   * mjtrain/Makefile:
+     - added include of mjoptim for new base class
+
+   * mjoptim/MJOptimizeBase.[h,cc]:
+     - new base class for optimization and training classes
+
+
 
 
Index: trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc	(revision 8642)
+++ trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc	(revision 8643)
@@ -111,4 +111,6 @@
 #include "MMatrixLoop.h"
 #include "MFillH.h"
+#include "MParameterCalc.h"
+#include "MContinue.h"
 
 // filters
@@ -225,5 +227,5 @@
 }
 
-MJOptimize::MJOptimize() : fDebug(-1), fNumEvents(0), fType(kSimplex), fNumMaxCalls(0), fTolerance(0), fTestTrain(0), fNameMinimizationValue("MinimizationValue")
+MJOptimize::MJOptimize() : /*fDebug(-1),*/ fNumEvents(0), fType(kSimplex), fNumMaxCalls(0), fTolerance(0), fTestTrain(0), fNameMinimizationValue("MinimizationValue")
 {
     fRules.SetOwner();
@@ -287,18 +289,4 @@
 }
 
-//------------------------------------------------------------------------
-//
-// Add a parameter used in your filters (see AddFilter) The parameter
-// index is returned,
-//
-//   Int_t idx = AddParameter("log10(MHillas.fSize)");
-//
-// The indices area starting with 0 always.
-//
-Int_t MJOptimize::AddParameter(const char *rule)
-{
-    fRules.Add(new TNamed(rule, ""));
-    return fRules.GetSize()-1;
-}
 
 //------------------------------------------------------------------------
@@ -319,27 +307,4 @@
 {
     fFilter.Add(new MFDataPhrase(rule));
-}
-
-//------------------------------------------------------------------------
-//
-// Add a cut which is used to fill the matrix, eg "MMcEvt.fOartId<1.5"
-// (The rule is applied, nit inverted: The matrix is filled with
-// the events fullfilling the condition)
-//
-void MJOptimize::AddPreCut(const char *rule)
-{
-    MFilter *f = new MFDataPhrase(rule);
-    f->SetBit(kCanDelete);
-    AddPreCut(f);
-}
-
-//------------------------------------------------------------------------
-//
-// Add a cut which is used to fill the matrix. If kCanDelete is set
-// MJOptimize takes the ownership.
-//
-void MJOptimize::AddPreCut(MFilter *f)
-{
-    fPreCuts.Add(f);
 }
 
@@ -739,16 +704,8 @@
     m->Print("cols");
 
-    //MParList parlist;
-
     //    MGeomCamMagic cam;
     //    parlist.AddToList(&cam);
 
-    MTaskList tlist;
-    parlist.Replace(&tlist);
-
-    MFillH fillh(m);
-
-    tlist.AddToList(&read);
-
+    // Setup filter and apply filter to filling of matrix
     MFilterList list;
     if (!list.AddToList(fPreCuts))
@@ -757,9 +714,21 @@
         SetupFilters(list);
     list.SetName("PreCuts");  // reset Name      set by SetupFilters
-    list.SetInverted(kFALSE); // reset inversion set by SetupFilters
-    fillh.SetFilter(&list);
-    tlist.AddToList(&list);
-
-    tlist.AddToList(&fillh);
+    list.SetInverted(kTRUE);  // reset inversion set by SetupFilters
+
+    MContinue cont(&list);
+
+    // Setup filling of matrix
+    MFillH fillh(m);
+
+    // Setup tasklist
+    MTaskList tlist;
+    parlist.Replace(&tlist);
+
+    // Create task list
+    tlist.AddToList(&read);        // read data
+    tlist.AddToList(fPreTasks);    // execute pre-filter tasks
+    tlist.AddToList(&cont);        // execute filters
+    tlist.AddToList(fPostTasks);   // execute post-filter tasks
+    tlist.AddToList(&fillh);       // Fill matrix
 
     tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
Index: trunk/MagicSoft/Mars/mjoptim/MJOptimize.h
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/MJOptimize.h	(revision 8642)
+++ trunk/MagicSoft/Mars/mjoptim/MJOptimize.h	(revision 8643)
@@ -2,6 +2,6 @@
 #define MARS_MJOptimize
 
-#ifndef MARS_MJob
-#include "MJob.h"
+#ifndef MARS_MJOptimizeBase
+#include "MJOptimizeBase.h"
 #endif
 
@@ -14,4 +14,5 @@
 class MAlphaFitter;
 
+class MTask;
 class MEvtLoop;
 class MParList;
@@ -26,5 +27,5 @@
 class MReadTree;
 
-class MJOptimize : public MJob
+class MJOptimize : public MJOptimizeBase
 {
 public:
@@ -41,8 +42,6 @@
 
 private:
-    Int_t fDebug;     // -1 no output, 0 MJOptimize output, 1 PrintStatistics output
     Int_t fNumEvents;
 
-    TList fRules;
     TList fFilter;
 
@@ -78,6 +77,4 @@
 
 protected:
-    TList   fPreCuts;
-
     TList   fNamesOn;
     TList   fNamesOff;
@@ -100,8 +97,5 @@
 
     // Interface for filter cuts
-    Int_t AddParameter(const char *rule);
     void AddFilter(const char *rule);
-    void AddPreCut(const char *rule);
-    void AddPreCut(MFilter *f);
 
     // Steering of optimization
Index: trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.cc
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.cc	(revision 8643)
+++ trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.cc	(revision 8643)
@@ -0,0 +1,111 @@
+/* ======================================================================== *\
+!
+! *
+! * 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 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
+!
+!   Copyright: MAGIC Software Development, 2005-2007
+!
+!
+\* ======================================================================== */
+
+/////////////////////////////////////////////////////////////////////////////
+//
+// MJOptimizeBase
+//
+// Base class for classes training a random forest
+//
+// The order when reading a file is:
+//   1) Execution of PreTasks  (set by user)
+//   2) Execution of PreCuts   (set by user)
+//   3) Selector               (calculated from number of requested events)
+//   4) Splitter               (if sample is split automatically in test/train)
+//   5) PostTasks              (set by user)
+//
+// The split into Pre- and PostTasks is done for speed reason. So, if
+// you calculate a vlue which is not needed for your PreCuts, you can
+// calculate it afterwards, which will speed up execution.
+//
+/////////////////////////////////////////////////////////////////////////////
+#include "MJOptimizeBase.h"
+
+#include <TFile.h>
+
+#include "MLog.h"
+#include "MLogManip.h"
+
+#include "MFDataPhrase.h"
+#include "MParameterCalc.h"
+
+#include "MStatusDisplay.h"
+
+ClassImp(MJOptimizeBase);
+
+using namespace std;
+
+//------------------------------------------------------------------------
+//
+// Add a cut which is used to fill the matrix, eg "MMcEvt.fPartId<1.5"
+// (The rule is applied, not inverted: The matrix is filled with
+// the events fullfilling the condition)
+//
+void MJOptimizeBase::AddCut(TList &l, const char *rule)
+{
+    MFilter *f = new MFDataPhrase(rule);
+    f->SetBit(kCanDelete); //FIXME!!!! Why does not any other list delete it???
+    Add(l, f);
+}
+
+//------------------------------------------------------------------------
+//
+// Add an additional parameter (MParameterCalc), eg "0.5", "MWeight"
+// The default container name is "MWeight"
+//
+void MJOptimizeBase::AddPar(TList &l, const char *rule, const char *pname)
+{
+    TString tname(pname);
+    tname += "Calc";
+
+    MParameterCalc *par = new MParameterCalc(rule, tname);
+    par->SetNameParameter(pname);
+//    par->SetBit(kCanDelete);  //FIXME!!!! MTaskList is deleting it
+    Add(l, par);
+}
+
+//------------------------------------------------------------------------
+//
+// Add a task/cut which is used to fill the matrix. If kCanDelete is set
+// MJOptimize takes the ownership.
+//
+void MJOptimizeBase::Add(TList &l, MTask *f)
+{
+    l.Add(f);
+}
+
+//------------------------------------------------------------------------
+//
+// Add a parameter used in your filters (see AddFilter) The parameter
+// index is returned,
+//
+//   Int_t idx = AddParameter("log10(MHillas.fSize)");
+//
+// The indices are starting with 0 always.
+//
+Int_t MJOptimizeBase::AddParameter(const char *rule)
+{
+    fRules.Add(new TNamed(rule, ""));
+    return fRules.GetSize()-1;
+}
Index: trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.h
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.h	(revision 8643)
+++ trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.h	(revision 8643)
@@ -0,0 +1,61 @@
+#ifndef MARS_MJOptimizeBase
+#define MARS_MJOptimizeBase
+
+#ifndef MARS_MJob
+#include "MJob.h"
+#endif
+
+class MTask;
+class MFilter;
+
+class MJOptimizeBase : public MJob
+{
+protected:
+    Int_t  fDebug;
+    Bool_t fEnableWeights;
+
+    TList fRules;
+
+    TList fPreCuts;
+    TList fTrainCuts;
+    TList fTestCuts;
+    TList fPreTasks;
+    TList fPostTasks;
+
+    void AddCut(TList &l, const char *rule);
+    void AddPar(TList &l, const char *rule, const char *name);
+    void Add(TList &l, MTask *f);
+
+public:
+    MJOptimizeBase() : fDebug(-1), fEnableWeights(kFALSE)
+    {
+    }
+
+    void AddPreTask(MTask *t)                    { Add(fPreTasks,  t); }
+    void AddPreTask(const char *rule,
+                    const char *name="MWeight")  { AddPar(fPreTasks, rule, name); }
+
+    void AddPostTask(MTask *t)                   { Add(fPostTasks, t); }
+    void AddPostTask(const char *rule,
+                     const char *name="MWeight") { AddPar(fPostTasks, rule, name); }
+
+    void SetDebug(Bool_t b=kTRUE)      { fDebug = b; }
+
+    void SetWeights(const char *rule)  { if (fEnableWeights) return; fEnableWeights=kTRUE; AddPostTask(rule); }
+    void SetWeights(MTask *t)          { if (fEnableWeights) return; fEnableWeights=kTRUE; AddPostTask(t);    }
+
+    void AddPreCut(const char *rule)   { AddCut(fPreCuts, rule); }
+    void AddPreCut(MFilter *f)         { Add(fPreCuts, (MTask*)(f)); }
+
+    void AddTrainCut(const char *rule) { AddCut(fTrainCuts, rule); }
+    void AddTrainCut(MFilter *f)       { Add(fTrainCuts, (MTask*)(f)); }
+
+    void AddTestCut(const char *rule)  { AddCut(fTestCuts, rule); }
+    void AddTestCut(MFilter *f)        { Add(fTestCuts, (MTask*)(f)); }
+
+    Int_t AddParameter(const char *rule);
+
+    ClassDef(MJOptimizeBase, 0)//Base class for all optimizations and trainings
+};
+
+#endif
Index: trunk/MagicSoft/Mars/mjoptim/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/Makefile	(revision 8642)
+++ trunk/MagicSoft/Mars/mjoptim/Makefile	(revision 8643)
@@ -29,5 +29,6 @@
 # mgeom:     MGeomCam
 
-SRCFILES = MJOptimize.cc \
+SRCFILES = MJOptimizeBase.cc \
+           MJOptimize.cc \
            MJOptimizeCuts.cc \
            MJOptimizeDisp.cc \
Index: trunk/MagicSoft/Mars/mjoptim/OptimLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/OptimLinkDef.h	(revision 8642)
+++ trunk/MagicSoft/Mars/mjoptim/OptimLinkDef.h	(revision 8643)
@@ -6,4 +6,5 @@
 
 #pragma link C++ class MJOptimize+;
+#pragma link C++ class MJOptimizeBase+;
 #pragma link C++ class MJOptimizeCuts+;
 #pragma link C++ class MJOptimizeDisp+;
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 8642)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 8643)
@@ -145,5 +145,5 @@
     rf.SetLogStream(fLog);
     rf.SetFileName(out);
-    rf.SetDebug(fDebug);
+    rf.SetDebug(fDebug>1);
     rf.SetNameOutput("Disp");
 
@@ -251,5 +251,5 @@
     hist.GetAlphaFitter().Print("result");
 
-    if (!WriteDisplay(out))
+    if (!WriteDisplay(out, "UPDATE"))
         return kFALSE;
 
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 8642)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 8643)
@@ -139,5 +139,5 @@
     rf.SetLogStream(fLog);
     rf.SetFileName(out);
-    rf.SetDebug(fDebug);
+    rf.SetDebug(fDebug>1);
     rf.SetNameOutput("MEnergyEst");
 
@@ -193,5 +193,5 @@
         return kFALSE;
 
-    if (!WriteDisplay(out))
+    if (!WriteDisplay(out, "UPDATE"))
         return kFALSE;
 
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 8642)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 8643)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2005-2006
+!   Copyright: MAGIC Software Development, 2005-2007
 !
 !
@@ -28,4 +28,15 @@
 //
 // Base class for classes training a random forest
+//
+// The order when reading a file is:
+//   1) Execution of PreTasks  (set by user)
+//   2) Execution of PreCuts   (set by user)
+//   3) Selector               (calculated from number of requested events)
+//   4) Splitter               (if sample is split automatically in test/train)
+//   5) PostTasks              (set by user)
+//
+// The split into Pre- and PostTasks is done for speed reason. So, if
+// you calculate a vlue which is not needed for your PreCuts, you can
+// calculate it afterwards, which will speed up execution.
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -45,5 +56,5 @@
 
 using namespace std;
-
+/*
 //------------------------------------------------------------------------
 //
@@ -120,2 +131,3 @@
     return kTRUE;
 }
+*/
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.h
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.h	(revision 8642)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.h	(revision 8643)
@@ -2,6 +2,6 @@
 #define MARS_MJTrainRanForest
 
-#ifndef MARS_MJob
-#include "MJob.h"
+#ifndef MARS_MJOptimizeBase
+#include "MJOptimizeBase.h"
 #endif
 
@@ -9,30 +9,13 @@
 class MFilter;
 
-class MJTrainRanForest : public MJob
+class MJTrainRanForest : public MJOptimizeBase
 {
 protected:
-    Bool_t fDebug;
-    Bool_t fEnableWeights;
-
-    TList fRules;
-
-    TList fPreCuts;
-    TList fTrainCuts;
-    TList fTestCuts;
-    TList fPreTasks;
-    TList fPostTasks;
-
     UShort_t fNumTrees;
     UShort_t fNdSize;
     UShort_t fNumTry;
 
-    Bool_t WriteDisplay(const char *fname) const;
-
-    void AddCut(TList &l, const char *rule);
-    void AddPar(TList &l, const char *rule, const char *name);
-    void Add(TList &l, MTask *f);
-
 public:
-    MJTrainRanForest() : fDebug(kFALSE), fEnableWeights(kFALSE)
+    MJTrainRanForest()
     {
         fNumTrees = 100; //100
@@ -41,31 +24,7 @@
     }
 
-    void AddPreTask(MTask *t)                    { Add(fPreTasks,  t); }
-    void AddPreTask(const char *rule,
-                    const char *name="MWeight")  { AddPar(fPreTasks, rule, name); }
-
-    void AddPostTask(MTask *t)                   { Add(fPostTasks, t); }
-    void AddPostTask(const char *rule,
-                     const char *name="MWeight") { AddPar(fPostTasks, rule, name); }
-
-    void SetDebug(Bool_t b=kTRUE)      { fDebug = b; }
-
-    void SetWeights(const char *rule)  { if (fEnableWeights) return; fEnableWeights=kTRUE; AddPostTask(rule); }
-    void SetWeights(MTask *t)          { if (fEnableWeights) return; fEnableWeights=kTRUE; AddPostTask(t);    }
-
-    void AddPreCut(const char *rule)   { AddCut(fPreCuts, rule); }
-    void AddPreCut(MFilter *f)         { Add(fPreCuts, (MTask*)(f)); }
-
-    void AddTrainCut(const char *rule) { AddCut(fTrainCuts, rule); }
-    void AddTrainCut(MFilter *f)       { Add(fTrainCuts, (MTask*)(f)); }
-
-    void AddTestCut(const char *rule)  { AddCut(fTestCuts, rule); }
-    void AddTestCut(MFilter *f)        { Add(fTestCuts, (MTask*)(f)); }
-
     void SetNumTrees(UShort_t n=100)   { fNumTrees = n; }
     void SetNdSize(UShort_t n=5)       { fNdSize   = n; }
     void SetNumTry(UShort_t n=0)       { fNumTry   = n; }
-
-    Int_t AddParameter(const char *rule);
 
     ClassDef(MJTrainRanForest, 0)//Base class for Random Forest training classes
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 8642)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 8643)
@@ -802,5 +802,5 @@
     rf.SetNumObsoleteVariables(1);
     rf.SetLastDataColumnHasWeights(fEnableWeights[kTrainOn] || fEnableWeights[kTrainOff]);
-    rf.SetDebug(fDebug);
+    rf.SetDebug(fDebug>1);
     rf.SetDisplay(fDisplay);
     rf.SetLogStream(fLog);
@@ -1039,5 +1039,5 @@
 
     // Write the display
-    if (!WriteDisplay(out))
+    if (!WriteDisplay(out, "UPDATE"))
         return kFALSE;
 
Index: trunk/MagicSoft/Mars/mjtrain/Makefile
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/Makefile	(revision 8642)
+++ trunk/MagicSoft/Mars/mjtrain/Makefile	(revision 8643)
@@ -22,5 +22,5 @@
            -I../manalysis -I../mfilter -I../mjobs -I../mtools \
            -I../mranforest -I../mhflux -I../mmc -I../mhist -I../mpointing \
-           -I../mimage
+           -I../mimage -I../mjoptim
 
 SRCFILES = MJTrainRanForest.cc \
