#include "TMHistogrammingList.h"

Bool_t TMHistogrammingList::instanceFlag = kFALSE;
TMHistogrammingList* TMHistogrammingList::single = NULL;
//___________________________________________________________________________________
TMHistogrammingList* TMHistogrammingList::Instance() {
 if(!instanceFlag) {
   instanceFlag = kTRUE;
   single = new TMHistogrammingList();
   return single;
 }
 else {
   return single;
 }
}
//___________________________________________________________________________________
TMHistogrammingList::TMHistogrammingList() :
  fList()
{}
//___________________________________________________________________________________
TMHistogrammingList::~TMHistogrammingList() {}
//___________________________________________________________________________________
void TMHistogrammingList::Clear() {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    delete (*it)->GetHistogrammingObj();
    delete (*it);
  }
  fList.clear();
}
//___________________________________________________________________________________
void TMHistogrammingList::SaveEntries(ofstream& sFile) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    sFile << (*it)->GetId() << "\t";
    sFile << (*it)->GetPlotNr() << "\t";
    sFile << (*it)->GetHistogrammingObj()->GetPaused() << "\t";
    sFile << (*it)->GetHistogrammingObj()->GetAutoReset() << "\t";
    sFile << (*it)->GetHistogrammingObj()->GetTriggerMask(0) << "\t";
    sFile << (*it)->GetHistogrammingObj()->GetTriggerMask(1) << "\t";
    sFile << "\n";
  }
}
//___________________________________________________________________________________
Bool_t TMHistogrammingList::GetAutoReset(UInt_t id, UInt_t plotNr) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      return (*it)->GetHistogrammingObj()->GetAutoReset();
    }
  }
  return kFALSE;
}
//___________________________________________________________________________________
void TMHistogrammingList::SetAutoReset(UInt_t id, UInt_t plotNr, Bool_t autoreset) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      (*it)->GetHistogrammingObj()->SetAutoReset(autoreset);
    }
  }
}
//___________________________________________________________________________________
Bool_t TMHistogrammingList::GetPaused(UInt_t id, UInt_t plotNr) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      return (*it)->GetHistogrammingObj()->GetPaused();
    }
  }
  return kFALSE;
}
//___________________________________________________________________________________
void TMHistogrammingList::SetPaused(UInt_t id, UInt_t plotNr, Bool_t paused) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      (*it)->GetHistogrammingObj()->SetPaused(paused);
    }
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::SetTriggerMask(UInt_t id, UInt_t plotNr, ULong_t word0, ULong_t word1) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      (*it)->GetHistogrammingObj()->SetTriggerMask(word0,word1);
    }
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::SetTriggerMaskAll(ULong_t word0, ULong_t word1) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->SetTriggerMask(word0,word1);
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::AddHistogrammingObj(TMHistogramming *disp, UInt_t id, UInt_t plotNr) {
  fList.push_back(new TMHistogrammingListNode(disp,id,plotNr));
}
//___________________________________________________________________________________
void TMHistogrammingList::RemoveHistogrammingObj(UInt_t id, UInt_t plotNr) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      delete (*it)->GetHistogrammingObj();
      delete (*it);
      fList.erase(it);
      break;
    }
    else if ((*it)->GetId()==id && (*it)->GetPlotNr()>plotNr) {
      (*it)->SetPlotNr((*it)->GetPlotNr()-1);
    }
  }
}
//___________________________________________________________________________________
TMHistogramming* TMHistogrammingList::GetHistogrammingObj(UInt_t id, UInt_t plotNr) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      return (*it)->GetHistogrammingObj();
    }
  }
  return NULL;
}
//___________________________________________________________________________________
void TMHistogrammingList::StartEvent(eventHeaderStruct *event, UInt_t globalEventNumber) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->StartEvent(event,globalEventNumber);
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::EndEvent(AliITSRawStreamSPD *str, UInt_t globalEventNumber) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->EndEvent(str,globalEventNumber);
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::Reset() {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->Reset();
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::ResetAuto() {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->ResetAuto();
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::ResetID(UInt_t id, UInt_t plotNr) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    if ((*it)->GetId()==id && (*it)->GetPlotNr()==plotNr) {
      (*it)->GetHistogrammingObj()->Reset();
      return;
    }
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::ProcessPixel(UInt_t eq, UInt_t hs, UInt_t chip, UInt_t col, UInt_t row) {
std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->ProcessPixel(eq,hs,chip,col,row);
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::ProcessCluster(Float_t x, Float_t y, Float_t z, UInt_t module, UInt_t size, UInt_t widz, UInt_t widphi, UInt_t type) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->ProcessCluster(x,y,z,module,size,widz,widphi,type);
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::ProcessClusters(HenrikClusterFinderSPD* clFinder) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->ProcessClusters(clFinder);
  }
}
//___________________________________________________________________________________
void TMHistogrammingList::ProcessEvent(eventHeaderStruct *event) {
  std::list<TMHistogrammingListNode*>::iterator it;
  for (it=fList.begin(); it!=fList.end(); it++) {
    (*it)->GetHistogrammingObj()->ProcessEvent(event);
  }
}


