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