00001
00002
00003 #include "OnlineDB/SiStripESSources/interface/SiStripFedCablingBuilderFromDb.h"
00004 #include "CalibFormats/SiStripObjects/interface/SiStripFecCabling.h"
00005 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00006 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
00007 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00008 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 #include "FWCore/ServiceRegistry/interface/Service.h"
00011 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00012 #include <cstdlib>
00013 #include <iostream>
00014 #include <sstream>
00015 #include <iomanip>
00016
00017 using namespace std;
00018 using namespace sistrip;
00019
00020
00022 SiStripFedCablingBuilderFromDb::SiStripFedCablingBuilderFromDb( const edm::ParameterSet& pset )
00023 : SiStripFedCablingESProducer( pset ),
00024 db_(0),
00025 source_(sistrip::UNDEFINED_CABLING_SOURCE)
00026 {
00027 findingRecord<SiStripFedCablingRcd>();
00028
00029 LogTrace(mlCabling_)
00030 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00031 << " Constructing object...";
00032
00033
00034 string source = pset.getUntrackedParameter<string>( "CablingSource", "UNDEFINED" );
00035 source_ = SiStripEnumsAndStrings::cablingSource( source );
00036
00037 LogTrace(mlCabling_)
00038 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00039 << " CablingSource configurable set to \"" << source << "\""
00040 << ". CablingSource member data set to: \""
00041 << SiStripEnumsAndStrings::cablingSource( source_ ) << "\"";
00042 }
00043
00044
00046 SiStripFedCablingBuilderFromDb::~SiStripFedCablingBuilderFromDb() {
00047 LogTrace(mlCabling_)
00048 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00049 << " Destructing object...";
00050 }
00051
00052
00054 SiStripFedCabling* SiStripFedCablingBuilderFromDb::make( const SiStripFedCablingRcd& ) {
00055 LogTrace(mlCabling_)
00056 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00057 << " Constructing FED cabling...";
00058
00059
00060 SiStripFedCabling* fed_cabling = new SiStripFedCabling();
00061
00062
00063 db_ = edm::Service<SiStripConfigDb>().operator->();
00064
00065
00066 if ( db_ ) {
00067 edm::LogVerbatim(mlCabling_)
00068 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00069 << " Pointer to SiStripConfigDb: 0x"
00070 << std::setw(8) << std::setfill('0')
00071 << std::hex << db_ << std::dec;
00072 } else {
00073 edm::LogError(mlCabling_)
00074 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00075 << " NULL pointer to SiStripConfigDb returned by DB \"service\"!"
00076 << " Cannot build FED cabling object!";
00077 return fed_cabling;
00078 }
00079
00080
00081 if ( db_->deviceFactory() ||
00082 db_->databaseCache() ) {
00083
00084
00085 SiStripFecCabling fec_cabling;
00086 buildFecCabling( db_, fec_cabling, source_ );
00087
00088
00089 getFedCabling( fec_cabling, *fed_cabling );
00090
00091
00092 writeFedCablingToCondDb( *fed_cabling );
00093
00094
00095
00096
00097
00098
00099
00100
00101 } else {
00102 edm::LogError(mlCabling_)
00103 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00104 << " NULL pointers to DeviceFactory and DatabaseCache returned by SiStripConfigDb!"
00105 << " Cannot build FED cabling object!";
00106 }
00107
00108 return fed_cabling;
00109
00110 }
00111
00112
00114 void SiStripFedCablingBuilderFromDb::buildFecCabling( SiStripConfigDb* const db,
00115 SiStripFecCabling& fec_cabling,
00116 const sistrip::CablingSource& source ) {
00117
00118 if ( source == sistrip::CABLING_FROM_CONNS ) { buildFecCablingFromFedConnections( db, fec_cabling ); }
00119 else if ( source == sistrip::CABLING_FROM_DEVICES ) { buildFecCablingFromDevices( db, fec_cabling ); }
00120 else if ( source == sistrip::CABLING_FROM_DETIDS ) { buildFecCablingFromDetIds( db, fec_cabling ); }
00121 else if ( source == sistrip::UNDEFINED_CABLING_SOURCE ) {
00122
00123 LogTrace(mlCabling_)
00124 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00125 << " Unexpected value for CablingSource: \""
00126 << SiStripEnumsAndStrings::cablingSource( source )
00127 << "\" Querying DB in order to build cabling from one of connections, devices or DetIds...";
00128 buildFecCabling( db, fec_cabling );
00129 return;
00130
00131 } else {
00132
00133 edm::LogError(mlCabling_)
00134 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00135 << " Cannot build SiStripFecCabling object!"
00136 << " sistrip::CablingSource has value: "
00137 << SiStripEnumsAndStrings::cablingSource( source );
00138 return;
00139
00140 }
00141
00142
00143 const NumberOfDevices& devs = fec_cabling.countDevices();
00144 std::stringstream ss;
00145 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00146 << " Built SiStripFecCabling object with following devices:"
00147 << endl << devs;
00148 edm::LogVerbatim(mlCabling_) << ss.str() << endl;
00149
00150 }
00151
00152
00154 void SiStripFedCablingBuilderFromDb::buildFecCabling( SiStripConfigDb* const db,
00155 SiStripFecCabling& fec_cabling ) {
00156 LogTrace(mlCabling_)
00157 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00158 << " Building cabling object...";
00159
00160 if ( !db->getFedConnections().empty() ) { buildFecCablingFromFedConnections( db, fec_cabling ); }
00161 else if ( !db->getDeviceDescriptions().empty() ) { buildFecCablingFromDevices( db, fec_cabling ); }
00162 else if ( !db->getDcuDetIds().empty() ) { buildFecCablingFromDetIds( db, fec_cabling ); }
00163 else {
00164
00165 edm::LogError(mlCabling_)
00166 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00167 << " Cannot build SiStripFecCabling object!"
00168 << " FedConnections, DeviceDescriptions and DcuDetIds vectors are all empty!";
00169 return;
00170
00171 }
00172
00173
00174 const NumberOfDevices& devices = fec_cabling.countDevices();
00175 std::stringstream ss;
00176 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00177 << " Built SiStripFecCabling object with following devices:"
00178 << std::endl << devices;
00179 edm::LogVerbatim(mlCabling_) << ss.str() << endl;
00180
00181 }
00182
00183
00219 void SiStripFedCablingBuilderFromDb::buildFecCablingFromFedConnections( SiStripConfigDb* const db,
00220 SiStripFecCabling& fec_cabling ) {
00221 edm::LogVerbatim(mlCabling_)
00222 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00223 << " Building FEC cabling from FED connections descriptions...";
00224
00225
00226
00227
00228
00229
00230
00231 SiStripConfigDb::FedConnectionsRange conns = db->getFedConnections();
00232 if ( conns.empty() ) {
00233 edm::LogError(mlCabling_)
00234 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00235 << " Unable to build FEC cabling!"
00236 << " No entries in FedConnections vector!";
00237 return;
00238 }
00239
00240
00241
00242 LogTrace(mlCabling_)
00243 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00244 << " Retrieving DCU-DetId vector from database...";
00245 SiStripConfigDb::DcuDetIdsRange range = db->getDcuDetIds();
00246 const SiStripConfigDb::DcuDetIdsV dcu_detid_vector( range.begin(), range.end() );
00247 if ( !dcu_detid_vector.empty() ) {
00248 LogTrace(mlCabling_)
00249 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00250 << " Found " << dcu_detid_vector.size()
00251 << " entries in DCU-DetId vector retrieved from database!";
00252 } else {
00253 edm::LogWarning(mlCabling_)
00254 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00255 << " No entries in DCU-DetId vector retrieved from database!";
00256 }
00257
00258
00259
00260 SiStripConfigDb::FedConnectionsV::const_iterator ifed = conns.begin();
00261 SiStripConfigDb::FedConnectionsV::const_iterator jfed = conns.end();
00262 for ( ; ifed != jfed; ++ifed ) {
00263
00264 if ( !(*ifed) ) {
00265 edm::LogWarning(mlCabling_)
00266 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00267 << " NULL pointer to FedConnection!";
00268 continue;
00269 }
00270
00271
00272 uint16_t fec_crate = static_cast<uint16_t>( (*ifed)->getFecCrateId() + sistrip::FEC_CRATE_OFFSET );
00273 uint16_t fec_slot = static_cast<uint16_t>( (*ifed)->getFecSlot() );
00274 uint16_t fec_ring = static_cast<uint16_t>( (*ifed)->getRingSlot() + sistrip::FEC_RING_OFFSET );
00275 uint16_t ccu_addr = static_cast<uint16_t>( (*ifed)->getCcuAddress() );
00276 uint16_t ccu_chan = static_cast<uint16_t>( (*ifed)->getI2cChannel() );
00277 uint16_t apv0 = static_cast<uint16_t>( (*ifed)->getApvAddress() );
00278 uint16_t apv1 = apv0 + 1;
00279 uint32_t dcu_id = static_cast<uint32_t>( (*ifed)->getDcuHardId() );
00280 uint32_t det_id = 0;
00281 uint16_t npairs = 0;
00282 uint16_t fed_id = static_cast<uint16_t>( (*ifed)->getFedId() );
00283 uint16_t fed_ch = static_cast<uint16_t>( (*ifed)->getFedChannel() );
00284 uint16_t length = 0;
00285
00286 FedChannelConnection conn( fec_crate, fec_slot, fec_ring, ccu_addr, ccu_chan,
00287 apv0, apv1,
00288 dcu_id, det_id, npairs,
00289 fed_id, fed_ch,
00290 length );
00291
00292 uint16_t fed_crate = sistrip::invalid_;
00293 uint16_t fed_slot = sistrip::invalid_;
00294 fed_crate = static_cast<uint16_t>( (*ifed)->getFedCrateId() );
00295 fed_slot = static_cast<uint16_t>( (*ifed)->getFedSlot() );
00296 conn.fedCrate( fed_crate );
00297 conn.fedSlot( fed_slot );
00298
00299 fec_cabling.addDevices( conn );
00300
00301 }
00302
00303
00304
00305 assignDcuAndDetIds( fec_cabling, dcu_detid_vector );
00306
00307 }
00308
00309
00352 void SiStripFedCablingBuilderFromDb::buildFecCablingFromDevices( SiStripConfigDb* const db,
00353 SiStripFecCabling& fec_cabling ) {
00354 edm::LogVerbatim(mlCabling_)
00355 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00356 << " Building FEC cabling object from device descriptions...";
00357
00358
00359
00360
00361
00362
00363
00364 LogTrace(mlCabling_)
00365 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00366 << " Retrieving APV descriptions from database...";
00367 SiStripConfigDb::DeviceDescriptionsRange apv_desc = db->getDeviceDescriptions( APV25 );
00368 if ( !apv_desc.empty() ) {
00369 LogTrace(mlCabling_)
00370 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00371 << " Retrieved " << apv_desc.size()
00372 << " APV descriptions from database!";
00373 } else {
00374 edm::LogError(mlCabling_)
00375 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00376 << " Unable to build FEC cabling!"
00377 << " No APV descriptions found!";
00378 return;
00379 }
00380
00381
00382
00383 LogTrace(mlCabling_)
00384 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00385 << " Retrieving DCU descriptions from database...";
00386 SiStripConfigDb::DeviceDescriptionsRange dcu_desc = db->getDeviceDescriptions( DCU );
00387
00388 if ( !dcu_desc.empty() ) {
00389
00390 uint16_t feh = 0;
00391 uint16_t ccu = 0;
00392 SiStripConfigDb::DeviceDescriptionsV::const_iterator idcu;
00393 for ( idcu = dcu_desc.begin(); idcu != dcu_desc.end(); idcu++ ) {
00394 dcuDescription* dcu = dynamic_cast<dcuDescription*>( *idcu );
00395 if ( !dcu ) { continue; }
00396 if ( dcu->getDcuType() == "FEH" ) { feh++; }
00397 else { ccu++; }
00398 }
00399
00400 LogTrace(mlCabling_)
00401 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00402 << " Retrieved " << feh
00403 << " DCU-FEH descriptions from database!"
00404 << " (and a further " << ccu << " DCUs for CCU modules, etc...)";
00405
00406 } else {
00407 edm::LogWarning(mlCabling_)
00408 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00409 << " No DCU descriptions found!";
00410 }
00411
00412
00413
00414
00415 LogTrace(mlCabling_)
00416 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00417 << " Retrieving DCU-DetId vector from database...";
00418 SiStripConfigDb::DcuDetIdsRange range = db->getDcuDetIds();
00419 const SiStripConfigDb::DcuDetIdsV dcu_detid_vector( range.begin(), range.end() );
00420 if ( !dcu_detid_vector.empty() ) {
00421 LogTrace(mlCabling_)
00422 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00423 << " Found " << dcu_detid_vector.size()
00424 << " entries in DCU-DetId vector retrieved from database!";
00425 } else {
00426 edm::LogWarning(mlCabling_)
00427 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00428 << " No entries in DCU-DetId vector retrieved from database!";
00429 }
00430
00431
00432
00433 LogTrace(mlCabling_)
00434 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00435 << " Retrieving FED ids from database...";
00436 SiStripConfigDb::FedIdsRange fed_ids = db->getFedIds();
00437
00438 if ( !fed_ids.empty() ) {
00439 LogTrace(mlCabling_)
00440 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00441 << " Retrieved " << fed_ids.size()
00442 << " FED ids from database!";
00443 } else {
00444 edm::LogWarning(mlCabling_)
00445 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00446 << " No FED ids found!";
00447 }
00448
00449
00450
00451 edm::LogVerbatim(mlCabling_)
00452 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00453 << " Building FEC cabling object from APV and DCU descriptions...";
00454
00455 SiStripConfigDb::DeviceDescriptionsV::const_iterator iapv;
00456 for ( iapv = apv_desc.begin(); iapv != apv_desc.end(); iapv++ ) {
00457
00458 if ( !(*iapv) ) {
00459 edm::LogWarning(mlCabling_)
00460 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00461 << " NULL pointer to DeviceDescription (of type APV25)!";
00462 continue;
00463 }
00464
00465 SiStripConfigDb::DeviceAddress addr = db->deviceAddress(**iapv);
00466 FedChannelConnection conn( addr.fecCrate_ + sistrip::FEC_CRATE_OFFSET,
00467 addr.fecSlot_,
00468 addr.fecRing_ + sistrip::FEC_RING_OFFSET,
00469 addr.ccuAddr_,
00470 addr.ccuChan_,
00471 addr.i2cAddr_ );
00472 fec_cabling.addDevices( conn );
00473
00474 }
00475
00476 SiStripConfigDb::DeviceDescriptionsV::const_iterator idcu;
00477 for ( idcu = dcu_desc.begin(); idcu != dcu_desc.end(); idcu++ ) {
00478
00479 if ( !(*idcu) ) {
00480 edm::LogWarning(mlCabling_)
00481 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00482 << " NULL pointer to DeviceDescription (of type DCU)!";
00483 continue;
00484 }
00485
00486 SiStripConfigDb::DeviceAddress addr = db->deviceAddress(**idcu);
00487 dcuDescription* dcu = dynamic_cast<dcuDescription*>( *idcu );
00488 if ( !dcu ) { continue; }
00489 if ( dcu->getDcuType() != "FEH" ) { continue; }
00490 FedChannelConnection conn( addr.fecCrate_ + sistrip::FEC_CRATE_OFFSET,
00491 addr.fecSlot_,
00492 addr.fecRing_ + sistrip::FEC_RING_OFFSET,
00493 addr.ccuAddr_,
00494 addr.ccuChan_,
00495 0, 0,
00496 dcu->getDcuHardId() );
00497 fec_cabling.dcuId( conn );
00498
00499 }
00500
00501 LogTrace(mlCabling_)
00502 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00503 << " Finished building FEC cabling object from APV and DCU descriptions!";
00504
00505 NumberOfDevices devs1 = fec_cabling.countDevices();
00506 std::stringstream ss1;
00507 ss1 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00508 << " Number of devices in FEC cabling object:" << std::endl;
00509 devs1.print(ss1);
00510 LogTrace(mlCabling_) << ss1.str();
00511
00512
00513
00514 std::vector<uint16_t>::const_iterator ifed = fed_ids.begin();
00515 uint16_t fed_ch = 0;
00516
00517
00518
00519 std::vector<uint32_t> used_keys;
00520
00521 LogTrace(mlCabling_)
00522 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00523 << " Randomly assigning FED ids/channels to APV pairs in front-end modules...";
00524
00525 if ( fed_ids.empty() ) {
00526 edm::LogError(mlCabling_)
00527 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00528 << " No FED ids retrieved from database! Unable to cable system!";
00529 } else {
00530
00531 bool complete = false;
00532 std::vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin();
00533 std::vector<SiStripFecCrate>::const_iterator jcrate = fec_cabling.crates().end();
00534 while ( !complete && icrate != jcrate ) {
00535 std::vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin();
00536 std::vector<SiStripFec>::const_iterator jfec = icrate->fecs().end();
00537 while ( !complete && ifec != jfec ) {
00538 std::vector<SiStripRing>::const_iterator iring = ifec->rings().begin();
00539 std::vector<SiStripRing>::const_iterator jring = ifec->rings().end();
00540 while ( !complete && iring != jring ) {
00541 std::vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin();
00542 std::vector<SiStripCcu>::const_iterator jccu = iring->ccus().end();
00543 while ( !complete && iccu != jccu ) {
00544 std::vector<SiStripModule>::const_iterator imod = iccu->modules().begin();
00545 std::vector<SiStripModule>::const_iterator jmod = iccu->modules().end();
00546 while ( !complete && imod != jmod ) {
00547
00548
00549 const_cast<SiStripModule&>(*imod).nApvPairs(0);
00550
00551 used_keys.push_back( SiStripFecKey( imod->fecCrate(),
00552 imod->fecSlot(),
00553 imod->fecRing(),
00554 imod->ccuAddr(),
00555 imod->ccuChan() ).key() );
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572 for ( uint16_t ipair = 0; ipair < imod->nApvPairs(); ipair++ ) {
00573
00574
00575 if ( ifed == fed_ids.end() ) { fed_ch++; ifed = fed_ids.begin(); }
00576 if ( fed_ch == 96 ) {
00577 edm::LogWarning(mlCabling_)
00578 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00579 << " Insufficient FED channels to cable all devices in control system!";
00580 complete = true;
00581 break;
00582 }
00583
00584
00585 pair<uint16_t,uint16_t> addr = imod->activeApvPair( imod->lldChannel(ipair) );
00586 SiStripModule::FedChannel fed_channel( (*ifed)/16+1,
00587 (*ifed)%16+2,
00588 *ifed,
00589 fed_ch );
00590 const_cast<SiStripModule&>(*imod).fedCh( addr.first, fed_channel );
00591 ifed++;
00592
00593 }
00594
00595 imod++;
00596 }
00597 iccu++;
00598 }
00599 iring++;
00600 }
00601 ifec++;
00602 }
00603 icrate++;
00604 }
00605
00606 }
00607
00608 std::sort( used_keys.begin(), used_keys.end() );
00609
00610 NumberOfDevices devs2 = fec_cabling.countDevices();
00611 std::stringstream ss2;
00612 ss2 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00613 << " Number of devices in FEC cabling object:" << std::endl;
00614 devs2.print(ss2);
00615 LogTrace(mlCabling_) << ss2.str();
00616
00617 LogTrace(mlCabling_)
00618 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00619 << " Finished randomly assigning FED ids/channels to APV pairs in front-end modules...";
00620
00621
00622
00623 LogTrace(mlCabling_)
00624 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00625 << " Assigning APV pairs in dummy front-end modules to any remaining \"uncabled\" FED ids/channels...";
00626
00627 if ( fed_ids.empty() ) {
00628 edm::LogError(mlCabling_)
00629 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00630 << " No FED ids retrieved from database! Unable to cable system!";
00631 } else {
00632
00633 uint16_t module = 0;
00634 bool complete = false;
00635 while ( !complete ) {
00636 for ( uint16_t lld = sistrip::LLD_CHAN_MIN; lld < sistrip::LLD_CHAN_MAX+1; lld++ ) {
00637
00638
00639 if ( ifed == fed_ids.end() ) { fed_ch++; ifed = fed_ids.begin(); }
00640 if ( fed_ch == 96 ) {
00641 LogTrace(mlCabling_)
00642 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00643 << " All FED channels are now cabled!";
00644 complete = true;
00645 break;
00646 }
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657 FedChannelConnection temp( fecCrate( module ),
00658 fecSlot( module ),
00659 fecRing( module ),
00660 ccuAddr( module ),
00661 ccuChan( module ),
00662 SiStripFecKey::i2cAddr(lld,true),
00663 SiStripFecKey::i2cAddr(lld,false),
00664 sistrip::invalid32_,
00665 sistrip::invalid32_,
00666 3,
00667 *ifed,
00668 fed_ch );
00669 uint16_t fed_crate = (*ifed)/16+1;
00670 uint16_t fed_slot = (*ifed)%16+2;
00671 temp.fedCrate( fed_crate );
00672 temp.fedSlot( fed_slot );
00673 fec_cabling.addDevices( temp );
00674 ifed++;
00675
00676 }
00677 module++;
00678 }
00679
00680 }
00681
00682 LogTrace(mlCabling_)
00683 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00684 << " Finished assigning APV pairs in dummy front-end modules to any remaining \"uncabled\" FED ids/channels...";
00685
00686 NumberOfDevices devs3 = fec_cabling.countDevices();
00687 std::stringstream ss3;
00688 ss3 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00689 << " Number of devices in FEC cabling object:" << std::endl;
00690 devs3.print(ss3);
00691 LogTrace(mlCabling_) << ss3.str();
00692
00693
00694
00695 assignDcuAndDetIds( fec_cabling, dcu_detid_vector );
00696
00697 }
00698
00699
00737 void SiStripFedCablingBuilderFromDb::buildFecCablingFromDetIds( SiStripConfigDb* const db,
00738 SiStripFecCabling& fec_cabling ) {
00739 edm::LogVerbatim(mlCabling_)
00740 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00741 << " Building FEC cabling object from DetIds...";
00742
00743
00744
00745
00746
00747
00748 uint32_t chans_per_ccu = 10;
00749 uint32_t ccus_per_ring = 10;
00750 uint32_t rings_per_fec = 8;
00751 uint32_t fecs_per_crate = 11;
00752
00753
00754
00755
00756 LogTrace(mlCabling_)
00757 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00758 << " Retrieving DCU-DetId vector from database...";
00759 SiStripConfigDb::DcuDetIdsRange range = db->getDcuDetIds();
00760 const SiStripConfigDb::DcuDetIdsV dcu_detid_vector( range.begin(), range.end() );
00761 if ( !dcu_detid_vector.empty() ) {
00762 LogTrace(mlCabling_)
00763 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00764 << " Found " << dcu_detid_vector.size()
00765 << " entries in DCU-DetId vector retrieved from database!";
00766 } else {
00767 edm::LogWarning(mlCabling_)
00768 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00769 << " No entries in DCU-DetId vector retrieved from database!"
00770 << " Unable to build FEC cabling!";
00771 return;
00772 }
00773
00774
00775
00776 uint32_t imodule = 0;
00777 SiStripConfigDb::DcuDetIdsV::const_iterator iter;
00778 for ( iter = dcu_detid_vector.begin(); iter != dcu_detid_vector.end(); iter++ ) {
00779
00780 if ( !(iter->second) ) {
00781 edm::LogWarning(mlCabling_)
00782 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00783 << " NULL pointer to TkDcuInfo!";
00784 continue;
00785 }
00786
00787 uint16_t fec_crate = ( imodule / ( chans_per_ccu * ccus_per_ring * rings_per_fec * fecs_per_crate ) ) + 1;
00788 uint16_t fec_slot = ( imodule / ( chans_per_ccu * ccus_per_ring * rings_per_fec ) ) % fecs_per_crate + 2;
00789 uint16_t fec_ring = ( imodule / ( chans_per_ccu * ccus_per_ring ) ) % rings_per_fec + 1;
00790 uint16_t ccu_addr = ( imodule / ( chans_per_ccu) ) % ccus_per_ring + 1;
00791 uint16_t ccu_chan = ( imodule ) % chans_per_ccu + 16;
00792
00793 uint32_t dcu_id = iter->second->getDcuHardId();
00794 uint32_t det_id = iter->second->getDetId();
00795 uint16_t npairs = iter->second->getApvNumber()/2;
00796 uint16_t length = (uint16_t) iter->second->getFibreLength();
00797
00798
00799
00800 if ( !dcu_id ) {
00801 dcu_id = SiStripFecKey( fec_crate,
00802 fec_slot,
00803 fec_ring,
00804 ccu_addr,
00805 ccu_chan ).key();
00806 }
00807 if ( !det_id ) { det_id = 0xFFFF + imodule; }
00808 if ( !npairs ) { npairs = rand()/2 ? 2 : 3; }
00809
00810
00811
00812 for ( uint16_t ipair = 0; ipair < npairs; ipair++ ) {
00813 uint16_t iapv = ( ipair == 1 && npairs == 2 ? 36 : 32 + 2 * ipair ) ;
00814 FedChannelConnection conn( fec_crate,
00815 fec_slot,
00816 fec_ring,
00817 ccu_addr,
00818 ccu_chan,
00819 iapv, iapv+1,
00820 dcu_id, det_id, npairs,
00821 0, 0,
00822 length,
00823 true, true, true, true );
00824 fec_cabling.addDevices( conn );
00825 }
00826
00827 imodule++;
00828 }
00829
00830
00831
00832 uint32_t fed_id = 50;
00833 uint32_t fed_ch = 0;
00834 for ( vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin(); icrate != fec_cabling.crates().end(); icrate++ ) {
00835 for ( vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++ ) {
00836 for ( vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++ ) {
00837 for ( vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++ ) {
00838 for ( vector<SiStripModule>::const_iterator imod = iccu->modules().begin(); imod != iccu->modules().end(); imod++ ) {
00839 if ( 96-fed_ch < imod->nApvPairs() ) { fed_id++; fed_ch = 0; }
00840 for ( uint16_t ipair = 0; ipair < imod->nApvPairs(); ipair++ ) {
00841 pair<uint16_t,uint16_t> addr = imod->activeApvPair( (*imod).lldChannel(ipair) );
00842 SiStripModule::FedChannel fed_channel( (fed_id-50)/16+1,
00843 (fed_id-50)%16+2,
00844 fed_id,
00845 fed_ch );
00846 const_cast<SiStripModule&>(*imod).fedCh( addr.first, fed_channel );
00847 fed_ch++;
00848 }
00849 }
00850 }
00851 }
00852 }
00853 }
00854
00855 }
00856
00857
00859 void SiStripFedCablingBuilderFromDb::assignDcuAndDetIds( SiStripFecCabling& fec_cabling,
00860 std::vector< std::pair<uint32_t,TkDcuInfo*> > in ) {
00861
00862
00863
00864 if ( in.empty() ) {
00865 edm::LogError(mlCabling_)
00866 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00867 << " No entries in DCU-DetId vector!";
00868 }
00869
00870
00871
00872 LogTrace(mlCabling_)
00873 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00874 << " Assigning DCU ids and DetIds to constructed modules...";
00875
00876 uint16_t channels = 0;
00877 uint16_t six = 0;
00878 uint16_t four = 0;
00879 uint16_t unknown = 0;
00880 uint16_t missing = 0;
00881
00882 for ( vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin(); icrate != fec_cabling.crates().end(); icrate++ ) {
00883 for ( vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++ ) {
00884 for ( vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++ ) {
00885 for ( vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++ ) {
00886 for ( vector<SiStripModule>::const_iterator imod = iccu->modules().begin(); imod != iccu->modules().end(); imod++ ) {
00887 SiStripModule& module = const_cast<SiStripModule&>(*imod);
00888
00889
00890 module.nApvPairs(0);
00891
00892
00893
00894 if ( !module.dcuId() ) {
00895 SiStripFecKey path( icrate->fecCrate(),
00896 ifec->fecSlot(),
00897 iring->fecRing(),
00898 iccu->ccuAddr(),
00899 imod->ccuChan() );
00900 uint32_t module_key = path.key();
00901 module.dcuId( module_key );
00902 stringstream ss;
00903 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00904 << " Found NULL DcuId! Setting 'dummy' value based control key 0x"
00905 << hex << setw(8) << setfill('0') << module_key << dec;
00906 edm::LogWarning(mlCabling_) << ss.str();
00907 }
00908
00909
00910
00911 if ( !module.detId() ) {
00912
00913
00914
00915 SiStripConfigDb::DcuDetIdsV::iterator iter = in.end();
00916 iter = SiStripConfigDb::findDcuDetId( in.begin(), in.end(), module.dcuId() );
00917 if ( iter != in.end() ) {
00918
00919 if ( !(iter->second) ) {
00920 edm::LogWarning(mlCabling_)
00921 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
00922 << " NULL pointer to TkDcuInfo!";
00923 continue;
00924 }
00925
00926
00927
00928 module.detId( iter->second->getDetId() );
00929 module.nApvPairs(0);
00930
00931
00932 uint16_t pairs = iter->second->getApvNumber()/2;
00933 channels += pairs;
00934 if ( pairs == 2 ) { four++; }
00935 else if ( pairs == 3 ) { six++; }
00936 else { unknown++; }
00937
00938
00939
00940 if ( module.nApvPairs() != 2 && module.nApvPairs() != 3 ) {
00941
00942 missing += ( iter->second->getApvNumber()/2 - module.nApvPairs() );
00943 stringstream ss1;
00944 ss1 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]" << std::endl
00945 << " Module with DCU id 0x"
00946 << hex << setw(8) << setfill('0') << module.dcuId() << dec
00947 << " and DetId 0x"
00948 << hex << setw(8) << setfill('0') << module.detId() << dec
00949 << " has unexpected number of APV pairs ("
00950 << module.nApvPairs() << ")." << std::endl
00951 << " Some APV pairs may have not been detected by the FEC scan." << std::endl
00952 << " Setting to value found in static map ("
00953 << iter->second->getApvNumber()/2 << ")...";
00954 edm::LogWarning(mlCabling_) << ss1.str();
00955 module.nApvPairs( iter->second->getApvNumber()/2 );
00956
00957 } else if ( module.nApvPairs() < iter->second->getApvNumber()/2 ) {
00958
00959 missing += ( iter->second->getApvNumber()/2 - module.nApvPairs() );
00960 stringstream ss2;
00961 ss2 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]" << std::endl
00962 << " Module with DCU id 0x"
00963 << hex << setw(8) << setfill('0') << module.dcuId() << dec
00964 << " and DetId 0x"
00965 << hex << setw(8) << setfill('0') << module.detId() << dec
00966 << " has number of APV pairs ("
00967 << module.nApvPairs()
00968 << ") that does not match value found in DCU-DetId vector ("
00969 << iter->second->getApvNumber()/2 << ")." << std::endl
00970 << " Some APV pairs may have not been detected by"
00971 << " the FEC scan or the DCU-DetId vector may be incorrect." << std::endl
00972 << " Setting to value found in static map ("
00973 << iter->second->getApvNumber()/2 << ")...";
00974 edm::LogWarning(mlCabling_) << ss2.str();
00975 module.nApvPairs( iter->second->getApvNumber()/2 );
00976
00977 }
00978
00979
00980
00981 if ( !module.length() ) {
00982 module.length( static_cast<uint16_t>( iter->second->getFibreLength() ) );
00983 }
00984
00985
00986
00987 in.erase( iter );
00988
00989 }
00990 }
00991
00992 }
00993 }
00994 }
00995 }
00996 }
00997
00998 std::stringstream sss;
00999 sss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]" << std::endl
01000 << " Connections in DCU-DetId map : " << channels << std::endl
01001 << " 4-APV modules : " << four << std::endl
01002 << " 6-APV modules : " << six << std::endl
01003 << " Unknown number of APV pairs : " << unknown << std::endl
01004 << " Total found APV pairs : " << ( channels - missing ) << std::endl
01005 << " Total missing APV pairs : " << missing << std::endl;
01006 edm::LogVerbatim(mlCabling_) << sss.str();
01007
01008 LogTrace(mlCabling_)
01009 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01010 << " Finished assigning DCU ids and DetIds to constructed modules...";
01011
01012
01013
01014 LogTrace(mlCabling_)
01015 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01016 << " Assigning \"random\" DetIds to modules with DCU ids not found in static table...";
01017
01018 uint32_t detid = 0x10000;
01019 for ( vector<SiStripFecCrate>::const_iterator icrate = fec_cabling.crates().begin(); icrate != fec_cabling.crates().end(); icrate++ ) {
01020 for ( vector<SiStripFec>::const_iterator ifec = icrate->fecs().begin(); ifec != icrate->fecs().end(); ifec++ ) {
01021 for ( vector<SiStripRing>::const_iterator iring = ifec->rings().begin(); iring != ifec->rings().end(); iring++ ) {
01022 for ( vector<SiStripCcu>::const_iterator iccu = iring->ccus().begin(); iccu != iring->ccus().end(); iccu++ ) {
01023 for ( vector<SiStripModule>::const_iterator imod = iccu->modules().begin(); imod != iccu->modules().end(); imod++ ) {
01024 SiStripModule& module = const_cast<SiStripModule&>(*imod);
01025
01026
01027
01028 if ( !module.detId() ) {
01029
01030 SiStripConfigDb::DcuDetIdsV::iterator iter = in.end();
01031 iter = SiStripConfigDb::findDcuDetId( in.begin(), in.end(), module.dcuId() );
01032 if ( iter != in.end() ) {
01033
01034
01035
01036 SiStripConfigDb::DcuDetIdsV::iterator idcu;
01037 if ( in.empty() ) { idcu = in.end(); }
01038 else {
01039 idcu = in.begin();
01040 while ( idcu != in.end() ) {
01041 if ( idcu->second ) {
01042 if ( static_cast<uint32_t>(idcu->second->getApvNumber()) ==
01043 static_cast<uint32_t>(2*module.nApvPairs()) ) { break; }
01044 }
01045 idcu++;
01046 }
01047 }
01048
01049
01050
01051 if ( idcu != in.end() ) {
01052
01053 if ( !(idcu->second) ) {
01054 edm::LogWarning(mlCabling_)
01055 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01056 << " NULL pointer to TkDcuInfo!";
01057 continue;
01058 }
01059
01060 module.detId( idcu->second->getDetId() );
01061 in.erase( idcu );
01062
01063 stringstream ss;
01064 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01065 << " Did not find module with DCU id 0x"
01066 << hex << setw(8) << setfill('0') << module.dcuId() << dec
01067 << " in DCU-DetId vector!" << endl
01068 << " Assigned 'random' DetId 0x"
01069 << hex << setw(8) << setfill('0') << module.detId() << dec;
01070 edm::LogWarning(mlCabling_) << ss.str();
01071
01072 } else {
01073
01074
01075 module.detId( detid );
01076 detid++;
01077
01078 stringstream ss;
01079 if ( in.empty() ) {
01080 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01081 << " Did not find module with DCU id 0x"
01082 << hex << setw(8) << setfill('0') << module.dcuId() << dec
01083 << " in DCU-DetId vector!"
01084 << " Could not assign 'random' DetId as DCU-DetID map is empty!"
01085 << " Assigned DetId based on incremented counter, with value 0x"
01086 << hex << setw(8) << setfill('0') << module.detId() << dec;
01087 } else {
01088 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01089 << " Did not find module with DCU id 0x"
01090 << hex << setw(8) << setfill('0') << module.dcuId() << dec
01091 << " in DCU-DetId vector!"
01092 << " Could not assign 'random' DetId as no modules had appropriate number of APV pairs ("
01093 << module.nApvPairs()
01094 << "). Assigned DetId based on incremented counter, with value 0x"
01095 << hex << setw(8) << setfill('0') << module.detId() << dec;
01096 }
01097 edm::LogWarning(mlCabling_) << ss.str();
01098
01099 }
01100 }
01101
01102 }
01103
01104 }
01105 }
01106 }
01107 }
01108 }
01109
01110 LogTrace(mlCabling_)
01111 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01112 << " Finished assigning \"random\" DetIds to modules with DCU ids not found in static table...";
01113
01114 LogTrace(mlCabling_)
01115 << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01116 << " Assigning \"random\" DetIds to modules with DCU ids not found in static table...";
01117
01118
01119
01120 if ( !in.empty() ) {
01121 stringstream ss;
01122 ss << "[SiStripFedCablingBuilderFromDb::" << __func__ << "]"
01123 << " Not all DetIds have been assigned to a DcuId! "
01124 << in.size() << " DetIds are unassigned!";
01125 edm::LogWarning(mlCabling_) << ss.str();
01126 }
01127
01128 }
01129
01130
01132 void SiStripFedCablingBuilderFromDb::getFedCabling( const SiStripFecCabling& fec_cabling,
01133 SiStripFedCabling& fed_cabling ) {
01134 vector<FedChannelConnection> conns;
01135 fec_cabling.connections( conns );
01136 fed_cabling.buildFedCabling( conns );
01137 }
01138
01139
01141 void SiStripFedCablingBuilderFromDb::getFecCabling( const SiStripFedCabling& fed_cabling,
01142 SiStripFecCabling& fec_cabling ) {
01143 fec_cabling.buildFecCabling( fed_cabling );
01144 }
01145
01146
01147
01148 void SiStripFedCablingBuilderFromDb::setIntervalFor( const edm::eventsetup::EventSetupRecordKey& key,
01149 const edm::IOVSyncValue& iov_sync,
01150 edm::ValidityInterval& iov_validity ) {
01151 edm::ValidityInterval infinity( iov_sync.beginOfTime(), iov_sync.endOfTime() );
01152 iov_validity = infinity;
01153 }
01154