CMS 3D CMS Logo

DTCCablingMapProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/SiPhase2Tracker
4 // Class: DTCCablingMapProducer
5 //
13 //
14 // Original Author: Luigi Calligaris, SPRACE, São Paulo, BR
15 // Created : Wed, 27 Feb 2019 21:41:13 GMT
16 //
17 //
18 
19 #include <memory>
20 #include <cstdint>
21 #include <unordered_map>
22 #include <utility>
23 
25 // #include "FWCore/Framework/interface/ESHandle.h"
33 
35 
40 
41 //
42 // CONSTANTS
43 //
44 
45 static constexpr const unsigned int gbt_id_minvalue = 0;
46 static constexpr const unsigned int gbt_id_maxvalue = 71;
47 static constexpr const unsigned int elink_id_minvalue = 0;
48 static constexpr const unsigned int elink_id_maxvalue = 6;
49 
51 
52 //
53 // SOME HELPER FUNCTIONS
54 //
55 
56 // trim from start (in place)
57 static inline void ltrim(std::string& s) {
58  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
59 }
60 
61 // trim from end (in place)
62 static inline void rtrim(std::string& s) {
63  s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
64 }
65 
66 // trim from both ends (in place)
67 static inline void trim(std::string& s) {
68  ltrim(s);
69  rtrim(s);
70 }
71 
73 public:
75  ~DTCCablingMapProducer() override;
76 
77  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
78 
79 private:
80  void beginJob() override;
81  void analyze(const edm::Event&, const edm::EventSetup&) override;
82  void endJob() override;
83 
84  virtual void LoadModulesToDTCCablingMapFromCSV(std::vector<std::string> const&);
85 
86 private:
95  std::unique_ptr<TrackerDetToDTCELinkCablingMap> pCablingMap_;
97 };
98 
101  desc.setComment("Stores a TrackerDetToDTCELinkCablingMap object into the database from a CSV file.");
102  desc.add<std::string>("dummy_fill_mode", "DUMMY_FILL_DISABLED");
103  desc.add<int>("verbosity", 0);
104  desc.add<unsigned>("csvFormat_ncolumns", 15);
105  desc.add<unsigned>("csvFormat_idetid", 0);
106  desc.add<unsigned>("csvFormat_idtcid", 0);
107  desc.add<unsigned>("csvFormat_igbtlinkid", 0);
108  desc.add<unsigned>("csvFormat_ielinkid", 0);
109  desc.add<long long unsigned int>("iovBeginTime", 1);
110  desc.add<std::string>("record", "TrackerDTCCablingMapRcd");
111  desc.add<std::vector<std::string>>("modulesToDTCCablingCSVFileNames", std::vector<std::string>());
112  descriptions.add("DTCCablingMapProducer", desc);
113 }
114 
116  : verbosity_(iConfig.getParameter<int>("verbosity")),
117  csvFormat_ncolumns_(iConfig.getParameter<unsigned>("csvFormat_ncolumns")),
118  csvFormat_idetid_(iConfig.getParameter<unsigned>("csvFormat_idetid")),
119  csvFormat_idtcid_(iConfig.getParameter<unsigned>("csvFormat_idtcid")),
120  csvFormat_igbtlinkid_(iConfig.getParameter<unsigned>("csvFormat_igbtlinkid")),
121  csvFormat_ielinkid_(iConfig.getParameter<unsigned>("csvFormat_ielinkid")),
122  iovBeginTime_(iConfig.getParameter<long long unsigned int>("iovBeginTime")),
123  pCablingMap_(std::make_unique<TrackerDetToDTCELinkCablingMap>()),
124  record_(iConfig.getParameter<std::string>("record")) {
125  std::string const dummy_fill_mode_param = iConfig.getParameter<std::string>("dummy_fill_mode");
126 
127  // We pass from the easy to use string to an int representation for this mode flag, as it is more efficient in comparisons
128  if (dummy_fill_mode_param == "DUMMY_FILL_DISABLED")
130  else if (dummy_fill_mode_param == "DUMMY_FILL_ELINK_ID")
132  else if (dummy_fill_mode_param == "DUMMY_FILL_ELINK_ID_AND_GBT_ID")
134  else {
135  throw cms::Exception("InvalidDummyFillMode")
136  << "Parameter dummy_fill_mode with invalid value: " << dummy_fill_mode_param;
137  }
138 
139  LoadModulesToDTCCablingMapFromCSV(iConfig.getParameter<std::vector<std::string>>("modulesToDTCCablingCSVFileNames"));
140 }
141 
143 
145  std::vector<std::string> const& modulesToDTCCablingCSVFileNames) {
146  using namespace std;
147 
148  for (std::string const& csvFileName : modulesToDTCCablingCSVFileNames) {
149  edm::FileInPath csvFilePath(csvFileName);
150 
151  ifstream csvFile;
152  csvFile.open(csvFilePath.fullPath().c_str());
153 
154  if (csvFile.is_open()) {
155  string csvLine;
156 
157  unsigned lineNumber = 0;
158 
159  while (std::getline(csvFile, csvLine)) {
160  if (verbosity_ >= 1) {
161  edm::LogInfo("CSVParser") << "Reading CSV file line: " << ++lineNumber << ": \"" << csvLine << "\"" << endl;
162  }
163 
164  istringstream csvStream(csvLine);
165  vector<string> csvColumn;
166  string csvElement;
167 
168  while (std::getline(csvStream, csvElement, ',')) {
169  trim(csvElement);
170  csvColumn.push_back(csvElement);
171  }
172 
173  if (verbosity_ >= 2) {
174  ostringstream splitted_line_info;
175 
176  splitted_line_info << "-- split line is: [";
177 
178  for (string const& s : csvColumn)
179  splitted_line_info << "\"" << s << "\", ";
180 
181  splitted_line_info << "]" << endl;
182 
183  edm::LogInfo("CSVParser") << splitted_line_info.str();
184  }
185 
186  if (csvColumn.size() == csvFormat_ncolumns_) {
187  // Skip the legend lines
188  if (0 == csvColumn[0].compare(std::string("Module_DetId/U"))) {
189  if (verbosity_ >= 1) {
190  edm::LogInfo("CSVParser") << "-- skipping legend line" << endl;
191  }
192  continue;
193  }
194 
195  uint32_t detIdRaw;
196 
197  try {
198  detIdRaw = std::stoi(csvColumn.at(csvFormat_idetid_));
199  } catch (std::exception const& e) {
200  if (verbosity_ >= 0) {
201  edm::LogError("CSVParser") << "-- malformed DetId string in CSV file: \"" << csvLine << "\"" << endl;
202  }
203  throw e;
204  }
205 
206  unsigned const dtc_id = strtoul(csvColumn.at(csvFormat_idtcid_).c_str(), nullptr, 10);
207  unsigned gbt_id;
208  unsigned elink_id;
209 
210  switch (dummy_fill_mode_) {
211  default:
212  case DUMMY_FILL_DISABLED:
213  gbt_id = strtoul(csvColumn.at(csvFormat_igbtlinkid_).c_str(), nullptr, 10);
214  elink_id = strtoul(csvColumn.at(csvFormat_ielinkid_).c_str(), nullptr, 10);
215  break;
216  case DUMMY_FILL_ELINK_ID:
217  gbt_id = strtoul(csvColumn.at(csvFormat_igbtlinkid_).c_str(), nullptr, 10);
218  for (elink_id = elink_id_minvalue; elink_id < elink_id_maxvalue + 1u; ++elink_id) {
219  if (!(pCablingMap_->knowsDTCELinkId(DTCELinkId(dtc_id, gbt_id, elink_id))))
220  break;
221  }
222  break;
224  for (gbt_id = gbt_id_minvalue; gbt_id < gbt_id_maxvalue + 1u; ++gbt_id) {
225  for (elink_id = elink_id_minvalue; elink_id < elink_id_maxvalue + 1u; ++elink_id) {
226  if (!(pCablingMap_->knowsDTCELinkId(DTCELinkId(dtc_id, gbt_id, elink_id))))
227  goto gbtlink_and_elinkid_generator_end; //break out of this double loop, this is one of the few "proper" uses of goto
228  }
229  }
230  gbtlink_and_elinkid_generator_end:
231  ((void)0); // This is a NOP, it's here just to have a valid (although dummy) instruction after the goto tag
232  break;
233  }
234 
235  DTCELinkId dtcELinkId(dtc_id, gbt_id, elink_id);
236 
237  if (verbosity_ >= 3) {
238  edm::LogInfo("CSVParser") << "-- DetId = " << detIdRaw << " (dtc_id, gbt_id, elink_id) = (" << dtc_id << ","
239  << gbt_id << "," << elink_id << ")" << endl;
240  }
241 
242  if (pCablingMap_->knowsDTCELinkId(dtcELinkId)) {
243  throw cms::Exception("DuplicateDTCELinkIdInCSV")
244  << "Reading CSV file: CRITICAL ERROR, duplicate dtcELinkId entry about (dtc_id, gbt_id, elink_id) = ("
245  << dtc_id << "," << gbt_id << "," << elink_id << ")";
246  }
247 
248  pCablingMap_->insert(dtcELinkId, detIdRaw);
249  } else {
250  if (verbosity_ >= 3) {
251  edm::LogInfo("CSVParser") << "Reading CSV file: Skipped a short line: \"" << csvLine << "\"" << endl;
252  }
253  }
254  }
255  } else {
256  throw cms::Exception("CSVFileNotFound") << "Unable to open input CSV file" << csvFilePath << endl;
257  }
258 
259  csvFile.close();
260  }
261 }
262 
264 
266  // using namespace edm;
267  using namespace std;
268 
270 
271  if (poolDbService.isAvailable()) {
272  poolDbService->writeOneIOV(*pCablingMap_, iovBeginTime_, record_);
273  } else {
274  throw cms::Exception("PoolDBServiceNotFound") << "A running PoolDBService instance is required.";
275  }
276 }
277 
279 
280 //define this as a plug-in
bool compare(const P &i, const P &j)
DTCCablingMapProducer(const edm::ParameterSet &)
static constexpr const unsigned int gbt_id_minvalue
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::string fullPath() const
Definition: FileInPath.cc:161
base
Main Program
Definition: newFWLiteAna.py:92
static void trim(std::string &s)
Log< level::Error, false > LogError
static void ltrim(std::string &s)
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
int iEvent
Definition: GenABIO.cc:224
unsigned long long Time_t
Definition: Time.h:14
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static constexpr const unsigned int elink_id_minvalue
static void rtrim(std::string &s)
Log< level::Info, false > LogInfo
void analyze(const edm::Event &, const edm::EventSetup &) override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool isAvailable() const
Definition: Service.h:40
static constexpr const unsigned int elink_id_maxvalue
std::unique_ptr< TrackerDetToDTCELinkCablingMap > pCablingMap_
static constexpr const unsigned int gbt_id_maxvalue
virtual void LoadModulesToDTCCablingMapFromCSV(std::vector< std::string > const &)