CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTUserKeyedConfigHandler.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * $Date: 2012/02/15 16:04:16 $
5  * $Revision: 1.8 $
6  * \author Paolo Ronchese INFN Padova
7  *
8  */
9 
10 //-----------------------
11 // This Class' Header --
12 //-----------------------
14 
15 //-------------------------------
16 // Collaborating Class Headers --
17 //-------------------------------
20 
21 
23 
28 
29 #include "RelationalAccess/ISchema.h"
30 #include "RelationalAccess/ITable.h"
31 #include "RelationalAccess/ICursor.h"
32 #include "RelationalAccess/IQuery.h"
33 #include "CoralBase/AttributeList.h"
34 #include "CoralBase/AttributeSpecification.h"
35 #include "CoralBase/Attribute.h"
36 
37 //---------------
38 // C++ Headers --
39 //---------------
40 
41 
42 //-------------------
43 // Initializations --
44 //-------------------
46 
47 //----------------
48 // Constructors --
49 //----------------
51  dataRun( ps.getParameter<int> ( "run" ) ),
52  dataTag( ps.getParameter<std::string> ( "tag" ) ),
53  onlineConnect( ps.getParameter<std::string> ( "onlineDB" ) ),
54  onlineAuthentication( ps.getParameter<std::string> (
55  "onlineAuthentication" ) ),
56  brickContainer( ps.getParameter<std::string> ( "container" ) ),
57  writeKeys( ps.getParameter<bool> ( "writeKeys" ) ),
58  writeData( ps.getParameter<bool> ( "writeData" ) ),
59  connection(),
60  isession() {
61  std::cout << " PopCon application for DT configuration export "
62  << onlineAuthentication << std::endl;
63 
64  std::vector<edm::ParameterSet> dtConfigKeys (
65  ps.getParameter< std::vector<edm::ParameterSet> >( "DTConfigKeys" )
66  );
67  std::vector<edm::ParameterSet>::const_iterator iter = dtConfigKeys.begin();
68  std::vector<edm::ParameterSet>::const_iterator iend = dtConfigKeys.end();
69  while ( iter != iend ) {
70  const edm::ParameterSet& cp = *iter++;
71  int configType = cp.getUntrackedParameter<int>( "configType" );
72  int configKey = cp.getUntrackedParameter<int>( "configKey" );
73  std::cout << "config: "
74  << configType << " -> "
75  << configKey << std::endl;
76  DTConfigKey userKey;
77  userKey.confType = configType;
78  userKey.confKey = configKey;
79  userConf.push_back( userKey );
80  }
81 }
82 
83 //--------------
84 // Destructor --
85 //--------------
87 }
88 
89 //--------------
90 // Operations --
91 //--------------
93 
94  //to access the information on the tag inside the offline database:
95  cond::TagInfo const & ti = tagInfo();
96  unsigned int last = ti.lastInterval.first;
97  std::cout << "last configuration key already copied for run: "
98  << last << std::endl;
99 
100  std::vector<DTConfigKey> lastKey;
101 // std::cout << "check for last " << std::endl;
102  if ( last == 0 ) {
103  DTCCBConfig* dummyConf = new DTCCBConfig( dataTag );
104  dummyConf->setStamp( 0 );
105  dummyConf->setFullKey( lastKey );
106  cond::Time_t snc = 1;
107  if ( writeKeys && ( dataRun > 1 ) )
108  m_to_transfer.push_back( std::make_pair( dummyConf, snc ) );
109  }
110  else {
111  std::cout << "get last payload" << std::endl;
112  Ref payload = lastPayload();
113  std::cout << "get last full key" << std::endl;
114  lastKey = payload->fullKey();
115  std::cout << "last key: " << std::endl;
116  std::vector<DTConfigKey>::const_iterator keyIter = lastKey.begin();
117  std::vector<DTConfigKey>::const_iterator keyIend = lastKey.end();
118  while ( keyIter != keyIend ) {
119  const DTConfigKey& keyList = *keyIter++;
120  std::cout << keyList.confType << " : " << keyList.confKey << std::endl;
121  }
122  }
123 
124  std::cout << "configure DbConnection" << std::endl;
125  // conn->configure( cond::CmsDefaults );
128  std::cout << "create DbSession" << std::endl;
130  std::cout << "open session" << std::endl;
132  std::cout << "start transaction" << std::endl;
134 
135  // get ccb identifiers map
136  std::cout << "retrieve CCB map" << std::endl;
137  std::map<int,DTCCBId> ccbMap;
138  coral::ITable& ccbMapTable =
139  isession.nominalSchema().tableHandle( "CCBMAP" );
140  std::auto_ptr<coral::IQuery>
141  ccbMapQuery( ccbMapTable.newQuery() );
142  ccbMapQuery->addToOutputList( "CCBID" );
143  ccbMapQuery->addToOutputList( "WHEEL" );
144  ccbMapQuery->addToOutputList( "SECTOR" );
145  ccbMapQuery->addToOutputList( "STATION" );
146  coral::ICursor& ccbMapCursor = ccbMapQuery->execute();
147  while( ccbMapCursor.next() ) {
148  const coral::AttributeList& row = ccbMapCursor.currentRow();
149  int ccb = row["CCBID" ].data<int>();
150  int wheel = row["WHEEL" ].data<int>();
151  int sector = row["SECTOR" ].data<int>();
152  int station = row["STATION"].data<int>();
153  DTCCBId ccbId;
154  ccbId. wheelId = wheel;
155  ccbId.stationId = station;
156  ccbId. sectorId = sector;
157  ccbMap.insert( std::pair<int,DTCCBId>( ccb, ccbId ) );
158  }
159 
160  // get brick types
161  std::cout << "retrieve brick types" << std::endl;
162  std::map<int,int> bktMap;
163  coral::AttributeList emptyBindVariableList;
164  std::auto_ptr<coral::IQuery>
165  brickTypeQuery( isession.nominalSchema().newQuery() );
166  brickTypeQuery->addToTableList( "CFGBRICKS" );
167  brickTypeQuery->addToTableList( "BRKT2CSETT" );
168  std::string bTypeCondition = "CFGBRICKS.BRKTYPE=BRKT2CSETT.BRKTYPE";
169  brickTypeQuery->addToOutputList( "CFGBRICKS.BRKID" );
170  brickTypeQuery->addToOutputList( "BRKT2CSETT.CSETTYPE" );
171  brickTypeQuery->setCondition( bTypeCondition, emptyBindVariableList );
172  coral::ICursor& brickTypeCursor = brickTypeQuery->execute();
173  while( brickTypeCursor.next() ) {
174  const coral::AttributeList& row = brickTypeCursor.currentRow();
175  int id = row["CFGBRICKS.BRKID" ].data<int>();
176  int bt = row["BRKT2CSETT.CSETTYPE"].data<short>();
177 // std::cout << "brick " << id << " type " << bt << std::endl;
178 // @@FIX - TEMPORARY PATCH
179 // if ( bt > 3 ) bt = 3;
180  bktMap.insert( std::pair<int,int>( id, bt ) );
181  }
182 
183  // get ccb config keys
184  std::cout << "retrieve CCB configuration keys" << std::endl;
185  std::map<int,std::map<int,int>*> keyMap;
186  std::map<int,int> cckMap;
187  coral::ITable& ccbRelTable =
188  isession.nominalSchema().tableHandle( "CCBRELATIONS" );
189  std::auto_ptr<coral::IQuery>
190  ccbRelQuery( ccbRelTable.newQuery() );
191  ccbRelQuery->addToOutputList( "CONFKEY" );
192  ccbRelQuery->addToOutputList( "CCBID" );
193  ccbRelQuery->addToOutputList( "CONFCCBKEY" );
194  coral::ICursor& ccbRelCursor = ccbRelQuery->execute();
195  // loop over all full configurations
196  while( ccbRelCursor.next() ) {
197  const coral::AttributeList& row = ccbRelCursor.currentRow();
198  int cfg = row["CONFKEY" ].data<int>();
199  int ccb = row["CCBID" ].data<int>();
200  int key = row["CONFCCBKEY"].data<int>();
201  // check for used configurations
202 // if ( cfgMap.find( cfg ) == cfgMap.end() ) continue;
203  if ( userDiscardedKey( cfg ) ) continue;
204  std::map<int,std::map<int,int>*>::const_iterator keyIter =
205  keyMap.find( cfg );
206  std::map<int,std::map<int,int>*>::const_iterator keyIend =
207  keyMap.end();
208  std::map<int,int>* mapPtr = 0;
209  // check for new full configuration
210  if ( keyIter != keyIend ) mapPtr = keyIter->second;
211  else keyMap.insert(
212  std::pair<int,std::map<int,int>*>( cfg,
213  mapPtr = new std::map<int,int> ) );
214  // store ccb config key
215  std::map<int,int>& mapRef( *mapPtr );
216  mapRef.insert( std::pair<int,int>( ccb, key ) );
217  // check for new ccb config key
218  if ( cckMap.find( key ) == cckMap.end() )
219  cckMap.insert( std::pair<int,int>( key, ccb ) );
220  }
221 
222  // get brick keys
223  std::cout << "retrieve CCB configuration bricks" << std::endl;
224  std::map<int,std::vector<int>*> brkMap;
225  coral::ITable& confBrickTable =
226  isession.nominalSchema().tableHandle( "CFG2BRKREL" );
227  std::auto_ptr<coral::IQuery>
228  confBrickQuery( confBrickTable.newQuery() );
229  confBrickQuery->addToOutputList( "CONFID" );
230  confBrickQuery->addToOutputList( "BRKID" );
231  coral::ICursor& confBrickCursor = confBrickQuery->execute();
232  // loop over all brick keys
233  while( confBrickCursor.next() ) {
234  const coral::AttributeList& row = confBrickCursor.currentRow();
235  int key = row["CONFID"].data<int>();
236  int brk = row["BRKID" ].data<int>();
237  // check for used ccb config key
238  if ( cckMap.find( key ) == cckMap.end() ) continue;
239  std::map<int,std::vector<int>*>::const_iterator brkIter =
240  brkMap.find( key );
241  std::map<int,std::vector<int>*>::const_iterator brkIend =
242  brkMap.end();
243  // check for new ccb config key
244  std::vector<int>* brkPtr = 0;
245  if ( brkIter != brkIend ) brkPtr = brkIter->second;
246  else brkMap.insert(
247  std::pair<int,std::vector<int>*>( key,
248  brkPtr = new std::vector<int> ) );
249  // store brick key
250  brkPtr->push_back( brk );
251  }
252 
253  // set run and full configuration in payload
254  DTCCBConfig* fullConf = new DTCCBConfig( dataTag );
255  fullConf->setStamp( 1 );
256  fullConf->setFullKey( userConf );
257  std::map<int,bool> userBricks;
258  std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
259  std::map<int,bool>::const_iterator uBrkIend = userBricks.end();
260  std::vector<DTConfigKey>::const_iterator cfgIter = userConf.begin();
261  std::vector<DTConfigKey>::const_iterator cfgIend = userConf.end();
262  while ( cfgIter != cfgIend ) {
263  const DTConfigKey& cfgEntry = *cfgIter++;
264  int cft = cfgEntry.confType;
265  int cfg = cfgEntry.confKey;
266  // retrieve ccb config map
267  std::map<int,std::map<int,int>*>::const_iterator keyIter =
268  keyMap.find( cfg );
269  std::map<int,std::map<int,int>*>::const_iterator keyIend =
270  keyMap.end();
271  std::map<int,int>* mapPtr = 0;
272  if ( keyIter != keyIend ) mapPtr = keyIter->second;
273  if ( mapPtr == 0 ) continue;
274  std::map<int,int>::const_iterator ccbIter = mapPtr->begin();
275  std::map<int,int>::const_iterator ccbIend = mapPtr->end();
276  while ( ccbIter != ccbIend ) {
277  const std::pair<int,int>& ccbEntry = *ccbIter++;
278  // get ccb config key
279  int ccb = ccbEntry.first;
280  int key = ccbEntry.second;
281  // retrieve chamber id
282  std::map<int,DTCCBId>::const_iterator ccbIter = ccbMap.find( ccb );
283  std::map<int,DTCCBId>::const_iterator ccbIend = ccbMap.end();
284  if ( ccbIter == ccbIend ) continue;
285  const DTCCBId& chaId = ccbIter->second;
286  // retrieve brick id list
287  std::map<int,std::vector<int>*>::const_iterator brkIter =
288  brkMap.find( key );
289  std::map<int,std::vector<int>*>::const_iterator brkIend =
290  brkMap.end();
291  if ( brkIter == brkIend ) continue;
292  std::vector<int>* brkPtr = brkIter->second;
293  if ( brkPtr == 0 ) continue;
294  // brick id lists in payload
295  std::vector<int> bkList;
296  bkList.reserve( 20 );
297  std::map<int,int>::const_iterator bktIter = bktMap.begin();
298  std::map<int,int>::const_iterator bktIend = bktMap.end();
299  std::vector<int>::const_iterator bkiIter = brkPtr->begin();
300  std::vector<int>::const_iterator bkiIend = brkPtr->end();
301  while ( bkiIter != bkiIend ) {
302  int brickId = *bkiIter++;
303  bktIter = bktMap.find( brickId );
304  if ( bktIter == bktIend ) continue;
305  if ( bktIter->second == cft ) {
306  bkList.push_back( brickId );
307  uBrkIter = userBricks.find( brickId );
308  if ( uBrkIter == uBrkIend )
309  userBricks.insert( std::pair<int,bool>( brickId, true ) );
310  }
311  }
312  fullConf->appendConfigKey( chaId.wheelId,
313  chaId.stationId,
314  chaId.sectorId,
315  bkList );
316  }
317  }
318  cond::Time_t snc = dataRun;
319  if ( writeKeys ) m_to_transfer.push_back( std::make_pair( fullConf, snc ) );
320  std::cout << "writing payload : " << sizeof( *fullConf )
321  << " ( " << ( fullConf->end() - fullConf->begin() )
322  << " ) " << std::endl;
323  if ( writeData ) chkConfigList( userBricks );
324 
326  isession.close();
327 
328  return;
329 
330 }
331 
332 
334  const std::map<int,bool>& userBricks ) {
335 
336  std::cout << "open POOL out db " << std::endl;
338 
339  std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
340  std::map<int,bool>::const_iterator uBrkIend = userBricks.end();
341 
342  coral::ITable& brickConfigTable =
343  isession.nominalSchema().tableHandle( "CFGBRICKS" );
344  std::auto_ptr<coral::IQuery>
345  brickConfigQuery( brickConfigTable.newQuery() );
346  brickConfigQuery->addToOutputList( "BRKID" );
347  brickConfigQuery->addToOutputList( "BRKNAME" );
348  coral::ICursor& brickConfigCursor = brickConfigQuery->execute();
349  DTKeyedConfig* brickData = 0;
350  std::vector<int> missingList;
351  std::vector<unsigned long long> checkedKeys;
352  while( brickConfigCursor.next() ) {
353  const coral::AttributeList& row = brickConfigCursor.currentRow();
354  int brickConfigId = row["BRKID"].data<int>();
355  uBrkIter = userBricks.find( brickConfigId );
356  if ( uBrkIter == uBrkIend ) continue;
357  if ( !( uBrkIter->second ) ) continue;
358  std::string brickConfigName = row["BRKNAME"].data<std::string>();
359  std::cout << "brick " << brickConfigId
360  << " : " << brickConfigName << std::endl;
361  checkedKeys.push_back( brickConfigId );
362  bool brickFound = false;
363  try {
364  std::cout << "load brick " << checkedKeys[0] << std::endl;
365  std::cout << "key list " << keyList << std::endl;
366  keyList->load( checkedKeys );
367  std::cout << "get brick..." << std::endl;
368  const DTKeyedConfig* brickCheck =
369  keyList->get<DTKeyedConfig>( 0 );
370  if ( brickCheck != 0 ) {
371  brickFound = ( brickCheck->getId() == brickConfigId );
372  }
373  }
374  catch ( std::exception e ) {
375  }
376  if ( !brickFound ) {
377  std::cout << "brick " << brickConfigId << " missing, copy request"
378  << std::endl;
379  missingList.push_back( brickConfigId );
380  }
381  checkedKeys.clear();
382  }
383  keyList->load( checkedKeys );
384 
385  std::vector<int>::const_iterator brickIter = missingList.begin();
386  std::vector<int>::const_iterator brickIend = missingList.end();
387  while ( brickIter != brickIend ) {
388  int brickConfigId = *brickIter++;
389  coral::AttributeList bindVariableList;
390  bindVariableList.extend( "brickId", typeid(int) );
391  bindVariableList["brickId"].data<int>() = brickConfigId;
392  std::auto_ptr<coral::IQuery>
393  brickDataQuery( isession.nominalSchema().newQuery() );
394  brickDataQuery->addToTableList( "CFGRELATIONS" );
395  brickDataQuery->addToTableList( "CONFIGCMDS" );
397  brickCondition = "CONFIGCMDS.CMDID=CFGRELATIONS.CMDID";
398  brickCondition += " and CFGRELATIONS.BRKID=:brickId";
399  brickDataQuery->addToOutputList( "CFGRELATIONS.BRKID" );
400  brickDataQuery->addToOutputList( "CONFIGCMDS.CONFDATA" );
401  brickDataQuery->setCondition( brickCondition, bindVariableList );
402  coral::ICursor& brickDataCursor = brickDataQuery->execute();
403  brickData = new DTKeyedConfig();
404  brickData->setId( brickConfigId );
405  while( brickDataCursor.next() ) {
406  const coral::AttributeList& row = brickDataCursor.currentRow();
407  brickData->add( row["CONFIGCMDS.CONFDATA"].data<std::string>() );
408  }
409  cond::KeyedElement k( brickData, brickConfigId );
410  std::cout << "now writing brick: " << brickConfigId << std::endl;
411  outdb->writeOne( k.m_obj, k.m_key, brickContainer );
412  }
413 
414  return;
415 
416 }
417 
418 
420  return dataTag;
421 }
422 
423 
425  const std::vector<DTConfigKey>& cfgl,
426  const std::vector<DTConfigKey>& cfgr ) {
427  if ( cfgl.size() != cfgr.size() ) return false;
428  std::map<int,int> lmap;
429  std::vector<DTConfigKey>::const_iterator lIter = cfgl.begin();
430  std::vector<DTConfigKey>::const_iterator lIend = cfgl.end();
431  while ( lIter != lIend ) {
432  const DTConfigKey& entry = *lIter++;
433  lmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
434  }
435  std::map<int,int> rmap;
436  std::vector<DTConfigKey>::const_iterator rIter = cfgr.begin();
437  std::vector<DTConfigKey>::const_iterator rIend = cfgr.end();
438  while ( rIter != rIend ) {
439  const DTConfigKey& entry = *rIter++;
440  rmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
441  }
442  std::map<int,int>::const_iterator lmIter = lmap.begin();
443  std::map<int,int>::const_iterator lmIend = lmap.end();
444  std::map<int,int>::const_iterator rmIter = rmap.begin();
445  std::map<int,int>::const_iterator rmIend = rmap.end();
446  while ( ( lmIter != lmIend ) &&
447  ( rmIter != rmIend ) ) {
448  const std::pair<int,int>& lEntry = *lmIter++;
449  const std::pair<int,int>& rEntry = *rmIter++;
450  if ( lEntry.first != rEntry.first ) return false;
451  if ( lEntry.second != rEntry.second ) return false;
452  }
453  return true;
454 }
455 
456 
458  std::vector<DTConfigKey>::const_iterator iter = userConf.begin();
459  std::vector<DTConfigKey>::const_iterator iend = userConf.end();
460  while ( iter != iend ) {
461  const DTConfigKey& entry = *iter++;
462  if ( entry.confKey == key ) return false;
463  }
464  return true;
465 }
466 
468  keyList = list;
469 }
470 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int wheelId
Definition: DTCCBConfig.h:43
void load(std::vector< unsigned long long > const &keys)
Definition: KeyList.cc:10
void setStamp(int s)
Definition: DTCCBConfig.cc:197
DbTransaction & transaction()
Definition: DbSession.cc:189
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:144
void add(const std::string &data)
std::vector< DTConfigKey > userConf
int commit()
commit transaction.
static void setList(cond::KeyList *list)
DbConnectionConfiguration & configuration()
Definition: DbConnection.cc:89
static bool sameConfigList(const std::vector< DTConfigKey > &cfgl, const std::vector< DTConfigKey > &cfgr)
const_iterator end() const
Definition: DTCCBConfig.cc:345
int sectorId
Definition: DTCCBConfig.h:45
int start(bool readOnly=false)
start transaction
BaseKeyed * m_obj
Definition: KeyedElement.h:35
unsigned long long Time_t
Definition: Time.h:16
int stationId
Definition: DTCCBConfig.h:44
std::pair< std::string, MonitorElement * > entry
Definition: ME_MAP.h:8
T const * get(int n) const
Definition: KeyList.h:39
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
DTUserKeyedConfigHandler(const edm::ParameterSet &ps)
int k[5][pyjets_maxn]
DbSession createSession() const
Definition: DbConnection.cc:72
coral::ISchema & nominalSchema()
Definition: DbSession.cc:228
void setId(int id)
void setAuthenticationPath(const std::string &p)
int getId() const
void chkConfigList(const std::map< int, bool > &userBricks)
list key
Definition: combine.py:13
cond::ValidityInterval lastInterval
Definition: TagInfo.h:11
tuple cout
Definition: gather_cfg.py:121
void setFullKey(const std::vector< DTConfigKey > &)
Definition: DTCCBConfig.cc:192
const_iterator begin() const
Definition: DTCCBConfig.cc:340
cond::Time_t m_key
Definition: KeyedElement.h:37
int appendConfigKey(int wheelId, int stationId, int sectorId, const std::vector< int > &confKey)
Definition: DTCCBConfig.cc:278
static cond::KeyList * keyList
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run