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