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->writeOne(pCablingMap_.release(), 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
rtrim
static void rtrim(std::string &s)
Definition: DTCCablingMapProducer.cc:62
TrackerDetToDTCELinkCablingMap
Definition: TrackerDetToDTCELinkCablingMap.h:29
DTCCablingMapProducer::beginJob
void beginJob() override
Definition: DTCCablingMapProducer.cc:142
EDAnalyzer.h
gbt_id_maxvalue
static constexpr const unsigned int gbt_id_maxvalue
Definition: DTCCablingMapProducer.cc:46
MessageLogger.h
DTCCablingMapProducer::verbosity_
int verbosity_
Definition: DTCCablingMapProducer.cc:88
DTCELinkId
Definition: DTCELinkId.h:28
DTCCablingMapProducer::csvFormat_ncolumns_
unsigned csvFormat_ncolumns_
Definition: DTCCablingMapProducer.cc:89
DTCCablingMapProducer::pCablingMap_
std::unique_ptr< TrackerDetToDTCELinkCablingMap > pCablingMap_
Definition: DTCCablingMapProducer.cc:95
Time.h
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
DTCCablingMapProducer::csvFormat_ielinkid_
unsigned csvFormat_ielinkid_
Definition: DTCCablingMapProducer.cc:93
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
edm::one::EDAnalyzer
Definition: EDAnalyzer.h:30
DTCCablingMapProducer::csvFormat_idtcid_
unsigned csvFormat_idtcid_
Definition: DTCCablingMapProducer.cc:91
TrackerDetToDTCELinkCablingMap.h
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
gbt_id_minvalue
static constexpr const unsigned int gbt_id_minvalue
Definition: DTCCablingMapProducer.cc:45
elink_id_maxvalue
static constexpr const unsigned int elink_id_maxvalue
Definition: DTCCablingMapProducer.cc:48
edm::FileInPath
Definition: FileInPath.h:64
MakerMacros.h
alignCSCRings.s
s
Definition: alignCSCRings.py:92
PoolDBOutputService.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::ConfigurationDescriptions::add
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:57
DTCCablingMapProducer::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: DTCCablingMapProducer.cc:263
DUMMY_FILL_ELINK_ID_AND_GBT_ID
Definition: DTCCablingMapProducer.cc:50
Service.h
DTCCablingMapProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: DTCCablingMapProducer.cc:99
DUMMY_FILL_DISABLED
Definition: DTCCablingMapProducer.cc:50
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
DTCCablingMapProducer::DTCCablingMapProducer
DTCCablingMapProducer(const edm::ParameterSet &)
Definition: DTCCablingMapProducer.cc:115
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
cond::Time_t
unsigned long long Time_t
Definition: Time.h:14
DTCCablingMapProducer::csvFormat_idetid_
unsigned csvFormat_idetid_
Definition: DTCCablingMapProducer.cc:90
edm::Service< cond::service::PoolDBOutputService >
createfilelist.int
int
Definition: createfilelist.py:10
iEvent
int iEvent
Definition: GenABIO.cc:224
DUMMY_FILL_ELINK_ID
Definition: DTCCablingMapProducer.cc:50
edm::EventSetup
Definition: EventSetup.h:58
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
DTCCablingMapProducer::dummy_fill_mode_
int dummy_fill_mode_
Definition: DTCCablingMapProducer.cc:87
DTCCablingMapProducer::iovBeginTime_
cond::Time_t iovBeginTime_
Definition: DTCCablingMapProducer.cc:94
DTCCablingMapProducer::record_
std::string record_
Definition: DTCCablingMapProducer.cc:96
DTCCablingMapProducer::csvFormat_igbtlinkid_
unsigned csvFormat_igbtlinkid_
Definition: DTCCablingMapProducer.cc:92
DTCCablingMapProducer::~DTCCablingMapProducer
~DTCCablingMapProducer() override
Definition: DTCCablingMapProducer.cc:278
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
std
Definition: JetResolutionObject.h:76
elink_id_minvalue
static constexpr const unsigned int elink_id_minvalue
Definition: DTCCablingMapProducer.cc:47
Frameworkfwd.h
TrackerDetToDTCELinkCablingMapRcd.h
Exception
Definition: hltDiff.cc:245
cond::service::PoolDBOutputService::writeOne
Hash writeOne(const T *payload, Time_t time, const std::string &recordName)
Definition: PoolDBOutputService.h:63
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
DTCCablingMapProducer::LoadModulesToDTCCablingMapFromCSV
virtual void LoadModulesToDTCCablingMapFromCSV(std::vector< std::string > const &)
Definition: DTCCablingMapProducer.cc:144
funct::void
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
DTCELinkId.h
ParameterSet.h
DTCCablingMapProducer::endJob
void endJob() override
Definition: DTCCablingMapProducer.cc:265
compare
bool compare(const P &i, const P &j)
Definition: BDHadronTrackMonitoringAnalyzer.cc:203
edm::Event
Definition: Event.h:73
newFWLiteAna.base
base
Definition: newFWLiteAna.py:92
trim
static void trim(std::string &s)
Definition: DTCCablingMapProducer.cc:67
DTCCablingMapProducer
Definition: DTCCablingMapProducer.cc:72
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:161
ltrim
static void ltrim(std::string &s)
Definition: DTCCablingMapProducer.cc:57
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37