CMS 3D CMS Logo

InputFile.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 Holder for an input TFile.
3 ----------------------------------------------------------------------*/
4 #include "TList.h"
5 #include "TStreamerInfo.h"
6 #include "TClass.h"
7 #include "InputFile.h"
8 
13 
14 #include <exception>
15 #include <iomanip>
16 
17 namespace edm {
18  InputFile::InputFile(char const* fileName, char const* msg, InputType inputType) :
19  file_(), fileName_(fileName), reportToken_(0), inputType_(inputType) {
20 
21  // ROOT's context management implicitly assumes that a file is opened and
22  // closed on the same thread. To avoid the problem, we declare a local
23  // TContext object; when it goes out of scope, its destructor unregisters
24  // the context, guaranteeing the context is unregistered in the same thread
25  // it was registered in. Fixes issue #15524.
26  TDirectory::TContext contextEraser;
27 
28  logFileAction(msg, fileName);
29  file_ = std::unique_ptr<TFile>(TFile::Open(fileName)); // propagate_const<T> has no reset() function
30  std::exception_ptr e = edm::threadLocalException::getException();
31  if(e != std::exception_ptr()) {
32  edm::threadLocalException::setException(std::exception_ptr());
33  std::rethrow_exception(e);
34  }
35  if(!file_) {
36  return;
37  }
38  if(file_->IsZombie()) {
39  file_ = nullptr; // propagate_const<T> has no reset() function
40  return;
41  }
42 
43  logFileAction(" Successfully opened file ", fileName);
44  }
45 
47  Close();
48  }
49 
50  void
51  InputFile::inputFileOpened(std::string const& logicalFileName,
52  std::string const& inputType,
53  std::string const& moduleName,
54  std::string const& label,
55  std::string const& fid,
56  std::vector<std::string> const& branchNames) {
57  Service<JobReport> reportSvc;
58  reportToken_ = reportSvc->inputFileOpened(fileName_,
59  logicalFileName,
60  std::string(),
61  inputType,
62  moduleName,
63  label,
64  fid,
65  branchNames);
66  }
67 
68  void
70  Service<JobReport> reportSvc;
71  reportSvc->eventReadFromFile(inputType_, reportToken_);
72  }
73 
74  void
75  InputFile::reportInputRunNumber(unsigned int run) const {
76  Service<JobReport> reportSvc;
77  reportSvc->reportInputRunNumber(run);
78  }
79 
80  void
81  InputFile::reportInputLumiSection(unsigned int run, unsigned int lumi) const {
82  Service<JobReport> reportSvc;
83  reportSvc->reportInputLumiSection(run, lumi);
84  }
85 
86  void
88  Service<JobReport> reportSvc;
89  reportSvc->reportSkippedFile(fileName, logicalFileName);
90  }
91 
92  void
93  InputFile::reportFallbackAttempt(std::string const& pfn, std::string const& logicalFileName, std::string const& errorMessage) {
94  Service<JobReport> reportSvc;
95  reportSvc->reportFallbackAttempt(pfn, logicalFileName, errorMessage);
96  }
97 
98  void
100  if(file_->IsOpen()) {
101  file_->Close();
102  try {
103  logFileAction(" Closed file ", fileName_.c_str());
104  Service<JobReport> reportSvc;
105  reportSvc->inputFileClosed(inputType_, reportToken_);
106  } catch(std::exception) {
107  // If Close() called in a destructor after an exception throw, the services may no longer be active.
108  // Therefore, we catch any reasonable new exception.
109  }
110  }
111  }
112 
113  void
114  InputFile::logFileAction(char const* msg, char const* fileName) const {
115  LogAbsolute("fileAction") << std::setprecision(0) << TimeOfDay() << msg << fileName;
116  FlushMessageLog();
117  }
118 
119  void
121  Service<JobReport> reportSvc;
122  reportSvc->reportReadBranches();
123  }
124 
125  void
126  InputFile::reportReadBranch(InputType inputType, std::string const& branchName) {
127  Service<JobReport> reportSvc;
128  reportSvc->reportReadBranch(inputType, branchName);
129  }
130 }
void Close()
Definition: InputFile.cc:99
InputType
Definition: InputType.h:5
void FlushMessageLog()
JobReport::Token reportToken_
Definition: InputFile.h:53
InputType inputType_
Definition: InputFile.h:54
void inputFileOpened(std::string const &logicalFileName, std::string const &inputType, std::string const &moduleName, std::string const &label, std::string const &fid, std::vector< std::string > const &branchNames)
Definition: InputFile.cc:51
static void reportReadBranch(InputType inputType, std::string const &branchname)
Definition: InputFile.cc:126
void reportInputRunNumber(unsigned int run) const
Definition: InputFile.cc:75
void logFileAction(char const *msg, char const *fileName) const
Definition: InputFile.cc:114
static void reportFallbackAttempt(std::string const &pfn, std::string const &logicalFileName, std::string const &errorMessage)
Definition: InputFile.cc:93
std::string moduleName(Provenance const &provenance)
Definition: Provenance.cc:27
static void reportReadBranches()
Definition: InputFile.cc:120
void reportInputLumiSection(unsigned int run, unsigned int lumi) const
Definition: InputFile.cc:81
static void reportSkippedFile(std::string const &fileName, std::string const &logicalFileName)
Definition: InputFile.cc:87
edm::propagate_const< std::unique_ptr< TFile > > file_
Definition: InputFile.h:51
std::string fileName_
Definition: InputFile.h:52
void setException(std::exception_ptr e)
HLT enums.
std::exception_ptr getException()
InputFile(char const *fileName, char const *msg, InputType inputType)
Definition: InputFile.cc:18
void eventReadFromFile() const
Definition: InputFile.cc:69