CMS 3D CMS Logo

ProductResolverIndexHelper.h
Go to the documentation of this file.
1 #ifndef DataFormats_Provenance_ProductResolverIndexHelper_h
2 #define DataFormats_Provenance_ProductResolverIndexHelper_h
3 
58 
59 #include <iosfwd>
60 #include <memory>
61 #include <set>
62 #include <string>
63 #include <vector>
64 #include <tuple>
65 #include <unordered_map>
66 
67 namespace edm {
68 
69  class TypeWithDict;
70 
71  namespace productholderindexhelper {
72  // The next function supports views. For the given wrapped type,
73  // which must be Wrapper<T>,
74  // this function returns the type of the contained type of T.
75  // If the type is not a recongnized container, it returns
76  // a TypeID(typeid(void)).
77  TypeID getContainedTypeFromWrapper(TypeID const& wrappedtypeID, std::string const& className);
78 
79  // The next function supports views. For the given type T,
80  // this function returns the type of the contained type of T.
81  // If the type is not a recongnized container, it returns
82  // a TypeID(typeid(void)).
83  // This calls getContainedTypefromWrapped internally
84  // If the TypeID for the wrapped type is already available,
85  // it is faster to call getContainedTypeFromWrapper directly.
86  TypeID getContainedType(TypeID const& typeID);
87  }
88 
90  public:
91 
93 
94  // The accessors below return a ProductResolverIndex that matches
95  // the arguments or a set of matching indexes using the Matches
96  // class. A returned index can have a value that indicates that it
97  // is invalid or ambiguous and the client should check for these
98  // values before using the index (see ProductIndexHolder.h).
99 
100  // If no matches are found or the ProductResolverIndexHelper
101  // has not been frozen yet, then an invalid index or a Matches
102  // object with numberOfMatches equal to 0 will be returned.
103 
104  // The ambiguous values can occur when the kind of type is ELEMENT_TYPE
105  // type. ELEMENT_TYPE is the type or base type of an object
106  // stored in a container that is the product. These types are used
107  // with data requests that use Views. For PRODUCT_TYPE types, ambiguity
108  // is not possible.
109 
110  // The next function returns the index for the one group that
111  // matches the type, module label, instance, and process.
112  // The 3 pointer arguments must point to C style strings terminated
113  // by a '\0' with the one following possible exception. If the
114  // process pointer is null or the process string empty, then
115  // this returns the index of the special ProductResolver that
116  // knows how to search for the product matching the type, module
117  // label, and instance and which is from the most recent process.
119  TypeID const& typeID,
120  char const* moduleLabel,
121  char const* instance,
122  char const* process = nullptr) const;
123 
124  using ModulesToIndiciesMap =std::unordered_multimap<std::string,std::tuple<TypeID const*, const char*, ProductResolverIndex>>;
125  ModulesToIndiciesMap indiciesForModulesInProcess( const std::string& iProcessName ) const;
126 
127  class Matches {
128  public:
129  Matches(ProductResolverIndexHelper const* productResolverIndexHelper,
130  unsigned int startInIndexAndNames,
131  unsigned int numberOfMatches);
132 
133  ProductResolverIndex index(unsigned int i) const;
134  unsigned int numberOfMatches() const { return numberOfMatches_; }
135  bool isFullyResolved(unsigned int i) const;
136  char const* moduleLabel(unsigned int i) const;
137  char const* processName(unsigned int i) const;
138  private:
140  unsigned int startInIndexAndNames_;
141  unsigned int numberOfMatches_;
142  };
143 
144  // Return ProductResolverIndex's for all product holders that
145  // match the type, module label, and product instance name.
146  // The pointer arguments must be C style strings terminated
147  // by a '\0'.
148  Matches
149  relatedIndexes(KindOfType kindOfType,
150  TypeID const& typeID,
151  char const* moduleLabel,
152  char const* instance) const;
153 
154  // Return indexes for all groups that match the type.
155  Matches
156  relatedIndexes(KindOfType kindOfType,
157  TypeID const& typeID) const;
158 
159  // This will throw if called after the object is frozen.
160  // The typeID must be for a type with a dictionary
161  // (the calling function is expected to check that)
162  // The pointer arguments must point at C style strings
163  // terminated by '\0'.
164  // 1. This creates an entry and new ProductResolverIndex for
165  // the product if it does not already exist. If it
166  // does exist then it throws.
167  // 2. If it does not already exist, this will create an
168  // entry and new ProductResolverIndex for the ProductResolver
169  // that will search for the matching type, label, and
170  // instance for the most recent process (internally indicated
171  // by an empty process string).
172  // This will then loop over the contained class (if it exists)
173  // and the base classes of the contained class.
174  // 1. If the matching type, label, instance, and process
175  // already exist then that entry is modified and marked
176  // ambiguous. If not, it inserts an entry which uses the same
177  // ProductResolverIndex as the containing product.
178  // 2. If it does not already exist it inserts a new
179  // entry with a new ProductResolverIndex for the case
180  // which searches for the most recent process.
182  insert(TypeID const& typeID,
183  char const* moduleLabel,
184  char const* instance,
185  char const* process,
186  TypeID const& containedTypeID,
187  std::vector<TypeWithDict>* baseTypesOfContainedType);
188 
190  insert(TypeID const& typeID,
191  char const* moduleLabel,
192  char const* instance,
193  char const* process);
194 
195  // Before the object is frozen the accessors above will
196  // fail to find a match. Once frozen, no more new entries
197  // can be added with insert.
198  void setFrozen();
199 
200  std::vector<std::string> const& lookupProcessNames() const;
201 
202  class Range {
203  public:
204  Range(unsigned int begin, unsigned int end) : begin_(begin), end_(end) { }
205  unsigned int begin() const { return begin_; }
206  unsigned int end() const { return end_; }
207  private:
208  unsigned int begin_;
209  unsigned int end_;
210  };
211 
213  public:
214  IndexAndNames(ProductResolverIndex index, unsigned int start, unsigned int startProcess) :
215  index_(index), startInBigNamesContainer_(start), startInProcessNames_(startProcess) { }
216  ProductResolverIndex index() const { return index_; }
217  unsigned int startInBigNamesContainer() const { return startInBigNamesContainer_; }
218  unsigned int startInProcessNames() const { return startInProcessNames_; }
219  private:
222  unsigned int startInProcessNames_;
223  };
224 
225  unsigned int beginElements() const { return beginElements_; }
226  std::vector<TypeID> const& sortedTypeIDs() const { return sortedTypeIDs_; }
227  std::vector<Range> const& ranges() const { return ranges_; }
228  std::vector<IndexAndNames> const& indexAndNames() const { return indexAndNames_; }
229  std::vector<char> const& processNames() const { return processNames_; }
230 
231  // The next few functions are intended for internal use
232  // but are public so tests can use them also.
233 
234  unsigned int indexToIndexAndNames(KindOfType kindOfType,
235  TypeID const& typeID,
236  char const* moduleLabel,
237  char const* instance,
238  char const* process) const;
239 
240  // Returns the index into sortedTypeIDs_. Returns the maximum unsigned
241  // int value if the type is not there.
242  unsigned int indexToType(KindOfType kindOfType, TypeID const& typeID) const;
243 
244  // Returns the index of the process name in processNames_. Returns the
245  // maximum unsigned int value if the process name is not found.
246  unsigned int processIndex(char const* process) const;
247 
248  // This will throw if it detects problems, but unless there is a bug
249  // there should never be any. Mostly it is checking for things which
250  // might cause out of bounds errors when accessing the vectors.
251  void sanityCheck() const;
252 
253  ProductResolverIndex nextIndexValue() const { return nextIndexValue_; }
254 
255  // For debugging only
256  void print(std::ostream& os) const;
257 
258  private:
259 
260  // Next available value for a ProductResolverIndex. This just
261  // increments by one each time a new value is assigned.
263 
264  // This is an index into sortedTypeIDs_ that tells where
265  // the entries corresponding to types of elements in containers
266  // start.
267  unsigned int beginElements_;
268 
269  // Sorted by putting all the product entries first and
270  // then the element entries. Then sorted by TypeID value.
271  // Most lookups start here by finding the TypeID and
272  // then using its position to find the corresponding Range
273  // in the ranges_ vector below.
274  std::vector<TypeID> sortedTypeIDs_;
275 
276  // There is a one to one correspondence between this vector
277  // and sortedTypeIDs_ and the corresponding elements appear
278  // in the same order. Each Range object holds the beginning and
279  // end of the corresponding elements in the indexAndNames_
280  // vector below.
281  std::vector<Range> ranges_;
282 
283  // Each element of this vector contains a ProductResolverIndex.
284  // It also contains indexes into vectors of characters that
285  // hold the corresponding moduleLabel, instance, and process
286  // name. Note this is sorted with product entries first then
287  // element entries, then by TypeID, then by moduleLabel,
288  // then by instance, and finally by process. Note that in
289  // a subset of entries that have matching type/label/instance
290  // the entry with an empty process name will always be the
291  // first process and startInProcessNames_ will always be 0
292  // for the empty process name.
293  std::vector<IndexAndNames> indexAndNames_;
294 
295  // These contain C style strings terminated
296  // by '\0' all concatenated together. In the
297  // first vector the strings come in pairs,
298  // always module label then instance. The pairs
299  // are ordered the same as the IndexAndNames
300  // vector. Within a TypeID, the moduleLabel/instance
301  // pair is not duplicated if it appears multiple times.
302  // The second vector contains all the process names
303  // in alphabetical order with no duplication. The
304  // first element is always the empty string.
305  std::vector<char> bigNamesContainer_;
306  std::vector<char> processNames_;
307 
308  // Duplicates the entries in processNames_ in
309  // a convenient format.
310  std::vector<std::string> lookupProcessNames_;
311 
312  // The rest of the data members are for temporary use
313  // while the data structure is being filled.
314 
315  class Item {
316  public:
317  Item(KindOfType kindOfType,
318  TypeID const& typeID,
319  std::string const& moduleLabel,
320  std::string const& instance,
321  std::string const& process,
322  ProductResolverIndex index);
323  KindOfType kindOfType() const { return kindOfType_; }
324  TypeID const& typeID() const { return typeID_; }
325  std::string const& moduleLabel() const { return moduleLabel_; }
326  std::string const& instance() const { return instance_; }
327  std::string const& process() const { return process_; }
328  ProductResolverIndex index() const { return index_; }
329 
330  void clearProcess() { process_.clear(); }
331  void setIndex(ProductResolverIndex v) { index_ = v; }
332 
333  bool operator<(Item const& right) const;
334 
335  private:
342  };
343 
345 
347  };
348 }
349 #endif
bool operator<(DetSet< T > const &x, DetSet< T > const &y)
Definition: DetSet.h:90
Definition: start.py:1
TypeID getContainedType(TypeID const &typeID)
ProductResolverIndex nextIndexValue() const
unsigned int ProductResolverIndex
std::vector< TypeID > const & sortedTypeIDs() const
TypeID getContainedTypeFromWrapper(TypeID const &wrappedtypeID, std::string const &className)
std::vector< l1t::Jet >::iterator end_
static PFTauRenderPlugin instance
std::unordered_multimap< std::string, std::tuple< TypeID const *, const char *, ProductResolverIndex >> ModulesToIndiciesMap
std::vector< Range > const & ranges() const
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
Range(unsigned int begin, unsigned int end)
edm::propagate_const< std::unique_ptr< std::set< std::string > > > processItems_
std::vector< IndexAndNames > const & indexAndNames() const
#define end
Definition: vmac.h:39
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
std::vector< IndexAndNames > indexAndNames_
std::vector< std::string > lookupProcessNames_
#define begin
Definition: vmac.h:32
HLT enums.
ProductResolverIndexHelper const * productResolverIndexHelper_
std::vector< char > const & processNames() const
IndexAndNames(ProductResolverIndex index, unsigned int start, unsigned int startProcess)
edm::propagate_const< std::unique_ptr< std::set< Item > > > items_
std::string className(const T &t)
Definition: ClassName.h:30