00001
00002
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006
00007 using namespace std;
00008 using namespace sistrip;
00009
00010
00011
00012 SiStripConfigDb::DeviceDescriptionsRange SiStripConfigDb::getDeviceDescriptions( std::string partition ) {
00013
00014
00015 if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00016 ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
00017 return devices_.emptyRange();
00018 }
00019
00020 try {
00021
00022 if ( !dbParams_.usingDbCache() ) {
00023
00024 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00025 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00026 for ( ; iter != jter; ++iter ) {
00027
00028 if ( partition == "" || partition == iter->second.partitionName() ) {
00029
00030 if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00031
00032 DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
00033 if ( range == devices_.emptyRange() ) {
00034
00035
00036 DeviceDescriptionsV tmp1;
00037 deviceFactory(__func__)->getFecDeviceDescriptions( iter->second.partitionName(),
00038 tmp1,
00039 iter->second.fecVersion().first,
00040 iter->second.fecVersion().second,
00041 false );
00042
00043
00044 DeviceDescriptionsV tmp2;
00045 #ifdef USING_NEW_DATABASE_MODEL
00046 FecFactory::vectorCopyI( tmp2, tmp1, true );
00047 #else
00048 tmp2 = tmp1;
00049 #endif
00050
00051
00052 devices_.loadNext( iter->second.partitionName(), tmp2 );
00053
00054
00055 DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
00056 std::stringstream ss;
00057 ss << "[SiStripConfigDb::" << __func__ << "]"
00058 << " Downloaded " << range.size()
00059 << " device descriptions to local cache for partition \""
00060 << iter->second.partitionName() << "\"" << std::endl;
00061 ss << "[SiStripConfigDb::" << __func__ << "]"
00062 << " Cache holds device descriptions for "
00063 << devices_.size() << " partitions.";
00064 LogTrace(mlConfigDb_) << ss.str();
00065
00066 }
00067
00068 }
00069
00070 }
00071
00072 } else {
00073
00074 #ifdef USING_NEW_DATABASE_MODEL
00075
00076 DeviceDescriptionsV* tmp1 = databaseCache(__func__)->getDevices();
00077
00078 if ( tmp1 ) {
00079
00080
00081 DeviceDescriptionsV tmp2;
00082 FecFactory::vectorCopyI( tmp2, *tmp1, true );
00083
00084
00085 devices_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
00086
00087 } else {
00088 edm::LogWarning(mlConfigDb_)
00089 << "[SiStripConfigDb::" << __func__ << "]"
00090 << " NULL pointer to DeviceDescriptions vector!";
00091 }
00092
00093 #endif // USING_NEW_DATABASE_MODEL
00094
00095 }
00096
00097 } catch (...) { handleException( __func__ ); }
00098
00099
00100 uint16_t np = 0;
00101 uint16_t nc = 0;
00102 DeviceDescriptionsRange devs;
00103 if ( partition != "" ) {
00104 devs = devices_.find( partition );
00105 np = 1;
00106 nc = devs.size();
00107 } else {
00108 if ( !devices_.empty() ) {
00109 devs = DeviceDescriptionsRange( devices_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00110 devices_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00111 } else { devs = devices_.emptyRange(); }
00112 np = devices_.size();
00113 nc = devs.size();
00114 }
00115
00116 stringstream ss;
00117 ss << "[SiStripConfigDb::" << __func__ << "]"
00118 << " Found " << nc << " device descriptions";
00119 if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputFecXmlFiles().size() << " 'fec.xml' file(s)"; }
00120 else { if ( !dbParams_.usingDbCache() ) { ss << " in " << np << " database partition(s)"; }
00121 else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
00122 if ( devices_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00123 else { LogTrace(mlConfigDb_) << ss.str(); }
00124
00125 return devs;
00126
00127 }
00128
00129
00130
00131 SiStripConfigDb::DeviceDescriptionsRange SiStripConfigDb::getDeviceDescriptions( DeviceType device_type,
00132 std::string partition ) {
00133
00134 typedDevices_.clear();
00135 apvDevices_.clear();
00136 muxDevices_.clear();
00137 dcuDevices_.clear();
00138 lldDevices_.clear();
00139 pllDevices_.clear();
00140 dohDevices_.clear();
00141
00142 DeviceDescriptionsRange tmp( typedDevices_.end(), typedDevices_.end() );
00143
00144 if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00145 ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
00146 return tmp;
00147 }
00148
00149 try {
00150 DeviceDescriptionsRange devs = SiStripConfigDb::getDeviceDescriptions( partition );
00151 if ( !devs.empty() ) {
00152 DeviceDescriptionsV tmp( devs.begin(), devs.end() );
00153 typedDevices_ = FecFactory::getDeviceFromDeviceVector( tmp, device_type );
00154 }
00155 } catch (...) { handleException( __func__ ); }
00156
00157 if ( device_type == APV25 ) {
00158 apvDevices_.swap( typedDevices_ );
00159 tmp = DeviceDescriptionsRange( apvDevices_.begin(), apvDevices_.end() );
00160 } else if ( device_type == APVMUX ) {
00161 muxDevices_.swap( typedDevices_ );
00162 tmp = DeviceDescriptionsRange( muxDevices_.begin(), muxDevices_.end() );
00163 } else if ( device_type == DCU ) {
00164 dcuDevices_.swap( typedDevices_ );
00165 tmp = DeviceDescriptionsRange( dcuDevices_.begin(), dcuDevices_.end() );
00166 } else if ( device_type == LASERDRIVER ) {
00167 lldDevices_.swap( typedDevices_ );
00168 tmp = DeviceDescriptionsRange( lldDevices_.begin(), lldDevices_.end() );
00169 } else if ( device_type == PLL ) {
00170 pllDevices_.swap( typedDevices_ );
00171 tmp = DeviceDescriptionsRange( pllDevices_.begin(), pllDevices_.end() );
00172 } else if ( device_type == DOH ) {
00173 dohDevices_.swap( typedDevices_ );
00174 tmp = DeviceDescriptionsRange( dohDevices_.begin(), dohDevices_.end() );
00175 } else {
00176 tmp = DeviceDescriptionsRange( typedDevices_.begin(), typedDevices_.end() );
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 stringstream ss;
00192 ss << "[SiStripConfigDb::" << __func__ << "]"
00193 << " Extracted " << tmp.size()
00194 << " device descriptions (for devices of type "
00195 << deviceType( device_type ) << ")";
00196 LogTrace(mlConfigDb_) << ss.str();
00197
00198 return tmp;
00199
00200 }
00201
00202
00203
00204 void SiStripConfigDb::addDeviceDescriptions( std::string partition, DeviceDescriptionsV& devs ) {
00205
00206 if ( !deviceFactory(__func__) ) { return; }
00207
00208 if ( partition.empty() ) {
00209 stringstream ss;
00210 ss << "[SiStripConfigDb::" << __func__ << "]"
00211 << " Partition string is empty,"
00212 << " therefore cannot add device descriptions to local cache!";
00213 edm::LogWarning(mlConfigDb_) << ss.str();
00214 return;
00215 }
00216
00217 if ( devs.empty() ) {
00218 stringstream ss;
00219 ss << "[SiStripConfigDb::" << __func__ << "]"
00220 << " Vector of device descriptions is empty,"
00221 << " therefore cannot add device descriptions to local cache!";
00222 edm::LogWarning(mlConfigDb_) << ss.str();
00223 return;
00224 }
00225
00226 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00227 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00228 for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00229 if ( iter == dbParams_.partitions().end() ) {
00230 stringstream ss;
00231 ss << "[SiStripConfigDb::" << __func__ << "]"
00232 << " Partition \"" << partition
00233 << "\" not found in partition list, "
00234 << " therefore cannot add device descriptions!";
00235 edm::LogWarning(mlConfigDb_) << ss.str();
00236 return;
00237 }
00238
00239 DeviceDescriptionsRange range = devices_.find( partition );
00240 if ( range == devices_.emptyRange() ) {
00241
00242
00243 DeviceDescriptionsV tmp;
00244 #ifdef USING_NEW_DATABASE_MODEL
00245 FecFactory::vectorCopyI( tmp, devs, true );
00246 #else
00247 tmp = devs;
00248 #endif
00249
00250
00251 devices_.loadNext( partition, tmp );
00252
00253
00254 std::stringstream ss;
00255 ss << "[SiStripConfigDb::" << __func__ << "]"
00256 << " Added " << devs.size()
00257 << " device descriptions to local cache for partition \""
00258 << partition << "\"."
00259 << " (Cache holds device descriptions for "
00260 << devices_.size() << " partitions.)";
00261 LogTrace(mlConfigDb_) << ss.str();
00262
00263 } else {
00264 stringstream ss;
00265 ss << "[SiStripConfigDb::" << __func__ << "]"
00266 << " Partition \"" << partition
00267 << "\" already found in local cache, "
00268 << " therefore cannot add device descriptions!";
00269 edm::LogWarning(mlConfigDb_) << ss.str();
00270 return;
00271 }
00272
00273 }
00274
00275
00276
00277 void SiStripConfigDb::uploadDeviceDescriptions( std::string partition ) {
00278
00279 if ( dbParams_.usingDbCache() ) {
00280 edm::LogWarning(mlConfigDb_)
00281 << "[SiStripConfigDb::" << __func__ << "]"
00282 << " Using database cache! No uploads allowed!";
00283 return;
00284 }
00285
00286 if ( !deviceFactory(__func__) ) { return; }
00287
00288 if ( devices_.empty() ) {
00289 stringstream ss;
00290 ss << "[SiStripConfigDb::" << __func__ << "]"
00291 << " Found no cached device descriptions, therefore no upload!";
00292 edm::LogWarning(mlConfigDb_) << ss.str();
00293 return;
00294 }
00295
00296 try {
00297
00298 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00299 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00300 for ( ; iter != jter; ++iter ) {
00301
00302 if ( partition == "" || partition == iter->second.partitionName() ) {
00303
00304 DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
00305 if ( range != devices_.emptyRange() ) {
00306
00307 DeviceDescriptionsV devs( range.begin(), range.end() );
00308
00309 deviceFactory(__func__)->setFecDeviceDescriptions( devs,
00310 iter->second.partitionName(),
00311 &(iter->second.fecVersion().first),
00312 &(iter->second.fecVersion().second),
00313 true );
00314
00315
00316 std::stringstream ss;
00317 ss << "[SiStripConfigDb::" << __func__ << "]"
00318 << " Uploaded " << devs.size()
00319 << " device descriptions to database for partition \""
00320 << iter->second.partitionName() << "\".";
00321 LogTrace(mlConfigDb_) << ss.str();
00322
00323 } else {
00324 stringstream ss;
00325 ss << "[SiStripConfigDb::" << __func__ << "]"
00326 << " Vector of device descriptions is empty for partition \""
00327 << iter->second.partitionName()
00328 << "\", therefore aborting upload for this partition!";
00329 edm::LogWarning(mlConfigDb_) << ss.str();
00330 continue;
00331 }
00332
00333 } else {
00334
00335
00336
00337
00338
00339
00340
00341 }
00342
00343 }
00344
00345 } catch (...) { handleException( __func__ ); }
00346
00347 allowCalibUpload_ = true;
00348
00349 }
00350
00351
00352
00353 void SiStripConfigDb::clearDeviceDescriptions( std::string partition ) {
00354 LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00355
00356 if ( devices_.empty() ) {
00357 stringstream ss;
00358 ss << "[SiStripConfigDb::" << __func__ << "]"
00359 << " Found no cached device descriptions!";
00360
00361 return;
00362 }
00363
00364
00365 DeviceDescriptions temporary_cache;
00366 if ( partition == "" ) { temporary_cache = DeviceDescriptions(); }
00367 else {
00368 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00369 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00370 for ( ; iter != jter; ++iter ) {
00371 if ( partition != iter->second.partitionName() ) {
00372 DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
00373 if ( range != devices_.emptyRange() ) {
00374 temporary_cache.loadNext( partition, DeviceDescriptionsV( range.begin(), range.end() ) );
00375 } else {
00376
00377
00378
00379
00380
00381 }
00382 }
00383 }
00384 }
00385
00386
00387 DeviceDescriptionsRange devs;
00388 if ( partition == "" ) {
00389 if ( !devices_.empty() ) {
00390 devs = DeviceDescriptionsRange( devices_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00391 devices_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00392 } else { devs = devices_.emptyRange(); }
00393 } else {
00394 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00395 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00396 for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00397 devs = devices_.find( iter->second.partitionName() );
00398 }
00399
00400 if ( devs != devices_.emptyRange() ) {
00401
00402 #ifdef USING_NEW_DATABASE_MODEL
00403 DeviceDescriptionsV::const_iterator ifed = devs.begin();
00404 DeviceDescriptionsV::const_iterator jfed = devs.end();
00405 for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00406 #endif
00407
00408 } else {
00409 stringstream ss;
00410 ss << "[SiStripConfigDb::" << __func__ << "]";
00411 if ( partition == "" ) { ss << " Found no device descriptions in local cache!"; }
00412 else { ss << " Found no device descriptions in local cache for partition \"" << partition << "\"!"; }
00413 edm::LogWarning(mlConfigDb_) << ss.str();
00414 }
00415
00416
00417 devices_ = temporary_cache;
00418
00419 }
00420
00421
00422
00423 void SiStripConfigDb::printDeviceDescriptions( std::string partition ) {
00424
00425 std::stringstream ss;
00426 ss << "[SiStripConfigDb::" << __func__ << "]"
00427 << " Contents of DeviceDescriptions container:" << std::endl;
00428 ss << " Number of partitions: " << devices_.size() << std::endl;
00429
00430
00431 uint16_t cntr = 0;
00432 DeviceDescriptions::const_iterator idev = devices_.begin();
00433 DeviceDescriptions::const_iterator jdev = devices_.end();
00434 for ( ; idev != jdev; ++idev ) {
00435
00436 cntr++;
00437 if ( partition == "" || partition == idev->first ) {
00438
00439 ss << " Partition number : " << cntr << " (out of " << devices_.size() << ")" << std::endl;
00440 ss << " Partition name : \"" << idev->first << "\"" << std::endl;
00441 ss << " Num of devices : " << idev->second.size() << std::endl;
00442
00443
00444 std::map< uint32_t, vector<std::string> > devices;
00445 DeviceDescriptionsV::const_iterator iter = idev->second.begin();
00446 DeviceDescriptionsV::const_iterator jter = idev->second.end();
00447 for ( ; iter != jter; ++iter ) {
00448 if ( *iter ) {
00449 DeviceAddress addr = deviceAddress( **iter );
00450 uint32_t key = SiStripFecKey( addr.fecCrate_,
00451 addr.fecSlot_,
00452 addr.fecRing_,
00453 0,
00454 0,
00455 0,
00456 0 ).key();
00457 std::stringstream data;
00458 data << (*iter)->getDeviceType()
00459 << "_"
00460 << SiStripFecKey( addr.fecCrate_,
00461 addr.fecSlot_,
00462 addr.fecRing_,
00463 addr.ccuAddr_,
00464 addr.ccuChan_,
00465 addr.lldChan_,
00466 addr.i2cAddr_ ).key();
00467 if ( find( devices[key].begin(), devices[key].end(), data.str() ) == devices[key].end() ) {
00468 devices[key].push_back( data.str() );
00469 }
00470 }
00471 }
00472
00473
00474 std::map< uint32_t, std::vector<std::string> > tmp;
00475 std::map< uint32_t, std::vector<std::string> >::const_iterator ii = devices.begin();
00476 std::map< uint32_t, std::vector<std::string> >::const_iterator jj = devices.end();
00477 for ( ; ii != jj; ++ii ) {
00478 std::vector<std::string> temp = ii->second;
00479 std::sort( temp.begin(), temp.end() );
00480 std::vector<std::string>::const_iterator iii = temp.begin();
00481 std::vector<std::string>::const_iterator jjj = temp.end();
00482 for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00483 }
00484 devices.clear();
00485 devices = tmp;
00486
00487
00488 std::map< uint32_t, std::vector<std::string> >::const_iterator idev = devices.begin();
00489 std::map< uint32_t, std::vector<std::string> >::const_iterator jdev = devices.end();
00490 for ( ; idev != jdev; ++idev ) {
00491 SiStripFecKey key(idev->first);
00492 ss << " Found " << std::setw(3) << idev->second.size()
00493 << " devices for FEC crate/slot/ring "
00494 << key.fecCrate() << "/"
00495 << key.fecSlot() << "/"
00496 << key.fecRing();
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 ss << std::endl;
00513 }
00514
00515 }
00516
00517 }
00518
00519 LogTrace(mlConfigDb_) << ss.str();
00520
00521 }
00522
00523
00524
00525 SiStripConfigDb::DeviceAddress SiStripConfigDb::deviceAddress( const deviceDescription& description ) {
00526
00527 deviceDescription& desc = const_cast<deviceDescription&>(description);
00528
00529 DeviceAddress addr;
00530 try {
00531 #ifdef USING_NEW_DATABASE_MODEL
00532 addr.fecCrate_ = static_cast<uint16_t>( desc.getCrateId() + sistrip::FEC_CRATE_OFFSET );
00533 #else
00534 addr.fecCrate_ = static_cast<uint16_t>( 0 + sistrip::FEC_CRATE_OFFSET );
00535 #endif
00536 addr.fecSlot_ = static_cast<uint16_t>( desc.getFecSlot() );
00537 addr.fecRing_ = static_cast<uint16_t>( desc.getRingSlot() + sistrip::FEC_RING_OFFSET );
00538 addr.ccuAddr_ = static_cast<uint16_t>( desc.getCcuAddress() );
00539 addr.ccuChan_ = static_cast<uint16_t>( desc.getChannel() );
00540 addr.lldChan_ = static_cast<uint16_t>( SiStripFecKey::lldChan( desc.getAddress() ) );
00541 addr.i2cAddr_ = static_cast<uint16_t>( desc.getAddress() );
00542 } catch (...) { handleException( __func__ ); }
00543
00544 return addr;
00545 }
00546
00547
00548
00549 string SiStripConfigDb::deviceType( const enumDeviceType& device_type ) const {
00550 if ( device_type == PLL ) { return "PLL"; }
00551 else if ( device_type == LASERDRIVER ) { return "LLD"; }
00552 else if ( device_type == DOH ) { return "DOH"; }
00553 else if ( device_type == APVMUX ) { return "MUX"; }
00554 else if ( device_type == APV25 ) { return "APV"; }
00555 else if ( device_type == DCU ) { return "DCU"; }
00556 else if ( device_type == GOH ) { return "GOH"; }
00557 else { return "UNKNOWN DEVICE!"; }
00558 }