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 
20 
21 #include <algorithm>
22 #include <memory>
23 
24 namespace edm {
26  std::shared_ptr<ProductRegistry const> reg,
27  std::shared_ptr<BranchIDListHelper const> branchIDListHelper,
28  ProcessConfiguration const& pc,
29  HistoryAppender* historyAppender,
30  unsigned int streamIndex) :
31  Base(reg, reg->productLookup(InEvent), pc, InEvent, historyAppender),
32  aux_(),
33  luminosityBlockPrincipal_(),
34  provRetrieverPtr_(new ProductProvenanceRetriever(streamIndex)),
35  unscheduledHandler_(),
36  moduleLabelsRunning_(),
37  eventSelectionIDs_(),
38  branchIDListHelper_(branchIDListHelper),
39  branchListIndexes_(),
40  branchListIndexToProcessIndex_(),
41  streamID_(streamIndex){}
42 
43  void
46  aux_ = EventAuxiliary();
48  provRetrieverPtr_->reset();
49  unscheduledHandler_.reset();
50  moduleLabelsRunning_.clear();
52  }
53 
54  void
56  ProcessHistoryRegistry const& processHistoryRegistry,
57  EventSelectionIDVector&& eventSelectionIDs,
58  BranchListIndexes&& branchListIndexes,
59  ProductProvenanceRetriever& provRetriever,
62  provRetrieverPtr_->deepSwap(provRetriever);
64  if(branchIDListHelper_->hasProducedProducts()) {
65  // Add index into BranchIDListRegistry for products produced this process
66  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
67  }
68  fillEventPrincipal(aux,processHistoryRegistry,reader);
69  }
70 
71  void
73  ProcessHistoryRegistry const& processHistoryRegistry,
74  EventSelectionIDVector&& eventSelectionIDs,
75  BranchListIndexes&& branchListIndexes) {
78  if(branchIDListHelper_->hasProducedProducts()) {
79  // Add index into BranchIDListRegistry for products produced this process
80  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
81  }
82  fillEventPrincipal(aux,processHistoryRegistry,nullptr);
83  }
84 
85  void
87  ProcessHistoryRegistry const& processHistoryRegistry,
89  if(aux.event() == invalidEventNumber) {
91  << "EventPrincipal::fillEventPrincipal, Invalid event number provided in EventAuxiliary, It is illegal for the event number to be 0\n";
92  }
93 
94  fillPrincipal(aux.processHistoryID(), processHistoryRegistry, reader);
95  aux_ = aux;
97 
98  if(branchListIndexes_.empty() and branchIDListHelper_->hasProducedProducts()) {
99  // Add index into BranchIDListRegistry for products produced this process
100  // if it hasn't already been filled in by the other fillEventPrincipal or by an earlier call to this function
101  branchListIndexes_.push_back(branchIDListHelper_->producedBranchListIndex());
102  }
103 
104  // Fill in helper map for Branch to ProductID mapping
105  ProcessIndex pix = 0;
106  for(auto const& blindex : branchListIndexes_) {
107  branchListIndexToProcessIndex_.insert(std::make_pair(blindex, pix));
108  ++pix;
109  }
110 
111  // Fill in the product ID's in the product holders.
112  for(auto const& prod : *this) {
113  if (prod->singleProduct()) {
114  prod->setProvenance(productProvenanceRetrieverPtr(), processHistory(), branchIDToProductID(prod->branchDescription().branchID()));
115  }
116  }
117  }
118 
119  void
120  EventPrincipal::setLuminosityBlockPrincipal(std::shared_ptr<LuminosityBlockPrincipal> const& lbp) {
122  }
123 
124  void
126  assert(run == luminosityBlockPrincipal_->run());
127  assert(lumi == luminosityBlockPrincipal_->luminosityBlock());
128  EventNumber_t event = aux_.id().event();
129  aux_.id() = EventID(run, lumi, event);
130  }
131 
132  RunPrincipal const&
135  }
136 
137  void
139  BranchDescription const& bd,
140  std::unique_ptr<WrapperBase> edp,
141  ProductProvenance const& productProvenance) {
142 
143  // assert commented out for DaqSource. When DaqSource no longer uses put(), the assert can be restored.
144  //assert(produced());
145  if(edp.get() == nullptr) {
146  throw Exception(errors::InsertFailure, "Null Pointer")
147  << "put: Cannot put because ptr to product is null."
148  << "\n";
149  }
150  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
152  assert(phb);
153  checkUniquenessAndType(edp.get(), phb);
154  // ProductHolder assumes ownership
155  phb->putProduct(std::move(edp), productProvenance);
156  }
157 
158  void
160  BranchDescription const& bd,
161  std::unique_ptr<WrapperBase> edp,
162  ProductProvenance const& productProvenance) {
163 
164  assert(!bd.produced());
165  productProvenanceRetrieverPtr()->insertIntoSet(productProvenance);
167  assert(phb);
168  checkUniquenessAndType(edp.get(), phb);
169  // ProductHolder assumes ownership
170  phb->putProduct(std::move(edp), productProvenance);
171  }
172 
173  void
175  if(phb.branchDescription().produced()) return; // nothing to do.
176  if(phb.product()) return; // nothing to do.
177  if(phb.productUnavailable()) return; // nothing to do.
178  if(!reader()) return; // nothing to do.
179 
180  // must attempt to load from persistent store
181  BranchKey const bk = BranchKey(phb.branchDescription());
182  {
183  if(mcc) {
185  }
186  std::shared_ptr<void> guard(nullptr,[this,mcc](const void*){
187  if(mcc) {
189  }
190  });
191 
192  std::unique_ptr<WrapperBase> edp(reader()->getProduct(bk, this));
193 
194  // Now fix up the ProductHolder
195  checkUniquenessAndType(edp.get(), &phb);
196  phb.putProduct(std::move(edp));
197  }
198  }
199 
200  BranchID
202  if(!pid.isValid()) {
203  throw Exception(errors::ProductNotFound, "InvalidID")
204  << "get by product ID: invalid ProductID supplied\n";
205  }
206  return productIDToBranchID(pid, branchIDListHelper_->branchIDLists(), branchListIndexes_);
207  }
208 
209  ProductID
211  if(!bid.isValid()) {
212  throw Exception(errors::NotFound, "InvalidID")
213  << "branchIDToProductID: invalid BranchID supplied\n";
214  }
215  typedef BranchIDListHelper::BranchIDToIndexMap BIDToIndexMap;
216  typedef BIDToIndexMap::const_iterator Iter;
217  typedef std::pair<Iter, Iter> IndexRange;
218 
219  IndexRange range = branchIDListHelper_->branchIDToIndexMap().equal_range(bid);
220  for(Iter it = range.first; it != range.second; ++it) {
221  BranchListIndex blix = it->second.first;
222  std::map<BranchListIndex, ProcessIndex>::const_iterator i = branchListIndexToProcessIndex_.find(blix);
223  if(i != branchListIndexToProcessIndex_.end()) {
224  ProductIndex productIndex = it->second.second;
225  ProcessIndex processIndex = i->second;
226  return ProductID(processIndex+1, productIndex+1);
227  }
228  }
229  // cannot throw, because some products may legitimately not have product ID's (e.g. pile-up).
230  return ProductID();
231  }
232 
233  unsigned int
235  return streamID_.value();
236  }
237 
240  exception<<"get by product ID: The product with given id: "<<pid
241  <<"\ntype: "<<phb->productType()
242  <<"\nproduct instance name: "<<phb->productInstanceName()
243  <<"\nprocess name: "<<phb->processName()
244  <<"\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.";
245  throw exception;
246  }
247 
248  BasicHandle
250  BranchID bid = pidToBid(pid);
251  ConstProductHolderPtr const phb = getProductHolder(bid);
252  if(phb == nullptr) {
253  return BasicHandle(makeHandleExceptionFactory([pid]()->std::shared_ptr<cms::Exception> {
254  std::shared_ptr<cms::Exception> whyFailed(std::make_shared<Exception>(errors::ProductNotFound, "InvalidID"));
255  *whyFailed
256  << "get by product ID: no product with given id: " << pid << "\n";
257  return whyFailed;
258  }));
259  }
260 
261  // Was this already deleted?
262  if(phb->productWasDeleted()) {
264  }
265  // Check for case where we tried on demand production and
266  // it failed to produce the object
267  if(phb->onDemand()) {
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 ProductID: could not get product with id: " << pid << "\n"
272  << "Unscheduled execution not allowed to get via ProductID.\n";
273  return whyFailed;
274  }));
275  }
277  phb->resolveProduct(status,false,nullptr);
278 
279  return BasicHandle(phb->productData());
280  }
281 
282  WrapperBase const*
284  return getByProductID(pid).wrapper();
285  }
286 
287  Provenance
289  BranchID bid = pidToBid(pid);
290  return getProvenance(bid, mcc);
291  }
292 
293  void
294  EventPrincipal::setUnscheduledHandler(std::shared_ptr<UnscheduledHandler> iHandler) {
295  unscheduledHandler_ = iHandler;
296  }
297 
298  std::shared_ptr<UnscheduledHandler>
300  return unscheduledHandler_;
301  }
302 
305  return eventSelectionIDs_;
306  }
307 
308  BranchListIndexes const&
310  return branchListIndexes_;
311  }
312 
313  bool
315  ModuleCallingContext const* mcc) const {
316 
317  // If it is a module already currently running in unscheduled
318  // mode, then there is a circular dependency related to which
319  // EDProducts modules require and produce. There is no safe way
320  // to recover from this. Here we check for this problem and throw
321  // an exception.
322  std::vector<std::string>::const_iterator i =
323  find_in_all(moduleLabelsRunning_, moduleLabel);
324 
325  if(i != moduleLabelsRunning_.end()) {
327  << "Hit circular dependency while trying to run an unscheduled module.\n"
328  << "The last module on the stack shown above requested data from the\n"
329  << "module with label: '" << moduleLabel << "'.\n"
330  << "This is illegal because this module is already running (it is in the\n"
331  << "stack shown above, it might or might not be asking for data from itself).\n"
332  << "More information related to resolving circular dependences can be found here:\n"
333  << "https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideUnscheduledExecution#Circular_Dependence_Errors.";
334  }
335 
336  UnscheduledSentry sentry(&moduleLabelsRunning_, moduleLabel);
337 
338  if(unscheduledHandler_) {
339  if(mcc == nullptr) {
341  << "EventPrincipal::unscheduledFill, Attempting to run unscheduled production\n"
342  << "with a null pointer to the ModuleCalling Context. This should never happen.\n"
343  << "Contact a Framework developer";
344  }
346  std::shared_ptr<void> guard(nullptr,[this,mcc](const void*){
348  });
349  unscheduledHandler_->tryToFill(moduleLabel, *const_cast<EventPrincipal*>(this), mcc);
350  }
351  return true;
352  }
353 }
RunPrincipal const & runPrincipal() const
EventNumber_t event() const
Definition: EventID.h:41
void clearPrincipal()
Definition: Principal.cc:304
int i
Definition: DBlmapReader.cc:9
unsigned short BranchListIndex
WrapperBase * product() const
Definition: ProductHolder.h:81
void setLuminosityBlockPrincipal(std::shared_ptr< LuminosityBlockPrincipal > const &lbp)
EventSelectionIDVector const & eventSelectionIDs() const
ConstProductHolderPtr getProductHolder(BranchID const &oid) const
Definition: Principal.cc:429
StreamContext const * getStreamContext() const
std::shared_ptr< LuminosityBlockPrincipal > luminosityBlockPrincipal_
std::shared_ptr< UnscheduledHandler > unscheduledHandler_
unsigned int EventNumber_t
Definition: EventRange.h:30
BasicHandle getByProductID(ProductID const &oid) const
EventSelectionIDVector eventSelectionIDs_
BranchID pidToBid(ProductID const &pid) const
tuple lumi
Definition: fjr2json.py:35
BranchListIndexes branchListIndexes_
Provenance getProvenance(ProductID const &pid, ModuleCallingContext const *mcc) const
std::map< BranchListIndex, ProcessIndex > branchListIndexToProcessIndex_
std::shared_ptr< BranchIDListHelper const > branchIDListHelper_
virtual void readFromSource_(ProductHolderBase const &phb, ModuleCallingContext const *mcc) const override
EventAuxiliary aux_
void putOnRead(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance)
bool isValid() const
Definition: BranchID.h:24
BranchListIndexes const & branchListIndexes() const
unsigned int LuminosityBlockNumber_t
ProcessHistory const & processHistory() const
Definition: Principal.h:137
ProductID branchIDToProductID(BranchID const &bid) const
WrapperBase const * getIt(ProductID const &pid) const
RunPrincipal const & runPrincipal() const
std::vector< EventSelectionID > EventSelectionIDVector
virtual bool unscheduledFill(std::string const &moduleLabel, ModuleCallingContext const *mcc) const override
static void throwProductDeletedException(ProductID const &pid, edm::EventPrincipal::ConstProductHolderPtr const phb)
void setUnscheduledHandler(std::shared_ptr< UnscheduledHandler > iHandler)
EventNumber_t const invalidEventNumber
std::vector< BranchListIndex > BranchListIndexes
BranchDescription const & branchDescription() const
Definition: ProductHolder.h:90
BranchID productIDToBranchID(ProductID const &pid, BranchIDLists const &lists, BranchListIndexes const &indexes)
ProcessHistoryID const & processHistoryID() const
Definition: Principal.h:141
std::shared_ptr< ProductProvenanceRetriever > productProvenanceRetrieverPtr() const
LuminosityBlockPrincipal const & luminosityBlockPrincipal() const
std::shared_ptr< HandleExceptionFactory > makeHandleExceptionFactory(T &&iFunctor)
void checkUniquenessAndType(WrapperBase const *prod, ProductHolderBase const *productHolder) const
Definition: Principal.cc:792
WrapperBase const * wrapper() const
Definition: BasicHandle.h:99
T const * getProduct(RefCore const &ref)
Definition: RefCoreGet.h:36
BranchID const & branchID() 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
void fillEventPrincipal(EventAuxiliary const &aux, ProcessHistoryRegistry const &processHistoryRegistry, DelayedReader *reader=nullptr)
unsigned short ProcessIndex
Definition: ProductID.h:25
DelayedReader * reader() const
Definition: Principal.h:171
unsigned int value() const
Definition: StreamID.h:46
ForwardSequence::const_iterator find_in_all(ForwardSequence const &s, Datum const &d)
wrappers for std::find
Definition: Algorithms.h:32
virtual unsigned int transitionIndex_() const override
void put(BranchDescription const &bd, std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance)
std::shared_ptr< UnscheduledHandler > unscheduledHandler() const
tuple pid
Definition: sysUtil.py:22
void setProcessHistoryID(ProcessHistoryID const &phid)
ProcessHistoryID const & processHistoryID() const
EventID const & id() const
std::shared_ptr< ProductProvenanceRetriever > provRetrieverPtr_
Base::ConstProductHolderPtr ConstProductHolderPtr
unsigned short ProductIndex
Definition: ProductID.h:26
unsigned int RunNumber_t
Definition: EventRange.h:32
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> preModuleDelayedGetSignal_
bool isValid() const
Definition: ProductID.h:35
tuple status
Definition: ntuplemaker.py:245
void setRunAndLumiNumber(RunNumber_t run, LuminosityBlockNumber_t lumi)
void fillPrincipal(ProcessHistoryID const &hist, ProcessHistoryRegistry const &phr, DelayedReader *reader)
Definition: Principal.cc:327
EventAuxiliary const & aux() const
void putProduct(std::unique_ptr< WrapperBase > edp, ProductProvenance const &productProvenance)
EventNumber_t event() const
void emit(Args &&...args) const
Definition: Signal.h:47
EventPrincipal(std::shared_ptr< ProductRegistry const > reg, std::shared_ptr< BranchIDListHelper const > branchIDListHelper, ProcessConfiguration const &pc, HistoryAppender *historyAppender, unsigned int streamIndex=0)
ProductHolderBase * getExistingProduct(BranchID const &branchID)
Definition: Principal.cc:383
std::vector< std::string > moduleLabelsRunning_
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> postModuleDelayedGetSignal_
bool productUnavailable() const
Definition: ProductHolder.h:69