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 namespace edm {
39  namespace limited {
40 
41  // -------------------------------------------------------
43  : maxEvents_(-1),
44  remainingEvents_(maxEvents_),
45  keptProducts_(),
46  hasNewlyDroppedBranch_(),
47  process_name_(),
48  productSelectorRules_(pset, "outputCommands", "OutputModule"),
49  productSelector_(),
50  moduleDescription_(),
51  wantAllEvents_(false),
52  selectors_(),
53  selector_config_id_(),
54  droppedBranchIDToKeptBranchID_(),
55  branchIDLists_(new BranchIDLists),
56  origBranchIDLists_(nullptr),
57  thinnedAssociationsHelper_(new ThinnedAssociationsHelper),
58  queue_(pset.getUntrackedParameter<unsigned int>("concurrencyLimit")) {
59  hasNewlyDroppedBranch_.fill(false);
60 
62  process_name_ = tns->getProcessName();
63 
64  selectEvents_ = pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
65 
66  selectEvents_.registerIt(); // Just in case this PSet is not registered
67 
69 
70  //need to set wantAllEvents_ in constructor
71  // we will make the remaining selectors once we know how many streams
72  selectors_.resize(1);
75  }
76 
80  }
81 
85  return;
87 
88  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
89  // single object. See the notes in the header for ProductSelector
90  // for more information.
91 
92  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
93  std::vector<BranchDescription const*> associationDescriptions;
94  std::set<BranchID> keptProductsInEvent;
95  std::set<std::string> processesWithSelectedMergeableRunProducts;
96 
97  for (auto const& it : preg.productList()) {
98  BranchDescription const& desc = it.second;
99  if (desc.transient()) {
100  // if the class of the branch is marked transient, output nothing
101  } else if (!desc.present() && !desc.produced()) {
102  // else if the branch containing the product has been previously dropped,
103  // output nothing
104  } else if (desc.unwrappedType() == typeid(ThinnedAssociation)) {
105  associationDescriptions.push_back(&desc);
106  } else if (selected(desc)) {
107  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
108  insertSelectedProcesses(desc, processesWithSelectedMergeableRunProducts);
109  } else {
110  // otherwise, output nothing,
111  // and mark the fact that there is a newly dropped branch of this type.
112  hasNewlyDroppedBranch_[desc.branchType()] = true;
113  }
114  }
115 
116  setProcessesWithSelectedMergeableRunProducts(processesWithSelectedMergeableRunProducts);
117 
118  thinnedAssociationsHelper.selectAssociationProducts(
119  associationDescriptions, keptProductsInEvent, keepAssociation_);
120 
121  for (auto association : associationDescriptions) {
122  if (keepAssociation_[association->branchID()]) {
123  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
124  } else {
125  hasNewlyDroppedBranch_[association->branchType()] = true;
126  }
127  }
128 
129  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
130  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
131 
132  thinnedAssociationsHelper_->updateFromParentProcess(
133  thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
134  }
135 
137  if (!droppedBranchIDToKeptBranchID_.empty()) {
138  // Make a private copy of the BranchIDLists.
140  // Check for branches dropped while an EDAlias was kept.
141  for (BranchIDList& branchIDList : *branchIDLists_) {
142  for (BranchID::value_type& branchID : branchIDList) {
143  // Replace BranchID of each dropped branch with that of the kept
144  // alias, so the alias branch will have the product ID of the original branch.
145  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter =
146  droppedBranchIDToKeptBranchID_.find(branchID);
147  if (iter != droppedBranchIDToKeptBranchID_.end()) {
148  branchID = iter->second;
149  }
150  }
151  }
152  }
153  }
154 
156  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
157  std::set<BranchID>& keptProductsInEvent) {
158  ProductSelector::checkForDuplicateKeptBranch(desc, trueBranchIDToKeptBranchDesc);
159 
161  switch (desc.branchType()) {
162  case InEvent: {
163  if (desc.produced()) {
164  keptProductsInEvent.insert(desc.originalBranchID());
165  } else {
166  keptProductsInEvent.insert(desc.branchID());
167  }
169  InputTag{desc.moduleLabel(), desc.productInstanceName(), desc.processName()});
170  break;
171  }
172  case InLumi: {
173  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(), PRODUCT_TYPE},
174  InputTag(desc.moduleLabel(), desc.productInstanceName(), desc.processName()));
175  break;
176  }
177  case InRun: {
178  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(), PRODUCT_TYPE},
179  InputTag(desc.moduleLabel(), desc.productInstanceName(), desc.processName()));
180  break;
181  }
182  default:
183  assert(false);
184  break;
185  }
186  // Now put it in the list of selected branches.
187  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
188  }
189 
191 
193  auto nstreams = iPC.numberOfStreams();
194  selectors_.resize(nstreams);
195 
196  bool seenFirst = false;
197  for (auto& s : selectors_) {
198  if (seenFirst) {
200  } else {
201  seenFirst = true;
202  }
203  }
204  preallocStreams(nstreams);
206  preallocate(iPC);
207  }
208 
210 
212 
214 
215  std::vector<ProductResolverIndexAndSkipBit> OutputModuleBase::productsUsedBySelection() const {
216  std::vector<ProductResolverIndexAndSkipBit> returnValue;
217  auto const& s = selectors_[0];
218  auto const n = s.numberOfTokens();
219  returnValue.reserve(n);
220 
221  for (unsigned int i = 0; i < n; ++i) {
222  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
223  }
224  return returnValue;
225  }
226 
228  EventPrincipal const& ep,
229  ModuleCallingContext const* mcc) {
230  if (wantAllEvents_)
231  return true;
232  auto& s = selectors_[id.value()];
234  e.setConsumer(this);
235  return s.wantEvent(e);
236  }
237 
239  EventSetupImpl const&,
240  ActivityRegistry* act,
241  ModuleCallingContext const* mcc) {
242  {
244  e.setConsumer(this);
245  EventSignalsSentry sentry(act, mcc);
246  write(e);
247  }
248 
249  auto remainingEvents = remainingEvents_.load();
250  bool keepTrying = remainingEvents > 0;
251  while (keepTrying) {
252  auto newValue = remainingEvents - 1;
253  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
254  if (keepTrying) {
255  // the exchange failed because the value was changed by another thread.
256  // remainingEvents was changed to be the new value of remainingEvents_;
257  keepTrying = remainingEvents > 0;
258  }
259  }
260  return true;
261  }
262 
264  RunForOutput r(rp, moduleDescription_, mcc, false);
265  r.setConsumer(this);
266  doBeginRun_(r);
267  return true;
268  }
269 
271  RunForOutput r(rp, moduleDescription_, mcc, true);
272  r.setConsumer(this);
273  doEndRun_(r);
274  return true;
275  }
276 
278  ModuleCallingContext const* mcc,
279  MergeableRunProductMetadata const* mrpm) {
280  RunForOutput r(rp, moduleDescription_, mcc, true, mrpm);
281  r.setConsumer(this);
282  writeRun(r);
283  }
284 
286  EventSetupImpl const&,
287  ModuleCallingContext const* mcc) {
288  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
289  lb.setConsumer(this);
291  return true;
292  }
293 
295  EventSetupImpl const&,
296  ModuleCallingContext const* mcc) {
297  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
298  lb.setConsumer(this);
300  return true;
301  }
302 
304  ModuleCallingContext const* mcc) {
305  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
306  lb.setConsumer(this);
308  }
309 
311 
315  }
316 
318 
320  if (isFileOpen()) {
321  reallyCloseFile();
322  }
323  }
324 
326 
328  if (!droppedBranchIDToKeptBranchID_.empty()) {
329  return branchIDLists_.get();
330  }
331  return origBranchIDLists_;
332  }
333 
335  return thinnedAssociationsHelper_.get();
336  }
337 
339 
340  bool OutputModuleBase::selected(BranchDescription const& desc) const { return productSelector_.selected(desc); }
341 
344  desc.setUnknown();
345  descriptions.addDefault(desc);
346  }
347 
349  ProductSelectorRules::fillDescription(desc, "outputCommands");
351  desc.addUntracked<unsigned int>("concurrencyLimit", 1);
352  }
353 
355 
356  static const std::string kBaseType("OutputModule");
358 
360  std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
361  bool anyProductProduced) {
364  outputModulePathPositions,
365  anyProductProduced);
366  }
367  } // namespace limited
368 } // namespace edm
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
bool doEvent(EventPrincipal const &ep, EventSetupImpl const &c, ActivityRegistry *, ModuleCallingContext const *)
std::atomic< int > remainingEvents_
bool selected(BranchDescription const &desc) const
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
std::map< BranchID, bool > keepAssociation_
virtual void setProcessesWithSelectedMergeableRunProducts(std::set< std::string > const &)
virtual bool isFileOpen() const
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void doPreallocate(PreallocationConfiguration const &)
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
virtual void doRespondToOpenInputFile_(FileBlock const &)
BranchIDLists const * origBranchIDLists_
virtual void write(EventForOutput const &)=0
ParameterSetID id() const
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetupImpl const &c, ModuleCallingContext const *)
ParameterSet const & getParameterSet(ParameterSetID const &id)
bool selected(BranchDescription const &desc) const
#define nullptr
virtual void doBeginLuminosityBlock_(LuminosityBlockForOutput const &)
bool doEndRun(RunPrincipal const &rp, EventSetupImpl const &c, ModuleCallingContext const *)
OutputModuleBase(ParameterSet const &pset)
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int >>> const &outputModulePathPositions, bool anyProductProduced)
std::string const & processName() const
std::vector< ProductResolverIndexAndSkipBit > productsUsedBySelection() 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
ModuleDescription const & description() const
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetupImpl const &c, ModuleCallingContext const *)
void configure(OutputModuleDescription const &desc)
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ProductList const & productList() const
virtual void openFile(FileBlock const &) const
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
virtual void doEndRun_(RunForOutput const &)
ProductResolverIndexAndSkipBit uncheckedIndexFrom(EDGetToken) const
void addDefault(ParameterSetDescription const &psetDescription)
static const std::string kBaseType("EDAnalyzer")
virtual void doBeginRun_(RunForOutput const &)
std::string const & moduleLabel() const
unsigned int value_type
Definition: BranchID.h:16
std::string const & productInstanceName() const
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
virtual void preallocate(PreallocationConfiguration const &)
virtual void writeRun(RunForOutput const &)=0
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
TypeID unwrappedTypeID() const
static void fillDescription(ParameterSetDescription &desc)
std::vector< BranchDescription const * > allBranchDescriptions() const
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
static void fillDescription(ParameterSetDescription &desc)
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *, MergeableRunProductMetadata const *)
void doOpenFile(FileBlock const &fb)
element_type const * get() const
void doRespondToOpenInputFile(FileBlock const &fb)
ProductSelectorRules productSelectorRules_
virtual void preallocStreams(unsigned int)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
virtual void doEndLuminosityBlock_(LuminosityBlockForOutput const &)
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
BranchIDLists const * branchIDLists() const
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
ModuleDescription moduleDescription_
HLT enums.
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
virtual void doRespondToCloseInputFile_(FileBlock const &)
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
virtual void preallocLumis(unsigned int)
void setConsumer(EDConsumerBase const *iConsumer)
BranchID const & originalBranchID() const
void insertSelectedProcesses(BranchDescription const &desc, std::set< std::string > &processes)
SelectedProductsForBranchType keptProducts_
static void prevalidate(ConfigurationDescriptions &)
void doCloseFile()
Tell the OutputModule that is must end the current file.
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 doBeginRun(RunPrincipal const &rp, EventSetupImpl const &c, ModuleCallingContext const *)
ParameterSet const & registerIt()
static const std::string & baseType()
void doRespondToCloseInputFile(FileBlock const &fb)
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)