CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RootPrimaryFileSequence.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 ----------------------------------------------------------------------*/
3 #include "DuplicateChecker.h"
4 #include "InputFile.h"
5 #include "PoolSource.h"
6 #include "RootFile.h"
8 #include "RootTree.h"
9 
19 
20 namespace edm {
22  ParameterSet const& pset,
24  InputFileCatalog const& catalog) :
25  RootInputFileSequence(pset, catalog),
26  input_(input),
27  firstFile_(true),
28  branchesMustMatch_(BranchDescription::Permissive),
29  orderedProcessHistoryIDs_(),
30  eventSkipperByID_(EventSkipperByID::create(pset).release()),
31  initialNumberOfEventsToSkip_(pset.getUntrackedParameter<unsigned int>("skipEvents")),
32  noEventSort_(pset.getUntrackedParameter<bool>("noEventSort")),
33  treeCacheSize_(noEventSort_ ? pset.getUntrackedParameter<unsigned int>("cacheSize") : 0U),
34  duplicateChecker_(new DuplicateChecker(pset)),
35  usingGoToEvent_(false),
36  enablePrefetching_(false),
37  enforceGUIDInFileName_(pset.getUntrackedParameter<bool>("enforceGUIDInFileName")) {
38 
39  // The SiteLocalConfig controls the TTreeCache size and the prefetching settings.
41  if(pSLC.isAvailable()) {
42  if(treeCacheSize_ != 0U && pSLC->sourceTTreeCacheSize()) {
43  treeCacheSize_ = *(pSLC->sourceTTreeCacheSize());
44  }
45  enablePrefetching_ = pSLC->enablePrefetching();
46  }
47 
48  std::string branchesMustMatch = pset.getUntrackedParameter<std::string>("branchesMustMatch", std::string("permissive"));
49  if(branchesMustMatch == std::string("strict")) branchesMustMatch_ = BranchDescription::Strict;
50 
51  // Prestage the files
54  }
55  // Open the first file.
58  if(rootFile()) break;
59  }
60  if(rootFile()) {
61  input_.productRegistryUpdate().updateFromInput(rootFile()->productRegistry()->productList());
64  }
65  }
66  }
67 
69  }
70 
71  void
73  closeFile_();
74  }
75 
76  std::unique_ptr<FileBlock>
78  if(firstFile_) {
79  // The first input file has already been opened.
80  firstFile_ = false;
81  if(!rootFile()) {
83  }
84  } else {
85  if(!nextFile()) {
86  assert(0);
87  }
88  }
89  if(!rootFile()) {
90  return std::unique_ptr<FileBlock>(new FileBlock);
91  }
92  return rootFile()->createFileBlock();
93  }
94 
95  void
97  // close the currently open file, if any, and delete the RootFile object.
98  if(rootFile()) {
99  std::unique_ptr<InputSource::FileCloseSentry>
101  rootFile()->close();
102  if(duplicateChecker_) duplicateChecker_->inputFileClosed();
103  rootFile().reset();
104  }
105  }
106 
107  void
109  // If we are not duplicate checking across files and we are not using random access to find events,
110  // then we can delete the IndexIntoFile for the file we are closing.
111  // If we can't delete all of it, then we can delete the parts we do not need.
112  bool deleteIndexIntoFile = !usingGoToEvent_ && !(duplicateChecker_ && duplicateChecker_->checkingAllFiles() && !duplicateChecker_->checkDisabled());
113  initTheFile(skipBadFiles, deleteIndexIntoFile, &input_, "primaryFiles", InputType::Primary);
114  }
115 
117  RootPrimaryFileSequence::makeRootFile(std::shared_ptr<InputFile> filePtr) {
118  size_t currentIndexIntoFile = sequenceNumberOfFile();
119  return std::make_shared<RootFile>(
120  fileName(),
122  logicalFileName(),
123  filePtr,
126  remainingEvents(),
128  input_.nStreams(),
132  input_.runHelper(),
133  noEventSort_,
138  nullptr, // associationsFromSecondary
143  currentIndexIntoFile,
150  }
151 
153  if(!noMoreFiles()) setAtNextFile();
154  if(noMoreFiles()) {
155  return false;
156  }
157 
159 
160  if(rootFile()) {
161  // make sure the new product registry is compatible with the main one
162  std::string mergeInfo = input_.productRegistryUpdate().merge(*rootFile()->productRegistry(),
163  fileName(),
165  if(!mergeInfo.empty()) {
166  throw Exception(errors::MismatchedInputFiles,"RootPrimaryFileSequence::nextFile()") << mergeInfo;
167  }
168  }
169  return true;
170  }
171 
173  if(atFirstFile()) {
174  return false;
175  }
177 
178  initFile(false);
179 
180  if(rootFile()) {
181  // make sure the new product registry is compatible to the main one
182  std::string mergeInfo = input_.productRegistryUpdate().merge(*rootFile()->productRegistry(),
183  fileName(),
185  if(!mergeInfo.empty()) {
186  throw Exception(errors::MismatchedInputFiles,"RootPrimaryFileSequence::previousEvent()") << mergeInfo;
187  }
188  }
189  if(rootFile()) rootFile()->setToLastEntry();
190  return true;
191  }
192 
195  if(noMoreFiles()) {
196  return InputSource::IsStop;
197  }
198  if(firstFile_) {
199  return InputSource::IsFile;
200  }
201  if(rootFile()) {
202  IndexIntoFile::EntryType entryType = rootFile()->getNextItemType(run, lumi, event);
203  if(entryType == IndexIntoFile::kEvent) {
204  return InputSource::IsEvent;
205  } else if(entryType == IndexIntoFile::kLumi) {
206  return InputSource::IsLumi;
207  } else if(entryType == IndexIntoFile::kRun) {
208  return InputSource::IsRun;
209  }
210  assert(entryType == IndexIntoFile::kEnd);
211  }
212  if(atLastFile()) {
213  return InputSource::IsStop;
214  }
215  return InputSource::IsFile;
216  }
217 
218  // Rewind to before the first event that was read.
219  void
221  if(!atFirstFile()) {
222  closeFile_();
223  setAtFirstFile();
224  }
225  if(!rootFile()) {
226  initFile(false);
227  }
228  rewindFile();
229  firstFile_ = true;
230  if(rootFile()) {
233  }
234  }
235  }
236 
237  // Rewind to the beginning of the current file
238  void
240  if(rootFile()) rootFile()->rewind();
241  }
242 
243  // Advance "offset" events. Offset can be positive or negative (or zero).
244  bool
246  assert(rootFile());
247  while(offset != 0) {
248  bool atEnd = rootFile()->skipEvents(offset);
249  if((offset > 0 || atEnd) && !nextFile()) {
250  return false;
251  }
252  if(offset < 0 && !previousFile()) {
253  setNoMoreFiles();
254  return false;
255  }
256  }
257  return true;
258  }
259 
260  bool
262  usingGoToEvent_ = true;
263  if(rootFile()) {
264  if(rootFile()->goToEvent(eventID)) {
265  return true;
266  }
267  // If only one input file, give up now, to save time.
268  if(rootFile() && indexesIntoFiles().size() == 1) {
269  return false;
270  }
271  // Save the current file and position so that we can restore them
272  // if we fail to restore the desired event
273  bool closedOriginalFile = false;
274  size_t const originalFileSequenceNumber = sequenceNumberOfFile();
275  IndexIntoFile::IndexIntoFileItr originalPosition = rootFile()->indexIntoFileIter();
276 
277  // Look for item (run/lumi/event) in files previously opened without reopening unnecessary files.
278  for(auto it = indexesIntoFiles().begin(), itEnd = indexesIntoFiles().end(); it != itEnd; ++it) {
279  if(*it && (*it)->containsItem(eventID.run(), eventID.luminosityBlock(), eventID.event())) {
280  // We found it. Close the currently open file, and open the correct one.
282  initFile(false);
283  // Now get the item from the correct file.
284  assert(rootFile());
285  bool found = rootFile()->goToEvent(eventID);
286  assert(found);
287  return true;
288  }
289  }
290  // Look for item in files not yet opened.
291  for(auto it = indexesIntoFiles().begin(), itEnd = indexesIntoFiles().end(); it != itEnd; ++it) {
292  if(!*it) {
294  initFile(false);
295  closedOriginalFile = true;
296  if((*it)->containsItem(eventID.run(), eventID.luminosityBlock(), eventID.event())) {
297  assert(rootFile());
298  if(rootFile()->goToEvent(eventID)) {
299  return true;
300  }
301  }
302  }
303  }
304  if(closedOriginalFile) {
305  setAtFileSequenceNumber(originalFileSequenceNumber);
306  initFile(false);
307  assert(rootFile());
308  rootFile()->setPosition(originalPosition);
309  }
310  }
311  return false;
312  }
313 
314  int
316  return input_.remainingEvents();
317  }
318 
319  int
322  }
323 
324  void
326  desc.addUntracked<unsigned int>("skipEvents", 0U)
327  ->setComment("Skip the first 'skipEvents' events that otherwise would have been processed.");
328  desc.addUntracked<bool>("noEventSort", true)
329  ->setComment("True: Process runs, lumis and events in the order they appear in the file (but see notes 1 and 2).\n"
330  "False: Process runs, lumis and events in each file in numerical order (run#, lumi#, event#) (but see note 3).\n"
331  "Note 1: Events within the same lumi will always be processed contiguously.\n"
332  "Note 2: Lumis within the same run will always be processed contiguously.\n"
333  "Note 3: Any sorting occurs independently in each input file (no sorting across input files).");
334  desc.addUntracked<unsigned int>("cacheSize", roottree::defaultCacheSize)
335  ->setComment("Size of ROOT TTree prefetch cache. Affects performance.");
336  std::string defaultString("permissive");
337  desc.addUntracked<std::string>("branchesMustMatch", defaultString)
338  ->setComment("'strict': Branches in each input file must match those in the first file.\n"
339  "'permissive': Branches in each input file may be any subset of those in the first file.");
340  desc.addUntracked<bool>("enforceGUIDInFileName", false)
341  ->setComment(
342  "True: file name part is required to be equal to the GUID of the file\n"
343  "False: file name can be anything");
344 
347  }
348 
351  if(rootFile()) {
352  if(!rootFile()->wasLastEventJustRead()) {
354  }
355  if(noMoreFiles() || atLastFile()) {
357  } else {
359  }
360  }
362  }
363 
366  if(rootFile()) {
367  if(!rootFile()->wasFirstEventJustRead()) {
369  }
370  if(!atFirstFile()) {
372  }
374  }
376  }
377 
378 }
RunNumber_t run() const
Definition: EventID.h:39
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
std::string const & logicalFileName() const
void initFile(bool skipBadFiles)
ProductRegistry & productRegistryUpdate()
Definition: InputSource.h:350
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::string const & fileName() const
InputSource::ItemType getNextItemType(RunNumber_t &run, LuminosityBlockNumber_t &lumi, EventNumber_t &event)
virtual void closeFile_() override
void setAtFileSequenceNumber(size_t offset)
virtual void initFile_(bool skipBadFiles) override
ProcessingController::ForwardState forwardState() const
tuple lumi
Definition: fjr2json.py:35
assert(m_qm.get())
unsigned long long EventNumber_t
std::vector< std::shared_ptr< IndexIntoFile > > const & indexesIntoFiles() const
ProcessingMode processingMode() const
RunsLumisAndEvents (default), RunsAndLumis, or Runs.
Definition: InputSource.h:256
static void fillDescription(ParameterSetDescription &desc)
static void fillDescription(ParameterSetDescription &desc)
std::unique_ptr< FileBlock > readFile_()
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
unsigned int const defaultCacheSize
Definition: RootTree.h:37
unsigned int LuminosityBlockNumber_t
static std::string const input
Definition: EdmProvDump.cc:44
RunHelperBase * runHelper()
Definition: PoolSource.h:47
ProcessingController::ReverseState reverseState() const
std::shared_ptr< RootFile > RootFileSharedPtr
bool goToEvent(EventID const &eventID)
static const StorageFactory * get(void)
int remainingEvents() const
Definition: InputSource.h:200
void stagein(const std::string &url) const
std::string const & lfn() const
virtual RootFileSharedPtr makeRootFile(std::shared_ptr< InputFile > filePtr) override
void initTheFile(bool skipBadFiles, bool deleteIndexIntoFile, InputSource *input, char const *inputTypeName, InputType inputType)
std::vector< ProcessHistoryID > orderedProcessHistoryIDs_
bool isAvailable() const
Definition: Service.h:46
RootPrimaryFileSequence(ParameterSet const &pset, PoolSource &input, InputFileCatalog const &catalog)
std::string merge(ProductRegistry const &other, std::string const &fileName, BranchDescription::MatchMode branchesMustMatch=BranchDescription::Permissive)
unsigned int nStreams() const
Definition: PoolSource.h:44
bool dropDescendants() const
Definition: PoolSource.h:41
bool bypassVersionCheck() const
Definition: PoolSource.h:42
#define end
Definition: vmac.h:37
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
std::shared_ptr< RootFile const > rootFile() const
int treeMaxVirtualSize() const
Definition: PoolSource.h:45
bool skipBadFiles() const
Definition: PoolSource.h:40
std::shared_ptr< EventSkipperByID const > eventSkipperByID() const
std::shared_ptr< BranchIDListHelper const > branchIDListHelper() const
Accessors for branchIDListHelper.
Definition: InputSource.h:178
bool labelRawDataLikeMC() const
Definition: PoolSource.h:43
edm::propagate_const< std::shared_ptr< DuplicateChecker > > duplicateChecker_
ProcessHistoryRegistry & processHistoryRegistryForUpdate()
Definition: InputSource.h:351
BranchDescription::MatchMode branchesMustMatch_
tuple skipBadFiles
Definition: example_cfg.py:64
static void fillDescription(ParameterSetDescription &desc)
#define begin
Definition: vmac.h:30
std::shared_ptr< ThinnedAssociationsHelper const > thinnedAssociationsHelper() const
Accessors for thinnedAssociationsHelper.
Definition: InputSource.h:182
void updateFromInput(ProductList const &other)
int remainingLuminosityBlocks() const
Definition: InputSource.h:208
unsigned int RunNumber_t
volatile std::atomic< bool > shutdown_flag false
ProcessConfiguration const & processConfiguration() const
Accessor for Process Configuration.
Definition: InputSource.h:214
std::shared_ptr< DuplicateChecker const > duplicateChecker() const
tuple size
Write out results.
ProductSelectorRules const & productSelectorRules() const
Definition: PoolSource.h:46