Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DataFormats/Provenance/interface/EventRange.h"
0002 #include "FWCore/Utilities/interface/Algorithms.h"
0003 #include <cassert>
0004 #include <ostream>
0005 //#include <limits>
0006 
0007 namespace edm {
0008   EventRange::EventRange()
0009       :  // Special cases since 0 means maximum
0010         startEventID_(0U, 0U, EventID::maxEventNumber()),
0011         endEventID_(0U, 0U, EventID::maxEventNumber()) {}
0012 
0013   EventRange::EventRange(RunNumber_t startRun,
0014                          LuminosityBlockNumber_t startLumi,
0015                          EventNumber_t startEvent,
0016                          RunNumber_t endRun,
0017                          LuminosityBlockNumber_t endLumi,
0018                          EventNumber_t endEvent)
0019       :  // Special cases since 0 means maximum
0020         startEventID_(startRun, startLumi, startEvent != 0 ? startEvent : EventID::maxEventNumber()),
0021         endEventID_(endRun, endLumi, endEvent != 0 ? endEvent : EventID::maxEventNumber()) {
0022     assert((startLumi == 0) == (endLumi == 0));
0023   }
0024 
0025   EventRange::EventRange(EventID const& begin, EventID const& end) : startEventID_(begin), endEventID_(end) {}
0026 
0027   std::ostream& operator<<(std::ostream& oStream, EventRange const& r) {
0028     if (r.startLumi() == 0) {
0029       oStream << "'" << r.startRun() << ":" << r.startEvent() << "-" << r.endRun() << ":" << r.endEvent() << "'";
0030     } else {
0031       oStream << "'" << r.startRun() << ":" << r.startLumi() << ":" << r.startEvent() << "-" << r.endRun() << ":"
0032               << r.endLumi() << ":" << r.endEvent() << "'";
0033     }
0034     return oStream;
0035   }
0036 
0037   bool contains(EventRange const& lh, EventID const& rh) {
0038     if (lh.startLumi() == 0) {
0039       return (contains_(lh, EventID(rh.run(), 0U, rh.event())));
0040     }
0041     return (contains_(lh, rh));
0042   }
0043 
0044   bool contains_(EventRange const& lh, EventID const& rh) { return (rh >= lh.startEventID() && rh <= lh.endEventID()); }
0045 
0046   bool contains(EventRange const& lh, EventRange const& rh) {
0047     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
0048     return (contains(lh, rh.startEventID()) && contains(lh, rh.endEventID()));
0049   }
0050 
0051   bool overlaps(EventRange const& lh, EventRange const& rh) {
0052     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
0053     return !distinct(lh, rh);
0054   }
0055 
0056   bool lessThanSpecial(EventRange const& lh, EventRange const& rh) {
0057     // First, separate the ranges so that those with 0 lumiID go first.
0058     if ((lh.startLumi() == 0) != (rh.startLumi() == 0)) {
0059       return lh.startLumi() == 0;
0060     }
0061     return lh.endEventID() < rh.startEventID();
0062   }
0063 
0064   bool lessThan(EventRange const& lh, EventRange const& rh) {
0065     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
0066     return lh.endEventID() < rh.startEventID();
0067   }
0068 
0069   bool distinct(EventRange const& lh, EventRange const& rh) {
0070     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
0071     return lessThan(lh, rh) || lessThan(rh, lh);
0072   }
0073 
0074   namespace {
0075     bool mergeSpecial(EventRange& lh, EventRange& rh) {
0076       // Don't combine a range with 0 lumiID with a range with non-zero lumiID.
0077       if ((lh.startLumi() == 0) != (rh.startLumi() == 0)) {
0078         return false;
0079       }
0080       if (overlaps(lh, rh)) {
0081         EventID begin = min(lh.startEventID(), rh.startEventID());
0082         EventID end = max(lh.endEventID(), rh.endEventID());
0083         rh = lh = EventRange(begin, end);
0084         return true;
0085       }
0086       return false;
0087     }
0088 
0089     bool sortByStartEventIDSpecial(EventRange const& lh, EventRange const& rh) {
0090       // First, separate the ranges so that those with 0 lumiID go first.
0091       if ((lh.startLumi() == 0) != (rh.startLumi() == 0)) {
0092         return lh.startLumi() == 0;
0093       }
0094       return lh.startEventID() < rh.startEventID();
0095     }
0096   }  // namespace
0097 
0098   std::vector<EventRange>& sortAndRemoveOverlaps(std::vector<EventRange>& eventRange) {
0099     if (eventRange.size() <= 1U)
0100       return eventRange;
0101     sort_all(eventRange, sortByStartEventIDSpecial);
0102     for (std::vector<EventRange>::iterator i = eventRange.begin() + 1, e = eventRange.end(); i != e; ++i) {
0103       std::vector<EventRange>::iterator iprev = i - 1;
0104       if (mergeSpecial(*iprev, *i)) {
0105         i = eventRange.erase(iprev);
0106         e = eventRange.end();
0107       }
0108     }
0109     return eventRange;
0110   }
0111 }  // namespace edm