/* ======================================================================== *\
!
! *
! * 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): Thomas Bretz, 10/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
!
!   Copyright: MAGIC Software Development, 2000-2003
!
!
\* ======================================================================== */

/////////////////////////////////////////////////////////////////////////////
//
// MCameraData
//
/////////////////////////////////////////////////////////////////////////////
#include "MCameraData.h"

#include "MCerPhotEvt.h"
#include "MCerPhotPix.h"

#include "MGeomCam.h"

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

#include "MPedestalCam.h"
#include "MPedestalPix.h"

#include "MSigmabar.h"

ClassImp(MCameraData);

using namespace std;

// --------------------------------------------------------------------------
//
// Creates a MCerPhotPix object for each pixel in the event
//
MCameraData::MCameraData(const char *name, const char *title)
{
    fName  = name  ? name  : "MCameraData";
    fTitle = title ? title : "Photons/PedRMS Information";
}

// --------------------------------------------------------------------------
//
// This is not yet implemented like it should.
//
/*
void MCameraData::Draw(Option_t* option) 
{
    //
    //   FIXME!!! Here the Draw function of the CamDisplay
    //   should be called to add the CamDisplay to the Pad.
    //   The drawing should be done in MCamDisplay::Paint
    //

    //    MGeomCam *geom = fType ? new MGeomCamMagic : new MGeomCamCT1;
    //    MCamDisplay *disp = new MCamDisplay(geom);
    //    delete geom;
    //    disp->DrawPhotNum(this);
}
*/

void MCameraData::Calc(const MCerPhotEvt &evt, const MPedestalCam &cam,
                      const MGeomCam &geom)
{
    const Int_t n = geom.GetNumPixels();

    fData.Set(n);
    fData.Reset();

    fValidity.Set(n);
    fValidity.Reset();

    const Int_t entries = evt.GetNumPixels();

    //
    // check the number of all pixels against the noise level and
    // set them to 'unused' state if necessary
    // 
    for (Int_t i=0; i<entries; i++)
    {
        const MCerPhotPix &pix = evt[i];

        const Int_t idx = pix.GetPixId();
        const Float_t noise = cam[idx].GetPedestalRms();

        if (noise<=0) // fData[idx]=0, fValidity[idx]=0
            continue;

        //
	// We calculate a correction factor which accounts for the 
	// fact that pixels have different size (see TDAS 02-14).
	//
        fData[idx] = pix.GetNumPhotons() * geom.GetPixRatioSqrt(idx) / noise;
        fValidity[idx] = 1;
    }
}

void MCameraData::Calc(const MCerPhotEvt &evt, const MSigmabar &sgb,
                      const MGeomCam &geom)
{
    Calc(evt, sgb.GetSigmabarInner(), geom);
}

void MCameraData::Calc(const MCerPhotEvt &evt, Double_t noise,
                      const MGeomCam &geom)
{
    const Int_t n = geom.GetNumPixels();

    fData.Set(n);
    fData.Reset();

    fValidity.Set(n);
    fValidity.Reset();

    if (noise<=0)
        return;

    const Int_t entries = evt.GetNumPixels();

    //
    // check the number of all pixels against the noise level and
    // set them to 'unused' state if necessary
    // 
    for (Int_t i=0; i<entries; i++)
    {
        const MCerPhotPix &pix = evt[i];

        const Int_t idx = pix.GetPixId();

        //
	// We calculate a correction factor which accounts for the 
	// fact that pixels have different size (see TDAS 02-14).
	//
        fData[idx] = pix.GetNumPhotons() * geom.GetPixRatio(idx) / noise;
        fValidity[idx] = 1;
    }
}

// --------------------------------------------------------------------------
//
// Returns, depending on the type flag:
//
//  0: Number of Photons*PixRatio
//  1: Error*sqrt(PixRatio)
//  2: Cleaning level = Num Photons*sqrt(PixRatio)/Error
//  3: Number of Photons
//  4: Error
//
Bool_t MCameraData::GetPixelContent(Double_t &val, Int_t idx, const MGeomCam &cam, Int_t type) const
{
    if (idx<0 || idx>=fData.GetSize())
        return kFALSE;

    val = fData[idx];
    return fValidity[idx];
}

void MCameraData::DrawPixelContent(Int_t num) const
{
    *fLog << warn << "MCameraData::DrawPixelContent - not available." << endl;
}
