CMS 3D CMS Logo

DTUserKeyedConfigHandler.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  *
4  * \author Paolo Ronchese INFN Padova
5  *
6  */
7 
8 //-----------------------
9 // This Class' Header --
10 //-----------------------
12 
13 //-------------------------------
14 // Collaborating Class Headers --
15 //-------------------------------
18 
19 
24 
25 #include "RelationalAccess/ISessionProxy.h"
26 #include "RelationalAccess/ITransaction.h"
27 #include "RelationalAccess/ISchema.h"
28 #include "RelationalAccess/ITable.h"
29 #include "RelationalAccess/ICursor.h"
30 #include "RelationalAccess/IQuery.h"
31 #include "CoralBase/AttributeList.h"
32 #include "CoralBase/AttributeSpecification.h"
33 #include "CoralBase/Attribute.h"
34 
35 //---------------
36 // C++ Headers --
37 //---------------
38 
39 #include <iostream>
40 #include <memory>
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  onlineAuthSys( ps.getUntrackedParameter<int>( "onlineAuthSys",1 ) ),
57  brickContainer( ps.getParameter<std::string> ( "container" ) ),
58  writeKeys( ps.getParameter<bool> ( "writeKeys" ) ),
59  writeData( ps.getParameter<bool> ( "writeData" ) ),
60  connection(),
61  isession() {
62  std::cout << " PopCon application for DT configuration export "
63  << onlineAuthentication << std::endl;
64 
65  std::vector<edm::ParameterSet> dtConfigKeys (
66  ps.getParameter< std::vector<edm::ParameterSet> >( "DTConfigKeys" )
67  );
68  std::vector<edm::ParameterSet>::const_iterator iter = dtConfigKeys.begin();
69  std::vector<edm::ParameterSet>::const_iterator iend = dtConfigKeys.end();
70  while ( iter != iend ) {
71  const edm::ParameterSet& cp = *iter++;
72  int configType = cp.getUntrackedParameter<int>( "configType" );
73  int configKey = cp.getUntrackedParameter<int>( "configKey" );
74  std::cout << "config: "
75  << configType << " -> "
76  << configKey << std::endl;
77  DTConfigKey userKey;
78  userKey.confType = configType;
79  userKey.confKey = configKey;
80  userConf.push_back( userKey );
81  }
82 }
83 
84 //--------------
85 // Destructor --
86 //--------------
88 }
89 
90 //--------------
91 // Operations --
92 //--------------
94 
95  //to access the information on the tag inside the offline database:
96  cond::TagInfo const & ti = tagInfo();
97  unsigned int last = ti.lastInterval.first;
98  std::cout << "last configuration key already copied for run: "
99  << last << std::endl;
100 
101  std::vector<DTConfigKey> lastKey;
102 // std::cout << "check for last " << std::endl;
103  if ( last == 0 ) {
104  DTCCBConfig* dummyConf = new DTCCBConfig( dataTag );
105  dummyConf->setStamp( 0 );
106  dummyConf->setFullKey( lastKey );
107  cond::Time_t snc = 1;
108  if ( writeKeys && ( dataRun > 1 ) )
109  m_to_transfer.push_back( std::make_pair( dummyConf, snc ) );
110  }
111  else {
112  std::cout << "get last payload" << std::endl;
113  Ref payload = lastPayload();
114  std::cout << "get last full key" << std::endl;
115  lastKey = payload->fullKey();
116  std::cout << "last key: " << std::endl;
117  std::vector<DTConfigKey>::const_iterator keyIter = lastKey.begin();
118  std::vector<DTConfigKey>::const_iterator keyIend = lastKey.end();
119  while ( keyIter != keyIend ) {
120  const DTConfigKey& keyList = *keyIter++;
121  std::cout << keyList.confType << " : " << keyList.confKey << std::endl;
122  }
123  }
124 
125  std::cout << "configure DbConnection" << std::endl;
126  // conn->configure( cond::CmsDefaults );
130  std::cout << "create/open DbSession" << std::endl;
132  std::cout << "start transaction" << std::endl;
133  isession->transaction().start( true );
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::unique_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::unique_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::unique_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 = nullptr;
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::unique_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 = nullptr;
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 = nullptr;
272  if ( keyIter != keyIend ) mapPtr = keyIter->second;
273  if ( mapPtr == nullptr ) 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 == nullptr ) 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 
325  isession->transaction().commit();
326 
327  return;
328 
329 }
330 
331 
333  const std::map<int,bool>& userBricks ) {
334 
335  std::cout << "open POOL out db " << std::endl;
337 
338  std::map<int,bool>::const_iterator uBrkIter = userBricks.begin();
339  std::map<int,bool>::const_iterator uBrkIend = userBricks.end();
340 
341  coral::ITable& brickConfigTable =
342  isession->nominalSchema().tableHandle( "CFGBRICKS" );
343  std::unique_ptr<coral::IQuery>
344  brickConfigQuery( brickConfigTable.newQuery() );
345  brickConfigQuery->addToOutputList( "BRKID" );
346  brickConfigQuery->addToOutputList( "BRKNAME" );
347  coral::ICursor& brickConfigCursor = brickConfigQuery->execute();
348  DTKeyedConfig* brickData = nullptr;
349  std::vector<int> missingList;
350  std::vector<unsigned long long> checkedKeys;
351  while( brickConfigCursor.next() ) {
352  const coral::AttributeList& row = brickConfigCursor.currentRow();
353  int brickConfigId = row["BRKID"].data<int>();
354  uBrkIter = userBricks.find( brickConfigId );
355  if ( uBrkIter == uBrkIend ) continue;
356  if ( !( uBrkIter->second ) ) continue;
357  std::string brickConfigName = row["BRKNAME"].data<std::string>();
358  std::cout << "brick " << brickConfigId
359  << " : " << brickConfigName << std::endl;
360  checkedKeys.push_back( brickConfigId );
361  bool brickFound = false;
362  try {
363  std::cout << "load brick " << checkedKeys[0] << std::endl;
364  std::cout << "key list " << keyList << std::endl;
365  keyList->load( checkedKeys );
366  std::cout << "get brick..." << std::endl;
367  std::shared_ptr<DTKeyedConfig> brickCheck =
368  keyList->get<DTKeyedConfig>( 0 );
369  if ( brickCheck.get() ) {
370  brickFound = ( brickCheck->getId() == brickConfigId );
371  }
372  }
373  catch ( std::exception const& ) {
374  }
375  if ( !brickFound ) {
376  std::cout << "brick " << brickConfigId << " missing, copy request"
377  << std::endl;
378  missingList.push_back( brickConfigId );
379  }
380  checkedKeys.clear();
381  }
382  keyList->load( checkedKeys );
383 
384  std::vector<int>::const_iterator brickIter = missingList.begin();
385  std::vector<int>::const_iterator brickIend = missingList.end();
386  while ( brickIter != brickIend ) {
387  int brickConfigId = *brickIter++;
388  coral::AttributeList bindVariableList;
389  bindVariableList.extend( "brickId", typeid(int) );
390  bindVariableList["brickId"].data<int>() = brickConfigId;
391  std::unique_ptr<coral::IQuery>
392  brickDataQuery( isession->nominalSchema().newQuery() );
393  brickDataQuery->addToTableList( "CFGRELATIONS" );
394  brickDataQuery->addToTableList( "CONFIGCMDS" );
396  brickCondition = "CONFIGCMDS.CMDID=CFGRELATIONS.CMDID";
397  brickCondition += " and CFGRELATIONS.BRKID=:brickId";
398  brickDataQuery->addToOutputList( "CFGRELATIONS.BRKID" );
399  brickDataQuery->addToOutputList( "CONFIGCMDS.CONFDATA" );
400  brickDataQuery->setCondition( brickCondition, bindVariableList );
401  coral::ICursor& brickDataCursor = brickDataQuery->execute();
402  brickData = new DTKeyedConfig();
403  brickData->setId( brickConfigId );
404  while( brickDataCursor.next() ) {
405  const coral::AttributeList& row = brickDataCursor.currentRow();
406  brickData->add( row["CONFIGCMDS.CONFDATA"].data<std::string>() );
407  }
408  cond::KeyedElement k( brickData, brickConfigId );
409  std::cout << "now writing brick: " << brickConfigId << std::endl;
410  outdb->writeOne( k.m_obj, k.m_key, brickContainer );
411  }
412 
413  return;
414 
415 }
416 
417 
419  return dataTag;
420 }
421 
422 
424  const std::vector<DTConfigKey>& cfgl,
425  const std::vector<DTConfigKey>& cfgr ) {
426  if ( cfgl.size() != cfgr.size() ) return false;
427  std::map<int,int> lmap;
428  std::vector<DTConfigKey>::const_iterator lIter = cfgl.begin();
429  std::vector<DTConfigKey>::const_iterator lIend = cfgl.end();
430  while ( lIter != lIend ) {
431  const DTConfigKey& entry = *lIter++;
432  lmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
433  }
434  std::map<int,int> rmap;
435  std::vector<DTConfigKey>::const_iterator rIter = cfgr.begin();
436  std::vector<DTConfigKey>::const_iterator rIend = cfgr.end();
437  while ( rIter != rIend ) {
438  const DTConfigKey& entry = *rIter++;
439  rmap.insert( std::pair<int,int>( entry.confType, entry.confKey ) );
440  }
441  std::map<int,int>::const_iterator lmIter = lmap.begin();
442  std::map<int,int>::const_iterator lmIend = lmap.end();
443  std::map<int,int>::const_iterator rmIter = rmap.begin();
444  std::map<int,int>::const_iterator rmIend = rmap.end();
445  while ( ( lmIter != lmIend ) &&
446  ( rmIter != rmIend ) ) {
447  const std::pair<int,int>& lEntry = *lmIter++;
448  const std::pair<int,int>& rEntry = *rmIter++;
449  if ( lEntry.first != rEntry.first ) return false;
450  if ( lEntry.second != rEntry.second ) return false;
451  }
452  return true;
453 }
454 
455 
457  std::vector<DTConfigKey>::const_iterator iter = userConf.begin();
458  std::vector<DTConfigKey>::const_iterator iend = userConf.end();
459  while ( iter != iend ) {
460  const DTConfigKey& entry = *iter++;
461  if ( entry.confKey == key ) return false;
462  }
463  return true;
464 }
465 
467  keyList = list;
468 }
469 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int wheelId
Definition: DTCCBConfig.h:45
void setStamp(int s)
Definition: DTCCBConfig.cc:174
void add(const std::string &data)
std::vector< DTConfigKey > userConf
static bool sameConfigList(const std::vector< DTConfigKey > &cfgl, const std::vector< DTConfigKey > &cfgr)
const_iterator end() const
Definition: DTCCBConfig.cc:291
int sectorId
Definition: DTCCBConfig.h:47
cond::ValidityInterval lastInterval
Definition: Types.h:75
static cond::persistency::KeyList * keyList
BaseKeyed * m_obj
Definition: KeyedElement.h:35
unsigned long long Time_t
Definition: Time.h:16
int stationId
Definition: DTCCBConfig.h:46
void setAuthenticationSystem(int authSysCode)
void writeOne(T *payload, Time_t time, const std::string &recordName, bool withlogging=false)
std::string id() const override
DTUserKeyedConfigHandler(const edm::ParameterSet &ps)
int k[5][pyjets_maxn]
void setId(int id)
std::shared_ptr< T > get(size_t n) const
Definition: KeyList.h:40
void chkConfigList(const std::map< int, bool > &userBricks)
std::shared_ptr< coral::ISessionProxy > isession
void setFullKey(const std::vector< DTConfigKey > &)
Definition: DTCCBConfig.cc:169
void load(const std::vector< unsigned long long > &keys)
Definition: KeyList.cc:14
cond::persistency::ConnectionPool connection
const_iterator begin() const
Definition: DTCCBConfig.cc:286
void setAuthenticationPath(const std::string &p)
std::shared_ptr< coral::ISessionProxy > createCoralSession(const std::string &connectionString, bool writeCapable=false)
static void setList(cond::persistency::KeyList *list)
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:238
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
cond::TagInfo_t const & tagInfo() const