Index: trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc	(revision 7170)
+++ trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.cc	(revision 7178)
@@ -69,9 +69,15 @@
 //
 MRFEnergyEst::MRFEnergyEst(const char *name, const char *title)
-    : fNumTrees(-1), fNumTry(-1), fNdSize(-1), fDebug(kFALSE),
-    fData(0), fEnergyEst(0), fTestMatrix(0), fEstimationMode(kMean)
+    : fDebug(kFALSE), fData(0), fEnergyEst(0),
+    fNumTrees(-1), fNumTry(-1), fNdSize(-1), 
+    fTestMatrix(0), fEstimationMode(kMean)
 {
     fName  = name  ? name  : gsDefName.Data();
     fTitle = title ? title : gsDefTitle.Data();
+}
+
+MRFEnergyEst::~MRFEnergyEst()
+{
+    fEForests.Delete();
 }
 
@@ -163,7 +169,4 @@
         MHMatrix matrix1(mat1, "MatrixHadrons");
         MHMatrix matrix0(mat0, "MatrixGammas");
-
-        //matrix1.AddColumns(&usedrules);
-        //matrix0.AddColumns(&usedrules);
 
         // training of RF
@@ -191,11 +194,11 @@
             gLog.SetNullOutput(kFALSE);
 
-        const Double_t E = (log10(grid[ie])+log10(grid[ie+1]))/2;
+        // Calculate bin center
+        const Double_t E = (TMath::Log10(grid[ie])+TMath::Log10(grid[ie+1]))/2;
 
         // save whole forest
         MRanForest *forest=(MRanForest*)plist.FindObject("MRanForest");
-        const TString title = Form("%.10f", E);
-        forest->SetTitle(title);
-        forest->Write(title);
+        forest->SetUserVal(E);
+        forest->Write(Form("%.10f", E));
     }
 
@@ -203,6 +206,4 @@
     usedrules.Write("rules");
 
-    fileRF.Close();
-
     return kTRUE;
 }
@@ -210,5 +211,5 @@
 Int_t MRFEnergyEst::ReadForests(MParList &plist)
 {
-    TFile fileRF(fFileName,"read");
+    TFile fileRF(fFileName, "read");
     if (!fileRF.IsOpen())
     {
@@ -218,6 +219,4 @@
 
     fEForests.Delete();
-
-    Int_t i=0;
 
     TIter Next(fileRF.GetListOfKeys());
@@ -230,9 +229,5 @@
             continue;
 
-        forest->SetTitle(o->GetTitle());
-        forest->SetBit(kCanDelete);
-
-        fBinning.Set(i+1);
-        fBinning[i++] = atof(o->GetTitle());
+        forest->SetUserVal(atof(o->GetName()));
 
         fEForests.Add(forest);
@@ -283,6 +278,10 @@
 //
 //
+#include <TGraph.h>
+#include <TF1.h>
 Int_t MRFEnergyEst::Process()
 {
+    static TF1 f1("f1", "gaus");
+
     TVector event;
     if (fTestMatrix)
@@ -291,20 +290,25 @@
         *fData >> event;
 
-    Double_t eest = 0;
-    Double_t hsum = 0;
+    Double_t sume = 0;
+    Double_t sumh = 0;
     Double_t maxh = 0;
     Double_t maxe = 0;
 
-    Int_t i=0;
+    Double_t max  = -1e10;
+    Double_t min  =  1e10;
+
+    //TH1C h("", "", fEForests.GetSize(), 0, 1);
 
     TIter Next(&fEForests);
     MRanForest *rf = 0;
+
+    TGraph g;
     while ((rf=(MRanForest*)Next()))
     {
         const Double_t h = rf->CalcHadroness(event);
-        const Double_t e = fBinning[i++];
-
-        hsum += h;
-        eest += e*h;
+        const Double_t e = rf->GetUserVal();
+        g.SetPoint(g.GetN(), e, h);
+        sume += e*h;
+        sumh += h;
         if (h>maxh)
         {
@@ -312,4 +316,8 @@
             maxe = e;
         }
+        if (e>max)
+            max = e;
+        if (e<min)
+            min = e;
     }
 
@@ -317,9 +325,17 @@
     {
     case kMean:
-        fEnergyEst->SetVal(pow(10, eest/hsum));
+        fEnergyEst->SetVal(pow(10, sume/sumh));
         break;
     case kMaximum:
         fEnergyEst->SetVal(pow(10, maxe));
         break;
+    case kFit:
+        f1.SetParameter(0, maxh);
+        f1.SetParameter(1, maxe);
+        f1.SetParameter(2, 0.125);
+        g.Fit(&f1, "Q0N");
+        fEnergyEst->SetVal(pow(10, f1.GetParameter(1)));
+        break;
+
     }
     fEnergyEst->SetReadyToSave();
@@ -353,4 +369,6 @@
         if (txt==(TString)"maximum")
             fEstimationMode = kMaximum;
+        if (txt==(TString)"fit")
+            fEstimationMode = kFit;
         rc = kTRUE;
     }
Index: trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h
===================================================================
--- trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h	(revision 7170)
+++ trunk/MagicSoft/Mars/mranforest/MRFEnergyEst.h	(revision 7178)
@@ -23,49 +23,54 @@
     {
         kMean,
-        kMaximum
+        kMaximum,
+        kFit
     };
 private:
-    Int_t        fNumTrees;  // Training parameters
-    Int_t        fNumTry;    // Training parameters
-    Int_t        fNdSize;    // Training parameters
+    Bool_t       fDebug;      // Debugging of eventloop while training on/off
 
-    Bool_t       fDebug;     // Debugging of eventloop while training on/off
-
-    TString      fFileName;
-    TObjArray    fEForests;
+    TString      fFileName;   // File name to forest
+    TObjArray    fEForests;   // List of forests
 
     MDataArray  *fData;       //! Used to store the MDataChains to get the event values
     MParameterD *fEnergyEst;  //! Used to storeestimated energy
 
-    MHMatrix *fTestMatrix;
+    Int_t        fNumTrees;   //! Training parameters
+    Int_t        fNumTry;     //! Training parameters
+    Int_t        fNdSize;     //! Training parameters
 
-    TArrayD   fBinning;
+    MHMatrix    *fTestMatrix; //! Test Matrix
 
     EstimationMode_t fEstimationMode;
 
+    // MRFEnergyEst
+    Int_t ReadForests(MParList &plist);
+
+    // MTask
     Int_t PreProcess(MParList *plist);
     Int_t Process();
 
-    Int_t ReadForests(MParList &plist);
-
+    // MParContainer
     Int_t ReadEnv(const TEnv &env, TString prefix, Bool_t print);
 
 public:
     MRFEnergyEst(const char *name=NULL, const char *title=NULL);
+    ~MRFEnergyEst();
 
+    // Setter for estimation
     void  SetFileName(TString str)     { fFileName = str; }
+    void  SetEstimationMode(EstimationMode_t op) { fEstimationMode = op; }
 
+    // Setter for training
     void  SetNumTrees(Int_t n=-1)      { fNumTrees = n; }
     void  SetNdSize(Int_t n=-1)        { fNdSize   = n; }
     void  SetNumTry(Int_t n=-1)        { fNumTry   = n; }
+    void  SetDebug(Bool_t b=kTRUE)     { fDebug = b; }
 
+    // Train Interface
+    Int_t Train(const MHMatrix &n, const TArrayD &grid);
+
+    // Test Interface
     void  SetTestMatrix(MHMatrix *m=0) { fTestMatrix=m; }
     void  InitMapping(MHMatrix *m=0)   { fTestMatrix=m; }
-
-    void  SetDebug(Bool_t b=kTRUE)     { fDebug = b; }
-
-    void  SetEstimationMode(EstimationMode_t op) { fEstimationMode = op; }
-
-    Int_t Train(const MHMatrix &n, const TArrayD &grid);
 
     ClassDef(MRFEnergyEst, 0) // Task
