// ESAF : Euso Simulation and Analysis Framework
// $Id: EAtmosphere.cc,v 1.11 2005/10/03 15:30:30 moreggia Exp $
// Alessandro Thea created May, 28 2004
#include "EAtmosphere.hh"
#include "ESinglePhoton.hh"
#include "EBunchPhotons.hh"
ClassImp(EAtmosphere)
TClonesArray* EAtmosphere::fgBunches = NULL;
TClonesArray* EAtmosphere::fgSingles = NULL;
EAtmosphere* EAtmosphere::fgCurrent = NULL;
    
//_____________________________________________________________________________
 EAtmosphere::EAtmosphere() : TObject() {
    // ctor
    if ( fgBunches == 0 )
        fgBunches = new TClonesArray("EBunchPhotons",20000);
    if ( fgSingles == 0 )
        fgSingles = new TClonesArray("ESinglePhoton",20000);
    fBunches = fgBunches;
    fSingles = fgSingles;
    fCopy       = kFALSE;
    fNumBunches = 0;
    fNumSingles = 0;
}
//_____________________________________________________________________________
 EAtmosphere::EAtmosphere(const EAtmosphere& other) : TObject() {
    // copy ctor
    other.Copy( *this );
}
//_____________________________________________________________________________
 EAtmosphere::~EAtmosphere() {
    // 
    // dtor
    //
    
    Clear();
    if ( IsCopy() ) ClearCopy();
    if ( GetCurrent() == this ) SetCurrent( 0 ); 
}
//_____________________________________________________________________________
 void EAtmosphere::Copy( TObject& other) const {
    // copy 
    EAtmosphere& atm = ( EAtmosphere&) other;
    // copy atmosphere variables
    atm.fNumBunches = fNumBunches;
    atm.fNumSingles = fNumSingles;
    atm.fMaxScatOrder = fMaxScatOrder;
    // create TClonesArray 
    atm.fBunches = (TClonesArray*)fBunches->Clone();
    atm.fSingles = (TClonesArray*)fSingles->Clone();
}
//_____________________________________________________________________________
 void EAtmosphere::Clear( Option_t* opt ) {
    // clear this obj
    if ( fBunches ) fBunches->Delete();
    if ( fSingles ) fSingles->Delete();
    fNumBunches = 0;
    fNumSingles = 0;
}
   
//______________________________________________________________________________
 void EAtmosphere::ClearCopy() {
  // delete TClonesArray in case of copied events
  delete fBunches; 
  fBunches = 0;
  delete fSingles; 
  fSingles = 0;
}
//______________________________________________________________________________
 void EAtmosphere::AddTestPhoton() {
    ESinglePhoton* ph = new ((*fSingles)[fNumSingles++]) ESinglePhoton;
    if ( fBunches->At(fNumBunches-1) ) {
        EBunchPhotons* bunch = (EBunchPhotons*)fBunches->At(fNumBunches-1);
        bunch->AddSingleRef(ph);
    }
}
//______________________________________________________________________________
 void EAtmosphere::AddTestBunch( Int_t nph) {
    new ((*fBunches)[fNumBunches++]) EBunchPhotons;
    for(Int_t i(0); i<nph; i++){
       AddTestPhoton(); 
    }
    
}