Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

PhPToRootFileDetector - source file

// $Id: PhPToRootFileDetector.cc,v 1.8 2005/09/27 20:19:08 thea Exp $
// Author: Alessandro Thea   2005/02/13

/*****************************************************************************
 * ESAF: Euso Simulation and Analysis Framework                              *
 *                                                                           *
 *  Id: PhPToRootFileDetector                                                *
 *  Package: <packagename>                                                   *
 *  Coordinator: <coordinator>                                               *
 *                                                                           *
 *****************************************************************************/

//_____________________________________________________________________________
//
// PhPToRootFileDetector
//
// <extensive class description>
//
//   Config file parameters
//   ======================
//
//   <parameter name>: <parameter description>
//   -Valid options: <available options>
//

#include "PhPToRootFileDetector.hh"
#include "EEvent.hh"
#include "EPhoton.hh"
#include "ETruth.hh"
#include "DetectorGeometry.hh"
#include "Photon.hh"
#include "ParentPhoton.hh"
#include "PhotonsOnPupil.hh"

#include <TFile.h>
#include <TTree.h>
#include <TClonesArray.h>
#include <TSystem.h>

// DEBUG
// #include "stdio.h"
// DEBUG

ClassImp(PhPToRootFileDetector)

//_____________________________________________________________________________
 PhPToRootFileDetector::PhPToRootFileDetector() : fNumPhotons(0) {
    //
    // Constructor
    //

    fFileName = Conf()->GetStr("PhPToRootFileDetector.fFileName");

    fTruth = new ETruth();
    fPhotons = new TClonesArray("EPhoton",10000);
    fGeometry = new DetectorGeometry();

    Double_t radius = Conf()->GetNum("PhPToRootFileDetector.fRadius");
    Double_t fov =  Conf()->GetNum("PhPToRootFileDetector.fFoV");

    fGeometry->SetRadius(radius);
    fGeometry->SetFoV(fov);


    Initialize();
}

//_____________________________________________________________________________
 PhPToRootFileDetector::~PhPToRootFileDetector() {
    //
    // Destructor
    //

    Finalize();

    SafeDelete( fTruth );
    SafeDelete( fPhotons );
    SafeDelete( fGeometry );

}

//______________________________________________________________________________
 Telemetry* PhPToRootFileDetector::Get( PhotonsOnPupil* pupil ) {
    //
    // Saves photons on fFileName
    //

    if ( !pupil ) { 
        MsgForm(EsafMsg::Warning,"PhotonsOnPupil is NULL. Skipping.");
        return 0;
    }

    EEvent* ev = EEvent::GetCurrent();

    // save the truth in the php file
    if ( ev && ev->GetTruth() )
        ev->GetTruth()->Copy(*fTruth);
    
    // DEBUG
    // FILE* flog = fopen("php-in.log","w");
    // DEBUG
    fNumPhotons = 0;
    while( Photon* p = pupil->GetPhoton() ) {
        EPhoton* eph = new ((*fPhotons)[fNumPhotons++]) EPhoton();

        eph->SetType( p->parent->Type() );
        eph->SetTheta( p->dir.Theta() );
        eph->SetPhi( p->dir.Phi() );
        eph->SetLambda( p->wl );
        eph->SetTime( p->time );
        eph->SetPos( p->pos );
    // DEBUG
    //    fprintf(flog,"%d   %.3f   %.3f   %.3f   %.3f   %.3f   %.3f   %.3fn",
    //            p->parent->Type(), p->dir.Theta()/deg, p->dir.Phi()/deg, p->wl/nm, p->time/ns, 
    //            p->pos.X(), p->pos.Y(), p->pos.Z());
    // DEBUG
    }
    // DEBUG
    // fclose(flog);
    // DEBUG

    MsgForm(EsafMsg::Info,"%d photons read from PhotonOnPuil, %d saved on phproot file",
            fNumPhotons, fPhotons->GetEntries());
    fTree->Fill();
        
    return 0;
}

//______________________________________________________________________________
 void PhPToRootFileDetector::Initialize() {
    //
    // Open file, create the tree and set the branches
    //
    

    string ext = ".php.root";
    if (fFileName.find(ext) != (fFileName.size()-ext.size()))
        fFileName += ext;

    if ( !gSystem->AccessPathName(fFileName.c_str())  ) {

        MsgForm(EsafMsg::Warning,"Error opening file");
        MsgForm(EsafMsg::Warning,"Probably it already exists or there is no ");
        MsgForm(EsafMsg::Warning,"write access permission to this directory");


        //if (fFileName.find(ext) != (fFileName.size()-8))
        fFileName.resize(fFileName.size()-ext.size());

        time_t t = time(NULL);
        char buffer[64];
        strftime(buffer, 64,".%d-%b-%G-%Hh%Mm%Ss", localtime(&t));
        fFileName += buffer;
        fFileName += ext;

    }
    
    TDirectory* olddir = gDirectory;

    fFile = new TFile(fFileName.c_str(),"CREATE");
    fFile->cd();
    if (fFile->IsZombie()) 
        FatalError("Cannot open root file");

    MsgForm(EsafMsg::Info,"PhPRoot file %s successfully opened",fFileName.c_str());

    fTree = new TTree("PhPTree","Photons OnPupil");
    fTree->Branch("ETruth","ETruth",&fTruth);
    fTree->Branch("EPhotons","TClonesArray",&fPhotons);
    fTree->Branch("ENumPhotons",&fNumPhotons,"ENumPhotons/I");

    if ( olddir ) olddir->cd();
}

//______________________________________________________________________________
 void PhPToRootFileDetector::Finalize() {
    //
    // Write the tree on file and close the file
    //

    fFile->Write();
    fFile->Close();
    MsgForm(EsafMsg::Info,"%s closed.",fFile->GetName());

    SafeDelete(fFile);
    
}

//______________________________________________________________________________
 void PhPToRootFileDetector::Reset() {
    //
    //
    //
    
    fPhotons->Clear();
    fTruth->Clear();
    fNumPhotons = 0;
}
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