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