CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 //---------------
22 // C++ Headers --
23 //---------------
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <algorithm>
28 #include <iostream>
29 #include <fstream>
30 
31 
33  public:
34  DTROSChannelId( int ddu, int ros, int channel );
35  int dduId() const;
36  int rosId() const;
37  int channelId() const;
38  private:
39  int ddu_id;
40  int ros_id;
42 };
43 
44 DTROSChannelId::DTROSChannelId( int ddu, int ros, int channel ):
45  ddu_id( ddu ),
46  ros_id( ros ),
47  channel_id( channel ) {
48 }
49 
50 int DTROSChannelId::dduId() const {
51  return ddu_id;
52 }
53 
54 int DTROSChannelId::rosId() const {
55  return ros_id;
56 }
57 
59  return channel_id;
60 }
61 
63  public:
64  bool operator()( const DTROSChannelId& idl,
65  const DTROSChannelId& idr ) const;
66  private:
67 };
68 
70  const DTROSChannelId& idr ) const {
71  if ( idl. dduId() < idr. dduId() ) return true;
72  if ( idl. dduId() > idr. dduId() ) return false;
73  if ( idl. rosId() < idr. rosId() ) return true;
74  if ( idl. rosId() > idr. rosId() ) return false;
75  if ( idl.channelId() < idr.channelId() ) return true;
76  if ( idl.channelId() > idr.channelId() ) return false;
77  return false;
78 }
79 
80 
81 class DTROBCardId {
82  public:
83  DTROBCardId( int wheel, int sector, int station, int rob );
84  int wheelId() const;
85  int sectorId() const;
86  int stationId() const;
87  int robId() const;
88  private:
89  int wheel_id;
90  int sector_id;
92  int rob_id;
93 };
94 
95 DTROBCardId::DTROBCardId( int wheel, int sector, int station, int rob ):
96  wheel_id( wheel ),
97  sector_id( sector ),
98  station_id( station ),
99  rob_id( rob ) {
100 }
101 
102 int DTROBCardId::wheelId() const {
103  return wheel_id;
104 }
105 
107  return sector_id;
108 }
109 
111  return station_id;
112 }
113 
114 int DTROBCardId::robId() const {
115  return rob_id;
116 }
117 
118 
120  public:
121  bool operator()( const DTROBCardId& idl,
122  const DTROBCardId& idr ) const;
123  private:
124 };
125 
127  const DTROBCardId& idr ) const {
128  if ( idl. wheelId() < idr. wheelId() ) return true;
129  if ( idl. wheelId() > idr. wheelId() ) return false;
130  if ( idl. sectorId() < idr. sectorId() ) return true;
131  if ( idl. sectorId() > idr. sectorId() ) return false;
132  if ( idl.stationId() < idr.stationId() ) return true;
133  if ( idl.stationId() > idr.stationId() ) return false;
134  if ( idl. robId() < idr. robId() ) return true;
135  if ( idl. robId() > idr. robId() ) return false;
136  return false;
137 }
138 
139 
141  public:
142  DTTDCChannelId( int tdc, int channel );
143  int tdcId() const;
144  int channelId() const;
145  private:
146  int tdc_id;
148 };
149 
150 DTTDCChannelId::DTTDCChannelId( int tdc, int channel ):
151  tdc_id( tdc ),
152  channel_id( channel ) {
153 }
154 
156  return tdc_id;
157 }
158 
160  return channel_id;
161 }
162 
163 
165  public:
166  bool operator()( const DTTDCChannelId& idl,
167  const DTTDCChannelId& idr ) const;
168  private:
169 };
170 
172  const DTTDCChannelId& idr ) const {
173  if ( idl. tdcId() < idr. tdcId() ) return true;
174  if ( idl. tdcId() > idr. tdcId() ) return false;
175  if ( idl.channelId() < idr.channelId() ) return true;
176  if ( idl.channelId() > idr.channelId() ) return false;
177  return false;
178 }
179 
180 
182  public:
183  DTPhysicalWireId( int superlayer, int layer, int cell );
184  int superlayerId() const;
185  int layerId() const;
186  int cellId() const;
187  private:
189  int layer_id;
190  int cell_id;
191 };
192 
193 DTPhysicalWireId::DTPhysicalWireId( int superlayer, int layer, int cell ):
194  superlayer_id( superlayer ),
195  layer_id( layer ),
196  cell_id( cell ) {
197 }
198 
200  return superlayer_id;
201 }
202 
204  return layer_id;
205 }
206 
208  return cell_id;
209 }
210 
211 
213  public:
214  bool operator()( const DTPhysicalWireId& idl,
215  const DTPhysicalWireId& idr ) const;
216  private:
217 };
218 
220  const DTPhysicalWireId& idr ) const {
221  if ( idl.superlayerId() < idr.superlayerId() ) return true;
222  if ( idl.superlayerId() > idr.superlayerId() ) return false;
223  if ( idl. layerId() < idr. layerId() ) return true;
224  if ( idl. layerId() > idr. layerId() ) return false;
225  if ( idl. cellId() < idr. cellId() ) return true;
226  if ( idl. cellId() > idr. cellId() ) return false;
227  return false;
228 }
229 
230 
232  public:
233  static int defaultIdValue;
234  static int ROSMapIdOffset;
235  static int ROBMapIdOffset;
236  static int TDCMapIdOffset;
237 };
238 
243 
244 
245 void DTCompactMapWriter::buildSteering( std::istream& jobDesc ) {
246  // template maps and lists, name and type
247  std::string map_type( "dummy_string" );
248  std::string map_file( "dummy_string" );
249  std::string map_list( "dummy_string" );
250 
251  // ros-rob and tdc-cell maps
253  int,
254  DTROBCardCompare> tdc_idm;
255  std::map<DTROSChannelId,DTROBCardId,DTROSChannelCompare> ros_map;
256  std::map<DTROSChannelId,DTROBCardId,DTROSChannelCompare> rob_map;
257 
258  int rob_count = 1;
259  int ros_count = 1;
260 
261  // parse job description file and loop over map templates and lists
262  bool jobEnd = false;
263  int max_line_length = 1000;
264  char* job_line = new char[max_line_length];
265  while ( jobDesc.getline( job_line, max_line_length ) ) {
266  // parse job description line
267  char* ptr_line = job_line;
268  char* end_line = job_line + max_line_length;
269  while( ptr_line < end_line ) {
270  // get description command key
271  // remove leading blanks
272  while ( *ptr_line == ' ' ) ptr_line++;
273  std::string key( ptr_line );
274  int off_blank = key.find( " " );
275  int off_equal = key.find( "=" );
276  if ( off_equal < 0 ) off_equal = max_line_length;
277  if ( off_blank < 0 ) off_blank = key.length();
278  int ioff = ( off_blank < off_equal ? off_blank : off_equal );
279  key.erase( ioff++, key.length() );
280  // go to command value
281  ptr_line += ++off_equal;
282  // exit loop at end of description file
283  if ( (jobEnd = ( key == "end" ) ) ) break;
284  // get description command value
285  while ( *ptr_line == ' ' ) ptr_line++;
286  std::string val( ptr_line );
287  int off_value = val.find( " " );
288  if ( ( off_value > 0 ) &&
289  ( off_value < static_cast<int>( val.length() ) ) )
290  val.erase( off_value++, val.length() );
291  // go to next token
292  ptr_line += off_value;
293  // set type, template or list file name
294  if ( key == "type" ) map_type = val;
295  if ( key == "map" ) map_file = val;
296  if ( key == "list" ) map_list = val;
297  }
298  if ( jobEnd ) break;
299 
300  // open map list file
301  std::ifstream lfile( map_list.c_str() );
302 
303  bool write;
304  // store TDC maps
305  if ( map_type == "TDC" ) {
306  // loop over TDC map list for concerned wheels and sectors
307  int station = 0;
308  int wheel;
309  int sector;
310  write = true;
311  while ( lfile >> wheel
312  >> sector
313  >> station ) fillTDCMap( map_file,
314  wheel, sector,
315  tdc_idm, station,
316  rob_count, write );
317  ++rob_count;
318  }
319 
320  // store ROS maps
321  if ( map_type == "ROS" ) {
322  // loop over ROS map list for concerned ddu, ros, wheel and sector
323  int ddu;
324  int ros;
325  int wheel;
326  int sector;
327  write = true;
328  while ( lfile >> ddu
329  >> ros
330  >> wheel
331  >> sector ) fillROSMap( map_file,
332  ddu, ros,
333  wheel, sector,
334  ros_map, rob_map,
335  ros_count, write );
336  ++ros_count;
337  }
338 
339  }
340  delete[] job_line;
341 
342  // merge ROS and TDC maps
343  fillReadOutMap( ros_count, tdc_idm, ros_map, rob_map );
344 
345  return;
346 
347 }
348 
350  int wheel, int sector,
352  int,
353  DTROBCardCompare>& tdc_idm,
354  int stationId,
355  int map_count, bool& write ) {
356 
357  // template map file name
358  std::ifstream ifile( map_file.c_str() );
359 
360  // get station number
361  int station;
362  int rob;
363  station = stationId;
364  ifile >> rob;
365  DTROBCardId key( wheel, sector, station, rob );
366  tdc_idm.insert( std::pair<DTROBCardId,int>( key, map_count ) );
367  if ( !write ) return;
368  // loop over TDC channels and cells
369  int superlayer;
370  int layer;
371  int wire;
372  int tdc;
373  int channel;
374  while ( ifile >> tdc
375  >> channel
376  >> superlayer
377  >> layer
378  >> wire ) {
379  // set ROB card, TDC channel and cell identifiers
380  DTTDCChannelId tdcChannel( tdc, channel );
381  DTPhysicalWireId physicalWire( superlayer, layer, wire );
382  // look for rob card connection map
384  << map_count << " "
385  << rob << " "
386  << tdc << " "
387  << channel << " "
391  << superlayer << " "
392  << layer << " "
393  << wire
394  << std::endl;
395 
396  }
397  write = false;
398  return;
399 }
400 
402  int ddu, int ros,
403  int whdef, int secdef,
405  DTROBCardId,
406  DTROSChannelCompare>& ddu_map,
408  DTROBCardId,
409  DTROSChannelCompare>& ros_map,
410  int map_count,
411  bool& write ) {
412 
413  DTROSChannelId rosBoard( ddu, ros, 0 );
414  DTROBCardId sectorId( whdef, secdef, 0, map_count );
415  ddu_map.insert( std::pair<DTROSChannelId,DTROBCardId>( rosBoard,
416  sectorId ) );
417 
418  // template map file name
419  std::ifstream ifile( map_file.c_str() );
420 
421  // loop over ROS channels and ROBs
422  int channel;
423  int wheel;
424  int sector;
425  int station;
426  int rob;
427  DTROSChannelId rosChanMap( ddu, ros, 0 );
428  if ( !write ) return;
429  while ( ifile >> channel
430  >> wheel
431  >> sector
432  >> station
433  >> rob ) {
434  // set sector to default unless specified
435  if ( !sector ) sector = DTMapElementIdentifiers::defaultIdValue;
436  if ( ! wheel ) wheel = DTMapElementIdentifiers::defaultIdValue;
437  // set ROS channel and ROB identifiers
438  DTROSChannelId rosChannel( ddu, ros, channel );
439  DTROBCardId robCard( wheel, sector, station, rob );
440  // store ROS channel to ROB connection into map
441  ros_map.insert( std::pair<DTROSChannelId,DTROBCardId>( rosChannel,
442  robCard ) );
443  }
444  write = false;
445 
446  return;
447 }
448 
450  DTROBCardId,
451  DTROSChannelCompare>& ros_m,
453  DTROBCardId,
454  DTROSChannelCompare>& ros_a,
455  int ddu_o, int ros_o,
456  int ddu_f, int ros_f ) {
457 
459  DTROBCardId,
460  DTROSChannelCompare>:: const_iterator ros_iter = ros_m.begin();
461  std::map<DTROSChannelId,
462  DTROBCardId,
463  DTROSChannelCompare>:: const_iterator ros_iend = ros_m.end();
464  while ( ros_iter != ros_iend ) {
465  const std::pair<DTROSChannelId,DTROBCardId>& rosLink = *ros_iter++;
466  const DTROSChannelId& rosChannelId = rosLink.first;
467  const DTROBCardId& robCardId = rosLink.second;
468  if ( rosChannelId.dduId() != ddu_o ) continue;
469  if ( rosChannelId.rosId() != ros_o ) continue;
470  const DTROSChannelId rosChanNewId( ddu_f, ros_f,
471  rosChannelId.channelId() );
472  ros_a.insert( std::pair<DTROSChannelId,DTROBCardId>( rosChanNewId,
473  robCardId ) );
474  }
475  return;
476 }
477 
479  DTROBCardId,
480  DTROSChannelCompare>& ros_m,
482  DTROBCardId,
483  DTROSChannelCompare>& ros_a ) {
484 
486  DTROBCardId,
487  DTROSChannelCompare>:: const_iterator ros_iter = ros_a.begin();
488  std::map<DTROSChannelId,
489  DTROBCardId,
490  DTROSChannelCompare>:: const_iterator ros_iend = ros_a.end();
491  while ( ros_iter != ros_iend ) ros_m.insert( *ros_iter++ );
492  return;
493 }
494 
495 
497  int ros_count,
499  int,
500  DTROBCardCompare>& tdc_idm,
502  DTROBCardId,
503  DTROSChannelCompare>& ddu_map,
505  DTROBCardId,
506  DTROSChannelCompare>& ros_map ) {
507 
508  bool check = true;
509  bool write = false;
510 
511  while ( check || write ) {
512  // ROS board to sector connection map
514  DTROBCardId,
515  DTROSChannelCompare>:: const_iterator ddu_iter = ddu_map.begin();
516  std::map<DTROSChannelId,
517  DTROBCardId,
518  DTROSChannelCompare>:: const_iterator ddu_iend = ddu_map.end();
519  // ROB card to TDC element map
520  std::map<DTROBCardId,
521  int,
522  DTROBCardCompare>::const_iterator idt_iend = tdc_idm.end();
523  // ROS channel to ROB connection map
524  std::map<DTROSChannelId,
525  DTROBCardId,
526  DTROSChannelCompare>:: const_iterator ros_iter = ros_map.begin();
527  std::map<DTROSChannelId,
528  DTROBCardId,
529  DTROSChannelCompare>:: const_iterator ros_iend = ros_map.end();
530 
531  check = false;
532  std::map<DTROSChannelId,
533  DTROBCardId,
534  DTROSChannelCompare> ros_app;
535 
536  // loop over ROS channels
537  while ( ros_iter != ros_iend ) {
538  // get ROS channel and ROB identifiers
539  const std::pair<DTROSChannelId,DTROBCardId>& rosLink = *ros_iter++;
540  const DTROSChannelId& rosChannelId = rosLink.first;
541  const DTROSChannelId rosChanMapId( rosChannelId.dduId(),
542  rosChannelId.rosId(), 0 );
543  const DTROBCardId& robCMapId = rosLink.second;
544  ddu_iter = ddu_map.find( rosChanMapId );
545  int whdef = 0;
546  int secdef = 0;
547  int rosMapCk = 0;
548  if ( ddu_iter != ddu_iend ) {
549  const DTROBCardId& sector = ddu_iter->second;
550  whdef = sector.wheelId();
551  secdef = sector.sectorId();
552  rosMapCk = sector.robId();
553  }
554  else {
555  std::cout << "DDU map "
556  << rosChannelId.dduId() << " "
557  << rosChannelId.rosId() << " 0 not found" << std::endl;
558  continue;
559  }
560  int wheel = robCMapId. wheelId();
561  int sector = robCMapId. sectorId();
562  int station = robCMapId.stationId();
563  int rob = robCMapId. robId();
564  if ( wheel != DTMapElementIdentifiers::defaultIdValue ) whdef = wheel;
565  if ( sector != DTMapElementIdentifiers::defaultIdValue ) secdef = sector;
566  const DTROBCardId robCardId( whdef, secdef, station, rob );
567  std::map<DTROBCardId,
568  int,
569  DTROBCardCompare>::const_iterator idt_iter =
570  tdc_idm.find( robCardId );
571  if ( idt_iter != idt_iend ) {
572  int tdcMapId = idt_iter->second;
573  ddu_iter = ddu_map.begin();
574  while ( ddu_iter != ddu_iend ) {
575  const std::pair<DTROSChannelId,DTROBCardId>& dduLink = *ddu_iter++;
576  const DTROSChannelId& ros_id = dduLink.first;
577  const DTROBCardId& sec_id = dduLink.second;
578  int whe_chk = sec_id.wheelId();
579  int sec_chk = sec_id.sectorId();
581  whe_chk = wheel;
583  sec_chk = sector;
584  if ( rosMapCk != sec_id.robId() ) continue;
585  DTROBCardId robCardId( whe_chk, sec_chk,
586  station, rob );
587  idt_iter = tdc_idm.find( robCardId );
588  int tdcMapCk = idt_iter->second;
589  if ( ( tdcMapId != tdcMapCk ) && ( !check ) ) {
590  cloneROS( ros_map, ros_app,
591  rosChannelId.dduId(), rosChannelId.rosId(),
592  ros_id. dduId(), ros_id. rosId() );
593  tdcMapId = tdcMapCk;
594  check = true;
595  }
596  if ( ( tdcMapId == tdcMapCk ) && check ) {
597  std::map<DTROSChannelId,
598  DTROBCardId,
599  DTROSChannelCompare>::iterator ddu_inew =
600  ddu_map.find( ros_id );
601  ddu_inew->second = DTROBCardId( sec_id.wheelId(),
602  sec_id.sectorId(),
603  sec_id.stationId(),
604  ros_count );
605  }
606  }
607  if ( check ) {
608  ros_count++;
609  break;
610  }
611  if ( write )
613  << rosMapCk << " "
614  << rosChannelId.channelId() << " "
617  << wheel << " "
618  << station << " "
619  << sector << " "
620  << rob << " "
622  << tdcMapId << std::endl;
623 
624  }
625  else {
626  std::cout << "TDC map "
627  << wheel << " "
628  << sector << " "
629  << station << " "
630  << rob << " not found" << std::endl;
631  }
632  }
633  appendROS( ros_map, ros_app );
634  if ( !write ) {
635  write = !check;
636  }
637  else {
638  ddu_iter = ddu_map.begin();
639  while ( ddu_iter != ddu_iend ) {
640  const std::pair<DTROSChannelId,DTROBCardId>& dduLink = *ddu_iter++;
641  const DTROSChannelId& ros_id = dduLink.first;
642  const DTROBCardId& sec_id = dduLink.second;
643  std::cout << ros_id.dduId() << " "
644  << ros_id.rosId() << " "
648  << sec_id.wheelId() << " "
650  << sec_id.sectorId() << " "
653  << sec_id.robId() << std::endl;
654  }
655  write = false;
656  }
657  }
658 
659  return;
660 }
661 
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)
bool check(const std::string &)
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
list key
Definition: combine.py:13
tuple cout
Definition: gather_cfg.py:121
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)