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,
25  unsigned int nStreams) :
26  RootInputFileSequence(pset, catalog),
27  input_(input),
28  firstFile_(true),
29  branchesMustMatch_(BranchDescription::Permissive),
30  orderedProcessHistoryIDs_(),
31  nStreams_(nStreams),
32  eventSkipperByID_(EventSkipperByID::create(pset).release()),
33  // The default value provided as the second argument to the getUntrackedParameter function call
34  // is not used when the ParameterSet has been validated and the parameters are not optional
35  // in the description. This is currently true when PoolSource is the primary input source.
36  // The modules that use PoolSource as a SecSource have not defined their fillDescriptions function
37  // yet, so the ParameterSet does not get validated yet. As soon as all the modules with a SecSource
38  // have defined descriptions, the defaults in the getUntrackedParameterSet function calls can
39  // and should be deleted from the code.
40  initialNumberOfEventsToSkip_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0U)),
41  noEventSort_(pset.getUntrackedParameter<bool>("noEventSort", true)),
42  skipBadFiles_(pset.getUntrackedParameter<bool>("skipBadFiles", false)),
43  bypassVersionCheck_(pset.getUntrackedParameter<bool>("bypassVersionCheck", false)),
44  treeCacheSize_(noEventSort_ ? pset.getUntrackedParameter<unsigned int>("cacheSize", roottree::defaultCacheSize) : 0U),
45  treeMaxVirtualSize_(pset.getUntrackedParameter<int>("treeMaxVirtualSize", -1)),
46  setRun_(pset.getUntrackedParameter<unsigned int>("setRunNumber", 0U)),
47  productSelectorRules_(pset, "inputCommands", "InputSource"),
48  duplicateChecker_(new DuplicateChecker(pset)),
49  dropDescendants_(pset.getUntrackedParameter<bool>("dropDescendantsOfDroppedBranches", true)),
50  labelRawDataLikeMC_(pset.getUntrackedParameter<bool>("labelRawDataLikeMC", true)),
51  usingGoToEvent_(false),
52  enablePrefetching_(false) {
53 
54  // The SiteLocalConfig controls the TTreeCache size and the prefetching settings.
56  if(pSLC.isAvailable()) {
57  if(treeCacheSize_ != 0U && pSLC->sourceTTreeCacheSize()) {
58  treeCacheSize_ = *(pSLC->sourceTTreeCacheSize());
59  }
60  enablePrefetching_ = pSLC->enablePrefetching();
61  }
62 
63  std::string branchesMustMatch = pset.getUntrackedParameter<std::string>("branchesMustMatch", std::string("permissive"));
64  if(branchesMustMatch == std::string("strict")) branchesMustMatch_ = BranchDescription::Strict;
65 
66  // Prestage the files
69  }
70  // Open the first file.
73  if(rootFile()) break;
74  }
75  if(rootFile()) {
76  input_.productRegistryUpdate().updateFromInput(rootFile()->productRegistry()->productList());
79  }
80  }
81  }
82 
84  }
85 
86  void
88  closeFile_();
89  }
90 
91  std::unique_ptr<FileBlock>
93  if(firstFile_) {
94  // The first input file has already been opened.
95  firstFile_ = false;
96  if(!rootFile()) {
98  }
99  } else {
100  if(!nextFile()) {
101  assert(0);
102  }
103  }
104  if(!rootFile()) {
105  return std::unique_ptr<FileBlock>(new FileBlock);
106  }
107  return rootFile()->createFileBlock();
108  }
109 
110  void
112  // close the currently open file, if any, and delete the RootFile object.
113  if(rootFile()) {
114  std::unique_ptr<InputSource::FileCloseSentry>
116  rootFile()->close();
117  if(duplicateChecker_) duplicateChecker_->inputFileClosed();
118  rootFile().reset();
119  }
120  }
121 
122  void
124  // If we are not duplicate checking across files and we are not using random access to find events,
125  // then we can delete the IndexIntoFile for the file we are closing.
126  // If we can't delete all of it, then we can delete the parts we do not need.
127  bool deleteIndexIntoFile = !usingGoToEvent_ && !(duplicateChecker_ && duplicateChecker_->checkingAllFiles() && !duplicateChecker_->checkDisabled());
128  initTheFile(skipBadFiles, deleteIndexIntoFile, &input_, "primaryFiles", InputType::Primary);
129  }
130 
132  RootPrimaryFileSequence::makeRootFile(std::shared_ptr<InputFile> filePtr) {
133  size_t currentIndexIntoFile = sequenceNumberOfFile();
134  return std::make_shared<RootFile>(
135  fileName(),
137  logicalFileName(),
138  filePtr,
141  remainingEvents(),
143  nStreams_,
147  setRun_,
148  noEventSort_,
153  std::vector<BranchID>(), // associationsFromSecondary_
158  currentIndexIntoFile,
164  }
165 
167  if(!noMoreFiles()) setAtNextFile();
168  if(noMoreFiles()) {
169  return false;
170  }
171 
173 
174  if(rootFile()) {
175  // make sure the new product registry is compatible with the main one
176  std::string mergeInfo = input_.productRegistryUpdate().merge(*rootFile()->productRegistry(),
177  fileName(),
179  if(!mergeInfo.empty()) {
180  throw Exception(errors::MismatchedInputFiles,"RootPrimaryFileSequence::nextFile()") << mergeInfo;
181  }
182  }
183  return true;
184  }
185 
187  if(atFirstFile()) {
188  return false;
189  }
191 
192  initFile(false);
193 
194  if(rootFile()) {
195  // make sure the new product registry is compatible to the main one
196  std::string mergeInfo = input_.productRegistryUpdate().merge(*rootFile()->productRegistry(),
197  fileName(),
199  if(!mergeInfo.empty()) {
200  throw Exception(errors::MismatchedInputFiles,"RootPrimaryFileSequence::previousEvent()") << mergeInfo;
201  }
202  }
203  if(rootFile()) rootFile()->setToLastEntry();
204  return true;
205  }
206 
209  if(noMoreFiles()) {
210  return InputSource::IsStop;
211  }
212  if(firstFile_) {
213  return InputSource::IsFile;
214  }
215  if(rootFile()) {
216  IndexIntoFile::EntryType entryType = rootFile()->getNextItemType(run, lumi, event);
217  if(entryType == IndexIntoFile::kEvent) {
218  return InputSource::IsEvent;
219  } else if(entryType == IndexIntoFile::kLumi) {
220  return InputSource::IsLumi;
221  } else if(entryType == IndexIntoFile::kRun) {
222  return InputSource::IsRun;
223  }
224  assert(entryType == IndexIntoFile::kEnd);
225  }
226  if(atLastFile()) {
227  return InputSource::IsStop;
228  }
229  return InputSource::IsFile;
230  }
231 
232  // Rewind to before the first event that was read.
233  void
235  if(!atFirstFile()) {
236  closeFile_();
237  setAtFirstFile();
238  }
239  if(!rootFile()) {
240  initFile(false);
241  }
242  rewindFile();
243  firstFile_ = true;
244  if(rootFile()) {
247  }
248  }
249  }
250 
251  // Rewind to the beginning of the current file
252  void
254  if(rootFile()) rootFile()->rewind();
255  }
256 
257  // Advance "offset" events. Offset can be positive or negative (or zero).
258  bool
260  assert(rootFile());
261  while(offset != 0) {
262  bool atEnd = rootFile()->skipEvents(offset);
263  if((offset > 0 || atEnd) && !nextFile()) {
264  return false;
265  }
266  if(offset < 0 && !previousFile()) {
267  setNoMoreFiles();
268  return false;
269  }
270  }
271  return true;
272  }
273 
274  bool
276  usingGoToEvent_ = true;
277  if(rootFile()) {
278  if(rootFile()->goToEvent(eventID)) {
279  return true;
280  }
281  // If only one input file, give up now, to save time.
282  if(rootFile() && indexesIntoFiles().size() == 1) {
283  return false;
284  }
285  // Save the current file and position so that we can restore them
286  // if we fail to restore the desired event
287  bool closedOriginalFile = false;
288  size_t const originalFileSequenceNumber = sequenceNumberOfFile();
289  IndexIntoFile::IndexIntoFileItr originalPosition = rootFile()->indexIntoFileIter();
290 
291  // Look for item (run/lumi/event) in files previously opened without reopening unnecessary files.
292  typedef std::vector<std::shared_ptr<IndexIntoFile> >::const_iterator Iter;
293  for(Iter it = indexesIntoFiles().begin(), itEnd = indexesIntoFiles().end(); it != itEnd; ++it) {
294  if(*it && (*it)->containsItem(eventID.run(), eventID.luminosityBlock(), eventID.event())) {
295  // We found it. Close the currently open file, and open the correct one.
297  initFile(false);
298  // Now get the item from the correct file.
299  assert(rootFile());
300  bool found = rootFile()->goToEvent(eventID);
301  assert(found);
302  return true;
303  }
304  }
305  // Look for item in files not yet opened.
306  for(Iter it = indexesIntoFiles().begin(), itEnd = indexesIntoFiles().end(); it != itEnd; ++it) {
307  if(!*it) {
309  initFile(false);
310  closedOriginalFile = true;
311  if((*it)->containsItem(eventID.run(), eventID.luminosityBlock(), eventID.event())) {
312  assert(rootFile());
313  if(rootFile()->goToEvent(eventID)) {
314  return true;
315  }
316  }
317  }
318  }
319  if(closedOriginalFile) {
320  setAtFileSequenceNumber(originalFileSequenceNumber);
321  initFile(false);
322  assert(rootFile());
323  rootFile()->setPosition(originalPosition);
324  }
325  }
326  return false;
327  }
328 
329  int
331  return input_.remainingEvents();
332  }
333 
334  int
337  }
338 
339  void
341  desc.addUntracked<unsigned int>("skipEvents", 0U)
342  ->setComment("Skip the first 'skipEvents' events that otherwise would have been processed.");
343  desc.addUntracked<bool>("noEventSort", true)
344  ->setComment("True: Process runs, lumis and events in the order they appear in the file (but see notes 1 and 2).\n"
345  "False: Process runs, lumis and events in each file in numerical order (run#, lumi#, event#) (but see note 3).\n"
346  "Note 1: Events within the same lumi will always be processed contiguously.\n"
347  "Note 2: Lumis within the same run will always be processed contiguously.\n"
348  "Note 3: Any sorting occurs independently in each input file (no sorting across input files).");
349  desc.addUntracked<bool>("skipBadFiles", false)
350  ->setComment("True: Ignore any missing or unopenable input file.\n"
351  "False: Throw exception if missing or unopenable input file.");
352  desc.addUntracked<bool>("bypassVersionCheck", false)
353  ->setComment("True: Bypass release version check.\n"
354  "False: Throw exception if reading file in a release prior to the release in which the file was written.");
355  desc.addUntracked<unsigned int>("cacheSize", roottree::defaultCacheSize)
356  ->setComment("Size of ROOT TTree prefetch cache. Affects performance.");
357  desc.addUntracked<int>("treeMaxVirtualSize", -1)
358  ->setComment("Size of ROOT TTree TBasket cache. Affects performance.");
359  desc.addUntracked<unsigned int>("setRunNumber", 0U)
360  ->setComment("If non-zero, change number of first run to this number. Apply same offset to all runs. Allowed only for simulation.");
361  desc.addUntracked<bool>("dropDescendantsOfDroppedBranches", true)
362  ->setComment("If True, also drop on input any descendent of any branch dropped on input.");
363  std::string defaultString("permissive");
364  desc.addUntracked<std::string>("branchesMustMatch", defaultString)
365  ->setComment("'strict': Branches in each input file must match those in the first file.\n"
366  "'permissive': Branches in each input file may be any subset of those in the first file.");
367  desc.addUntracked<bool>("labelRawDataLikeMC", true)
368  ->setComment("If True: replace module label for raw data to match MC. Also use 'LHC' as process.");
369 
370  ProductSelectorRules::fillDescription(desc, "inputCommands");
373  }
374 
377  if(rootFile()) {
378  if(!rootFile()->wasLastEventJustRead()) {
380  }
381  if(noMoreFiles() || atLastFile()) {
383  } else {
385  }
386  }
388  }
389 
392  if(rootFile()) {
393  if(!rootFile()->wasFirstEventJustRead()) {
395  }
396  if(!atFirstFile()) {
398  }
400  }
402  }
403 
404 }
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
static void fillDescription(ParameterSetDescription &desc, char const *parameterName)
void initFile(bool skipBadFiles)
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)
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())
std::shared_ptr< ThinnedAssociationsHelper > thinnedAssociationsHelper() const
Accessor for thinnedAssociationsHelper.
Definition: InputSource.h:178
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:251
static void fillDescription(ParameterSetDescription &desc)
std::shared_ptr< DuplicateChecker > duplicateChecker_
static void fillDescription(ParameterSetDescription &desc)
std::unique_ptr< FileBlock > readFile_()
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
RootPrimaryFileSequence(ParameterSet const &pset, PoolSource &input, InputFileCatalog const &catalog, unsigned int nStreams)
unsigned int const defaultCacheSize
Definition: RootTree.h:37
unsigned int LuminosityBlockNumber_t
static std::string const input
Definition: EdmProvDump.cc:43
std::shared_ptr< EventSkipperByID > eventSkipperByID_
ProcessingController::ReverseState reverseState() const
bool goToEvent(EventID const &eventID)
static const StorageFactory * get(void)
int remainingEvents() const
Definition: InputSource.h:195
void stagein(const std::string &url) const
std::string const & lfn() const
std::shared_ptr< BranchIDListHelper > branchIDListHelper() const
Accessor for branchIDListHelper.
Definition: InputSource.h:175
virtual RootFileSharedPtr makeRootFile(std::shared_ptr< InputFile > filePtr) override
void initTheFile(bool skipBadFiles, bool deleteIndexIntoFile, InputSource *input, char const *inputTypeName, InputType inputType)
RootFileSharedPtr const & rootFile() const
std::vector< ProcessHistoryID > orderedProcessHistoryIDs_
bool isAvailable() const
Definition: Service.h:46
std::string merge(ProductRegistry const &other, std::string const &fileName, BranchDescription::MatchMode branchesMustMatch=BranchDescription::Permissive)
#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 > RootFileSharedPtr
ProductSelectorRules productSelectorRules_
ProductRegistry & productRegistryUpdate() const
Definition: InputSource.h:345
BranchDescription::MatchMode branchesMustMatch_
tuple skipBadFiles
Definition: example_cfg.py:64
static void fillDescription(ParameterSetDescription &desc)
#define begin
Definition: vmac.h:30
ProcessHistoryRegistry & processHistoryRegistryForUpdate() const
Definition: InputSource.h:346
void updateFromInput(ProductList const &other)
int remainingLuminosityBlocks() const
Definition: InputSource.h:203
unsigned int RunNumber_t
volatile std::atomic< bool > shutdown_flag false
ProcessConfiguration const & processConfiguration() const
Accessor for Process Configuration.
Definition: InputSource.h:209
SurfaceDeformation * create(int type, const std::vector< double > &params)
tuple size
Write out results.