OutputModule.h
Go to the documentation of this file.
1 #ifndef art_Framework_Core_OutputModule_h
2 #define art_Framework_Core_OutputModule_h
3 
4 // ======================================================================
5 //
6 // OutputModule - The base class of all "modules" that write Events to an
7 // output stream.
8 //
9 // ======================================================================
10 
32 #include "fhiclcpp/ParameterSet.h"
33 #include "fhiclcpp/types/Atom.h"
37 
38 #include <array>
39 #include <cassert>
40 #include <memory>
41 #include <set>
42 #include <string>
43 #include <vector>
44 
45 // ----------------------------------------------------------------------
46 
47 namespace art {
48  class OutputModule;
49  class ResultsPrincipal;
50 }
51 
53 public:
54  OutputModule(OutputModule const&) = delete;
55  OutputModule& operator=(OutputModule const&) = delete;
56 
57  template <typename T>
58  friend class WorkerT;
59  friend class OutputWorker;
62 
63  // Configuration
64  struct Config {
65  struct KeysToIgnore {
66  std::set<std::string>
68  {
69  return {"module_label", "streamName", "FCMDPlugins"};
70  }
71  static auto
72  get()
73  {
74  return KeysToIgnore{}();
75  }
76  };
77 
81  fhicl::Name("outputCommands"),
82  std::vector<std::string>{"keep *"}};
86  };
87 
88  explicit OutputModule(fhicl::TableFragment<Config> const& pset,
89  fhicl::ParameterSet const& containing_pset);
90  explicit OutputModule(fhicl::ParameterSet const& pset);
91 
92  virtual ~OutputModule() noexcept = default;
93 
94  // Accessor for maximum number of events to be written.
95  // -1 is used for unlimited.
96  int maxEvents() const;
97 
98  // Accessor for remaining number of events to be written.
99  // -1 is used for unlimited.
100  int remainingEvents() const;
101 
102  bool
103  fileIsOpen() const
104  {
105  return isFileOpen();
106  }
108 
109  // Name of output file (may be overridden if default implementation is
110  // not appropriate).
111  virtual std::string const& lastClosedFileName() const;
112 
113  bool selected(BranchDescription const&) const;
114  SelectionsArray const& keptProducts() const;
115  std::array<bool, NumBranchTypes> const& hasNewlyDroppedBranch() const;
116 
117  BranchChildren const& branchChildren() const;
118 
119  void selectProducts(ProductList const&);
120 
123  ModuleDescription const&);
124 
125 protected:
126  // The returned pointer will be null unless the this is currently
127  // executing its event loop function ('write').
129 
130  ModuleDescription const& description() const;
131 
132  // Called after selectProducts() has done its work.
133  virtual void postSelectProducts();
134 
135  // Called to register products if necessary.
138  ModuleDescription const&);
139 
140 private:
141  // TODO: Give OutputModule an interface (protected?) that supplies
142  // client code with the needed functionality *without* giving away
143  // implementation details ... don't just return a reference to
144  // keptProducts_, because we are looking to have the flexibility to
145  // change the implementation of keptProducts_ without modifying
146  // clients. When this change is made, we'll have a one-time-only
147  // task of modifying clients (classes derived from OutputModule) to
148  // use the newly-introduced interface. TODO: Consider using shared
149  // pointers here?
150 
151  // keptProducts_ are pointers to the BranchDescription objects
152  // describing the branches we are to write.
153  //
154  // We do not own the BranchDescriptions to which we point.
155 
156  SelectionsArray keptProducts_{{}}; // filled by aggregation
157  std::array<bool, NumBranchTypes> hasNewlyDroppedBranch_{
158  {false}}; // filled by aggregation
160  std::unique_ptr<GroupSelector const> groupSelector_{nullptr};
161  int maxEvents_{-1};
163 
166 
167  using BranchParents = std::map<ProductID, std::set<ParentageID>>;
169 
171 
176 
177  std::vector<std::string> pluginNames_{}; // For diagnostics.
178 
179  using PluginCollection_t =
180  std::vector<std::unique_ptr<FileCatalogMetadataPlugin>>;
182 
183  //------------------------------------------------------------------
184  // private member functions
185  //------------------------------------------------------------------
186  void configure(OutputModuleDescription const& desc);
187 
188  void doBeginJob();
189  void doEndJob();
190  bool doEvent(EventPrincipal const& ep,
191  CurrentProcessingContext const* cpc,
193  bool doBeginRun(RunPrincipal const& rp, CurrentProcessingContext const* cpc);
194  bool doEndRun(RunPrincipal const& rp, CurrentProcessingContext const* cpc);
195  bool doBeginSubRun(SubRunPrincipal const& srp,
196  CurrentProcessingContext const* cpc);
197  bool doEndSubRun(SubRunPrincipal const& srp,
198  CurrentProcessingContext const* cpc);
199  void doWriteRun(RunPrincipal& rp);
200  void doWriteSubRun(SubRunPrincipal& srp);
204  void doOpenFile(FileBlock const& fb);
209  void doSelectProducts(ProductList const&);
210 
212  workerType() const
213  {
214  return "OutputWorker";
215  }
216 
217  // Tell the OutputModule that it must end the current file.
218  void doCloseFile();
219 
220  // Do the end-of-file tasks; this is only called internally, after
221  // the appropriate tests have been done.
222  void reallyCloseFile();
223 
224  virtual void
226  {}
227 
228  // Ask the OutputModule if we should end the current file.
229  // N.B. The default file granularity is 'Unset', which means that
230  // even if an output module requests to close its file, the
231  // file will not switch. To ensure that a file switch requires
232  // where desired, the author of the output module MUST provide
233  // an override. It would be desirable to check if both
234  // requestsToCloseFile() and fileGranularity() could be checked
235  // at compile time. However, such a check would require an
236  // interface change.
237  virtual bool
239  {
240  return false;
241  }
242  virtual Granularity
244  {
245  return Granularity::Unset;
246  }
247  virtual void setFileStatus(OutputFileStatus);
248 
249  virtual void beginJob();
250  virtual void endJob();
251  virtual void beginRun(RunPrincipal const&);
252  virtual void endRun(RunPrincipal const&);
253  virtual void writeRun(RunPrincipal& r) = 0;
254  virtual void setRunAuxiliaryRangeSetID(RangeSet const&);
255  virtual void beginSubRun(SubRunPrincipal const&);
256  virtual void endSubRun(SubRunPrincipal const&);
257  virtual void writeSubRun(SubRunPrincipal& sr) = 0;
258  virtual void setSubRunAuxiliaryRangeSetID(RangeSet const&);
259  virtual void event(EventPrincipal const&);
260  virtual void write(EventPrincipal& e) = 0;
261 
262  virtual void openFile(FileBlock const&);
263  virtual void respondToOpenInputFile(FileBlock const&);
264  virtual void readResults(ResultsPrincipal const& resp);
265  virtual void respondToCloseInputFile(FileBlock const&);
266  virtual void respondToOpenOutputFiles(FileBlock const&);
267  virtual void respondToCloseOutputFiles(FileBlock const&);
268 
269  virtual bool isFileOpen() const;
270 
272 
274  void fillDependencyGraph();
275 
276  bool limitReached() const;
277 
278  // The following member functions are part of the Template Method
279  // pattern, used for implementing doCloseFile() and maybeEndFile().
280 
281  virtual void startEndFile();
282  virtual void writeFileFormatVersion();
283  virtual void writeFileIdentifier();
284  virtual void writeFileIndex();
285  virtual void writeEventHistory();
286  virtual void writeProcessConfigurationRegistry();
287  virtual void writeProcessHistoryRegistry();
288  virtual void writeParameterSetRegistry();
289  virtual void writeParentageRegistry();
290  virtual void writeProductDescriptionRegistry();
292  virtual void doWriteFileCatalogMetadata(
295  virtual void writeProductDependencies();
296  virtual void writeBranchMapper();
297  virtual void finishEndFile();
298 
300 }; // OutputModule
301 
302 inline art::CurrentProcessingContext const*
304 {
305  return current_context_.get();
306 }
307 
308 inline art::ModuleDescription const&
310 {
311  return moduleDescription_;
312 }
313 
314 inline int
316 {
317  return maxEvents_;
318 }
319 
320 inline int
322 {
323  return remainingEvents_;
324 }
325 
326 inline bool
328 {
330  return groupSelector_->selected(pd);
331 }
332 
333 inline auto
335 {
336  return keptProducts_;
337 }
338 
339 inline auto
341  -> std::array<bool, NumBranchTypes> const&
342 {
343  return hasNewlyDroppedBranch_;
344 }
345 
346 inline art::BranchChildren const&
348 {
349  return branchChildren_;
350 }
351 
352 inline void
354 {
356 }
357 
358 inline bool
360 {
361  return remainingEvents_ == 0;
362 }
363 
364 #endif /* art_Framework_Core_OutputModule_h */
365 
366 // Local Variables:
367 // mode: c++
368 // End:
virtual void beginJob()
virtual void writeProductDependencies()
bool doBeginSubRun(SubRunPrincipal const &srp, CurrentProcessingContext const *cpc)
std::unique_ptr< GroupSelector const > groupSelector_
Definition: OutputModule.h:160
virtual void respondToCloseOutputFiles(FileBlock const &)
std::vector< std::pair< std::string, std::string >> collection_type
fhicl::Atom< std::string > fileName
Definition: OutputModule.h:83
virtual void doWriteFileCatalogMetadata(FileCatalogMetadata::collection_type const &md, FileCatalogMetadata::collection_type const &ssmd)
CurrentProcessingContext const * currentContext() const
Definition: OutputModule.h:303
void setModuleDescription(ModuleDescription const &md)
Definition: OutputModule.h:353
fhicl::Sequence< std::string > outputCommands
Definition: OutputModule.h:80
fhicl::Atom< std::string > moduleType
Definition: OutputModule.h:78
BranchChildren const & branchChildren() const
Definition: OutputModule.h:347
virtual void writeFileFormatVersion()
std::string streamName_
Definition: OutputModule.h:174
virtual void startEndFile()
virtual void writeProcessHistoryRegistry()
virtual void writeRun(RunPrincipal &r)=0
virtual void writeSubRun(SubRunPrincipal &sr)=0
virtual void writeProductDescriptionRegistry()
std::vector< std::string > pluginNames_
Definition: OutputModule.h:177
OutputModule & operator=(OutputModule const &)=delete
void writeFileCatalogMetadata()
std::map< BranchKey, BranchDescription > ProductList
Definition: ProductList.h:15
void doWriteSubRun(SubRunPrincipal &srp)
pointer get() const noexcept
Definition: exempt_ptr.h:146
std::vector< BranchDescription > ProductDescriptions
virtual Granularity fileGranularity() const
Definition: OutputModule.h:243
virtual bool isFileOpen() const
virtual void writeParentageRegistry()
int maxEvents() const
Definition: OutputModule.h:315
virtual void readResults(ResultsPrincipal const &resp)
BranchChildren branchChildren_
Definition: OutputModule.h:170
fhicl::TableFragment< EventObserverBase::EOConfig > eoFragment
Definition: OutputModule.h:79
virtual void writeFileIndex()
std::vector< std::unique_ptr< FileCatalogMetadataPlugin >> PluginCollection_t
Definition: OutputModule.h:180
virtual void endRun(RunPrincipal const &)
void doRespondToCloseInputFile(FileBlock const &fb)
OutputFileStatus fileStatus() const
virtual void setSubRunAuxiliaryRangeSetID(RangeSet const &)
virtual std::string const & lastClosedFileName() const
GroupSelectorRules groupSelectorRules_
Definition: OutputModule.h:159
bool limitReached() const
Definition: OutputModule.h:359
PluginCollection_t plugins_
Definition: OutputModule.h:181
virtual void openFile(FileBlock const &)
OutputFileStatus
virtual void writeEventHistory()
ModuleDescription moduleDescription_
Definition: OutputModule.h:164
std::set< std::string > operator()()
Definition: OutputModule.h:67
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:157
cet::exempt_ptr< CurrentProcessingContext const > current_context_
Definition: OutputModule.h:165
virtual bool requestsToCloseFile() const
Definition: OutputModule.h:238
virtual void respondToOpenOutputFiles(FileBlock const &)
virtual void write(EventPrincipal &e)=0
void doSetSubRunAuxiliaryRangeSetID(RangeSet const &)
SelectionsArray keptProducts_
Definition: OutputModule.h:156
ServiceHandle< CatalogInterface > ci_
Definition: OutputModule.h:175
void doWriteEvent(EventPrincipal &ep)
void registerProducts(MasterProductRegistry &, ProductDescriptions &, ModuleDescription const &)
caf::StandardRecord * sr
SelectionsArray const & keptProducts() const
Definition: OutputModule.h:334
std::array< Selections, NumBranchTypes > SelectionsArray
Definition: Selections.h:12
bool doEndRun(RunPrincipal const &rp, CurrentProcessingContext const *cpc)
std::array< bool, NumBranchTypes > const & hasNewlyDroppedBranch() const
Definition: OutputModule.h:340
virtual void incrementInputFileNumber()
Definition: OutputModule.h:225
bool doBeginRun(RunPrincipal const &rp, CurrentProcessingContext const *cpc)
bool selected(BranchDescription const &) const
Definition: OutputModule.h:327
bool fileIsOpen() const
Definition: OutputModule.h:103
ModuleDescription const & description() const
Definition: OutputModule.h:309
std::string configuredFileName_
Definition: OutputModule.h:172
virtual void beginRun(RunPrincipal const &)
virtual void writeFileIdentifier()
int remainingEvents() const
Definition: OutputModule.h:321
std::string dataTier_
Definition: OutputModule.h:173
void fillDependencyGraph()
virtual void doRegisterProducts(MasterProductRegistry &, ProductDescriptions &, ModuleDescription const &)
virtual void postSelectProducts()
void doRespondToOpenOutputFiles(FileBlock const &fb)
fhicl::Atom< std::string > streamName
Definition: OutputModule.h:85
fhicl::Atom< std::string > dataTier
Definition: OutputModule.h:84
void selectProducts(ProductList const &)
void doWriteRun(RunPrincipal &rp)
virtual void endSubRun(SubRunPrincipal const &)
void doSelectProducts(ProductList const &)
virtual void endJob()
assert(nhit_max >=nhit_nbins)
TRandom3 r(0)
PluginCollection_t makePlugins_(fhicl::ParameterSet const &top_pset)
Service to store calibration data products (CDP) in the SQLite3 metadatabase of a file...
Definition: FillParentInfo.h:8
friend class OutputWorker
Definition: OutputModule.h:59
void doOpenFile(FileBlock const &fb)
virtual ~OutputModule() noexcept=default
virtual void setRunAuxiliaryRangeSetID(RangeSet const &)
std::map< ProductID, std::set< ParentageID >> BranchParents
Definition: OutputModule.h:167
virtual void finishEndFile()
virtual void setFileStatus(OutputFileStatus)
void doRespondToCloseOutputFiles(FileBlock const &fb)
std::string workerType() const
Definition: OutputModule.h:212
Float_t e
Definition: plot.C:35
virtual void respondToOpenInputFile(FileBlock const &)
virtual void writeProcessConfigurationRegistry()
virtual void writeBranchMapper()
virtual void writeParameterSetRegistry()
virtual void respondToCloseInputFile(FileBlock const &)
bool doEndSubRun(SubRunPrincipal const &srp, CurrentProcessingContext const *cpc)
void updateBranchParents(EventPrincipal const &ep)
bool doEvent(EventPrincipal const &ep, CurrentProcessingContext const *cpc, CountingStatistics &)
virtual void event(EventPrincipal const &)
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void beginSubRun(SubRunPrincipal const &)
OutputModule(OutputModule const &)=delete
BranchParents branchParents_
Definition: OutputModule.h:168
void doSetRunAuxiliaryRangeSetID(RangeSet const &)
void configure(OutputModuleDescription const &desc)
enum BeamMode string