CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
OutputModule.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 
3 ----------------------------------------------------------------------*/
4 
6 
26 
28 
29 #include <cassert>
30 #include <iostream>
31 
32 namespace edm {
33 
34  // -------------------------------------------------------
36  maxEvents_(-1),
37  remainingEvents_(maxEvents_),
38  keptProducts_(),
39  hasNewlyDroppedBranch_(),
40  process_name_(),
41  productSelectorRules_(pset, "outputCommands", "OutputModule"),
42  productSelector_(),
43  moduleDescription_(),
44  wantAllEvents_(false),
45  selectors_(),
46  selector_config_id_(),
47  droppedBranchIDToKeptBranchID_(),
48  branchIDLists_(new BranchIDLists),
49  origBranchIDLists_(nullptr),
50  thinnedAssociationsHelper_(new ThinnedAssociationsHelper),
51  branchParents_(),
52  branchChildren_() {
53 
54  hasNewlyDroppedBranch_.fill(false);
55 
57  process_name_ = tns->getProcessName();
58 
60  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
61 
62  selectEvents_.registerIt(); // Just in case this PSet is not registered
63 
65  selectors_.resize(1);
66  //need to set wantAllEvents_ in constructor
67  // we will make the remaining selectors once we know how many streams
71  selectors_[0]);
72 
75 
76  }
77 
81  }
82 
84  ThinnedAssociationsHelper const& thinnedAssociationsHelper) {
85  if(productSelector_.initialized()) return;
87 
88  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
89  // single object. See the notes in the header for ProductSelector
90  // for more information.
91 
92  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
93  std::vector<BranchDescription const*> associationDescriptions;
94  std::set<BranchID> keptProductsInEvent;
95 
96  for(auto const& it : preg.productList()) {
97  BranchDescription const& desc = it.second;
98  if(desc.transient()) {
99  // if the class of the branch is marked transient, output nothing
100  } else if(!desc.present() && !desc.produced()) {
101  // else if the branch containing the product has been previously dropped,
102  // output nothing
103  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
104  associationDescriptions.push_back(&desc);
105  } else if(selected(desc)) {
106  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
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  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
115  keptProductsInEvent,
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(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
130  }
131 
133  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
134  std::set<BranchID>& keptProductsInEvent) {
135 
137  trueBranchIDToKeptBranchDesc);
138 
139  switch (desc.branchType()) {
140  case InEvent:
141  {
142  if(desc.produced()) {
143  keptProductsInEvent.insert(desc.originalBranchID());
144  } else {
145  keptProductsInEvent.insert(desc.branchID());
146  }
148  InputTag{desc.moduleLabel(),
149  desc.productInstanceName(),
150  desc.processName()});
151  break;
152  }
153  case InLumi:
154  {
155  consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
156  InputTag(desc.moduleLabel(),
157  desc.productInstanceName(),
158  desc.processName()));
159  break;
160  }
161  case InRun:
162  {
163  consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
164  InputTag(desc.moduleLabel(),
165  desc.productInstanceName(),
166  desc.processName()));
167  break;
168  }
169  default:
170  assert(false);
171  break;
172  }
173  // Now put it in the list of selected branches.
174  keptProducts_[desc.branchType()].push_back(&desc);
175  }
176 
178 
180  auto nstreams = iPC.numberOfStreams();
181  selectors_.resize(nstreams);
182 
183  bool seenFirst = false;
184  for(auto& s : selectors_) {
185  if(seenFirst) {
189  s);
190  }
191  seenFirst = true;
192  }
193  }
194 
195 
197  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
199 
200  this->beginJob();
201  }
202 
204  endJob();
205  }
206 
207 
209  //This cast is safe since we only call const functions of the EventPrincipal after this point
210  PrincipalGetAdapter adapter(const_cast<EventPrincipal&>(ep), moduleDescription_);
211  adapter.setConsumer(this);
212  Trig result;
213  auto bh = adapter.getByToken_(TypeID(typeid(TriggerResults)),PRODUCT_TYPE, token, mcc);
214  convert_handle(std::move(bh), result);
215  return result;
216  }
217 
219 
220  auto& s = selectors_[id.value()];
221  detail::TRBESSentry products_sentry(s);
222 
223  return wantAllEvents_ or s.wantEvent(ep,mcc);
224  }
225 
226  bool
228  EventSetup const&,
229  ActivityRegistry* act,
230  ModuleCallingContext const* mcc) {
231 
232  FDEBUG(2) << "writeEvent called\n";
233 
234  {
235  std::lock_guard<std::mutex> guard(mutex_);
236 
237  {
238  std::lock_guard<SharedResourcesAcquirer> guardAcq(resourceAcquirer_);
239  EventSignalsSentry signals(act,mcc);
240  write(ep, mcc);
241  }
243  }
244  if(remainingEvents_ > 0) {
246  }
247  return true;
248  }
249 
250 // bool OutputModule::wantEvent(Event const& ev)
251 // {
252 // getTriggerResults(ev);
253 // bool eventAccepted = false;
254 
255 // typedef std::vector<NamedEventSelector>::const_iterator iter;
256 // for(iter i = selectResult_.begin(), e = selectResult_.end();
257 // !eventAccepted && i != e; ++i)
258 // {
259 // eventAccepted = i->acceptEvent(*prods_);
260 // }
261 
262 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
263 // return eventAccepted;
264 // }
265 
266  bool
268  EventSetup const&,
269  ModuleCallingContext const* mcc) {
270  FDEBUG(2) << "beginRun called\n";
271  beginRun(rp, mcc);
272  return true;
273  }
274 
275  bool
277  EventSetup const&,
278  ModuleCallingContext const* mcc) {
279  FDEBUG(2) << "endRun called\n";
280  endRun(rp, mcc);
281  return true;
282  }
283 
284  void
286  ModuleCallingContext const* mcc) {
287  FDEBUG(2) << "writeRun called\n";
288  writeRun(rp, mcc);
289  }
290 
291  bool
293  EventSetup const&,
294  ModuleCallingContext const* mcc) {
295  FDEBUG(2) << "beginLuminosityBlock called\n";
296  beginLuminosityBlock(lbp, mcc);
297  return true;
298  }
299 
300  bool
302  EventSetup const&,
303  ModuleCallingContext const* mcc) {
304  FDEBUG(2) << "endLuminosityBlock called\n";
305  endLuminosityBlock(lbp, mcc);
306  return true;
307  }
308 
310  ModuleCallingContext const* mcc) {
311  FDEBUG(2) << "writeLuminosityBlock called\n";
312  writeLuminosityBlock(lbp, mcc);
313  }
314 
316  openFile(fb);
317  }
318 
321  }
322 
325  }
326 
327  void
330  }
331 
332  void
333  OutputModule::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
334  postForkReacquireResources(iChildIndex, iNumberOfChildren);
335  }
336 
338  if(!isFileOpen()) reallyOpenFile();
339  }
340 
342  if(isFileOpen()) {
344  reallyCloseFile();
345  branchParents_.clear();
347  }
348  }
349 
351  }
352 
353  BranchIDLists const*
355  if(!droppedBranchIDToKeptBranchID_.empty()) {
356  // Make a private copy of the BranchIDLists.
358  // Check for branches dropped while an EDAlias was kept.
359  for(BranchIDList& branchIDList : *branchIDLists_) {
360  for(BranchID::value_type& branchID : branchIDList) {
361  // 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.
362  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
363  if(iter != droppedBranchIDToKeptBranchID_.end()) {
364  branchID = iter->second;
365  }
366  }
367  }
368  return branchIDLists_.get();
369  }
370  return origBranchIDLists_;
371  }
372 
375  return thinnedAssociationsHelper_.get();
376  }
377 
378  ModuleDescription const&
380  return moduleDescription_;
381  }
382 
383  bool
385  return productSelector_.selected(desc);
386  }
387 
388  void
391  desc.setUnknown();
392  descriptions.addDefault(desc);
393  }
394 
395  void
397  ProductSelectorRules::fillDescription(desc, "outputCommands");
399  }
400 
401  void
403  }
404 
405 
406  static const std::string kBaseType("OutputModule");
407  const std::string&
409  return kBaseType;
410  }
411 
412  void
413  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
414  bool anyProductProduced) {
416  description().moduleLabel(),
417  outputModulePathPositions,
418  anyProductProduced);
419  }
420 
421  void
423  for(EventPrincipal::const_iterator i = ep.begin(), iEnd = ep.end(); i != iEnd; ++i) {
424  if((*i) && (*i)->productProvenancePtr() != 0) {
425  BranchID const& bid = (*i)->branchDescription().branchID();
426  BranchParents::iterator it = branchParents_.find(bid);
427  if(it == branchParents_.end()) {
428  it = branchParents_.insert(std::make_pair(bid, std::set<ParentageID>())).first;
429  }
430  it->second.insert((*i)->productProvenancePtr()->parentageID());
432  }
433  }
434  }
435 
436  void
438  for(BranchParents::const_iterator i = branchParents_.begin(), iEnd = branchParents_.end();
439  i != iEnd; ++i) {
440  BranchID const& child = i->first;
441  std::set<ParentageID> const& eIds = i->second;
442  for(std::set<ParentageID>::const_iterator it = eIds.begin(), itEnd = eIds.end();
443  it != itEnd; ++it) {
444  Parentage entryDesc;
445  ParentageRegistry::instance()->getMapped(*it, entryDesc);
446  std::vector<BranchID> const& parents = entryDesc.parents();
447  for(std::vector<BranchID>::const_iterator j = parents.begin(), jEnd = parents.end();
448  j != jEnd; ++j) {
449  branchChildren_.insertChild(*j, child);
450  }
451  }
452  }
453  }
454 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:153
BasicHandle getByToken_(TypeID const &id, KindOfType kindOfType, EDGetToken token, ModuleCallingContext const *mcc) const
void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
void setConsumer(EDConsumerBase const *iConsumer)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
Definition: OutputModule.h:169
int i
Definition: DBlmapReader.cc:9
bool selected(BranchDescription const &desc) const
static void fillDescription(ParameterSetDescription &desc, char const *parameterName)
TPRegexp parents
Definition: eve_filter.cc:24
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
virtual void writeLuminosityBlock(LuminosityBlockPrincipal const &, ModuleCallingContext const *)=0
ParameterSet selectEvents_
Definition: OutputModule.h:164
virtual void write(EventPrincipal const &e, ModuleCallingContext const *)=0
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
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventIDconst &, edm::Timestampconst & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:174
const_iterator end() const
Definition: Principal.h:158
ParameterSetID id() const
ParameterSet const & getParameterSet(ParameterSetID const &id)
void doPreallocate(PreallocationConfiguration const &)
assert(m_qm.get())
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:181
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
void insertEmpty(BranchID parent)
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
virtual void reallyOpenFile()
Definition: OutputModule.h:235
void insertChild(BranchID parent, BranchID child)
static const std::string & baseType()
void updateBranchParents(EventPrincipal const &ep)
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *mcc)
std::string const & processName() const
void doOpenFile(FileBlock const &fb)
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
void doCloseFile()
Tell the OutputModule that is must end the current file.
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
virtual void endLuminosityBlock(LuminosityBlockPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:225
std::vector< BranchID > const & parents() const
Definition: Parentage.h:37
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
std::string process_name_
Definition: OutputModule.h:155
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
std::mutex mutex_
Definition: OutputModule.h:182
bool getMapped(key_type const &k, value_type &result) const
std::string const & moduleLabel() const
unsigned int value_type
Definition: BranchID.h:16
std::string const & productInstanceName() const
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *mcc)
virtual void beginLuminosityBlock(LuminosityBlockPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:224
static SharedResourcesRegistry * instance()
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *act, ModuleCallingContext const *mcc)
tuple result
Definition: query.py:137
TypeID unwrappedTypeID() const
void doPreForkReleaseResources()
std::unique_ptr< BranchIDLists > branchIDLists_
Definition: OutputModule.h:170
int j
Definition: DBlmapReader.cc:9
std::vector< BranchDescription const * > allBranchDescriptions() const
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:233
void doRespondToCloseInputFile(FileBlock const &fb)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
void fillDependencyGraph()
std::unique_ptr< ThinnedAssociationsHelper > thinnedAssociationsHelper_
Definition: OutputModule.h:173
void convert_handle(BasicHandle &&bh, Handle< T > &result)
Definition: ConvertHandle.h:20
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:231
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
Definition: OutputModule.cc:83
static void prevalidate(ConfigurationDescriptions &)
void registerSharedResource(const std::string &)
A resource name must be registered before it can be used in the createAcquirer call.
const_iterator begin() const
Definition: Principal.h:157
ThinnedAssociationsHelper const * thinnedAssociationsHelper() const
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.
virtual void beginRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:221
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:228
static void fillDescription(ParameterSetDescription &desc)
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
ProductSelector productSelector_
Definition: OutputModule.h:157
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
BranchChildren branchChildren_
Definition: OutputModule.h:179
boost::filter_iterator< FilledProductPtr, ProductHolderCollection::const_iterator > const_iterator
Definition: Principal.h:56
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
BranchIDLists const * branchIDLists() const
virtual void endJob()
Definition: OutputModule.h:220
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:156
Trig getTriggerResults(EDGetTokenT< TriggerResults > const &token, EventPrincipal const &ep, ModuleCallingContext const *) const
virtual void beginJob()
Definition: OutputModule.h:219
BranchID const & originalBranchID() const
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:229
volatile std::atomic< bool > shutdown_flag false
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)
void configure(OutputModuleDescription const &desc)
Definition: OutputModule.cc:78
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:227
preg
Definition: Schedule.cc:370
static ParentageRegistry * instance()
std::atomic< int > remainingEvents_
Definition: OutputModule.h:135
virtual void writeRun(RunPrincipal const &, ModuleCallingContext const *)=0
virtual void endRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:222
bool configureEventSelector(edm::ParameterSet const &iPSet, std::string const &iProcessName, std::vector< std::string > const &iAllTriggerNames, edm::detail::TriggerResultsBasedEventSelector &oSelector)
ParameterSet const & registerIt()
ModuleDescription const & description() const
BranchParents branchParents_
Definition: OutputModule.h:177
virtual void preForkReleaseResources()
Definition: OutputModule.h:230
OutputModule(ParameterSet const &pset)
Definition: OutputModule.cc:35
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)
ParameterSetID selector_config_id_
Definition: OutputModule.h:165