CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FedDescriptions.cc
Go to the documentation of this file.
1 // Last commit: $Id: FedDescriptions.cc,v 1.33 2011/09/02 11:25:25 eulisse Exp $
2 
7 
8 using namespace std;
9 using namespace sistrip;
10 
11 // -----------------------------------------------------------------------------
12 //
14 
15  // Check
16  if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
17  ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
18  return feds_.emptyRange();
19  }
20 
21  try {
22 
23  if ( !dbParams_.usingDbCache() ) {
24 
25  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
26  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
27  for ( ; iter != jter; ++iter ) {
28 
29  if ( partition == "" || partition == iter->second.partitionName() ) {
30 
31  if ( iter->second.partitionName() == SiStripPartition::defaultPartitionName_ ) { continue; }
32 
33  FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
34  if ( range == feds_.emptyRange() ) {
35 
36  // Extract versions
37  deviceFactory(__func__)->setUsingStrips( usingStrips_ );
38  int16_t major = iter->second.fedVersion().first;
39  int16_t minor = iter->second.fedVersion().second;
40  if ( iter->second.fedVersion().first == 0 &&
41  iter->second.fedVersion().second == 0 ) {
42  major = -1; //@@ "current state" for fed factory!
43  minor = -1; //@@ "current state" for fed factory!
44  }
45 
46  // Retrive FED descriptions
47  FedDescriptionsV tmp1;
48  tmp1 = *( deviceFactory(__func__)->getFed9UDescriptions( iter->second.partitionName(),
49  major,
50  minor ) );
51 
52  // Make local copy
53  FedDescriptionsV tmp2;
54  Fed9U::Fed9UDeviceFactory::vectorCopy( tmp2, tmp1 );
55 
56  // Add to cache
57  feds_.loadNext( iter->second.partitionName(), tmp2 );
58 
59  // Some debug
60  FedDescriptionsRange feds = feds_.find( iter->second.partitionName() );
61  std::stringstream ss;
62  ss << "[SiStripConfigDb::" << __func__ << "]"
63  << " Downloaded " << feds.size()
64  << " FED descriptions to local cache for partition \""
65  << iter->second.partitionName() << "\"" << std::endl;
66  ss << "[SiStripConfigDb::" << __func__ << "]"
67  << " Cache holds FED descriptions for "
68  << feds_.size() << " partitions.";
69  LogTrace(mlConfigDb_) << ss.str();
70 
71  }
72 
73  }
74 
75  }
76 
77  } else { // Using database cache
78 
79  FedDescriptionsV* tmp1 = databaseCache(__func__)->getFed9UDescriptions();
80 
81  if ( tmp1 ) {
82 
83  // Make local copy
84  FedDescriptionsV tmp2;
85  Fed9U::Fed9UDeviceFactory::vectorCopy( tmp2, *tmp1 );
86 
87  // Add to cache
88  feds_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
89 
90  } else {
92  << "[SiStripConfigDb::" << __func__ << "]"
93  << " NULL pointer to FED descriptions vector!";
94  }
95 
96  }
97 
98  } catch (... ) { handleException( __func__ ); }
99 
100  // Create range object
101  uint16_t np = 0;
102  uint16_t nc = 0;
104  if ( partition != "" ) {
105  feds = feds_.find( partition );
106  np = 1;
107  nc = feds.size();
108  } else {
109  if ( !feds_.empty() ) {
110  feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
111  feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
112  } else { feds = feds_.emptyRange(); }
113  np = feds_.size();
114  nc = feds.size();
115  }
116 
117  stringstream ss;
118  ss << "[SiStripConfigDb::" << __func__ << "]"
119  << " Found " << nc << " FED descriptions";
120  if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputFedXmlFiles().size() << " 'fed.xml' file(s)"; }
121  else { if ( !dbParams_.usingDbCache() ) { ss << " in " << np << " database partition(s)"; }
122  else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
123  if ( feds_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
124  else { LogTrace(mlConfigDb_) << ss.str(); }
125 
126  return feds;
127 
128 }
129 
130 // -----------------------------------------------------------------------------
131 //
132 void SiStripConfigDb::addFedDescriptions( std::string partition, FedDescriptionsV& feds ) {
133 
134  if ( !deviceFactory(__func__) ) { return; }
135 
136  if ( partition.empty() ) {
137  stringstream ss;
138  ss << "[SiStripConfigDb::" << __func__ << "]"
139  << " Partition string is empty,"
140  << " therefore cannot add FED descriptions to local cache!";
141  edm::LogWarning(mlConfigDb_) << ss.str();
142  return;
143  }
144 
145  if ( feds.empty() ) {
146  stringstream ss;
147  ss << "[SiStripConfigDb::" << __func__ << "]"
148  << " Vector of FED descriptions is empty,"
149  << " therefore cannot add FED descriptions to local cache!";
150  edm::LogWarning(mlConfigDb_) << ss.str();
151  return;
152  }
153 
154  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
155  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
156  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
157  if ( iter == dbParams_.partitions().end() ) {
158  stringstream ss;
159  ss << "[SiStripConfigDb::" << __func__ << "]"
160  << " Partition \"" << partition
161  << "\" not found in partition list, "
162  << " therefore cannot add FED descriptions!";
163  edm::LogWarning(mlConfigDb_) << ss.str();
164  return;
165  }
166 
167  FedDescriptionsRange range = feds_.find( partition );
168  if ( range == feds_.emptyRange() ) {
169 
170  // Make local copy
172  Fed9U::Fed9UDeviceFactory::vectorCopy( tmp, feds );
173 
174  // Add to local cache
175  feds_.loadNext( partition, tmp );
176 
177  // Some debug
178  std::stringstream ss;
179  ss << "[SiStripConfigDb::" << __func__ << "]"
180  << " Added " << feds.size()
181  << " FED descriptions to local cache for partition \""
182  << iter->second.partitionName() << "\"" << std::endl;
183  ss << "[SiStripConfigDb::" << __func__ << "]"
184  << " Cache holds FED descriptions for "
185  << feds_.size() << " partitions.";
186  LogTrace(mlConfigDb_) << ss.str();
187 
188  } else {
189  stringstream ss;
190  ss << "[SiStripConfigDb::" << __func__ << "]"
191  << " Partition \"" << partition
192  << "\" already found in local cache, "
193  << " therefore cannot add new FED descriptions!";
194  edm::LogWarning(mlConfigDb_) << ss.str();
195  return;
196  }
197 
198 }
199 
200 // -----------------------------------------------------------------------------
201 //
202 void SiStripConfigDb::uploadFedDescriptions( std::string partition ) {
203 
204  if ( dbParams_.usingDbCache() ) {
206  << "[SiStripConfigDb::" << __func__ << "]"
207  << " Using database cache! No uploads allowed!";
208  return;
209  }
210 
211  if ( !deviceFactory(__func__) ) { return; }
212 
213  if ( feds_.empty() ) {
214  stringstream ss;
215  ss << "[SiStripConfigDb::" << __func__ << "]"
216  << " Found no cached FED descriptions, therefore no upload!";
217  edm::LogWarning(mlConfigDb_) << ss.str();
218  return;
219  }
220 
221  try {
222 
223  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
224  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
225  for ( ; iter != jter; ++iter ) {
226 
227  if ( partition == "" || partition == iter->second.partitionName() ) {
228 
229  FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
230  if ( range != feds_.emptyRange() ) {
231 
232  FedDescriptionsV feds( range.begin(), range.end() );
233 
234  SiStripPartition::Versions fedVersion = iter->second.fedVersion();
235  deviceFactory(__func__)->setFed9UDescriptions( feds,
236  iter->second.partitionName(),
237  (uint16_t*)(&(fedVersion.first)),
238  (uint16_t*)(&(fedVersion.second)),
239  1 ); // new major version
240 
241  // Some debug
242  std::stringstream ss;
243  ss << "[SiStripConfigDb::" << __func__ << "]"
244  << " Uploaded " << feds.size()
245  << " FED descriptions to database for partition \""
246  << iter->second.partitionName() << "\"";
247  LogTrace(mlConfigDb_) << ss.str();
248 
249  } else {
250  stringstream ss;
251  ss << "[SiStripConfigDb::" << __func__ << "]"
252  << " Vector of FED descriptions is empty for partition \""
253  << iter->second.partitionName()
254  << "\", therefore aborting upload for this partition!";
255  edm::LogWarning(mlConfigDb_) << ss.str();
256  continue;
257  }
258 
259  } else {
260  // stringstream ss;
261  // ss << "[SiStripConfigDb::" << __func__ << "]"
262  // << " Cannot find partition \"" << partition
263  // << "\" in cached partitions list: \""
264  // << dbParams_.partitionNames( dbParams_.partitionNames() )
265  // << "\", therefore aborting upload for this partition!";
266  // edm::LogWarning(mlConfigDb_) << ss.str();
267  }
268 
269  }
270 
271  } catch (...) { handleException( __func__ ); }
272 
273  allowCalibUpload_ = true;
274 
275 }
276 
277 // -----------------------------------------------------------------------------
278 //
279 void SiStripConfigDb::clearFedDescriptions( std::string partition ) {
280  LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
281 
282  if ( feds_.empty() ) {
283  stringstream ss;
284  ss << "[SiStripConfigDb::" << __func__ << "]"
285  << " Found no cached FED descriptions!";
286  //edm::LogWarning(mlConfigDb_) << ss.str();
287  return;
288  }
289 
290  // Reproduce temporary cache for "all partitions except specified one" (or clear all if none specified)
291  FedDescriptions temporary_cache;
292  if ( partition == "" ) { temporary_cache = FedDescriptions(); }
293  else {
294  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
295  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
296  for ( ; iter != jter; ++iter ) {
297  if ( partition != iter->second.partitionName() ) {
298  FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
299  if ( range != feds_.emptyRange() ) {
300  temporary_cache.loadNext( partition, FedDescriptionsV( range.begin(), range.end() ) );
301  }
302  } else {
303  FedDescriptionsRange range = feds_.find( iter->second.partitionName() );
304  if ( range != feds_.emptyRange() ) {
306  << "[SiStripConfigDb::" << __func__ << "]"
307  << " Deleting FED descriptions for partition \""
308  << iter->second.partitionName()
309  << "\" from local cache...";
310  }
311  }
312  }
313  }
314 
315  // Delete objects in local cache for specified partition (or all if not specified)
317  if ( partition == "" ) {
318  if ( !feds_.empty() ) {
319  feds = FedDescriptionsRange( feds_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
320  feds_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
321  } else { feds = feds_.emptyRange(); }
322  } else {
323  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
324  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
325  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
326  feds = feds_.find( iter->second.partitionName() );
327  }
328 
329  if ( feds != feds_.emptyRange() ) {
330  FedDescriptionsV::const_iterator ifed = feds.begin();
331  FedDescriptionsV::const_iterator jfed = feds.end();
332  for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
333  } else {
334  stringstream ss;
335  ss << "[SiStripConfigDb::" << __func__ << "]";
336  if ( partition == "" ) { ss << " Found no FED descriptions in local cache!"; }
337  else { ss << " Found no FED descriptions in local cache for partition \"" << partition << "\"!"; }
338  edm::LogWarning(mlConfigDb_) << ss.str();
339  }
340 
341  // Overwrite local cache with temporary cache
342  feds_ = temporary_cache;
343 
344 }
345 
346 // -----------------------------------------------------------------------------
347 //
348 void SiStripConfigDb::printFedDescriptions( std::string partition ) {
349 
350  std::stringstream ss;
351  ss << "[SiStripConfigDb::" << __func__ << "]"
352  << " Contents of FedDescriptions container:" << std::endl;
353  ss << " Number of partitions: " << feds_.size() << std::endl;
354 
355  // Loop through partitions
356  uint16_t cntr = 0;
357  FedDescriptions::const_iterator iconn = feds_.begin();
358  FedDescriptions::const_iterator jconn = feds_.end();
359  for ( ; iconn != jconn; ++iconn ) {
360 
361  cntr++;
362  if ( partition == "" || partition == iconn->first ) {
363 
364  ss << " Partition number : " << cntr << " (out of " << feds_.size() << ")" << std::endl;
365  ss << " Partition name : \"" << iconn->first << "\"" << std::endl;
366  ss << " Num of FED ids : " << iconn->second.size() << std::endl;
367 
368  // Extract FED crates and ids
369  std::map< uint16_t, vector<uint16_t> > feds;
370  FedDescriptionsV::const_iterator iter = iconn->second.begin();
371  FedDescriptionsV::const_iterator jter = iconn->second.end();
372  for ( ; iter != jter; ++iter ) {
373  if ( *iter ) {
374  uint16_t key = (*iter)->getCrateNumber();
375  uint16_t data = (*iter)->getFedId();
376  if ( find( feds[key].begin(), feds[key].end(), data ) == feds[key].end() ) {
377  feds[key].push_back( data );
378  }
379  }
380  }
381 
382  // Sort contents
383  std::map< uint16_t, std::vector<uint16_t> > tmp;
384  std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
385  std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
386  for ( ; ii != jj; ++ii ) {
387  std::vector<uint16_t> temp = ii->second;
388  std::sort( temp.begin(), temp.end() );
389  std::vector<uint16_t>::const_iterator iii = temp.begin();
390  std::vector<uint16_t>::const_iterator jjj = temp.end();
391  for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
392  }
393  feds.clear();
394  feds = tmp;
395 
396  // Print FED crates and ids
397  std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
398  std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
399  for ( ; ifed != jfed; ++ifed ) {
400  ss << " Found " << std::setw(2) << ifed->second.size()
401  << " FED ids for crate number " << std::setw(2) << ifed->first << " : ";
402  if ( !ifed->second.empty() ) {
403  uint16_t first = ifed->second.front();
404  uint16_t last = ifed->second.front();
405  std::vector<uint16_t>::const_iterator icrate = ifed->second.begin();
406  std::vector<uint16_t>::const_iterator jcrate = ifed->second.end();
407  for ( ; icrate != jcrate; ++icrate ) {
408  if ( icrate != ifed->second.begin() ) {
409  if ( *icrate != last+1 ) {
410  ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
411  if ( icrate != ifed->second.end() ) { first = *(icrate+1); }
412  }
413  }
414  last = *icrate;
415  }
416  if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
417  ss << std::endl;
418  }
419  }
420 
421  }
422 
423  }
424 
425  LogTrace(mlConfigDb_) << ss.str();
426 
427 }
428 
429 // -----------------------------------------------------------------------------
432 
433  fedIds_.clear();
434 
435  if ( ( !dbParams_.usingDbCache() && !deviceFactory(__func__) ) ||
436  ( dbParams_.usingDbCache() && !databaseCache(__func__) ) ) {
437  return FedIdsRange( fedIds_.end(), fedIds_.end() );
438  }
439 
440  try {
441 
442  // Inhibit download of strip info
443  bool using_strips = usingStrips_;
444  if ( factory_ ) { factory_->setUsingStrips( false ); }
445  FedDescriptionsRange feds = getFedDescriptions( partition );
446  if ( factory_ ) { factory_->setUsingStrips( using_strips ); }
447 
448  if ( !feds.empty() ) {
449  FedDescriptionsV::const_iterator ifed = feds.begin();
450  FedDescriptionsV::const_iterator jfed = feds.end();
451  for ( ; ifed != jfed; ++ifed ) {
452  if ( *ifed ) { fedIds_.push_back( (*ifed)->getFedId() ); }
453  else {
455  << "[SiStripConfigDb::" << __func__ << "]"
456  << " NULL pointer to FedDescription!";
457  continue;
458  }
459  }
460  }
461 
462  } catch (...) { handleException( __func__ ); }
463 
464  if ( fedIds_.empty() ) {
466  << "[SiStripConfigDb::" << __func__ << "]"
467  << " No FED ids found!";
468  }
469 
470  return FedIdsRange( fedIds_.begin(), fedIds_.end() );
471 
472 }
473 
static std::string defaultPartitionName_
void loadNext(K const &k, std::vector< T > const &v)
Definition: MapOfVectors.h:97
FedDescriptionsRange getFedDescriptions(std::string partition="")
FedDescriptions::range FedDescriptionsRange
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
void addFedDescriptions(std::string partition, FedDescriptionsV &)
static const char mlConfigDb_[]
static const char mlCabling_[]
int np
Definition: AMPTWrapper.h:33
FedIdsRange getFedIds(std::string partition="")
#define end
Definition: vmac.h:38
bool first
Definition: L1TdeRCT.cc:94
#define LogTrace(id)
void printFedDescriptions(std::string partition="")
void uploadFedDescriptions(std::string partition="")
boost::iterator_range< FedIds::const_iterator > FedIdsRange
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 clearFedDescriptions(std::string partition="")
std::vector< FedDescription * > FedDescriptionsV