00001
00002
00003
00004
00005 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00006 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008
00009 #ifdef USING_NEW_DATABASE_MODEL
00010
00011 using namespace std;
00012 using namespace sistrip;
00013
00014
00025 SiStripConfigDb::AnalysisDescriptionsRange SiStripConfigDb::getAnalysisDescriptions( AnalysisType analysis_type,
00026 std::string partition ) {
00027
00028
00029 if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00030 ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
00031 return analyses_.emptyRange();
00032 }
00033
00034 try {
00035
00036 if ( !dbParams_.usingDbCache() ) {
00037
00038 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00039 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00040 for ( ; iter != jter; ++iter ) {
00041
00042 if ( partition == "" || partition == iter->second.partitionName() ) {
00043
00044 if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00045
00046 AnalysisDescriptionsRange range = analyses_.find( iter->second.partitionName() );
00047 if ( range == analyses_.emptyRange() ) {
00048
00049 AnalysisDescriptionsV tmp1;
00050 if ( analysis_type == AnalysisDescription::T_ANALYSIS_FASTFEDCABLING ) {
00051 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00052 iter->second.fastCablingVersion().first,
00053 iter->second.fastCablingVersion().second,
00054 analysis_type );
00055 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_TIMING ) {
00056 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00057 iter->second.apvTimingVersion().first,
00058 iter->second.apvTimingVersion().second,
00059 analysis_type );
00060 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_OPTOSCAN ) {
00061 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00062 iter->second.optoScanVersion().first,
00063 iter->second.optoScanVersion().second,
00064 analysis_type );
00065 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_VPSPSCAN ) {
00066 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00067 iter->second.vpspScanVersion().first,
00068 iter->second.vpspScanVersion().second,
00069 analysis_type );
00070 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_CALIBRATION ) {
00071 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00072 iter->second.apvCalibVersion().first,
00073 iter->second.apvCalibVersion().second,
00074 analysis_type );
00075 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_PEDESTALS ) {
00076 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00077 iter->second.pedestalsVersion().first,
00078 iter->second.pedestalsVersion().second,
00079 analysis_type );
00080 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_APVLATENCY ) {
00081 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00082 iter->second.apvLatencyVersion().first,
00083 iter->second.apvLatencyVersion().second,
00084 analysis_type );
00085 } else if ( analysis_type == AnalysisDescription::T_ANALYSIS_FINEDELAY ) {
00086 tmp1 = deviceFactory(__func__)->getAnalysisHistory( iter->second.partitionName(),
00087 iter->second.fineDelayVersion().first,
00088 iter->second.fineDelayVersion().second,
00089 analysis_type );
00090 } else {
00091 std::stringstream ss;
00092 ss << "[SiStripConfigDb::" << __func__ << "]"
00093 << " Unexpected analysis type \""
00094 << analysisType( analysis_type )
00095 << "\"! Aborting download...";
00096 edm::LogWarning(mlConfigDb_) << ss.str();
00097 return analyses_.emptyRange();
00098 }
00099
00100
00101 AnalysisDescriptionsV tmp2;
00102 CommissioningAnalysisFactory::vectorCopy( tmp1, tmp2 );
00103
00104
00105 analyses_.loadNext( iter->second.partitionName(), tmp2 );
00106
00107
00108 AnalysisDescriptionsRange anals = analyses_.find( iter->second.partitionName() );
00109 std::stringstream ss;
00110 ss << "[SiStripConfigDb::" << __func__ << "]"
00111 << " Downloaded " << anals.size()
00112 << " analysis descriptions of type \""
00113 << analysisType( analysis_type )
00114 << "\" to local cache for partition \""
00115 << iter->second.partitionName() << "\"" << std::endl;
00116 ss << "[SiStripConfigDb::" << __func__ << "]"
00117 << " Cache holds analysis descriptions for "
00118 << analyses_.size() << " partitions.";
00119 LogTrace(mlConfigDb_) << ss.str();
00120
00121 }
00122 }
00123 }
00124
00125 } else {
00126 std::stringstream ss;
00127 ss << "[SiStripConfigDb::" << __func__ << "]"
00128 << " No database cache for analysis objects!";
00129 edm::LogWarning(mlConfigDb_) << ss.str();
00130 }
00131
00132 } catch (...) { handleException( __func__ ); }
00133
00134
00135 uint16_t np = 0;
00136 uint16_t nc = 0;
00137 AnalysisDescriptionsRange anals = analyses_.emptyRange();
00138 if ( partition != "" ) {
00139 anals = analyses_.find( partition );
00140 np = 1;
00141 nc = anals.size();
00142 } else {
00143 if ( !analyses_.empty() ) {
00144 anals = AnalysisDescriptionsRange( analyses_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00145 analyses_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00146 } else { anals = analyses_.emptyRange(); }
00147 np = analyses_.size();
00148 nc = anals.size();
00149 }
00150
00151 stringstream ss;
00152 ss << "[SiStripConfigDb::" << __func__ << "]"
00153 << " Found " << nc << " analysis descriptions";
00154 if ( !dbParams_.usingDbCache() ) { ss << " in " << np << " database partition(s)"; }
00155 else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; }
00156 if ( analyses_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00157 else { LogTrace(mlConfigDb_) << ss.str(); }
00158
00159 return anals;
00160
00161 }
00162
00163
00164
00165 void SiStripConfigDb::addAnalysisDescriptions( std::string partition, AnalysisDescriptionsV& anals ) {
00166
00167 if ( !deviceFactory(__func__) ) { return; }
00168
00169 if ( partition.empty() ) {
00170 stringstream ss;
00171 ss << "[SiStripConfigDb::" << __func__ << "]"
00172 << " Partition string is empty,"
00173 << " therefore cannot add analysis descriptions to local cache!";
00174 edm::LogWarning(mlConfigDb_) << ss.str();
00175 return;
00176 }
00177
00178 if ( anals.empty() ) {
00179 stringstream ss;
00180 ss << "[SiStripConfigDb::" << __func__ << "]"
00181 << " Vector of analysis descriptions is empty,"
00182 << " therefore cannot add analysis descriptions to local cache!";
00183 edm::LogWarning(mlConfigDb_) << ss.str();
00184 return;
00185 }
00186
00187 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00188 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00189 for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00190 if ( iter == dbParams_.partitions().end() ) {
00191 stringstream ss;
00192 ss << "[SiStripConfigDb::" << __func__ << "]"
00193 << " Partition \"" << partition
00194 << "\" not found in partition list, "
00195 << " therefore cannot add analysis descriptions!";
00196 edm::LogWarning(mlConfigDb_) << ss.str();
00197 return;
00198 }
00199
00200 AnalysisDescriptionsRange range = analyses_.find( partition );
00201 if ( range == analyses_.emptyRange() ) {
00202
00203
00204 AnalysisDescriptionsV tmp;
00205 CommissioningAnalysisFactory::vectorCopy( anals, tmp );
00206
00207
00208 analyses_.loadNext( partition, tmp );
00209
00210
00211 std::stringstream ss;
00212 ss << "[SiStripConfigDb::" << __func__ << "]"
00213 << " Added " << anals.size()
00214 << " analysis descriptions to local cache for partition \""
00215 << partition << "\"."
00216 << " (Cache holds analysis descriptions for "
00217 << analyses_.size() << " partitions.)";
00218 LogTrace(mlConfigDb_) << ss.str();
00219
00220 } else {
00221 stringstream ss;
00222 ss << "[SiStripConfigDb::" << __func__ << "]"
00223 << " Partition \"" << partition
00224 << "\" already found in local cache, "
00225 << " therefore cannot add analysis descriptions!";
00226 edm::LogWarning(mlConfigDb_) << ss.str();
00227 return;
00228 }
00229
00230 }
00231
00232
00233
00234 void SiStripConfigDb::uploadAnalysisDescriptions( bool calibration_for_physics,
00235 std::string partition ) {
00236
00237 if ( dbParams_.usingDbCache() ) {
00238 edm::LogWarning(mlConfigDb_)
00239 << "[SiStripConfigDb::" << __func__ << "]"
00240 << " Using database cache! No uploads allowed!";
00241 return;
00242 }
00243
00244 if ( !deviceFactory(__func__) ) { return; }
00245
00246 if ( analyses_.empty() ) {
00247 edm::LogWarning(mlConfigDb_)
00248 << "[SiStripConfigDb::" << __func__ << "]"
00249 << " Found no cached analysis descriptions, therefore no upload!";
00250 return;
00251 }
00252
00253 if ( calibration_for_physics && !allowCalibUpload_ ) {
00254 edm::LogWarning(mlConfigDb_)
00255 << "[SiStripConfigDb::" << __func__ << "]"
00256 << " Attempting to upload calibration constants"
00257 << " without uploading any hardware descriptions!"
00258 << " Aborting upload...";
00259 return;
00260 } else { allowCalibUpload_ = false; }
00261
00262 try {
00263
00264 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00265 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00266 for ( ; iter != jter; ++iter ) {
00267
00268 if ( partition == "" || partition == iter->second.partitionName() ) {
00269
00270 AnalysisDescriptionsRange range = analyses_.find( iter->second.partitionName() );
00271 if ( range != analyses_.emptyRange() ) {
00272
00273 AnalysisDescriptionsV anals( range.begin(), range.end() );
00274
00275 AnalysisType analysis_type = AnalysisDescription::T_UNKNOWN;
00276 if ( anals.front() ) { analysis_type = anals.front()->getType(); }
00277 if ( analysis_type == AnalysisDescription::T_UNKNOWN ) {
00278 edm::LogWarning(mlConfigDb_)
00279 << "[SiStripConfigDb::" << __func__ << "]"
00280 << " Analysis type is UNKNOWN. Aborting upload!";
00281 return;
00282 }
00283
00284 uint32_t version = deviceFactory(__func__)->uploadAnalysis( iter->second.runNumber(),
00285 iter->second.partitionName(),
00286 analysis_type,
00287 anals,
00288 calibration_for_physics );
00289
00290
00291 if ( calibration_for_physics ) { deviceFactory(__func__)->uploadAnalysisState( version ); }
00292
00293
00294 std::stringstream ss;
00295 ss << "[SiStripConfigDb::" << __func__ << "]"
00296 << " Uploaded " << anals.size()
00297 << " device descriptions to database for partition \""
00298 << iter->second.partitionName() << "\".";
00299 LogTrace(mlConfigDb_) << ss.str();
00300
00301 } else {
00302 stringstream ss;
00303 ss << "[SiStripConfigDb::" << __func__ << "]"
00304 << " Vector of device descriptions is empty for partition \""
00305 << iter->second.partitionName()
00306 << "\", therefore aborting upload for this partition!";
00307 edm::LogWarning(mlConfigDb_) << ss.str();
00308 continue;
00309 }
00310
00311 } else {
00312
00313
00314
00315
00316
00317
00318
00319 }
00320
00321 }
00322
00323 } catch (...) { handleException( __func__ ); }
00324
00325 allowCalibUpload_ = true;
00326
00327 }
00328
00329
00330
00331 void SiStripConfigDb::clearAnalysisDescriptions( std::string partition ) {
00332 LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00333
00334 if ( analyses_.empty() ) {
00335 stringstream ss;
00336 ss << "[SiStripConfigDb::" << __func__ << "]"
00337 << " Found no cached analysis descriptions!";
00338
00339 return;
00340 }
00341
00342
00343 AnalysisDescriptions temporary_cache;
00344 if ( partition == "" ) { temporary_cache = AnalysisDescriptions(); }
00345 else {
00346 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00347 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00348 for ( ; iter != jter; ++iter ) {
00349 if ( partition != iter->second.partitionName() ) {
00350 AnalysisDescriptionsRange range = analyses_.find( iter->second.partitionName() );
00351 if ( range != analyses_.emptyRange() ) {
00352 temporary_cache.loadNext( partition, AnalysisDescriptionsV( range.begin(), range.end() ) );
00353 } else {
00354
00355
00356
00357
00358
00359 }
00360 }
00361 }
00362 }
00363
00364
00365 AnalysisDescriptionsRange anals = analyses_.emptyRange();
00366 if ( partition == "" ) {
00367 if ( !analyses_.empty() ) {
00368 anals = AnalysisDescriptionsRange( analyses_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00369 analyses_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00370 } else { anals = analyses_.emptyRange(); }
00371 } else {
00372 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00373 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00374 for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00375 anals = analyses_.find( iter->second.partitionName() );
00376 }
00377
00378 if ( anals != analyses_.emptyRange() ) {
00379 AnalysisDescriptionsV::const_iterator ianal = anals.begin();
00380 AnalysisDescriptionsV::const_iterator janal = anals.end();
00381 for ( ; ianal != janal; ++ianal ) { if ( *ianal ) { delete *ianal; } }
00382 } else {
00383 stringstream ss;
00384 ss << "[SiStripConfigDb::" << __func__ << "]";
00385 if ( partition == "" ) { ss << " Found no analysis descriptions in local cache!"; }
00386 else { ss << " Found no analysis descriptions in local cache for partition \"" << partition << "\"!"; }
00387 edm::LogWarning(mlConfigDb_) << ss.str();
00388 }
00389
00390
00391 analyses_ = temporary_cache;
00392
00393 }
00394
00395
00396
00397 void SiStripConfigDb::printAnalysisDescriptions( std::string partition ) {
00398
00399 std::stringstream ss;
00400 ss << "[SiStripConfigDb::" << __func__ << "]"
00401 << " Contents of AnalysisDescriptions container:" << std::endl;
00402 ss << " Number of partitions: " << analyses_.size() << std::endl;
00403
00404
00405 uint16_t cntr = 0;
00406 AnalysisDescriptions::const_iterator ianal = analyses_.begin();
00407 AnalysisDescriptions::const_iterator janal = analyses_.end();
00408 for ( ; ianal != janal; ++ianal ) {
00409
00410 cntr++;
00411 if ( partition == "" || partition == ianal->first ) {
00412
00413 ss << " Partition number : " << cntr << " (out of " << analyses_.size() << ")" << std::endl;
00414 ss << " Partition name : \"" << ianal->first << "\"" << std::endl;
00415 ss << " Num of analyses : " << ianal->second.size() << std::endl;
00416
00417
00418 std::map< uint32_t, vector<uint32_t> > analyses;
00419 AnalysisDescriptionsV::const_iterator iter = ianal->second.begin();
00420 AnalysisDescriptionsV::const_iterator jter = ianal->second.end();
00421 for ( ; iter != jter; ++iter ) {
00422 if ( *iter ) {
00423 DeviceAddress addr = deviceAddress( **iter );
00424 uint32_t key = SiStripFecKey( addr.fecCrate_,
00425 addr.fecSlot_,
00426 addr.fecRing_,
00427 0,
00428 0,
00429 0,
00430 0 ).key();
00431 uint32_t data = SiStripFecKey( addr.fecCrate_,
00432 addr.fecSlot_,
00433 addr.fecRing_,
00434 addr.ccuAddr_,
00435 addr.ccuChan_,
00436 addr.lldChan_,
00437 addr.i2cAddr_ ).key();
00438 if ( find( analyses[key].begin(), analyses[key].end(), data ) == analyses[key].end() ) {
00439 analyses[key].push_back( data );
00440 }
00441 }
00442 }
00443
00444
00445 std::map< uint32_t, std::vector<uint32_t> > tmp;
00446 std::map< uint32_t, std::vector<uint32_t> >::const_iterator ii = analyses.begin();
00447 std::map< uint32_t, std::vector<uint32_t> >::const_iterator jj = analyses.end();
00448 for ( ; ii != jj; ++ii ) {
00449 std::vector<uint32_t> temp = ii->second;
00450 std::sort( temp.begin(), temp.end() );
00451 std::vector<uint32_t>::const_iterator iii = temp.begin();
00452 std::vector<uint32_t>::const_iterator jjj = temp.end();
00453 for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00454 }
00455 analyses.clear();
00456 analyses = tmp;
00457
00458
00459 std::map< uint32_t, std::vector<uint32_t> >::const_iterator ianal = analyses.begin();
00460 std::map< uint32_t, std::vector<uint32_t> >::const_iterator janal = analyses.end();
00461 for ( ; ianal != janal; ++ianal ) {
00462 SiStripFecKey key(ianal->first);
00463 ss << " Found " << std::setw(3) << ianal->second.size()
00464 << " analyses for FEC crate/slot/ring "
00465 << key.fecCrate() << "/"
00466 << key.fecSlot() << "/"
00467 << key.fecRing();
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483 ss << std::endl;
00484 }
00485
00486 }
00487
00488 }
00489
00490 LogTrace(mlConfigDb_) << ss.str();
00491
00492 }
00493
00494
00495
00496 SiStripConfigDb::DeviceAddress SiStripConfigDb::deviceAddress( const AnalysisDescription& desc ) {
00497
00498 DeviceAddress addr;
00499 try {
00500 addr.fecCrate_ = static_cast<uint16_t>( desc.getCrate() + sistrip::FEC_CRATE_OFFSET );
00501 addr.fecSlot_ = static_cast<uint16_t>( desc.getSlot() );
00502 addr.fecRing_ = static_cast<uint16_t>( desc.getRing() + sistrip::FEC_RING_OFFSET );
00503 addr.ccuAddr_ = static_cast<uint16_t>( desc.getCcuAdr() );
00504 addr.ccuChan_ = static_cast<uint16_t>( desc.getCcuChan() );
00505 addr.lldChan_ = static_cast<uint16_t>( SiStripFecKey::lldChan( desc.getI2cAddr() ) );
00506 addr.i2cAddr_ = static_cast<uint16_t>( desc.getI2cAddr() );
00507 addr.fedId_ = static_cast<uint16_t>( desc.getFedId() );
00508 addr.feUnit_ = static_cast<uint16_t>( desc.getFeUnit() );
00509 addr.feChan_ = static_cast<uint16_t>( desc.getFeChan() );
00510 } catch (...) { handleException( __func__ ); }
00511
00512 return addr;
00513 }
00514
00515
00516
00517 std::string SiStripConfigDb::analysisType( AnalysisType analysis_type ) const {
00518 if ( analysis_type == AnalysisDescription::T_ANALYSIS_FASTFEDCABLING ) { return "FAST_CABLING"; }
00519 else if ( analysis_type == AnalysisDescription::T_ANALYSIS_TIMING ) { return "APV_TIMING"; }
00520 else if ( analysis_type == AnalysisDescription::T_ANALYSIS_OPTOSCAN ) { return "OPTO_SCAN"; }
00521 else if ( analysis_type == AnalysisDescription::T_ANALYSIS_PEDESTALS ) { return "PEDESTALS"; }
00522 else if ( analysis_type == AnalysisDescription::T_ANALYSIS_APVLATENCY ) { return "APV_LATENCY"; }
00523 else if ( analysis_type == AnalysisDescription::T_ANALYSIS_FINEDELAY ) { return "FINE_DELAY"; }
00524 else if ( analysis_type == AnalysisDescription::T_ANALYSIS_CALIBRATION ) { return "CALIBRATION"; }
00525 else if ( analysis_type == AnalysisDescription::T_UNKNOWN ) { return "UNKNOWN ANALYSIS TYPE"; }
00526 else { return "UNDEFINED ANALYSIS TYPE"; }
00527 }
00528
00529 #endif // USING_NEW_DATABASE_MODEL