CMS 3D CMS Logo

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