Index: /trunk/MagicSoft/Mars/Changelog
===================================================================
--- /trunk/MagicSoft/Mars/Changelog	(revision 2586)
+++ /trunk/MagicSoft/Mars/Changelog	(revision 2587)
@@ -4,4 +4,15 @@
 
                                                  -*-*- END OF LINE -*-*-
+ 2003/12/02: Thomas Bretz
+ 
+   * mfilter/MFEventSelector2.[h,cc]:
+     - fixed and reorganized screen output
+     - added some small comments to the code
+     - moved some code from Process() to the new private function Select
+     - fixed uncounted events (underflow bins were not counted)
+     - renamed nonsens fErrors to fCounter
+     - fixed usage of fDisplay
+
+
 
  2003/12/01: Markus Gaug
Index: /trunk/MagicSoft/Mars/mfilter/MFEventSelector2.cc
===================================================================
--- /trunk/MagicSoft/Mars/mfilter/MFEventSelector2.cc	(revision 2586)
+++ /trunk/MagicSoft/Mars/mfilter/MFEventSelector2.cc	(revision 2587)
@@ -17,5 +17,5 @@
 !
 !   Author(s): Thomas Bretz,   01/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
-!              Wolfgang Wittek 11/2003 <mailto:wittek@mppmu.mpg.de>
+!   Author(s): Wolfgang Wittek 11/2003 <mailto:wittek@mppmu.mpg.de>
 !
 !   Copyright: MAGIC Software Development, 2000-2003
@@ -156,10 +156,10 @@
 // --------------------------------------------------------------------------
 //
-// 
+// Delete fHistRes if instatiated
 //
 MFEventSelector2::~MFEventSelector2()
 {
-  if (fHistRes)
-    delete fHistRes;
+    if (fHistRes)
+        delete fHistRes;
 }
 
@@ -170,8 +170,11 @@
 void MFEventSelector2::SetUseOrigDistribution(Bool_t b)
 {
-  fUseOrigDistribution = b;
- 
-  *fLog << "MFEventSelector2 : use the distribution from the input file as the target distribution"
-        << endl;
+    fUseOrigDistribution = b;
+
+    *fLog << inf;
+    *fLog << "MFEventSelector2 set ";
+    if (!b)
+        *fLog << "not ";
+    *fLog << "to use the distribution from the input file as target distribution." << endl;
 }
 
@@ -210,7 +213,7 @@
     }
 
-    *fLog << "-------------------------" << endl;
-    *fLog << "MFEventSelector2 : Start of eventloop to generate the original distribution" 
-          << endl;
+    *fLog << inf << underline << endl;
+    *fLog << "MFEventSelector2::ReadDistribution:" << endl;
+    *fLog << " - Start of eventloop to generate the original distribution..." << endl;
 
     MEvtLoop run(GetName());
@@ -243,9 +246,8 @@
     tlist.PrintStatistics(0, kTRUE);
 
-    *fLog << "MFEventSelector2::ReadDistribution; Original distribution has " 
-          << fHistOrig->GetHist().GetEntries() << " entries" << endl;
-    *fLog << "MFEventSelector2 : End of eventloop to generate the original distribution" 
-          << endl;
-    *fLog << "-------------------------" << endl;
+    *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;
 
     return read.Rewind();
@@ -266,9 +268,5 @@
     // set the nominal distribution equal to the original distribution
 
-    TH1 *hnp;
-    if (fUseOrigDistribution)
-      hnp = (TH1*)(fHistOrig->GetHist()).Clone();
-    else
-      hnp = &fHistNom->GetHist();
+    TH1 *hnp = fUseOrigDistribution ? (TH1*)(fHistOrig->GetHist()).Clone() : &fHistNom->GetHist();
 
     TH1 &hn = *hnp;
@@ -302,18 +300,16 @@
     if (fNumMax>0)
     {
-        *fLog << "MFEventSelector2::PrepareHistograms; requested no.of events = "
-              << fNumMax << ",  maximum no.of events possible = "      
-              << hn.Integral() << endl;
+        *fLog << inf;
+        *fLog << "MFEventSelector2::PrepareHistograms:" << endl;
+        *fLog << " - requested number of events = " << fNumMax << endl;
+        *fLog << " - maximum number of events possible = " << hn.Integral() << endl;
 
         if (fNumMax > hn.Integral())
 	{
-          *fLog << "MFEventSelector2::PrepareHistograms; requested no.of events ("
-                << fNumMax << ") is too high" << endl;
-          *fLog << "                                     reduce it to "
-                << hn.Integral() << endl;
+            *fLog << warn << "WARNING - Requested no.of events (" << fNumMax;
+            *fLog << ") is too high... reduced to " << hn.Integral() << endl;
 	}
-
-        else 
-          hn.Scale(fNumMax/hn.Integral());
+        else
+            hn.Scale(fNumMax/hn.Integral());
     }
 
@@ -358,4 +354,5 @@
             return kFALSE;
         }
+        // FALLTHROUGH!
     case 2:
         if (!fDataY.PreProcess(parlist))
@@ -364,4 +361,5 @@
             return kFALSE;
         }
+        // FALLTHROUGH!
     case 1:
         if (!fDataX.PreProcess(parlist))
@@ -385,5 +383,5 @@
 Int_t MFEventSelector2::PreProcess(MParList *parlist)
 {
-    memset(fErrors, 0, sizeof(fErrors));
+    memset(fCounter, 0, sizeof(fCounter));
 
     MTaskList *tasklist = (MTaskList*)parlist->FindObject("MTaskList");
@@ -424,4 +422,35 @@
 
     return read->CallPreProcess(parlist);
+}
+
+// --------------------------------------------------------------------------
+//
+// Part of Process(). Select() at the end checks whether a selection should
+// be done or not. Under-/Overflowbins are rejected.
+//
+Bool_t MFEventSelector2::Select(Int_t bin)
+{
+    // under- and overflow bins are not counted
+    if (bin<0)
+        return kFALSE;
+
+    Bool_t rc = kFALSE;
+
+    if (gRandom->Rndm()*fIs[bin]<=fNom[bin])
+    {
+        // how many events do we still want to read in this bin
+        fNom[bin] -= 1;
+        rc = kTRUE;
+
+        // fill bin (same as Fill(valx, valy, valz))
+        TH1 &h = fHistRes->GetHist();
+        h.AddBinContent(bin+1);
+        h.SetEntries(h.GetEntries()+1);
+    }
+
+    // how many events are still pending to be read
+    fIs[bin] -= 1;
+
+    return rc;
 }
 
@@ -436,6 +465,4 @@
 Int_t MFEventSelector2::Process()
 {
-    fResult = kFALSE;
-
     // get x,y and z (0 if fData not valid)
     const Double_t valx=fDataX.GetValue();
@@ -443,38 +470,9 @@
     const Double_t valz=fDataZ.GetValue();
 
-
-    // get corresponding bin number
-    const Int_t bin = fHistNom->FindFixBin(valx, valy, valz)-1;
-
-    // under- and overflow bins are not counted
-    if (bin<0)
-        return kTRUE;
-
-
-    if (gRandom->Rndm()*fIs[bin]<=fNom[bin])
-    {
-        // how many events do we still want to read in this bin
-        fNom[bin]-=1;
-        fResult = kTRUE;
-
-        // fill bin (same as Fill(valx, valy, valz))
-        TH1 &h = fHistRes->GetHist();
-        h.AddBinContent(bin+1);
-        h.SetEntries(h.GetEntries()+1);
-    }
-    // how many events are still pending to be read
-    fIs[bin]-=1;
-
-    //----------------------
-    Int_t rc;
-    if (fResult)
-    {
-        rc = 0;
-        fErrors[rc]++;
-        return kTRUE;
-    }
-    rc = 1;
-    fErrors[rc]++;
-    //----------------------
+    // Get corresponding bin number and check
+    // whether a selection should be made
+    fResult = Select(fHistNom->FindFixBin(valx, valy, valz)-1);
+
+    fCounter[fResult ? 1 : 0]++;
 
     return kTRUE;
@@ -488,37 +486,30 @@
 {
     //---------------------------------
-    if (GetNumExecutions() != 0)
+
+    if (GetNumExecutions()>0)
     {
       *fLog << inf << endl;
       *fLog << GetDescriptor() << " execution statistics:" << endl;
       *fLog << dec << setfill(' ');
-      *fLog << " " << setw(7) << fErrors[1] << " (" << setw(3) 
-            << (int)(fErrors[1]*100/GetNumExecutions()) 
+      *fLog << " " << setw(7) << fCounter[1] << " (" << setw(3)
+            << (int)(fCounter[0]*100/GetNumExecutions())
             << "%) Events not selected" << endl;
 
-      *fLog << " " << fErrors[0] << " (" 
-            << (int)(fErrors[0]*100/GetNumExecutions()) 
-            << "%) Events selected!" << endl;
+      *fLog << " " << fCounter[0] << " ("
+            << (int)(fCounter[1]*100/GetNumExecutions())
+            << "%) Events selected" << endl;
       *fLog << endl;
     }
+
     //---------------------------------
 
-    if (!fCanvas || !fDisplay)
-        return kTRUE;
-
-    fCanvas->cd(4);
-    fHistRes->DrawClone("nonew");
-    fCanvas->Modified();
-    fCanvas->Update();
+    if (fDisplay && fDisplay->HasCanvas(fCanvas))
+    {
+        fCanvas->cd(4);
+        fHistRes->DrawClone("nonew");
+        fCanvas->Modified();
+        fCanvas->Update();
+    }
 
     return kTRUE;
 }
-
-
-
-
-
-
-
-
-
Index: /trunk/MagicSoft/Mars/mfilter/MFEventSelector2.h
===================================================================
--- /trunk/MagicSoft/Mars/mfilter/MFEventSelector2.h	(revision 2586)
+++ /trunk/MagicSoft/Mars/mfilter/MFEventSelector2.h	(revision 2587)
@@ -44,5 +44,5 @@
 
     Bool_t fResult;
-    Int_t fErrors[2];
+    Int_t  fCounter[2];
 
     TH1   &InitHistogram(MH3* &hist);
@@ -50,4 +50,5 @@
     void   PrepareHistograms();
     Bool_t PreProcessData(MParList *parlist);
+    Bool_t Select(Int_t bin);
 
     Int_t PreProcess(MParList *parlist);
