/* ======================================================================== *\
!
! *
! * 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    12/2000 <mailto:tbretz@uni-sw.gwdg.de>
!   Author(s): Rudolf Bock     10/2001 <mailto:Rudolf.Bock@cern.ch>
!
!   Copyright: MAGIC Software Development, 2000-2002
!
!
\* ======================================================================== */

//////////////////////////////////////////////////////////////////////////////
//
// MHillasSrcCalc
//
// Task to calculate the source dependant part of the hillas parameters
//
//////////////////////////////////////////////////////////////////////////////
#include "MHillasSrcCalc.h"

#include <fstream.h>

#include "MParList.h"

#include "MHillasSrc.h"

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

ClassImp(MHillasSrcCalc);

// -------------------------------------------------------------------------
//
// Default constructor. The first argument is the name of a container
// containing the source position in the camera plain (MScrPosCam).
// The default is "MSrcPosCam". hil is the name of a container
// of type MHillasSrc (or derived) in which the parameters are stored
// The default is "MHillasSrc"
//
MHillasSrcCalc::MHillasSrcCalc(const char *src, const char *hil,
                               const char *name, const char *title)
{
    fName  = name  ? name  : "MHillasSrcCalc";
    fTitle = title ? title : "add parameters dependent on source position (MHillasSrc)";

    fSrcName    = src;
    fHillasName = hil;
}

// -------------------------------------------------------------------------
//
Bool_t MHillasSrcCalc::PreProcess(MParList *pList)
{
    fHillas = (MHillas*)pList->FindObject("MHillas");
    if (!fHillas)
    {
        *fLog << err << dbginf << "MHillas not found... aborting." << endl;
        return kFALSE;
    }

    fSrcPos = (MSrcPosCam*)pList->FindObject(fSrcName, "MSrcPosCam");
    if (!fSrcPos)
    {
        *fLog << err << dbginf << fSrcName << " [MSrcPosCam] not found... aborting." << endl;
        return kFALSE;
    }

    fHillasSrc = (MHillasSrc*)pList->FindCreateObj("MHillasSrc", fHillasName);
    if (!fHillasSrc)
        return kFALSE;

    fHillasSrc->SetSrcPos(fSrcPos);

    return kTRUE;
}

// -------------------------------------------------------------------------
//
Bool_t MHillasSrcCalc::Process()
{
    return fHillasSrc->Calc(fHillas) ? kTRUE : kCONTINUE;
}


void MHillasSrcCalc::SavePrimitive(ofstream &out, Option_t *o="")
{
    out << "   MHillasSrcCalc " << ToLower(fName) << "(\"";
    out << fSrcName << "\", \"" << fHillasName << "\", \"";
    out << fName << "\", \"" << fTitle << "\");" << endl;
}
