CMS 3D CMS Logo

EventPrincipal.cc
Go to the documentation of this file.
2 
26 
27 #include <algorithm>
28 #include <cassert>
29 #include <limits>
30 #include <memory>
31 
32 namespace edm {
33  EventPrincipal::EventPrincipal(std::shared_ptr<ProductRegistry const> reg,
34  std::shared_ptr<BranchIDListHelper const> branchIDListHelper,
35  std::shared_ptr<ThinnedAssociationsHelper const> thinnedAssociationsHelper,
36  ProcessConfiguration const& pc,
37  HistoryAppender* historyAppender,
38  unsigned int streamIndex,
39  bool isForPrimaryProcess,
40  ProcessBlockHelperBase const* processBlockHelper)
41  : Base(reg, reg->productLookup(InEvent), pc, InEvent, historyAppender, isForPrimaryProcess),
42  aux_(),
43  luminosityBlockPrincipal_(nullptr),
44  provRetrieverPtr_(new ProductProvenanceRetriever(streamIndex, *reg)),
45  eventSelectionIDs_(),
46  branchIDListHelper_(branchIDListHelper),
47  processBlockHelper_(processBlockHelper),
48  thinnedAssociationsHelper_(thinnedAssociationsHelper),
49  branchListIndexes_(),
50  branchListIndexToProcessIndex_(),
51  streamID_(streamIndex) {
53 
54  for (auto& prod : *this) {
55  if (prod->singleProduct()) {
56  prod->setProductProvenanceRetriever(productProvenanceRetrieverPtr());
57  }
58  }
59  }
60 
63  aux_ = EventAuxiliary();
64  //do not clear luminosityBlockPrincipal_ since
65  // it is only connected at beginLumi transition
66  provRetrieverPtr_->reset();
67  }
68 
70  ProcessHistory const* processHistory,
71  EventSelectionIDVector eventSelectionIDs,
72  BranchListIndexes branchListIndexes,
73  EventToProcessBlockIndexes const& eventToProcessBlockIndexes,
74  ProductProvenanceRetriever const& provRetriever,
76  bool deepCopyRetriever) {
78  if (deepCopyRetriever) {
79  provRetrieverPtr_->deepCopy(provRetriever);
80  } else {
81  provRetrieverPtr_->mergeParentProcessRetriever(provRetriever);
82  }
84  if (branchIDListHelper_->hasProducedProducts()) {
85  // Add index into BranchIDListRegistry for products produced this process
86  branchListIndexes.push_back(branchIDListHelper_->producedBranchListIndex());
87  }
89  }
92  }
93 
95  ProcessHistory const* processHistory,
96  EventSelectionIDVector eventSelectionIDs,
97  BranchListIndexes branchListIndexes,
100 
102  if (branchIDListHelper_->hasProducedProducts()) {
103  // Add index into BranchIDListRegistry for products produced this process
104  branchListIndexes.push_back(branchIDListHelper_->producedBranchListIndex());
105  }
107  }
109  }
110 
112  ProcessHistory const* processHistory,
114  if (branchListIndexes_.empty() and branchIDListHelper_->hasProducedProducts()) {
115  // Add index into BranchIDListRegistry for products produced this process
116  // if it hasn't already been filled in by the other fillEventPrincipal or by an earlier call to this function
117  BranchListIndexes indexes;
118  indexes.push_back(branchIDListHelper_->producedBranchListIndex());
120  }
122  }
123 
125  ProcessHistory const* processHistory,
127  if (aux.event() == invalidEventNumber) {
128  throw Exception(errors::LogicError) << "EventPrincipal::fillEventPrincipal, Invalid event number provided in "
129  "EventAuxiliary, It is illegal for the event number to be 0\n";
130  }
131 
133  aux_ = aux;
135  }
136 
138  //fromInput does not contain entries for what is being produced in this job.
139  auto end = branchListIndexes_.end();
140  if (end != branchListIndexes_.begin() and branchIDListHelper_->hasProducedProducts()) {
141  --end;
142  }
143 
144  return not std::equal(fromInput.begin(), fromInput.end(), branchListIndexes_.begin(), end);
145  }
146 
150  // Fill in helper map for Branch to ProductID mapping
151  if (not branchListIndexes_.empty()) {
152  ProcessIndex pix = 0;
153  branchListIndexToProcessIndex_.resize(1 + *std::max_element(branchListIndexes_.begin(), branchListIndexes_.end()),
155  for (auto const& blindex : branchListIndexes_) {
156  branchListIndexToProcessIndex_[blindex] = pix;
157  ++pix;
158  }
159  }
160 
161  // Fill in the product ID's in the product holders.
162  for (auto& prod : *this) {
163  if (prod->singleProduct()) {
164  // If an alias is in the same process as the original then isAlias will be true.
165  // Under that condition, we want the ProductID to be the same as the original.
166  // If not, then we've internally changed the original BranchID to the alias BranchID
167  // in the ProductID lookup so we need the alias BranchID.
168 
169  auto const& bd = prod->branchDescription();
170  prod->setProductID(branchIDToProductID(bd.isAlias() ? bd.originalBranchID() : bd.branchID()));
171  }
172  }
173  }
174 
176 
179  assert(lumi == luminosityBlockPrincipal_->luminosityBlock());
180  EventNumber_t event = aux_.id().event();
181  aux_.id() = EventID(run, lumi, event);
182  }
183 
185 
187  std::unique_ptr<WrapperBase> edp,
188  ProductProvenance const& productProvenance) const {
189  // assert commented out for DaqSource. When DaqSource no longer uses put(), the assert can be restored.
190  //assert(produced());
191  if (edp.get() == nullptr) {
192  throw Exception(errors::InsertFailure, "Null Pointer") << "put: Cannot put because ptr to product is null."
193  << "\n";
194  }
195  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
196  auto phb = getExistingProduct(bd.branchID());
197  assert(phb);
198  // ProductResolver assumes ownership
199  dynamic_cast<ProductPutterBase const*>(phb)->putProduct(std::move(edp));
200  }
201 
202  void EventPrincipal::put(ProductResolverIndex index, std::unique_ptr<WrapperBase> edp, ParentageID parentage) const {
203  if (edp.get() == nullptr) {
204  throw Exception(errors::InsertFailure, "Null Pointer") << "put: Cannot put because ptr to product is null."
205  << "\n";
206  }
207  auto phb = getProductResolverByIndex(index);
208 
210  ProductProvenance(phb->branchDescription().branchID(), std::move(parentage)));
211 
212  assert(phb);
213  // ProductResolver assumes ownership
214  dynamic_cast<ProductPutterBase const*>(phb)->putProduct(std::move(edp));
215  }
216 
218  std::unique_ptr<WrapperBase> edp,
219  std::optional<ProductProvenance> productProvenance) const {
220  assert(!bd.produced());
221  if (productProvenance) {
222  productProvenanceRetrieverPtr()->insertIntoSet(std::move(*productProvenance));
223  }
224  auto phb = getExistingProduct(bd.branchID());
225  assert(phb);
226  // ProductResolver assumes ownership
227  dynamic_cast<ProductPutterBase const*>(phb)->putProduct(std::move(edp));
228  }
229 
231  if (!pid.isValid()) {
232  throw Exception(errors::ProductNotFound, "InvalidID") << "get by product ID: invalid ProductID supplied\n";
233  }
234  return productIDToBranchID(pid, branchIDListHelper_->branchIDLists(), branchListIndexes_);
235  }
236 
238  if (!bid.isValid()) {
239  throw Exception(errors::NotFound, "InvalidID") << "branchIDToProductID: invalid BranchID supplied\n";
240  }
241  typedef BranchIDListHelper::BranchIDToIndexMap BIDToIndexMap;
242  typedef BIDToIndexMap::const_iterator Iter;
243  typedef std::pair<Iter, Iter> IndexRange;
244 
245  IndexRange range = branchIDListHelper_->branchIDToIndexMap().equal_range(bid);
246  for (Iter it = range.first; it != range.second; ++it) {
247  BranchListIndex blix = it->second.first;
248  if (blix < branchListIndexToProcessIndex_.size()) {
249  auto v = branchListIndexToProcessIndex_[blix];
251  ProductIndex productIndex = it->second.second;
252  ProcessIndex processIndex = v;
253  return ProductID(processIndex + 1, productIndex + 1);
254  }
255  }
256  }
257  // cannot throw, because some products may legitimately not have product ID's (e.g. pile-up).
258  return ProductID();
259  }
260 
263  }
264 
265  unsigned int EventPrincipal::transitionIndex_() const { return streamID_.value(); }
266 
268 
269  static void throwProductDeletedException(ProductID const& pid,
272  exception << "get by product ID: The product with given id: " << pid << "\ntype: " << phb->productType()
273  << "\nproduct instance name: " << phb->productInstanceName() << "\nprocess name: " << phb->processName()
274  << "\nwas already deleted. This is a configuration error. Please change the configuration of the module "
275  "which caused this exception to state it reads this data.";
276  throw exception;
277  }
278 
280  BranchID bid = pidToBid(pid);
282  if (phb == nullptr) {
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 << "get by product ID: no product with given id: " << pid << "\n";
286  return whyFailed;
287  }));
288  }
289 
290  // Was this already deleted?
291  if (phb->productWasDeleted()) {
293  }
294  // Check for case where we tried on demand production and
295  // it failed to produce the object
296  if (phb->unscheduledWasNotRun()) {
297  return BasicHandle(makeHandleExceptionFactory([pid]() -> std::shared_ptr<cms::Exception> {
298  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
299  *whyFailed << "get by ProductID: could not get product with id: " << pid << "\n"
300  << "Unscheduled execution not allowed to get via ProductID.\n";
301  return whyFailed;
302  }));
303  }
304  auto resolution = phb->resolveProduct(*this, false, nullptr, nullptr);
305 
306  auto data = resolution.data();
307  if (data) {
308  return BasicHandle(data->wrapper(), &(data->provenance()));
309  }
310  return BasicHandle(nullptr, nullptr);
311  }
312 
313  WrapperBase const* EventPrincipal::getIt(ProductID const& pid) const { return getByProductID(pid).wrapper(); }
314 
315  std::optional<std::tuple<WrapperBase const*, unsigned int>> EventPrincipal::getThinnedProduct(
316  ProductID const& pid, unsigned int key) const {
318  pid,
319  key,
321  [this](ProductID const& p) { return pidToBid(p); },
322  [this](BranchID const& b) { return getThinnedAssociation(b); },
323  [this](ProductID const& p) { return getIt(p); });
324  }
325 
327  std::vector<WrapperBase const*>& foundContainers,
328  std::vector<unsigned int>& keys) const {
330  pid,
332  [this](ProductID const& p) { return pidToBid(p); },
333  [this](BranchID const& b) { return getThinnedAssociation(b); },
334  [this](ProductID const& p) { return getIt(p); },
335  foundContainers,
336  keys);
337  }
338 
340  unsigned int key,
341  ProductID const& thinnedID) const {
342  BranchID parent = pidToBid(parentID);
343  BranchID thinned = pidToBid(thinnedID);
344 
345  try {
347  parentID, parent, key, thinnedID, thinned, *thinnedAssociationsHelper_, [this](BranchID const& branchID) {
348  return getThinnedAssociation(branchID);
349  });
350  if (auto factory = std::get_if<detail::GetThinnedKeyFromExceptionFactory>(&ret)) {
351  return [func = *factory]() {
352  auto ex = func();
353  ex.addContext("Calling EventPrincipal::getThinnedKeyFrom()");
354  return ex;
355  };
356  } else {
357  return ret;
358  }
359  } catch (Exception& ex) {
360  ex.addContext("Calling EventPrincipal::getThinnedKeyFrom()");
361  throw ex;
362  }
363  }
364 
366  BranchID bid = pidToBid(pid);
367  return getProvenance(bid);
368  }
369 
371  BranchID bid = pidToBid(pid);
372  return getStableProvenance(bid);
373  }
374 
376 
378 
381  }
382 
384  ConstProductResolverPtr const phb = getProductResolver(branchID);
385 
386  if (phb == nullptr) {
388  << "EventPrincipal::getThinnedAssociation, ThinnedAssociation ProductResolver cannot be found\n"
389  << "This should never happen. Contact a Framework developer";
390  }
391  ProductData const* productData = (phb->resolveProduct(*this, false, nullptr, nullptr)).data();
392  if (productData == nullptr) {
393  return nullptr;
394  }
395  WrapperBase const* product = productData->wrapper();
396  if (!(typeid(edm::ThinnedAssociation) == product->dynamicTypeInfo())) {
398  << "EventPrincipal::getThinnedProduct, product has wrong type, not a ThinnedAssociation.\n";
399  }
401  return wrapper->product();
402  }
403 
404 } // namespace edm
void fillPrincipal(DelayedReader *reader)
Definition: Principal.cc:402
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper_
std::optional< std::tuple< WrapperBase const *, unsigned int > > getThinnedProduct(ProductID const &pid, unsigned int key) const override
void setLuminosityBlockPrincipal(LuminosityBlockPrincipal *lbp)
void clearPrincipal()
Definition: Principal.cc:382
void commonFillEventPrincipal(EventAuxiliary const &aux, ProcessHistory const *processHistory, DelayedReader *reader)
unsigned short BranchListIndex
bool isValid() const
Definition: ProductID.h:32
unsigned int ProductResolverIndex
EventNumber_t event() const
EventAuxiliary const & aux() const
RunNumber_t run() const
std::variant< unsigned int, detail::GetThinnedKeyFromExceptionFactory, std::monostate > OptionalThinnedKey
EventToProcessBlockIndexes eventToProcessBlockIndexes_
ProductRegistry const & productRegistry() const
Definition: Principal.h:146
EventSelectionIDVector eventSelectionIDs_
ret
prodAgent to be discontinued
unsigned int processBlockIndex(std::string const &processName) const override
edm::ThinnedAssociation const * getThinnedAssociation(edm::BranchID const &branchID) const
RunPrincipal const & runPrincipal() const
std::optional< std::tuple< WrapperBase const *, unsigned int > > getThinnedProduct(ProductID const &pid, unsigned int key, ThinnedAssociationsHelper const &thinnedAssociationsHelper, F1 pidToBid, F2 getThinnedAssociation, F3 getByProductID)
std::variant< unsigned int, GetThinnedKeyFromExceptionFactory, std::monostate > getThinnedKeyFrom_implementation(ProductID const &parentID, BranchID const &parent, unsigned int key, ProductID const &thinnedID, BranchID thinned, ThinnedAssociationsHelper const &thinnedAssociationsHelper, F &&getThinnedAssociation)
BranchListIndexes branchListIndexes_
ProductResolverBase * getExistingProduct(BranchID const &branchID)
Definition: Principal.cc:501
unsigned long long EventNumber_t
LuminosityBlockPrincipal const & luminosityBlockPrincipal() const
edm::propagate_const< LuminosityBlockPrincipal * > luminosityBlockPrincipal_
void changedIndexes_() final
std::shared_ptr< BranchIDListHelper const > branchIDListHelper_
reader
Definition: DQM.py:105
EventAuxiliary aux_
Provenance const & getProvenance(ProductID const &pid) const
edm::propagate_const< std::shared_ptr< ProductProvenanceRetriever > > provRetrieverPtr_
assert(be >=bs)
OptionalThinnedKey getThinnedKeyFrom(ProductID const &parent, unsigned int key, ProductID const &thinned) const override
WrapperBase const * wrapper() const
Definition: ProductData.h:35
unsigned int LuminosityBlockNumber_t
BasicHandle getByProductID(ProductID const &oid) const
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, ProcessBlockHelperBase const *processBlockHelper=nullptr)
BranchID pidToBid(ProductID const &pid) const
bool equal(const T &first, const T &second)
Definition: Equal.h:32
std::vector< EventSelectionID > EventSelectionIDVector
EventToProcessBlockIndexes const & eventToProcessBlockIndexes() const
static void throwProductDeletedException(ProductID const &pid, edm::EventPrincipal::ConstProductResolverPtr const phb)
void getThinnedProducts(ProductID const &pid, ThinnedAssociationsHelper const &thinnedAssociationsHelper, F1 pidToBid, F2 getThinnedAssociation, F3 getByProductID, std::vector< WrapperBase const *> &foundContainers, std::vector< unsigned int > &keys)
bool isValid() const
Definition: BranchID.h:22
EventID const & id() const
ProductID branchIDToProductID(BranchID const &bid) const
EventNumber_t const invalidEventNumber
std::vector< BranchListIndex > BranchListIndexes
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance) const
BranchID productIDToBranchID(ProductID const &pid, BranchIDLists const &lists, BranchListIndexes const &indexes)
std::type_info const & dynamicTypeInfo() const
Definition: WrapperBase.h:42
std::shared_ptr< HandleExceptionFactory > makeHandleExceptionFactory(T &&iFunctor)
Base::ConstProductResolverPtr ConstProductResolverPtr
void getThinnedProducts(ProductID const &pid, std::vector< WrapperBase const *> &foundContainers, std::vector< unsigned int > &keys) const override
void updateBranchListIndexes(BranchListIndexes &&)
std::multimap< BranchID, IndexPair > BranchIDToIndexMap
virtual unsigned int processBlockIndex(std::string const &processName, EventToProcessBlockIndexes const &) const =0
ProcessHistoryID const & processHistoryID() const
RunPrincipal const & runPrincipal() const
std::vector< ProcessIndex > branchListIndexToProcessIndex_
unsigned short ProcessIndex
Definition: ProductID.h:25
WrapperBase const * getIt(ProductID const &pid) const override
WrapperBase const * wrapper() const noexcept(true)
Definition: BasicHandle.h:73
unsigned int transitionIndex_() const override
void putOnRead(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, std::optional< ProductProvenance > productProvenance) const
ProcessHistory const & processHistory() const
Definition: Principal.h:140
ProductProvenanceRetriever const * productProvenanceRetrieverPtr() const
double b
Definition: hdecay.h:118
void addContext(std::string const &context)
Definition: Exception.cc:165
void setProcessHistoryID(ProcessHistoryID const &phid)
bool wasBranchListIndexesChangedFromInput(BranchListIndexes const &) const
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistory const *processHistory, DelayedReader *reader=nullptr)
ProcessBlockHelperBase const * processBlockHelper_
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
StableProvenance const & getStableProvenance(ProductID const &pid) const
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:142
EventSelectionIDVector const & eventSelectionIDs() const
unsigned short ProductIndex
Definition: ProductID.h:26
unsigned int RunNumber_t
unsigned int value() const
Definition: StreamID.h:43
void setRunAndLumiNumber(RunNumber_t run, LuminosityBlockNumber_t lumi)
BranchListIndexes const & branchListIndexes() const
const_iterator end() const
Definition: Principal.h:163
ConstProductResolverPtr getProductResolverByIndex(ProductResolverIndex const &oid) const
Definition: Principal.cc:562
def move(src, dest)
Definition: eostools.py:511
EventNumber_t event() const
Definition: EventID.h:40
DelayedReader * reader() const
Definition: Principal.h:187
Definition: event.py:1
void insertIntoSet(ProductProvenance provenanceProduct) const
static HepMC::HEPEVT_Wrapper wrapper
ConstProductResolverPtr getProductResolver(BranchID const &oid) const
Definition: Principal.cc:554