CMS 3D CMS Logo

DTCompactMapWriter.cc
Go to the documentation of this file.
1 
11 //----------------------
12 // This Class' Header --
13 //----------------------
15 
16 //-------------------------------
17 // Collaborating Class Headers --
18 //-------------------------------
19 
20 //---------------
21 // C++ Headers --
22 //---------------
23 #include <string>
24 #include <vector>
25 #include <map>
26 #include <algorithm>
27 #include <iostream>
28 #include <fstream>
29 
31 public:
32  DTROSChannelId(int ddu, int ros, int channel);
33  int dduId() const;
34  int rosId() const;
35  int channelId() const;
36 
37 private:
38  int ddu_id;
39  int ros_id;
41 };
42 
43 DTROSChannelId::DTROSChannelId(int ddu, int ros, int channel) : ddu_id(ddu), ros_id(ros), channel_id(channel) {}
44 
45 int DTROSChannelId::dduId() const { return ddu_id; }
46 
47 int DTROSChannelId::rosId() const { return ros_id; }
48 
49 int DTROSChannelId::channelId() const { return channel_id; }
50 
52 public:
53  bool operator()(const DTROSChannelId& idl, const DTROSChannelId& idr) const;
54 
55 private:
56 };
57 
59  if (idl.dduId() < idr.dduId())
60  return true;
61  if (idl.dduId() > idr.dduId())
62  return false;
63  if (idl.rosId() < idr.rosId())
64  return true;
65  if (idl.rosId() > idr.rosId())
66  return false;
67  if (idl.channelId() < idr.channelId())
68  return true;
69  if (idl.channelId() > idr.channelId())
70  return false;
71  return false;
72 }
73 
74 class DTROBCardId {
75 public:
76  DTROBCardId(int wheel, int sector, int station, int rob);
77  int wheelId() const;
78  int sectorId() const;
79  int stationId() const;
80  int robId() const;
81 
82 private:
83  int wheel_id;
84  int sector_id;
86  int rob_id;
87 };
88 
89 DTROBCardId::DTROBCardId(int wheel, int sector, int station, int rob)
90  : wheel_id(wheel), sector_id(sector), station_id(station), rob_id(rob) {}
91 
92 int DTROBCardId::wheelId() const { return wheel_id; }
93 
94 int DTROBCardId::sectorId() const { return sector_id; }
95 
96 int DTROBCardId::stationId() const { return station_id; }
97 
98 int DTROBCardId::robId() const { return rob_id; }
99 
101 public:
102  bool operator()(const DTROBCardId& idl, const DTROBCardId& idr) const;
103 
104 private:
105 };
106 
107 bool DTROBCardCompare::operator()(const DTROBCardId& idl, const DTROBCardId& idr) const {
108  if (idl.wheelId() < idr.wheelId())
109  return true;
110  if (idl.wheelId() > idr.wheelId())
111  return false;
112  if (idl.sectorId() < idr.sectorId())
113  return true;
114  if (idl.sectorId() > idr.sectorId())
115  return false;
116  if (idl.stationId() < idr.stationId())
117  return true;
118  if (idl.stationId() > idr.stationId())
119  return false;
120  if (idl.robId() < idr.robId())
121  return true;
122  if (idl.robId() > idr.robId())
123  return false;
124  return false;
125 }
126 
128 public:
129  DTTDCChannelId(int tdc, int channel);
130  int tdcId() const;
131  int channelId() const;
132 
133 private:
134  int tdc_id;
136 };
137 
138 DTTDCChannelId::DTTDCChannelId(int tdc, int channel) : tdc_id(tdc), channel_id(channel) {}
139 
140 int DTTDCChannelId::tdcId() const { return tdc_id; }
141 
142 int DTTDCChannelId::channelId() const { return channel_id; }
143 
145 public:
146  bool operator()(const DTTDCChannelId& idl, const DTTDCChannelId& idr) const;
147 
148 private:
149 };
150 
152  if (idl.tdcId() < idr.tdcId())
153  return true;
154  if (idl.tdcId() > idr.tdcId())
155  return false;
156  if (idl.channelId() < idr.channelId())
157  return true;
158  if (idl.channelId() > idr.channelId())
159  return false;
160  return false;
161 }
162 
164 public:
165  DTPhysicalWireId(int superlayer, int layer, int cell);
166  int superlayerId() const;
167  int layerId() const;
168  int cellId() const;
169 
170 private:
172  int layer_id;
173  int cell_id;
174 };
175 
176 DTPhysicalWireId::DTPhysicalWireId(int superlayer, int layer, int cell)
177  : superlayer_id(superlayer), layer_id(layer), cell_id(cell) {}
178 
180 
181 int DTPhysicalWireId::layerId() const { return layer_id; }
182 
183 int DTPhysicalWireId::cellId() const { return cell_id; }
184 
186 public:
187  bool operator()(const DTPhysicalWireId& idl, const DTPhysicalWireId& idr) const;
188 
189 private:
190 };
191 
193  if (idl.superlayerId() < idr.superlayerId())
194  return true;
195  if (idl.superlayerId() > idr.superlayerId())
196  return false;
197  if (idl.layerId() < idr.layerId())
198  return true;
199  if (idl.layerId() > idr.layerId())
200  return false;
201  if (idl.cellId() < idr.cellId())
202  return true;
203  if (idl.cellId() > idr.cellId())
204  return false;
205  return false;
206 }
207 
209 public:
210  static int defaultIdValue;
211  static int ROSMapIdOffset;
212  static int ROBMapIdOffset;
213  static int TDCMapIdOffset;
214 };
215 
220 
221 void DTCompactMapWriter::buildSteering(std::istream& jobDesc) {
222  // template maps and lists, name and type
223  std::string map_type("dummy_string");
224  std::string map_file("dummy_string");
225  std::string map_list("dummy_string");
226 
227  // ros-rob and tdc-cell maps
228  std::map<DTROBCardId, int, DTROBCardCompare> tdc_idm;
229  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare> ros_map;
230  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare> rob_map;
231 
232  int rob_count = 1;
233  int ros_count = 1;
234 
235  // parse job description file and loop over map templates and lists
236  bool jobEnd = false;
237  int max_line_length = 1000;
238  char* job_line = new char[max_line_length];
239  while (jobDesc.getline(job_line, max_line_length)) {
240  // parse job description line
241  char* ptr_line = job_line;
242  char* end_line = job_line + max_line_length;
243  while (ptr_line < end_line) {
244  // get description command key
245  // remove leading blanks
246  while (*ptr_line == ' ')
247  ptr_line++;
248  std::string key(ptr_line);
249  int off_blank = key.find(" ");
250  int off_equal = key.find("=");
251  if (off_equal < 0)
252  off_equal = max_line_length;
253  if (off_blank < 0)
254  off_blank = key.length();
255  int ioff = (off_blank < off_equal ? off_blank : off_equal);
256  key.erase(ioff++, key.length());
257  // go to command value
258  ptr_line += ++off_equal;
259  // exit loop at end of description file
260  if ((jobEnd = (key == "end")))
261  break;
262  // get description command value
263  while (*ptr_line == ' ')
264  ptr_line++;
265  std::string val(ptr_line);
266  int off_value = val.find(" ");
267  if ((off_value > 0) && (off_value < static_cast<int>(val.length())))
268  val.erase(off_value++, val.length());
269  // go to next token
270  ptr_line += off_value;
271  // set type, template or list file name
272  if (key == "type")
273  map_type = val;
274  if (key == "map")
275  map_file = val;
276  if (key == "list")
277  map_list = val;
278  }
279  if (jobEnd)
280  break;
281 
282  // open map list file
283  std::ifstream lfile(map_list.c_str());
284 
285  bool write;
286  // store TDC maps
287  if (map_type == "TDC") {
288  // loop over TDC map list for concerned wheels and sectors
289  int station = 0;
290  int wheel;
291  int sector;
292  write = true;
293  while (lfile >> wheel >> sector >> station)
294  fillTDCMap(map_file, wheel, sector, tdc_idm, station, rob_count, write);
295  ++rob_count;
296  }
297 
298  // store ROS maps
299  if (map_type == "ROS") {
300  // loop over ROS map list for concerned ddu, ros, wheel and sector
301  int ddu;
302  int ros;
303  int wheel;
304  int sector;
305  write = true;
306  while (lfile >> ddu >> ros >> wheel >> sector)
307  fillROSMap(map_file, ddu, ros, wheel, sector, ros_map, rob_map, ros_count, write);
308  ++ros_count;
309  }
310  }
311  delete[] job_line;
312 
313  // merge ROS and TDC maps
314  fillReadOutMap(ros_count, tdc_idm, ros_map, rob_map);
315 
316  return;
317 }
318 
320  int wheel,
321  int sector,
322  std::map<DTROBCardId, int, DTROBCardCompare>& tdc_idm,
323  int stationId,
324  int map_count,
325  bool& write) {
326  // template map file name
327  std::ifstream ifile(map_file.c_str());
328 
329  // get station number
330  int station;
331  int rob;
332  station = stationId;
333  ifile >> rob;
334  DTROBCardId key(wheel, sector, station, rob);
335  tdc_idm.insert(std::pair<DTROBCardId, int>(key, map_count));
336  if (!write)
337  return;
338  // loop over TDC channels and cells
339  int superlayer;
340  int layer;
341  int wire;
342  int tdc;
343  int channel;
344  while (ifile >> tdc >> channel >> superlayer >> layer >> wire) {
345  // set ROB card, TDC channel and cell identifiers
346  DTTDCChannelId tdcChannel(tdc, channel);
347  DTPhysicalWireId physicalWire(superlayer, layer, wire);
348  // look for rob card connection map
349  std::cout << DTMapElementIdentifiers::TDCMapIdOffset << " " << map_count << " " << rob << " " << tdc << " "
350  << channel << " " << DTMapElementIdentifiers::defaultIdValue << " "
352  << superlayer << " " << layer << " " << wire << std::endl;
353  }
354  write = false;
355  return;
356 }
357 
359  int ddu,
360  int ros,
361  int whdef,
362  int secdef,
363  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ddu_map,
364  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_map,
365  int map_count,
366  bool& write) {
367  DTROSChannelId rosBoard(ddu, ros, 0);
368  DTROBCardId sectorId(whdef, secdef, 0, map_count);
369  ddu_map.insert(std::pair<DTROSChannelId, DTROBCardId>(rosBoard, sectorId));
370 
371  // template map file name
372  std::ifstream ifile(map_file.c_str());
373 
374  // loop over ROS channels and ROBs
375  int channel;
376  int wheel;
377  int sector;
378  int station;
379  int rob;
380  DTROSChannelId rosChanMap(ddu, ros, 0);
381  if (!write)
382  return;
383  while (ifile >> channel >> wheel >> sector >> station >> rob) {
384  // set sector to default unless specified
385  if (!sector)
387  if (!wheel)
389  // set ROS channel and ROB identifiers
390  DTROSChannelId rosChannel(ddu, ros, channel);
391  DTROBCardId robCard(wheel, sector, station, rob);
392  // store ROS channel to ROB connection into map
393  ros_map.insert(std::pair<DTROSChannelId, DTROBCardId>(rosChannel, robCard));
394  }
395  write = false;
396 
397  return;
398 }
399 
400 void DTCompactMapWriter::cloneROS(std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_m,
401  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_a,
402  int ddu_o,
403  int ros_o,
404  int ddu_f,
405  int ros_f) {
406  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iter = ros_m.begin();
407  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iend = ros_m.end();
408  while (ros_iter != ros_iend) {
409  const std::pair<DTROSChannelId, DTROBCardId>& rosLink = *ros_iter++;
410  const DTROSChannelId& rosChannelId = rosLink.first;
411  const DTROBCardId& robCardId = rosLink.second;
412  if (rosChannelId.dduId() != ddu_o)
413  continue;
414  if (rosChannelId.rosId() != ros_o)
415  continue;
416  const DTROSChannelId rosChanNewId(ddu_f, ros_f, rosChannelId.channelId());
417  ros_a.insert(std::pair<DTROSChannelId, DTROBCardId>(rosChanNewId, robCardId));
418  }
419  return;
420 }
421 
422 void DTCompactMapWriter::appendROS(std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_m,
423  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_a) {
424  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iter = ros_a.begin();
425  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iend = ros_a.end();
426  while (ros_iter != ros_iend)
427  ros_m.insert(*ros_iter++);
428  return;
429 }
430 
432  std::map<DTROBCardId, int, DTROBCardCompare>& tdc_idm,
433  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ddu_map,
434  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>& ros_map) {
435  bool check = true;
436  bool write = false;
437 
438  while (check || write) {
439  // ROS board to sector connection map
440  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ddu_iter = ddu_map.begin();
441  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ddu_iend = ddu_map.end();
442  // ROB card to TDC element map
443  std::map<DTROBCardId, int, DTROBCardCompare>::const_iterator idt_iend = tdc_idm.end();
444  // ROS channel to ROB connection map
445  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iter = ros_map.begin();
446  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::const_iterator ros_iend = ros_map.end();
447 
448  check = false;
449  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare> ros_app;
450 
451  // loop over ROS channels
452  while (ros_iter != ros_iend) {
453  // get ROS channel and ROB identifiers
454  const std::pair<DTROSChannelId, DTROBCardId>& rosLink = *ros_iter++;
455  const DTROSChannelId& rosChannelId = rosLink.first;
456  const DTROSChannelId rosChanMapId(rosChannelId.dduId(), rosChannelId.rosId(), 0);
457  const DTROBCardId& robCMapId = rosLink.second;
458  ddu_iter = ddu_map.find(rosChanMapId);
459  int whdef = 0;
460  int secdef = 0;
461  int rosMapCk = 0;
462  if (ddu_iter != ddu_iend) {
463  const DTROBCardId& sector = ddu_iter->second;
464  whdef = sector.wheelId();
465  secdef = sector.sectorId();
466  rosMapCk = sector.robId();
467  } else {
468  std::cout << "DDU map " << rosChannelId.dduId() << " " << rosChannelId.rosId() << " 0 not found" << std::endl;
469  continue;
470  }
471  int wheel = robCMapId.wheelId();
472  int sector = robCMapId.sectorId();
473  int station = robCMapId.stationId();
474  int rob = robCMapId.robId();
476  whdef = wheel;
478  secdef = sector;
479  const DTROBCardId robCardId(whdef, secdef, station, rob);
480  std::map<DTROBCardId, int, DTROBCardCompare>::const_iterator idt_iter = tdc_idm.find(robCardId);
481  if (idt_iter != idt_iend) {
482  int tdcMapId = idt_iter->second;
483  ddu_iter = ddu_map.begin();
484  while (ddu_iter != ddu_iend) {
485  const std::pair<DTROSChannelId, DTROBCardId>& dduLink = *ddu_iter++;
486  const DTROSChannelId& ros_id = dduLink.first;
487  const DTROBCardId& sec_id = dduLink.second;
488  int whe_chk = sec_id.wheelId();
489  int sec_chk = sec_id.sectorId();
491  whe_chk = wheel;
493  sec_chk = sector;
494  if (rosMapCk != sec_id.robId())
495  continue;
496  DTROBCardId robCardId(whe_chk, sec_chk, station, rob);
497  idt_iter = tdc_idm.find(robCardId);
498  int tdcMapCk = idt_iter->second;
499  if ((tdcMapId != tdcMapCk) && (!check)) {
500  cloneROS(ros_map, ros_app, rosChannelId.dduId(), rosChannelId.rosId(), ros_id.dduId(), ros_id.rosId());
501  tdcMapId = tdcMapCk;
502  check = true;
503  }
504  if ((tdcMapId == tdcMapCk) && check) {
505  std::map<DTROSChannelId, DTROBCardId, DTROSChannelCompare>::iterator ddu_inew = ddu_map.find(ros_id);
506  ddu_inew->second = DTROBCardId(sec_id.wheelId(), sec_id.sectorId(), sec_id.stationId(), ros_count);
507  }
508  }
509  if (check) {
510  ros_count++;
511  break;
512  }
513  if (write)
514  std::cout << DTMapElementIdentifiers::ROSMapIdOffset << " " << rosMapCk << " " << rosChannelId.channelId()
516  << " " << wheel << " " << station << " " << sector << " " << rob << " "
517  << DTMapElementIdentifiers::TDCMapIdOffset << " " << tdcMapId << std::endl;
518 
519  } else {
520  std::cout << "TDC map " << wheel << " " << sector << " " << station << " " << rob << " not found" << std::endl;
521  }
522  }
523  appendROS(ros_map, ros_app);
524  if (!write) {
525  write = !check;
526  } else {
527  ddu_iter = ddu_map.begin();
528  while (ddu_iter != ddu_iend) {
529  const std::pair<DTROSChannelId, DTROBCardId>& dduLink = *ddu_iter++;
530  const DTROSChannelId& ros_id = dduLink.first;
531  const DTROBCardId& sec_id = dduLink.second;
532  std::cout << ros_id.dduId() << " " << ros_id.rosId() << " " << DTMapElementIdentifiers::defaultIdValue << " "
534  << sec_id.wheelId() << " " << DTMapElementIdentifiers::defaultIdValue << " " << sec_id.sectorId()
536  << " " << sec_id.robId() << std::endl;
537  }
538  write = false;
539  }
540  }
541 
542  return;
543 }
bool operator()(const DTROSChannelId &idl, const DTROSChannelId &idr) const
int channelId() const
int rosId() const
bool operator()(const DTTDCChannelId &idl, const DTTDCChannelId &idr) const
DTTDCChannelId(int tdc, int channel)
static void buildSteering(std::istream &jobDesc)
bool operator()(const DTPhysicalWireId &idl, const DTPhysicalWireId &idr) const
DTPhysicalWireId(int superlayer, int layer, int cell)
static void appendROS(std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ros_m, std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ros_a)
static void fillROSMap(const std::string &map_file, int ddu, int ros, int whdef, int secdef, std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ddu_map, std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ros_map, int map_count, bool &write)
int channelId() const
int wheelId() const
static void cloneROS(std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ros_m, std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ros_a, int ddu_o, int ros_o, int ddu_f, int ros_f)
int robId() const
DTROSChannelId(int ddu, int ros, int channel)
int dduId() const
int superlayerId() const
static void fillReadOutMap(int ros_count, std::map< DTROBCardId, int, DTROBCardCompare > &tdc_idm, std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ddu_map, std::map< DTROSChannelId, DTROBCardId, DTROSChannelCompare > &ros_map)
int stationId() const
bool operator()(const DTROBCardId &idl, const DTROBCardId &idr) const
int sectorId() const
static void fillTDCMap(const std::string &map_file, int wheel, int sector, std::map< DTROBCardId, int, DTROBCardCompare > &tdc_idm, int stationId, int map_count, bool &write)
DTROBCardId(int wheel, int sector, int station, int rob)