/* ======================================================================== *\
!
! *
! * 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   02/2004 <mailto:markus@ifae.es>
!
!   Copyright: MAGIC Software Development, 2000-2004
!
\* ======================================================================== */
/////////////////////////////////////////////////////////////////////////////
//                                                     
// MCalibrationPix                                     
//                                                     
// Base Storage container for a calibration pixel. Holds mean and sigmas,  
// their errors, the fit probability and the number of pickup events for 
// the high-gain and the low-gain derived values.    
//                                                                         
// Errors are stored internally as variances, but are returned and filled 
// as square root of the variances. 
//
// Calls to GetMean(), GetMeanErr(), GetSigma(), GetSigmaErr(), GetProb() or 
// GetNumPickup() and GetNumBlackout() test first the bit kHiGainSaturation 
// before returning the high-gain or low-gain value, analogue for the 
// corr. Setters.
//
// The three flags: kValid, kExcluded and kHiGainSaturation may be set.
// The colors: kGREEN, kBLUE, kUV and kCT1 may be set. 
//
/////////////////////////////////////////////////////////////////////////////
#include "MCalibrationPix.h"

ClassImp(MCalibrationPix);

using namespace std;

// --------------------------------------------------------------------------
//
// Default Constructor: 
//
// Sets:
// - fPixId to -1
// - fFlags to 0
//
// Calls:
// - Clear()
//
MCalibrationPix::MCalibrationPix(const char *name, const char *title)
    : fPixId(-1),
      fFlags(0)
{

  fName  = name  ? name  : "MCalibrationPix";
  fTitle = title ? title : "Container of the fit results of MHCalibrationPixs ";

  Clear();

}

// ------------------------------------------------------------------------
//
// Sets:
// - all variables to -1
// - all flags to kFALSE
//
void MCalibrationPix::Clear(Option_t *o)
{

  fHiGainNumBlackout   =  -1 ;
  fHiGainNumPickup     =  -1 ;
  fHiGainMean          =  -1.;
  fHiGainMeanVar       =  -1.;
  fHiGainProb          =  -1.;
  fHiGainSigma         =  -1.;
  fHiGainSigmaVar      =  -1.;

  fLoGainNumBlackout   =  -1 ;
  fLoGainNumPickup     =  -1 ;
  fLoGainMean          =  -1.;
  fLoGainMeanVar       =  -1.;
  fLoGainProb          =  -1.;
  fLoGainSigma         =  -1.;
  fLoGainSigmaVar      =  -1.;

  SetHiGainSaturation  ( kFALSE );
  SetExcluded          ( kFALSE );
  SetValid             ( kFALSE );

}

// --------------------------------------------------------------------------
//
// Set the Hi Gain Saturation Bit from outside
//
void MCalibrationPix::SetHiGainSaturation(Bool_t b)
{
    b ?  SETBIT(fFlags, kHiGainSaturation) : CLRBIT(fFlags, kHiGainSaturation); 
}


// --------------------------------------------------------------------------
//
// Set the Excluded Bit from outside 
//
void MCalibrationPix::SetExcluded(Bool_t b )
{ 
    b ?  SETBIT(fFlags, kExcluded) : CLRBIT(fFlags, kExcluded); 
}

// --------------------------------------------------------------------------
//
// Set the Valid Bit from outside 
//
void MCalibrationPix::SetValid(Bool_t b )
{ 
    b ?  SETBIT(fFlags, kValid) : CLRBIT(fFlags, kValid); 
}

// --------------------------------------------------------------------------
//
// Get the Relative Variance of either High Gain or Low Gain Mean 
// depending on IsHighGainSaturation()
//
// If variance is smaller than 0. return -1.
//
Float_t MCalibrationPix::GetMeanRelVar()  const
{

  if (IsHiGainSaturation())
    if (fLoGainMeanVar < 0.)
      return -1.;
    else
      return fLoGainMeanVar / (fLoGainMean * fLoGainMean);
  else
    if (fHiGainMeanVar < 0.)
      return -1.;
    else
      return fHiGainMeanVar / (fHiGainMean * fHiGainMean);
}

// --------------------------------------------------------------------------
//
// Get the Square of either High Gain or Low Gain Mean 
// depending on IsHighGainSaturation()
//
Float_t MCalibrationPix::GetMeanSquare()  const
{

  if (IsHiGainSaturation())
    return fLoGainMean == -1. ? -1. : fLoGainMean * fLoGainMean;
  else
    return fHiGainMean == -1. ? -1. : fHiGainMean * fHiGainMean;
}


// --------------------------------------------------------------------------
//
// Get the Relative Variance of either High Gain or Low Gain Sigma 
// depending on IsHighGainSaturation()
//
// If variance is smaller than 0. return -1.
//
Float_t MCalibrationPix::GetSigmaRelVar()  const
{

  if (IsHiGainSaturation())
    if (fLoGainSigmaVar < 0.)
      return -1.;
    else
      return fLoGainSigmaVar / (fLoGainSigma * fLoGainSigma);
  else
    if (fHiGainSigmaVar < 0.)
      return -1.;
    else
      return fHiGainSigmaVar / (fHiGainSigma * fHiGainSigma);
}

// --------------------------------------------------------------------------
//
// Get the High Gain Mean Error: Takes square root of fHiGainMeanVar
//
Float_t MCalibrationPix::GetHiGainMeanErr()  const
{
  if (fLoGainMeanVar < 0.)
    return -1.;

  return TMath::Sqrt(fLoGainMeanVar);
}


// --------------------------------------------------------------------------
//
// Get the High Gain Sigma Error: Takes square root of fHiGainSigmaVar
//
Float_t MCalibrationPix::GetHiGainSigmaErr()  const
{
  if (fHiGainSigmaVar < 0.)
    return -1.;

  return TMath::Sqrt(fHiGainSigmaVar);
}

// --------------------------------------------------------------------------
//
// Get the Low Gain Mean Error: Takes square root of fLoGainMeanVar
//
Float_t MCalibrationPix::GetLoGainMeanErr()  const
{
  if (fLoGainMeanVar < 0.)
    return -1.;

  return TMath::Sqrt(fLoGainMeanVar);
}

// --------------------------------------------------------------------------
//
// Get the Low Gain Mean Rel Variance
//
Float_t MCalibrationPix::GetLoGainMeanRelVar()  const
{
  if (fLoGainMeanVar < 0.)
    return -1.;
  if (fLoGainMean   == 0.)
    return -1.;

  return fLoGainMeanVar / ( fLoGainMean * fLoGainMean);
}

// --------------------------------------------------------------------------
//
// Get the Low Gain Sigma Error: Takes square root of fHiGainSigmaVar
//
Float_t MCalibrationPix::GetLoGainSigmaErr()  const
{
  if (fLoGainSigmaVar < 0.)
    return -1.;

  return TMath::Sqrt(fLoGainSigmaVar);
}

// --------------------------------------------------------------------------
//
// Test bit kHiGainSaturation
//
Bool_t MCalibrationPix::IsHiGainSaturation()    const
{ 
  return TESTBIT(fFlags,kHiGainSaturation);  
}

// --------------------------------------------------------------------------
//
// Test bit kExcluded
//
Bool_t MCalibrationPix::IsExcluded()     const
{ 
   return TESTBIT(fFlags,kExcluded);  
}

// --------------------------------------------------------------------------
//
// Test bit kValid
//
Bool_t MCalibrationPix::IsValid()     const
{ 
   return TESTBIT(fFlags,kValid);  
}

