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