CMS 3D CMS Logo

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