Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 8643)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 8644)
@@ -18,4 +18,54 @@
 
                                                  -*-*- END OF LINE -*-*-
+
+ 2007/07/24 Thomas Bretz
+
+   * mbase/MEvtLoop.cc:
+     - removed the "Status Display" in front of the window name
+
+   * mcalib/MCalibrationChargeCalc.cc:
+     - initialized some more variables in the constructor as suggested
+       by valgrind
+
+   * mjobs/MJCut.cc:
+     - reset number of off-source positions in on-/off-mode to one
+
+   * mjobs/MJob.[h,cc]:
+     - added many comments
+     - simplified the functions for setting a new resource file
+     - replaced some repeated code by new function CombinePath
+     - added new member function HasWritePermission
+
+   * mjoptim/MJOptimize.cc:
+     - set title as window title of eventloop
+
+   * mjoptim/MJOptimizeCuts.cc:
+     - stop if filter is set and it is not MFMagicCuts
+
+   * mjoptim/MJOptimizeCuts.cc, mjoptim/MJOptimizeDisp.cc,
+     mjoptim/MJOptimizeEnergy.cc:
+     - set window title of status display
+
+   * mjtrain/MJTrainDisp.cc, mjtrain/MJTrainEnergy.cc, 
+     mjtrain/MJTrainSeparation.cc:
+     - set window title of status display
+     - check for write permission of the output file
+
+   * mjtrain/MJTrainDisp.[h,cc]:
+     - implemented new plots showing the result in a more
+       convinient way
+     - allow to set a different result value than dist
+
+   * mranforest/MRanForest.[h,cc]:
+     - improvement to output when training. Moved code to a
+       new member function
+
+   * mranforest/MRanForestCalc.cc:
+     - set window title of status display
+   
+   * mtools/MTFillMatrix.[h,cc]:
+     - implemented setting name and title in the constructor
+     - set window title when wvent loop is started to title
+
 
 
Index: /trunk/MagicSoft/Mars/mbase/MEvtLoop.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mbase/MEvtLoop.cc	(revision 8644)
@@ -240,5 +240,5 @@
         fDisplay->SetNoContextMenu();
         // Set window and icon name
-        fDisplay->SetWindowName(TString("Status Display: ")+fName);
+        fDisplay->SetWindowName(fName);
         fDisplay->SetIconName(fName);
         // Start automatic update
Index: /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 8644)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MCalibrationChargeCalc.cc,v 1.182 2007-06-18 14:36:16 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MCalibrationChargeCalc.cc,v 1.183 2007-07-24 13:36:35 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -302,5 +302,6 @@
 //
 MCalibrationChargeCalc::MCalibrationChargeCalc(const char *name, const char *title)
-    : fGeom(NULL), fSignal(NULL), fCalibPattern(NULL), fExtractor(NULL)
+    : fPulserColor(MCalibrationCam::kNONE), fContinousCalibration(kFALSE),
+    fGeom(NULL), fSignal(NULL), fCalibPattern(NULL), fExtractor(NULL)
 {
         
Index: /trunk/MagicSoft/Mars/mjobs/MJCut.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjobs/MJCut.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjobs/MJCut.cc	(revision 8644)
@@ -553,4 +553,10 @@
 
     // --------------------------------------------------------------------------------
+
+    if (!set.IsWobbleMode() && fNumOffSourcePos!=1)
+    {
+        *fLog << inf << "No wobble mode but NumOffSoucePos!=1 (" << fNumOffSourcePos << ")... reset to 1." << endl;
+        fNumOffSourcePos = 1;
+    }
 
     // Possible source position (eg. Wobble Mode)
Index: /trunk/MagicSoft/Mars/mjobs/MJob.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjobs/MJob.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjobs/MJob.cc	(revision 8644)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz, 8/2004 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2004
+!   Copyright: MAGIC Software Development, 2000-2007
 !
 !
@@ -34,4 +34,6 @@
 // SetDebugEnv(3) // do 2) and debug setting env completely
 //
+// To allow overwriting the output files call SetOverwrite()
+//
 /////////////////////////////////////////////////////////////////////////////
 #include "MJob.h"
@@ -66,4 +68,12 @@
 }
 
+//------------------------------------------------------------------------
+//
+// If MJob is the owner of fEnv delete fEnv.
+//
+// Reset the owenership bit.
+//
+// Set fEnv to NULL.
+//
 void MJob::ClearEnv()
 {
@@ -74,4 +84,8 @@
 }
 
+//------------------------------------------------------------------------
+//
+// ClearEnv()
+//
 MJob::~MJob()
 {
@@ -79,18 +93,14 @@
 }
 
-Bool_t MJob::SetEnv(const char *env, const char *prefix)
-{
-    ClearEnv();
-
-    fEnv = new MEnv(env);
-    SetBit(kIsOwner);
-
-    if (!fEnv->IsValid())
-    {
-        ClearEnv();
-        return kFALSE;
-    }
-
+//------------------------------------------------------------------------
+//
+// If prefix==0 the prefix is taken from fName up to the first
+// whitespace.
+//
+// A trailing dot is removed.void MJob::SetPrefix(const char *prefix)
+void MJob::SetPrefix(const char *prefix)
+{
     fEnvPrefix = prefix;
+
     if (!prefix)
         fEnvPrefix = fName.First(' ')>0 ? fName(0, fName.First(' ')) : fName;
@@ -98,8 +108,33 @@
     if (fEnvPrefix.EndsWith("."))
         fEnvPrefix.Remove(fEnvPrefix.Length()-1);
-
-    return kTRUE;
-}
-
+}
+
+//------------------------------------------------------------------------
+//
+// Create a new MEnv from the file env. MJob takes of course the
+// ownership of the newly created MEnv.
+//
+// SetPrefix(prefix)
+//
+// return kFALSE if MEnv is invalid
+//
+Bool_t MJob::SetEnv(const char *env, const char *prefix)
+{
+    SetEnv(new MEnv(env), prefix);
+
+    // Take the owenership of the MEnv instance
+    SetBit(kIsOwner);
+
+    return fEnv->IsValid();
+}
+
+//------------------------------------------------------------------------
+//
+// Set a new fEnv and a new general prefix.
+//
+// Calls SetPrefix(prefix)
+//
+// MJob does not take the owenership of the MEnv instance.
+//
 void MJob::SetEnv(MEnv *env, const char *prefix)
 {
@@ -108,13 +143,17 @@
     fEnv = env;
 
-    fEnvPrefix = prefix;
-    if (!prefix)
-        fEnvPrefix = fName.First(' ')>0 ? fName(0, fName.First(' ')) : fName;
-
-    if (fEnvPrefix.EndsWith("."))
-        fEnvPrefix.Remove(fEnvPrefix.Length()-1);
-}
-
-void MJob::FixPath(TString &path) const
+    SetPrefix(prefix);
+}
+
+//------------------------------------------------------------------------
+//
+// Removes LF's from the path (necessary if the resource file was written
+// with a different operating system than Linux.
+//
+// Removes a trailing slash if the path is not the root-path.
+//
+// Adds fname to the path if given.
+//
+void MJob::FixPath(TString &path)
 {
     path.ReplaceAll("\015", "");
@@ -127,4 +166,30 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Calls FixPath
+//
+// Adds fname to the path if given.
+//
+TString MJob::CombinePath(TString path, TString fname)
+{
+    FixPath(path);
+
+    if (fname.IsNull())
+        return path;
+
+    if (path!=(TString)"/")
+        path += "/";
+
+    path += fname;
+
+    return path;
+}
+
+//------------------------------------------------------------------------
+//
+// Sets the output path. The exact meaning (could also be a file) is
+// deined by the derived class.
+//
 void MJob::SetPathOut(const char *path)
 {
@@ -133,4 +198,9 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Sets the input path. The exact meaning (could also be a file) is
+// deined by the derived class.
+//
 void MJob::SetPathIn(const char *path)
 {
@@ -139,4 +209,8 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Returns the TEnv
+//
 const TEnv *MJob::GetEnv() const
 {
@@ -144,4 +218,9 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Checks GetEnvValue(*fEnv, fEnvPrefix, name, dftl)
+// For details see MParContainer
+//
 Int_t MJob::GetEnv(const char *name, Int_t dflt) const
 {
@@ -149,4 +228,9 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Checks GetEnvValue(*fEnv, fEnvPrefix, name, dftl)
+// For details see MParContainer
+//
 Double_t MJob::GetEnv(const char *name, Double_t dflt) const
 {
@@ -154,4 +238,9 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Checks GetEnvValue(*fEnv, fEnvPrefix, name, dftl)
+// For details see MParContainer
+//
 const char *MJob::GetEnv(const char *name, const char *dflt) const
 {
@@ -159,4 +248,9 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Checks IsEnvDefined(*fEnv, fEnvPrefix, name, fEnvDebug>2)
+// For details see MParContainer
+//
 Bool_t MJob::HasEnv(const char *name) const
 {
@@ -164,4 +258,15 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Check the resource file for
+//   PathOut
+//   PathIn
+//   MaxEvents
+//   Overwrite
+//   EnvDebug
+//
+// and call the virtual function CheckEnvLocal
+//
 Bool_t MJob::CheckEnv()
 {
@@ -202,4 +307,9 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Call the eventsloops ReadEnv and print untouched resources afterwards
+// if fEnvDebug>1
+//
 Bool_t MJob::SetupEnv(MEvtLoop &loop) const
 {
@@ -214,4 +324,33 @@
 
     return kTRUE;
+}
+
+//------------------------------------------------------------------------
+//
+// Checks whether write permissions to fname exists including
+// the fOverwrite data amember.
+//
+Bool_t MJob::HasWritePermission(TString fname) const
+{
+    gSystem->ExpandPathName(fname);
+
+    const Bool_t exists = !gSystem->AccessPathName(fname, kFileExists);
+    if (!exists)
+        return kTRUE;
+
+    const Bool_t write = !gSystem->AccessPathName(fname, kWritePermission);
+    if (!write)
+    {
+        *fLog << err << "ERROR - No permission to write to " << fname << endl;
+        return kFALSE;
+    }
+
+    if (!fOverwrite)
+        return kTRUE;
+
+    *fLog << err;
+    *fLog << "ERROR - File " << fname << " already exists and overwrite not allowed." << endl;
+
+    return kFALSE;
 }
 
@@ -274,6 +413,6 @@
 //------------------------------------------------------------------------
 //
-// Write containers in cont (and - if available) the status display to
-// fPathOut+"/"+name
+// Write containers in cont to fPathOut+"/"+name, or fPathOut only
+// if name is empty.
 //
 Bool_t MJob::WriteContainer(TCollection &cont, const char *name, const char *option, const int compr) const
@@ -285,15 +424,9 @@
     }
 
-    TString oname(fPathOut);
-    if (!TString(name).IsNull())
-    {
-        if (oname!="/")
-            oname += "/";
-        oname += name;
-    }
+    const TString oname = CombinePath(fPathOut, name);
 
     *fLog << inf << "Writing to file: " << oname << endl;
 
-    TString title("File Written by ");
+    TString title("File written by ");
     title += fName;
 
@@ -308,4 +441,14 @@
 }
 
+//------------------------------------------------------------------------
+//
+// return kTRUE if no display is set.
+//
+// Write the display to the TFile with name name, options option and
+// compression level comp.
+//
+// If name IsNull fPathOut is assumed to contain the name, otherwise
+// name is appended to fPathOut. fPathOut might be null.
+//
 Bool_t MJob::WriteDisplay(const char *name, const char *option, const int compr) const
 {
@@ -348,9 +491,5 @@
 
     // check if path ends with a slash
-    if (!path.EndsWith("/"))
-        path += "/";
-
-    // compile full qualified path
-    path += fname;
+    path = CombinePath(path, fname);
 
     gLog << dbg << "MJob::ExpandPath - Filename expanded to " << path << endl;
@@ -360,4 +499,8 @@
 }
 
+//------------------------------------------------------------------------
+//
+// Sorts the array.
+//
 void MJob::SortArray(TArrayI &arr)
 {
Index: /trunk/MagicSoft/Mars/mjobs/MJob.h
===================================================================
--- /trunk/MagicSoft/Mars/mjobs/MJob.h	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjobs/MJob.h	(revision 8644)
@@ -14,7 +14,13 @@
     enum { kIsOwner = BIT(14) };
 
-    void FixPath(TString &path) const;
+    // Helper functions for paths
+    static TString CombinePath(TString path, TString fname);
+    static void FixPath(TString &path);
+
+    // Helper functions to set a new fEnv
+    void SetPrefix(const char *prefix);
     void ClearEnv();
 
+    // Data members for resource files
     const MEnv *fEnv;         // Resource file
     TString     fEnvPrefix;   // Prefix for resources
@@ -28,4 +34,5 @@
     Int_t     fMaxEvents;     // Maximum number of events
 
+    // FIXME: Split into MJobSequence and MJobDataSet
     MSequence fSequence;      // Sequence
 
@@ -71,4 +78,6 @@
     TString GetPathIn() const   { return fPathIn; }
 
+    Bool_t HasWritePermission(TString fname) const;
+
     // Others
     MStatusDisplay *GetDisplay() { return fDisplay; }
Index: /trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjoptim/MJOptimize.cc	(revision 8644)
@@ -647,5 +647,5 @@
 
     // Setup eventloop
-    MEvtLoop evtloop;
+    MEvtLoop evtloop(fTitle);
     evtloop.SetParList(&parlist);
     evtloop.SetDisplay(fDisplay); // set display for evtloop and all childs
@@ -734,5 +734,5 @@
     tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
 
-    MEvtLoop fillloop;
+    MEvtLoop fillloop(fTitle);
     fillloop.SetParList(&parlist);
     fillloop.SetDisplay(fDisplay);
Index: /trunk/MagicSoft/Mars/mjoptim/MJOptimizeCuts.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjoptim/MJOptimizeCuts.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjoptim/MJOptimizeCuts.cc	(revision 8644)
@@ -85,4 +85,5 @@
 #include "MLog.h"
 #include "MLogManip.h"
+#include "MStatusDisplay.h"
 
 // Eventloop
@@ -138,4 +139,9 @@
 Bool_t MJOptimizeCuts::RunOnOffCore(MHAlpha &histon, MHAlpha &histof, const char *fname, MFilter *filter, MAlphaFitter *fit, const char *tree)
 {
+    SetTitle(Form("OptimizeCuts: %s", fname));
+
+    if (fDisplay)
+        fDisplay->SetTitle(fTitle);
+
     fLog->Separator("Preparing On/Off-optimization");
 
@@ -162,6 +168,14 @@
     histof.InitMapping(&m, 1);
 
-    if (filter && filter->InheritsFrom(MFMagicCuts::Class()))
-        ((MFMagicCuts*)filter)->InitMapping(&m);
+    if (filter)
+    {
+        if (filter->InheritsFrom(MFMagicCuts::Class()))
+            ((MFMagicCuts*)filter)->InitMapping(&m);
+        else
+        {
+            *fLog << err << "ERROR - Currently only MFMagicCuts is supported." << endl;
+            return kFALSE;
+        }
+    }
 
     parlist.AddToList(&histon);
@@ -249,4 +263,9 @@
 Bool_t MJOptimizeCuts::RunOnCore(MHAlpha &hist, const char *fname, MFilter *filter, MAlphaFitter *fit)
 {
+    SetTitle(Form("OptimizeCuts: %s", fname));
+
+    if (fDisplay)
+        fDisplay->SetTitle(fTitle);
+
     fLog->Separator("Preparing On-only-optimization");
 
Index: /trunk/MagicSoft/Mars/mjoptim/MJOptimizeDisp.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjoptim/MJOptimizeDisp.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjoptim/MJOptimizeDisp.cc	(revision 8644)
@@ -60,4 +60,5 @@
 #include "MLog.h"
 #include "MLogManip.h"
+#include "MStatusDisplay.h"
 
 // eventloop
@@ -97,4 +98,9 @@
 Bool_t MJOptimizeDisp::RunDisp(const char *fname, const char *rule, MTask *weights)
 {
+    SetTitle(Form("OptimizeDisp: %s", fname));
+
+    if (fDisplay)
+        fDisplay->SetTitle(fTitle);
+
     fLog->Separator("Preparing Disp optimization");
 
Index: /trunk/MagicSoft/Mars/mjoptim/MJOptimizeEnergy.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjoptim/MJOptimizeEnergy.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjoptim/MJOptimizeEnergy.cc	(revision 8644)
@@ -53,4 +53,5 @@
 #include "MLog.h"
 #include "MLogManip.h"
+#include "MStatusDisplay.h"
 
 // eventloop
@@ -86,4 +87,9 @@
 Bool_t MJOptimizeEnergy::RunEnergy(const char *fname, const char *rule, MTask *weights)
 {
+    SetTitle(Form("OptimizeEnergy: %s", fname));
+
+    if (fDisplay)
+        fDisplay->SetTitle(fTitle);
+
     fLog->Separator("Preparing Energy optimization");
 
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 8644)
@@ -18,5 +18,5 @@
 !   Author(s): Thomas Bretz 11/2005 <mailto:tbretz@astro.uni-wuerzburg.de>
 !
-!   Copyright: MAGIC Software Development, 2005
+!   Copyright: MAGIC Software Development, 2005-2007
 !
 !
@@ -55,4 +55,7 @@
 #include "MJTrainDisp.h"
 
+#include <TLine.h>
+#include <TCanvas.h>
+
 #include "MHMatrix.h"
 
@@ -64,4 +67,5 @@
 #include "MTFillMatrix.h"
 #include "MChisqEval.h"
+#include "MStatusDisplay.h"
 
 // eventloop
@@ -92,6 +96,104 @@
 using namespace std;
 
+const TString MJTrainDisp::fgTrainParameter = "MHillasSrc.fDist*MGeomCam.fConvMm2Deg";
+
+// --------------------------------------------------------------------------
+//
+// Display a result histogram either vs. size or energy
+// FIXME: Could be moved into a new histogram class.
+//
+void MJTrainDisp::DisplayHist(TCanvas &c, Int_t i, MH3 &mh3) const
+{
+    MH::SetPalette("pretty");
+
+    TH1 &hist = *(TH1*)mh3.GetHist().Clone();
+    hist.SetBit(TH1::kNoStats);
+    hist.SetDirectory(0);
+
+    TLine line;
+    line.SetLineStyle(kDashed);
+    line.SetLineWidth(1);
+
+    c.cd(i);
+    gPad->SetBorderMode(0);
+    gPad->SetFrameBorderMode(0);
+    //gPad->SetFillColor(kWhite);
+    gPad->SetLogx();
+    gPad->SetGridx();
+    gPad->SetGridy();
+    //gPad->SetLeftMargin(0.12);
+    //gPad->SetRightMargin(0.12);
+
+    for (int x=0; x<=hist.GetNbinsX(); x++)
+    {
+        Float_t n = 0;
+        for (int y=1; y<=2; y++)
+            n += hist.GetBinContent(x,y);
+
+        if (n==0)
+            continue;
+
+        for (int y=0; y<=hist.GetNbinsY(); y++)
+            hist.SetBinContent(x, y, 200*hist.GetBinContent(x,y)/n);
+    }
+
+    hist.SetMaximum(100);
+    hist.DrawCopy("colz");
+
+    line.DrawLine(10, 0.04, 31623, 0.04);
+
+    c.cd(i+2);
+    gPad->SetBorderMode(0);
+    gPad->SetFrameBorderMode(0);
+    //gPad->SetFillColor(kWhite);
+    gPad->SetLogx();
+    gPad->SetGridx();
+    gPad->SetGridy();
+    //gPad->SetLeftMargin(0.12);
+    //gPad->SetRightMargin(0.12);
+
+    for (int x=0; x<=hist.GetNbinsX(); x++)
+    {
+        Float_t n = 0;
+        for (int y=0; y<=hist.GetNbinsY(); y++)
+            n += hist.GetBinContent(x,y);
+
+        if (n==0)
+            continue;
+
+        for (int y=0; y<=hist.GetNbinsY(); y++)
+            hist.SetBinContent(x, y, 100*hist.GetBinContent(x,y)/n);
+    }
+
+    hist.SetMaximum(25);
+    hist.DrawCopy("colz");
+
+    line.DrawLine(10, 0.04, 31623, 0.04);
+}
+
+// --------------------------------------------------------------------------
+//
+// Display the result histograms in a new tab.
+//
+void MJTrainDisp::DisplayResult(MH3 &hsize, MH3 &henergy)
+{
+    TCanvas &c = fDisplay->AddTab("Disp");
+    c.Divide(2,2);
+
+    DisplayHist(c, 1, hsize);
+    DisplayHist(c, 2, henergy);
+}
+
+// --------------------------------------------------------------------------
+//
+// Run Disp optimization
+//
 Bool_t MJTrainDisp::Train(const char *out, const MDataSet &set, Int_t num)
 {
+    SetTitle(Form("TrainDisp: %s", out));
+
+    if (fDisplay)
+        fDisplay->SetTitle(fTitle);
+
     if (!set.IsValid())
     {
@@ -99,4 +201,7 @@
         return kFALSE;
     }
+
+    if (!HasWritePermission(out))
+        return kFALSE;
 
     *fLog << inf;
@@ -119,9 +224,8 @@
     if (fEnableWeights)
         train.AddColumn("MWeight.fVal");
-    train.AddColumn("MHillasSrc.fDist*MGeomCam.fConvMm2Deg");
-    //train.AddColumn("TMath::Hypot(MHillasSrc.fDCA, MHillasSrc.fDist)*MGeomCam.fConvMm2Deg");
+    train.AddColumn(fgTrainParameter);
 
     // ----------------------- Fill Matrix RF ----------------------
-    MTFillMatrix fill;
+    MTFillMatrix fill(fTitle);
     fill.SetDisplay(fDisplay);
     fill.SetLogStream(fLog);
@@ -136,5 +240,5 @@
 
     // ------------------------ Train RF --------------------------
-    MRanForestCalc rf;
+    MRanForestCalc rf(fTitle);
     rf.SetNumTrees(fNumTrees);
     rf.SetNdSize(fNdSize);
@@ -212,11 +316,11 @@
     eval.SetY1("sqrt(ThetaSquared.fVal)");
 
-    MH3 hdisp1("MHillas.fSize",  "sqrt(ThetaSquared.fVal)");
-    MH3 hdisp2("MMcEvt.fEnergy", "sqrt(ThetaSquared.fVal)");
+    MH3 hdisp1("MHillas.fSize",  "ThetaSquared.fVal");
+    MH3 hdisp2("MMcEvt.fEnergy", "ThetaSquared.fVal");
     hdisp1.SetTitle("\\vartheta distribution vs. Size:Size [phe]:\\vartheta [\\circ]");
     hdisp2.SetTitle("\\vartheta distribution vs. Energy:Enerhy [GeV]:\\vartheta [\\circ]");
 
-    MBinning binsx(50, 10, 100000, "BinningMH3X", "log");
-    MBinning binsy(50, 0,  1,      "BinningMH3Y", "lin");
+    MBinning binsx( 70, 10, 31623, "BinningMH3X", "log");
+    MBinning binsy( 75, 0,  0.3,   "BinningMH3Y", "lin");
 
     plist.AddToList(&binsx);
@@ -239,8 +343,10 @@
     tlist.AddToList(&eval);
 
-    MEvtLoop loop;
+    MEvtLoop loop(fTitle);
     loop.SetLogStream(fLog);
     loop.SetDisplay(fDisplay);
     loop.SetParList(&plist);
+    //if (!SetupEnv(loop))
+    //    return kFALSE;
 
     if (!loop.Eventloop())
@@ -251,5 +357,8 @@
     hist.GetAlphaFitter().Print("result");
 
-    if (!WriteDisplay(out, "UPDATE"))
+    DisplayResult(hdisp1, hdisp2);
+
+    SetPathOut(out);
+    if (!WriteDisplay(0, "UPDATE"))
         return kFALSE;
 
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.h
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.h	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.h	(revision 8644)
@@ -6,10 +6,24 @@
 #endif
 
+class TCanvas;
+
+class MH3;
 class MDataSet;
 
 class MJTrainDisp : public MJTrainRanForest
 {
+private:
+    static const TString fgTrainParameter;
+
+    TString fTrainParameter;
+
+    void DisplayHist(TCanvas &c, Int_t i, MH3 &mh3) const;
+    void DisplayResult(MH3 &hsize, MH3 &henergy);
+
 public:
-    MJTrainDisp() { }
+    MJTrainDisp() : fTrainParameter(fgTrainParameter) { }
+
+    void SetTrainParameter(const char *txt) { fTrainParameter=txt; }
+
     Bool_t Train(const char *out, const MDataSet &set, Int_t num);
 
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 8644)
@@ -62,4 +62,5 @@
 #include "MDataSet.h"
 #include "MTFillMatrix.h"
+#include "MStatusDisplay.h"
 
 // eventloop
@@ -86,4 +87,9 @@
 Bool_t MJTrainEnergy::Train(const char *out, const MDataSet &set, Int_t num)
 {
+    SetTitle(Form("TrainEnergy: %s", out));
+
+    if (fDisplay)
+        fDisplay->SetTitle(fTitle);
+
     if (!set.IsValid())
     {
@@ -91,4 +97,7 @@
         return kFALSE;
     }
+
+    if (!HasWritePermission(out))
+        return kFALSE;
 
     *fLog << inf;
@@ -102,7 +111,13 @@
 
     if (!set.AddFilesOn(readtrn))
-        return kFALSE;
+    {
+        *fLog << err << "ERROR - Adding SequencesOn." << endl;
+        return kFALSE;
+    }
     if (!set.AddFilesOff(readtst))
-        return kFALSE;
+    {
+        *fLog << err << "ERROR - Adding SequencesOff." << endl;
+        return kFALSE;
+    }
 
     // ----------------------- Setup Matrix ------------------
@@ -117,5 +132,5 @@
 
     // ----------------------- Fill Matrix RF ----------------------
-    MTFillMatrix fill;
+    MTFillMatrix fill(fTitle);
     fill.SetDisplay(fDisplay);
     fill.SetLogStream(fLog);
@@ -130,5 +145,5 @@
 
     // ------------------------ Train RF --------------------------
-    MRanForestCalc rf;
+    MRanForestCalc rf(fTitle);
     rf.SetNumTrees(fNumTrees);
     rf.SetNdSize(fNdSize);
@@ -185,13 +200,16 @@
     tlist.AddToList(&fillh);
 
-    MEvtLoop loop;
+    MEvtLoop loop(fTitle);
     loop.SetLogStream(fLog);
     loop.SetDisplay(fDisplay);
     loop.SetParList(&plist);
+    //if (!SetupEnv(loop))
+    //   return kFALSE;
 
     if (!loop.Eventloop())
         return kFALSE;
 
-    if (!WriteDisplay(out, "UPDATE"))
+    SetPathOut(out);
+    if (!WriteDisplay(0, "UPDATE"))
         return kFALSE;
 
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 8644)
@@ -589,4 +589,7 @@
 Bool_t MJTrainSeparation::Train(const char *out)
 {
+    if (fDisplay)
+        fDisplay->SetTitle(out);
+
     if (!fDataSetTrain.IsValid())
     {
@@ -606,5 +609,8 @@
     }
 
-    TStopwatch clock;
+    if (!HasWritePermission(out))
+        return kFALSE;
+
+        TStopwatch clock;
     clock.Start();
 
@@ -939,4 +945,6 @@
     loop.SetLogStream(fLog);
     loop.SetParList(&plist);
+    //if (!SetupEnv(loop))
+    //   return kFALSE;
 
     wgt.SetVal(1);
@@ -1039,5 +1047,6 @@
 
     // Write the display
-    if (!WriteDisplay(out, "UPDATE"))
+    SetPathOut(out);
+    if (!WriteDisplay(0, "UPDATE"))
         return kFALSE;
 
Index: /trunk/MagicSoft/Mars/mranforest/MRanForest.cc
===================================================================
--- /trunk/MagicSoft/Mars/mranforest/MRanForest.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mranforest/MRanForest.cc	(revision 8644)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MRanForest.cc,v 1.26 2006-11-02 08:57:00 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MRanForest.cc,v 1.27 2007-07-24 13:35:39 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -124,5 +124,4 @@
 
     fNTimesOutBag = rf.fNTimesOutBag;
-
 }
 
@@ -435,6 +434,6 @@
     // initialize running output
 
-    float minfloat=fHadTrue[TMath::LocMin(fHadTrue.GetSize(),fHadTrue.GetArray())];
-    Bool_t calcResolution=(minfloat>0.001);
+    float minfloat=TMath::MinElement(fHadTrue.GetSize(),fHadTrue.GetArray());
+    Bool_t calcResolution=(minfloat>FLT_MIN);
 
     if (fTreeNo==1)
@@ -443,7 +442,7 @@
 
         if(calcResolution)
-            *fLog << "TreeNum BagSize NumNodes TestSize res/% (from oob-data -> overest. of error)" << endl;
+            *fLog << "TreeNum BagSize NumNodes TestSize  Bias/%   var/%   res/% (from oob-data)" << endl;
         else
-            *fLog << "TreeNum BagSize NumNodes TestSize rms/% (from oob-data -> overest. of error)" << endl;
+            *fLog << "TreeNum BagSize NumNodes TestSize  Bias/au  var/au  rms/au (from oob-data)" << endl;
                      //        12345678901234567890123456789012345678901234567890
     }
@@ -498,14 +497,29 @@
                        jinbag,winbag,nclass);
 
-    //-------------------------------------------------------------------
-    // error-estimates from out-of-bag data (oob data):
-    //
-    // For a single tree the events not(!) contained in the bootstrap
-    // sample of this tree can be used to obtain estimates for the
-    // classification error of this tree.
-    // If you take a certain event, it is contained in the oob-data of
-    // 1/3 of the trees (see comment to ModifyData). This means that the
-    // classification error determined from oob-data is underestimated,
-    // but can still be taken as upper limit.
+    const Double_t ferr = EstimateError(jinbag, calcResolution);
+
+    fRanTree->SetError(ferr);
+
+    // adding tree to forest
+    AddTree();
+
+    return fTreeNo<fNumTrees;
+}
+
+//-------------------------------------------------------------------
+// error-estimates from out-of-bag data (oob data):
+//
+// For a single tree the events not(!) contained in the bootstrap
+// sample of this tree can be used to obtain estimates for the
+// classification error of this tree.
+// If you take a certain event, it is contained in the oob-data of
+// 1/3 of the trees (see comment to ModifyData). This means that the
+// classification error determined from oob-data is underestimated,
+// but can still be taken as upper limit.
+//
+Double_t MRanForest::EstimateError(const MArrayI &jinbag, Bool_t calcResolution)
+{
+    const Int_t numdata = GetNumData();
+
     Int_t ninbag = 0;
     for (Int_t ievt=0;ievt<numdata;ievt++)
@@ -522,18 +536,33 @@
 
     Int_t n=0;
-    Float_t ferr=0;
-
-    for (Int_t ievt=0;ievt<numdata;ievt++)
-    {
-        if(fNTimesOutBag[ievt]!=0)
-        {
-            float val = fHadEst[ievt]/float(fNTimesOutBag[ievt])-fHadTrue[ievt];
-            if(calcResolution) val/=fHadTrue[ievt];
-
-            ferr += val*val;
-            n++;
-        }
-    }
-    ferr = TMath::Sqrt(ferr/n);
+
+    Double_t sum=0;
+    Double_t sq =0;
+    for (Int_t i=0; i<numdata; i++)
+    {
+        if (fNTimesOutBag[i]==0)
+            continue;
+
+        const Float_t hadest = fHadEst[i]/fNTimesOutBag[i];
+
+        const Float_t val = calcResolution ?
+            hadest/fHadTrue[i] - 1 : hadest - fHadTrue[i];
+
+        sum += val;
+        sq  += val*val;
+        n++;
+    }
+
+    if (calcResolution)
+    {
+        sum *= 100;
+        sq  *= 10000;
+    }
+
+    sum /= n;
+    sq  /= n;
+
+    const Double_t var  = TMath::Sqrt(sq-sum*sum);
+    const Double_t ferr = TMath::Sqrt(sq);
 
     //-------------------------------------------------------------------
@@ -543,13 +572,10 @@
     *fLog << setw(9) << fRanTree->GetNumEndNodes();
     *fLog << Form("  %9.1f", 100.*n/numdata);
-    *fLog << Form("%18.2f", ferr*100.);
+    *fLog << Form(" %7.2f", sum);
+    *fLog << Form(" %7.2f", var);
+    *fLog << Form(" %7.2f", ferr);
     *fLog << endl;
 
-    fRanTree->SetError(ferr);
-
-    // adding tree to forest
-    AddTree();
-
-    return fTreeNo<fNumTrees;
+    return ferr;
 }
 
Index: /trunk/MagicSoft/Mars/mranforest/MRanForest.h
===================================================================
--- /trunk/MagicSoft/Mars/mranforest/MRanForest.h	(revision 8643)
+++ /trunk/MagicSoft/Mars/mranforest/MRanForest.h	(revision 8644)
@@ -74,4 +74,6 @@
     Double_t fUserVal;     // A user value describing this tree (eg E-mc)
 
+    Double_t EstimateError(const MArrayI &jinbag, Bool_t calcResolution);
+
 protected:
     // create and modify (->due to bagging) fDataSort
Index: /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc
===================================================================
--- /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mranforest/MRanForestCalc.cc	(revision 8644)
@@ -1,4 +1,4 @@
 /* ======================================================================== *\
-! $Name: not supported by cvs2svn $:$Id: MRanForestCalc.cc,v 1.25 2006-11-02 08:57:00 tbretz Exp $
+! $Name: not supported by cvs2svn $:$Id: MRanForestCalc.cc,v 1.26 2007-07-24 13:35:39 tbretz Exp $
 ! --------------------------------------------------------------------------
 !
@@ -229,5 +229,5 @@
         tlist.AddToList(&fillh);
 
-        MEvtLoop evtloop;
+        MEvtLoop evtloop(fName);
         evtloop.SetParList(&plist);
         evtloop.SetDisplay(fDisplay);
Index: /trunk/MagicSoft/Mars/mtools/MTFillMatrix.cc
===================================================================
--- /trunk/MagicSoft/Mars/mtools/MTFillMatrix.cc	(revision 8643)
+++ /trunk/MagicSoft/Mars/mtools/MTFillMatrix.cc	(revision 8644)
@@ -154,4 +154,17 @@
 }
 
+void MTFillMatrix::Init(const char *name, const char *title)
+{
+    fName  = name  ? name  : "MFillMatrix";
+    fTitle = title ? title : "Tool to fill MHMatrix from file";
+}
+
+MTFillMatrix::MTFillMatrix(const char *name, const char *title)
+: fReference(0), fReader(0), fDestMatrix1(0), fDestMatrix2(0),
+  fNumDestEvents1(0), fNumDestEvents2(0), fWriteFile1(0), fWriteFile2(0)
+{
+    Init(name, title);
+}
+
 // --------------------------------------------------------------------------
 //
@@ -164,11 +177,9 @@
 // the predefined size, but it is much faster.
 //
-MTFillMatrix::MTFillMatrix(const MH3 *ref)
+MTFillMatrix::MTFillMatrix(const MH3 *ref, const char *name, const char *title)
 : fReference(0), fReader(0), fDestMatrix1(0), fDestMatrix2(0),
   fNumDestEvents1(0), fNumDestEvents2(0), fWriteFile1(0), fWriteFile2(0)
 {
-    fName  = "MFillMatrix";
-    fTitle = "Tool to fill MHMatrix from file";
-
+    Init(name, title);
     if (ref)
         fReference = (MH3*)ref->Clone();
Index: /trunk/MagicSoft/Mars/mtools/MTFillMatrix.h
===================================================================
--- /trunk/MagicSoft/Mars/mtools/MTFillMatrix.h	(revision 8643)
+++ /trunk/MagicSoft/Mars/mtools/MTFillMatrix.h	(revision 8644)
@@ -34,4 +34,6 @@
     TList     fPostTasks;
 
+    void Init(const char *name, const char *title);
+
     void Add(const TList &src, const TClass *cls, TList &dest);
 
@@ -42,5 +44,6 @@
 
 public:
-    MTFillMatrix(const MH3 *ref=NULL);
+    MTFillMatrix(const char *name=0, const char *title=0);
+    MTFillMatrix(const MH3 *ref, const char *name=0, const char *title=0);
     ~MTFillMatrix();
 
