00001
00002
00003 #include "OnlineDB/SiStripConfigDb/interface/SiStripConfigDb.h"
00004 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00005 #include "CondFormats/SiStripObjects/interface/FedChannelConnection.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007
00008 using namespace std;
00009 using namespace sistrip;
00010
00011
00012
00013 SiStripConfigDb::FedDescriptionsRange SiStripConfigDb::getFedDescriptions( std::string partition ) {
00014
00015
00016 if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00017 ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
00018 return feds_.emptyRange();
00019 }
00020
00021 try {
00022
00023 if ( !dbParams_.usingDbCache() ) {
00024
00025 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00026 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00027 for ( ; iter != jter; ++iter ) {
00028
00029 if ( partition == "" || partition == iter->second.partitionName() ) {
00030
00031 if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
00032
00033 FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00034 if ( range == feds_.emptyRange() ) {
00035
00036
00037 deviceFactory(__func__)->setUsingStrips( usingStrips_ );
00038 int16_t major = iter->second.fedVersion().first;
00039 int16_t minor = iter->second.fedVersion().second;
00040 if ( iter->second.fedVersion().first == 0 &&
00041 iter->second.fedVersion().second == 0 ) {
00042 major = -1;
00043 minor = -1;
00044 }
00045
00046
00047 FedDescriptionsV tmp1;
00048 tmp1 = *( deviceFactory(__func__)->getFed9UDescriptions( iter->second.partitionName(),
00049 major,
00050 minor ) );
00051
00052
00053 FedDescriptionsV tmp2;
00054 #ifdef USING_NEW_DATABASE_MODEL
00055 Fed9U::Fed9UDeviceFactory::vectorCopy( tmp2, tmp1 );
00056 #else
00057 tmp2 = tmp1;
00058 #endif
00059
00060
00061 feds_.loadNext( iter->second.partitionName(), tmp2 );
00062
00063
00064 FedDescriptionsRange feds = feds_.find( iter->second.partitionName() );
00065 std::stringstream ss;
00066 ss << "[SiStripConfigDb::" << __func__ << "]"
00067 << " Downloaded " << feds.size()
00068 << " FED descriptions to local cache for partition \""
00069 << iter->second.partitionName() << "\"" << std::endl;
00070 ss << "[SiStripConfigDb::" << __func__ << "]"
00071 << " Cache holds FED descriptions for "
00072 << feds_.size() << " partitions.";
00073 LogTrace(mlConfigDb_) << ss.str();
00074
00075 }
00076
00077 }
00078
00079 }
00080
00081 } else {
00082
00083 #ifdef USING_NEW_DATABASE_MODEL
00084
00085 FedDescriptionsV* tmp1 = databaseCache(__func__)->getFed9UDescriptions();
00086
00087 if ( tmp1 ) {
00088
00089
00090 FedDescriptionsV tmp2;
00091 Fed9U::Fed9UDeviceFactory::vectorCopy( tmp2, *tmp1 );
00092
00093
00094 feds_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
00095
00096 } else {
00097 edm::LogWarning(mlConfigDb_)
00098 << "[SiStripConfigDb::" << __func__ << "]"
00099 << " NULL pointer to FED descriptions vector!";
00100 }
00101
00102 #endif
00103
00104 }
00105
00106 } catch (... ) { handleException( __func__ ); }
00107
00108
00109 uint16_t np = 0;
00110 uint16_t nc = 0;
00111 FedDescriptionsRange feds;
00112 if ( partition != "" ) {
00113 feds = feds_.find( partition );
00114 np = 1;
00115 nc = feds.size();
00116 } else {
00117 if ( !feds_.empty() ) {
00118 feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00119 feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00120 } else { feds = feds_.emptyRange(); }
00121 np = feds_.size();
00122 nc = feds.size();
00123 }
00124
00125 stringstream ss;
00126 ss << "[SiStripConfigDb::" << __func__ << "]"
00127 << " Found " << nc << " FED descriptions";
00128 if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputFedXmlFiles().size() << " 'fed.xml' file(s)"; }
00129 else { if ( !dbParams_.usingDbCache() ) { ss << " in " << np << " database partition(s)"; }
00130 else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
00131 if ( feds_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
00132 else { LogTrace(mlConfigDb_) << ss.str(); }
00133
00134 return feds;
00135
00136 }
00137
00138
00139
00140 void SiStripConfigDb::addFedDescriptions( std::string partition, FedDescriptionsV& feds ) {
00141
00142 if ( !deviceFactory(__func__) ) { return; }
00143
00144 if ( partition.empty() ) {
00145 stringstream ss;
00146 ss << "[SiStripConfigDb::" << __func__ << "]"
00147 << " Partition string is empty,"
00148 << " therefore cannot add FED descriptions to local cache!";
00149 edm::LogWarning(mlConfigDb_) << ss.str();
00150 return;
00151 }
00152
00153 if ( feds.empty() ) {
00154 stringstream ss;
00155 ss << "[SiStripConfigDb::" << __func__ << "]"
00156 << " Vector of FED descriptions is empty,"
00157 << " therefore cannot add FED descriptions to local cache!";
00158 edm::LogWarning(mlConfigDb_) << ss.str();
00159 return;
00160 }
00161
00162 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00163 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00164 for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00165 if ( iter == dbParams_.partitions().end() ) {
00166 stringstream ss;
00167 ss << "[SiStripConfigDb::" << __func__ << "]"
00168 << " Partition \"" << partition
00169 << "\" not found in partition list, "
00170 << " therefore cannot add FED descriptions!";
00171 edm::LogWarning(mlConfigDb_) << ss.str();
00172 return;
00173 }
00174
00175 FedDescriptionsRange range = feds_.find( partition );
00176 if ( range == feds_.emptyRange() ) {
00177
00178
00179 FedDescriptionsV tmp;
00180 #ifdef USING_NEW_DATABASE_MODEL
00181 Fed9U::Fed9UDeviceFactory::vectorCopy( tmp, feds );
00182 #else
00183 tmp = feds;
00184 #endif
00185
00186
00187 feds_.loadNext( partition, tmp );
00188
00189
00190 std::stringstream ss;
00191 ss << "[SiStripConfigDb::" << __func__ << "]"
00192 << " Added " << feds.size()
00193 << " FED descriptions to local cache for partition \""
00194 << iter->second.partitionName() << "\"" << std::endl;
00195 ss << "[SiStripConfigDb::" << __func__ << "]"
00196 << " Cache holds FED descriptions for "
00197 << feds_.size() << " partitions.";
00198 LogTrace(mlConfigDb_) << ss.str();
00199
00200 } else {
00201 stringstream ss;
00202 ss << "[SiStripConfigDb::" << __func__ << "]"
00203 << " Partition \"" << partition
00204 << "\" already found in local cache, "
00205 << " therefore cannot add new FED descriptions!";
00206 edm::LogWarning(mlConfigDb_) << ss.str();
00207 return;
00208 }
00209
00210 }
00211
00212
00213
00214 void SiStripConfigDb::uploadFedDescriptions( std::string partition ) {
00215
00216 if ( dbParams_.usingDbCache() ) {
00217 edm::LogWarning(mlConfigDb_)
00218 << "[SiStripConfigDb::" << __func__ << "]"
00219 << " Using database cache! No uploads allowed!";
00220 return;
00221 }
00222
00223 if ( !deviceFactory(__func__) ) { return; }
00224
00225 if ( feds_.empty() ) {
00226 stringstream ss;
00227 ss << "[SiStripConfigDb::" << __func__ << "]"
00228 << " Found no cached FED descriptions, therefore no upload!";
00229 edm::LogWarning(mlConfigDb_) << ss.str();
00230 return;
00231 }
00232
00233 try {
00234
00235 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00236 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00237 for ( ; iter != jter; ++iter ) {
00238
00239 if ( partition == "" || partition == iter->second.partitionName() ) {
00240
00241 FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00242 if ( range != feds_.emptyRange() ) {
00243
00244 FedDescriptionsV feds( range.begin(), range.end() );
00245
00246 deviceFactory(__func__)->setFed9UDescriptions( feds,
00247 iter->second.partitionName(),
00248 (uint16_t*)(&iter->second.fedVersion().first),
00249 (uint16_t*)(&iter->second.fedVersion().second),
00250 1 );
00251
00252
00253 std::stringstream ss;
00254 ss << "[SiStripConfigDb::" << __func__ << "]"
00255 << " Uploaded " << feds.size()
00256 << " FED descriptions to database for partition \""
00257 << iter->second.partitionName() << "\"";
00258 LogTrace(mlConfigDb_) << ss.str();
00259
00260 } else {
00261 stringstream ss;
00262 ss << "[SiStripConfigDb::" << __func__ << "]"
00263 << " Vector of FED descriptions is empty for partition \""
00264 << iter->second.partitionName()
00265 << "\", therefore aborting upload for this partition!";
00266 edm::LogWarning(mlConfigDb_) << ss.str();
00267 continue;
00268 }
00269
00270 } else {
00271
00272
00273
00274
00275
00276
00277
00278 }
00279
00280 }
00281
00282 } catch (...) { handleException( __func__ ); }
00283
00284 allowCalibUpload_ = true;
00285
00286 }
00287
00288
00289
00290 void SiStripConfigDb::clearFedDescriptions( std::string partition ) {
00291 LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
00292
00293 if ( feds_.empty() ) {
00294 stringstream ss;
00295 ss << "[SiStripConfigDb::" << __func__ << "]"
00296 << " Found no cached FED descriptions!";
00297
00298 return;
00299 }
00300
00301
00302 FedDescriptions temporary_cache;
00303 if ( partition == "" ) { temporary_cache = FedDescriptions(); }
00304 else {
00305 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00306 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00307 for ( ; iter != jter; ++iter ) {
00308 if ( partition != iter->second.partitionName() ) {
00309 FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00310 if ( range != feds_.emptyRange() ) {
00311 temporary_cache.loadNext( partition, FedDescriptionsV( range.begin(), range.end() ) );
00312 }
00313 } else {
00314 FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
00315 if ( range != feds_.emptyRange() ) {
00316 LogTrace(mlConfigDb_)
00317 << "[SiStripConfigDb::" << __func__ << "]"
00318 << " Deleting FED descriptions for partition \""
00319 << iter->second.partitionName()
00320 << "\" from local cache...";
00321 }
00322 }
00323 }
00324 }
00325
00326
00327 FedDescriptionsRange feds;
00328 if ( partition == "" ) {
00329 if ( !feds_.empty() ) {
00330 feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
00331 feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
00332 } else { feds = feds_.emptyRange(); }
00333 } else {
00334 SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
00335 SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
00336 for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
00337 feds = feds_.find( iter->second.partitionName() );
00338 }
00339
00340 if ( feds != feds_.emptyRange() ) {
00341
00342 #ifdef USING_NEW_DATABASE_MODEL
00343 FedDescriptionsV::const_iterator ifed = feds.begin();
00344 FedDescriptionsV::const_iterator jfed = feds.end();
00345 for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
00346 #endif
00347
00348 } else {
00349 stringstream ss;
00350 ss << "[SiStripConfigDb::" << __func__ << "]";
00351 if ( partition == "" ) { ss << " Found no FED descriptions in local cache!"; }
00352 else { ss << " Found no FED descriptions in local cache for partition \"" << partition << "\"!"; }
00353 edm::LogWarning(mlConfigDb_) << ss.str();
00354 }
00355
00356
00357 feds_ = temporary_cache;
00358
00359 }
00360
00361
00362
00363 void SiStripConfigDb::printFedDescriptions( std::string partition ) {
00364
00365 std::stringstream ss;
00366 ss << "[SiStripConfigDb::" << __func__ << "]"
00367 << " Contents of FedDescriptions container:" << std::endl;
00368 ss << " Number of partitions: " << feds_.size() << std::endl;
00369
00370
00371 uint16_t cntr = 0;
00372 FedDescriptions::const_iterator iconn = feds_.begin();
00373 FedDescriptions::const_iterator jconn = feds_.end();
00374 for ( ; iconn != jconn; ++iconn ) {
00375
00376 cntr++;
00377 if ( partition == "" || partition == iconn->first ) {
00378
00379 ss << " Partition number : " << cntr << " (out of " << feds_.size() << ")" << std::endl;
00380 ss << " Partition name : \"" << iconn->first << "\"" << std::endl;
00381 ss << " Num of FED ids : " << iconn->second.size() << std::endl;
00382
00383
00384 std::map< uint16_t, vector<uint16_t> > feds;
00385 FedDescriptionsV::const_iterator iter = iconn->second.begin();
00386 FedDescriptionsV::const_iterator jter = iconn->second.end();
00387 for ( ; iter != jter; ++iter ) {
00388 if ( *iter ) {
00389 uint16_t key = (*iter)->getCrateNumber();
00390 uint16_t data = (*iter)->getFedId();
00391 if ( find( feds[key].begin(), feds[key].end(), data ) == feds[key].end() ) {
00392 feds[key].push_back( data );
00393 }
00394 }
00395 }
00396
00397
00398 std::map< uint16_t, std::vector<uint16_t> > tmp;
00399 std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
00400 std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
00401 for ( ; ii != jj; ++ii ) {
00402 std::vector<uint16_t> temp = ii->second;
00403 std::sort( temp.begin(), temp.end() );
00404 std::vector<uint16_t>::const_iterator iii = temp.begin();
00405 std::vector<uint16_t>::const_iterator jjj = temp.end();
00406 for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
00407 }
00408 feds.clear();
00409 feds = tmp;
00410
00411
00412 std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
00413 std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
00414 for ( ; ifed != jfed; ++ifed ) {
00415 ss << " Found " << std::setw(2) << ifed->second.size()
00416 << " FED ids for crate number " << std::setw(2) << ifed->first << " : ";
00417 if ( !ifed->second.empty() ) {
00418 uint16_t first = ifed->second.front();
00419 uint16_t last = ifed->second.front();
00420 std::vector<uint16_t>::const_iterator icrate = ifed->second.begin();
00421 std::vector<uint16_t>::const_iterator jcrate = ifed->second.end();
00422 for ( ; icrate != jcrate; ++icrate ) {
00423 if ( icrate != ifed->second.begin() ) {
00424 if ( *icrate != last+1 ) {
00425 ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
00426 if ( icrate != ifed->second.end() ) { first = *(icrate+1); }
00427 }
00428 }
00429 last = *icrate;
00430 }
00431 if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
00432 ss << std::endl;
00433 }
00434 }
00435
00436 }
00437
00438 }
00439
00440 LogTrace(mlConfigDb_) << ss.str();
00441
00442 }
00443
00444
00446 SiStripConfigDb::FedIdsRange SiStripConfigDb::getFedIds( std::string partition ) {
00447
00448 fedIds_.clear();
00449
00450 if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
00451 ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
00452 return FedIdsRange( fedIds_.end(), fedIds_.end() );
00453 }
00454
00455 try {
00456
00457
00458 bool using_strips = usingStrips_;
00459 if ( factory_ ) { factory_->setUsingStrips( false ); }
00460 FedDescriptionsRange feds = getFedDescriptions( partition );
00461 if ( factory_ ) { factory_->setUsingStrips( using_strips ); }
00462
00463 if ( !feds.empty() ) {
00464 FedDescriptionsV::const_iterator ifed = feds.begin();
00465 FedDescriptionsV::const_iterator jfed = feds.end();
00466 for ( ; ifed != jfed; ++ifed ) {
00467 if ( *ifed ) { fedIds_.push_back( (*ifed)->getFedId() ); }
00468 else {
00469 edm::LogError(mlCabling_)
00470 << "[SiStripConfigDb::" << __func__ << "]"
00471 << " NULL pointer to FedDescription!";
00472 continue;
00473 }
00474 }
00475 }
00476
00477 } catch (...) { handleException( __func__ ); }
00478
00479 if ( fedIds_.empty() ) {
00480 edm::LogWarning(mlConfigDb_)
00481 << "[SiStripConfigDb::" << __func__ << "]"
00482 << " No FED ids found!";
00483 }
00484
00485 return FedIdsRange( fedIds_.begin(), fedIds_.end() );
00486
00487 }
00488