CondHDF5ESSource

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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
// -*- C++ -*-
//
// Package:     CondCore/HDF5ESSource
// Class  :     CondHDF5ESSource
//
// Implementation:
//     [Notes on implementation]
//
// Original Author:  Christopher Jones
//         Created:  Fri, 16 Jun 2023 15:17:53 GMT
//

// system include files
#include <cassert>
#include <iostream>

// user include files
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/ESProductResolverProvider.h"
#include "FWCore/Framework/interface/IOVSyncValue.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/Framework/interface/ValidityInterval.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/Concurrency/interface/SerialTaskQueue.h"

#include "CondFormats/SerializationHelper/interface/SerializationHelperFactory.h"

#include "IOVSyncValue.h"
#include "DataProduct.h"
#include "Record.h"
#include "HDF5ProductResolver.h"
#include "convertSyncValue.h"
#include "h5_File.h"
#include "h5_Group.h"
#include "h5_DataSet.h"
#include "h5_Attribute.h"
#include "Compression.h"

using namespace cond::hdf5;

class CondHDF5ESSource : public edm::EventSetupRecordIntervalFinder, public edm::eventsetup::ESProductResolverProvider {
public:
  using EventSetupRecordKey = edm::eventsetup::EventSetupRecordKey;
  explicit CondHDF5ESSource(edm::ParameterSet const&);

  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
  bool isConcurrentFinder() const final { return true; }
  void setIntervalFor(EventSetupRecordKey const&, edm::IOVSyncValue const&, edm::ValidityInterval&) final;
  KeyedResolversVector registerResolvers(EventSetupRecordKey const&, unsigned int iovIndex) final;

  edm::SerialTaskQueue queue_;
  std::mutex mutex_;
  std::vector<Record> records_;
  std::string filename_;
  cms::h5::File file_;
  Compression compression_ = Compression::kNone;
};

//
// constants, enums and typedefs
//

//
// static data member definitions
//
namespace {
  cond::hdf5::Compression nameToEnum(std::string const& iName) {
    if (iName == "zlib") {
      return Compression::kZLIB;
    } else if (iName == "lzma") {
      return Compression::kLZMA;
    } else if (iName == "none") {
      return Compression::kNone;
    } else {
      throw cms::Exception("BadCompressionType") << "unknown compression type used in file '" << iName << "'";
    }
    return Compression::kNone;
  }
}  // namespace

//
// constructors and destructor
//
CondHDF5ESSource::CondHDF5ESSource(edm::ParameterSet const& iPSet)
    : filename_(iPSet.getUntrackedParameter<std::string>("filename")),
      file_(filename_, cms::h5::File::kReadOnly),
      compression_(nameToEnum(file_.findAttribute("default_payload_compressor")->readString())) {
  const auto globalTagsGroup = file_.findGroup("GlobalTags");
  const auto chosenTag = globalTagsGroup->findGroup(iPSet.getParameter<std::string>("globalTag"));
  const auto tagsDataSet = chosenTag->findDataSet("Tags");
  const auto recordsGroup = file_.findGroup("Records");

  std::vector<hobj_ref_t> tags = tagsDataSet->readRefs();

  std::set<std::string> recordsToExclude;
  {
    auto exclude = iPSet.getParameter<std::vector<std::string>>("excludeRecords");
    recordsToExclude = std::set(exclude.begin(), exclude.end());
  }

  for (auto t : tags) {
    auto tagGroup = file_.derefGroup(t);
    Record record;
    record.name_ = tagGroup->findAttribute("record")->readString();
    //std::cout << record.name_ << std::endl;

    if (recordsToExclude.end() != recordsToExclude.find(record.name_)) {
      //std::cout << "excluding " << record.name_ << std::endl;
      continue;
    }

    auto recordGroup = recordsGroup->findGroup(record.name_);
    //std::cout << "found record group" << std::endl;
    auto dataProductsGroup = recordGroup->findGroup("DataProducts");
    //std::cout << "found DataProducts group" << std::endl;

    for (size_t i = 0; i < dataProductsGroup->getNumObjs(); ++i) {
      std::string productGroupName = dataProductsGroup->getObjnameByIdx(i);
      //std::cout << "looking for " << productGroupName << std::endl;
      auto dataProductGroup = dataProductsGroup->findGroup(productGroupName);

      auto const typeAttr = dataProductGroup->findAttribute("type");
      std::string typeName = typeAttr->readString();
      //loading the factory should also trigger registering the Record and DataProduct keys
      cond::serialization::SerializationHelperFactory::get()->create(typeName);
      std::string name = productGroupName.substr(typeName.size() + 1, productGroupName.size());
      if (name.size() == 1 and name[0] == '-') {
        name = std::string();
      }
      record.dataProducts_.emplace_back(std::move(name), std::move(typeName));
    }

    {
      auto const typeAttr = tagGroup->findAttribute("time_type");
      std::string typeName = typeAttr->readString();
      record.iovIsRunLumi_ = (typeName == "run_lumi");
    }

    std::vector<hobj_ref_t> payloadRefForIOVs;
    {
      auto const firstDataSet = tagGroup->findDataSet("first");
      auto const lastDataSet = tagGroup->findDataSet("last");

      record.iovFirsts_ = firstDataSet->readSyncValues();
      record.iovLasts_ = lastDataSet->readSyncValues();

      {
        auto const payloadDataSet = tagGroup->findDataSet("payload");
        payloadRefForIOVs = payloadDataSet->readRefs();
        assert(payloadRefForIOVs.size() == record.iovFirsts_.size() * record.dataProducts_.size());
      }
    }
    size_t dataProductIndex = 0;
    for (auto r : payloadRefForIOVs) {
      record.dataProducts_[dataProductIndex].payloadForIOVs_.push_back(r);
      ++dataProductIndex;
      if (dataProductIndex >= record.dataProducts_.size()) {
        dataProductIndex = 0;
      }
    }

    //now that we've loaded a plugin that is associated to the record, the record should be registered
    auto key =
        edm::eventsetup::EventSetupRecordKey(edm::eventsetup::heterocontainer::HCTypeTag::findType(record.name_));
    assert(key != edm::eventsetup::heterocontainer::HCTypeTag());
    //tell system we retrieve this Record
    findingRecordWithKey(key);
    usingRecordWithKey(key);

    records_.emplace_back(std::move(record));
  }
  std::sort(records_.begin(), records_.end(), [](auto const& l, auto const& r) { return l.name_ < r.name_; });
}

void CondHDF5ESSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
  edm::ParameterSetDescription desc;
  desc.addUntracked<std::string>("filename")->setComment("HDF5 file containing the conditions");
  desc.add<std::string>("globalTag")->setComment("Which global tag to use from the file");
  desc.add<std::vector<std::string>>("excludeRecords", std::vector<std::string>())
      ->setComment("List of Records that should not be read from the file");

  descriptions.addDefault(desc);
}

void CondHDF5ESSource::setIntervalFor(EventSetupRecordKey const& iRecordKey,
                                      edm::IOVSyncValue const& iSync,
                                      edm::ValidityInterval& iIOV) {
  using namespace cond::hdf5;

  auto const itRecord =
      std::lower_bound(records_.begin(), records_.end(), iRecordKey.name(), [](auto const& iE, auto const& iV) {
        return iE.name_ < iV;
      });
  assert(itRecord != records_.end());
  auto const& record = *itRecord;
  assert(record.name_ == iRecordKey.name());
  auto sync = convertSyncValue(iSync, record.iovIsRunLumi_);
  auto itFound = findMatchingFirst(record.iovFirsts_, sync);
  if (itFound == record.iovFirsts_.end()) {
    //std::cout << "BAD SYNC for record " << iRecordKey.name() << std::endl;
    iIOV = edm::ValidityInterval::invalidInterval();
    return;
  }
  iIOV = edm::ValidityInterval{
      convertSyncValue(*itFound, record.iovIsRunLumi_),
      convertSyncValue(record.iovLasts_[itFound - record.iovFirsts_.begin()], record.iovIsRunLumi_)};
}

CondHDF5ESSource::KeyedResolversVector CondHDF5ESSource::registerResolvers(EventSetupRecordKey const& iRecordKey,
                                                                           unsigned int iovIndex) {
  CondHDF5ESSource::KeyedResolversVector returnValue;

  //std::cout << "Register proxies called " << iRecordKey.name() << std::endl;
  auto const itRecord =
      std::lower_bound(records_.begin(), records_.end(), iRecordKey.name(), [](auto const& iE, auto const& iV) {
        return iE.name_ < iV;
      });
  assert(itRecord != records_.end());
  auto const& record = *itRecord;
  assert(record.name_ == iRecordKey.name());
  for (auto const& dataProduct : record.dataProducts_) {
    //std::cout << "Making DataProduct " << dataProduct.type_ << " '" << dataProduct.name_ << "' for Record "
    //          << record.name_ << std::endl;
    auto helper = cond::serialization::SerializationHelperFactory::get()->create(dataProduct.type_);
    returnValue.emplace_back(
        edm::eventsetup::DataKey(edm::eventsetup::heterocontainer::HCTypeTag::findType(dataProduct.type_),
                                 dataProduct.name_.c_str()),
        std::make_shared<HDF5ProductResolver>(
            &queue_, std::move(helper), &file_, filename_, compression_, &record, &dataProduct));
  }
  return returnValue;
}

DEFINE_FWK_EVENTSETUP_SOURCE(CondHDF5ESSource);