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 //
10 
11 // system include files
12 #include <cassert>
13 
14 // user include files
16 
36 
37 
38 namespace edm {
39  namespace global {
40 
41  // -------------------------------------------------------
43  maxEvents_(-1),
44  remainingEvents_(maxEvents_),
45  keptProducts_(),
46  hasNewlyDroppedBranch_(),
47  process_name_(),
48  productSelectorRules_(pset, "outputCommands", "OutputModule"),
49  productSelector_(),
50  moduleDescription_(),
51  wantAllEvents_(false),
52  selectors_(),
53  selector_config_id_(),
54  droppedBranchIDToKeptBranchID_(),
55  branchIDLists_(new BranchIDLists),
56  origBranchIDLists_(nullptr),
57  thinnedAssociationsHelper_(new ThinnedAssociationsHelper) {
58 
59  hasNewlyDroppedBranch_.fill(false);
60 
62  process_name_ = tns->getProcessName();
63 
65  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
66 
67  selectEvents_.registerIt(); // Just in case this PSet is not registered
68 
70 
71  //need to set wantAllEvents_ in constructor
72  // we will make the remaining selectors once we know how many streams
73  selectors_.resize(1);
77  selectors_[0],
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 
100  for(auto const& it : preg.productList()) {
101  BranchDescription const& desc = it.second;
102  if(desc.transient()) {
103  // if the class of the branch is marked transient, output nothing
104  } else if(!desc.present() && !desc.produced()) {
105  // else if the branch containing the product has been previously dropped,
106  // output nothing
107  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
108  associationDescriptions.push_back(&desc);
109  } else if(selected(desc)) {
110  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
111  } else {
112  // otherwise, output nothing,
113  // and mark the fact that there is a newly dropped branch of this type.
114  hasNewlyDroppedBranch_[desc.branchType()] = true;
115  }
116  }
117 
118  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
119  keptProductsInEvent,
121 
122  for(auto association : associationDescriptions) {
123  if(keepAssociation_[association->branchID()]) {
124  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
125  } else {
126  hasNewlyDroppedBranch_[association->branchType()] = true;
127  }
128  }
129 
130  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
131  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
132 
133  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
134  }
135 
137  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
138  std::set<BranchID>& keptProductsInEvent) {
139 
141  trueBranchIDToKeptBranchDesc);
142 
143  EDGetToken token;
144  switch (desc.branchType()) {
145  case InEvent:
146  {
147  if(desc.produced()) {
148  keptProductsInEvent.insert(desc.originalBranchID());
149  } else {
150  keptProductsInEvent.insert(desc.branchID());
151  }
153  InputTag{desc.moduleLabel(),
154  desc.productInstanceName(),
155  desc.processName()});
156  break;
157  }
158  case InLumi:
159  {
160  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
161  InputTag(desc.moduleLabel(),
162  desc.productInstanceName(),
163  desc.processName()));
164  break;
165  }
166  case InRun:
167  {
168  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
169  InputTag(desc.moduleLabel(),
170  desc.productInstanceName(),
171  desc.processName()));
172  break;
173  }
174  default:
175  assert(false);
176  break;
177  }
178  // Now put it in the list of selected branches.
179  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
180  }
181 
183 
185  auto nstreams = iPC.numberOfStreams();
186  selectors_.resize(nstreams);
187 
188  bool seenFirst = false;
189  for(auto& s : selectors_) {
190  if(seenFirst) {
194  s,
196  } else {
197  seenFirst = true;
198  }
199  }
200  }
201 
203  this->beginJob();
204  }
205 
207  endJob();
208  }
209 
211  if(wantAllEvents_) return true;
212  auto& s = selectors_[id.value()];
214  e.setConsumer(this);
215  return s.wantEvent(e);
216  }
217 
218  bool
220  EventSetup const&,
221  ActivityRegistry* act,
222  ModuleCallingContext const* mcc) {
223 
224  {
226  e.setConsumer(this);
227  EventSignalsSentry sentry(act,mcc);
228  write(e);
229  }
230 
231  auto remainingEvents = remainingEvents_.load();
232  bool keepTrying = remainingEvents > 0;
233  while(keepTrying) {
234  auto newValue = remainingEvents - 1;
235  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
236  if(keepTrying) {
237  // the exchange failed because the value was changed by another thread.
238  // remainingEvents was changed to be the new value of remainingEvents_;
239  keepTrying = remainingEvents > 0;
240  }
241  }
242  return true;
243  }
244 
245  bool
247  EventSetup const&,
248  ModuleCallingContext const* mcc) {
250  r.setConsumer(this);
251  doBeginRun_(r);
252  return true;
253  }
254 
255  bool
257  EventSetup const&,
258  ModuleCallingContext const* mcc) {
260  r.setConsumer(this);
261  doEndRun_(r);
262  return true;
263  }
264 
265  void
267  ModuleCallingContext const* mcc) {
269  r.setConsumer(this);
270  writeRun(r);
271  }
272 
273  bool
275  EventSetup const&,
276  ModuleCallingContext const* mcc) {
278  lb.setConsumer(this);
280  return true;
281  }
282 
283  bool
285  EventSetup const&,
286  ModuleCallingContext const* mcc) {
288  lb.setConsumer(this);
290  return true;
291  }
292 
294  ModuleCallingContext const* mcc) {
296  lb.setConsumer(this);
298  }
299 
301  openFile(fb);
302  }
303 
306  }
307 
310  }
311 
313  if(isFileOpen()) {
314  reallyCloseFile();
315  }
316  }
317 
319  }
320 
321  BranchIDLists const*
323  if(!droppedBranchIDToKeptBranchID_.empty()) {
324  // Make a private copy of the BranchIDLists.
326  // Check for branches dropped while an EDAlias was kept.
327  for(BranchIDList& branchIDList : *branchIDLists_) {
328  for(BranchID::value_type& branchID : branchIDList) {
329  // 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.
330  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
331  if(iter != droppedBranchIDToKeptBranchID_.end()) {
332  branchID = iter->second;
333  }
334  }
335  }
336  return branchIDLists_.get();
337  }
338  return origBranchIDLists_;
339  }
340 
343  return thinnedAssociationsHelper_.get();
344  }
345 
346  ModuleDescription const&
348  return moduleDescription_;
349  }
350 
351  bool
353  return productSelector_.selected(desc);
354  }
355 
356  void
359  desc.setUnknown();
360  descriptions.addDefault(desc);
361  }
362 
363  void
365  ProductSelectorRules::fillDescription(desc, "outputCommands");
367  }
368 
369  void
371  }
372 
373 
374  static const std::string kBaseType("OutputModule");
375  const std::string&
377  return kBaseType;
378  }
379 
380  void
381  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
382  bool anyProductProduced) {
384  description().moduleLabel(),
385  outputModulePathPositions,
386  anyProductProduced);
387  }
388  }
389 }
static const std::string kBaseType("EDAnalyzer")
bool selected(BranchDescription const &desc) const
void doCloseFile()
Tell the OutputModule that is must end the current file.
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
ModuleDescription const & description() const
BranchIDLists const * origBranchIDLists_
void doRespondToOpenInputFile(FileBlock const &fb)
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *)
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
virtual bool isFileOpen() const
OutputModuleBase(ParameterSet const &pset)
ParameterSetID id() const
ParameterSet const & getParameterSet(ParameterSetID const &id)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
virtual void openFile(FileBlock const &)
virtual void doRespondToOpenInputFile_(FileBlock 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
SelectedProductsForBranchType keptProducts_
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
BranchIDLists const * branchIDLists()
#define nullptr
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *, ModuleCallingContext const *)
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
ModuleDescription moduleDescription_
ProductList const & productList() const
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
void addDefault(ParameterSetDescription const &psetDescription)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
std::string const & moduleLabel() const
unsigned int value_type
Definition: BranchID.h:16
std::string const & productInstanceName() const
void doPreallocate(PreallocationConfiguration const &)
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
std::map< BranchID, bool > keepAssociation_
bool selected(BranchDescription const &desc) const
virtual void doBeginLuminosityBlock_(LuminosityBlockForOutput const &)
TypeID unwrappedTypeID() const
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
static void prevalidate(ConfigurationDescriptions &)
std::vector< BranchDescription const * > allBranchDescriptions() const
static const std::string & baseType()
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
void configure(OutputModuleDescription const &desc)
static void fillDescription(ParameterSetDescription &desc)
static void fillDescription(ParameterSetDescription &desc)
virtual void writeRun(RunForOutput const &)=0
virtual void doEndRun_(RunForOutput const &)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
ProductSelectorRules productSelectorRules_
void doRespondToCloseInputFile(FileBlock const &fb)
std::atomic< int > remainingEvents_
virtual void doEndLuminosityBlock_(LuminosityBlockForOutput const &)
virtual void doBeginRun_(RunForOutput const &)
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
HLT enums.
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
virtual void write(EventForOutput const &)=0
void setConsumer(EDConsumerBase const *iConsumer)
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
BranchID const & originalBranchID() const
virtual void doRespondToCloseInputFile_(FileBlock 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)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
void doOpenFile(FileBlock const &fb)
ParameterSet const & registerIt()
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)