using namespace std;
#include "hshowergeantreader.h"
#include "hgeantshower.h"
#include "hgeantmaxtrk.h"
#include "showerdef.h"
#include "hdebug.h"
#include "hades.h"
#include "hevent.h"
#include "hcategory.h"
#include "hdatasource.h"
#include "TDirectory.h"
#include <iostream> 
#include <iomanip>
ClassImp(HShowerGeantReader)
HShowerGeantReader::HShowerGeantReader(void) {
  fEventId = 0;
}
HShowerGeantReader::~HShowerGeantReader(void) {
}
Bool_t HShowerGeantReader::init(void) {
  fGeantShowCat = (HMatrixCategory*)(gHades->getCurrentEvent()
                          ->getCategory(catShowerGeantRaw));  
  if (!fGeantShowCat) {
     if (!fInputFile) {
        t = NULL;
        return kFALSE;   
     }
     if (fInputFile->cd("dirSimul")) { 
        fGeantShowCat = (HMatrixCategory*)(gDirectory->Get("HGeantShower"));
        fInputFile->cd("..");        
     } else {
        Int_t ini[2] = {6,MAXTRKSHOW};      
        fGeantShowCat = new HMatrixCategory("HGeantShower",2,ini,1.0);
     }
     gHades->getCurrentEvent()->
               addCategory(catShowerGeantRaw,fGeantShowCat,"Simul");
     printf("\n%s(%d,%d) matrix category created for HGeant input\n",
              fGeantShowCat->getClassName(),
              fGeantShowCat->getSize(0), fGeantShowCat->getSize(1));
  }
  if (!fInputFile) {
     t = NULL;
     return kTRUE;   
  }
  if ((t=(TTree*)fInputFile->Get("T")) == NULL) return kFALSE;
  t->SetBranchAddress("HGeantShower",&fGeantShowCat); 
  t->SetBranchStatus("HGeantShower",kTRUE);
  fGeantShowCat->activateBranch(t,2);
  
  if (gHades->getOutputFile() != NULL) gHades->getOutputFile()->cd(); 
  return kTRUE;
}
Bool_t HShowerGeantReader::execute(void) {
  if(t) {   
     if(t->GetEntry(fEventId)==0) return kFALSE;  
  }
  fEventId++;
  return kTRUE;  
}