CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ProductRegistry.cc
Go to the documentation of this file.
1 
12 
14 
21 
22 #include <cassert>
23 #include <iterator>
24 #include <limits>
25 #include <sstream>
26 #include <ostream>
27 
28 namespace edm {
29  namespace {
30  void checkDicts(BranchDescription const& productDesc) {
31  if(productDesc.transient()) {
32  checkClassDictionaries(TypeID(productDesc.wrappedType().typeInfo()), false);
33  } else {
34  checkClassDictionaries(TypeID(productDesc.wrappedType().typeInfo()), true);
35  }
36  }
37  }
38 
40  productList_(),
41  transient_() {
42  }
43 
45  frozen_(false),
46  constProductList_(),
47  productProduced_(),
48  anyProductProduced_(false),
49  eventProductLookup_(new ProductHolderIndexHelper),
50  lumiProductLookup_(new ProductHolderIndexHelper),
51  runProductLookup_(new ProductHolderIndexHelper),
52  eventNextIndexValue_(0),
53  lumiNextIndexValue_(0),
54  runNextIndexValue_(0),
55 
56  branchIDToIndex_(),
57  missingDictionaries_() {
58  for(bool& isProduced : productProduced_) isProduced = false;
59  }
60 
61  void
63  frozen_ = false;
64  constProductList_.clear();
65  for(bool& isProduced : productProduced_) isProduced = false;
66  anyProductProduced_ = false;
67  eventProductLookup_.reset(new ProductHolderIndexHelper);
68  lumiProductLookup_.reset(new ProductHolderIndexHelper);
69  runProductLookup_.reset(new ProductHolderIndexHelper);
70  eventNextIndexValue_ = 0;
71  lumiNextIndexValue_ = 0;
72  runNextIndexValue_ = 0;
73 
74  branchIDToIndex_.clear();
75  missingDictionaries_.clear();
76  }
77 
79  productList_(productList),
80  transient_() {
81  freezeIt(toBeFrozen);
82  }
83 
84  void
86  bool fromListener) {
87  assert(productDesc.produced());
88  throwIfFrozen();
89  checkDicts(productDesc);
90  std::pair<ProductList::iterator, bool> ret =
91  productList_.insert(std::make_pair(BranchKey(productDesc), productDesc));
92  if(!ret.second) {
93  throw Exception(errors::Configuration, "Duplicate Process")
94  << "The process name " << productDesc.processName() << " was previously used on these products.\n"
95  << "Please modify the configuration file to use a distinct process name.\n";
96  }
97  addCalled(productDesc, fromListener);
98  }
99 
100  void
102  std::string const& labelAlias,
103  std::string const& instanceAlias) {
104  assert(productDesc.produced());
105  assert(productDesc.branchID().isValid());
106  throwIfFrozen();
107  BranchDescription bd(productDesc, labelAlias, instanceAlias);
108  std::pair<ProductList::iterator, bool> ret =
109  productList_.insert(std::make_pair(BranchKey(bd), bd));
110  assert(ret.second);
111  transient_.aliasToOriginal_.emplace_back(labelAlias,
112  productDesc.moduleLabel());
113  addCalled(bd, false);
114  }
115 
116  void
118  assert(!productDesc.produced());
119  throwIfFrozen();
120  BranchKey k = BranchKey(productDesc);
121  ProductList::iterator iter = productList_.find(k);
122  if(iter == productList_.end()) {
123  productList_.insert(std::make_pair(k, productDesc));
124  } else {
125  assert(combinable(iter->second, productDesc));
126  iter->second.merge(productDesc);
127  }
128  }
129 
130  bool
133  for(ProductList::const_iterator it = productList_.begin(), itEnd = productList_.end();
134  it != itEnd; ++it) {
135  if(it->second.branchType() == brType) {
136  return true;
137  }
138  }
139  return false;
140  }
141 
142  std::shared_ptr<ProductHolderIndexHelper> const&
144  if (branchType == InEvent) return transient_.eventProductLookup_;
145  if (branchType == InLumi) return transient_.lumiProductLookup_;
147  }
148 
149  void
150  ProductRegistry::setFrozen(bool initializeLookupInfo) {
151  if(frozen()) return;
152  freezeIt();
153  if(initializeLookupInfo) {
155  }
157  }
158 
159  void
161  if(frozen()) {
162  throw cms::Exception("ProductRegistry", "throwIfFrozen")
163  << "cannot modify the ProductRegistry because it is frozen\n";
164  }
165  }
166 
167  void
169  if(!frozen()) {
170  throw cms::Exception("ProductRegistry", "throwIfNotFrozen")
171  << "cannot read the ProductRegistry because it is not yet frozen\n";
172  }
173  }
174 
175  void
177  }
178 
179  std::vector<std::string>
181  std::vector<std::string> result;
182  result.reserve(productList().size());
183 
184  for(auto const& product : productList()) {
185  result.push_back(product.second.branchName());
186  }
187  return result;
188  }
189 
190  std::vector<BranchDescription const*>
192  std::vector<BranchDescription const*> result;
193  result.reserve(productList().size());
194 
195  for(auto const& product : productList()) {
196  result.push_back(&product.second);
197  }
198  return result;
199  }
200 
201  void
203  for(auto const& product : other) {
204  copyProduct(product.second);
205  }
206  }
207 
208  void
209  ProductRegistry::updateFromInput(std::vector<BranchDescription> const& other) {
210  for(BranchDescription const& branchDescription : other) {
211  copyProduct(branchDescription);
212  }
213  }
214 
217  std::string const& fileName,
218  BranchDescription::MatchMode branchesMustMatch) {
219  std::ostringstream differences;
220 
221  ProductRegistry::ProductList::iterator j = productList_.begin();
222  ProductRegistry::ProductList::iterator s = productList_.end();
223  ProductRegistry::ProductList::const_iterator i = other.productList().begin();
224  ProductRegistry::ProductList::const_iterator e = other.productList().end();
225 
226  // Loop over entries in the main product registry.
227  while(j != s || i != e) {
228  if(j != s && j->second.produced()) {
229  // Ignore branches just produced (i.e. not in input file).
230  ++j;
231  } else if(j == s || (i != e && i->first < j->first)) {
232  if(i->second.present()) {
233  differences << "Branch '" << i->second.branchName() << "' is in file '" << fileName << "'\n";
234  differences << " but not in previous files.\n";
235  } else {
236  productList_.insert(*i);
237  transient_.branchIDToIndex_[i->second.branchID()] = getNextIndexValue(i->second.branchType());
238  ++nextIndexValue(i->second.branchType());
239  }
240  ++i;
241  } else if(i == e || (j != s && j->first < i->first)) {
242  if(j->second.present() && branchesMustMatch == BranchDescription::Strict) {
243  differences << "Branch '" << j->second.branchName() << "' is in previous files\n";
244  differences << " but not in file '" << fileName << "'.\n";
245  }
246  ++j;
247  } else {
248  std::string difs = match(j->second, i->second, fileName);
249  if(difs.empty()) {
250  j->second.merge(i->second);
251  } else {
252  differences << difs;
253  }
254  ++i;
255  ++j;
256  }
257  }
259  return differences.str();
260  }
261 
263  constProductList().clear();
264  for(auto const& product : productList_) {
265  auto const& key = product.first;
266  auto const& desc = product.second;
267  constProductList().insert(std::make_pair(key, BranchDescription(desc)));
268  }
269  }
270 
272 
273  std::map<TypeID, TypeID> containedTypeMap;
274  TypeSet missingDicts;
275 
277  constProductList().clear();
278 
279  for(auto const& product : productList_) {
280  auto const& key = product.first;
281  auto const& desc = product.second;
282 
283  constProductList().insert(std::make_pair(key, BranchDescription(desc)));
284 
285  if(desc.produced()) {
286  setProductProduced(desc.branchType());
287  }
288 
289  //only do the following if the data is supposed to be available in the event
290  if(desc.present()) {
291  if(!bool(desc.unwrappedType())) {
292  missingDicts.insert(TypeID(desc.unwrappedType().typeInfo()));
293  } else if(!bool(desc.wrappedType())) {
294  missingDicts.insert(TypeID(desc.wrappedType().typeInfo()));
295  } else {
296  TypeID wrappedTypeID(desc.wrappedType().typeInfo());
297  TypeID typeID(desc.unwrappedType().typeInfo());
298  TypeID containedTypeID;
299  auto const& iter = containedTypeMap.find(typeID);
300  if(iter != containedTypeMap.end()) {
301  containedTypeID = iter->second;
302  } else {
303  containedTypeID = productholderindexhelper::getContainedTypeFromWrapper(wrappedTypeID, typeID.className());
304  containedTypeMap.emplace(typeID, containedTypeID);
305  }
307  productLookup(desc.branchType())->insert(typeID,
308  desc.moduleLabel().c_str(),
309  desc.productInstanceName().c_str(),
310  desc.processName().c_str(),
311  containedTypeID);
312 
313  transient_.branchIDToIndex_[desc.branchID()] = index;
314  }
315  }
316  }
317  productLookup(InEvent)->setFrozen();
318  productLookup(InLumi)->setFrozen();
319  productLookup(InRun)->setFrozen();
320 
322  transient_.lumiNextIndexValue_ = productLookup(InLumi)->nextIndexValue();
323  transient_.runNextIndexValue_ = productLookup(InRun)->nextIndexValue();
324 
325  for(auto const& product : productList_) {
326  auto const& desc = product.second;
327  if (transient_.branchIDToIndex_.find(desc.branchID()) == transient_.branchIDToIndex_.end()) {
328  transient_.branchIDToIndex_[desc.branchID()] = getNextIndexValue(desc.branchType());
329  ++nextIndexValue(desc.branchType());
330  }
331  }
332 
333  missingDictionariesForUpdate().reserve(missingDicts.size());
334  copy_all(missingDicts, std::back_inserter(missingDictionariesForUpdate()));
335  }
336 
338  std::map<BranchID, ProductHolderIndex>::const_iterator itFind = transient_.branchIDToIndex_.find(iID);
339  if(itFind == transient_.branchIDToIndex_.end()) {
341  }
342  return itFind->second;
343  }
344 
345  void ProductRegistry::print(std::ostream& os) const {
346  for(auto const& product: productList_) {
347  os << product.second << "\n-----\n";
348  }
349  }
350 
351  ProductHolderIndex const&
353  if (branchType == InEvent) return transient_.eventNextIndexValue_;
354  if (branchType == InLumi) return transient_.lumiNextIndexValue_;
356  }
357 
360  if (branchType == InEvent) return transient_.eventNextIndexValue_;
361  if (branchType == InLumi) return transient_.lumiNextIndexValue_;
363  }
364 }
int i
Definition: DBlmapReader.cc:9
void setProductProduced(BranchType branchType)
std::shared_ptr< ProductHolderIndexHelper > eventProductLookup_
ProductHolderIndex & nextIndexValue(BranchType branchType)
std::vector< TypeID > & missingDictionariesForUpdate()
void throwIfNotFrozen() const
TypeID getContainedTypeFromWrapper(TypeID const &wrappedtypeID, std::string const &className)
std::vector< std::string > allBranchNames() const
void checkClassDictionaries(TypeID const &type, bool recursive=true)
std::map< BranchKey, BranchDescription > ProductList
ProductHolderIndex indexFrom(BranchID const &iID) const
assert(m_qm.get())
std::shared_ptr< ProductHolderIndexHelper > lumiProductLookup_
bool anyProducts(BranchType const brType) const
std::shared_ptr< ProductHolderIndexHelper > const & productLookup(BranchType branchType) const
std::string const & processName() const
ProductHolderIndex eventNextIndexValue_
unsigned int ProductHolderIndex
ProductList::size_type size() const
bool isValid() const
Definition: BranchID.h:24
ProductHolderIndex const & getNextIndexValue(BranchType branchType) const
void addLabelAlias(BranchDescription const &productdesc, std::string const &labelAlias, std::string const &instanceAlias)
BranchType
Definition: BranchType.h:11
std::shared_ptr< ProductHolderIndexHelper > runProductLookup_
virtual void addCalled(BranchDescription const &, bool iFromListener)
ProductList const & productList() const
std::string const & moduleLabel() const
std::set< TypeID > TypeSet
ConstProductList & constProductList()
bool combinable(BranchDescription const &a, BranchDescription const &b)
tuple result
Definition: query.py:137
ProductHolderIndex lumiNextIndexValue_
int j
Definition: DBlmapReader.cc:9
std::vector< BranchDescription const * > allBranchDescriptions() const
std::string merge(ProductRegistry const &other, std::string const &fileName, BranchDescription::MatchMode branchesMustMatch=BranchDescription::Permissive)
boost::array< bool, NumBranchTypes > productProduced_
BranchID const & branchID() const
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
Definition: HCMethods.h:49
void setFrozen(bool initializeLookupInfo=true)
void freezeIt(bool frozen=true)
void print(std::ostream &os) const
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
ProductHolderIndex runNextIndexValue_
void sort_all(RandomAccessSequence &s)
wrappers for std::sort
Definition: Algorithms.h:120
std::vector< std::pair< std::string, std::string > > aliasToOriginal_
Func copy_all(ForwardSequence &s, Func f)
wrappers for copy
Definition: Algorithms.h:24
void throwIfFrozen() const
void updateFromInput(ProductList const &other)
std::map< BranchID, ProductHolderIndex > branchIDToIndex_
volatile std::atomic< bool > shutdown_flag false
void addProduct(BranchDescription const &productdesc, bool iFromListener=false)
void copyProduct(BranchDescription const &productdesc)
std::string match(BranchDescription const &a, BranchDescription const &b, std::string const &fileName)