00001
00011
00012
00013
00014 #include "CondTools/DT/interface/DTCompactMapWriter.h"
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <string>
00025 #include <vector>
00026 #include <map>
00027 #include <algorithm>
00028 #include <iostream>
00029 #include <fstream>
00030
00031
00032 class DTROSChannelId {
00033 public:
00034 DTROSChannelId( int ddu, int ros, int channel );
00035 int dduId() const;
00036 int rosId() const;
00037 int channelId() const;
00038 private:
00039 int ddu_id;
00040 int ros_id;
00041 int channel_id;
00042 };
00043
00044 DTROSChannelId::DTROSChannelId( int ddu, int ros, int channel ):
00045 ddu_id( ddu ),
00046 ros_id( ros ),
00047 channel_id( channel ) {
00048 }
00049
00050 int DTROSChannelId::dduId() const {
00051 return ddu_id;
00052 }
00053
00054 int DTROSChannelId::rosId() const {
00055 return ros_id;
00056 }
00057
00058 int DTROSChannelId::channelId() const {
00059 return channel_id;
00060 }
00061
00062 class DTROSChannelCompare {
00063 public:
00064 bool operator()( const DTROSChannelId& idl,
00065 const DTROSChannelId& idr ) const;
00066 private:
00067 };
00068
00069 bool DTROSChannelCompare::operator()( const DTROSChannelId& idl,
00070 const DTROSChannelId& idr ) const {
00071 if ( idl. dduId() < idr. dduId() ) return true;
00072 if ( idl. dduId() > idr. dduId() ) return false;
00073 if ( idl. rosId() < idr. rosId() ) return true;
00074 if ( idl. rosId() > idr. rosId() ) return false;
00075 if ( idl.channelId() < idr.channelId() ) return true;
00076 if ( idl.channelId() > idr.channelId() ) return false;
00077 return false;
00078 }
00079
00080
00081 class DTROBCardId {
00082 public:
00083 DTROBCardId( int wheel, int sector, int station, int rob );
00084 int wheelId() const;
00085 int sectorId() const;
00086 int stationId() const;
00087 int robId() const;
00088 private:
00089 int wheel_id;
00090 int sector_id;
00091 int station_id;
00092 int rob_id;
00093 };
00094
00095 DTROBCardId::DTROBCardId( int wheel, int sector, int station, int rob ):
00096 wheel_id( wheel ),
00097 sector_id( sector ),
00098 station_id( station ),
00099 rob_id( rob ) {
00100 }
00101
00102 int DTROBCardId::wheelId() const {
00103 return wheel_id;
00104 }
00105
00106 int DTROBCardId::sectorId() const {
00107 return sector_id;
00108 }
00109
00110 int DTROBCardId::stationId() const {
00111 return station_id;
00112 }
00113
00114 int DTROBCardId::robId() const {
00115 return rob_id;
00116 }
00117
00118
00119 class DTROBCardCompare {
00120 public:
00121 bool operator()( const DTROBCardId& idl,
00122 const DTROBCardId& idr ) const;
00123 private:
00124 };
00125
00126 bool DTROBCardCompare::operator()( const DTROBCardId& idl,
00127 const DTROBCardId& idr ) const {
00128 if ( idl. wheelId() < idr. wheelId() ) return true;
00129 if ( idl. wheelId() > idr. wheelId() ) return false;
00130 if ( idl. sectorId() < idr. sectorId() ) return true;
00131 if ( idl. sectorId() > idr. sectorId() ) return false;
00132 if ( idl.stationId() < idr.stationId() ) return true;
00133 if ( idl.stationId() > idr.stationId() ) return false;
00134 if ( idl. robId() < idr. robId() ) return true;
00135 if ( idl. robId() > idr. robId() ) return false;
00136 return false;
00137 }
00138
00139
00140 class DTTDCChannelId {
00141 public:
00142 DTTDCChannelId( int tdc, int channel );
00143 int tdcId() const;
00144 int channelId() const;
00145 private:
00146 int tdc_id;
00147 int channel_id;
00148 };
00149
00150 DTTDCChannelId::DTTDCChannelId( int tdc, int channel ):
00151 tdc_id( tdc ),
00152 channel_id( channel ) {
00153 }
00154
00155 int DTTDCChannelId::tdcId() const {
00156 return tdc_id;
00157 }
00158
00159 int DTTDCChannelId::channelId() const {
00160 return channel_id;
00161 }
00162
00163
00164 class DTTDCChannelCompare {
00165 public:
00166 bool operator()( const DTTDCChannelId& idl,
00167 const DTTDCChannelId& idr ) const;
00168 private:
00169 };
00170
00171 bool DTTDCChannelCompare::operator()( const DTTDCChannelId& idl,
00172 const DTTDCChannelId& idr ) const {
00173 if ( idl. tdcId() < idr. tdcId() ) return true;
00174 if ( idl. tdcId() > idr. tdcId() ) return false;
00175 if ( idl.channelId() < idr.channelId() ) return true;
00176 if ( idl.channelId() > idr.channelId() ) return false;
00177 return false;
00178 }
00179
00180
00181 class DTPhysicalWireId {
00182 public:
00183 DTPhysicalWireId( int superlayer, int layer, int cell );
00184 int superlayerId() const;
00185 int layerId() const;
00186 int cellId() const;
00187 private:
00188 int superlayer_id;
00189 int layer_id;
00190 int cell_id;
00191 };
00192
00193 DTPhysicalWireId::DTPhysicalWireId( int superlayer, int layer, int cell ):
00194 superlayer_id( superlayer ),
00195 layer_id( layer ),
00196 cell_id( cell ) {
00197 }
00198
00199 int DTPhysicalWireId::superlayerId() const {
00200 return superlayer_id;
00201 }
00202
00203 int DTPhysicalWireId::layerId() const {
00204 return layer_id;
00205 }
00206
00207 int DTPhysicalWireId::cellId() const {
00208 return cell_id;
00209 }
00210
00211
00212 class DTPhysicalWireCompare {
00213 public:
00214 bool operator()( const DTPhysicalWireId& idl,
00215 const DTPhysicalWireId& idr ) const;
00216 private:
00217 };
00218
00219 bool DTPhysicalWireCompare::operator()( const DTPhysicalWireId& idl,
00220 const DTPhysicalWireId& idr ) const {
00221 if ( idl.superlayerId() < idr.superlayerId() ) return true;
00222 if ( idl.superlayerId() > idr.superlayerId() ) return false;
00223 if ( idl. layerId() < idr. layerId() ) return true;
00224 if ( idl. layerId() > idr. layerId() ) return false;
00225 if ( idl. cellId() < idr. cellId() ) return true;
00226 if ( idl. cellId() > idr. cellId() ) return false;
00227 return false;
00228 }
00229
00230
00231 class DTMapElementIdentifiers {
00232 public:
00233 static int defaultIdValue;
00234 static int ROSMapIdOffset;
00235 static int ROBMapIdOffset;
00236 static int TDCMapIdOffset;
00237 };
00238
00239 int DTMapElementIdentifiers::defaultIdValue = 0x7fffffff;
00240 int DTMapElementIdentifiers::ROSMapIdOffset = 2000001000;
00241 int DTMapElementIdentifiers::ROBMapIdOffset = 2000002000;
00242 int DTMapElementIdentifiers::TDCMapIdOffset = 2000003000;
00243
00244
00245 void DTCompactMapWriter::buildSteering( std::istream& jobDesc ) {
00246
00247 std::string map_type( "dummy_string" );
00248 std::string map_file( "dummy_string" );
00249 std::string map_list( "dummy_string" );
00250
00251
00252 std::map<DTROBCardId,
00253 int,
00254 DTROBCardCompare> tdc_idm;
00255 std::map<DTROSChannelId,DTROBCardId,DTROSChannelCompare> ros_map;
00256 std::map<DTROSChannelId,DTROBCardId,DTROSChannelCompare> rob_map;
00257
00258 int rob_count = 1;
00259 int ros_count = 1;
00260
00261
00262 bool jobEnd = false;
00263 int max_line_length = 1000;
00264 char* job_line = new char[max_line_length];
00265 while ( jobDesc.getline( job_line, max_line_length ) ) {
00266
00267 char* ptr_line = job_line;
00268 char* end_line = job_line + max_line_length;
00269 while( ptr_line < end_line ) {
00270
00271
00272 while ( *ptr_line == ' ' ) ptr_line++;
00273 std::string key( ptr_line );
00274 int off_blank = key.find( " " );
00275 int off_equal = key.find( "=" );
00276 if ( off_equal < 0 ) off_equal = max_line_length;
00277 if ( off_blank < 0 ) off_blank = key.length();
00278 int ioff = ( off_blank < off_equal ? off_blank : off_equal );
00279 key.erase( ioff++, key.length() );
00280
00281 ptr_line += ++off_equal;
00282
00283 if ( (jobEnd = ( key == "end" ) ) ) break;
00284
00285 while ( *ptr_line == ' ' ) ptr_line++;
00286 std::string val( ptr_line );
00287 int off_value = val.find( " " );
00288 if ( ( off_value > 0 ) &&
00289 ( off_value < static_cast<int>( val.length() ) ) )
00290 val.erase( off_value++, val.length() );
00291
00292 ptr_line += off_value;
00293
00294 if ( key == "type" ) map_type = val;
00295 if ( key == "map" ) map_file = val;
00296 if ( key == "list" ) map_list = val;
00297 }
00298 if ( jobEnd ) break;
00299
00300
00301 std::ifstream lfile( map_list.c_str() );
00302
00303 bool write;
00304
00305 if ( map_type == "TDC" ) {
00306
00307 int station = 0;
00308 int wheel;
00309 int sector;
00310 write = true;
00311 while ( lfile >> wheel
00312 >> sector
00313 >> station ) fillTDCMap( map_file,
00314 wheel, sector,
00315 tdc_idm, station,
00316 rob_count, write );
00317 ++rob_count;
00318 }
00319
00320
00321 if ( map_type == "ROS" ) {
00322
00323 int ddu;
00324 int ros;
00325 int wheel;
00326 int sector;
00327 write = true;
00328 while ( lfile >> ddu
00329 >> ros
00330 >> wheel
00331 >> sector ) fillROSMap( map_file,
00332 ddu, ros,
00333 wheel, sector,
00334 ros_map, rob_map,
00335 ros_count, write );
00336 ++ros_count;
00337 }
00338
00339 }
00340 delete job_line;
00341
00342
00343 fillReadOutMap( ros_count, tdc_idm, ros_map, rob_map );
00344
00345 return;
00346
00347 }
00348
00349 void DTCompactMapWriter::fillTDCMap( const std::string& map_file,
00350 int wheel, int sector,
00351 std::map<DTROBCardId,
00352 int,
00353 DTROBCardCompare>& tdc_idm,
00354 int stationId,
00355 int map_count, bool& write ) {
00356
00357
00358 std::ifstream ifile( map_file.c_str() );
00359
00360
00361 int station;
00362 int rob;
00363 station = stationId;
00364 ifile >> rob;
00365 DTROBCardId key( wheel, sector, station, rob );
00366 tdc_idm.insert( std::pair<DTROBCardId,int>( key, map_count ) );
00367 if ( !write ) return;
00368
00369 int superlayer;
00370 int layer;
00371 int wire;
00372 int tdc;
00373 int channel;
00374 while ( ifile >> tdc
00375 >> channel
00376 >> superlayer
00377 >> layer
00378 >> wire ) {
00379
00380 DTTDCChannelId tdcChannel( tdc, channel );
00381 DTPhysicalWireId physicalWire( superlayer, layer, wire );
00382
00383 std::cout << DTMapElementIdentifiers::TDCMapIdOffset << " "
00384 << map_count << " "
00385 << rob << " "
00386 << tdc << " "
00387 << channel << " "
00388 << DTMapElementIdentifiers::defaultIdValue << " "
00389 << DTMapElementIdentifiers::defaultIdValue << " "
00390 << DTMapElementIdentifiers::defaultIdValue << " "
00391 << superlayer << " "
00392 << layer << " "
00393 << wire
00394 << std::endl;
00395
00396 }
00397 write = false;
00398 return;
00399 }
00400
00401 void DTCompactMapWriter::fillROSMap( const std::string& map_file,
00402 int ddu, int ros,
00403 int whdef, int secdef,
00404 std::map<DTROSChannelId,
00405 DTROBCardId,
00406 DTROSChannelCompare>& ddu_map,
00407 std::map<DTROSChannelId,
00408 DTROBCardId,
00409 DTROSChannelCompare>& ros_map,
00410 int map_count,
00411 bool& write ) {
00412
00413 DTROSChannelId rosBoard( ddu, ros, 0 );
00414 DTROBCardId sectorId( whdef, secdef, 0, map_count );
00415 ddu_map.insert( std::pair<DTROSChannelId,DTROBCardId>( rosBoard,
00416 sectorId ) );
00417
00418
00419 std::ifstream ifile( map_file.c_str() );
00420
00421
00422 int channel;
00423 int wheel;
00424 int sector;
00425 int station;
00426 int rob;
00427 DTROSChannelId rosChanMap( ddu, ros, 0 );
00428 if ( !write ) return;
00429 while ( ifile >> channel
00430 >> wheel
00431 >> sector
00432 >> station
00433 >> rob ) {
00434
00435 if ( !sector ) sector = DTMapElementIdentifiers::defaultIdValue;
00436 if ( ! wheel ) wheel = DTMapElementIdentifiers::defaultIdValue;
00437
00438 DTROSChannelId rosChannel( ddu, ros, channel );
00439 DTROBCardId robCard( wheel, sector, station, rob );
00440
00441 ros_map.insert( std::pair<DTROSChannelId,DTROBCardId>( rosChannel,
00442 robCard ) );
00443 }
00444 write = false;
00445
00446 return;
00447 }
00448
00449 void DTCompactMapWriter::cloneROS( std::map<DTROSChannelId,
00450 DTROBCardId,
00451 DTROSChannelCompare>& ros_m,
00452 std::map<DTROSChannelId,
00453 DTROBCardId,
00454 DTROSChannelCompare>& ros_a,
00455 int ddu_o, int ros_o,
00456 int ddu_f, int ros_f ) {
00457
00458 std::map<DTROSChannelId,
00459 DTROBCardId,
00460 DTROSChannelCompare>:: const_iterator ros_iter = ros_m.begin();
00461 std::map<DTROSChannelId,
00462 DTROBCardId,
00463 DTROSChannelCompare>:: const_iterator ros_iend = ros_m.end();
00464 while ( ros_iter != ros_iend ) {
00465 const std::pair<DTROSChannelId,DTROBCardId>& rosLink = *ros_iter++;
00466 const DTROSChannelId& rosChannelId = rosLink.first;
00467 const DTROBCardId& robCardId = rosLink.second;
00468 if ( rosChannelId.dduId() != ddu_o ) continue;
00469 if ( rosChannelId.rosId() != ros_o ) continue;
00470 const DTROSChannelId rosChanNewId( ddu_f, ros_f,
00471 rosChannelId.channelId() );
00472 ros_a.insert( std::pair<DTROSChannelId,DTROBCardId>( rosChanNewId,
00473 robCardId ) );
00474 }
00475 return;
00476 }
00477
00478 void DTCompactMapWriter::appendROS( std::map<DTROSChannelId,
00479 DTROBCardId,
00480 DTROSChannelCompare>& ros_m,
00481 std::map<DTROSChannelId,
00482 DTROBCardId,
00483 DTROSChannelCompare>& ros_a ) {
00484
00485 std::map<DTROSChannelId,
00486 DTROBCardId,
00487 DTROSChannelCompare>:: const_iterator ros_iter = ros_a.begin();
00488 std::map<DTROSChannelId,
00489 DTROBCardId,
00490 DTROSChannelCompare>:: const_iterator ros_iend = ros_a.end();
00491 while ( ros_iter != ros_iend ) ros_m.insert( *ros_iter++ );
00492 return;
00493 }
00494
00495
00496 void DTCompactMapWriter::fillReadOutMap(
00497 int ros_count,
00498 std::map<DTROBCardId,
00499 int,
00500 DTROBCardCompare>& tdc_idm,
00501 std::map<DTROSChannelId,
00502 DTROBCardId,
00503 DTROSChannelCompare>& ddu_map,
00504 std::map<DTROSChannelId,
00505 DTROBCardId,
00506 DTROSChannelCompare>& ros_map ) {
00507
00508 bool check = true;
00509 bool write = false;
00510
00511 while ( check || write ) {
00512
00513 std::map<DTROSChannelId,
00514 DTROBCardId,
00515 DTROSChannelCompare>:: const_iterator ddu_iter = ddu_map.begin();
00516 std::map<DTROSChannelId,
00517 DTROBCardId,
00518 DTROSChannelCompare>:: const_iterator ddu_iend = ddu_map.end();
00519
00520 std::map<DTROBCardId,
00521 int,
00522 DTROBCardCompare>::const_iterator idt_iend = tdc_idm.end();
00523
00524 std::map<DTROSChannelId,
00525 DTROBCardId,
00526 DTROSChannelCompare>:: const_iterator ros_iter = ros_map.begin();
00527 std::map<DTROSChannelId,
00528 DTROBCardId,
00529 DTROSChannelCompare>:: const_iterator ros_iend = ros_map.end();
00530
00531 check = false;
00532 std::map<DTROSChannelId,
00533 DTROBCardId,
00534 DTROSChannelCompare> ros_app;
00535
00536
00537 while ( ros_iter != ros_iend ) {
00538
00539 const std::pair<DTROSChannelId,DTROBCardId>& rosLink = *ros_iter++;
00540 const DTROSChannelId& rosChannelId = rosLink.first;
00541 const DTROSChannelId rosChanMapId( rosChannelId.dduId(),
00542 rosChannelId.rosId(), 0 );
00543 const DTROBCardId& robCMapId = rosLink.second;
00544 ddu_iter = ddu_map.find( rosChanMapId );
00545 int whdef = 0;
00546 int secdef = 0;
00547 int rosMapCk = 0;
00548 if ( ddu_iter != ddu_iend ) {
00549 const DTROBCardId& sector = ddu_iter->second;
00550 whdef = sector.wheelId();
00551 secdef = sector.sectorId();
00552 rosMapCk = sector.robId();
00553 }
00554 else {
00555 std::cout << "DDU map "
00556 << rosChannelId.dduId() << " "
00557 << rosChannelId.rosId() << " 0 not found" << std::endl;
00558 continue;
00559 }
00560 int wheel = robCMapId. wheelId();
00561 int sector = robCMapId. sectorId();
00562 int station = robCMapId.stationId();
00563 int rob = robCMapId. robId();
00564 if ( wheel != DTMapElementIdentifiers::defaultIdValue ) whdef = wheel;
00565 if ( sector != DTMapElementIdentifiers::defaultIdValue ) secdef = sector;
00566 const DTROBCardId robCardId( whdef, secdef, station, rob );
00567 std::map<DTROBCardId,
00568 int,
00569 DTROBCardCompare>::const_iterator idt_iter =
00570 tdc_idm.find( robCardId );
00571 if ( idt_iter != idt_iend ) {
00572 int tdcMapId = idt_iter->second;
00573 ddu_iter = ddu_map.begin();
00574 while ( ddu_iter != ddu_iend ) {
00575 const std::pair<DTROSChannelId,DTROBCardId>& dduLink = *ddu_iter++;
00576 const DTROSChannelId& ros_id = dduLink.first;
00577 const DTROBCardId& sec_id = dduLink.second;
00578 int whe_chk = sec_id.wheelId();
00579 int sec_chk = sec_id.sectorId();
00580 if ( wheel != DTMapElementIdentifiers::defaultIdValue )
00581 whe_chk = wheel;
00582 if ( sector != DTMapElementIdentifiers::defaultIdValue )
00583 sec_chk = sector;
00584 if ( rosMapCk != sec_id.robId() ) continue;
00585 DTROBCardId robCardId( whe_chk, sec_chk,
00586 station, rob );
00587 idt_iter = tdc_idm.find( robCardId );
00588 int tdcMapCk = idt_iter->second;
00589 if ( ( tdcMapId != tdcMapCk ) && ( !check ) ) {
00590 cloneROS( ros_map, ros_app,
00591 rosChannelId.dduId(), rosChannelId.rosId(),
00592 ros_id. dduId(), ros_id. rosId() );
00593 tdcMapId = tdcMapCk;
00594 check = true;
00595 }
00596 if ( ( tdcMapId == tdcMapCk ) && check ) {
00597 std::map<DTROSChannelId,
00598 DTROBCardId,
00599 DTROSChannelCompare>::iterator ddu_inew =
00600 ddu_map.find( ros_id );
00601 ddu_inew->second = DTROBCardId( sec_id.wheelId(),
00602 sec_id.sectorId(),
00603 sec_id.stationId(),
00604 ros_count );
00605 }
00606 }
00607 if ( check ) {
00608 ros_count++;
00609 break;
00610 }
00611 if ( write )
00612 std::cout << DTMapElementIdentifiers::ROSMapIdOffset << " "
00613 << rosMapCk << " "
00614 << rosChannelId.channelId() << " "
00615 << DTMapElementIdentifiers::defaultIdValue << " "
00616 << DTMapElementIdentifiers::defaultIdValue << " "
00617 << wheel << " "
00618 << station << " "
00619 << sector << " "
00620 << rob << " "
00621 << DTMapElementIdentifiers::TDCMapIdOffset << " "
00622 << tdcMapId << std::endl;
00623
00624 }
00625 else {
00626 std::cout << "TDC map "
00627 << wheel << " "
00628 << sector << " "
00629 << station << " "
00630 << rob << " not found" << std::endl;
00631 }
00632 }
00633 appendROS( ros_map, ros_app );
00634 if ( !write ) {
00635 write = !check;
00636 }
00637 else {
00638 ddu_iter = ddu_map.begin();
00639 while ( ddu_iter != ddu_iend ) {
00640 const std::pair<DTROSChannelId,DTROBCardId>& dduLink = *ddu_iter++;
00641 const DTROSChannelId& ros_id = dduLink.first;
00642 const DTROBCardId& sec_id = dduLink.second;
00643 std::cout << ros_id.dduId() << " "
00644 << ros_id.rosId() << " "
00645 << DTMapElementIdentifiers::defaultIdValue << " "
00646 << DTMapElementIdentifiers::defaultIdValue << " "
00647 << DTMapElementIdentifiers::defaultIdValue << " "
00648 << sec_id.wheelId() << " "
00649 << DTMapElementIdentifiers::defaultIdValue << " "
00650 << sec_id.sectorId() << " "
00651 << DTMapElementIdentifiers::defaultIdValue << " "
00652 << DTMapElementIdentifiers::ROSMapIdOffset << " "
00653 << sec_id.robId() << std::endl;
00654 }
00655 write = false;
00656 }
00657 }
00658
00659 return;
00660 }
00661