CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RootEmbeddedFileSequence.cc
Go to the documentation of this file.
1 /*----------------------------------------------------------------------
2 ----------------------------------------------------------------------*/
3 #include "EmbeddedRootSource.h"
4 #include "InputFile.h"
5 #include "RootFile.h"
7 #include "RootTree.h"
8 
18 
19 #include "CLHEP/Random/RandFlat.h"
20 
21 #include <random>
22 
23 namespace edm {
24  class EventPrincipal;
25 
27  ParameterSet const& pset,
30  unsigned int nStreams) :
31  RootInputFileSequence(pset, catalog),
32  input_(input),
33  orderedProcessHistoryIDs_(),
34  nStreams_(nStreams),
35  sequential_(pset.getUntrackedParameter<bool>("sequential", false)),
36  sameLumiBlock_(pset.getUntrackedParameter<bool>("sameLumiBlock", false)),
37  fptr_(nullptr),
38  eventsRemainingInFile_(0),
39  // The default value provided as the second argument to the getUntrackedParameter function call
40  // is not used when the ParameterSet has been validated and the parameters are not optional
41  // in the description. This is currently true when PoolSource is the primary input source.
42  // The modules that use PoolSource as a SecSource have not defined their fillDescriptions function
43  // yet, so the ParameterSet does not get validated yet. As soon as all the modules with a SecSource
44  // have defined descriptions, the defaults in the getUntrackedParameterSet function calls can
45  // and should be deleted from the code.
46  initialNumberOfEventsToSkip_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0U)),
47  skipBadFiles_(pset.getUntrackedParameter<bool>("skipBadFiles", false)),
48  bypassVersionCheck_(pset.getUntrackedParameter<bool>("bypassVersionCheck", false)),
49  treeMaxVirtualSize_(pset.getUntrackedParameter<int>("treeMaxVirtualSize", -1)),
50  productSelectorRules_(pset, "inputCommands", "InputSource"),
51  enablePrefetching_(false) {
52 
53  if(noFiles()) {
54  throw Exception(errors::Configuration) << "RootEmbeddedFileSequence no input files specified for secondary input source.\n";
55  }
56  //
57  // The SiteLocalConfig controls the TTreeCache size and the prefetching settings.
59  if(pSLC.isAvailable()) {
60  enablePrefetching_ = pSLC->enablePrefetching();
61  }
62 
63  // Set the pointer to the function that reads an event.
64  if(sameLumiBlock_) {
65  if(sequential_) {
67  } else {
69  }
70  } else {
71  if(sequential_) {
73  } else {
75  }
76  }
77 
78  // For the secondary input source we do not stage in.
79  if(sequential_) {
80  // We open the first file
81  if(!atFirstFile()) {
83  initFile(false);
84  }
85  assert(rootFile());
86  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
87  if(!sameLumiBlock_) {
89  }
90  } else {
91  // We randomly choose the first file to open.
92  // We cannot use the random number service yet.
93  std::ifstream f("/dev/urandom");
94  unsigned int seed;
95  f.read(reinterpret_cast<char*>(&seed), sizeof(seed));
96  std::default_random_engine dre(seed);
97  size_t count = numberOfFiles();
98  std::uniform_int_distribution<int> distribution(0, count - 1);
99  while(!rootFile() && count != 0) {
100  --count;
101  int offset = distribution(dre);
102  setAtFileSequenceNumber(offset);
104  }
105  }
106  if(rootFile()) {
107  input_.productRegistryUpdate().updateFromInput(rootFile()->productRegistry()->productList());
108  }
109  }
110 
112  }
113 
114  void
116  closeFile_();
117  }
118 
120  // delete the RootFile object.
121  if(rootFile()) {
122  rootFile().reset();
123  }
124  }
125 
127  initTheFile(skipBadFiles, false, nullptr, "mixingFiles", InputType::SecondarySource);
128  }
129 
131  RootEmbeddedFileSequence::makeRootFile(std::shared_ptr<InputFile> filePtr) {
132  size_t currentIndexIntoFile = sequenceNumberOfFile();
133  return std::make_shared<RootFile>(
134  fileName(),
136  logicalFileName(),
137  filePtr,
138  nullptr, // eventSkipperByID_
139  false, // initialNumberOfEventsToSkip_ != 0 (not used)
140  -1, // remainingEvents()
141  -1, // remainingLuminosityBlocks()
142  nStreams_,
143  0U, // treeCacheSize_
146  0U, // setRun_
147  false, // noEventSort_
150  std::make_shared<BranchIDListHelper>(),
151  std::make_shared<ThinnedAssociationsHelper>(),
152  std::vector<BranchID>(), // associationsFromSecondary_
153  nullptr, // duplicateChecker_
154  false, // dropDescendants_
157  currentIndexIntoFile,
160  false, // labelRawDataLikeMC_
161  false, // usingGoToEvent_
163  }
164 
165  void
166  RootEmbeddedFileSequence::dropUnwantedBranches_(std::vector<std::string> const& wantedBranches) {
167  std::vector<std::string> rules;
168  rules.reserve(wantedBranches.size() + 1);
169  rules.emplace_back("drop *");
170  for(std::string const& branch : wantedBranches) {
171  rules.push_back("keep " + branch + "_*");
172  }
173  ParameterSet pset;
174  pset.addUntrackedParameter("inputCommands", rules);
175  productSelectorRules_ = ProductSelectorRules(pset, "inputCommands", "InputSource");
176  }
177 
178  void
180  // offset is decremented by the number of events actually skipped.
181  bool completed = rootFile()->skipEntries(offset);
182  while(!completed) {
183  setAtNextFile();
184  if(noMoreFiles()) {
185  setAtFirstFile();
186  }
187  initFile(false);
188  assert(rootFile());
189  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
190  completed = rootFile()->skipEntries(offset);
191  }
192  }
193 
194  bool
195  RootEmbeddedFileSequence::readOneSequential(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const*) {
196  skipBadFiles_ = false;
197  assert(rootFile());
198  rootFile()->nextEventEntry();
199  bool found = rootFile()->readCurrentEvent(cache);
200  if(!found) {
201  setAtNextFile();
202  if(noMoreFiles()) {
203  setAtFirstFile();
204  }
205  initFile(false);
206  assert(rootFile());
207  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
208  return readOneSequential(cache, fileNameHash, nullptr, nullptr);
209  }
210  fileNameHash = lfnHash();
211  return true;
212  }
213 
214  bool
215  RootEmbeddedFileSequence::readOneSequentialWithID(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const* idp) {
216  assert(idp);
217  EventID const& id = *idp;
218  skipBadFiles_ = false;
221  if(offset > 0) {
222  assert(rootFile());
223  while(offset > 0) {
224  bool found = readOneSequentialWithID(cache, fileNameHash, nullptr, idp);
225  if(!found) {
226  return false;
227  }
228  --offset;
229  }
230  }
231  assert(rootFile());
232  if(noMoreFiles() ||
233  rootFile()->indexIntoFileIter().run() != id.run() ||
234  rootFile()->indexIntoFileIter().lumi() != id.luminosityBlock()) {
235  bool found = skipToItem(id.run(), id.luminosityBlock(), 0, 0, false);
236  if(!found) {
237  return false;
238  }
239  }
240  assert(rootFile());
241  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
242  if(found) {
243  found = rootFile()->readCurrentEvent(cache);
244  }
245  if(!found) {
246  found = skipToItemInNewFile(id.run(), id.luminosityBlock(), 0);
247  if(!found) {
248  return false;
249  }
250  return readOneSequentialWithID(cache, fileNameHash, nullptr, idp);
251  }
252  fileNameHash = lfnHash();
253  return true;
254  }
255 
256  void
258  skipBadFiles_ = false;
259  EventID const& id = idx.eventID();
260  bool found = skipToItem(id.run(), id.luminosityBlock(), id.event(), idx.fileNameHash());
261  if(!found) {
262  throw Exception(errors::NotFound) <<
263  "RootEmbeddedFileSequence::readOneSpecified(): Secondary Input files" <<
264  " do not contain specified event:\n" << id << "\n";
265  }
266  assert(rootFile());
267  found = rootFile()->readCurrentEvent(cache);
268  assert(found);
269  fileNameHash = idx.fileNameHash();
270  if(fileNameHash == 0U) {
271  fileNameHash = lfnHash();
272  }
273  }
274 
275  bool
276  RootEmbeddedFileSequence::readOneRandom(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const*) {
277  assert(rootFile());
278  assert(engine);
279  skipBadFiles_ = false;
280  unsigned int currentSeqNumber = sequenceNumberOfFile();
281  while(eventsRemainingInFile_ == 0) {
282 
283  unsigned int newSeqNumber = CLHEP::RandFlat::shootInt(engine, fileCatalogItems().size());
284  setAtFileSequenceNumber(newSeqNumber);
285  if(newSeqNumber != currentSeqNumber) {
286  initFile(false);
287  currentSeqNumber = newSeqNumber;
288  }
289  eventsRemainingInFile_ = rootFile()->eventTree().entries();
290  if(eventsRemainingInFile_ == 0) {
291  throw Exception(errors::NotFound) <<
292  "RootEmbeddedFileSequence::readOneRandom(): Secondary Input file " << fileName() << " contains no events.\n";
293  }
294  rootFile()->setAtEventEntry(CLHEP::RandFlat::shootInt(engine, eventsRemainingInFile_) - 1);
295  }
296  rootFile()->nextEventEntry();
297 
298  bool found = rootFile()->readCurrentEvent(cache);
299  if(!found) {
300  rootFile()->setAtEventEntry(0);
301  bool found = rootFile()->readCurrentEvent(cache);
302  assert(found);
303  }
304  fileNameHash = lfnHash();
306  return true;
307  }
308 
309  bool
310  RootEmbeddedFileSequence::readOneRandomWithID(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const* idp) {
311  assert(engine);
312  assert(idp);
313  EventID const& id = *idp;
314  skipBadFiles_ = false;
315  if(noMoreFiles() || !rootFile() ||
316  rootFile()->indexIntoFileIter().run() != id.run() ||
317  rootFile()->indexIntoFileIter().lumi() != id.luminosityBlock()) {
318  bool found = skipToItem(id.run(), id.luminosityBlock(), 0);
319  if(!found) {
320  return false;
321  }
322  int eventsInLumi = 0;
323  assert(rootFile());
324  while(rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock())) ++eventsInLumi;
325  found = skipToItem(id.run(), id.luminosityBlock(), 0);
326  assert(found);
327  int eventInLumi = CLHEP::RandFlat::shootInt(engine, eventsInLumi);
328  for(int i = 0; i < eventInLumi; ++i) {
329  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
330  assert(found);
331  }
332  }
333  assert(rootFile());
334  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
335  if(found) {
336  found = rootFile()->readCurrentEvent(cache);
337  }
338  if(!found) {
339  bool found = rootFile()->setEntryAtItem(id.run(), id.luminosityBlock(), 0);
340  if(!found) {
341  return false;
342  }
343  return readOneRandomWithID(cache, fileNameHash, engine, idp);
344  }
345  fileNameHash = lfnHash();
346  return true;
347  }
348 
349  bool
350  RootEmbeddedFileSequence::readOneEvent(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const* id) {
351  assert(!sameLumiBlock_ || id != nullptr);
352  assert(sequential_ || engine != nullptr);
353  return (this->*fptr_)(cache, fileNameHash, engine, id);
354  }
355 
356  void
358  desc.addUntracked<bool>("sequential", false)
359  ->setComment("True: loopEvents() reads events sequentially from beginning of first file.\n"
360  "False: loopEvents() first reads events beginning at random event. New files also chosen randomly");
361  desc.addUntracked<bool>("sameLumiBlock", false)
362  ->setComment("True: loopEvents() reads events only in same lumi as the specified event.\n"
363  "False: loopEvents() reads events regardless of lumi.");
364  desc.addUntracked<unsigned int>("skipEvents", 0U)
365  ->setComment("Skip the first 'skipEvents' events. Used only if 'sequential' is True and 'sameLumiBlock' is False");
366  desc.addUntracked<bool>("skipBadFiles", false)
367  ->setComment("True: Ignore any missing or unopenable input file.\n"
368  "False: Throw exception if missing or unopenable input file.");
369  desc.addUntracked<bool>("bypassVersionCheck", false)
370  ->setComment("True: Bypass release version check.\n"
371  "False: Throw exception if reading file in a release prior to the release in which the file was written.");
372  desc.addUntracked<int>("treeMaxVirtualSize", -1)
373  ->setComment("Size of ROOT TTree TBasket cache. Affects performance.");
374 
375  ProductSelectorRules::fillDescription(desc, "inputCommands");
376  }
377 }
bool(RootEmbeddedFileSequence::* fptr_)(EventPrincipal &, size_t &, CLHEP::HepRandomEngine *, EventID const *)
int i
Definition: DBlmapReader.cc:9
std::string const & logicalFileName() const
static void fillDescription(ParameterSetDescription &desc, char const *parameterName)
virtual RootFileSharedPtr makeRootFile(std::shared_ptr< InputFile > filePtr) override
ProcessHistoryRegistry & processHistoryRegistryForUpdate()
void initFile(bool skipBadFiles)
void readOneSpecified(EventPrincipal &cache, size_t &fileNameHash, SecondaryEventIDAndFileInfo const &id)
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::string const & fileName() const
void setAtFileSequenceNumber(size_t offset)
tuple lumi
Definition: fjr2json.py:35
assert(m_qm.get())
std::vector< std::shared_ptr< IndexIntoFile > > const & indexesIntoFiles() const
virtual void initFile_(bool skipBadFiles) override
#define nullptr
ProductRegistry & productRegistryUpdate() const
static std::string const input
Definition: EdmProvDump.cc:43
bool readOneRandomWithID(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *id)
bool readOneSequential(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *)
bool readOneRandom(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *)
RootEmbeddedFileSequence(ParameterSet const &pset, EmbeddedRootSource &input, InputFileCatalog const &catalog, unsigned int nStreams)
std::vector< FileCatalogItem > const & fileCatalogItems() const
void initTheFile(bool skipBadFiles, bool deleteIndexIntoFile, InputSource *input, char const *inputTypeName, InputType inputType)
RootFileSharedPtr const & rootFile() const
bool isAvailable() const
Definition: Service.h:46
bool readOneEvent(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *id)
double f[11][100]
ProductSelectorRules productSelectorRules_
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
bool readOneSequentialWithID(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *id)
bool skipToItem(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, size_t fileNameHash=0U, bool currentFileFirst=true)
static void fillDescription(ParameterSetDescription &desc)
static EntryNumber_t const invalidEntry
void addUntrackedParameter(std::string const &name, T const &value)
Definition: ParameterSet.h:208
tuple idx
DEBUGGING if hasattr(process,&quot;trackMonIterativeTracking2012&quot;): print &quot;trackMonIterativeTracking2012 D...
bool skipToItemInNewFile(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event)
tuple skipBadFiles
Definition: example_cfg.py:64
void dropUnwantedBranches_(std::vector< std::string > const &wantedBranches)
void updateFromInput(ProductList const &other)
std::vector< ProcessHistoryID > orderedProcessHistoryIDs_
void skipEntries(unsigned int offset)
volatile std::atomic< bool > shutdown_flag false
std::shared_ptr< RootFile > RootFileSharedPtr
tuple size
Write out results.