LCCD  1.5.0
ConditionsMap.hh
1 #ifndef ConditionsMap_h
2 #define ConditionsMap_h
3 
4 #include "lcio.h"
5 #include "lccd.h"
6 
7 #include "EVENT/LCCollection.h"
8 #include "Exceptions.h"
9 
10 #include "lccd/IConditionsChangeListener.hh"
11 
12 #include <map>
13 #include <typeinfo>
14 #include <sstream>
15 
16 #include <iostream>
17 
18 namespace lccd {
19 
27  template <class KEY, class LCCONDOBJECT>
29 
30  public:
31 
32  typedef typename std::map< KEY, LCCONDOBJECT >::iterator MapIter ;
33 
34  // PMF: pointer to member function that returns the key
35  typedef KEY (LCCONDOBJECT::*PMF)() ;
36 
37  // PMF: pointer to member function that returns the key if it is
38  // declared const
39  typedef KEY (LCCONDOBJECT::*PMFC)() const ;
40 
43  ConditionsMap( PMF pmf ) : _pmf(pmf), _pmfc(NULL) { _isPMFconst = false; }
44 
47  ConditionsMap( PMFC pmfc ) : _pmf(NULL), _pmfc(pmfc) { _isPMFconst = true; }
48 
50  virtual ~ConditionsMap() {}
51 
52 
54  const std::map< KEY, LCCONDOBJECT> & map() { return _map ; }
55 
56 
59  LCCONDOBJECT& find( KEY key) {
60 
61  MapIter it = _map.find( key ) ;
62 
63  if( it == _map.end() ){
64  std::stringstream err ;
65  err << "ConditionsMap::find: no entry for key: " << key ;
66  throw lccd::DataNotAvailableException( err.str() ) ;
67  }
68  else
69  return it->second ;
70  }
71 
72 
75  void conditionsChanged( lcio::LCCollection* col ) {
76 
77  _map.clear() ;
78 
79  // add all elements in the collection to the map
80  for( int i=0; i< col->getNumberOfElements() ; i++ ){
81 
82  // this requires that the c'tor exists in LCCONDOBJECT !
83  LCCONDOBJECT obj( col->getElementAt( i ) ) ;
84 
85 
86  if(_isPMFconst == false) {
87 
88  _map.insert( std::make_pair( (obj.*_pmf)() , obj) ) ;
89 
90  } else if(_isPMFconst == true) {
91 
92  _map.insert( std::make_pair( (obj.*_pmfc)() , obj) ) ;
93 
94  }
95 
96  // call member function for key ^^^^^^^ ,e.g.
97  // _map.insert( std::make_pair( obj.getCellID() , obj) ) ;
98  }
99 
100 // print( std::cout ) ;
101  }
102 
107  void print( std::ostream& os ) {
108 
109  os << "ConditionsMap"
110 // << std::typeid(keyType).name() << ","
111 // << typeid( typename LCCONDOBJECT ).name()
112  << std::endl ;
113 
114  // for( std::map< KEY, LCCONDOBJECT >::iterator it = _map.begin() ;
115  for( MapIter it = _map.begin() ;
116  it != _map.end() ;
117  it++ ){
118 
119  os << " key: " << it->first << " - [" << it->second.id() << "]"
120  << " - " << typeid( it->second ).name()
121  << std::endl ;
122  }
123 
124  }
125 
126 
127  protected:
128 
131 
132  std::map< KEY, LCCONDOBJECT > _map{} ;
133  PMF _pmf{} ;
134  PMFC _pmfc{} ;
135 
136  bool _isPMFconst{} ;
137 };
138 
139 } //end namespace
140 
141 #endif // ConditionsMap_h
ConditionsMap(PMFC pmfc)
Constructor: provide the pointer to the member fuction that returns the key if it is declared const...
Definition: ConditionsMap.hh:47
void conditionsChanged(lcio::LCCollection *col)
Repopulate the map with new conditions data.
Definition: ConditionsMap.hh:75
Simple interface that allows notification of implementation classes if a conditions data set has chan...
Definition: IConditionsChangeListener.hh:22
virtual ~ConditionsMap()
The d&#39;tor.
Definition: ConditionsMap.hh:50
ConditionsMap(PMF pmf)
Constructor: provide the pointer to the member fuction that returns the key.
Definition: ConditionsMap.hh:43
const std::map< KEY, LCCONDOBJECT > & map()
The map.
Definition: ConditionsMap.hh:54
Exception used for data not available.
Definition: lccd_exceptions.h:56
ConditionsMap()
No default c&#39;tor.
Definition: ConditionsMap.hh:130
LCCONDOBJECT & find(KEY key)
Returns a reference to the conditions object for key.
Definition: ConditionsMap.hh:59
Template class for maps of conditions data.
Definition: ConditionsMap.hh:28
void print(std::ostream &os)
Print the conditions map to the specified output stream.
Definition: ConditionsMap.hh:107