CMS 3D CMS Logo

AssociativeIterator.h
Go to the documentation of this file.
1 #ifndef DataFormats_Common_AssociativeIterator_h
2 #define DataFormats_Common_AssociativeIterator_h
3 
44 
45 namespace edm {
46  class Event;
47  template <class T>
48  class View;
49  template <class T>
50  class Handle;
51  template <class T>
52  class Association;
53  template <class T>
54  class RefToBase;
55  template <class T>
56  class Ptr;
57  template <class C, class T, class F>
58  class Ref;
59 } // namespace edm
60 
61 namespace edm {
62 
63  // Helper classes to convert one ref type to another.
64  // Now it's able to convert anything to itself, and RefToBase to anything else
65  // This won't be needed if we used Ptr
66  namespace helper {
67  template <typename RefFrom, typename RefTo>
68  struct RefConverter {
69  static RefTo convert(const RefFrom &ref) { return RefTo(ref); }
70  };
71  template <typename T>
72  struct RefConverter<RefToBase<T>, Ptr<T> > {
73  static Ptr<T> convert(const RefToBase<T> &ref) {
74  return Ptr<T>(ref.id(), ref.isAvailable() ? ref.get() : 0, ref.key());
75  }
76  };
77  template <typename T, typename C, typename V, typename F>
78  struct RefConverter<RefToBase<T>, Ref<C, V, F> > {
79  static Ref<C, V, F> convert(const RefToBase<T> &ref) { return ref.template castTo<Ref<C, V, F> >(); }
80  };
81  } // namespace helper
82 
84  // the implementation uses View, and works for RefType = Ref, RefToBase and Ptr
85  template <typename RefType, typename EventType>
87  public:
91 
92  RefType get(const ProductID &id, size_t idx) const {
93  typedef typename edm::RefToBase<element_type>
94  BaseRefType; // could also use Ptr, but then I can't do Ptr->RefToBase
95  if (id_ != id) {
96  id_ = id;
97  iEvent_.get(id_, view_);
98  }
99  BaseRefType ref = view_->refAt(idx);
101  return conv::convert(ref);
102  }
103 
104  private:
105  //This class is not intended to be used across threads
109  };
110 
111  // unfortunately it's not possible to define value_type of an Association<C> correctly
112  // so we need yet another template trick
113  namespace helper {
114  template <typename AC>
116  typedef typename AC::value_type type;
117  };
118 
119  template <typename C>
122  };
123  } // namespace helper
124 
125  template <typename KeyRefType, typename AssociativeCollection, typename ItemGetter>
127  public:
128  typedef KeyRefType key_type;
131  typedef typename std::pair<key_type, val_type> value_type;
132 
134 
136  AssociativeIterator(const AssociativeCollection &map, const ItemGetter &getter);
137 
141  // self_type & skipTo(const ProductID &id, size_t offs = 0) ; // to be implemented one day
142 
143  const value_type &operator*() const { return *(this->get()); }
144  const value_type *operator->() const { return (this->get()); }
145  const value_type *get() const {
146  chkPair();
147  return &pair_;
148  }
149 
150  const key_type &key() const {
151  chkPair();
152  return pair_.first;
153  }
154  const val_type &val() const { return map_.get(idx_); }
155  const ProductID &id() const { return ioi_->first; }
156 
157  operator bool() const { return idx_ < map_.size(); }
158  self_type end() const;
159 
160  bool operator==(const self_type &other) const { return other.idx_ == idx_; }
161  bool operator!=(const self_type &other) const { return other.idx_ != idx_; }
162  bool operator<(const self_type &other) const { return other.idx_ < idx_; }
163 
164  private:
165  typedef typename AssociativeCollection::id_offset_vector id_offset_vector;
166  typedef typename id_offset_vector::const_iterator id_offset_iterator;
167  const AssociativeCollection &map_;
169  size_t idx_;
170 
171  ItemGetter getter_;
172 
173  //This class is not intended to be used across threads
174  CMS_SA_ALLOW mutable bool pairOk_;
176 
177  void chkPair() const;
178  };
179 
180  template <typename KeyRefType, typename AC, typename IG>
182  : map_(map), ioi_(map_.ids().begin()), ioi2_(ioi_ + 1), idx_(0), getter_(getter), pairOk_(false) {}
183 
184  template <typename KeyRefType, typename AC, typename IG>
186  pairOk_ = false;
187  idx_++;
188  if (ioi2_ < map_.ids().end()) {
189  if (ioi2_->second == idx_) {
190  ++ioi_;
191  ++ioi2_;
192  }
193  }
194  return *this;
195  }
196 
197  template <typename KeyRefType, typename AC, typename IG>
199  pairOk_ = false;
200  idx_--;
201  if (ioi_->second < idx_) {
202  --ioi_;
203  --ioi2_;
204  }
205  return *this;
206  }
207 
208  template <typename KeyRefType, typename AC, typename IG>
210  pairOk_ = false;
211  ioi_++;
212  ioi2_++;
213  if (ioi_ == map_.ids().end()) {
214  idx_ = map_.size();
215  } else {
216  idx_ = ioi_->second;
217  }
218  }
219 
220  /*
221  template<typename KeyRefType, typename AC, typename IG>
222  AssociativeIterator<KeyRefType,AC,IG> & AssociativeIterator<KeyRefType,AC,IG>::skipTo(const ProductID &id, size_t offs) {
223  pairOk_ = false;
224  throw Exception(errors::UnimplementedFeature);
225  }
226  */
227 
228  template <typename KeyRefType, typename AC, typename IG>
230  self_type ret(map_, getter_);
231  ret.ioi_ = map_.ids().end();
232  ret.ioi2_ = ret.ioi_ + 1;
233  ret.idx_ = map_.size();
234  return ret;
235  }
236 
237  template <typename KeyRefType, typename AC, typename IG>
239  if (pairOk_)
240  return;
241  pair_.first = getter_.get(id(), idx_ - ioi_->second);
242  pair_.second = map_.get(idx_);
243  pairOk_ = true;
244  }
245 
246  template <typename KeyRefType, typename AC, typename EventType>
248  const AC &map, const EventType &event) {
250  return AssociativeIterator<KeyRefType, AC, Getter>(map, Getter{event});
251  }
252 } // namespace edm
253 
254 #endif
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:542
cms::cuda::V
cudaStream_t T uint32_t const T *__restrict__ const uint32_t *__restrict__ uint32_t int cudaStream_t V
Definition: HistoContainer.h:99
electrons_cff.bool
bool
Definition: electrons_cff.py:366
edm::AssociativeIterator::operator->
const value_type * operator->() const
Definition: AssociativeIterator.h:144
funct::false
false
Definition: Factorize.h:29
edm::AssociativeIterator::key_val_type
KeyRefType::value_type key_val_type
Definition: AssociativeIterator.h:129
edm::EventItemGetter::get
RefType get(const ProductID &id, size_t idx) const
Definition: AssociativeIterator.h:92
GetRecoTauVFromDQM_MC_cff.EventType
EventType
Definition: GetRecoTauVFromDQM_MC_cff.py:17
edm::RefToBase::key
size_t key() const
Definition: RefToBase.h:219
sistrip::View
View
Definition: ConstantsForView.h:26
conv
static HepMC::IO_HEPEVT conv
Definition: BeamHaloProducer.cc:48
edm::AssociativeIterator::operator--
self_type & operator--()
Definition: AssociativeIterator.h:198
edm::AssociativeIterator::key_type
KeyRefType key_type
Definition: AssociativeIterator.h:128
edm::AssociativeIterator::getter_
ItemGetter getter_
Definition: AssociativeIterator.h:171
edm::AssociativeIterator::pairOk_
bool pairOk_
Definition: AssociativeIterator.h:174
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::AssociativeIterator::self_type
AssociativeIterator< KeyRefType, AssociativeCollection, ItemGetter > self_type
Definition: AssociativeIterator.h:133
edm::AssociativeIterator::AssociativeIterator
AssociativeIterator(const AssociativeCollection &map, const ItemGetter &getter)
Create the associative iterator, pointing at the beginning of the collection.
Definition: AssociativeIterator.h:181
edm::helper::AssociativeCollectionValueType
Definition: AssociativeIterator.h:115
edm::AssociativeIterator::val
const val_type & val() const
Definition: AssociativeIterator.h:154
edm::AssociativeIterator::get
const value_type * get() const
Definition: AssociativeIterator.h:145
edm::AssociativeIterator::id_offset_vector
AssociativeCollection::id_offset_vector id_offset_vector
Definition: AssociativeIterator.h:165
edm::EventItemGetter
Helper class that fetches some type of Ref given ProductID and index, using the edm::Event.
Definition: AssociativeIterator.h:86
edm::EventItemGetter::view_
Handle< View< element_type > > view_
Definition: AssociativeIterator.h:106
GenParticlesHelper::IG
reco::GenParticleCollection::const_iterator IG
Definition: GenParticlesHelper.h:10
edm::Handle
Definition: AssociativeIterator.h:50
edm::AssociativeIterator::nextProductID
self_type & nextProductID()
Definition: AssociativeIterator.h:209
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
edm::Ref
Definition: AssociativeIterator.h:58
heavyIonCSV_trainingSettings.idx
idx
Definition: heavyIonCSV_trainingSettings.py:5
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::helper::AssociativeCollectionValueType< Association< C > >::type
Association< C >::reference_type type
Definition: AssociativeIterator.h:121
edm::AssociativeIterator::end
self_type end() const
Definition: AssociativeIterator.h:229
edm::AssociativeIterator::id
const ProductID & id() const
Definition: AssociativeIterator.h:155
edm::EventItemGetter::EventItemGetter
EventItemGetter(const EventType &iEvent)
Definition: AssociativeIterator.h:89
edm::helper::RefConverter
Definition: AssociativeIterator.h:68
edm::AssociativeIterator::ioi2_
id_offset_iterator ioi2_
Definition: AssociativeIterator.h:168
ProductID.h
edm::makeAssociativeIterator
AssociativeIterator< KeyRefType, AC, edm::EventItemGetter< KeyRefType, EventType > > makeAssociativeIterator(const AC &map, const EventType &event)
Definition: AssociativeIterator.h:247
Event
trackingPlots.other
other
Definition: trackingPlots.py:1460
edm::AssociativeIterator::operator*
const value_type & operator*() const
Definition: AssociativeIterator.h:143
edm::RefToBase::get
value_type const * get() const
Definition: RefToBase.h:209
edm::helper::RefConverter< RefToBase< T >, Ptr< T > >::convert
static Ptr< T > convert(const RefToBase< T > &ref)
Definition: AssociativeIterator.h:73
edm::AssociativeIterator::operator++
self_type & operator++()
Definition: AssociativeIterator.h:185
fileCollector.convert
def convert(infile, ofile)
Definition: fileCollector.py:47
edm::AssociativeIterator::val_type
helper::AssociativeCollectionValueType< AssociativeCollection >::type val_type
Definition: AssociativeIterator.h:130
thread_safety_macros.h
edm::RefToBase::isAvailable
bool isAvailable() const
Definition: RefToBase.h:119
helper
Definition: helper.py:1
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::AssociativeIterator::map_
const AssociativeCollection & map_
Definition: AssociativeIterator.h:167
edm::helper::AssociativeCollectionValueType::type
AC::value_type type
Definition: AssociativeIterator.h:116
edm::AssociativeIterator
Definition: AssociativeIterator.h:126
edm::Association
Definition: Association.h:18
reco::JetExtendedAssociation::value_type
Container::value_type value_type
Definition: JetExtendedAssociation.h:30
edm::EventItemGetter::id_
ProductID id_
Definition: AssociativeIterator.h:107
edm::AssociativeIterator::pair_
value_type pair_
Definition: AssociativeIterator.h:175
edm::Ptr
Definition: AssociationVector.h:31
edm::RefToBase::id
ProductID id() const
Definition: RefToBase.h:214
edm::AssociativeIterator::operator==
bool operator==(const self_type &other) const
Definition: AssociativeIterator.h:160
edm::AssociativeIterator::id_offset_iterator
id_offset_vector::const_iterator id_offset_iterator
Definition: AssociativeIterator.h:166
edm::EventItemGetter::element_type
RefType::value_type element_type
Definition: AssociativeIterator.h:88
edm::AssociativeIterator::ioi_
id_offset_iterator ioi_
Definition: AssociativeIterator.h:168
edm::AssociativeIterator::operator!=
bool operator!=(const self_type &other) const
Definition: AssociativeIterator.h:161
gen::C
C
Definition: PomwigHadronizer.cc:78
triggerObjects_cff.id
id
Definition: triggerObjects_cff.py:29
T
long double T
Definition: Basic3DVectorLD.h:48
edm::EventItemGetter::iEvent_
const EventType & iEvent_
Definition: AssociativeIterator.h:108
edm::AssociativeIterator::value_type
std::pair< key_type, val_type > value_type
Definition: AssociativeIterator.h:131
edm::helper::RefConverter< RefToBase< T >, Ref< C, V, F > >::convert
static Ref< C, V, F > convert(const RefToBase< T > &ref)
Definition: AssociativeIterator.h:79
edm::EventItemGetter::~EventItemGetter
~EventItemGetter()
Definition: AssociativeIterator.h:90
edm::helper::RefConverter::convert
static RefTo convert(const RefFrom &ref)
Definition: AssociativeIterator.h:69
edm::RefToBase
Definition: AssociativeIterator.h:54
EDProductGetter.h
genParticles_cff.map
map
Definition: genParticles_cff.py:11
edm::AssociativeIterator::key
const key_type & key() const
Definition: AssociativeIterator.h:150
event
Definition: event.py:1
edm::AssociativeIterator::chkPair
void chkPair() const
Definition: AssociativeIterator.h:238
edm::ProductID
Definition: ProductID.h:27
edm::AssociativeIterator::operator<
bool operator<(const self_type &other) const
Definition: AssociativeIterator.h:162
edm::AssociativeIterator::idx_
size_t idx_
Definition: AssociativeIterator.h:169