Index: trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 8698)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainDisp.cc	(revision 8704)
@@ -87,4 +87,5 @@
 #include "MBinning.h"
 #include "MH3.h"
+#include "MHn.h"
 #include "MHThetaSq.h"
 
@@ -115,5 +116,40 @@
     line.SetLineWidth(1);
 
-    c.cd(i);
+    c.cd(i+4);
+    gPad->SetBorderMode(0);
+    gPad->SetFrameBorderMode(0);
+    //gPad->SetFillColor(kWhite);
+    gPad->SetLogx();
+    gPad->SetGridx();
+    gPad->SetGridy();
+    //gPad->SetLeftMargin(0.12);
+    //gPad->SetRightMargin(0.12);
+
+    const Float_t cutval = hist.GetYaxis()->GetBinLowEdge(4);
+
+    TH1D heff;
+    heff.SetName(Form("Eff%s", hist.GetName()));
+    heff.SetTitle(Form("Cut efficiency vs. %s for \\vartheta<%.3f", hist.GetName(), TMath::Sqrt(cutval)));
+    heff.SetDirectory(0);
+    heff.SetXTitle(hist.GetXaxis()->GetTitle());
+    heff.SetYTitle("Efficiency");
+
+    MH::SetBinning(&heff, hist.GetXaxis());
+
+
+    for (int x=0; x<=hist.GetNbinsX()+1; x++)
+    {
+        const Double_t n0 = hist.Integral(x, x, -1, -1);
+        if (n0>0)
+            heff.SetBinContent(x, hist.Integral(x, x, -1, 3)/n0);
+    }
+
+    heff.SetMinimum(0);
+    heff.SetMaximum(1);
+    heff.DrawCopy();
+
+    line.DrawLine(10, 0.5, 31623, 0.5);
+
+    c.cd(i+0);
     gPad->SetBorderMode(0);
     gPad->SetFrameBorderMode(0);
@@ -179,5 +215,5 @@
 {
     TCanvas &c = fDisplay->AddTab("Disp");
-    c.Divide(2,2);
+    c.Divide(2,3);
 
     DisplayHist(c, 1, hsize);
@@ -215,7 +251,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 ------------------
@@ -277,5 +319,5 @@
 
     MParameterD par("ThetaSquaredCut");
-    par.SetVal(0.2);
+    par.SetVal(0.215*0.215);
     plist.AddToList(&par);
 
@@ -297,4 +339,37 @@
     cont.SetInverted();
 
+    const char *rule = "(MHillasSrc.fDist*MGeomCam.fConvMm2Deg)^2 + (Disp.fVal)^2 - (2*MHillasSrc.fDist*MGeomCam.fConvMm2Deg*Disp.fVal*cos(MHillasSrc.fAlpha*kDeg2Rad))";
+
+    MParameterCalc calcthetasq(rule, "MThetaSqCalc");
+    calcthetasq.SetNameParameter("ThetaSquared");
+
+    MChisqEval eval;
+    eval.SetY1("sqrt(ThetaSquared.fVal)");
+
+    // ----------- Setup binnings ----------------
+    MBinning binsS(50,  10,    100000, "BinningSize",         "log");
+    MBinning binsE(70,  10,    31623,  "BinningEnergy",       "log");
+    MBinning binsG(50, -10,    10,     "BinningSlope",        "lin");
+    MBinning binsX(50, -1,     1,      "BinningResidualDist", "lin");
+    MBinning binsL(50,  0,     0.3,    "BinningLeakage",      "lin");
+    MBinning binsT(51, -0.005, 0.505,  "BinningTheta",        "asin");
+    MBinning binsC(50,  1e-2,  1,      "BinningConc",         "log");
+    MBinning binsW(50,  0,     0.5,    "BinningLength",       "lin");
+    MBinning binsM(50,  0,     0.3,    "BinningWidth",        "lin");
+    MBinning binsV(75,  0, par.GetVal()*25, "BinningThetaSq", "lin");
+
+    plist.AddToList(&binsG);
+    plist.AddToList(&binsS);
+    plist.AddToList(&binsX);
+    plist.AddToList(&binsE);
+    plist.AddToList(&binsL);
+    plist.AddToList(&binsT);
+    plist.AddToList(&binsC);
+    plist.AddToList(&binsV);
+    plist.AddToList(&binsW);
+    plist.AddToList(&binsM);
+
+    // ----------- Setup some histograms ----------------
+
     MHThetaSq hist;
     hist.SkipHistTime();
@@ -302,46 +377,89 @@
     hist.SkipHistEnergy();
 
-    MFillH fillh(&hist, "", "FillThetaSq");
-
-    // 0 =  disp^2 - 2*disp*dist*cos(alpha) + dist^2
-
-    // cos^2 -1 = - sin^2
-
-    // disp = +dist* (cos(alpha) +/- sqrt(cos^2(alpha) - 1) )
-
-    const char *rule = "(MHillasSrc.fDist*MGeomCam.fConvMm2Deg)^2 + (Disp.fVal)^2 - (2*MHillasSrc.fDist*MGeomCam.fConvMm2Deg*Disp.fVal*cos(MHillasSrc.fAlpha*kDeg2Rad))";
-
-    MParameterCalc calcthetasq(rule, "MThetaSqCalc");
-    calcthetasq.SetNameParameter("ThetaSquared");
-
-    MChisqEval eval;
-    eval.SetY1("sqrt(ThetaSquared.fVal)");
+    // To speed it up we could precalculate it.
+    const char *res = "Disp.fVal-MHillasSrc.fDist*3.37e-3";
+
+    MHn hres1("Disp1", "Xi Residual (Dist/Disp)");
+    hres1.AddHist("MHillas.fSize", res);
+    hres1.InitName("ResSize;Size;ResidualDist");
+    hres1.InitTitle(";S [phe];Disp-Dist [\\circ];");
+    hres1.SetDrawOption("colz profx");
+    hres1.AddHist("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)/3.37e-3", res);
+    hres1.InitName("ResSlope;Slope;ResidualDist");
+    hres1.InitTitle(";Slope;Disp-Dist [\\circ];");
+    hres1.SetDrawOption("colz profx");
+    hres1.AddHist("MNewImagePar.fLeakage1", res);
+    hres1.InitName("ResLeak;Leakage;ResidualDist");
+    hres1.InitTitle(";Leak;Disp-Dist [\\circ];");
+    hres1.SetDrawOption("colz profx");
+    hres1.AddHist("MPointingPos.fZd", res);
+    hres1.InitName("ResTheta;Theta;ResidualDist");
+    hres1.InitTitle(";Zd [\\circ];Disp-Dist [\\circ];");
+    hres1.SetDrawOption("colz profx");
+
+    MHn hres2("Disp2", "Dist Residual (Disp-Dist)");
+    hres2.AddHist("MHillas.fLength*3.37e-3", res);
+    hres2.InitName("ResLength;Length;ResidualDist");
+    hres2.InitTitle(";L [\\circ];Disp-Dist [\\circ];");
+    hres2.SetDrawOption("colz profx");
+    hres2.AddHist("MNewImagePar.fConc1", res);
+    hres2.InitName("ResConc1;Conc;ResidualDist");
+    hres2.InitTitle(";C;Disp-Dist [\\circ];");
+    hres2.SetDrawOption("colz profx");
+    hres2.AddHist("MHillas.fWidth*3.37e-3", res);
+    hres2.InitName("ResWidth;Width;ResidualDist");
+    hres2.InitTitle(";W [\\circ];Disp-Dist [\\circ];");
+    hres2.SetDrawOption("colz profx");
+    hres2.AddHist("MMcEvt.fEnergy", res);
+    hres2.InitName("ResEmc;Energy;ResidualDist");
+    hres2.InitTitle(";E_{mc} [GeV];Disp-Dist [\\circ];");
+    hres2.SetDrawOption("colz profx");
 
     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( 70, 10, 31623, "BinningMH3X", "log");
-    MBinning binsy( 75, 0,  0.3,   "BinningMH3Y", "lin");
-
-    plist.AddToList(&binsx);
-    plist.AddToList(&binsy);
-
-    MFillH fillh2a(&hdisp1, "", "FillSize");
-    MFillH fillh2b(&hdisp2, "", "FillEnergy");
-    fillh2a.SetDrawOption("colz profx");
-    fillh2b.SetDrawOption("colz profx");
-    fillh2a.SetNameTab("Size");
-    fillh2b.SetNameTab("Energy");
+    hdisp1.SetName("Size;Size;ThetaSq");
+    hdisp2.SetName("Energy;Energy;ThetaSq");
+    hdisp1.SetTitle("\\vartheta distribution vs. Size:Size [phe]:\\vartheta^2 [\\circ]");
+    hdisp2.SetTitle("\\vartheta distribution vs. Energy:Energy [GeV]:\\vartheta^2 [\\circ]");
+
+    // -------------- Setup fill tasks ----------------
+
+    MFillH fillh(&hist, "", "FillThetaSq");
+    MFillH fillh2a(&hres1, "", "FillResiduals1");
+    MFillH fillh2b(&hres2, "", "FillResiduals2");
+    MFillH fillh2c(&hdisp1, "", "FillSize");
+    MFillH fillh2d(&hdisp2, "", "FillEnergy");
+    fillh2c.SetBit(MFillH::kDoNotDisplay);
+    fillh2d.SetBit(MFillH::kDoNotDisplay);
+
+    // --------------- Setup weighting -------------------
+
+    if (fEnableWeights)
+    {
+        fillh.SetWeight();
+        fillh2a.SetWeight();
+        fillh2b.SetWeight();
+        fillh2c.SetWeight();
+        fillh2d.SetWeight();
+        eval.SetNameWeight();
+    }
+
+    // --------------- Setup tasklist -------------------
 
     tlist.AddToList(&readtst);
+    tlist.AddToList(fPreTasks);
     tlist.AddToList(&cont);
     tlist.AddToList(&rf);
     tlist.AddToList(&calcthetasq);
+    tlist.AddToList(fPostTasks);
     tlist.AddToList(&fillh);
     tlist.AddToList(&fillh2a);
     tlist.AddToList(&fillh2b);
+    tlist.AddToList(&fillh2c);
+    tlist.AddToList(&fillh2d);
+    tlist.AddToList(fTestTasks);
     tlist.AddToList(&eval);
+
+    // ------------- Setup/run eventloop -----------------
 
     MEvtLoop loop(fTitle);
@@ -355,4 +473,6 @@
         return kFALSE;
 
+    // ---------------- Prepare result -------------------
+
     // Print the result
     *fLog << inf;
@@ -361,11 +481,12 @@
     hist.GetAlphaFitter().Print("result");
 
+    // The user has closed the display
+    if (!fDisplay)
+        return kTRUE;
+
     DisplayResult(hdisp1, hdisp2);
 
     SetPathOut(out);
-    if (!WriteDisplay(0, "UPDATE"))
-        return kFALSE;
-
-    return kTRUE;
+    return WriteDisplay(0, "UPDATE");
 }
 
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 8698)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 8704)
@@ -76,4 +76,6 @@
 
 // histograms
+#include "MHn.h"
+#include "MBinning.h"
 #include "MHEnergyEst.h"
 
@@ -128,5 +130,5 @@
     train.AddColumn("MMcEvt.fImpact/100");
     train.AddColumn("MMcEvt.fTelescopeTheta*TMath::RadToDeg()");
-    train.AddColumn("MMcEvt.fEnergy");
+    train.AddColumn(fTrainParameter);
 
 
@@ -156,4 +158,5 @@
     rf.SetDebug(fDebug>1);
     rf.SetNameOutput("MEnergyEst");
+    rf.SetFunction(fResultFunction);
 
     /*
@@ -188,8 +191,76 @@
     cont.SetInverted();
 
+    // -------------------------------------------------------------
+    MBinning binsS(50, 10,     100000, "BinningSize",           "log");
+    MBinning binsE(70, 10,     31623,  "BinningEnergy",         "log");
+    MBinning binsG(50,-10,     10,     "BinningSlope",          "lin");
+    MBinning binsR(50, -1,     1,      "BinningEnergyResidual", "lin");
+    MBinning binsL(50,  0,     0.3,    "BinningLeakage",        "lin");
+    MBinning binsT(51, -0.005, 0.505,  "BinningTheta",          "asin");
+    MBinning binsD(50,  0,     1.6,    "BinningDist",           "lin");
+    MBinning binsC(50,  1e-2,  1,      "BinningConc",           "log");
+
+    plist.AddToList(&binsG);
+    plist.AddToList(&binsS);
+    plist.AddToList(&binsR);
+    plist.AddToList(&binsE);
+    plist.AddToList(&binsL);
+    plist.AddToList(&binsT);
+    plist.AddToList(&binsD);
+    plist.AddToList(&binsC);
+
     MHEnergyEst hist;
+
+    // To speed it up we could precalculate it.
+    const char *res = "log10(MEnergyEst.fVal)-log10(MMcEvt.fEnergy)";
+
+    MHn hres1("Energy1", "Energy Residual (lg E_{est} - lg E_{mc})");
+    hres1.AddHist("MHillas.fSize", res);
+    hres1.InitName("ResSize;Size;EnergyResidual");
+    hres1.InitTitle(";S [phe];\\Delta lg E;");
+    hres1.SetDrawOption("colz profx");
+    hres1.AddHist("MHillasExt.fSlopeLong*sign(MHillasSrc.fCosDeltaAlpha)/3.37e-3", res);
+    hres1.InitName("ResSlope;Slope;EnergyResidual");
+    hres1.InitTitle(";Slope;\\Delta lg E;");
+    hres1.SetDrawOption("colz profx");
+    hres1.AddHist("MNewImagePar.fLeakage1", res);
+    hres1.InitName("ResLeak;Leakage;EnergyResidual");
+    hres1.InitTitle(";Leak;\\Delta lg E;");
+    hres1.SetDrawOption("colz profx");
+    hres1.AddHist("MHillasSrc.fDist*3.37e-3", res);
+    hres1.InitName("ResDist;Dist;EnergyResidual");
+    hres1.InitTitle(";D [\\circ];\\Delta lg E;");
+    hres1.SetDrawOption("colz profx");
+
+    MHn hres2("Energy2", "Energy Residual (lg E_{est} - lg E_{mc})");
+    hres2.AddHist("MMcEvt.fEnergy", res);
+    hres2.InitName("ResEmc;Energy;EnergyResidual");
+    hres2.InitTitle(";E_{mc} [GeV];\\Delta lg E;");
+    hres2.SetDrawOption("colz profx");
+    hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
+    hres2.AddHist("MPointingPos.fZd", res);
+    hres2.InitName("ResTheta;Theta;EnergyResidual");
+    hres2.InitTitle(";Zd [\\circ];\\Delta lg E;");
+    hres2.SetDrawOption("colz profx");
+    hres2.AddHist("MEnergyEst.fVal", res);
+    hres2.InitName("ResEest;Energy;EnergyResidual");
+    hres2.InitTitle(";E_{est} [GeV];\\Delta lg E;");
+    hres2.SetDrawOption("colz profx");
+    hres2.SetAutoRange(kFALSE, kFALSE, kFALSE);
+    hres2.AddHist("MNewImagePar.fConc1", res);
+    hres2.InitName("ResConc1;Conc;EnergyResidual");
+    hres2.InitTitle(";C;\\Delta lg E;");
+    hres2.SetDrawOption("colz profx");
+
     MFillH fillh(&hist);
+    MFillH fillh1(&hres1, "", "FillResiduals1");
+    MFillH fillh2(&hres2, "", "FillResiduals2");
+
     if (fEnableWeights)
+    {
         fillh.SetWeight();
+        fillh1.SetWeight();
+        fillh2.SetWeight();
+    }
 
     tlist.AddToList(&readtst);
@@ -199,4 +270,7 @@
     tlist.AddToList(fPostTasks);
     tlist.AddToList(&fillh);
+    tlist.AddToList(&fillh1);
+    tlist.AddToList(&fillh2);
+    tlist.AddToList(fTestTasks);
 
     MEvtLoop loop(fTitle);
Index: trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.h
===================================================================
--- trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.h	(revision 8698)
+++ trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.h	(revision 8704)
@@ -10,6 +10,20 @@
 class MJTrainEnergy : public MJTrainRanForest
 {
+private:
+    TString fTrainParameter;
+    TString fResultFunction;
+
 public:
-    MJTrainEnergy() { }
+    MJTrainEnergy() { SetTrainLin(); }
+
+    void SetTrainLog() { SetTrainFunc("log10(MMcEvt.fEnergy)", "pow(10, x)"); }
+    void SetTrainLin() { SetTrainFunc("MMcEvt.fEnergy", "x"); }
+
+    void SetTrainFunc(const char *par, const char *res)
+    {
+        fTrainParameter = par;
+        fResultFunction = res;
+    }
+
     Bool_t Train(const char *out, const MDataSet &set, Int_t num);
 
