Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:54:31

0001 #include "DataFormats/Provenance/interface/FileIndex.h"
0002 #include "FWCore/Utilities/interface/Algorithms.h"
0003 
0004 #include <algorithm>
0005 #include <iomanip>
0006 #include <ostream>
0007 
0008 namespace edm {
0009 
0010   FileIndex::FileIndex() : entries_(), transient_() {}
0011 
0012   // The default value for sortState_ reflects the fact that
0013   // the index is always sorted using Run, Lumi, and Event
0014   // number by the PoolOutputModule before being written out.
0015   // In the other case when we create a new FileIndex, the
0016   // vector is empty, which is consistent with it having been
0017   // sorted.
0018 
0019   FileIndex::Transients::Transients()
0020       : allInEntryOrder_(false), resultCached_(false), sortState_(kSorted_Run_Lumi_Event) {}
0021 
0022   void FileIndex::Transients::reset() {
0023     allInEntryOrder_ = false;
0024     resultCached_ = false;
0025     sortState_ = kSorted_Run_Lumi_Event;
0026   }
0027 
0028   void FileIndex::addEntry(RunNumber_t run, LuminosityBlockNumber_t lumi, EventNumber_t event, EntryNumber_t entry) {
0029     entries_.emplace_back(run, lumi, event, entry);
0030     resultCached() = false;
0031     sortState() = kNotSorted;
0032   }
0033 
0034   void FileIndex::sortBy_Run_Lumi_Event() {
0035     stable_sort_all(entries_);
0036     resultCached() = false;
0037     sortState() = kSorted_Run_Lumi_Event;
0038   }
0039 
0040   void FileIndex::sortBy_Run_Lumi_EventEntry() {
0041     stable_sort_all(entries_, Compare_Run_Lumi_EventEntry());
0042     resultCached() = false;
0043     sortState() = kSorted_Run_Lumi_EventEntry;
0044   }
0045 
0046   bool FileIndex::allEventsInEntryOrder() const {
0047     if (!resultCached()) {
0048       resultCached() = true;
0049       EntryNumber_t maxEntry = Element::invalidEntry;
0050       for (std::vector<FileIndex::Element>::const_iterator it = entries_.begin(), itEnd = entries_.end(); it != itEnd;
0051            ++it) {
0052         if (it->getEntryType() == kEvent) {
0053           if (it->entry_ < maxEntry) {
0054             allInEntryOrder() = false;
0055             return false;
0056           }
0057           maxEntry = it->entry_;
0058         }
0059       }
0060       allInEntryOrder() = true;
0061       return true;
0062     }
0063     return allInEntryOrder();
0064   }
0065 
0066   FileIndex::const_iterator FileIndex::findPosition(RunNumber_t run,
0067                                                     LuminosityBlockNumber_t lumi,
0068                                                     EventNumber_t event) const {
0069     assert(sortState() != kNotSorted);
0070 
0071     const_iterator itEnd = entries_.end();
0072     const_iterator it;
0073     Element el(run, lumi, event);
0074     if (sortState() == kSorted_Run_Lumi_Event) {
0075       it = lower_bound_all(entries_, el);
0076       bool lumiMissing = (lumi == 0 && event != 0);
0077       if (lumiMissing) {
0078         while (it != itEnd && it->run_ < run) {
0079           ++it;
0080         }
0081         while (it != itEnd && (it->run_ == run && it->event_ < event)) {
0082           ++it;
0083         }
0084       }
0085     } else {
0086       it = lower_bound_all(entries_, el, Compare_Run_Lumi_EventEntry());
0087     }
0088     return it;
0089   }
0090 
0091   FileIndex::const_iterator FileIndex::findEventPosition(RunNumber_t run,
0092                                                          LuminosityBlockNumber_t lumi,
0093                                                          EventNumber_t event) const {
0094     const_iterator it = findPosition(run, lumi, event);
0095     const_iterator itEnd = entries_.end();
0096     while (it != itEnd && it->getEntryType() != FileIndex::kEvent) {
0097       ++it;
0098     }
0099     if (it == itEnd) {
0100       return itEnd;
0101     }
0102     if (lumi == 0) {
0103       lumi = it->lumi_;
0104     }
0105     if (it->run_ != run || it->lumi_ != lumi || it->event_ != event) {
0106       if (sortState() == kSorted_Run_Lumi_Event) {
0107         return itEnd;
0108       }
0109       // not sorted by event, so we need to do a linear search
0110       while (it != itEnd && it->run_ == run && it->lumi_ == lumi && it->event_ != event) {
0111         ++it;
0112       }
0113       if (it->run_ != run || it->lumi_ != lumi || it->event_ != event) {
0114         return itEnd;
0115       }
0116     }
0117     return it;
0118   }
0119 
0120   FileIndex::const_iterator FileIndex::findLumiPosition(RunNumber_t run, LuminosityBlockNumber_t lumi) const {
0121     const_iterator it = findPosition(run, lumi, 0U);
0122     const_iterator itEnd = entries_.end();
0123     while (it != itEnd && it->getEntryType() != FileIndex::kLumi) {
0124       ++it;
0125     }
0126     if (it == itEnd) {
0127       return itEnd;
0128     }
0129     if (it->run_ != run || it->lumi_ != lumi) {
0130       return itEnd;
0131     }
0132     return it;
0133   }
0134 
0135   FileIndex::const_iterator FileIndex::findRunPosition(RunNumber_t run) const {
0136     const_iterator it = findPosition(run, 0U, 0U);
0137     const_iterator itEnd = entries_.end();
0138     while (it != itEnd && it->getEntryType() != FileIndex::kRun) {
0139       ++it;
0140     }
0141     if (it == itEnd) {
0142       return itEnd;
0143     }
0144     if (it->run_ != run) {
0145       return itEnd;
0146     }
0147     return it;
0148   }
0149 
0150   FileIndex::const_iterator FileIndex::findLumiOrRunPosition(RunNumber_t run, LuminosityBlockNumber_t lumi) const {
0151     const_iterator it = findPosition(run, lumi, 0U);
0152     const_iterator itEnd = entries_.end();
0153     while (it != itEnd && it->getEntryType() != FileIndex::kLumi && it->getEntryType() != FileIndex::kRun) {
0154       ++it;
0155     }
0156     return it;
0157   }
0158 
0159   FileIndex::const_iterator FileIndex::findEventEntryPosition(RunNumber_t run,
0160                                                               LuminosityBlockNumber_t lumi,
0161                                                               EventNumber_t event,
0162                                                               EntryNumber_t entry) const {
0163     assert(sortState() != kNotSorted);
0164     const_iterator it;
0165     const_iterator itEnd = entries_.end();
0166     if (sortState() == kSorted_Run_Lumi_EventEntry) {
0167       assert(lumi != 0U);
0168       Element el(run, lumi, event, entry);
0169       it = lower_bound_all(entries_, el, Compare_Run_Lumi_EventEntry());
0170     } else {
0171       it = findEventPosition(run, lumi, event);
0172       while (it != itEnd && it->entry_ != entry && it->event_ == event) {
0173         ++it;
0174       }
0175     }
0176     if (it == itEnd)
0177       return itEnd;
0178     if (lumi == 0)
0179       lumi = it->lumi_;
0180     if (it->run_ != run || it->lumi_ != lumi || it->event_ != event || it->entry_ != entry)
0181       return itEnd;
0182     return it;
0183   }
0184 
0185   bool operator<(FileIndex::Element const& lh, FileIndex::Element const& rh) {
0186     if (lh.run_ == rh.run_) {
0187       if (lh.lumi_ == rh.lumi_) {
0188         return lh.event_ < rh.event_;
0189       }
0190       return lh.lumi_ < rh.lumi_;
0191     }
0192     return lh.run_ < rh.run_;
0193   }
0194 
0195   bool Compare_Run_Lumi_EventEntry::operator()(FileIndex::Element const& lh, FileIndex::Element const& rh) {
0196     if (lh.run_ == rh.run_) {
0197       if (lh.lumi_ == rh.lumi_) {
0198         if (lh.event_ == 0U && rh.event_ == 0U) {
0199           return false;
0200         } else if (lh.event_ == 0U) {
0201           return true;
0202         } else if (rh.event_ == 0U) {
0203           return false;
0204         } else {
0205           return lh.entry_ < rh.entry_;
0206         }
0207       }
0208       return lh.lumi_ < rh.lumi_;
0209     }
0210     return lh.run_ < rh.run_;
0211   }
0212 
0213   std::ostream& operator<<(std::ostream& os, FileIndex const& fileIndex) {
0214     os << "\nPrinting FileIndex contents.  This includes a list of all Runs, LuminosityBlocks\n"
0215        << "and Events stored in the root file.\n\n";
0216     os << std::setw(15) << "Run" << std::setw(15) << "Lumi" << std::setw(15) << "Event" << std::setw(15)
0217        << "TTree Entry"
0218        << "\n";
0219     for (std::vector<FileIndex::Element>::const_iterator it = fileIndex.begin(), itEnd = fileIndex.end(); it != itEnd;
0220          ++it) {
0221       if (it->getEntryType() == FileIndex::kEvent) {
0222         os << std::setw(15) << it->run_ << std::setw(15) << it->lumi_ << std::setw(15) << it->event_ << std::setw(15)
0223            << it->entry_ << "\n";
0224       } else if (it->getEntryType() == FileIndex::kLumi) {
0225         os << std::setw(15) << it->run_ << std::setw(15) << it->lumi_ << std::setw(15) << " " << std::setw(15)
0226            << it->entry_ << "  (LuminosityBlock)"
0227            << "\n";
0228       } else if (it->getEntryType() == FileIndex::kRun) {
0229         os << std::setw(15) << it->run_ << std::setw(15) << " " << std::setw(15) << " " << std::setw(15) << it->entry_
0230            << "  (Run)"
0231            << "\n";
0232       }
0233     }
0234     return os;
0235   }
0236 }  // namespace edm