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