00001 #include "UTIL/LCRelationNavigator.h"
00002
00003 #include <algorithm>
00004 #include <cassert>
00005 #include "IMPL/LCCollectionVec.h"
00006 #include "IMPL/LCFlagImpl.h"
00007 #include "IMPL/LCRelationImpl.h"
00008 #include "EVENT/LCIO.h"
00009
00010 #define RELATIONFROMTYPESTR "FromType"
00011 #define RELATIONTOTYPESTR "ToType"
00012
00013 using namespace EVENT ;
00014 using namespace IMPL ;
00015
00016 namespace UTIL{
00017
00018
00019 LCRelationNavigator::LCRelationNavigator( const EVENT::LCCollection* col ) :
00020
00021 _from( col->getParameters().getStringVal( RELATIONFROMTYPESTR ) ) ,
00022 _to( col->getParameters().getStringVal( RELATIONTOTYPESTR ) ) {
00023
00024 initialize(col) ;
00025 }
00026
00027 void LCRelationNavigator::initialize( const LCCollection* col ) {
00028
00029
00030 if( col->getTypeName() != LCIO::LCRELATION ) {
00031 return ;
00032 }
00033
00034 int n = col->getNumberOfElements() ;
00035
00036 for(int i=0; i < n; i++){
00037
00038 LCRelation* rel = dynamic_cast<LCRelation*>( col->getElementAt(i) ) ;
00039
00040 addRelation( rel->getFrom() , rel->getTo() , rel->getWeight() ) ;
00041
00042 }
00043 }
00044
00045
00046 const std::string & LCRelationNavigator::getFromType() const { return _from ; }
00047 const std::string & LCRelationNavigator::getToType() const { return _to ; }
00048
00049 const EVENT::LCObjectVec&
00050 LCRelationNavigator::getRelatedToObjects(EVENT::LCObject * from) const{
00051
00052 return _map[ from ].first ;
00053 }
00054
00055 const EVENT::LCObjectVec&
00056 LCRelationNavigator::getRelatedFromObjects(EVENT::LCObject * to) const{
00057
00058 return _rMap[ to ].first ;
00059 }
00060
00061 const EVENT::FloatVec & LCRelationNavigator::getRelatedToWeights(EVENT::LCObject * from) const {
00062
00063 return _map[ from ].second ;
00064 }
00065
00066 const EVENT::FloatVec & LCRelationNavigator::getRelatedFromWeights(EVENT::LCObject * to) const {
00067
00068 return _rMap[ to ].second ;
00069 }
00070
00071 void LCRelationNavigator::addRelation(EVENT::LCObject * from,
00072 EVENT::LCObject * to,
00073 float weight) {
00074 addRelation( from , to , weight , _map ) ;
00075 addRelation( to , from, weight , _rMap ) ;
00076 }
00077
00078 void LCRelationNavigator::addRelation(EVENT::LCObject * from,
00079 EVENT::LCObject * to,
00080 float weight,
00081 RelMap& map) {
00082
00083 LCObjectVec& vTo = map[ from ].first ;
00084 FloatVec & vWgt = map[ from ].second ;
00085
00086 bool isNewObject = true ;
00087 int n = vTo.size() ;
00088 for(int i=0; i<n ; i++){
00089 if( to == vTo[i] ){
00090 vWgt[i] += weight ;
00091 isNewObject = false ;
00092 break ;
00093 }
00094 }
00095 if( isNewObject ){
00096 vTo.push_back( to ) ;
00097 vWgt.push_back( weight) ;
00098 }
00099 }
00100
00101
00102
00103 void LCRelationNavigator::removeRelation(EVENT::LCObject * from, EVENT::LCObject * to) {
00104 removeRelation( from, to, _map ) ;
00105 removeRelation( to, from, _rMap ) ;
00106 }
00107
00108
00109
00110 void LCRelationNavigator::removeRelation(EVENT::LCObject * from, EVENT::LCObject * to, RelMap& map ){
00111
00112 RelMap::iterator iter = map.find( from ) ;
00113 if( iter != map.end() ) {
00114
00115
00116 LCObjectVec& vTo = iter->second.first ;
00117 FloatVec & vWgt = iter->second.second ;
00118
00119
00120
00121
00122 LCObjectVec::iterator iTo = find( vTo.begin(), vTo.end() , to ) ;
00123
00124 if( iTo != vTo.end() ){
00125
00126 FloatVec::iterator iWgt = vWgt.begin() ;
00127
00128 advance( iWgt , distance( vTo.begin() , iTo ) ) ;
00129
00130 vTo.erase( iTo ) ;
00131 vWgt.erase( iWgt ) ;
00132
00133 if( vTo.empty() ) {
00134 assert( vWgt.empty() ) ;
00135 map.erase( iter ) ;
00136 }
00137 }
00138
00139 }
00140 }
00141
00142 EVENT::LCCollection * LCRelationNavigator::createLCCollection() {
00143
00144 LCCollectionVec* col = new LCCollectionVec( LCIO::LCRELATION ) ;
00145
00146
00147 col->parameters().setValue( RELATIONFROMTYPESTR , getFromType() ) ;
00148 col->parameters().setValue( RELATIONTOTYPESTR , getToType() ) ;
00149
00150
00151 bool storeWeights = false ;
00152 for(RelMap::iterator iter = _map.begin() ;
00153 iter != _map.end() ; iter++ ) {
00154
00155 LCObject* from = iter->first ;
00156 LCObjectVec& vTo = iter->second.first ;
00157 FloatVec & vWgt = iter->second.second ;
00158
00159 unsigned int n = vTo.size() ;
00160 assert( n == vWgt.size() ) ;
00161
00162 for( unsigned int i=0 ; i<n ; i++ ){
00163
00164 col->addElement( new LCRelationImpl( from , vTo[i] , vWgt[i] ) ) ;
00165 if( vWgt[i] != 1.0f ) storeWeights = true ;
00166 }
00167 }
00168 if( storeWeights ) {
00169 LCFlagImpl flag(0) ;
00170 flag.setBit( LCIO::LCREL_WEIGHTED ) ;
00171 col->setFlag( flag.getFlag() ) ;
00172 }
00173
00174
00175 return col ;
00176 }
00177 }