Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-04-02 23:19:22

0001 #include "CondCore/CondDB/interface/Time.h"
0002 #include "CondCore/CondDB/interface/Exception.h"
0003 #include "CondCore/CondDB/interface/Types.h"
0004 //
0005 #include "DataFormats/Provenance/interface/LuminosityBlockID.h"
0006 //
0007 #include <initializer_list>
0008 #include <vector>
0009 #include <map>
0010 
0011 namespace cond {
0012 
0013   namespace time {
0014     static const std::pair<const char*, TimeType> s_timeTypeMap[] = {std::make_pair("Run", cond::runnumber),
0015                                                                      std::make_pair("Time", cond::timestamp),
0016                                                                      std::make_pair("Lumi", cond::lumiid),
0017                                                                      std::make_pair("Hash", cond::hash),
0018                                                                      std::make_pair("User", cond::userid)};
0019     std::string timeTypeName(TimeType type) {
0020       if (type == invalid)
0021         return "";
0022       return s_timeTypeMap[type].first;
0023     }
0024 
0025     TimeType timeTypeFromName(const std::string& name) {
0026       for (auto const& i : s_timeTypeMap)
0027         if (name == i.first)
0028           return i.second;
0029       const cond::TimeTypeSpecs& theSpec = cond::findSpecs(name);
0030       return theSpec.type;
0031       //throwException( "TimeType \""+name+"\" is unknown.","timeTypeFromName");
0032     }
0033 
0034     Time_t tillTimeFromNextSince(Time_t nextSince, TimeType timeType) {
0035       if (nextSince == time::MAX_VAL)
0036         return time::MAX_VAL;
0037       if (timeType != (TimeType)TIMESTAMP) {
0038         return nextSince - 1;
0039       } else {
0040         auto unpackedTime = unpack(nextSince);
0041         //number of seconds in nanoseconds (avoid multiply and divide by 1e09)
0042         Time_t totalSecondsInNanoseconds = ((Time_t)unpackedTime.first) * 1000000000;
0043         //total number of nanoseconds
0044         Time_t totalNanoseconds = totalSecondsInNanoseconds + ((Time_t)(unpackedTime.second));
0045         //now decrementing of 1 nanosecond
0046         totalNanoseconds--;
0047         //now repacking (just change the value of the previous pair)
0048         unpackedTime.first = (unsigned int)(totalNanoseconds / 1000000000);
0049         unpackedTime.second = (unsigned int)(totalNanoseconds - (Time_t)unpackedTime.first * 1000000000);
0050         return pack(unpackedTime);
0051       }
0052     }
0053 
0054     Time_t tillTimeForIOV(Time_t since, unsigned int iovSize, TimeType timeType) {
0055       if (since == time::MAX_VAL)
0056         return time::MAX_VAL;
0057       if (timeType != (TimeType)TIMESTAMP) {
0058         return since + iovSize - 1;
0059       } else {
0060         auto unpackedTime = unpack(since);
0061         unpackedTime.first = unpackedTime.first + iovSize;
0062         return tillTimeFromNextSince(pack(unpackedTime), timeType);
0063       }
0064     }
0065 
0066     Time_t lumiTime(unsigned int run, unsigned int lumiNr) { return cond::time::pack(std::make_pair(run, lumiNr)); }
0067 
0068     Time_t lumiIdToRun(Time_t lumiId) { return cond::time::unpack(lumiId).first; }
0069 
0070     Time_t lumiIdToLumiNr(Time_t lumiId) { return cond::time::unpack(lumiId).second; }
0071 
0072     Time_t sinceGroupSize(TimeType tp) {
0073       if (tp == TIMESTAMP)
0074         return SINCE_TIME_GROUP_SIZE;
0075       if (tp == LUMIID)
0076         return SINCE_LUMI_GROUP_SIZE;
0077       if (tp == HASH)
0078         return SINCE_HASH_GROUP_SIZE;
0079       return SINCE_RUN_GROUP_SIZE;
0080     }
0081 
0082     // framework conversions
0083     edm::IOVSyncValue toIOVSyncValue(Time_t time, TimeType timetype, bool startOrStop) {
0084       switch (timetype) {
0085         case RUNNUMBER:
0086           return edm::IOVSyncValue(edm::EventID(time,
0087                                                 startOrStop ? 0 : edm::EventID::maxEventNumber(),
0088                                                 startOrStop ? 0 : edm::EventID::maxEventNumber()));
0089         case LUMIID: {
0090           edm::LuminosityBlockID l(time);
0091           return edm::IOVSyncValue(
0092               edm::EventID(l.run(), l.luminosityBlock(), startOrStop ? 0 : edm::EventID::maxEventNumber()));
0093         }
0094         case TIMESTAMP:
0095           return edm::IOVSyncValue(edm::Timestamp(time));
0096         default:
0097           return edm::IOVSyncValue::invalidIOVSyncValue();
0098       }
0099     }
0100 
0101     Time_t fromIOVSyncValue(edm::IOVSyncValue const& time, TimeType timetype) {
0102       switch (timetype) {
0103         case RUNNUMBER:
0104           return time.eventID().run();
0105         case LUMIID: {
0106           edm::LuminosityBlockID lum(time.eventID().run(), time.luminosityBlockNumber());
0107           return lum.value();
0108         }
0109         case TIMESTAMP:
0110           return time.time().value();
0111         default:
0112           return 0;
0113       }
0114     }
0115 
0116     // the minimal maximum-time an IOV can extend to
0117     edm::IOVSyncValue limitedIOVSyncValue(Time_t time, TimeType timetype) {
0118       switch (timetype) {
0119         case RUNNUMBER:
0120           // last lumi and event of this run
0121           return edm::IOVSyncValue(
0122               edm::EventID(time, edm::LuminosityBlockID::maxLuminosityBlockNumber(), edm::EventID::maxEventNumber()));
0123         case LUMIID: {
0124           // the same lumiblock
0125           edm::LuminosityBlockID l(time);
0126           return edm::IOVSyncValue(edm::EventID(l.run(), l.luminosityBlock(), edm::EventID::maxEventNumber()));
0127         }
0128         case TIMESTAMP:
0129           // next event ?
0130           return edm::IOVSyncValue::invalidIOVSyncValue();
0131         default:
0132           return edm::IOVSyncValue::invalidIOVSyncValue();
0133       }
0134     }
0135 
0136     edm::IOVSyncValue limitedIOVSyncValue(edm::IOVSyncValue const& time, TimeType timetype) {
0137       switch (timetype) {
0138         case RUNNUMBER:
0139           // last event of this run
0140           return edm::IOVSyncValue(edm::EventID(time.eventID().run(),
0141                                                 edm::LuminosityBlockID::maxLuminosityBlockNumber(),
0142                                                 edm::EventID::maxEventNumber()));
0143         case LUMIID:
0144           // the same lumiblock
0145           return edm::IOVSyncValue(
0146               edm::EventID(time.eventID().run(), time.luminosityBlockNumber(), edm::EventID::maxEventNumber()));
0147         case TIMESTAMP:
0148           // same lumiblock
0149           return edm::IOVSyncValue(
0150               edm::EventID(time.eventID().run(), time.luminosityBlockNumber(), edm::EventID::maxEventNumber()));
0151         default:
0152           return edm::IOVSyncValue::invalidIOVSyncValue();
0153       }
0154     }
0155 
0156     std::string transactionIdForLumiTime(Time_t time, unsigned int iovSize, const std::string& secretKey) {
0157       auto unpackedTime = cond::time::unpack(time);
0158       unsigned int offset = 1 + iovSize;
0159       cond::Time_t id = 0;
0160       if (unpackedTime.second < offset) {
0161         id = lumiTime(unpackedTime.first, 1);
0162       } else {
0163         unsigned int res = (unpackedTime.second - offset) % iovSize;
0164         id = lumiTime(unpackedTime.first, unpackedTime.second - res);
0165       }
0166       std::stringstream transId;
0167       transId << id;
0168       if (!secretKey.empty()) {
0169         transId << "_" << secretKey;
0170       }
0171       return transId.str();
0172     }
0173 
0174   }  // namespace time
0175 
0176 }  // namespace cond