Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:13

0001 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0002 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0003 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 #include "FWCore/MessageLogger/interface/JobReport.h"
0008 #include "TFile.h"
0009 #include "TROOT.h"
0010 
0011 #include <map>
0012 #include <unistd.h>
0013 
0014 const std::string TFileService::kSharedResource = "TFileService";
0015 thread_local TFileDirectory TFileService::tFileDirectory_;
0016 
0017 TFileService::TFileService(const edm::ParameterSet& cfg, edm::ActivityRegistry& r)
0018     : file_(nullptr),
0019       fileName_(cfg.getParameter<std::string>("fileName")),
0020       fileNameRecorded_(false),
0021       closeFileFast_(cfg.getUntrackedParameter<bool>("closeFileFast", false)) {
0022   tFileDirectory_ = TFileDirectory("", "", TFile::Open(fileName_.c_str(), "RECREATE"), "");
0023   file_ = tFileDirectory_.file_;
0024 
0025   // activities to monitor in order to set the proper directory
0026   r.watchPreModuleConstruction(this, &TFileService::setDirectoryName);
0027   r.watchPreModuleBeginJob(this, &TFileService::setDirectoryName);
0028   r.watchPreModuleEndJob(this, &TFileService::setDirectoryName);
0029   r.watchPreModuleEvent(this, &TFileService::preModuleEvent);
0030   r.watchPostModuleEvent(this, &TFileService::postModuleEvent);
0031 
0032   r.watchPreModuleGlobalBeginRun(this, &TFileService::preModuleGlobal);
0033   r.watchPostModuleGlobalBeginRun(this, &TFileService::postModuleGlobal);
0034   r.watchPreModuleGlobalEndRun(this, &TFileService::preModuleGlobal);
0035   r.watchPostModuleGlobalEndRun(this, &TFileService::postModuleGlobal);
0036 
0037   r.watchPreModuleGlobalBeginLumi(this, &TFileService::preModuleGlobal);
0038   r.watchPostModuleGlobalBeginLumi(this, &TFileService::postModuleGlobal);
0039   r.watchPreModuleGlobalEndLumi(this, &TFileService::preModuleGlobal);
0040   r.watchPostModuleGlobalEndLumi(this, &TFileService::postModuleGlobal);
0041 
0042   // delay writing into JobReport after BeginJob
0043   r.watchPostBeginJob(this, &TFileService::afterBeginJob);
0044 }
0045 
0046 TFileService::~TFileService() {
0047   file_->Write();
0048   if (closeFileFast_)
0049     gROOT->GetListOfFiles()->Remove(file_);
0050   file_->Close();
0051   delete file_;
0052 }
0053 
0054 void TFileService::setDirectoryName(const edm::ModuleDescription& desc) {
0055   tFileDirectory_.file_ = file_;
0056   tFileDirectory_.dir_ = desc.moduleLabel();
0057   tFileDirectory_.descr_ = tFileDirectory_.dir_ + " (" + desc.moduleName() + ") folder";
0058 }
0059 
0060 void TFileService::preModuleEvent(edm::StreamContext const&, edm::ModuleCallingContext const& mcc) {
0061   setDirectoryName(*mcc.moduleDescription());
0062 }
0063 
0064 void TFileService::postModuleEvent(edm::StreamContext const&, edm::ModuleCallingContext const& mcc) {
0065   edm::ModuleCallingContext const* previous_mcc = mcc.previousModuleOnThread();
0066   if (previous_mcc) {
0067     setDirectoryName(*previous_mcc->moduleDescription());
0068   }
0069 }
0070 
0071 void TFileService::preModuleGlobal(edm::GlobalContext const&, edm::ModuleCallingContext const& mcc) {
0072   setDirectoryName(*mcc.moduleDescription());
0073 }
0074 
0075 void TFileService::postModuleGlobal(edm::GlobalContext const&, edm::ModuleCallingContext const& mcc) {
0076   edm::ModuleCallingContext const* previous_mcc = mcc.previousModuleOnThread();
0077   if (previous_mcc) {
0078     setDirectoryName(*previous_mcc->moduleDescription());
0079   }
0080 }
0081 
0082 void TFileService::afterBeginJob() {
0083   if (!fileName_.empty()) {
0084     if (!fileNameRecorded_) {
0085       std::string fullName(1024, '\0');
0086 
0087       while (getcwd(&fullName[0], fullName.size()) == nullptr) {
0088         if (errno != ERANGE) {
0089           throw cms::Exception("TFileService")
0090               << "Failed to get current directory (errno=" << errno << "): " << strerror(errno);
0091         }
0092         fullName.resize(fullName.size() * 2, '\0');
0093       }
0094       fullName.resize(fullName.find('\0'));
0095 
0096       fullName += "/" + fileName_;
0097 
0098       std::map<std::string, std::string> fileData;
0099       fileData.insert(std::make_pair("Source", "TFileService"));
0100 
0101       edm::Service<edm::JobReport> reportSvc;
0102       reportSvc->reportAnalysisFile(fullName, fileData);
0103       fileNameRecorded_ = true;
0104     }
0105   }
0106 }