Universita' di GenovaINFN Sezione di Genova  
AIRWATCH / EUSO Genova

RecoModuleData - source file

// ESAF : Euso Simulation and Analysis Framework
// Reconstruction Module data container
// $Id: RecoModuleData.cc,v 1.14 2005/09/25 13:26:46 naumov Exp $
// Marco Pallavicini created Feb, 16 2004
// Description:
// Rather simple and crude data container
// A RecoModuleData can store data in the form of integers, doubles and
// objects of any kind
// Each data is identified with a name
// Name duplication is not allowed
// Objects are stored internally as pointer to void, therefore the user must
// know the type of object he is reading back and apply the right casting
//

#include <iostream>
#include <exception>
#include "RecoModuleData.hh"
#include "RecoModule.hh"

ClassImp(RecoModuleData)

// ctor
 RecoModuleData::RecoModuleData(RecoModule *p) : EsafMsgSource(), pModule(p)  {
}

// dtor
 RecoModuleData::~RecoModuleData() {
}

// returns true if a name is valid (not already existing)
 Bool_t RecoModuleData::CheckName(const string& name) {
    if ( (fIntMap.count(name) == 0)    &&
         (fDoubleMap.count(name) == 0) &&
         (fObjMap.count(name) == 0) )
        return true;
    return false;
}

// add an integer value to the map
// returns 0 if OK, 1 if value already existing
 Int_t RecoModuleData::Add(const string& name, const Int_t& val) {
    if ( CheckName(name) ) {
        fIntMap[name]=val;
        return 0;
    }
    Msg(EsafMsg::Warning) << "Duplicate name definition in module "+GetOwner()->GetName() << MsgDispatch;
    return 1;
}

// add an integer value to the map
// returns 0 if OK, 1 if value already existing
 Int_t RecoModuleData::Add(const string& name, const Double_t& val) {
    if ( CheckName(name) ) {
        fDoubleMap[name]=val;
        return 0;
    }
    Msg(EsafMsg::Warning) << "Duplicate name definition in module "+GetOwner()->GetName() << MsgDispatch;
    return 1;
}

// add an integer value to the map
// returns 0 if OK, 1 if value already existing
 Int_t RecoModuleData::Add(const string& name, void* p) {
    if ( CheckName(name) ) {
        fObjMap[name]=p;
        return 0;
    }
    Msg(EsafMsg::Warning) << "Duplicate name definition in module "+GetOwner()->GetName() << MsgDispatch;
    return 1;
}

// get methods
// get an integer parameter
 Int_t RecoModuleData::GetInt(const string& name) const {
    if ( fIntMap.count(name) == 1 )
        return fIntMap.find( name )->second ;
    if ( fIntMap.count(name) > 1 )
        throw runtime_error("Duplicate integer parameter definition in module "+
        GetOwner()->GetName());
    Msg(EsafMsg::Warning) << name << " is NOT existing integer parameter in module "+GetOwner()->GetName() << MsgDispatch;
    return -999999;
}

// get a Double_t parameter
 Double_t RecoModuleData::GetDouble(const string& name) const {
    if ( fDoubleMap.count(name) == 1 )
        return fDoubleMap.find( name )->second;
    if ( fDoubleMap.count(name) > 1 )
        throw runtime_error("Duplicate Double_t parameter definition in module"+
        GetOwner()->GetName());
    Msg(EsafMsg::Warning) << name << " is NOT existing double parameter in module "+GetOwner()->GetName() << MsgDispatch;
    return -999997.;
}

// get any object
// the user must know the object type and cast it properly
// this must be used to store:
// single pointer to objects
// pointers to collection of objects as vector<> or map<>
// pointers to TClonesArrays
// any other type of object ALWAYS as pointers
 void* RecoModuleData::GetObj(const string& name) const {
   
    if ( fObjMap.count( name ) == 1 )
        return fObjMap.find( name )->second;
    if ( fObjMap.count( name ) > 1 )
        throw runtime_error("Duplicate object definition in module "+GetOwner()->GetName());
    Msg(EsafMsg::Warning) << name << " is NOT existing object in module "+GetOwner()->GetName() << MsgDispatch;
    return NULL;
}

// remove an object
// object is NOT deleted
 Bool_t RecoModuleData::RemoveObj(const string& name) {
    if ( fObjMap.count( name ) == 1 ) {
        fObjMap.erase( name );
        return true;
    }
    throw runtime_error("Removing Unknown object in module "+GetOwner()->GetName());
    return false;
}

// clean maps
 void RecoModuleData::CleanMaps() {    

    Msg(EsafMsg::Debug) << "Cleaning " << fIntMap.size() << " Integers and " << fDoubleMap.size() << " Doubles " << " in the Module " << GetOwner()->GetName() << " map" << MsgDispatch;
    fIntMap.clear();
    fDoubleMap.clear();
    if ( fObjMap.size() > 0 ) {
        Msg(EsafMsg::Warning) << "The module "  << GetOwner()->GetName() << " the object map has still the following unremoved objects: " << MsgDispatch;
        map<string,void*>::const_iterator it;
        for( it = fObjMap.begin(); it != fObjMap.end(); it++) {
            Msg(EsafMsg::Warning) <<  "Name= "<<it->first<<"   Address="<<(Int_t)it->second<<MsgDispatch;
        }
		Msg(EsafMsg::Panic) <<  "Error in fObjMap handling" << MsgDispatch;
    }
    fObjMap.clear();
}
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