Project

General

Profile

TFileService.h

Kyle Knoepfel, 11/02/2017 09:06 AM

 
1
#ifndef art_Framework_Services_Optional_TFileService_h
2
#define art_Framework_Services_Optional_TFileService_h
3

    
4
// ======================================================================
5
//
6
// TFileService
7
//
8
// ======================================================================
9

    
10
#include "art/Framework/Core/OutputFileGranularity.h"
11
#include "art/Framework/IO/FileStatsCollector.h"
12
#include "art/Framework/IO/Root/RootOutputClosingCriteria.h"
13
#include "art/Framework/Services/Optional/TFileDirectory.h"
14
#include "art/Framework/Services/Registry/ServiceMacros.h"
15
#include "fhiclcpp/types/Atom.h"
16
#include "fhiclcpp/types/Name.h"
17
#include "fhiclcpp/types/OptionalTable.h"
18

    
19
namespace art {
20
  class ActivityRegistry;   // declaration only
21
  class ModuleDescription;  // declaration only
22
  class TFileService;       // defined below
23
}
24
namespace fhicl {
25
  class ParameterSet;       // declaration only
26
}
27

    
28
// ----------------------------------------------------------------------
29

    
30
class art::TFileService
31
  : public TFileDirectory
32
{
33
  // non-copyable:
34
  TFileService(TFileService const&) = delete;
35
  TFileService operator = (TFileService const&) = delete;
36

    
37
public:
38

    
39
  static constexpr const char* default_tmpDir = "<parent-path-of-filename>";
40

    
41
  struct Config {
42
    fhicl::Atom<bool> closeFileFast{fhicl::Name("closeFileFast"), true};
43
    fhicl::Atom<std::string> fileName{fhicl::Name("fileName")};
44
    fhicl::Atom<std::string> tmpDir{fhicl::Name("tmpDir"), default_tmpDir};
45
    fhicl::OptionalTable<ClosingCriteria::Config> fileProperties{fhicl::Name("fileProperties")};
46
  };
47

    
48
  // c'tor:
49
  using Parameters = ServiceTable<Config>;
50
  TFileService(ServiceTable<Config> const& config,
51
               art::ActivityRegistry& r);
52

    
53
  // d'tor:
54
  ~TFileService();
55

    
56
  using Callback_t = TFileDirectory::Callback_t;
57

    
58
  void
59
  registerFileSwitchCallback(Callback_t c);
60

    
61
  template <typename T>
62
  void
63
  registerFileSwitchCallback(T* provider, void (T::*)());
64

    
65
  // accessor:
66
  TFile& file() const { return *file_; }
67

    
68
private:
69
  bool const closeFileFast_;
70
  FileStatsCollector fstats_;
71
  std::string filePattern_;
72
  std::string uniqueFilename_;
73
  std::string tmpDir_;
74

    
75
  // File-switching mechanics
76
  std::string lastClosedFile_{};
77
  Granularity currentGranularity_{Granularity::Unset};
78
  std::chrono::steady_clock::time_point beginTime_{};
79
  // Do not use default-constructed "ClosingCriteria" as that will be
80
  // sure to trigger a file switch at the first call of
81
  // requestsToCloseFile_().
82
  ClosingCriteria fileSwitchCriteria_{ClosingCriteria::Config{}};
83
  OutputFileStatus status_{OutputFileStatus::Closed};
84
  FileProperties fp_{};
85

    
86
  // set current directory according to module name and prepare to create
87
  // directory
88
  void setDirectoryName_(art::ModuleDescription const& desc);
89
  void openFile_();
90
  void closeFile_();
91
  void maybeSwitchFiles_();
92
  bool requestsToCloseFile_();
93
}; // TFileService
94

    
95
// ======================================================================
96

    
97
template <typename T>
98
void
99
art::TFileService::registerFileSwitchCallback(T* provider, void (T::*f)())
100
{
101
  auto cb = [provider, f]{ return (provider->*f)(); };
102
  registerFileSwitchCallback(cb);
103
}
104

    
105

    
106
DECLARE_ART_SERVICE(TFileService, LEGACY)
107
#endif /* art_Framework_Services_Optional_TFileService_h */
108

    
109
// Local Variables:
110
// mode: c++
111
// End: