// 14-jan-2003 
#include "AliProductionFile.h"
#include <assert.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <TROOT.h>
#include <TFile.h>
#include "AliRun.h" 

ClassImp(AliProductionFile)
  // How to use?
  /* 
     gSystem->Load("~pavlinov/ALICE/PaiClass/AliPAIlib.so");
     prodFile = new AliProductionFile;
     prodFile->Init(mode);     // mode corresponds the some variant of production 
     for(Int_t i=0; i<n; i++){ // n - number of events 
        Int_t nparticles = prodFile->GetNextEvent();
	if (nparticles <= 0) break; // last event
     }
     prodFile->Print(); 
  */

AliProductionFile::AliProductionFile(const char *name,const char *tit,
const bool putToBrowser) : TNamed(name,tit)
{
   if(putToBrowser) gROOT->GetListOfBrowsables()->Add(this);

   fDirBase   = "/auto/alice/proposal/production2/";
   fDir       = "";
   fMode      = 0;
   fNFile     = fNFileGood = fNFileBad = 0;
   fNFileMax  = 1;

   fNeventAll = fNCurrent = 0;
   fNevInFile.Set(fNFileMax);

   fDebug = 1;
}

AliProductionFile::~AliProductionFile() 
{
  if(fFile) {
    if (gAlice) {
       delete gAlice;  // don't delete after closing file
       gAlice = 0;
    }
    fFile->Close();
    delete fFile;
  }
}

void AliProductionFile::Init(const int mode)
{
  fMode  = mode;
  fNFile = fNFileGood = fNFileBad = 0;
  Int_t p[4]={50,75,100,200};
  Int_t p2[5]={30,50,75,100,200};
  switch (mode) {
    // -----
  case 1:
    fDir = fDirBase + "EMCAL_5655_21/bkg/kBackground2000/";
    fNFileMax = 100;
    break;
  case 2:
    fDir = fDirBase + "EMCAL_5655_21/bkg/kBackground4000/";
    fNFileMax = 250;
    break;
  case 3:
    fDir = fDirBase + "EMCAL_5655_21/bkg/kBackground8000/";
    fNFileMax = 1000;
    break;
  case 4:
    fDir = fDirBase + "EMCAL_5655_21/hijing/kHijingCentral/";
    fNFileMax = 1000;
    break;
    // -- Pythia BEAUTY
  case 11: // bad
  case 12:
  case 13:
  case 14: // bad
    fDir  = fDirBase + "EMCAL_5655_21/fragmfcn/kPythiaBeauty";
    fDir += p[mode-11];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia CHARM
  case 15:
  case 16: // bad - probably not closed
  case 17: // bad - probably not closed
  case 18: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_5655_21/fragmfcn/kPythiaCharm";
    fDir += p[mode-15];
    fDir += "/";
    fNFileMax = 1;
    // -- Pythia GLUON
    break;
  case 19:
  case 20:
  case 21:
  case 22: // bad
    fDir  = fDirBase + "EMCAL_5655_21/fragmfcn/kPythiaGluon";
    fDir += p[mode-19];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia RadBeauty
  case 23: // bad
  case 24: // bad
  case 25: // bad
  case 26: // bad
    fDir  = fDirBase + "EMCAL_5655_21/fragmfcn/kPythiaRadBeauty";
    fDir += p[mode-23];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia RadCharm
  case 27: // bad
  case 28: // bad
  case 29: 
  case 30: // bad
    fDir  = fDirBase + "EMCAL_5655_21/fragmfcn/kPythiaRadCharm";
    fDir += p[mode-27];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia RadCharm
  case 31: // bad
  case 32: // bad
  case 33: 
  case 34: // bad
    fDir  = fDirBase + "EMCAL_5655_21/fragmfcn/kPythiaRadCharm";
    fDir += p[mode-31];
    fDir += "/";
    fNFileMax = 1;
    break;
    // =============================================================
    // Pythia jets
    // =============================================================
  case 41:
  case 42:
  case 43: 
  case 44:
  case 45:
    fDir  = fDirBase + "EMCAL_5655_21/jets/kPythiaJet";
    fDir += p2[mode-41];
    fDir += "/";
    fNFileMax = 10;
    break;
  case 46:
  case 47:
  case 48: 
  case 49:
  case 50:
    fDir  = fDirBase + "EMCAL_5655_21/jets/kPythiaRadJet";
    fDir += p2[mode-46];
    fDir += "/";
    fNFileMax = 10;
    break;
    // #############################################################
    // #        GEOMETRY EMCAL_5664_21                             #
    // #############################################################
  case 51:
    fDir = fDirBase + "EMCAL_6564_21/bkg/kBackground2000/";
    fNFileMax = 100;
    break;
  case 52:
    fDir = fDirBase + "EMCAL_6564_21/bkg/kBackground4000/";
    fNFileMax = 250;
  case 53:
    fDir = fDirBase + "EMCAL_6564_21/bkg/kBackground8000/";
    fNFileMax = 1000;
    break;
    // -- Pythia BEAUTY
  case 61:
  case 62: // bad - probably not closed
  case 63: // bad - probably not closed
  case 64: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_6564_21/fragmfcn/kPythiaBeauty";
    fDir += p[mode-61];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia CHARM
  case 65: // bad - probably not closed
  case 66: // bad - probably not closed
  case 67:
  case 68: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_6564_21/fragmfcn/kPythiaCharm";
    fDir += p[mode-65];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia GLUON
  case 69:
  case 70:
  case 71: // bad - probably not closed
  case 72: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_6564_21/fragmfcn/kPythiaGluon";
    fDir += p[mode-69];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia RadBeauty
  case 73: 
  case 74: // bad - probably not closed
  case 75: // bad - probably not closed
  case 76: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_6564_21/fragmfcn/kPythiaRadBeauty";
    fDir += p[mode-73];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia RadCharm
  case 77: // bad - probably not closed
  case 78: // bad - probably not closed
  case 79: // bad - probably not closed
  case 80: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_6564_21/fragmfcn/kPythiaRadCharm";
    fDir += p[mode-77];
    fDir += "/";
    fNFileMax = 1;
    break;
    // -- Pythia RadCharm
  case 81: // bad - probably not closed
  case 82: // bad - probably not closed
  case 83: // bad - probably not closed
  case 84: // bad - probably not closed
    fDir  = fDirBase + "EMCAL_6564_21/fragmfcn/kPythiaRadCharm";
    fDir += p[mode-81];
    fDir += "/";
    fNFileMax = 1;
    break;
    // =============================================================
    // Pythia jets
    // =============================================================
  case 91:
  case 92:
  case 93: 
  case 94:
  case 95:
    fDir  = fDirBase + "EMCAL_6564_21/jets/kPythiaJet";
    fDir += p2[mode-91];
    fDir += "/";
    fNFileMax = 10;
    break;
  case 96: // fisrt file is broken(skipped)
  case 97:
  case 98: 
  case 99:
  case 100:
    fDir  = fDirBase + "EMCAL_6564_21/jets/kPythiaRadJet";
    fDir += p2[mode-96];
    fDir += "/";
    fNFileMax = 10;
    break;
    // #############################################################
    // #        GEOMETRY EMCAL_5664_21_HC                          #
    // #############################################################
  case 101: // something with geometry
    fDir = fDirBase + "EMCAL_6564_21_HC/bkg/kBackground2000/";
    fNFileMax = 100;
    break;
  case 102:
    fDir = fDirBase + "EMCAL_6564_21_HC/bkg/kBackground4000/";
    fNFileMax = 250;
  case 103:
    fDir = fDirBase + "EMCAL_6564_21_HC/bkg/kBackground8000/";
    fNFileMax = 1000;
    break;
  default:
    printf("<I> AliProductionFile::Init mode %i is wrong \n", mode);
    assert(0);
  }

  fNeventAll = fNCurrent = 0;
  fNevInFile.Set(fNFileMax);

  gAlice = GetNextgAlice();
}

AliRun *AliProductionFile::GetNextgAlice()
{// gAlice - one per file
  if(fNFile>=1 && fFile) {
     if (gAlice) {
       /* hang on this , I dont know why ?? 24-jun-2003
        delete gAlice;  // don't delete after closing file
        gAlice = 0;
       */
     }
     fFile->Close();
     fNevInFile[fNFile-1] = fNCurrent;
     fNCurrent            = 0;
  }

 NEXT:
  if(fNFile == fNFileMax) {
     printf(" fNFile == fNFileMax : %i -> %i\n", fNFile, fNFileMax);
     return 0;
  }

  fNFile++;
  char nam[300];
  sprintf(nam,"%s%5.5i/galice.root", fDir.Data(), fNFile);

  fFile = new TFile(nam, "READ");
  if(!fFile->IsOpen()) {
    // Usually directory does not exist
     fNFileBad++;
     printf("File %s is not open : fNFileBad %i \n", nam, fNFileBad);
     goto NEXT;
  }

  AliRun *run = (AliRun*)(fFile->Get("gAlice"));
  fMaxEventPerFile = run->GetEventsPerRun();
  if(run && fMaxEventPerFile>0) fNFileGood++;
  else    fNFileBad++;

  printf(" File %s is open \n Events per file %i current event %i\n", 
  nam, fMaxEventPerFile, fNCurrent);
  printf(" #file %i #good %i #bad %i ", fNFile, fNFileGood, fNFileBad);
  printf(" gAlice  %x\n", gAlice);

  return  run;
}

Int_t AliProductionFile::GetNextEvent()
{
   Int_t nparticles = 0;
 NEXT:
   nparticles = 0;
   if(gAlice) {
     // Last event in current file ??
      if (fNCurrent == fMaxEventPerFile) {
	 printf(" <I> Get next gAlice due to last event in file \n"); 
         gAlice = GetNextgAlice();
         if(gAlice == 0) return 0; // LAST file -- ?? 
      }
      nparticles = gAlice->GetEvent(fNCurrent);
      if(fDebug>1) printf(" read events %7i cur.event %i \n", fNeventAll, fNCurrent);
      fNCurrent++;
      fNeventAll++;
      fNevInFile[fNFile-1] = fNCurrent;
   }
   if(nparticles <=0 && fNFile < fNFileMax) {
      gAlice = GetNextgAlice();
      goto NEXT;
   }
   return nparticles;
}

Int_t AliProductionFile::ReadFile(Int_t opt)
{
   Int_t i=0;
   while(1){ // n - number of events 
      Int_t nparticles = GetNextEvent();
      printf("Event %5i nparticles %i \n", i, nparticles);
      if (nparticles <= 0) break; // last event
      i++;
   }
   Print();
   return 1;
}

void AliProductionFile::Print()
{
  for(Int_t i=0; i<fNFile; i++){
    printf("%3i file has %10i events \n", i, fNevInFile[i]);   
  }
  printf(" Mode %10i  Number of file in directory %10i(fNFileMax)\n", fMode, fNFileMax); 
  printf(" Directory %s \n", fDir.Data()); 
  printf(" Read      files %i\n", fNFile);
  printf(" Read GOOD files %i\n", fNFileGood);
  printf(" Read BAD  files %i\n == \n", fNFileBad);
  printf(" Read events     %i\n", fNeventAll);
}

void AliProductionFile::Test(int mode, int n)
{ // see rt.C also 
  Init(mode);
  for(Int_t i=0; i<n; i++){ // n - number of events 
     Int_t nparticles = GetNextEvent();
     if (nparticles <= 0) break; // last event
  }
  Print(); 
}

//AliProductionFile::

/* 17-jun-2003 - all hijing statistics; reading time ~ 34 minutes
 Mode          4  Number of file in directory       1000(fNFileMax)
 Directory /auto/alice/proposal/production2/EMCAL_5655_21/hijing/kHijingCentral/
 Read      files 1000
 Read GOOD files 963
 Read BAD  files 37
*/