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 
22 
24 
25 #include <cassert>
26 #include <iostream>
27 
28 namespace edm {
29 
30  // -------------------------------------------------------
32  maxEvents_(-1),
33  remainingEvents_(maxEvents_),
34  keptProducts_(),
35  hasNewlyDroppedBranch_(),
36  process_name_(),
37  productSelectorRules_(pset, "outputCommands", "OutputModule"),
38  productSelector_(),
39  moduleDescription_(),
40  wantAllEvents_(false),
41  selectors_(),
42  selector_config_id_(),
43  droppedBranchIDToKeptBranchID_(),
44  branchIDLists_(new BranchIDLists),
45  origBranchIDLists_(nullptr),
46  branchParents_(),
47  branchChildren_() {
48 
49  hasNewlyDroppedBranch_.fill(false);
50 
52  process_name_ = tns->getProcessName();
53 
54  ParameterSet selectevents =
55  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
56 
57  selectevents.registerIt(); // Just in case this PSet is not registered
58 
59  selector_config_id_ = selectevents.id();
63  selectors_);
66 
67  }
68 
72  }
73 
75  if(productSelector_.initialized()) return;
77 
78  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
79  // single object. See the notes in the header for ProductSelector
80  // for more information.
81 
82  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
83 
84  for(auto const& it : preg.productList()) {
85  BranchDescription const& desc = it.second;
86  if(desc.transient()) {
87  // if the class of the branch is marked transient, output nothing
88  } else if(!desc.present() && !desc.produced()) {
89  // else if the branch containing the product has been previously dropped,
90  // output nothing
91  } else if(selected(desc)) {
92  // else if the branch has been selected, put it in the list of selected branches.
93  if(desc.produced()) {
94  // First we check if an equivalent branch has already been selected due to an EDAlias.
95  // We only need the check for products produced in this process.
96  BranchID const& trueBranchID = desc.originalBranchID();
97  std::map<BranchID, BranchDescription const*>::const_iterator iter = trueBranchIDToKeptBranchDesc.find(trueBranchID);
98  if(iter != trueBranchIDToKeptBranchDesc.end()) {
99  throw edm::Exception(errors::Configuration, "Duplicate Output Selection")
100  << "Two (or more) equivalent branches have been selected for output.\n"
101  << "#1: " << BranchKey(desc) << "\n"
102  << "#2: " << BranchKey(*iter->second) << "\n"
103  << "Please drop at least one of them.\n";
104  }
105  trueBranchIDToKeptBranchDesc.insert(std::make_pair(trueBranchID, &desc));
106  }
107  switch (desc.branchType()) {
108  case InEvent:
109  {
111  InputTag{desc.moduleLabel(),
112  desc.productInstanceName(),
113  desc.processName()});
114  break;
115  }
116  case InLumi:
117  {
118  consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
119  InputTag(desc.moduleLabel(),
120  desc.productInstanceName(),
121  desc.processName()));
122  break;
123  }
124  case InRun:
125  {
126  consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
127  InputTag(desc.moduleLabel(),
128  desc.productInstanceName(),
129  desc.processName()));
130  break;
131  }
132  default:
133  assert(false);
134  break;
135  }
136  // Now put it in the list of selected branches.
137  keptProducts_[desc.branchType()].push_back(&desc);
138  } else {
139  // otherwise, output nothing,
140  // and mark the fact that there is a newly dropped branch of this type.
141  hasNewlyDroppedBranch_[desc.branchType()] = true;
142  }
143  }
144  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
145  for(auto const& it : preg.productList()) {
146  BranchDescription const& desc = it.second;
147  if(!desc.produced() || desc.isAlias()) continue;
148  BranchID const& branchID = desc.branchID();
149  std::map<BranchID, BranchDescription const*>::const_iterator iter = trueBranchIDToKeptBranchDesc.find(branchID);
150  if(iter != trueBranchIDToKeptBranchDesc.end()) {
151  // This branch, produced in this process, or an alias of it, was persisted.
152  BranchID const& keptBranchID = iter->second->branchID();
153  if(keptBranchID != branchID) {
154  // An EDAlias branch was persisted.
155  droppedBranchIDToKeptBranchID_.insert(std::make_pair(branchID.id(), keptBranchID.id()));
156  }
157  }
158  }
159  }
160 
162 
164  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
166 
167  this->beginJob();
168  }
169 
171  endJob();
172  }
173 
174 
176  return selectors_.getOneTriggerResults(ep, mcc); }
177 
178  namespace {
179  }
180 
181  bool
183  EventSetup const&,
184  ActivityRegistry* act,
185  ModuleCallingContext const* mcc) {
186 
187  FDEBUG(2) << "writeEvent called\n";
188 
189  {
190  std::lock_guard<std::mutex> guard(mutex_);
191  detail::TRBESSentry products_sentry(selectors_);
192  if(!wantAllEvents_) {
193  if(!selectors_.wantEvent(ep, mcc)) {
194  return true;
195  }
196  }
197 
198  {
199  std::lock_guard<SharedResourcesAcquirer> guardAcq(resourceAcquirer_);
200  EventSignalsSentry signals(act,mcc);
201  write(ep, mcc);
202  }
204  }
205  if(remainingEvents_ > 0) {
207  }
208  return true;
209  }
210 
211 // bool OutputModule::wantEvent(Event const& ev)
212 // {
213 // getTriggerResults(ev);
214 // bool eventAccepted = false;
215 
216 // typedef std::vector<NamedEventSelector>::const_iterator iter;
217 // for(iter i = selectResult_.begin(), e = selectResult_.end();
218 // !eventAccepted && i != e; ++i)
219 // {
220 // eventAccepted = i->acceptEvent(*prods_);
221 // }
222 
223 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
224 // return eventAccepted;
225 // }
226 
227  bool
229  EventSetup const&,
230  ModuleCallingContext const* mcc) {
231  FDEBUG(2) << "beginRun called\n";
232  beginRun(rp, mcc);
233  return true;
234  }
235 
236  bool
238  EventSetup const&,
239  ModuleCallingContext const* mcc) {
240  FDEBUG(2) << "endRun called\n";
241  endRun(rp, mcc);
242  return true;
243  }
244 
245  void
247  ModuleCallingContext const* mcc) {
248  FDEBUG(2) << "writeRun called\n";
249  writeRun(rp, mcc);
250  }
251 
252  bool
254  EventSetup const&,
255  ModuleCallingContext const* mcc) {
256  FDEBUG(2) << "beginLuminosityBlock called\n";
257  beginLuminosityBlock(lbp, mcc);
258  return true;
259  }
260 
261  bool
263  EventSetup const&,
264  ModuleCallingContext const* mcc) {
265  FDEBUG(2) << "endLuminosityBlock called\n";
266  endLuminosityBlock(lbp, mcc);
267  return true;
268  }
269 
271  ModuleCallingContext const* mcc) {
272  FDEBUG(2) << "writeLuminosityBlock called\n";
273  writeLuminosityBlock(lbp, mcc);
274  }
275 
277  openFile(fb);
278  }
279 
282  }
283 
286  }
287 
288  void
291  }
292 
293  void
294  OutputModule::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
295  postForkReacquireResources(iChildIndex, iNumberOfChildren);
296  }
297 
299  if(!isFileOpen()) reallyOpenFile();
300  }
301 
303  if(isFileOpen()) {
305  reallyCloseFile();
306  branchParents_.clear();
308  }
309  }
310 
312  }
313 
314  BranchIDLists const*
316  if(!droppedBranchIDToKeptBranchID_.empty()) {
317  // Make a private copy of the BranchIDLists.
319  // Check for branches dropped while an EDAlias was kept.
320  for(BranchIDList& branchIDList : *branchIDLists_) {
321  for(BranchID::value_type& branchID : branchIDList) {
322  // 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.
323  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
324  if(iter != droppedBranchIDToKeptBranchID_.end()) {
325  branchID = iter->second;
326  }
327  }
328  }
329  return branchIDLists_.get();
330  }
331  return origBranchIDLists_;
332  }
333 
334  ModuleDescription const&
336  return moduleDescription_;
337  }
338 
339  bool
341  return productSelector_.selected(desc);
342  }
343 
344  void
347  desc.setUnknown();
348  descriptions.addDefault(desc);
349  }
350 
351  void
353  ProductSelectorRules::fillDescription(desc, "outputCommands");
355  }
356 
357  void
359  }
360 
361 
362  static const std::string kBaseType("OutputModule");
363  const std::string&
365  return kBaseType;
366  }
367 
368  void
369  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
370  bool anyProductProduced) {
372  description().moduleLabel(),
373  outputModulePathPositions,
374  anyProductProduced);
375  }
376 
377  void
379  for(EventPrincipal::const_iterator i = ep.begin(), iEnd = ep.end(); i != iEnd; ++i) {
380  if((*i) && (*i)->productProvenancePtr() != 0) {
381  BranchID const& bid = (*i)->branchDescription().branchID();
382  BranchParents::iterator it = branchParents_.find(bid);
383  if(it == branchParents_.end()) {
384  it = branchParents_.insert(std::make_pair(bid, std::set<ParentageID>())).first;
385  }
386  it->second.insert((*i)->productProvenancePtr()->parentageID());
388  }
389  }
390  }
391 
392  void
394  for(BranchParents::const_iterator i = branchParents_.begin(), iEnd = branchParents_.end();
395  i != iEnd; ++i) {
396  BranchID const& child = i->first;
397  std::set<ParentageID> const& eIds = i->second;
398  for(std::set<ParentageID>::const_iterator it = eIds.begin(), itEnd = eIds.end();
399  it != itEnd; ++it) {
400  Parentage entryDesc;
401  ParentageRegistry::instance()->getMapped(*it, entryDesc);
402  std::vector<BranchID> const& parents = entryDesc.parents();
403  for(std::vector<BranchID>::const_iterator j = parents.begin(), jEnd = parents.end();
404  j != jEnd; ++j) {
405  branchChildren_.insertChild(*j, child);
406  }
407  }
408  }
409  }
410 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:146
void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
Definition: OutputModule.h:161
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
void selectProducts(ProductRegistry const &preg)
Definition: OutputModule.cc:74
virtual void write(EventPrincipal const &e, ModuleCallingContext const *)=0
ModuleDescription moduleDescription_
Definition: OutputModule.h:151
const_iterator end() const
Definition: Principal.h:160
ParameterSetID id() const
ParameterSet const & getParameterSet(ParameterSetID const &id)
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:170
#define nullptr
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
void insertEmpty(BranchID parent)
virtual void reallyOpenFile()
Definition: OutputModule.h:220
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:163
#define FDEBUG(lev)
Definition: DebugMacros.h:18
unsigned int id() const
Definition: BranchID.h:23
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:210
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)
std::string process_name_
Definition: OutputModule.h:148
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
std::mutex mutex_
Definition: OutputModule.h:171
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:209
static SharedResourcesRegistry * instance()
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ActivityRegistry *act, ModuleCallingContext const *mcc)
TypeID unwrappedTypeID() const
void doPreForkReleaseResources()
std::unique_ptr< BranchIDLists > branchIDLists_
Definition: OutputModule.h:162
int j
Definition: DBlmapReader.cc:9
std::vector< BranchDescription const * > allBranchDescriptions() const
BranchID const & branchID() const
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
static void fillDescription(ParameterSetDescription &desc)
detail::TriggerResultsBasedEventSelector selectors_
Definition: OutputModule.h:154
virtual bool isFileOpen() const
Definition: OutputModule.h:218
void doRespondToCloseInputFile(FileBlock const &fb)
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
void fillDependencyGraph()
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:216
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:159
static const std::string kLegacyModuleResourceName
All legacy modules share this resource.
handle_t getOneTriggerResults(EventPrincipal const &e, ModuleCallingContext const *)
virtual void beginRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:206
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:213
static void fillDescription(ParameterSetDescription &desc)
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *mcc)
bool wantEvent(EventPrincipal const &e, ModuleCallingContext const *)
static const std::string kBaseType("EDAnalyzer")
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:145
ProductSelector productSelector_
Definition: OutputModule.h:150
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
BranchChildren branchChildren_
Definition: OutputModule.h:168
boost::filter_iterator< FilledProductPtr, ProductHolderCollection::const_iterator > const_iterator
Definition: Principal.h:58
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
BranchIDLists const * branchIDLists() const
virtual void endJob()
Definition: OutputModule.h:205
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:149
virtual void beginJob()
Definition: OutputModule.h:204
BranchID const & originalBranchID() const
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:214
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:69
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:212
preg
Definition: Schedule.cc:369
static ParentageRegistry * instance()
std::atomic< int > remainingEvents_
Definition: OutputModule.h:128
virtual void writeRun(RunPrincipal const &, ModuleCallingContext const *)=0
virtual void endRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:207
bool configureEventSelector(edm::ParameterSet const &iPSet, std::string const &iProcessName, std::vector< std::string > const &iAllTriggerNames, edm::detail::TriggerResultsBasedEventSelector &oSelector)
ParameterSet const & registerIt()
Trig getTriggerResults(EventPrincipal const &ep, ModuleCallingContext const *) const
ModuleDescription const & description() const
BranchParents branchParents_
Definition: OutputModule.h:166
virtual void preForkReleaseResources()
Definition: OutputModule.h:215
OutputModule(ParameterSet const &pset)
Definition: OutputModule.cc:31
ParameterSetID selector_config_id_
Definition: OutputModule.h:157