00001
00002
00003 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00004 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00005 #include "DQM/SiStripCommissioningDbClients/interface/FineDelayHistosUsingDb.h"
00006 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
00007 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
00008 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00011 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
00012 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00013 #include <Geometry/CommonTopologies/interface/Topology.h>
00014 #include <CondFormats/DataRecord/interface/SiStripFedCablingRcd.h>
00015 #include <CondFormats/SiStripObjects/interface/SiStripFedCabling.h>
00016 #include <CondFormats/SiStripObjects/interface/FedChannelConnection.h>
00017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00018 #include <iostream>
00019
00020 using namespace sistrip;
00021
00022
00024 FineDelayHistosUsingDb::FineDelayHistosUsingDb( DQMOldReceiver* mui,
00025 SiStripConfigDb* const db )
00026 : CommissioningHistosUsingDb( db ),
00027 SamplingHistograms( mui, FINE_DELAY ),
00028 tracker_(0)
00029 {
00030 LogTrace(mlDqmClient_)
00031 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00032 << " Constructing object...";
00033 delays_.clear();
00034 }
00035
00036
00038 FineDelayHistosUsingDb::FineDelayHistosUsingDb( DQMStore* bei,
00039 SiStripConfigDb* const db )
00040 : CommissioningHistosUsingDb( db ),
00041 SamplingHistograms( bei, FINE_DELAY ),
00042 tracker_(0)
00043 {
00044 LogTrace(mlDqmClient_)
00045 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00046 << " Constructing object...";
00047 delays_.clear();
00048 }
00049
00050
00052 FineDelayHistosUsingDb::~FineDelayHistosUsingDb() {
00053 LogTrace(mlDqmClient_)
00054 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00055 << " Destructing object...";
00056 }
00057
00058
00060 void FineDelayHistosUsingDb::configure( const edm::ParameterSet& pset,
00061 const edm::EventSetup& setup ) {
00062
00063 edm::ESHandle<TrackerGeometry> estracker;
00064 setup.get<TrackerDigiGeometryRecord>().get(estracker);
00065 tracker_=&(* estracker);
00066 SamplingHistograms::configure(pset,setup);
00067 }
00068
00069
00071 void FineDelayHistosUsingDb::uploadConfigurations() {
00072
00073 if ( !db() ) {
00074 edm::LogWarning(mlDqmClient_)
00075 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00076 << " NULL pointer to SiStripConfigDb interface!"
00077 << " Aborting upload...";
00078 return;
00079 }
00080
00081
00082 db()->clearDeviceDescriptions();
00083 SiStripConfigDb::DeviceDescriptionsRange devices = db()->getDeviceDescriptions( PLL );
00084 bool upload = update( devices );
00085
00086
00087 if ( !upload ) {
00088 edm::LogWarning(mlDqmClient_)
00089 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00090 << " Found invalid PLL settings (coarse > 15)"
00091 << " Aborting update to database...";
00092 return;
00093 }
00094
00095
00096 if ( doUploadConf() ) {
00097 LogTrace(mlDqmClient_)
00098 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00099 << " Uploading PLL settings to DB...";
00100 db()->uploadDeviceDescriptions();
00101 LogTrace(mlDqmClient_)
00102 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00103 << " Upload of PLL settings to DB finished!";
00104 } else {
00105 edm::LogWarning(mlDqmClient_)
00106 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00107 << " TEST only! No PLL settings will be uploaded to DB...";
00108 }
00109
00110
00111 db()->clearFedDescriptions();
00112 SiStripConfigDb::FedDescriptionsRange feds = db()->getFedDescriptions();
00113 update( feds );
00114
00115
00116 if ( doUploadConf() ) {
00117 LogTrace(mlDqmClient_)
00118 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00119 << " Uploading FED ticker thresholds to DB...";
00120 db()->uploadFedDescriptions();
00121 LogTrace(mlDqmClient_)
00122 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00123 << " Upload of FED ticker thresholds to DB finished!";
00124 } else {
00125 edm::LogWarning(mlDqmClient_)
00126 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00127 << " TEST only! No FED ticker thresholds will be uploaded to DB...";
00128 }
00129
00130 }
00131
00132 void FineDelayHistosUsingDb::computeDelays() {
00133
00134 if(delays_.size()>0) return;
00135
00136
00137 float x = 0.;
00138 float y = 0.;
00139 float z = 0.;
00140 GlobalPoint referenceP_ = GlobalPoint(x,y,z);
00141 const double c = 30;
00142
00143
00144 float bestDelay_ = 0.;
00145 if(data().size()) {
00146 Analyses::const_iterator iter = data().begin();
00147 bestDelay_ = dynamic_cast<SamplingAnalysis*>(iter->second)->maximum();
00148 }
00149
00150
00151 std::vector<uint16_t> ids = cabling()->feds() ;
00152
00153
00154 for (std::vector<uint16_t>::const_iterator ifed = ids.begin(); ifed != ids.end(); ++ifed) {
00155 const std::vector<FedChannelConnection>& conns = cabling()->connections(*ifed);
00156
00157 for ( std::vector<FedChannelConnection>::const_iterator iconn = conns.begin(); iconn != conns.end(); iconn++ ) {
00158
00159 if(DetId(iconn->detId()).det()!=DetId::Tracker) continue;
00160
00161
00162 if(tracker_) {
00163 float dist = tracker_->idToDetUnit(DetId(iconn->detId()))->toLocal(referenceP_).mag();
00164 float tof = dist/c ;
00165
00166 int delay = int(round(bestDelay_+tof));
00167
00168 delays_[SiStripFecKey( iconn->fecCrate(),
00169 iconn->fecSlot(),
00170 iconn->fecRing(),
00171 iconn->ccuAddr(),
00172 iconn->ccuChan(), 0 ).key()] = delay;
00173 } else {
00174 edm::LogError(mlDqmClient_)
00175 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00176 << " Tracker geometry not initialized. Impossible to compute the delays.";
00177 }
00178 }
00179 }
00180 }
00181
00182
00184 bool FineDelayHistosUsingDb::update( SiStripConfigDb::DeviceDescriptionsRange devices ) {
00185
00186
00187 computeDelays();
00188
00189
00190 uint16_t updated = 0;
00191 std::vector<SiStripFecKey> invalid;
00192 SiStripConfigDb::DeviceDescriptionsV::const_iterator idevice;
00193 for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
00194
00195
00196 if ( (*idevice)->getDeviceType() != PLL ) { continue; }
00197
00198
00199 pllDescription* desc = dynamic_cast<pllDescription*>( *idevice );
00200 if ( !desc ) { continue; }
00201
00202
00203 const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
00204
00205
00206 uint32_t fec_key = SiStripFecKey( addr.fecCrate_,
00207 addr.fecSlot_,
00208 addr.fecRing_,
00209 addr.ccuAddr_,
00210 addr.ccuChan_,
00211 0 ).key();
00212 SiStripFecKey fec_path = SiStripFecKey( fec_key );
00213
00214
00215 int delay = delays_[fec_key];
00216 int delayCoarse = delay/25;
00217 int delayFine = int(round((delay-25*delayCoarse)*24./25.));
00218 if(delayFine==24) { delayFine=0; ++delayCoarse; }
00219
00220 if ( delayCoarse > 15 ) { invalid.push_back(fec_key); delayCoarse = sistrip::invalid_; }
00221
00222
00223 if ( delayCoarse != sistrip::invalid_ &&
00224 delayFine != sistrip::invalid_ ) {
00225
00226 std::stringstream ss;
00227 ss << "[FineDelayHistosUsingDb::" << __func__ << "]"
00228 << " Updating coarse/fine PLL settings"
00229 << " for Crate/FEC/slot/ring/CCU "
00230 << fec_path.fecCrate() << "/"
00231 << fec_path.fecSlot() << "/"
00232 << fec_path.fecRing() << "/"
00233 << fec_path.ccuAddr() << "/"
00234 << fec_path.ccuChan()
00235 << " from "
00236 << static_cast<uint16_t>( desc->getDelayCoarse() ) << "/"
00237 << static_cast<uint16_t>( desc->getDelayFine() );
00238 desc->setDelayCoarse(delayCoarse);
00239 desc->setDelayFine(delayFine);
00240 updated++;
00241 ss << " to "
00242 << static_cast<uint16_t>( desc->getDelayCoarse() ) << "/"
00243 << static_cast<uint16_t>( desc->getDelayFine() );
00244 LogTrace(mlDqmClient_) << ss.str();
00245
00246 } else {
00247 LogTrace(mlDqmClient_)
00248 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00249 << " Unexpected PLL delay settings for Crate/FEC/slot/ring/CCU "
00250 << fec_path.fecCrate() << "/"
00251 << fec_path.fecSlot() << "/"
00252 << fec_path.fecRing() << "/"
00253 << fec_path.ccuAddr() << "/"
00254 << fec_path.ccuChan();
00255 }
00256
00257 }
00258
00259
00260 if ( !invalid.empty() ) {
00261 std::stringstream ss;
00262 ss << "[FineDelayHistosUsingDb::" << __func__ << "]"
00263 << " Found PLL coarse setting of 15"
00264 << " (not allowed!) for following channels"
00265 << " (Crate/FEC/slot/ring/CCU/LLD): ";
00266 std::vector<SiStripFecKey>::iterator ikey = invalid.begin();
00267 std::vector<SiStripFecKey>::iterator jkey = invalid.end();
00268 for ( ; ikey != jkey; ++ikey ) {
00269 ss << ikey->fecCrate() << "/"
00270 << ikey->fecSlot() << "/"
00271 << ikey->fecRing() << "/"
00272 << ikey->ccuAddr() << "/"
00273 << ikey->ccuChan() << ", ";
00274 }
00275 edm::LogWarning(mlDqmClient_) << ss.str();
00276 return false;
00277 }
00278
00279 edm::LogVerbatim(mlDqmClient_)
00280 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00281 << " Updated PLL settings for "
00282 << updated << " modules";
00283 return true;
00284 }
00285
00286
00288 void FineDelayHistosUsingDb::update( SiStripConfigDb::FedDescriptionsRange feds ) {
00289
00290
00291 computeDelays();
00292
00293
00294 std::vector<uint16_t> ids = cabling()->feds() ;
00295
00296
00297 for ( SiStripConfigDb::FedDescriptionsV::const_iterator ifed = feds.begin(); ifed != feds.end(); ifed++ ) {
00298
00299 if ( find( ids.begin(), ids.end(), (*ifed)->getFedId() ) == ids.end() ) { continue; }
00300 const std::vector<FedChannelConnection>& conns = cabling()->connections((*ifed)->getFedId());
00301
00302 for ( std::vector<FedChannelConnection>::const_iterator iconn = conns.begin(); iconn != conns.end(); iconn++ ) {
00303
00304 if(DetId(iconn->detId()).det()!=DetId::Tracker) continue;
00305
00306 Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
00307
00308 int fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
00309 int fedDelayFine = (*ifed)->getFineDelay(fedChannel);
00310 int fedDelay = int(fedDelayCoarse*25. - fedDelayFine*24./25.);
00311
00312 int delay = delays_[SiStripFecKey( iconn->fecCrate(),
00313 iconn->fecSlot(),
00314 iconn->fecRing(),
00315 iconn->ccuAddr(),
00316 iconn->ccuChan(), 0 ).key()];
00317
00318
00319 fedDelay -= delay;
00320 fedDelayCoarse = (fedDelay/25)+1;
00321 fedDelayFine = fedDelayCoarse*25-fedDelay;
00322 if(fedDelayFine==25) { fedDelayFine = 0; --fedDelayCoarse; }
00323
00324 std::stringstream ss;
00325 ss << "[FineDelayHistosUsingDb::" << __func__ << "]"
00326 << " Updating the FED delay"
00327 << " for loop FED id/ch "
00328 << (*ifed)->getFedId() << "/" << iconn->fedCh()
00329 << " from "
00330 << (*ifed)->getCoarseDelay( fedChannel) << "/" << (*ifed)->getFineDelay( fedChannel)
00331 << " to ";
00332 (*ifed)->setDelay(fedChannel, fedDelayCoarse, fedDelayFine);
00333 ss << (*ifed)->getCoarseDelay(fedChannel) << "/" << (*ifed)->getFineDelay( fedChannel) << std::endl;
00334 LogTrace(mlDqmClient_) << ss.str();
00335 }
00336 }
00337
00338 edm::LogVerbatim(mlDqmClient_)
00339 << "[FineDelayHistosUsingDb::" << __func__ << "]"
00340 << " Updated FED delay for " << ids.size() << " FEDs!";
00341
00342 }
00343
00344
00346 void FineDelayHistosUsingDb::create( SiStripConfigDb::AnalysisDescriptionsV& desc,
00347 Analysis analysis ) {
00348
00349 #ifdef USING_NEW_DATABASE_MODEL
00350
00351 SamplingAnalysis* anal = dynamic_cast<SamplingAnalysis*>( analysis->second );
00352 if ( !anal ) { return; }
00353
00354 SiStripFecKey fec_key( anal->fecKey() );
00355 SiStripFedKey fed_key( anal->fedKey() );
00356
00357 FineDelayAnalysisDescription* tmp;
00358 tmp = new FineDelayAnalysisDescription( anal->maximum(),
00359 anal->error(),
00360 0,
00361 0,
00362 0,
00363 0,
00364 0,
00365 0,
00366 db()->dbParams().partitions().begin()->second.partitionName(),
00367 db()->dbParams().partitions().begin()->second.runNumber(),
00368 anal->isValid(),
00369 "",
00370 fed_key.fedId(),
00371 fed_key.feUnit(),
00372 fed_key.feChan(),
00373 fed_key.fedApv() );
00374
00375
00376 typedef std::vector<std::string> Strings;
00377 Strings errors = anal->getErrorCodes();
00378 Strings::const_iterator istr = errors.begin();
00379 Strings::const_iterator jstr = errors.end();
00380 for ( ; istr != jstr; ++istr ) { tmp->addComments( *istr ); }
00381
00382
00383 desc.push_back( tmp );
00384
00385 #endif
00386
00387 }