CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 ProductHolder,
77  // holding the Provenance for the corresponding EDProduct.
78  // 3. set up the caches in the EventPrincipal to know about this
79  // ProductHolder.
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(input ? new InputSource::FileOpenSentry(*input, lfn_, usedFallback_) : nullptr);
232  if (service.isAvailable()) {
233  service->openingFile(lfn(), inputType, -1);
234  }
235  try {
236  filePtr = std::make_shared<InputFile>(gSystem->ExpandPathName(fileName().c_str()), " Initiating request to open file ", inputType);
237  } catch (cms::Exception const& e) {
238  if (!skipBadFiles) {
239  if (hasFallbackUrl) {
240  std::ostringstream out;
241  out << e.explainSelf();
242  std::string pfn(gSystem->ExpandPathName(fallbackFileName().c_str()));
244  originalInfo = e.additionalInfo();
245  } else {
247  Exception ex(errors::FileOpenError, "", e);
248  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
249  std::ostringstream out;
250  out << "Input file " << fileName() << " could not be opened.";
251  ex.addAdditionalInfo(out.str());
252  throw ex;
253  }
254  }
255  }
256  if (!filePtr && (hasFallbackUrl)) {
257  try {
258  usedFallback_ = true;
259  std::string fallbackFullName = gSystem->ExpandPathName(fallbackFileName().c_str());
260  filePtr.reset(new InputFile(fallbackFullName.c_str(), " 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.size()) {
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  }
283  if (filePtr) {
284  size_t currentIndexIntoFile = fileIter_ - fileIterBegin_;
285  rootFile_ = makeRootFile(filePtr);
286  assert(rootFile_);
288  setIndexIntoFile(currentIndexIntoFile);
289  rootFile_->reportOpened(inputTypeName);
290  } else {
292  if (!skipBadFiles) {
293  throw Exception(errors::FileOpenError) << "RootFileSequenceBase::initTheFile(): Input file " << fileName()
294  << " was not found or could not be opened.\n";
295  }
296  LogWarning("") << "Input file: " << fileName() << " was not found or could not be opened, and will be skipped.\n";
297  }
298  }
299 
302  if (rootFile() and service.isAvailable()) {
303  service->closedFile(lfn(), usedFallback());
304  }
305  closeFile_();
306  }
307 
309  indexesIntoFiles_[index] = rootFile()->indexIntoFileSharedPtr();
310  }
311 
312 } // namespace edm
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_
tuple lumi
Definition: fjr2json.py:35
std::vector< FileCatalogItem >::const_iterator const fileIterBegin_
assert(m_qm.get())
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
tuple InputFile
Open Root file and provide MEs ############.
static void reportFallbackAttempt(std::string const &pfn, std::string const &logicalFileName, std::string const &errorMessage)
Definition: InputFile.cc:86
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:46
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:235
static void reportSkippedFile(std::string const &fileName, std::string const &logicalFileName)
Definition: InputFile.cc:80
virtual RootFileSharedPtr makeRootFile(std::shared_ptr< InputFile > filePtr)=0
#define end
Definition: vmac.h:37
virtual void initFile_(bool skipBadFiles)=0
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
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:227
tuple skipBadFiles
Definition: example_cfg.py:64
#define begin
Definition: vmac.h:30
std::shared_ptr< LuminosityBlockAuxiliary > readLuminosityBlockAuxiliary_()
virtual void closeFile_()=0
RootInputFileSequence(ParameterSet const &pset, InputFileCatalog const &catalog)
unsigned int RunNumber_t
volatile std::atomic< bool > shutdown_flag false
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_
tuple size
Write out results.
std::vector< FileCatalogItem > const & fileCatalogItems() const
std::string const & fallbackFileName() const
InputFileCatalog const & catalog_