Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-23 02:05:06

0001 #ifndef IOPool_Input_InputFile_h
0002 #define IOPool_Input_InputFile_h
0003 
0004 /*----------------------------------------------------------------------
0005 
0006 Holder for an input TFile.
0007 ----------------------------------------------------------------------*/
0008 #include "FWCore/MessageLogger/interface/JobReport.h"
0009 #include "FWCore/Utilities/interface/InputType.h"
0010 #include "FWCore/Utilities/interface/propagate_const.h"
0011 
0012 #include "TFile.h"
0013 #include "TTree.h"
0014 #include "TTreeCache.h"
0015 
0016 #include <map>
0017 #include <string>
0018 #include <vector>
0019 
0020 class TObject;
0021 
0022 namespace edm {
0023   class InputFile {
0024   public:
0025     explicit InputFile(char const* fileName, char const* msg, InputType inputType);
0026     ~InputFile();
0027 
0028     InputFile(InputFile const&) = delete;             // Disallow copying and moving
0029     InputFile& operator=(InputFile const&) = delete;  // Disallow copying and moving
0030 
0031     void Close();
0032     void inputFileOpened(std::string const& logicalFileName,
0033                          std::string const& inputType,
0034                          std::string const& moduleName,
0035                          std::string const& label,
0036                          std::string const& fid,
0037                          std::vector<std::string> const& branchNames);
0038     void eventReadFromFile() const;
0039     void reportInputRunNumber(unsigned int run) const;
0040     void reportInputLumiSection(unsigned int run, unsigned int lumi) const;
0041     static void reportSkippedFile(std::string const& fileName, std::string const& logicalFileName);
0042     static void reportFallbackAttempt(std::string const& pfn,
0043                                       std::string const& logicalFileName,
0044                                       std::string const& errorMessage);
0045     // reportReadBranches is a per job report, rather than per file report.
0046     // Nevertheless, it is defined here for convenience.
0047     static void reportReadBranches();
0048     static void reportReadBranch(InputType inputType, std::string const& branchname);
0049 
0050     TObject* Get(char const* name) { return file_->Get(name); }
0051     std::unique_ptr<TTreeCache> createCacheWithSize(TTree& iTree, unsigned int cacheSize) {
0052       iTree.SetCacheSize(static_cast<Long64_t>(cacheSize));
0053       std::unique_ptr<TTreeCache> newCache(dynamic_cast<TTreeCache*>(file_->GetCacheRead(&iTree)));
0054       file_->SetCacheRead(nullptr, &iTree, TFile::kDoNotDisconnect);
0055       return newCache;
0056     }
0057 
0058     class CacheGuard {
0059     public:
0060       CacheGuard(TFile* file, TObject* tree, TFileCacheRead* tfcr) : file_(file), tree_(tree) {
0061         file_->SetCacheRead(tfcr, tree_, TFile::kDoNotDisconnect);
0062       }
0063       CacheGuard() = delete;
0064       CacheGuard(CacheGuard const&) = delete;
0065       CacheGuard& operator=(CacheGuard const&) = delete;
0066       CacheGuard(CacheGuard&&) = delete;
0067       CacheGuard& operator=(CacheGuard&&) = delete;
0068       ~CacheGuard() { file_->SetCacheRead(nullptr, tree_, TFile::kDoNotDisconnect); }
0069 
0070     private:
0071       TFile* file_;
0072       TObject* tree_;
0073     };
0074     [[nodiscard]] CacheGuard setCacheReadTemporarily(TFileCacheRead* tfcr, TObject* iTree) {
0075       return CacheGuard(file_.get(), iTree, tfcr);
0076     }
0077     void clearCacheRead(TObject* iTree) { file_->SetCacheRead(nullptr, iTree, TFile::kDoNotDisconnect); }
0078     void logFileAction(char const* msg, char const* fileName) const;
0079 
0080   private:
0081     edm::propagate_const<std::unique_ptr<TFile>> file_;
0082     std::string fileName_;
0083     JobReport::Token reportToken_;
0084     InputType inputType_;
0085   };
0086 }  // namespace edm
0087 #endif