#include "hrun.h"
#include "hades.h"
#include "hmessagemgr.h"
ClassImp(HParVersion)
ClassImp(HRun)
HParVersion::HParVersion(const Text_t* name) : TNamed(name,"version info"){
  
  rootVersion=0;
  for(Int_t i=0;i<3;i++) {inputVersions[i]=-1;}
}
HRun::HRun(const Text_t* name,const Text_t* refName)
              : TNamed(name,"run parameters") {
  
  parVersions=new TList();
  refRun=refName;
}
HRun::HRun(Int_t r,Int_t rr) {
  
  parVersions=new TList();
  Char_t name[255];
  sprintf(name,"%i",r);
  SetName(name);
  setRefRun(rr);
}
HRun::HRun(HRun &run):TNamed(run) {
  
  SetName(run.GetName());
  parVersions=new TList();
  TList* lv=run.getParVersions();
  TIter next(lv);
  HParVersion* pv;
  while ((pv=(HParVersion*)next())) {
    parVersions->Add(pv);
  }
  refRun=run.refRun;
}
HRun::~HRun() {
  
  if (parVersions) {
    parVersions->Delete();
    delete  parVersions;
    parVersions=0;
  }
}
void HRun::addParVersion(HParVersion *pv) {
  
  parVersions->Add(pv);
}
HParVersion* HRun::getParVersion(const Text_t* name) {
  
  
  return (HParVersion*)parVersions->FindObject(name);
}
void HRun::resetInputVersions() {
  TIter next(parVersions);
  HParVersion* v;
  while ((v=(HParVersion*)next())) {
    v->resetInputVersions();
  }
}
void HRun::resetOutputVersions() {
  TIter next(parVersions);
  HParVersion* v;
  while ((v=(HParVersion*)next())) {
    v->setRootVersion(0);
  }
}
void HRun::print() {
  
  
  gHades->getMsg()->info(10,HMessageMgr::DET_RUNTIMEDB,this->GetName(),"run: %d",getRunId());
  TIter next(parVersions);
  HParVersion* v;
  while ((v=(HParVersion*)next())) {
    gHades->getMsg()->info(10,HMessageMgr::DET_RUNTIMEDB,this->GetName(),"%-22s%10d%11d%9d", v->GetName(),v->getInputVersion(1),v->getInputVersion(2), v->getRootVersion());
  }
}
void HRun::write(fstream& fout) {
  
  fout<<"run: "<<GetName()<<'\n';
  HParVersion* v;
  TIter next(parVersions);
  while ((v=(HParVersion*)next())) {
    fout.setf(ios::left,ios::adjustfield);
    fout<<"  "<<setw(45)<<v->GetName();
    fout.setf(ios::right,ios::adjustfield);
    fout<<setw(11)<<v->getInputVersion(1)
        <<setw(11)<<v->getInputVersion(2)
        <<setw(11)<<v->getRootVersion()<<'\n';
  }
}