Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 5608)
+++ trunk/MagicSoft/Mars/Changelog	(revision 5609)
@@ -28,4 +28,7 @@
    * msignal/MExtractTimeAndChargeSpline.cc
      - another bugfix for the low-gain extraction
+
+   * mhcalib/MHCalibrationRelTimeCam.[h,cc]
+     - fixed bug in display of the averaged cams.
 
 
Index: trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.cc	(revision 5608)
+++ trunk/MagicSoft/Mars/mhcalib/MHCalibrationCam.cc	(revision 5609)
@@ -699,6 +699,6 @@
       for (Int_t i=0; i<npixels; i++)
       {
-        fHiGainArray->AddAt(new MHCalibrationPix(Form("%s%s",fHistName.Data(),"HiGainPix"),
-                                                 Form("%s%s",fHistTitle.Data()," High Gain Pixel")),i);
+        fHiGainArray->AddAt(new MHCalibrationPix(Form("%sHiGainArea%04d",fHistName.Data(),i),
+                                                 Form("%s High Gain Pixel %4d",fHistTitle.Data(),i)),i);
         
         MHCalibrationPix &pix = (*this)[i];          
@@ -719,6 +719,6 @@
     for (Int_t j=0; j<nareas; j++)
       {
-        fAverageHiGainAreas->AddAt(new MHCalibrationPix(Form("%s%s",fHistName.Data(),"HiGainArea"),
-                                                        Form("%s%s",fHistTitle.Data()," High Gain Area Idx ")),j);
+        fAverageHiGainAreas->AddAt(new MHCalibrationPix(Form("%sHiGainArea%d",fHistName.Data(),j),
+                                                        Form("%s High Gain Area Idx %d",fHistTitle.Data(),j)),j);
         
         MHCalibrationPix &pix = GetAverageHiGainArea(j);
@@ -742,6 +742,6 @@
       for (Int_t j=0; j<nsectors; j++)
         {
-          fAverageHiGainSectors->AddAt(new MHCalibrationPix(Form("%s%s",fHistName.Data(),"HiGainSector"),
-                                                            Form("%s%s",fHistTitle.Data()," High Gain Sector ")),j);
+          fAverageHiGainSectors->AddAt(new MHCalibrationPix(Form("%sHiGainSector%02d",fHistName.Data(),j),
+                                                            Form("%s High Gain Sector %02d",fHistTitle.Data(),j)),j);
           MHCalibrationPix &pix = GetAverageHiGainSector(j);
 
@@ -778,6 +778,6 @@
       for (Int_t i=0; i<npixels; i++)
       {
-        fLoGainArray->AddAt(new MHCalibrationPix(Form("%s%s",fHistName.Data(),"LoGainPix"),
-                                                 Form("%s%s",fHistTitle.Data()," Low Gain Pixel")),i);
+        fLoGainArray->AddAt(new MHCalibrationPix(Form("%sLoGainPix%04d",fHistName.Data(),i),
+                                                 Form("%s Low Gain Pixel%$04d",fHistTitle.Data(),i)),i);
         
         MHCalibrationPix &pix = (*this)(i);          
@@ -798,6 +798,6 @@
     for (Int_t j=0; j<nareas; j++)
       {
-        fAverageLoGainAreas->AddAt(new MHCalibrationPix(Form("%s%s",fHistName.Data(),"LoGainArea"),
-                                                        Form("%s%s",fHistTitle.Data()," Low Gain Area Idx ")),j);
+        fAverageLoGainAreas->AddAt(new MHCalibrationPix(Form("%sLoGainArea%d",fHistName.Data(),j),
+                                                        Form("%s Low Gain Area Idx %d",fHistTitle.Data(),j)),j);
         
         MHCalibrationPix &pix = GetAverageLoGainArea(j);
@@ -821,6 +821,6 @@
       for (Int_t j=0; j<nsectors; j++)
         {
-          fAverageLoGainSectors->AddAt(new MHCalibrationPix(Form("%s%s",fHistName.Data(),"LoGainSector"),
-                                                            Form("%s%s",fHistTitle.Data()," Low Gain Sector ")),j);
+          fAverageLoGainSectors->AddAt(new MHCalibrationPix(Form("%sLoGainSector%02d",fHistName.Data(),j),
+                                                            Form("%s Low Gain Sector %02d",fHistTitle.Data(),j)),j);
           MHCalibrationPix &pix = GetAverageLoGainSector(j);
 
Index: trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.cc
===================================================================
--- trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.cc	(revision 5608)
+++ trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.cc	(revision 5609)
@@ -413,15 +413,57 @@
   MBadPixelsCam   *badcam = fIntensBad ? fIntensBad->GetCam() : fBadPixels;
 
+  const Int_t nareas = fAverageHiGainAreas->GetSize();
+  const Int_t nsectors = fAverageHiGainSectors->GetSize();
+
+  TArrayI satarea(nareas);
+  TArrayI satsect(nsectors);
+  fNumareahi  .Reset(); 
+  fNumsectorhi.Reset(); 
+
   for (Int_t i=0; i<fHiGainArray->GetSize(); i++)
     {
       
       MHCalibrationPix &histhi = (*this)[i];
-      
+
       if (histhi.IsExcluded())
         continue;
       
+      const Int_t aidx   = (*fGeom)[i].GetAidx();
+      const Int_t sector = (*fGeom)[i].GetSector();
+
       MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)(*relcam)[i] ;
 
+      fNumareahi[aidx]++;
+      fNumsectorhi[sector]++;
+      //
+      // Check saturation
+      // 
       if (histhi.GetSaturated() > fNumHiGainSaturationLimit*histhi.GetHGausHist()->GetEntries())
+        {
+          pix.SetHiGainSaturation();
+          histhi.SetExcluded();
+          satarea[aidx]++;
+          satsect[sector]++;
+        }
+      else
+        if (IsLoGain())
+          (*this)(i).SetExcluded();
+
+      //
+      // Check histogram overflow
+      // 
+      CheckOverflow(histhi);
+      if (IsLoGain())
+        CheckOverflow((*this)(i));
+
+    }
+
+  for (Int_t j=0; j<nareas; j++)
+    {
+      
+      MHCalibrationPix     &histhi = GetAverageHiGainArea(j);      
+      MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)relcam->GetAverageArea(j);
+
+      if (satarea[j] > 0.5*fNumareahi[j])
         {
           pix.SetHiGainSaturation();
@@ -430,38 +472,12 @@
       else
         if (IsLoGain())
-          (*this)(i).SetExcluded();
-
-      Stat_t overflow = histhi.GetHGausHist()->GetBinContent(histhi.GetHGausHist()->GetNbinsX()+1);
-      if (overflow > 0.1)
-        {
-          *fLog << warn << "HiGain Hist-overflow occurred " << overflow
-                << " times in pix: " << i << " (w/o saturation!) " << endl;
-          //          bad.SetUncalibrated( MBadPixelsPix::kHiGainOverFlow ); 
-        }
-
-      overflow = histhi.GetHGausHist()->GetBinContent(0);
-      if (overflow > 0.1)
-        {
-          *fLog << warn << "HiGain Hist-underflow occurred " << overflow
-                << " times in pix: " << i << " (w/o saturation!) " << endl;
-          //          bad.SetUncalibrated( MBadPixelsPix::kHiGainOverFlow ); 
-        }
-    }
-
-  for (Int_t j=0; j<fAverageHiGainAreas->GetSize(); j++)
-    {
-      
-      MHCalibrationPix &histhi = GetAverageHiGainArea(j);      
-
-      if (histhi.GetSaturated() > fNumHiGainSaturationLimit*histhi.GetHGausHist()->GetEntries())
-        {
-          MCalibrationRelTimePix &pix = (MCalibrationRelTimePix&)relcam->GetAverageArea(j);
-          pix.SetHiGainSaturation();
-          histhi.SetExcluded();
-        }
-      else
-        if (IsLoGain())
           GetAverageLoGainArea(j).SetExcluded();
 
+      //
+      // Check histogram overflow
+      // 
+      CheckOverflow(histhi);
+      if (IsLoGain())
+        CheckOverflow(GetAverageLoGainArea(j));
    }
   
@@ -472,5 +488,5 @@
       MCalibrationRelTimePix  &pix = (MCalibrationRelTimePix&)relcam->GetAverageSector(j) ;
 
-      if (histhi.GetSaturated() > fNumHiGainSaturationLimit*histhi.GetHGausHist()->GetEntries())
+      if (satsect[j] > 0.5*fNumsectorhi[j])
         {
           pix.SetHiGainSaturation();
@@ -480,4 +496,11 @@
         if (IsLoGain())        
           GetAverageLoGainSector(j).SetExcluded();
+
+      //
+      // Check histogram overflow
+      // 
+      CheckOverflow(histhi);
+      if (IsLoGain())
+        CheckOverflow(GetAverageLoGainSector(j));
     }
 
@@ -632,6 +655,6 @@
        // Ask for Hi-Gain saturation
        //
-       if (hipix.GetSaturated() > fNumHiGainSaturationLimit*hipix.GetHGausHist()->GetEntries() && IsLoGain())
-         {
+       if (hipix.IsExcluded() && IsLoGain())
+        {
            MHCalibrationPix &lopix = GetAverageLoGainArea(i);
            DrawDataCheckPixel(lopix,0.);
@@ -641,4 +664,28 @@
     }      
 }
+
+void MHCalibrationRelTimeCam::CheckOverflow( MHCalibrationPix &pix )
+{
+
+  if (pix.IsExcluded())
+    return;
+
+  TH1F *hist = pix.GetHGausHist();
+  
+  Stat_t overflow = hist->GetBinContent(hist->GetNbinsX()+1);
+  if (overflow > 0.0005*hist->GetEntries())
+    {
+      *fLog << warn << "HiGain Hist-overflow occurred " << overflow
+            << " times in " << pix.GetName() << " (w/o saturation!) " << endl;
+    }
+  
+  overflow = hist->GetBinContent(0);
+  if (overflow > 0.0005*hist->GetEntries())
+    {
+      *fLog << warn << "HiGain Hist-underflow occurred " << overflow
+            << " times in " << pix.GetName() << " (w/o saturation!) " << endl;
+    }
+}
+
 
 // -----------------------------------------------------------------------------
Index: trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.h
===================================================================
--- trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.h	(revision 5608)
+++ trunk/MagicSoft/Mars/mhcalib/MHCalibrationRelTimeCam.h	(revision 5609)
@@ -48,4 +48,5 @@
   void   FinalizeBadPixels();
 
+  void   CheckOverflow( MHCalibrationPix &pix);
   void   DrawDataCheckPixel(MHCalibrationPix &pix, const Float_t refline);
   
