CMS 3D CMS Logo

CondHDF5ESSource.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondCore/HDF5ESSource
4 // Class : CondHDF5ESSource
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Christopher Jones
10 // Created: Fri, 16 Jun 2023 15:17:53 GMT
11 //
12 
13 // system include files
14 #include <cassert>
15 #include <iostream>
16 
17 // user include files
28 
30 
31 #include "IOVSyncValue.h"
32 #include "DataProduct.h"
33 #include "Record.h"
34 #include "HDF5ProductResolver.h"
35 #include "convertSyncValue.h"
36 #include "h5_File.h"
37 #include "h5_Group.h"
38 #include "h5_DataSet.h"
39 #include "h5_Attribute.h"
40 #include "Compression.h"
41 
42 using namespace cond::hdf5;
43 
45 public:
47  explicit CondHDF5ESSource(edm::ParameterSet const&);
48 
49  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
50 
51 private:
52  bool isConcurrentFinder() const final { return true; }
53  void setIntervalFor(EventSetupRecordKey const&, edm::IOVSyncValue const&, edm::ValidityInterval&) final;
54  KeyedResolversVector registerResolvers(EventSetupRecordKey const&, unsigned int iovIndex) final;
55 
58  std::vector<Record> records_;
62 };
63 
64 //
65 // constants, enums and typedefs
66 //
67 
68 //
69 // static data member definitions
70 //
71 namespace {
72  cond::hdf5::Compression nameToEnum(std::string const& iName) {
73  if (iName == "zlib") {
74  return Compression::kZLIB;
75  } else if (iName == "lzma") {
76  return Compression::kLZMA;
77  } else if (iName == "none") {
78  return Compression::kNone;
79  } else {
80  throw cms::Exception("BadCompressionType") << "unknown compression type used in file '" << iName << "'";
81  }
82  return Compression::kNone;
83  }
84 } // namespace
85 
86 //
87 // constructors and destructor
88 //
90  : filename_(iPSet.getUntrackedParameter<std::string>("filename")),
91  file_(filename_, cms::h5::File::kReadOnly),
92  compression_(nameToEnum(file_.findAttribute("default_payload_compressor")->readString())) {
93  const auto globalTagsGroup = file_.findGroup("GlobalTags");
94  const auto chosenTag = globalTagsGroup->findGroup(iPSet.getParameter<std::string>("globalTag"));
95  const auto tagsDataSet = chosenTag->findDataSet("Tags");
96  const auto recordsGroup = file_.findGroup("Records");
97 
98  std::vector<hobj_ref_t> tags = tagsDataSet->readRefs();
99 
100  std::set<std::string> recordsToExclude;
101  {
102  auto exclude = iPSet.getParameter<std::vector<std::string>>("excludeRecords");
103  recordsToExclude = std::set(exclude.begin(), exclude.end());
104  }
105 
106  for (auto t : tags) {
107  auto tagGroup = file_.derefGroup(t);
108  Record record;
109  record.name_ = tagGroup->findAttribute("record")->readString();
110  //std::cout << record.name_ << std::endl;
111 
112  if (recordsToExclude.end() != recordsToExclude.find(record.name_)) {
113  //std::cout << "excluding " << record.name_ << std::endl;
114  continue;
115  }
116 
117  auto recordGroup = recordsGroup->findGroup(record.name_);
118  //std::cout << "found record group" << std::endl;
119  auto dataProductsGroup = recordGroup->findGroup("DataProducts");
120  //std::cout << "found DataProducts group" << std::endl;
121 
122  for (size_t i = 0; i < dataProductsGroup->getNumObjs(); ++i) {
123  std::string productGroupName = dataProductsGroup->getObjnameByIdx(i);
124  //std::cout << "looking for " << productGroupName << std::endl;
125  auto dataProductGroup = dataProductsGroup->findGroup(productGroupName);
126 
127  auto const typeAttr = dataProductGroup->findAttribute("type");
128  std::string typeName = typeAttr->readString();
129  //loading the factory should also trigger registering the Record and DataProduct keys
131  std::string name = productGroupName.substr(typeName.size() + 1, productGroupName.size());
132  if (name.size() == 1 and name[0] == '-') {
133  name = std::string();
134  }
135  record.dataProducts_.emplace_back(std::move(name), std::move(typeName));
136  }
137 
138  {
139  auto const typeAttr = tagGroup->findAttribute("time_type");
140  std::string typeName = typeAttr->readString();
141  record.iovIsRunLumi_ = (typeName == "run_lumi");
142  }
143 
144  std::vector<hobj_ref_t> payloadRefForIOVs;
145  {
146  auto const firstDataSet = tagGroup->findDataSet("first");
147  auto const lastDataSet = tagGroup->findDataSet("last");
148 
149  record.iovFirsts_ = firstDataSet->readSyncValues();
150  record.iovLasts_ = lastDataSet->readSyncValues();
151 
152  {
153  auto const payloadDataSet = tagGroup->findDataSet("payload");
154  payloadRefForIOVs = payloadDataSet->readRefs();
155  assert(payloadRefForIOVs.size() == record.iovFirsts_.size() * record.dataProducts_.size());
156  }
157  }
158  size_t dataProductIndex = 0;
159  for (auto r : payloadRefForIOVs) {
160  record.dataProducts_[dataProductIndex].payloadForIOVs_.push_back(r);
161  ++dataProductIndex;
162  if (dataProductIndex >= record.dataProducts_.size()) {
163  dataProductIndex = 0;
164  }
165  }
166 
167  //now that we've loaded a plugin that is associated to the record, the record should be registered
168  auto key =
171  //tell system we retrieve this Record
174 
175  records_.emplace_back(std::move(record));
176  }
177  std::sort(records_.begin(), records_.end(), [](auto const& l, auto const& r) { return l.name_ < r.name_; });
178 }
179 
182  desc.addUntracked<std::string>("filename")->setComment("HDF5 file containing the conditions");
183  desc.add<std::string>("globalTag")->setComment("Which global tag to use from the file");
184  desc.add<std::vector<std::string>>("excludeRecords", std::vector<std::string>())
185  ->setComment("List of Records that should not be read from the file");
186 
187  descriptions.addDefault(desc);
188 }
189 
191  edm::IOVSyncValue const& iSync,
192  edm::ValidityInterval& iIOV) {
193  using namespace cond::hdf5;
194 
195  auto const itRecord =
196  std::lower_bound(records_.begin(), records_.end(), iRecordKey.name(), [](auto const& iE, auto const& iV) {
197  return iE.name_ < iV;
198  });
199  assert(itRecord != records_.end());
200  auto const& record = *itRecord;
201  assert(record.name_ == iRecordKey.name());
202  auto sync = convertSyncValue(iSync, record.iovIsRunLumi_);
203  auto itFound = findMatchingFirst(record.iovFirsts_, sync);
204  if (itFound == record.iovFirsts_.end()) {
205  //std::cout << "BAD SYNC for record " << iRecordKey.name() << std::endl;
207  return;
208  }
209  iIOV = edm::ValidityInterval{
210  convertSyncValue(*itFound, record.iovIsRunLumi_),
211  convertSyncValue(record.iovLasts_[itFound - record.iovFirsts_.begin()], record.iovIsRunLumi_)};
212 }
213 
215  unsigned int iovIndex) {
217 
218  //std::cout << "Register proxies called " << iRecordKey.name() << std::endl;
219  auto const itRecord =
220  std::lower_bound(records_.begin(), records_.end(), iRecordKey.name(), [](auto const& iE, auto const& iV) {
221  return iE.name_ < iV;
222  });
223  assert(itRecord != records_.end());
224  auto const& record = *itRecord;
225  assert(record.name_ == iRecordKey.name());
226  for (auto const& dataProduct : record.dataProducts_) {
227  //std::cout << "Making DataProduct " << dataProduct.type_ << " '" << dataProduct.name_ << "' for Record "
228  // << record.name_ << std::endl;
229  auto helper = cond::serialization::SerializationHelperFactory::get()->create(dataProduct.type_);
230  returnValue.emplace_back(
232  dataProduct.name_.c_str()),
233  std::make_shared<HDF5ProductResolver>(
234  &queue_, std::move(helper), &file_, filename_, compression_, &record, &dataProduct));
235  }
236  return returnValue;
237 }
238 
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
std::string filename_
Definition: helper.py:1
std::shared_ptr< Group > derefGroup(hobj_ref_t iRef) const
Definition: h5_File.cc:75
std::vector< Record > records_
static std::mutex mutex
Definition: Proxy.cc:8
void usingRecordWithKey(const EventSetupRecordKey &key)
CondHDF5ESSource(edm::ParameterSet const &)
assert(be >=bs)
edm::SerialTaskQueue queue_
std::shared_ptr< Group > findGroup(std::string const &iName) const
Definition: h5_File.cc:66
void addDefault(ParameterSetDescription const &psetDescription)
bool isConcurrentFinder() const final
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
key
prepare the HTCondor submission files and eventually submit them
std::vector< IOVSyncValue >::const_iterator findMatchingFirst(std::vector< IOVSyncValue > const &iIOVs, IOVSyncValue iMatch)
Definition: IOVSyncValue.cc:19
Namespace of DDCMS conversion namespace.
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
cms::h5::File file_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< std::pair< DataKey, std::shared_ptr< ESProductResolver > >> KeyedResolversVector
void setIntervalFor(EventSetupRecordKey const &, edm::IOVSyncValue const &, edm::ValidityInterval &) final
static const ValidityInterval & invalidInterval()
IOVSyncValue convertSyncValue(edm::IOVSyncValue const &iFrom, bool iIsRunLumi)
#define get
KeyedResolversVector registerResolvers(EventSetupRecordKey const &, unsigned int iovIndex) final
Compression compression_
def move(src, dest)
Definition: eostools.py:511
void findingRecordWithKey(const eventsetup::EventSetupRecordKey &)
static HCTypeTag findType(char const *iTypeName)
find a type based on the types name, if not found will return default HCTypeTag
Definition: HCTypeTag.cc:121