CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CondDBESSource.cc
Go to the documentation of this file.
1 //
2 // Package: CondCore/ESSources
3 // Module: CondDBESSource
4 //
5 // Description: <one line class summary>
6 //
7 // Implementation:
8 // <Notes on implementation>
9 //
10 // Author: Zhen Xie
11 //
12 #include "CondDBESSource.h"
13 
14 #include "boost/shared_ptr.hpp"
19 
21 
24 
29 //#include <cstdlib>
31 #include <exception>
32 //#include <iostream>
33 
34 
36 #include <iomanip>
37 
38 namespace {
39  /* utility ot build the name of the plugin corresponding to a given record
40  se ESSources
41  */
42  std::string
43  buildName( const std::string& iRecordName) {
44  return iRecordName+"@NewProxy";
45  }
46 
47 
48  /* utility class to return a IOVs associated to a given "name"
49  This implementation return the IOV associated to a record...
50  It is essentialy a workaround to get the full IOV out of the tag colector
51  that is not accessible as hidden in the ESSource
52  FIXME: need to support label??
53  */
54  class CondGetterFromESSource : public cond::CondGetter {
55  public:
56  CondGetterFromESSource(CondDBESSource::ProxyMap const & ip) : m_proxies(ip){}
57  virtual ~CondGetterFromESSource(){}
58 
59  cond::IOVProxy get(std::string name) const {
60  CondDBESSource::ProxyMap::const_iterator p = m_proxies.find(name);
61  if ( p != m_proxies.end())
62  return (*p).second->proxy()->iov();
63  return cond::IOVProxy();
64  }
65 
66  CondDBESSource::ProxyMap const & m_proxies;
67  };
68 
69  // dump the state of a DataProxy
70  void dumpInfo(std::ostream & out, std::string const & recName, cond::DataProxyWrapperBase const & proxy) {
71  cond::SequenceState state(proxy.proxy()->iov().state());
72  out << recName << " / " << proxy.label() << ": "
73  << proxy.connString() << ", " << proxy.tag() << "\n "
74  << state.size() << ", " << state.revision() << ", "
75  << cond::time::to_boost(state.timestamp()) << "\n "
76  << state.comment()
77  << "\n "
78  << " make " << proxy.proxy()->stats.nMake
79  << ", load " << proxy.proxy()->stats.nLoad
80  ;
81  if ( proxy.proxy()->stats.nLoad>0) {
82  out << "\n oids,sinces:";
83  cond::BasePayloadProxy::ObjIds const & ids = proxy.proxy()->stats.ids;
84  for (cond::BasePayloadProxy::ObjIds::const_iterator id=ids.begin(); id!=ids.end(); ++id)
85  out << " "
86  // << std::ios::hex
87  << (*id).oid1 <<"-"<< (*id).oid2 <<","
88  // << std::ios::dec
89  << (*id).since;
90  }
91  }
92 
93 }
94 
95 
96 /*
97  * config Param
98  * RefreshEachRun: if true will refresh the IOV at each new run (or lumiSection)
99  * DumpStat: if true dump the statistics of all DataProxy (currently on cout)
100  * DBParameters: configuration set of the connection
101  * globaltag: The GlobalTag
102  * toGet: list of record label tag connection-string to add/overwrite the content of the global-tag
103  */
105  m_connection(),
106  lastRun(0), // for the refresh
107  doRefresh(iConfig.getUntrackedParameter<bool>("RefreshEachRun",false)),
108  doDump(iConfig.getUntrackedParameter<bool>("DumpStat",false))
109 {
110  Stats s = {0,0,0,0,0};
111  stats=s;
112  //std::cout<<"CondDBESSource::CondDBESSource"<<std::endl;
113  /*parameter set parsing and pool environment setting
114  */
115 
116  // default connection string
117  // inproduction used for the global tag
118  std::string userconnect= iConfig.getParameter<std::string>("connect");
119 
120 
121  // connection configuration
122  edm::ParameterSet connectionPset = iConfig.getParameter<edm::ParameterSet>("DBParameters");
123  m_connection.configuration().setParameters( connectionPset );
125 
126 
127  // load additional record/tag info it will overwrite the global tag
128  std::map<std::string,cond::TagMetadata> replacement;
129  if( iConfig.exists("toGet") ){
130  typedef std::vector< edm::ParameterSet > Parameters;
131  Parameters toGet = iConfig.getParameter<Parameters>("toGet");
132  for(Parameters::iterator itToGet = toGet.begin(); itToGet != toGet.end(); ++itToGet ) {
134  nm.recordname=itToGet->getParameter<std::string>("record");
135  nm.labelname=itToGet->getUntrackedParameter<std::string>("label","");
136  nm.tag=itToGet->getParameter<std::string>("tag");
137  nm.pfn=itToGet->getUntrackedParameter<std::string>("connect",userconnect);
138  // nm.objectname=itFound->second;
139  std::string k=nm.recordname+"@"+nm.labelname;
140  replacement.insert(std::pair<std::string,cond::TagMetadata>(k,nm));
141  }
142  }
143 
144  // get the global tag, merge with "replacement" store in "tagCollection"
145  std::string globaltag;
146  if( iConfig.exists("globaltag")) globaltag=iConfig.getParameter<std::string>("globaltag");
147 
148  fillTagCollectionFromDB(userconnect,
149  iConfig.getUntrackedParameter<std::string>("pfnPrefix",""),
150  iConfig.getUntrackedParameter<std::string>("pfnPostfix",""),
151  globaltag,
152  replacement);
153 
154 
155  TagCollection::iterator it;
156  TagCollection::iterator itBeg=m_tagCollection.begin();
157  TagCollection::iterator itEnd=m_tagCollection.end();
158 
159  typedef std::map<std::string, cond::DbSession> Sessions;
160  Sessions sessions;
161 
162  /* load DataProxy Plugin (it is strongly typed due to EventSetup ideosyncrasis)
163  * construct proxy
164  * contrary to EventSetup the "object-name" is not used as identifier: multiple entries in a record are
165  * dinstinguished only by their label...
166  * done in two step: first create ProxyWrapper loading ALL required dictionaries
167  * this will allow to initialize POOL in one go for each "database"
168  * The real initialization of the Data-Proxies is done in the second loop
169  */
170  std::vector<cond::DataProxyWrapperBase *> proxyWrappers(m_tagCollection.size());
171  size_t ipb=0;
172  for(it=itBeg;it!=itEnd;++it){
173  proxyWrappers[ipb++] =
174  cond::ProxyFactory::get()->create(buildName(it->recordname));
175  }
176  // now all required libraries have been loaded
177  // init sessions and DataProxies
178  ipb=0;
179  for(it=itBeg;it!=itEnd;++it){
180  Sessions::iterator p = sessions.find( it->pfn);
181  cond::DbSession nsess;
182  if (p==sessions.end()) {
183  //open db get tag info (i.e. the IOV token...)
184  nsess = m_connection.createSession();
185  nsess.open( it->pfn, true );
186  // keep transaction open if source is not transactional (such as FronTier)
187  if (!nsess.isTransactional()) nsess.transaction().start(true);
188  sessions.insert(std::make_pair(it->pfn,nsess));
189  } else nsess = (*p).second;
190  cond::MetaData metadata(nsess);
192  transaction.start(true);
193  std::string iovtoken = metadata.getToken(it->tag);
194  transaction.commit();
195  // owenship...
196  ProxyP proxy(proxyWrappers[ipb++]);
197  // instert in the map
198  m_proxies.insert(std::make_pair(it->recordname, proxy));
199  // initialize
200  proxy->lateInit(nsess,iovtoken,
201  it->labelname, it->pfn, it->tag
202  );
203  }
204 
205  // one loaded expose all other tags to the Proxy!
206  CondGetterFromESSource visitor(m_proxies);
207  ProxyMap::iterator b= m_proxies.begin();
208  ProxyMap::iterator e= m_proxies.end();
209  for (;b!=e;b++) {
210  (*b).second->proxy()->loadMore(visitor);
211 
214  if( recordKey.type() != edm::eventsetup::EventSetupRecordKey::TypeTag() ) {
215  findingRecordWithKey( recordKey );
216  usingRecordWithKey( recordKey );
217  }
218  }
219 
220  stats.nData=m_proxies.size();
221 }
222 
223 
225  //dump info FIXME: find a more suitable place...
226  if (doDump) {
227  std::cout << "CondDBESSource Statistics" << std::endl
228  << "DataProxy " << stats.nData
229  <<" setInterval " << stats.nSet
230  <<" Runs " << stats.nRun
231  <<" Refresh " << stats.nRefresh
232  <<" Actual Refresh " << stats.nActualRefresh;
233  std::cout << std::endl;
234  std::cout << "Global Proxy Statistics" << std::endl
235  << "proxy " << cond::BasePayloadProxy::gstats.nProxy
236  << " make " << cond::BasePayloadProxy::gstats.nMake
237  << " load " << cond::BasePayloadProxy::gstats.nLoad;
238  std::cout << std::endl;
239 
240 
241  ProxyMap::iterator b= m_proxies.begin();
242  ProxyMap::iterator e= m_proxies.end();
243  for (;b!=e;b++) {
244  dumpInfo(std::cout,(*b).first,*(*b).second);
245  std::cout << "\n" << std::endl;
246  }
247 
248  // FIXME
249  // We shall eventually close transaction and session...
250  }
251 }
252 
253 
254 //
255 // invoked by EventSetUp: for a given record return the smallest IOV for which iTime is valid
256 // limit to next run/lumisection of Refresh is required
257 //
258 void
260 
261  stats.nSet++;
262 
263  std::string recordname=iKey.name();
265 
266  //FIXME use equal_range
267  ProxyMap::const_iterator b = m_proxies.lower_bound(recordname);
268  ProxyMap::const_iterator e = m_proxies.upper_bound(recordname);
269  if ( b == e) {
270  LogDebug ("CondDBESSource")<<"no DataProxy (Pluging) found for record "<<recordname;
271  return;
272  }
273 
274  // compute the smallest interval (assume all objects have the same timetype....)
275  cond::ValidityInterval recordValidity(0,cond::TIMELIMIT);
277  bool userTime=true;
278  for (ProxyMap::const_iterator p=b;p!=e;++p) {
279  // refresh if required...
280  if (doRefresh) {
281  stats.nActualRefresh += (*p).second->proxy()->refresh();
282  stats.nRefresh++;
283  }
284 
285  {
286  // not required anymore, keep here for the time being
287  if(iTime.eventID().run()!=lastRun) {
288  lastRun=iTime.eventID().run();
289  stats.nRun++;
290  }
291  }
292 
293 
294  timetype = (*p).second->proxy()->timetype();
295 
296  cond::Time_t abtime = cond::fromIOVSyncValue(iTime,timetype);
297  userTime = (0==abtime);
298 
299  //std::cout<<"abtime "<<abtime<<std::endl;
300 
301  //query the IOVSequence
302  cond::ValidityInterval validity = (*p).second->proxy()->setIntervalFor(abtime);
303 
304  recordValidity.first = std::max(recordValidity.first,validity.first);
305  recordValidity.second = std::min(recordValidity.second,validity.second);
306 
307  //std::cout<<"setting validity "<<recordValidity.first<<" "<<recordValidity.second<<" for ibtime "<<abtime<< std::endl;
308 
309  }
310 
311  // to force refresh we set end-value to the minimum such an IOV can exend to: current run or lumiblock
312 
313  if ( (!userTime) && recordValidity.second!=0) {
314  edm::IOVSyncValue start = cond::toIOVSyncValue(recordValidity.first, timetype, true);
315  edm::IOVSyncValue stop = doRefresh ? cond::limitedIOVSyncValue (iTime, timetype)
316  : cond::toIOVSyncValue(recordValidity.second, timetype, false);
317 
318  oInterval = edm::ValidityInterval( start, stop );
319  }
320 }
321 
322 
323 //required by EventSetup System
324 void
326  std::string recordname=iRecordKey.name();
327 
328  ProxyMap::const_iterator b = m_proxies.lower_bound(recordname);
329  ProxyMap::const_iterator e = m_proxies.upper_bound(recordname);
330  if ( b == e) {
331  LogDebug ("CondDBESSource")<<"no DataProxy (Pluging) found for record "<<recordname;
332  return;
333  }
334 
335  for (ProxyMap::const_iterator p=b;p!=e;++p) {
336  if(0 != (*p).second.get()) {
337  edm::eventsetup::TypeTag type = (*p).second->type();
338  edm::eventsetup::DataKey key( type, edm::eventsetup::IdTags((*p).second->label().c_str()) );
339  aProxyList.push_back(KeyedProxies::value_type(key,(*p).second->edmProxy()));
340  }
341  }
342 }
343 
344 // required by the EventSetup System
345 void
347  const edm::ValidityInterval&)
348 {
349  //LogDebug ("CondDBESSource")<<"newInterval";
350  invalidateProxies(iRecordType);
351 }
352 
353 
354 // fills tagcollection merging with replacement
355 void
356 CondDBESSource::fillTagCollectionFromDB( const std::string & coraldb,
357  const std::string & prefix,
358  const std::string & postfix,
359  const std::string & roottag,
360  std::map<std::string,cond::TagMetadata>& replacement){
361  // std::cout<<"fillTagCollectionFromDB"<<std::endl;
362 
363 
364  std::set< cond::TagMetadata > tagcoll;
365 
366  if (!roottag.empty()) {
367  if (coraldb.empty())
368  throw cond::Exception(std::string("ESSource: requested global tag ")+roottag+" but not connection string given");
370  session.open( coraldb, true );
372  transaction.start(true);
373  cond::TagCollectionRetriever tagRetriever( session, prefix, postfix );
374  tagRetriever.getTagCollection(roottag,tagcoll);
375  transaction.commit();
376  }
377 
378  std::set<cond::TagMetadata>::iterator it;
379  std::set<cond::TagMetadata>::iterator itBeg=tagcoll.begin();
380  std::set<cond::TagMetadata>::iterator itEnd=tagcoll.end();
381 
382  // FIXME the logic is a bit perverse: can be surely linearized (at least simplified!) ....
383  for(it=itBeg; it!=itEnd; ++it){
384  std::string k=it->recordname+"@"+it->labelname;
385  std::map<std::string,cond::TagMetadata>::iterator fid=replacement.find(k);
386  if(fid != replacement.end()){
388  m.recordname=it->recordname;
389  m.labelname=it->labelname;
390  m.pfn=fid->second.pfn;
391  m.tag=fid->second.tag;
392  m.objectname=it->objectname;
393  m_tagCollection.insert(m);
394  replacement.erase(fid);
395  }else{
396  m_tagCollection.insert(*it);
397  }
398  }
399  std::map<std::string,cond::TagMetadata>::iterator itrep;
400  std::map<std::string,cond::TagMetadata>::iterator itrepBeg=replacement.begin();
401  std::map<std::string,cond::TagMetadata>::iterator itrepEnd=replacement.end();
402  for(itrep=itrepBeg; itrep!=itrepEnd; ++itrep){
403  //std::cout<<"appending"<<std::endl;
404  //std::cout<<"pfn "<<itrep->second.pfn<<std::endl;
405  //std::cout<<"objectname "<<itrep->second.objectname<<std::endl;
406  //std::cout<<"tag "<<itrep->second.tag<<std::endl;
407  //std::cout<<"recordname "<<itrep->second.recordname<<std::endl;
408  m_tagCollection.insert(itrep->second);
409  }
410 }
411 
412 
413 // backward compatibility for configuration files
415 public:
416  explicit PoolDBESSource( const edm::ParameterSet& ps) :
417  CondDBESSource(ps){}
418 };
419 
421 //define this as a plug-in
423 
#define LogDebug(id)
RunNumber_t run() const
Definition: EventID.h:42
type
Definition: HCALResponse.h:22
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
static HCTypeTag findType(const char *iTypeName)
find a type based on the types name, if not found will return default HCTypeTag
Definition: HCTypeTag.cc:126
edm::IOVSyncValue limitedIOVSyncValue(cond::Time_t time, cond::TimeType timetype)
list globaltag
Definition: align_cfg.py:6
const EventID & eventID() const
Definition: IOVSyncValue.h:42
DbTransaction & transaction()
Definition: DbSession.cc:153
void open(const std::string &connectionString, bool readOnly=false)
Definition: DbSession.cc:119
virtual void registerProxies(const edm::eventsetup::EventSetupRecordKey &iRecordKey, KeyedProxies &aProxyList)
void getTagCollection(const std::string &globaltag, std::set< cond::TagMetadata > &result)
DbConnectionConfiguration & configuration()
Definition: DbConnection.cc:89
std::string objectname
Definition: TagMetadata.h:12
bool exists(std::string const &parameterName) const
checks if a parameter exists
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:19
#define min(a, b)
Definition: mlp_lapack.h:161
void usingRecordWithKey(const EventSetupRecordKey &)
virtual ProxyP proxy() const =0
Container::value_type value_type
const std::string getToken(const std::string &tagname) const
Definition: MetaData.cc:53
cond::DbConnection m_connection
void setParameters(const edm::ParameterSet &connectionPset)
TimeType
Definition: Time.h:21
void fillTagCollectionFromDB(const std::string &coraldb, const std::string &prefix, const std::string &postfix, const std::string &roottag, std::map< std::string, cond::TagMetadata > &replacement)
std::string labelname
Definition: TagMetadata.h:11
int start(bool readOnly=false)
start transaction
std::string const & label() const
Definition: DataProxy.h:79
edm::IOVSyncValue toIOVSyncValue(cond::Time_t time, cond::TimeType timetype, bool startOrStop)
unsigned long long Time_t
Definition: Time.h:16
std::string tag
Definition: TagMetadata.h:8
void invalidateProxies(const EventSetupRecordKey &iRecordKey)
const T & max(const T &a, const T &b)
bool isTransactional() const
Definition: DbSession.cc:144
std::vector< std::pair< DataKey, boost::shared_ptr< DataProxy > > > KeyedProxies
virtual void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &)
int start(bool readOnly=false)
start transaction
int k[5][pyjets_maxn]
tuple out
Definition: dbtoconf.py:99
std::vector< ObjId > ObjIds
Definition: PayloadProxy.h:31
std::multimap< std::string, ProxyP > ProxyMap
std::string const & connString() const
Definition: DataProxy.h:81
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:74
PoolDBESSource(const edm::ParameterSet &ps)
TagCollection m_tagCollection
DbSession createSession() const
Definition: DbConnection.cc:72
const Time_t TIMELIMIT(std::numeric_limits< Time_t >::max())
double b
Definition: hdecay.h:120
std::string const & tag() const
Definition: DataProxy.h:82
std::string pfn
Definition: TagMetadata.h:9
char state
Definition: procUtils.cc:75
std::vector< AlignmentParameters * > Parameters
Definition: Utilities.h:29
heterocontainer::HCTypeTag TypeTag
ProxyMap m_proxies
list key
Definition: combine.py:13
static const ValidityInterval & invalidInterval()
tuple cout
Definition: gather_cfg.py:41
virtual void newInterval(const edm::eventsetup::EventSetupRecordKey &iRecordType, const edm::ValidityInterval &iInterval)
called when a new interval of validity occurs for iRecordType
cond::Time_t fromIOVSyncValue(edm::IOVSyncValue const &time, cond::TimeType timetype)
string s
Definition: asciidump.py:422
CondDBESSource(const edm::ParameterSet &)
std::string recordname
Definition: TagMetadata.h:10
int commit()
commit transaction. Will disconnect from database if connection timeout==0 or connectted time close t...
tuple toGet
Definition: align_tpl.py:67
list fid
Definition: NewTree.py:51
boost::shared_ptr< cond::DataProxyWrapperBase > ProxyP
boost::posix_time::ptime to_boost(Time_t iValue)
T get(const Candidate &c)
Definition: component.h:56
void findingRecordWithKey(const eventsetup::EventSetupRecordKey &)
unsigned int lastRun