1#include "Conditions/SimpleCSVTableProvider.h"
3#include "Conditions/GeneralCSVLoader.h"
4#include "Conditions/SimpleTableStreamers.h"
5#include "Conditions/URLStreamer.h"
11SimpleCSVTableProvider::~SimpleCSVTableProvider() {}
13SimpleCSVTableProvider::SimpleCSVTableProvider(
14 const std::string& name,
const std::string& tagname,
18 columns_ = parameters.
getParameter<std::vector<std::string>>(
"columns");
19 std::string dtype = parameters.
getParameter<std::string>(
"dataType");
20 if (dtype ==
"int" || dtype ==
"integer")
21 objectType_ = SimpleCSVTableProvider::OBJ_int;
22 if (dtype ==
"double" || dtype ==
"float")
23 objectType_ = SimpleCSVTableProvider::OBJ_double;
26 parameters.
getParameter<std::string>(
"conditions_baseURL");
28 if (parameters.
exists(
"entries")) {
29 std::vector<framework::config::Parameters> plist =
30 parameters.
getParameter<std::vector<framework::config::Parameters>>(
32 if (!plist.empty()) entriesFromPython(plist);
35 entriesURL_ = parameters.
getParameter<std::string>(
"entriesURL");
36 if (!entriesURL_.empty()) entriesFromCSV();
39void SimpleCSVTableProvider::entriesFromPython(
40 std::vector<framework::config::Parameters>& plist) {
41 for (
auto aprov : plist) {
42 SimpleCSVTableProvider::Entry item;
43 int firstRun = aprov.getParameter<
int>(
"firstRun", -1);
44 int lastRun = aprov.getParameter<
int>(
"lastRun", -1);
45 std::string rtype = aprov.getParameter<std::string>(
"runType",
"any");
46 bool isMC = (rtype ==
"any" || rtype ==
"MC");
47 bool isData = (rtype ==
"any" || rtype ==
"data");
49 item.url_ = aprov.getParameter<std::string>(
"URL");
50 if (objectType_ == OBJ_int && aprov.exists(
"values")) {
51 item.ivalues_ = aprov.getParameter<std::vector<int>>(
"values");
52 if (item.ivalues_.size() != columns_.size()) {
53 EXCEPTION_RAISE(
"ConditionsException",
54 "Mismatch in values vector (" +
55 std::to_string(item.ivalues_.size()) +
56 ") and columns vector (" +
57 std::to_string(columns_.size()) +
") in " +
58 getConditionObjectName());
61 if (objectType_ == OBJ_double && aprov.exists(
"values")) {
62 item.dvalues_ = aprov.getParameter<std::vector<double>>(
"values");
63 if (item.dvalues_.size() != columns_.size()) {
64 EXCEPTION_RAISE(
"ConditionsException",
65 "Mismatch in values vector (" +
66 std::to_string(item.dvalues_.size()) +
67 ") and columns vector (" +
68 std::to_string(columns_.size()) +
") in " +
69 getConditionObjectName());
74 for (
auto tabledef : entries_) {
75 if (item.iov_.overlaps(tabledef.iov_)) {
76 std::stringstream err;
77 err <<
"Table '" << getConditionObjectName()
78 <<
"' has entries with overlapping providers " << tabledef.url_
79 <<
" and " << item.url_;
80 EXCEPTION_RAISE(
"ConditionsException", err.str());
84 entries_.push_back(item);
88void SimpleCSVTableProvider::entriesFromCSV() {
89 std::string csvurl = expandEnv(entriesURL_);
90 std::unique_ptr<std::istream> pstr = urlstream(csvurl);
92 StreamCSVLoader loader(*(pstr.get()));
94 if (!loader.nextRow())
return;
97 const char* column_names[] = {
"FIRST_RUN",
"LAST_RUN",
"RUNTYPE",
"URL", 0};
98 std::vector<std::string> cnames = loader.columnNames();
99 for (
int i = 0; column_names[i] != 0; i++) {
101 for (
auto name : cnames) {
102 if (!strcasecmp(name.c_str(), column_names[i])) found =
true;
105 EXCEPTION_RAISE(
"CSVNoSuchColumn",
"No such column '" +
106 std::string(column_names[i]) +
107 "' reading index CSV");
112 bool valid_for_data = strcasecmp(
"MC", loader.get(
"RUNTYPE").c_str());
113 bool valid_for_mc = strcasecmp(
"DATA", loader.get(
"RUNTYPE").c_str());
114 int firstRun = loader.getInteger(
"FIRST_RUN");
115 int lastRun = loader.getInteger(
"LAST_RUN");
119 e.url_ = loader.get(
"URL");
122 entries_.push_back(e);
123 }
while (loader.nextRow());
126std::string SimpleCSVTableProvider::expandEnv(
const std::string& s)
const {
128 std::string::size_type j = 0;
129 for (std::string::size_type i = s.find(
"${", j); i < s.size();
130 i = s.find(
"${", j)) {
131 if (i != j) retval.append(s, j, i - j);
133 std::string key = std::string(s, i + 2, j - i - 2);
134 if (key ==
"LDMX_CONDITION_TAG")
135 retval += getTagName();
136 else if (key ==
"LDMX_CONDITION_BASEURL")
137 retval += conditions_baseURL_;
139 const char* cenv = getenv(key.c_str());
148 if (j < s.size()) retval.append(s, j);
150 if (retval.find(
"://") == std::string::npos) {
151 retval = conditions_baseURL_ + retval;
157std::pair<const framework::ConditionsObject*, framework::ConditionsIOV>
159 for (
auto tabledef : entries_) {
162 if (tabledef.iov_.validForEvent(context)) {
163 std::string expurl = expandEnv(tabledef.url_);
165 if (expurl ==
"python:") {
167 if (objectType_ == OBJ_int) {
171 table->
add(0, tabledef.ivalues_);
174 }
else if (objectType_ == OBJ_double) {
179 table->
add(0, tabledef.dvalues_);
184 std::unique_ptr<std::istream> stream = urlstream(expurl);
186 if (objectType_ == OBJ_int) {
193 }
else if (objectType_ == OBJ_double) {
#define DECLARE_CONDITIONS_PROVIDER_NS(NS, CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
be configured to load conditions objects from CSV files.
void setIdMask(unsigned int mask)
Set an AND mask to be applied to the id.
void add(unsigned int id, const std::vector< T > &values)
Add an entry to the table.
static void load(IntegerTableCondition &, std::istream &)
Load the table from a stream Columns must be defined by the user.
Class which defines the run/event/type range for which a given condition is valid,...
Base class for all conditions objects, very simple.
Class which represents the process under execution.
Class encapsulating parameters for configuring a processor.
T getParameter(const std::string &name) const
Retrieve the parameter of the given name.
bool exists(const std::string &name) const
Check to see if a parameter exists.
All classes in the ldmx-sw project use this namespace.