Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:13

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       return;
0038     }
0039     if (file_->IsZombie()) {
0040       file_ = nullptr;  // propagate_const<T> has no reset() function
0041       return;
0042     }
0043 
0044     logFileAction("  Successfully opened file ", fileName);
0045   }
0046 
0047   InputFile::~InputFile() { Close(); }
0048 
0049   void InputFile::inputFileOpened(std::string const& logicalFileName,
0050                                   std::string const& inputType,
0051                                   std::string const& moduleName,
0052                                   std::string const& label,
0053                                   std::string const& fid,
0054                                   std::vector<std::string> const& branchNames) {
0055     Service<JobReport> reportSvc;
0056     reportToken_ = reportSvc->inputFileOpened(
0057         fileName_, logicalFileName, std::string(), inputType, moduleName, label, fid, branchNames);
0058   }
0059 
0060   void InputFile::eventReadFromFile() const {
0061     Service<JobReport> reportSvc;
0062     reportSvc->eventReadFromFile(inputType_, reportToken_);
0063   }
0064 
0065   void InputFile::reportInputRunNumber(unsigned int run) const {
0066     Service<JobReport> reportSvc;
0067     reportSvc->reportInputRunNumber(run);
0068   }
0069 
0070   void InputFile::reportInputLumiSection(unsigned int run, unsigned int lumi) const {
0071     Service<JobReport> reportSvc;
0072     reportSvc->reportInputLumiSection(run, lumi);
0073   }
0074 
0075   void InputFile::reportSkippedFile(std::string const& fileName, std::string const& logicalFileName) {
0076     Service<JobReport> reportSvc;
0077     reportSvc->reportSkippedFile(fileName, logicalFileName);
0078   }
0079 
0080   void InputFile::reportFallbackAttempt(std::string const& pfn,
0081                                         std::string const& logicalFileName,
0082                                         std::string const& errorMessage) {
0083     Service<JobReport> reportSvc;
0084     reportSvc->reportFallbackAttempt(pfn, logicalFileName, errorMessage);
0085   }
0086 
0087   void InputFile::Close() {
0088     if (file_->IsOpen()) {
0089       file_->Close();
0090       try {
0091         logFileAction("  Closed file ", fileName_.c_str());
0092         Service<JobReport> reportSvc;
0093         reportSvc->inputFileClosed(inputType_, reportToken_);
0094       } catch (std::exception const&) {
0095         // If Close() called in a destructor after an exception throw, the services may no longer be active.
0096         // Therefore, we catch any reasonable new exception.
0097       }
0098     }
0099   }
0100 
0101   void InputFile::logFileAction(char const* msg, char const* fileName) const {
0102     LogAbsolute("fileAction") << std::setprecision(0) << TimeOfDay() << msg << fileName;
0103     FlushMessageLog();
0104   }
0105 
0106   void InputFile::reportReadBranches() {
0107     Service<JobReport> reportSvc;
0108     reportSvc->reportReadBranches();
0109   }
0110 
0111   void InputFile::reportReadBranch(InputType inputType, std::string const& branchName) {
0112     Service<JobReport> reportSvc;
0113     reportSvc->reportReadBranch(inputType, branchName);
0114   }
0115 }  // namespace edm