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 
25 
27 
28 #include <cassert>
29 #include <iostream>
30 
31 namespace edm {
32 
33  // -------------------------------------------------------
35  maxEvents_(-1),
36  remainingEvents_(maxEvents_),
37  keptProducts_(),
38  hasNewlyDroppedBranch_(),
39  process_name_(),
40  productSelectorRules_(pset, "outputCommands", "OutputModule"),
41  productSelector_(),
42  moduleDescription_(),
43  wantAllEvents_(false),
44  selectors_(),
45  selector_config_id_(),
46  droppedBranchIDToKeptBranchID_(),
47  branchIDLists_(new BranchIDLists),
48  origBranchIDLists_(nullptr),
49  thinnedAssociationsHelper_(new ThinnedAssociationsHelper) {
50 
51  hasNewlyDroppedBranch_.fill(false);
52 
54  process_name_ = tns->getProcessName();
55 
57  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
58 
59  selectEvents_.registerIt(); // Just in case this PSet is not registered
60 
62  selectors_.resize(1);
63  //need to set wantAllEvents_ in constructor
64  // we will make the remaining selectors once we know how many streams
68  selectors_[0]);
69 
72 
73  }
74 
78  }
79 
81  ThinnedAssociationsHelper const& thinnedAssociationsHelper) {
82  if(productSelector_.initialized()) return;
84 
85  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
86  // single object. See the notes in the header for ProductSelector
87  // for more information.
88 
89  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
90  std::vector<BranchDescription const*> associationDescriptions;
91  std::set<BranchID> keptProductsInEvent;
92 
93  for(auto const& it : preg.productList()) {
94  BranchDescription const& desc = it.second;
95  if(desc.transient()) {
96  // if the class of the branch is marked transient, output nothing
97  } else if(!desc.present() && !desc.produced()) {
98  // else if the branch containing the product has been previously dropped,
99  // output nothing
100  } else if(desc.unwrappedType() == typeid(ThinnedAssociation)) {
101  associationDescriptions.push_back(&desc);
102  } else if(selected(desc)) {
103  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
104  } else {
105  // otherwise, output nothing,
106  // and mark the fact that there is a newly dropped branch of this type.
107  hasNewlyDroppedBranch_[desc.branchType()] = true;
108  }
109  }
110 
111  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
112  keptProductsInEvent,
114 
115  for(auto association : associationDescriptions) {
116  if(keepAssociation_[association->branchID()]) {
117  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
118  } else {
119  hasNewlyDroppedBranch_[association->branchType()] = true;
120  }
121  }
122 
123  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
124  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
125 
126  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
127  }
128 
130  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
131  std::set<BranchID>& keptProductsInEvent) {
132 
134  trueBranchIDToKeptBranchDesc);
135 
136  switch (desc.branchType()) {
137  case InEvent:
138  {
139  if(desc.produced()) {
140  keptProductsInEvent.insert(desc.originalBranchID());
141  } else {
142  keptProductsInEvent.insert(desc.branchID());
143  }
145  InputTag{desc.moduleLabel(),
146  desc.productInstanceName(),
147  desc.processName()});
148  break;
149  }
150  case InLumi:
151  {
152  consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
153  InputTag(desc.moduleLabel(),
154  desc.productInstanceName(),
155  desc.processName()));
156  break;
157  }
158  case InRun:
159  {
160  consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
161  InputTag(desc.moduleLabel(),
162  desc.productInstanceName(),
163  desc.processName()));
164  break;
165  }
166  default:
167  assert(false);
168  break;
169  }
170  // Now put it in the list of selected branches.
171  keptProducts_[desc.branchType()].push_back(&desc);
172  }
173 
175 
177  auto nstreams = iPC.numberOfStreams();
178  selectors_.resize(nstreams);
179 
180  bool seenFirst = false;
181  for(auto& s : selectors_) {
182  if(seenFirst) {
186  s);
187  }
188  seenFirst = true;
189  }
190  }
191 
192 
194  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
196 
197  this->beginJob();
198  }
199 
201  endJob();
202  }
203 
204 
206  //This cast is safe since we only call const functions of the EventPrincipal after this point
208  adapter.setConsumer(this);
209  Trig result;
210  auto bh = adapter.getByToken_(TypeID(typeid(TriggerResults)),PRODUCT_TYPE, token, mcc);
211  convert_handle(std::move(bh), result);
212  return result;
213  }
214 
216 
217  auto& s = selectors_[id.value()];
218  return wantAllEvents_ or s.wantEvent(ep,mcc);
219  }
220 
221  bool
223  EventSetup const&,
224  ActivityRegistry* act,
225  ModuleCallingContext const* mcc) {
226 
227  FDEBUG(2) << "writeEvent called\n";
228 
229  {
230  std::lock_guard<std::mutex> guard(mutex_);
231 
232  {
233  std::lock_guard<SharedResourcesAcquirer> guardAcq(resourceAcquirer_);
234  EventSignalsSentry signals(act,mcc);
235  write(ep, mcc);
236  }
237  }
238  if(remainingEvents_ > 0) {
240  }
241  return true;
242  }
243 
244 // bool OutputModule::wantEvent(Event const& ev)
245 // {
246 // getTriggerResults(ev);
247 // bool eventAccepted = false;
248 
249 // typedef std::vector<NamedEventSelector>::const_iterator iter;
250 // for(iter i = selectResult_.begin(), e = selectResult_.end();
251 // !eventAccepted && i != e; ++i)
252 // {
253 // eventAccepted = i->acceptEvent(*prods_);
254 // }
255 
256 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
257 // return eventAccepted;
258 // }
259 
260  bool
262  EventSetup const&,
263  ModuleCallingContext const* mcc) {
264  FDEBUG(2) << "beginRun called\n";
265  beginRun(rp, mcc);
266  return true;
267  }
268 
269  bool
271  EventSetup const&,
272  ModuleCallingContext const* mcc) {
273  FDEBUG(2) << "endRun called\n";
274  endRun(rp, mcc);
275  return true;
276  }
277 
278  void
280  ModuleCallingContext const* mcc) {
281  FDEBUG(2) << "writeRun called\n";
282  writeRun(rp, mcc);
283  }
284 
285  bool
287  EventSetup const&,
288  ModuleCallingContext const* mcc) {
289  FDEBUG(2) << "beginLuminosityBlock called\n";
290  beginLuminosityBlock(lbp, mcc);
291  return true;
292  }
293 
294  bool
296  EventSetup const&,
297  ModuleCallingContext const* mcc) {
298  FDEBUG(2) << "endLuminosityBlock called\n";
299  endLuminosityBlock(lbp, mcc);
300  return true;
301  }
302 
304  ModuleCallingContext const* mcc) {
305  FDEBUG(2) << "writeLuminosityBlock called\n";
306  writeLuminosityBlock(lbp, mcc);
307  }
308 
310  openFile(fb);
311  }
312 
315  }
316 
319  }
320 
321  void
324  }
325 
326  void
327  OutputModule::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
328  postForkReacquireResources(iChildIndex, iNumberOfChildren);
329  }
330 
332  if(!isFileOpen()) reallyOpenFile();
333  }
334 
336  if(isFileOpen()) {
337  reallyCloseFile();
338  }
339  }
340 
342  }
343 
344  BranchIDLists const*
346  if(!droppedBranchIDToKeptBranchID_.empty()) {
347  // Make a private copy of the BranchIDLists.
349  // Check for branches dropped while an EDAlias was kept.
350  for(BranchIDList& branchIDList : *branchIDLists_) {
351  for(BranchID::value_type& branchID : branchIDList) {
352  // 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.
353  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
354  if(iter != droppedBranchIDToKeptBranchID_.end()) {
355  branchID = iter->second;
356  }
357  }
358  }
359  return branchIDLists_.get();
360  }
361  return origBranchIDLists_;
362  }
363 
366  return thinnedAssociationsHelper_.get();
367  }
368 
369  ModuleDescription const&
371  return moduleDescription_;
372  }
373 
374  bool
376  return productSelector_.selected(desc);
377  }
378 
379  void
382  desc.setUnknown();
383  descriptions.addDefault(desc);
384  }
385 
386  void
388  ProductSelectorRules::fillDescription(desc, "outputCommands");
390  }
391 
392  void
394  }
395 
396 
397  static const std::string kBaseType("OutputModule");
398  const std::string&
400  return kBaseType;
401  }
402 
403  void
404  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
405  bool anyProductProduced) {
407  description().moduleLabel(),
408  outputModulePathPositions,
409  anyProductProduced);
410  }
411 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:150
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:166
bool selected(BranchDescription const &desc) const
static void fillDescription(ParameterSetDescription &desc, char const *parameterName)
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:161
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:155
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:171
ParameterSetID id() const
BranchIDLists const * branchIDLists()
ParameterSet const & getParameterSet(ParameterSetID const &id)
void doPreallocate(PreallocationConfiguration const &)
assert(m_qm.get())
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:173
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
Definition: OutputModule.h:167
bool prePrefetchSelection(StreamID id, EventPrincipal const &, ModuleCallingContext const *)
virtual void reallyOpenFile()
Definition: OutputModule.h:227
static const std::string & baseType()
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: OutputModule.h:170
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:168
#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)
tuple result
Definition: mps_fire.py:83
virtual void endLuminosityBlock(LuminosityBlockPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:217
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:152
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
std::mutex mutex_
Definition: OutputModule.h:174
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:216
static SharedResourcesRegistry * instance()
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *act, ModuleCallingContext const *mcc)
def move
Definition: eostools.py:510
TypeID unwrappedTypeID() const
void doPreForkReleaseResources()
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:225
void doRespondToCloseInputFile(FileBlock const &fb)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
void convert_handle(BasicHandle &&bh, Handle< T > &result)
Definition: ConvertHandle.h:19
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:223
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
Definition: OutputModule.cc:80
static void prevalidate(ConfigurationDescriptions &)
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 beginRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:213
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:220
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:158
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:149
ProductSelector productSelector_
Definition: OutputModule.h:154
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:212
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:153
Trig getTriggerResults(EDGetTokenT< TriggerResults > const &token, EventPrincipal const &ep, ModuleCallingContext const *) const
virtual void beginJob()
Definition: OutputModule.h:211
BranchID const & originalBranchID() const
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:221
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:75
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:219
preg
Definition: Schedule.cc:374
std::atomic< int > remainingEvents_
Definition: OutputModule.h:132
virtual void writeRun(RunPrincipal const &, ModuleCallingContext const *)=0
virtual void endRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:214
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
virtual void preForkReleaseResources()
Definition: OutputModule.h:222
OutputModule(ParameterSet const &pset)
Definition: OutputModule.cc:34
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)
ParameterSetID selector_config_id_
Definition: OutputModule.h:162