Index: trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc	(revision 7142)
+++ trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc	(revision 7169)
@@ -70,5 +70,5 @@
 MRFEnergyEst::MRFEnergyEst(const char *name, const char *title)
     : fNumTrees(-1), fNumTry(-1), fNdSize(-1), fDebug(kFALSE),
-    fData(0), fEnergyEst(0), fTestMatrix(0)
+    fData(0), fEnergyEst(0), fTestMatrix(0), fEstimationMode(kMean)
 {
     fName  = name  ? name  : gsDefName.Data();
@@ -91,8 +91,6 @@
     }
 
-    const TMatrix &m = matrixtrain.GetM();
-
-    const Int_t ncols = m.GetNcols();
-    const Int_t nrows = m.GetNrows();
+    const Int_t ncols = matrixtrain.GetM().GetNcols();
+    const Int_t nrows = matrixtrain.GetM().GetNrows();
     if (ncols<=0 || nrows <=0)
     {
@@ -132,4 +130,5 @@
         Int_t irow0=0;
 
+        const TMatrix &m = matrixtrain.GetM();
         for (Int_t j=0; j<nrows; j++)
         {
@@ -165,6 +164,6 @@
         MHMatrix matrix0(mat0, "MatrixGammas");
 
-        matrix1.AddColumns(&usedrules);
-        matrix0.AddColumns(&usedrules);
+        //matrix1.AddColumns(&usedrules);
+        //matrix0.AddColumns(&usedrules);
 
         // training of RF
@@ -186,9 +185,9 @@
         evtloop.SetParList(&plist);
 
+        if (!evtloop.Eventloop())
+            return kFALSE;
+
         if (fDebug)
             gLog.SetNullOutput(kFALSE);
-
-        if (!evtloop.Eventloop())
-            return kFALSE;
 
         const Double_t E = (log10(grid[ie])+log10(grid[ie+1]))/2;
@@ -220,4 +219,6 @@
     fEForests.Delete();
 
+    Int_t i=0;
+
     TIter Next(fileRF.GetListOfKeys());
     TObject *o=0;
@@ -231,4 +232,7 @@
         forest->SetTitle(o->GetTitle());
         forest->SetBit(kCanDelete);
+
+        fBinning.Set(i+1);
+        fBinning[i++] = atof(o->GetTitle());
 
         fEForests.Add(forest);
@@ -289,4 +293,8 @@
     Double_t eest = 0;
     Double_t hsum = 0;
+    Double_t maxh = 0;
+    Double_t maxe = 0;
+
+    Int_t i=0;
 
     TIter Next(&fEForests);
@@ -295,11 +303,24 @@
     {
         const Double_t h = rf->CalcHadroness(event);
-        const Double_t e = atof(rf->GetTitle());
+        const Double_t e = fBinning[i++];
 
         hsum += h;
         eest += e*h;
-    }
-
-    fEnergyEst->SetVal(pow(10, eest/hsum));
+        if (h>maxh)
+        {
+            maxh = h;
+            maxe = e;
+        }
+    }
+
+    switch (fEstimationMode)
+    {
+    case kMean:
+        fEnergyEst->SetVal(pow(10, eest/hsum));
+        break;
+    case kMaximum:
+        fEnergyEst->SetVal(pow(10, maxe));
+        break;
+    }
     fEnergyEst->SetReadyToSave();
 
@@ -323,4 +344,15 @@
         SetDebug(GetEnvValue(env, prefix, "Debug", fDebug));
     }
+    if (IsEnvDefined(env, prefix, "EstimationMode", print))
+    {
+        TString txt = GetEnvValue(env, prefix, "EstimationMode", "");
+        txt = txt.Strip(TString::kBoth);
+        txt.ToLower();
+        if (txt==(TString)"mean")
+            fEstimationMode = kMean;
+        if (txt==(TString)"maximum")
+            fEstimationMode = kMaximum;
+        rc = kTRUE;
+    }
     return rc;
 }
Index: trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h	(revision 7142)
+++ trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h	(revision 7169)
@@ -9,6 +9,7 @@
 #include <TObjArray.h>
 #endif
-
-class TArrayD;
+#ifndef ROOT_TArrayD
+#include <TArrayD.h>
+#endif
 
 class MHMatrix;
@@ -18,4 +19,10 @@
 class MRFEnergyEst : public MTask
 {
+public:
+    enum EstimationMode_t
+    {
+        kMean,
+        kMaximum
+    };
 private:
     Int_t        fNumTrees;  // Training parameters
@@ -32,4 +39,8 @@
 
     MHMatrix *fTestMatrix;
+
+    TArrayD   fBinning;
+
+    EstimationMode_t fEstimationMode;
 
     Int_t PreProcess(MParList *plist);
@@ -52,5 +63,7 @@
     void  InitMapping(MHMatrix *m=0)   { fTestMatrix=m; }
 
-    void SetDebug(Bool_t b=kTRUE)      { fDebug = b; }
+    void  SetDebug(Bool_t b=kTRUE)     { fDebug = b; }
+
+    void  SetEstimationMode(EstimationMode_t op) { fEstimationMode = op; }
 
     Int_t Train(const MHMatrix &n, const TArrayD &grid);
