Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 8697)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 8698)
@@ -18,4 +18,31 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+
+ 2007/08/23 Thomas Bretz
+
+   * mhbase/MH3.[h,cc]:
+     - added new data members fBins, allowing to set bins from the outside
+     - removed obsolete SetColors
+     - set palette automatically if it is a 2D histogram (more flexible
+       solution pending)
+
+   * mhbase/MHn.cc:
+     - added comments
+     - allow setting of binning without parlist
+     - added functions to set Scale, Log, AutoRange and Sumw2
+     - delete obsolete pads, if any
+
+   * mjoptim/MJOptimizeBase.h:
+     - added new data meber fTestTasks to be executed only for testing
+     - added comments
+
+   * mjtrain/MJTrainRanForest.cc:
+     - removed obsolete code already in comments
+
+   * mranforest/MRanForestCalc.[h,cc]:
+     - added the possibility to set a function which converts the
+       result of the rf. eg "pow(10, x)"
+
 
 
Index: /trunk/MagicSoft/Mars/NEWS
===================================================================
--- /trunk/MagicSoft/Mars/NEWS	(revision 8697)
+++ /trunk/MagicSoft/Mars/NEWS	(revision 8698)
@@ -26,4 +26,8 @@
    - general: The "Status Display: " was removed from the window title
      within an Eventloop.
+
+   - general: added a new flexible histogram-class (MHn) which can be used
+     for example in testing after optimizations or training to produce
+     user defined histograms.
 
    - merpp: Merpp can read file format version 9 now. Please realize
Index: /trunk/MagicSoft/Mars/mhbase/MH3.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 8697)
+++ /trunk/MagicSoft/Mars/mhbase/MH3.cc	(revision 8698)
@@ -88,5 +88,6 @@
 // Class Version 3:
 // ----------------
-//   - Byte_t fStyleBits
+//   - Byte_t   fStyleBits
+//   + MBinning fBins[3]
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -145,4 +146,8 @@
     fData[2] = NULL;
 
+    fBins[0] = NULL;
+    fBins[1] = NULL;
+    fBins[2] = NULL;
+
     fName  = gsDefName;
     fTitle = gsDefTitle;
@@ -172,4 +177,8 @@
     fData[1] = NULL;
     fData[2] = NULL;
+
+    fBins[0] = NULL;
+    fBins[1] = NULL;
+    fBins[2] = NULL;
 
     fName  = gsDefName;
@@ -197,4 +206,8 @@
     fData[2] = NULL;
 
+    fBins[0] = NULL;
+    fBins[1] = NULL;
+    fBins[2] = NULL;
+
     switch (fDimension)
     {
@@ -233,4 +246,8 @@
     fData[2] = NULL;
 
+    fBins[0] = NULL;
+    fBins[1] = NULL;
+    fBins[2] = NULL;
+
     fName  = gsDefName;
     fTitle = gsDefTitle;
@@ -260,4 +277,8 @@
     fData[2] = new MDataPhrase(memberz);
 
+    fBins[0] = NULL;
+    fBins[1] = NULL;
+    fBins[2] = NULL;
+
     fName  = gsDefName;
     fTitle = gsDefTitle;
@@ -339,5 +360,5 @@
     {
     case 3:
-        binsz = (MBinning*)plist->FindObject(bz, "MBinning");
+        binsz = fBins[2] ? fBins[2] : (MBinning*)plist->FindObject(bz, "MBinning");
         if (!binsz)
         {
@@ -354,5 +375,5 @@
             fHist->SetBit(kIsLogz);
     case 2:
-        binsy = (MBinning*)plist->FindObject(by, "MBinning");
+        binsy = fBins[1] ? fBins[1] : (MBinning*)plist->FindObject(by, "MBinning");
         if (!binsy)
         {
@@ -369,5 +390,5 @@
             fHist->SetBit(kIsLogy);
     case 1:
-        binsx = (MBinning*)plist->FindObject(bx, "MBinning");
+        binsx = fBins[0] ? fBins[0] : (MBinning*)plist->FindObject(bx, "MBinning");
         if (!binsx)
         {
@@ -507,5 +528,4 @@
     {
         GetRangeX(*fHist, lo, hi);
-        cout << "====> " << GetName() << " " << fHist->GetName() << ": " << lo << " " << hi <<" " << fHist->GetNbinsX() <<  endl;
         fHist->GetXaxis()->SetRange(lo-2, hi+1);
     }
@@ -525,21 +545,10 @@
 }
 
-// --------------------------------------------------------------------------
-//
-// Setup a inversed deep blue sea palette for the fCenter histogram.
-//
-void MH3::SetColors() const
-{
-    // FIXME: This must be redone each time the canvas is repainted....
-    gStyle->SetPalette(51, NULL);
-    Int_t c[50];
-    for (int i=0; i<50; i++)
-        c[49-i] = gStyle->GetColorPalette(i);
-    gStyle->SetPalette(50, c);
-}
-
 void MH3::Paint(Option_t *o)
 {
     TProfile *p=0;
+
+    if (fDimension==2)
+        MH::SetPalette("pretty");
 
     const TString pfx(Form("%sProfX", fHist->GetName()));
Index: /trunk/MagicSoft/Mars/mhbase/MH3.h
===================================================================
--- /trunk/MagicSoft/Mars/mhbase/MH3.h	(revision 8697)
+++ /trunk/MagicSoft/Mars/mhbase/MH3.h	(revision 8698)
@@ -11,4 +11,5 @@
 class TH1;
 class MData;
+class MBinning;
 
 class MH3 : public MH
@@ -23,4 +24,5 @@
     TH1        *fHist;           // Histogram to fill
     MData      *fData[3];        // Object from which the data is filled
+    MBinning   *fBins[3];        // Binning set omitting the parlist access
     Double_t    fScale[3];       // Scale for the three axis (eg unit)
     Byte_t      fStyleBits;      // Set the range of a histogram automatically in Finalize
@@ -58,4 +60,9 @@
     void SetAutoRange(Bool_t x=kTRUE, Bool_t y=kTRUE, Bool_t z=kTRUE) { SetAutoRangeX(x); SetAutoRangeY(y); SetAutoRangeZ(z); }
 
+    void SetBinningX(MBinning *x) { fBins[0] = x; }
+    void SetBinningY(MBinning *y) { fBins[1] = y; }
+    void SetBinningZ(MBinning *z) { fBins[2] = z; }
+    void SetBinnings(MBinning *x=0, MBinning *y=0, MBinning *z=0) { SetBinningX(x); SetBinningY(y); SetBinningZ(z); }
+
     void Sumw2() const { if (fHist) fHist->Sumw2(); }
 
@@ -90,5 +97,4 @@
     void SetTitle(const char *title);
 
-    void SetColors() const;
     void Draw(Option_t *opt=NULL);
     void Paint(Option_t *opt="");
Index: /trunk/MagicSoft/Mars/mhbase/MHn.cc
===================================================================
--- /trunk/MagicSoft/Mars/mhbase/MHn.cc	(revision 8697)
+++ /trunk/MagicSoft/Mars/mhbase/MHn.cc	(revision 8698)
@@ -27,56 +27,103 @@
 // MHn
 //
-//   +-----------------+
-//   |1                |
-//   |                 |
-//   |                 |
-//   |                 |
-//   |                 |
-//   +-----------------+
-//
-//   +-----------------+      +--------+--------+
-//   |1                |      |1       |2       |
-//   |                 |      |        |        |
-//   +-----------------+      |        |        |
-//   |2                |      |        |        |
-//   |                 |      |        |        |
-//   +-----------------+      +--------+--------+
-//
-//   +--------+--------+      +-----+-----+-----+
-//   |1       |2       |      |1    |3          |
-//   |        |        |      |     |           |
-//   +--------+--------+      +-----+           +
-//   |3       |        |      |2    |           |
-//   |        |        |      |     |           |
-//   +--------+--------+      +-----+-----+-----+
-//
-//   +--------+--------+      +------+----------+
-//   |1       |2       |      |1     |4         |
-//   |        |        |      +------+          |
-//   +--------+--------+      |2     |          |
-//   |3       |4       |      +------+          |
-//   |        |        |      |3     |          |
-//   +--------+--------+      +------+----------+
-//
-//   +-----+-----+-----+      +--------+--------+
-//   |1    |2    |3    |      |1       |2       |
-//   |     |     |     |      +--------+--------|
-//   +-----+-----+-----+      |3       |4       |
-//   |4    |5    |     |      +--------+        |
-//   |     |     |     |      |5       |        |
-//   +-----+-----+-----+      +--------+--------+
-//
-//   +-----+-----+-----+      +--------+--------+
-//   |1    |2    |3    |      |1       |2       |
-//   |     |     |     |      +--------+--------+
-//   +-----+-----+-----+      |3       |4       |
-//   |4    |5    |6    |      +--------+--------+
-//   |     |     |     |      |5       |6       |
-//   +-----+-----+-----+      +--------+--------+
+//
+// Initialization
+// --------------
+//
+// MHn is a histogram class which derives from MH as all Mars histogram
+// classes do, i.e. to fill the histogram use MFillH. (Example below)
+//
+// After instantisation of MHn add the histograms of your interest using
+// AddHist. Before the next AddHist is called you can now set the options
+// of your histogram using InitName, InitTitle, SetLog, SetAutoRange,
+// SetScale and SetDrawOption.
+//
+//
+// Layout
+// ------
+//
+// The layout of the histograms on the screen depends on the number of
+// initialized histograms and the option SetLayout. For details see below.
+//
+//  SetLayout(MHn::kSimple)
+//       < default >          SetLayout(MHn::kComplex)
+// ========================= ==========================
+//    +-----------------+
+//    |1                |
+//    |                 |
+//    |                 |
+//    |                 |
+//    |                 |
+//    +-----------------+
+//
+//    +-----------------+        +--------+--------+
+//    |1                |        |1       |2       |
+//    |                 |        |        |        |
+//    +-----------------+        |        |        |
+//    |2                |        |        |        |
+//    |                 |        |        |        |
+//    +-----------------+        +--------+--------+
+//
+//    +--------+--------+        +-----+-----+-----+
+//    |1       |2       |        |1    |3          |
+//    |        |        |        |     |           |
+//    +--------+--------+        +-----+           +
+//    |3       |        |        |2    |           |
+//    |        |        |        |     |           |
+//    +--------+--------+        +-----+-----+-----+
+//
+//    +--------+--------+        +------+----------+
+//    |1       |2       |        |1     |4         |
+//    |        |        |        +------+          |
+//    +--------+--------+        |2     |          |
+//    |3       |4       |        +------+          |
+//    |        |        |        |3     |          |
+//    +--------+--------+        +------+----------+
+//
+//    +-----+-----+-----+        +--------+--------+
+//    |1    |2    |3    |        |1       |2       |
+//    |     |     |     |        +--------+--------|
+//    +-----+-----+-----+        |3       |4       |
+//    |4    |5    |     |        +--------+        |
+//    |     |     |     |        |5       |        |
+//    +-----+-----+-----+        +--------+--------+
+//
+//    +-----+-----+-----+        +--------+--------+
+//    |1    |2    |3    |        |1       |2       |
+//    |     |     |     |        +--------+--------+
+//    +-----+-----+-----+        |3       |4       |
+//    |4    |5    |6    |        +--------+--------+
+//    |     |     |     |        |5       |6       |
+//    +-----+-----+-----+        +--------+--------+
 //
 //
 // For example:
-//   MHn myhist
-//   ...
+// ------------
+//    // Instatiate the MHn class with default name and title
+//    MHn hres("Energy2", "Energy Residual (lg E_{est} - lg E_{mc})");
+//
+//    // Initialize your first histogram (here a 2D histogram with x- and y-axis)
+//    hres.AddHist("MMcEvt.fEnergy", "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)");
+//    // Initialize the name of the histogram (ResEmc) and the title of the
+//    // binnings ((here BinningEnergy for x-axis and BinningEnergyResidual for y-axis)
+//    hres.InitName("ResEmc;Energy;EnergyResidual");
+//    // Initialize the title of the histogram and the axis titles
+//    hres.InitTitle(";E_{mc} [GeV];\\Delta lg E;");
+//    // Initialize the draw option for the histogram
+//    hres.SetDrawOption("colz profx");
+//    // for more details on the options and more options see below
+//    // or the class reference of MH3
+//
+//    // Initialize a second histogram
+//    hres.AddHist("MPointingPos.fZd", "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)");
+//    hres.InitName("ResTheta;Theta;EnergyResidual");
+//    hres.InitTitle(";Zd [\\circ];\\Delta lg E;");
+//    hres.SetDrawOption("colz profx");
+//
+//    // Note that only AddHist is mandatory. All other options can be omitted
+//
+//    // Initialize the filling task which can now be added to the tasklist
+//    MFillH fill(&hres);
+//    ...
 //
 /////////////////////////////////////////////////////////////////////////////
@@ -268,4 +315,33 @@
 
     return kTRUE;
+}
+
+void MHn::SetScale(Double_t x, Double_t y, Double_t z) const
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->SetScale(x, y, z);
+}
+
+void MHn::SetLog(Bool_t x, Bool_t y, Bool_t z) const
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->SetLog(x, y, z);
+}
+void MHn::SetAutoRange(Bool_t x, Bool_t y, Bool_t z) const
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->SetAutoRange(x, y, z);
+}
+
+void MHn::Sumw2() const
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->Sumw2();
+}
+
+void MHn::SetBinnings(MBinning *x, MBinning *y, MBinning *z) const
+{
+    if (fHist[fNum-1])
+        fHist[fNum-1]->SetBinnings(x, y, z);
 }
 
@@ -337,8 +413,14 @@
             break;
         case 3:   // 3
-        case 4:   // 4
             pad->Divide(2,2);
+            delete pad->GetPad(4);
+            break;
+         case 4:   // 4
+            pad->Divide(2,2);
             break;
         case 5:   // 5
+            pad->Divide(3,2);
+            delete pad->GetPad(6);
+            break;
         case 6:   // 6
             pad->Divide(3,2);
Index: /trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.h
===================================================================
--- /trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.h	(revision 8697)
+++ /trunk/MagicSoft/Mars/mjoptim/MJOptimizeBase.h	(revision 8698)
@@ -13,13 +13,15 @@
 protected:
     Int_t  fDebug;
-    Bool_t fEnableWeights;
+    Bool_t fEnableWeights;  // Enable using weights
 
-    TList fRules;
+    TList fRules;           // Contains the parameters which can be accessed by matrix column
+    TList fTrainParameters; // Parameters in the last columns
 
-    TList fPreCuts;
-    TList fTrainCuts;
-    TList fTestCuts;
-    TList fPreTasks;
-    TList fPostTasks;
+    TList fPreCuts;         // Cuts executed for training and testing
+    TList fTrainCuts;       // Cuts executed only in training
+    TList fTestCuts;        // Cuts executed only in testing
+    TList fPreTasks;        // Tasks executed before cut execution
+    TList fPostTasks;       // Tasks executed after cut execution
+    TList fTestTasks;       // Tasks executed after cut execution for testing
 
     void AddCut(TList &l, const char *rule);
@@ -39,4 +41,8 @@
     void AddPostTask(const char *rule,
                      const char *name="MWeight") { AddPar(fPostTasks, rule, name); }
+
+    void AddTestTask(MTask *t)                   { Add(fTestTasks, t); }
+    void AddTestTask(const char *rule,
+                     const char *name="MWeight") { AddPar(fTestTasks, rule, name); }
 
     void SetDebug(Bool_t b=kTRUE)      { fDebug = b; }
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 8697)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainRanForest.cc	(revision 8698)
@@ -43,91 +43,5 @@
 #include "MJTrainRanForest.h"
 
-#include <TFile.h>
-
-#include "MLog.h"
-#include "MLogManip.h"
-
-#include "MFDataPhrase.h"
-#include "MParameterCalc.h"
-
-#include "MStatusDisplay.h"
-
 ClassImp(MJTrainRanForest);
 
 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 MJTrainRanForest::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 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::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 MJTrainRanForest::AddParameter(const char *rule)
-{
-    fRules.Add(new TNamed(rule, ""));
-    return fRules.GetSize()-1;
-}
-
-Bool_t MJTrainRanForest::WriteDisplay(const char *fname) const
-{
-    TFile file(fname, "UPDATE");
-    if (!file.IsOpen())
-    {
-        *fLog << err << "ERROR - Couldn't open file " << fname << " for writing." << endl;
-        return kFALSE;
-    }
-
-    *fLog << inf << "Wrinting to " << fname << ":" << endl;
-    *fLog << " - MStatusDisplay..." << flush;
-    if (fDisplay && fDisplay->Write()<=0)
-    {
-        *fLog << err << "Unable to write MStatusDisplay to " << fname << endl;
-        return kFALSE;
-    }
-    *fLog << inf << "ok." << endl;
-
-    return kTRUE;
-}
-*/
Index: /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc	(revision 8697)
+++ /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc	(revision 8698)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MRanForestCalc.cc,v 1.27 2007-07-26 11:13:00 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MRanForestCalc.cc,v 1.28 2007-08-23 10:25:08 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -66,5 +66,5 @@
 
 MRanForestCalc::MRanForestCalc(const char *name, const char *title)
-    : fData(0), fRFOut(0), fTestMatrix(0),
+    : fData(0), fRFOut(0), fTestMatrix(0), fFunc("Function", "x"),
     fNumTrees(-1), fNumTry(-1), fNdSize(-1), fNumObsoleteVariables(1),
     fLastDataColumnHasWeights(kFALSE),
@@ -73,4 +73,6 @@
     fName  = name  ? name  : gsDefName.Data();
     fTitle = title ? title : gsDefTitle.Data();
+
+    gROOT->GetListOfFunctions()->Remove(&fFunc);
 
     // FIXME:
@@ -83,4 +85,13 @@
 {
     fEForests.Delete();
+}
+
+// --------------------------------------------------------------------------
+//
+// Set a function which is applied to the output of the random forest
+//
+Bool_t MRanForestCalc::SetFunction(const char *func)
+{
+    return !fFunc.Compile(func);
 }
 
@@ -291,4 +302,7 @@
     }
 
+    if (fileRF.GetListOfKeys()->FindObject("Function"))
+        fFunc.Read("Function");
+
     return kTRUE;
 }
@@ -326,5 +340,5 @@
 }
 
-Int_t MRanForestCalc::Process()
+Double_t MRanForestCalc::Eval() const
 {
     TVector event;
@@ -338,8 +352,5 @@
     {
         MRanForest *rf = static_cast<MRanForest*>(fEForests.UncheckedAt(0));
-        fRFOut->SetVal(rf->CalcHadroness(event));
-        fRFOut->SetReadyToSave();
-
-        return kTRUE;
+        return rf->CalcHadroness(event);
     }
 
@@ -383,9 +394,7 @@
     {
     case kMean:
-        fRFOut->SetVal(pow(10, sume/sumh));
-        break;
+        return sume/sumh;
     case kMaximum:
-        fRFOut->SetVal(pow(10, maxe));
-        break;
+        return maxe;
     case kFit:
         f1.SetParameter(0, maxh);
@@ -393,8 +402,15 @@
         f1.SetParameter(2, 0.125);
         g.Fit(&f1, "Q0N");
-        fRFOut->SetVal(pow(10, f1.GetParameter(1)));
-        break;
-    }
-
+        return f1.GetParameter(1);
+    }
+
+    return 0;
+}
+
+Int_t MRanForestCalc::Process()
+{
+    const Double_t val = Eval();
+
+    fRFOut->SetVal(fFunc.Eval(val));
     fRFOut->SetReadyToSave();
 
Index: /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h
===================================================================
--- /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h	(revision 8697)
+++ /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.h	(revision 8698)
@@ -12,4 +12,8 @@
 #ifndef ROOT_TArrayD
 #include <TArrayD.h>
+#endif
+
+#ifndef ROOT_TF1
+#include <TF1.h>
 #endif
 
@@ -36,4 +40,5 @@
     MParameterD *fRFOut;                //! Used to store result
     MHMatrix    *fTestMatrix;           //! Test Matrix used in Process (together with MMatrixLoop)
+    TF1          fFunc;                 //! Function to apply to the result
 
     TObjArray    fEForests;             //! List of forests read or to be written
@@ -60,4 +65,5 @@
     // MRanForestCalc
     Int_t ReadForests(MParList &plist);
+    Double_t Eval() const;
 
     // MParContainer
@@ -84,4 +90,6 @@
     void SetNumTry(UShort_t n=0)     { fNumTry   = n; }
     void SetDebug(Bool_t b=kTRUE)    { fDebug    = b; }
+
+    Bool_t SetFunction(const char *name="x");
 
     void SetNumObsoleteVariables(Int_t n=1)          { fNumObsoleteVariables = n; }
