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.35 2009/04/03 16:11:53 lowette 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  deviceFactory(__func__)->setFecDeviceDescriptions( devs,
302  iter->second.partitionName(),
303  &(iter->second.fecVersion().first),
304  &(iter->second.fecVersion().second),
305  true ); // new major version
306 
307  // Some debug
308  std::stringstream ss;
309  ss << "[SiStripConfigDb::" << __func__ << "]"
310  << " Uploaded " << devs.size()
311  << " device descriptions to database for partition \""
312  << iter->second.partitionName() << "\".";
313  LogTrace(mlConfigDb_) << ss.str();
314 
315  } else {
316  stringstream ss;
317  ss << "[SiStripConfigDb::" << __func__ << "]"
318  << " Vector of device descriptions is empty for partition \""
319  << iter->second.partitionName()
320  << "\", therefore aborting upload for this partition!";
321  edm::LogWarning(mlConfigDb_) << ss.str();
322  continue;
323  }
324 
325  } else {
326  // stringstream ss;
327  // ss << "[SiStripConfigDb::" << __func__ << "]"
328  // << " Cannot find partition \"" << partition
329  // << "\" in cached partitions list: \""
330  // << dbParams_.partitionNames( dbParams_.partitionNames() )
331  // << "\", therefore aborting upload for this partition!";
332  // edm::LogWarning(mlConfigDb_) << ss.str();
333  }
334 
335  }
336 
337  } catch (...) { handleException( __func__ ); }
338 
339  allowCalibUpload_ = true;
340 
341 }
342 
343 // -----------------------------------------------------------------------------
344 //
345 void SiStripConfigDb::clearDeviceDescriptions( std::string partition ) {
346  LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
347 
348  if ( devices_.empty() ) {
349  stringstream ss;
350  ss << "[SiStripConfigDb::" << __func__ << "]"
351  << " Found no cached device descriptions!";
352  //edm::LogWarning(mlConfigDb_) << ss.str();
353  return;
354  }
355 
356  // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
357  DeviceDescriptions temporary_cache;
358  if ( partition == "" ) { temporary_cache = DeviceDescriptions(); }
359  else {
360  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
361  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
362  for ( ; iter != jter; ++iter ) {
363  if ( partition != iter->second.partitionName() ) {
364  DeviceDescriptionsRange range = devices_.find( iter->second.partitionName() );
365  if ( range != devices_.emptyRange() ) {
366  temporary_cache.loadNext( partition, DeviceDescriptionsV( range.begin(), range.end() ) );
367  } else {
368  // stringstream ss;
369  // ss << "[SiStripConfigDb::" << __func__ << "]"
370  // << " Cannot find partition \"" << iter->second.partitionName()
371  // << "\" in local cache!";
372  // edm::LogWarning(mlConfigDb_) << ss.str();
373  }
374  }
375  }
376  }
377 
378  // Delete objects in local cache for specified partition (or all if not specified)
380  if ( partition == "" ) {
381  if ( !devices_.empty() ) {
382  devs = DeviceDescriptionsRange( devices_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
383  devices_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
384  } else { devs = devices_.emptyRange(); }
385  } else {
386  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
387  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
388  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
389  devs = devices_.find( iter->second.partitionName() );
390  }
391 
392  if ( devs != devices_.emptyRange() ) {
393  DeviceDescriptionsV::const_iterator ifed = devs.begin();
394  DeviceDescriptionsV::const_iterator jfed = devs.end();
395  for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
396  } else {
397  stringstream ss;
398  ss << "[SiStripConfigDb::" << __func__ << "]";
399  if ( partition == "" ) { ss << " Found no device descriptions in local cache!"; }
400  else { ss << " Found no device descriptions in local cache for partition \"" << partition << "\"!"; }
401  edm::LogWarning(mlConfigDb_) << ss.str();
402  }
403 
404  // Overwrite local cache with temporary cache
405  devices_ = temporary_cache;
406 
407 }
408 
409 // -----------------------------------------------------------------------------
410 //
411 void SiStripConfigDb::printDeviceDescriptions( std::string partition ) {
412 
413  std::stringstream ss;
414  ss << "[SiStripConfigDb::" << __func__ << "]"
415  << " Contents of DeviceDescriptions container:" << std::endl;
416  ss << " Number of partitions: " << devices_.size() << std::endl;
417 
418  // Loop through partitions
419  uint16_t cntr = 0;
420  DeviceDescriptions::const_iterator idev = devices_.begin();
421  DeviceDescriptions::const_iterator jdev = devices_.end();
422  for ( ; idev != jdev; ++idev ) {
423 
424  cntr++;
425  if ( partition == "" || partition == idev->first ) {
426 
427  ss << " Partition number : " << cntr << " (out of " << devices_.size() << ")" << std::endl;
428  ss << " Partition name : \"" << idev->first << "\"" << std::endl;
429  ss << " Num of devices : " << idev->second.size() << std::endl;
430 
431  // Extract FEC crate, slot, etc
432  std::map< uint32_t, vector<std::string> > devices;
433  DeviceDescriptionsV::const_iterator iter = idev->second.begin();
434  DeviceDescriptionsV::const_iterator jter = idev->second.end();
435  for ( ; iter != jter; ++iter ) {
436  if ( *iter ) {
437  DeviceAddress addr = deviceAddress( **iter );
438  uint32_t key = SiStripFecKey( addr.fecCrate_,
439  addr.fecSlot_,
440  addr.fecRing_,
441  0,
442  0,
443  0,
444  0 ).key();
445  std::stringstream data;
446  data << (*iter)->getDeviceType()
447  << "_"
448  << SiStripFecKey( addr.fecCrate_,
449  addr.fecSlot_,
450  addr.fecRing_,
451  addr.ccuAddr_,
452  addr.ccuChan_,
453  addr.lldChan_,
454  addr.i2cAddr_ ).key();
455  if ( find( devices[key].begin(), devices[key].end(), data.str() ) == devices[key].end() ) {
456  devices[key].push_back( data.str() );
457  }
458  }
459  }
460 
461  // Sort contents
462  std::map< uint32_t, std::vector<std::string> > tmp;
463  std::map< uint32_t, std::vector<std::string> >::const_iterator ii = devices.begin();
464  std::map< uint32_t, std::vector<std::string> >::const_iterator jj = devices.end();
465  for ( ; ii != jj; ++ii ) {
466  std::vector<std::string> temp = ii->second;
467  std::sort( temp.begin(), temp.end() );
468  std::vector<std::string>::const_iterator iii = temp.begin();
469  std::vector<std::string>::const_iterator jjj = temp.end();
470  for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
471  }
472  devices.clear();
473  devices = tmp;
474 
475  // Print FEC crate, slot, etc...
476  std::map< uint32_t, std::vector<std::string> >::const_iterator idev = devices.begin();
477  std::map< uint32_t, std::vector<std::string> >::const_iterator jdev = devices.end();
478  for ( ; idev != jdev; ++idev ) {
479  SiStripFecKey key(idev->first);
480  ss << " Found " << std::setw(3) << idev->second.size()
481  << " devices for FEC crate/slot/ring "
482  << key.fecCrate() << "/"
483  << key.fecSlot() << "/"
484  << key.fecRing();
485  //<< " (ccu/module/lld/i2c): ";
486  // if ( !idev->second.empty() ) {
487  // uint16_t first = idev->second.front();
488  // uint16_t last = idev->second.front();
489  // std::vector<std::string>::const_iterator chan = idev->second.begin();
490  // for ( ; chan != idev->second.end(); chan++ ) {
491  // if ( chan != idev->second.begin() ) {
492  // if ( *chan != last+1 ) {
493  // ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
494  // if ( chan != idev->second.end() ) { first = *(chan+1); }
495  // }
496  // }
497  // last = *chan;
498  // }
499  // if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
500  ss << std::endl;
501  }
502 
503  }
504 
505  }
506 
507  LogTrace(mlConfigDb_) << ss.str();
508 
509 }
510 
511 // -----------------------------------------------------------------------------
512 //
514 
515  deviceDescription& desc = const_cast<deviceDescription&>(description);
516 
517  DeviceAddress addr;
518  try {
519  addr.fecCrate_ = static_cast<uint16_t>( desc.getCrateId() + sistrip::FEC_CRATE_OFFSET ); //@@ temporary offset?
520  addr.fecSlot_ = static_cast<uint16_t>( desc.getFecSlot() );
521  addr.fecRing_ = static_cast<uint16_t>( desc.getRingSlot() + sistrip::FEC_RING_OFFSET ); //@@ temporary offset?
522  addr.ccuAddr_ = static_cast<uint16_t>( desc.getCcuAddress() );
523  addr.ccuChan_ = static_cast<uint16_t>( desc.getChannel() );
524  addr.lldChan_ = static_cast<uint16_t>( SiStripFecKey::lldChan( desc.getAddress() ) );
525  addr.i2cAddr_ = static_cast<uint16_t>( desc.getAddress() );
526  } catch (...) { handleException( __func__ ); }
527 
528  return addr;
529 }
530 
531 // -----------------------------------------------------------------------------
532 //
533 string SiStripConfigDb::deviceType( const enumDeviceType& device_type ) const {
534  if ( device_type == PLL ) { return "PLL"; }
535  else if ( device_type == LASERDRIVER ) { return "LLD"; }
536  else if ( device_type == DOH ) { return "DOH"; }
537  else if ( device_type == APVMUX ) { return "MUX"; }
538  else if ( device_type == APV25 ) { return "APV"; }
539  else if ( device_type == DCU ) { return "DCU"; }
540  else if ( device_type == GOH ) { return "GOH"; }
541  else { return "UNKNOWN DEVICE!"; }
542 }
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
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
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
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