Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

SimpleTelemetry - source file

// class SimpleTelemetry
// very simple implementation of Telemetry
// it just writes data onto a ASCII file
// M. Pallavicini - created
//

#include "SimpleTelemetry.hh"
#include "MacroCellData.hh"
#include "MacroCell.hh"
#include "MacroCellHit.hh"
#include "MCTruth.hh"

ClassImp(SimpleTelemetry)

//______________________________________________________________________________
SimpleTelemetry::SimpleTelemetry() : 
   Telemetry(), fTextDump("Empty"), fp(NULL)
{
    // 
    // constructor
    //
    SetState( kFALSE );
}

//______________________________________________________________________________
SimpleTelemetry::~SimpleTelemetry() {
    // 
    // delete all MacroCellData objects in the vector
    //
    if ( IsOpen() )
        Close();
    Clear();
} 

//______________________________________________________________________________
 void SimpleTelemetry::Clear() {
    //
    // Delete current event
    //
    
    for( unsigned int i=0; i < fData.size(); i++) {
        MacroCellData* p = fData[i];
        if ( p )
            delete p;
    }
    
    fData.clear();
}

//______________________________________________________________________________
 void SimpleTelemetry::Add( MacroCellData* pData ) {
    //
    // add data coming from a MacroCell
    //
    fData.push_back( pData );
}

//______________________________________________________________________________
 void SimpleTelemetry::SimulateTCU() {
    // 
    // Simulate TCU: trigger and on-board data handling
    // does nothing in this implementation
    //
    Msg(EsafMsg::Info) << fData.size() << " MacroCellData collected." << MsgDispatch;
    Msg(EsafMsg::Info) << "TCU simulation done" << MsgDispatch;
    return;
}

//______________________________________________________________________________
 Bool_t SimpleTelemetry::Open( const char* fname ) {
    //
    // Open the output file
    //
    if ( IsOpen() )
        return kFALSE;

    fFileName = fname;

    // check if a file with this name already exists
    // in this case, change file name
    if (fFileName.find(".dat.gz") != (fFileName.size()-7))
    fFileName += ".dat.gz";

    fp = gzopen(fFileName.c_str(),"rt");
    if ( fp ) {
        fFileName.resize(fFileName.size()-7);
        gzclose( fp );
        char new_file_name[500];
        char temp_s[100];
        time_t tt = time(NULL);
        strftime(temp_s,99,"%d-%b-%G:%H:%M:%S",localtime(&tt));
        sprintf(new_file_name,"%s.%s",fFileName.c_str(),temp_s);
        fFileName = new_file_name;
    }
    fFileName += ".dat.gz";
    fp = gzopen(fFileName.c_str(),"wt+");

    if ( fp == NULL ) {
        FatalError("Error opening Telemetry file");
    }
    Msg(EsafMsg::Info) << "SimpleTelemetry: " <<  fFileName << " telemetry file opened" << MsgDispatch;

    SetState( kTRUE );

    return kTRUE;
}

//______________________________________________________________________________
 Bool_t SimpleTelemetry::Close() {
    // 
    // Close the file if open
    //
    
    Msg(EsafMsg::Info)<< "Closing telemetry file" << MsgDispatch;

    if ( !IsOpen() )
        return kFALSE;
    
    gzclose(fp);
  
    SetState( kFALSE );
  
    return kTRUE;
}

//______________________________________________________________________________
 Bool_t SimpleTelemetry::Write() {
    // 
    // Write data on file
    //
    
    // if file is not open, open it
    if ( !IsOpen() ) 
        return kFALSE;
   
 
    // write header data
    WriteHeader();
    
    // write data
    WriteData();
    
    // clean memory
    Clear();
    
    return kTRUE;
}

//______________________________________________________________________________
 string& SimpleTelemetry::Dump() {
    //
    // Dump on a string a returns it the same string is used at each call
    // 
    
    fTextDump = "This is a dummy eventn";
    return fTextDump;
}

//______________________________________________________________________________
 void SimpleTelemetry::WriteHeader() {
    // 
    // Write header
    //

    // run number
    //gzprintf(fp,"fRunNumber = %dn",gEusoApplication->GetRunNumber());
    // run date
    //gzprintf(fp,"RunDate = %sn",EusoTimeOrbit::Get()->GetRunDate().c_str());

    // general data
    // FIXME: mostly dummy or hard wired
    // FIXME: need to recover infos from right places; to be done
//      if ( GetTruth()) {
//          gzprintf(fp,"EventGenerator = %sn",
//  			GetTruth()->GetLightToEuso()->GetName().c_str());
    if ( !IsOpen() ) {
      gzprintf(fp,"GenerationTree = 000000n");
      gzprintf(fp,"ShowerDumpLevel = 0n");
      gzprintf(fp,"EnergySlope = 0n");
      gzprintf(fp,"EnergyRangeLow = 0n");
      gzprintf(fp,"EnergyRangeHigh = 0n");
      gzprintf(fp,"ThetaRangeLow = 0n");
      gzprintf(fp,"ThetaRangeHigh = 0n");
      gzprintf(fp,"PhiRangeLow = 0n");
      gzprintf(fp,"PhiRangeHigh = 6.28n");
      gzprintf(fp,"ElectromagneticFlag = 1n");
      gzprintf(fp,"HadronEnergyCutOff = 0n");
      gzprintf(fp,"MuonEnergyCutOff = 0n");
      gzprintf(fp,"ElectronEnergyCutOff = 0n");
      gzprintf(fp,"PhotonEnergyCutOff = 0n");
      gzprintf(fp,"ThinHybridLevel = 1n");
      gzprintf(fp,"EusoAltitude = 430000n");
      gzprintf(fp,"EntrancePupilDiameter = 2n");
      gzprintf(fp,"FocalNumber = 0n");
      gzprintf(fp,"EusoPixelSize = 2n");
      gzprintf(fp,"PixelDigitalThreshold = 6n");
      gzprintf(fp,"TriggerThreshold = 2n");
    }
}

//______________________________________________________________________________
 void SimpleTelemetry::WriteData() {
    // 
    // Write data
    // 

    if ( !IsOpen() ) return;

    // number of macrocells that gave signal
    gzprintf(fp,"NumMacrocells = %dn",NumCell());
    // total number of macrocell hits
    int hits=0;
    for( int iCell=0; iCell < NumCell(); iCell++) {
        MacroCellData *pData = fData[iCell];
        if ( pData ) {
            hits += pData->NumMacroCellHits();   
        }
    }
    gzprintf(fp,"NumMacroCellHits = %dn",hits);

    // macrocell hits
    // write a table with the follwing data:
    //    hit,cell,gtu,row,col,counts,theta,phi 
    int hit_counter=0;
    for( int iCell=0; iCell < NumCell(); iCell++) {
        MacroCellData *pD = fData[iCell];
        if ( pD ) {
            for(int iGtu = pD->GtuStart(); iGtu < pD->GtuEnd(); iGtu++) {
                if ( pD->Hits( iGtu ) ) {
                    for( unsigned int iH=0; iH < pD->Hits(iGtu)->size(); iH++) {
                        MacroCellHit *pH = pD->Hit( iGtu, iH );
                        if ( pH ) {
                            hit_counter++;
                            if ( iGtu != pH->Gtu()) {
                                cerr << "n\n******** GTU Mapping Error !!!n\n";
                            }
                            gzprintf(fp,"%dt%dt%dt%dt%dt%dt%ft%fn",
                                    hit_counter, pD->Cell()->Id(), iGtu,  
                                    pH->Row(), pH->Col(), pH->Hits(),
                                    pH->GetThetaFOV(), pH->GetPhiFOV());	
                        }
                    }
                }
            }
        }
    }
}
About Us | EUSO Official Website | Web pages created by Roberto Pesce and Alessandro Thea - Last Update 14-May-2005 21:31