Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

FileUnisimLightToEuso - source file

// ESAF : Euso Simulation and Analysis Framework
// $Id: FileUnisimLightToEuso.cc,v 1.6 2005/05/08 22:28:22 thea Exp $
// M. Pallavicini created Jul,  2 2002

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

ClassImp(FileUnisimLightToEuso)

//_________________________________________________________________________________________ 
FileUnisimLightToEuso::FileUnisimLightToEuso() : LightToEuso("UNISIM_FILE"), fP(NULL), fTruth(0){
    //
    // Constructor
    //
    fName = "";
    fFirstEvent = 0;
    fCurrentEvent = 0;
}

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

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

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


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

    if ( Conf()->IsDefined( "FileUnisimLightToEuso.FileName" ) ) {
        fName      = Conf()->GetStr("FileUnisimLightToEuso.FileName");
        fFirstEvent = (Int_t)Conf()->GetNum("FileUnisimLightToEuso.FirstEvent") ;
    }
    else
        Msg(EsafMsg::Panic) << "Unknown input file name in FileUnisimLightToEuso" << MsgDispatch;

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

    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();

    while (1) {	
        fCurrentEvent++;  
        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());
                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;
        }
    }
}
 
//_________________________________________________________________________________________ 
 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 14-May-2005 21:31