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,
68  provRetrieverPtr_->deepCopy(provRetriever);
70  if(branchIDListHelper_->hasProducedProducts()) {
71  // Add index into BranchIDListRegistry for products produced this process
72  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
73  }
74  fillEventPrincipal(aux,processHistoryRegistry,reader);
75  }
76 
77  void
79  ProcessHistoryRegistry const& processHistoryRegistry,
84  if(branchIDListHelper_->hasProducedProducts()) {
85  // Add index into BranchIDListRegistry for products produced this process
86  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
87  }
88  fillEventPrincipal(aux,processHistoryRegistry,nullptr);
89  }
90 
91  void
93  ProcessHistoryRegistry const& processHistoryRegistry,
95  if(aux.event() == invalidEventNumber) {
97  << "EventPrincipal::fillEventPrincipal, Invalid event number provided in EventAuxiliary, It is illegal for the event number to be 0\n";
98  }
99 
100  fillPrincipal(aux.processHistoryID(), processHistoryRegistry, reader);
101  aux_ = aux;
103 
104  if(branchListIndexes_.empty() and branchIDListHelper_->hasProducedProducts()) {
105  // Add index into BranchIDListRegistry for products produced this process
106  // if it hasn't already been filled in by the other fillEventPrincipal or by an earlier call to this function
107  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
108  }
109 
110  // Fill in helper map for Branch to ProductID mapping
111  ProcessIndex pix = 0;
112  for(auto const& blindex : branchListIndexes_) {
113  branchListIndexToProcessIndex_.insert(std::make_pair(blindex, pix));
114  ++pix;
115  }
116 
117  // Fill in the product ID's in the product holders.
118  for(auto& prod : *this) {
119  if (prod->singleProduct()) {
120  // If an alias is in the same process as the original then isAlias will be true.
121  // Under that condition, we want the ProductID to be the same as the original.
122  // If not, then we've internally changed the original BranchID to the alias BranchID
123  // in the ProductID lookup so we need the alias BranchID.
124  auto const & bd =prod->branchDescription();
125  prod->setProvenance(productProvenanceRetrieverPtr(),
126  processHistory(),
127  branchIDToProductID(bd.isAlias()?bd.originalBranchID(): bd.branchID()));
128  }
129  }
130  }
131 
132  void
133  EventPrincipal::setLuminosityBlockPrincipal(std::shared_ptr<LuminosityBlockPrincipal> const& lbp) {
135  }
136 
137  void
139  assert(run == luminosityBlockPrincipal_->run());
140  assert(lumi == luminosityBlockPrincipal_->luminosityBlock());
141  EventNumber_t event = aux_.id().event();
142  aux_.id() = EventID(run, lumi, event);
143  }
144 
145  RunPrincipal const&
148  }
149 
150  void
152  BranchDescription const& bd,
153  std::unique_ptr<WrapperBase> edp,
154  ProductProvenance const& productProvenance) const {
155 
156  // assert commented out for DaqSource. When DaqSource no longer uses put(), the assert can be restored.
157  //assert(produced());
158  if(edp.get() == nullptr) {
159  throw Exception(errors::InsertFailure, "Null Pointer")
160  << "put: Cannot put because ptr to product is null."
161  << "\n";
162  }
163  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
164  auto phb = getExistingProduct(bd.branchID());
165  assert(phb);
166  // ProductResolver assumes ownership
167  phb->putProduct(std::move(edp));
168  }
169 
170  void
172  BranchDescription const& bd,
173  std::unique_ptr<WrapperBase> edp,
174  ProductProvenance const* productProvenance) const {
175 
176  assert(!bd.produced());
177  if (productProvenance) {
178  productProvenanceRetrieverPtr()->insertIntoSet(*productProvenance);
179  }
180  auto phb = getExistingProduct(bd.branchID());
181  assert(phb);
182  // ProductResolver assumes ownership
183  phb->putProduct(std::move(edp));
184  }
185 
186  BranchID
188  if(!pid.isValid()) {
189  throw Exception(errors::ProductNotFound, "InvalidID")
190  << "get by product ID: invalid ProductID supplied\n";
191  }
192  return productIDToBranchID(pid, branchIDListHelper_->branchIDLists(), branchListIndexes_);
193  }
194 
195  ProductID
197  if(!bid.isValid()) {
198  throw Exception(errors::NotFound, "InvalidID")
199  << "branchIDToProductID: invalid BranchID supplied\n";
200  }
201  typedef BranchIDListHelper::BranchIDToIndexMap BIDToIndexMap;
202  typedef BIDToIndexMap::const_iterator Iter;
203  typedef std::pair<Iter, Iter> IndexRange;
204 
205  IndexRange range = branchIDListHelper_->branchIDToIndexMap().equal_range(bid);
206  for(Iter it = range.first; it != range.second; ++it) {
207  BranchListIndex blix = it->second.first;
208  std::map<BranchListIndex, ProcessIndex>::const_iterator i = branchListIndexToProcessIndex_.find(blix);
209  if(i != branchListIndexToProcessIndex_.end()) {
210  ProductIndex productIndex = it->second.second;
211  ProcessIndex processIndex = i->second;
212  return ProductID(processIndex+1, productIndex+1);
213  }
214  }
215  // cannot throw, because some products may legitimately not have product ID's (e.g. pile-up).
216  return ProductID();
217  }
218 
219  unsigned int
221  return streamID_.value();
222  }
223 
226  exception<<"get by product ID: The product with given id: "<<pid
227  <<"\ntype: "<<phb->productType()
228  <<"\nproduct instance name: "<<phb->productInstanceName()
229  <<"\nprocess name: "<<phb->processName()
230  <<"\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.";
231  throw exception;
232  }
233 
236  BranchID bid = pidToBid(pid);
238  if(phb == nullptr) {
239  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
240  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
241  *whyFailed
242  << "get by product ID: no product with given id: " << pid << "\n";
243  return whyFailed;
244  }));
245  }
246 
247  // Was this already deleted?
248  if(phb->productWasDeleted()) {
250  }
251  // Check for case where we tried on demand production and
252  // it failed to produce the object
253  if(phb->unscheduledWasNotRun()) {
254  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
255  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
256  *whyFailed
257  << "get by ProductID: could not get product with id: " << pid << "\n"
258  << "Unscheduled execution not allowed to get via ProductID.\n";
259  return whyFailed;
260  }));
261  }
262  auto resolution = phb->resolveProduct(*this,false,nullptr,nullptr);
263 
264  auto data = resolution.data();
265  if(data) {
266  return BasicHandle(data->wrapper(), &(data->provenance()));
267  }
268  return BasicHandle(nullptr,nullptr);
269  }
270 
271  WrapperBase const*
273  return getByProductID(pid).wrapper();
274  }
275 
276  WrapperBase const*
277  EventPrincipal::getThinnedProduct(ProductID const& pid, unsigned int& key) const {
278 
279  BranchID parent = pidToBid(pid);
280 
281  // Loop over thinned containers which were made by selecting elements from the parent container
282  for(auto associatedBranches = thinnedAssociationsHelper_->parentBegin(parent),
283  iEnd = thinnedAssociationsHelper_->parentEnd(parent);
284  associatedBranches != iEnd; ++associatedBranches) {
285 
286  ThinnedAssociation const* thinnedAssociation =
287  getThinnedAssociation(associatedBranches->association());
288  if(thinnedAssociation == nullptr) continue;
289 
290  if(associatedBranches->parent() != pidToBid(thinnedAssociation->parentCollectionID())) {
291  continue;
292  }
293 
294  unsigned int thinnedIndex = 0;
295  // Does this thinned container have the element referenced by key?
296  // If yes, thinnedIndex is set to point to it in the thinned container
297  if(!thinnedAssociation->hasParentIndex(key, thinnedIndex)) {
298  continue;
299  }
300  // Get the thinned container and return a pointer if we can find it
301  ProductID const& thinnedCollectionPID = thinnedAssociation->thinnedCollectionID();
302  BasicHandle bhThinned = getByProductID(thinnedCollectionPID);
303  if(!bhThinned.isValid()) {
304  // Thinned container is not found, try looking recursively in thinned containers
305  // which were made by selecting elements from this thinned container.
306  WrapperBase const* wrapperBase = getThinnedProduct(thinnedCollectionPID, thinnedIndex);
307  if(wrapperBase != nullptr) {
308  key = thinnedIndex;
309  return wrapperBase;
310  } else {
311  continue;
312  }
313  }
314  key = thinnedIndex;
315  return bhThinned.wrapper();
316  }
317  return nullptr;
318  }
319 
320  void
322  std::vector<WrapperBase const*>& foundContainers,
323  std::vector<unsigned int>& keys) const {
324 
325  BranchID parent = pidToBid(pid);
326 
327  // Loop over thinned containers which were made by selecting elements from the parent container
328  for(auto associatedBranches = thinnedAssociationsHelper_->parentBegin(parent),
329  iEnd = thinnedAssociationsHelper_->parentEnd(parent);
330  associatedBranches != iEnd; ++associatedBranches) {
331 
332  ThinnedAssociation const* thinnedAssociation =
333  getThinnedAssociation(associatedBranches->association());
334  if(thinnedAssociation == nullptr) continue;
335 
336  if(associatedBranches->parent() != pidToBid(thinnedAssociation->parentCollectionID())) {
337  continue;
338  }
339 
340  unsigned nKeys = keys.size();
341  unsigned int doNotLookForThisIndex = std::numeric_limits<unsigned int>::max();
342  std::vector<unsigned int> thinnedIndexes(nKeys, doNotLookForThisIndex);
343  bool hasAny = false;
344  for(unsigned k = 0; k < nKeys; ++k) {
345  // Already found this one
346  if(foundContainers[k] != nullptr) continue;
347  // Already know this one is not in this thinned container
348  if(keys[k] == doNotLookForThisIndex) continue;
349  // Does the thinned container hold the entry of interest?
350  // Modifies thinnedIndexes[k] only if it returns true and
351  // sets it to the index in the thinned collection.
352  if(thinnedAssociation->hasParentIndex(keys[k], thinnedIndexes[k])) {
353  hasAny = true;
354  }
355  }
356  if(!hasAny) {
357  continue;
358  }
359  // Get the thinned container and set the pointers and indexes into
360  // it (if we can find it)
361  ProductID thinnedCollectionPID = thinnedAssociation->thinnedCollectionID();
362  BasicHandle bhThinned = getByProductID(thinnedCollectionPID);
363  if(!bhThinned.isValid()) {
364  // Thinned container is not found, try looking recursively in thinned containers
365  // which were made by selecting elements from this thinned container.
366  getThinnedProducts(thinnedCollectionPID, foundContainers, thinnedIndexes);
367  for(unsigned k = 0; k < nKeys; ++k) {
368  if(foundContainers[k] == nullptr) continue;
369  if(thinnedIndexes[k] == doNotLookForThisIndex) continue;
370  keys[k] = thinnedIndexes[k];
371  }
372  } else {
373  for(unsigned k = 0; k < nKeys; ++k) {
374  if(thinnedIndexes[k] == doNotLookForThisIndex) continue;
375  keys[k] = thinnedIndexes[k];
376  foundContainers[k] = bhThinned.wrapper();
377  }
378  }
379  }
380  }
381 
382  Provenance
384  BranchID bid = pidToBid(pid);
385  return getProvenance(bid, mcc);
386  }
387 
388  void
390  applyToResolvers([&iConfigure](ProductResolverBase* iResolver) {
391  iResolver->setupUnscheduled(iConfigure);
392  });
393  }
394 
397  return eventSelectionIDs_;
398  }
399 
400  BranchListIndexes const&
402  return branchListIndexes_;
403  }
404 
407 
408  ConstProductResolverPtr const phb = getProductResolver(branchID);
409 
410  if(phb == nullptr) {
412  << "EventPrincipal::getThinnedAssociation, ThinnedAssociation ProductResolver cannot be found\n"
413  << "This should never happen. Contact a Framework developer";
414  }
415  ProductData const* productData = (phb->resolveProduct(*this,false,nullptr,nullptr)).data();
416  if (productData == nullptr) {
417  return nullptr;
418  }
419  WrapperBase const* product = productData->wrapper();
420  if(!(typeid(edm::ThinnedAssociation) == product->dynamicTypeInfo())) {
422  << "EventPrincipal::getThinnedProduct, product has wrong type, not a ThinnedAssociation.\n";
423  }
424  Wrapper<ThinnedAssociation> const* wrapper = static_cast<Wrapper<ThinnedAssociation> const*>(product);
425  return wrapper->product();
426  }
427 
428 }
RunPrincipal const & runPrincipal() const
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper_
EventNumber_t event() const
Definition: EventID.h:41
void clearPrincipal()
Definition: Principal.cc:299
int i
Definition: DBlmapReader.cc:9
unsigned short BranchListIndex
void setLuminosityBlockPrincipal(std::shared_ptr< LuminosityBlockPrincipal > const &lbp)
EventSelectionIDVector const & eventSelectionIDs() const
virtual WrapperBase const * getThinnedProduct(ProductID const &pid, unsigned int &key) const override
std::type_info const & dynamicTypeInfo() const
Definition: WrapperBase.h:38
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:373
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)
virtual void setupUnscheduled(UnscheduledConfigurator const &)
Provenance getProvenance(ProductID const &pid, ModuleCallingContext const *mcc) const
void applyToResolvers(F iFunc)
Definition: Principal.h:211
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:135
ProductID branchIDToProductID(BranchID const &bid) const
RunPrincipal const & runPrincipal() const
std::vector< EventSelectionID > EventSelectionIDVector
virtual 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:139
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:178
void setupUnscheduled(UnscheduledConfigurator const &)
unsigned int value() const
Definition: StreamID.h:46
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 getProductResolver(BranchID const &oid) const
Definition: Principal.cc:433
virtual 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:317
EventAuxiliary const & aux() const
EventNumber_t event() const
def move(src, dest)
Definition: eostools.py:510
virtual WrapperBase const * getIt(ProductID const &pid) const override
Definition: event.py:1
static HepMC::HEPEVT_Wrapper wrapper
void insertIntoSet(ProductProvenance const &provenanceProduct) const