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  hasNewlyDroppedBranch_.fill(false);
57 
59  process_name_ = tns->getProcessName();
60 
61  selectEvents_ = pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
62 
63  selectEvents_.registerIt(); // Just in case this PSet is not registered
64 
66  selectors_.resize(1);
67  //need to set wantAllEvents_ in constructor
68  // we will make the remaining selectors once we know how many streams
71 
73  }
74 
78  }
79 
83  return;
85 
86  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
87  // single object. See the notes in the header for ProductSelector
88  // for more information.
89 
90  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
91  std::vector<BranchDescription const*> associationDescriptions;
92  std::set<BranchID> keptProductsInEvent;
93  std::set<std::string> processesWithSelectedMergeableRunProducts;
94 
95  for (auto const& it : preg.productList()) {
96  BranchDescription const& desc = it.second;
97  if (desc.transient()) {
98  // if the class of the branch is marked transient, output nothing
99  } else if (!desc.present() && !desc.produced()) {
100  // else if the branch containing the product has been previously dropped,
101  // output nothing
102  } else if (desc.unwrappedType() == typeid(ThinnedAssociation)) {
103  associationDescriptions.push_back(&desc);
104  } else if (selected(desc)) {
105  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
106  insertSelectedProcesses(desc, processesWithSelectedMergeableRunProducts);
107  } else {
108  // otherwise, output nothing,
109  // and mark the fact that there is a newly dropped branch of this type.
110  hasNewlyDroppedBranch_[desc.branchType()] = true;
111  }
112  }
113 
114  setProcessesWithSelectedMergeableRunProducts(processesWithSelectedMergeableRunProducts);
115 
116  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions, keptProductsInEvent, keepAssociation_);
117 
118  for (auto association : associationDescriptions) {
119  if (keepAssociation_[association->branchID()]) {
120  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
121  } else {
122  hasNewlyDroppedBranch_[association->branchType()] = true;
123  }
124  }
125 
126  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
127  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
128 
129  thinnedAssociationsHelper_->updateFromParentProcess(
130  thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
131  }
132 
134  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
135  std::set<BranchID>& keptProductsInEvent) {
136  ProductSelector::checkForDuplicateKeptBranch(desc, trueBranchIDToKeptBranchDesc);
137 
139 
140  std::vector<std::string> missingDictionaries;
141  if (!checkDictionary(missingDictionaries, desc.className(), desc.unwrappedType())) {
142  std::string context("Calling OutputModule::keepThisBranch, checking dictionaries for kept types");
143  throwMissingDictionariesException(missingDictionaries, context);
144  }
145 
146  switch (desc.branchType()) {
147  case InEvent: {
148  if (desc.produced()) {
149  keptProductsInEvent.insert(desc.originalBranchID());
150  } else {
151  keptProductsInEvent.insert(desc.branchID());
152  }
154  InputTag{desc.moduleLabel(), desc.productInstanceName(), desc.processName()});
155  break;
156  }
157  case InLumi: {
158  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(), PRODUCT_TYPE},
159  InputTag(desc.moduleLabel(), desc.productInstanceName(), desc.processName()));
160  break;
161  }
162  case InRun: {
163  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(), PRODUCT_TYPE},
164  InputTag(desc.moduleLabel(), desc.productInstanceName(), desc.processName()));
165  break;
166  }
167  default:
168  assert(false);
169  break;
170  }
171  // Now put it in the list of selected branches.
172  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
173  }
174 
176 
178  auto nstreams = iPC.numberOfStreams();
179  selectors_.resize(nstreams);
180 
181  bool seenFirst = false;
182  for (auto& s : selectors_) {
183  if (seenFirst) {
185  }
186  seenFirst = true;
187  }
188  }
189 
191  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
193 
194  this->beginJob();
195  }
196 
198 
200  //This cast is safe since we only call const functions of the EventForOutputafter this point
201  Trig result;
203  return result;
204  }
205 
207 
208  std::vector<ProductResolverIndexAndSkipBit> OutputModule::productsUsedBySelection() const {
209  std::vector<ProductResolverIndexAndSkipBit> returnValue;
210  auto const& s = selectors_[0];
211  auto const n = s.numberOfTokens();
212  returnValue.reserve(n);
213 
214  for (unsigned int i = 0; i < n; ++i) {
215  returnValue.emplace_back(uncheckedIndexFrom(s.token(i)));
216  }
217  return returnValue;
218  }
219 
221  if (wantAllEvents_)
222  return true;
223  auto& s = selectors_[id.value()];
225  e.setConsumer(this);
226  return s.wantEvent(e);
227  }
228 
230  EventSetupImpl const&,
231  ActivityRegistry* act,
232  ModuleCallingContext const* mcc) {
233  FDEBUG(2) << "writeEvent called\n";
234 
235  {
237  e.setConsumer(this);
238  EventSignalsSentry sentry(act, mcc);
239  write(e);
240  }
241  if (remainingEvents_ > 0) {
243  }
244  return true;
245  }
246 
247  // bool OutputModule::wantEvent(EventForOutput const& ev)
248  // {
249  // getTriggerResults(ev);
250  // bool eventAccepted = false;
251 
252  // typedef std::vector<NamedEventSelector>::const_iterator iter;
253  // for(iter i = selectResult_.begin(), e = selectResult_.end();
254  // !eventAccepted && i != e; ++i)
255  // {
256  // eventAccepted = i->acceptEvent(*prods_);
257  // }
258 
259  // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
260  // return eventAccepted;
261  // }
262 
264  FDEBUG(2) << "beginRun called\n";
265  RunForOutput r(rp, moduleDescription_, mcc, false);
266  r.setConsumer(this);
267  beginRun(r);
268  return true;
269  }
270 
272  FDEBUG(2) << "endRun called\n";
273  RunForOutput r(rp, moduleDescription_, mcc, true);
274  r.setConsumer(this);
275  endRun(r);
276  return true;
277  }
278 
280  ModuleCallingContext const* mcc,
281  MergeableRunProductMetadata const* mrpm) {
282  FDEBUG(2) << "writeRun called\n";
283  RunForOutput r(rp, moduleDescription_, mcc, true, mrpm);
284  r.setConsumer(this);
285  writeRun(r);
286  }
287 
289  EventSetupImpl const&,
290  ModuleCallingContext const* mcc) {
291  FDEBUG(2) << "beginLuminosityBlock called\n";
292  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, false);
293  lb.setConsumer(this);
295  return true;
296  }
297 
299  EventSetupImpl const&,
300  ModuleCallingContext const* mcc) {
301  FDEBUG(2) << "endLuminosityBlock called\n";
302  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
303  lb.setConsumer(this);
304  endLuminosityBlock(lb);
305  return true;
306  }
307 
309  FDEBUG(2) << "writeLuminosityBlock called\n";
310  LuminosityBlockForOutput lb(lbp, moduleDescription_, mcc, true);
311  lb.setConsumer(this);
313  }
314 
316 
318 
320 
322  if (isFileOpen()) {
323  reallyCloseFile();
324  }
325  }
326 
328 
330  if (!droppedBranchIDToKeptBranchID_.empty()) {
331  // Make a private copy of the BranchIDLists.
333  // Check for branches dropped while an EDAlias was kept.
334  for (BranchIDList& branchIDList : *branchIDLists_) {
335  for (BranchID::value_type& branchID : branchIDList) {
336  // 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.
337  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter =
338  droppedBranchIDToKeptBranchID_.find(branchID);
339  if (iter != droppedBranchIDToKeptBranchID_.end()) {
340  branchID = iter->second;
341  }
342  }
343  }
344  return branchIDLists_.get();
345  }
346  return origBranchIDLists_;
347  }
348 
350  return thinnedAssociationsHelper_.get();
351  }
352 
354 
355  bool OutputModule::selected(BranchDescription const& desc) const { return productSelector_.selected(desc); }
356 
359  desc.setUnknown();
360  descriptions.addDefault(desc);
361  }
362 
364  std::vector<std::string> const& defaultOutputCommands) {
365  ProductSelectorRules::fillDescription(desc, "outputCommands", defaultOutputCommands);
367  }
368 
370 
371  static const std::string kBaseType("OutputModule");
373 
375  std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
376  bool anyProductProduced) {
379  outputModulePathPositions,
380  anyProductProduced);
381  }
382 } // namespace edm
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:171
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:187
bool selected(BranchDescription const &desc) const
virtual void endRun(RunForOutput const &)
Definition: OutputModule.h:230
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:182
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *mcc, MergeableRunProductMetadata const *)
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) 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:176
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:192
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetupImpl const &c, ModuleCallingContext const *mcc)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetupImpl const &c, ModuleCallingContext const *mcc)
ParameterSetID id() const
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
BranchIDLists const * branchIDLists()
bool doEndRun(RunPrincipal const &rp, EventSetupImpl const &c, ModuleCallingContext const *mcc)
ParameterSet const & getParameterSet(ParameterSetID const &id)
void doPreallocate(PreallocationConfiguration const &)
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:194
#define nullptr
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
virtual void beginRun(RunForOutput const &)
Definition: OutputModule.h:229
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
Definition: OutputModule.h:188
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
static const std::string & baseType()
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: OutputModule.h:191
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:189
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
bool doEvent(EventPrincipal const &ep, EventSetupImpl const &c, ActivityRegistry *act, ModuleCallingContext const *mcc)
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
ProductResolverIndexAndSkipBit uncheckedIndexFrom(EDGetToken) const
std::string process_name_
Definition: OutputModule.h:173
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()
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
static void fillDescription(ParameterSetDescription &desc)
virtual bool isFileOpen() const
Definition: OutputModule.h:244
void doRespondToCloseInputFile(FileBlock const &fb)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int >>> const &outputModulePathPositions, bool anyProductProduced)
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
Definition: OutputModule.cc:80
static void prevalidate(ConfigurationDescriptions &)
virtual void endLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:233
bool doBeginRun(RunPrincipal const &rp, EventSetupImpl const &c, ModuleCallingContext const *mcc)
virtual void beginLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:232
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:236
Trig getTriggerResults(EDGetTokenT< TriggerResults > const &token, EventForOutput const &e) const
static const std::string kBaseType("EDAnalyzer")
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
Definition: OutputModule.h:179
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:170
HLT enums.
ProductSelector productSelector_
Definition: OutputModule.h:175
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:228
void setConsumer(EDConsumerBase const *iConsumer)
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:174
bool needToRunSelection() const
virtual void beginJob()
Definition: OutputModule.h:227
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:237
std::vector< ProductResolverIndexAndSkipBit > productsUsedBySelection() 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)
virtual void writeRun(RunForOutput const &)=0
void configure(OutputModuleDescription const &desc)
Definition: OutputModule.cc:75
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:235
#define FDEBUG(lev)
Definition: DebugMacros.h:19
std::atomic< int > remainingEvents_
Definition: OutputModule.h:153
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:239
ParameterSetID selector_config_id_
Definition: OutputModule.h:183