CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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,
20  sistrip::APV_LATENCY ),
22  sistrip::APV_LATENCY ),
23  SamplingHistograms( pset.getParameter<edm::ParameterSet>("LatencyParameters"),
24  bei,
25  sistrip::APV_LATENCY )
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().size() || !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 = NULL;
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  anal = dynamic_cast<SamplingAnalysis*>( it->second );
141  latency = uint16_t(ceil(anal->maximum()/(-25.)))>latency ? uint16_t(ceil(anal->maximum()/(-25.))) : latency;
142  }
143  for( CommissioningHistograms::Analysis it = data().begin(); it!=data().end();++it) {
144  if(dynamic_cast<SamplingAnalysis*>( it->second ) &&
145  dynamic_cast<SamplingAnalysis*>( it->second )->granularity()==sistrip::PARTITION )
146  anal = dynamic_cast<SamplingAnalysis*>( it->second );
147  shift[SiStripFecKey(anal->fecKey()).fecCrate()] = anal->maximum()-(latency*(-25));
148  }
149  if(!perPartition_) {
150  latency = globalLatency;
151  for(int i=0;i<5;i++) shift[i] = globalShift;
152  }
153 
154  // Take into account the minimum coarse delay to bring the coarse delay down
155  // the same quantity is subtracted to the coarse delay of each APV
156  latency -= minCoarseDelay;
157 
158  // Iterate through devices and update device descriptions
159  uint16_t updatedAPV = 0;
160  uint16_t updatedPLL = 0;
161  std::vector<SiStripFecKey> invalid;
162  for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
163  // Check device type
164  if ( (*idevice)->getDeviceType() != APV25 ) { continue; }
165  // Cast to retrieve appropriate description object
166  apvDescription* desc = dynamic_cast<apvDescription*>( *idevice );
167  if ( !desc ) { continue; }
168  // Retrieve device addresses from device description
169  const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
170  // Do it!
171  std::stringstream ss;
172  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
173  << " Updating latency APV settings for crate/FEC/slot/ring/CCU/i2cAddr "
174  << addr.fecCrate_ << "/"
175  << addr.fecSlot_ << "/"
176  << addr.fecRing_ << "/"
177  << addr.ccuAddr_ << "/"
178  << addr.ccuChan_ << "/"
179  << addr.i2cAddr_
180  << " from "
181  << static_cast<uint16_t>(desc->getLatency());
182  desc->setLatency(latency);
183  ss << " to "
184  << static_cast<uint16_t>(desc->getLatency());
185  LogTrace(mlDqmClient_) << ss.str();
186  updatedAPV++;
187  }
188 
189  // Change also the PLL delay
190  for ( idevice = devices.begin(); idevice != devices.end(); idevice++ ) {
191  // Check device type
192  if ( (*idevice)->getDeviceType() != PLL ) { continue; }
193  // Cast to retrieve appropriate description object
194  pllDescription* desc = dynamic_cast<pllDescription*>( *idevice );
195  if ( !desc ) { continue; }
196  if ( desc->getDelayCoarse() >= 15 ) { continue; }
197  // Retrieve device addresses from device description
198  const SiStripConfigDb::DeviceAddress& addr = db()->deviceAddress(*desc);
199  // Construct key from device description
200  uint32_t fec_key = SiStripFecKey( addr.fecCrate_,
201  addr.fecSlot_,
202  addr.fecRing_,
203  addr.ccuAddr_,
204  addr.ccuChan_,
205  0 ).key();
206  SiStripFecKey fec_path = SiStripFecKey( fec_key );
207  // Do it!
208  float delay = desc->getDelayCoarse()*25+desc->getDelayFine()*25./24. + shift[addr.fecCrate_];
209  int delayCoarse = int(delay/25);
210  int delayFine = int(round((delay-25*delayCoarse)*24./25.));
211  if(delayFine==24) { delayFine=0; ++delayCoarse; }
212  delayCoarse -= minCoarseDelay;
213  // maximum coarse setting
214  if ( delayCoarse > 15 ) { invalid.push_back(fec_key); delayCoarse = sistrip::invalid_; }
215  // Update PLL settings
216  if ( delayCoarse != sistrip::invalid_ &&
217  delayFine != sistrip::invalid_ ) {
218  std::stringstream ss;
219  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
220  << " Updating coarse/fine PLL settings"
221  << " for Crate/FEC/slot/ring/CCU "
222  << fec_path.fecCrate() << "/"
223  << fec_path.fecSlot() << "/"
224  << fec_path.fecRing() << "/"
225  << fec_path.ccuAddr() << "/"
226  << fec_path.ccuChan()
227  << " from "
228  << static_cast<uint16_t>( desc->getDelayCoarse() ) << "/"
229  << static_cast<uint16_t>( desc->getDelayFine() );
230  desc->setDelayCoarse(delayCoarse);
231  desc->setDelayFine(delayFine);
232  updatedPLL++;
233  ss << " to "
234  << static_cast<uint16_t>( desc->getDelayCoarse() ) << "/"
235  << static_cast<uint16_t>( desc->getDelayFine() );
236  LogTrace(mlDqmClient_) << ss.str();
237  }
238  }
239 
240  // Retrieve FED ids from cabling
241  auto ids = cabling()->fedIds() ;
242 
243  // loop over the FED ids to determine min and max values of coarse delay
244  uint16_t minDelay = 256;
245  uint16_t maxDelay = 0;
246  uint16_t fedDelayCoarse = 0;
247  for ( auto ifed = feds.begin(); ifed != feds.end(); ifed++ ) {
248  // If FED id not found in list (from cabling), then continue
249  if ( find( ids.begin(), ids.end(), (*ifed)->getFedId() ) == ids.end() ) { continue; }
250  auto conns = cabling()->fedConnections((*ifed)->getFedId());
251  // loop over the connections for that FED
252  for ( auto iconn = conns.begin(); iconn != conns.end(); iconn++ ) {
253  // check that this is a tracker module
254  if(DetId(iconn->detId()).det()!=DetId::Tracker) continue;
255  // build the Fed9UAddress for that channel. Used to update the description.
256  Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
257  // retreive the current value for the delays
258  fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
259  // update min and max
260  minDelay = minDelay<fedDelayCoarse ? minDelay : fedDelayCoarse;
261  maxDelay = maxDelay>fedDelayCoarse ? maxDelay : fedDelayCoarse;
262  }
263  }
264 
265  // compute the FED coarse global offset
266  int offset = (10-minDelay)*25; // try to ensure 10BX room for later fine delay scan
267  if(maxDelay+(offset/25)>MAXFEDCOARSE) offset = (MAXFEDCOARSE-maxDelay)*25; // otherwise, take the largest possible
268 
269  // loop over the FED ids
270  for ( SiStripConfigDb::FedDescriptionsV::const_iterator ifed = feds.begin(); ifed != feds.end(); ifed++ ) {
271  // If FED id not found in list (from cabling), then continue
272  if ( find( ids.begin(), ids.end(), (*ifed)->getFedId() ) == ids.end() ) { continue; }
273  auto conns = cabling()->fedConnections((*ifed)->getFedId());
274  // loop over the connections for that FED
275  for ( auto iconn = conns.begin(); iconn != conns.end(); iconn++ ) {
276  // check that this is a tracker module
277  if(DetId(iconn->detId()).det()!=DetId::Tracker) continue;
278  // build the Fed9UAddress for that channel. Used to update the description.
279  Fed9U::Fed9UAddress fedChannel = Fed9U::Fed9UAddress(iconn->fedCh());
280  // retreive the current value for the delays
281  int fedDelayCoarse = (*ifed)->getCoarseDelay(fedChannel);
282  int fedDelayFine = (*ifed)->getFineDelay(fedChannel);
283  // compute the FED delay
284  // this is done by substracting the best (PLL) delay to the present value (from the db)
285  int fedDelay = int(fedDelayCoarse*25. - fedDelayFine*24./25. - round(shift[iconn->fecCrate()]) + offset);
286  fedDelayCoarse = (fedDelay/25)+1;
287  fedDelayFine = fedDelayCoarse*25-fedDelay;
288  if(fedDelayFine==25) { fedDelayFine = 0; --fedDelayCoarse; }
289  // update the FED delay
290  std::stringstream ss;
291  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
292  << " Updating the FED delay"
293  << " for loop FED id/ch "
294  << (*ifed)->getFedId() << "/" << iconn->fedCh()
295  << " from "
296  << (*ifed)->getCoarseDelay( fedChannel) << "/" << (*ifed)->getFineDelay( fedChannel)
297  << " to ";
298  (*ifed)->setDelay(fedChannel, fedDelayCoarse, fedDelayFine);
299  ss << (*ifed)->getCoarseDelay(fedChannel) << "/" << (*ifed)->getFineDelay( fedChannel);
300  LogTrace(mlDqmClient_) << ss.str();
301  }
302  }
303 
304  // Summary output
306  << "[LatencyHistosUsingDb::" << __func__ << "]"
307  << " Updated FED delays for " << ids.size() << " FEDs!";
308 
309  // Check if invalid settings were found
310  if ( !invalid.empty() ) {
311  std::stringstream ss;
312  ss << "[LatencyHistosUsingDb::" << __func__ << "]"
313  << " Found PLL coarse setting of 15"
314  << " (not allowed!) for following channels"
315  << " (Crate/FEC/slot/ring/CCU/LLD): ";
316  std::vector<SiStripFecKey>::iterator ikey = invalid.begin();
317  std::vector<SiStripFecKey>::iterator jkey = invalid.end();
318  for ( ; ikey != jkey; ++ikey ) {
319  ss << ikey->fecCrate() << "/"
320  << ikey->fecSlot() << "/"
321  << ikey->fecRing() << "/"
322  << ikey->ccuAddr() << "/"
323  << ikey->ccuChan() << ", ";
324  }
325  edm::LogWarning(mlDqmClient_) << ss.str();
326  return false;
327  }
328 
329  // Summary output
331  << "[LatencyHistosUsingDb::" << __func__ << "] "
332  << "Updated settings for " << updatedAPV << " APV devices and " << updatedPLL << " PLL devices.";
333  return true;
334 }
335 
336 // -----------------------------------------------------------------------------
339  Analysis analysis ) {
340 
341  SamplingAnalysis* anal = dynamic_cast<SamplingAnalysis*>( analysis->second );
342  if ( !anal ) { return; }
343 
344  SiStripFecKey fec_key( anal->fecKey() ); //@@ analysis->first
345  SiStripFedKey fed_key( anal->fedKey() );
346 
347  uint16_t latency = static_cast<uint16_t>( ( anal->maximum() / (-25.) ) + 0.5 );
348 
349  ApvLatencyAnalysisDescription* tmp;
350  tmp = new ApvLatencyAnalysisDescription( latency,
351  0,
352  0,
353  0,
354  0,
355  0,
356  0,
357  db()->dbParams().partitions().begin()->second.partitionName(),
358  db()->dbParams().partitions().begin()->second.runNumber(),
359  anal->isValid(),
360  "",
361  fed_key.fedId(),
362  fed_key.feUnit(),
363  fed_key.feChan(),
364  fed_key.fedApv() );
365 
366  // Add comments
367  typedef std::vector<std::string> Strings;
368  Strings errors = anal->getErrorCodes();
369  Strings::const_iterator istr = errors.begin();
370  Strings::const_iterator jstr = errors.end();
371  for ( ; istr != jstr; ++istr ) { tmp->addComments( *istr ); }
372 
373  // Store description
374  desc.push_back( tmp );
375 
376 }
377 
379 {
380  perPartition_ = this->pset().getParameter<bool>("OptimizePerPartition");
381 }
382 
virtual void uploadConfigurations()
T getParameter(std::string const &) const
int i
Definition: DBlmapReader.cc:9
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
virtual void configure(const edm::ParameterSet &, const edm::EventSetup &)
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)
tuple db
Definition: EcalCondDB.py:151
FedDescriptions::range FedDescriptionsRange
static const char mlDqmClient_[]
#define NULL
Definition: scimark2.h:8
const uint16_t & fecSlot() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
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
const SiStripDbParams & dbParams() const
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
void create(SiStripConfigDb::AnalysisDescriptionsV &, Analysis)
std::vector< AnalysisDescription * > AnalysisDescriptionsV
static unsigned int const shift
const uint16_t & ccuChan() const
const float & maximum() const
SiStripConfigDb *const db() const
DeviceDescriptionsRange getDeviceDescriptions(std::string partition="")
void uploadDeviceDescriptions(std::string partition="")
SiStripFedCabling *const cabling() const
const VString & getErrorCodes() const
tuple size
Write out results.