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_.reset(new std::unordered_multimap<size_t, size_t>);
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  typedef std::vector<std::shared_ptr<IndexIntoFile> >::const_iterator Iter;
143  for(Iter it = indexesIntoFiles_.begin(), itEnd = indexesIntoFiles_.end(); it != itEnd; ++it) {
144  if(!*it) {
145  // File not yet opened.
147  initFile_(false);
148  assert(rootFile());
149  bool found = rootFile()->setEntryAtItem(run, lumi, event);
150  if(found) {
151  return true;
152  }
153  }
154  }
155  // Not found
156  return false;
157  }
158 
159  bool
161  // Attempt to find item in currently open input file.
162  bool found = currentFileFirst && rootFile() && rootFile()->setEntryAtItem(run, lumi, event);
163  if(!found) {
164  // If only one input file, give up now, to save time.
165  if(currentFileFirst && rootFile() && indexesIntoFiles_.size() == 1) {
166  return false;
167  }
168  // Look for item (run/lumi/event) in files previously opened without reopening unnecessary files.
169  typedef std::vector<std::shared_ptr<IndexIntoFile> >::const_iterator Iter;
170  for(Iter it = indexesIntoFiles_.begin(), itEnd = indexesIntoFiles_.end(); it != itEnd; ++it) {
171  if(*it && (*it)->containsItem(run, lumi, event)) {
172  // We found it. Close the currently open file, and open the correct one.
173  std::vector<FileCatalogItem>::const_iterator currentIter = fileIter_;
175  if(fileIter_ != currentIter) {
176  initFile(false);
177  }
178  // Now get the item from the correct file.
179  assert(rootFile());
180  found = rootFile()->setEntryAtItem(run, lumi, event);
181  assert(found);
182  return true;
183  }
184  }
185  return (fileNameHash != 0U && skipToItemInNewFile(run, lumi, event, fileNameHash)) || skipToItemInNewFile(run, lumi, event);
186  }
187  return true;
188  }
189 
190  void
192  bool deleteIndexIntoFile,
193  InputSource* input,
194  char const* inputTypeName,
195  InputType inputType) {
196  // We are really going to close the open file.
197 
199  size_t currentIndexIntoFile = fileIterLastOpened_ - fileIterBegin_;
200  if(deleteIndexIntoFile) {
201  indexesIntoFiles_[currentIndexIntoFile].reset();
202  } else {
203  if(indexesIntoFiles_[currentIndexIntoFile]) indexesIntoFiles_[currentIndexIntoFile]->inputFileClosed();
204  }
206  }
207  closeFile_();
208 
209  if(noMoreFiles()) {
210  // No files specified
211  return;
212  }
213 
214  // Check if the logical file name was found.
215  if(fileName().empty()) {
216  // LFN not found in catalog.
218  if(!skipBadFiles) {
219  throw cms::Exception("LogicalFileNameNotFound", "RootFileSequenceBase::initTheFile()\n")
220  << "Logical file name '" << logicalFileName() << "' was not found in the file catalog.\n"
221  << "If you wanted a local file, you forgot the 'file:' prefix\n"
222  << "before the file name in your configuration file.\n";
223  }
224  LogWarning("") << "Input logical file: " << logicalFileName() << " was not found in the catalog, and will be skipped.\n";
225  return;
226  }
227 
228  lfn_ = logicalFileName().empty() ? fileName() : logicalFileName();
229  lfnHash_ = std::hash<std::string>()(lfn_);
230  usedFallback_ = false;
231 
232  // Determine whether we have a fallback URL specified; if so, prepare it;
233  // Only valid if it is non-empty and differs from the original filename.
234  bool hasFallbackUrl = !fallbackFileName().empty() && fallbackFileName() != fileName();
235 
236  std::shared_ptr<InputFile> filePtr;
237  std::list<std::string> originalInfo;
238  try {
239  std::unique_ptr<InputSource::FileOpenSentry> sentry(input ? new InputSource::FileOpenSentry(*input, lfn_, usedFallback_) : nullptr);
240  filePtr = std::make_shared<InputFile>(gSystem->ExpandPathName(fileName().c_str()), " Initiating request to open file ", inputType);
241  }
242  catch (cms::Exception const& e) {
243  if(!skipBadFiles) {
244  if(hasFallbackUrl) {
245  std::ostringstream out;
246  out << e.explainSelf();
247  std::string pfn(gSystem->ExpandPathName(fallbackFileName().c_str()));
249  originalInfo = e.additionalInfo();
250  } else {
252  Exception ex(errors::FileOpenError, "", e);
253  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
254  std::ostringstream out;
255  out << "Input file " << fileName() << " could not be opened.";
256  ex.addAdditionalInfo(out.str());
257  throw ex;
258  }
259  }
260  }
261  if(!filePtr && (hasFallbackUrl)) {
262  try {
263  usedFallback_ = true;
264  std::unique_ptr<InputSource::FileOpenSentry> sentry(input ? new InputSource::FileOpenSentry(*input, lfn_, usedFallback_) : nullptr);
265  std::string fallbackFullName = gSystem->ExpandPathName(fallbackFileName().c_str());
266  filePtr.reset(new InputFile(fallbackFullName.c_str(), " Fallback request to file ", inputType));
267  }
268  catch (cms::Exception const& e) {
269  if(!skipBadFiles) {
272  ex.addContext("Calling RootFileSequenceBase::initTheFile()");
273  std::ostringstream out;
274  out << "Input file " << fileName() << " could not be opened.\n";
275  out << "Fallback Input file " << fallbackFileName() << " also could not be opened.";
276  if (originalInfo.size()) {
277  out << std::endl << "Original exception info is above; fallback exception info is below.";
278  ex.addAdditionalInfo(out.str());
279  for (auto const & s : originalInfo) {
280  ex.addAdditionalInfo(s);
281  }
282  } else {
283  ex.addAdditionalInfo(out.str());
284  }
285  throw ex;
286  }
287  }
288  }
289  if(filePtr) {
290  size_t currentIndexIntoFile = fileIter_ - fileIterBegin_;
291  rootFile_ = makeRootFile(filePtr);
292  assert(rootFile_);
294  setIndexIntoFile(currentIndexIntoFile);
295  rootFile_->reportOpened(inputTypeName);
296  } else {
298  if(!skipBadFiles) {
300  "RootFileSequenceBase::initTheFile(): Input file " << fileName() << " was not found or could not be opened.\n";
301  }
302  LogWarning("") << "Input file: " << fileName() << " was not found or could not be opened, and will be skipped.\n";
303  }
304  }
305  void
307  indexesIntoFiles_[index] = rootFile()->indexIntoFileSharedPtr();
308  }
309 }
std::string const & logicalFileName() const
InputType
Definition: InputType.h:5
list pfn
Definition: dbtoconf.py:76
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:43
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)
RootFileSharedPtr const & rootFile() const
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)
tuple out
Definition: dbtoconf.py:99
bool skipToItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, size_t fileNameHash=0U, bool currentFileFirst=true)
std::unique_ptr< std::unordered_multimap< size_t, size_t > > findFileForSpecifiedID_
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
tuple size
Write out results.
std::vector< FileCatalogItem > const & fileCatalogItems() const
std::string const & fallbackFileName() const
InputFileCatalog const & catalog_