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

//////////////////////////////////////////////////////////////////////////////
//                                                                        
//  MGeomApply                                                            
//                                                                        
//  Applies the geometry to geometry dependant containers.
//
//  It changes the size of the arrays in the containers to a size
//  matching the number of pixels, eg:
//
//   MPedestalCam
//   MBlindPixels
//
//  It uses the geometry (MGeomCam) found in the parameter list.
//  If it is not found the task tries to create the geometry
//  specified in the constructor. The default geometry is
//  MGeomCamMagic.
// 
//  Input Containers:
//   [MGeomCam]
//
//  Output Containers:
//   [MPedestalCam]
//   [MBlindPixels]
//
//////////////////////////////////////////////////////////////////////////////
#include "MGeomApply.h"

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

#include "MParList.h"

#include "MGeomCam.h"
#include "MPedestalCam.h"
#include "MCalibrationCam.h"
#include "MBlindPixels.h"

ClassImp(MGeomApply);

using namespace std;

// --------------------------------------------------------------------------
//
//  Default constructor. MGeomCamMagic is the default geometry.
//
MGeomApply::MGeomApply(const char *name, const char *title) : fGeomName("MGeomCamMagic")
{
    fName  = name  ? name  : "MGeomApply";
    fTitle = title ? title : "Task to apply geometry settings";
}

// --------------------------------------------------------------------------
//
//  Try to find 'MGeomCam' in the Parameter List. If it is not found,
//  try to create a fGeomName object.
//
Int_t MGeomApply::PreProcess(MParList *pList)
{
    MGeomCam *cam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
    if (cam)
        return kTRUE;

    cam = (MGeomCam*)pList->FindCreateObj(AddSerialNumber(fGeomName), "MGeomCam");

    return cam ? kTRUE : kFALSE;
}

// --------------------------------------------------------------------------
//
//  Try to find 'MGeomCam' in the Parameter List. If it is not found,
//  processing is stopped.
//
//  The size of MPedestalCam and MBlindPixels is set accordingly.
//
Bool_t MGeomApply::ReInit(MParList *pList)
{
    MGeomCam *cam = (MGeomCam*)pList->FindObject(AddSerialNumber("MGeomCam"));
    if (!cam)
    {
        *fLog << err << GetDescriptor() << ": No MGeomCam found... aborting." << endl;
        return kFALSE;
    }

    // FIXME, workaround: this call to CalcPixRatio is here just to allow
    // the use of some camera files from the 0.7 beta version in which the 
    // array containing pixel ratios is not initialized.

    cam->CalcPixRatio();

    MPedestalCam *ped = (MPedestalCam*)pList->FindObject(AddSerialNumber("MPedestalCam"));
    if (ped)
        ped->InitSize(cam->GetNumPixels());


    MCalibrationCam *cal = (MCalibrationCam*)pList->FindObject(AddSerialNumber("MCalibrationCam"));
    if (cal)
        cal->InitSize(cam->GetNumPixels());


    MBlindPixels *bnd = (MBlindPixels*)pList->FindObject(AddSerialNumber("MBlindPixels"));
    if (bnd)
        bnd->InitSize(cam->GetNumPixels());

    return kTRUE;
}
