Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:01:29

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 lumiId) { return cond::time::pack(std::make_pair(run, lumiId)); }
0067 
0068     Time_t sinceGroupSize(TimeType tp) {
0069       if (tp == TIMESTAMP)
0070         return SINCE_TIME_GROUP_SIZE;
0071       if (tp == LUMIID)
0072         return SINCE_LUMI_GROUP_SIZE;
0073       if (tp == HASH)
0074         return SINCE_HASH_GROUP_SIZE;
0075       return SINCE_RUN_GROUP_SIZE;
0076     }
0077 
0078     // framework conversions
0079     edm::IOVSyncValue toIOVSyncValue(Time_t time, TimeType timetype, bool startOrStop) {
0080       switch (timetype) {
0081         case RUNNUMBER:
0082           return edm::IOVSyncValue(edm::EventID(time,
0083                                                 startOrStop ? 0 : edm::EventID::maxEventNumber(),
0084                                                 startOrStop ? 0 : edm::EventID::maxEventNumber()));
0085         case LUMIID: {
0086           edm::LuminosityBlockID l(time);
0087           return edm::IOVSyncValue(
0088               edm::EventID(l.run(), l.luminosityBlock(), startOrStop ? 0 : edm::EventID::maxEventNumber()));
0089         }
0090         case TIMESTAMP:
0091           return edm::IOVSyncValue(edm::Timestamp(time));
0092         default:
0093           return edm::IOVSyncValue::invalidIOVSyncValue();
0094       }
0095     }
0096 
0097     Time_t fromIOVSyncValue(edm::IOVSyncValue const& time, TimeType timetype) {
0098       switch (timetype) {
0099         case RUNNUMBER:
0100           return time.eventID().run();
0101         case LUMIID: {
0102           edm::LuminosityBlockID lum(time.eventID().run(), time.luminosityBlockNumber());
0103           return lum.value();
0104         }
0105         case TIMESTAMP:
0106           return time.time().value();
0107         default:
0108           return 0;
0109       }
0110     }
0111 
0112     // the minimal maximum-time an IOV can extend to
0113     edm::IOVSyncValue limitedIOVSyncValue(Time_t time, TimeType timetype) {
0114       switch (timetype) {
0115         case RUNNUMBER:
0116           // last lumi and event of this run
0117           return edm::IOVSyncValue(edm::EventID(time, edm::EventID::maxEventNumber(), edm::EventID::maxEventNumber()));
0118         case LUMIID: {
0119           // the same lumiblock
0120           edm::LuminosityBlockID l(time);
0121           return edm::IOVSyncValue(edm::EventID(l.run(), l.luminosityBlock(), edm::EventID::maxEventNumber()));
0122         }
0123         case TIMESTAMP:
0124           // next event ?
0125           return edm::IOVSyncValue::invalidIOVSyncValue();
0126         default:
0127           return edm::IOVSyncValue::invalidIOVSyncValue();
0128       }
0129     }
0130 
0131     edm::IOVSyncValue limitedIOVSyncValue(edm::IOVSyncValue const& time, TimeType timetype) {
0132       switch (timetype) {
0133         case RUNNUMBER:
0134           // last event of this run
0135           return edm::IOVSyncValue(
0136               edm::EventID(time.eventID().run(), edm::EventID::maxEventNumber(), edm::EventID::maxEventNumber()));
0137         case LUMIID:
0138           // the same lumiblock
0139           return edm::IOVSyncValue(
0140               edm::EventID(time.eventID().run(), time.luminosityBlockNumber(), edm::EventID::maxEventNumber()));
0141         case TIMESTAMP:
0142           // same lumiblock
0143           return edm::IOVSyncValue(
0144               edm::EventID(time.eventID().run(), time.luminosityBlockNumber(), edm::EventID::maxEventNumber()));
0145         default:
0146           return edm::IOVSyncValue::invalidIOVSyncValue();
0147       }
0148     }
0149 
0150     std::string transactionIdForLumiTime(Time_t time, unsigned int iovSize, const std::string& secretKey) {
0151       auto unpackedTime = cond::time::unpack(time);
0152       unsigned int offset = 1 + iovSize;
0153       cond::Time_t id = 0;
0154       if (unpackedTime.second < offset) {
0155         id = lumiTime(unpackedTime.first, 1);
0156       } else {
0157         unsigned int res = (unpackedTime.second - offset) % iovSize;
0158         id = lumiTime(unpackedTime.first, unpackedTime.second - res);
0159       }
0160       std::stringstream transId;
0161       transId << id;
0162       if (!secretKey.empty()) {
0163         transId << "_" << secretKey;
0164       }
0165       return transId.str();
0166     }
0167 
0168   }  // namespace time
0169 
0170 }  // namespace cond