LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
TFileService_service.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // TFileService
4 //
5 // ======================================================================
6 
8 
21 #include "cetlib/assert_only_one_thread.h"
22 #include "fhiclcpp/ParameterSet.h"
23 
24 #include "TFile.h"
25 #include "TROOT.h"
26 
27 namespace {
28  std::string const dev_null{"/dev/null"};
29 }
30 
31 using namespace std;
32 using art::TFileService;
34 
35 // ----------------------------------------------------------------------
36 
37 TFileService::TFileService(ServiceTable<Config> const& config,
39  : TFileDirectory{"", "", nullptr, ""}
40  , closeFileFast_{config().closeFileFast()}
41  , fstats_{config.get_PSet().get<std::string>("service_type"),
42  ServiceHandle<TriggerNamesService const>{}->getProcessName()}
43  , filePattern_{config().fileName()}
44  , tmpDir_{config().tmpDir()}
45 {
46  ClosingCriteria::Config fpConfig;
47  requireCallback_ = config().fileProperties(fpConfig);
48  if (requireCallback_) {
51  }
52  openFile_();
53 
54  // Activities to monitor in order to set the proper directory.
55  r.sPostOpenFile.watch(
56  [this](std::string const& fileName) { fstats_.recordInputFile(fileName); });
57 
58  r.sPreModuleBeginJob.watch(this, &TFileService::setDirectoryName_);
59  r.sPreModuleEndJob.watch(this, &TFileService::setDirectoryName_);
60 
61  r.sPreModuleConstruction.watch(this, &TFileService::setDirectoryName_);
62  r.sPreModuleRespondToOpenInputFile.watch(this,
64  r.sPreModuleRespondToCloseInputFile.watch(this,
66  r.sPreModuleRespondToOpenOutputFiles.watch(this,
68  r.sPreModuleRespondToCloseOutputFiles.watch(this,
70 
71  r.sPreModuleBeginRun.watch(this, &TFileService::setDirectoryName_);
72  r.sPreModuleEndRun.watch(this, &TFileService::setDirectoryName_);
73 
74  r.sPreModuleBeginSubRun.watch(this, &TFileService::setDirectoryName_);
75  r.sPreModuleEndSubRun.watch(this, &TFileService::setDirectoryName_);
76 
77  r.sPreModule.watch(this, &TFileService::setDirectoryName_);
78 
79  // Activities to monitor to keep track of events, subruns and runs seen.
80  r.sPostProcessEvent.watch([this](Event const& e) {
83  fstats_.recordEvent(e.id());
84  if (requestsToCloseFile_()) {
86  }
87  });
88  r.sPostEndSubRun.watch([this](SubRun const& sr) {
91  fstats_.recordSubRun(sr.id());
92  if (requestsToCloseFile_()) {
94  }
95  });
96  r.sPostEndRun.watch([this](Run const& r) {
99  fstats_.recordRun(r.id());
100  if (requestsToCloseFile_()) {
102  }
103  });
104  r.sPostCloseFile.watch([this] {
107  if (requestsToCloseFile_()) {
109  }
110  });
111 }
112 
114 {
115  closeFile_();
116 }
117 
118 void
120 {
121  registerCallback(cb);
122 }
123 
124 void
126 {
127  CET_ASSERT_ONLY_ONE_THREAD();
128  dir_ = desc.moduleLabel();
129  descr_ = dir_;
130  descr_ += " (";
131  descr_ += desc.moduleName();
132  descr_ += ") folder";
133 }
134 
135 std::string
137 {
138  if (filePattern_ == dev_null)
139  return dev_null;
140  return unique_filename(
142  "/TFileService");
143 }
144 
145 std::string
146 TFileService::fileNameAtClose_(std::string const& filename)
147 {
148  return filePattern_ == dev_null ?
149  dev_null :
151 }
152 
153 void
155 {
157  assert(file_ == nullptr && "TFile pointer should always be zero here!");
158  beginTime_ = std::chrono::steady_clock::now();
159  file_ = new TFile{uniqueFilename_.c_str(), "RECREATE"};
162 }
163 
164 void
166 {
167  file_->Write();
168  if (closeFileFast_) {
169  gROOT->GetListOfFiles()->Remove(file_);
170  }
171  file_->Close();
172  delete file_;
173  file_ = nullptr;
177 }
178 
179 void
181 {
182  // FIXME: Should maybe include the granularity check in
183  // requestsToCloseFile_().
185  return;
186 
188  closeFile_();
189  detail::logFileAction("Closed TFileService file ", lastClosedFile_);
190  detail::logFileAction("Switching to new TFileService file with pattern ",
191  filePattern_);
192  fp_ = FileProperties{};
193  openFile_();
194  invokeCallbacks();
195 }
196 
197 bool
199 {
200  using namespace std::chrono;
201  unsigned int constexpr oneK{1024u};
202  fp_.updateSize(file_->GetSize() / oneK);
203  fp_.updateAge(duration_cast<seconds>(steady_clock::now() - beginTime_));
205 }
206 
std::string tmpDir_
Definition: TFileService.h:74
std::function< void()> Callback_t
void registerFileSwitchCallback(Callback_t c)
#define DEFINE_ART_SERVICE(svc)
Definition: ServiceMacros.h:93
void recordRun(RunID const &id)
std::string lastClosedFile_
Definition: TFileService.h:77
void validateFileNamePattern(bool do_check, std::string const &pattern)
void logFileAction(const char *msg, std::string const &file)
Definition: logFileAction.cc:9
STL namespace.
void recordEvent(EventID const &id)
PostCloseFileRenamer fRenamer_
Definition: TFileService.h:71
bool const closeFileFast_
Definition: TFileService.h:69
FileStatsCollector fstats_
Definition: TFileService.h:70
Definition: Run.h:30
std::string unique_filename(std::string stem, std::string extension=".root")
ClosingCriteria fileSwitchCriteria_
Definition: TFileService.h:83
bool should_close(FileProperties const &) const
void updateAge(std::chrono::seconds const age)
void registerCallback(Callback_t)
std::string const & moduleName() const
auto granularity() const
std::string uniqueFilename_
Definition: TFileService.h:73
RunID const & id() const
Definition: Run.h:41
static constexpr const char * default_tmpDir
Definition: TFileService.h:37
void recordSubRun(SubRunID const &id)
Granularity currentGranularity_
Definition: TFileService.h:78
std::chrono::steady_clock::time_point beginTime_
Definition: TFileService.h:79
void recordInputFile(std::string const &inputFileName)
std::string filePattern_
Definition: TFileService.h:72
std::string const & moduleLabel() const
void setDirectoryName_(art::ModuleDescription const &desc)
std::enable_if_t< B!=Granularity::InputFile > update(OutputFileStatus const status)
std::string parent_path(std::string const &path)
Definition: parent_path.cc:15
OutputFileStatus status_
Definition: TFileService.h:84
SubRunID id() const
Definition: SubRun.h:53
std::string maybeRenameFile(std::string const &inPath, std::string const &toPattern)
std::string fileNameAtClose_(std::string const &)
Float_t e
Definition: plot.C:34
void updateSize(unsigned const size)
FileProperties fp_
Definition: TFileService.h:85
EventID id() const
Definition: Event.h:56
std::string fileNameAtOpen_()