Index: trunk/MagicSoft/Mars/Changelog
===================================================================
--- trunk/MagicSoft/Mars/Changelog	(revision 2598)
+++ trunk/MagicSoft/Mars/Changelog	(revision 2599)
@@ -4,4 +4,21 @@
 
                                                  -*-*- END OF LINE -*-*-
+ 2003/12/04: Markus Gaug
+
+  * manalysis/MCalibration*
+     - implemented some of Thomas Bretz suggestions to make the code nicer
+     - implemented the possibility to have cosmics in the calibration data 
+       and still fit it
+     - conversion HiGain/LoGain now left out (it seems to be already done in 
+	the data)
+
+  * mhist/MHCalibration*
+     - implemented some of Thomas Bretz suggestions to make the code nicer
+     - implemented the possibility to have cosmics in the calibration data 
+       and still fit it
+
+  * macros/calibration.C
+     - MStatusDisplay of calibration histograms a little bit more readable
+
  2003/12/03: Abelardo Moralejo
 
Index: trunk/MagicSoft/Mars/macros/calibration.C
===================================================================
--- trunk/MagicSoft/Mars/macros/calibration.C	(revision 2598)
+++ trunk/MagicSoft/Mars/macros/calibration.C	(revision 2599)
@@ -62,8 +62,8 @@
     tlist.AddToList(&fill);
 
-    MStatusDisplay *d1 = new MStatusDisplay;
+    //    MStatusDisplay *d1 = new MStatusDisplay;
  
     // Set update time to 3s
-    d1->SetUpdateTime(3000);
+    //    d1->SetUpdateTime(3000);
  
     //
@@ -72,5 +72,5 @@
     MEvtLoop evtloop;
     evtloop.SetParList(&plist);
-    evtloop.SetDisplay(d1);  	
+    //    evtloop.SetDisplay(d1);  	
 
     //
@@ -82,6 +82,4 @@
     tlist.PrintStatistics();
 
-    MPedestalCam *ped = plist.FindObject("MPedestalCam");
-    ped.Print();
 
     //
@@ -122,5 +120,5 @@
 
     MHCamEvent hist2;
-    hist2.SetType(0);
+    hist2.SetType(8);
     plist2.AddToList(&hist2);
     MFillH fill2("MHCamEvent", "MCalibrationCam");
@@ -145,7 +143,24 @@
     //
     MCalibrationCam *cam = plist2.FindObject("MCalibrationCam");
-    MCalibrationPix *pix = cam->GetCalibrationPix(523);
-    pix->Draw();
-
+    cam.Print();
+
+    Int_t pixnr;
+
+    while (1) 
+      {
+        
+        cout << "Which pixel number do you want to display? (Press 0 to exit)" << endl;
+        cin >> pixnr;
+        if (pixnr == 0)
+          break;
+        
+        if (pixnr >= 577)
+          break;
+        
+        MCalibrationPix *pix = cam->GetCalibrationPix(pixnr);
+        pix->Draw();
+    
+      }
+    
     //
     // Here we are confronted to a serious bug in ROOT:
@@ -170,4 +185,6 @@
     MHCamera disp11  (geomcam, "MCalibrationCam;rq", "Reduced Charges");
     MHCamera disp12  (geomcam, "MCalibrationCam;errrq", "Error of Reduced Charges");
+    MHCamera disp13  (geomcam, "MCalibrationCam;phe", "Nr. of Phe's (F-Factor Method)");
+    MHCamera disp14  (geomcam, "MCalibrationCam;convphe", "Conversion Factor (F-Factor Method)");
 
     disp1.SetCamContent(*cam, 0);
@@ -183,4 +200,6 @@
     disp11.SetCamContent(*cam, 10);
     disp12.SetCamContent(*cam, 11);
+    disp12.SetCamContent(*cam, 12);
+    disp13.SetCamContent(*cam, 13);
 
     disp1.SetYTitle("Q [FADC counts]");
@@ -196,4 +215,6 @@
     disp11.SetYTitle("Q [FADC counts]");
     disp12.SetYTitle("\\Delta_{Q} [FADC counts]");
+    disp13.SetYTitle("Nr Phe's");
+    disp14.SetYTitle("Conversion Factor [Phe/FADC count]");
 
     MStatusDisplay *d2 = new MStatusDisplay;
@@ -202,6 +223,6 @@
     d2->SetUpdateTime(1000);
 
-    TCanvas *c1 = &d2->AddTab("Fitted Charges"); 
-    c1->Divide(5, 2);
+    TCanvas *c1 = &d2->AddTab("Charges Mean"); 
+    c1->Divide(2, 2);
 
     TObject *obj;
@@ -211,5 +232,5 @@
     obj=disp1.DrawCopy("hist");
 
-    c1->cd(6);
+    c1->cd(3);
     gPad->SetBorderMode(0);
     obj->Draw();
@@ -219,29 +240,36 @@
     obj=disp2.DrawCopy("hist");
 
-    c1->cd(7);
-    gPad->SetBorderMode(0);
-    obj->Draw();
-
-    c1->cd(3);
+    c1->cd(4);
+    gPad->SetBorderMode(0);
+    obj->Draw();
+
+    TCanvas *c11 = &d2->AddTab("Charges Sigma"); 
+    c11->Divide(2, 2);
+
+    c11->cd(1);
     gStyle->SetOptStat(1101);
     obj=disp3.DrawCopy("hist");
 
-    c1->cd(8);
-    gPad->SetBorderMode(0);
-    obj->Draw();
-
-    c1->cd(4);
+    c11->cd(3);
+    gPad->SetBorderMode(0);
+    obj->Draw();
+
+    c11->cd(2);
     gStyle->SetOptStat(1101);
     obj=disp4.DrawCopy("hist");
 
-    c1->cd(9);
-    gPad->SetBorderMode(0);
-    obj->Draw();
-
-    c1->cd(5);
+    c11->cd(4);
+    gPad->SetBorderMode(0);
+    obj->Draw();
+
+
+    TCanvas *c12 = &d2->AddTab("Fit Prob."); 
+    c12->Divide(1, 2);
+
+    c12->cd(1);
     gStyle->SetOptStat(1101);
     obj=disp5.DrawCopy("hist");
 
-    c1->cd(10);
+    c12->cd(2);
     gPad->SetBorderMode(0);
     obj->Draw();
@@ -282,5 +310,5 @@
 
     c3->cd(2);
-    gStyle->SetOptStat(1101);
+    gStyle->SetOptStat(1111);
     obj=disp10.DrawCopy("hist");
 
@@ -305,4 +333,22 @@
     obj->Draw();
 
+    TCanvas *c5 = &d2->AddTab("F-Factor Method");
+    c5->Divide(2, 2);
+
+    c5->cd(1);
+    gStyle->SetOptStat(1111);
+    obj=disp13.DrawCopy("hist");
+
+    c5->cd(3);
+    obj->Draw();
+
+    c5->cd(2);
+    gStyle->SetOptStat(1101);
+    obj=disp14.DrawCopy("hist");
+
+    c5->cd(4);
+    obj->Draw();
+
+
 #endif
 
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationBlindPix.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationBlindPix.h	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationBlindPix.h	(revision 2599)
@@ -51,7 +51,7 @@
   Float_t GetErrT()      const    { return fErrT;      }
   
-  Bool_t FillQ(Int_t q)            { return fHist->FillBPQ(q); }
-  Bool_t FillT(Int_t t)            { return fHist->FillBPT(t); }  
-  Bool_t FillRQvsT(Float_t rq, Int_t t) { return fHist->FillBPQvsN(rq,t); }    
+  Bool_t FillQ(Int_t q)            { return fHist->FillBlindPixelQ(q); }
+  Bool_t FillT(Int_t t)            { return fHist->FillBlindPixelT(t); }  
+  Bool_t FillRQvsT(Float_t rq, Int_t t) { return fHist->FillBlindPixelQvsN(rq,t); }    
   
   Bool_t IsValid()                 { return fLambda > 0. || fErrLambda > 0.; }
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCalc.cc	(revision 2599)
@@ -163,8 +163,4 @@
       }
 
-    //
-    // fRawEvt->GetNumPixels() does not work !!!!!!!!!!
-    //
-    fCalibrations->InitSize(577);
 
     switch (fColor)
@@ -189,15 +185,4 @@
       }
     
-
-    // MTime does not work!!!!
-
-    //    fEvtTime = (MTime*)pList->FindObject("MRawEvtTime");
-    //    if (!fEvtTime)
-    //      {
-    //        *fLog << dbginf << "MTime could not be created ... ¡!¡!¡!¡!" << endl;        
-    //      }
-    
-    // fEvtTime->SetTime(0.);
-
     
     return kTRUE;
@@ -222,5 +207,19 @@
     fNumHiGainSamples =  fRunHeader->GetNumSamplesHiGain();
     fNumLoGainSamples =  fRunHeader->GetNumSamplesLoGain();
- 
+
+    //
+    // FIXME: The next statement simply does not work: 
+    //        fRawEvt->GetNumPixels() returns always 0
+    // fCalibrations->InitSize(fRawEvt->GetNumPixels());
+    //
+
+    fCalibrations->InitSize(577);    
+
+    for (Int_t i=0;i<577;i++)
+      {
+        MCalibrationPix &pix = (*fCalibrations)[i];
+        pix.ChangePixId(i);
+      }
+    
     return kTRUE;
  
@@ -238,5 +237,5 @@
     fEvents++;
 
-    Bool_t cosmic = kFALSE;
+    Int_t cosmicpix = 0;
 
     MCalibrationBlindPix &blindpixel = *(fCalibrations->GetBlindPixel());
@@ -251,8 +250,4 @@
         const Int_t pixid = pixel.GetPixelId();
 
-        if (!fCalibrations->IsPixelUsed(pixid))
-          if (!fCalibrations->AddPixel(pixid)) 
-            *fLog << err << dbginf << "MCalibrationPix(" << pixid << ") could not be created !!" << endl;
-        
         Byte_t *ptr = pixel.GetHiGainSamples();
         Byte_t mid  = pixel.GetIdxMaxHiGainSample();
@@ -270,10 +265,12 @@
             mid = pixel.GetIdxMaxLoGainSample();
 
-            sum = (max == gkSaturationLimit              // overflow of LoGain ??? -> GimmeABreak!!!
-                   ? fHistOverFlow++, gkLoGainOverFlow   // OUCH (Florian was maybe right)
-                   : sum*gkConversionHiLo    );          // OUFF (Florian was wrong) !! 
-
-            //            *fLog << warn << "Warning: Saturation of HiGain reached in slice " << (int)mid << " !!! " << endl;
-            //            *fLog << warn << "Max = " << max << endl;
+            // 
+            //  FIXME: It seems the conversion HiGain LoGain is already
+            //         performed in the data?!?
+            //
+            sum = (max > gkSaturationLimit              // overflow of LoGain ??? -> GimmeABreak!!!
+                   ? fHistOverFlow++, gkLoGainOverFlow  // OUCH (Florian was maybe right)
+                   : sum  );                            // OUFF (Florian was wrong) !! 
+            //                   : sum*gkConversionHiLo    );          // OUFF (Florian was wrong) !! 
 
             if (fHistOverFlow) 
@@ -282,5 +279,4 @@
 
           }
-	
 
         MPedestalPix    &ped = (*fPedestals)[pixid];
@@ -298,5 +294,5 @@
 
 	// 
-	// This is a very primitive check for the number of 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
@@ -305,6 +301,6 @@
 	//
 
-	if ((float)sum < pedes+5.*pedrms)
-           cosmic = kTRUE;
+	if ((float)sum < pedes+4.*pedrms)
+           cosmicpix++;
 
         Float_t rsum      = (float)sum - pedes;
@@ -314,21 +310,24 @@
             
           case gkCalibrationBlindPixelId:
+
 	    // 
 	    // FIXME: This works only when the blind pixel ID is much larger than 
 	    //        the rest of the pixels (which is the case right now)
 	    //
-//	    if (!cosmic)
-	       if (!blindpixel.FillQ(sum)) 
+            if (cosmicpix < 100.)
+              {
+                if (!blindpixel.FillQ(sum)) 
                   *fLog << warn << 
-                  "Overflow or Underflow occurred filling Blind Pixel sum = " << sum << endl;
-
-            if (!blindpixel.FillT((int)mid)) 
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling Blind Pixel time = " << (int)mid << endl;
-
-            if (!blindpixel.FillRQvsT(rsum,fEvents))
-              *fLog << warn << 
-                "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
-
+                    "Overflow or Underflow occurred filling Blind Pixel sum = " << sum << endl;
+
+                if (!blindpixel.FillT((int)mid)) 
+                  *fLog << warn << 
+                    "Overflow or Underflow occurred filling Blind Pixel time = " << (int)mid << endl;
+
+                if (!blindpixel.FillRQvsT(rsum,fEvents))
+                  *fLog << warn << 
+                    "Overflow or Underflow occurred filling Blind Pixel eventnr = " << fEvents << endl;
+              }
+            
           case gkCalibrationPINDiodeId:
             if (!pindiode.FillQ(sum)) 
@@ -364,9 +363,7 @@
       } /* while (pixel.Next()) */
 
-    if (cosmic)
+    if (cosmicpix > 300.)
 	fCosmics++;
 
-    fCalibrations->SetReadyToSave();
-    
     return kTRUE;
 }
@@ -405,4 +402,5 @@
 
   *fLog << GetDescriptor() << " Fitting the Normal Pixels" << endl;
+
   //
   // loop over the pedestal events and check if we have calibration
@@ -411,21 +409,15 @@
     {
 
-      if (fCalibrations->IsPixelUsed(pixid))
-        {
-
-          MCalibrationPix &pix = (*fCalibrations)[pixid];
-
-          const Float_t ped    = (*fPedestals)[pixid].GetPedestal() * fNumHiGainSamples;
-          const Float_t prms   = (*fPedestals)[pixid].GetPedestalRms() * fNumHiGainSamples;
-
-          pix.SetPedestal(ped,prms);
-
-          if (TESTBIT(fFlags,kUseTFits))
-            pix.FitT();
-
-          if (!pix.FitQ())
-             continue;
-
-        }
+      MCalibrationPix &pix = (*fCalibrations)[pixid];
+
+      const Float_t ped    = (*fPedestals)[pixid].GetPedestal() * fNumHiGainSamples;
+      const Float_t prms   = (*fPedestals)[pixid].GetPedestalRms() * fNumHiGainSamples;
+
+      pix.SetPedestal(ped,prms);
+
+      if (TESTBIT(fFlags,kUseTFits))
+        pix.FitT();
+      
+      pix.FitQ();
     }
 
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCam.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCam.cc	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCam.cc	(revision 2599)
@@ -61,5 +61,5 @@
     fTitle = title ? title : "Storage container for the Calibration Information in the camera";
 
-    fPixels     = new TClonesArray("MCalibrationPix",0);
+    fPixels     = new TClonesArray("MCalibrationPix",1);
     fBlindPixel = new MCalibrationBlindPix();
     fPINDiode   = new MCalibrationPINDiode();
@@ -92,7 +92,5 @@
 
   //
-  // Here it is important to use GetEntriesFast, 
-  // We get the array index of the last "filled" fPixel entry
-  // (Not the number of "filled" fPixels!!)
+  // Here we get the number of "filled" fPixels!!
   //
   return fPixels->GetEntriesFast();
@@ -116,22 +114,4 @@
     return *static_cast<MCalibrationPix*>(fPixels->UncheckedAt(i));
 }
-
-Bool_t MCalibrationCam::AddPixel(Int_t idx) 
-{
-
-  //
-  // Check bounds first
-  //
-  if (!CheckBounds(idx))
-    InitSize(idx);
-
-  //
-  // in case, new cannot allocate memory, 
-  // return FALSE
-  //
-  return (new ((*fPixels)[idx]) MCalibrationPix(idx));
-
-}
-
 
 // --------------------------------------------------------------------------
@@ -173,7 +153,4 @@
 Bool_t MCalibrationCam::IsPixelFitted(Int_t idx) const 
 {
-  if (!IsPixelUsed(idx))
-    return kFALSE;
-
   return ((*this)[idx].GetRQ() > 0. && (*this)[idx].GetErrRQ() > 0.);
 }
@@ -208,12 +185,8 @@
     {
 
-      Int_t id = 0;
-      
       TIter Next(fPixels);
       MCalibrationPix *pix;
       while ((pix=(MCalibrationPix*)Next()))
         {
-          if (!IsPixelUsed(id++))
-            continue;
           if (pix->FitQ())
             nsuccess++;
@@ -222,6 +195,4 @@
   else                  // fit only the pixel with index i
     {
-      if (!IsPixelUsed(i))
-        return 0;
       if((*this)[i].FitQ())
         nsuccess++;
@@ -247,12 +218,8 @@
   UShort_t nsuccess = 0;
 
-  Int_t id = 0;
-
   TIter Next(fPixels);
   MCalibrationPix *pix;
   while ((pix=(MCalibrationPix*)Next()))
     {
-      if (!IsPixelUsed(id++))
-        continue;
       if (pix->FitQ())
         nsuccess++;
@@ -293,13 +260,9 @@
     {
 
-      Int_t id = 0;
-      
       TIter Next(fPixels);
       MCalibrationPix *pix;
       while ((pix=(MCalibrationPix*)Next()))
         {
-          if (!IsPixelUsed(id++))
-            continue;
-          if (pix->FitT())
+         if (pix->FitT())
             nsuccess++;
         }
@@ -331,12 +294,8 @@
   UShort_t nsuccess = 0;
 
-  Int_t id = 0;
-
   TIter Next(fPixels);
   MCalibrationPix *pix;
   while ((pix=(MCalibrationPix*)Next()))
     {
-      if (!IsPixelUsed(id++))
-        continue;
       if (pix->FitT())
         nsuccess++;
@@ -391,15 +350,6 @@
   // we want to keep all pixel not used with a NULL pointer.
   //
-  fPixels->Expand(size);
-  //
-  // Set all entries to the null pointer.  
-  // Later we fill the array per pixId with the contruction: new (fPixels[i]) MCalibrationPix(pixid)
-  // To check, if pixels is already filled, we test the NULL pointer (see IsPixelUsed)
-  //
-  for (Int_t i=0; i< size; i++)
-    {
-      MCalibrationPix *pix = &(*this)[i];
-      pix = NULL;
-    }
+  fPixels->ExpandCreate(size);
+
 }
   
@@ -413,8 +363,6 @@
     while ((pix=(MCalibrationPix*)Next()))
     {
-        if (!IsPixelUsed(id))
-          continue;
-
-        *fLog << id << ": ";
+
+        *fLog << id << ": " << pix->GetPed() << " " << pix->GetPedRms() << " Charges: " ;
         *fLog << pix->GetQ() << " " << pix->GetRQ() << endl;
 
@@ -426,7 +374,4 @@
 Bool_t MCalibrationCam::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
 {
-
-  if (!IsPixelFitted(idx))
-    return kFALSE;
 
   switch (type)
@@ -468,4 +413,10 @@
       val = (*this)[idx].GetErrRQ();
       break;
+    case 12:
+      val = (*this)[idx].GetPheFFactorMethod();
+      break;
+    case 13:
+      val = (*this)[idx].GetConversionFFactorMethod();
+      break;
     default:
       return kFALSE;
@@ -474,7 +425,9 @@
 }
 
-void MCalibrationCam::DrawPixelContent(Int_t num) const
-{
-    *fLog << warn << "MCalibrationCam::DrawPixelContent - not available." << endl;
+void MCalibrationCam::DrawPixelContent(Int_t idx) const
+{
+
+  (*this)[idx].Draw();
+
 }
 
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationCam.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationCam.h	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationCam.h	(revision 2599)
@@ -77,6 +77,4 @@
   Bool_t CheckBounds(Int_t i) const;
 
-  Bool_t AddPixel(Int_t idx);
-
   void Print(Option_t *o="") const;
   
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationPix.cc	(revision 2599)
@@ -40,11 +40,25 @@
 using namespace std;
 
-static const TString gsDefHTitle = "Calibration Histograms Pixel ";
 // --------------------------------------------------------------------------
 //
 // Default Constructor. 
 //
-MCalibrationPix::MCalibrationPix(Int_t pix, const char *name, const char *title)
-    : fPixId(pix)
+MCalibrationPix::MCalibrationPix(const char *name, const char *title)
+    : fPixId(-1),
+      fQ(-1.),
+      fErrQ(-1.),
+      fSigmaQ(-1.),
+      fErrSigmaQ(-1.),
+      fQProb(-1.),
+      fPed(-1.),
+      fPedRms(-1.),
+      fT(-1.),
+      fSigmaT(-1.),
+      fTProb(-1.),
+      fRQ(-1.),
+      fErrRQ(-1.),
+      fFactor(1.3),
+      fPheFFactorMethod(-1.),
+      fConversionFFactorMethod(-1.)
 {
 
@@ -52,13 +66,5 @@
   fTitle = title ? title : "Container of the MHCalibrationPixels and the fit results";
 
-  fHist = new MHCalibrationPixel(fPixId,"MHCalibrationPixel",gsDefHTitle.Data()+fPixId);
-
-  fQ   = fErrQ     = 0.;
-  fPed = fPedRms   = 0.;
-  fT   = fSigmaT  = 0.;
-  fRQ  = fErrRQ = 0.;
-  fSigmaQ = fErrSigmaQ = 0.;
-  fQProb  = 0.;
-
+  fHist = new MHCalibrationPixel("MHCalibrationPixel","Calibration Histograms Pixel");
 }
 
@@ -67,4 +73,14 @@
   delete fHist;
 }
+
+
+void MCalibrationPix::ChangePixId(Int_t i)
+{
+  
+  fPixId = i;
+  fHist->ChangeHistId(i);
+  
+}
+
 
 // ------------------------------------------------------------------------
@@ -84,5 +100,5 @@
 
   if (fPed && fPedRms)
-    fHist->SetLowerFitRange(fPed + 3.5*fPedRms);
+    fHist->SetLowerFitRange(fPed + 2.0*fPedRms);
   else
     *fLog << warn << "Cannot set lower fit range to suppress cosmics: Pedestals not available" << endl;
@@ -101,9 +117,18 @@
   fQProb     = fHist->GetQProb();
 
-  if (fPed)
+  if ((fPed > 0.)  && (fPedRms > 0.))
+    {
+      
     fRQ      = fQ - fPed;
-  if (fPedRms)
     fErrRQ   = TMath::Sqrt(fErrQ*fErrQ + fPedRms*fPedRms);
-  
+
+    fPheFFactorMethod = 
+      fFactor 
+      * fRQ * fRQ 
+      / (fSigmaQ * fSigmaQ - fPedRms*fPedRms) ;
+
+    fConversionFFactorMethod = fPheFFactorMethod / fRQ ;
+    
+    }
 
   return kTRUE;
@@ -142,6 +167,2 @@
 }
 
-void MCalibrationPix::Test()
-{
-  *fLog << "TEST: pixid: " << fPixId << endl;  
-}
Index: trunk/MagicSoft/Mars/manalysis/MCalibrationPix.h
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MCalibrationPix.h	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MCalibrationPix.h	(revision 2599)
@@ -12,7 +12,4 @@
 private:
 
-  //
-  // FIXME: Derive class from MCerphotPix ??
-  //
   Int_t   fPixId;           // the pixel Id
   
@@ -32,10 +29,14 @@
   Float_t fRQ;               // The reduced mean charge after the fit
   Float_t fErrRQ;            // The error of the reduced mean charge after the fit  
-  
+
+  Float_t fFactor;            // The F-factor
+  Float_t fPheFFactorMethod;  // The number of Phe's calculated after the F-factor method
+  Float_t fConversionFFactorMethod; // The conversion factor to Phe's calculated after the F-factor method
+    
   MHCalibrationPixel *fHist; // Pointer to the histograms performing the fits, etc.  
   
 public:
 
-  MCalibrationPix(Int_t pix=-1, const char *name=NULL, const char *title=NULL);
+  MCalibrationPix(const char *name=NULL, const char *title=NULL);
   ~MCalibrationPix();
   
@@ -66,5 +67,6 @@
   Bool_t IsValid()      const           { return fRQ >=0 || fErrRQ >= 0; }
   Int_t  GetPixId()     const           { return fPixId;   }
-
+  void ChangePixId(Int_t i);
+  
   Bool_t FitQ();
   Bool_t FitT();
@@ -73,6 +75,7 @@
   virtual void Draw(Option_t *opt="")     { fHist->Draw(opt); }
   
-  void Test();
-    
+  Float_t GetPheFFactorMethod() const        { return fPheFFactorMethod;  }  
+  Float_t GetConversionFFactorMethod() const { return fConversionFFactorMethod;  }
+  
   ClassDef(MCalibrationPix, 1)	// Storage Container for Calibration information of one pixel
 };
Index: trunk/MagicSoft/Mars/manalysis/MPedCalcPedRun.cc
===================================================================
--- trunk/MagicSoft/Mars/manalysis/MPedCalcPedRun.cc	(revision 2598)
+++ trunk/MagicSoft/Mars/manalysis/MPedCalcPedRun.cc	(revision 2599)
@@ -60,7 +60,5 @@
 
     AddToBranchList("fHiGainPixId");
-    //AddToBranchList("fLoGainPixId");
     AddToBranchList("fHiGainFadcSamples");
-    //AddToBranchList("fLoGainFadcSamples");
 }
 
@@ -98,4 +96,6 @@
     fNumHiGainSamples =  fRunheader->GetNumSamplesHiGain();
 
+    fPedestals->InitSize(fRunheader->GetNumPixel());
+
     return kTRUE;
 
@@ -105,9 +105,10 @@
 Int_t MPedCalcPedRun::Process()
 {
+
     MRawEvtPixelIter pixel(fRawEvt);
 
     while (pixel.Next())
     {
-              Byte_t *ptr = pixel.GetHiGainSamples();
+             Byte_t *ptr = pixel.GetHiGainSamples();
         const Byte_t *end = ptr + fRawEvt->GetNumHiGainSamples();
 
@@ -115,12 +116,9 @@
         const Float_t higainrms = CalcHiGainRms(ptr, end, higainped);
 
-        //const Float_t higainpederr = CalcHiGainMeanErr(higainrms);
-        //const Float_t higainrmserr = CalcHiGainRmsErr(higainrms);
-
         const UInt_t pixid = pixel.GetPixelId();
         MPedestalPix &pix = (*fPedestals)[pixid];
 
         pix.Set(higainped, higainrms);
-       //pix.SetPedestalRms(higainpederr, higainrmserr);
+
     }
 
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationBlindPixel.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationBlindPixel.cc	(revision 2598)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationBlindPixel.cc	(revision 2599)
@@ -69,22 +69,22 @@
 
     // Create a large number of bins, later we will rebin
-    fBPQfirst = 0;
-    fBPQlast  = gkStartBlindPixelBinNr;
-    fBPQnbins = gkStartBlindPixelBinNr;
-
-    fHBPQ = new TH1I("HBPQ","Distribution of Summed FADC Slices",fBPQnbins,fBPQfirst,fBPQlast);
-    fHBPQ->SetXTitle("Sum FADC Slices");
-    fHBPQ->SetYTitle("Nr. of events");
-    fHBPQ->Sumw2();
-
-    fErrBPQfirst = 0.;
-    fErrBPQlast  = gkStartBlindPixelBinNr;
-    fErrBPQnbins = gkStartBlindPixelBinNr;
-
-    fHBPErrQ = new TH1F("HBPErrQ","Distribution of Variances of Summed FADC Slices",
-                        fErrBPQnbins,fErrBPQfirst,fErrBPQlast);
-    fHBPErrQ->SetXTitle("Variance Summed FADC Slices");
-    fHBPErrQ->SetYTitle("Nr. of events");
-    fHBPErrQ->Sumw2();
+    fBlindPixelQfirst = 0;
+    fBlindPixelQlast  = gkStartBlindPixelBinNr;
+    fBlindPixelQnbins = gkStartBlindPixelBinNr;
+
+    fHBlindPixelQ = new TH1I("HBlindPixelQ","Distribution of Summed FADC Slices",fBlindPixelQnbins,fBlindPixelQfirst,fBlindPixelQlast);
+    fHBlindPixelQ->SetXTitle("Sum FADC Slices");
+    fHBlindPixelQ->SetYTitle("Nr. of events");
+    fHBlindPixelQ->Sumw2();
+
+    fErrBlindPixelQfirst = 0.;
+    fErrBlindPixelQlast  = gkStartBlindPixelBinNr;
+    fErrBlindPixelQnbins = gkStartBlindPixelBinNr;
+
+    fHBlindPixelErrQ = new TH1F("HBlindPixelErrQ","Distribution of Variances of Summed FADC Slices",
+                        fErrBlindPixelQnbins,fErrBlindPixelQfirst,fErrBlindPixelQlast);
+    fHBlindPixelErrQ->SetXTitle("Variance Summed FADC Slices");
+    fHBlindPixelErrQ->SetYTitle("Nr. of events");
+    fHBlindPixelErrQ->Sumw2();
 
     Axis_t tfirst = -0.5;
@@ -92,8 +92,8 @@
     Int_t nbins   = 16;
 
-    fHBPT = new TH1I("HBPT","Distribution of Mean Arrival Times",nbins,tfirst,tlast);
-    fHBPT->SetXTitle("Mean Arrival Times [FADC slice nr]");
-    fHBPT->SetYTitle("Nr. of events");
-    fHBPT->Sumw2();
+    fHBlindPixelT = new TH1I("HBlindPixelT","Distribution of Mean Arrival Times",nbins,tfirst,tlast);
+    fHBlindPixelT->SetXTitle("Mean Arrival Times [FADC slice nr]");
+    fHBlindPixelT->SetYTitle("Nr. of events");
+    fHBlindPixelT->Sumw2();
 
     // We define a reasonable number and later enlarge it if necessary
@@ -102,7 +102,7 @@
     Axis_t nlast  = (Axis_t)nbins - 0.5;
 
-    fHBPQvsN = new TH1I("HBPQvsN","Sum of Charges vs. Event Number",nbins,nfirst,nlast);
-    fHBPQvsN->SetXTitle("Event Nr.");
-    fHBPQvsN->SetYTitle("Sum of FADC slices");
+    fHBlindPixelQvsN = new TH1I("HBlindPixelQvsN","Sum of Charges vs. Event Number",nbins,nfirst,nlast);
+    fHBlindPixelQvsN->SetXTitle("Event Nr.");
+    fHBlindPixelQvsN->SetYTitle("Sum of FADC slices");
 
     fgSinglePheFitFunc = &gfKto8;
@@ -113,7 +113,7 @@
 {
 
-  delete fHBPQ;
-  delete fHBPT;
-  delete fHBPErrQ;
+  delete fHBlindPixelQ;
+  delete fHBlindPixelT;
+  delete fHBlindPixelErrQ;
   
   if (fSinglePheFit)
@@ -127,7 +127,7 @@
 void MHCalibrationBlindPixel::ResetBin(Int_t i)
 {
-    fHBPQ->SetBinContent (i, 1.e-20);
-    fHBPErrQ->SetBinContent  (i, 1.e-20);
-    fHBPT->SetBinContent(i, 1.e-20);
+    fHBlindPixelQ->SetBinContent (i, 1.e-20);
+    fHBlindPixelErrQ->SetBinContent  (i, 1.e-20);
+    fHBlindPixelT->SetBinContent(i, 1.e-20);
 }
 
@@ -209,5 +209,5 @@
     gPad->SetTicks();
 
-    fHBPQ->DrawCopy(opt);
+    fHBlindPixelQ->DrawCopy(opt);
     
     if (fSinglePheFit)
@@ -231,9 +231,9 @@
     gPad->SetLogy(1);
     gPad->SetBorderMode(0);
-    fHBPT->DrawCopy(opt);
-
-    if (fHBPT->GetFunction("GausTime"))
+    fHBlindPixelT->DrawCopy(opt);
+
+    if (fHBlindPixelT->GetFunction("GausTime"))
       {
-        TF1 *tfit = fHBPT->GetFunction("GausTime");
+        TF1 *tfit = fHBlindPixelT->GetFunction("GausTime");
         if (tfit->GetProb() < 0.01)
           tfit->SetLineColor(kRed);
@@ -248,5 +248,5 @@
     c->cd(4);
 
-    fHBPQvsN->DrawCopy(opt);
+    fHBlindPixelQvsN->DrawCopy(opt);
 
     c->Modified();
@@ -260,7 +260,7 @@
   gRandom->SetSeed();
 
-  if (fHBPQ->GetEntries() != 0)
+  if (fHBlindPixelQ->GetEntries() != 0)
     {
-      *fLog << err << "Histogram " << fHBPQ->GetTitle() << " is already filled. " << endl;
+      *fLog << err << "Histogram " << fHBlindPixelQ->GetTitle() << " is already filled. " << endl;
       *fLog << err << "Create new class MHCalibrationBlindPixel for simulation! " << endl;
       return kFALSE;
@@ -268,13 +268,13 @@
   
   TF1 *simulateSinglePhe = new TF1("simulateSinglePhe",fgSinglePheFitFunc,
-                                   fBPQfirst,fBPQlast,fgSinglePheFitNPar);
+                                   fBlindPixelQfirst,fBlindPixelQlast,fgSinglePheFitNPar);
   
   simulateSinglePhe->SetParameters(lambda,mu0,mu1,sigma0,sigma1);
   simulateSinglePhe->SetParNames("#lambda","#mu_0","#mu_1","#sigma_0","#sigma_1");
-  simulateSinglePhe->SetNpx(fBPQnbins);  
+  simulateSinglePhe->SetNpx(fBlindPixelQnbins);  
 
   for (Int_t i=0;i<10000; i++) 
     {
-      fHBPQ->Fill(simulateSinglePhe->GetRandom());
+      fHBlindPixelQ->Fill(simulateSinglePhe->GetRandom());
     }
   
@@ -283,5 +283,5 @@
 
 
-void MHCalibrationBlindPixel::ChangeFitFunc(BPFitFunc fitfunc, Int_t par)
+void MHCalibrationBlindPixel::ChangeFitFunc(BlindPixelFitFunc fitfunc, Int_t par)
 {
   
@@ -303,6 +303,6 @@
   // Get the fitting ranges
   //
-  rmin = (rmin != 0.) ? rmin : fBPQfirst;
-  rmax = (rmax != 0.) ? rmax : fBPQlast;
+  rmin = (rmin != 0.) ? rmin : fBlindPixelQfirst;
+  rmax = (rmax != 0.) ? rmax : fBlindPixelQlast;
 
   //
@@ -310,9 +310,9 @@
   // otherwise the fit goes gaga because of high number of dimensions ...
   //
-  const Stat_t   entries      = fHBPQ->GetSumOfWeights();
+  const Stat_t   entries      = fHBlindPixelQ->GetSumOfWeights();
   const Double_t lambda_guess = 0.2;
-  const Double_t mu_0_guess = fHBPQ->GetBinCenter(fHBPQ->GetMaximumBin());
-  const Double_t si_0_guess = mu_0_guess/500.;
-  const Double_t mu_1_guess = mu_0_guess + 2.;
+  const Double_t mu_0_guess = fHBlindPixelQ->GetBinCenter(fHBlindPixelQ->GetMaximumBin());
+  const Double_t si_0_guess = mu_0_guess/10.;
+  const Double_t mu_1_guess = mu_0_guess + 50.;
   const Double_t si_1_guess = si_0_guess + si_0_guess;
 
@@ -325,5 +325,5 @@
   fSinglePheFit->SetParLimits(3,1.0,rmax-rmin);
   fSinglePheFit->SetParLimits(4,1.7,rmax-rmin);
-  fSinglePheFit->SetParLimits(5,0.,2.*entries);
+  fSinglePheFit->SetParLimits(5,0.,2.5*entries);
   //
   // Normalize the histogram to facilitate faster fitting of the area
@@ -333,5 +333,5 @@
   // ROOT gives us another nice example of user-unfriendly behavior:
   // Although the normalization of the function fSinglePhe and the 
-  // Histogram fHBPQ agree (!!), the fit does not normalize correctly INTERNALLY
+  // Histogram fHBlindPixelQ agree (!!), the fit does not normalize correctly INTERNALLY
   // in the fitting procedure !!!
   // 
@@ -342,7 +342,7 @@
   // So, WE have to adapt to that internal flaw of ROOT:
   //
-  const Int_t  npx     = fSinglePheFit->GetNpx();
-  const Int_t  bins    = fHBPQ->GetXaxis()->GetLast()-fHBPQ->GetXaxis()->GetFirst();
-  //  fHBPQ->Scale(gkSq2Pi*(float)bins/npx/entries);
+  //  const Int_t  npx     = fSinglePheFit->GetNpx();
+  //  const Int_t  bins    = fHBlindPixelQ->GetXaxis()->GetLast()-fHBlindPixelQ->GetXaxis()->GetFirst();
+  //  fHBlindPixelQ->Scale(gkSq2Pi*(float)bins/npx/entries);
 
   // 
@@ -353,5 +353,5 @@
   //  fSinglePheFit->SetNpx(fQnbins);  
 
-  fHBPQ->Fit("SinglePheFit",opt);
+  fHBlindPixelQ->Fit(fSinglePheFit,opt);
 
   fLambda = fSinglePheFit->GetParameter(0);
@@ -403,18 +403,18 @@
   // If you find another solution which WORKS!!, please tell me!!
   //
-  Int_t nbins = 100;
-
-  *fLog << "New number of bins in HSinQ: " << CutEdges(fHBPQ,nbins) << endl;
-
-  fBPQfirst = fHBPQ->GetBinLowEdge(fHBPQ->GetXaxis()->GetFirst());
-  fBPQlast  = fHBPQ->GetBinLowEdge(fHBPQ->GetXaxis()->GetLast())+fHBPQ->GetBinWidth(0);
-  fBPQnbins = nbins;
-
-  *fLog << "New number of bins in HErrQ: " << CutEdges(fHBPErrQ,30) << endl;
-  fErrBPQfirst = fHBPErrQ->GetBinLowEdge(fHBPErrQ->GetXaxis()->GetFirst());
-  fErrBPQlast  = fHBPErrQ->GetBinLowEdge(fHBPErrQ->GetXaxis()->GetLast())+fHBPErrQ->GetBinWidth(0);
-  fErrBPQnbins = nbins;
-
-  CutEdges(fHBPQvsN,0);
+  Int_t nbins = 50;
+
+  *fLog << "New number of bins in HSinQ: " << CutEdges(fHBlindPixelQ,nbins) << endl;
+
+  fBlindPixelQfirst = fHBlindPixelQ->GetBinLowEdge(fHBlindPixelQ->GetXaxis()->GetFirst());
+  fBlindPixelQlast  = fHBlindPixelQ->GetBinLowEdge(fHBlindPixelQ->GetXaxis()->GetLast())+fHBlindPixelQ->GetBinWidth(0);
+  fBlindPixelQnbins = nbins;
+
+  *fLog << "New number of bins in HErrQ: " << CutEdges(fHBlindPixelErrQ,30) << endl;
+  fErrBlindPixelQfirst = fHBlindPixelErrQ->GetBinLowEdge(fHBlindPixelErrQ->GetXaxis()->GetFirst());
+  fErrBlindPixelQlast  = fHBlindPixelErrQ->GetBinLowEdge(fHBlindPixelErrQ->GetXaxis()->GetLast())+fHBlindPixelErrQ->GetBinWidth(0);
+  fErrBlindPixelQnbins = nbins;
+
+  CutEdges(fHBlindPixelQvsN,0);
 
 }
@@ -426,9 +426,9 @@
     return kFALSE;
 
-  rmin = (rmin != 0.) ? rmin : 0.;
-  rmax = (rmax != 0.) ? rmax : 16.;
-
-  const Stat_t   entries     = fHBPT->GetEntries();
-  const Double_t mu_guess    = fHBPT->GetBinCenter(fHBPT->GetMaximumBin());
+  rmin = (rmin != 0.) ? rmin : 4.;
+  rmax = (rmax != 0.) ? rmax : 9.;
+
+  const Stat_t   entries     = fHBlindPixelT->GetEntries();
+  const Double_t mu_guess    = fHBlindPixelT->GetBinCenter(fHBlindPixelT->GetMaximumBin());
   const Double_t sigma_guess = (rmax - rmin)/2.;
   const Double_t area_guess  = entries/gkSq2Pi;
@@ -441,5 +441,12 @@
   fTimeGausFit->SetParLimits(2,0.,rmax-rmin);
 
-  fHBPT->Fit("GausTime",opt);
+  fHBlindPixelT->Fit(fTimeGausFit,opt);
+
+  rmin = fTimeGausFit->GetParameter(1) - 2.*fTimeGausFit->GetParameter(2);
+  rmax = fTimeGausFit->GetParameter(1) + 2.*fTimeGausFit->GetParameter(2);
+  fTimeGausFit->SetRange(rmin,rmax);  
+
+  fHBlindPixelT->Fit(fTimeGausFit,opt);
+
 
   fMeanT     = fTimeGausFit->GetParameter(2);
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationBlindPixel.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationBlindPixel.h	(revision 2598)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationBlindPixel.h	(revision 2599)
@@ -32,19 +32,19 @@
 private:
 
-  TH1I* fHBPQ;        //-> Histogram with the single Phe spectrum
-  TH1F* fHBPErrQ;     //-> Variance of summed FADC slices
-  TH1I* fHBPT;        //-> Variance of summed FADC slices
-  TH1I* fHBPQvsN;     //-> Summed Charge vs. Event Nr. 
+  TH1I* fHBlindPixelQ;        //-> Histogram with the single Phe spectrum
+  TH1F* fHBlindPixelErrQ;     //-> Variance of summed FADC slices
+  TH1I* fHBlindPixelT;        //-> Variance of summed FADC slices
+  TH1I* fHBlindPixelQvsN;     //-> Summed Charge vs. Event Nr. 
   
   TF1 *fSinglePheFit;
   TF1 *fTimeGausFit;  
 
-  Axis_t  fBPQfirst;
-  Axis_t  fBPQlast;
-  Int_t   fBPQnbins;
+  Axis_t  fBlindPixelQfirst;
+  Axis_t  fBlindPixelQlast;
+  Int_t   fBlindPixelQnbins;
   
-  Axis_t fErrBPQfirst;
-  Axis_t fErrBPQlast;
-  Int_t  fErrBPQnbins;
+  Axis_t fErrBlindPixelQfirst;
+  Axis_t fErrBlindPixelQlast;
+  Int_t  fErrBlindPixelQnbins;
 
   void ResetBin(Int_t i);
@@ -54,5 +54,5 @@
   Bool_t fFitOK;  
   
-  BPFitFunc fgSinglePheFitFunc;     // In the beginning, 
+  BlindPixelFitFunc fgSinglePheFitFunc;     // In the beginning, 
   Int_t     fgSinglePheFitNPar;     // we want to be flexible using different functions
 
@@ -83,8 +83,8 @@
   ~MHCalibrationBlindPixel();
 
-  Bool_t FillBPQ(Int_t q)         { return fHBPQ->Fill(q) > -1;  }  
-  Bool_t FillErrBPQ(Float_t errq) { return fHBPErrQ->Fill(errq) > -1; }
-  Bool_t FillBPT(Int_t t)         { return fHBPT->Fill(t) > -1;  }
-  Bool_t FillBPQvsN(Stat_t rq, Int_t t) { return fHBPQvsN->Fill(t,rq) > -1;  }  
+  Bool_t FillBlindPixelQ(Int_t q)         { return fHBlindPixelQ->Fill(q) > -1;  }  
+  Bool_t FillErrBlindPixelQ(Float_t errq) { return fHBlindPixelErrQ->Fill(errq) > -1; }
+  Bool_t FillBlindPixelT(Int_t t)         { return fHBlindPixelT->Fill(t) > -1;  }
+  Bool_t FillBlindPixelQvsN(Stat_t rq, Int_t t) { return fHBlindPixelQvsN->Fill(t,rq) > -1;  }  
   
   const Double_t GetLambda()   const { return fLambda; }
@@ -109,6 +109,6 @@
   const Double_t GetSigmaTErr()    const { return fSigmaTErr; }
 
-  const TH1F *GetHErrQ() { return fHBPErrQ; }
-  const TH1F *GetHErrQ() const { return fHBPErrQ; }
+  const TH1F *GetHErrQ() { return fHBlindPixelErrQ; }
+  const TH1F *GetHErrQ() const { return fHBlindPixelErrQ; }
   
   Bool_t SimulateSinglePhe(Double_t lambda,
@@ -121,5 +121,5 @@
   Bool_t FitT(Axis_t rmin=0., Axis_t rmax=0.,Option_t *opt="R0+");
 
-  void ChangeFitFunc(BPFitFunc fitfunc, Int_t par=5);
+  void ChangeFitFunc(BlindPixelFitFunc fitfunc, Int_t par=5);
 
 
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationConfig.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationConfig.h	(revision 2598)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationConfig.h	(revision 2599)
@@ -12,5 +12,5 @@
 
 // Global rejection criteria for the acceptance of a fit: Prob=0.01 == 99% Probability
-const Float_t gkProbLimit = 0.01;
+const Float_t gkProbLimit = 0.001;
 
 // Starting number of bins for the histo:
@@ -30,5 +30,5 @@
 
 // typedef to the fitting functions for the blind pixel
-typedef Double_t (*BPFitFunc)(Double_t *, Double_t *);
+typedef Double_t (*BlindPixelFitFunc)(Double_t *, Double_t *);
 
 #endif /* MARS_MHCalibrationBlindPixelConfig */
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.cc
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.cc	(revision 2598)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.cc	(revision 2599)
@@ -60,11 +60,17 @@
 // Default Constructor. 
 //
-MHCalibrationPixel::MHCalibrationPixel(Int_t pix, const char *name, const char *title)
-      : fPixId(pix),
+MHCalibrationPixel::MHCalibrationPixel(const char *name, const char *title)
+      : fPixId(-1),
         fQGausFit(NULL), 
 	fTGausFit(NULL), 
 	fFitLegend(NULL),
 	fLowerFitRange(0.),
-        fFitOK(kFALSE)
+        fFitOK(kFALSE),
+        fQChisquare(-1.),
+        fQProb(-1.),
+        fQNdf(-1),
+        fTChisquare(-1.),
+        fTProb(-1.),
+        fTNdf(-1)
 { 
 
@@ -72,8 +78,5 @@
     fTitle = title ? title : "Fill the accumulated charges and times of all events and perform fits";
 
-    TString qname  = "HQ";
     TString qtitle = "Distribution of Summed FADC Slices Pixel ";
-    qname  += pix;
-    qtitle += pix;
 
     // Create a large number of bins, later we will rebin
@@ -82,5 +85,5 @@
     fQnbins = gkStartPixelBinNr;
 
-    fHQ = new TH1I( qname.Data(),qtitle.Data(),
+    fHQ = new TH1I("HQ",qtitle.Data(),
                    fQnbins,fQfirst,fQlast);
     fHQ->SetXTitle("Sum FADC Slices");
@@ -88,8 +91,7 @@
     fHQ->Sumw2();
 
-    TString tname  = "HT";
+    fHQ->SetDirectory(NULL);
+
     TString ttitle = "Distribution of Mean Arrival Times Pixel ";
-    tname  += pix;
-    ttitle += pix;
 
     Axis_t tfirst = -0.5;
@@ -97,5 +99,5 @@
     Int_t nbins   = 16;
 
-    fHT = new TH1I(tname.Data(),ttitle.Data(),
+    fHT = new TH1I("HT",ttitle.Data(),
                   nbins,tfirst,tlast);
     fHT->SetXTitle("Mean Arrival Times [FADC slice nr]");
@@ -103,8 +105,7 @@
     fHT->Sumw2();
 
-    TString qvsnname  = "HQvsN";
+    fHT->SetDirectory(NULL);
+
     TString qvsntitle = "Sum of Charges vs. Event Number Pixel ";
-    qvsnname  += pix;
-    qvsntitle += pix;
 
     // We define a reasonable number and later enlarge it if necessary
@@ -113,16 +114,10 @@
     Axis_t nlast  = (Axis_t)nbins - 0.5;
 
-    fHQvsN = new TH1I(qvsnname.Data(),qvsntitle.Data(),
+    fHQvsN = new TH1I("HQvsN",qvsntitle.Data(),
                      nbins,nfirst,nlast);
     fHQvsN->SetXTitle("Event Nr.");
     fHQvsN->SetYTitle("Sum of FADC slices");
 
-    fQChisquare = -1.;
-    fQProb      = -1.;
-    fQNdf       = -1;
-    
-    fTChisquare = -1.;
-    fTProb      = -1.;
-    fTNdf       = -1;
+    fHQvsN->SetDirectory(NULL);
 
 }
@@ -144,4 +139,24 @@
 }
 
+
+void MHCalibrationPixel::ChangeHistId(Int_t id)
+{
+
+  fPixId = id;
+  
+  TString nameQ = TString(fHQ->GetName());
+  nameQ += id;
+  fHQ->SetName(nameQ.Data());
+
+  TString nameT = TString(fHT->GetName());
+  nameT += id;
+  fHT->SetName(nameT.Data());
+
+  TString nameQvsN  = TString(fHQvsN->GetName());
+  nameQvsN += id;
+  fHQvsN->SetName(nameQvsN.Data());
+}
+
+
 void MHCalibrationPixel::Reset()
 {
@@ -206,5 +221,5 @@
 
   char line8[32];
-  sprintf(line8,"Probability: %4.2f ",fQProb);
+  sprintf(line8,"Probability: %4.3f ",fQProb);
   fFitLegend->AddText(line8);
 
@@ -266,13 +281,12 @@
     fHT->DrawCopy(opt);
 
-    if (fHT->GetFunction("GausTime"))
+    if (fTGausFit)
       {
-        TF1 *tfit = fHT->GetFunction("GausTime");
-        if (tfit->GetProb() < 0.01)
-          tfit->SetLineColor(kRed);
+        if (fTChisquare > 1.)
+          fTGausFit->SetLineColor(kRed);
         else
-          tfit->SetLineColor(kGreen);
-
-        tfit->DrawCopy("same");
+          fTGausFit->SetLineColor(kGreen);
+
+        fTGausFit->DrawCopy("same");
         c->Modified();
         c->Update();
@@ -290,10 +304,10 @@
 Bool_t MHCalibrationPixel::FitT(Axis_t rmin, Axis_t rmax, Option_t *option)
 {
-  
+
   if (fTGausFit)
     return kFALSE;
 
-  rmin = (rmin != 0.) ? rmin : -0.5;
-  rmax = (rmax != 0.) ? rmax : 15.5;
+  rmin = (rmin != 0.) ? rmin : 4.;
+  rmax = (rmax != 0.) ? rmax : 9.;
 
   const Stat_t   entries     = fHT->GetEntries();
@@ -302,5 +316,7 @@
   const Double_t area_guess  = entries/gkSq2Pi;
 
-  fTGausFit = new TF1("GausTime","gaus",rmin,rmax);  
+  TString name = TString("GausTime");
+  name += fPixId;
+  fTGausFit = new TF1(name.Data(),"gaus",rmin,rmax);  
 
   if (!fTGausFit) 
@@ -314,7 +330,14 @@
   fTGausFit->SetParLimits(0,0.,entries);
   fTGausFit->SetParLimits(1,rmin,rmax);
-  fTGausFit->SetParLimits(2,0.,rmax-rmin);
-
-  fHT->Fit("GausTime",option);
+  fTGausFit->SetParLimits(2,0.,(rmax-rmin)/2.);
+  fTGausFit->SetRange(rmin,rmax);
+
+  fHT->Fit(fTGausFit,option);
+
+  rmin = fTGausFit->GetParameter(1) - 3.*fTGausFit->GetParameter(2);
+  rmax = fTGausFit->GetParameter(1) + 3.*fTGausFit->GetParameter(2);
+  fTGausFit->SetRange(rmin,rmax);  
+
+  fHT->Fit(fTGausFit,option);
 
   fTChisquare = fTGausFit->GetChisquare();
@@ -324,5 +347,5 @@
   fTSigma     = fTGausFit->GetParameter(2);
 
-  if (fTProb < gkProbLimit) 
+  if (fTChisquare > 1.) 
     {
       *fLog << warn << "Fit of the Arrival times failed ! " << endl;
@@ -344,4 +367,5 @@
   //
   Axis_t rmin = (fLowerFitRange != 0.) ? fLowerFitRange : fQfirst;
+  Axis_t rmax = 0.;
 
   //
@@ -352,7 +376,10 @@
   const Double_t ar_guess = entries/gkSq2Pi;
   const Double_t mu_guess = fHQ->GetBinCenter(fHQ->GetMaximumBin());
-  const Double_t si_guess = mu_guess/500.;
-
-  fQGausFit = new TF1("QGausFit","gaus",rmin,fQlast);
+  const Double_t si_guess = mu_guess/50.;
+
+  TString name = TString("QGausFit");
+  name += fPixId;
+
+  fQGausFit = new TF1(name.Data(),"gaus",rmin,fQlast);
 
   if (!fQGausFit) 
@@ -367,6 +394,20 @@
   fQGausFit->SetParLimits(1,rmin,fQlast);
   fQGausFit->SetParLimits(2,0.,fQlast-rmin);
-
-  fHQ->Fit("QGausFit",option);
+  fQGausFit->SetRange(rmin,fQlast);
+  fQGausFit->Update();
+
+  fHQ->Fit(fQGausFit,option);
+  
+  rmin = fQGausFit->GetParameter(1) - 3.*fQGausFit->GetParameter(2);
+  rmax = fQGausFit->GetParameter(1) + 3.*fQGausFit->GetParameter(2);
+
+  fQGausFit->SetRange(rmin,rmax);  
+  fHQ->Fit(fQGausFit,option);
+
+  rmin = fQGausFit->GetParameter(1) - 3.5*fQGausFit->GetParameter(2);
+  rmax = fQGausFit->GetParameter(1) + 3.5*fQGausFit->GetParameter(2);
+
+  fQGausFit->SetRange(rmin,rmax);  
+  fHQ->Fit(fQGausFit,option);
 
   fQChisquare = fQGausFit->GetChisquare();
@@ -399,10 +440,5 @@
 {
 
-  //
-  // The number 100 is necessary because it is the internal binning
-  // of ROOT functions. A call to SetNpx() does NOT help
-  // If you find another solution which WORKS!!, please tell me!!
-  //
-  Int_t nbins = 100;
+  Int_t nbins = 50;
 
   CutEdges(fHQ,nbins);
Index: trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.h
===================================================================
--- trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.h	(revision 2598)
+++ trunk/MagicSoft/Mars/mhist/MHCalibrationPixel.h	(revision 2599)
@@ -68,6 +68,8 @@
 public:
 
-  MHCalibrationPixel(Int_t pix=-1, const char *name=NULL, const char *title=NULL);
+  MHCalibrationPixel(const char *name=NULL, const char *title=NULL);
   ~MHCalibrationPixel();
+
+  void ChangeHistId(Int_t i);
   
   Bool_t SetupFill(const MParList *pList);
@@ -105,6 +107,6 @@
   const TH1I *GetHQvsN() const { return fHQvsN; }
   
-  Bool_t FitQ(Option_t *option="RQ0+");  
-  Bool_t FitT(Axis_t rmin=0, Axis_t rmax=0, Option_t *option="RQ0+");    
+  Bool_t FitQ(Option_t *option="RQ0");  
+  Bool_t FitT(Axis_t rmin=0, Axis_t rmax=0, Option_t *option="RQ0");    
 
   virtual void Draw(Option_t *option="");
