Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 7699)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 7700)
@@ -73,4 +73,7 @@
        fPostTasks lists
 
+   * mjtrain/MJTrainEnergy.cc: 
+     - implemented usage of weights
+
 
 
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 7699)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainEnergy.cc	(revision 7700)
@@ -107,4 +107,6 @@
     MHMatrix train("Train");
     train.AddColumns(fRules);
+    if (fEnableWeights)
+        train.AddColumn("MWeight.fVal");
     train.AddColumn("MMcEvt.fImpact/100");
     train.AddColumn("MMcEvt.fTelescopeTheta*kRad2Deg");
@@ -120,4 +122,6 @@
     fill.AddPreCuts(fPreCuts);
     fill.AddPreCuts(fTrainCuts);
+    fill.AddPreTasks(fPreTasks);
+    fill.AddPostTasks(fPostTasks);
     if (!fill.Process())
         return kFALSE;
@@ -129,4 +133,5 @@
     rf.SetNumTry(fNumTry);
     rf.SetNumObsoleteVariables(3);
+    rf.SetLastDataColumnHasWeights(fEnableWeights);
     rf.SetDisplay(fDisplay);
     rf.SetLogStream(fLog);
@@ -168,8 +173,12 @@
     MHEnergyEst hist;
     MFillH fillh(&hist);
+    if (fEnableWeights)
+        fillh.SetWeight();
 
     tlist.AddToList(&readtst);
+    tlist.AddToList(fPreTasks);
     tlist.AddToList(&cont);
     tlist.AddToList(&rf);
+    tlist.AddToList(fPostTasks);
     tlist.AddToList(&fillh);
 
Index: /trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc
===================================================================
--- /trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 7699)
+++ /trunk/MagicSoft/Mars/mjtrain/MJTrainSeparation.cc	(revision 7700)
@@ -47,5 +47,4 @@
 #include "MDataSet.h"
 #include "MTFillMatrix.h"
-#include "MChisqEval.h"
 #include "MStatusDisplay.h"
 
@@ -59,4 +58,6 @@
 #include "MContinue.h"
 #include "MFillH.h"
+#include "MSrcPosRndm.h"
+#include "MHillasCalc.h"
 #include "MRanForestCalc.h"
 #include "MParameterCalc.h"
@@ -201,4 +202,5 @@
     gPad->SetGridx();
     gPad->SetGridy();
+    gr4.SetMinimum(0);
     gr4.SetMarkerStyle(kFullDotMedium);
     gr4.DrawClone("ALP")->SetBit(kCanDelete);
@@ -400,5 +402,5 @@
     // Target spectrum
     TF1 flx("Flux", "[0]/1000*(x/1000)^(-2.6)", min, max);
-    flx.SetParameter(0, 2e-7);
+    flx.SetParameter(0, fFlux);
 
     // Number n0 of events this spectrum would produce per s and m^2
@@ -430,4 +432,10 @@
     *fLog << "Total effective on time:                                  " << data/r  << "s" << endl;
 
+    const Double_t ratio = T*r/data;
+    *fLog << "Ratio of Monte Carlo to data observation time:            " << ratio << endl;
+
+    // 3570.5/43440.2 = 0.082
+
+
     // this yields a number of n events to be read for training
     const Double_t n = r*T;                        //[#]
@@ -443,5 +451,5 @@
     *fLog << "Events available in MC sample:                            " << nummc << endl;
 
-    *fLog << "MC read probability:                                      " << data/n << endl;
+//    *fLog << "MC read probability:                                      " << data/n << endl;
 
     // more data requested than available => Scale down num MC events
@@ -503,4 +511,10 @@
     {
         *fLog << err << "ERROR - DataSet for testing invalid!" << endl;
+        return kFALSE;
+    }
+
+    if (fDataSetTrain.IsWobbleMode()!=fDataSetTest.IsWobbleMode())
+    {
+        *fLog << err << "ERROR - Train- and Test-DataSet have different observation modes!" << endl;
         return kFALSE;
     }
@@ -528,4 +542,5 @@
     read4.DisableAutoScheme();
 
+    // Setup four reading tasks with the on- and off-data of the two datasets
     fDataSetTrain.AddFilesOn(read1);
     fDataSetTrain.AddFilesOff(read3);
@@ -534,17 +549,25 @@
     fDataSetTest.AddFilesOn(read4);
 
-    // ----------------------- Setup RF ----------------------
+    // ----------------------- Setup RF Matrix ----------------------
     MHMatrix train("Train");
     train.AddColumns(fRules);
+    if (fEnableWeightsOn || fEnableWeightsOff)
+        train.AddColumn("MWeight.fVal");
     train.AddColumn("MHadronness.fVal");
 
     // ----------------------- Fill Matrix RF ----------------------
 
+    // Setup the hadronness container identifying gammas and off-data
+    // and setup a container for the weights
     MParameterD had("MHadronness");
-
+    MParameterD wgt("MWeight");
+
+    // Add them to the parameter list
     MParList plistx;
     plistx.AddToList(&had);
+    plistx.AddToList(&wgt);
     plistx.AddToList(this);
 
+    // Setup the tool class to fill the matrix
     MTFillMatrix fill;
     fill.SetLogStream(fLog);
@@ -555,10 +578,18 @@
     // Set classifier for gammas
     had.SetVal(0);
+    wgt.SetVal(1);
+
+    // Setup the tool class to read the gammas and read them
     fill.SetName("FillGammas");
     fill.SetDestMatrix1(&train, fNumTrainOn);
     fill.SetReader(&read1);
+    fill.AddPreTasks(fPreTasksOn);
+    fill.AddPreTasks(fPreTasks);
+    fill.AddPostTasks(fPostTasksOn);
+    fill.AddPostTasks(fPostTasks);
     if (!fill.Process(plistx))
         return kFALSE;
 
+    // Check the number or read events
     const Int_t numgammastrn = train.GetNumRows();
     if (numgammastrn==0)
@@ -568,12 +599,37 @@
     }
 
-    // Set classifier for hadrons
+    // Remove possible post tasks
+    fill.ClearPreTasks();
+    fill.ClearPostTasks();
+
+    // Set classifier for background
     had.SetVal(1);
+    wgt.SetVal(1);
+
+    // In case of wobble mode we have to do something special
+    MSrcPosRndm srcrndm;
+    srcrndm.SetDistOfSource(0.4);
+
+    MHillasCalc hcalc;
+    hcalc.SetFlags(MHillasCalc::kCalcHillasSrc);
+
+    if (fDataSetTrain.IsWobbleMode())
+    {
+        fPreTasksOff.AddFirst(&hcalc);
+        fPreTasksOff.AddFirst(&srcrndm);
+    }
+
+    // Setup the tool class to read the background and read them
     fill.SetName("FillBackground");
     fill.SetDestMatrix1(&train, fNumTrainOff);
     fill.SetReader(&read3);
+    fill.AddPreTasks(fPreTasksOff);
+    fill.AddPreTasks(fPreTasks);
+    fill.AddPostTasks(fPostTasksOff);
+    fill.AddPostTasks(fPostTasks);
     if (!fill.Process(plistx))
         return kFALSE;
 
+    // Check the number or read events
     const Int_t numbackgrndtrn = train.GetNumRows()-numgammastrn;
     if (numbackgrndtrn==0)
@@ -590,4 +646,5 @@
     rf.SetNumTry(fNumTry);
     rf.SetNumObsoleteVariables(1);
+    rf.SetLastDataColumnHasWeights(fEnableWeightsOn || fEnableWeightsOff);
     rf.SetDebug(fDebug);
     rf.SetDisplay(fDisplay);
@@ -596,27 +653,28 @@
     rf.SetNameOutput("MHadronness");
 
+    // Train the random forest either by classification or regression
     if (fUseRegression)
     {
-        if (!rf.TrainSingleRF(train))                  // regression
+        if (!rf.TrainRegression(train)) // regression
             return kFALSE;
     }
     else
     {
-        MBinning b(2, -0.5, 1.5, "BinningHadronness", "lin");
-        if (!rf.TrainSingleRF(train, b.GetEdgesD()))   // classification
+        if (!rf.TrainSingleRF(train))   // classification
             return kFALSE;
     }
 
-    //if (!rf.TrainMultiRF(train, b.GetEdgesD()))    // classification
-    //    return;
-
-    //fDisplay = rf.GetDisplay();
-
-
+    // Output information about what was going on so far.
     *fLog << all;
-    fLog->Separator("The forest was tested with...");
+    fLog->Separator("The forest was trained with...");
 
     *fLog << "Training method:" << endl;
     *fLog << " * " << (fUseRegression?"regression":"classification") << endl;
+    if (fEnableWeightsOn)
+        *fLog << " * weights for on-data" << endl;
+    if (fEnableWeightsOff)
+        *fLog << " * weights for off-data" << endl;
+    if (fDataSetTrain.IsWobbleMode())
+        *fLog << " * random source position in a distance of 0.4°" << endl;
     *fLog << endl;
     *fLog << "Events used for training:"   << endl;
@@ -628,4 +686,5 @@
     *fLog << " * Result:     " << (float)numgammastrn/numbackgrndtrn << endl;
 
+    // Chekc if testing is requested
     if (!fDataSetTest.IsValid())
         return kTRUE;
@@ -634,4 +693,5 @@
     fLog->Separator("Test");
 
+    // Setup parlist and tasklist for testing
     MParList  plist;
     MTaskList tlist;
@@ -642,4 +702,6 @@
     plist.AddToList(&mcevt);
 
+    plist.AddToList(&wgt);
+
     // ----- Setup histograms -----
     MBinning binsy(50, 0 , 1,      "BinningMH3Y", "lin");
@@ -652,7 +714,7 @@
     MH3 h32("MHillas.fSize",  "MHadronness.fVal");
     MH3 h40("MMcEvt.fEnergy", "MHadronness.fVal");
-    h31.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
-    h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness");
-    h40.SetTitle("Background probability vs. Energy:Energy [GeV]:Hadronness");
+    h31.SetTitle("Background probability vs. Size:Size [phe]:Hadronness h");
+    h32.SetTitle("Background probability vs. Size:Size [phe]:Hadronness h");
+    h40.SetTitle("Background probability vs. Energy:Energy [GeV]:Hadronness h");
 
     MHHadronness hist;
@@ -663,4 +725,11 @@
     MFillH fillh2(&h32);
     MFillH fillh4(&h40);
+    fillh0.SetWeight("MWeight");
+    fillh1.SetWeight("MWeight");
+    fillh2.SetWeight("MWeight");
+    fillh4.SetWeight("MWeight");
+    fillh1.SetDrawOption("colz profy");
+    fillh2.SetDrawOption("colz profy");
+    fillh4.SetDrawOption("colz profy");
     fillh1.SetNameTab("Background");
     fillh2.SetNameTab("GammasH");
@@ -677,5 +746,5 @@
     c0.SetInverted();
 
-    MFEventSelector sel;
+    MFEventSelector sel; // FIXME: USING IT (WITH PROB?) in READ will by much faster!!!
     sel.SetNumSelectEvts(fNumTestOff);
 
@@ -685,11 +754,18 @@
     // ----- Setup tasklist -----
     tlist.AddToList(&read2);
+    tlist.AddToList(&c1);
+    tlist.AddToList(fPreTasksOff);
+    tlist.AddToList(fPreTasks);
     tlist.AddToList(&c0);
-    tlist.AddToList(&c1);
     tlist.AddToList(&rf);
+    tlist.AddToList(fPostTasksOff);
+    tlist.AddToList(fPostTasks);
     tlist.AddToList(&fillh0);
     tlist.AddToList(&fillh1);
 
-    // ----- Run eventloop on gammas -----
+    // Enable Acceleration
+    tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
+
+    // ----- Run eventloop on background -----
     MEvtLoop loop;
     loop.SetDisplay(fDisplay);
@@ -697,29 +773,59 @@
     loop.SetParList(&plist);
 
+    wgt.SetVal(1);
     if (!loop.Eventloop())
         return kFALSE;
 
-    // ----- Setup and run eventloop on background -----
+    // ----- Setup and run eventloop on gammas -----
     sel.SetNumSelectEvts(fNumTestOn);
     fillh0.ResetBit(MFillH::kDoNotDisplay);
 
+    // Remove PreTasksOff and PostTasksOff from the list
+    tlist.RemoveFromList(fPreTasksOff);
+    tlist.RemoveFromList(fPostTasksOff);
+
+    // replace the reading task by a new one
     tlist.Replace(&read4);
+
+    // Add the PreTasksOn directly after the reading task
+    tlist.AddToListAfter(fPreTasksOn, &c1);
+
+    // Add the PostTasksOn after rf
+    tlist.AddToListAfter(fPostTasksOn, &rf);
+
+    // Replace fillh1 by fillh2
     tlist.Replace(&fillh2);
+
+    // Add fillh4 after the new fillh2
     tlist.AddToListAfter(&fillh4, &fillh2);
 
+    // Enable Acceleration
+    tlist.SetAccelerator(MTask::kAccDontReset|MTask::kAccDontTime);
+
+    wgt.SetVal(1);
     if (!loop.Eventloop())
         return kFALSE;
 
+    // Display the result plots
     DisplayResult(h31, h32);
 
+    // Write the display
     if (!WriteDisplay(out))
         return kFALSE;
+
+    // Show what was going on in the testing
+    const Double_t numgammastst   = h32.GetHist().GetEntries();
+    const Double_t numbackgrndtst = h31.GetHist().GetEntries();
 
     *fLog << all;
     fLog->Separator("The forest was tested with...");
-
-    const Double_t numgammastst   = h32.GetHist().GetEntries();
-    const Double_t numbackgrndtst = h31.GetHist().GetEntries();
-
+    *fLog << "Test method:" << endl;
+    *fLog << " * Random Forest: " << out << endl;
+    if (fEnableWeightsOn)
+        *fLog << " * weights for on-data" << endl;
+    if (fEnableWeightsOff)
+        *fLog << " * weights for off-data" << endl;
+    if (fDataSetTrain.IsWobbleMode())
+        *fLog << " * random source position in a distance of 0.4°" << endl;
     *fLog << "Events used for test:"   << endl;
     *fLog << " * Gammas:     " << numgammastst   << endl;
