LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LCRelationNavigator.cc
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <cassert>
5 #include "IMPL/LCCollectionVec.h"
6 #include "IMPL/LCFlagImpl.h"
7 #include "IMPL/LCRelationImpl.h"
8 #include "EVENT/LCIO.h"
9 
10 #define RELATIONFROMTYPESTR "FromType"
11 #define RELATIONTOTYPESTR "ToType"
12 
13 using namespace EVENT ;
14 using namespace IMPL ;
15 
16 namespace UTIL{
17 
18 
19  LCRelationNavigator::LCRelationNavigator( const EVENT::LCCollection* col ) :
20 
21  _from( col->getParameters().getStringVal( RELATIONFROMTYPESTR ) ) ,
22  _to( col->getParameters().getStringVal( RELATIONTOTYPESTR ) ) {
23 
24  initialize(col) ;
25  }
26 
28 
29 
30  if( col->getTypeName() != LCIO::LCRELATION ) {
31  return ;
32  }
33 
34  int n = col->getNumberOfElements() ;
35 
36  for(int i=0; i < n; i++){
37 
38  LCRelation* rel = dynamic_cast<LCRelation*>( col->getElementAt(i) ) ;
39 
40  addRelation( rel->getFrom() , rel->getTo() , rel->getWeight() ) ;
41 
42  }
43  }
44 
45 
46  const std::string & LCRelationNavigator::getFromType() const { return _from ; }
47  const std::string & LCRelationNavigator::getToType() const { return _to ; }
48 
49  const EVENT::LCObjectVec&
51 
52  return _map[ from ].first ;
53  }
54 
55  const EVENT::LCObjectVec&
57 
58  return _rMap[ to ].first ;
59  }
60 
62 
63  return _map[ from ].second ;
64  }
65 
67 
68  return _rMap[ to ].second ;
69  }
70 
72  EVENT::LCObject * to,
73  float weight) {
74  addRelation( from , to , weight , _map ) ;
75  addRelation( to , from, weight , _rMap ) ;
76  }
77 
79  EVENT::LCObject * to,
80  float weight,
81  RelMap& map) {
82 
83  LCObjectVec& vTo = map[ from ].first ;
84  FloatVec & vWgt = map[ from ].second ;
85 
86  bool isNewObject = true ;
87  int n = vTo.size() ;
88  for(int i=0; i<n ; i++){
89  if( to == vTo[i] ){
90  vWgt[i] += weight ;
91  isNewObject = false ;
92  break ;
93  }
94  }
95  if( isNewObject ){
96  vTo.push_back( to ) ;
97  vWgt.push_back( weight) ;
98  }
99  }
100 
101 
102 
104  removeRelation( from, to, _map ) ;
105  removeRelation( to, from, _rMap ) ;
106  }
107 
108 
109 
111 
112  RelMap::iterator iter = map.find( from ) ;
113  if( iter != map.end() ) {
114 
115 
116  LCObjectVec& vTo = iter->second.first ;
117  FloatVec & vWgt = iter->second.second ;
118 
119  // doesn't work as we need to remove/erase the corresponding weight as well ...
120  // vTo.erase( remove(vTo.begin(),vTo.end(), to ), vTo.end()).
121 
122  LCObjectVec::iterator iTo = find( vTo.begin(), vTo.end() , to ) ;
123 
124  if( iTo != vTo.end() ){
125 
126  FloatVec::iterator iWgt = vWgt.begin() ;
127 
128  advance( iWgt , distance( vTo.begin() , iTo ) ) ;
129 
130  vTo.erase( iTo ) ;
131  vWgt.erase( iWgt ) ;
132 
133  if( vTo.empty() ) { // remove empty relation vectors
134  assert( vWgt.empty() ) ;
135  map.erase( iter ) ;
136  }
137  }
138 
139  }
140  }
141 
143 
144  LCCollectionVec* col = new LCCollectionVec( LCIO::LCRELATION ) ;
145 
146 
149 
150 
151  bool storeWeights = false ;
152  for(RelMap::iterator iter = _map.begin() ;
153  iter != _map.end() ; iter++ ) {
154 
155  LCObject* from = iter->first ;
156  LCObjectVec& vTo = iter->second.first ;
157  FloatVec & vWgt = iter->second.second ;
158 
159  unsigned int n = vTo.size() ;
160  assert( n == vWgt.size() ) ;
161 
162  for( unsigned int i=0 ; i<n ; i++ ){
163 
164  col->addElement( new LCRelationImpl( from , vTo[i] , vWgt[i] ) ) ;
165  if( vWgt[i] != 1.0f ) storeWeights = true ;
166  }
167  }
168  if( storeWeights ) {
169  LCFlagImpl flag(0) ;
170  flag.setBit( LCIO::LCREL_WEIGHTED ) ;
171  col->setFlag( flag.getFlag() ) ;
172  }
173 
174 
175  return col ;
176  }
177 }
The generic object that is held in an LCCollection.
Definition: LCObject.h:30
Implementation of the LCRelation.
virtual const EVENT::FloatVec & getRelatedToWeights(EVENT::LCObject *from) const
The weights of the relations returned by a call to getRelatedToObjects(from).
T empty(T...args)
virtual EVENT::LCParameters & parameters()
Parameters defined for this run.
virtual const EVENT::LCObjectVec & getRelatedFromObjects(EVENT::LCObject *to) const
All from-objects related to the given object ( the inverse relationship).
Implementation of the LCCollection using (inheriting from) an STL vector of LCObjects.
virtual LCObject * getTo() const =0
The &#39;to&#39; object of the given relation.
T end(T...args)
virtual void removeRelation(EVENT::LCObject *from, EVENT::LCObject *to)
Remove a given relation.
void setFlag(int flag)
Sets the flag word for this collection.
virtual LCObject * getElementAt(int index) const =0
Returns pointer to element at index - no range check, use getNumberOfEntries().
STL class.
virtual float getWeight() const =0
The weight of the given relation - only if collection flag bit LCIO::LCREL_WEIGHTED is set...
T push_back(T...args)
virtual int getFlag() const
Returns the flag word.
Definition: LCFlagImpl.cc:15
virtual const EVENT::LCObjectVec & getRelatedToObjects(EVENT::LCObject *from) const
All objects that the given from-object is related to.
T erase(T...args)
virtual EVENT::LCCollection * createLCCollection()
Remove a given relation.
Implementation of helper class to create and interpret the 32-bit flag word in LCCollections.
Definition: LCFlagImpl.h:15
virtual const std::string & getTypeName() const =0
Returns the type name of the collection - valid names are defined in LCIO.
virtual void addElement(EVENT::LCObject *obj)
Adds the given element to (end of) the collection.
virtual void setValue(const std::string &key, int value)=0
Set integer value for the given key.
virtual const std::string & getFromType() const
The type of the &#39;from&#39; objects in this relation.
#define RELATIONTOTYPESTR
virtual const std::string & getToType() const
The type of the &#39;to&#39; objects in this relation.
virtual int getNumberOfElements() const =0
Returns the number of elements in the collection.
A single weighted relationship between two LCObjects.
Definition: LCRelation.h:29
T find(T...args)
T size(T...args)
virtual void initialize(const EVENT::LCCollection *col)
The generic collection used in LCIO.
Definition: LCCollection.h:29
#define RELATIONFROMTYPESTR
T begin(T...args)
virtual LCObject * getFrom() const =0
The &#39;from&#39; object of the given relation.
virtual void addRelation(EVENT::LCObject *from, EVENT::LCObject *to, float weight=1.0)
Adds a relation.
virtual void setBit(int bit)
Sets bit to 1.
Definition: LCFlagImpl.cc:18
virtual const EVENT::FloatVec & getRelatedFromWeights(EVENT::LCObject *to) const
The weights of the relations returned by a call to getRelatedFromObjects(to).