Index: trunk/MagicSoft/Mars/mtools/MChisqEval.cc
===================================================================
--- trunk/MagicSoft/Mars/mtools/MChisqEval.cc	(revision 2741)
+++ trunk/MagicSoft/Mars/mtools/MChisqEval.cc	(revision 2744)
@@ -32,4 +32,7 @@
 
 #include "MDataChain.h"
+#include "MParameters.h" // MParameterD
+
+#include "MParList.h"
 
 ClassImp(MChisqEval);
@@ -120,4 +123,8 @@
             return kFALSE;
 
+    fResult = (MParameterD*)plist->FindCreateObj("MParameterD", "MFitResult");
+    if (!fResult)
+        return kFALSE;
+
     return kTRUE;
 }
@@ -138,4 +145,7 @@
 {
     fChisq /= GetNumExecutions();
+
+    fResult->SetVal(fChisq);
+
     return kTRUE;
 }
Index: trunk/MagicSoft/Mars/mtools/MChisqEval.h
===================================================================
--- trunk/MagicSoft/Mars/mtools/MChisqEval.h	(revision 2741)
+++ trunk/MagicSoft/Mars/mtools/MChisqEval.h	(revision 2744)
@@ -7,4 +7,5 @@
 
 class MData;
+class MParameterD;
 
 class MChisqEval : public MTask
@@ -14,5 +15,6 @@
     static const TString gsDefTitle;
 
-    Double_t fChisq; //! Evaluated chi square
+    Double_t     fChisq;  //! Evaluated chi square
+    MParameterD *fResult; //! Storage for result
 
     MData   *fData0; // Data Member one (monte carlo data or chisq function)
Index: trunk/MagicSoft/Mars/mtools/MTFillMatrix.cc
===================================================================
--- trunk/MagicSoft/Mars/mtools/MTFillMatrix.cc	(revision 2741)
+++ trunk/MagicSoft/Mars/mtools/MTFillMatrix.cc	(revision 2744)
@@ -35,4 +35,7 @@
 // reference histogram.
 //
+// If no reference histogram is available the number of events are
+// randomly choosen from the sample with a probability which fits
+// the total destination number of events.
 //
 // Here is an example of how to choose 1000 events somehow distributed in
@@ -49,5 +52,5 @@
 // read.DisableAutoScheme();           // make sure everything is read
 //
-// MTFillMatrix fill(ref);             // setup MTFillMatrix
+// MTFillMatrix fill(&ref);            // setup MTFillMatrix
 // fill.SetNumDestEvents1(1000);       // setup number of events to select
 // fill.SetDestMatrix1(&matrix1);      // setup destination matrix
@@ -91,5 +94,5 @@
 #include "MContinue.h"
 #include "MFilterList.h"
-#include "MFRandomSplit.h"
+#include "MFEventSelector.h"
 #include "MFEventSelector2.h"
 
@@ -151,13 +154,23 @@
 // at MFEventSelector2 which is used to select the events.
 //
-// FIXME: Make a copy of ref.
-//
-MTFillMatrix::MTFillMatrix(const MH3 &ref)
-: fReference(ref), fReader(0), fDestMatrix1(0),
-fDestMatrix2(0), fNumDestEvents1(0), fNumDestEvents2(0),
-fWriteFile1(0), fWriteFile2(0)
+// If no MH3 *ref is given the events are randomly selected from the
+// total sample - this may result in samples which don't have exactly
+// the predefined size, but it is much faster.
+//
+MTFillMatrix::MTFillMatrix(const MH3 *ref)
+: 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";
+
+    if (ref)
+        fReference = (MH3*)ref->Clone();
+}
+
+MTFillMatrix::~MTFillMatrix()
+{
+    if (fReference)
+        delete fReference;
 }
 
@@ -178,7 +191,7 @@
     *fLog << "Fill " << fDestMatrix1->GetDescriptor() << " with " << fNumDestEvents1 << endl;
     *fLog << "Fill " << fDestMatrix2->GetDescriptor() << " with " << fNumDestEvents2 << endl;
-        *fLog << "Distribution choosen ";
-    if (fReference.GetHist().GetEntries()>0)
-        *fLog << "from " << fReference.GetDescriptor();
+    *fLog << "Distribution choosen ";
+    if (fReference && fReference->GetHist().GetEntries()>0)
+        *fLog << "from " << fReference->GetDescriptor();
     else
         *fLog << "randomly";
@@ -195,7 +208,26 @@
     // A selector to select a given number of events from a sample
     //
-    MFEventSelector2 selector(fReference);
-    selector.SetNumMax(fNumDestEvents1+fNumDestEvents2);
-    selector.SetInverted();
+    // FIXME: Merge MFEventSelector and MFEventSelector2
+    MFilter *selector=0;
+    if (fReference)
+    {
+        // Case of a reference/nominal distribution
+        // The events must be read before selection
+        MFEventSelector2 *sel = new MFEventSelector2(*fReference);
+        sel->SetNumMax(fNumDestEvents1+fNumDestEvents2);
+        sel->SetInverted();
+
+        selector = sel;
+    }
+    else
+    {
+        // Case of a random distribution
+        // The events can be selected before reading
+        MFEventSelector *sel = new MFEventSelector;
+        sel->SetNumSelectEvts(fNumDestEvents1+fNumDestEvents2);
+        fReader->SetSelector(sel);
+
+        selector = sel;
+    }
 
     //
@@ -203,5 +235,5 @@
     // selected by the 'selector'
     //
-    MContinue cont(&selector);
+    MContinue cont(selector);
 
     //
@@ -209,5 +241,6 @@
     //
     const Double_t prob = (Double_t)fNumDestEvents1/(fNumDestEvents1+fNumDestEvents2);
-    MFRandomSplit split(prob);
+    MFEventSelector split;
+    split.SetSelectionRatio(prob);
 
     //
@@ -227,11 +260,12 @@
 
     // entries in MTaskList
-    tlist.AddToList(fReader);    // Read events
-    tlist.AddToList(&cont);      // select a sample of events
-    tlist.AddToList(&invsplit);  // process invsplit (which implicitly processes split)
+    tlist.AddToList(fReader);        // Read events
+    if (fReference)
+        tlist.AddToList(&cont);      // select a sample of events
+    tlist.AddToList(&invsplit);      // process invsplit (which implicitly processes split)
     if (fDestMatrix1 && fNumDestEvents1>0)
-        tlist.AddToList(&fill1); // fill matrix 1
+        tlist.AddToList(&fill1);     // fill matrix 1
     if (fDestMatrix2 && fNumDestEvents2>0)
-        tlist.AddToList(&fill2); // fill matrix 2
+        tlist.AddToList(&fill2);     // fill matrix 2
     if (fWriteFile1)
     {
@@ -252,12 +286,18 @@
     evtloop.SetDisplay(fDisplay);
     evtloop.SetLogStream(fLog);
-    if (!evtloop.Eventloop())
+
+    const Bool_t rc = evtloop.Eventloop();
+
+    // Print execution statistics of the tasklist
+    if (rc)
+        tlist.PrintStatistics();
+
+    delete selector;
+
+    if (!rc)
     {
         *fLog << err << GetDescriptor() << ": Failed." << endl;
         return kFALSE;
     }
-
-    // Print execution statistics of the tasklist
-    tlist.PrintStatistics();
 
     // Check the result of filling...
Index: trunk/MagicSoft/Mars/mtools/MTFillMatrix.h
===================================================================
--- trunk/MagicSoft/Mars/mtools/MTFillMatrix.h	(revision 2741)
+++ trunk/MagicSoft/Mars/mtools/MTFillMatrix.h	(revision 2744)
@@ -13,5 +13,5 @@
 {
 private:
-    MH3       fReference;
+    MH3      *fReference;
     MRead    *fReader;
     MHMatrix *fDestMatrix1;
@@ -26,5 +26,6 @@
 
 public:
-    MTFillMatrix(const MH3 &ref);
+    MTFillMatrix(const MH3 *ref=NULL);
+    ~MTFillMatrix();
 
     void SetDestMatrix1(MHMatrix *matrix, UInt_t num=0)
