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 
36 
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 
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 
101  for(auto const& it : preg.productList()) {
102  BranchDescription const& desc = it.second;
103  if(desc.transient()) {
104  // if the class of the branch is marked transient, output nothing
105  } else if(!desc.present() && !desc.produced()) {
106  // else if the branch containing the product has been previously dropped,
107  // output nothing
108  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
109  associationDescriptions.push_back(&desc);
110  } else if(selected(desc)) {
111  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
112  } else {
113  // otherwise, output nothing,
114  // and mark the fact that there is a newly dropped branch of this type.
115  hasNewlyDroppedBranch_[desc.branchType()] = true;
116  }
117  }
118 
119  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
120  keptProductsInEvent,
122 
123  for(auto association : associationDescriptions) {
124  if(keepAssociation_[association->branchID()]) {
125  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
126  } else {
127  hasNewlyDroppedBranch_[association->branchType()] = true;
128  }
129  }
130 
131  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
132  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
133 
134  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
135  }
136 
138  if(!droppedBranchIDToKeptBranchID_.empty()) {
139  // Make a private copy of the BranchIDLists.
141  // Check for branches dropped while an EDAlias was kept.
142  for(BranchIDList& branchIDList : *branchIDLists_) {
143  for(BranchID::value_type& branchID : branchIDList) {
144  // Replace BranchID of each dropped branch with that of the kept
145  // alias, so the alias branch will have the product ID of the original branch.
146  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = 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 
160  trueBranchIDToKeptBranchDesc);
161 
162  EDGetToken token;
163  switch (desc.branchType()) {
164  case InEvent:
165  {
166  if(desc.produced()) {
167  keptProductsInEvent.insert(desc.originalBranchID());
168  } else {
169  keptProductsInEvent.insert(desc.branchID());
170  }
172  InputTag{desc.moduleLabel(),
173  desc.productInstanceName(),
174  desc.processName()});
175  break;
176  }
177  case InLumi:
178  {
179  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
180  InputTag(desc.moduleLabel(),
181  desc.productInstanceName(),
182  desc.processName()));
183  break;
184  }
185  case InRun:
186  {
187  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
188  InputTag(desc.moduleLabel(),
189  desc.productInstanceName(),
190  desc.processName()));
191  break;
192  }
193  default:
194  assert(false);
195  break;
196  }
197  // Now put it in the list of selected branches.
198  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
199  }
200 
202 
204  auto nstreams = iPC.numberOfStreams();
205  selectors_.resize(nstreams);
206 
207  bool seenFirst = false;
208  for(auto& s : selectors_) {
209  if(seenFirst) {
213  s,
215  } else {
216  seenFirst = true;
217  }
218  }
219  preallocStreams(nstreams);
220  preallocate(iPC);
221  }
222 
224  this->beginJob();
225  }
226 
228  endJob();
229  }
230 
232  return !wantAllEvents_;
233  }
234 
235  std::vector<ProductResolverIndexAndSkipBit>
237  std::vector<ProductResolverIndexAndSkipBit> returnValue;
238  auto const& s = selectors_[0];
239  auto const n = s.numberOfTokens();
240  returnValue.reserve(n);
241 
242  for(unsigned int i=0; i< n;++i) {
243  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
244  }
245  return returnValue;
246  }
247 
249  if(wantAllEvents_) return true;
250  auto& s = selectors_[id.value()];
252  e.setConsumer(this);
253  return s.wantEvent(e);
254  }
255 
256  bool
258  EventSetup const&,
259  ActivityRegistry* act,
260  ModuleCallingContext const* mcc) {
261 
262  {
264  e.setConsumer(this);
265  EventSignalsSentry sentry(act,mcc);
266  write(e);
267  }
268 
269  auto remainingEvents = remainingEvents_.load();
270  bool keepTrying = remainingEvents > 0;
271  while(keepTrying) {
272  auto newValue = remainingEvents - 1;
273  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
274  if(keepTrying) {
275  // the exchange failed because the value was changed by another thread.
276  // remainingEvents was changed to be the new value of remainingEvents_;
277  keepTrying = remainingEvents > 0;
278  }
279  }
280  return true;
281  }
282 
283  bool
285  EventSetup const&,
286  ModuleCallingContext const* mcc) {
288  r.setConsumer(this);
289  doBeginRun_(r);
290  return true;
291  }
292 
293  bool
295  EventSetup const&,
296  ModuleCallingContext const* mcc) {
298  r.setConsumer(this);
299  doEndRun_(r);
300  return true;
301  }
302 
303  void
305  ModuleCallingContext const* mcc) {
307  r.setConsumer(this);
308  writeRun(r);
309  }
310 
311  bool
313  EventSetup const&,
314  ModuleCallingContext const* mcc) {
316  lb.setConsumer(this);
318  return true;
319  }
320 
321  bool
323  EventSetup const&,
324  ModuleCallingContext const* mcc) {
326  lb.setConsumer(this);
328  return true;
329  }
330 
332  ModuleCallingContext const* mcc) {
334  lb.setConsumer(this);
336  }
337 
339  openFile(fb);
340  }
341 
345  }
346 
349  }
350 
352  if(isFileOpen()) {
353  reallyCloseFile();
354  }
355  }
356 
358  }
359 
360  BranchIDLists const*
362  if(!droppedBranchIDToKeptBranchID_.empty()) {
363  return branchIDLists_.get();
364  }
365  return origBranchIDLists_;
366  }
367 
370  return thinnedAssociationsHelper_.get();
371  }
372 
373  ModuleDescription const&
375  return moduleDescription_;
376  }
377 
378  bool
380  return productSelector_.selected(desc);
381  }
382 
383  void
386  desc.setUnknown();
387  descriptions.addDefault(desc);
388  }
389 
390  void
392  ProductSelectorRules::fillDescription(desc, "outputCommands");
394  desc.addUntracked<unsigned int>("concurrencyLimit",1);
395  }
396 
397  void
399  }
400 
401 
402  static const std::string kBaseType("OutputModule");
403  const std::string&
405  return kBaseType;
406  }
407 
408  void
409  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
410  bool anyProductProduced) {
412  description().moduleLabel(),
413  outputModulePathPositions,
414  anyProductProduced);
415  }
416  }
417 }
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, 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 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 doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *, ModuleCallingContext const *)
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
ParameterSet const & getParameterSet(ParameterSetID const &id)
bool selected(BranchDescription const &desc) const
virtual void doBeginLuminosityBlock_(LuminosityBlockForOutput const &)
OutputModuleBase(ParameterSet const &pset)
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
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 *)
#define nullptr
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 doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext 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 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_
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *)
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_
void setConsumer(EDConsumerBase const *iConsumer)
BranchID const & originalBranchID() const
SelectedProductsForBranchType keptProducts_
static void prevalidate(ConfigurationDescriptions &)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
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)
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)