CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EventPrincipal.cc
Go to the documentation of this file.
2 
24 
25 #include <algorithm>
26 #include <cassert>
27 #include <limits>
28 #include <memory>
29 
30 namespace edm {
32  std::shared_ptr<ProductRegistry const> reg,
33  std::shared_ptr<BranchIDListHelper const> branchIDListHelper,
34  std::shared_ptr<ThinnedAssociationsHelper const> thinnedAssociationsHelper,
35  ProcessConfiguration const& pc,
36  HistoryAppender* historyAppender,
37  unsigned int streamIndex) :
38  Base(reg, reg->productLookup(InEvent), pc, InEvent, historyAppender),
39  aux_(),
40  luminosityBlockPrincipal_(),
41  provRetrieverPtr_(new ProductProvenanceRetriever(streamIndex)),
42  unscheduledHandler_(),
43  eventSelectionIDs_(),
44  branchIDListHelper_(branchIDListHelper),
45  thinnedAssociationsHelper_(thinnedAssociationsHelper),
46  branchListIndexes_(),
47  branchListIndexToProcessIndex_(),
48  streamID_(streamIndex) {
50  }
51 
52  void
55  aux_ = EventAuxiliary();
56  luminosityBlockPrincipal_ = nullptr; // propagate_const<T> has no reset() function
57  provRetrieverPtr_->reset();
58  unscheduledHandler_.reset();
60  }
61 
62  void
64  ProcessHistoryRegistry const& processHistoryRegistry,
65  EventSelectionIDVector&& eventSelectionIDs,
66  BranchListIndexes&& branchListIndexes,
67  ProductProvenanceRetriever const& provRetriever,
70  provRetrieverPtr_->deepCopy(provRetriever);
72  if(branchIDListHelper_->hasProducedProducts()) {
73  // Add index into BranchIDListRegistry for products produced this process
74  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
75  }
76  fillEventPrincipal(aux,processHistoryRegistry,reader);
77  }
78 
79  void
81  ProcessHistoryRegistry const& processHistoryRegistry,
82  EventSelectionIDVector&& eventSelectionIDs,
83  BranchListIndexes&& branchListIndexes) {
86  if(branchIDListHelper_->hasProducedProducts()) {
87  // Add index into BranchIDListRegistry for products produced this process
88  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
89  }
90  fillEventPrincipal(aux,processHistoryRegistry,nullptr);
91  }
92 
93  void
95  ProcessHistoryRegistry const& processHistoryRegistry,
97  if(aux.event() == invalidEventNumber) {
99  << "EventPrincipal::fillEventPrincipal, Invalid event number provided in EventAuxiliary, It is illegal for the event number to be 0\n";
100  }
101 
102  fillPrincipal(aux.processHistoryID(), processHistoryRegistry, reader);
103  aux_ = aux;
105 
106  if(branchListIndexes_.empty() and branchIDListHelper_->hasProducedProducts()) {
107  // Add index into BranchIDListRegistry for products produced this process
108  // if it hasn't already been filled in by the other fillEventPrincipal or by an earlier call to this function
109  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
110  }
111 
112  // Fill in helper map for Branch to ProductID mapping
113  ProcessIndex pix = 0;
114  for(auto const& blindex : branchListIndexes_) {
115  branchListIndexToProcessIndex_.insert(std::make_pair(blindex, pix));
116  ++pix;
117  }
118 
119  // Fill in the product ID's in the product holders.
120  for(auto& prod : *this) {
121  if (prod->singleProduct()) {
122  // If an alias is in the same process as the original then isAlias will be true.
123  // Under that condition, we want the ProductID to be the same as the original.
124  // If not, then we've internally changed the original BranchID to the alias BranchID
125  // in the ProductID lookup so we need the alias BranchID.
126  auto const & bd =prod->branchDescription();
127  prod->setProvenance(productProvenanceRetrieverPtr(),
128  processHistory(),
129  branchIDToProductID(bd.isAlias()?bd.originalBranchID(): bd.branchID()));
130  }
131  }
132  }
133 
134  void
135  EventPrincipal::setLuminosityBlockPrincipal(std::shared_ptr<LuminosityBlockPrincipal> const& lbp) {
137  }
138 
139  void
141  assert(run == luminosityBlockPrincipal_->run());
142  assert(lumi == luminosityBlockPrincipal_->luminosityBlock());
143  EventNumber_t event = aux_.id().event();
144  aux_.id() = EventID(run, lumi, event);
145  }
146 
147  RunPrincipal const&
150  }
151 
152  void
154  BranchDescription const& bd,
155  std::unique_ptr<WrapperBase> edp,
156  ProductProvenance const& productProvenance) const {
157 
158  // assert commented out for DaqSource. When DaqSource no longer uses put(), the assert can be restored.
159  //assert(produced());
160  if(edp.get() == nullptr) {
161  throw Exception(errors::InsertFailure, "Null Pointer")
162  << "put: Cannot put because ptr to product is null."
163  << "\n";
164  }
165  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
166  auto phb = getExistingProduct(bd.branchID());
167  assert(phb);
168  checkUniquenessAndType(edp.get(), phb);
169  // ProductHolder assumes ownership
170  phb->putProduct(std::move(edp), productProvenance);
171  }
172 
173  void
175  BranchDescription const& bd,
176  std::unique_ptr<WrapperBase> edp,
177  ProductProvenance const& productProvenance) const {
178 
179  assert(!bd.produced());
180  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
181  auto phb = getExistingProduct(bd.branchID());
182  assert(phb);
183  checkUniquenessAndType(edp.get(), phb);
184  // ProductHolder assumes ownership
185  phb->putProduct(std::move(edp), productProvenance);
186  }
187 
188  void
190  if(phb.branchDescription().produced()) return; // nothing to do.
191  if(phb.product()) return; // nothing to do.
192  if(phb.productUnavailable()) return; // nothing to do.
193  if(!reader()) return; // nothing to do.
194 
195  // must attempt to load from persistent store
196  BranchKey const bk = BranchKey(phb.branchDescription());
197  {
198  if(mcc) {
200  }
201  std::shared_ptr<void> guard(nullptr,[this,mcc](const void*){
202  if(mcc) {
204  }
205  });
206 
207  std::unique_ptr<WrapperBase> edp(reader()->getProduct(bk, this));
208 
209  // Now fix up the ProductHolder
210  checkUniquenessAndType(edp.get(), &phb);
211  phb.putProduct(std::move(edp));
212  }
213  }
214 
215  BranchID
217  if(!pid.isValid()) {
218  throw Exception(errors::ProductNotFound, "InvalidID")
219  << "get by product ID: invalid ProductID supplied\n";
220  }
221  return productIDToBranchID(pid, branchIDListHelper_->branchIDLists(), branchListIndexes_);
222  }
223 
224  ProductID
226  if(!bid.isValid()) {
227  throw Exception(errors::NotFound, "InvalidID")
228  << "branchIDToProductID: invalid BranchID supplied\n";
229  }
230  typedef BranchIDListHelper::BranchIDToIndexMap BIDToIndexMap;
231  typedef BIDToIndexMap::const_iterator Iter;
232  typedef std::pair<Iter, Iter> IndexRange;
233 
234  IndexRange range = branchIDListHelper_->branchIDToIndexMap().equal_range(bid);
235  for(Iter it = range.first; it != range.second; ++it) {
236  BranchListIndex blix = it->second.first;
237  std::map<BranchListIndex, ProcessIndex>::const_iterator i = branchListIndexToProcessIndex_.find(blix);
238  if(i != branchListIndexToProcessIndex_.end()) {
239  ProductIndex productIndex = it->second.second;
240  ProcessIndex processIndex = i->second;
241  return ProductID(processIndex+1, productIndex+1);
242  }
243  }
244  // cannot throw, because some products may legitimately not have product ID's (e.g. pile-up).
245  return ProductID();
246  }
247 
248  unsigned int
250  return streamID_.value();
251  }
252 
255  exception<<"get by product ID: The product with given id: "<<pid
256  <<"\ntype: "<<phb->productType()
257  <<"\nproduct instance name: "<<phb->productInstanceName()
258  <<"\nprocess name: "<<phb->processName()
259  <<"\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.";
260  throw exception;
261  }
262 
263  BasicHandle
265  BranchID bid = pidToBid(pid);
266  ConstProductHolderPtr const phb = getProductHolder(bid);
267  if(phb == nullptr) {
268  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
269  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
270  *whyFailed
271  << "get by product ID: no product with given id: " << pid << "\n";
272  return whyFailed;
273  }));
274  }
275 
276  // Was this already deleted?
277  if(phb->productWasDeleted()) {
279  }
280  // Check for case where we tried on demand production and
281  // it failed to produce the object
282  if(phb->onDemand()) {
283  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
284  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
285  *whyFailed
286  << "get by ProductID: could not get product with id: " << pid << "\n"
287  << "Unscheduled execution not allowed to get via ProductID.\n";
288  return whyFailed;
289  }));
290  }
292  phb->resolveProduct(status,*this,false,nullptr,nullptr);
293 
294  return BasicHandle(phb->productData());
295  }
296 
297  WrapperBase const*
299  return getByProductID(pid).wrapper();
300  }
301 
302  WrapperBase const*
303  EventPrincipal::getThinnedProduct(ProductID const& pid, unsigned int& key) const {
304 
305  BranchID parent = pidToBid(pid);
306 
307  // Loop over thinned containers which were made by selecting elements from the parent container
308  for(auto associatedBranches = thinnedAssociationsHelper_->parentBegin(parent),
309  iEnd = thinnedAssociationsHelper_->parentEnd(parent);
310  associatedBranches != iEnd; ++associatedBranches) {
311 
312  ThinnedAssociation const* thinnedAssociation =
313  getThinnedAssociation(associatedBranches->association());
314  if(thinnedAssociation == nullptr) continue;
315 
316  if(associatedBranches->parent() != pidToBid(thinnedAssociation->parentCollectionID())) {
317  continue;
318  }
319 
320  unsigned int thinnedIndex = 0;
321  // Does this thinned container have the element referenced by key?
322  // If yes, thinnedIndex is set to point to it in the thinned container
323  if(!thinnedAssociation->hasParentIndex(key, thinnedIndex)) {
324  continue;
325  }
326  // Get the thinned container and return a pointer if we can find it
327  ProductID const& thinnedCollectionPID = thinnedAssociation->thinnedCollectionID();
328  BasicHandle bhThinned = getByProductID(thinnedCollectionPID);
329  if(!bhThinned.isValid()) {
330  // Thinned container is not found, try looking recursively in thinned containers
331  // which were made by selecting elements from this thinned container.
332  WrapperBase const* wrapperBase = getThinnedProduct(thinnedCollectionPID, thinnedIndex);
333  if(wrapperBase != nullptr) {
334  key = thinnedIndex;
335  return wrapperBase;
336  } else {
337  continue;
338  }
339  }
340  key = thinnedIndex;
341  return bhThinned.wrapper();
342  }
343  return nullptr;
344  }
345 
346  void
348  std::vector<WrapperBase const*>& foundContainers,
349  std::vector<unsigned int>& keys) const {
350 
351  BranchID parent = pidToBid(pid);
352 
353  // Loop over thinned containers which were made by selecting elements from the parent container
354  for(auto associatedBranches = thinnedAssociationsHelper_->parentBegin(parent),
355  iEnd = thinnedAssociationsHelper_->parentEnd(parent);
356  associatedBranches != iEnd; ++associatedBranches) {
357 
358  ThinnedAssociation const* thinnedAssociation =
359  getThinnedAssociation(associatedBranches->association());
360  if(thinnedAssociation == nullptr) continue;
361 
362  if(associatedBranches->parent() != pidToBid(thinnedAssociation->parentCollectionID())) {
363  continue;
364  }
365 
366  unsigned nKeys = keys.size();
367  unsigned int doNotLookForThisIndex = std::numeric_limits<unsigned int>::max();
368  std::vector<unsigned int> thinnedIndexes(nKeys, doNotLookForThisIndex);
369  bool hasAny = false;
370  for(unsigned k = 0; k < nKeys; ++k) {
371  // Already found this one
372  if(foundContainers[k] != nullptr) continue;
373  // Already know this one is not in this thinned container
374  if(keys[k] == doNotLookForThisIndex) continue;
375  // Does the thinned container hold the entry of interest?
376  // Modifies thinnedIndexes[k] only if it returns true and
377  // sets it to the index in the thinned collection.
378  if(thinnedAssociation->hasParentIndex(keys[k], thinnedIndexes[k])) {
379  hasAny = true;
380  }
381  }
382  if(!hasAny) {
383  continue;
384  }
385  // Get the thinned container and set the pointers and indexes into
386  // it (if we can find it)
387  ProductID thinnedCollectionPID = thinnedAssociation->thinnedCollectionID();
388  BasicHandle bhThinned = getByProductID(thinnedCollectionPID);
389  if(!bhThinned.isValid()) {
390  // Thinned container is not found, try looking recursively in thinned containers
391  // which were made by selecting elements from this thinned container.
392  getThinnedProducts(thinnedCollectionPID, foundContainers, thinnedIndexes);
393  for(unsigned k = 0; k < nKeys; ++k) {
394  if(foundContainers[k] == nullptr) continue;
395  if(thinnedIndexes[k] == doNotLookForThisIndex) continue;
396  keys[k] = thinnedIndexes[k];
397  }
398  } else {
399  for(unsigned k = 0; k < nKeys; ++k) {
400  if(thinnedIndexes[k] == doNotLookForThisIndex) continue;
401  keys[k] = thinnedIndexes[k];
402  foundContainers[k] = bhThinned.wrapper();
403  }
404  }
405  }
406  }
407 
408  Provenance
410  BranchID bid = pidToBid(pid);
411  return getProvenance(bid, mcc);
412  }
413 
414  void
415  EventPrincipal::setUnscheduledHandler(std::shared_ptr<UnscheduledHandler> iHandler) {
416  unscheduledHandler_ = iHandler;
417  }
418 
419  std::shared_ptr<const UnscheduledHandler>
421  return unscheduledHandler_;
422  }
423 
426  return eventSelectionIDs_;
427  }
428 
429  BranchListIndexes const&
431  return branchListIndexes_;
432  }
433 
436 
437  ConstProductHolderPtr const phb = getProductHolder(branchID);
438 
439  if(phb == nullptr) {
441  << "EventPrincipal::getThinnedAssociation, ThinnedAssociation ProductHolder cannot be found\n"
442  << "This should never happen. Contact a Framework developer";
443  }
445  ProductData const* productData = phb->resolveProduct(status,*this,false,nullptr,nullptr);
446  if (productData == nullptr) {
447  return nullptr;
448  }
449  WrapperBase const* product = productData->wrapper();
450  if(!(typeid(edm::ThinnedAssociation) == product->dynamicTypeInfo())) {
452  << "EventPrincipal::getThinnedProduct, product has wrong type, not a ThinnedAssociation.\n";
453  }
455  return wrapper->product();
456  }
457 
458  bool
461  ModuleCallingContext const* mcc) const {
462  if(unscheduledHandler_) {
463  if(mcc == nullptr) {
465  << "EventPrincipal::unscheduledFill, Attempting to run unscheduled production\n"
466  << "with a null pointer to the ModuleCalling Context. This should never happen.\n"
467  << "Contact a Framework developer";
468  }
470  std::shared_ptr<void> guard(nullptr,[this,mcc](const void*){
472  });
473  auto handlerCall = [this,&moduleLabel,&mcc]() {
474  unscheduledHandler_->tryToFill(moduleLabel, *this, mcc);
475  };
476  if (sra) {
477  sra->temporaryUnlock(handlerCall);
478  } else {
479  handlerCall();
480  }
481  }
482  return true;
483  }
484 }
RunPrincipal const & runPrincipal() const
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper_
EventNumber_t event() const
Definition: EventID.h:41
void clearPrincipal()
Definition: Principal.cc:321
int i
Definition: DBlmapReader.cc:9
unsigned short BranchListIndex
WrapperBase const * product() const
Definition: ProductHolder.h:85
void setLuminosityBlockPrincipal(std::shared_ptr< LuminosityBlockPrincipal > const &lbp)
EventSelectionIDVector const & eventSelectionIDs() const
ConstProductHolderPtr getProductHolder(BranchID const &oid) const
Definition: Principal.cc:460
StreamContext const * getStreamContext() const
std::type_info const & dynamicTypeInfo() const
Definition: WrapperBase.h:38
BasicHandle getByProductID(ProductID const &oid) const
EventSelectionIDVector eventSelectionIDs_
BranchID pidToBid(ProductID const &pid) const
tuple lumi
Definition: fjr2json.py:35
BranchListIndexes branchListIndexes_
edm::ThinnedAssociation const * getThinnedAssociation(edm::BranchID const &branchID) const
assert(m_qm.get())
Provenance getProvenance(ProductID const &pid, ModuleCallingContext const *mcc) const
std::map< BranchListIndex, ProcessIndex > branchListIndexToProcessIndex_
unsigned long long EventNumber_t
T const * getProduct(RefCore const &ref)
Definition: RefCoreGet.h:41
WrapperBase const * wrapper() const
Definition: ProductData.h:32
std::shared_ptr< BranchIDListHelper const > branchIDListHelper_
virtual void readFromSource_(ProductHolderBase const &phb, ModuleCallingContext const *mcc) const override
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:144
ProductID branchIDToProductID(BranchID const &bid) const
void putOnRead(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
RunPrincipal const & runPrincipal() const
std::vector< EventSelectionID > EventSelectionIDVector
virtual WrapperBase const * getThinnedProduct(ProductID const &pid, unsigned int &key) const override
static void throwProductDeletedException(ProductID const &pid, edm::EventPrincipal::ConstProductHolderPtr const phb)
void setUnscheduledHandler(std::shared_ptr< UnscheduledHandler > iHandler)
std::shared_ptr< const UnscheduledHandler > unscheduledHandler() const
EventNumber_t const invalidEventNumber
std::vector< BranchListIndex > BranchListIndexes
BranchDescription const & branchDescription() const
Definition: ProductHolder.h:93
BranchID productIDToBranchID(ProductID const &pid, BranchIDLists const &lists, BranchListIndexes const &indexes)
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:148
LuminosityBlockPrincipal const & luminosityBlockPrincipal() const
std::shared_ptr< HandleExceptionFactory > makeHandleExceptionFactory(T &&iFunctor)
void checkUniquenessAndType(WrapperBase const *prod, ProductHolderBase const *productHolder) const
Definition: Principal.cc:843
def move
Definition: eostools.py:510
WrapperBase const * wrapper() const
Definition: BasicHandle.h:98
virtual WrapperBase const * getIt(ProductID const &pid) const override
BranchID const & branchID() const
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
ProductID const & thinnedCollectionID() const
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
std::multimap< BranchID, IndexPair > BranchIDToIndexMap
ProductProvenanceRetriever const * productProvenanceRetrieverPtr() const
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
unsigned short ProcessIndex
Definition: ProductID.h:25
DelayedReader * reader() const
Definition: Principal.h:185
unsigned int value() const
Definition: StreamID.h:46
virtual unsigned int transitionIndex_() const override
T const * product() const
Definition: Wrapper.h:32
void putProduct(std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
tuple pid
Definition: sysUtil.py:22
string const
Definition: compareJSON.py:14
edm::propagate_const< std::shared_ptr< LuminosityBlockPrincipal > > luminosityBlockPrincipal_
void setProcessHistoryID(ProcessHistoryID const &phid)
bool isValid() const
Definition: BasicHandle.h:90
ProcessHistoryID const & processHistoryID() const
EventID const & id() const
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=0)
Base::ConstProductHolderPtr ConstProductHolderPtr
unsigned short ProductIndex
Definition: ProductID.h:26
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)
unsigned int RunNumber_t
ProductID const & parentCollectionID() const
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> preModuleDelayedGetSignal_
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:344
EventAuxiliary const & aux() const
virtual void getThinnedProducts(ProductID const &pid, std::vector< WrapperBase const * > &foundContainers, std::vector< unsigned int > &keys) const override
EventNumber_t event() const
void emit(Args &&...args) const
Definition: Signal.h:47
ProductHolderBase * getExistingProduct(BranchID const &branchID)
Definition: Principal.cc:400
tuple status
Definition: mps_update.py:57
static HepMC::HEPEVT_Wrapper wrapper
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> postModuleDelayedGetSignal_
std::shared_ptr< UnscheduledHandler const > unscheduledHandler_
virtual bool unscheduledFill(std::string const &moduleLabel, SharedResourcesAcquirer *sra, ModuleCallingContext const *mcc) const override
bool productUnavailable() const
Definition: ProductHolder.h:73
void insertIntoSet(ProductProvenance const &provenanceProduct) const