Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 3625)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargeCalc.cc	(revision 3626)
@@ -717,6 +717,4 @@
   fCam->SetReadyToSave();
   
-  *fLog << err << "Sectors: " << fGeom->GetNumSectors() << endl;
-
   *fLog << inf << endl;
   *fLog << GetDescriptor() << ": Calibration statistics:" << endl;
Index: trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.h	(revision 3625)
+++ trunk/MagicSoft/Mars/mcalib/MCalibrationChargePix.h	(revision 3626)
@@ -93,6 +93,5 @@
   Float_t fLoGainNumPickup;
 
-  enum  { kHiGainSaturation, kLoGainSaturation,
-          kExcluded, 
+  enum  { kHiGainSaturation, kExcluded, 
           kBlindPixelMethodValid, kFFactorMethodValid, 
 	  kPINDiodeMethodValid, kCombinedMethodValid };
@@ -156,5 +155,4 @@
   // Bit Setters
   void SetHiGainSaturation    (  const Bool_t b = kTRUE );
-  void SetLoGainSaturation    (  const Bool_t b = kTRUE );
   void SetExcluded            (  const Bool_t b = kTRUE );
   void SetBlindPixelMethodValid( const Bool_t b = kTRUE );
@@ -235,5 +233,4 @@
   Bool_t IsExcluded()                          const;
   Bool_t IsHiGainSaturation()                  const;
-  Bool_t IsLoGainSaturation()                  const;
   Bool_t IsBlindPixelMethodValid()             const;
   Bool_t IsPINDiodeMethodValid()               const;
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.cc	(revision 3625)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.cc	(revision 3626)
@@ -22,40 +22,88 @@
 !
 \* ======================================================================== */
-
 /////////////////////////////////////////////////////////////////////////////
 //                                                               
 // MHCalibrationChargeCam                                               
 //
-// Fills the extracted signals of MExtractedSignalCam into the MH-classes: 
-//
-// MHCalibrationChargeHiGainPix, MHCalibrationChargeLoGainPix for each pixel 
-// and additionally for an average of the inner and the outer pixels, respectively.
+// Fills the extracted signals of MExtractedSignalCam into the MHGausEvents-classes 
+// MHCalibrationChargeHiGainPix and MHCalibrationChargeLoGainPix for every:
+//
+// - pixel, stored in the TObjArray's fHiGainArray and fLoGainArray
+//
+// - average pixel per area index (e.g. inner and outer for the MAGIC camera), 
+//   stored in the TObjArray's fAverageHiGainAreas and fAverageLoGainAreas
+//
+// - average pixel per camera sector (e.g. sectors 1-6 for the MAGIC camera), 
+//   stored in the TObjArray's fAverageHiGainSectors and fAverageLoGainSectors
 // 
-// By default, subsequently the hi-gain classes are treated unless 
-// more than fNumHiGainSaturationLimit (default: 1%) of the overall FADC 
-// slices show saturation. In that case, the low-gain classes are treated. 
+// Every signal is filled into a Charge histogram and an array, in order to perform 
+// a Fourier analysis (see MHGausEvents). The signals are moreover averaged on an 
+// event-by-event basis and written into the TObjArray's fAverageHiGainAreas, 
+// fAverageLoGainAreas, fAverageHiGainSectors and fAverageLoGainSectors.
+//
+// Additionally, the (FADC slice) position of the maximum is stored in an Absolute 
+// Arrival Time histogram. This histogram serves for a rough cross-check if the 
+// signal does not lie at or outside the edges of the extraction window. 
+//
+// The Charge histograms are fitted to a Gaussian, mean and sigma with its errors 
+// and the fit probability are extracted. If none of these values are NaN's and 
+// if the probability is bigger than fProbLimit (default: 0.5%), the fit is valid.
+// Otherwise, the fit is repeated within ranges of the previous mean +- 5 sigma. 
+// In case this does not make the fit valid, the histogram means and RMS's are 
+// taken directly and the following flags are set:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kHiGainNotFitted ) or  
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kLoGainNotFitted ) and 
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnreliableRun   ) 
+// 
+// Outliers of more than MHCalibrationChargePix::fPickupLimit (default: 5) sigmas 
+// from the mean are counted as PickUp events (stored in MHCalibrationChargePix::fPickup) 
+//
+// Unless more than fNumHiGainSaturationLimit (default: 1%) of the overall FADC 
+// slices show saturation, the following flag is set:
+// - MCalibrationChargePix::SetHiGainSaturation();
+// In that case, the calibration constants are derived from the low-gain results.
+//
 // If more than fNumLoGainSaturationLimit (default: 1%) of the overall 
-// low-gain FADC slices saturate, the pixel is declared not valid and no further 
-// treatment is pursued. 
+// low-gain FADC slices saturate, the following flags are set:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kLoGainSaturation ) and
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    )
 // 
-// The filled histograms are fitted to a Gaussian and the mean and sigma with 
-// its errors and the fit probability are extracted. If none of these values are 
-// NaN's and if the probability is bigger than fProbLimit (default: 0.5%), the fit 
-// is declared valid. Otherwise, the fit is repeated within ranges of the previous mean 
-// +- 5 sigma. In case that this does not help, the histogram means and RMS's are taken directly,
-// but the flag kFitValid is set to FALSE. Outliers of more than fPickUpLimit (default: 5) sigmas 
-// from the mean are counted as PickUp events. 
-//
-// Additionally, the slice number with the highest value is stored and a corresponding 
-// histogram is filled. This histogram serves only for a rough cross-check if the 
-// signal does not lie at the edges of chose extraction window. 
-//
 // The class also fills arrays with the signal vs. event number, creates a fourier 
-// spectrum out of it and investigates if the projected frequencies follow an exponential 
+// spectrum and investigates if the projected fourier components follow an exponential 
 // distribution. In case that the probability of the exponential fit is less than 
-// fProbLimit, the pixel is declared HiGainOscillating or LoGainOscillating, respectively.
+// fProbLimit (default: 0.5%), the following flags are set:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kHiGainOscillating ) or
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kLoGainOscillating ) and
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnreliableRun     )
 // 
-// The results are written into MCalibrationChargeCam.
-//                                                               
+// This same procedure is performed for the average pixels.
+//
+// The following results are written into MCalibrationChargeCam:
+//
+// - MCalibrationChargePix::SetHiGainSaturation(Bool_t b) 
+// - MCalibrationChargePix::SetHiGainMeanCharge()
+// - MCalibrationChargePix::SetHiGainMeanChargeErr()
+// - MCalibrationChargePix::SetHiGainSigmaCharge()
+// - MCalibrationChargePix::SetHiGainSigmaChargeErr()
+// - MCalibrationChargePix::SetHiGainChargeProb()
+// - MCalibrationChargePix::SetHiGainNumPickup()
+//
+// - MCalibrationChargePix::SetLoGainMeanCharge()
+// - MCalibrationChargePix::SetLoGainMeanChargeErr()
+// - MCalibrationChargePix::SetLoGainSigmaCharge()
+// - MCalibrationChargePix::SetLoGainSigmaChargeErr()
+// - MCalibrationChargePix::SetLoGainChargeProb()
+// - MCalibrationChargePix::SetLoGainNumPickup()
+//
+// - MCalibrationChargePix::SetAbsTimeMean()
+// - MCalibrationChargePix::SetAbsTimeRms()
+//
+// The last two items are filled with the Hi-Gain or Lo-Gain result, depending on 
+// the saturation status. 
+//
+// For all averaged areas, the fitted sigma is multiplied with the square root of 
+// the number involved pixels in order to be able to compare it to the average of 
+// sigmas in the camera.
+//
 /////////////////////////////////////////////////////////////////////////////
 #include "MHCalibrationChargeCam.h"
@@ -95,11 +143,27 @@
 using namespace std;
 
+const Int_t   MHCalibrationChargeCam::fgAverageNbins             = 4000;
 const Float_t MHCalibrationChargeCam::fgNumHiGainSaturationLimit = 0.01;
 const Float_t MHCalibrationChargeCam::fgNumLoGainSaturationLimit = 0.005;
 const Int_t   MHCalibrationChargeCam::fgPulserFrequency          = 500;
-//
+// --------------------------------------------------------------------------
+//
+// Default Constructor. 
+//
+// Sets:
+// - all pointers to NULL
+//
+// Initializes and sets owner of:
+// - fHiGainArray, fLoGainArray
+// - fAverageHiGainAreas, fAverageLoGainAreas
+// - fAverageHiGainSectors, fAverageLoGainSectors
+//
+// Initializes:
+// - fNumHiGainSaturationLimit to fgNumHiGainSaturationLimit
+// - fNumLoGainSaturationLimit to fgNumLoGainSaturationLimit
+// - fPulserFrequency to fgPulserFrequency
 //
 MHCalibrationChargeCam::MHCalibrationChargeCam(const char *name, const char *title)
-    : fRawEvt(NULL), fGeom(NULL), fBadPixels(NULL)
+    : fCam(NULL), fRawEvt(NULL), fGeom(NULL), fBadPixels(NULL)
 {
     fName  = name  ? name  : "MHCalibrationChargeCam";
@@ -131,6 +195,8 @@
 // --------------------------------------------------------------------------
 //
-// Delete the TClonesArray of MHCalibrationChargePix containers
-// Delete the average pixels
+// Deletes the TClonesArray of:
+// - fHiGainArray, fLoGainArray
+// - fAverageHiGainAreas, fAverageLoGainAreas
+// - fAverageHiGainSectors, fAverageLoGainSectors
 //
 MHCalibrationChargeCam::~MHCalibrationChargeCam()
@@ -148,5 +214,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th High Gain pixel (pixel number)
 //
 MHCalibrationChargeHiGainPix &MHCalibrationChargeCam::operator[](UInt_t i)
@@ -157,5 +223,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th High Gain pixel (pixel number)
 //
 const MHCalibrationChargeHiGainPix &MHCalibrationChargeCam::operator[](UInt_t i) const
@@ -166,5 +232,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th Low Gain pixel (pixel number)
 //
 MHCalibrationChargeLoGainPix &MHCalibrationChargeCam::operator()(UInt_t i)
@@ -175,5 +241,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th Low Gain pixel (pixel number)
 //
 const MHCalibrationChargeLoGainPix &MHCalibrationChargeCam::operator()(UInt_t i) const
@@ -184,5 +250,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th High Gain pixel Area (area number)
 //
 MHCalibrationChargeHiGainPix &MHCalibrationChargeCam::GetAverageHiGainArea(UInt_t i)
@@ -193,5 +259,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th High Gain pixel Area (area number)
 //
 const MHCalibrationChargeHiGainPix &MHCalibrationChargeCam::GetAverageHiGainArea(UInt_t i) const
@@ -202,5 +268,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th Low Gain pixel Area (area number)
 //
 MHCalibrationChargeLoGainPix &MHCalibrationChargeCam::GetAverageLoGainArea(UInt_t i)
@@ -211,5 +277,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th Low Gain pixel Area (area number)
 //
 const MHCalibrationChargeLoGainPix &MHCalibrationChargeCam::GetAverageLoGainArea(UInt_t i) const
@@ -220,5 +286,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th High Gain Sector (sector number)
 //
 MHCalibrationChargeHiGainPix &MHCalibrationChargeCam::GetAverageHiGainSector(UInt_t i)
@@ -229,5 +295,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th High Gain Sector (sector number)
 //
 const MHCalibrationChargeHiGainPix &MHCalibrationChargeCam::GetAverageHiGainSector(UInt_t i) const
@@ -238,5 +304,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th Low Gain Sector (sector number)
 //
 MHCalibrationChargeLoGainPix &MHCalibrationChargeCam::GetAverageLoGainSector(UInt_t i)
@@ -247,5 +313,5 @@
 // --------------------------------------------------------------------------
 //
-// Get i-th pixel (pixel number)
+// Get i-th Low Gain Sector (sector number)
 //
 const MHCalibrationChargeLoGainPix &MHCalibrationChargeCam::GetAverageLoGainSector(UInt_t i) const
@@ -257,5 +323,9 @@
 //
 // Our own clone function is necessary since root 3.01/06 or Mars 0.4
-// I don't know the reason
+// I don't know the reason. 
+//
+// Creates new MHCalibrationChargeCam
+// Deletes the TObjArray's and Clones them individually
+// Copies the TArray's
 //
 TObject *MHCalibrationChargeCam::Clone(const char *) const
@@ -312,9 +382,11 @@
     }
 
-  cam->fAverageAreaSat         = fAverageAreaSat;            
+  cam->fAverageAreaNum         = fAverageAreaNum;
+  cam->fAverageAreaSat         = fAverageAreaSat;
   cam->fAverageAreaSigma       = fAverageAreaSigma;      
   cam->fAverageAreaSigmaErr    = fAverageAreaSigmaErr;   
   cam->fAverageAreaRelSigma    = fAverageAreaRelSigma;
   cam->fAverageAreaRelSigmaErr = fAverageAreaRelSigmaErr;   
+  cam->fAverageSectorNum       = fAverageSectorNum;      
 
   return cam;
@@ -323,6 +395,12 @@
 // --------------------------------------------------------------------------
 //
-// To setup the object we get the number of pixels from a MGeomCam object
-// in the Parameter list.
+// Gets the pointers to:
+// - MRawEvtData
+// - MGeomCam
+//
+// Calls Delete-Function of:
+// - fHiGainArray, fLoGainArray
+// - fAverageHiGainAreas, fAverageLoGainAreas
+// - fAverageHiGainSectors, fAverageLoGainSectors
 //
 Bool_t MHCalibrationChargeCam::SetupFill(const MParList *pList)
@@ -355,4 +433,37 @@
 }
 
+// --------------------------------------------------------------------------
+//
+// Gets or creates the pointers to:
+// - MBadPixelsCam
+// - MCalibrationChargeCam
+//
+// Searches pointer to:
+// - MExtractedSignalCam
+//
+// Initializes, if empty to MExtractedSignalCam::GetSize() for:
+// - fHiGainArray, fLoGainArray
+//
+// Initializes, if empty to MGeomCam::GetNumAreas() for:
+// - fAverageHiGainAreas, fAverageLoGainAreas
+//
+// Initializes, if empty to MGeomCam::GetNumSectors() for:
+// - fAverageHiGainSectors, fAverageLoGainSectors
+// 
+// Initializes TArrays to MGeomCam::GetNumAreas and MGeomCam::GetNumSectors, respectively
+// Fills with number of valid pixels (if !MBadPixelsPix::IsBad()):
+// - fAverageAreaNum[area index]
+// - fAverageSectorNum[area index]
+//
+// Calls InitializeHiGainHists() and InitializeLoGainHists() for every entry in:
+// - fHiGainArray, fLoGainArray
+// - fAverageHiGainAreas, fAverageLoGainAreas
+// - fAverageHiGainSectors, fAverageLoGainSectors
+//
+// Sets Titles and Names for the Charge Histograms and 
+// Sets number of bins to fAverageNbins for:
+// - fAverageHiGainAreas, fAverageLoGainAreas
+// - fAverageHiGainSectors, fAverageLoGainSectors
+// 
 Bool_t MHCalibrationChargeCam::ReInit(MParList *pList)
 {
@@ -374,103 +485,11 @@
   }
 
-  const Int_t n = signal->GetSize();
-
-  if (fHiGainArray->GetEntries()==0)
-  {
-      fHiGainArray->Expand(n);
-  
-      for (Int_t i=0; i<n; i++)
-      {
-	  //
-	  // Oscillating pixels
-	  //
-	  (*fHiGainArray)[i] = new MHCalibrationChargeHiGainPix;
-          MHCalibrationChargeHiGainPix &hist = (*this)[i];
-
-          if ((*fBadPixels)[i].IsBad())
-	  {
-	    *fLog << warn << "Excluded pixel: " << i << " from calibration " << endl;
-	    hist.SetExcluded();
-	  }
-	  hist.Init();
-	  hist.ChangeHistId(i);
-          hist.SetEventFrequency(fPulserFrequency);
-      }
-  }
-
- 
-  if (fLoGainArray->GetEntries()==0)
-  {
-      fLoGainArray->Expand(n);
-      
-      for (Int_t i=0; i<n; i++)
-      {
-	  (*fLoGainArray)[i] = new MHCalibrationChargeLoGainPix;
-          MHCalibrationChargeLoGainPix &hist = (*this)(i);
-	  //
-	  // Pixels with non-valid behavior
-	  //
-          if ((*fBadPixels)[i].IsBad())
-            hist.SetExcluded();
-
-	  hist.Init();
-	  hist.ChangeHistId(i);
-          hist.SetEventFrequency(fPulserFrequency);
-      }
-      
-  }
-
-  const Int_t nareas = fGeom->GetNumAreas();
-
-  if (fAverageHiGainAreas->GetEntries()==0)
-  {
-      fAverageHiGainAreas->Expand(nareas);
-
-      for (Int_t j=0; j<nareas; j++)
-      {
-	  //
-	  // Oscillating pixels
-	  //
-	  (*fAverageHiGainAreas)[j] = 
-            new MHCalibrationChargeHiGainPix("AverageHiGainArea",
-                                             "Average HiGain FADC sums of pixel area idx ");
-          MHCalibrationChargeHiGainPix &hist = GetAverageHiGainArea(j);
-
-          hist.GetHGausHist()->SetTitle("Summed FADC slices average Hi Gain pixels Area Idx ");
-          hist.GetHAbsTime()->SetTitle("Absolute Arrival Time average Hi Gain pixels Area Idx ");
-          hist.SetChargeFirst(-1000.);
-          hist.SetChargeLast(4000.);
-          hist.SetChargeNbins(4000);
-	  hist.Init();
-	  hist.ChangeHistId(j);
-          hist.SetEventFrequency(fPulserFrequency);
-      }
-  }
-
-  if (fAverageLoGainAreas->GetEntries()==0)
-  {
-      fAverageLoGainAreas->Expand(nareas);
-  
-      for (Int_t j=0; j<nareas; j++)
-      {
-	  //
-	  // Oscillating pixels
-	  //
-	  (*fAverageLoGainAreas)[j] = 
-            new MHCalibrationChargeLoGainPix("AverageLoGainArea",
-                                             "Average LoGain FADC sums of pixel area idx ");
-          MHCalibrationChargeLoGainPix &hist = GetAverageLoGainArea(j);
-
-          hist.GetHGausHist()->SetTitle("Summed FADC slices average Lo Gain pixels Area Idx ");
-          hist.GetHAbsTime()->SetTitle("Absolute Arrival Time average Lo Gain pixels Area Idx ");
-          hist.SetChargeFirst(-1000.);
-          hist.SetChargeLast(4000.);
-          hist.SetChargeNbins(4000);
-	  hist.Init();
-	  hist.ChangeHistId(j);
-          hist.SetEventFrequency(fPulserFrequency);
-      }
-  }
-
+  const Int_t n        = signal->GetSize();
+  const Int_t nsectors = fGeom->GetNumSectors();
+  const Int_t nareas   = fGeom->GetNumAreas();
+
+  //
+  // The function TArrayF::Set() already sets all entries to 0.
+  //
   fAverageAreaNum.        Set(nareas);
   fAverageAreaSat.        Set(nareas);           
@@ -479,17 +498,69 @@
   fAverageAreaRelSigma.   Set(nareas);   
   fAverageAreaRelSigmaErr.Set(nareas);
+  fAverageSectorNum.      Set(nsectors);
+
+  for (Int_t i=0; i<n; i++)
+    {
+      if ((*fBadPixels)[i].IsBad())
+        continue;
+      fAverageAreaNum  [(*fGeom)[i].GetAidx()  ]++;
+      fAverageSectorNum[(*fGeom)[i].GetSector()]++;
+    }
+
+  if (fHiGainArray->GetEntries()==0)
+  {
+      fHiGainArray->Expand(n);
+      for (Int_t i=0; i<n; i++)
+      {
+	  (*fHiGainArray)[i] = new MHCalibrationChargeHiGainPix;
+          InitializeHiGainHists((*this)[i],(*fBadPixels)[i],i);
+      }
+  }
+
+ 
+  if (fLoGainArray->GetEntries()==0)
+  {
+      fLoGainArray->Expand(n);
+      
+      for (Int_t i=0; i<n; i++)
+      {
+	  (*fLoGainArray)[i] = new MHCalibrationChargeLoGainPix;
+          InitializeLoGainHists((*this)(i),(*fBadPixels)[i],i);
+      }
+      
+  }
+
+  if (fAverageHiGainAreas->GetEntries()==0)
+  {
+    fAverageHiGainAreas->Expand(nareas);
     
-  for (Int_t i=0; i<n; i++)
-    {
-      if ((*this)[i].IsExcluded())
-        continue;
-
-      const Int_t aidx  = (*fGeom)[i].GetAidx();
-      fAverageAreaNum[aidx]++;
-    }
-
-
-  const Int_t nsectors = fGeom->GetNumSectors();
-
+    for (Int_t j=0; j<nareas; j++)
+      {
+        (*fAverageHiGainAreas)[j] = 
+          new MHCalibrationChargeHiGainPix("AverageHiGainArea",
+                                           "Average HiGain FADC sums area idx ");
+        InitializeHiGainHists(GetAverageHiGainArea(j),fCam->GetAverageBadArea(j),j);        
+        GetAverageHiGainArea(j).GetHGausHist()->SetTitle("Summed FADC slices average HiGain Area Idx ");
+        GetAverageHiGainArea(j).GetHAbsTime()->SetTitle("Absolute Arrival Time average HiGain Area Idx ");
+        GetAverageHiGainArea(j).SetChargeNbins(fAverageNbins);
+      }
+  }
+
+  if (fAverageLoGainAreas->GetEntries()==0)
+    {
+      fAverageLoGainAreas->Expand(nareas);
+      
+      for (Int_t j=0; j<nareas; j++)
+        {
+	  (*fAverageLoGainAreas)[j] = 
+            new MHCalibrationChargeLoGainPix("AverageLoGainArea",
+                                             "Average LoGain FADC sums of pixel area idx ");
+          InitializeLoGainHists(GetAverageLoGainArea(j),fCam->GetAverageBadArea(j),j);        
+          GetAverageLoGainArea(j).GetHGausHist()->SetTitle("Summed FADC slices average LoGain Area Idx ");
+          GetAverageLoGainArea(j).GetHAbsTime()->SetTitle("Absolute Arrival Time average LoGain Area Idx ");
+          GetAverageLoGainArea(j).SetChargeNbins(fAverageNbins);
+        }
+    }
+  
   if (fAverageHiGainSectors->GetEntries()==0)
   {
@@ -498,20 +569,11 @@
       for (Int_t j=0; j<nsectors; j++)
       {
-	  //
-	  // Oscillating pixels
-	  //
 	  (*fAverageHiGainSectors)[j] = 
             new MHCalibrationChargeHiGainPix("AverageHiGainSector",
                                              "Average HiGain FADC sums of pixel sector ");
-          MHCalibrationChargeHiGainPix &hist = GetAverageHiGainSector(j);
-
-          hist.GetHGausHist()->SetTitle("Summed FADC slices average Hi Gain pixels Sector ");
-          hist.GetHAbsTime()->SetTitle("Absolute Arrival Time average Hi Gain pixels Sector ");
-          hist.SetChargeFirst(-1000.);
-          hist.SetChargeLast(4000.);
-          hist.SetChargeNbins(4000);
-	  hist.Init();
-	  hist.ChangeHistId(j);
-          hist.SetEventFrequency(fPulserFrequency);
+          InitializeHiGainHists(GetAverageHiGainSector(j),fCam->GetAverageBadSector(j),j);        
+          GetAverageHiGainSector(j).GetHGausHist()->SetTitle("Summed FADC slices average HiGain Sector ");
+          GetAverageHiGainSector(j).GetHAbsTime()->SetTitle("Absolute Arrival Time average HiGain Sector ");
+          GetAverageHiGainSector(j).SetChargeNbins(fAverageNbins);
       }
   }
@@ -523,40 +585,90 @@
       for (Int_t j=0; j<nsectors; j++)
       {
-	  //
-	  // Oscillating pixels
-	  //
 	  (*fAverageLoGainSectors)[j] = 
             new MHCalibrationChargeLoGainPix("AverageLoGainSector",
                                              "Average LoGain FADC sums of pixel sector ");
-          MHCalibrationChargeLoGainPix &hist = GetAverageLoGainSector(j);
-
-          hist.GetHGausHist()->SetTitle("Summed FADC slices average Lo Gain pixels Sector ");
-          hist.GetHAbsTime()->SetTitle("Absolute Arrival Time average Lo Gain pixels Sector ");
-          hist.SetChargeFirst(-1000.);
-          hist.SetChargeLast(4000.);
-          hist.SetChargeNbins(4000);
-	  hist.Init();
-	  hist.ChangeHistId(j);
-          hist.SetEventFrequency(fPulserFrequency);
+          InitializeLoGainHists(GetAverageLoGainSector(j),fCam->GetAverageBadSector(j),j); 
+          GetAverageLoGainSector(j).GetHGausHist()->SetTitle("Summed FADC slices average LoGain Sector ");
+          GetAverageLoGainSector(j).GetHAbsTime()->SetTitle("Absolute Arrival Time average LoGain Sector ");
+          GetAverageLoGainSector(j).SetChargeNbins(fAverageNbins);
       }
   }
 
-  fAverageSectorNum.        Set(nsectors);
-
-  for (Int_t i=0; i<n; i++)
-    {
-      if ((*this)[i].IsExcluded())
-        continue;
-
-      const Int_t sector  = (*fGeom)[i].GetSector();
-      fAverageSectorNum[sector]++;
-    }
-
-  
   return kTRUE;
 }
-  
-
-// --------------------------------------------------------------------------
+
+// -------------------------------------------------------------
+//
+// If MBadPixelsPix::IsBad():
+// - calls MHCalibrationChargeHiGainPix::SetExcluded()
+//
+// Calls:
+// - MHCalibrationChargePix::Init()
+// - MHCalibrationChargePix::ChangeHistId(i)
+// - MHGausEvents::SetEventFrequency(fPulserFrequency)
+// 
+void MHCalibrationChargeCam::InitializeHiGainHists(MHCalibrationChargeHiGainPix &hist,MBadPixelsPix &bad, const Int_t i)
+{
+  
+  if (bad.IsBad())
+    {
+      *fLog << warn << "Excluded pixel: " << i << " from calibration " << endl;
+      hist.SetExcluded();
+    }
+  
+  hist.Init();
+  hist.ChangeHistId(i);
+  hist.SetEventFrequency(fPulserFrequency);
+          
+}
+
+// -------------------------------------------------------------
+//
+// If MBadPixelsPix::IsBad():
+// - calls MHCalibrationChargeLoGainPix::SetExcluded()
+//
+// Calls:
+// - MHCalibrationChargePix::Init()
+// - MHCalibrationChargePix::ChangeHistId(i)
+// - MHGausEvents::SetEventFrequency(fPulserFrequency)
+// 
+void MHCalibrationChargeCam::InitializeLoGainHists(MHCalibrationChargeLoGainPix &hist,MBadPixelsPix &bad, const Int_t i)
+{
+
+  if (bad.IsBad())
+    hist.SetExcluded();
+  
+  hist.Init();
+  hist.ChangeHistId(i);
+  hist.SetEventFrequency(fPulserFrequency);
+}
+
+  
+// --------------------------------------------------------------------------
+//
+// Retrieves from MExtractedSignalCam:
+// - number of pixels
+// - first used LoGain FADC slice
+//
+// Retrieves from MGeomCam:
+// - number of pixel areas
+// - number of sectors
+//
+// For all TObjArray's (including the averaged ones), the following steps are performed: 
+//
+// 1) Test size and return kFALSE if not matching
+//
+// 2) Fill Charges histograms (MHGausEvents::FillHistAndArray()) with:
+// - MExtractedSignalPix::GetExtractedSignalHiGain();
+// - MExtractedSignalPix::GetExtractedSignalLoGain();
+//
+// 3) Set number of saturated slices (MHCalibrationChargePix::SetSaturated()) with:
+// - MExtractedSignalPix::GetNumHiGainSaturated();
+// - MExtractedSignalPix::GetNumLoGainSaturated();
+//
+// 4) Fill AbsTime histograms (MHCalibrationChargePix::FillAbsTime()) with:
+// - MRawEvtPixelIter::GetIdxMaxHiGainSample();       
+// - MRawEvtPixelIter::GetIdxMaxLoGainSample(first slice);
+//
 Bool_t MHCalibrationChargeCam::Fill(const MParContainer *par, const Stat_t w)
 {
@@ -725,11 +837,12 @@
 // --------------------------------------------------------------------------
 //
-// 1) Return if the charge distribution is already succesfully fitted  
-//    or if the histogram is empty
-// 2) Fit the histograms with a Gaussian
-// 3) In case of failure set the bit kFitted to false and take histogram means and RMS
-// 4) Check for pickup noise
-// 5) Check the fourier spectrum 
-// 5) Retrieve the results and store them in this class
+// For all TObjArray's (including the averaged ones), the following steps are performed: 
+//
+// 1) Return if the pixel is excluded
+// 
+// 2) Call to FinalizeHiGainHists() and FinalizeLoGainHists()
+//
+// For all averaged areas, the fitted sigma is multiplied with the square root of 
+// the number involved pixels
 //
 Bool_t MHCalibrationChargeCam::Finalize()
@@ -832,4 +945,29 @@
 }
 
+// ---------------------------------------------------------------------------
+//
+// Returns if the histogram is empty and sets the following flag:
+// - MBadPixelsPix::SetUnsuitable(MBadPixelsPix::kUnsuitableRun)
+//
+// Returns if the number of saturated slices (MHCalibrationChargePix::GetSaturated()) is 
+// higher than the allowed limit (fNumHiGainSaturationLimit*histogram-entries), creates 
+// the fourier spectrum and sets the following flags:
+// - MCalibrationChargePix::SetHiGainSaturation()
+//
+// Fits the histograms with a Gaussian, in case of failure 
+// calls MHCalibrationChargePix::RepeatFit(), in case of repeated failure 
+// calls MHGausEvents::BypassFit() and sets the following flags:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kHiGainNotFitted )
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnreliableRun   )
+// 
+// Counts the number of pickup events
+//
+// Creates the fourier spectrum and tests MHGausEvents::IsFourierSpectrumOK(). 
+// In case no, sets the following flags:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kHiGainOscillating )
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnreliableRun     )
+//
+// Retrieves the results and store them in MCalibrationChargePix
+//
 void MHCalibrationChargeCam::FinalizeHiGainHists(MHCalibrationChargeHiGainPix &hist, 
                                                  MCalibrationChargePix &pix, 
@@ -851,5 +989,5 @@
       return;
     }
-    
+
     //
     // 2) Fit the Hi Gain histograms with a Gaussian
@@ -877,18 +1015,4 @@
     hist.CreateFourierSpectrum();
     
-    //
-    // 6) Retrieve the results and store them in this class
-    //
-    pix.SetHiGainMeanCharge(     hist.GetMean()     );
-    pix.SetHiGainMeanChargeErr(  hist.GetMeanErr()  );
-    pix.SetHiGainSigmaCharge(    hist.GetSigma()    );
-    pix.SetHiGainSigmaChargeErr( hist.GetSigmaErr() );
-    pix.SetHiGainChargeProb    ( hist.GetProb()     );
-    
-    pix.SetAbsTimeMean         ( hist.GetAbsTimeMean());
-    pix.SetAbsTimeRms          ( hist.GetAbsTimeRms() );
-    
-    pix.SetHiGainNumPickup     (  hist.GetPickup()           );
-    
     if (!hist.IsFourierSpectrumOK())
       {
@@ -897,7 +1021,49 @@
         bad.SetUnsuitable(   MBadPixelsPix::kUnreliableRun     );
       }
-}
-
-
+
+    //
+    // 6) Retrieve the results and store them in this class
+    //
+    pix.SetHiGainMeanCharge(     hist.GetMean()      );
+    pix.SetHiGainMeanChargeErr(  hist.GetMeanErr()   );
+    pix.SetHiGainSigmaCharge(    hist.GetSigma()     );
+    pix.SetHiGainSigmaChargeErr( hist.GetSigmaErr()  );
+    pix.SetHiGainChargeProb    ( hist.GetProb()      );
+    
+    pix.SetAbsTimeMean         ( hist.GetAbsTimeMean());
+    pix.SetAbsTimeRms          ( hist.GetAbsTimeRms() );
+    
+    pix.SetHiGainNumPickup     ( hist.GetPickup()     );
+    
+}
+
+// ---------------------------------------------------------------------------
+//
+// Returns if the histogram is empty. If yes and the flag MCalibrationChargePix::IsHiGainSaturation()
+// is set, sets the following flag:
+// - MBadPixelsPix::SetUnsuitable(MBadPixelsPix::kUnsuitableRun)
+//
+// Returns if the number of saturated slices (MHCalibrationChargePix::GetSaturated()) is 
+// higher than the allowed limit (fNumLoGainSaturationLimit*histogram-entries), creates 
+// the fourier spectrum and sets the following flags:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kLoGainSaturation ); 
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
+//
+// Fits the histograms with a Gaussian, in case of failure calls MHCalibrationChargePix::RepeatFit(), 
+// in case of repeated failure calls MHGausEvents::BypassFit() and sets the following flags:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kLoGainNotFitted )
+//   If the flag MCalibrationChargePix::IsHiGainSaturation() is set, sets additionally:
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnreliableRun   )
+// 
+// Counts the number of pickup events
+//
+// Creates the fourier spectrum and tests MHGausEvents::IsFourierSpectrumOK(). 
+// In case no, sets the following flags:
+// - MBadPixelsPix::SetUncalibrated( MBadPixelsPix::kLoGainOscillating )
+//   If the flag MCalibrationChargePix::IsHiGainSaturation() is set, sets additionally:
+// - MBadPixelsPix::SetUnsuitable(   MBadPixelsPix::kUnreliableRun     )
+//
+// Retrieves the results and store them in MCalibrationChargePix
+//
 void MHCalibrationChargeCam::FinalizeLoGainHists(MHCalibrationChargeLoGainPix &hist, 
                                                  MCalibrationChargePix &pix, 
@@ -915,5 +1081,4 @@
     {
       *fLog << warn << "Saturated Lo Gain histogram in pixel: " << pix.GetPixId() << endl;
-      pix.SetLoGainSaturation();
       bad.SetUncalibrated( MBadPixelsPix::kLoGainSaturation ); 
       bad.SetUnsuitable(   MBadPixelsPix::kUnsuitableRun    );
@@ -947,4 +1112,10 @@
     hist.CreateFourierSpectrum();
     
+    if (!hist.IsFourierSpectrumOK())
+      {
+        bad.SetUncalibrated( MBadPixelsPix::kLoGainOscillating );
+        if (pix.IsHiGainSaturation())
+          bad.SetUnsuitable(MBadPixelsPix::kUnreliableRun);
+      }
     //
     // 6) Retrieve the results and store them in this class
@@ -964,14 +1135,10 @@
     pix.SetLoGainNumPickup     (  hist.GetPickup()    );
 
-    if (!hist.IsFourierSpectrumOK())
-      {
-        bad.SetUncalibrated( MBadPixelsPix::kLoGainOscillating );
-        if (pix.IsHiGainSaturation())
-          bad.SetUnsuitable(MBadPixelsPix::kUnreliableRun);
-      }
 }    
 
-
-
+// --------------------------------------------------------------------------
+//
+// Dummy, needed by MCamEvent
+//
 Bool_t MHCalibrationChargeCam::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
 {
@@ -988,5 +1155,15 @@
 }
 
-
+// -----------------------------------------------------------------------------
+// 
+// Default draw:
+//
+// Displays the averaged areas, both High Gain and Low Gain 
+//
+// The following options can be chosen:
+//
+// "": displays the fHGausHist and the fHAbsTime
+// "all": executes additionally MHGausEvents::Draw(), with options
+//
 void MHCalibrationChargeCam::Draw(const Option_t *opt)
 {
@@ -1021,4 +1198,10 @@
 }
 
+// -----------------------------------------------------------------------------
+// 
+// Default draw:
+//
+// Displays a TPaveText with the re-normalized sigmas of the average area
+//
 void MHCalibrationChargeCam::DrawAverageSigma(Bool_t sat, Bool_t inner,
                                               Float_t sigma, Float_t sigmaerr,
Index: trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.h
===================================================================
--- trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.h	(revision 3625)
+++ trunk/MagicSoft/Mars/mcalib/MHCalibrationChargeCam.h	(revision 3626)
@@ -36,11 +36,12 @@
 private:
 
+  static const Int_t   fgAverageNbins;               // The default for fAverageNbins
   static const Float_t fgNumHiGainSaturationLimit;   // The default for fNumHiGainSaturationLimit
   static const Float_t fgNumLoGainSaturationLimit;   // The default for fNumLoGainSaturationLimit
   static const Int_t   fgPulserFrequency;            // The default for fPulserFrequency
   
+  Int_t   fAverageNbins;              // Number of bins for the average histograms
   Float_t fNumHiGainSaturationLimit;  // Rel. amount sat. higain FADC slices until pixel is called saturated
   Float_t fNumLoGainSaturationLimit;  // Rel. amount sat. logain FADC slices until pixel is called saturated
-
   Int_t   fPulserFrequency;           // Light pulser frequency
   
@@ -66,4 +67,7 @@
   TArrayI fAverageSectorNum;          // Number of pixels in average pixels per sector 
 
+  void InitializeHiGainHists(MHCalibrationChargeHiGainPix &hist, MBadPixelsPix &bad, const Int_t i);
+  void InitializeLoGainHists(MHCalibrationChargeLoGainPix &hist, MBadPixelsPix &bad, const Int_t i);  
+  
   void FinalizeHiGainHists(MHCalibrationChargeHiGainPix &hist, MCalibrationChargePix &pix, MBadPixelsPix &bad);
   void FinalizeLoGainHists(MHCalibrationChargeLoGainPix &hist, MCalibrationChargePix &pix, MBadPixelsPix &bad);
@@ -77,6 +81,7 @@
   ~MHCalibrationChargeCam();
 
+  void SetAverageNbins(    const Int_t bins=fgAverageNbins  )          { fAverageNbins = bins; }
+  void SetNumLoGainSaturationLimit( const Float_t lim=fgNumLoGainSaturationLimit) { fNumLoGainSaturationLimit = lim; }
   void SetNumHiGainSaturationLimit( const Float_t lim=fgNumHiGainSaturationLimit) { fNumHiGainSaturationLimit = lim; }
-  void SetNumLoGainSaturationLimit( const Float_t lim=fgNumLoGainSaturationLimit) { fNumLoGainSaturationLimit = lim; }
   void SetPulserFrequency        ( const Int_t   f=fgPulserFrequency)          { fPulserFrequency = f;            }
   
