CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // Original Author: Chris Jones
10 // Created: Wed, 31 Jul 2013 15:59:19 GMT
11 //
12 
13 // system include files
14 #include <cassert>
15 
16 // user include files
18 
33 
34 
35 namespace edm {
36  namespace one {
37 
38  // -------------------------------------------------------
40  maxEvents_(-1),
41  remainingEvents_(maxEvents_),
42  keptProducts_(),
43  hasNewlyDroppedBranch_(),
44  process_name_(),
45  productSelectorRules_(pset, "outputCommands", "OutputModule"),
46  productSelector_(),
47  moduleDescription_(),
48  wantAllEvents_(false),
49  selectors_(),
50  selector_config_id_(),
51  droppedBranchIDToKeptBranchID_(),
52  branchIDLists_(new BranchIDLists),
53  origBranchIDLists_(nullptr),
54  branchParents_(),
55  branchChildren_() {
56 
57  hasNewlyDroppedBranch_.fill(false);
58 
60  process_name_ = tns->getProcessName();
61 
62  ParameterSet selectevents =
63  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
64 
65  selectevents.registerIt(); // Just in case this PSet is not registered
66 
67  selector_config_id_ = selectevents.id();
71  selectors_);
72  }
73 
77  }
78 
80  if(productSelector_.initialized()) return;
82 
83  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
84  // single object. See the notes in the header for ProductSelector
85  // for more information.
86 
87  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
88 
89  for(auto const& it : preg.productList()) {
90  BranchDescription const& desc = it.second;
91  if(desc.transient()) {
92  // if the class of the branch is marked transient, output nothing
93  } else if(!desc.present() && !desc.produced()) {
94  // else if the branch containing the product has been previously dropped,
95  // output nothing
96  } else if(selected(desc)) {
97  // else if the branch has been selected, put it in the list of selected branches.
98  if(desc.produced()) {
99  // First we check if an equivalent branch has already been selected due to an EDAlias.
100  // We only need the check for products produced in this process.
101  BranchID const& trueBranchID = desc.originalBranchID();
102  std::map<BranchID, BranchDescription const*>::const_iterator iter = trueBranchIDToKeptBranchDesc.find(trueBranchID);
103  if(iter != trueBranchIDToKeptBranchDesc.end()) {
104  throw edm::Exception(errors::Configuration, "Duplicate Output Selection")
105  << "Two (or more) equivalent branches have been selected for output.\n"
106  << "#1: " << BranchKey(desc) << "\n"
107  << "#2: " << BranchKey(*iter->second) << "\n"
108  << "Please drop at least one of them.\n";
109  }
110  trueBranchIDToKeptBranchDesc.insert(std::make_pair(trueBranchID, &desc));
111  }
112  switch (desc.branchType()) {
113  case InEvent:
114  {
116  InputTag{desc.moduleLabel(),
117  desc.productInstanceName(),
118  desc.processName()});
119  break;
120  }
121  case InLumi:
122  {
123  consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
124  InputTag(desc.moduleLabel(),
125  desc.productInstanceName(),
126  desc.processName()));
127  break;
128  }
129  case InRun:
130  {
131  consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
132  InputTag(desc.moduleLabel(),
133  desc.productInstanceName(),
134  desc.processName()));
135  break;
136  }
137  default:
138  assert(false);
139  break;
140  }
141  // Now put it in the list of selected branches.
142  keptProducts_[desc.branchType()].push_back(&desc);
143  } else {
144  // otherwise, output nothing,
145  // and mark the fact that there is a newly dropped branch of this type.
146  hasNewlyDroppedBranch_[desc.branchType()] = true;
147  }
148  }
149  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
150  for(auto const& it : preg.productList()) {
151  BranchDescription const& desc = it.second;
152  if(!desc.produced() || desc.isAlias()) continue;
153  BranchID const& branchID = desc.branchID();
154  std::map<BranchID, BranchDescription const*>::const_iterator iter = trueBranchIDToKeptBranchDesc.find(branchID);
155  if(iter != trueBranchIDToKeptBranchDesc.end()) {
156  // This branch, produced in this process, or an alias of it, was persisted.
157  BranchID const& keptBranchID = iter->second->branchID();
158  if(keptBranchID != branchID) {
159  // An EDAlias branch was persisted.
160  droppedBranchIDToKeptBranchID_.insert(std::make_pair(branchID.id(), keptBranchID.id()));
161  }
162  }
163  }
164  }
165 
167 
169  this->beginJob();
170  }
171 
173  endJob();
174  }
175 
176 
178  ModuleCallingContext const* mcc) const {
179  return selectors_.getOneTriggerResults(ep, mcc); }
180 
181  bool
183  EventSetup const&,
184  ModuleCallingContext const* mcc) {
185  detail::TRBESSentry products_sentry(selectors_);
186 
187  if(!wantAllEvents_) {
188  if(!selectors_.wantEvent(ep, mcc)) {
189  return true;
190  }
191  }
192  write(ep, mcc);
194  if(remainingEvents_ > 0) {
196  }
197  return true;
198  }
199 
200  bool
202  EventSetup const&,
203  ModuleCallingContext const* mcc) {
204  doBeginRun_(rp, mcc);
205  return true;
206  }
207 
208  bool
210  EventSetup const&,
211  ModuleCallingContext const* mcc) {
212  doEndRun_(rp, mcc);
213  return true;
214  }
215 
216  void
218  ModuleCallingContext const* mcc) {
219  writeRun(rp, mcc);
220  }
221 
222  bool
224  EventSetup const&,
225  ModuleCallingContext const* mcc) {
226  doBeginLuminosityBlock_(lbp, mcc);
227  return true;
228  }
229 
230  bool
232  EventSetup const&,
233  ModuleCallingContext const* mcc) {
234  doEndLuminosityBlock_(lbp, mcc);
235  return true;
236  }
237 
239  ModuleCallingContext const* mcc) {
240  writeLuminosityBlock(lbp, mcc);
241  }
242 
244  openFile(fb);
245  }
246 
249  }
250 
253  }
254 
255  void
258  }
259 
260  void
261  OutputModuleBase::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
262  postForkReacquireResources(iChildIndex, iNumberOfChildren);
263  }
264 
265  void
267 
268  void
269  OutputModuleBase::postForkReacquireResources(unsigned int /*iChildIndex*/, unsigned int /*iNumberOfChildren*/) {}
270 
271 
273  if(!isFileOpen()) reallyOpenFile();
274  }
275 
277  if(isFileOpen()) {
279  reallyCloseFile();
280  branchParents_.clear();
282  }
283  }
284 
286  }
287 
288  BranchIDLists const*
290  if(!droppedBranchIDToKeptBranchID_.empty()) {
291  // Make a private copy of the BranchIDLists.
293  // Check for branches dropped while an EDAlias was kept.
294  for(BranchIDList& branchIDList : *branchIDLists_) {
295  for(BranchID::value_type& branchID : branchIDList) {
296  // 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.
297  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
298  if(iter != droppedBranchIDToKeptBranchID_.end()) {
299  branchID = iter->second;
300  }
301  }
302  }
303  return branchIDLists_.get();
304  }
305  return origBranchIDLists_;
306  }
307 
308  ModuleDescription const&
310  return moduleDescription_;
311  }
312 
313  bool
315  return productSelector_.selected(desc);
316  }
317 
318  void
321  desc.setUnknown();
322  descriptions.addDefault(desc);
323  }
324 
325  void
327  ProductSelectorRules::fillDescription(desc, "outputCommands");
329  }
330 
331  void
333  }
334 
335 
336  static const std::string kBaseType("OutputModule");
337  const std::string&
339  return kBaseType;
340  }
341 
342  void
343  OutputModuleBase::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
344  bool anyProductProduced) {
346  description().moduleLabel(),
347  outputModulePathPositions,
348  anyProductProduced);
349  }
350 
351  void
353  for(EventPrincipal::const_iterator i = ep.begin(), iEnd = ep.end(); i != iEnd; ++i) {
354  if((*i) && (*i)->productProvenancePtr() != 0) {
355  BranchID const& bid = (*i)->branchDescription().branchID();
356  BranchParents::iterator it = branchParents_.find(bid);
357  if(it == branchParents_.end()) {
358  it = branchParents_.insert(std::make_pair(bid, std::set<ParentageID>())).first;
359  }
360  it->second.insert((*i)->productProvenancePtr()->parentageID());
362  }
363  }
364  }
365 
366  void
368  for(BranchParents::const_iterator i = branchParents_.begin(), iEnd = branchParents_.end();
369  i != iEnd; ++i) {
370  BranchID const& child = i->first;
371  std::set<ParentageID> const& eIds = i->second;
372  for(std::set<ParentageID>::const_iterator it = eIds.begin(), itEnd = eIds.end();
373  it != itEnd; ++it) {
374  Parentage entryDesc;
375  ParentageRegistry::instance()->getMapped(*it, entryDesc);
376  std::vector<BranchID> const& parents = entryDesc.parents();
377  for(std::vector<BranchID>::const_iterator j = parents.begin(), jEnd = parents.end();
378  j != jEnd; ++j) {
379  branchChildren_.insertChild(*j, child);
380  }
381  }
382  }
383  }
384 
385 
386  }
387 }
BranchIDLists const * branchIDLists() const
static const std::string kBaseType("EDAnalyzer")
std::unique_ptr< BranchIDLists > branchIDLists_
int i
Definition: DBlmapReader.cc:9
bool selected(BranchDescription const &desc) const
static void fillDescription(ParameterSetDescription &desc, char const *parameterName)
ModuleDescription const & description() const
TPRegexp parents
Definition: eve_filter.cc:24
BranchType const & branchType() const
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
SelectedProductsForBranchType keptProducts_
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp, ModuleCallingContext const *)
ModuleDescription moduleDescription_
void doWriteRun(RunPrincipal const &rp, ModuleCallingContext const *)
static void prevalidate(ConfigurationDescriptions &)
const_iterator end() const
Definition: Principal.h:160
ParameterSetID id() const
void configure(OutputModuleDescription const &desc)
ParameterSet const & getParameterSet(ParameterSetID const &id)
#define nullptr
virtual void doEndRun_(RunPrincipal const &, ModuleCallingContext const *)
void insertEmpty(BranchID parent)
void insertChild(BranchID parent, BranchID child)
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
std::string const & processName() const
ParameterSet getUntrackedParameterSet(std::string const &name, ParameterSet const &defaultValue) const
void selectProducts(ProductRegistry const &preg)
ProductSelectorRules productSelectorRules_
unsigned int id() const
Definition: BranchID.h:23
virtual void doBeginRun_(RunPrincipal const &, ModuleCallingContext const *)
BranchIDLists const * branchIDLists_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Trig getTriggerResults(EventPrincipal const &ep, ModuleCallingContext const *) const
std::vector< BranchID > const & parents() const
Definition: Parentage.h:37
virtual void doRespondToCloseInputFile_(FileBlock const &)
ProductList const & productList() const
virtual void doRespondToOpenInputFile_(FileBlock const &)
bool doEvent(EventPrincipal const &ep, EventSetup const &c, ModuleCallingContext const *)
virtual void openFile(FileBlock const &)
void addDefault(ParameterSetDescription const &psetDescription)
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
bool selected(BranchDescription const &desc) const
TypeID unwrappedTypeID() const
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
int j
Definition: DBlmapReader.cc:9
std::vector< BranchDescription const * > allBranchDescriptions() const
virtual void writeRun(RunPrincipal const &, ModuleCallingContext const *)=0
BranchID const & branchID() const
virtual void preForkReleaseResources()
static void fillDescription(ParameterSetDescription &desc)
virtual void doBeginLuminosityBlock_(LuminosityBlockPrincipal const &, ModuleCallingContext const *)
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
OutputModuleBase(ParameterSet const &pset)
virtual void doEndLuminosityBlock_(LuminosityBlockPrincipal const &, ModuleCallingContext const *)
void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
void doRespondToCloseInputFile(FileBlock const &fb)
const_iterator begin() const
Definition: Principal.h:159
handle_t getOneTriggerResults(EventPrincipal const &e, ModuleCallingContext const *)
void doRespondToOpenInputFile(FileBlock const &fb)
bool wantEvent(EventPrincipal const &e, ModuleCallingContext const *)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
virtual void write(EventPrincipal const &e, ModuleCallingContext const *)=0
std::vector< BranchID::value_type > BranchIDList
Definition: BranchIDList.h:18
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, ModuleCallingContext const *)
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
boost::filter_iterator< FilledProductPtr, ProductHolderCollection::const_iterator > const_iterator
Definition: Principal.h:58
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
static const std::string & baseType()
void doOpenFile(FileBlock const &fb)
virtual void writeLuminosityBlock(LuminosityBlockPrincipal const &, ModuleCallingContext const *)=0
virtual void postForkReacquireResources(unsigned int, unsigned int)
BranchIDLists const * origBranchIDLists_
BranchID const & originalBranchID() const
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)
volatile std::atomic< bool > shutdown_flag false
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 doCloseFile()
Tell the OutputModule that is must end the current file.
static void fillDescription(ParameterSetDescription &desc)
virtual bool isFileOpen() const
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
preg
Definition: Schedule.cc:362
void updateBranchParents(EventPrincipal const &ep)
static ParentageRegistry * instance()
ProductSelector productSelector_
bool configureEventSelector(edm::ParameterSet const &iPSet, std::string const &iProcessName, std::vector< std::string > const &iAllTriggerNames, edm::detail::TriggerResultsBasedEventSelector &oSelector)
ParameterSet const & registerIt()
detail::TriggerResultsBasedEventSelector selectors_
ParameterSetID selector_config_id_
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *)