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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
// -*- C++ -*-
//
// Package: CondTools/SiPhase2Tracker
// Class: DTCCablingMapProducer
//
/**\class DTCCablingMapProducer DTCCablingMapProducer.cc CondTools/SiPhase2Tracker/plugins/DTCCablingMapProducer.cc
Description: [one line class summary]
Implementation:
[Notes on implementation]
*/
//
// Original Author: Luigi Calligaris, SPRACE, São Paulo, BR
// Created : Wed, 27 Feb 2019 21:41:13 GMT
//
//
#include <memory>
#include <cstdint>
#include <unordered_map>
#include <utility>
#include "FWCore/Framework/interface/Frameworkfwd.h"
// #include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
#include "CondFormats/Common/interface/Time.h"
#include "CondFormats/DataRecord/interface/TrackerDetToDTCELinkCablingMapRcd.h"
#include "CondFormats/SiPhase2TrackerObjects/interface/TrackerDetToDTCELinkCablingMap.h"
#include "CondFormats/SiPhase2TrackerObjects/interface/DTCELinkId.h"
//
// CONSTANTS
//
static constexpr const unsigned int gbt_id_minvalue = 0;
static constexpr const unsigned int gbt_id_maxvalue = 71;
static constexpr const unsigned int elink_id_minvalue = 0;
static constexpr const unsigned int elink_id_maxvalue = 6;
enum { DUMMY_FILL_DISABLED = 0, DUMMY_FILL_ELINK_ID = 1, DUMMY_FILL_ELINK_ID_AND_GBT_ID = 2 };
//
// SOME HELPER FUNCTIONS
//
// trim from start (in place)
static inline void ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
}
// trim from end (in place)
static inline void rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
}
// trim from both ends (in place)
static inline void trim(std::string& s) {
ltrim(s);
rtrim(s);
}
class DTCCablingMapProducer : public edm::one::EDAnalyzer<> {
public:
explicit DTCCablingMapProducer(const edm::ParameterSet&);
~DTCCablingMapProducer() override;
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
private:
void beginJob() override;
void analyze(const edm::Event&, const edm::EventSetup&) override;
void endJob() override;
virtual void LoadModulesToDTCCablingMapFromCSV(std::vector<std::string> const&);
private:
int dummy_fill_mode_;
int verbosity_;
unsigned csvFormat_ncolumns_;
unsigned csvFormat_idetid_;
unsigned csvFormat_idtcid_;
unsigned csvFormat_igbtlinkid_;
unsigned csvFormat_ielinkid_;
cond::Time_t iovBeginTime_;
std::unique_ptr<TrackerDetToDTCELinkCablingMap> pCablingMap_;
std::string record_;
};
void DTCCablingMapProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.setComment("Stores a TrackerDetToDTCELinkCablingMap object into the database from a CSV file.");
desc.add<std::string>("dummy_fill_mode", "DUMMY_FILL_DISABLED");
desc.add<int>("verbosity", 0);
desc.add<unsigned>("csvFormat_ncolumns", 15);
desc.add<unsigned>("csvFormat_idetid", 0);
desc.add<unsigned>("csvFormat_idtcid", 0);
desc.add<unsigned>("csvFormat_igbtlinkid", 0);
desc.add<unsigned>("csvFormat_ielinkid", 0);
desc.add<long long unsigned int>("iovBeginTime", 1);
desc.add<std::string>("record", "TrackerDTCCablingMapRcd");
desc.add<std::vector<std::string>>("modulesToDTCCablingCSVFileNames", std::vector<std::string>());
descriptions.add("DTCCablingMapProducer", desc);
}
DTCCablingMapProducer::DTCCablingMapProducer(const edm::ParameterSet& iConfig)
: verbosity_(iConfig.getParameter<int>("verbosity")),
csvFormat_ncolumns_(iConfig.getParameter<unsigned>("csvFormat_ncolumns")),
csvFormat_idetid_(iConfig.getParameter<unsigned>("csvFormat_idetid")),
csvFormat_idtcid_(iConfig.getParameter<unsigned>("csvFormat_idtcid")),
csvFormat_igbtlinkid_(iConfig.getParameter<unsigned>("csvFormat_igbtlinkid")),
csvFormat_ielinkid_(iConfig.getParameter<unsigned>("csvFormat_ielinkid")),
iovBeginTime_(iConfig.getParameter<long long unsigned int>("iovBeginTime")),
pCablingMap_(std::make_unique<TrackerDetToDTCELinkCablingMap>()),
record_(iConfig.getParameter<std::string>("record")) {
std::string const dummy_fill_mode_param = iConfig.getParameter<std::string>("dummy_fill_mode");
// We pass from the easy to use string to an int representation for this mode flag, as it is more efficient in comparisons
if (dummy_fill_mode_param == "DUMMY_FILL_DISABLED")
dummy_fill_mode_ = DUMMY_FILL_DISABLED;
else if (dummy_fill_mode_param == "DUMMY_FILL_ELINK_ID")
dummy_fill_mode_ = DUMMY_FILL_ELINK_ID;
else if (dummy_fill_mode_param == "DUMMY_FILL_ELINK_ID_AND_GBT_ID")
dummy_fill_mode_ = DUMMY_FILL_ELINK_ID_AND_GBT_ID;
else {
throw cms::Exception("InvalidDummyFillMode")
<< "Parameter dummy_fill_mode with invalid value: " << dummy_fill_mode_param;
}
LoadModulesToDTCCablingMapFromCSV(iConfig.getParameter<std::vector<std::string>>("modulesToDTCCablingCSVFileNames"));
}
void DTCCablingMapProducer::beginJob() {}
void DTCCablingMapProducer::LoadModulesToDTCCablingMapFromCSV(
std::vector<std::string> const& modulesToDTCCablingCSVFileNames) {
using namespace std;
for (std::string const& csvFileName : modulesToDTCCablingCSVFileNames) {
edm::FileInPath csvFilePath(csvFileName);
ifstream csvFile;
csvFile.open(csvFilePath.fullPath().c_str());
if (csvFile.is_open()) {
string csvLine;
unsigned lineNumber = 0;
while (std::getline(csvFile, csvLine)) {
if (verbosity_ >= 1) {
edm::LogInfo("CSVParser") << "Reading CSV file line: " << ++lineNumber << ": \"" << csvLine << "\"" << endl;
}
istringstream csvStream(csvLine);
vector<string> csvColumn;
string csvElement;
while (std::getline(csvStream, csvElement, ',')) {
trim(csvElement);
csvColumn.push_back(csvElement);
}
if (verbosity_ >= 2) {
ostringstream splitted_line_info;
splitted_line_info << "-- split line is: [";
for (string const& s : csvColumn)
splitted_line_info << "\"" << s << "\", ";
splitted_line_info << "]" << endl;
edm::LogInfo("CSVParser") << splitted_line_info.str();
}
if (csvColumn.size() == csvFormat_ncolumns_) {
// Skip the legend lines
if (0 == csvColumn[0].compare(std::string("Module_DetId/U"))) {
if (verbosity_ >= 1) {
edm::LogInfo("CSVParser") << "-- skipping legend line" << endl;
}
continue;
}
uint32_t detIdRaw;
try {
detIdRaw = std::stoi(csvColumn.at(csvFormat_idetid_));
} catch (std::exception const& e) {
if (verbosity_ >= 0) {
edm::LogError("CSVParser") << "-- malformed DetId string in CSV file: \"" << csvLine << "\"" << endl;
}
throw e;
}
unsigned const dtc_id = strtoul(csvColumn.at(csvFormat_idtcid_).c_str(), nullptr, 10);
unsigned gbt_id;
unsigned elink_id;
switch (dummy_fill_mode_) {
default:
case DUMMY_FILL_DISABLED:
gbt_id = strtoul(csvColumn.at(csvFormat_igbtlinkid_).c_str(), nullptr, 10);
elink_id = strtoul(csvColumn.at(csvFormat_ielinkid_).c_str(), nullptr, 10);
break;
case DUMMY_FILL_ELINK_ID:
gbt_id = strtoul(csvColumn.at(csvFormat_igbtlinkid_).c_str(), nullptr, 10);
for (elink_id = elink_id_minvalue; elink_id < elink_id_maxvalue + 1u; ++elink_id) {
if (!(pCablingMap_->knowsDTCELinkId(DTCELinkId(dtc_id, gbt_id, elink_id))))
break;
}
break;
case DUMMY_FILL_ELINK_ID_AND_GBT_ID:
for (gbt_id = gbt_id_minvalue; gbt_id < gbt_id_maxvalue + 1u; ++gbt_id) {
for (elink_id = elink_id_minvalue; elink_id < elink_id_maxvalue + 1u; ++elink_id) {
if (!(pCablingMap_->knowsDTCELinkId(DTCELinkId(dtc_id, gbt_id, elink_id))))
goto gbtlink_and_elinkid_generator_end; //break out of this double loop, this is one of the few "proper" uses of goto
}
}
gbtlink_and_elinkid_generator_end:
((void)0); // This is a NOP, it's here just to have a valid (although dummy) instruction after the goto tag
break;
}
DTCELinkId dtcELinkId(dtc_id, gbt_id, elink_id);
if (verbosity_ >= 3) {
edm::LogInfo("CSVParser") << "-- DetId = " << detIdRaw << " (dtc_id, gbt_id, elink_id) = (" << dtc_id << ","
<< gbt_id << "," << elink_id << ")" << endl;
}
if (pCablingMap_->knowsDTCELinkId(dtcELinkId)) {
throw cms::Exception("DuplicateDTCELinkIdInCSV")
<< "Reading CSV file: CRITICAL ERROR, duplicate dtcELinkId entry about (dtc_id, gbt_id, elink_id) = ("
<< dtc_id << "," << gbt_id << "," << elink_id << ")";
}
pCablingMap_->insert(dtcELinkId, detIdRaw);
} else {
if (verbosity_ >= 3) {
edm::LogInfo("CSVParser") << "Reading CSV file: Skipped a short line: \"" << csvLine << "\"" << endl;
}
}
}
} else {
throw cms::Exception("CSVFileNotFound") << "Unable to open input CSV file" << csvFilePath << endl;
}
csvFile.close();
}
}
void DTCCablingMapProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {}
void DTCCablingMapProducer::endJob() {
// using namespace edm;
using namespace std;
edm::Service<cond::service::PoolDBOutputService> poolDbService;
if (poolDbService.isAvailable()) {
poolDbService->writeOneIOV(*pCablingMap_, iovBeginTime_, record_);
} else {
throw cms::Exception("PoolDBServiceNotFound") << "A running PoolDBService instance is required.";
}
}
DTCCablingMapProducer::~DTCCablingMapProducer() {}
//define this as a plug-in
DEFINE_FWK_MODULE(DTCCablingMapProducer);
|