Index: trunk/MagicSoft/Mars/mfbase/MFEventSelector2.cc
===================================================================
--- trunk/MagicSoft/Mars/mfbase/MFEventSelector2.cc	(revision 5448)
+++ trunk/MagicSoft/Mars/mfbase/MFEventSelector2.cc	(revision 5667)
@@ -19,5 +19,5 @@
 !   Author(s): Wolfgang Wittek 11/2003 <mailto:wittek@mppmu.mpg.de>
 !
-!   Copyright: MAGIC Software Development, 2000-2003
+!   Copyright: MAGIC Software Development, 2000-2005
 !
 !
@@ -29,5 +29,5 @@
 //
 // This is a filter to make a selection of events from a file, according to
-// a certain predetermined distribution in a given parameter (or combination
+// a certain requested distribution in a given parameter (or combination
 // of parameters). The distribution is passed to the class through a histogram
 // of the relevant parameter(s) contained in an object of type MH3. The filter
@@ -38,4 +38,9 @@
 // values of the parameters, and is dictated by the input histogram).
 //
+// This procedure requires the determination of the original distribution
+// of the given parameters for the total sample of events on the input file.
+// If the event loop contains a filter with name "FilterSelector2", this
+// filter will be applied when determining the original distribution.
+//
 // See Constructor for more instructions and also the example below:
 //
@@ -111,7 +116,9 @@
 #include "MH3.h"            // MH3
 #include "MRead.h"          // MRead
+//#include "MProgressBar.h"   // 
 #include "MEvtLoop.h"       // MEvtLoop
 #include "MTaskList.h"      // MTaskList
 #include "MBinning.h"       // MBinning
+#include "MContinue.h"      // 
 #include "MFillH.h"         // MFillH
 #include "MParList.h"       // MParList
@@ -148,13 +155,19 @@
 //     note that also in this case a dummy nominal distribution has to be
 //     provided in the first argument (the dummy distribution defines the
-//     variable(s) of interest and the binnings)
+//     variable(s) of interest, their binnings and their requested ranges;
+//     events outside these ranges won't be accepted).
 //
 MFEventSelector2::MFEventSelector2(MH3 &hist, const char *name, const char *title)
 : fHistOrig(NULL), fHistNom(&hist), fHistRes(NULL),
   fDataX(hist.GetRule('x')), fDataY(hist.GetRule('y')),
-  fDataZ(hist.GetRule('z')), fNumMax(-1), fHistIsProbability(kFALSE)
+  fDataZ(hist.GetRule('z')), fNumMax(-1), fHistIsProbability(kFALSE),
+  fUseOrigDist(kTRUE)
 {
     fName  = name  ? (TString)name  : gsDefName;
     fTitle = title ? (TString)title : gsDefTitle;
+
+    // name of filter to be applied when determining the original distribution
+    // for all data on the input file
+    fFilterName = "FilterSelector2";
 }
 
@@ -195,5 +208,5 @@
 // Reading task of the present loop is used in a new eventloop.
 //
-Bool_t MFEventSelector2::ReadDistribution(MRead &read)
+Bool_t MFEventSelector2::ReadDistribution(MRead &read, MFilter *filter)
 {
     if (read.GetEntries() > kMaxUInt) // FIXME: LONG_MAX ???
@@ -205,7 +218,19 @@
     *fLog << inf << underline << endl;
     *fLog << "MFEventSelector2::ReadDistribution:" << endl;
-    *fLog << " - Start of eventloop to generate the original distribution..." << endl;
-
-    MEvtLoop run(GetName());
+    *fLog << "**********************" << endl;
+    *fLog << " - Start of eventloop to generate the original distribution..." 
+          << endl;
+
+    if (filter != NULL)
+    {
+      *fLog << "                      filter used : " << filter->GetName()
+            << endl;
+    }
+
+
+    MEvtLoop run("ReadDistribution");
+    //MProgressBar bar;
+    //run.SetProgressBar(&bar);
+
     MParList plist;
     MTaskList tlist;
@@ -224,20 +249,34 @@
 
     MFillH fill(fHistOrig);
+    fill.SetName("FillHistOrig");
     fill.SetBit(MFillH::kDoNotDisplay);
     tlist.AddToList(&read);
+
+    MContinue contfilter(filter);
+    if (filter != NULL)
+    {
+      contfilter.SetName("ContFilter");
+      tlist.AddToList(&contfilter);
+    }
+
     tlist.AddToList(&fill);
     run.SetDisplay(fDisplay);
     if (!run.Eventloop())
     {
-        *fLog << err << dbginf << "Evtloop in MFEventSelector2::ReadDistribution failed." << endl;
+        *fLog << err << dbginf 
+              << "Evtloop in MFEventSelector2::ReadDistribution failed." 
+              << endl;
         return kFALSE;
     }
 
-    tlist.PrintStatistics();
+    tlist.PrintStatistics(0, kTRUE);
 
     *fLog << inf;
     *fLog << "MFEventSelector2::ReadDistribution:" << endl;
-    *fLog << " - Original distribution has " << fHistOrig->GetHist().GetEntries() << " entries." << endl;
-    *fLog << " - End of eventloop to generate the original distribution." << endl;
+    *fLog << " - Original distribution has " 
+          << fHistOrig->GetHist().GetEntries() << " entries." << endl;
+    *fLog << " - End of eventloop to generate the original distribution." 
+          << endl;
+    *fLog << "**********************" << endl;
 
     return read.Rewind();
@@ -258,6 +297,7 @@
     // set the nominal distribution equal to the original distribution
 
-    const Bool_t useorigdist = fHistNom->GetHist().GetEntries()==0;
-    TH1 *hnp =  useorigdist ? (TH1*)(fHistOrig->GetHist()).Clone() : &fHistNom->GetHist();
+    const Bool_t fUseOrigDist = fHistNom->GetHist().GetEntries()==0;
+    TH1 *hnp =  fUseOrigDist ? (TH1*)(fHistOrig->GetHist()).Clone() : 
+                              &fHistNom->GetHist();
 
     TH1 &hn = *hnp;
@@ -327,5 +367,5 @@
     }
 
-    if (useorigdist)
+    if (fUseOrigDist)
       delete hnp;
 }
@@ -370,5 +410,5 @@
 //  3) Initialize the histogram for the resulting distribution
 //  4) Prepare the random selection
-//  5) Repreprocess the reading task.
+//  5) Repreprocess the reading and filter task.
 //
 Int_t MFEventSelector2::PreProcess(MParList *parlist)
@@ -386,5 +426,6 @@
         return kFALSE;
 
-    fHistNom->SetTitle(fHistIsProbability ? "ProbabilityDistribution" : "Users Nominal Distribution");
+    fHistNom->SetTitle(fHistIsProbability ? "ProbabilityDistribution" : 
+                                            "Users Nominal Distribution");
 
     if (fHistIsProbability)
@@ -410,11 +451,29 @@
     }
 
-    if (!ReadDistribution(*read))
+    MFilter *filter = (MFilter*)tasklist->FindObject(fFilterName);
+    if (!filter)
+    {
+      *fLog << inf 
+            << "No filter will be used when making the original distribution"
+            << endl;
+      filter = NULL;
+    }
+
+
+    if (!ReadDistribution(*read, filter))
         return kFALSE;
+
 
     // Prepare histograms and arrays for selection
     PrepareHistograms();
 
-    return read->CallPreProcess(parlist);
+    *fLog << "MFEventSelector2::PreProcess; call PreProcess() for read and filter object again"
+          << endl;
+    Int_t rcr = read->CallPreProcess(parlist);
+    Int_t rcf = filter->CallPreProcess(parlist);
+
+    *fLog << "rcr, rcf = " << rcr << ",  " << rcf << endl;
+
+    return rcf*rcr;
 }
 
@@ -426,5 +485,5 @@
 Bool_t MFEventSelector2::Select(Int_t bin)
 {
-    // under- and overflow bins are not counted
+    // under- and overflow bins are not accepted
     if (bin<0)
         return kFALSE;
@@ -450,4 +509,6 @@
 }
 
+// --------------------------------------------------------------------------
+//
 Bool_t MFEventSelector2::SelectProb(Int_t ibin) const
 {
@@ -455,5 +516,6 @@
     // If value is outside histogram range, accept event
     //
-    return ibin<0 ? kTRUE : fHistNom->GetHist().GetBinContent(ibin) > gRandom->Uniform();
+    return ibin<0 ? kTRUE : 
+           fHistNom->GetHist().GetBinContent(ibin) > gRandom->Uniform();
 }
 
@@ -473,11 +535,26 @@
     const Double_t valz=fDataZ.GetValue();
 
+    // don't except the event if it is outside the axis range
+    // of the requested distribution
     const Int_t ibin = fHistNom->FindFixBin(valx, valy, valz)-1;
-
-    // Get corresponding bin number and check
-    // whether a selection should be made
+    if (!fHistIsProbability)
+    {
+      if (ibin < 0)
+      {
+        fResult = kFALSE;
+        fCounter[1] += 1;
+        return kTRUE;
+      }
+    }
+
+    // check whether a selection should be made
     fResult = fHistIsProbability ? SelectProb(ibin) : Select(ibin);
-
-    fCounter[fResult ? 1 : 0]++;
+    if (!fResult)
+    {
+      fCounter[2] += 1;
+      return kTRUE;
+    }
+
+    fCounter[0] += 1;
 
     return kTRUE;
@@ -497,10 +574,16 @@
         *fLog << GetDescriptor() << " execution statistics:" << endl;
         *fLog << dec << setfill(' ');
+
         *fLog << " " << setw(7) << fCounter[1] << " (" << setw(3)
-              << (int)(fCounter[0]*100/GetNumExecutions())
-              << "%) Events not selected" << endl;
+              << (int)((Float_t)(fCounter[1]*100)/(Float_t)(GetNumExecutions())+0.5)
+              << "%) Events not selected due to under/over flow" << endl;
+
+        *fLog << " " << setw(7) << fCounter[2] << " (" << setw(3)
+              << (int)((Float_t)(fCounter[2]*100)/(Float_t)(GetNumExecutions())+0.5)
+              << "%) Events not selected due to requested distribution" 
+              << endl;
 
         *fLog << " " << fCounter[0] << " ("
-              << (int)(fCounter[1]*100/GetNumExecutions())
+              << (int)((Float_t)(fCounter[0]*100)/(Float_t)(GetNumExecutions())+0.5)
               << "%) Events selected" << endl;
         *fLog << endl;
@@ -519,2 +602,8 @@
     return kTRUE;
 }
+
+
+
+
+
+
Index: trunk/MagicSoft/Mars/mfbase/MFEventSelector2.h
===================================================================
--- trunk/MagicSoft/Mars/mfbase/MFEventSelector2.h	(revision 5448)
+++ trunk/MagicSoft/Mars/mfbase/MFEventSelector2.h	(revision 5667)
@@ -41,10 +41,16 @@
     TCanvas   *fCanvas;   //! canvas for online display
 
+    TString    fFilterName; // name of the MFilter object to be used
+                            // when determining the original distribution
+
     Bool_t fResult;
     Bool_t fHistIsProbability;
-    Int_t  fCounter[2];
+    Bool_t fUseOrigDist;    // flag indicating that in the selection the
+                            // original distribution is not changed
+
+    Int_t  fCounter[3];
 
     TH1   &InitHistogram(MH3* &hist);
-    Bool_t ReadDistribution(MRead &read);
+    Bool_t ReadDistribution(MRead &read, MFilter *filter);
     void   PrepareHistograms();
     Bool_t PreProcessData(MParList *parlist);
