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 // Original Author: Chris Jones
10 // Created: Wed, 31 Jul 2013 15:59:19 GMT
11 //
12 
13 // system include files
14 #include <cassert>
15 
16 // user include files
18 
39 
40 namespace edm {
41  namespace one {
42 
43  // -------------------------------------------------------
45  maxEvents_(-1),
46  remainingEvents_(maxEvents_),
47  keptProducts_(),
48  hasNewlyDroppedBranch_(),
49  process_name_(),
50  productSelectorRules_(pset, "outputCommands", "OutputModule"),
51  productSelector_(),
52  moduleDescription_(),
53  wantAllEvents_(false),
54  selectors_(),
55  selector_config_id_(),
56  droppedBranchIDToKeptBranchID_(),
57  branchIDLists_(new BranchIDLists),
58  origBranchIDLists_(nullptr),
59  thinnedAssociationsHelper_(new ThinnedAssociationsHelper) {
60 
61  hasNewlyDroppedBranch_.fill(false);
62 
64  process_name_ = tns->getProcessName();
65 
67  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
68 
69  selectEvents_.registerIt(); // Just in case this PSet is not registered
70 
72 
73  //need to set wantAllEvents_ in constructor
74  // we will make the remaining selectors once we know how many streams
75  selectors_.resize(1);
79  selectors_[0],
81 
82  }
83 
88  }
89 
92  if(productSelector_.initialized()) return;
94 
95  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
96  // single object. See the notes in the header for ProductSelector
97  // for more information.
98 
99  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
100  std::vector<BranchDescription const*> associationDescriptions;
101  std::set<BranchID> keptProductsInEvent;
102 
103  for(auto const& it : preg.productList()) {
104  BranchDescription const& desc = it.second;
105  if(desc.transient()) {
106  // if the class of the branch is marked transient, output nothing
107  } else if(!desc.present() && !desc.produced()) {
108  // else if the branch containing the product has been previously dropped,
109  // output nothing
110  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
111  associationDescriptions.push_back(&desc);
112  } else if(selected(desc)) {
113  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
114  } else {
115  // otherwise, output nothing,
116  // and mark the fact that there is a newly dropped branch of this type.
117  hasNewlyDroppedBranch_[desc.branchType()] = true;
118  }
119  }
120 
121  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
122  keptProductsInEvent,
124 
125  for(auto association : associationDescriptions) {
126  if(keepAssociation_[association->branchID()]) {
127  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
128  } else {
129  hasNewlyDroppedBranch_[association->branchType()] = true;
130  }
131  }
132 
133  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
134  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
135 
136  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
137  }
138 
140  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
141  std::set<BranchID>& keptProductsInEvent) {
142 
144  trueBranchIDToKeptBranchDesc);
145 
146  EDGetToken token;
147 
148  std::vector<std::string> missingDictionaries;
149  if (!checkDictionary(missingDictionaries, desc.className(), desc.unwrappedType())) {
150  std::string context("Calling OutputModuleBase::keepThisBranch, checking dictionaries for kept types");
151  throwMissingDictionariesException(missingDictionaries, context);
152  }
153 
154  switch (desc.branchType()) {
155  case InEvent:
156  {
157  if(desc.produced()) {
158  keptProductsInEvent.insert(desc.originalBranchID());
159  } else {
160  keptProductsInEvent.insert(desc.branchID());
161  }
163  InputTag{desc.moduleLabel(),
164  desc.productInstanceName(),
165  desc.processName()});
166  break;
167  }
168  case InLumi:
169  {
170  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
171  InputTag(desc.moduleLabel(),
172  desc.productInstanceName(),
173  desc.processName()));
174  break;
175  }
176  case InRun:
177  {
178  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
179  InputTag(desc.moduleLabel(),
180  desc.productInstanceName(),
181  desc.processName()));
182  break;
183  }
184  default:
185  assert(false);
186  break;
187  }
188  // Now put it in the list of selected branches.
189  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
190  }
191 
193 
196  std::vector<std::shared_ptr<SerialTaskQueue>>(1, std::make_shared<SerialTaskQueue>())};
197  }
198 
200  auto nstreams = iPC.numberOfStreams();
201  selectors_.resize(nstreams);
202 
203  bool seenFirst = false;
204  for(auto& s : selectors_) {
205  if(seenFirst) {
209  s,
211  } else {
212  seenFirst = true;
213  }
214  }
215  }
216 
219  this->beginJob();
220  }
221 
223  endJob();
224  }
225 
227  return !wantAllEvents_;
228  }
229 
230  std::vector<ProductResolverIndexAndSkipBit>
232  std::vector<ProductResolverIndexAndSkipBit> returnValue;
233  auto const& s = selectors_[0];
234  auto const n = s.numberOfTokens();
235  returnValue.reserve(n);
236 
237  for(unsigned int i=0; i< n;++i) {
238  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
239  }
240  return returnValue;
241  }
242 
244  if(wantAllEvents_) return true;
245  auto& s = selectors_[id.value()];
247  e.setConsumer(this);
248  return s.wantEvent(e);
249  }
250 
251  bool
253  EventSetup const&,
254  ActivityRegistry* act,
255  ModuleCallingContext const* mcc) {
256 
257  {
259  e.setConsumer(this);
260  EventSignalsSentry sentry(act,mcc);
261  write(e);
262  }
263  if(remainingEvents_ > 0) {
265  }
266  return true;
267  }
268 
269  bool
271  EventSetup const&,
272  ModuleCallingContext const* mcc) {
273  RunForOutput r(rp, moduleDescription_, mcc, false);
274  r.setConsumer(this);
275  doBeginRun_(r);
276  return true;
277  }
278 
279  bool
281  EventSetup const&,
282  ModuleCallingContext const* mcc) {
283  RunForOutput r(rp, moduleDescription_, mcc, true);
284  r.setConsumer(this);
285  doEndRun_(r);
286  return true;
287  }
288 
289  void
291  ModuleCallingContext const* mcc) {
292  RunForOutput r(rp, moduleDescription_, mcc, true);
293  r.setConsumer(this);
294  writeRun(r);
295  }
296 
297  bool
299  EventSetup const&,
300  ModuleCallingContext const* mcc) {
301  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
302  lb.setConsumer(this);
304  return true;
305  }
306 
307  bool
309  EventSetup const&,
310  ModuleCallingContext const* mcc) {
311  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
312  lb.setConsumer(this);
314 
315  return true;
316  }
317 
319  ModuleCallingContext const* mcc) {
320  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
321  lb.setConsumer(this);
323  }
324 
326  openFile(fb);
327  }
328 
331  }
332 
335  }
336 
338  if(isFileOpen()) {
339  reallyCloseFile();
340  }
341  }
342 
344  }
345 
346  BranchIDLists const*
348  if(!droppedBranchIDToKeptBranchID_.empty()) {
349  // Make a private copy of the BranchIDLists.
351  // Check for branches dropped while an EDAlias was kept.
352  for(BranchIDList& branchIDList : *branchIDLists_) {
353  for(BranchID::value_type& branchID : branchIDList) {
354  // Replace BranchID of each dropped branch with that of the kept alias, so the alias branch will have the product ID of the original branch.
355  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
356  if(iter != droppedBranchIDToKeptBranchID_.end()) {
357  branchID = iter->second;
358  }
359  }
360  }
361  return branchIDLists_.get();
362  }
363  return origBranchIDLists_;
364  }
365 
368  return thinnedAssociationsHelper_.get();
369  }
370 
371  ModuleDescription const&
373  return moduleDescription_;
374  }
375 
376  bool
378  return productSelector_.selected(desc);
379  }
380 
381  void
384  desc.setUnknown();
385  descriptions.addDefault(desc);
386  }
387 
388  void
390  ProductSelectorRules::fillDescription(desc, "outputCommands");
392  }
393 
394  void
396  }
397 
398 
399  static const std::string kBaseType("OutputModule");
400  const std::string&
402  return kBaseType;
403  }
404 
405  void
406  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
407  bool anyProductProduced) {
409  description().moduleLabel(),
410  outputModulePathPositions,
411  anyProductProduced);
412  }
413  }
414 }
static const std::string kBaseType("EDAnalyzer")
bool selected(BranchDescription const &desc) const
ModuleDescription const & description() const
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
void throwMissingDictionariesException(std::vector< std::string > &missingDictionaries, std::string const &context)
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
SelectedProductsForBranchType keptProducts_
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *)
ModuleDescription moduleDescription_
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *)
static void prevalidate(ConfigurationDescriptions &)
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *, ModuleCallingContext const *)
ParameterSetID id() const
SubProcessParentageHelper const * subProcessParentageHelper_
void configure(OutputModuleDescription const &desc)
ParameterSet const & getParameterSet(ParameterSetID const &id)
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext 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
#define nullptr
virtual void doBeginLuminosityBlock_(LuminosityBlockForOutput const &)
ProductSelectorRules productSelectorRules_
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
virtual void doRespondToCloseInputFile_(FileBlock const &)
ProductList const & productList() const
virtual void doRespondToOpenInputFile_(FileBlock const &)
virtual void openFile(FileBlock 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 &)
virtual void write(EventForOutput const &)=0
ProductResolverIndexAndSkipBit uncheckedIndexFrom(EDGetToken) const
void addDefault(ParameterSetDescription const &psetDescription)
std::string const & className() const
std::atomic< int > remainingEvents_
std::string const & moduleLabel() const
unsigned int value_type
Definition: BranchID.h:16
std::string const & productInstanceName() const
bool checkDictionary(std::vector< std::string > &missingDictionaries, TypeID const &typeID)
bool selected(BranchDescription const &desc) const
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
TypeID unwrappedTypeID() const
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
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)
SubProcessParentageHelper const * subProcessParentageHelper_
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
SharedResourcesAcquirer resourcesAcquirer_
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
OutputModuleBase(ParameterSet const &pset)
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
virtual void writeRun(RunForOutput const &)=0
virtual void doEndLuminosityBlock_(LuminosityBlockForOutput const &)
void doRespondToCloseInputFile(FileBlock const &fb)
virtual SharedResourcesAcquirer createAcquirer()
void doRespondToOpenInputFile(FileBlock const &fb)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
virtual void doBeginRun_(RunForOutput const &)
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
HLT enums.
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
static const std::string & baseType()
void doPreallocate(PreallocationConfiguration const &)
void setConsumer(EDConsumerBase const *iConsumer)
void doOpenFile(FileBlock const &fb)
BranchIDLists const * origBranchIDLists_
BranchID const & originalBranchID() const
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext 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)
void doCloseFile()
Tell the OutputModule that is must end the current file.
static void fillDescription(ParameterSetDescription &desc)
virtual bool isFileOpen() const
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
ProductSelector productSelector_
std::vector< ProductResolverIndexAndSkipBit > productsUsedBySelection() const
ParameterSet const & registerIt()
ParameterSetID selector_config_id_
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)
BranchIDLists const * branchIDLists()
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
std::map< BranchID, bool > keepAssociation_