CMS 3D CMS Logo

OutputModule.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 
3 ----------------------------------------------------------------------*/
4 
6 
31 
33 
34 #include <cassert>
35 #include <iostream>
36 
37 namespace edm {
38 
39  // -------------------------------------------------------
41  maxEvents_(-1),
42  remainingEvents_(maxEvents_),
43  keptProducts_(),
44  hasNewlyDroppedBranch_(),
45  process_name_(),
46  productSelectorRules_(pset, "outputCommands", "OutputModule"),
47  productSelector_(),
48  moduleDescription_(),
49  wantAllEvents_(false),
50  selectors_(),
51  selector_config_id_(),
52  droppedBranchIDToKeptBranchID_(),
53  branchIDLists_(new BranchIDLists),
54  origBranchIDLists_(nullptr),
55  thinnedAssociationsHelper_(new ThinnedAssociationsHelper) {
56 
57  hasNewlyDroppedBranch_.fill(false);
58 
60  process_name_ = tns->getProcessName();
61 
63  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
64 
65  selectEvents_.registerIt(); // Just in case this PSet is not registered
66 
68  selectors_.resize(1);
69  //need to set wantAllEvents_ in constructor
70  // we will make the remaining selectors once we know how many streams
74  selectors_[0],
76 
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  std::set<std::string> processesWithSelectedMergeableRunProducts;
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);
113  processesWithSelectedMergeableRunProducts);
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  setProcessesWithSelectedMergeableRunProducts(processesWithSelectedMergeableRunProducts);
122 
123  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
124  keptProductsInEvent,
126 
127  for(auto association : associationDescriptions) {
128  if(keepAssociation_[association->branchID()]) {
129  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
130  } else {
131  hasNewlyDroppedBranch_[association->branchType()] = true;
132  }
133  }
134 
135  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
136  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
137 
138  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
139  }
140 
142  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
143  std::set<BranchID>& keptProductsInEvent) {
144 
146  trueBranchIDToKeptBranchDesc);
147 
148  EDGetToken token;
149 
150  std::vector<std::string> missingDictionaries;
151  if (!checkDictionary(missingDictionaries, desc.className(), desc.unwrappedType())) {
152  std::string context("Calling OutputModule::keepThisBranch, checking dictionaries for kept types");
153  throwMissingDictionariesException(missingDictionaries, context);
154  }
155 
156  switch (desc.branchType()) {
157  case InEvent:
158  {
159  if(desc.produced()) {
160  keptProductsInEvent.insert(desc.originalBranchID());
161  } else {
162  keptProductsInEvent.insert(desc.branchID());
163  }
165  InputTag{desc.moduleLabel(),
166  desc.productInstanceName(),
167  desc.processName()});
168  break;
169  }
170  case InLumi:
171  {
172  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
173  InputTag(desc.moduleLabel(),
174  desc.productInstanceName(),
175  desc.processName()));
176  break;
177  }
178  case InRun:
179  {
180  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
181  InputTag(desc.moduleLabel(),
182  desc.productInstanceName(),
183  desc.processName()));
184  break;
185  }
186  default:
187  assert(false);
188  break;
189  }
190  // Now put it in the list of selected branches.
191  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
192  }
193 
195 
197  auto nstreams = iPC.numberOfStreams();
198  selectors_.resize(nstreams);
199 
200  bool seenFirst = false;
201  for(auto& s : selectors_) {
202  if(seenFirst) {
206  s,
208  }
209  seenFirst = true;
210  }
211  }
212 
213 
215  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
217 
218  this->beginJob();
219  }
220 
222  endJob();
223  }
224 
225 
227  //This cast is safe since we only call const functions of the EventForOutputafter this point
228  Trig result;
229  e.getByToken<TriggerResults>(token, result);
230  return result;
231  }
232 
234  return !wantAllEvents_;
235  }
236 
237  std::vector<ProductResolverIndexAndSkipBit>
239  std::vector<ProductResolverIndexAndSkipBit> returnValue;
240  auto const& s = selectors_[0];
241  auto const n = s.numberOfTokens();
242  returnValue.reserve(n);
243 
244  for(unsigned int i=0; i< n;++i) {
245  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
246  }
247  return returnValue;
248  }
249 
251  if(wantAllEvents_) return true;
252  auto& s = selectors_[id.value()];
254  e.setConsumer(this);
255  return s.wantEvent(e);
256  }
257 
258  bool
260  EventSetup const&,
261  ActivityRegistry* act,
262  ModuleCallingContext const* mcc) {
263 
264  FDEBUG(2) << "writeEvent called\n";
265 
266  {
268  e.setConsumer(this);
269  EventSignalsSentry sentry(act,mcc);
270  write(e);
271  }
272  if(remainingEvents_ > 0) {
274  }
275  return true;
276  }
277 
278 // bool OutputModule::wantEvent(EventForOutput const& ev)
279 // {
280 // getTriggerResults(ev);
281 // bool eventAccepted = false;
282 
283 // typedef std::vector<NamedEventSelector>::const_iterator iter;
284 // for(iter i = selectResult_.begin(), e = selectResult_.end();
285 // !eventAccepted && i != e; ++i)
286 // {
287 // eventAccepted = i->acceptEvent(*prods_);
288 // }
289 
290 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
291 // return eventAccepted;
292 // }
293 
294  bool
296  EventSetup const&,
297  ModuleCallingContext const* mcc) {
298  FDEBUG(2) << "beginRun called\n";
299  RunForOutput r(rp, moduleDescription_, mcc,false);
300  r.setConsumer(this);
301  beginRun(r);
302  return true;
303  }
304 
305  bool
307  EventSetup const&,
308  ModuleCallingContext const* mcc) {
309  FDEBUG(2) << "endRun called\n";
310  RunForOutput r(rp, moduleDescription_, mcc,true);
311  r.setConsumer(this);
312  endRun(r);
313  return true;
314  }
315 
316  void
318  ModuleCallingContext const* mcc,
319  MergeableRunProductMetadata const* mrpm) {
320  FDEBUG(2) << "writeRun called\n";
321  RunForOutput r(rp, moduleDescription_, mcc,true, mrpm);
322  r.setConsumer(this);
323  writeRun(r);
324  }
325 
326  bool
328  EventSetup const&,
329  ModuleCallingContext const* mcc) {
330  FDEBUG(2) << "beginLuminosityBlock called\n";
331  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc,false);
332  lb.setConsumer(this);
334  return true;
335  }
336 
337  bool
339  EventSetup const&,
340  ModuleCallingContext const* mcc) {
341  FDEBUG(2) << "endLuminosityBlock called\n";
342  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc,true);
343  lb.setConsumer(this);
344  endLuminosityBlock(lb);
345  return true;
346  }
347 
349  ModuleCallingContext const* mcc) {
350  FDEBUG(2) << "writeLuminosityBlock called\n";
351  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc,true);
352  lb.setConsumer(this);
354  }
355 
357  openFile(fb);
358  }
359 
362  }
363 
366  }
367 
369  if(isFileOpen()) {
370  reallyCloseFile();
371  }
372  }
373 
375  }
376 
377  BranchIDLists const*
379  if(!droppedBranchIDToKeptBranchID_.empty()) {
380  // Make a private copy of the BranchIDLists.
382  // Check for branches dropped while an EDAlias was kept.
383  for(BranchIDList& branchIDList : *branchIDLists_) {
384  for(BranchID::value_type& branchID : branchIDList) {
385  // 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.
386  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
387  if(iter != droppedBranchIDToKeptBranchID_.end()) {
388  branchID = iter->second;
389  }
390  }
391  }
392  return branchIDLists_.get();
393  }
394  return origBranchIDLists_;
395  }
396 
399  return thinnedAssociationsHelper_.get();
400  }
401 
402  ModuleDescription const&
404  return moduleDescription_;
405  }
406 
407  bool
409  return productSelector_.selected(desc);
410  }
411 
412  void
415  desc.setUnknown();
416  descriptions.addDefault(desc);
417  }
418 
419  void
420  OutputModule::fillDescription(ParameterSetDescription& desc, std::vector<std::string> const& defaultOutputCommands) {
421  ProductSelectorRules::fillDescription(desc, "outputCommands",defaultOutputCommands);
423  }
424 
425  void
427  }
428 
429 
430  static const std::string kBaseType("OutputModule");
431  const std::string&
433  return kBaseType;
434  }
435 
436  void
437  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
438  bool anyProductProduced) {
440  description().moduleLabel(),
441  outputModulePathPositions,
442  anyProductProduced);
443  }
444 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:166
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
Definition: OutputModule.h:182
bool selected(BranchDescription const &desc) const
virtual void endRun(RunForOutput const &)
Definition: OutputModule.h:226
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
void throwMissingDictionariesException(std::vector< std::string > &missingDictionaries, std::string const &context)
ParameterSet selectEvents_
Definition: OutputModule.h:177
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *mcc, MergeableRunProductMetadata const *)
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
ModuleDescription moduleDescription_
Definition: OutputModule.h:171
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:187
ParameterSetID id() const
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
BranchIDLists const * branchIDLists()
ParameterSet const & getParameterSet(ParameterSetID const &id)
void doPreallocate(PreallocationConfiguration const &)
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:189
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
virtual void beginRun(RunForOutput const &)
Definition: OutputModule.h:225
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
Definition: OutputModule.h:183
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
static const std::string & baseType()
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: OutputModule.h:186
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *mcc)
std::string const & processName() const
void doOpenFile(FileBlock const &fb)
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
bool selected(BranchDescription const &desc) const
BranchIDLists const * origBranchIDLists_
Definition: OutputModule.h:184
#define nullptr
#define FDEBUG(lev)
Definition: DebugMacros.h:20
Definition: Electron.h:6
void doCloseFile()
Tell the OutputModule that is must end the current file.
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ProductList const & productList() const
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
ProductResolverIndexAndSkipBit uncheckedIndexFrom(EDGetToken) const
bool getByToken(EDGetToken token, TypeID const &typeID, BasicHandle &result) const
std::string process_name_
Definition: OutputModule.h:168
void addDefault(ParameterSetDescription const &psetDescription)
std::string const & className() const
static void fillDescriptions(ConfigurationDescriptions &descriptions)
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)
~OutputModule() override
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
static SharedResourcesRegistry * instance()
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *act, ModuleCallingContext const *mcc)
static void fillDescription(ParameterSetDescription &desc, std::vector< std::string > const &iDefaultOutputCommands=ProductSelectorRules::defaultSelectionStrings())
TypeID unwrappedTypeID() const
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
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
static void fillDescription(ParameterSetDescription &desc)
virtual bool isFileOpen() const
Definition: OutputModule.h:240
void doRespondToCloseInputFile(FileBlock const &fb)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
Definition: OutputModule.cc:87
static void prevalidate(ConfigurationDescriptions &)
virtual void endLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:229
virtual void beginLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:228
void registerSharedResource(const std::string &)
A resource name must be registered before it can be used in the createAcquirer call.
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:232
Trig getTriggerResults(EDGetTokenT< TriggerResults > const &token, EventForOutput const &e) const
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *mcc)
static const std::string kBaseType("EDAnalyzer")
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
Definition: OutputModule.h:174
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:165
HLT enums.
ProductSelector productSelector_
Definition: OutputModule.h:170
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
virtual void endJob()
Definition: OutputModule.h:224
void setConsumer(EDConsumerBase const *iConsumer)
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:169
bool needToRunSelection() const
virtual void beginJob()
Definition: OutputModule.h:223
BranchID const & originalBranchID() const
void insertSelectedProcesses(BranchDescription const &desc, std::set< std::string > &processes)
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:233
std::vector< ProductResolverIndexAndSkipBit > productsUsedBySelection() const
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *mcc)
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)
virtual void writeRun(RunForOutput const &)=0
void configure(OutputModuleDescription const &desc)
Definition: OutputModule.cc:82
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:231
std::atomic< int > remainingEvents_
Definition: OutputModule.h:148
ParameterSet const & registerIt()
ModuleDescription const & description() const
OutputModule(ParameterSet const &pset)
Definition: OutputModule.cc:40
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)
virtual void write(EventForOutput const &)=0
virtual void setProcessesWithSelectedMergeableRunProducts(std::set< std::string > const &)
Definition: OutputModule.h:235
ParameterSetID selector_config_id_
Definition: OutputModule.h:178