#include "machdefs.h"
#include <stdlib.h>
#include <typeinfo>
#include <iostream>
#include <string>

#include "nomhistadapter.h"
#include "pihisto.h"
#include "pihisto2d.h"
#include "pipodrw.h"
#include "servnobjm.h"

#ifndef SANS_EVOLPLANCK
#include "objfitter.h"
#include "fitsntuple.h"
#include "fitsxntuple.h"
#endif

//-------------------------------------------------------------------------
// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet Histo / HProf
//-------------------------------------------------------------------------

/* --Methode-- */
NOMAdapter_Histo::NOMAdapter_Histo(Histo* o)
  : NObjMgrAdapter(o)
{
mHis = o;
}

/* --Methode-- */
NOMAdapter_Histo::~NOMAdapter_Histo()
{
}

/* --Methode-- */
NObjMgrAdapter* NOMAdapter_Histo::Clone(AnyDataObj* o)
{
Histo* h = dynamic_cast<Histo *>(o);
if (h) return ( new NOMAdapter_Histo(h) );
return ( new NObjMgrAdapter(o) );
}

/* --Methode-- */
string NOMAdapter_Histo::GetDataObjType()
{
HProf * hp = dynamic_cast<HProf *>(mHis);
if(hp) return("HProf "); else return("Histo ");
}

/* --Methode-- */
string NOMAdapter_Histo::GetInfoString(int lev)
{
  char buff[128];
  string rs;
  if (lev > 2) {
    sprintf(buff, "Histo: NBin=%d XMin=%lg XMax=%lg\n", mHis->NBins(), 
	  mHis->XMin(), mHis->XMax());
    rs += buff;
  }
  sprintf(buff, "Entries= %lg \n", mHis->NEntries());
  rs += buff;
  if (lev > 0) {
    sprintf(buff, "Overflow= %lg \n", mHis->NOver());
    rs += buff;
    sprintf(buff, "Underflow= %lg \n", mHis->NUnder());
    rs += buff;
  } 
  sprintf(buff, "Mean= %lg \n", mHis->Mean());
  rs += buff;
  sprintf(buff, "Sigma= %lg \n", mHis->Sigma());
  rs += buff;
  return rs;
}

/* --Methode-- */
AnyDataObj* NOMAdapter_Histo::CloneDataObj(bool /*share*/)
{
mHis->UpdateHisto();  // pour le cas ou c'est un HProf
HProf * hp = dynamic_cast<HProf *>(mHis);
if(hp==NULL) return( new Histo(*mHis) );
return( new HProf(*hp) );
}

/* --Methode-- */
void NOMAdapter_Histo::SavePPF(POutPersist& pos, string const & nom)
{
#ifdef SANS_EVOLPLANCK
// PEIDA-EROS L'histo est lui-meme PPersist
string tag = nom;  // A cause de const
mHis->Write(pos,0,tag);
#else
ObjFileIO<Histo> fio(mHis);
fio.Write(pos, nom);
#endif
}

/* --Methode-- */
void NOMAdapter_Histo::Print(ostream& os)
{
mHis->Print(60);
}

/* --Methode-- */
PIDrawer* NOMAdapter_Histo::GetDrawer(string & dopt)
{
if (typeid(*mHis) == typeid(HProf))  dopt = "fcirclemarker5 " + dopt;
else dopt = "thinline " + dopt;
PIHisto * pih = new PIHisto(mHis, false);
return( pih );
}

/* --Methode-- */
NTupleInterface* NOMAdapter_Histo::GetNTupleInterface(bool& adel)
{
adel = true;
return( new NTupInt_Histo(mHis) );
}

/* --Methode-- */
GeneralFitData* NOMAdapter_Histo::GetGeneralFitData(bool& adel
 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
 ,int i1,int i2,int j1,int j2)
{
adel = false;
if(!mHis) return(NULL);

int nx = mHis->NBins();
if(nx<=0) return(NULL);

i1 = (i1<0||i1>=nx)? 0: i1;
i2 = (i2<0||i2>=nx||i2<i1)? nx-1: i2;

GeneralFitData* mGData = new GeneralFitData(1,i2-i1+1,0);
adel = true;

for(int i=i1;i<=i2;i++) {
  double x = mHis->BinCenter(i);
  double f = (*mHis)(i);
  double e = (mHis->HasErrors())? mHis->Error(i) : 1.;
  e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
  mGData->AddData1(x,f,e);
}

return mGData;
}

AnyDataObj* NOMAdapter_Histo::FitResidusObj(GeneralFit& mfit)
{
Histo* h = NULL;
#ifdef SANS_EVOLPLANCK
h = mHis->FitResidus(mfit); 
#else
h = new Histo(ObjectFitter::FitResidus(*mHis,mfit));
#endif
return h;
}

AnyDataObj* NOMAdapter_Histo::FitFunctionObj(GeneralFit& mfit)
{
Histo* h = NULL;
#ifdef SANS_EVOLPLANCK
h = mHis->FitFunction(mfit); 
#else
h = new Histo(ObjectFitter::FitFunction(*mHis,mfit));
#endif
return h;
}

// -------------------------------------------------------------

/* --Methode-- */
NTupInt_Histo::NTupInt_Histo(Histo* h)
{
mHis = h;
}

/* --Methode-- */
NTupInt_Histo::~NTupInt_Histo()
{
}

/* --Methode-- */
uint_4 NTupInt_Histo::NbLines() const 
{
return(mHis->NBins());
}

/* --Methode-- */
uint_4 NTupInt_Histo::NbColumns() const
{
return(4);
}

/* --Methode-- */
r_8* NTupInt_Histo::GetLineD(int k) const
{
int i;
if ((k < 0) || (k >= mHis->NBins())) 
    for(i=0; i<4; i++)  mRet[i] = 0.;
else { 
  mRet[0] = k;  mRet[1] = mHis->BinCenter(k);
  mRet[2] = (*mHis)(k); mRet[3] = mHis->Error(k); 
  }
return(mRet);
}

/* --Methode-- */
string NTupInt_Histo::VarList_C(const char* nx) const
{
string nomx;
if (nx) nomx = nx;
else nomx = "_xh_";
string vardec = "double i,x,val,err; \n";
vardec += "i = " + nomx + "[0];  x = " + nomx + "[1]; \n";
vardec += "val = " + nomx + "[2];  err = " + nomx + "[3]; \n";
return(vardec);
}

//-------------------------------------------------------------------------
// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet Histo2D
//-------------------------------------------------------------------------


/* --Methode-- */
NOMAdapter_Histo2D::NOMAdapter_Histo2D(Histo2D* o)
  : NObjMgrAdapter(o)
{
mHis = o;
}

/* --Methode-- */
NOMAdapter_Histo2D::~NOMAdapter_Histo2D()
{
}

/* --Methode-- */
NObjMgrAdapter* NOMAdapter_Histo2D::Clone(AnyDataObj* o)
{
Histo2D* h = dynamic_cast<Histo2D *>(o);
if (h) return ( new NOMAdapter_Histo2D(h) );
return ( new NObjMgrAdapter(o) );
}

/* --Methode-- */
string NOMAdapter_Histo2D::GetDataObjType()
{
return ("Histo2D ");
}

/* --Methode-- */
AnyDataObj* NOMAdapter_Histo2D::CloneDataObj(bool /*share*/)
{
return ( new Histo2D(*mHis) );
}

/* --Methode-- */
void NOMAdapter_Histo2D::SavePPF(POutPersist& pos, string const & nom)
{
#ifdef SANS_EVOLPLANCK
// PEIDA-EROS L'histo est lui-meme PPersist
string tag = nom;  // A cause de const
mHis->Write(pos,0,tag);
#else
ObjFileIO<Histo2D> fio(mHis);
fio.Write(pos, nom);
#endif
}

/* --Methode-- */
void NOMAdapter_Histo2D::Print(ostream& os)
{
mHis->Print();
}

/* --Methode-- */
PIDrawer* NOMAdapter_Histo2D::GetDrawer(string & dopt)
{
dopt = "thinline " + dopt;
return( new PIHisto2D(mHis, false) );
}

/* --Methode-- */
P2DArrayAdapter* NOMAdapter_Histo2D::Get2DArray(string & dopt)
{
return (new POH2DAdapter(mHis, false) );
}

/* --Methode-- */
NTupleInterface* NOMAdapter_Histo2D::GetNTupleInterface(bool& adel)
{
adel = true;
return( new NTupInt_Histo2D(mHis) );
}


/* --Methode-- */
GeneralFitData* NOMAdapter_Histo2D::GetGeneralFitData(bool& adel
 ,GeneralFitData::FitErrType errtype,double errscale,double errmin
 ,int i1,int i2,int j1,int j2)
{
adel = false;
if(!mHis) return(NULL);

int nx = mHis->NBinX();
int ny = mHis->NBinY();
if(nx<=0 || ny<=0) return(NULL);

i1 = (i1<0||i1>=nx)? 0: i1;
i2 = (i2<0||i2>=nx||i2<i1)? nx-1: i2;
j1 = (j1<0||j1>=ny)? 0: j1;
j2 = (j2<0||j2>=ny||j2<j1)? ny-1: j2;

GeneralFitData* mGData = new GeneralFitData(2,(i2-i1+1)*(j2-j1+1),0);
adel = true;

for(int i=i1;i<=i2;i++) for(int j=j1;j<=j2;j++) {
  double x,y; mHis->BinCenter(i,j,x,y);
  double f = (*mHis)(i,j);
  double e = (mHis->HasErrors())? mHis->Error(i,j) : 1.;
  e = GeneralFitData::ComputeError(f,e,errtype,errscale,errmin);
  mGData->AddData2(x,y,f,e);
}

return mGData;
}

AnyDataObj* NOMAdapter_Histo2D::FitResidusObj(GeneralFit& mfit)
{
Histo2D* h2 = NULL;
#ifdef SANS_EVOLPLANCK
h2 = mHis->FitFunction(mfit);
#else
h2 = new Histo2D(ObjectFitter::FitResidus(*mHis,mfit));
#endif
return h2;
}

AnyDataObj* NOMAdapter_Histo2D::FitFunctionObj(GeneralFit& mfit)
{
Histo2D* h2 = NULL;
#ifdef SANS_EVOLPLANCK
h2 = mHis->FitFunction(mfit);
#else
h2 = new Histo2D(ObjectFitter::FitFunction(*mHis,mfit));
#endif
return h2;
}


// -------------------------------------------------------------

/* --Methode-- */
NTupInt_Histo2D::NTupInt_Histo2D(Histo2D* h)
{
mHis = h;
}

/* --Methode-- */
NTupInt_Histo2D::~NTupInt_Histo2D()
{
}

/* --Methode-- */
uint_4 NTupInt_Histo2D::NbLines() const 
{
return(mHis->NBinX()*mHis->NBinY());
}

/* --Methode-- */
uint_4 NTupInt_Histo2D::NbColumns() const 
{
return(6);
}

/* --Methode-- */
r_8* NTupInt_Histo2D::GetLineD(int n) const
{
int i,j;
r_8 f2,f3;
if ((n < 0) || (n >= mHis->NBinX()*mHis->NBinY())) 
    for(i=0; i<6; i++)  mRet[i] = 0.;
else { 
  i = n%mHis->NBinX();  j = n/mHis->NBinX(); 
  mRet[0] = i;  mRet[1] = j;
  mHis->BinCenter(i,j,f2,f3);
  mRet[2] = f2;  mRet[3] = f3;  
  mRet[4] =  (*mHis)(i,j);  mRet[5] = mHis->Error(i, j);
  }
return(mRet);
}

/* --Methode-- */
string NTupInt_Histo2D::VarList_C(const char* nx) const
{
string nomx;
if (nx) nomx = nx;
else nomx = "_xh_";
string vardec = "double i,j,x,y,val,err; \n";
vardec += "i = " + nomx + "[0];  j = " + nomx + "[1]; \n";
vardec += "x = " + nomx + "[2];  y = " + nomx + "[3]; \n";
vardec += "val = " + nomx + "[4];  err = " + nomx + "[5]; \n";
return(vardec);
}



//-------------------------------------------------------------------------
// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet NTuple
//-------------------------------------------------------------------------

/* --Methode-- */
NOMAdapter_NTuple::NOMAdapter_NTuple(NTuple* o)
  : NObjMgrAdapter(o)
{
mNt = o;
}

/* --Methode-- */
NOMAdapter_NTuple::~NOMAdapter_NTuple()
{
}

/* --Methode-- */
NObjMgrAdapter* NOMAdapter_NTuple::Clone(AnyDataObj* o)
{
NTuple* nt = dynamic_cast<NTuple *>(o);
if (nt) return ( new NOMAdapter_NTuple(nt) );
return ( new NObjMgrAdapter(o) );
}


/* --Methode-- */
string NOMAdapter_NTuple::GetDataObjType()
{
return ("NTuple ");
}

/* --Methode-- */
AnyDataObj* NOMAdapter_NTuple::CloneDataObj(bool /*share*/)
{
return ( new NTuple(*mNt) );
}

/* --Methode-- */
void NOMAdapter_NTuple::ReadFits(string const & flnm)
{
#ifdef SANS_EVOLPLANCK
  cerr << " NOMAdapter_NTuple::ReadFits() Non disponible !! " << endl;
#else
  FitsInFile fis(flnm);
  fis >> (*mNt);
#endif
}

/* --Methode-- */
void NOMAdapter_NTuple::SaveFits(string const & flnm)
{
#ifdef SANS_EVOLPLANCK
  cerr << " NOMAdapter_NTuple::SaveFits() Non disponible !! " << endl;
#else
  FitsOutFile fos(flnm);
  fos << (*mNt);

#endif
}


/* --Methode-- */
void NOMAdapter_NTuple::SavePPF(POutPersist& pos, string const & nom)
{
#ifdef SANS_EVOLPLANCK
// PEIDA-EROS L'histo est lui-meme PPersist
string tag = nom;  // A cause de const
mNt->Write(pos,0,tag);
#else
ObjFileIO<NTuple> fio(mNt);
fio.Write(pos, nom);
#endif
}

/* --Methode-- */
void NOMAdapter_NTuple::Print(ostream& os)
{
os << mNt->Info();
os << *(mNt);
}


/* --Methode-- */
NTupleInterface* NOMAdapter_NTuple::GetNTupleInterface(bool& adel)
{
adel = false;
return(mNt);
// return( new NTupInt_NTuple(mNt) );
}

//-------------------------------------------------------------------------
// Class Adaptateur d'objet (Pour NamedObjMgr) d'objet XNTuple
//-------------------------------------------------------------------------

/* --Methode-- */
NOMAdapter_XNTuple::NOMAdapter_XNTuple(XNTuple* o)
  : NObjMgrAdapter(o)
{
mNt = o;
}

/* --Methode-- */
NOMAdapter_XNTuple::~NOMAdapter_XNTuple()
{
}

/* --Methode-- */
NObjMgrAdapter* NOMAdapter_XNTuple::Clone(AnyDataObj* o)
{
XNTuple* nt = dynamic_cast<XNTuple *>(o);
if (nt) return ( new NOMAdapter_XNTuple(nt) );
return ( new NObjMgrAdapter(o) );
}

/* --Methode-- */
string NOMAdapter_XNTuple::GetDataObjType()
{
return ("XNTuple ");
}

/* --Methode-- */
AnyDataObj* NOMAdapter_XNTuple::CloneDataObj(bool /*share*/)
{
return ( new XNTuple(*mNt) );
}

/* --Methode-- */
void NOMAdapter_XNTuple::ReadFits(string const & flnm)
{
#ifdef SANS_EVOLPLANCK
  cerr << " NOMAdapter_XNTuple::ReadFits() Non disponible !! " << endl;
#else
  FitsInFile fis(flnm);
  fis >> (*mNt);
#endif
}

/* --Methode-- */
void NOMAdapter_XNTuple::SaveFits(string const & flnm)
{
#ifdef SANS_EVOLPLANCK
  cerr << " NOMAdapter_XNTuple::SaveFits() Non disponible !! " << endl;
#else
  FitsOutFile fos(flnm);
  fos << (*mNt);

#endif
}

/* --Methode-- */
void NOMAdapter_XNTuple::SavePPF(POutPersist& pos, string const & nom)
{
#ifdef SANS_EVOLPLANCK
// PEIDA-EROS L'histo est lui-meme PPersist
string tag = nom;  // A cause de const
mNt->Write(pos,0,tag);
#else
ObjFileIO<XNTuple> fio(mNt);
fio.Write(pos, nom);
#endif
}

/* --Methode-- */
void NOMAdapter_XNTuple::Print(ostream& os)
{
// os << mNt->Info();
mNt->Show(os);
}


/* --Methode-- */
NTupleInterface* NOMAdapter_XNTuple::GetNTupleInterface(bool& adel)
{
adel = false;
return(mNt);
}
