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);
221  preallocate(iPC);
222  }
223 
225  this->beginJob();
226  }
227 
229  endJob();
230  }
231 
233  return !wantAllEvents_;
234  }
235 
236  std::vector<ProductResolverIndexAndSkipBit>
238  std::vector<ProductResolverIndexAndSkipBit> returnValue;
239  auto const& s = selectors_[0];
240  auto const n = s.numberOfTokens();
241  returnValue.reserve(n);
242 
243  for(unsigned int i=0; i< n;++i) {
244  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
245  }
246  return returnValue;
247  }
248 
250  if(wantAllEvents_) return true;
251  auto& s = selectors_[id.value()];
253  e.setConsumer(this);
254  return s.wantEvent(e);
255  }
256 
257  bool
259  EventSetup const&,
260  ActivityRegistry* act,
261  ModuleCallingContext const* mcc) {
262 
263  {
265  e.setConsumer(this);
266  EventSignalsSentry sentry(act,mcc);
267  write(e);
268  }
269 
270  auto remainingEvents = remainingEvents_.load();
271  bool keepTrying = remainingEvents > 0;
272  while(keepTrying) {
273  auto newValue = remainingEvents - 1;
274  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
275  if(keepTrying) {
276  // the exchange failed because the value was changed by another thread.
277  // remainingEvents was changed to be the new value of remainingEvents_;
278  keepTrying = remainingEvents > 0;
279  }
280  }
281  return true;
282  }
283 
284  bool
286  EventSetup const&,
287  ModuleCallingContext const* mcc) {
288  RunForOutput r(rp, moduleDescription_, mcc, false);
289  r.setConsumer(this);
290  doBeginRun_(r);
291  return true;
292  }
293 
294  bool
296  EventSetup const&,
297  ModuleCallingContext const* mcc) {
298  RunForOutput r(rp, moduleDescription_, mcc, true);
299  r.setConsumer(this);
300  doEndRun_(r);
301  return true;
302  }
303 
304  void
306  ModuleCallingContext const* mcc) {
307  RunForOutput r(rp, moduleDescription_, mcc, true);
308  r.setConsumer(this);
309  writeRun(r);
310  }
311 
312  bool
314  EventSetup const&,
315  ModuleCallingContext const* mcc) {
316  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
317  lb.setConsumer(this);
319  return true;
320  }
321 
322  bool
324  EventSetup const&,
325  ModuleCallingContext const* mcc) {
326  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
327  lb.setConsumer(this);
329  return true;
330  }
331 
333  ModuleCallingContext const* mcc) {
334  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
335  lb.setConsumer(this);
337  }
338 
340  openFile(fb);
341  }
342 
346  }
347 
350  }
351 
353  if(isFileOpen()) {
354  reallyCloseFile();
355  }
356  }
357 
359  }
360 
361  BranchIDLists const*
363  if(!droppedBranchIDToKeptBranchID_.empty()) {
364  return branchIDLists_.get();
365  }
366  return origBranchIDLists_;
367  }
368 
371  return thinnedAssociationsHelper_.get();
372  }
373 
374  ModuleDescription const&
376  return moduleDescription_;
377  }
378 
379  bool
381  return productSelector_.selected(desc);
382  }
383 
384  void
387  desc.setUnknown();
388  descriptions.addDefault(desc);
389  }
390 
391  void
393  ProductSelectorRules::fillDescription(desc, "outputCommands");
395  desc.addUntracked<unsigned int>("concurrencyLimit",1);
396  }
397 
398  void
400  }
401 
402 
403  static const std::string kBaseType("OutputModule");
404  const std::string&
406  return kBaseType;
407  }
408 
409  void
410  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
411  bool anyProductProduced) {
413  description().moduleLabel(),
414  outputModulePathPositions,
415  anyProductProduced);
416  }
417  }
418 }
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_
virtual void preallocLumis(unsigned int)
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)