OutputModule.h
Go to the documentation of this file.
1 #ifndef art_Framework_Core_OutputModule_h
2 #define art_Framework_Core_OutputModule_h
3 // vim: set sw=2 expandtab :
4 
5 // ==============================================================
6 // The base class of all modules that write to an output stream.
7 // ==============================================================
8 
30 #include "fhiclcpp/ParameterSet.h"
31 #include "fhiclcpp/types/Atom.h"
35 
36 #include <array>
37 #include <atomic>
38 #include <cstddef>
39 #include <memory>
40 #include <set>
41 #include <string>
42 #include <vector>
43 
44 namespace art {
45 
46  class ResultsPrincipal;
47 
48  class OutputModule : public Observer, public detail::SharedModule {
49  friend class WorkerT<OutputModule>;
50  friend class OutputWorker;
51 
52  public:
55 
56  using PluginCollection_t =
57  std::vector<std::unique_ptr<FileCatalogMetadataPlugin>>;
58 
59  public: // CONFIGURATION
60  struct Config {
61  struct KeysToIgnore {
62  std::set<std::string>
64  {
65  return {"module_label"};
66  }
67  static auto
68  get()
69  {
70  return KeysToIgnore{}();
71  }
72  };
76  fhicl::Name("outputCommands"),
77  std::vector<std::string>{"keep *"}};
82  fhicl::Name("FCMDPlugins"),
84  "The 'FCMDPlugins' parameter is a sequence of tables of the form:\n\n"
85  " FCMDPlugins: [ { plugin_type: <pluginClassName> ...}, ... ]\n\n"
86  "where each sequence element is the configuration for an instance\n"
87  "of a FileCatalogMetadataPlugin object. Please review the "
88  "documentation\n"
89  "at the top of \"art/Framework/Core/FileCatalogMetadataPlugin.h\" "
90  "for\n"
91  "more information.")};
92  };
93 
94  public: // MEMBER FUNCTIONS -- Special Member Functions
95  virtual ~OutputModule() noexcept;
96  explicit OutputModule(fhicl::ParameterSet const& pset);
97  explicit OutputModule(fhicl::TableFragment<Config> const& pset,
98  fhicl::ParameterSet const& containing_pset);
99  OutputModule(OutputModule const&) = delete;
100  OutputModule(OutputModule&&) = delete;
101  OutputModule& operator=(OutputModule const&) = delete;
102  OutputModule& operator=(OutputModule&&) = delete;
103 
104  public: // MEMBER FUNCTIONS
105  // Accessor for maximum number of events to be written.
106  // -1 is used for unlimited.
107  int maxEvents() const;
108  // Accessor for remaining number of events to be written.
109  // -1 is used for unlimited.
110  int remainingEvents() const;
111  bool fileIsOpen() const;
113  // Name of output file (may be overridden if default implementation is
114  // not appropriate).
115  virtual std::string const& lastClosedFileName() const;
116  SelectionsArray const& keptProducts() const;
117  bool selected(BranchDescription const&) const;
118  std::array<bool, NumBranchTypes> const& hasNewlyDroppedBranch() const;
119  void selectProducts(ProductTables const&);
120  void doSelectProducts(ProductTables const&);
122  BranchChildren const& branchChildren() const;
123 
124  protected:
125  // Called to register products if necessary.
127  ModuleDescription const&);
128 
129  private:
131  virtual void doBeginJob(detail::SharedResources const& resources);
132  // Called after selectProducts() has done its work.
133  virtual void postSelectProducts();
134  void doEndJob();
135  void doRespondToOpenInputFile(FileBlock const& fb);
136  void doRespondToCloseInputFile(FileBlock const& fb);
137  void doRespondToOpenOutputFiles(FileBlock const& fb);
138  void doRespondToCloseOutputFiles(FileBlock const& fb);
139  bool doBeginRun(RunPrincipal const& rp, ModuleContext const&);
140  bool doEndRun(RunPrincipal const& rp, ModuleContext const& mc);
141  bool doBeginSubRun(SubRunPrincipal const& srp, ModuleContext const& mc);
142  bool doEndSubRun(SubRunPrincipal const& srp, ModuleContext const& mc);
143  bool doEvent(EventPrincipal const& ep,
144  ModuleContext const& mc,
145  std::atomic<std::size_t>& counts_run,
146  std::atomic<std::size_t>& counts_passed,
147  std::atomic<std::size_t>& counts_failed);
148 
149  void doWriteRun(RunPrincipal& rp);
150  void doWriteSubRun(SubRunPrincipal& srp);
151  void doWriteEvent(EventPrincipal& ep, ModuleContext const& mc);
154  bool doCloseFile();
155  bool doOpenFile(FileBlock const& fb);
156 
157  // Implementation API, intended to be provided by derived classes.
158  std::string workerType() const;
159 
160  // Do the end-of-file tasks; this is only called internally, after
161  // the appropriate tests have been done.
162  void reallyCloseFile();
163  virtual void incrementInputFileNumber();
164  // Ask the OutputModule if we should end the current file.
165  // N.B. The default file granularity is 'Unset', which means that
166  // even if an output module requests to close its file, the
167  // file will not switch. To ensure that a file switch requires
168  // where desired, the author of the output module MUST provide
169  // an override. It would be desirable to check if both
170  // requestsToCloseFile() and fileGranularity() could be checked
171  // at compile time. However, such a check would require an
172  // interface change.
173  virtual bool requestsToCloseFile() const;
174  virtual Granularity fileGranularity() const;
175  virtual void setFileStatus(OutputFileStatus);
176  virtual void beginJob();
177  virtual void endJob();
178  virtual void beginRun(RunPrincipal const&);
179  virtual void endRun(RunPrincipal const&);
180  virtual void writeRun(RunPrincipal& r) = 0;
181  virtual void setRunAuxiliaryRangeSetID(RangeSet const&);
182  virtual void beginSubRun(SubRunPrincipal const&);
183  virtual void endSubRun(SubRunPrincipal const&);
184  virtual void writeSubRun(SubRunPrincipal& sr) = 0;
185  virtual void setSubRunAuxiliaryRangeSetID(RangeSet const&);
186  virtual void event(EventPrincipal const&);
187  virtual void write(EventPrincipal& e) = 0;
188  virtual void openFile(FileBlock const&);
189  virtual void respondToOpenInputFile(FileBlock const&);
190  virtual void readResults(ResultsPrincipal const& resp);
191  virtual void respondToCloseInputFile(FileBlock const&);
192  virtual void respondToOpenOutputFiles(FileBlock const&);
193  virtual void respondToCloseOutputFiles(FileBlock const&);
194  virtual bool isFileOpen() const;
196  void fillDependencyGraph();
197  bool limitReached() const;
198 
199  // The following member functions are part of the Template Method
200  // pattern, used for implementing doCloseFile() and maybeEndFile().
201  virtual void startEndFile();
202  virtual void writeFileFormatVersion();
203  virtual void writeFileIdentifier();
204  virtual void writeFileIndex();
205  virtual void writeEventHistory();
206  virtual void writeProcessConfigurationRegistry();
207  virtual void writeProcessHistoryRegistry();
208  virtual void writeParameterSetRegistry();
209  virtual void writeBranchIDListRegistry();
210  virtual void writeParentageRegistry();
211  virtual void writeProductDescriptionRegistry();
213  virtual void doWriteFileCatalogMetadata(
216  virtual void writeProductDependencies();
217  virtual void finishEndFile();
219  std::vector<fhicl::ParameterSet> const& psets);
220 
221  // TODO: Give OutputModule an interface (protected?) that supplies
222  // client code with the needed functionality *without* giving away
223  // implementation details ... don't just return a reference to
224  // keptProducts_, because we are looking to have the flexibility
225  // to change the implementation of keptProducts_ without modifying
226  // clients. When this change is made, we'll have a one-time-only
227  // task of modifying clients (classes derived from OutputModule)
228  // to use the newly-introduced interface. TODO: Consider using
229  // shared pointers here?
230  //
231  // keptProducts_ are BranchDescription objects OWNED BY VALUE
232  // describing the branches we are to write.
234  std::array<std::unique_ptr<GroupSelector const>, NumBranchTypes>
235  groupSelector_{{nullptr}};
236  std::array<bool, NumBranchTypes> hasNewlyDroppedBranch_{{false}};
238  int maxEvents_{-1};
240  using BranchParents = std::map<ProductID, std::set<ParentageID>>;
241  std::map<ProductID, std::set<ParentageID>> branchParents_{};
248 
249  // For diagnostics.
250  std::vector<std::string> pluginNames_{};
252  };
253 
254 } // namespace art
255 
256 #endif /* art_Framework_Core_OutputModule_h */
257 
258 // Local Variables:
259 // mode: c++
260 // End:
virtual void writeFileIdentifier()
bool doOpenFile(FileBlock const &fb)
std::array< bool, NumBranchTypes > const & hasNewlyDroppedBranch() const
void doWriteEvent(EventPrincipal &ep, ModuleContext const &mc)
std::vector< std::pair< std::string, std::string >> collection_type
virtual void writeProcessConfigurationRegistry()
fhicl::Atom< std::string > fileName
Definition: OutputModule.h:78
void updateBranchParents(EventPrincipal &ep)
virtual void readResults(ResultsPrincipal const &resp)
virtual void doBeginJob(detail::SharedResources const &resources)
virtual void finishEndFile()
fhicl::Sequence< std::string > outputCommands
Definition: OutputModule.h:75
virtual void incrementInputFileNumber()
fhicl::Atom< std::string > moduleType
Definition: OutputModule.h:73
std::string streamName_
Definition: OutputModule.h:245
virtual void event(EventPrincipal const &)
std::string string
Definition: nybbler.cc:12
virtual void respondToCloseOutputFiles(FileBlock const &)
virtual void writeRun(RunPrincipal &r)=0
virtual void writeSubRun(SubRunPrincipal &sr)=0
virtual void doRegisterProducts(ProductDescriptions &, ModuleDescription const &)
std::vector< std::string > pluginNames_
Definition: OutputModule.h:250
OutputModule & operator=(OutputModule const &)=delete
bool doEndSubRun(SubRunPrincipal const &srp, ModuleContext const &mc)
ChannelGroupService::Name Name
virtual void respondToOpenInputFile(FileBlock const &)
void writeFileCatalogMetadata()
void doWriteSubRun(SubRunPrincipal &srp)
std::vector< BranchDescription > ProductDescriptions
void doSelectProducts(ProductTables const &)
int maxEvents() const
bool doBeginSubRun(SubRunPrincipal const &srp, ModuleContext const &mc)
virtual void writeFileFormatVersion()
virtual void beginRun(RunPrincipal const &)
bool doEvent(EventPrincipal const &ep, ModuleContext const &mc, std::atomic< std::size_t > &counts_run, std::atomic< std::size_t > &counts_passed, std::atomic< std::size_t > &counts_failed)
BranchChildren branchChildren_
Definition: OutputModule.h:242
virtual void endRun(RunPrincipal const &)
virtual bool isFileOpen() const
virtual std::string const & lastClosedFileName() const
virtual ~OutputModule() noexcept
std::vector< std::unique_ptr< FileCatalogMetadataPlugin >> PluginCollection_t
Definition: OutputModule.h:57
void doRespondToCloseInputFile(FileBlock const &fb)
OutputFileStatus fileStatus() const
void selectProducts(ProductTables const &)
fhicl::TableFragment< Observer::EOConfig > eoFragment
Definition: OutputModule.h:74
virtual void writeFileIndex()
virtual void respondToOpenOutputFiles(FileBlock const &)
GroupSelectorRules groupSelectorRules_
Definition: OutputModule.h:237
bool limitReached() const
PluginCollection_t plugins_
Definition: OutputModule.h:251
void registerProducts(ProductDescriptions &, ModuleDescription const &)
std::string workerType() const
Definition: OutputModule.cc:94
virtual void setSubRunAuxiliaryRangeSetID(RangeSet const &)
OutputFileStatus
const double e
fhicl::OptionalDelegatedParameter fcmdPlugins
Definition: OutputModule.h:81
std::set< std::string > operator()()
Definition: OutputModule.h:63
virtual void writeParameterSetRegistry()
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:236
virtual void endSubRun(SubRunPrincipal const &)
bool doEndRun(RunPrincipal const &rp, ModuleContext const &mc)
std::array< std::unique_ptr< GroupSelector const >, NumBranchTypes > groupSelector_
Definition: OutputModule.h:235
virtual void setRunAuxiliaryRangeSetID(RangeSet const &)
virtual void writeParentageRegistry()
virtual void endJob()
virtual void write(EventPrincipal &e)=0
void doSetSubRunAuxiliaryRangeSetID(RangeSet const &)
SelectionsArray keptProducts_
Definition: OutputModule.h:233
ServiceHandle< CatalogInterface > ci_
Definition: OutputModule.h:246
virtual void writeProductDescriptionRegistry()
std::array< Selections, NumBranchTypes > SelectionsArray
Definition: Selections.h:12
virtual Granularity fileGranularity() const
OutputModule(fhicl::ParameterSet const &pset)
Definition: OutputModule.cc:73
std::map< ProductID, std::set< ParentageID > > branchParents_
Definition: OutputModule.h:241
bool selected(BranchDescription const &) const
bool fileIsOpen() const
Definition: OutputModule.cc:88
std::string configuredFileName_
Definition: OutputModule.h:243
virtual bool requestsToCloseFile() const
int remainingEvents() const
std::string dataTier_
Definition: OutputModule.h:244
void fillDependencyGraph()
cet::BasicPluginFactory pluginFactory_
Definition: OutputModule.h:247
#define Comment
void doRespondToOpenOutputFiles(FileBlock const &fb)
virtual void postSelectProducts()
fhicl::Atom< std::string > streamName
Definition: OutputModule.h:80
PluginCollection_t makePlugins_(std::vector< fhicl::ParameterSet > const &psets)
fhicl::Atom< std::string > dataTier
Definition: OutputModule.h:79
virtual void openFile(FileBlock const &)
void doWriteRun(RunPrincipal &rp)
virtual void writeEventHistory()
SelectionsArray const & keptProducts() const
virtual void writeProcessHistoryRegistry()
friend class OutputWorker
Definition: OutputModule.h:50
virtual void writeProductDependencies()
virtual void setFileStatus(OutputFileStatus)
std::map< ProductID, std::set< ParentageID >> BranchParents
Definition: OutputModule.h:240
virtual void doWriteFileCatalogMetadata(FileCatalogMetadata::collection_type const &md, FileCatalogMetadata::collection_type const &ssmd)
virtual void respondToCloseInputFile(FileBlock const &)
void doRespondToCloseOutputFiles(FileBlock const &fb)
static constexpr double sr
Definition: Units.h:166
BranchChildren const & branchChildren() const
virtual void beginSubRun(SubRunPrincipal const &)
virtual void beginJob()
void doRespondToOpenInputFile(FileBlock const &fb)
virtual void writeBranchIDListRegistry()
virtual void startEndFile()
void doSetRunAuxiliaryRangeSetID(RangeSet const &)
void configure(OutputModuleDescription const &desc)
bool doBeginRun(RunPrincipal const &rp, ModuleContext const &)