CMS 3D CMS Logo

EventPrincipal.cc
Go to the documentation of this file.
2 
23 
24 #include <algorithm>
25 #include <cassert>
26 #include <limits>
27 #include <memory>
28 
29 namespace edm {
31  std::shared_ptr<ProductRegistry const> reg,
32  std::shared_ptr<BranchIDListHelper const> branchIDListHelper,
33  std::shared_ptr<ThinnedAssociationsHelper const> thinnedAssociationsHelper,
34  ProcessConfiguration const& pc,
35  HistoryAppender* historyAppender,
36  unsigned int streamIndex,
37  bool isForPrimaryProcess) :
38  Base(reg, reg->productLookup(InEvent), pc, InEvent, historyAppender,isForPrimaryProcess),
39  aux_(),
40  luminosityBlockPrincipal_(),
41  provRetrieverPtr_(new ProductProvenanceRetriever(streamIndex)),
42  eventSelectionIDs_(),
43  branchIDListHelper_(branchIDListHelper),
44  thinnedAssociationsHelper_(thinnedAssociationsHelper),
45  branchListIndexes_(),
46  branchListIndexToProcessIndex_(),
47  streamID_(streamIndex) {
49  }
50 
51  void
54  aux_ = EventAuxiliary();
55  luminosityBlockPrincipal_ = nullptr; // propagate_const<T> has no reset() function
56  provRetrieverPtr_->reset();
58  }
59 
60  void
62  ProcessHistoryRegistry const& processHistoryRegistry,
65  ProductProvenanceRetriever const& provRetriever,
67  bool deepCopyRetriever) {
69  if (deepCopyRetriever) {
70  provRetrieverPtr_->deepCopy(provRetriever);
71  } else {
72  provRetrieverPtr_->mergeParentProcessRetriever(provRetriever);
73  }
75  if(branchIDListHelper_->hasProducedProducts()) {
76  // Add index into BranchIDListRegistry for products produced this process
77  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
78  }
79  fillEventPrincipal(aux,processHistoryRegistry,reader);
80  }
81 
82  void
84  ProcessHistoryRegistry const& processHistoryRegistry,
89  if(branchIDListHelper_->hasProducedProducts()) {
90  // Add index into BranchIDListRegistry for products produced this process
91  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
92  }
93  fillEventPrincipal(aux,processHistoryRegistry,nullptr);
94  }
95 
96  void
98  ProcessHistoryRegistry const& processHistoryRegistry,
100  if(aux.event() == invalidEventNumber) {
102  << "EventPrincipal::fillEventPrincipal, Invalid event number provided in EventAuxiliary, It is illegal for the event number to be 0\n";
103  }
104 
105  fillPrincipal(aux.processHistoryID(), processHistoryRegistry, reader);
106  aux_ = aux;
108 
109  if(branchListIndexes_.empty() and branchIDListHelper_->hasProducedProducts()) {
110  // Add index into BranchIDListRegistry for products produced this process
111  // if it hasn't already been filled in by the other fillEventPrincipal or by an earlier call to this function
112  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
113  }
114 
115  // Fill in helper map for Branch to ProductID mapping
116  ProcessIndex pix = 0;
117  for(auto const& blindex : branchListIndexes_) {
118  branchListIndexToProcessIndex_.insert(std::make_pair(blindex, pix));
119  ++pix;
120  }
121 
122  // Fill in the product ID's in the product holders.
123  for(auto& prod : *this) {
124  if (prod->singleProduct()) {
125  // If an alias is in the same process as the original then isAlias will be true.
126  // Under that condition, we want the ProductID to be the same as the original.
127  // If not, then we've internally changed the original BranchID to the alias BranchID
128  // in the ProductID lookup so we need the alias BranchID.
129  auto const & bd =prod->branchDescription();
130  prod->setProvenance(productProvenanceRetrieverPtr(),
131  processHistory(),
132  branchIDToProductID(bd.isAlias()?bd.originalBranchID(): bd.branchID()));
133  }
134  }
135  }
136 
137  void
138  EventPrincipal::setLuminosityBlockPrincipal(std::shared_ptr<LuminosityBlockPrincipal> const& lbp) {
140  }
141 
142  void
144  assert(run == luminosityBlockPrincipal_->run());
145  assert(lumi == luminosityBlockPrincipal_->luminosityBlock());
146  EventNumber_t event = aux_.id().event();
147  aux_.id() = EventID(run, lumi, event);
148  }
149 
150  RunPrincipal const&
153  }
154 
155  void
157  BranchDescription const& bd,
158  std::unique_ptr<WrapperBase> edp,
159  ProductProvenance const& productProvenance) const {
160 
161  // assert commented out for DaqSource. When DaqSource no longer uses put(), the assert can be restored.
162  //assert(produced());
163  if(edp.get() == nullptr) {
164  throw Exception(errors::InsertFailure, "Null Pointer")
165  << "put: Cannot put because ptr to product is null."
166  << "\n";
167  }
168  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
169  auto phb = getExistingProduct(bd.branchID());
170  assert(phb);
171  // ProductResolver assumes ownership
172  phb->putProduct(std::move(edp));
173  }
174 
175  void
178  std::unique_ptr<WrapperBase> edp,
179  ParentageID parentage) const {
180  if(edp.get() == nullptr) {
181  throw Exception(errors::InsertFailure, "Null Pointer")
182  << "put: Cannot put because ptr to product is null."
183  << "\n";
184  }
185  auto phb = getProductResolverByIndex(index);
186 
187  productProvenanceRetrieverPtr()->insertIntoSet(ProductProvenance(phb->branchDescription().branchID(), std::move(parentage)));
188 
189  assert(phb);
190  // ProductResolver assumes ownership
191  phb->putProduct(std::move(edp));
192 
193  }
194 
195  void
197  BranchDescription const& bd,
198  std::unique_ptr<WrapperBase> edp,
199  ProductProvenance const* productProvenance) const {
200 
201  assert(!bd.produced());
202  if (productProvenance) {
203  productProvenanceRetrieverPtr()->insertIntoSet(*productProvenance);
204  }
205  auto phb = getExistingProduct(bd.branchID());
206  assert(phb);
207  // ProductResolver assumes ownership
208  phb->putProduct(std::move(edp));
209  }
210 
211  BranchID
213  if(!pid.isValid()) {
214  throw Exception(errors::ProductNotFound, "InvalidID")
215  << "get by product ID: invalid ProductID supplied\n";
216  }
217  return productIDToBranchID(pid, branchIDListHelper_->branchIDLists(), branchListIndexes_);
218  }
219 
220  ProductID
222  if(!bid.isValid()) {
223  throw Exception(errors::NotFound, "InvalidID")
224  << "branchIDToProductID: invalid BranchID supplied\n";
225  }
226  typedef BranchIDListHelper::BranchIDToIndexMap BIDToIndexMap;
227  typedef BIDToIndexMap::const_iterator Iter;
228  typedef std::pair<Iter, Iter> IndexRange;
229 
230  IndexRange range = branchIDListHelper_->branchIDToIndexMap().equal_range(bid);
231  for(Iter it = range.first; it != range.second; ++it) {
232  BranchListIndex blix = it->second.first;
233  std::map<BranchListIndex, ProcessIndex>::const_iterator i = branchListIndexToProcessIndex_.find(blix);
234  if(i != branchListIndexToProcessIndex_.end()) {
235  ProductIndex productIndex = it->second.second;
236  ProcessIndex processIndex = i->second;
237  return ProductID(processIndex+1, productIndex+1);
238  }
239  }
240  // cannot throw, because some products may legitimately not have product ID's (e.g. pile-up).
241  return ProductID();
242  }
243 
244  unsigned int
246  return streamID_.value();
247  }
248 
251  exception<<"get by product ID: The product with given id: "<<pid
252  <<"\ntype: "<<phb->productType()
253  <<"\nproduct instance name: "<<phb->productInstanceName()
254  <<"\nprocess name: "<<phb->processName()
255  <<"\nwas already deleted. This is a configuration error. Please change the configuration of the module which caused this exception to state it reads this data.";
256  throw exception;
257  }
258 
261  BranchID bid = pidToBid(pid);
263  if(phb == nullptr) {
264  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
265  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
266  *whyFailed
267  << "get by product ID: no product with given id: " << pid << "\n";
268  return whyFailed;
269  }));
270  }
271 
272  // Was this already deleted?
273  if(phb->productWasDeleted()) {
275  }
276  // Check for case where we tried on demand production and
277  // it failed to produce the object
278  if(phb->unscheduledWasNotRun()) {
279  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
280  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
281  *whyFailed
282  << "get by ProductID: could not get product with id: " << pid << "\n"
283  << "Unscheduled execution not allowed to get via ProductID.\n";
284  return whyFailed;
285  }));
286  }
287  auto resolution = phb->resolveProduct(*this,false,nullptr,nullptr);
288 
289  auto data = resolution.data();
290  if(data) {
291  return BasicHandle(data->wrapper(), &(data->provenance()));
292  }
293  return BasicHandle(nullptr,nullptr);
294  }
295 
296  WrapperBase const*
298  return getByProductID(pid).wrapper();
299  }
300 
301  WrapperBase const*
302  EventPrincipal::getThinnedProduct(ProductID const& pid, unsigned int& key) const {
303 
304  BranchID parent = pidToBid(pid);
305 
306  // Loop over thinned containers which were made by selecting elements from the parent container
307  for(auto associatedBranches = thinnedAssociationsHelper_->parentBegin(parent),
308  iEnd = thinnedAssociationsHelper_->parentEnd(parent);
309  associatedBranches != iEnd; ++associatedBranches) {
310 
311  ThinnedAssociation const* thinnedAssociation =
312  getThinnedAssociation(associatedBranches->association());
313  if(thinnedAssociation == nullptr) continue;
314 
315  if(associatedBranches->parent() != pidToBid(thinnedAssociation->parentCollectionID())) {
316  continue;
317  }
318 
319  unsigned int thinnedIndex = 0;
320  // Does this thinned container have the element referenced by key?
321  // If yes, thinnedIndex is set to point to it in the thinned container
322  if(!thinnedAssociation->hasParentIndex(key, thinnedIndex)) {
323  continue;
324  }
325  // Get the thinned container and return a pointer if we can find it
326  ProductID const& thinnedCollectionPID = thinnedAssociation->thinnedCollectionID();
327  BasicHandle bhThinned = getByProductID(thinnedCollectionPID);
328  if(!bhThinned.isValid()) {
329  // Thinned container is not found, try looking recursively in thinned containers
330  // which were made by selecting elements from this thinned container.
331  WrapperBase const* wrapperBase = getThinnedProduct(thinnedCollectionPID, thinnedIndex);
332  if(wrapperBase != nullptr) {
333  key = thinnedIndex;
334  return wrapperBase;
335  } else {
336  continue;
337  }
338  }
339  key = thinnedIndex;
340  return bhThinned.wrapper();
341  }
342  return nullptr;
343  }
344 
345  void
347  std::vector<WrapperBase const*>& foundContainers,
348  std::vector<unsigned int>& keys) const {
349 
350  BranchID parent = pidToBid(pid);
351 
352  // Loop over thinned containers which were made by selecting elements from the parent container
353  for(auto associatedBranches = thinnedAssociationsHelper_->parentBegin(parent),
354  iEnd = thinnedAssociationsHelper_->parentEnd(parent);
355  associatedBranches != iEnd; ++associatedBranches) {
356 
357  ThinnedAssociation const* thinnedAssociation =
358  getThinnedAssociation(associatedBranches->association());
359  if(thinnedAssociation == nullptr) continue;
360 
361  if(associatedBranches->parent() != pidToBid(thinnedAssociation->parentCollectionID())) {
362  continue;
363  }
364 
365  unsigned nKeys = keys.size();
366  unsigned int doNotLookForThisIndex = std::numeric_limits<unsigned int>::max();
367  std::vector<unsigned int> thinnedIndexes(nKeys, doNotLookForThisIndex);
368  bool hasAny = false;
369  for(unsigned k = 0; k < nKeys; ++k) {
370  // Already found this one
371  if(foundContainers[k] != nullptr) continue;
372  // Already know this one is not in this thinned container
373  if(keys[k] == doNotLookForThisIndex) continue;
374  // Does the thinned container hold the entry of interest?
375  // Modifies thinnedIndexes[k] only if it returns true and
376  // sets it to the index in the thinned collection.
377  if(thinnedAssociation->hasParentIndex(keys[k], thinnedIndexes[k])) {
378  hasAny = true;
379  }
380  }
381  if(!hasAny) {
382  continue;
383  }
384  // Get the thinned container and set the pointers and indexes into
385  // it (if we can find it)
386  ProductID thinnedCollectionPID = thinnedAssociation->thinnedCollectionID();
387  BasicHandle bhThinned = getByProductID(thinnedCollectionPID);
388  if(!bhThinned.isValid()) {
389  // Thinned container is not found, try looking recursively in thinned containers
390  // which were made by selecting elements from this thinned container.
391  getThinnedProducts(thinnedCollectionPID, foundContainers, thinnedIndexes);
392  for(unsigned k = 0; k < nKeys; ++k) {
393  if(foundContainers[k] == nullptr) continue;
394  if(thinnedIndexes[k] == doNotLookForThisIndex) continue;
395  keys[k] = thinnedIndexes[k];
396  }
397  } else {
398  for(unsigned k = 0; k < nKeys; ++k) {
399  if(thinnedIndexes[k] == doNotLookForThisIndex) continue;
400  keys[k] = thinnedIndexes[k];
401  foundContainers[k] = bhThinned.wrapper();
402  }
403  }
404  }
405  }
406 
407  Provenance
409  BranchID bid = pidToBid(pid);
410  return getProvenance(bid, mcc);
411  }
412 
415  return eventSelectionIDs_;
416  }
417 
418  BranchListIndexes const&
420  return branchListIndexes_;
421  }
422 
425 
426  ConstProductResolverPtr const phb = getProductResolver(branchID);
427 
428  if(phb == nullptr) {
430  << "EventPrincipal::getThinnedAssociation, ThinnedAssociation ProductResolver cannot be found\n"
431  << "This should never happen. Contact a Framework developer";
432  }
433  ProductData const* productData = (phb->resolveProduct(*this,false,nullptr,nullptr)).data();
434  if (productData == nullptr) {
435  return nullptr;
436  }
437  WrapperBase const* product = productData->wrapper();
438  if(!(typeid(edm::ThinnedAssociation) == product->dynamicTypeInfo())) {
440  << "EventPrincipal::getThinnedProduct, product has wrong type, not a ThinnedAssociation.\n";
441  }
442  Wrapper<ThinnedAssociation> const* wrapper = static_cast<Wrapper<ThinnedAssociation> const*>(product);
443  return wrapper->product();
444  }
445 
446 }
RunPrincipal const & runPrincipal() const
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper_
EventNumber_t event() const
Definition: EventID.h:41
void clearPrincipal()
Definition: Principal.cc:314
unsigned short BranchListIndex
void setLuminosityBlockPrincipal(std::shared_ptr< LuminosityBlockPrincipal > const &lbp)
EventSelectionIDVector const & eventSelectionIDs() const
WrapperBase const * getThinnedProduct(ProductID const &pid, unsigned int &key) const override
unsigned int ProductResolverIndex
std::type_info const & dynamicTypeInfo() const
Definition: WrapperBase.h:43
RunNumber_t run() const
BasicHandle getByProductID(ProductID const &oid) const
EventSelectionIDVector eventSelectionIDs_
BranchID pidToBid(ProductID const &pid) const
BranchListIndexes branchListIndexes_
edm::ThinnedAssociation const * getThinnedAssociation(edm::BranchID const &branchID) const
ProductResolverBase * getExistingProduct(BranchID const &branchID)
Definition: Principal.cc:429
EventPrincipal(std::shared_ptr< ProductRegistry const > reg, std::shared_ptr< BranchIDListHelper const > branchIDListHelper, std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper, ProcessConfiguration const &pc, HistoryAppender *historyAppender, unsigned int streamIndex=0, bool isForPrimaryProcess=true)
Provenance getProvenance(ProductID const &pid, ModuleCallingContext const *mcc) const
std::map< BranchListIndex, ProcessIndex > branchListIndexToProcessIndex_
unsigned long long EventNumber_t
WrapperBase const * wrapper() const
Definition: ProductData.h:32
std::shared_ptr< BranchIDListHelper const > branchIDListHelper_
EventAuxiliary aux_
bool isValid() const
Definition: BranchID.h:24
edm::propagate_const< std::shared_ptr< ProductProvenanceRetriever > > provRetrieverPtr_
BranchListIndexes const & branchListIndexes() const
unsigned int LuminosityBlockNumber_t
ProcessHistory const & processHistory() const
Definition: Principal.h:141
ProductID branchIDToProductID(BranchID const &bid) const
RunPrincipal const & runPrincipal() const
std::vector< EventSelectionID > EventSelectionIDVector
void getThinnedProducts(ProductID const &pid, std::vector< WrapperBase const * > &foundContainers, std::vector< unsigned int > &keys) const override
static void throwProductDeletedException(ProductID const &pid, edm::EventPrincipal::ConstProductResolverPtr const phb)
EventNumber_t const invalidEventNumber
std::vector< BranchListIndex > BranchListIndexes
BranchID productIDToBranchID(ProductID const &pid, BranchIDLists const &lists, BranchListIndexes const &indexes)
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:145
LuminosityBlockPrincipal const & luminosityBlockPrincipal() const
std::shared_ptr< HandleExceptionFactory > makeHandleExceptionFactory(T &&iFunctor)
Base::ConstProductResolverPtr ConstProductResolverPtr
WrapperBase const * wrapper() const
Definition: BasicHandle.h:90
BranchID const & branchID() const
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
ProductID const & thinnedCollectionID() const
std::multimap< BranchID, IndexPair > BranchIDToIndexMap
ProductProvenanceRetriever const * productProvenanceRetrieverPtr() const
int k[5][pyjets_maxn]
unsigned short ProcessIndex
Definition: ProductID.h:25
DelayedReader * reader() const
Definition: Principal.h:184
unsigned int value() const
Definition: StreamID.h:46
void insertIntoSet(ProductProvenance provenanceProduct) const
T const * product() const
Definition: Wrapper.h:32
void putOnRead(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const *productProvenance) const
edm::propagate_const< std::shared_ptr< LuminosityBlockPrincipal > > luminosityBlockPrincipal_
void setProcessHistoryID(ProcessHistoryID const &phid)
bool isValid() const
Definition: BasicHandle.h:82
ProcessHistoryID const & processHistoryID() const
EventID const & id() const
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=0)
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
ConstProductResolverPtr getProductResolverByIndex(ProductResolverIndex const &oid) const
Definition: Principal.cc:498
ConstProductResolverPtr getProductResolver(BranchID const &oid) const
Definition: Principal.cc:489
unsigned int transitionIndex_() const override
unsigned short ProductIndex
Definition: ProductID.h:26
unsigned int RunNumber_t
ProductID const & parentCollectionID() const
bool isValid() const
Definition: ProductID.h:35
bool hasParentIndex(unsigned int parentIndex, unsigned int &thinnedIndex) const
void setRunAndLumiNumber(RunNumber_t run, LuminosityBlockNumber_t lumi)
void fillPrincipal(ProcessHistoryID const &hist, ProcessHistoryRegistry const &phr, DelayedReader *reader)
Definition: Principal.cc:346
EventAuxiliary const & aux() const
EventNumber_t event() const
def move(src, dest)
Definition: eostools.py:510
WrapperBase const * getIt(ProductID const &pid) const override
Definition: event.py:1
static HepMC::HEPEVT_Wrapper wrapper