Index: trunk/MagicSoft/Mars/manalysis/MPedestalCam.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MPedestalCam.cc	(revision 3034)
+++ trunk/MagicSoft/Mars/manalysis/MPedestalCam.cc	(revision 3038)
@@ -143,5 +143,5 @@
 // Get i-th pixel (pixel number)
 //
-const MHPedestalPixel &MPedestalCam::operator()(UInt_t i) const
+MHPedestalPixel &MPedestalCam::operator()(UInt_t i) const
 {
   return *static_cast<MHPedestalPixel*>(fHArray->UncheckedAt(i));
@@ -155,9 +155,44 @@
 
     fArray->ForEach(TObject, Clear)();
-    //    fHArray->ForEach(TObject, Clear)();
 
     fTotalEntries     = 0;
     fExtractSlices    = 0.;
 }
+
+
+// --------------------------------------------------------------------------
+//
+// Our own clone function is necessary since root 3.01/06 or Mars 0.4
+// I don't know the reason
+//
+TObject *MPedestalCam::Clone(const char *) const
+{
+
+  const Int_t n1 = fArray->GetSize();
+  const Int_t n2 = fHArray->GetSize();
+  
+  //
+  // FIXME, this might be done faster and more elegant, by direct copy.
+  //
+  MPedestalCam *cam = new MPedestalCam;
+  
+  cam->fArray->Expand(n1);
+  cam->fHArray->Expand(n2);
+  
+  for (int i=0; i<n1; i++)
+    {
+      delete (*cam->fArray)[i];
+      (*cam->fArray)[i] = (*fArray)[i]->Clone();
+    }
+
+  for (int i=0; i<n2; i++)
+    {
+      delete (*cam->fHArray)[i];
+      (*cam->fHArray)[i] = (*fHArray)[i]->Clone();
+    }
+  return cam;
+}
+
+
 
 // --------------------------------------------------------------------------
@@ -170,5 +205,4 @@
 {
 
-  fArray->ForEach(MPedestalPix, InitUseHists)();
   fHArray->Delete();
 
@@ -189,4 +223,5 @@
     }
   
+
   Float_t slices = (Float_t)signal->GetNumUsedFADCSlices();
 
@@ -218,4 +253,6 @@
           MHPedestalPixel &hist = (*this)(i);
           hist.ChangeHistId(i);
+          MPedestalPix &pix = (*this)[i];
+          pix.InitUseHists();
         }
     }
@@ -229,13 +266,20 @@
   for (Int_t i=0; i<n; i++)
     {
+
       const MExtractedSignalPix &pix = (*signal)[i];
       
       const Float_t sig = pix.GetExtractedSignalHiGain();
-      const Float_t sigPerSlice = sig/fExtractSlices;
-      
+
       MHPedestalPixel &hist = (*this)(i);
-
-      hist.FillCharge(sigPerSlice);
-      hist.FillChargevsN(sigPerSlice);
+      //
+      // Don't fill signal per slice, we get completely screwed up 
+      // with the sigma. Better fill like it is and renorm later
+      //
+      //      const Float_t sigPerSlice = sig/fExtractSlices;
+      //      hist.FillCharge(sigPerSlice);
+      //      hist.FillChargevsN(sigPerSlice);
+      const Float_t signal = sig;
+      hist.FillCharge(signal);
+      hist.FillChargevsN(signal);
     }
   
@@ -247,4 +291,5 @@
     for (Int_t i=0; i<fHArray->GetSize(); i++)
     {
+
         MHPedestalPixel &hist = (*this)(i);
 
@@ -262,4 +307,6 @@
         //
         hist.FitCharge();
+        hist.Renorm(fExtractSlices);
+
     }
     return kTRUE;
@@ -343,12 +390,12 @@
 
   const Float_t pederr   = rms/TMath::Sqrt((Float_t)fTotalEntries);
-  const Float_t rmserr   = rms/TMath::Sqrt((Float_t)fTotalEntries/2.);
+  const Float_t rmserr   = rms/TMath::Sqrt((Float_t)fTotalEntries*2.);
 
   const MHPedestalPixel &hist = (*this)(idx);
 
   const Float_t mean     = hist.GetChargeMean();
-  const Float_t meanerr  = hist.GetChargeMeanErr() * TMath::Sqrt((Float_t)fExtractSlices);
-  const Float_t sigma    = hist.GetChargeSigma()   * TMath::Sqrt((Float_t)fExtractSlices);
-  const Float_t sigmaerr = hist.GetChargeSigmaErr()* TMath::Sqrt((Float_t)fExtractSlices);
+  const Float_t meanerr  = hist.GetChargeMeanErr();
+  const Float_t sigma    = hist.GetChargeSigma() ;
+  const Float_t sigmaerr = hist.GetChargeSigmaErr();
   const Float_t prob     = hist.GetChargeProb();
 
@@ -368,58 +415,59 @@
       break;
     case 4:
-      //      if ((*this)[idx].IsFitValid())
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = mean;
-        //      else
-        //        return kFALSE;
       break;
     case 5:
-      //      if ((*this)[idx].IsFitValid())
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = meanerr;
-        //      else
-        //        return kFALSE;
       break;
     case 6:
-      //      if ((*this)[idx].IsFitValid())
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = sigma;
-        //      else
-        //        return kFALSE;
       break;
     case 7:
-      //      if ((*this)[idx].IsFitValid())
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = sigmaerr;
-        //      else
-        //        return kFALSE;
       break;
     case 8:
-      //      if ((*this)[idx].IsFitValid())
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = prob;
-        //      else
-        //        return kFALSE;
       break;
     case 9:
-      //      if ((*this)[idx].IsFitValid())
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = 2.*(ped-mean)/(ped+mean);
-        //      else
-        //        return kFALSE;
       break;
     case 10:
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = TMath::Sqrt((pederr*pederr + meanerr*meanerr) * (ped*ped + mean*mean))
             *2./(ped+mean)/(ped+mean);
       break;
     case 11:
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = 2.*(pederr - meanerr)/(pederr + meanerr);
       break;
     case 12:
-      val = 2.*(rms-sigma)/(rms+sigma);
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
+      val = 2.*(sigma-rms)/(sigma+rms);
       break;
     case 13:
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
       val = TMath::Sqrt((rmserr*rmserr + sigmaerr*sigmaerr) * (rms*rms + sigma*sigma))
             *2./(rms+sigma)/(rms+sigma);
       break;
     case 14:
-      //      if ((*this)[idx].IsFitValid())
-      val = 2.*(rmserr - sigmaerr)/(rmserr + sigmaerr);
-              //      else
-        //        return kFALSE;
+      if (!(*this)(idx).IsFitOK())
+        return kFALSE;
+      val = 2.*(sigmaerr - rmserr)/(sigmaerr + rmserr);
       break;
     default:
@@ -431,4 +479,4 @@
 void MPedestalCam::DrawPixelContent(Int_t idx) const
 {
-  (*this)[idx].Draw();
-}
+  (*this)(idx).Draw();
+}
