/* ======================================================================== *\
!
! *
! * 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  2001 <mailto:tbretz@uni-sw.gwdg.de>
!
!   Copyright: MAGIC Software Development, 2000-2001
!
!
\* ======================================================================== */

///////////////////////////////////////////////////////////////////////
//
// MHHillasSrc
//
// This class contains histograms for every Hillas parameter
//
///////////////////////////////////////////////////////////////////////
#include "MHHillasSrc.h"

#include <math.h>

#include <TH1.h>
#include <TPad.h>
#include <TCanvas.h>

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

#include "MGeomCam.h"

#include "MParList.h"

#include "MHillas.h"
#include "MHillasSrc.h"

ClassImp(MHHillasSrc);

// --------------------------------------------------------------------------
//
// Setup four histograms for Alpha, and Dist
//
MHHillasSrc::MHHillasSrc(const char *name, const char *title)
    : fUseMmScale(kTRUE)
{
    //
    //   set the name and title of this object
    //
    fName  = name  ? name  : "MHHillasSrc";
    fTitle = title ? title : "Container for Hillas histograms";

    //
    // loop over all Pixels and create two histograms
    // one for the Low and one for the High gain
    // connect all the histogram with the container fHist
    //
    fAlpha    = new TH1F("Alpha",    "Alpha of Ellipse",            181,  -90,  90);
    fDist     = new TH1F("Dist",     "Dist of Ellipse",             100,    0, 445);
    fCosDA    = new TH1F("CosDA",    "cos(Delta,Alpha) of Ellipse", 101,   -1,   1);

    fAlpha->SetDirectory(NULL);
    fDist->SetDirectory(NULL);
    fCosDA->SetDirectory(NULL);

    fAlpha->SetXTitle("\\alpha [\\circ]");
    fDist->SetXTitle("Dist [mm]");
    fCosDA->SetXTitle("cos(\\delta,\\alpha)");

    fAlpha->SetYTitle("Counts");
    fDist->SetYTitle("Counts");
    fCosDA->SetYTitle("Counts");
}

// --------------------------------------------------------------------------
//
// Delete the four histograms
//
MHHillasSrc::~MHHillasSrc()
{
    delete fAlpha;
    delete fDist;
    delete fCosDA;
}

// --------------------------------------------------------------------------
//
// Setup the Binning for the histograms automatically if the correct
// instances of MBinning (with the names 'BinningAlpha' and 'BinningDist')
// are found in the parameter list
// Use this function if you want to set the conversion factor which
// is used to convert the mm-scale in the camera plain into the deg-scale
// used for histogram presentations. The conversion factor is part of
// the camera geometry. Please create a corresponding MGeomCam container.
//
Bool_t MHHillasSrc::SetupFill(const MParList *plist)
{
    const MGeomCam *geom = (MGeomCam*)plist->FindObject("MGeomCam");
    if (!geom)
        *fLog << warn << dbginf << "No Camera Geometry available. Using mm-scale for histograms." << endl;
    else
    {
        fMm2Deg = geom->GetConvMm2Deg();
        SetMmScale(kFALSE);
    }

    ApplyBinning(*plist, "Alpha",    fAlpha);
    ApplyBinning(*plist, "Dist",     fDist);

    return kTRUE;
}

// --------------------------------------------------------------------------
//
// Fill the four histograms with data from a MHillas-Container.
// Be careful: Only call this with an object of type MHillas
//
Bool_t MHHillasSrc::Fill(const MParContainer *par, const Stat_t w)
{
    if (!par)
    {
        *fLog << err << "MHHillasSrc::Fill: Pointer (!=NULL) expected." << endl;
        return kFALSE;
    }

    const MHillasSrc &h = *(MHillasSrc*)par;

    fAlpha->Fill(h.GetAlpha(), w);
    fDist ->Fill(fUseMmScale ? h.GetDist() : fMm2Deg*h.GetDist(), w);
    fCosDA->Fill(h.GetCosDeltaAlpha(), w);

    return kTRUE;
}

// --------------------------------------------------------------------------
//
// Use this function to setup your own conversion factor between degrees
// and millimeters. The conversion factor should be the one calculated in
// MGeomCam. Use this function with Caution: You could create wrong values
// by setting up your own scale factor.
//
void MHHillasSrc::SetMm2Deg(Float_t mmdeg)
{
    if (mmdeg<=0)
    {
        *fLog << warn << dbginf << "Warning - Conversion factor <= 0 - nonsense. Ignored." << endl;
        return;
    }

    if (fMm2Deg>0)
        *fLog << warn << dbginf << "Warning - Conversion factor already set. Overwriting" << endl;

    fMm2Deg = mmdeg;
}

// --------------------------------------------------------------------------
//
// With this function you can convert the histogram ('on the fly') between
// degrees and millimeters.
//
void MHHillasSrc::SetMmScale(Bool_t mmscale)
{
    if (fUseMmScale == mmscale)
        return;

    if (fMm2Deg<0)
    {
        *fLog << warn << GetDescriptor() << ": Warning - Sorry, no conversion factor for conversion available." << endl;
        return;
    }

    const Double_t scale = mmscale ? 1./fMm2Deg : fMm2Deg;
    MH::ScaleAxis(fDist, scale);

    fDist->SetXTitle(mmscale ? "Dist [mm]" : "Dist [\\circ]");

    fUseMmScale = mmscale;
}

// --------------------------------------------------------------------------
//
// Creates a new canvas and draws the two histograms into it.
// Be careful: The histograms belongs to this object and won't get deleted
// together with the canvas.
//
void MHHillasSrc::Draw(Option_t *)
{
    TVirtualPad *pad = gPad ? gPad : MakeDefCanvas(this);
    pad->SetBorderMode(0);

    AppendPad("");

    // FIXME: Display Source position

    pad->Divide(2, 2);

    pad->cd(1);
    gPad->SetBorderMode(0);
    fAlpha->Draw();

    pad->cd(2);
    gPad->SetBorderMode(0);
    fDist->Draw();

    pad->cd(4);
    gPad->SetBorderMode(0);
    //gPad->SetLogy();
    fCosDA->Draw();

    pad->Modified();
    pad->Update();
}

void MHHillasSrc::Paint(Option_t *opt)
{
    if (fCosDA->GetEntries()==0)
        return;

    TVirtualPad *savepad = gPad;
    savepad->cd(4);
    gPad->SetLogy();
    gPad = savepad;
}

TH1 *MHHillasSrc::GetHistByName(const TString name)
{
    if (name.Contains("Alpha", TString::kIgnoreCase))
        return fAlpha;
    if (name.Contains("Dist", TString::kIgnoreCase))
        return fDist;
    if (name.Contains("CosDA", TString::kIgnoreCase))
        return fCosDA;

    return NULL;
}
