// ESAF : Euso Simulation and Analysis Framework
// $Id: OADBConverter.cc,v 1.8 2005/05/17 21:48:30 thea Exp $
// A.Thea, J.Watts created Mar, 22 2004

#include "OADBConverter.hh"
#include "OADBHeader.hh"
#include "OADBPhotons.hh"
#include <string> 
#include <vector> 
#include <Riostream.h>

ClassImp(OADBConverter)
    
// ctor
 OADBConverter::OADBConverter( OADBTree *t ) : fPhotons(NULL) {
    // init tree
    fTree = t;
    fHeader = &(t->GetHeader());
    fTree->SetBranchAddress("photons", &fPhotons);
    
    // length of the keywords
    fKeyLength = 16;

    fBinFile = new fstream;
    
}

// dtor
 OADBConverter::~OADBConverter() {
    delete fBinFile;
}

// convert oatree into a binary ESAF-readable file
 void OADBConverter::SaveBinary( const char *path ) {

    //ofstream fBinFile;
    fBinFile->open( path, ios::binary
    if ( !(fBinFile->is_open()) )
        cout << Form("SaveBinary: Unable to open %s",path) << endl;

    WriteHeader();
    
    Int_t iEntry(0);

    InsertBegin(kPhotons);
    for( Int_t iWl(0); iWl < fHeader->GetNumWavelengths(); iWl++ ) {
        // be sure to have photons loaded before getting the wlid
        fTree->GetEntry(iEntry);
        InsertBegin(kWavelength);
        Int_t wlId = fPhotons->GetWavelengthId(); 
        fBinFile->write((char*)&wlId,sizeof(Int_t));

        for( Int_t iAng(0); iAng < fHeader->GetNumAngles(); iAng++ ){
           fTree->GetEntry(iEntry++);
           WritePhotons(); 
        } 
        InsertEnd(kWavelength);
    }
    InsertEnd(kPhotons);
    
    fBinFile->close();

}

// checks binary
 void OADBConverter::CheckBinary( const char *path ) {
    fBinFile->open( path, ios::in

    Read();

    fBinFile->close();
}

 string OADBConverter::KeyWord( OAEKeyType type ) {

    string buffer;
    switch ( type ) {
        case kBegin:
            buffer = "BEGIN"; 
            break;
        case kEnd:
            buffer = "END"; 
            break;
        case kHeader:
            buffer = "HEADER"; 
            break;
        case kPhotons:
            buffer = "PHOTONS"; 
            break;
        case kWavelength:
            buffer = "WAVELENGTH"; 
            break;
        case kAngle:
            buffer = "ANGLE"; 
            break;
        default:
            buffer.resize(0);
            break;
    }
    return buffer.c_str();
}

// insert begin keyword
 void OADBConverter::InsertBegin( OAEKeyType type ) {
    string dummy = "BEGIN";
    dummy+=KeyWord(type);
    dummy.resize(fKeyLength);
    fBinFile->write(dummy.c_str(), fKeyLength);
}

// insert end keyword
 void OADBConverter::InsertEnd( OAEKeyType type ) {
    string dummy = "END";
    dummy+=KeyWord(type);
    dummy.resize(fKeyLength);
    fBinFile->write(dummy.c_str(), fKeyLength);
}

// append header to fBinFile
 void OADBConverter::WriteHeader() {

    Int_t num;
    string comment;
    comment  = "# ESAF - Euso Simulation and Analysis Frameworkn";
    comment += "# OpticsAnalysisModule Photon Database Filen";
    comment += "# The header contains the following informations:n";
    comment += "# TODO";
    size_t commentSize = comment.size();

    // insert step, radius, entrance Z, exit Z
    size_t dataSize = 10;
    Float_t data[dataSize];
    data[0]=fHeader->GetXYStep();
    data[1]=fHeader->GetOpticsRadius();
    data[2]=fHeader->GetEntranceDiscRadius();
    data[3]=fHeader->GetFirstLensBottom();
    data[4]=fHeader->GetFirstLensTop();
    data[5]=fHeader->GetSecondLensBottom();
    data[6]=fHeader->GetSecondLensTop();
    data[7]=fHeader->GetEntranceZ();
    data[8]=fHeader->GetExitZ();
    data[9]=fHeader->GetPosZ();
    
    size_t headSize =  sizeof(char)*commentSize
                      +sizeof(size_t)
                      +sizeof(Int_t)*(5+fHeader->GetNumRows())
                      +sizeof(Float_t)*(dataSize+fHeader->GetNumWavelengths()
                           +fHeader->GetNumAngles());

    InsertBegin( kHeader );
    fBinFile->write((char*)&headSize, sizeof(size_t));
    fBinFile->write((char*)&commentSize, sizeof(size_t));
    fBinFile->write(comment.c_str(), comment.size());
   
    fBinFile->write((char*)&dataSize, sizeof(size_t));
    fBinFile->write((char*)data, sizeof(Float_t)*dataSize);

    // insert Wavelengths 
    num = fHeader->GetNumWavelengths();
    fBinFile->write((char*)&num, sizeof(Int_t));
    fBinFile->write((char*)fHeader->GetWavelengths(), sizeof(Float_t)*num);
    // inser Angles
    num = fHeader->GetNumAngles();
    fBinFile->write((char*)&num, sizeof(Int_t));
    fBinFile->write((char*)fHeader->GetAngles(), sizeof(Float_t)*num);

    // insert Positions
    num=fHeader->GetNumPos();
    fBinFile->write((char*)&num, sizeof(Int_t));
    num=fHeader->GetNumRows();
    fBinFile->write((char*)&num, sizeof(Int_t));
    fBinFile->write((char*)fHeader->GetRows(), sizeof(Int_t)*num);

    InsertEnd( kHeader );

}

// append a bunch of photons to fBinFile
 void OADBConverter::WritePhotons() {
    if (!fPhotons) {
        cout << "WritePhotons: invalid pointer" << endl;
        return;
    }
    if ( fPhotons->GetSize() != fHeader->GetNumPos() ) {
        cout << "WritePhotons: mismatch between header and fPhotons" << endl;
        return;
    }
    InsertBegin( kAngle );
    Int_t angleId = fPhotons->GetAngleId(); 
    Float_t ph[5];

    fBinFile->write((char*)&angleId,sizeof(Int_t));
    for(Int_t i(0); i<fPhotons->GetSize(); i++) {
        ph[0]=fPhotons->GetThroughput()[i];
        ph[1]=fPhotons->GetXOut()[i];
        ph[2]=fPhotons->GetYOut()[i];
        ph[3]=fPhotons->GetThetaOut()[i];
        ph[4]=fPhotons->GetPhiOut()[i];

        fBinFile->write((char*)ph,sizeof(Float_t)*5);
    }

    InsertEnd( kAngle );
}

// reads the header of the binary file and dispays its contents
 void OADBConverter::Read() {
    Float_t *values(NULL);
    char buffer[512];
    
    fBinFile->read(buffer, fKeyLength);

    if ( (KeyWord(kBegin)+KeyWord(kHeader)) == buffer)
        cout << "Header found" << endl;
    
    size_t headSize;
    fBinFile->read((char*)&headSize, sizeof(size_t));
    cout << "header " << headSize << " bytes long" << endl;
   
    // check the end of the header to be where it should
    streampos sp = fBinFile->tellg();
    fBinFile->seekg(headSize, ios::cur

    fBinFile->read(buffer, fKeyLength);
    if ( (KeyWord(kEnd)+KeyWord(kHeader)) == buffer)
        cout << "End Header found" << endl;
    else {
        cout << "End Header missing or misplaced. Exiting..." << endl;
        return;    
    }
    
    fBinFile->seekg(sp);

    // read comment
    size_t commentSize;
    char* comment;

    fBinFile->read((char*)&commentSize, sizeof(size_t));
    cout << "comment size: " << commentSize << endl;
    comment = new char[commentSize];
    fBinFile->read((char*)comment, commentSize);
    cout << comment << endl;
    delete [] comment;

    Int_t numData;
    fBinFile->read((char*)&numData, sizeof(Int_t));
    values = new Float_t[numData];
    fBinFile->read((char*)values, sizeof(Float_t)*numData);
    cout << "XYStep = " <<  values[0] << endl;
    cout << "OpticsRadius = " << values[1] << endl;
    cout << "EntranceDiscRadius = " << values[2] << endl;
    cout << "FirstLensBottom = " << values[3] << endl;
    cout << "FirstLensTop = " << values[4] << endl;
    cout << "SecondLensBottom = " << values[5] << endl;
    cout << "SecondLensTop = " << values[6] << endl;
    cout << "EntranceZ = " << values[7] << endl;
    cout << "ExitZ = " << values[8] << endl;
    cout << "PosZ = " << values[9] << endl;
    delete [] values;

    // read wls
    Int_t numWls;
    fBinFile->read((char*)&numWls, sizeof(Int_t));
    cout << numWls << " wls found" << endl;

    values = new Float_t[numWls];
    fBinFile->read((char*)values, sizeof(Float_t)*numWls);
    for(Int_t i(0); i<numWls; i++) 
        cout << i << "  " << values[i] << endl;
    delete [] values;
    
    // read angles
    Int_t numAngles;
    fBinFile->read((char*)&numAngles, sizeof(Int_t));
    cout << numAngles << " angles found" << endl;

    values = new Float_t[numAngles];
    fBinFile->read((char*)values, sizeof(Float_t)*numAngles);
    for(Int_t i(0); i<numAngles; i++) 
        cout << i << "  " << values[i] << endl;
    delete [] values;

    // positions
    Int_t numPos;
    fBinFile->read((char*)&numPos, sizeof(Int_t));
    cout << numPos << " positions found" << endl;

    Int_t rows;
    Int_t *cols(NULL);
    fBinFile->read((char*)&rows, sizeof(Int_t));
    cout << rows << " rows found" << endl;

    Int_t posCheck(0);
    cols = new Int_t[rows];
    fBinFile->read((char*)cols, sizeof(Float_t)*rows);
    for(Int_t i(0); i<rows; i++) {
        cout << i << "  " << cols[i] << endl;
        posCheck+=cols[i];
    }
    if ( numPos != posCheck ) {
        cout << "mismatch between number of columns and total " << flush;
        cout << "number of positions in the header. Exiting..." << endl; 
    }

    fBinFile->read(buffer, fKeyLength);
    if ( (KeyWord(kEnd)+KeyWord(kHeader)) == buffer)
        cout << "Header closed" << endl;


    fBinFile->read(buffer, fKeyLength);
    if ( (KeyWord(kBegin)+KeyWord(kPhotons)) == buffer)
        cout << "Photon list found" << endl;
    else {
        cout << "Photon missing or misplaced. Exiting" << endl;
        return;
    }

    // calculate the length of the photons' list
    size_t angleSize = sizeof(char)*(2*fKeyLength)+sizeof(Int_t)+sizeof(Float_t)*(5*numPos);
    size_t wlsize = sizeof(char)*(2*fKeyLength)+sizeof(Int_t)+angleSize*numAngles;
    size_t phSize = wlsize*numWls;

    cout << "Calculated list size " << phSize << endl;
    
    // check the end of the list to be where it should
    sp = fBinFile->tellg();
    fBinFile->seekg(phSize, ios::cur

    fBinFile->read(buffer, fKeyLength);
    if ( (KeyWord(kEnd)+KeyWord(kPhotons)) == buffer)
        cout << "End Photons found" << endl;
    else {
        cout << "End Photons missing or misplaced. Exiting..." << endl;
        return;    
    }
    fBinFile->seekg(sp);
    
    // checking all the key positions
    vector< vector<streampos> > keyMap;
    for ( Int_t iWl(0); iWl < numWls; iWl++) {
        // check the wl key
        fBinFile->read(buffer, fKeyLength);
        if ( (KeyWord(kBegin)+KeyWord(kWavelength)) != buffer){
            cout << buffer << endl;
            cout << "Wavelength keyword " << iWl << " not found. Exiting" << endl;   
            return;
        }
        
        // check wl id
        Int_t wlId;
        fBinFile->read((char*)&wlId, sizeof(Int_t));
        if ( wlId != iWl){
            cout << "Wavelength id mismatch: wlId = " << wlId << "   iWl = " << iWl << endl;   
            return;
        }
       
        cout << " wlId = " << wlId << endl; 
        vector<streampos> dummy;
        for ( Int_t iAng(0); iAng < numAngles; iAng++) {
            // check the angle key
            fBinFile->read(buffer, fKeyLength);
            if ( (KeyWord(kBegin)+KeyWord(kAngle)) != buffer) {
                cout << "Angle keyword " << iAng << " not found. Exiting" << endl;   
                return;
            }

            // check angle id 
            Int_t angleId;
            fBinFile->read((char*)&angleId, sizeof(Int_t));
            if ( angleId != iAng){
                cout << " id mismatch: angleId = " << angleId << "   iAng = " << iAng << endl;   
                return;
            }
            
            cout << " angleId = " << angleId << endl; 
            // keep the pos of the first ph
            streampos cur = fBinFile->tellg();
            
            // move at the end
            fBinFile->seekg(sizeof(Float_t)*5*numPos,ios::cur
            
            // check the end key
            fBinFile->read(buffer, fKeyLength);
            if ( (KeyWord(kEnd)+KeyWord(kAngle)) != buffer) {
                cout << buffer << endl;
                cout << "End Angle keyword " << iAng << " not found. Exiting" << endl;   
                return;
            }

            // save the streamer position of the first ph
            dummy.push_back(cur);
        }
        
        // check the end key
        fBinFile->read(buffer, fKeyLength);
        if ( (KeyWord(kEnd)+KeyWord(kWavelength)) != buffer) {
            cout << "End Wavelength keyword " << iWl << " not found. Exiting" << endl;   
            return;
        }
        
        keyMap.push_back(dummy); 
    }
    delete [] cols;
}


ROOT page - Class index - Class Hierarchy - Top of the page

This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.