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 
29  : RootInputFileSequence(pset, catalog),
30  input_(input),
31  orderedProcessHistoryIDs_(),
32  sequential_(pset.getUntrackedParameter<bool>("sequential", false)),
33  sameLumiBlock_(pset.getUntrackedParameter<bool>("sameLumiBlock", false)),
34  fptr_(nullptr),
35  eventsRemainingInFile_(0),
36  // The default value provided as the second argument to the getUntrackedParameter function call
37  // is not used when the ParameterSet has been validated and the parameters are not optional
38  // in the description. This is currently true when PoolSource is the primary input source.
39  // The modules that use PoolSource as a SecSource have not defined their fillDescriptions function
40  // yet, so the ParameterSet does not get validated yet. As soon as all the modules with a SecSource
41  // have defined descriptions, the defaults in the getUntrackedParameterSet function calls can
42  // and should be deleted from the code.
43  initialNumberOfEventsToSkip_(pset.getUntrackedParameter<unsigned int>("skipEvents", 0U)),
44  enablePrefetching_(false),
45  enforceGUIDInFileName_(pset.getUntrackedParameter<bool>("enforceGUIDInFileName", false)) {
46  if (noFiles()) {
47  throw Exception(errors::Configuration) << "RootEmbeddedFileSequence no input files specified for secondary input source.\n";
48  }
49  //
50  // The SiteLocalConfig controls the TTreeCache size and the prefetching settings.
52  if (pSLC.isAvailable()) {
53  enablePrefetching_ = pSLC->enablePrefetching();
54  }
55 
56  // Set the pointer to the function that reads an event.
57  if (sameLumiBlock_) {
58  if (sequential_) {
60  } else {
62  }
63  } else {
64  if (sequential_) {
66  } else {
68  }
69  }
70 
71  // For the secondary input source we do not stage in.
72  if (sequential_) {
73  // We open the first file
74  if (!atFirstFile()) {
76  initFile(false);
77  }
78  assert(rootFile());
79  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
80  if (!sameLumiBlock_) {
82  }
83  } else {
84  // We randomly choose the first file to open.
85  // We cannot use the random number service yet.
86  std::ifstream f("/dev/urandom");
87  unsigned int seed;
88  f.read(reinterpret_cast<char*>(&seed), sizeof(seed));
89  std::default_random_engine dre(seed);
90  size_t count = numberOfFiles();
91  std::uniform_int_distribution<int> distribution(0, count - 1);
92  while (!rootFile() && count != 0) {
93  --count;
94  int offset = distribution(dre);
97  }
98  }
99  if (rootFile()) {
100  input_.productRegistryUpdate().updateFromInput(rootFile()->productRegistry()->productList());
101  }
102  }
103 
105 
107 
109  // delete the RootFile object.
110  if (rootFile()) {
111  rootFile().reset();
112  }
113  }
114 
116  initTheFile(skipBadFiles, false, nullptr, "mixingFiles", InputType::SecondarySource);
117  }
118 
120  std::shared_ptr<InputFile> filePtr) {
121  size_t currentIndexIntoFile = sequenceNumberOfFile();
122  return std::make_shared<RootFile>(fileName(),
124  logicalFileName(),
125  filePtr,
126  input_.nStreams(),
128  input_.runHelper(),
133  currentIndexIntoFile,
138  }
139 
141  // offset is decremented by the number of events actually skipped.
142  bool completed = rootFile()->skipEntries(offset);
143  while (!completed) {
144  setAtNextFile();
145  if (noMoreFiles()) {
146  setAtFirstFile();
147  }
148  initFile(false);
149  assert(rootFile());
150  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
151  completed = rootFile()->skipEntries(offset);
152  }
153  }
154 
155  bool
156  RootEmbeddedFileSequence::readOneSequential(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const*) {
157  assert(rootFile());
158  rootFile()->nextEventEntry();
159  bool found = rootFile()->readCurrentEvent(cache);
160  if (!found) {
161  setAtNextFile();
162  if (noMoreFiles()) {
163  setAtFirstFile();
164  }
165  initFile(false);
166  assert(rootFile());
167  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
168  return readOneSequential(cache, fileNameHash, nullptr, nullptr);
169  }
170  fileNameHash = lfnHash();
171  return true;
172  }
173 
174  bool
175  RootEmbeddedFileSequence::readOneSequentialWithID(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const* idp) {
176  assert(idp);
177  EventID const& id = *idp;
180  if (offset > 0) {
181  assert(rootFile());
182  while (offset > 0) {
183  bool found = readOneSequentialWithID(cache, fileNameHash, nullptr, idp);
184  if (!found) {
185  return false;
186  }
187  --offset;
188  }
189  }
190  assert(rootFile());
191  if (noMoreFiles() || rootFile()->indexIntoFileIter().run() != id.run() ||
192  rootFile()->indexIntoFileIter().lumi() != id.luminosityBlock()) {
193  bool found = skipToItem(id.run(), id.luminosityBlock(), 0, 0, false);
194  if (!found) {
195  return false;
196  }
197  }
198  assert(rootFile());
199  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
200  if (found) {
201  found = rootFile()->readCurrentEvent(cache);
202  }
203  if (!found) {
204  found = skipToItemInNewFile(id.run(), id.luminosityBlock(), 0);
205  if (!found) {
206  return false;
207  }
208  return readOneSequentialWithID(cache, fileNameHash, nullptr, idp);
209  }
210  fileNameHash = lfnHash();
211  return true;
212  }
213 
215  size_t& fileNameHash,
217  EventID const& id = idx.eventID();
218  bool found = skipToItem(id.run(), id.luminosityBlock(), id.event(), idx.fileNameHash());
219  if (!found) {
220  throw Exception(errors::NotFound) << "RootEmbeddedFileSequence::readOneSpecified(): Secondary Input files"
221  << " do not contain specified event:\n"
222  << id << "\n";
223  }
224  assert(rootFile());
225  found = rootFile()->readCurrentEvent(cache);
226  assert(found);
227  fileNameHash = idx.fileNameHash();
228  if (fileNameHash == 0U) {
229  fileNameHash = lfnHash();
230  }
231  }
232 
233  bool
234  RootEmbeddedFileSequence::readOneRandom(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const*) {
235  assert(rootFile());
236  assert(engine);
237  unsigned int currentSeqNumber = sequenceNumberOfFile();
238  while (eventsRemainingInFile_ == 0) {
239  unsigned int newSeqNumber = CLHEP::RandFlat::shootInt(engine, fileCatalogItems().size());
240  setAtFileSequenceNumber(newSeqNumber);
241  if (newSeqNumber != currentSeqNumber) {
242  initFile(false);
243  currentSeqNumber = newSeqNumber;
244  }
245  eventsRemainingInFile_ = rootFile()->eventTree().entries();
246  if (eventsRemainingInFile_ == 0) {
247  throw Exception(errors::NotFound) << "RootEmbeddedFileSequence::readOneRandom(): Secondary Input file "
248  << fileName() << " contains no events.\n";
249  }
250  rootFile()->setAtEventEntry(CLHEP::RandFlat::shootInt(engine, eventsRemainingInFile_) - 1);
251  }
252  rootFile()->nextEventEntry();
253 
254  bool found = rootFile()->readCurrentEvent(cache);
255  if (!found) {
256  rootFile()->setAtEventEntry(0);
257  bool found = rootFile()->readCurrentEvent(cache);
258  assert(found);
259  }
260  fileNameHash = lfnHash();
262  return true;
263  }
264 
265  bool
266  RootEmbeddedFileSequence::readOneRandomWithID(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const* idp) {
267  assert(engine);
268  assert(idp);
269  EventID const& id = *idp;
270  if (noMoreFiles() || !rootFile() || rootFile()->indexIntoFileIter().run() != id.run() ||
271  rootFile()->indexIntoFileIter().lumi() != id.luminosityBlock()) {
272  bool found = skipToItem(id.run(), id.luminosityBlock(), 0);
273  if (!found) {
274  return false;
275  }
276  int eventsInLumi = 0;
277  assert(rootFile());
278  while (rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock()))
279  ++eventsInLumi;
280  found = skipToItem(id.run(), id.luminosityBlock(), 0);
281  assert(found);
282  int eventInLumi = CLHEP::RandFlat::shootInt(engine, eventsInLumi);
283  for (int i = 0; i < eventInLumi; ++i) {
284  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
285  assert(found);
286  }
287  }
288  assert(rootFile());
289  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
290  if (found) {
291  found = rootFile()->readCurrentEvent(cache);
292  }
293  if (!found) {
294  bool found = rootFile()->setEntryAtItem(id.run(), id.luminosityBlock(), 0);
295  if (!found) {
296  return false;
297  }
298  return readOneRandomWithID(cache, fileNameHash, engine, idp);
299  }
300  fileNameHash = lfnHash();
301  return true;
302  }
303 
304  bool
305  RootEmbeddedFileSequence::readOneEvent(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const* id) {
306  assert(!sameLumiBlock_ || id != nullptr);
307  assert(sequential_ || engine != nullptr);
308  return (this->*fptr_)(cache, fileNameHash, engine, id);
309  }
310 
312  desc.addUntracked<bool>("sequential", false)
313  ->setComment(
314  "True: loopEvents() reads events sequentially from beginning of first file.\n"
315  "False: loopEvents() first reads events beginning at random event. New files also chosen randomly");
316  desc.addUntracked<bool>("sameLumiBlock", false)
317  ->setComment(
318  "True: loopEvents() reads events only in same lumi as the specified event.\n"
319  "False: loopEvents() reads events regardless of lumi.");
320  desc.addUntracked<unsigned int>("skipEvents", 0U)
321  ->setComment(
322  "Skip the first 'skipEvents' events. Used only if 'sequential' is True and 'sameLumiBlock' is False");
323  desc.addUntracked<bool>("enforceGUIDInFileName", false)
324  ->setComment(
325  "True: file name part is required to be equal to the GUID of the file\n"
326  "False: file name can be anything");
327  }
328 } // namespace edm
bool(RootEmbeddedFileSequence::* fptr_)(EventPrincipal &, size_t &, CLHEP::HepRandomEngine *, EventID const *)
ProductRegistry & productRegistryUpdate()
int i
Definition: DBlmapReader.cc:9
std::string const & logicalFileName() const
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)
unsigned int nStreams() const
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
bool bypassVersionCheck() const
static std::string const input
Definition: EdmProvDump.cc:44
bool readOneRandomWithID(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *id)
std::shared_ptr< RootFile > RootFileSharedPtr
bool readOneSequential(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *)
bool readOneRandom(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *)
std::vector< FileCatalogItem > const & fileCatalogItems() const
RunHelperBase * runHelper()
void initTheFile(bool skipBadFiles, bool deleteIndexIntoFile, InputSource *input, char const *inputTypeName, InputType inputType)
bool isAvailable() const
Definition: Service.h:46
bool readOneEvent(EventPrincipal &cache, size_t &fileNameHash, CLHEP::HepRandomEngine *, EventID const *id)
double f[11][100]
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)
std::shared_ptr< RootFile const > rootFile() const
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
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
ProductSelectorRules const & productSelectorRules() const
void updateFromInput(ProductList const &other)
std::vector< ProcessHistoryID > orderedProcessHistoryIDs_
void skipEntries(unsigned int offset)
volatile std::atomic< bool > shutdown_flag false
tuple size
Write out results.
RootEmbeddedFileSequence(ParameterSet const &pset, EmbeddedRootSource &input, InputFileCatalog const &catalog)