CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DeviceDescriptions.cc
Go to the documentation of this file.
1 // Last commit: $Id: DeviceDescriptions.cc,v 1.38 2011/09/02 11:25:25 eulisse Exp $
2 
6 
7 using namespace std;
8 using namespace sistrip;
9 
10 // -----------------------------------------------------------------------------
11 //
13 
14  // Check
15  if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
16  ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
17  return devices_.emptyRange();
18  }
19 
20  try {
21 
22  if ( !dbParams_.usingDbCache() ) {
23 
24  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
25  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
26  for ( ; iter != jter; ++iter ) {
27 
28  if ( partition == "" || partition == iter->second.partitionName() ) {
29 
30  if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
31 
32  DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
33  if ( range == devices_.emptyRange() ) {
34 
35  // Retrieve conections
37  deviceFactory(__func__)->getFecDeviceDescriptions( iter->second.partitionName(),
38  tmp1,
39  iter->second.fecVersion().first,
40  iter->second.fecVersion().second,
41  //#ifdef USING_DATABASE_MASKING
42  iter->second.maskVersion().first,
43  iter->second.maskVersion().second,
44  //#endif
45  false ); //@@ do not get DISABLED devices
46 
47  // Make local copy
49  FecFactory::vectorCopyI( tmp2, tmp1, true );
50 
51  // Add to cache
52  devices_.loadNext( iter->second.partitionName(), tmp2 );
53 
54  // Some debug
55  DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
56  std::stringstream ss;
57  ss << "[SiStripConfigDb::" << __func__ << "]"
58  << " Downloaded " << range.size()
59  << " device descriptions to local cache for partition \""
60  << iter->second.partitionName() << "\"" << std::endl;
61  ss << "[SiStripConfigDb::" << __func__ << "]"
62  << " Cache holds device descriptions for "
63  << devices_.size() << " partitions.";
64  LogTrace(mlConfigDb_) << ss.str();
65 
66  }
67 
68  }
69 
70  }
71 
72  } else { // Using database cache
73 
74  DeviceDescriptionsV* tmp1 = databaseCache(__func__)->getDevices();
75 
76  if ( tmp1 ) {
77 
78  // Make local copy
80  FecFactory::vectorCopyI( tmp2, *tmp1, true );
81 
82  // Add to cache
83  devices_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
84 
85  } else {
87  << "[SiStripConfigDb::" << __func__ << "]"
88  << " NULL pointer to DeviceDescriptions vector!";
89  }
90 
91  }
92 
93  } catch (...) { handleException( __func__ ); }
94 
95  // Create range object
96  uint16_t np = 0;
97  uint16_t nc = 0;
99  if ( partition != "" ) {
100  devs = devices_.find( partition );
101  np = 1;
102  nc = devs.size();
103  } else {
104  if ( !devices_.empty() ) {
105  devs = DeviceDescriptionsRange( devices_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
106  devices_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
107  } else { devs = devices_.emptyRange(); }
108  np = devices_.size();
109  nc = devs.size();
110  }
111 
112  stringstream ss;
113  ss << "[SiStripConfigDb::" << __func__ << "]"
114  << " Found " << nc << " device descriptions";
115  if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputFecXmlFiles().size() << " 'fec.xml' file(s)"; }
116  else { if ( !dbParams_.usingDbCache() ) { ss << " in " << np << " database partition(s)"; }
117  else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
118  if ( devices_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
119  else { LogTrace(mlConfigDb_) << ss.str(); }
120 
121  return devs;
122 
123 }
124 
125 // -----------------------------------------------------------------------------
126 //
128  std::string partition ) {
129 
130  typedDevices_.clear();
131  apvDevices_.clear();
132  muxDevices_.clear();
133  dcuDevices_.clear();
134  lldDevices_.clear();
135  pllDevices_.clear();
136  dohDevices_.clear();
137 
138  DeviceDescriptionsRange tmp( typedDevices_.end(), typedDevices_.end() );
139 
140  if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
141  ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
142  return tmp;
143  }
144 
145  try {
147  if ( !devs.empty() ) {
148  DeviceDescriptionsV tmp( devs.begin(), devs.end() );
149  typedDevices_ = FecFactory::getDeviceFromDeviceVector( tmp, device_type );
150  }
151  } catch (...) { handleException( __func__ ); }
152 
153  if ( device_type == APV25 ) {
154  apvDevices_.swap( typedDevices_ );
155  tmp = DeviceDescriptionsRange( apvDevices_.begin(), apvDevices_.end() );
156  } else if ( device_type == APVMUX ) {
157  muxDevices_.swap( typedDevices_ );
158  tmp = DeviceDescriptionsRange( muxDevices_.begin(), muxDevices_.end() );
159  } else if ( device_type == DCU ) {
160  dcuDevices_.swap( typedDevices_ );
161  tmp = DeviceDescriptionsRange( dcuDevices_.begin(), dcuDevices_.end() );
162  } else if ( device_type == LASERDRIVER ) {
163  lldDevices_.swap( typedDevices_ );
164  tmp = DeviceDescriptionsRange( lldDevices_.begin(), lldDevices_.end() );
165  } else if ( device_type == PLL ) {
166  pllDevices_.swap( typedDevices_ );
167  tmp = DeviceDescriptionsRange( pllDevices_.begin(), pllDevices_.end() );
168  } else if ( device_type == DOH ) {
169  dohDevices_.swap( typedDevices_ );
170  tmp = DeviceDescriptionsRange( dohDevices_.begin(), dohDevices_.end() );
171  } else {
172  tmp = DeviceDescriptionsRange( typedDevices_.begin(), typedDevices_.end() );
173  }
174 
175 // stringstream sss;
176 // sss << " Number of devices:"
177 // << " TYP: " << typedDevices_.size()
178 // << " APV: " << apvDevices_.size()
179 // << " MUX: " << muxDevices_.size()
180 // << " DCU: " << dcuDevices_.size()
181 // << " LLD: " << lldDevices_.size()
182 // << " PLL: " << pllDevices_.size()
183 // << " DOH: " << dohDevices_.size()
184 // << " tmp: " << tmp.size();
185 // LogTrace(mlConfigDb_) << sss.str();
186 
187  stringstream ss;
188  ss << "[SiStripConfigDb::" << __func__ << "]"
189  << " Extracted " << tmp.size()
190  << " device descriptions (for devices of type "
191  << deviceType( device_type ) << ")";
192  LogTrace(mlConfigDb_) << ss.str();
193 
194  return tmp;
195 
196 }
197 
198 // -----------------------------------------------------------------------------
199 //
200 void SiStripConfigDb::addDeviceDescriptions( std::string partition, DeviceDescriptionsV& devs ) {
201 
202  if ( !deviceFactory(__func__) ) { return; }
203 
204  if ( partition.empty() ) {
205  stringstream ss;
206  ss << "[SiStripConfigDb::" << __func__ << "]"
207  << " Partition string is empty,"
208  << " therefore cannot add device descriptions to local cache!";
209  edm::LogWarning(mlConfigDb_) << ss.str();
210  return;
211  }
212 
213  if ( devs.empty() ) {
214  stringstream ss;
215  ss << "[SiStripConfigDb::" << __func__ << "]"
216  << " Vector of device descriptions is empty,"
217  << " therefore cannot add device descriptions to local cache!";
218  edm::LogWarning(mlConfigDb_) << ss.str();
219  return;
220  }
221 
222  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
223  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
224  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
225  if ( iter == dbParams_.partitions().end() ) {
226  stringstream ss;
227  ss << "[SiStripConfigDb::" << __func__ << "]"
228  << " Partition \"" << partition
229  << "\" not found in partition list, "
230  << " therefore cannot add device descriptions!";
231  edm::LogWarning(mlConfigDb_) << ss.str();
232  return;
233  }
234 
235  DeviceDescriptionsRange range = devices_.find( partition );
236  if ( range == devices_.emptyRange() ) {
237 
238  // Make local copy
240  FecFactory::vectorCopyI( tmp, devs, true );
241 
242  // Add to local cache
243  devices_.loadNext( partition, tmp );
244 
245  // Some debug
246  std::stringstream ss;
247  ss << "[SiStripConfigDb::" << __func__ << "]"
248  << " Added " << devs.size()
249  << " device descriptions to local cache for partition \""
250  << partition << "\"."
251  << " (Cache holds device descriptions for "
252  << devices_.size() << " partitions.)";
253  LogTrace(mlConfigDb_) << ss.str();
254 
255  } else {
256  stringstream ss;
257  ss << "[SiStripConfigDb::" << __func__ << "]"
258  << " Partition \"" << partition
259  << "\" already found in local cache, "
260  << " therefore cannot add device descriptions!";
261  edm::LogWarning(mlConfigDb_) << ss.str();
262  return;
263  }
264 
265 }
266 
267 // -----------------------------------------------------------------------------
268 //
269 void SiStripConfigDb::uploadDeviceDescriptions( std::string partition ) {
270 
271  if ( dbParams_.usingDbCache() ) {
273  << "[SiStripConfigDb::" << __func__ << "]"
274  << " Using database cache! No uploads allowed!";
275  return;
276  }
277 
278  if ( !deviceFactory(__func__) ) { return; }
279 
280  if ( devices_.empty() ) {
281  stringstream ss;
282  ss << "[SiStripConfigDb::" << __func__ << "]"
283  << " Found no cached device descriptions, therefore no upload!";
284  edm::LogWarning(mlConfigDb_) << ss.str();
285  return;
286  }
287 
288  try {
289 
290  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
291  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
292  for ( ; iter != jter; ++iter ) {
293 
294  if ( partition == "" || partition == iter->second.partitionName() ) {
295 
296  DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
297  if ( range != devices_.emptyRange() ) {
298 
299  DeviceDescriptionsV devs( range.begin(), range.end() );
300 
301  SiStripPartition::Versions fecVersion = iter->second.fecVersion();
302  deviceFactory(__func__)->setFecDeviceDescriptions( devs,
303  iter->second.partitionName(),
304  &(fecVersion.first),
305  &(fecVersion.second),
306  true ); // new major version
307 
308  // Some debug
309  std::stringstream ss;
310  ss << "[SiStripConfigDb::" << __func__ << "]"
311  << " Uploaded " << devs.size()
312  << " device descriptions to database for partition \""
313  << iter->second.partitionName() << "\".";
314  LogTrace(mlConfigDb_) << ss.str();
315 
316  } else {
317  stringstream ss;
318  ss << "[SiStripConfigDb::" << __func__ << "]"
319  << " Vector of device descriptions is empty for partition \""
320  << iter->second.partitionName()
321  << "\", therefore aborting upload for this partition!";
322  edm::LogWarning(mlConfigDb_) << ss.str();
323  continue;
324  }
325 
326  } else {
327  // stringstream ss;
328  // ss << "[SiStripConfigDb::" << __func__ << "]"
329  // << " Cannot find partition \"" << partition
330  // << "\" in cached partitions list: \""
331  // << dbParams_.partitionNames( dbParams_.partitionNames() )
332  // << "\", therefore aborting upload for this partition!";
333  // edm::LogWarning(mlConfigDb_) << ss.str();
334  }
335 
336  }
337 
338  } catch (...) { handleException( __func__ ); }
339 
340  allowCalibUpload_ = true;
341 
342 }
343 
344 // -----------------------------------------------------------------------------
345 //
346 void SiStripConfigDb::clearDeviceDescriptions( std::string partition ) {
347  LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
348 
349  if ( devices_.empty() ) {
350  stringstream ss;
351  ss << "[SiStripConfigDb::" << __func__ << "]"
352  << " Found no cached device descriptions!";
353  //edm::LogWarning(mlConfigDb_) << ss.str();
354  return;
355  }
356 
357  // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
358  DeviceDescriptions temporary_cache;
359  if ( partition == "" ) { temporary_cache = DeviceDescriptions(); }
360  else {
361  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
362  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
363  for ( ; iter != jter; ++iter ) {
364  if ( partition != iter->second.partitionName() ) {
365  DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
366  if ( range != devices_.emptyRange() ) {
367  temporary_cache.loadNext( partition, DeviceDescriptionsV( range.begin(), range.end() ) );
368  } else {
369  // stringstream ss;
370  // ss << "[SiStripConfigDb::" << __func__ << "]"
371  // << " Cannot find partition \"" << iter->second.partitionName()
372  // << "\" in local cache!";
373  // edm::LogWarning(mlConfigDb_) << ss.str();
374  }
375  }
376  }
377  }
378 
379  // Delete objects in local cache for specified partition (or all if not specified)
381  if ( partition == "" ) {
382  if ( !devices_.empty() ) {
383  devs = DeviceDescriptionsRange( devices_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
384  devices_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
385  } else { devs = devices_.emptyRange(); }
386  } else {
387  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
388  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
389  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
390  devs = devices_.find( iter->second.partitionName() );
391  }
392 
393  if ( devs != devices_.emptyRange() ) {
394  DeviceDescriptionsV::const_iterator ifed = devs.begin();
395  DeviceDescriptionsV::const_iterator jfed = devs.end();
396  for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
397  } else {
398  stringstream ss;
399  ss << "[SiStripConfigDb::" << __func__ << "]";
400  if ( partition == "" ) { ss << " Found no device descriptions in local cache!"; }
401  else { ss << " Found no device descriptions in local cache for partition \"" << partition << "\"!"; }
402  edm::LogWarning(mlConfigDb_) << ss.str();
403  }
404 
405  // Overwrite local cache with temporary cache
406  devices_ = temporary_cache;
407 
408 }
409 
410 // -----------------------------------------------------------------------------
411 //
412 void SiStripConfigDb::printDeviceDescriptions( std::string partition ) {
413 
414  std::stringstream ss;
415  ss << "[SiStripConfigDb::" << __func__ << "]"
416  << " Contents of DeviceDescriptions container:" << std::endl;
417  ss << " Number of partitions: " << devices_.size() << std::endl;
418 
419  // Loop through partitions
420  uint16_t cntr = 0;
421  DeviceDescriptions::const_iterator idev = devices_.begin();
422  DeviceDescriptions::const_iterator jdev = devices_.end();
423  for ( ; idev != jdev; ++idev ) {
424 
425  cntr++;
426  if ( partition == "" || partition == idev->first ) {
427 
428  ss << " Partition number : " << cntr << " (out of " << devices_.size() << ")" << std::endl;
429  ss << " Partition name : \"" << idev->first << "\"" << std::endl;
430  ss << " Num of devices : " << idev->second.size() << std::endl;
431 
432  // Extract FEC crate, slot, etc
433  std::map< uint32_t, vector<std::string> > devices;
434  DeviceDescriptionsV::const_iterator iter = idev->second.begin();
435  DeviceDescriptionsV::const_iterator jter = idev->second.end();
436  for ( ; iter != jter; ++iter ) {
437  if ( *iter ) {
438  DeviceAddress addr = deviceAddress( **iter );
439  uint32_t key = SiStripFecKey( addr.fecCrate_,
440  addr.fecSlot_,
441  addr.fecRing_,
442  0,
443  0,
444  0,
445  0 ).key();
446  std::stringstream data;
447  data << (*iter)->getDeviceType()
448  << "_"
449  << SiStripFecKey( addr.fecCrate_,
450  addr.fecSlot_,
451  addr.fecRing_,
452  addr.ccuAddr_,
453  addr.ccuChan_,
454  addr.lldChan_,
455  addr.i2cAddr_ ).key();
456  if ( find( devices[key].begin(), devices[key].end(), data.str() ) == devices[key].end() ) {
457  devices[key].push_back( data.str() );
458  }
459  }
460  }
461 
462  // Sort contents
463  std::map< uint32_t, std::vector<std::string> > tmp;
464  std::map< uint32_t, std::vector<std::string> >::const_iterator ii = devices.begin();
465  std::map< uint32_t, std::vector<std::string> >::const_iterator jj = devices.end();
466  for ( ; ii != jj; ++ii ) {
467  std::vector<std::string> temp = ii->second;
468  std::sort( temp.begin(), temp.end() );
469  std::vector<std::string>::const_iterator iii = temp.begin();
470  std::vector<std::string>::const_iterator jjj = temp.end();
471  for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
472  }
473  devices.clear();
474  devices = tmp;
475 
476  // Print FEC crate, slot, etc...
477  std::map< uint32_t, std::vector<std::string> >::const_iterator idev = devices.begin();
478  std::map< uint32_t, std::vector<std::string> >::const_iterator jdev = devices.end();
479  for ( ; idev != jdev; ++idev ) {
480  SiStripFecKey key(idev->first);
481  ss << " Found " << std::setw(3) << idev->second.size()
482  << " devices for FEC crate/slot/ring "
483  << key.fecCrate() << "/"
484  << key.fecSlot() << "/"
485  << key.fecRing();
486  //<< " (ccu/module/lld/i2c): ";
487  // if ( !idev->second.empty() ) {
488  // uint16_t first = idev->second.front();
489  // uint16_t last = idev->second.front();
490  // std::vector<std::string>::const_iterator chan = idev->second.begin();
491  // for ( ; chan != idev->second.end(); chan++ ) {
492  // if ( chan != idev->second.begin() ) {
493  // if ( *chan != last+1 ) {
494  // ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
495  // if ( chan != idev->second.end() ) { first = *(chan+1); }
496  // }
497  // }
498  // last = *chan;
499  // }
500  // if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
501  ss << std::endl;
502  }
503 
504  }
505 
506  }
507 
508  LogTrace(mlConfigDb_) << ss.str();
509 
510 }
511 
512 // -----------------------------------------------------------------------------
513 //
515 
516  deviceDescription& desc = const_cast<deviceDescription&>(description);
517 
518  DeviceAddress addr;
519  try {
520  addr.fecCrate_ = static_cast<uint16_t>( desc.getCrateId() + sistrip::FEC_CRATE_OFFSET ); //@@ temporary offset?
521  addr.fecSlot_ = static_cast<uint16_t>( desc.getFecSlot() );
522  addr.fecRing_ = static_cast<uint16_t>( desc.getRingSlot() + sistrip::FEC_RING_OFFSET ); //@@ temporary offset?
523  addr.ccuAddr_ = static_cast<uint16_t>( desc.getCcuAddress() );
524  addr.ccuChan_ = static_cast<uint16_t>( desc.getChannel() );
525  addr.lldChan_ = static_cast<uint16_t>( SiStripFecKey::lldChan( desc.getAddress() ) );
526  addr.i2cAddr_ = static_cast<uint16_t>( desc.getAddress() );
527  } catch (...) { handleException( __func__ ); }
528 
529  return addr;
530 }
531 
532 // -----------------------------------------------------------------------------
533 //
534 string SiStripConfigDb::deviceType( const enumDeviceType& device_type ) const {
535  if ( device_type == PLL ) { return "PLL"; }
536  else if ( device_type == LASERDRIVER ) { return "LLD"; }
537  else if ( device_type == DOH ) { return "DOH"; }
538  else if ( device_type == APVMUX ) { return "MUX"; }
539  else if ( device_type == APV25 ) { return "APV"; }
540  else if ( device_type == DCU ) { return "DCU"; }
541  else if ( device_type == GOH ) { return "GOH"; }
542  else { return "UNKNOWN DEVICE!"; }
543 }
static std::string defaultPartitionName_
const uint16_t & fecRing() const
void loadNext(K const &k, std::vector< T > const &v)
Definition: MapOfVectors.h:97
void addDeviceDescriptions(std::string partition, DeviceDescriptionsV &)
static const uint16_t FEC_RING_OFFSET
const uint16_t & lldChan() const
const uint16_t & fecSlot() const
void clearDeviceDescriptions(std::string partition="")
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
std::pair< uint32_t, uint32_t > Versions
const uint32_t & key() const
Definition: SiStripKey.h:126
static const char mlConfigDb_[]
Utility class that identifies a position within the strip tracker control structure, down to the level of an APV25.
Definition: SiStripFecKey.h:46
int np
Definition: AMPTWrapper.h:33
enumDeviceType DeviceType
DeviceDescriptions::range DeviceDescriptionsRange
#define end
Definition: vmac.h:38
DeviceAddress deviceAddress(const deviceDescription &)
#define LogTrace(id)
const uint16_t & fecCrate() const
tuple description
Definition: idDealer.py:66
std::string deviceType(const enumDeviceType &device_type) const
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
#define begin
Definition: vmac.h:31
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
list key
Definition: combine.py:13
void printDeviceDescriptions(std::string partition="")
static const uint16_t FEC_CRATE_OFFSET
DeviceDescriptionsRange getDeviceDescriptions(std::string partition="")
void uploadDeviceDescriptions(std::string partition="")
std::vector< DeviceDescription * > DeviceDescriptionsV