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 global {
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  hasNewlyDroppedBranch_.fill(false);
59 
61  process_name_ = tns->getProcessName();
62 
63  selectEvents_ = pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
64 
65  selectEvents_.registerIt(); // Just in case this PSet is not registered
66 
68 
69  //need to set wantAllEvents_ in constructor
70  // we will make the remaining selectors once we know how many streams
71  selectors_.resize(1);
74  }
75 
79  }
80 
84  return;
86 
87  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
88  // single object. See the notes in the header for ProductSelector
89  // for more information.
90 
91  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
92  std::vector<BranchDescription const*> associationDescriptions;
93  std::set<BranchID> keptProductsInEvent;
94  std::set<std::string> processesWithSelectedMergeableRunProducts;
95 
96  for (auto const& it : preg.productList()) {
97  BranchDescription const& desc = it.second;
98  if (desc.transient()) {
99  // if the class of the branch is marked transient, output nothing
100  } else if (!desc.present() && !desc.produced()) {
101  // else if the branch containing the product has been previously dropped,
102  // output nothing
103  } else if (desc.unwrappedType() == typeid(ThinnedAssociation)) {
104  associationDescriptions.push_back(&desc);
105  } else if (selected(desc)) {
106  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
107  insertSelectedProcesses(desc, processesWithSelectedMergeableRunProducts);
108  } else {
109  // otherwise, output nothing,
110  // and mark the fact that there is a newly dropped branch of this type.
111  hasNewlyDroppedBranch_[desc.branchType()] = true;
112  }
113  }
114 
115  setProcessesWithSelectedMergeableRunProducts(processesWithSelectedMergeableRunProducts);
116 
117  thinnedAssociationsHelper.selectAssociationProducts(
118  associationDescriptions, keptProductsInEvent, keepAssociation_);
119 
120  for (auto association : associationDescriptions) {
121  if (keepAssociation_[association->branchID()]) {
122  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
123  } else {
124  hasNewlyDroppedBranch_[association->branchType()] = true;
125  }
126  }
127 
128  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
129  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
130 
131  thinnedAssociationsHelper_->updateFromParentProcess(
132  thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
133  }
134 
136  if (!droppedBranchIDToKeptBranchID_.empty()) {
137  // Make a private copy of the BranchIDLists.
139  // Check for branches dropped while an EDAlias was kept.
140  for (BranchIDList& branchIDList : *branchIDLists_) {
141  for (BranchID::value_type& branchID : branchIDList) {
142  // Replace BranchID of each dropped branch with that of the kept
143  // alias, so the alias branch will have the product ID of the original branch.
144  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter =
145  droppedBranchIDToKeptBranchID_.find(branchID);
146  if (iter != droppedBranchIDToKeptBranchID_.end()) {
147  branchID = iter->second;
148  }
149  }
150  }
151  }
152  }
153 
155  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
156  std::set<BranchID>& keptProductsInEvent) {
157  ProductSelector::checkForDuplicateKeptBranch(desc, trueBranchIDToKeptBranchDesc);
158 
159  EDGetToken token;
160  switch (desc.branchType()) {
161  case InEvent: {
162  if (desc.produced()) {
163  keptProductsInEvent.insert(desc.originalBranchID());
164  } else {
165  keptProductsInEvent.insert(desc.branchID());
166  }
168  InputTag{desc.moduleLabel(), desc.productInstanceName(), desc.processName()});
169  break;
170  }
171  case InLumi: {
172  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(), PRODUCT_TYPE},
173  InputTag(desc.moduleLabel(), desc.productInstanceName(), desc.processName()));
174  break;
175  }
176  case InRun: {
177  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(), PRODUCT_TYPE},
178  InputTag(desc.moduleLabel(), desc.productInstanceName(), desc.processName()));
179  break;
180  }
181  default:
182  assert(false);
183  break;
184  }
185  // Now put it in the list of selected branches.
186  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
187  }
188 
190 
192  auto nstreams = iPC.numberOfStreams();
193  selectors_.resize(nstreams);
194 
195  bool seenFirst = false;
196  for (auto& s : selectors_) {
197  if (seenFirst) {
199  } else {
200  seenFirst = true;
201  }
202  }
203  preallocStreams(nstreams);
205  preallocate(iPC);
206  }
207 
209 
211 
213 
214  std::vector<ProductResolverIndexAndSkipBit> OutputModuleBase::productsUsedBySelection() const {
215  std::vector<ProductResolverIndexAndSkipBit> returnValue;
216  auto const& s = selectors_[0];
217  auto const n = s.numberOfTokens();
218  returnValue.reserve(n);
219 
220  for (unsigned int i = 0; i < n; ++i) {
221  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
222  }
223  return returnValue;
224  }
225 
227  EventPrincipal const& ep,
228  ModuleCallingContext const* mcc) {
229  if (wantAllEvents_)
230  return true;
231  auto& s = selectors_[id.value()];
233  e.setConsumer(this);
234  return s.wantEvent(e);
235  }
236 
238  EventSetupImpl const&,
239  ActivityRegistry* act,
240  ModuleCallingContext const* mcc) {
241  {
243  e.setConsumer(this);
244  EventSignalsSentry sentry(act, mcc);
245  write(e);
246  }
247 
248  auto remainingEvents = remainingEvents_.load();
249  bool keepTrying = remainingEvents > 0;
250  while (keepTrying) {
251  auto newValue = remainingEvents - 1;
252  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
253  if (keepTrying) {
254  // the exchange failed because the value was changed by another thread.
255  // remainingEvents was changed to be the new value of remainingEvents_;
256  keepTrying = remainingEvents > 0;
257  }
258  }
259  return true;
260  }
261 
263  RunForOutput r(rp, moduleDescription_, mcc, false);
264  r.setConsumer(this);
265  doBeginRun_(r);
266  return true;
267  }
268 
270  RunForOutput r(rp, moduleDescription_, mcc, true);
271  r.setConsumer(this);
272  doEndRun_(r);
273  return true;
274  }
275 
277  ModuleCallingContext const* mcc,
278  MergeableRunProductMetadata const* mrpm) {
279  RunForOutput r(rp, moduleDescription_, mcc, true, mrpm);
280  r.setConsumer(this);
281  writeRun(r);
282  }
283 
285  EventSetupImpl const&,
286  ModuleCallingContext const* mcc) {
287  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
288  lb.setConsumer(this);
290  return true;
291  }
292 
294  EventSetupImpl const&,
295  ModuleCallingContext const* mcc) {
296  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
297  lb.setConsumer(this);
299  return true;
300  }
301 
303  ModuleCallingContext const* mcc) {
304  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
305  lb.setConsumer(this);
307  }
308 
310 
314  }
315 
317 
319  if (isFileOpen()) {
320  reallyCloseFile();
321  }
322  }
323 
325 
327  if (!droppedBranchIDToKeptBranchID_.empty()) {
328  return branchIDLists_.get();
329  }
330  return origBranchIDLists_;
331  }
332 
334  return thinnedAssociationsHelper_.get();
335  }
336 
338 
339  bool OutputModuleBase::selected(BranchDescription const& desc) const { return productSelector_.selected(desc); }
340 
343  desc.setUnknown();
344  descriptions.addDefault(desc);
345  }
346 
348  ProductSelectorRules::fillDescription(desc, "outputCommands");
350  }
351 
353 
354  static const std::string kBaseType("OutputModule");
356 
358  std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
359  bool anyProductProduced) {
361  description().moduleLabel(),
362  outputModulePathPositions,
363  anyProductProduced);
364  }
365  } // namespace global
366 } // namespace edm
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)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetupImpl const &c, ModuleCallingContext const *)
ParameterSetID id() const
ParameterSet const & getParameterSet(ParameterSetID const &id)
#define nullptr
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_
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)
bool doEndRun(RunPrincipal const &rp, EventSetupImpl const &c, ModuleCallingContext const *)
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)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int >>> const &outputModulePathPositions, bool anyProductProduced)
virtual void preallocLumis(unsigned int)
bool doEvent(EventPrincipal const &ep, EventSetupImpl const &c, ActivityRegistry *, ModuleCallingContext const *)
ProductSelectorRules productSelectorRules_
void doRespondToCloseInputFile(FileBlock const &fb)
std::atomic< int > remainingEvents_
virtual void doEndLuminosityBlock_(LuminosityBlockForOutput const &)
virtual void doBeginRun_(RunForOutput 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)
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 doBeginRun(RunPrincipal const &rp, EventSetupImpl const &c, ModuleCallingContext const *)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetupImpl 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)