CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DataFormats/Provenance/src/EventRange.cc

Go to the documentation of this file.
00001 #include "DataFormats/Provenance/interface/EventRange.h"
00002 #include "FWCore/Utilities/interface/Algorithms.h"
00003 #include <cassert>
00004 #include <ostream>
00005 //#include <limits>
00006 
00007 namespace edm {
00008   EventRange::EventRange() :
00009       // Special cases since 0 means maximum
00010       startEventID_(0U, 0U, EventID::maxEventNumber()),
00011       endEventID_(0U, 0U, EventID::maxEventNumber()) {
00012   }
00013 
00014   EventRange::EventRange(RunNumber_t startRun, LumiNumber_t startLumi, EventNumber_t startEvent,
00015                          RunNumber_t endRun, LumiNumber_t endLumi, EventNumber_t endEvent) :
00016       // Special cases since 0 means maximum
00017       startEventID_(startRun, startLumi, startEvent != 0 ? startEvent : EventID::maxEventNumber()),
00018       endEventID_(endRun, endLumi, endEvent != 0 ? endEvent : EventID::maxEventNumber()) {
00019     assert((startLumi == 0) == (endLumi == 0));
00020   }
00021 
00022   EventRange::EventRange(EventID const& begin, EventID const& end) :
00023       startEventID_(begin),
00024       endEventID_(end) {
00025   }
00026 
00027   std::ostream& operator<<(std::ostream& oStream, EventRange const& r) {
00028     if (r.startLumi() == 0) {
00029       oStream << "'" << r.startRun() << ":" << r.startEvent() << "-"
00030                      << r.endRun() << ":" << r.endEvent() << "'";
00031     } else {
00032       oStream << "'" << r.startRun() << ":" << r.startLumi() << ":" << r.startEvent() << "-"
00033                      << r.endRun() << ":" << r.endLumi() << ":" << r.endEvent() << "'";
00034     }
00035     return oStream;
00036   }
00037 
00038   bool contains(EventRange const& lh, EventID const& rh) {
00039     if (lh.startLumi() == 0) {
00040       return (contains_(lh, EventID(rh.run(), 0U, rh.event())));
00041     }
00042     return (contains_(lh, rh));
00043   }
00044 
00045   bool contains_(EventRange const& lh, EventID const& rh) {
00046     return (rh >= lh.startEventID() && rh <= lh.endEventID());
00047   }
00048 
00049   bool contains(EventRange const& lh, EventRange const& rh) {
00050     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
00051     return (contains(lh, rh.startEventID()) && contains(lh, rh.endEventID()));
00052   }
00053 
00054   bool overlaps(EventRange const& lh, EventRange const& rh) {
00055     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
00056     return !distinct(lh, rh);
00057   }
00058 
00059   bool lessThanSpecial(EventRange const& lh, EventRange const& rh) {
00060     // First, separate the ranges so that those with 0 lumiID go first.
00061     if ((lh.startLumi() == 0) != (rh.startLumi() == 0)) {
00062       return lh.startLumi() == 0;
00063     }
00064     return lh.endEventID() < rh.startEventID();
00065   }
00066 
00067   bool lessThan(EventRange const& lh, EventRange const& rh) {
00068     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
00069     return lh.endEventID() < rh.startEventID();
00070   }
00071 
00072   bool distinct(EventRange const& lh, EventRange const& rh) {
00073     assert((lh.startLumi() == 0) == (rh.startLumi() == 0));
00074     return lessThan(lh, rh) || lessThan(rh, lh); 
00075   }
00076 
00077   namespace {
00078     bool mergeSpecial(EventRange& lh, EventRange& rh) {
00079       // Don't combine a range with 0 lumiID with a range with non-zero lumiID.
00080       if ((lh.startLumi() == 0) != (rh.startLumi() == 0)) {
00081         return false;
00082       }
00083       if (overlaps(lh, rh)) {
00084         EventID begin = min(lh.startEventID(), rh.startEventID());
00085         EventID end = max(lh.endEventID(), rh.endEventID());
00086         rh = lh = EventRange(begin, end);
00087         return true;
00088       }
00089       return false;
00090     }
00091 
00092     bool sortByStartEventIDSpecial(EventRange const& lh, EventRange const& rh) {
00093       // First, separate the ranges so that those with 0 lumiID go first.
00094       if ((lh.startLumi() == 0) != (rh.startLumi() == 0)) {
00095         return lh.startLumi() == 0;
00096       }
00097       return lh.startEventID() < rh.startEventID();
00098     }
00099   }
00100 
00101   std::vector<EventRange>&
00102   sortAndRemoveOverlaps(std::vector<EventRange>& eventRange) {
00103     if (eventRange.size() <= 1U) return eventRange;
00104     sort_all(eventRange, sortByStartEventIDSpecial);
00105     for (std::vector<EventRange>::iterator i = eventRange.begin() + 1, e = eventRange.end();
00106         i != e; ++i) {
00107       std::vector<EventRange>::iterator iprev = i - 1;
00108       if (mergeSpecial(*iprev, *i)) {
00109         i = eventRange.erase(iprev);
00110         e = eventRange.end();
00111       }
00112     }
00113     return eventRange;
00114   }
00115 }