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 limited {
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  queue_(pset.getUntrackedParameter<unsigned int>("concurrencyLimit")) {
59 
60  hasNewlyDroppedBranch_.fill(false);
61 
63  process_name_ = tns->getProcessName();
64 
66  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
67 
68  selectEvents_.registerIt(); // Just in case this PSet is not registered
69 
71 
72  //need to set wantAllEvents_ in constructor
73  // we will make the remaining selectors once we know how many streams
74  selectors_.resize(1);
78  selectors_[0],
80 
81  }
82 
86  }
87 
90  if(productSelector_.initialized()) return;
92 
93  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
94  // single object. See the notes in the header for ProductSelector
95  // for more information.
96 
97  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
98  std::vector<BranchDescription const*> associationDescriptions;
99  std::set<BranchID> keptProductsInEvent;
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);
112  } else {
113  // otherwise, output nothing,
114  // and mark the fact that there is a newly dropped branch of this type.
115  hasNewlyDroppedBranch_[desc.branchType()] = true;
116  }
117  }
118 
119  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
120  keptProductsInEvent,
122 
123  for(auto association : associationDescriptions) {
124  if(keepAssociation_[association->branchID()]) {
125  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
126  } else {
127  hasNewlyDroppedBranch_[association->branchType()] = true;
128  }
129  }
130 
131  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
132  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
133 
134  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
135  }
136 
138  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
139  std::set<BranchID>& keptProductsInEvent) {
140 
142  trueBranchIDToKeptBranchDesc);
143 
144  EDGetToken token;
145  switch (desc.branchType()) {
146  case InEvent:
147  {
148  if(desc.produced()) {
149  keptProductsInEvent.insert(desc.originalBranchID());
150  } else {
151  keptProductsInEvent.insert(desc.branchID());
152  }
154  InputTag{desc.moduleLabel(),
155  desc.productInstanceName(),
156  desc.processName()});
157  break;
158  }
159  case InLumi:
160  {
161  token = consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
162  InputTag(desc.moduleLabel(),
163  desc.productInstanceName(),
164  desc.processName()));
165  break;
166  }
167  case InRun:
168  {
169  token = consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
170  InputTag(desc.moduleLabel(),
171  desc.productInstanceName(),
172  desc.processName()));
173  break;
174  }
175  default:
176  assert(false);
177  break;
178  }
179  // Now put it in the list of selected branches.
180  keptProducts_[desc.branchType()].push_back(std::make_pair(&desc, token));
181  }
182 
184 
186  auto nstreams = iPC.numberOfStreams();
187  selectors_.resize(nstreams);
188 
189  bool seenFirst = false;
190  for(auto& s : selectors_) {
191  if(seenFirst) {
195  s,
197  } else {
198  seenFirst = true;
199  }
200  }
201  }
202 
204  this->beginJob();
205  }
206 
208  endJob();
209  }
210 
212  if(wantAllEvents_) return true;
213  auto& s = selectors_[id.value()];
215  e.setConsumer(this);
216  return s.wantEvent(e);
217  }
218 
219  bool
221  EventSetup const&,
222  ActivityRegistry* act,
223  ModuleCallingContext const* mcc) {
224 
225  {
227  e.setConsumer(this);
228  EventSignalsSentry sentry(act,mcc);
229  write(e);
230  }
231 
232  auto remainingEvents = remainingEvents_.load();
233  bool keepTrying = remainingEvents > 0;
234  while(keepTrying) {
235  auto newValue = remainingEvents - 1;
236  keepTrying = !remainingEvents_.compare_exchange_strong(remainingEvents, newValue);
237  if(keepTrying) {
238  // the exchange failed because the value was changed by another thread.
239  // remainingEvents was changed to be the new value of remainingEvents_;
240  keepTrying = remainingEvents > 0;
241  }
242  }
243  return true;
244  }
245 
246  bool
248  EventSetup const&,
249  ModuleCallingContext const* mcc) {
251  r.setConsumer(this);
252  doBeginRun_(r);
253  return true;
254  }
255 
256  bool
258  EventSetup const&,
259  ModuleCallingContext const* mcc) {
261  r.setConsumer(this);
262  doEndRun_(r);
263  return true;
264  }
265 
266  void
268  ModuleCallingContext const* mcc) {
270  r.setConsumer(this);
271  writeRun(r);
272  }
273 
274  bool
276  EventSetup const&,
277  ModuleCallingContext const* mcc) {
279  lb.setConsumer(this);
281  return true;
282  }
283 
284  bool
286  EventSetup const&,
287  ModuleCallingContext const* mcc) {
289  lb.setConsumer(this);
291  return true;
292  }
293 
295  ModuleCallingContext const* mcc) {
297  lb.setConsumer(this);
299  }
300 
302  openFile(fb);
303  }
304 
307  }
308 
311  }
312 
314  if(isFileOpen()) {
315  reallyCloseFile();
316  }
317  }
318 
320  }
321 
322  BranchIDLists const*
324  if(!droppedBranchIDToKeptBranchID_.empty()) {
325  // Make a private copy of the BranchIDLists.
327  // Check for branches dropped while an EDAlias was kept.
328  for(BranchIDList& branchIDList : *branchIDLists_) {
329  for(BranchID::value_type& branchID : branchIDList) {
330  // 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.
331  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
332  if(iter != droppedBranchIDToKeptBranchID_.end()) {
333  branchID = iter->second;
334  }
335  }
336  }
337  return branchIDLists_.get();
338  }
339  return origBranchIDLists_;
340  }
341 
344  return thinnedAssociationsHelper_.get();
345  }
346 
347  ModuleDescription const&
349  return moduleDescription_;
350  }
351 
352  bool
354  return productSelector_.selected(desc);
355  }
356 
357  void
360  desc.setUnknown();
361  descriptions.addDefault(desc);
362  }
363 
364  void
366  ProductSelectorRules::fillDescription(desc, "outputCommands");
368  desc.addUntracked<unsigned int>("concurrencyLimit",1);
369  }
370 
371  void
373  }
374 
375 
376  static const std::string kBaseType("OutputModule");
377  const std::string&
379  return kBaseType;
380  }
381 
382  void
383  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
384  bool anyProductProduced) {
386  description().moduleLabel(),
387  outputModulePathPositions,
388  anyProductProduced);
389  }
390  }
391 }
virtual void writeLuminosityBlock(LuminosityBlockForOutput const &)=0
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
std::atomic< int > remainingEvents_
bool selected(BranchDescription const &desc) const
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
std::map< BranchID, bool > keepAssociation_
virtual bool isFileOpen() const
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
void doPreallocate(PreallocationConfiguration const &)
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
virtual void doRespondToOpenInputFile_(FileBlock const &)
BranchIDLists const * origBranchIDLists_
virtual void write(EventForOutput const &)=0
ParameterSetID id() const
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *, ModuleCallingContext const *)
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
ParameterSet const & getParameterSet(ParameterSetID const &id)
bool selected(BranchDescription const &desc) const
virtual void doBeginLuminosityBlock_(LuminosityBlockForOutput const &)
OutputModuleBase(ParameterSet const &pset)
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
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
ModuleDescription const & description() const
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *)
#define nullptr
void configure(OutputModuleDescription const &desc)
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
BranchIDLists const * branchIDLists()
ProductList const & productList() const
virtual void openFile(FileBlock const &) const
void selectAssociationProducts(std::vector< BranchDescription const * > const &associationDescriptions, std::set< BranchID > const &keptProductsInEvent, std::map< BranchID, bool > &keepAssociation) const
virtual void doEndRun_(RunForOutput const &)
void addDefault(ParameterSetDescription const &psetDescription)
static const std::string kBaseType("EDAnalyzer")
virtual void doBeginRun_(RunForOutput const &)
std::string const & moduleLabel() const
unsigned int value_type
Definition: BranchID.h:16
std::string const & productInstanceName() const
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
virtual void writeRun(RunForOutput const &)=0
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
TypeID unwrappedTypeID() const
static void fillDescription(ParameterSetDescription &desc)
std::vector< BranchDescription const * > allBranchDescriptions() const
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext 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)
void doOpenFile(FileBlock const &fb)
void doRespondToOpenInputFile(FileBlock const &fb)
ProductSelectorRules productSelectorRules_
static void fillDescriptions(ConfigurationDescriptions &descriptions)
virtual void doEndLuminosityBlock_(LuminosityBlockForOutput const &)
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *)
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
ModuleDescription moduleDescription_
HLT enums.
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
virtual void doRespondToCloseInputFile_(FileBlock const &)
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
void setConsumer(EDConsumerBase const *iConsumer)
BranchID const & originalBranchID() const
SelectedProductsForBranchType keptProducts_
static void prevalidate(ConfigurationDescriptions &)
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
void doCloseFile()
Tell the OutputModule that is must end the current file.
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)
ParameterSet const & registerIt()
static const std::string & baseType()
void doRespondToCloseInputFile(FileBlock const &fb)
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)