Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:05

0001 /*----------------------------------------------------------------------
0002 Holder for an input TFile.
0003 ----------------------------------------------------------------------*/
0004 #include "TList.h"
0005 #include "TStreamerInfo.h"
0006 #include "TClass.h"
0007 #include "InputFile.h"
0008 
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "FWCore/ServiceRegistry/interface/Service.h"
0011 #include "FWCore/Utilities/interface/ExceptionPropagate.h"
0012 #include "FWCore/Utilities/interface/TimeOfDay.h"
0013 
0014 #include <exception>
0015 #include <iomanip>
0016 
0017 namespace edm {
0018   InputFile::InputFile(char const* fileName, char const* msg, InputType inputType)
0019       : file_(), fileName_(fileName), reportToken_(0), inputType_(inputType) {
0020     logFileAction(msg, fileName);
0021     {
0022       // ROOT's context management implicitly assumes that a file is opened and
0023       // closed on the same thread.  To avoid the problem, we declare a local
0024       // TContext object; when it goes out of scope, its destructor unregisters
0025       // the context, guaranteeing the context is unregistered in the same thread
0026       // it was registered in.  Fixes issue #15524.
0027       TDirectory::TContext contextEraser;
0028 
0029       file_ = std::unique_ptr<TFile>(TFile::Open(fileName));  // propagate_const<T> has no reset() function
0030     }
0031     std::exception_ptr e = edm::threadLocalException::getException();
0032     if (e != std::exception_ptr()) {
0033       edm::threadLocalException::setException(std::exception_ptr());
0034       std::rethrow_exception(e);
0035     }
0036     if (!file_) {
0037       throw edm::Exception(errors::FileOpenError) << "TFile::Open failed.";
0038     }
0039     if (file_->IsZombie()) {
0040       throw edm::Exception(errors::FileOpenError) << "TFile::Open returned zombie.";
0041     }
0042 
0043     logFileAction("  Successfully opened file ", fileName);
0044   }
0045 
0046   InputFile::~InputFile() { Close(); }
0047 
0048   void InputFile::inputFileOpened(std::string const& logicalFileName,
0049                                   std::string const& inputType,
0050                                   std::string const& moduleName,
0051                                   std::string const& label,
0052                                   std::string const& fid,
0053                                   std::vector<std::string> const& branchNames) {
0054     Service<JobReport> reportSvc;
0055     reportToken_ = reportSvc->inputFileOpened(
0056         fileName_, logicalFileName, std::string(), inputType, moduleName, label, fid, branchNames);
0057   }
0058 
0059   void InputFile::eventReadFromFile() const {
0060     Service<JobReport> reportSvc;
0061     reportSvc->eventReadFromFile(inputType_, reportToken_);
0062   }
0063 
0064   void InputFile::reportInputRunNumber(unsigned int run) const {
0065     Service<JobReport> reportSvc;
0066     reportSvc->reportInputRunNumber(run);
0067   }
0068 
0069   void InputFile::reportInputLumiSection(unsigned int run, unsigned int lumi) const {
0070     Service<JobReport> reportSvc;
0071     reportSvc->reportInputLumiSection(run, lumi);
0072   }
0073 
0074   void InputFile::reportSkippedFile(std::string const& fileName, std::string const& logicalFileName) {
0075     Service<JobReport> reportSvc;
0076     reportSvc->reportSkippedFile(fileName, logicalFileName);
0077   }
0078 
0079   void InputFile::reportFallbackAttempt(std::string const& pfn,
0080                                         std::string const& logicalFileName,
0081                                         std::string const& errorMessage) {
0082     Service<JobReport> reportSvc;
0083     reportSvc->reportFallbackAttempt(pfn, logicalFileName, errorMessage);
0084   }
0085 
0086   void InputFile::Close() {
0087     if (file_->IsOpen()) {
0088       file_->Close();
0089       try {
0090         logFileAction("  Closed file ", fileName_.c_str());
0091         Service<JobReport> reportSvc;
0092         reportSvc->inputFileClosed(inputType_, reportToken_);
0093       } catch (std::exception const&) {
0094         // If Close() called in a destructor after an exception throw, the services may no longer be active.
0095         // Therefore, we catch any reasonable new exception.
0096       }
0097     }
0098   }
0099 
0100   void InputFile::logFileAction(char const* msg, char const* fileName) const {
0101     LogAbsolute("fileAction") << std::setprecision(0) << TimeOfDay() << msg << fileName;
0102     FlushMessageLog();
0103   }
0104 
0105   void InputFile::reportReadBranches() {
0106     Service<JobReport> reportSvc;
0107     reportSvc->reportReadBranches();
0108   }
0109 
0110   void InputFile::reportReadBranch(InputType inputType, std::string const& branchName) {
0111     Service<JobReport> reportSvc;
0112     reportSvc->reportReadBranch(inputType, branchName);
0113   }
0114 }  // namespace edm