Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 7696)
+++ trunk/MagicSoft/Mars/Changelog	(revision 7697)
@@ -46,4 +46,11 @@
      - removed the obsolste dereferencing from the call to FindBestSplit
      - added some const-qualifiers in funciton calls
+     - make a copy of tclasspop in BuildTree to be able to give the
+       array as a const qualified reference. It is not used at any other
+       place
+     - in TreeHad first get the pointers to the vector with the data to
+       get rid of the range check done by root. This has also the advantage
+       that all TreeHad member function can be unified into a single
+       member function
 
    * mhflux/MAlphaFitter.cc:
@@ -51,4 +58,18 @@
        in the case of off-data. This did in no means effect the result,
        just the performance.
+
+   * mhbase/MH3.cc:
+     - convert the options ToLower case first before checking
+
+   * mjtrain/MJTrainRanForest.[h,cc]:
+     - added AddPar member function
+     - added fPreTasks and fPostTasks
+     - added fEnableWeights
+     - added member functions suporting setting pre- and posttasks
+       and weights
+
+   * mtools/MTFillMatrix.h:
+     - added new member function to clear the fPreCuts, fPreTasks and
+       fPostTasks lists
 
 
Index: trunk/MagicSoft/Mars/NEWS
===================================================================
--- trunk/MagicSoft/Mars/NEWS	(revision 7696)
+++ trunk/MagicSoft/Mars/NEWS	(revision 7697)
@@ -12,4 +12,6 @@
    - general: Added a missing feature in the MFilterLIst class which
      prevented MFEnergySlope from working correctly in trainenergy.C
+
+   - general: Accelerated the random forest training and usage a bit
 
    - merpp: Adapted to new raw data file format version 6
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 7696)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 7697)
@@ -35,7 +35,8 @@
 #include "MLogManip.h"
 
+#include "MF.h"
+#include "MParameterCalc.h"
+
 #include "MStatusDisplay.h"
-
-#include "MF.h"
 
 ClassImp(MJTrainRanForest);
@@ -45,6 +46,6 @@
 //------------------------------------------------------------------------
 //
-// 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
+// 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)
 //
@@ -52,14 +53,30 @@
 {
     MFilter *f = new MF(rule);
-    f->SetBit(kCanDelete);
-    AddCut(l, f);
+    f->SetBit(kCanDelete); //FIXME!!!! Why does not any other list delete it???
+    Add(l, f);
 }
 
 //------------------------------------------------------------------------
 //
-// Add a cut which is used to fill the matrix. If kCanDelete is set
+// Add an additional parameter (MParameterCalc), eg "0.5", "MWeight"
+// The default container name is "MWeight"
+//
+void MJTrainRanForest::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 MJTrainRanForest::AddCut(TList &l, MFilter *f)
+void MJTrainRanForest::Add(TList &l, MTask *f)
 {
     l.Add(f);
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.h
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.h	(revision 7696)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.h	(revision 7697)
@@ -6,4 +6,5 @@
 #endif
 
+class MTask;
 class MFilter;
 
@@ -12,4 +13,5 @@
 protected:
     Bool_t fDebug;
+    Bool_t fEnableWeights;
 
     TList fRules;
@@ -18,4 +20,6 @@
     TList fTrainCuts;
     TList fTestCuts;
+    TList fPreTasks;
+    TList fPostTasks;
 
     UShort_t fNumTrees;
@@ -26,8 +30,9 @@
 
     void AddCut(TList &l, const char *rule);
-    void AddCut(TList &l, MFilter *f);
+    void AddPar(TList &l, const char *rule, const char *name);
+    void Add(TList &l, MTask *f);
 
 public:
-    MJTrainRanForest() : fDebug(kFALSE)
+    MJTrainRanForest() : fDebug(kFALSE), fEnableWeights(kFALSE)
     {
         fNumTrees = 100; //100
@@ -36,18 +41,29 @@
     }
 
-    void SetDebug(Bool_t b=kTRUE) { fDebug = b; }
+    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)         { AddCut(fPreCuts, f); }
+    void AddPreCut(MFilter *f)         { Add(fPreCuts, (MTask*)(f)); }
 
     void AddTrainCut(const char *rule) { AddCut(fTrainCuts, rule); }
-    void AddTrainCut(MFilter *f)       { AddCut(fTrainCuts, f); }
+    void AddTrainCut(MFilter *f)       { Add(fTrainCuts, (MTask*)(f)); }
 
     void AddTestCut(const char *rule)  { AddCut(fTestCuts, rule); }
-    void AddTestCut(MFilter *f)        { AddCut(fTestCuts, f); }
+    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; }
+    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);
Index: trunk/MagicSoft/Mars/mranforest/MRanTree.cc
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanTree.cc	(revision 7696)
+++ trunk/MagicSoft/Mars/mranforest/MRanTree.cc	(revision 7697)
@@ -99,5 +99,5 @@
 
 void MRanTree::GrowTree(TMatrix *mat, const MArrayF &hadtrue, const MArrayI &idclass,
-                        MArrayI &datasort, const MArrayI &datarang, MArrayF &tclasspop,
+                        MArrayI &datasort, const MArrayI &datarang, const MArrayF &tclasspop,
                         const Float_t &mean, const Float_t &square, const MArrayI &jinbag, const MArrayF &winbag,
                         const int nclass)
@@ -192,5 +192,5 @@
     for (Int_t mt=0; mt<fNumTry; mt++)
     {
-        const Int_t mvar=Int_t(gRandom->Rndm()*mdim);
+        const Int_t mvar= gRandom->Integer(mdim);
         const Int_t mn  = mvar*numdata;
 
@@ -201,5 +201,5 @@
         Double_t rld=0;
 
-        MArrayF wl(nclass); // left node //nclass
+        MArrayF wl(nclass);     // left node //nclass
         MArrayF wr(tclasspop);  // right node//nclass
 
@@ -212,5 +212,5 @@
 
             // do classification, Gini index as split rule
-            rln+=u*(2*wl[k]+u);
+            rln+=u*( 2*wl[k]+u);
             rrn+=u*(-2*wr[k]+u);
 
@@ -445,5 +445,5 @@
 void MRanTree::BuildTree(MArrayI &datasort,const MArrayI &datarang, const MArrayF &hadtrue,
                          const MArrayI &idclass, MArrayI &bestsplit, MArrayI &bestsplitnext,
-                         MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
+                         const MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
                          Int_t ninbag, const int nclass)
 {
@@ -481,4 +481,5 @@
     MArrayF mean(nrnodes);
     MArrayF square(nrnodes);
+    MArrayF lclasspop(tclasspop);
 
     mean[0]=tmean;
@@ -502,5 +503,5 @@
 
           for (Int_t j=0;j<nclass;j++)
-              tclasspop[j]=classpop[j*nrnodes+kbuild];
+              lclasspop[j]=classpop[j*nrnodes+kbuild];
 
           Int_t msplit, nbest;
@@ -508,5 +509,5 @@
 
           if ((this->*FindBestSplit)(datasort,datarang,hadtrue,idclass,ndstart,
-                                     ndend, tclasspop,mean[kbuild],square[kbuild],msplit,decsplit,
+                                     ndend, lclasspop,mean[kbuild],square[kbuild],msplit,decsplit,
                                      nbest,winbag,nclass))
           {
@@ -621,7 +622,6 @@
 }
 
-Double_t MRanTree::TreeHad(const TVector &event)
-{
-    Int_t kt=0;
+Double_t MRanTree::TreeHad(const Float_t *evt)
+{
     // to optimize on storage space node status and node class
     // are coded into fBestVar:
@@ -631,36 +631,31 @@
     // hadronness assigned to node kt = fBestSplit[kt]
 
-    for (Int_t k=0;k<fNumNodes;k++)
-    {
-        if (fBestVar[kt]<0)
+    // To get rid of the range check of the root classes
+    const Float_t *split = fBestSplit.GetArray();
+    const Int_t   *map1  = fTreeMap1.GetArray();
+    const Int_t   *map2  = fTreeMap2.GetArray();
+    const Int_t   *best  = fBestVar.GetArray();
+
+    Int_t kt=0;
+    for (Int_t k=0; k<fNumNodes; k++)
+    {
+        if (best[kt]<0)
             break;
 
-        const Int_t m=fBestVar[kt];
-        kt = event(m)<=fBestSplit[kt] ? fTreeMap1[kt] : fTreeMap2[kt];
-    }
-
-    return fBestSplit[kt];
+        const Int_t m=best[kt];
+        kt = evt[m]<=split[kt] ? map1[kt] : map2[kt];
+    }
+
+    return split[kt];
+}
+
+Double_t MRanTree::TreeHad(const TVector &event)
+{
+    return TreeHad(event.GetMatrixArray());
 }
 
 Double_t MRanTree::TreeHad(const TMatrixRow &event)
 {
-    Int_t kt=0;
-    // to optimize on storage space node status and node class
-    // are coded into fBestVar:
-    // status of node kt = TMath::Sign(1,fBestVar[kt])
-    // class  of node kt = fBestVar[kt]+2 (class defined by larger
-    //  node population, actually not used)
-    // hadronness assigned to node kt = fBestSplit[kt]
-
-    for (Int_t k=0;k<fNumNodes;k++)
-    {
-        if (fBestVar[kt]<0)
-            break;
-
-        const Int_t m=fBestVar[kt];
-        kt = event(m)<=fBestSplit[kt] ? fTreeMap1[kt] : fTreeMap2[kt];
-    }
-
-    return fBestSplit[kt];
+    return TreeHad(event.GetPtr());
 }
 
Index: trunk/MagicSoft/Mars/mranforest/MRanTree.h
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRanTree.h	(revision 7696)
+++ trunk/MagicSoft/Mars/mranforest/MRanTree.h	(revision 7697)
@@ -45,4 +45,5 @@
          Int_t &, const MArrayF &, const int); //!
 
+    Double_t TreeHad(const Float_t *evt);
 
     int FindBestSplitGini(const MArrayI &datasort, const MArrayI &datarang,
@@ -66,5 +67,5 @@
     void BuildTree(MArrayI &datasort, const MArrayI &datarang, const MArrayF &hadtrue,
                    const MArrayI &idclass,MArrayI &bestsplit,MArrayI &bestsplitnext,
-                   MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
+                   const MArrayF &tclasspop, const Float_t &tmean, const Float_t &tsquare, const MArrayF &winbag,
                    Int_t ninbag, const int nclass);
 
@@ -97,5 +98,5 @@
     // functions used in tree growing process
     void GrowTree(TMatrix *mat, const MArrayF &hadtrue, const MArrayI &idclass,
-                  MArrayI &datasort, const MArrayI &datarang,MArrayF &tclasspop,
+                  MArrayI &datasort, const MArrayI &datarang,const MArrayF &tclasspop,
                   const Float_t &mean, const Float_t &square, const MArrayI &jinbag, const MArrayF &winbag,
                   const int nclass);
Index: trunk/MagicSoft/Mars/mtools/MTFillMatrix.h
===================================================================
--- trunk/MagicSoft/Mars/mtools/MTFillMatrix.h	(revision 7696)
+++ trunk/MagicSoft/Mars/mtools/MTFillMatrix.h	(revision 7697)
@@ -90,4 +90,8 @@
     void AddPostTasks(const TList &list);
 
+    void ClearPreCuts()   { fPreCuts.Clear(); }
+    void ClearPreTasks()  { fPreTasks.Clear(); }
+    void ClearPostTasks() { fPostTasks.Clear(); }
+
     Bool_t Process(const MParList &plist=MParList());
     Bool_t WriteMatrix1(const TString &fname) const { return WriteMatrix(fDestMatrix1, fname, 1); }
