CMS 3D CMS Logo

LatencyHistosUsingDb.cc
Go to the documentation of this file.
1 
7 #include <iostream>
8 
9 #define MAXFEDCOARSE 15
10 
11 using namespace sistrip;
12 
13 // -----------------------------------------------------------------------------
16  DQMStore* bei,
17  SiStripConfigDb* const db )
18  : CommissioningHistograms( pset.getParameter<edm::ParameterSet>("LatencyParameters"),
19  bei,
23  SamplingHistograms( pset.getParameter<edm::ParameterSet>("LatencyParameters"),
24  bei,
26 {
28  << "[LatencyHistosUsingDb::" << __func__ << "]"
29  << " Constructing object...";
30 }
31 
32 // -----------------------------------------------------------------------------
36  << "[LatencyHistosUsingDb::" << __func__ << "]"
37  << " Destructing object...";
38 }
39 
40 // -----------------------------------------------------------------------------
43 
44  if ( !db() ) {
46  << "[LatencyHistosUsingDb::" << __func__ << "]"
47  << " NULL pointer to SiStripConfigDb interface!"
48  << " Aborting upload...";
49  return;
50  }
51 
54  bool upload = update( devices, feds );
55  // Check if new PLL settings are valid
56  if ( !upload ) {
58  << "[LatencyHistosUsingDb::" << __func__ << "]"
59  << " Found invalid PLL settings (coarse > 15)"
60  << " Aborting update to database...";
61  return;
62  }
63 
64  if ( doUploadConf() ) {
65  // Update APV descriptions with new Latency settings
67  << "[LatencyHistosUsingDb::" << __func__ << "]"
68  << " Uploading APV settings to DB...";
71  << "[LatencyHistosUsingDb::" << __func__ << "]"
72  << " Upload of APV settings to DB finished!";
73  // Update FED descriptions
75  << "[LatencyHistosUsingDb::" << __func__ << "]"
76  << " Uploading FED delays to DB...";
79  << "[LatencyHistosUsingDb::" << __func__ << "]"
80  << " Upload of FED delays to DB finished!";
81  } else {
83  << "[LatencyHistosUsingDb::" << __func__ << "]"
84  << " TEST only! No APV settings will be uploaded to DB...";
85  }
86 
87 }
88 
89 // -----------------------------------------------------------------------------
93 
94  // Obtain the latency from the analysis object
95  if(data().empty() || !data().begin()->second->isValid() ) {
97  << "[LatencyHistosUsingDb::" << __func__ << "]"
98  << " Updated NO Latency settings. No analysis result available !" ;
99  return false;
100  }
101 
102  // Compute the minimum coarse delay
103  uint16_t minCoarseDelay = 256;
104  SiStripConfigDb::DeviceDescriptionsV::const_iterator idevice;
105  for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
106  // Check device type
107  if ( (*idevice)->getDeviceType() == PLL ) {
108  // Cast to retrieve appropriate description object
109  pllDescription* desc = dynamic_cast<pllDescription*>( *idevice );
110  if ( desc ) {
111 /*
112  // add 1 to aim at 1 and not 0 (just to avoid a special 0 value for security)
113  int delayCoarse = desc->getDelayCoarse() - 1;
114  delayCoarse = delayCoarse < 0 ? 0 : delayCoarse;
115  minCoarseDelay = minCoarseDelay < delayCoarse ? minCoarseDelay : delayCoarse;
116 */
117  int delayCoarse = desc->getDelayCoarse();
118  minCoarseDelay = minCoarseDelay < delayCoarse ? minCoarseDelay : delayCoarse;
119  }
120  }
121  }
122 
123  // Compute latency and PLL shift from the sampling measurement
124  SamplingAnalysis* anal = nullptr;
125  for( CommissioningHistograms::Analysis it = data().begin(); it!=data().end();++it) {
126  if(dynamic_cast<SamplingAnalysis*>( it->second ) &&
127  dynamic_cast<SamplingAnalysis*>( it->second )->granularity()==sistrip::TRACKER)
128  anal = dynamic_cast<SamplingAnalysis*>( it->second );
129  }
130  if(!anal) return false;
131  uint16_t globalLatency = uint16_t(ceil(anal->maximum()/(-25.)));
132  float globalShift = anal->maximum()-(globalLatency*(-25));
133 
134  // Compute latency and PLL shift per partition... this is an option
135  uint16_t latency = globalLatency;
136  float shift[5] = {0.};
137  for( CommissioningHistograms::Analysis it = data().begin(); it!=data().end();++it) {
138  if(dynamic_cast<SamplingAnalysis*>( it->second ) &&
139  dynamic_cast<SamplingAnalysis*>( it->second )->granularity()==sistrip::PARTITION )
140  {
141  anal = dynamic_cast<SamplingAnalysis*>( it->second );
142  latency = uint16_t(ceil(anal->maximum()/(-25.)))>latency ? uint16_t(ceil(anal->maximum()/(-25.))) : latency;
143  }
144  }
145  for( CommissioningHistograms::Analysis it = data().begin(); it!=data().end();++it) {
146  if(dynamic_cast<SamplingAnalysis*>( it->second ) &&
147  dynamic_cast<SamplingAnalysis*>( it->second )->granularity()==sistrip::PARTITION )
148  {
149  anal = dynamic_cast<SamplingAnalysis*>( it->second );
150  shift[SiStripFecKey(anal->fecKey()).fecCrate()] = anal->maximum()-(latency*(-25));
151  }
152  }
153  if(!perPartition_) {
154  latency = globalLatency;
155  for(int i=0;i<5;i++) shift[i] = globalShift;
156  }
157 
158  // Take into account the minimum coarse delay to bring the coarse delay down
159  // the same quantity is subtracted to the coarse delay of each APV
160  latency -= minCoarseDelay;
161 
162  // Iterate through devices and update device descriptions
163  uint16_t updatedAPV = 0;
164  uint16_t updatedPLL = 0;
165  std::vector<SiStripFecKey> invalid;
166  for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
167  // Check device type
168  if ( (*idevice)->getDeviceType() != APV25 ) { continue; }
169  // Cast to retrieve appropriate description object
170  apvDescription* desc = dynamic_cast<apvDescription*>( *idevice );
171  if ( !desc ) { continue; }
172  // Retrieve device addresses from device description
173  const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
174  // Do it!
175  std::stringstream ss;
176  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
177  << " Updating latency APV settings for crate/FEC/slot/ring/CCU/i2cAddr "
178  << addr.fecCrate_ << "/"
179  << addr.fecSlot_ << "/"
180  << addr.fecRing_ << "/"
181  << addr.ccuAddr_ << "/"
182  << addr.ccuChan_ << "/"
183  << addr.i2cAddr_
184  << " from "
185  << static_cast<uint16_t>(desc->getLatency());
186  desc->setLatency(latency);
187  ss << " to "
188  << static_cast<uint16_t>(desc->getLatency());
189  LogTrace(mlDqmClient_) << ss.str();
190  updatedAPV++;
191  }
192 
193  // Change also the PLL delay
194  for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
195  // Check device type
196  if ( (*idevice)->getDeviceType() != PLL ) { continue; }
197  // Cast to retrieve appropriate description object
198  pllDescription* desc = dynamic_cast<pllDescription*>( *idevice );
199  if ( !desc ) { continue; }
200  if ( desc->getDelayCoarse() >= 15 ) { continue; }
201  // Retrieve device addresses from device description
202  const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
203  // Construct key from device description
204  uint32_t fec_key = SiStripFecKey( addr.fecCrate_,
205  addr.fecSlot_,
206  addr.fecRing_,
207  addr.ccuAddr_,
208  addr.ccuChan_,
209  0 ).key();
210  SiStripFecKey fec_path = SiStripFecKey( fec_key );
211  // Do it!
212  float delay = desc->getDelayCoarse()*25+desc->getDelayFine()*25./24. + shift[addr.fecCrate_];
213  int delayCoarse = int(delay/25);
214  int delayFine = int(round((delay-25*delayCoarse)*24./25.));
215  if(delayFine==24) { delayFine=0; ++delayCoarse; }
216  delayCoarse -= minCoarseDelay;
217  // maximum coarse setting
218  if ( delayCoarse > 15 ) { invalid.push_back(fec_key); delayCoarse = sistrip::invalid_; }
219  // Update PLL settings
220  if ( delayCoarse != sistrip::invalid_ &&
221  delayFine != sistrip::invalid_ ) {
222  std::stringstream ss;
223  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
224  << " Updating coarse/fine PLL settings"
225  << " for Crate/FEC/slot/ring/CCU "
226  << fec_path.fecCrate() << "/"
227  << fec_path.fecSlot() << "/"
228  << fec_path.fecRing() << "/"
229  << fec_path.ccuAddr() << "/"
230  << fec_path.ccuChan()
231  << " from "
232  << static_cast<uint16_t>( desc->getDelayCoarse() ) << "/"
233  << static_cast<uint16_t>( desc->getDelayFine() );
234  desc->setDelayCoarse(delayCoarse);
235  desc->setDelayFine(delayFine);
236  updatedPLL++;
237  ss << " to "
238  << static_cast<uint16_t>( desc->getDelayCoarse() ) << "/"
239  << static_cast<uint16_t>( desc->getDelayFine() );
240  LogTrace(mlDqmClient_) << ss.str();
241  }
242  }
243 
244  // Retrieve FED ids from cabling
245  auto ids = cabling()->fedIds() ;
246 
247  // loop over the FED ids to determine min and max values of coarse delay
248  uint16_t minDelay = 256;
249  uint16_t maxDelay = 0;
250  uint16_t fedDelayCoarse = 0;
251  for ( auto ifed = feds.begin(); ifed != feds.end(); ifed++ ) {
252  // If FED id not found in list (from cabling), then continue
253  if ( find( ids.begin(), ids.end(), (*ifed)->getFedId() ) == ids.end() ) { continue; }
254  auto conns = cabling()->fedConnections((*ifed)->getFedId());
255  // loop over the connections for that FED
256  for ( auto iconn = conns.begin(); iconn != conns.end(); iconn++ ) {
257  // check that this is a tracker module
258  if(DetId(iconn->detId()).det()!=DetId::Tracker) continue;
259  // build the Fed9UAddress for that channel. Used to update the description.
260  Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
261  // retreive the current value for the delays
262  fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
263  // update min and max
264  minDelay = minDelay<fedDelayCoarse ? minDelay : fedDelayCoarse;
265  maxDelay = maxDelay>fedDelayCoarse ? maxDelay : fedDelayCoarse;
266  }
267  }
268 
269  // compute the FED coarse global offset
270  int offset = (10-minDelay)*25; // try to ensure 10BX room for later fine delay scan
271  if(maxDelay+(offset/25)>MAXFEDCOARSE) offset = (MAXFEDCOARSE-maxDelay)*25; // otherwise, take the largest possible
272 
273  // loop over the FED ids
274  for ( SiStripConfigDb::FedDescriptionsV::const_iterator ifed = feds.begin(); ifed != feds.end(); ifed++ ) {
275  // If FED id not found in list (from cabling), then continue
276  if ( find( ids.begin(), ids.end(), (*ifed)->getFedId() ) == ids.end() ) { continue; }
277  auto conns = cabling()->fedConnections((*ifed)->getFedId());
278  // loop over the connections for that FED
279  for ( auto iconn = conns.begin(); iconn != conns.end(); iconn++ ) {
280  // check that this is a tracker module
281  if(DetId(iconn->detId()).det()!=DetId::Tracker) continue;
282  // build the Fed9UAddress for that channel. Used to update the description.
283  Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
284  // retreive the current value for the delays
285  int fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
286  int fedDelayFine = (*ifed)->getFineDelay(fedChannel);
287  // compute the FED delay
288  // this is done by substracting the best (PLL) delay to the present value (from the db)
289  int fedDelay = int(fedDelayCoarse*25. - fedDelayFine*24./25. - round(shift[iconn->fecCrate()]) + offset);
290  fedDelayCoarse = (fedDelay/25)+1;
291  fedDelayFine = fedDelayCoarse*25-fedDelay;
292  if(fedDelayFine==25) { fedDelayFine = 0; --fedDelayCoarse; }
293  // update the FED delay
294  std::stringstream ss;
295  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
296  << " Updating the FED delay"
297  << " for loop FED id/ch "
298  << (*ifed)->getFedId() << "/" << iconn->fedCh()
299  << " from "
300  << (*ifed)->getCoarseDelay( fedChannel) << "/" << (*ifed)->getFineDelay( fedChannel)
301  << " to ";
302  (*ifed)->setDelay(fedChannel, fedDelayCoarse, fedDelayFine);
303  ss << (*ifed)->getCoarseDelay(fedChannel) << "/" << (*ifed)->getFineDelay( fedChannel);
304  LogTrace(mlDqmClient_) << ss.str();
305  }
306  }
307 
308  // Summary output
310  << "[LatencyHistosUsingDb::" << __func__ << "]"
311  << " Updated FED delays for " << ids.size() << " FEDs!";
312 
313  // Check if invalid settings were found
314  if ( !invalid.empty() ) {
315  std::stringstream ss;
316  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
317  << " Found PLL coarse setting of 15"
318  << " (not allowed!) for following channels"
319  << " (Crate/FEC/slot/ring/CCU/LLD): ";
320  std::vector<SiStripFecKey>::iterator ikey = invalid.begin();
321  std::vector<SiStripFecKey>::iterator jkey = invalid.end();
322  for ( ; ikey != jkey; ++ikey ) {
323  ss << ikey->fecCrate() << "/"
324  << ikey->fecSlot() << "/"
325  << ikey->fecRing() << "/"
326  << ikey->ccuAddr() << "/"
327  << ikey->ccuChan() << ", ";
328  }
329  edm::LogWarning(mlDqmClient_) << ss.str();
330  return false;
331  }
332 
333  // Summary output
335  << "[LatencyHistosUsingDb::" << __func__ << "] "
336  << "Updated settings for " << updatedAPV << " APV devices and " << updatedPLL << " PLL devices.";
337  return true;
338 }
339 
340 // -----------------------------------------------------------------------------
343  Analysis analysis ) {
344 
345  SamplingAnalysis* anal = dynamic_cast<SamplingAnalysis*>( analysis->second );
346  if ( !anal ) { return; }
347 
348  SiStripFecKey fec_key( anal->fecKey() ); //@@ analysis->first
349  SiStripFedKey fed_key( anal->fedKey() );
350 
351  uint16_t latency = static_cast<uint16_t>( ( anal->maximum() / (-25.) ) + 0.5 );
352 
353  ApvLatencyAnalysisDescription* tmp;
354  tmp = new ApvLatencyAnalysisDescription( latency,
355  0,
356  0,
357  0,
358  0,
359  0,
360  0,
361  db()->dbParams().partitions().begin()->second.partitionName(),
362  db()->dbParams().partitions().begin()->second.runNumber(),
363  anal->isValid(),
364  "",
365  fed_key.fedId(),
366  fed_key.feUnit(),
367  fed_key.feChan(),
368  fed_key.fedApv() );
369 
370  // Add comments
371  typedef std::vector<std::string> Strings;
372  Strings errors = anal->getErrorCodes();
373  Strings::const_iterator istr = errors.begin();
374  Strings::const_iterator jstr = errors.end();
375  for ( ; istr != jstr; ++istr ) { tmp->addComments( *istr ); }
376 
377  // Store description
378  desc.push_back( tmp );
379 
380 }
381 
383 {
385  perPartition_ = this->pset().getParameter<bool>("OptimizePerPartition");
386 }
387 
def upload(url, args, files)
T getParameter(std::string const &) const
Analysis for latency run.
const uint32_t & fedKey() const
const uint16_t & fecRing() const
A container class for generic run and event-related info, information required by the commissioning a...
Definition: SiStripFedKey.h:56
const edm::ParameterSet & pset() const
virtual bool isValid() const
FedDescriptionsRange getFedDescriptions(std::string partition="")
std::vector< std::string > Strings
Definition: MsgTools.h:18
bool update(SiStripConfigDb::DeviceDescriptionsRange, SiStripConfigDb::FedDescriptionsRange)
FedDescriptions::range FedDescriptionsRange
static const char mlDqmClient_[]
void uploadConfigurations() override
const uint16_t & fecSlot() const
void configure(const edm::ParameterSet &, const edm::EventSetup &) override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
sistrip classes
const uint32_t & key() const
Definition: SiStripKey.h:125
Utility class that identifies a position within the strip tracker control structure, down to the level of an APV25.
Definition: SiStripFecKey.h:45
U second(std::pair< T, U > const &p)
const_iterator_range partitions() const
LatencyHistosUsingDb(const edm::ParameterSet &pset, DQMStore *, SiStripConfigDb *const )
#define MAXFEDCOARSE
FedsConstIterRange fedIds() const
DeviceDescriptions::range DeviceDescriptionsRange
An interface class to the DeviceFactory.
const uint32_t & fecKey() const
DeviceAddress deviceAddress(const deviceDescription &)
#define LogTrace(id)
const uint16_t & fecCrate() const
Definition: DetId.h:18
void create(SiStripConfigDb::AnalysisDescriptionsV &, Analysis) override
const SiStripDbParams & dbParams() const
void configure(const edm::ParameterSet &, const edm::EventSetup &) override
void uploadFedDescriptions(std::string partition="")
const uint16_t & ccuAddr() const
static const uint16_t invalid_
Definition: Constants.h:16
ConnsConstIterRange fedConnections(uint16_t fed_id) const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
#define begin
Definition: vmac.h:30
HLT enums.
std::vector< AnalysisDescription * > AnalysisDescriptionsV
static unsigned int const shift
const uint16_t & ccuChan() const
const float & maximum() const
Definition: errors.py:1
SiStripConfigDb *const db() const
DeviceDescriptionsRange getDeviceDescriptions(std::string partition="")
void uploadDeviceDescriptions(std::string partition="")
SiStripFedCabling *const cabling() const
const VString & getErrorCodes() const
latency
hardware algo