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