/* ======================================================================== *\
!
! *
! * 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@astro.uni-wuerzburg.de>
!   Author(s): Wolfgang Wittek  2002 <mailto:wittek@mppmu.mpg.de>
!
!   Copyright: MAGIC Software Development, 2000-2004
!
!
\* ======================================================================== */

/////////////////////////////////////////////////////////////////////////////
//
// MHVsSize
//
// This class contains histograms for the source independent image parameters
//
/////////////////////////////////////////////////////////////////////////////
#include "MHVsSize.h"

#include <TH2.h>
#include <TPad.h>
#include <TStyle.h>
#include <TCanvas.h>

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

#include "MParList.h"

#include "MHillas.h"
#include "MHillasSrc.h"
#include "MNewImagePar.h"
#include "MGeomCam.h"
#include "MBinning.h"

ClassImp(MHVsSize);

using namespace std;

// --------------------------------------------------------------------------
//
// Setup four histograms for Width, Length
//
MHVsSize::MHVsSize(const char *name, const char *title)
    : fHillas(NULL), fNewImagePar(NULL), fMm2Deg(1), fUseMmScale(kTRUE)
{
    //
    //   set the name and title of this object
    //
    fName  = name  ? name  : "MHVsSize";
    fTitle = title ? title : "Source independent image parameters";

    fLength.SetNameTitle("Length", "Length vs. Size");
    fWidth.SetNameTitle( "Width",  "Width vs. Size");
    fDist.SetNameTitle(  "Dist",   "Dist vs. Size");
    fConc1.SetNameTitle( "Conc1",  "Conc1 vs. Size");

    fLength.SetDirectory(NULL);
    fWidth.SetDirectory(NULL);
    fDist.SetDirectory(NULL);
    fConc1.SetDirectory(NULL);

    fLength.SetXTitle("Size [phe]");
    fWidth.SetXTitle("Size [phe]");
    fDist.SetXTitle("Size [phe]");
    fConc1.SetXTitle("Size [phe]");

    fLength.SetYTitle("Length [mm]");
    fWidth.SetYTitle("Width [mm]");
    fDist.SetYTitle("Distance [mm]");
    fConc1.SetYTitle("Conc1 [ratio]");

    MBinning binse, binsl, binsd, binsc;
    binse.SetEdgesLog(50, 10, 1e5);
    binsl.SetEdges(100,  0, 296.7/2);
    binsd.SetEdges(100,  0, 445);
    binsc.SetEdgesLog(100,  1e-5, 5e-3);

    MH::SetBinning(&fLength, &binse, &binsl);
    MH::SetBinning(&fWidth,  &binse, &binsl);
    MH::SetBinning(&fDist,   &binse, &binsd);
    MH::SetBinning(&fConc1,  &binse, &binsc);

    fLength.UseCurrentStyle();
    fWidth.UseCurrentStyle();
    fDist.UseCurrentStyle();
    fConc1.UseCurrentStyle();
}

// --------------------------------------------------------------------------
//
// Setup the Binning for the histograms automatically if the correct
// instances of MBinning (with the names 'BinningWidth' and 'BinningLength')
// 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 MHVsSize::SetupFill(const MParList *plist)
{
    MGeomCam *geom = (MGeomCam*)plist->FindObject("MGeomCam");
    if (!geom)
        *fLog << warn << GetDescriptor() << ": No Camera Geometry available. Using mm-scale for histograms." << endl;
    else
    {
        fMm2Deg = geom->GetConvMm2Deg();
        SetMmScale(kFALSE);
    }

    fHillas = (MHillas*)plist->FindObject("MHillas");
    if (!fHillas)
    {
        *fLog << err << "MHillas not found... abort." << endl;
        return kFALSE;
    }

    fNewImagePar = (MNewImagePar*)plist->FindObject("MNewImagePar");
    if (!fNewImagePar)
    {
        *fLog << err << "MNewImagePar not found... abort." << endl;
        return kFALSE;
    }

    /*
    ApplyBinning(*plist, "Width",  fWidth);
    ApplyBinning(*plist, "Length", fLength);
    ApplyBinning(*plist, "Dist",   fDistC);
    ApplyBinning(*plist, "Delta",  fDelta);
    ApplyBinning(*plist, "Size",   fSize);

    const MBinning *bins = (MBinning*)plist->FindObject("BinningCamera");
    if (!bins)
    {
        float r = fGeomCam ? fGeomCam->GetMaxRadius() : 600;
        r *= 0.9;
        if (!fUseMmScale)
            r *= fMm2Deg;

        MBinning b;
        b.SetEdges(61, -r, r);
        SetBinning(fCenter, &b, &b);
    }
    else
        SetBinning(fCenter, bins, bins);
    */

    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 MHVsSize::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 MHVsSize::SetMmScale(Bool_t mmscale)
{
    if (fUseMmScale == mmscale)
        return;

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

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

    if (mmscale)
    {
        fLength.SetYTitle("Length [mm]");
        fWidth.SetYTitle("Width [mm]");
        fDist.SetYTitle("Distance [mm]");
    }
    else
    {
        fLength.SetYTitle("Length [\\circ]");
        fWidth.SetYTitle("Width [\\circ]");
        fDist.SetYTitle("Distance [\\circ]");
    }

    fUseMmScale = mmscale;
}

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

    const Double_t scale = fUseMmScale ? 1 : fMm2Deg;

    fLength.Fill(fHillas->GetSize(), scale*fHillas->GetLength(),     w);
    fWidth.Fill( fHillas->GetSize(), scale*fHillas->GetWidth(),      w);
    fDist.Fill(  fHillas->GetSize(), scale*src->GetDist(),           w);
    fConc1.Fill( fHillas->GetSize(), scale*fNewImagePar->GetConc1(), w);

    return kTRUE;
}

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

    AppendPad("");

    TString opt(o);
    opt.ToLower();

    // FIXME: If same-option given make two independant y-axis!
    const Bool_t same = opt.Contains("same");

    if (!same)
        pad->Divide(2,2);
    else
    {
        fDist.SetMarkerColor(kBlue);
        fConc1.SetMarkerColor(kBlue);
        fWidth.SetMarkerColor(kBlue);
        fLength.SetMarkerColor(kBlue);
    }

    pad->cd(1);
    gPad->SetBorderMode(0);
    gPad->SetLogx();
    fLength.Draw(same?"same":"");

    pad->cd(2);
    gPad->SetBorderMode(0);
    gPad->SetLogx();
    fWidth.Draw(same?"same":"");

    pad->cd(3);
    gPad->SetBorderMode(0);
    gPad->SetLogx();
    fDist.Draw(same?"same":"");

    pad->cd(4);
    gPad->SetBorderMode(0);
    gPad->SetLogx();
    gPad->SetLogy();
    fConc1.Draw(same?"same":"");
}

void MHVsSize::Paint(Option_t *opt)
{
    //SetColors();
    //MH::Paint();
}
