Index: trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc
===================================================================
--- trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc	(revision 8636)
+++ 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 8636)
+++ 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 8636)
+++ 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 8636)
+++ 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+;
