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 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 
59  hasNewlyDroppedBranch_.fill(false);
60 
62  process_name_ = tns->getProcessName();
63 
65  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
66 
67  selectEvents_.registerIt(); // Just in case this PSet is not registered
68 
70 
71  //need to set wantAllEvents_ in constructor
72  // we will make the remaining selectors once we know how many streams
73  selectors_.resize(1);
77  selectors_[0],
79 
80  }
81 
85  }
86 
89  if(productSelector_.initialized()) return;
91 
92  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
93  // single object. See the notes in the header for ProductSelector
94  // for more information.
95 
96  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
97  std::vector<BranchDescription const*> associationDescriptions;
98  std::set<BranchID> keptProductsInEvent;
99 
100  for(auto const& it : preg.productList()) {
101  BranchDescription const& desc = it.second;
102  if(desc.transient()) {
103  // if the class of the branch is marked transient, output nothing
104  } else if(!desc.present() && !desc.produced()) {
105  // else if the branch containing the product has been previously dropped,
106  // output nothing
107  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
108  associationDescriptions.push_back(&desc);
109  } else if(selected(desc)) {
110  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
111  } else {
112  // otherwise, output nothing,
113  // and mark the fact that there is a newly dropped branch of this type.
114  hasNewlyDroppedBranch_[desc.branchType()] = true;
115  }
116  }
117 
118  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
119  keptProductsInEvent,
121 
122  for(auto association : associationDescriptions) {
123  if(keepAssociation_[association->branchID()]) {
124  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
125  } else {
126  hasNewlyDroppedBranch_[association->branchType()] = true;
127  }
128  }
129 
130  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
131  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
132 
133  thinnedAssociationsHelper_->updateFromParentProcess(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 = 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 
159  trueBranchIDToKeptBranchDesc);
160 
161  EDGetToken token;
162  switch (desc.branchType()) {
163  case InEvent:
164  {
165  if(desc.produced()) {
166  keptProductsInEvent.insert(desc.originalBranchID());
167  } else {
168  keptProductsInEvent.insert(desc.branchID());
169  }
171  InputTag{desc.moduleLabel(),
172  desc.productInstanceName(),
173  desc.processName()});
174  break;
175  }
176  case InLumi:
177  {
178  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
179  InputTag(desc.moduleLabel(),
180  desc.productInstanceName(),
181  desc.processName()));
182  break;
183  }
184  case InRun:
185  {
186  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
187  InputTag(desc.moduleLabel(),
188  desc.productInstanceName(),
189  desc.processName()));
190  break;
191  }
192  default:
193  assert(false);
194  break;
195  }
196  // Now put it in the list of selected branches.
197  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
198  }
199 
201 
203  auto nstreams = iPC.numberOfStreams();
204  selectors_.resize(nstreams);
205 
206  bool seenFirst = false;
207  for(auto& s : selectors_) {
208  if(seenFirst) {
212  s,
214  } else {
215  seenFirst = true;
216  }
217  }
218  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) {
287  RunForOutput r(rp, moduleDescription_, mcc, false);
288  r.setConsumer(this);
289  doBeginRun_(r);
290  return true;
291  }
292 
293  bool
295  EventSetup const&,
296  ModuleCallingContext const* mcc) {
297  RunForOutput r(rp, moduleDescription_, mcc, true);
298  r.setConsumer(this);
299  doEndRun_(r);
300  return true;
301  }
302 
303  void
305  ModuleCallingContext const* mcc) {
306  RunForOutput r(rp, moduleDescription_, mcc, true);
307  r.setConsumer(this);
308  writeRun(r);
309  }
310 
311  bool
313  EventSetup const&,
314  ModuleCallingContext const* mcc) {
315  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
316  lb.setConsumer(this);
318  return true;
319  }
320 
321  bool
323  EventSetup const&,
324  ModuleCallingContext const* mcc) {
325  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
326  lb.setConsumer(this);
328  return true;
329  }
330 
332  ModuleCallingContext const* mcc) {
333  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
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  }
395 
396  void
398  }
399 
400 
401  static const std::string kBaseType("OutputModule");
402  const std::string&
404  return kBaseType;
405  }
406 
407  void
408  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
409  bool anyProductProduced) {
411  description().moduleLabel(),
412  outputModulePathPositions,
413  anyProductProduced);
414  }
415  }
416 }
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
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)
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
std::vector< ProductResolverIndexAndSkipBit > productsUsedBySelection() const
BranchID const & originalBranchID() const
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()
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)