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 //
43 // CONSTANTS
44 //
45 
46 static constexpr const unsigned int gbt_id_minvalue = 0;
47 static constexpr const unsigned int gbt_id_maxvalue = 72;
48 static constexpr const unsigned int elink_id_minvalue = 0;
49 static constexpr const unsigned int elink_id_maxvalue = 7;
50 
51 
52 //
53 // SOME HELPER FUNCTIONS
54 //
55 
56 
57 
58 
59 // trim from start (in place)
60 static inline void ltrim(std::string &s)
61 {
62  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
63  {
64  return !std::isspace(ch);
65  }));
66 }
67 
68 // trim from end (in place)
69 static inline void rtrim(std::string &s)
70 {
71  s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch)
72  {
73  return !std::isspace(ch);
74  }).base(), s.end());
75 }
76 
77 // trim from both ends (in place)
78 static inline void trim(std::string &s)
79 {
80  ltrim(s);
81  rtrim(s);
82 }
83 
84 
85 
87 {
88  public:
90  ~DTCCablingMapProducer() override;
91 
92  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
93 
94  private:
95  void beginJob() override;
96  void analyze(const edm::Event&, const edm::EventSetup&) override;
97  void endJob() override;
98 
99  virtual void LoadModulesToDTCCablingMapFromCSV(std::vector<std::string> const&);
100 
101  private:
105  unsigned csvFormat_idetid_ ;
106  unsigned csvFormat_idtcid_ ;
110  std::unique_ptr<TrackerDetToDTCELinkCablingMap> pCablingMap_ ;
112 };
113 
114 
116 {
118  desc.setComment("Stores a TrackerDetToDTCELinkCablingMap object into the database from a CSV file.");
119  desc.add<bool>("generate_fake_valid_gbtlink_and_elinkid", false);
120  desc.add<int>("verbosity", 0);
121  desc.add<unsigned>("csvFormat_idetid" , 0);
122  desc.add<unsigned>("csvFormat_ncolumns" , 15);
123  desc.add<unsigned>("csvFormat_idtcid" , 10);
124  desc.add<unsigned>("csvFormat_igbtlinkid" , 11);
125  desc.add<unsigned>("csvFormat_ielinkid" , 12);
126  desc.add<long long unsigned int>("iovBeginTime" , 1);
127  desc.add<std::string>("record","TrackerDTCCablingMapRcd");
128  desc.add<std::vector<std::string>>("modulesToDTCCablingCSVFileNames",std::vector<std::string>());
129  descriptions.add("DTCCablingMapProducer", desc);
130 }
131 
133  generate_fake_valid_gbtlink_and_elinkid_ (iConfig.getParameter<bool>("generate_fake_valid_gbtlink_and_elinkid")),
134  verbosity_ (iConfig.getParameter<int>("verbosity")),
135  csvFormat_ncolumns_ (iConfig.getParameter<unsigned>("csvFormat_ncolumns")),
136  csvFormat_idetid_ (iConfig.getParameter<unsigned>("csvFormat_idetid" )),
137  csvFormat_idtcid_ (iConfig.getParameter<unsigned>("csvFormat_idtcid" )),
138  iovBeginTime_ (iConfig.getParameter<long long unsigned int>("iovBeginTime")),
140  record_ (iConfig.getParameter<std::string>("record"))
141 {
142  LoadModulesToDTCCablingMapFromCSV(iConfig.getParameter<std::vector<std::string>>("modulesToDTCCablingCSVFileNames"));
143 }
144 
146 {
147 
148 }
149 
150 void DTCCablingMapProducer::LoadModulesToDTCCablingMapFromCSV(std::vector<std::string> const& modulesToDTCCablingCSVFileNames)
151 {
152  using namespace std;
153 
154  for (std::string const& csvFileName : modulesToDTCCablingCSVFileNames)
155  {
156  edm::FileInPath csvFilePath(csvFileName);
157 
158  ifstream csvFile;
159  csvFile.open(csvFilePath.fullPath().c_str());
160 
161  if (csvFile.is_open())
162  {
163  string csvLine;
164 
165  unsigned lineNumber = 0;
166 
167  while (std::getline(csvFile, csvLine))
168  {
169  if (verbosity_ >= 1)
170  {
171  edm::LogInfo("CSVParser") << "Reading CSV file line: " << ++lineNumber << ": \"" << csvLine << "\"" << endl;
172  }
173 
174  istringstream csvStream(csvLine);
175  vector<string> csvColumn;
176  string csvElement;
177 
178  while (std::getline(csvStream, csvElement, ','))
179  {
180  trim(csvElement);
181  csvColumn.push_back(csvElement);
182  }
183 
184  if (verbosity_ >= 2)
185  {
186  ostringstream splitted_line_info;
187 
188  splitted_line_info << "-- split line is: [";
189 
190  for (string const& s : csvColumn)
191  splitted_line_info << "\"" << s << "\", ";
192 
193  splitted_line_info << "]" << endl;
194 
195  edm::LogInfo("CSVParser") << splitted_line_info.str();
196  }
197 
198  if (csvColumn.size() == csvFormat_ncolumns_)
199  {
200  // Skip the legend lines
201  if (0 == csvColumn[0].compare(std::string("Module DetId/U")))
202  {
203  if (verbosity_ >= 1)
204  {
205  edm::LogInfo("CSVParser") << "-- skipping legend line" << endl;
206  }
207  continue;
208  }
209 
210  uint32_t detIdRaw;
211 
212  try
213  {
214  detIdRaw = std::stoi( csvColumn[csvFormat_idetid_] );
215  }
216  catch (std::exception const& e)
217  {
218  if (verbosity_ >= 0)
219  {
220  edm::LogError("CSVParser") << "-- malformed DetId string in CSV file: \"" << csvLine << "\"" << endl;
221  }
222  throw e;
223  }
224 
225  unsigned const dtc_id = strtoul(csvColumn[csvFormat_idtcid_].c_str(), nullptr, 10);
226  unsigned gbt_id;
227  unsigned elink_id;
228 
229 
231  {
232  for (gbt_id = gbt_id_minvalue; gbt_id < gbt_id_maxvalue + 1u; ++gbt_id)
233  {
234  for (elink_id = elink_id_minvalue; elink_id < elink_id_maxvalue + 1u; ++elink_id)
235  {
236  if ( !(pCablingMap_->knowsDTCELinkId( DTCELinkId(dtc_id, gbt_id, elink_id) )) )
237  goto gbtlink_and_elinkid_generator_end; //break out of this double loop
238  }
239  }
240  gbtlink_and_elinkid_generator_end:
241  ((void)0); // This is a NOP, it's here just to have a valid (although dummy) instruction after the goto tag
242  }
243  else
244  {
245  gbt_id = strtoul(csvColumn[csvFormat_igbtlinkid_].c_str(), nullptr, 10);
246  elink_id = strtoul(csvColumn[csvFormat_ielinkid_ ].c_str(), nullptr, 10);
247  }
248 
249 
250  DTCELinkId dtcELinkId( dtc_id, gbt_id, elink_id );
251 
252  if (verbosity_ >= 3)
253  {
254  edm::LogInfo("CSVParser") << "-- DetId = " << detIdRaw << " (dtc_id, gbt_id, elink_id) = (" << dtc_id << "," << gbt_id << "," << elink_id << ")" << endl;
255  }
256 
257  if (pCablingMap_->knowsDTCELinkId(dtcELinkId))
258  {
259  ostringstream message;
260  message << "Reading CSV file: CRITICAL ERROR, duplicated dtcELinkId entry about (dtc_id, gbt_id, elink_id) = (" << dtc_id << "," << gbt_id << "," << elink_id << ")";
261 
262  throw cms::Exception(message.str());
263  }
264 
265  pCablingMap_->insert(dtcELinkId, detIdRaw);
266  }
267  else
268  {
269  if (verbosity_ >= 3)
270  {
271  edm::LogInfo("CSVParser") << "Reading CSV file: Skipped a short line: \"" << csvLine << "\"" << endl;
272  }
273  }
274  }
275  }
276  else
277  {
278  throw cms::Exception("DTCCablingMapProducer: Unable to open input CSV file") << csvFilePath << endl;
279  }
280 
281  csvFile.close();
282  }
283 }
284 
286 {
287 
288 }
289 
291 {
292 // using namespace edm;
293  using namespace std;
294 
296 
297  if( poolDbService.isAvailable() )
298  {
299  poolDbService->writeOne( pCablingMap_.release(), iovBeginTime_, record_ );
300  }
301  else
302  {
303  throw cms::Exception("PoolDBService required.");
304  }
305 }
306 
308 {
309 
310 }
311 
312 //define this as a plug-in
T getParameter(std::string const &) const
bool compare(const P &i, const P &j)
DTCCablingMapProducer(const edm::ParameterSet &)
static const unsigned int gbt_id_minvalue
static void trim(std::string &s)
static void ltrim(std::string &s)
static const unsigned int elink_id_minvalue
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned long long Time_t
Definition: Time.h:16
static const unsigned int gbt_id_maxvalue
bool isAvailable() const
Definition: Service.h:40
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
base
Make Sure CMSSW is Setup ##.
static const unsigned int elink_id_maxvalue
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static void rtrim(std::string &s)
void analyze(const edm::Event &, const edm::EventSetup &) override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::string fullPath() const
Definition: FileInPath.cc:163
std::unique_ptr< TrackerDetToDTCELinkCablingMap > pCablingMap_
#define constexpr
virtual void LoadModulesToDTCCablingMapFromCSV(std::vector< std::string > const &)