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  if(wantAllEvents_) return true;
229  auto& s = selectors_[id.value()];
231  e.setConsumer(this);
232  return s.wantEvent(e);
233  }
234 
235  bool
237  EventSetup const&,
238  ActivityRegistry* act,
239  ModuleCallingContext const* mcc) {
240 
241  FDEBUG(2) << "writeEvent called\n";
242 
243  {
245  e.setConsumer(this);
246  EventSignalsSentry sentry(act,mcc);
247  write(e);
248  }
249  if(remainingEvents_ > 0) {
251  }
252  return true;
253  }
254 
255 // bool OutputModule::wantEvent(EventForOutput const& ev)
256 // {
257 // getTriggerResults(ev);
258 // bool eventAccepted = false;
259 
260 // typedef std::vector<NamedEventSelector>::const_iterator iter;
261 // for(iter i = selectResult_.begin(), e = selectResult_.end();
262 // !eventAccepted && i != e; ++i)
263 // {
264 // eventAccepted = i->acceptEvent(*prods_);
265 // }
266 
267 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
268 // return eventAccepted;
269 // }
270 
271  bool
273  EventSetup const&,
274  ModuleCallingContext const* mcc) {
275  FDEBUG(2) << "beginRun called\n";
277  r.setConsumer(this);
278  beginRun(r);
279  return true;
280  }
281 
282  bool
284  EventSetup const&,
285  ModuleCallingContext const* mcc) {
286  FDEBUG(2) << "endRun called\n";
288  r.setConsumer(this);
289  endRun(r);
290  return true;
291  }
292 
293  void
295  ModuleCallingContext const* mcc) {
296  FDEBUG(2) << "writeRun called\n";
298  r.setConsumer(this);
299  writeRun(r);
300  }
301 
302  bool
304  EventSetup const&,
305  ModuleCallingContext const* mcc) {
306  FDEBUG(2) << "beginLuminosityBlock called\n";
308  lb.setConsumer(this);
310  return true;
311  }
312 
313  bool
315  EventSetup const&,
316  ModuleCallingContext const* mcc) {
317  FDEBUG(2) << "endLuminosityBlock called\n";
319  lb.setConsumer(this);
320  endLuminosityBlock(lb);
321  return true;
322  }
323 
325  ModuleCallingContext const* mcc) {
326  FDEBUG(2) << "writeLuminosityBlock called\n";
328  lb.setConsumer(this);
330  }
331 
333  openFile(fb);
334  }
335 
338  }
339 
342  }
343 
344  void
347  }
348 
349  void
350  OutputModule::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
351  postForkReacquireResources(iChildIndex, iNumberOfChildren);
352  }
353 
355  if(!isFileOpen()) reallyOpenFile();
356  }
357 
359  if(isFileOpen()) {
360  reallyCloseFile();
361  }
362  }
363 
365  }
366 
367  BranchIDLists const*
369  if(!droppedBranchIDToKeptBranchID_.empty()) {
370  // Make a private copy of the BranchIDLists.
372  // Check for branches dropped while an EDAlias was kept.
373  for(BranchIDList& branchIDList : *branchIDLists_) {
374  for(BranchID::value_type& branchID : branchIDList) {
375  // 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.
376  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
377  if(iter != droppedBranchIDToKeptBranchID_.end()) {
378  branchID = iter->second;
379  }
380  }
381  }
382  return branchIDLists_.get();
383  }
384  return origBranchIDLists_;
385  }
386 
389  return thinnedAssociationsHelper_.get();
390  }
391 
392  ModuleDescription const&
394  return moduleDescription_;
395  }
396 
397  bool
399  return productSelector_.selected(desc);
400  }
401 
402  void
405  desc.setUnknown();
406  descriptions.addDefault(desc);
407  }
408 
409  void
410  OutputModule::fillDescription(ParameterSetDescription& desc, std::vector<std::string> const& defaultOutputCommands) {
411  ProductSelectorRules::fillDescription(desc, "outputCommands",defaultOutputCommands);
413  }
414 
415  void
417  }
418 
419 
420  static const std::string kBaseType("OutputModule");
421  const std::string&
423  return kBaseType;
424  }
425 
426  void
427  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
428  bool anyProductProduced) {
430  description().moduleLabel(),
431  outputModulePathPositions,
432  anyProductProduced);
433  }
434 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:153
void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
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:169
bool selected(BranchDescription const &desc) const
virtual void endRun(RunForOutput const &)
Definition: OutputModule.h:220
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:164
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:158
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:174
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:176
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
virtual void beginRun(RunForOutput const &)
Definition: OutputModule.h:219
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
Definition: OutputModule.h:170
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
virtual void reallyOpenFile()
Definition: OutputModule.h:233
static const std::string & baseType()
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: OutputModule.h:173
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:171
#define nullptr
#define FDEBUG(lev)
Definition: DebugMacros.h:18
Definition: Electron.h:4
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
bool getByToken(EDGetToken token, TypeID const &typeID, BasicHandle &result) const
std::string process_name_
Definition: OutputModule.h:155
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)
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
void doPreForkReleaseResources()
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:231
void doRespondToCloseInputFile(FileBlock const &fb)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:229
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
Definition: OutputModule.cc:86
static void prevalidate(ConfigurationDescriptions &)
virtual void endLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:223
virtual void beginLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:222
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:226
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:161
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:152
HLT enums.
ProductSelector productSelector_
Definition: OutputModule.h:157
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:218
void setConsumer(EDConsumerBase const *iConsumer)
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:156
virtual void beginJob()
Definition: OutputModule.h:217
BranchID const & originalBranchID() const
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:227
virtual ~OutputModule()
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:225
std::atomic< int > remainingEvents_
Definition: OutputModule.h:135
ParameterSet const & registerIt()
ModuleDescription const & description() const
virtual void preForkReleaseResources()
Definition: OutputModule.h:228
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:165