Index: trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 2574)
+++ trunk/MagicSoft/Mars/manalysis/AnalysisLinkDef.h	(revision 2581)
@@ -20,4 +20,5 @@
 #pragma link C++ class MPedestalPix+;
 #pragma link C++ class MPedestalCam+;
+#pragma link C++ class MPedCalcPedRun+;
 #pragma link C++ class MMcPedestalCopy+;
 #pragma link C++ class MMcPedestalNSBAdd+;
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc	(revision 2574)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc	(revision 2581)
@@ -136,6 +136,7 @@
 {
 
-    fOverFlow = 0;
-    fNrEvents = 0;
+    fHistOverFlow = 0;
+    fEvents       = 0;
+    fCosmics      = 0;
 
     fRawEvt = (MRawEvtData*)pList->FindObject("MRawEvtData");
@@ -235,5 +236,7 @@
 {
 
-    fNrEvents++;
+    fEvents++;
+
+    Bool_t cosmic = kFALSE;
 
     MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
@@ -267,30 +270,23 @@
             mid = pixel.GetIdxMaxLoGainSample();
 
-            sum = (max == gkSaturationLimit          // overflow of LoGain ??? -> GimmeABreak!!!
-                   ? fOverFlow++, gkLoGainOverFlow   // OUCH (Florian was maybe right)
-                   : sum*gkConversionHiLo    );      // OUFF (Florian was wrong) !! 
+            sum = (max == gkSaturationLimit              // overflow of LoGain ??? -> GimmeABreak!!!
+                   ? fHistOverFlow++, gkLoGainOverFlow   // OUCH (Florian was maybe right)
+                   : sum*gkConversionHiLo    );          // OUFF (Florian was wrong) !! 
 
             //            *fLog << warn << "Warning: Saturation of HiGain reached in slice " << (int)mid << " !!! " << endl;
             //            *fLog << warn << "Max = " << max << endl;
 
-            if (fOverFlow) 
+            if (fHistOverFlow) 
               *fLog << err << dbginf << "Warning: Saturation of LoGain reached! " 
                     << err << dbginf << "sum = " << sum << endl;
 
           }
-        
-        //
-        // sanity check (old MC files sometimes have pixids>577)
-        //
-        if (fPedestals && !fPedestals->CheckBounds(pixid))
-          {
-            *fLog << inf << "Pixel ID larger than camera... skipping event." << endl;
-            return kCONTINUE;
-              }
-        
+	
+
         MPedestalPix    &ped = (*fPedestals)[pixid];
         MCalibrationPix &pix = (*fCalibrations)[pixid];
         
-        Float_t pedes = ped.GetPedestal();
+        Float_t pedes  = ped.GetPedestal();
+        Float_t pedrms = ped.GetPedestalRms();
         
         //
@@ -298,5 +294,17 @@
         // Assume pedestals per time slice ==> multiply with number of slices
         //
-        pedes *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
+        pedes  *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
+        pedrms *= (sat ? fNumLoGainSamples : fNumHiGainSamples );
+
+	// 
+	// This is a very primitive check for the number of cosmics
+	// The cut will be applied in the fit, but for the blind pixel,
+	// we need to remove this event
+	//
+	// FIXME: In the future need a much more sophisticated one!!!
+	//
+
+	if ((float)sum < pedes+5.*pedrms)
+           cosmic = kTRUE;
 
         Float_t rsum      = (float)sum - pedes;
@@ -306,13 +314,20 @@
             
           case gkCalibrationBlindPixelId:
-            if (!blindpixel.FillQ(sum)) 
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling Blind Pixel means = " << sum << endl;
+	    // 
+	    // FIXME: This works only when the blind pixel ID is much larger than 
+	    //        the rest of the pixels (which is the case right now)
+	    //
+//	    if (!cosmic)
+	       if (!blindpixel.FillQ(sum)) 
+                  *fLog << warn << 
+                  "Overflow or Underflow occurred filling Blind Pixel sum = " << sum << endl;
+
             if (!blindpixel.FillT((int)mid)) 
               *fLog << warn << 
                 "Overflow or Underflow occurred filling Blind Pixel time = " << (int)mid << endl;
-            if (!blindpixel.FillRQvsT(rsum,fNrEvents))
+
+            if (!blindpixel.FillRQvsT(rsum,fEvents))
               *fLog << warn << 
-                "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fNrEvents << endl;
+                "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
 
           case gkCalibrationPINDiodeId:
@@ -323,7 +338,7 @@
               *fLog << warn << 
                 "Overflow or Underflow occurred filling HT: time = " << (int)mid << endl;
-            if (!pindiode.FillRQvsT(rsum,fNrEvents))
+            if (!pindiode.FillRQvsT(rsum,fEvents))
               *fLog << warn << 
-                "Overflow or Underflow occurred filling HQvsN: eventnr = " << fNrEvents << endl;
+                "Overflow or Underflow occurred filling HQvsN: eventnr = " << fEvents << endl;
 
           default:
@@ -336,7 +351,7 @@
             // Fill the reduced charge into the control histo for better visibility
             //
-            if (!pix.FillRQvsT(rsum,fNrEvents))
+            if (!pix.FillRQvsT(rsum,fEvents))
               *fLog << warn << "Could not fill red. Q vs. EvtNr of pixel: " << pixid 
-                    << " signal = " << rsum  << " event Nr: " << fNrEvents << endl;
+                    << " signal = " << rsum  << " event Nr: " << fEvents << endl;
 
 
@@ -348,4 +363,7 @@
 
       } /* while (pixel.Next()) */
+
+    if (cosmic)
+	fCosmics++;
 
     fCalibrations->SetReadyToSave();
@@ -398,4 +416,8 @@
           MCalibrationPix &pix = (*fCalibrations)[pixid];
 
+          const Float_t ped    = (*fPedestals)[pixid].GetPedestal() * fNumHiGainSamples;
+          const Float_t prms   = (*fPedestals)[pixid].GetPedestalRms() * fNumHiGainSamples;
+
+          pix.SetPedestal(ped,prms);
 
           if (TESTBIT(fFlags,kUseTFits))
@@ -405,7 +427,4 @@
              continue;
 
-          const Float_t ped    = (*fPedestals)[pixid].GetPedestal();
-          const Float_t prms   = (*fPedestals)[pixid].GetPedestalRms();
-          pix.SetPedestal(ped,prms);
         }
     }
@@ -413,12 +432,10 @@
   fCalibrations->SetReadyToSave();
   
-  if (GetNumExecutions()==0 || fOverFlow==0)
+  if (GetNumExecutions()==0)
     return kTRUE;
   
-  *fLog << inf << endl;
-  *fLog << GetDescriptor() << " execution statistics:" << endl;
-  *fLog << dec << setfill(' ');
-  *fLog << " " << setw(7) << fOverFlow << " (" << setw(3) << (int)(fOverFlow*100/GetNumExecutions()) << "%) Evts skipped due to: lo gain saturated." << endl;
-  
+  *fLog << endl;
+  *fLog << dec << setfill(' ') << fCosmics << " Events presumably cosmics" << endl;
+
   return kTRUE;
 }
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.h	(revision 2574)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.h	(revision 2581)
@@ -35,6 +35,7 @@
   MTime                    *fEvtTime;      // Time of the event
 
-  Int_t fNrEvents;                         // Number of events  
-  Int_t fOverFlow;                         // Number of events with saturated Low Gain
+  Int_t fEvents;                           // Number of events  
+  Int_t fHistOverFlow;                     // Number of events with saturated Low Gain
+  Int_t fCosmics;                          // Number of events due to supposed cosmics
 
   Byte_t fNumHiGainSamples; 
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc	(revision 2574)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc	(revision 2581)
@@ -83,4 +83,9 @@
     return kTRUE;
 
+  if (fPed && fPedRms)
+    fHist->SetLowerFitRange(fPed + 3.5*fPedRms);
+  else
+    *fLog << warn << "Cannot set lower fit range to suppress cosmics: Pedestals not available" << endl;
+
   if(!fHist->FitQ())
     {
Index: trunk/MagicSoft/Mars/manalysis/Makefile
===================================================================
--- trunk/MagicSoft/Mars/manalysis/Makefile	(revision 2574)
+++ trunk/MagicSoft/Mars/manalysis/Makefile	(revision 2581)
@@ -33,4 +33,5 @@
 SRCFILES = MPedestalPix.cc \
            MPedestalCam.cc \
+           MPedCalcPedRun.cc \
            MMcPedestalCopy.cc \
            MMcPedestalNSBAdd.cc \
