Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

FileUnisimLightToEuso - source file

// ESAF : Euso Simulation and Analysis Framework
// $Id: FileUnisimLightToEuso.cc,v 1.11 2005/10/02 14:17:55 thea Exp $
// M. Pallavicini created Jul,  2 2002

#include "FileUnisimLightToEuso.hh"
#include "EEventTruthAdder.hh"
#include "DetectorGeometry.hh"
#include <zlib.h>

using namespace sou;

ClassImp(FileUnisimLightToEuso)

//_________________________________________________________________________________________ 
 FileUnisimLightToEuso::FileUnisimLightToEuso() : LightToEuso("UNISIM_FILE"), fP(NULL), fTruth(0){
    //
    // Constructor
    //
    fName = "";
    fFirstEvent = 0;
    fCurrentEvent = 0;
    fPhotons.SetZdisk( Conf()->GetNum("FileUnisimLightToEuso.fZdisk") );
}

//_________________________________________________________________________________________
 FileUnisimLightToEuso::~FileUnisimLightToEuso() {
    // 
    // Destructor
    // 
}

//_________________________________________________________________________________________    
 void FileUnisimLightToEuso::ReplaceInputFile(const char* fn) {

    if ( fP )
        FatalError("FileUnisimLightToEuso: Cannot change name when file is already open!");
    if (!fn) {
        MsgForm(EsafMsg::Warning,"FileUnisimLightToEuso: NULL file name ignored");
        return;
    }
    Conf()->ReplaceStr("FileUnisimLightToEuso.FileName",fn);
    Msg(EsafMsg::Info) << "Input file name changed to " << fn << MsgDispatch;
}


//_________________________________________________________________________________________
 void FileUnisimLightToEuso::Open() {
    //
    // Open file
    //

    fName      = Conf()->GetStr("FileUnisimLightToEuso.fFileName");
    fFirstEvent = (Int_t)Conf()->GetNum("FileUnisimLightToEuso.fFirstEvent") ;

    if ( fFirstEvent < 0 ) {
        Msg(EsafMsg::Warning) << "fFirstEvent < 0 in cfg file. Setting to 0." << MsgDispatch;
        fFirstEvent = 0;
    }

    fP = (FILE*)gzopen( fName.c_str(), "rb" );

    if( fP == NULL ) 
        Msg(EsafMsg::Panic) << "Can't open file " << fName << MsgDispatch;
    Msg(EsafMsg::Info) << "File: " << fName << " successfully opened " << MsgDispatch;
}

//_________________________________________________________________________________________
 PhotonsOnPupil *FileUnisimLightToEuso::Get(const DetectorGeometry* dg) {
    // 
    // Returns the list of photons for the pupil
    //
    if ( !fP )
        Open();    

    EEvent* event = EEvent::GetCurrent();

    // set the target's size
    fPhotons.SetRmaxDisk( dg->GetRadius() );

    while (1) {	
        if (fCurrentEvent >= fFirstEvent)	    
            if ( fPhotons.Load( fP ) ) {
                MCTruth *t = GetTruth();
                if (event) {
                    EEventTruthAdder a( t );
                    event->Fill( a );
                }
                MsgForm(EsafMsg::Info,"Unisim Event %d: %d photons loaded:",
                        fCurrentEvent, fPhotons.GetNphotons());
                MsgForm(EsafMsg::Info,"    Energy %.2E MeV, Theta = %2f deg, Phi = %.2f deg",
                        t->GetEnergy()/MeV, t->GetTheta()/deg, t->GetPhi()/deg);
                MsgForm(EsafMsg::Info,"    at (%.2f,%.2f,%.2f) km",
                        t->GetInitPos().X()/km,t->GetInitPos().Y()/km,t->GetInitPos().Z());
                fCurrentEvent++;  	
                return &fPhotons;
            }
            else {
                Msg(EsafMsg::Info) << "FileUnisimLightToEuso: End of file " << fName << " reached" << MsgDispatch;
                return (UnisimPhotonsOnPupil*)0;
            }
        else {

            if (!fPhotons.SkipEvent( fP )) {
                Msg(EsafMsg::Info) << "FileUnisimLightToEuso: End of file " << fName << " reached" << MsgDispatch;
                return (UnisimPhotonsOnPupil*)0;
            }
            fCurrentEvent++;
        }

    }
}
 
//_________________________________________________________________________________________ 
 MCTruth* FileUnisimLightToEuso::GetTruth(){
#ifdef debug
    cout << fPhotons.Truth()->Dump() << endl;
#endif 
    return fPhotons.Truth();
}
About Us | EUSO Official Website | Web pages created by Roberto Pesce and Alessandro Thea - Last Update Wed Nov 16 16:57:39 2005 Wed Nov 16 16:29:22 2005