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 
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 ProductHolder,
86  // holding the Provenance for the corresponding EDProduct.
87  // 3. set up the caches in the EventPrincipal to know about this
88  // ProductHolder.
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 ? new InputSource::FileOpenSentry(*input, lfn_, usedFallback_) : nullptr);
238  filePtr = std::make_shared<InputFile>(gSystem->ExpandPathName(fileName().c_str()), " Initiating request to open file ", inputType);
239  }
240  catch (cms::Exception const& e) {
241  if(!skipBadFiles) {
242  if(hasFallbackUrl) {
243  std::ostringstream out;
244  out << e.explainSelf();
245  std::string pfn(gSystem->ExpandPathName(fallbackFileName().c_str()));
247  originalInfo = e.additionalInfo();
248  } else {
250  Exception ex(errors::FileOpenError, "", e);
251  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
252  std::ostringstream out;
253  out << "Input file " << fileName() << " could not be opened.";
254  ex.addAdditionalInfo(out.str());
255  throw ex;
256  }
257  }
258  }
259  if(!filePtr && (hasFallbackUrl)) {
260  try {
261  usedFallback_ = true;
262  std::unique_ptr<InputSource::FileOpenSentry> sentry(input ? new InputSource::FileOpenSentry(*input, lfn_, usedFallback_) : nullptr);
263  std::string fallbackFullName = gSystem->ExpandPathName(fallbackFileName().c_str());
264  filePtr.reset(new InputFile(fallbackFullName.c_str(), " Fallback request to file ", inputType));
265  }
266  catch (cms::Exception const& e) {
267  if(!skipBadFiles) {
270  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
271  std::ostringstream out;
272  out << "Input file " << fileName() << " could not be opened.\n";
273  out << "Fallback Input file " << fallbackFileName() << " also could not be opened.";
274  if (originalInfo.size()) {
275  out << std::endl << "Original exception info is above; fallback exception info is below.";
276  ex.addAdditionalInfo(out.str());
277  for (auto const & s : originalInfo) {
278  ex.addAdditionalInfo(s);
279  }
280  } else {
281  ex.addAdditionalInfo(out.str());
282  }
283  throw ex;
284  }
285  }
286  }
287  if(filePtr) {
288  size_t currentIndexIntoFile = fileIter_ - fileIterBegin_;
289  rootFile_ = makeRootFile(filePtr);
290  assert(rootFile_);
292  setIndexIntoFile(currentIndexIntoFile);
293  rootFile_->reportOpened(inputTypeName);
294  } else {
296  if(!skipBadFiles) {
298  "RootFileSequenceBase::initTheFile(): Input file " << fileName() << " was not found or could not be opened.\n";
299  }
300  LogWarning("") << "Input file: " << fileName() << " was not found or could not be opened, and will be skipped.\n";
301  }
302  }
303 
304  void
306  indexesIntoFiles_[index] = rootFile()->indexIntoFileSharedPtr();
307  }
308 
309 }
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
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: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
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
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_