CMS 3D CMS Logo

TFileService.cc

Go to the documentation of this file.
00001 #include "PhysicsTools/UtilAlgos/interface/TFileService.h"
00002 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00003 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
00004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00005 #include "FWCore/ServiceRegistry/interface/Service.h"
00006 #include "FWCore/MessageLogger/interface/JobReport.h"
00007 #include "TFile.h"
00008 #include "TROOT.h"
00009 
00010 using namespace edm;
00011 using namespace std;
00012 
00013 TFileService::TFileService(const ParameterSet & cfg, ActivityRegistry & r) :
00014   TFileDirectory("", "", new TFile(cfg.getParameter<string>("fileName").c_str() , "RECREATE"), ""),
00015   file_(TFileDirectory::file_),
00016   fileName_(cfg.getParameter<string>("fileName")),
00017   fileNameRecorded_(false),
00018   closeFileFast_(cfg.getUntrackedParameter<bool>("closeFileFast", false)) 
00019 {
00020   // activities to monitor in order to set the proper directory
00021   r.watchPreModuleConstruction(this, & TFileService::setDirectoryName);
00022   r.watchPreModule(this, & TFileService::setDirectoryName);
00023   r.watchPreModuleBeginJob(this, & TFileService::setDirectoryName);
00024   r.watchPreModuleEndJob(this, & TFileService::setDirectoryName);
00025   r.watchPreModuleBeginRun(this, & TFileService::setDirectoryName);
00026   r.watchPreModuleEndRun(this, & TFileService::setDirectoryName);
00027   r.watchPreModuleBeginLumi(this, & TFileService::setDirectoryName);
00028   r.watchPreModuleEndLumi(this, & TFileService::setDirectoryName);
00029   // delay writing into JobReport after BeginJob
00030   r.watchPostBeginJob(this,&TFileService::afterBeginJob);
00031 }
00032 
00033 TFileService::~TFileService() {
00034   file_->Write();
00035   if(closeFileFast_) gROOT->GetListOfFiles()->Remove(file_); 
00036   file_->Close();
00037   delete file_;
00038 }
00039 
00040 void TFileService::setDirectoryName(const ModuleDescription & desc) {
00041   dir_ = desc.moduleLabel_;
00042   descr_ = (dir_ + " (" + desc.moduleName_ + ") folder").c_str();
00043 }
00044 
00045 void TFileService::afterBeginJob() {
00046 
00047   if(!fileName_.empty())  {
00048     if(!fileNameRecorded_) {
00049       string fullName;
00050       fullName.reserve(1024);
00051       fullName = getcwd(&fullName[0],1024);
00052       fullName += "/" + fileName_;
00053 
00054       map<string,string> fileData;
00055       fileData.insert(make_pair("Source","TFileService"));
00056 
00057       Service<JobReport> reportSvc;
00058       reportSvc->reportAnalysisFile(fullName,fileData);
00059       fileNameRecorded_ = true;
00060     }
00061   }
00062 }

Generated on Tue Jun 9 17:42:41 2009 for CMSSW by  doxygen 1.5.4