Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#include "CondCore/CondDB/interface/Time.h"
#include "CondCore/CondDB/interface/Exception.h"
#include "CondCore/CondDB/interface/Types.h"
//
#include "DataFormats/Provenance/interface/LuminosityBlockID.h"
//
#include <initializer_list>
#include <vector>
#include <map>

namespace cond {

  namespace time {
    static const std::pair<const char*, TimeType> s_timeTypeMap[] = {std::make_pair("Run", cond::runnumber),
                                                                     std::make_pair("Time", cond::timestamp),
                                                                     std::make_pair("Lumi", cond::lumiid),
                                                                     std::make_pair("Hash", cond::hash),
                                                                     std::make_pair("User", cond::userid)};
    std::string timeTypeName(TimeType type) {
      if (type == invalid)
        return "";
      return s_timeTypeMap[type].first;
    }

    TimeType timeTypeFromName(const std::string& name) {
      for (auto const& i : s_timeTypeMap)
        if (name == i.first)
          return i.second;
      const cond::TimeTypeSpecs& theSpec = cond::findSpecs(name);
      return theSpec.type;
      //throwException( "TimeType \""+name+"\" is unknown.","timeTypeFromName");
    }

    Time_t tillTimeFromNextSince(Time_t nextSince, TimeType timeType) {
      if (nextSince == time::MAX_VAL)
        return time::MAX_VAL;
      if (timeType != (TimeType)TIMESTAMP) {
        return nextSince - 1;
      } else {
        auto unpackedTime = unpack(nextSince);
        //number of seconds in nanoseconds (avoid multiply and divide by 1e09)
        Time_t totalSecondsInNanoseconds = ((Time_t)unpackedTime.first) * 1000000000;
        //total number of nanoseconds
        Time_t totalNanoseconds = totalSecondsInNanoseconds + ((Time_t)(unpackedTime.second));
        //now decrementing of 1 nanosecond
        totalNanoseconds--;
        //now repacking (just change the value of the previous pair)
        unpackedTime.first = (unsigned int)(totalNanoseconds / 1000000000);
        unpackedTime.second = (unsigned int)(totalNanoseconds - (Time_t)unpackedTime.first * 1000000000);
        return pack(unpackedTime);
      }
    }

    Time_t tillTimeForIOV(Time_t since, unsigned int iovSize, TimeType timeType) {
      if (since == time::MAX_VAL)
        return time::MAX_VAL;
      if (timeType != (TimeType)TIMESTAMP) {
        return since + iovSize - 1;
      } else {
        auto unpackedTime = unpack(since);
        unpackedTime.first = unpackedTime.first + iovSize;
        return tillTimeFromNextSince(pack(unpackedTime), timeType);
      }
    }

    Time_t lumiTime(unsigned int run, unsigned int lumiNr) { return cond::time::pack(std::make_pair(run, lumiNr)); }

    Time_t lumiIdToRun(Time_t lumiId) { return cond::time::unpack(lumiId).first; }

    Time_t lumiIdToLumiNr(Time_t lumiId) { return cond::time::unpack(lumiId).second; }

    Time_t sinceGroupSize(TimeType tp) {
      if (tp == TIMESTAMP)
        return SINCE_TIME_GROUP_SIZE;
      if (tp == LUMIID)
        return SINCE_LUMI_GROUP_SIZE;
      if (tp == HASH)
        return SINCE_HASH_GROUP_SIZE;
      return SINCE_RUN_GROUP_SIZE;
    }

    // framework conversions
    edm::IOVSyncValue toIOVSyncValue(Time_t time, TimeType timetype, bool startOrStop) {
      switch (timetype) {
        case RUNNUMBER:
          return edm::IOVSyncValue(edm::EventID(time,
                                                startOrStop ? 0 : edm::EventID::maxEventNumber(),
                                                startOrStop ? 0 : edm::EventID::maxEventNumber()));
        case LUMIID: {
          edm::LuminosityBlockID l(time);
          return edm::IOVSyncValue(
              edm::EventID(l.run(), l.luminosityBlock(), startOrStop ? 0 : edm::EventID::maxEventNumber()));
        }
        case TIMESTAMP:
          return edm::IOVSyncValue(edm::Timestamp(time));
        default:
          return edm::IOVSyncValue::invalidIOVSyncValue();
      }
    }

    Time_t fromIOVSyncValue(edm::IOVSyncValue const& time, TimeType timetype) {
      switch (timetype) {
        case RUNNUMBER:
          return time.eventID().run();
        case LUMIID: {
          edm::LuminosityBlockID lum(time.eventID().run(), time.luminosityBlockNumber());
          return lum.value();
        }
        case TIMESTAMP:
          return time.time().value();
        default:
          return 0;
      }
    }

    // the minimal maximum-time an IOV can extend to
    edm::IOVSyncValue limitedIOVSyncValue(Time_t time, TimeType timetype) {
      switch (timetype) {
        case RUNNUMBER:
          // last lumi and event of this run
          return edm::IOVSyncValue(
              edm::EventID(time, edm::LuminosityBlockID::maxLuminosityBlockNumber(), edm::EventID::maxEventNumber()));
        case LUMIID: {
          // the same lumiblock
          edm::LuminosityBlockID l(time);
          return edm::IOVSyncValue(edm::EventID(l.run(), l.luminosityBlock(), edm::EventID::maxEventNumber()));
        }
        case TIMESTAMP:
          // next event ?
          return edm::IOVSyncValue::invalidIOVSyncValue();
        default:
          return edm::IOVSyncValue::invalidIOVSyncValue();
      }
    }

    edm::IOVSyncValue limitedIOVSyncValue(edm::IOVSyncValue const& time, TimeType timetype) {
      switch (timetype) {
        case RUNNUMBER:
          // last event of this run
          return edm::IOVSyncValue(edm::EventID(time.eventID().run(),
                                                edm::LuminosityBlockID::maxLuminosityBlockNumber(),
                                                edm::EventID::maxEventNumber()));
        case LUMIID:
          // the same lumiblock
          return edm::IOVSyncValue(
              edm::EventID(time.eventID().run(), time.luminosityBlockNumber(), edm::EventID::maxEventNumber()));
        case TIMESTAMP:
          // same lumiblock
          return edm::IOVSyncValue(
              edm::EventID(time.eventID().run(), time.luminosityBlockNumber(), edm::EventID::maxEventNumber()));
        default:
          return edm::IOVSyncValue::invalidIOVSyncValue();
      }
    }

    std::string transactionIdForLumiTime(Time_t time, unsigned int iovSize, const std::string& secretKey) {
      auto unpackedTime = cond::time::unpack(time);
      unsigned int offset = 1 + iovSize;
      cond::Time_t id = 0;
      if (unpackedTime.second < offset) {
        id = lumiTime(unpackedTime.first, 1);
      } else {
        unsigned int res = (unpackedTime.second - offset) % iovSize;
        id = lumiTime(unpackedTime.first, unpackedTime.second - res);
      }
      std::stringstream transId;
      transId << id;
      if (!secretKey.empty()) {
        transId << "_" << secretKey;
      }
      return transId.str();
    }

  }  // namespace time

}  // namespace cond