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