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_(nullptr),
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  //do not clear luminosityBlockPrincipal_ since
56  // it is only connected at beginLumi transition
57  provRetrieverPtr_->reset();
59  }
60 
61  void
63  ProcessHistoryRegistry const& processHistoryRegistry,
66  ProductProvenanceRetriever const& provRetriever,
68  bool deepCopyRetriever) {
70  if (deepCopyRetriever) {
71  provRetrieverPtr_->deepCopy(provRetriever);
72  } else {
73  provRetrieverPtr_->mergeParentProcessRetriever(provRetriever);
74  }
76  if(branchIDListHelper_->hasProducedProducts()) {
77  // Add index into BranchIDListRegistry for products produced this process
78  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
79  }
80  fillEventPrincipal(aux,processHistoryRegistry,reader);
81  }
82 
83  void
85  ProcessHistoryRegistry const& processHistoryRegistry,
90  if(branchIDListHelper_->hasProducedProducts()) {
91  // Add index into BranchIDListRegistry for products produced this process
92  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
93  }
94  fillEventPrincipal(aux,processHistoryRegistry,nullptr);
95  }
96 
97  void
99  ProcessHistoryRegistry const& processHistoryRegistry,
101  if(aux.event() == invalidEventNumber) {
103  << "EventPrincipal::fillEventPrincipal, Invalid event number provided in EventAuxiliary, It is illegal for the event number to be 0\n";
104  }
105 
106  fillPrincipal(aux.processHistoryID(), processHistoryRegistry, reader);
107  aux_ = aux;
109 
110  if(branchListIndexes_.empty() and branchIDListHelper_->hasProducedProducts()) {
111  // Add index into BranchIDListRegistry for products produced this process
112  // if it hasn't already been filled in by the other fillEventPrincipal or by an earlier call to this function
113  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
114  }
115 
116  // Fill in helper map for Branch to ProductID mapping
117  ProcessIndex pix = 0;
118  for(auto const& blindex : branchListIndexes_) {
119  branchListIndexToProcessIndex_.insert(std::make_pair(blindex, pix));
120  ++pix;
121  }
122 
123  // Fill in the product ID's in the product holders.
124  for(auto& prod : *this) {
125  if (prod->singleProduct()) {
126  // If an alias is in the same process as the original then isAlias will be true.
127  // Under that condition, we want the ProductID to be the same as the original.
128  // If not, then we've internally changed the original BranchID to the alias BranchID
129  // in the ProductID lookup so we need the alias BranchID.
130  auto const & bd =prod->branchDescription();
131  prod->setProvenance(productProvenanceRetrieverPtr(),
132  processHistory(),
133  branchIDToProductID(bd.isAlias()?bd.originalBranchID(): bd.branchID()));
134  }
135  }
136  }
137 
138  void
141  }
142 
143  void
145  assert(run == luminosityBlockPrincipal_->run());
146  assert(lumi == luminosityBlockPrincipal_->luminosityBlock());
147  EventNumber_t event = aux_.id().event();
148  aux_.id() = EventID(run, lumi, event);
149  }
150 
151  RunPrincipal const&
154  }
155 
156  void
158  BranchDescription const& bd,
159  std::unique_ptr<WrapperBase> edp,
160  ProductProvenance const& productProvenance) const {
161 
162  // assert commented out for DaqSource. When DaqSource no longer uses put(), the assert can be restored.
163  //assert(produced());
164  if(edp.get() == nullptr) {
165  throw Exception(errors::InsertFailure, "Null Pointer")
166  << "put: Cannot put because ptr to product is null."
167  << "\n";
168  }
169  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
170  auto phb = getExistingProduct(bd.branchID());
171  assert(phb);
172  // ProductResolver assumes ownership
173  phb->putProduct(std::move(edp));
174  }
175 
176  void
179  std::unique_ptr<WrapperBase> edp,
180  ParentageID parentage) const {
181  if(edp.get() == nullptr) {
182  throw Exception(errors::InsertFailure, "Null Pointer")
183  << "put: Cannot put because ptr to product is null."
184  << "\n";
185  }
186  auto phb = getProductResolverByIndex(index);
187 
188  productProvenanceRetrieverPtr()->insertIntoSet(ProductProvenance(phb->branchDescription().branchID(), std::move(parentage)));
189 
190  assert(phb);
191  // ProductResolver assumes ownership
192  phb->putProduct(std::move(edp));
193 
194  }
195 
196  void
198  BranchDescription const& bd,
199  std::unique_ptr<WrapperBase> edp,
200  ProductProvenance const* productProvenance) const {
201 
202  assert(!bd.produced());
203  if (productProvenance) {
204  productProvenanceRetrieverPtr()->insertIntoSet(*productProvenance);
205  }
206  auto phb = getExistingProduct(bd.branchID());
207  assert(phb);
208  // ProductResolver assumes ownership
209  phb->putProduct(std::move(edp));
210  }
211 
212  BranchID
214  if(!pid.isValid()) {
215  throw Exception(errors::ProductNotFound, "InvalidID")
216  << "get by product ID: invalid ProductID supplied\n";
217  }
218  return productIDToBranchID(pid, branchIDListHelper_->branchIDLists(), branchListIndexes_);
219  }
220 
221  ProductID
223  if(!bid.isValid()) {
224  throw Exception(errors::NotFound, "InvalidID")
225  << "branchIDToProductID: invalid BranchID supplied\n";
226  }
227  typedef BranchIDListHelper::BranchIDToIndexMap BIDToIndexMap;
228  typedef BIDToIndexMap::const_iterator Iter;
229  typedef std::pair<Iter, Iter> IndexRange;
230 
231  IndexRange range = branchIDListHelper_->branchIDToIndexMap().equal_range(bid);
232  for(Iter it = range.first; it != range.second; ++it) {
233  BranchListIndex blix = it->second.first;
234  std::map<BranchListIndex, ProcessIndex>::const_iterator i = branchListIndexToProcessIndex_.find(blix);
235  if(i != branchListIndexToProcessIndex_.end()) {
236  ProductIndex productIndex = it->second.second;
237  ProcessIndex processIndex = i->second;
238  return ProductID(processIndex+1, productIndex+1);
239  }
240  }
241  // cannot throw, because some products may legitimately not have product ID's (e.g. pile-up).
242  return ProductID();
243  }
244 
245  unsigned int
247  return streamID_.value();
248  }
249 
252  exception<<"get by product ID: The product with given id: "<<pid
253  <<"\ntype: "<<phb->productType()
254  <<"\nproduct instance name: "<<phb->productInstanceName()
255  <<"\nprocess name: "<<phb->processName()
256  <<"\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.";
257  throw exception;
258  }
259 
262  BranchID bid = pidToBid(pid);
264  if(phb == nullptr) {
265  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
266  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
267  *whyFailed
268  << "get by product ID: no product with given id: " << pid << "\n";
269  return whyFailed;
270  }));
271  }
272 
273  // Was this already deleted?
274  if(phb->productWasDeleted()) {
276  }
277  // Check for case where we tried on demand production and
278  // it failed to produce the object
279  if(phb->unscheduledWasNotRun()) {
280  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
281  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
282  *whyFailed
283  << "get by ProductID: could not get product with id: " << pid << "\n"
284  << "Unscheduled execution not allowed to get via ProductID.\n";
285  return whyFailed;
286  }));
287  }
288  auto resolution = phb->resolveProduct(*this,false,nullptr,nullptr);
289 
290  auto data = resolution.data();
291  if(data) {
292  return BasicHandle(data->wrapper(), &(data->provenance()));
293  }
294  return BasicHandle(nullptr,nullptr);
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 
416  return eventSelectionIDs_;
417  }
418 
419  BranchListIndexes const&
421  return branchListIndexes_;
422  }
423 
426 
427  ConstProductResolverPtr const phb = getProductResolver(branchID);
428 
429  if(phb == nullptr) {
431  << "EventPrincipal::getThinnedAssociation, ThinnedAssociation ProductResolver cannot be found\n"
432  << "This should never happen. Contact a Framework developer";
433  }
434  ProductData const* productData = (phb->resolveProduct(*this,false,nullptr,nullptr)).data();
435  if (productData == nullptr) {
436  return nullptr;
437  }
438  WrapperBase const* product = productData->wrapper();
439  if(!(typeid(edm::ThinnedAssociation) == product->dynamicTypeInfo())) {
441  << "EventPrincipal::getThinnedProduct, product has wrong type, not a ThinnedAssociation.\n";
442  }
443  Wrapper<ThinnedAssociation> const* wrapper = static_cast<Wrapper<ThinnedAssociation> const*>(product);
444  return wrapper->product();
445  }
446 
447 }
RunPrincipal const & runPrincipal() const
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper_
void setLuminosityBlockPrincipal(LuminosityBlockPrincipal *lbp)
EventNumber_t event() const
Definition: EventID.h:41
void clearPrincipal()
Definition: Principal.cc:333
unsigned short BranchListIndex
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:444
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
edm::propagate_const< LuminosityBlockPrincipal * > luminosityBlockPrincipal_
WrapperBase const * wrapper() const
Definition: ProductData.h:32
std::shared_ptr< BranchIDListHelper const > branchIDListHelper_
EventAuxiliary aux_
#define nullptr
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:139
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:143
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:182
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
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:513
ConstProductResolverPtr getProductResolver(BranchID const &oid) const
Definition: Principal.cc:504
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:360
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