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 
23 #include <cassert>
24 
25 namespace edm {
26  // This grotesque little function exists just to allow calling of
27  // ConstProductRegistry::allBranchDescriptions in the context of
28  // OutputModule's initialization list, rather than in the body of
29  // the constructor.
30 
31  std::vector<BranchDescription const*>
34  return reg->allBranchDescriptions();
35  }
36 
37  std::vector<std::string> const& getAllTriggerNames() {
39  return tns->getTrigPaths();
40  }
41 }
42 
43 namespace {
44  //--------------------------------------------------------
45  // Remove whitespace (spaces and tabs) from a std::string.
46  void remove_whitespace(std::string& s) {
47  s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
48  s.erase(std::remove(s.begin(), s.end(), '\t'), s.end());
49  }
50 
51  void test_remove_whitespace() {
52  std::string a("noblanks");
53  std::string b("\t no blanks \t");
54 
55  remove_whitespace(b);
56  assert(a == b);
57  }
58 
59  //--------------------------------------------------------
60  // Given a path-spec (std::string of the form "a:b", where the ":b" is
61  // optional), return a parsed_path_spec_t containing "a" and "b".
62 
63  typedef std::pair<std::string, std::string> parsed_path_spec_t;
64  void parse_path_spec(std::string const& path_spec,
65  parsed_path_spec_t& output) {
66  std::string trimmed_path_spec(path_spec);
67  remove_whitespace(trimmed_path_spec);
68 
69  std::string::size_type colon = trimmed_path_spec.find(":");
70  if(colon == std::string::npos) {
71  output.first = trimmed_path_spec;
72  } else {
73  output.first = trimmed_path_spec.substr(0, colon);
74  output.second = trimmed_path_spec.substr(colon + 1,
75  trimmed_path_spec.size());
76  }
77  }
78 
79  void test_parse_path_spec() {
80  std::vector<std::string> paths;
81  paths.push_back("a:p1");
82  paths.push_back("b:p2");
83  paths.push_back(" c");
84  paths.push_back("ddd\t:p3");
85  paths.push_back("eee: p4 ");
86 
87  std::vector<parsed_path_spec_t> parsed(paths.size());
88  for(size_t i = 0; i < paths.size(); ++i) {
89  parse_path_spec(paths[i], parsed[i]);
90  }
91 
92  assert(parsed[0].first == "a");
93  assert(parsed[0].second == "p1");
94  assert(parsed[1].first == "b");
95  assert(parsed[1].second == "p2");
96  assert(parsed[2].first == "c");
97  assert(parsed[2].second == "");
98  assert(parsed[3].first == "ddd");
99  assert(parsed[3].second == "p3");
100  assert(parsed[4].first == "eee");
101  assert(parsed[4].second == "p4");
102  }
103 }
104 
105 namespace edm {
106  namespace test {
108  test_remove_whitespace();
109  test_parse_path_spec();
110  }
111  }
112 
113  // -------------------------------------------------------
115  maxEvents_(-1),
116  remainingEvents_(maxEvents_),
117  keptProducts_(),
118  hasNewlyDroppedBranch_(),
119  process_name_(),
120  groupSelectorRules_(pset, "outputCommands", "OutputModule"),
121  groupSelector_(),
122  moduleDescription_(),
123  current_context_(0),
124  prodsValid_(false),
125  wantAllEvents_(false),
126  selectors_(),
127  selector_config_id_(),
128  branchParents_(),
129  branchChildren_() {
130 
131  hasNewlyDroppedBranch_.assign(false);
132 
134  process_name_ = tns->getProcessName();
135 
136  ParameterSet selectevents =
137  pset.getUntrackedParameterSet("SelectEvents", ParameterSet());
138 
139  selectevents.registerIt(); // Just in case this PSet is not registered
140 
141  selector_config_id_ = selectevents.id();
142  // If selectevents is an emtpy ParameterSet, then we are to write
143  // all events, or one which contains a vstrig 'SelectEvents' that
144  // is empty, we are to write all events. We have no need for any
145  // EventSelectors.
146  if(selectevents.empty()) {
147  wantAllEvents_ = true;
149  return;
150  }
151 
152  std::vector<std::string> path_specs =
153  selectevents.getParameter<std::vector<std::string> >("SelectEvents");
154 
155  if(path_specs.empty()) {
156  wantAllEvents_ = true;
158  return;
159  }
160 
161  // If we get here, we have the possibility of having to deal with
162  // path_specs that look at more than one process.
163  std::vector<parsed_path_spec_t> parsed_paths(path_specs.size());
164  for(size_t i = 0; i < path_specs.size(); ++i) {
165  parse_path_spec(path_specs[i], parsed_paths[i]);
166  }
168  }
169 
172  }
173 
175  if(groupSelector_.initialized()) return;
178 
179  // TODO: See if we can collapse keptProducts_ and groupSelector_ into a
180  // single object. See the notes in the header for GroupSelector
181  // for more information.
182 
183  ProductRegistry::ProductList::const_iterator it =
184  reg->productList().begin();
185  ProductRegistry::ProductList::const_iterator end =
186  reg->productList().end();
187 
188  for(; it != end; ++it) {
189  BranchDescription const& desc = it->second;
190  if(desc.transient()) {
191  // if the class of the branch is marked transient, output nothing
192  } else if(!desc.present() && !desc.produced()) {
193  // else if the branch containing the product has been previously dropped,
194  // output nothing
195  } else if(selected(desc)) {
196  // else if the branch has been selected, put it in the list of selected branches
197  keptProducts_[desc.branchType()].push_back(&desc);
198  } else {
199  // otherwise, output nothing,
200  // and mark the fact that there is a newly dropped branch of this type.
201  hasNewlyDroppedBranch_[desc.branchType()] = true;
202  }
203  }
204  }
205 
207 
209  selectProducts();
210  this->beginJob();
211  }
212 
214  endJob();
215  }
216 
217 
219  return selectors_.getOneTriggerResults(ev);
220  }
221 
223  // This is bad, because we're returning handles into an Event that
224  // is destructed before the return. It might not fail, because the
225  // actual EventPrincipal is not destroyed, but it still needs to
226  // be cleaned up.
227  Event ev(const_cast<EventPrincipal&>(ep),
229  return getTriggerResults(ev);
230  }
231 
232  namespace {
233  class PVSentry {
234  public:
235  PVSentry(detail::CachedProducts& prods, bool& valid) : p(prods), v(valid) {}
236  ~PVSentry() {
237  p.clear();
238  v = false;
239  }
240  private:
241  detail::CachedProducts& p;
242  bool& v;
243 
244  PVSentry(PVSentry const&); // not implemented
245  PVSentry& operator=(PVSentry const&); // not implemented
246  };
247  }
248 
249  bool
251  EventSetup const&,
252  CurrentProcessingContext const* cpc) {
253  detail::CPCSentry sentry(current_context_, cpc);
254  PVSentry products_sentry(selectors_, prodsValid_);
255 
256  FDEBUG(2) << "writeEvent called\n";
257 
258  if(!wantAllEvents_) {
259  // use module description and const_cast unless interface to
260  // event is changed to just take a const EventPrincipal
261  Event e(const_cast<EventPrincipal&>(ep), moduleDescription_);
262  if(!selectors_.wantEvent(e)) {
263  return true;
264  }
265  }
266  write(ep);
268  if(remainingEvents_ > 0) {
270  }
271  return true;
272  }
273 
274 // bool OutputModule::wantEvent(Event const& ev)
275 // {
276 // getTriggerResults(ev);
277 // bool eventAccepted = false;
278 
279 // typedef std::vector<NamedEventSelector>::const_iterator iter;
280 // for(iter i = selectResult_.begin(), e = selectResult_.end();
281 // !eventAccepted && i != e; ++i)
282 // {
283 // eventAccepted = i->acceptEvent(*prods_);
284 // }
285 
286 // FDEBUG(2) << "Accept event " << ep.id() << " " << eventAccepted << "\n";
287 // return eventAccepted;
288 // }
289 
290  bool
292  EventSetup const&,
293  CurrentProcessingContext const* cpc) {
294  detail::CPCSentry sentry(current_context_, cpc);
295  FDEBUG(2) << "beginRun called\n";
296  beginRun(rp);
297  return true;
298  }
299 
300  bool
302  EventSetup const&,
303  CurrentProcessingContext const* cpc) {
304  detail::CPCSentry sentry(current_context_, cpc);
305  FDEBUG(2) << "endRun called\n";
306  endRun(rp);
307  return true;
308  }
309 
310  void
312  FDEBUG(2) << "writeRun called\n";
313  writeRun(rp);
314  }
315 
316  bool
318  EventSetup const&,
319  CurrentProcessingContext const* cpc) {
320  detail::CPCSentry sentry(current_context_, cpc);
321  FDEBUG(2) << "beginLuminosityBlock called\n";
323  return true;
324  }
325 
326  bool
328  EventSetup const&,
329  CurrentProcessingContext const* cpc) {
330  detail::CPCSentry sentry(current_context_, cpc);
331  FDEBUG(2) << "endLuminosityBlock called\n";
332  endLuminosityBlock(lbp);
333  return true;
334  }
335 
337  FDEBUG(2) << "writeLuminosityBlock called\n";
339  }
340 
342  openFile(fb);
343  }
344 
347  }
348 
351  }
352 
355  }
356 
359  }
360 
361  void
364  }
365 
366  void
367  OutputModule::doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren) {
368  postForkReacquireResources(iChildIndex, iNumberOfChildren);
369  }
370 
372  if(!isFileOpen()) doOpenFile();
373  }
374 
376  if(isFileOpen()) reallyCloseFile();
377  }
378 
381  startEndFile();
393  finishEndFile();
394  branchParents_.clear();
396  }
397 
400  return current_context_;
401  }
402 
403  ModuleDescription const&
405  return moduleDescription_;
406  }
407 
408  bool
410  return groupSelector_.selected(desc);
411  }
412 
413  void
416  desc.setUnknown();
417  descriptions.addDefault(desc);
418  }
419 
420  void
422  GroupSelectorRules::fillDescription(desc, "outputCommands");
424  }
425 
426  void
428  }
429 
430 
431  static const std::string kBaseType("OutputModule");
432  const std::string&
434  return kBaseType;
435  }
436 
437  void
438  OutputModule::setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
439  bool anyProductProduced) {
440 
441  ParameterSet selectEventsInfo;
443  selectEventsInfo.addParameter<bool>("InProcessHistory", anyProductProduced);
444  std::string const& label = description().moduleLabel();
445  std::vector<std::string> endPaths;
446  std::vector<int> endPathPositions;
447 
448  // The label will be empty if and only if this is a SubProcess
449  // SubProcess's do not appear on any end path
450  if (!label.empty()) {
451  std::map<std::string, std::vector<std::pair<std::string, int> > >::const_iterator iter = outputModulePathPositions.find(label);
452  assert(iter != outputModulePathPositions.end());
453  for (std::vector<std::pair<std::string, int> >::const_iterator i = iter->second.begin(), e = iter->second.end();
454  i != e; ++i) {
455  endPaths.push_back(i->first);
456  endPathPositions.push_back(i->second);
457  }
458  }
459  selectEventsInfo.addParameter<std::vector<std::string> >("EndPaths", endPaths);
460  selectEventsInfo.addParameter<std::vector<int> >("EndPathPositions", endPathPositions);
461  if (!selectEventsInfo.exists("SelectEvents")) {
462  selectEventsInfo.addParameter<std::vector<std::string> >("SelectEvents", std::vector<std::string>());
463  }
464  selectEventsInfo.registerIt();
465 
466  selector_config_id_ = selectEventsInfo.id();
467  }
468 
469  void
471  for(EventPrincipal::const_iterator i = ep.begin(), iEnd = ep.end(); i != iEnd; ++i) {
472  if((*i) && (*i)->productProvenancePtr() != 0) {
473  BranchID const& bid = (*i)->branchDescription().branchID();
474  BranchParents::iterator it = branchParents_.find(bid);
475  if(it == branchParents_.end()) {
476  it = branchParents_.insert(std::make_pair(bid, std::set<ParentageID>())).first;
477  }
478  it->second.insert((*i)->productProvenancePtr()->parentageID());
480  }
481  }
482  }
483 
484  void
486  for(BranchParents::const_iterator i = branchParents_.begin(), iEnd = branchParents_.end();
487  i != iEnd; ++i) {
488  BranchID const& child = i->first;
489  std::set<ParentageID> const& eIds = i->second;
490  for(std::set<ParentageID>::const_iterator it = eIds.begin(), itEnd = eIds.end();
491  it != itEnd; ++it) {
492  Parentage entryDesc;
493  ParentageRegistry::instance()->getMapped(*it, entryDesc);
494  std::vector<BranchID> const& parents = entryDesc.parents();
495  for(std::vector<BranchID>::const_iterator j = parents.begin(), jEnd = parents.end();
496  j != jEnd; ++j) {
497  branchChildren_.insertChild(*j, child);
498  }
499  }
500  }
501  }
502 }
nocap nocap const skelname & operator=(const skelname &)
void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren)
T getParameter(std::string const &) const
virtual void doOpenFile()
Definition: OutputModule.h:202
bool empty() const
Definition: ParameterSet.h:219
virtual void writeProductDependencies()
Definition: OutputModule.h:226
int i
Definition: DBlmapReader.cc:9
std::vector< BranchDescription const * > getAllBranchDescriptions()
Definition: OutputModule.cc:32
virtual void respondToCloseOutputFiles(FileBlock const &)
Definition: OutputModule.h:196
bool initialized() const
Definition: GroupSelector.h:34
TPRegexp parents
Definition: eve_filter.cc:24
BranchType const & branchType() const
ModuleDescription moduleDescription_
Definition: OutputModule.h:130
virtual void writeFileFormatVersion()
Definition: OutputModule.h:217
bool doEndLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, CurrentProcessingContext const *cpc)
const_iterator end() const
Definition: Principal.h:141
ModuleDescription const * moduleDescription() const
handle_t getOneTriggerResults(Event const &e)
ParameterSetID id() const
static ThreadSafeRegistry * instance()
CurrentProcessingContext const * current_context_
Definition: OutputModule.h:133
virtual void respondToOpenOutputFiles(FileBlock const &)
Definition: OutputModule.h:195
detail::CachedProducts selectors_
Definition: OutputModule.h:140
ParameterSet const & getParameterSet(ParameterSetID const &id)
virtual void writeIndexIntoFile()
Definition: OutputModule.h:219
bool & produced() const
virtual void endRun(RunPrincipal const &)
Definition: OutputModule.h:187
bool exists(std::string const &parameterName) const
checks if a parameter exists
boost::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:125
void insertEmpty(BranchID parent)
bool getMapped(key_type const &k, value_type &result) const
void insertChild(BranchID parent, BranchID child)
static const std::string & baseType()
void updateBranchParents(EventPrincipal const &ep)
virtual void beginRun(RunPrincipal const &)
Definition: OutputModule.h:186
virtual void finishEndFile()
Definition: OutputModule.h:228
static void fillDescription(ParameterSetDescription &desc, char const *parameterName)
bool selected(BranchDescription const &desc) const
#define FDEBUG(lev)
Definition: DebugMacros.h:18
virtual void writeParentageRegistry()
Definition: OutputModule.h:224
std::string const & moduleLabel() const
uint16_t size_type
dictionary map
Definition: Association.py:205
void doCloseFile()
Tell the OutputModule that is must end the current file.
std::vector< BranchID > const & parents() const
Definition: Parentage.h:38
U second(std::pair< T, U > const &p)
void setEventSelectionInfo(std::map< std::string, std::vector< std::pair< std::string, int > > > const &outputModulePathPositions, bool anyProductProduced)
void run_all_output_module_tests()
std::string process_name_
Definition: OutputModule.h:127
virtual void endLuminosityBlock(LuminosityBlockPrincipal const &)
Definition: OutputModule.h:190
virtual void writeProcessConfigurationRegistry()
Definition: OutputModule.h:220
void addDefault(ParameterSetDescription const &psetDescription)
static void fillDescriptions(ConfigurationDescriptions &descriptions)
bool doEvent(EventPrincipal const &ep, EventSetup const &c, CurrentProcessingContext const *cpc)
ParameterSet const & getUntrackedParameterSet(std::string const &name, ParameterSet const &defaultValue) const
SelectionsArray keptProducts_
Definition: OutputModule.h:124
void addParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:145
void doWriteLuminosityBlock(LuminosityBlockPrincipal const &lbp)
void doPreForkReleaseResources()
virtual void beginLuminosityBlock(LuminosityBlockPrincipal const &)
Definition: OutputModule.h:189
int j
Definition: DBlmapReader.cc:9
GroupSelectorRules groupSelectorRules_
Definition: OutputModule.h:128
GroupSelector groupSelector_
Definition: OutputModule.h:129
void doRespondToCloseOutputFiles(FileBlock const &fb)
static void fillDescription(ParameterSetDescription &desc)
#define end
Definition: vmac.h:38
void copyForModify(ParameterSet const &other)
bool first
Definition: L1TdeRCT.cc:94
virtual void write(EventPrincipal const &e)=0
virtual bool isFileOpen() const
Definition: OutputModule.h:200
bool & transient() const
void doRespondToCloseInputFile(FileBlock const &fb)
virtual void writeFileIdentifier()
Definition: OutputModule.h:218
void fillDependencyGraph()
void setup(std::vector< parsed_path_spec_t > const &path_specs, std::vector< std::string > const &triggernames, const std::string &process_name)
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:198
static void prevalidate(ConfigurationDescriptions &)
CurrentProcessingContext const * currentContext() const
virtual void writeRun(RunPrincipal const &)=0
const_iterator begin() const
Definition: Principal.h:140
double b
Definition: hdecay.h:120
void doWriteRun(RunPrincipal const &rp)
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:193
static void fillDescription(ParameterSetDescription &desc)
static const std::string kBaseType("EDAnalyzer")
bool doBeginLuminosityBlock(LuminosityBlockPrincipal const &lbp, EventSetup const &c, CurrentProcessingContext const *cpc)
boost::filter_iterator< FilledGroupPtr, GroupCollection::const_iterator > const_iterator
Definition: Principal.h:55
std::vector< std::string > const & getAllTriggerNames()
Definition: OutputModule.cc:37
BranchChildren branchChildren_
Definition: OutputModule.h:148
bool selected(BranchDescription const &desc) const
Trig getTriggerResults(Event const &ep) const
virtual void writeParameterSetRegistry()
Definition: OutputModule.h:222
double a
Definition: hdecay.h:121
Strings const & getTrigPaths() const
virtual void endJob()
Definition: OutputModule.h:185
virtual void beginJob()
Definition: OutputModule.h:184
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:194
virtual ~OutputModule()
bool doEndRun(RunPrincipal const &rp, EventSetup const &c, CurrentProcessingContext const *cpc)
void configure(OutputModuleDescription const &desc)
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:192
std::vector< BranchDescription const * > allBranchDescriptions() const
void setupDefault(std::vector< std::string > const &triggernames)
bool doBeginRun(RunPrincipal const &rp, EventSetup const &c, CurrentProcessingContext const *cpc)
void initialize(GroupSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
virtual void startEndFile()
Definition: OutputModule.h:216
ParameterSet const & registerIt()
virtual void writeBranchMapper()
Definition: OutputModule.h:227
virtual void writeLuminosityBlock(LuminosityBlockPrincipal const &)=0
ModuleDescription const & description() const
BranchParents branchParents_
Definition: OutputModule.h:146
mathSSE::Vec4< T > v
virtual void preForkReleaseResources()
Definition: OutputModule.h:197
OutputModule(ParameterSet const &pset)
bool wantEvent(Event const &e)
virtual void writeBranchIDListRegistry()
Definition: OutputModule.h:223
virtual void writeProcessHistoryRegistry()
Definition: OutputModule.h:221
void doRespondToOpenOutputFiles(FileBlock const &fb)
virtual void writeProductDescriptionRegistry()
Definition: OutputModule.h:225
ParameterSetID selector_config_id_
Definition: OutputModule.h:143