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  : catalog_(catalog),
24  lfn_("unknown"),
25  lfnHash_(0U),
26  usedFallback_(false),
27  findFileForSpecifiedID_(nullptr),
28  fileIterBegin_(fileCatalogItems().begin()),
29  fileIterEnd_(fileCatalogItems().end()),
30  fileIter_(fileIterEnd_),
31  fileIterLastOpened_(fileIterEnd_),
32  rootFile_(),
33  indexesIntoFiles_(fileCatalogItems().size()) {}
34 
35  std::vector<FileCatalogItem> const& RootInputFileSequence::fileCatalogItems() const {
36  return catalog_.fileCatalogItems();
37  }
38 
39  std::shared_ptr<ProductRegistry const> RootInputFileSequence::fileProductRegistry() const {
40  assert(rootFile());
41  return rootFile()->productRegistry();
42  }
43 
44  std::shared_ptr<BranchIDListHelper const> RootInputFileSequence::fileBranchIDListHelper() const {
45  assert(rootFile());
46  return rootFile()->branchIDListHelper();
47  }
48 
50 
51  std::shared_ptr<RunAuxiliary> RootInputFileSequence::readRunAuxiliary_() {
52  assert(rootFile());
53  return rootFile()->readRunAuxiliary_();
54  }
55 
56  std::shared_ptr<LuminosityBlockAuxiliary> RootInputFileSequence::readLuminosityBlockAuxiliary_() {
57  assert(rootFile());
58  return rootFile()->readLuminosityBlockAuxiliary_();
59  }
60 
62  assert(rootFile());
63  rootFile()->readRun_(runPrincipal);
64  }
65 
67  assert(rootFile());
68  rootFile()->readLuminosityBlock_(lumiPrincipal);
69  }
70 
71  // readEvent() is responsible for setting up the EventPrincipal.
72  //
73  // 1. fill an EventPrincipal with a unique EventID
74  // 2. For each entry in the provenance, put in one ProductResolver,
75  // holding the Provenance for the corresponding EDProduct.
76  // 3. set up the caches in the EventPrincipal to know about this
77  // ProductResolver.
78  //
79  // We do *not* create the EDProduct instance (the equivalent of reading
80  // the branch containing this EDProduct. That will be done by the Delayed Reader,
81  // when it is asked to do so.
82  //
83 
85  assert(rootFile());
86  rootFile()->readEvent(eventPrincipal);
87  }
88 
91  EventNumber_t event) const {
92  if (!rootFile())
93  return false;
94  return rootFile()->containsItem(run, lumi, event);
95  }
96 
100  size_t fileNameHash) {
101  // Look for item in files not yet opened. We have a hash of the logical file name
102  assert(fileNameHash != 0U);
103  // If the lookup table is not yet filled in, fill it.
105  // We use a multimap because there may be hash collisions (Two different LFNs could have the same hash).
106  // We map the hash of the LFN to the index into the list of files.
108  std::make_unique<std::unordered_multimap<size_t, size_t>>(); // propagate_const<T> has no reset() function
109  auto hasher = std::hash<std::string>();
110  for (auto fileIter = fileIterBegin_; fileIter != fileIterEnd_; ++fileIter) {
111  findFileForSpecifiedID_->insert(std::make_pair(hasher(fileIter->logicalFileName()), fileIter - fileIterBegin_));
112  }
113  }
114  // Look up the logical file name in the table
115  auto range = findFileForSpecifiedID_->equal_range(fileNameHash);
116  for (auto iter = range.first; iter != range.second; ++iter) {
117  // Don't look in files previously opened, because those have already been searched.
118  if (!indexesIntoFiles_[iter->second]) {
119  setAtFileSequenceNumber(iter->second);
120  initFile_(false);
121  assert(rootFile());
122  bool found = rootFile()->setEntryAtItem(run, lumi, event);
123  if (found) {
124  return true;
125  }
126  }
127  }
128  // Not found
129  return false;
130  }
131 
133  // Look for item in files not yet opened. We do not have a valid hash of the logical file name.
134  for (auto it = indexesIntoFiles_.begin(), itEnd = indexesIntoFiles_.end(); it != itEnd; ++it) {
135  if (!*it) {
136  // File not yet opened.
138  initFile_(false);
139  assert(rootFile());
140  bool found = rootFile()->setEntryAtItem(run, lumi, event);
141  if (found) {
142  return true;
143  }
144  }
145  }
146  // Not found
147  return false;
148  }
149 
151  RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, size_t fileNameHash, bool currentFileFirst) {
152  // Attempt to find item in currently open input file.
153  bool found = currentFileFirst && rootFile() && rootFile()->setEntryAtItem(run, lumi, event);
154  if (!found) {
155  // If only one input file, give up now, to save time.
156  if (currentFileFirst && rootFile() && indexesIntoFiles_.size() == 1) {
157  return false;
158  }
159  // Look for item (run/lumi/event) in files previously opened without reopening unnecessary files.
160  for (auto it = indexesIntoFiles_.begin(), itEnd = indexesIntoFiles_.end(); it != itEnd; ++it) {
161  if (*it && (*it)->containsItem(run, lumi, event)) {
162  // We found it. Close the currently open file, and open the correct one.
163  std::vector<FileCatalogItem>::const_iterator currentIter = fileIter_;
165  if (fileIter_ != currentIter) {
166  initFile(false);
167  }
168  // Now get the item from the correct file.
169  assert(rootFile());
170  found = rootFile()->setEntryAtItem(run, lumi, event);
171  assert(found);
172  return true;
173  }
174  }
175  return (fileNameHash != 0U && skipToItemInNewFile(run, lumi, event, fileNameHash)) ||
177  }
178  return true;
179  }
180 
181  //Initiate the file using multiple data catalogs
183  bool skipBadFiles, bool deleteIndexIntoFile, InputSource* input, char const* inputTypeName, InputType inputType) {
184  // We are really going to close the open file.
185 
187  size_t currentIndexIntoFile = fileIterLastOpened_ - fileIterBegin_;
188  if (deleteIndexIntoFile) {
189  indexesIntoFiles_[currentIndexIntoFile].reset();
190  } else {
191  if (indexesIntoFiles_[currentIndexIntoFile])
192  indexesIntoFiles_[currentIndexIntoFile]->inputFileClosed();
193  }
195  }
196  closeFile_();
197 
198  if (noMoreFiles()) {
199  // No files specified
200  return;
201  }
202 
203  // Check if the logical file name was found.
204  if (fileNames()[0].empty()) {
205  // LFN not found in catalog.
207  if (!skipBadFiles) {
208  throw cms::Exception("LogicalFileNameNotFound", "RootFileSequenceBase::initTheFile()\n")
209  << "Logical file name '" << logicalFileName() << "' was not found in the file catalog.\n"
210  << "If you wanted a local file, you forgot the 'file:' prefix\n"
211  << "before the file name in your configuration file.\n";
212  }
213  LogWarning("") << "Input logical file: " << logicalFileName()
214  << " was not found in the catalog, and will be skipped.\n";
215  return;
216  }
217 
218  lfn_ = logicalFileName().empty() ? fileNames()[0] : logicalFileName();
219  lfnHash_ = std::hash<std::string>()(lfn_);
220 
221  std::shared_ptr<InputFile> filePtr;
222  std::list<std::string> originalInfo;
223 
224  std::vector<std::string> const& fNames = fileNames();
225 
226  //this tries to open the file using multiple PFNs corresponding to different data catalogs
227  std::list<std::string> exInfo;
228  for (std::vector<std::string>::const_iterator it = fNames.begin(); it != fNames.end(); ++it) {
229  try {
230  std::unique_ptr<InputSource::FileOpenSentry> sentry(
231  input ? std::make_unique<InputSource::FileOpenSentry>(*input, lfn_, false) : nullptr);
232  std::unique_ptr<char[]> name(gSystem->ExpandPathName(it->c_str()));
233  filePtr = std::make_shared<InputFile>(name.get(), " Initiating request to open file ", inputType);
234  break;
235  } catch (cms::Exception const& e) {
236  if (!skipBadFiles && std::next(it) == fNames.end()) {
239  ex.addContext("Calling RootInputFileSequence::initTheFile()");
240  std::ostringstream out;
241  out << "Input file " << (*it) << " could not be opened.";
242  ex.addAdditionalInfo(out.str());
243  //report previous exceptions when use other names to open file
244  for (auto const& s : exInfo)
245  ex.addAdditionalInfo(s);
246  throw ex;
247  } else {
248  exInfo.push_back("Calling RootInputFileSequence::initTheFile(): fail to open the file with name " + (*it));
249  }
250  }
251  }
252 
253  if (filePtr) {
254  size_t currentIndexIntoFile = fileIter_ - fileIterBegin_;
255  rootFile_ = makeRootFile(filePtr);
256  if (input) {
257  rootFile_->setSignals(&(input->preEventReadFromSourceSignal_), &(input->postEventReadFromSourceSignal_));
258  }
259  assert(rootFile_);
261  setIndexIntoFile(currentIndexIntoFile);
262  rootFile_->reportOpened(inputTypeName);
263  } else {
264  std::string fName = !fNames.empty() ? fNames[0] : "";
265  InputFile::reportSkippedFile(fName, logicalFileName()); //0 cause exception?
266  if (!skipBadFiles) {
267  throw Exception(errors::FileOpenError) << "RootFileSequenceBase::initTheFile(): Input file " << fName
268  << " was not found or could not be opened.\n";
269  }
270  LogWarning("RootInputFileSequence")
271  << "Input file: " << fName << " was not found or could not be opened, and will be skipped.\n";
272  }
273  }
274 
276  indexesIntoFiles_[index] = rootFile()->indexIntoFileSharedPtr();
277  }
278 
279 } // namespace edm
edm::RootInputFileSequence::noMoreFiles
bool noMoreFiles() const
Definition: RootInputFileSequence.h:65
edm::RootInputFileSequence::findFileForSpecifiedID_
edm::propagate_const< std::unique_ptr< std::unordered_multimap< size_t, size_t > > > findFileForSpecifiedID_
Definition: RootInputFileSequence.h:95
edm::RunNumber_t
unsigned int RunNumber_t
Definition: RunLumiEventNumber.h:14
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
RootInputFileSequence.h
edm::RootInputFileSequence::fileProductRegistry
std::shared_ptr< ProductRegistry const > fileProductRegistry() const
Definition: RootInputFileSequence.cc:39
input
static const std::string input
Definition: EdmProvDump.cc:48
MessageLogger.h
funct::false
false
Definition: Factorize.h:34
edm::RootInputFileSequence::setAtFileSequenceNumber
void setAtFileSequenceNumber(size_t offset)
Definition: RootInputFileSequence.h:71
cms::Exception::addContext
void addContext(std::string const &context)
Definition: Exception.cc:165
getEcalConditions_orcoffint2r_cff.catalog
catalog
Definition: getEcalConditions_orcoffint2r_cff.py:40
edm::RootInputFileSequence::closeFile_
virtual void closeFile_()=0
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::RootInputFileSequence::lfnHash_
size_t lfnHash_
Definition: RootInputFileSequence.h:93
BranchID.h
edm::RootInputFileSequence::makeRootFile
virtual RootFileSharedPtr makeRootFile(std::shared_ptr< InputFile > filePtr)=0
edm::LuminosityBlockPrincipal
Definition: LuminosityBlockPrincipal.h:31
cms::cuda::assert
assert(be >=bs)
IndexIntoFile.h
edm::InputFileCatalog
Definition: InputFileCatalog.h:32
ProductRegistry.h
edm::RootInputFileSequence::indexesIntoFiles_
std::vector< std::shared_ptr< IndexIntoFile > > indexesIntoFiles_
Definition: RootInputFileSequence.h:101
remoteMonitoring_LED_IterMethod_cfg.skipBadFiles
skipBadFiles
Definition: remoteMonitoring_LED_IterMethod_cfg.py:24
edm::RootInputFileSequence::logicalFileName
std::string const & logicalFileName() const
Definition: RootInputFileSequence.h:78
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
edm::LuminosityBlockNumber_t
unsigned int LuminosityBlockNumber_t
Definition: RunLumiEventNumber.h:13
edm::InputType
InputType
Definition: InputType.h:5
end
#define end
Definition: vmac.h:39
edm::errors::FileOpenError
Definition: EDMException.h:49
MainPageGenerator.fName
fName
Definition: MainPageGenerator.py:301
edm::Exception
Definition: EDMException.h:77
edm::RootInputFileSequence::initFile
void initFile(bool skipBadFiles)
Definition: RootInputFileSequence.h:53
edm::RootInputFileSequence::setIndexIntoFile
void setIndexIntoFile(size_t index)
Definition: RootInputFileSequence.cc:275
edm::RootInputFileSequence::lfn_
std::string lfn_
Definition: RootInputFileSequence.h:92
alignCSCRings.s
s
Definition: alignCSCRings.py:92
edm::InputFile::reportSkippedFile
static void reportSkippedFile(std::string const &fileName, std::string const &logicalFileName)
Definition: InputFile.cc:75
edm::EventPrincipal
Definition: EventPrincipal.h:46
edm::RootInputFileSequence::fileIterBegin_
const std::vector< FileCatalogItem >::const_iterator fileIterBegin_
Definition: RootInputFileSequence.h:96
edm::RootInputFileSequence::fileIter_
std::vector< FileCatalogItem >::const_iterator fileIter_
Definition: RootInputFileSequence.h:98
edm::EventNumber_t
unsigned long long EventNumber_t
Definition: RunLumiEventNumber.h:12
edm::RootInputFileSequence::readLuminosityBlockAuxiliary_
std::shared_ptr< LuminosityBlockAuxiliary > readLuminosityBlockAuxiliary_()
Definition: RootInputFileSequence.cc:56
edm::RootInputFileSequence::readRun_
void readRun_(RunPrincipal &runPrincipal)
Definition: RootInputFileSequence.cc:61
ParameterSetDescription.h
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
edm::RootInputFileSequence::RootInputFileSequence
RootInputFileSequence(ParameterSet const &pset, InputFileCatalog const &catalog)
Definition: RootInputFileSequence.cc:22
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
edm::LogWarning
Definition: MessageLogger.h:141
edm::RootInputFileSequence::initFile_
virtual void initFile_(bool skipBadFiles)=0
edm::ParameterSet
Definition: ParameterSet.h:36
cms::Exception::addAdditionalInfo
void addAdditionalInfo(std::string const &info)
Definition: Exception.cc:169
edm::RootInputFileSequence::fileIterLastOpened_
std::vector< FileCatalogItem >::const_iterator fileIterLastOpened_
Definition: RootInputFileSequence.h:99
visDQMUpload.hasher
hasher
Definition: visDQMUpload.py:152
edm::RootInputFileSequence::skipToItemInNewFile
bool skipToItemInNewFile(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event)
Definition: RootInputFileSequence.cc:132
edm::RootInputFileSequence::rootFile
std::shared_ptr< RootFile const > rootFile() const
Definition: RootInputFileSequence.h:87
StorageFactory.h
edm::RootInputFileSequence::readEvent
void readEvent(EventPrincipal &cache)
Definition: RootInputFileSequence.cc:84
edm::RootInputFileSequence::containedInCurrentFile
bool containedInCurrentFile(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event) const
Definition: RootInputFileSequence.cc:89
edm::RootInputFileSequence::rootFile_
edm::propagate_const< RootFileSharedPtr > rootFile_
Definition: RootInputFileSequence.h:100
RootFile.h
edm::RootInputFileSequence::~RootInputFileSequence
virtual ~RootInputFileSequence()
Definition: RootInputFileSequence.cc:49
edm::RootInputFileSequence::catalog_
InputFileCatalog const & catalog_
Definition: RootInputFileSequence.h:91
edm::RootInputFileSequence::fileBranchIDListHelper
std::shared_ptr< BranchIDListHelper const > fileBranchIDListHelper() const
Definition: RootInputFileSequence.cc:44
writedatasetfile.run
run
Definition: writedatasetfile.py:27
edm::RootInputFileSequence::fileIterEnd_
const std::vector< FileCatalogItem >::const_iterator fileIterEnd_
Definition: RootInputFileSequence.h:97
relativeConstraints.empty
bool empty
Definition: relativeConstraints.py:46
Exception
Definition: hltDiff.cc:246
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
edm::RootInputFileSequence::readRunAuxiliary_
std::shared_ptr< RunAuxiliary > readRunAuxiliary_()
Definition: RootInputFileSequence.cc:51
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
edm::RootInputFileSequence::initTheFile
void initTheFile(bool skipBadFiles, bool deleteIndexIntoFile, InputSource *input, char const *inputTypeName, InputType inputType)
Definition: RootInputFileSequence.cc:182
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
cms::Exception
Definition: Exception.h:70
edm::InputSource
Definition: InputSource.h:76
ParameterSet.h
edm::RunPrincipal
Definition: RunPrincipal.h:34
event
Definition: event.py:1
lumi
Definition: LumiSectionData.h:20
edm::InputFileCatalog::fileCatalogItems
std::vector< FileCatalogItem > const & fileCatalogItems() const
Definition: InputFileCatalog.h:39
begin
#define begin
Definition: vmac.h:32
edm::RootInputFileSequence::fileNames
std::vector< std::string > const & fileNames() const
Definition: RootInputFileSequence.h:76
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
edm::RootInputFileSequence::readLuminosityBlock_
void readLuminosityBlock_(LuminosityBlockPrincipal &lumiPrincipal)
Definition: RootInputFileSequence.cc:66
edm::RootInputFileSequence::skipToItem
bool skipToItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, size_t fileNameHash=0U, bool currentFileFirst=true)
Definition: RootInputFileSequence.cc:150
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
edm::RootInputFileSequence::fileCatalogItems
std::vector< FileCatalogItem > const & fileCatalogItems() const
Definition: RootInputFileSequence.cc:35