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 
24 
26 
27 #include <cassert>
28 #include <iostream>
29 
30 namespace edm {
31 
32  // -------------------------------------------------------
34  maxEvents_(-1),
35  remainingEvents_(maxEvents_),
36  keptProducts_(),
37  hasNewlyDroppedBranch_(),
38  process_name_(),
39  productSelectorRules_(pset, "outputCommands", "OutputModule"),
40  productSelector_(),
41  moduleDescription_(),
42  wantAllEvents_(false),
43  selectors_(),
44  selector_config_id_(),
45  droppedBranchIDToKeptBranchID_(),
46  branchIDLists_(new BranchIDLists),
47  origBranchIDLists_(nullptr),
48  thinnedAssociationsHelper_(new ThinnedAssociationsHelper),
49  branchParents_(),
50  branchChildren_() {
51 
52  hasNewlyDroppedBranch_.fill(false);
53 
55  process_name_ = tns->getProcessName();
56 
57  ParameterSet selectevents =
58  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
59 
60  selectevents.registerIt(); // Just in case this PSet is not registered
61 
62  selector_config_id_ = selectevents.id();
66  selectors_);
69 
70  }
71 
75  }
76 
78  ThinnedAssociationsHelper const& thinnedAssociationsHelper) {
79  if(productSelector_.initialized()) return;
81 
82  // TODO: See if we can collapse keptProducts_ and productSelector_ into a
83  // single object. See the notes in the header for ProductSelector
84  // for more information.
85 
86  std::map<BranchID, BranchDescription const*> trueBranchIDToKeptBranchDesc;
87  std::vector<BranchDescription const*> associationDescriptions;
88  std::set<BranchID> keptProductsInEvent;
89 
90  for(auto const& it : preg.productList()) {
91  BranchDescription const& desc = it.second;
92  if(desc.transient()) {
93  // if the class of the branch is marked transient, output nothing
94  } else if(!desc.present() && !desc.produced()) {
95  // else if the branch containing the product has been previously dropped,
96  // output nothing
97  } else if(desc.unwrappedType().typeInfo() == typeid(ThinnedAssociation)) {
98  associationDescriptions.push_back(&desc);
99  } else if(selected(desc)) {
100  keepThisBranch(desc, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
101  } else {
102  // otherwise, output nothing,
103  // and mark the fact that there is a newly dropped branch of this type.
104  hasNewlyDroppedBranch_[desc.branchType()] = true;
105  }
106  }
107 
108  thinnedAssociationsHelper.selectAssociationProducts(associationDescriptions,
109  keptProductsInEvent,
111 
112  for(auto association : associationDescriptions) {
113  if(keepAssociation_[association->branchID()]) {
114  keepThisBranch(*association, trueBranchIDToKeptBranchDesc, keptProductsInEvent);
115  } else {
116  hasNewlyDroppedBranch_[association->branchType()] = true;
117  }
118  }
119 
120  // Now fill in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
121  ProductSelector::fillDroppedToKept(preg, trueBranchIDToKeptBranchDesc, droppedBranchIDToKeptBranchID_);
122 
123  thinnedAssociationsHelper_->updateFromParentProcess(thinnedAssociationsHelper, keepAssociation_, droppedBranchIDToKeptBranchID_);
124  }
125 
127  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
128  std::set<BranchID>& keptProductsInEvent) {
129 
131  trueBranchIDToKeptBranchDesc);
132 
133  switch (desc.branchType()) {
134  case InEvent:
135  {
136  if(desc.produced()) {
137  keptProductsInEvent.insert(desc.originalBranchID());
138  } else {
139  keptProductsInEvent.insert(desc.branchID());
140  }
142  InputTag{desc.moduleLabel(),
143  desc.productInstanceName(),
144  desc.processName()});
145  break;
146  }
147  case InLumi:
148  {
149  consumes<InLumi>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
150  InputTag(desc.moduleLabel(),
151  desc.productInstanceName(),
152  desc.processName()));
153  break;
154  }
155  case InRun:
156  {
157  consumes<InRun>(TypeToGet{desc.unwrappedTypeID(),PRODUCT_TYPE},
158  InputTag(desc.moduleLabel(),
159  desc.productInstanceName(),
160  desc.processName()));
161  break;
162  }
163  default:
164  assert(false);
165  break;
166  }
167  // Now put it in the list of selected branches.
168  keptProducts_[desc.branchType()].push_back(&desc);
169  }
170 
172 
174  std::vector<std::string> res = {SharedResourcesRegistry::kLegacyModuleResourceName};
176 
177  this->beginJob();
178  }
179 
181  endJob();
182  }
183 
184 
186  return selectors_.getOneTriggerResults(ep, mcc); }
187 
188  namespace {
189  }
190 
191  bool
193  EventSetup const&,
194  ActivityRegistry* act,
195  ModuleCallingContext const* mcc) {
196 
197  FDEBUG(2) << "writeEvent called\n";
198 
199  {
200  std::lock_guard<std::mutex> guard(mutex_);
201  detail::TRBESSentry products_sentry(selectors_);
202  if(!wantAllEvents_) {
203  if(!selectors_.wantEvent(ep, mcc)) {
204  return true;
205  }
206  }
207 
208  {
209  std::lock_guard<SharedResourcesAcquirer> guardAcq(resourceAcquirer_);
210  EventSignalsSentry signals(act,mcc);
211  write(ep, mcc);
212  }
214  }
215  if(remainingEvents_ > 0) {
217  }
218  return true;
219  }
220 
221 // bool OutputModule::wantEvent(Event const& ev)
222 // {
223 // getTriggerResults(ev);
224 // bool eventAccepted = false;
225 
226 // typedef std::vector<NamedEventSelector>::const_iterator iter;
227 // for(iter i = selectResult_.begin(), e = selectResult_.end();
228 // !eventAccepted && i != e; ++i)
229 // {
230 // eventAccepted = i->acceptEvent(*prods_);
231 // }
232 
233 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
234 // return eventAccepted;
235 // }
236 
237  bool
239  EventSetup const&,
240  ModuleCallingContext const* mcc) {
241  FDEBUG(2) << "beginRun called\n";
242  beginRun(rp, mcc);
243  return true;
244  }
245 
246  bool
248  EventSetup const&,
249  ModuleCallingContext const* mcc) {
250  FDEBUG(2) << "endRun called\n";
251  endRun(rp, mcc);
252  return true;
253  }
254 
255  void
257  ModuleCallingContext const* mcc) {
258  FDEBUG(2) << "writeRun called\n";
259  writeRun(rp, mcc);
260  }
261 
262  bool
264  EventSetup const&,
265  ModuleCallingContext const* mcc) {
266  FDEBUG(2) << "beginLuminosityBlock called\n";
267  beginLuminosityBlock(lbp, mcc);
268  return true;
269  }
270 
271  bool
273  EventSetup const&,
274  ModuleCallingContext const* mcc) {
275  FDEBUG(2) << "endLuminosityBlock called\n";
276  endLuminosityBlock(lbp, mcc);
277  return true;
278  }
279 
281  ModuleCallingContext const* mcc) {
282  FDEBUG(2) << "writeLuminosityBlock called\n";
283  writeLuminosityBlock(lbp, mcc);
284  }
285 
287  openFile(fb);
288  }
289 
292  }
293 
296  }
297 
298  void
301  }
302 
303  void
304  OutputModule::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
305  postForkReacquireResources(iChildIndex, iNumberOfChildren);
306  }
307 
309  if(!isFileOpen()) reallyOpenFile();
310  }
311 
313  if(isFileOpen()) {
315  reallyCloseFile();
316  branchParents_.clear();
318  }
319  }
320 
322  }
323 
324  BranchIDLists const*
326  if(!droppedBranchIDToKeptBranchID_.empty()) {
327  // Make a private copy of the BranchIDLists.
329  // Check for branches dropped while an EDAlias was kept.
330  for(BranchIDList& branchIDList : *branchIDLists_) {
331  for(BranchID::value_type& branchID : branchIDList) {
332  // 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.
333  std::map<BranchID::value_type, BranchID::value_type>::const_iterator iter = droppedBranchIDToKeptBranchID_.find(branchID);
334  if(iter != droppedBranchIDToKeptBranchID_.end()) {
335  branchID = iter->second;
336  }
337  }
338  }
339  return branchIDLists_.get();
340  }
341  return origBranchIDLists_;
342  }
343 
346  return thinnedAssociationsHelper_.get();
347  }
348 
349  ModuleDescription const&
351  return moduleDescription_;
352  }
353 
354  bool
356  return productSelector_.selected(desc);
357  }
358 
359  void
362  desc.setUnknown();
363  descriptions.addDefault(desc);
364  }
365 
366  void
368  ProductSelectorRules::fillDescription(desc, "outputCommands");
370  }
371 
372  void
374  }
375 
376 
377  static const std::string kBaseType("OutputModule");
378  const std::string&
380  return kBaseType;
381  }
382 
383  void
384  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
385  bool anyProductProduced) {
387  description().moduleLabel(),
388  outputModulePathPositions,
389  anyProductProduced);
390  }
391 
392  void
394  for(EventPrincipal::const_iterator i = ep.begin(), iEnd = ep.end(); i != iEnd; ++i) {
395  if((*i) && (*i)->productProvenancePtr() != 0) {
396  BranchID const& bid = (*i)->branchDescription().branchID();
397  BranchParents::iterator it = branchParents_.find(bid);
398  if(it == branchParents_.end()) {
399  it = branchParents_.insert(std::make_pair(bid, std::set<ParentageID>())).first;
400  }
401  it->second.insert((*i)->productProvenancePtr()->parentageID());
403  }
404  }
405  }
406 
407  void
409  for(BranchParents::const_iterator i = branchParents_.begin(), iEnd = branchParents_.end();
410  i != iEnd; ++i) {
411  BranchID const& child = i->first;
412  std::set<ParentageID> const& eIds = i->second;
413  for(std::set<ParentageID>::const_iterator it = eIds.begin(), itEnd = eIds.end();
414  it != itEnd; ++it) {
415  Parentage entryDesc;
416  ParentageRegistry::instance()->getMapped(*it, entryDesc);
417  std::vector<BranchID> const& parents = entryDesc.parents();
418  for(std::vector<BranchID>::const_iterator j = parents.begin(), jEnd = parents.end();
419  j != jEnd; ++j) {
420  branchChildren_.insertChild(*j, child);
421  }
422  }
423  }
424  }
425 }
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:151
void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
void keepThisBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc, std::set< BranchID > &keptProductsInEvent)
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
Definition: OutputModule.h:166
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
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:156
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:171
const_iterator end() const
Definition: Principal.h:158
ParameterSetID id() const
ParameterSet const & getParameterSet(ParameterSetID const &id)
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:178
virtual void reallyCloseFile()
SharedResourcesAcquirer createAcquirer(std::vector< std::string > const &) const
void insertEmpty(BranchID parent)
virtual void reallyOpenFile()
Definition: OutputModule.h:230
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: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)
virtual void endLuminosityBlock(LuminosityBlockPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:220
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:153
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
std::mutex mutex_
Definition: OutputModule.h:179
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:219
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:167
int j
Definition: DBlmapReader.cc:9
std::vector< BranchDescription const * > allBranchDescriptions() const
BranchID const & branchID() const
TypeWithDict const & unwrappedType() const
std::type_info const & typeInfo() const
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, ModuleCallingContext const *mcc)
static void fillDescription(ParameterSetDescription &desc)
detail::TriggerResultsBasedEventSelector selectors_
Definition: OutputModule.h:159
virtual bool isFileOpen() const
Definition: OutputModule.h:228
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:170
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:226
void selectProducts(ProductRegistry const &preg, ThinnedAssociationsHelper const &)
Definition: OutputModule.cc:77
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.
handle_t getOneTriggerResults(EventPrincipal const &e, ModuleCallingContext const *)
virtual void beginRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:216
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:223
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:150
ProductSelector productSelector_
Definition: OutputModule.h:155
bool initialized() const
std::vector< std::string > const & getAllTriggerNames()
BranchChildren branchChildren_
Definition: OutputModule.h:176
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:215
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:154
virtual void beginJob()
Definition: OutputModule.h:214
BranchID const & originalBranchID() const
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:224
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:72
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:222
preg
Definition: Schedule.cc:370
static ParentageRegistry * instance()
std::atomic< int > remainingEvents_
Definition: OutputModule.h:133
virtual void writeRun(RunPrincipal const &, ModuleCallingContext const *)=0
virtual void endRun(RunPrincipal const &, ModuleCallingContext const *)
Definition: OutputModule.h:217
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:174
virtual void preForkReleaseResources()
Definition: OutputModule.h:225
OutputModule(ParameterSet const &pset)
Definition: OutputModule.cc:33
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)
ParameterSetID selector_config_id_
Definition: OutputModule.h:162