Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 2759)
+++ trunk/MagicSoft/Mars/Changelog	(revision 2760)
@@ -5,4 +5,9 @@
                                                  -*-*- END OF LINE -*-*-
  2004/01/09: Markus Gaug
+
+   * mcalib/MCalibrationCalc.cc
+     - catch the possibility of a calibration file, 
+       unintendedly being a pedestal or cosmic file. 
+       Program now exits with an error before doing the fits
 
    * mcalib/MCalibrationPix.[h,cc]
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 2759)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 2760)
@@ -92,5 +92,5 @@
 
 #include "MRawRunHeader.h"
-#include "MRawEvtData.h"       // MRawEvtData::GetNumPixels
+#include "MRawEvtData.h"     
 #include "MRawEvtPixelIter.h"
 
@@ -106,6 +106,5 @@
 // --------------------------------------------------------------------------
 //
-// Default constructor. b is the number of slices before the maximum slice,
-// a the number of slices behind the maximum slice which is taken as signal.
+// Default constructor. 
 //
 MCalibrationCalc::MCalibrationCalc(const char *name, const char *title)
@@ -234,4 +233,5 @@
     fNumHiGainSamples =  fSignals->GetNumUsedHiGainFADCSlices();
     fNumLoGainSamples =  fSignals->GetNumUsedLoGainFADCSlices();
+    fSqrtHiGainSamples = TMath::Sqrt((Float_t)fNumHiGainSamples);
 
     fCalibrations->InitSize(cam->GetNumPixels());    
@@ -280,12 +280,23 @@
 	MExtractedSignalPix &sig =  (*fSignals)[pixid];
         MPedestalPix        &ped =  (*fPedestals)[pixid];
-        Float_t pedrms     = ped.GetPedestalRms();
-	Float_t sumhi      = sig.GetExtractedSignalHiGain();
+        Float_t pedrms           = ped.GetPedestalRms()*fSqrtHiGainSamples;
+	Float_t sumhi            = sig.GetExtractedSignalHiGain();
         
-	if (sumhi < 15.*pedrms )   // cut at 3.5 sigma
+        //
+        // We consider a pixel as presumably due to cosmics 
+        // if its sum of FADC slices is lower than 3 pedestal RMS
+        //
+	if (sumhi < 3.*pedrms )  
 	  cosmicpix++;
      }
 
-    if (cosmicpix > 100.)
+    //
+    // If the camera contains more than 230 
+    // (this is the number of outer pixels plus about 50 inner ones)
+    // presumed pixels due to cosmics, then the event is discarted. 
+    // This procedure is more or less equivalent to keeping only events 
+    // with at least 350 pixels with high signals.
+    //
+    if (cosmicpix > 230.)
       {
 	fCosmics++;
@@ -391,4 +402,19 @@
 
   *fLog << inf << endl;
+
+  if (fEvents == 0)
+    {
+
+      *fLog << err << GetDescriptor() 
+            << ": This run contains only cosmics or pedestals, " 
+            << "cannot find events with more than 350 illuminated pixels. " << endl;
+      return kFALSE;
+    }
+  
+  if (fEvents < fCosmics)
+    *fLog << warn << GetDescriptor() 
+          << ": WARNING: Run contains more cosmics or pedestals than calibration events " << endl;
+
+
   *fLog << GetDescriptor() << " Cut Histogram Edges" << endl;
 
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.h	(revision 2759)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.h	(revision 2760)
@@ -43,5 +43,6 @@
   Byte_t fNumHiGainSamples; 
   Byte_t fNumLoGainSamples; 
-
+  Float_t fSqrtHiGainSamples; 
+  
   Byte_t fFlags;                           // Flag for the fits used
    
