CMS 3D CMS Logo

OutputModule.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 
3 ----------------------------------------------------------------------*/
4 
6 
30 
32 
33 #include <cassert>
34 #include <iostream>
35 
36 namespace edm {
37 
38  // -------------------------------------------------------
40  maxEvents_(-1),
41  remainingEvents_(maxEvents_),
42  keptProducts_(),
43  hasNewlyDroppedBranch_(),
44  process_name_(),
45  productSelectorRules_(pset, "outputCommands", "OutputModule"),
46  productSelector_(),
47  moduleDescription_(),
48  wantAllEvents_(false),
49  selectors_(),
50  selector_config_id_(),
51  droppedBranchIDToKeptBranchID_(),
52  branchIDLists_(new BranchIDLists),
53  origBranchIDLists_(nullptr),
54  thinnedAssociationsHelper_(new ThinnedAssociationsHelper) {
55 
56  hasNewlyDroppedBranch_.fill(false);
57 
59  process_name_ = tns->getProcessName();
60 
62  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
63 
64  selectEvents_.registerIt(); // Just in case this PSet is not registered
65 
67  selectors_.resize(1);
68  //need to set wantAllEvents_ in constructor
69  // we will make the remaining selectors once we know how many streams
73  selectors_[0],
75 
78 
79  }
80 
84  }
85 
88  if(productSelector_.initialized()) return;
90 
91  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
92  // single object. See the notes in the header for ProductSelector
93  // for more information.
94 
95  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
96  std::vector<BranchDescription const*> associationDescriptions;
97  std::set<BranchID> keptProductsInEvent;
98 
99  for(auto const& it : preg.productList()) {
100  BranchDescription const& desc = it.second;
101  if(desc.transient()) {
102  // if the class of the branch is marked transient, output nothing
103  } else if(!desc.present() && !desc.produced()) {
104  // else if the branch containing the product has been previously dropped,
105  // output nothing
106  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
107  associationDescriptions.push_back(&desc);
108  } else if(selected(desc)) {
109  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
110  } else {
111  // otherwise, output nothing,
112  // and mark the fact that there is a newly dropped branch of this type.
113  hasNewlyDroppedBranch_[desc.branchType()] = true;
114  }
115  }
116 
117  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
118  keptProductsInEvent,
120 
121  for(auto association : associationDescriptions) {
122  if(keepAssociation_[association->branchID()]) {
123  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
124  } else {
125  hasNewlyDroppedBranch_[association->branchType()] = true;
126  }
127  }
128 
129  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
130  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
131 
132  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
133  }
134 
136  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
137  std::set<BranchID>& keptProductsInEvent) {
138 
140  trueBranchIDToKeptBranchDesc);
141 
142  EDGetToken token;
143 
144  std::vector<std::string> missingDictionaries;
145  if (!checkDictionary(missingDictionaries, desc.className(), desc.unwrappedType())) {
146  std::string context("Calling OutputModule::keepThisBranch, checking dictionaries for kept types");
147  throwMissingDictionariesException(missingDictionaries, context);
148  }
149 
150  switch (desc.branchType()) {
151  case InEvent:
152  {
153  if(desc.produced()) {
154  keptProductsInEvent.insert(desc.originalBranchID());
155  } else {
156  keptProductsInEvent.insert(desc.branchID());
157  }
159  InputTag{desc.moduleLabel(),
160  desc.productInstanceName(),
161  desc.processName()});
162  break;
163  }
164  case InLumi:
165  {
166  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
167  InputTag(desc.moduleLabel(),
168  desc.productInstanceName(),
169  desc.processName()));
170  break;
171  }
172  case InRun:
173  {
174  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
175  InputTag(desc.moduleLabel(),
176  desc.productInstanceName(),
177  desc.processName()));
178  break;
179  }
180  default:
181  assert(false);
182  break;
183  }
184  // Now put it in the list of selected branches.
185  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
186  }
187 
189 
191  auto nstreams = iPC.numberOfStreams();
192  selectors_.resize(nstreams);
193 
194  bool seenFirst = false;
195  for(auto& s : selectors_) {
196  if(seenFirst) {
200  s,
202  }
203  seenFirst = true;
204  }
205  }
206 
207 
209  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
211 
212  this->beginJob();
213  }
214 
216  endJob();
217  }
218 
219 
221  //This cast is safe since we only call const functions of the EventForOutputafter this point
222  Trig result;
223  e.getByToken<TriggerResults>(token, result);
224  return result;
225  }
226 
228  return !wantAllEvents_;
229  }
230 
231  std::vector<ProductResolverIndexAndSkipBit>
233  std::vector<ProductResolverIndexAndSkipBit> returnValue;
234  auto const& s = selectors_[0];
235  auto const n = s.numberOfTokens();
236  returnValue.reserve(n);
237 
238  for(unsigned int i=0; i< n;++i) {
239  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
240  }
241  return returnValue;
242  }
243 
245  if(wantAllEvents_) return true;
246  auto& s = selectors_[id.value()];
248  e.setConsumer(this);
249  return s.wantEvent(e);
250  }
251 
252  bool
254  EventSetup const&,
255  ActivityRegistry* act,
256  ModuleCallingContext const* mcc) {
257 
258  FDEBUG(2) << "writeEvent called\n";
259 
260  {
262  e.setConsumer(this);
263  EventSignalsSentry sentry(act,mcc);
264  write(e);
265  }
266  if(remainingEvents_ > 0) {
268  }
269  return true;
270  }
271 
272 // bool OutputModule::wantEvent(EventForOutput const& ev)
273 // {
274 // getTriggerResults(ev);
275 // bool eventAccepted = false;
276 
277 // typedef std::vector<NamedEventSelector>::const_iterator iter;
278 // for(iter i = selectResult_.begin(), e = selectResult_.end();
279 // !eventAccepted && i != e; ++i)
280 // {
281 // eventAccepted = i->acceptEvent(*prods_);
282 // }
283 
284 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
285 // return eventAccepted;
286 // }
287 
288  bool
290  EventSetup const&,
291  ModuleCallingContext const* mcc) {
292  FDEBUG(2) << "beginRun called\n";
293  RunForOutput r(rp, moduleDescription_, mcc,false);
294  r.setConsumer(this);
295  beginRun(r);
296  return true;
297  }
298 
299  bool
301  EventSetup const&,
302  ModuleCallingContext const* mcc) {
303  FDEBUG(2) << "endRun called\n";
304  RunForOutput r(rp, moduleDescription_, mcc,true);
305  r.setConsumer(this);
306  endRun(r);
307  return true;
308  }
309 
310  void
312  ModuleCallingContext const* mcc) {
313  FDEBUG(2) << "writeRun called\n";
314  RunForOutput r(rp, moduleDescription_, mcc,true);
315  r.setConsumer(this);
316  writeRun(r);
317  }
318 
319  bool
321  EventSetup const&,
322  ModuleCallingContext const* mcc) {
323  FDEBUG(2) << "beginLuminosityBlock called\n";
324  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc,false);
325  lb.setConsumer(this);
327  return true;
328  }
329 
330  bool
332  EventSetup const&,
333  ModuleCallingContext const* mcc) {
334  FDEBUG(2) << "endLuminosityBlock called\n";
335  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc,true);
336  lb.setConsumer(this);
337  endLuminosityBlock(lb);
338  return true;
339  }
340 
342  ModuleCallingContext const* mcc) {
343  FDEBUG(2) << "writeLuminosityBlock called\n";
344  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc,true);
345  lb.setConsumer(this);
347  }
348 
350  openFile(fb);
351  }
352 
355  }
356 
359  }
360 
362  if(isFileOpen()) {
363  reallyCloseFile();
364  }
365  }
366 
368  }
369 
370  BranchIDLists const*
372  if(!droppedBranchIDToKeptBranchID_.empty()) {
373  // Make a private copy of the BranchIDLists.
375  // Check for branches dropped while an EDAlias was kept.
376  for(BranchIDList& branchIDList : *branchIDLists_) {
377  for(BranchID::value_type& branchID : branchIDList) {
378  // 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.
379  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
380  if(iter != droppedBranchIDToKeptBranchID_.end()) {
381  branchID = iter->second;
382  }
383  }
384  }
385  return branchIDLists_.get();
386  }
387  return origBranchIDLists_;
388  }
389 
392  return thinnedAssociationsHelper_.get();
393  }
394 
395  ModuleDescription const&
397  return moduleDescription_;
398  }
399 
400  bool
402  return productSelector_.selected(desc);
403  }
404 
405  void
408  desc.setUnknown();
409  descriptions.addDefault(desc);
410  }
411 
412  void
413  OutputModule::fillDescription(ParameterSetDescription& desc, std::vector<std::string> const& defaultOutputCommands) {
414  ProductSelectorRules::fillDescription(desc, "outputCommands",defaultOutputCommands);
416  }
417 
418  void
420  }
421 
422 
423  static const std::string kBaseType("OutputModule");
424  const std::string&
426  return kBaseType;
427  }
428 
429  void
430  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
431  bool anyProductProduced) {
433  description().moduleLabel(),
434  outputModulePathPositions,
435  anyProductProduced);
436  }
437 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:164
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:180
bool selected(BranchDescription const &desc) const
virtual void endRun(RunForOutput const &)
Definition: OutputModule.h:224
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:175
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:169
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:185
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:187
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
virtual void beginRun(RunForOutput const &)
Definition: OutputModule.h:223
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
Definition: OutputModule.h:181
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
static const std::string & baseType()
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: OutputModule.h:184
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:182
#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:166
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
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *mcc)
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:236
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:86
static void prevalidate(ConfigurationDescriptions &)
virtual void endLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:227
virtual void beginLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:226
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:230
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:172
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:163
HLT enums.
ProductSelector productSelector_
Definition: OutputModule.h:168
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:222
void setConsumer(EDConsumerBase const *iConsumer)
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:167
bool needToRunSelection() const
virtual void beginJob()
Definition: OutputModule.h:221
BranchID const & originalBranchID() const
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:231
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:81
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:229
std::atomic< int > remainingEvents_
Definition: OutputModule.h:146
ParameterSet const & registerIt()
ModuleDescription const & description() const
OutputModule(ParameterSet const &pset)
Definition: OutputModule.cc:39
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)
virtual void write(EventForOutput const &)=0
ParameterSetID selector_config_id_
Definition: OutputModule.h:176