CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FedConnections.cc
Go to the documentation of this file.
1 // Last commit: $Id: FedConnections.cc,v 1.35 2011/09/02 11:25:25 eulisse Exp $
2 
5 #include <ostream>
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 connections_.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  FedConnectionsRange range = connections_.find( iter->second.partitionName() );
33  if ( range == connections_.emptyRange() ) {
34 
35  FedConnectionsV tmp2;
36 
37  // Retrieve connections
38  FedConnectionsV tmp1;
39  deviceFactory(__func__)->getConnectionDescriptions( iter->second.partitionName(),
40  tmp1,
41  iter->second.cabVersion().first,
42  iter->second.cabVersion().second,
43  //#ifdef USING_DATABASE_MASKING
44  iter->second.maskVersion().first,
45  iter->second.maskVersion().second,
46  //#endif
47  false ); //@@ do not get DISABLED connections
48 
49  // Make local copy
50  ConnectionFactory::vectorCopyI( tmp2, tmp1, true );
51 
52  // Add to cache
53  connections_.loadNext( iter->second.partitionName(), tmp2 );
54 
55  // Some debug
56  FedConnectionsRange conns = connections_.find( iter->second.partitionName() );
57  std::stringstream ss;
58  ss << "[SiStripConfigDb::" << __func__ << "]"
59  << " Downloaded " << conns.size()
60  << " FED connections to local cache for partition \""
61  << iter->second.partitionName() << "\"" << std::endl;
62  ss << "[SiStripConfigDb::" << __func__ << "]"
63  << " Cache holds FED connections for "
64  << connections_.size() << " partitions.";
65  LogTrace(mlConfigDb_) << ss.str();
66 
67  }
68 
69  }
70 
71  }
72 
73  } else { // Use database cache
74 
75  FedConnectionsV* tmp1 = databaseCache(__func__)->getConnections();
76 
77  if ( tmp1 ) {
78 
79  // Make local copy
80  FedConnectionsV tmp2;
81  ConnectionFactory::vectorCopyI( tmp2, *tmp1, true );
82 
83  // Add to cache
84  connections_.loadNext( SiStripPartition::defaultPartitionName_, tmp2 );
85 
86  } else {
88  << "[SiStripConfigDb::" << __func__ << "]"
89  << " NULL pointer to FedConnections vector!";
90  }
91 
92  }
93 
94  } catch (...) { handleException( __func__ ); }
95 
96  // Create range object
97  uint16_t np = 0;
98  uint16_t nc = 0;
99  FedConnectionsRange conns;
100  if ( partition != "" ) {
101  conns = connections_.find( partition );
102  np = 1;
103  nc = conns.size();
104  } else {
105  if ( !connections_.empty() ) {
106  conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
107  connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
108  } else { conns = connections_.emptyRange(); }
109  np = connections_.size();
110  nc = conns.size();
111  }
112 
113  stringstream ss;
114  ss << "[SiStripConfigDb::" << __func__ << "]"
115  << " Found " << nc << " FED connections";
116  if ( !dbParams_.usingDb() ) { ss << " in " << dbParams_.inputModuleXmlFiles().size() << " 'module.xml' file(s)"; }
117  else { if ( !dbParams_.usingDbCache() ) { ss << " in " << np << " database partition(s)"; }
118  else { ss << " from shared memory name '" << dbParams_.sharedMemory() << "'"; } }
119  if ( connections_.empty() ) { edm::LogWarning(mlConfigDb_) << ss.str(); }
120  else { LogTrace(mlConfigDb_) << ss.str(); }
121 
122  return conns;
123 
124 }
125 
126 // -----------------------------------------------------------------------------
127 //
128 void SiStripConfigDb::addFedConnections( std::string partition, FedConnectionsV& conns ) {
129 
130  if ( !deviceFactory(__func__) ) { return; }
131 
132  if ( partition.empty() ) {
133  stringstream ss;
134  ss << "[SiStripConfigDb::" << __func__ << "]"
135  << " Partition string is empty,"
136  << " therefore cannot add FED connections to local cache!";
137  edm::LogWarning(mlConfigDb_) << ss.str();
138  return;
139  }
140 
141  if ( conns.empty() ) {
142  stringstream ss;
143  ss << "[SiStripConfigDb::" << __func__ << "]"
144  << " Vector of FED connections is empty,"
145  << " therefore cannot add FED connections to local cache!";
146  edm::LogWarning(mlConfigDb_) << ss.str();
147  return;
148  }
149 
150  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
151  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
152  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
153  if ( iter == dbParams_.partitions().end() ) {
154  stringstream ss;
155  ss << "[SiStripConfigDb::" << __func__ << "]"
156  << " Partition \"" << partition
157  << "\" not found in partition list, "
158  << " therefore cannot add FED connections!";
159  edm::LogWarning(mlConfigDb_) << ss.str();
160  return;
161  }
162 
163  FedConnectionsRange range = connections_.find( partition );
164  if ( range == connections_.emptyRange() ) {
165 
166  // Make local copy
168  ConnectionFactory::vectorCopyI( tmp, conns, true );
169 
170  // Add to local cache
171  connections_.loadNext( partition, tmp );
172 
173  // Some debug
174  std::stringstream ss;
175  ss << "[SiStripConfigDb::" << __func__ << "]"
176  << " Added " << conns.size()
177  << " FED connections to local cache for partition \""
178  << partition << "\"" << std::endl;
179  ss << "[SiStripConfigDb::" << __func__ << "]"
180  << " Cache holds FED connections for "
181  << connections_.size() << " partitions.";
182  LogTrace(mlConfigDb_) << ss.str();
183 
184  } else {
185  stringstream ss;
186  ss << "[SiStripConfigDb::" << __func__ << "]"
187  << " Partition \"" << partition
188  << "\" already found in local cache, "
189  << " therefore cannot add new FED connections!";
190  edm::LogWarning(mlConfigDb_) << ss.str();
191  return;
192  }
193 
194 }
195 
196 // -----------------------------------------------------------------------------
197 //
198 void SiStripConfigDb::uploadFedConnections( std::string partition ) {
199 
200  if ( dbParams_.usingDbCache() ) {
202  << "[SiStripConfigDb::" << __func__ << "]"
203  << " Using database cache! No uploads allowed!";
204  return;
205  }
206 
207  if ( !deviceFactory(__func__) ) { return; }
208 
209  if ( connections_.empty() ) {
210  stringstream ss;
211  ss << "[SiStripConfigDb::" << __func__ << "]"
212  << " Found no cached FED connections, therefore no upload!";
213  edm::LogWarning(mlConfigDb_) << ss.str();
214  return;
215  }
216 
217  if ( dbParams_.usingDb() ) {
218 
219  try {
220 
221  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
222  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
223  for ( ; iter != jter; ++iter ) {
224 
225  if ( partition == "" || partition == iter->second.partitionName() ) {
226 
227  FedConnectionsRange range = connections_.find( iter->second.partitionName() );
228  if ( range != connections_.emptyRange() ) {
229 
230  FedConnectionsV conns( range.begin(), range.end() );
231 
232  SiStripPartition::Versions cabVersion = iter->second.cabVersion();
233  deviceFactory(__func__)->setConnectionDescriptions( conns,
234  iter->second.partitionName(),
235  &(cabVersion.first),
236  &(cabVersion.second),
237  true ); // new major version
238 
239  // Some debug
240  std::stringstream ss;
241  ss << "[SiStripConfigDb::" << __func__ << "]"
242  << " Uploaded " << conns.size()
243  << " FED connections to database for partition \""
244  << iter->second.partitionName() << "\".";
245  LogTrace(mlConfigDb_) << ss.str();
246 
247  } else {
248  stringstream ss;
249  ss << "[SiStripConfigDb::" << __func__ << "]"
250  << " Vector of FED connections is empty for partition \""
251  << iter->second.partitionName()
252  << "\", therefore aborting upload for this partition!";
253  edm::LogWarning(mlConfigDb_) << ss.str();
254  continue;
255  }
256 
257  } else {
258  // stringstream ss;
259  // ss << "[SiStripConfigDb::" << __func__ << "]"
260  // << " Cannot find partition \"" << partition
261  // << "\" in cached partitions list: \""
262  // << dbParams_.partitionNames( dbParams_.partitionNames() )
263  // << "\", therefore aborting upload for this partition!";
264  // edm::LogWarning(mlConfigDb_) << ss.str();
265  }
266 
267  }
268 
269  } catch (...) { handleException( __func__ ); }
270 
271  }
272 
273  allowCalibUpload_ = true;
274 
275 }
276 
277 // -----------------------------------------------------------------------------
278 //
279 void SiStripConfigDb::clearFedConnections( std::string partition ) {
280  LogTrace(mlConfigDb_) << "[SiStripConfigDb::" << __func__ << "]";
281 
282  if ( connections_.empty() ) {
283  stringstream ss;
284  ss << "[SiStripConfigDb::" << __func__ << "]"
285  << " Found no cached FED connections!";
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  FedConnections temporary_cache;
292  if ( partition == "" ) { temporary_cache = FedConnections(); }
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  FedConnectionsRange range = connections_.find( iter->second.partitionName() );
299  if ( range != connections_.emptyRange() ) {
300  temporary_cache.loadNext( partition, FedConnectionsV( range.begin(), range.end() ) );
301  } else {
302  // stringstream ss;
303  // ss << "[SiStripConfigDb::" << __func__ << "]"
304  // << " Cannot find partition \"" << iter->second.partitionName()
305  // << "\" in local cache!";
306  // edm::LogWarning(mlConfigDb_) << ss.str();
307  }
308  }
309  }
310  }
311 
312  // Delete objects in local cache for specified partition (or all if not specified)
313  FedConnectionsRange conns;
314  if ( partition == "" ) {
315  if ( !connections_.empty() ) {
316  conns = FedConnectionsRange( connections_.find( dbParams_.partitions().begin()->second.partitionName() ).begin(),
317  connections_.find( (--(dbParams_.partitions().end()))->second.partitionName() ).end() );
318  } else { conns = connections_.emptyRange(); }
319  } else {
320  SiStripDbParams::SiStripPartitions::const_iterator iter = dbParams_.partitions().begin();
321  SiStripDbParams::SiStripPartitions::const_iterator jter = dbParams_.partitions().end();
322  for ( ; iter != jter; ++iter ) { if ( partition == iter->second.partitionName() ) { break; } }
323  conns = connections_.find( iter->second.partitionName() );
324  }
325 
326  if ( conns != connections_.emptyRange() ) {
327  FedConnectionsV::const_iterator ifed = conns.begin();
328  FedConnectionsV::const_iterator jfed = conns.end();
329  for ( ; ifed != jfed; ++ifed ) { if ( *ifed ) { delete *ifed; } }
330  } else {
331  stringstream ss;
332  ss << "[SiStripConfigDb::" << __func__ << "]";
333  if ( partition == "" ) { ss << " Found no FED connections in local cache!"; }
334  else { ss << " Found no FED connections in local cache for partition \"" << partition << "\"!"; }
335  edm::LogWarning(mlConfigDb_) << ss.str();
336  }
337 
338  // Overwrite local cache with temporary cache
339  connections_ = temporary_cache;
340 
341 }
342 
343 // -----------------------------------------------------------------------------
344 //
345 void SiStripConfigDb::printFedConnections( std::string partition ) {
346 
347  std::stringstream ss;
348  ss << "[SiStripConfigDb::" << __func__ << "]"
349  << " Contents of FedConnections container:" << std::endl;
350  ss << " Number of partitions: " << connections_.size() << std::endl;
351 
352  // Loop through partitions
353  uint16_t cntr = 0;
354  FedConnections::const_iterator iconn = connections_.begin();
355  FedConnections::const_iterator jconn = connections_.end();
356  for ( ; iconn != jconn; ++iconn ) {
357 
358  cntr++;
359  if ( partition == "" || partition == iconn->first ) {
360 
361  ss << " Partition number : " << cntr << " (out of " << connections_.size() << ")" << std::endl;
362  ss << " Partition name : \"" << iconn->first << "\"" << std::endl;
363  ss << " Num of connections : " << iconn->second.size() << std::endl;
364 
365  // Extract FED ids and channels
366  std::map< uint16_t, vector<uint16_t> > feds;
367  FedConnectionsV::const_iterator iter = iconn->second.begin();
368  FedConnectionsV::const_iterator jter = iconn->second.end();
369  for ( ; iter != jter; ++iter ) {
370  if ( *iter ) {
371  uint16_t fed_id = (*iter)->getFedId();
372  uint16_t fed_ch = (*iter)->getFedChannel();
373  if ( find( feds[fed_id].begin(), feds[fed_id].end(), fed_ch ) == feds[fed_id].end() ) {
374  feds[fed_id].push_back( fed_ch );
375  }
376  }
377  }
378 
379  // Sort contents
380  std::map< uint16_t, std::vector<uint16_t> > tmp;
381  std::map< uint16_t, std::vector<uint16_t> >::const_iterator ii = feds.begin();
382  std::map< uint16_t, std::vector<uint16_t> >::const_iterator jj = feds.end();
383  for ( ; ii != jj; ++ii ) {
384  std::vector<uint16_t> temp = ii->second;
385  std::sort( temp.begin(), temp.end() );
386  std::vector<uint16_t>::const_iterator iii = temp.begin();
387  std::vector<uint16_t>::const_iterator jjj = temp.end();
388  for ( ; iii != jjj; ++iii ) { tmp[ii->first].push_back( *iii ); }
389  }
390  feds.clear();
391  feds = tmp;
392 
393  // Print FED ids and channels
394  std::map< uint16_t, std::vector<uint16_t> >::const_iterator ifed = feds.begin();
395  std::map< uint16_t, std::vector<uint16_t> >::const_iterator jfed = feds.end();
396  for ( ; ifed != jfed; ++ifed ) {
397  ss << " Found " << std::setw(2) << ifed->second.size()
398  << " channels for FED id " << std::setw(3) << ifed->first << " : ";
399  if ( !ifed->second.empty() ) {
400  uint16_t first = ifed->second.front();
401  uint16_t last = ifed->second.front();
402  std::vector<uint16_t>::const_iterator ichan = ifed->second.begin();
403  std::vector<uint16_t>::const_iterator jchan = ifed->second.end();
404  for ( ; ichan != jchan; ++ichan ) {
405  if ( ichan != ifed->second.begin() ) {
406  if ( *ichan != last+1 ) {
407  ss << std::setw(2) << first << "->" << std::setw(2) << last << ", ";
408  if ( ichan != ifed->second.end() ) { first = *(ichan+1); }
409  }
410  }
411  last = *ichan;
412  }
413  if ( first != last ) { ss << std::setw(2) << first << "->" << std::setw(2) << last; }
414  ss << std::endl;
415  }
416  }
417 
418  }
419 
420  }
421 
422  LogTrace(mlConfigDb_) << ss.str();
423 
424 }
static std::string defaultPartitionName_
void loadNext(K const &k, std::vector< T > const &v)
Definition: MapOfVectors.h:97
void uploadFedConnections(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
FedConnections::range FedConnectionsRange
static const char mlConfigDb_[]
int np
Definition: AMPTWrapper.h:33
FedConnectionsRange getFedConnections(std::string partition="")
#define end
Definition: vmac.h:38
bool first
Definition: L1TdeRCT.cc:94
#define LogTrace(id)
void clearFedConnections(std::string partition="")
void printFedConnections(std::string partition="")
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
#define begin
Definition: vmac.h:31
std::vector< FedConnection * > FedConnectionsV
void addFedConnections(std::string partition, FedConnectionsV &)