/* ======================================================================== *\
!
! *
! * This file is part of MARS, the MAGIC Analysis and Reconstruction
! * Software. It is distributed to you in the hope that it can be a useful
! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
! * It is distributed WITHOUT ANY WARRANTY.
! *
! * Permission to use, copy, modify and distribute this software and its
! * documentation for any purpose is hereby granted without fee,
! * provided that the above copyright notice appear in all copies and
! * that both that copyright notice and this permission notice appear
! * in supporting documentation. It is provided "as is" without express
! * or implied warranty.
! *
!
!
!   Author(s): Markus Gaug   11/2003 <mailto:markus@ifae.es>
!
!   Copyright: MAGIC Software Development, 2000-2004
!
!
\* ======================================================================== */
/////////////////////////////////////////////////////////////////////////////
//                                                               
// MCalibrationChargeCam                                               
//                                                               
// Storage container for charge calibration results of the whole camera.
//
// Individual pixels have to be cast when retrieved e.g.:
// MCalibrationChargePix &avpix = (MCalibrationChargePix&)(*fChargeCam)[i]
// 
// The following calibration constants can be retrieved from each pixel:
//
// - GetConversionFactorFFactor    ( Int_t idx, Float_t &mean, Float_t &err, Float_t &sigma );
// - GetConversionFactorBlindPixel ( Int_t idx, Float_t &mean, Float_t &err, Float_t &sigma );
// - GetConversionFactorPINDiode   ( Int_t idx, Float_t &mean, Float_t &err, Float_t &sigma );
// - GetConversionFactorCombined   ( Int_t idx, Float_t &mean, Float_t &err, Float_t &sigma );
//
// where: 
// - idx is the pixel software ID
// - "mean" is the mean conversion constant, to be multiplied with the retrieved signal 
//   in order to get a calibrated number of PHOTONS. 
// - "err" is the pure statistical uncertainty about the MEAN
// - "sigma", if mulitplied with the square root of signal, gives the approximate sigma of the 
//   retrieved mean number of incident Cherenkov photons.
//
// Note, Conversion is ALWAYS (included the F-Factor method!) from summed FADC slices to PHOTONS.
//
// Averaged values over one whole area index (e.g. inner or outer pixels for 
// the MAGIC camera), can be retrieved via: 
// MCalibrationChargePix &avpix = (MCalibrationChargePix&)fChargeCam->GetAverageArea(i)
//
// Averaged values over one whole camera sector can be retrieved via: 
// MCalibrationChargePix &avpix = (MCalibrationChargePix&)fChargeCam->GetAverageSector(i)
//
// Note the averageing has been done on an event-by-event basis. Resulting 
// Sigma's of the Gauss fit have been multiplied with the square root of the number 
// of involved pixels in order to make a direct comparison possible with the mean of 
// sigmas. 
//
// See also: MCalibrationChargePix, MCalibrationChargeCalc
//           MHCalibrationChargePix, MHCalibrationChargeCam              
//
// The types of GetPixelContent() are as follows:
// 
// Fitted values:
// ============== 
//
// 0: Fitted Charge                              (see MCalibrationPix::GetMean())
// 1: Error of fitted Charge                     (see MCalibrationPix::GetMeanErr())
// 2: Sigma of fitted Charge                     (see MCalibrationPix::GetSigma())
// 3: Error of Sigma of fitted Charge            (see MCalibrationPix::GetSigmaErr())
//
// Useful variables derived from the fit results:
// =============================================
//
// 4: Probability Gauss fit Charge distribution  (see MCalibrationPix::GetProb())
// 5: Reduced Sigma of fitted Charge             (see MCalibrationChargePix::GetRSigma())
// 6: Error Reduced Sigma of fitted Charge       (see MCalibrationChargePix::GetRSigmaErr())
// 7: Reduced Sigma per Charge                 
// 8: Error of Reduced Sigma per Charge 
//
// Results of the different calibration methods:
// =============================================
//
//  9: Nr. Photo-electrons from F-Factor Method   (see MCalibrationChargePix::GetPheFFactorMethod())
// 10: Error Nr. Photo-el. from F-Factor Method   (see MCalibrationChargePix::GetPheFFactorMethodErr())
// 11: Conversion factor   from F-Factor Method   (see MCalibrationChargePix::GetMeanConversionFFactorMethod())
// 12: Error conv. factor  from F-Factor Method   (see MCalibrationChargePix::GetConversionFFactorMethodErr())
// 13: Overall F-Factor    from F-Factor Method   (see MCalibrationChargePix::GetTotalFFactorFFactorMethod())
// 14: Error F-Factor      from F-Factor Method   (see MCalibrationChargePix::GetTotalFFactorFFactorMethodErr())
// 15: Pixels valid calibration F-Factor-Method   (see MCalibrationChargePix::IsFFactorMethodValid())           
// 16: Conversion factor   from BlindPixel Method (see MCalibrationChargePix::GetMeanConversionBlindPixelMethod()) 
// 17: Error conv. factor  from BlindPixel Method (see MCalibrationChargePix::GetConversionBlindPixelMethodErr())  
// 18: Overall F-Factor    from BlindPixel Method (see MCalibrationChargePix::GetTotalFFactorBlindPixelMethod())   
// 19: Error F-Factor      from BlindPixel Method (see MCalibrationChargePix::GetTotalFFactorBlindPixelMethodErr())
// 20: Pixels valid calibration BlindPixel-Method (see MCalibrationChargePix::IsBlindPixelMethodValid())           
// 21: Conversion factor   from PINDiode Method   (see MCalibrationChargePix::GetMeanConversionPINDiodeMethod()) 
// 22: Error conv. factor  from PINDiode Method   (see MCalibrationChargePix::GetConversionPINDiodeMethodErr())  
// 23: Overall F-Factor    from PINDiode Method   (see MCalibrationChargePix::GetTotalFFactorPINDiodeMethod())   
// 24: Error F-Factor      from PINDiode Method   (see MCalibrationChargePix::GetTotalFFactorPINDiodeMethodErr())
// 25: Pixels valid calibration PINDiode-Method   (see MCalibrationChargePix::IsPINDiodeMethodValid())           
//                                                
// Localized defects:                             
// ==================
//
// 26: Excluded Pixels
// 27: Number of pickup events in the Hi Gain     (see MCalibrationPix::GetHiGainNumPickup())
// 28: Number of pickup events in the Lo Gain     (see MCalibrationPix::GetLoGainNumPickup())
//
// Other classifications of pixels:
// ================================
//
// 29: Pixels with saturated Hi-Gain              (see MCalibrationPix::IsHighGainSaturation())
//
// Used Pedestals:
// ===============
//
// 30: Pedestal for entire signal extr. range     (see MCalibrationChargePix::Ped())
// 31: Error Pedestal entire signal extr. range   (see MCalibrationChargePix::PedErr())
// 32: Ped. RMS entire signal extraction range    (see MCalibrationChargePix::PedRms())
// 33: Error Ped. RMS entire signal extr. range   (see MCalibrationChargePix::PedRmsErr())
//
// Calculated absolute arrival times (very low precision!):
// ========================================================
//
// 34: Absolute Arrival time of the signal        (see MCalibrationChargePix::GetAbsTimeMean())
// 35: RMS Ab.  Arrival time of the signal        (see MCalibrationChargePix::GetAbsTimeRms())
//
/////////////////////////////////////////////////////////////////////////////
#include "MCalibrationChargeCam.h"
#include "MCalibrationCam.h"

#include <TH2.h>
#include <TCanvas.h>
#include <TClonesArray.h>

#include "MLog.h"
#include "MLogManip.h"

#include "MGeomCam.h"
#include "MGeomPix.h"

#include "MBadPixelsCam.h"
#include "MBadPixelsPix.h"

#include "MCalibrationChargePix.h"
#include "MCalibrationChargeBlindPix.h"
#include "MCalibrationChargePINDiode.h"

ClassImp(MCalibrationChargeCam);

using namespace std;
// --------------------------------------------------------------------------
//
// Default constructor. 
//
// Sets all pointers to 0
// 
// Creates a TClonesArray of MCalibrationChargePix containers, initialized to 1 entry, destinated 
// to hold one container per pixel. Later, a call to MCalibrationChargeCam::InitSize() 
// has to be performed (in MGeomApply). 
//
// Creates a TClonesArray of MCalibrationChargePix containers, initialized to 1 entry, destinated 
// to hold one container per pixel AREA. Later, a call to MCalibrationChargeCam::InitAreas() 
// has to be performed (in MGeomApply). 
//
// Creates a TClonesArray of MCalibrationChargePix containers, initialized to 1 entry, destinated
// to hold one container per camera SECTOR. Later, a call to MCalibrationChargeCam::InitSectors() 
// has to be performed (in MGeomApply). 
//
// Calls:
// - Clear()
//
MCalibrationChargeCam::MCalibrationChargeCam(const char *name, const char *title)
    : fOffsets(NULL),
      fSlopes(NULL),
      fOffvsSlope(NULL)
{
  fName  = name  ? name  : "MCalibrationChargeCam";
  fTitle = title ? title : "Storage container for the Calibration Information in the camera";
  
  fPixels           = new TClonesArray("MCalibrationChargePix",1);
  fAverageAreas     = new TClonesArray("MCalibrationChargePix",1);
  fAverageSectors   = new TClonesArray("MCalibrationChargePix",1);
  
  Clear();
}

// --------------------------------------------------------------------------
//
// Deletes the histograms if they exist
//
MCalibrationChargeCam::~MCalibrationChargeCam()
{

  if (fOffsets)
    delete fOffsets;
  if (fSlopes)
    delete fSlopes;
  if (fOffvsSlope)
    delete fOffvsSlope;
}


// --------------------------------------
//
// Sets all variable to 0.
// Sets all flags to kFALSE
// Calls MCalibrationCam::Clear()
//
void MCalibrationChargeCam::Clear(Option_t *o)
{

  SetFFactorMethodValid    ( kFALSE );

  MCalibrationCam::Clear();

  return;
}


// -----------------------------------------------
//
// Sets the kFFactorMethodValid bit from outside
//
void MCalibrationChargeCam::SetFFactorMethodValid(const Bool_t b)
{
    b ? SETBIT(fFlags, kFFactorMethodValid) : CLRBIT(fFlags, kFFactorMethodValid); 
}


// --------------------------------------------------------------------------
//
// Test bit kFFactorMethodValid
//
Bool_t  MCalibrationChargeCam::IsFFactorMethodValid()   const
{
  return TESTBIT(fFlags,kFFactorMethodValid);
}

// --------------------------------------------------------------------------
//
// Print first the well fitted pixels 
// and then the ones which are not FitValid
//
void MCalibrationChargeCam::Print(Option_t *o) const
{

  *fLog << all << GetDescriptor() << ":" << endl;
  int id = 0;
  
  *fLog << all << "Calibrated pixels:" << endl;
  *fLog << all << endl;

  TIter Next(fPixels);
  MCalibrationChargePix *pix;
  while ((pix=(MCalibrationChargePix*)Next()))
    {
      
      if (!pix->IsExcluded()) 
	{                            

          *fLog << all << "Pix " << pix->GetPixId() 
                << ":  Ped.  Rms: "            << pix->GetPedRms()        << " +- " << pix->GetPedRmsErr() 
		<< "   Mean signal: "          << pix->GetMean()    << " +- " << pix->GetSigma() 
		<< "   Reduced Sigma: "        << pix->GetRSigma() 
		<< "   Nr Phe's: "             << pix->GetPheFFactorMethod() 
                << " Saturated? :" << pix->IsHiGainSaturation() 
		<< endl;
          id++;
	}
    }
  
  *fLog << all << id << " pixels" << endl;
  id = 0;
  
   
  *fLog << all << endl;
  *fLog << all << "Excluded pixels:" << endl;
  *fLog << all << endl;
  
  id = 0;

  TIter Next4(fPixels);
  while ((pix=(MCalibrationChargePix*)Next4()))
  {
      if (pix->IsExcluded())
      {
	  *fLog << all << pix->GetPixId() << endl;
	  id++;
      }
  }
  *fLog << all << id << " Excluded pixels " << endl;
  *fLog << endl;

  TIter Next5(fAverageAreas);
  while ((pix=(MCalibrationChargePix*)Next5()))
  {
    *fLog << all << "Average Area:" 
          << "   Ped.  Rms: "            << pix->GetPedRms()        << " +- " << pix->GetPedRmsErr() 
          << "   Mean signal: "          << pix->GetMean()    << " +- " << pix->GetMeanErr() 
          << "   Sigma signal: "         << pix->GetSigma()    << " +- "<< pix->GetSigmaErr() 
          << "   Reduced Sigma: "        << pix->GetRSigma() 
          << "   Nr Phe's: "             << pix->GetPheFFactorMethod() 
          << endl;
  }

  TIter Next6(fAverageSectors);
  while ((pix=(MCalibrationChargePix*)Next5()))
  {
    *fLog << all << "Average Sector:" 
          << "   Ped.  Rms: "            << pix->GetPedRms()        << " +- " << pix->GetPedRmsErr() 
          << "   Mean signal: "          << pix->GetMean()    << " +- " << pix->GetMeanErr() 
          << "   Sigma signal: "         << pix->GetSigma()    << " +- "<< pix->GetSigmaErr() 
          << "   Reduced Sigma: "        << pix->GetRSigma() 
          << "   Nr Phe's: "             << pix->GetPheFFactorMethod() 
          << endl;
  }

}


// --------------------------------------------------------------------------
//
// The types are as follows:
// 
// Fitted values:
// ============== 
//
// 0: Fitted Charge                              (see MCalibrationPix::GetMean())
// 1: Error of fitted Charge                     (see MCalibrationPix::GetMeanErr())
// 2: Sigma of fitted Charge                     (see MCalibrationPix::GetSigma())
// 3: Error of Sigma of fitted Charge            (see MCalibrationPix::GetSigmaErr())
//
// Useful variables derived from the fit results:
// =============================================
//
// 4: Probability Gauss fit Charge distribution  (see MCalibrationPix::GetProb())
// 5: Reduced Sigma of fitted Charge             (see MCalibrationChargePix::GetRSigma())
// 6: Error Reduced Sigma of fitted Charge       (see MCalibrationChargePix::GetRSigmaErr())
// 7: Reduced Sigma per Charge                 
// 8: Error of Reduced Sigma per Charge 
//
// Results of the different calibration methods:
// =============================================
//
//  9: Nr. Photo-electrons from F-Factor Method   (see MCalibrationChargePix::GetPheFFactorMethod())
// 10: Error Nr. Photo-el. from F-Factor Method   (see MCalibrationChargePix::GetPheFFactorMethodErr())
// 11: Conversion factor   from F-Factor Method   (see MCalibrationChargePix::GetMeanConversionFFactorMethod())
// 12: Error conv. factor  from F-Factor Method   (see MCalibrationChargePix::GetConversionFFactorMethodErr())
// 13: Overall F-Factor    from F-Factor Method   (see MCalibrationChargePix::GetTotalFFactorFFactorMethod())
// 14: Error F-Factor      from F-Factor Method   (see MCalibrationChargePix::GetTotalFFactorFFactorMethodErr())
// 15: Pixels valid calibration F-Factor-Method   (see MCalibrationChargePix::IsFFactorMethodValid())           
// 16: Conversion factor   from BlindPixel Method (see MCalibrationChargePix::GetMeanConversionBlindPixelMethod()) 
// 17: Error conv. factor  from BlindPixel Method (see MCalibrationChargePix::GetConversionBlindPixelMethodErr())  
// 18: Overall F-Factor    from BlindPixel Method (see MCalibrationChargePix::GetTotalFFactorBlindPixelMethod())   
// 19: Error F-Factor      from BlindPixel Method (see MCalibrationChargePix::GetTotalFFactorBlindPixelMethodErr())
// 20: Pixels valid calibration BlindPixel-Method (see MCalibrationChargePix::IsBlindPixelMethodValid())           
// 21: Conversion factor   from PINDiode Method   (see MCalibrationChargePix::GetMeanConversionPINDiodeMethod()) 
// 22: Error conv. factor  from PINDiode Method   (see MCalibrationChargePix::GetConversionPINDiodeMethodErr())  
// 23: Overall F-Factor    from PINDiode Method   (see MCalibrationChargePix::GetTotalFFactorPINDiodeMethod())   
// 24: Error F-Factor      from PINDiode Method   (see MCalibrationChargePix::GetTotalFFactorPINDiodeMethodErr())
// 25: Pixels valid calibration PINDiode-Method   (see MCalibrationChargePix::IsPINDiodeMethodValid())           
//                                                
// Localized defects:                             
// ==================
//
// 26: Excluded Pixels
// 27: Number of pickup events in the Hi Gain     (see MCalibrationPix::GetHiGainNumPickup())
// 28: Number of pickup events in the Lo Gain     (see MCalibrationPix::GetLoGainNumPickup())
//
// Other classifications of pixels:
// ================================
//
// 29: Pixels with saturated Hi-Gain              (see MCalibrationPix::IsHighGainSaturation())
//
// Used Pedestals:
// ===============
//
// 30: Pedestal for entire signal extr. range     (see MCalibrationChargePix::Ped())
// 31: Error Pedestal entire signal extr. range   (see MCalibrationChargePix::PedErr())
// 32: Ped. RMS entire signal extraction range    (see MCalibrationChargePix::PedRms())
// 33: Error Ped. RMS entire signal extr. range   (see MCalibrationChargePix::PedRmsErr())
//
// Calculated absolute arrival times (very low precision!):
// ========================================================
//
// 34: Absolute Arrival time of the signal        (see MCalibrationChargePix::GetAbsTimeMean())
// 35: RMS Ab.  Arrival time of the signal        (see MCalibrationChargePix::GetAbsTimeRms())
//
Bool_t MCalibrationChargeCam::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
{

  if (idx > GetSize())
    return kFALSE;

  Float_t area = cam[idx].GetA();

 if (area == 0)
    return kFALSE;

 MCalibrationChargePix &pix = (MCalibrationChargePix&)(*this)[idx];

  switch (type)
    {
    case 0:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetMean();
      break;
    case 1:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetMeanErr();
      break;
    case 2:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetSigma();
      break;
    case 3:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetSigmaErr();
      break;
    case 4:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetProb();
      break;
    case 5:
      if (pix.IsExcluded())
        return kFALSE;
      if (pix.GetRSigma() == -1.)
	  return kFALSE;
      val = pix.GetRSigma();
      break;
    case 6:
      if (pix.IsExcluded())
        return kFALSE;    
      if (pix.GetRSigma() == -1.)
	  return kFALSE;
      val = pix.GetRSigmaErr();
      break;
    case 7:
      if (pix.IsExcluded())
        return kFALSE;
      if (pix.GetRSigma() == -1.)
        return kFALSE;
      if (pix.GetMean() == 0.)
        return kFALSE;
      val = pix.GetRSigma() / pix.GetMean();
      break;
    case 8:
      if (pix.IsExcluded())
        return kFALSE;
      if (pix.GetRSigmaRelVar() <= 0.)
        return kFALSE;
      if (pix.GetMeanRelVar() <= 0.)
        return kFALSE;
      // relative error Rsigma square
      val =  pix.GetRSigmaRelVar() + pix.GetMeanRelVar();
      //a calculate relative error out of squares
      if (val < 0)
        val = -1.;
      else
        val  =   TMath::Sqrt(val) * pix.GetRSigma() / pix.GetMean();
      break;
    case 9:
      if (pix.IsExcluded() || !pix.IsFFactorMethodValid())
        return kFALSE;
      val = pix.GetPheFFactorMethod();
      break;
    case 10:
      if (pix.IsExcluded() || !pix.IsFFactorMethodValid())
        return kFALSE;
      val = pix.GetPheFFactorMethodErr();
      break;
    case 11:
      if (pix.IsExcluded() || !pix.IsFFactorMethodValid())
        return kFALSE;
      val = pix.GetMeanConvFADC2Phe();
      break;
    case 12:
      if (pix.IsExcluded() || !pix.IsFFactorMethodValid())
        return kFALSE;
      val = pix.GetMeanConvFADC2PheErr();
      break;
    case 13:
      if (pix.IsExcluded() || !pix.IsFFactorMethodValid())
        return kFALSE;
      val = pix.GetMeanFFactorFADC2Phot();
      break;
    case 14:
      if (pix.IsExcluded() || !pix.IsFFactorMethodValid())
        return kFALSE;
      val = pix.GetMeanFFactorFADC2Phot();
      break;
    case 15:
      if (pix.IsExcluded())
        return kFALSE;
      if (pix.IsFFactorMethodValid())
        val = 1;
      else
        return kFALSE;
      break;
    case 16:
      return kFALSE;
      break;
    case 17:
      return kFALSE;
      break;
    case 18:
      return kFALSE;
      break;
    case 19:
      return kFALSE;
      break;
    case 20:
      return kFALSE;
      break;
    case 21:
      return kFALSE;
      break;
    case 22:
      return kFALSE;
      break;
    case 23:
      return kFALSE;
      break;
    case 24:
      return kFALSE;
      break;
    case 25:
      return kFALSE;
      break;
    case 26:
      if (pix.IsExcluded())
        val = 1.;
      else
        return kFALSE;
      break;
    case 27:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetHiGainNumPickup();
      break;
    case 28:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetLoGainNumPickup();
      break;
    case 29:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.IsHiGainSaturation();
      break;
    case 30:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetPed();
      break;
    case 31:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetPedErr();
      break;
    case 32:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetPedRms();
      break;
    case 33:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetPedErr()/2.;
      break;
    case 34:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetAbsTimeMean();
      break;
    case 35:
      if (pix.IsExcluded())
        return kFALSE;
      val = pix.GetAbsTimeRms();
      break;
    default:
      return kFALSE;
    }

  return val!=-1.;

}

// --------------------------------------------------------------------------
//
// Calls MCalibrationChargePix::DrawClone()
//
void MCalibrationChargeCam::DrawPixelContent(Int_t idx) const
{
  MCalibrationChargePix &pix = (MCalibrationChargePix&)(*this)[idx];
  pix.DrawClone();
}



Bool_t MCalibrationChargeCam::GetConversionFactorFFactor(Int_t ipx, Float_t &mean, Float_t &err, Float_t &ffactor)
{

  MCalibrationChargePix &pix = (MCalibrationChargePix&)(*this)[ipx];

  Float_t conv = pix.GetMeanConvFADC2Phe();

  if (conv < 0.)
    return kFALSE;

  mean    = conv;
  err     = pix.GetMeanConvFADC2PheErr();
  ffactor = pix.GetMeanFFactorFADC2Phot();

  return kTRUE;
}


/*
void MCalibrationChargeCam::DrawHiLoFits()
{

  if (!fOffsets)
    fOffsets = new TH1D("pp","Offsets of the HiGain LoGain Fit",100,-600.,400.);
  if (!fSlopes)
    fSlopes  = new TH1D("mm","Slopes of the HiGain LoGain Fit",100,-2.,2.);
  if (!fOffvsSlope)
    fOffvsSlope = new TH2D("aa","Slopes vs Offsets of the HiGain LoGain Fit",100,-600.,400.,100,-2.,2.);

  TIter Next(fPixels);
  MCalibrationPix *pix;
  MHCalibrationPixel *hist;
  while ((pix=(MCalibrationPix*)Next()))
    {
      hist = pix->GetHist();
      hist->FitHiGainvsLoGain();
      fOffsets->Fill(hist->GetOffset(),1.);
      fSlopes->Fill(hist->GetSlope(),1.);
      fOffvsSlope->Fill(hist->GetOffset(),hist->GetSlope(),1.);
    }

   TCanvas *c1 = new TCanvas();

   c1->Divide(1,3);
   c1->cd(1);
   fOffsets->Draw();
   gPad->Modified();
   gPad->Update();

   c1->cd(2);
  fSlopes->Draw();
  gPad->Modified();
  gPad->Update();

  c1->cd(3);
  fOffvsSlope->Draw("col1");
  gPad->Modified();
  gPad->Update();
}

*/
