test
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 
47  if(noFiles()) {
48  throw Exception(errors::Configuration) << "RootEmbeddedFileSequence no input files specified for secondary input source.\n";
49  }
50  //
51  // The SiteLocalConfig controls the TTreeCache size and the prefetching settings.
53  if(pSLC.isAvailable()) {
54  enablePrefetching_ = pSLC->enablePrefetching();
55  }
56 
57  // Set the pointer to the function that reads an event.
58  if(sameLumiBlock_) {
59  if(sequential_) {
61  } else {
63  }
64  } else {
65  if(sequential_) {
67  } else {
69  }
70  }
71 
72  // For the secondary input source we do not stage in.
73  if(sequential_) {
74  // We open the first file
75  if(!atFirstFile()) {
77  initFile(false);
78  }
79  assert(rootFile());
80  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
81  if(!sameLumiBlock_) {
83  }
84  } else {
85  // We randomly choose the first file to open.
86  // We cannot use the random number service yet.
87  std::ifstream f("/dev/urandom");
88  unsigned int seed;
89  f.read(reinterpret_cast<char*>(&seed), sizeof(seed));
90  std::default_random_engine dre(seed);
91  size_t count = numberOfFiles();
92  std::uniform_int_distribution<int> distribution(0, count - 1);
93  while(!rootFile() && count != 0) {
94  --count;
95  int offset = distribution(dre);
98  }
99  }
100  if(rootFile()) {
101  input_.productRegistryUpdate().updateFromInput(rootFile()->productRegistry()->productList());
102  }
103  }
104 
106  }
107 
108  void
110  closeFile_();
111  }
112 
114  // delete the RootFile object.
115  if(rootFile()) {
116  rootFile().reset();
117  }
118  }
119 
121  initTheFile(skipBadFiles, false, nullptr, "mixingFiles", InputType::SecondarySource);
122  }
123 
125  RootEmbeddedFileSequence::makeRootFile(std::shared_ptr<InputFile> filePtr) {
126  size_t currentIndexIntoFile = sequenceNumberOfFile();
127  return std::make_shared<RootFile>(
128  fileName(),
130  logicalFileName(),
131  filePtr,
132  input_.nStreams(),
134  input_.runHelper(),
139  currentIndexIntoFile,
143  }
144 
145  void
147  // offset is decremented by the number of events actually skipped.
148  bool completed = rootFile()->skipEntries(offset);
149  while(!completed) {
150  setAtNextFile();
151  if(noMoreFiles()) {
152  setAtFirstFile();
153  }
154  initFile(false);
155  assert(rootFile());
156  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
157  completed = rootFile()->skipEntries(offset);
158  }
159  }
160 
161  bool
162  RootEmbeddedFileSequence::readOneSequential(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const*) {
163  assert(rootFile());
164  rootFile()->nextEventEntry();
165  bool found = rootFile()->readCurrentEvent(cache);
166  if(!found) {
167  setAtNextFile();
168  if(noMoreFiles()) {
169  setAtFirstFile();
170  }
171  initFile(false);
172  assert(rootFile());
173  rootFile()->setAtEventEntry(IndexIntoFile::invalidEntry);
174  return readOneSequential(cache, fileNameHash, nullptr, nullptr);
175  }
176  fileNameHash = lfnHash();
177  return true;
178  }
179 
180  bool
181  RootEmbeddedFileSequence::readOneSequentialWithID(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine*, EventID const* idp) {
182  assert(idp);
183  EventID const& id = *idp;
186  if(offset > 0) {
187  assert(rootFile());
188  while(offset > 0) {
189  bool found = readOneSequentialWithID(cache, fileNameHash, nullptr, idp);
190  if(!found) {
191  return false;
192  }
193  --offset;
194  }
195  }
196  assert(rootFile());
197  if(noMoreFiles() ||
198  rootFile()->indexIntoFileIter().run() != id.run() ||
199  rootFile()->indexIntoFileIter().lumi() != id.luminosityBlock()) {
200  bool found = skipToItem(id.run(), id.luminosityBlock(), 0, 0, false);
201  if(!found) {
202  return false;
203  }
204  }
205  assert(rootFile());
206  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
207  if(found) {
208  found = rootFile()->readCurrentEvent(cache);
209  }
210  if(!found) {
211  found = skipToItemInNewFile(id.run(), id.luminosityBlock(), 0);
212  if(!found) {
213  return false;
214  }
215  return readOneSequentialWithID(cache, fileNameHash, nullptr, idp);
216  }
217  fileNameHash = lfnHash();
218  return true;
219  }
220 
221  void
223  EventID const& id = idx.eventID();
224  bool found = skipToItem(id.run(), id.luminosityBlock(), id.event(), idx.fileNameHash());
225  if(!found) {
226  throw Exception(errors::NotFound) <<
227  "RootEmbeddedFileSequence::readOneSpecified(): Secondary Input files" <<
228  " do not contain specified event:\n" << id << "\n";
229  }
230  assert(rootFile());
231  found = rootFile()->readCurrentEvent(cache);
232  assert(found);
233  fileNameHash = idx.fileNameHash();
234  if(fileNameHash == 0U) {
235  fileNameHash = lfnHash();
236  }
237  }
238 
239  bool
240  RootEmbeddedFileSequence::readOneRandom(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const*) {
241  assert(rootFile());
242  assert(engine);
243  unsigned int currentSeqNumber = sequenceNumberOfFile();
244  while(eventsRemainingInFile_ == 0) {
245 
246  unsigned int newSeqNumber = CLHEP::RandFlat::shootInt(engine, fileCatalogItems().size());
247  setAtFileSequenceNumber(newSeqNumber);
248  if(newSeqNumber != currentSeqNumber) {
249  initFile(false);
250  currentSeqNumber = newSeqNumber;
251  }
252  eventsRemainingInFile_ = rootFile()->eventTree().entries();
253  if(eventsRemainingInFile_ == 0) {
254  throw Exception(errors::NotFound) <<
255  "RootEmbeddedFileSequence::readOneRandom(): Secondary Input file " << fileName() << " contains no events.\n";
256  }
257  rootFile()->setAtEventEntry(CLHEP::RandFlat::shootInt(engine, eventsRemainingInFile_) - 1);
258  }
259  rootFile()->nextEventEntry();
260 
261  bool found = rootFile()->readCurrentEvent(cache);
262  if(!found) {
263  rootFile()->setAtEventEntry(0);
264  bool found = rootFile()->readCurrentEvent(cache);
265  assert(found);
266  }
267  fileNameHash = lfnHash();
269  return true;
270  }
271 
272  bool
273  RootEmbeddedFileSequence::readOneRandomWithID(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const* idp) {
274  assert(engine);
275  assert(idp);
276  EventID const& id = *idp;
277  if(noMoreFiles() || !rootFile() ||
278  rootFile()->indexIntoFileIter().run() != id.run() ||
279  rootFile()->indexIntoFileIter().lumi() != id.luminosityBlock()) {
280  bool found = skipToItem(id.run(), id.luminosityBlock(), 0);
281  if(!found) {
282  return false;
283  }
284  int eventsInLumi = 0;
285  assert(rootFile());
286  while(rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock())) ++eventsInLumi;
287  found = skipToItem(id.run(), id.luminosityBlock(), 0);
288  assert(found);
289  int eventInLumi = CLHEP::RandFlat::shootInt(engine, eventsInLumi);
290  for(int i = 0; i < eventInLumi; ++i) {
291  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
292  assert(found);
293  }
294  }
295  assert(rootFile());
296  bool found = rootFile()->setEntryAtNextEventInLumi(id.run(), id.luminosityBlock());
297  if(found) {
298  found = rootFile()->readCurrentEvent(cache);
299  }
300  if(!found) {
301  bool found = rootFile()->setEntryAtItem(id.run(), id.luminosityBlock(), 0);
302  if(!found) {
303  return false;
304  }
305  return readOneRandomWithID(cache, fileNameHash, engine, idp);
306  }
307  fileNameHash = lfnHash();
308  return true;
309  }
310 
311  bool
312  RootEmbeddedFileSequence::readOneEvent(EventPrincipal& cache, size_t& fileNameHash, CLHEP::HepRandomEngine* engine, EventID const* id) {
313  assert(!sameLumiBlock_ || id != nullptr);
314  assert(sequential_ || engine != nullptr);
315  return (this->*fptr_)(cache, fileNameHash, engine, id);
316  }
317 
318  void
320  desc.addUntracked<bool>("sequential", false)
321  ->setComment("True: loopEvents() reads events sequentially from beginning of first file.\n"
322  "False: loopEvents() first reads events beginning at random event. New files also chosen randomly");
323  desc.addUntracked<bool>("sameLumiBlock", false)
324  ->setComment("True: loopEvents() reads events only in same lumi as the specified event.\n"
325  "False: loopEvents() reads events regardless of lumi.");
326  desc.addUntracked<unsigned int>("skipEvents", 0U)
327  ->setComment("Skip the first 'skipEvents' events. Used only if 'sequential' is True and 'sameLumiBlock' is False");
328  }
329 }
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)