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