Index: trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 2885)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationCalc.cc	(revision 2904)
@@ -27,22 +27,24 @@
 //   MCalibrationCalc
 //
-//   This is a task which calculates the number of photons from the FADC
-//   time slices. At the moment it integrates simply the FADC values.
-//
-//
-//  The class MCalibrationCam hold one entry of type MCalibrationPix for
-//  every pixel. It is filled in the following way:
-//  PreParocess: MalibrationCam::InitSize(577) is called which allocates
-//               memory in an TClonesArray of type MCalibrationPix and
-//               all pointers to NULL.
-//
-//  Process:     The NULL pointer is tested on every pixel via
-//               MalibrationCam::IsPixelUsed(npix).
-//
-//               In case, IsPixelUsed returns NULL,
-//               MalibrationCam::AddPixel(npix) is invoked which creates a
-//               new MCalibrationPix(npix) in the npix's entry
-//               of the TClonesArray.
-//
+//   Task to calculate the calibration conversion factors from the FADC
+//   time slices. The integrated time slices have to be delivered by an 
+//   MExtractedSignalCam. The pedestals by an MPedestalCam and possibly 
+//   arrival times from MArrivalTime
+//
+//   The output container MCalibrationCam holds one entry of type MCalibrationPix 
+//   for every pixel. It is filled in the following way:
+//
+//   ProProcess: Search for MPedestalCam, MExtractedSignalCam
+//               Initialize MCalibrationCam
+//               Initialize MArrivalTime if exists
+//               Initialize pulser light wavelength
+//               
+//   ReInit:     MCalibrationCam::InitSize(NumPixels) is called which allocates
+//               memory in a TClonesArray of type MCalibrationPix
+//               Initialize number of used FADC slices
+//               Optionally exclude pixels from calibration               
+//
+//   Process:    Optionally, a cut on cosmics can be performed
+//               
 //               Every MCalibrationPix holds a histogram class,
 //               MHCalibrationPixel which itself hold histograms of type:
@@ -52,22 +54,22 @@
 //               HChargevsN(npix) (distribution of charges vs. event number.
 //
-// PostProcess:  All histograms HCharge(npix) are fitted to a Gaussian
-//               All histograms HTime(npix) are fitted to a Gaussian
-//               The histogram HBlindPixelCharge (blind pixel) is fitted to
-//               a single
-//                   PhE fit
-//               The histogram HBlindPixelTime (blind pixel) is fitted to a
-//               Gaussian
-//               The histograms of the PIN Diode are fitted to Gaussians
-//
-//               Fits can be excluded via the commands:
-//               MalibrationCam::SetSkipTimeFits()   (skip all time fits)
-//               MalibrationCam::SetSkipBlindPixelFits()  (skip all blind
-//               pixel fits)
-//               MalibrationCam::SetSkipPinDiodeFits()  (skip all PIN Diode
-//               fits)
+//  PostProcess:  All histograms HCharge(npix) are fitted to a Gaussian
+//                All histograms HTime(npix) are fitted to a Gaussian
+//                The histogram HBlindPixelCharge (blind pixel) is fitted to
+//                a single PhE fit
+//
+//                The histograms of the PIN Diode are fitted to Gaussians
+//
+//                Fits can be excluded via the commands:
+//                MalibrationCam::SkipTimeFits()   (skip all time fits)
+//                MalibrationCam::SkipBlindPixelFits()  (skip all blind
+//                pixel fits)
+//                MalibrationCam::SkipPinDiodeFits()  (skip all PIN Diode
+//                fits)
 //
 //  Input Containers:
 //   MRawEvtData
+//   MPedestalCam
+//   MExtractedSignalCam
 //
 //  Output Containers:
@@ -80,5 +82,5 @@
 #include <fstream>
 
-// FXIME: This has to be removed!!!!
+// FXIME: This has to be removed!!!! (YES, WHEN WE HAVE ACCESS TO THE DATABASE!!!!!)
 #include "MCalibrationConfig.h"
 
@@ -106,4 +108,6 @@
 #include "MCalibrationPINDiode.h"
 
+#include "MArrivalTime.h"
+
 ClassImp(MCalibrationCalc);
 
@@ -116,6 +120,6 @@
 MCalibrationCalc::MCalibrationCalc(const char *name, const char *title)
     : fPedestals(NULL), fCalibrations(NULL), fSignals(NULL),
-      fRawEvt(NULL), fRunHeader(NULL), fEvtTime(NULL),
-      fEvents(0), fHistOverFlow(0), fCosmics(0),
+      fRawEvt(NULL), fRunHeader(NULL), fArrivalTime(NULL), fEvtTime(NULL),
+      fEvents(0), fCosmics(0),
       fNumHiGainSamples(0), fNumLoGainSamples(0), fConversionHiLo(0.),
       fNumExcludedPixels(0),
@@ -131,7 +135,9 @@
     AddToBranchList("MRawEvtData.fLoGainFadcSamples");
 
-    SETBIT(fFlags, kUseTimeFits);
+    SETBIT(fFlags, kUseTimes);
     SETBIT(fFlags, kUseBlindPixelFit);
     SETBIT(fFlags, kUsePinDiodeFit);
+    SETBIT(fFlags, kUseCosmicsRejection);
+    SETBIT(fFlags, kUseQualityChecks);
 
 }
@@ -158,4 +164,8 @@
 //  - MHCalibrationBlindPixel
 //  - MCalibrationCam
+//
+// The following output containers are only searched, but not created
+//
+//  - MArrivaltime
 //  - MTime
 //
@@ -186,4 +196,10 @@
       }
 
+    fArrivalTime  = (MArrivalTime*)pList->FindObject("MArrivalTime");
+
+    if (!fArrivalTime)
+      CLRBIT(fFlags,kUseTimes);
+
+    fEvtTime      = (MTime*)pList->FindObject("MTime");
 
     switch (fColor)
@@ -268,4 +284,7 @@
                                      fSignals->GetLastUsedSliceLoGain());
         
+        if (!TESTBIT(fFlags,kUseQualityChecks))
+          pix.SetExcludeQualityCheck();
+
       }
     
@@ -340,149 +359,175 @@
 {
 
-    Int_t cosmicpix = 0;
-
-    MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
-    MCalibrationPINDiode &pindiode   = *(fCalibrations->GetPINDiode());
-
-    MRawEvtPixelIter pixel(fRawEvt);
-
-    //
-    // Create a first loop to sort out the cosmics ...
-    // 
-    // This is a very primitive check for the number of cosmicpixs
-    // 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!!!
-    //
-
-    while (pixel.Next())
-      {
-	
-	const UInt_t pixid = pixel.GetPixelId();
-	
-	MExtractedSignalPix &sig =  (*fSignals)[pixid];
-        MPedestalPix        &ped =  (*fPedestals)[pixid];
-        Float_t pedrms           = ped.GetPedestalRms()*fSqrtHiGainSamples;
-	Float_t sumhi            = sig.GetExtractedSignalHiGain();
+  //
+  // Initialize pointers to blind pixel, PIN Diode and individual pixels
+  //
+  MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
+  MCalibrationPINDiode &pindiode   = *(fCalibrations->GetPINDiode());
+  
+  MRawEvtPixelIter pixel(fRawEvt);
+  
+  //
+  // Perform cosmics cut
+  //
+  if (TESTBIT(fFlags,kUseCosmicsRejection))
+    {
         
-        //
-        // 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 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++;
-	return kCONTINUE;
-      }
-
-    pixel.Reset();
-    fEvents++;
-
-
-    //
-    // Create a second loop to do fill the calibration histograms
-    // 
-
-    while (pixel.Next())
-      {
-
-	const UInt_t pixid = pixel.GetPixelId();
-	
-	MCalibrationPix &pix = (*fCalibrations)[pixid];
-
-        if (pix.IsExcluded())
-          continue;
-
-	MExtractedSignalPix &sig =  (*fSignals)[pixid];
-        
-	Float_t sumhi  = sig.GetExtractedSignalHiGain();
-	Float_t sumlo  = sig.GetExtractedSignalLoGain();
-	Float_t mtime  = sig.GetMeanArrivalTime();
-
-
-        switch(pixid)
-          {
-            
-          case gkCalibrationBlindPixelId:
-
-	    if (!blindpixel.FillCharge(sumhi)) 
-	      *fLog << warn << 
-		"Overflow or Underflow occurred filling Blind Pixel sum = " << sumhi << endl;
-
-	    if (!blindpixel.FillTime((int)mtime)) 
-	      *fLog << warn << 
-		"Overflow or Underflow occurred filling Blind Pixel time = " << mtime << endl;
-	    
-	    if (!blindpixel.FillRChargevsTime(sumhi,fEvents))
-	      *fLog << warn << 
-		"Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
-            break;
-            
-          case gkCalibrationPINDiodeId:
-            if (!pindiode.FillCharge(sumhi)) 
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling PINDiode: sum = " << sumhi << endl;
-            if (!pindiode.FillTime((int)mtime)) 
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling PINDiode: time = " << mtime << endl;
-            if (!pindiode.FillRChargevsTime(sumhi,fEvents))
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling PINDiode: eventnr = " << fEvents << endl;
-	    break;
-
-          default:
-
-	    pix.SetChargesInGraph(sumhi,sumlo);
-
-	    if (!pix.FillRChargevsTimeLoGain(sumlo,fEvents))
-	      *fLog << warn << "Could not fill Lo Gain Charge vs. EvtNr of pixel: " 
-		    << pixid << " signal = " << sumlo  << " event Nr: " << fEvents << endl;
-		
-	    if (!pix.FillRChargevsTimeHiGain(sumhi,fEvents))
-	      *fLog << warn << "Could not fill Hi Gain Charge vs. EvtNr of pixel: " 
-		    << pixid << " signal = " << sumhi  << " event Nr: " << fEvents << endl;
-
-	    if (sig.IsLoGainUsed())
-	      {
-		
-		if (!pix.FillChargeLoGain(sumlo))
-		  *fLog << warn << "Could not fill Lo Gain Charge of pixel: " << pixid 
-			<< " signal = " << sumlo << endl;
-
-		if (!pix.FillTimeLoGain((int)mtime)) 
-		  *fLog << warn << "Could not fill Lo Gain Time of pixel: " 
-			<< pixid << " time = " << mtime << endl;
-		
-	      }
-	    else
-	      {
-		if (!pix.FillChargeHiGain(sumhi))
-		  *fLog << warn << "Could not fill Hi Gain Charge of pixel: " << pixid 
-			<< " signal = " << sumhi << endl;
-		
-		if (!pix.FillTimeHiGain((int)mtime)) 
-		  *fLog << warn << "Could not fill Hi Gain Time of pixel: " 
-			<< pixid << " time = " << mtime << endl;
-		
-	      }
-	    break;
-	    
-          } /* switch(pixid) */
-
-      } /* while (pixel.Next()) */
-
-    return kTRUE;
+      Int_t cosmicpix = 0;
+      
+      //
+      // Create a first loop to sort out the cosmics ...
+      // 
+      // This is a very primitive check for the number of cosmicpixs
+      // 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!!!
+      //
+      
+      while (pixel.Next())
+        {
+          
+          const UInt_t pixid = pixel.GetPixelId();
+          
+          MExtractedSignalPix &sig =  (*fSignals)[pixid];
+          MPedestalPix        &ped =  (*fPedestals)[pixid];
+          Float_t pedrms           = ped.GetPedestalRms()*fSqrtHiGainSamples;
+          Float_t sumhi            = sig.GetExtractedSignalHiGain();
+          
+          //
+          // 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 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++;
+          return kCONTINUE;
+        }
+      
+      pixel.Reset();
+    }
+  
+  fEvents++;
+  
+  //
+  // Create a (second) loop to do fill the calibration histograms
+  // 
+  
+  while (pixel.Next())
+    {
+      
+      const UInt_t pixid = pixel.GetPixelId();
+      
+      MCalibrationPix &pix = (*fCalibrations)[pixid];
+      
+      if (pix.IsExcluded())
+        continue;
+      
+      MExtractedSignalPix &sig =  (*fSignals)[pixid];
+      
+      const Float_t sumhi  = sig.GetExtractedSignalHiGain();
+      const Float_t sumlo  = sig.GetExtractedSignalLoGain();
+
+      Double_t mtime = 0.;
+
+      if (TESTBIT(fFlags,kUseTimes))
+        mtime = (*fArrivalTime)[pixid];
+      
+      switch(pixid)
+        {
+          
+        case gkCalibrationBlindPixelId:
+          
+          if (!blindpixel.FillCharge(sumhi)) 
+            *fLog << warn << 
+              "Overflow or Underflow occurred filling Blind Pixel sum = " << sumhi << endl;
+          
+          if (TESTBIT(fFlags,kUseTimes))
+            {
+              if (!blindpixel.FillTime(mtime))
+                *fLog << warn << 
+                  "Overflow or Underflow occurred filling Blind Pixel time = " << mtime << endl;
+            }
+          
+          if (!blindpixel.FillRChargevsTime(sumhi,fEvents))
+            *fLog << warn << 
+              "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
+          break;
+          
+        case gkCalibrationPINDiodeId:
+
+          if (!pindiode.FillCharge(sumhi)) 
+            *fLog << warn << 
+              "Overflow or Underflow occurred filling PINDiode: sum = " << sumhi << endl;
+
+          if (TESTBIT(fFlags,kUseTimes))
+            {
+              if (!pindiode.FillTime(mtime))
+                *fLog << warn << 
+                  "Overflow or Underflow occurred filling PINDiode: time = " << mtime << endl;
+            }
+          
+          if (!pindiode.FillRChargevsTime(sumhi,fEvents))
+            *fLog << warn << 
+              "Overflow or Underflow occurred filling PINDiode: eventnr = " << fEvents << endl;
+          break;
+          
+        default:
+          
+          pix.FillChargesInGraph(sumhi,sumlo);
+
+          if (!pix.FillRChargevsTimeLoGain(sumlo,fEvents))
+            *fLog << warn << "Could not fill Lo Gain Charge vs. EvtNr of pixel: " 
+                  << pixid << " signal = " << sumlo  << " event Nr: " << fEvents << endl;
+          
+          if (!pix.FillRChargevsTimeHiGain(sumhi,fEvents))
+            *fLog << warn << "Could not fill Hi Gain Charge vs. EvtNr of pixel: " 
+                  << pixid << " signal = " << sumhi  << " event Nr: " << fEvents << endl;
+          
+          if (sig.IsLoGainUsed())
+            {
+              
+              if (!pix.FillChargeLoGain(sumlo))
+                *fLog << warn << "Could not fill Lo Gain Charge of pixel: " << pixid 
+                      << " signal = " << sumlo << endl;
+              
+              if (TESTBIT(fFlags,kUseTimes))
+                {
+                  if (!pix.FillTimeLoGain(mtime)) 
+                    *fLog << warn << "Could not fill Lo Gain Time of pixel: " 
+                          << pixid << " time = " << mtime << endl;
+                }
+            } /* if (sig.IsLoGainUsed()) */
+          else
+            {
+              if (!pix.FillChargeHiGain(sumhi))
+                *fLog << warn << "Could not fill Hi Gain Charge of pixel: " << pixid 
+                      << " signal = " << sumhi << endl;
+              
+              if (TESTBIT(fFlags,kUseTimes))
+                {
+                  if (!pix.FillTimeHiGain(mtime))
+                    *fLog << warn << "Could not fill Hi Gain Time of pixel: " 
+                          << pixid << " time = " << mtime << endl;
+                }
+            } /* else (sig.IsLoGainUsed()) */
+          break;
+          
+        } /* switch(pixid) */
+      
+    } /* while (pixel.Next()) */
+  
+  return kTRUE;
 }
 
@@ -531,5 +576,5 @@
         }
 
-      //blindpixel.DrawClone();
+      blindpixel.DrawClone();
     }
   else 
@@ -573,5 +618,5 @@
       // Perform the Gauss fits to the arrival times
       //
-      if (TESTBIT(fFlags,kUseTimeFits))
+      if (TESTBIT(fFlags,kUseTimes))
         pix.FitTime();
       
