using namespace std;
#include "hwallcalpar.h"
#include "hades.h"
#include "hruntimedb.h"
#include "hspectrometer.h"
#include "hwalldetector.h"
#include "hpario.h"
#include "hdetpario.h"
#include <iostream>
ClassImp(HWallCalPar)
ClassImp(HWallCalParCell)
HWallCalPar::HWallCalPar(const Char_t* name,const Char_t* title,
                         const Char_t* context)
             : HParSet(name,title,context) {
  
  strcpy(detName,"Wall");
  HDetector* det= gHades->getSetup()->getDetector("Wall");
  Int_t nComp=det->getMaxComponents(); 
  array = new TObjArray(nComp);
  for(Int_t i=0; i<nComp; i++) array->AddAt(new HWallCalParCell(),i);
}
HWallCalPar::~HWallCalPar() {
  
  array->Delete();
  delete array;
}
Bool_t HWallCalPar::init(HParIo* inp,Int_t* set) {
  
  HDetParIo* input=inp->getDetParIo("HWallParIo");
  if (input) return (input->init(this,set));
  return kFALSE;
}
Int_t HWallCalPar::write(HParIo* output) {
  
  HDetParIo* out=output->getDetParIo("HWallParIo");
  if (out) return out->write(this);
  return -1;
}
void HWallCalPar::clear() {
  
  for(Int_t i=0;i<getSize();i++) (*this)[i].clear();
  status=kFALSE;
  resetInputVersions();
}
void HWallCalPar::printParam() {
  
  printf("Calibration parameters for the Wall detector\n");
  printf("cell TDC_K TDC_offset ADC_K ADC_offset\n");
  for(Int_t i=0;i<getSize();i++) {
    HWallCalParCell& cell=(*this)[i];
    printf("%i  %8.3f %9.3f %7.4f %7.4f %8.3f %9.3f \n",i,cell.getTDC_Slope(),cell.getTDC_Offset(),cell.getTDC_WalkCorr1(),cell.getTDC_WalkCorr2(),cell.getADC_Slope(),cell.getADC_Offset());
  }
}
void HWallCalPar::readline(const Char_t *buf, Int_t *set) {
  
  Int_t  cell;
  Float_t TDC_slope, TDC_offset, TDC_WalkCorr1, TDC_WalkCorr2, ADC_slope, ADC_offset;
  sscanf(buf,"%i%f%f%f%f%f%f", &cell, &TDC_slope, &TDC_offset, &TDC_WalkCorr1, &TDC_WalkCorr2, &ADC_slope, &ADC_offset);
  if (!set[0]) return;
  (*this)[cell].fill(TDC_slope,TDC_offset,TDC_WalkCorr1,TDC_WalkCorr2,ADC_slope,ADC_offset); 
  set[0]=999;
}
void HWallCalPar::putAsciiHeader(TString& header) {
  
  header=
    "# Calibration parameters for the WALL detector\n"
    "# Format:\n"
    "# cell  TDC_slope   TDC_offset   TDC_WalkCorr1   TDC_WalkCorr2   ADC_slope   ADC_offset\n";
}
Bool_t HWallCalPar::writeline(Char_t *buf,Int_t cell) {
  
  HWallCalParCell& c=(*this)[cell];
  sprintf(buf,"%i %8.3f %9.3f %7.4f %7.4f %8.3f %9.3f\n",
          cell, c.getTDC_Slope(),c.getTDC_Offset(),c.getTDC_WalkCorr1(),c.getTDC_WalkCorr2(),c.getADC_Slope(),c.getADC_Offset());
  return kTRUE;
}