CMS 3D CMS Logo

OutputModuleBase.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: FWCore/Framework
4 // Class : OutputModuleBase
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 //
10 
11 // system include files
12 #include <cassert>
13 
14 // user include files
16 
37 
38 
39 namespace edm {
40  namespace global {
41 
42  // -------------------------------------------------------
44  maxEvents_(-1),
45  remainingEvents_(maxEvents_),
46  keptProducts_(),
47  hasNewlyDroppedBranch_(),
48  process_name_(),
49  productSelectorRules_(pset, "outputCommands", "OutputModule"),
50  productSelector_(),
51  moduleDescription_(),
52  wantAllEvents_(false),
53  selectors_(),
54  selector_config_id_(),
55  droppedBranchIDToKeptBranchID_(),
56  branchIDLists_(new BranchIDLists),
57  origBranchIDLists_(nullptr),
58  thinnedAssociationsHelper_(new ThinnedAssociationsHelper) {
59 
60  hasNewlyDroppedBranch_.fill(false);
61 
63  process_name_ = tns->getProcessName();
64 
66  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
67 
68  selectEvents_.registerIt(); // Just in case this PSet is not registered
69 
71 
72  //need to set wantAllEvents_ in constructor
73  // we will make the remaining selectors once we know how many streams
74  selectors_.resize(1);
78  selectors_[0],
80 
81  }
82 
86  }
87 
90  if(productSelector_.initialized()) return;
92 
93  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
94  // single object. See the notes in the header for ProductSelector
95  // for more information.
96 
97  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
98  std::vector<BranchDescription const*> associationDescriptions;
99  std::set<BranchID> keptProductsInEvent;
100  std::set<std::string> processesWithSelectedMergeableRunProducts;
101 
102  for(auto const& it : preg.productList()) {
103  BranchDescription const& desc = it.second;
104  if(desc.transient()) {
105  // if the class of the branch is marked transient, output nothing
106  } else if(!desc.present() && !desc.produced()) {
107  // else if the branch containing the product has been previously dropped,
108  // output nothing
109  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
110  associationDescriptions.push_back(&desc);
111  } else if(selected(desc)) {
112  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
114  processesWithSelectedMergeableRunProducts);
115  } else {
116  // otherwise, output nothing,
117  // and mark the fact that there is a newly dropped branch of this type.
118  hasNewlyDroppedBranch_[desc.branchType()] = true;
119  }
120  }
121 
122  setProcessesWithSelectedMergeableRunProducts(processesWithSelectedMergeableRunProducts);
123 
124  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
125  keptProductsInEvent,
127 
128  for(auto association : associationDescriptions) {
129  if(keepAssociation_[association->branchID()]) {
130  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
131  } else {
132  hasNewlyDroppedBranch_[association->branchType()] = true;
133  }
134  }
135 
136  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
137  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
138 
139  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
140  }
141 
143  if(!droppedBranchIDToKeptBranchID_.empty()) {
144  // Make a private copy of the BranchIDLists.
146  // Check for branches dropped while an EDAlias was kept.
147  for(BranchIDList& branchIDList : *branchIDLists_) {
148  for(BranchID::value_type& branchID : branchIDList) {
149  // Replace BranchID of each dropped branch with that of the kept
150  // alias, so the alias branch will have the product ID of the original branch.
151  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
152  if(iter != droppedBranchIDToKeptBranchID_.end()) {
153  branchID = iter->second;
154  }
155  }
156  }
157  }
158  }
159 
161  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
162  std::set<BranchID>& keptProductsInEvent) {
163 
165  trueBranchIDToKeptBranchDesc);
166 
167  EDGetToken token;
168  switch (desc.branchType()) {
169  case InEvent:
170  {
171  if(desc.produced()) {
172  keptProductsInEvent.insert(desc.originalBranchID());
173  } else {
174  keptProductsInEvent.insert(desc.branchID());
175  }
177  InputTag{desc.moduleLabel(),
178  desc.productInstanceName(),
179  desc.processName()});
180  break;
181  }
182  case InLumi:
183  {
184  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
185  InputTag(desc.moduleLabel(),
186  desc.productInstanceName(),
187  desc.processName()));
188  break;
189  }
190  case InRun:
191  {
192  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
193  InputTag(desc.moduleLabel(),
194  desc.productInstanceName(),
195  desc.processName()));
196  break;
197  }
198  default:
199  assert(false);
200  break;
201  }
202  // Now put it in the list of selected branches.
203  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
204  }
205 
207 
209  auto nstreams = iPC.numberOfStreams();
210  selectors_.resize(nstreams);
211 
212  bool seenFirst = false;
213  for(auto& s : selectors_) {
214  if(seenFirst) {
218  s,
220  } else {
221  seenFirst = true;
222  }
223  }
224  preallocStreams(nstreams);
226  preallocate(iPC);
227  }
228 
230  this->beginJob();
231  }
232 
234  endJob();
235  }
236 
238  return !wantAllEvents_;
239  }
240 
241  std::vector<ProductResolverIndexAndSkipBit>
243  std::vector<ProductResolverIndexAndSkipBit> returnValue;
244  auto const& s = selectors_[0];
245  auto const n = s.numberOfTokens();
246  returnValue.reserve(n);
247 
248  for(unsigned int i=0; i< n;++i) {
249  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
250  }
251  return returnValue;
252  }
253 
255  if(wantAllEvents_) return true;
256  auto& s = selectors_[id.value()];
258  e.setConsumer(this);
259  return s.wantEvent(e);
260  }
261 
262  bool
264  EventSetup const&,
265  ActivityRegistry* act,
266  ModuleCallingContext const* mcc) {
267 
268  {
270  e.setConsumer(this);
271  EventSignalsSentry sentry(act,mcc);
272  write(e);
273  }
274 
275  auto remainingEvents = remainingEvents_.load();
276  bool keepTrying = remainingEvents > 0;
277  while(keepTrying) {
278  auto newValue = remainingEvents - 1;
279  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
280  if(keepTrying) {
281  // the exchange failed because the value was changed by another thread.
282  // remainingEvents was changed to be the new value of remainingEvents_;
283  keepTrying = remainingEvents > 0;
284  }
285  }
286  return true;
287  }
288 
289  bool
291  EventSetup const&,
292  ModuleCallingContext const* mcc) {
293  RunForOutput r(rp, moduleDescription_, mcc, false);
294  r.setConsumer(this);
295  doBeginRun_(r);
296  return true;
297  }
298 
299  bool
301  EventSetup const&,
302  ModuleCallingContext const* mcc) {
303  RunForOutput r(rp, moduleDescription_, mcc, true);
304  r.setConsumer(this);
305  doEndRun_(r);
306  return true;
307  }
308 
309  void
311  ModuleCallingContext const* mcc,
312  MergeableRunProductMetadata const* mrpm) {
313  RunForOutput r(rp, moduleDescription_, mcc, true, mrpm);
314  r.setConsumer(this);
315  writeRun(r);
316  }
317 
318  bool
320  EventSetup const&,
321  ModuleCallingContext const* mcc) {
322  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
323  lb.setConsumer(this);
325  return true;
326  }
327 
328  bool
330  EventSetup const&,
331  ModuleCallingContext const* mcc) {
332  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
333  lb.setConsumer(this);
335  return true;
336  }
337 
339  ModuleCallingContext const* mcc) {
340  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
341  lb.setConsumer(this);
343  }
344 
346  openFile(fb);
347  }
348 
352  }
353 
356  }
357 
359  if(isFileOpen()) {
360  reallyCloseFile();
361  }
362  }
363 
365  }
366 
367  BranchIDLists const*
369  if(!droppedBranchIDToKeptBranchID_.empty()) {
370  return branchIDLists_.get();
371  }
372  return origBranchIDLists_;
373  }
374 
377  return thinnedAssociationsHelper_.get();
378  }
379 
380  ModuleDescription const&
382  return moduleDescription_;
383  }
384 
385  bool
387  return productSelector_.selected(desc);
388  }
389 
390  void
393  desc.setUnknown();
394  descriptions.addDefault(desc);
395  }
396 
397  void
399  ProductSelectorRules::fillDescription(desc, "outputCommands");
401  }
402 
403  void
405  }
406 
407 
408  static const std::string kBaseType("OutputModule");
409  const std::string&
411  return kBaseType;
412  }
413 
414  void
415  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
416  bool anyProductProduced) {
418  description().moduleLabel(),
419  outputModulePathPositions,
420  anyProductProduced);
421  }
422  }
423 }
static const std::string kBaseType("EDAnalyzer")
virtual void preallocate(PreallocationConfiguration const &)
bool selected(BranchDescription const &desc) const
void doCloseFile()
Tell the OutputModule that is must end the current file.
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
ModuleDescription const & description() const
BranchIDLists const * origBranchIDLists_
BranchIDLists const * branchIDLists() const
virtual void setProcessesWithSelectedMergeableRunProducts(std::set< std::string > const &)
void doRespondToOpenInputFile(FileBlock const &fb)
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
virtual void preallocStreams(unsigned int)
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *)
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
virtual bool isFileOpen() const
OutputModuleBase(ParameterSet const &pset)
ParameterSetID id() const
ParameterSet const & getParameterSet(ParameterSetID const &id)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
virtual void openFile(FileBlock const &)
virtual void doRespondToOpenInputFile_(FileBlock const &)
std::string const & processName() const
static void fillDescription(ParameterSetDescription &desc, char const *parameterName, std::vector< std::string > const &defaultStrings=defaultSelectionStrings())
ParameterSet getUntrackedParameterSet(std::string const &name, ParameterSet const &defaultValue) const
SelectedProductsForBranchType keptProducts_
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
#define nullptr
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *, ModuleCallingContext const *)
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
ModuleDescription moduleDescription_
ProductList const & productList() const
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
ProductResolverIndexAndSkipBit uncheckedIndexFrom(EDGetToken) const
void addDefault(ParameterSetDescription const &psetDescription)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
std::string const & moduleLabel() const
unsigned int value_type
Definition: BranchID.h:16
std::string const & productInstanceName() const
void doPreallocate(PreallocationConfiguration const &)
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
std::map< BranchID, bool > keepAssociation_
bool selected(BranchDescription const &desc) const
virtual void doBeginLuminosityBlock_(LuminosityBlockForOutput const &)
TypeID unwrappedTypeID() const
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
static void prevalidate(ConfigurationDescriptions &)
std::vector< BranchDescription const * > allBranchDescriptions() const
static const std::string & baseType()
bool configureEventSelector(edm::ParameterSet const &iPSet, std::string const &iProcessName, std::vector< std::string > const &iAllTriggerNames, edm::detail::TriggerResultsBasedEventSelector &oSelector, ConsumesCollector &&iC)
BranchID const & branchID() const
TypeWithDict const & unwrappedType() const
void configure(OutputModuleDescription const &desc)
static void fillDescription(ParameterSetDescription &desc)
static void fillDescription(ParameterSetDescription &desc)
virtual void writeRun(RunForOutput const &)=0
element_type const * get() const
virtual void doEndRun_(RunForOutput const &)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
virtual void preallocLumis(unsigned int)
ProductSelectorRules productSelectorRules_
void doRespondToCloseInputFile(FileBlock const &fb)
std::atomic< int > remainingEvents_
virtual void doEndLuminosityBlock_(LuminosityBlockForOutput const &)
virtual void doBeginRun_(RunForOutput const &)
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
HLT enums.
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
virtual void write(EventForOutput const &)=0
void setConsumer(EDConsumerBase const *iConsumer)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
std::vector< ProductResolverIndexAndSkipBit > productsUsedBySelection() const
BranchID const & originalBranchID() const
void insertSelectedProcesses(BranchDescription const &desc, std::set< std::string > &processes)
virtual void doRespondToCloseInputFile_(FileBlock const &)
ParameterSetID registerProperSelectionInfo(edm::ParameterSet const &iInitial, std::string const &iLabel, std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
void doOpenFile(FileBlock const &fb)
ParameterSet const & registerIt()
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *, MergeableRunProductMetadata const *)
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)