CMS 3D CMS Logo

RootInputFileSequence.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 ----------------------------------------------------------------------*/
3 #include "RootFile.h"
5 
13 
14 #include "TSystem.h"
15 
16 namespace edm {
17  class BranchIDListHelper;
18  class EventPrincipal;
19  class LuminosityBlockPrincipal;
20  class RunPrincipal;
21 
23  : catalog_(catalog),
24  lfn_("unknown"),
25  lfnHash_(0U),
26  usedFallback_(false),
27  findFileForSpecifiedID_(nullptr),
28  fileIterBegin_(fileCatalogItems().begin()),
29  fileIterEnd_(fileCatalogItems().end()),
30  fileIter_(fileIterEnd_),
31  fileIterLastOpened_(fileIterEnd_),
32  rootFile_(),
33  indexesIntoFiles_(fileCatalogItems().size()) {}
34 
35  std::vector<FileCatalogItem> const& RootInputFileSequence::fileCatalogItems() const {
36  return catalog_.fileCatalogItems();
37  }
38 
39  std::shared_ptr<ProductRegistry const> RootInputFileSequence::fileProductRegistry() const {
40  assert(rootFile());
41  return rootFile()->productRegistry();
42  }
43 
44  std::shared_ptr<BranchIDListHelper const> RootInputFileSequence::fileBranchIDListHelper() const {
45  assert(rootFile());
46  return rootFile()->branchIDListHelper();
47  }
48 
50 
51  std::shared_ptr<RunAuxiliary> RootInputFileSequence::readRunAuxiliary_() {
52  assert(rootFile());
53  return rootFile()->readRunAuxiliary_();
54  }
55 
56  std::shared_ptr<LuminosityBlockAuxiliary> RootInputFileSequence::readLuminosityBlockAuxiliary_() {
57  assert(rootFile());
58  return rootFile()->readLuminosityBlockAuxiliary_();
59  }
60 
62  assert(rootFile());
63  rootFile()->readRun_(runPrincipal);
64  }
65 
67  assert(rootFile());
68  rootFile()->readLuminosityBlock_(lumiPrincipal);
69  }
70 
71  // readEvent() is responsible for setting up the EventPrincipal.
72  //
73  // 1. fill an EventPrincipal with a unique EventID
74  // 2. For each entry in the provenance, put in one ProductResolver,
75  // holding the Provenance for the corresponding EDProduct.
76  // 3. set up the caches in the EventPrincipal to know about this
77  // ProductResolver.
78  //
79  // We do *not* create the EDProduct instance (the equivalent of reading
80  // the branch containing this EDProduct. That will be done by the Delayed Reader,
81  // when it is asked to do so.
82  //
83 
85  assert(rootFile());
86  rootFile()->readEvent(eventPrincipal);
87  }
88 
91  EventNumber_t event) const {
92  if (!rootFile())
93  return false;
94  return rootFile()->containsItem(run, lumi, event);
95  }
96 
100  size_t fileNameHash) {
101  // Look for item in files not yet opened. We have a hash of the logical file name
102  assert(fileNameHash != 0U);
103  // If the lookup table is not yet filled in, fill it.
105  // We use a multimap because there may be hash collisions (Two different LFNs could have the same hash).
106  // We map the hash of the LFN to the index into the list of files.
108  std::make_unique<std::unordered_multimap<size_t, size_t>>(); // propagate_const<T> has no reset() function
109  auto hasher = std::hash<std::string>();
110  for (auto fileIter = fileIterBegin_; fileIter != fileIterEnd_; ++fileIter) {
111  findFileForSpecifiedID_->insert(std::make_pair(hasher(fileIter->logicalFileName()), fileIter - fileIterBegin_));
112  }
113  }
114  // Look up the logical file name in the table
115  auto range = findFileForSpecifiedID_->equal_range(fileNameHash);
116  for (auto iter = range.first; iter != range.second; ++iter) {
117  // Don't look in files previously opened, because those have already been searched.
118  if (!indexesIntoFiles_[iter->second]) {
119  setAtFileSequenceNumber(iter->second);
120  initFile_(false);
121  assert(rootFile());
122  bool found = rootFile()->setEntryAtItem(run, lumi, event);
123  if (found) {
124  return true;
125  }
126  }
127  }
128  // Not found
129  return false;
130  }
131 
133  // Look for item in files not yet opened. We do not have a valid hash of the logical file name.
134  for (auto it = indexesIntoFiles_.begin(), itEnd = indexesIntoFiles_.end(); it != itEnd; ++it) {
135  if (!*it) {
136  // File not yet opened.
138  initFile_(false);
139  assert(rootFile());
140  bool found = rootFile()->setEntryAtItem(run, lumi, event);
141  if (found) {
142  return true;
143  }
144  }
145  }
146  // Not found
147  return false;
148  }
149 
151  RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, size_t fileNameHash, bool currentFileFirst) {
152  // Attempt to find item in currently open input file.
153  bool found = currentFileFirst && rootFile() && rootFile()->setEntryAtItem(run, lumi, event);
154  if (!found) {
155  // If only one input file, give up now, to save time.
156  if (currentFileFirst && rootFile() && indexesIntoFiles_.size() == 1) {
157  return false;
158  }
159  // Look for item (run/lumi/event) in files previously opened without reopening unnecessary files.
160  for (auto it = indexesIntoFiles_.begin(), itEnd = indexesIntoFiles_.end(); it != itEnd; ++it) {
161  if (*it && (*it)->containsItem(run, lumi, event)) {
162  // We found it. Close the currently open file, and open the correct one.
163  std::vector<FileCatalogItem>::const_iterator currentIter = fileIter_;
165  if (fileIter_ != currentIter) {
166  initFile(false);
167  }
168  // Now get the item from the correct file.
169  assert(rootFile());
170  found = rootFile()->setEntryAtItem(run, lumi, event);
171  assert(found);
172  return true;
173  }
174  }
175  return (fileNameHash != 0U && skipToItemInNewFile(run, lumi, event, fileNameHash)) ||
176  skipToItemInNewFile(run, lumi, event);
177  }
178  return true;
179  }
180 
182  bool skipBadFiles, bool deleteIndexIntoFile, InputSource* input, char const* inputTypeName, InputType inputType) {
183  // We are really going to close the open file.
184 
186  size_t currentIndexIntoFile = fileIterLastOpened_ - fileIterBegin_;
187  if (deleteIndexIntoFile) {
188  indexesIntoFiles_[currentIndexIntoFile].reset();
189  } else {
190  if (indexesIntoFiles_[currentIndexIntoFile])
191  indexesIntoFiles_[currentIndexIntoFile]->inputFileClosed();
192  }
194  }
195  closeFile_();
196 
197  if (noMoreFiles()) {
198  // No files specified
199  return;
200  }
201 
202  // Check if the logical file name was found.
203  if (fileName().empty()) {
204  // LFN not found in catalog.
206  if (!skipBadFiles) {
207  throw cms::Exception("LogicalFileNameNotFound", "RootFileSequenceBase::initTheFile()\n")
208  << "Logical file name '" << logicalFileName() << "' was not found in the file catalog.\n"
209  << "If you wanted a local file, you forgot the 'file:' prefix\n"
210  << "before the file name in your configuration file.\n";
211  }
212  LogWarning("") << "Input logical file: " << logicalFileName()
213  << " was not found in the catalog, and will be skipped.\n";
214  return;
215  }
216 
217  lfn_ = logicalFileName().empty() ? fileName() : logicalFileName();
218  lfnHash_ = std::hash<std::string>()(lfn_);
219  usedFallback_ = false;
220 
221  // Determine whether we have a fallback URL specified; if so, prepare it;
222  // Only valid if it is non-empty and differs from the original filename.
223  bool hasFallbackUrl = !fallbackFileName().empty() && fallbackFileName() != fileName();
224 
225  std::shared_ptr<InputFile> filePtr;
226  std::list<std::string> originalInfo;
227  try {
228  std::unique_ptr<InputSource::FileOpenSentry> sentry(
229  input ? std::make_unique<InputSource::FileOpenSentry>(*input, lfn_, usedFallback_) : nullptr);
230  std::unique_ptr<char[]> name(gSystem->ExpandPathName(fileName().c_str()));
231  ;
232  filePtr = std::make_shared<InputFile>(name.get(), " Initiating request to open file ", inputType);
233  } catch (cms::Exception const& e) {
234  if (!skipBadFiles) {
235  if (hasFallbackUrl) {
236  std::ostringstream out;
237  out << e.explainSelf();
238 
239  std::unique_ptr<char[]> name(gSystem->ExpandPathName(fallbackFileName().c_str()));
240  std::string pfn(name.get());
242  originalInfo = e.additionalInfo();
243  } else {
245  Exception ex(errors::FileOpenError, "", e);
246  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
247  std::ostringstream out;
248  out << "Input file " << fileName() << " could not be opened.";
249  ex.addAdditionalInfo(out.str());
250  throw ex;
251  }
252  }
253  }
254  if (!filePtr && (hasFallbackUrl)) {
255  try {
256  usedFallback_ = true;
257  std::unique_ptr<InputSource::FileOpenSentry> sentry(
258  input ? std::make_unique<InputSource::FileOpenSentry>(*input, lfn_, usedFallback_) : nullptr);
259  std::unique_ptr<char[]> fallbackFullName(gSystem->ExpandPathName(fallbackFileName().c_str()));
260  filePtr.reset(new InputFile(fallbackFullName.get(), " Fallback request to file ", inputType));
261  } catch (cms::Exception const& e) {
262  if (!skipBadFiles) {
265  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
266  std::ostringstream out;
267  out << "Input file " << fileName() << " could not be opened.\n";
268  out << "Fallback Input file " << fallbackFileName() << " also could not be opened.";
269  if (!originalInfo.empty()) {
270  out << std::endl << "Original exception info is above; fallback exception info is below.";
271  ex.addAdditionalInfo(out.str());
272  for (auto const& s : originalInfo) {
273  ex.addAdditionalInfo(s);
274  }
275  } else {
276  ex.addAdditionalInfo(out.str());
277  }
278  throw ex;
279  }
280  }
281  }
282  if (filePtr) {
283  size_t currentIndexIntoFile = fileIter_ - fileIterBegin_;
284  rootFile_ = makeRootFile(filePtr);
285  if (input) {
287  }
288  assert(rootFile_);
290  setIndexIntoFile(currentIndexIntoFile);
291  rootFile_->reportOpened(inputTypeName);
292  } else {
294  if (!skipBadFiles) {
295  throw Exception(errors::FileOpenError) << "RootFileSequenceBase::initTheFile(): Input file " << fileName()
296  << " was not found or could not be opened.\n";
297  }
298  LogWarning("") << "Input file: " << fileName() << " was not found or could not be opened, and will be skipped.\n";
299  }
300  }
301 
303  indexesIntoFiles_[index] = rootFile()->indexIntoFileSharedPtr();
304  }
305 
306 } // namespace edm
size
Write out results.
std::string const & logicalFileName() const
InputType
Definition: InputType.h:5
void initFile(bool skipBadFiles)
std::string const & fileName() const
void setAtFileSequenceNumber(size_t offset)
virtual std::string explainSelf() const
Definition: Exception.cc:108
std::vector< FileCatalogItem >::const_iterator fileIter_
std::vector< FileCatalogItem >::const_iterator const fileIterEnd_
std::vector< FileCatalogItem >::const_iterator const fileIterBegin_
#define nullptr
std::shared_ptr< BranchIDListHelper const > fileBranchIDListHelper() const
unsigned long long EventNumber_t
unsigned int LuminosityBlockNumber_t
std::list< std::string > const & additionalInfo() const
Definition: Exception.cc:149
static std::string const input
Definition: EdmProvDump.cc:48
static void reportFallbackAttempt(std::string const &pfn, std::string const &logicalFileName, std::string const &errorMessage)
Definition: InputFile.cc:80
std::vector< FileCatalogItem >::const_iterator fileIterLastOpened_
std::vector< FileCatalogItem > const & fileCatalogItems() const
void initTheFile(bool skipBadFiles, bool deleteIndexIntoFile, InputSource *input, char const *inputTypeName, InputType inputType)
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:169
static void reportSkippedFile(std::string const &fileName, std::string const &logicalFileName)
Definition: InputFile.cc:75
virtual RootFileSharedPtr makeRootFile(std::shared_ptr< InputFile > filePtr)=0
#define end
Definition: vmac.h:39
virtual void initFile_(bool skipBadFiles)=0
void readRun_(RunPrincipal &runPrincipal)
std::vector< std::shared_ptr< IndexIntoFile > > indexesIntoFiles_
void readLuminosityBlock_(LuminosityBlockPrincipal &lumiPrincipal)
std::shared_ptr< RootFile const > rootFile() const
bool skipToItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, size_t fileNameHash=0U, bool currentFileFirst=true)
edm::propagate_const< RootFileSharedPtr > rootFile_
bool containedInCurrentFile(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event) const
bool skipToItemInNewFile(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event)
std::shared_ptr< RunAuxiliary > readRunAuxiliary_()
void addContext(std::string const &context)
Definition: Exception.cc:165
#define begin
Definition: vmac.h:32
HLT enums.
std::shared_ptr< LuminosityBlockAuxiliary > readLuminosityBlockAuxiliary_()
virtual void closeFile_()=0
RootInputFileSequence(ParameterSet const &pset, InputFileCatalog const &catalog)
unsigned int RunNumber_t
void readEvent(EventPrincipal &cache)
std::shared_ptr< ProductRegistry const > fileProductRegistry() const
edm::propagate_const< std::unique_ptr< std::unordered_multimap< size_t, size_t > > > findFileForSpecifiedID_
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> preEventReadFromSourceSignal_
Definition: InputSource.h:317
signalslot::Signal< void(StreamContext const &, ModuleCallingContext const &)> postEventReadFromSourceSignal_
Definition: InputSource.h:318
Definition: event.py:1
std::vector< FileCatalogItem > const & fileCatalogItems() const
std::string const & fallbackFileName() const
InputFileCatalog const & catalog_