Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 9143)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.cc	(revision 9157)
@@ -77,6 +77,6 @@
 #include "MReadMarsFile.h"
 #include "MRawFileRead.h"
-#include "MRawEvtData.h"
 #include "MGeomApply.h"
+#include "MContinue.h"
 #include "MPedestalSubtract.h"
 #include "MTriggerPatternDecode.h"
@@ -85,7 +85,11 @@
 #include "MPedCalcPedRun.h"
 #include "MPedCalcFromLoGain.h"
-#include "MFTriggerPattern.h"
 #include "MBadPixelsCalc.h"
 #include "MPedestalSubtract.h"
+
+// filter
+#include "MFilterList.h"
+#include "MFTriggerPattern.h"
+#include "MFDataMember.h"
 
 // Display helpers
@@ -114,5 +118,6 @@
     : fExtractor(NULL), fDisplayType(kDisplayDataCheck),
     fExtractType(kUsePedRun), fExtractionType(kFundamental),
-    /*fIsUseHists(kFALSE),*/ fDeadPixelCheck(kFALSE), fMinEvents(50)
+    /*fIsUseHists(kFALSE),*/ fDeadPixelCheck(kFALSE), fMinEvents(50),
+    fMinPedestals(100), fMaxPedestals(0), fMinCosmics(25), fMaxCosmics(100)
 {
     fName  = name  ? name  : "MJPedestal";
@@ -682,4 +687,10 @@
     fMinEvents = (UInt_t)GetEnv("MinEvents", (Int_t)fMinEvents);
 
+    fMinPedestals = (UInt_t)GetEnv("MinPedestals", (Int_t)fMinPedestals);
+    fMaxPedestals = (UInt_t)GetEnv("MaxPedestals", (Int_t)fMaxPedestals);
+
+    fMinCosmics   = (UInt_t)GetEnv("MinCosmics", (Int_t)fMinCosmics);
+    fMaxCosmics   = (UInt_t)GetEnv("MaxCosmics", (Int_t)fMaxCosmics);
+
     if (!MJCalib::CheckEnvLocal())
         return kFALSE;
@@ -1022,18 +1033,5 @@
     }
 
-    // This will make that for data with version less than 5, where
-    // trigger patterns were not yet correct, all the events in the real
-    // data file will be processed. In any case there are no interleaved
-    // calibration events in such data, so this is fine.
-    // The selection is done with the trigger bits before prescaling
-    // Extract pulse position from Lvl1 events.
     MTriggerPatternDecode decode;
-    MFTriggerPattern fcalib("SelectCosmics");
-    fcalib.SetDefault(kTRUE);
-    fcalib.DenyAll();
-    fcalib.RequireTriggerLvl1();
-    fcalib.AllowTriggerLvl2();
-    fcalib.AllowSumTrigger();
-
     tlist.AddToList(&decode);
 
@@ -1046,9 +1044,37 @@
     tlist.AddToList(&pedsub);
 
+    // ----------------------------------------------------------------
+    //  Setup filter for pulse position extraction and its extraction
+
+    // This will make that for data with version less than 5, where
+    // trigger patterns were not yet correct, all the events in the real
+    // data file will be processed. In any case there are no interleaved
+    // calibration events in such data, so this is fine.
+    // The selection is done with the trigger bits before prescaling
+    // Extract pulse position from Lvl1 events.
+    MFTriggerPattern fcos("SelectCosmics");
+    fcos.SetDefault(kTRUE);
+    fcos.DenyAll();
+    fcos.RequireTriggerLvl1();
+    fcos.AllowTriggerLvl2();
+    fcos.AllowSumTrigger();
+
+    // Number of events filled into the histogram presenting the
+    // trigger area
+    MFDataMember filterc("MHCalibrationPulseTimeCam.GetNumEvents", '<', fMaxCosmics, "LimitNumCosmics");
+
+    // Combine both filters
+    MFilterList flistc("&&", "FilterCosmics");
+    flistc.AddToList(&fcos);
+
+    // For the case the pulse positon check is switched on
+    // compile the tasklist accordingly
     // FIXME: MUX Monte Carlos?!??
     if (fIsPulsePosCheck)
     {
-        fillpul.SetFilter(&fcalib);
-	tlist.AddToList(&fcalib);
+        flistc.AddToList(&filterc);
+        fillpul.SetFilter(&flistc);
+
+	tlist.AddToList(&flistc);
         tlist.AddToList(&fillpul);
     }
@@ -1059,11 +1085,6 @@
     //    tlist.AddToList(&fillC);
 
-    // ----------------------------------------------------------------------
-    //   Now we make sure, that in all cases the ranges are setup correctly
-    // ----------------------------------------------------------------------
-    MTaskEnv taskenv("ExtractPedestal");
-
-    //------------------------------
-    // Require that before the Prescaling we had only a pedestal trigger
+    // ------------------------------------------------------------
+    //  Setup filter for pedestal extraction
     MFTriggerPattern ftp2("SelectPedestals");
     ftp2.SetDefault(kTRUE);
@@ -1071,28 +1092,41 @@
     ftp2.RequirePedestal();
 
-    // FIXME: WHAT D WE DO IN CASE OF MUX MCs????
-    if (!fSequence.IsMonteCarlo() && (!fExtractor || !fExtractor->HasLoGain()))
-    {
-        taskenv.SetFilter(&ftp2);
-        tlist.AddToList(&ftp2);
-    }
-    //------------------------------
-
-    switch (fExtractType)
-    {
-    case kUsePedRun:
-        // In case  other than 'fundamental' second argument is obsolete
-        // pedcalc.SetExtractWindow(0,14);  // kUsePedRun (take default from class)
-        taskenv.SetDefault(&pedcalc);
-        tlist.AddToList(&taskenv);
-        break;
-
-    case kUseData:
-        // In case  other than 'fundamental' second argument is obsolete
-        // pedlogain.SetExtractWindow(15,14); // kUseData (take default from class)
-        taskenv.SetDefault(&pedlogain);
-        tlist.AddToList(&taskenv);
-        break;
-    }
+    // Limit number of events from which a pedestal is extracted
+    MFDataMember filterp(Form("%s.fNumEvents", fPedestalCamOut.GetName()), '<', fMaxPedestals, "LimitNumPedestal");
+
+    // Combine both filters together
+    MFilterList flistp("&&", "FilterPedestal");
+    // If data is not MC and has no lo-gains select pedestals from trigger pattern
+    if (!fSequence.IsMonteCarlo() && !(fExtractor && fExtractor->HasLoGain()))
+        flistp.AddToList(&ftp2);
+    if (fMaxPedestals>0)
+        flistp.AddToList(&filterp);
+
+    // ------------------------------------------------------------
+    //  Setup pedestal extraction
+    MTaskEnv taskenv("ExtractPedestal");
+
+    taskenv.SetDefault(fExtractType==kUsePedRun ?
+                       static_cast<MTask*>(&pedcalc) :
+                       static_cast<MTask*>(&pedlogain));
+
+    taskenv.SetFilter(&flistp);
+    tlist.AddToList(&flistp);
+    tlist.AddToList(&taskenv);
+
+    // ------------------------------------------------------------
+    //  Setup a filter which defines when the loop is stopped
+    MFilterList flist("||");
+    flist.SetInverted();
+    if (fMaxPedestals>0)
+        flist.AddToList(&filterp);
+    if (fIsPulsePosCheck && fMaxCosmics>0)
+        flist.AddToList(&filterc);
+
+    MContinue stop(&flist, "Stop");
+    stop.SetRc(kFALSE);
+    if (flist.GetNumEntries()>0)
+        tlist.AddToList(&stop);
+
 /*
     if (fIsUseHists && fExtractor)
@@ -1186,4 +1220,16 @@
     }
 
+    if (fIsPulsePosCheck && pulcam.GetNumEvents()<fMinCosmics)
+    {
+        *fLog << err << GetDescriptor() << ": Failed. Less than the required " << fMinCosmics << " cosmics evts processed." << endl;
+        return kFALSE;
+    }
+
+    if (fPedestalCamOut.GetNumEvents()<fMinPedestals)
+    {
+        *fLog << err << GetDescriptor() << ": Failed. Less than the required " << fMinPedestals << " pedetsl evts processed." << endl;
+        return kFALSE;
+    }
+
     if (fDeadPixelCheck)
     {
Index: trunk/MagicSoft/Mars/mjobs/MJPedestal.h
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJPedestal.h	(revision 9143)
+++ trunk/MagicSoft/Mars/mjobs/MJPedestal.h	(revision 9157)
@@ -75,4 +75,8 @@
 
     UInt_t  fMinEvents;                      // Minimum number of events
+    UInt_t  fMinPedestals;                   // Minimum number of events
+    UInt_t  fMaxPedestals;                   // Maximum number of events for pedestal extraction
+    UInt_t  fMinCosmics;                     // Minimum number of events
+    UInt_t  fMaxCosmics;                     // Maximum number of events for pulse    extraction
 
     MExtractor *ReadCalibration();
Index: trunk/MagicSoft/Mars/mjobs/MJStar.cc
===================================================================
--- trunk/MagicSoft/Mars/mjobs/MJStar.cc	(revision 9143)
+++ trunk/MagicSoft/Mars/mjobs/MJStar.cc	(revision 9157)
@@ -411,5 +411,6 @@
     MFillH fillsp("MHSrcPosCam", "MSrcPosCam", "FillSrcPosCam");
     fillsp.SetNameTab("Src");
-    tlist2.AddToList(&fillsp);
+    if (fSequence.IsMonteCarlo())
+        tlist2.AddToList(&fillsp);
 
 
