CMS 3D CMS Logo

DataReader.cc

Go to the documentation of this file.
00001 #include "CondTools/L1Trigger/interface/DataReader.h"
00002 
00003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00004 
00005 #include "CondCore/DBCommon/interface/CoralTransaction.h"
00006 
00007 #include "CondCore/PluginSystem/interface/ProxyFactory.h"
00008 
00009 #include "CondCore/IOVService/interface/IOVService.h"
00010 #include "CondCore/IOVService/interface/IOVIterator.h"
00011 
00012 
00013 namespace l1t
00014 {
00015     
00016   //DataReader::DataReader (const std::string & connect, const std::string & catalog)
00017   //    : DataManager (connect, catalog)
00018   DataReader::DataReader( const std::string& connectString,
00019                           const std::string& authenticationPath )
00020     : DataManager( connectString, authenticationPath )
00021 {
00022     // we always maintain pool connection open, so that DataProxy could load the data.
00023 //     pool->connect ();
00024   //connection->connect ( session );
00025 }
00026 
00027 DataReader::~DataReader ()
00028 {
00029 //     pool->disconnect ();
00030   //connection->disconnect ();
00031 }
00032 
00033 L1TriggerKey DataReader::readKey (const std::string & tag, const edm::RunNumber_t & run)
00034 {
00035 //     pool->startTransaction (true);
00036   cond::PoolTransaction& pool = connection->poolTransaction() ;
00037     pool.start (true);
00038 
00039     // get interval for given time, and laod intervals fromd DB if they do not exist for the tag
00040     if (intervals.find (tag) == intervals.end ())
00041         intervals.insert (std::make_pair (tag, loadIntervals (tag)));
00042 
00043     // get correct interval and laod the key for it, we could use interval (...) method, but it returns
00044     // a bit different type, then I need here. I.e. no payload, only start and end times.
00045     Interval<edm::RunNumber_t, std::string> data = intervals [tag].find (run);
00046     if (data == Interval<edm::RunNumber_t, std::string>::invalid ())
00047         throw cond::Exception ("DataReader::readKey") << "Provided time value " << run << " does not exist in the database "
00048             << "for l1 tag \"" << tag << "\"";
00049 
00050     cond::TypedRef<L1TriggerKey> key (pool, data.payload ());
00051     L1TriggerKey ret (*key); // clone key, so that it could be returned nicely
00052 
00053     pool.commit ();
00054 
00055     return ret;
00056 }
00057 
00058 L1TriggerKey DataReader::readKey (const std::string & payloadToken)
00059 {
00060 //     pool->startTransaction (true);
00061   cond::PoolTransaction& pool = connection->poolTransaction() ;
00062     pool.start (true);
00063 
00064     cond::TypedRef<L1TriggerKey> key (pool, payloadToken);
00065     L1TriggerKey ret (*key); // clone key, so that it could be returned nicely
00066 
00067     pool.commit ();
00068 
00069     return ret;
00070 }
00071 
00072 IntervalManager<edm::RunNumber_t, std::string> DataReader::loadIntervals (const std::string & tag)
00073 {
00074     // convert tag to tag token
00075 //     coral->connect (cond::ReadOnly);
00076 //     coral->startTransaction (true);
00077   //connection->connect ( session );
00078 
00079   cond::CoralTransaction& coral = connection->coralTransaction() ;
00080     coral.start (true);
00081     std::string tagToken = metadata->getToken (tag);
00082     coral.commit();
00083 //     coral->disconnect ();
00084 
00085 //     pool->startTransaction (false);
00086   cond::PoolTransaction& pool = connection->poolTransaction() ;
00087     pool.start (false);
00088 
00089     intervals.clear ();
00090 
00091     cond::IOVService iov (pool);
00092     cond::IOVIterator * iovIt = iov.newIOVIterator (tagToken);
00093     IntervalManager<edm::RunNumber_t, std::string> intervals;
00094 
00095     // list all intervals and add to intervals manager
00096     while (iovIt->next ())
00097         intervals.addInterval (Interval<edm::RunNumber_t, std::string> (iovIt->validity ().first, iovIt->validity ().second,
00098                     iovIt->payloadToken ()));
00099 
00100     delete iovIt;
00101     pool.commit ();
00102 
00103     //connection->disconnect ();
00104 
00105     return intervals;
00106 }
00107 
00108 std::pair<edm::RunNumber_t, edm::RunNumber_t> DataReader::invalid ()
00109 {
00110     static std::pair<edm::RunNumber_t, edm::RunNumber_t> invalidPair = std::pair<edm::RunNumber_t, edm::RunNumber_t> (10, 9);
00111     return invalidPair;
00112 }
00113 
00114 std::pair<edm::RunNumber_t, edm::RunNumber_t> DataReader::interval (const std::string & tag, const edm::RunNumber_t & run)
00115 {
00116     // if we do not have tag in our small cache, insert it into it
00117     if (intervals.find (tag) == intervals.end ())
00118         intervals.insert (std::make_pair (tag, loadIntervals (tag)));
00119 
00120     Interval<edm::RunNumber_t, std::string> interval = intervals [tag].find (run);
00121     if (interval != Interval<edm::RunNumber_t, std::string>::invalid ())
00122         return std::make_pair (interval.start (), interval.end ());
00123     else
00124         // not nice, but I have no Idea how to make nicer
00125         return invalid ();
00126 }
00127 
00128 std::string DataReader::payloadToken (const std::string & tag, const edm::RunNumber_t run) const
00129 {
00130 //     coral->connect (cond::ReadOnly);
00131 //     coral->startTransaction (true);
00132   //connection->connect ( session );
00133   cond::CoralTransaction& coral = connection->coralTransaction() ;
00134     coral.start (true);
00135     std::string payload;
00136 
00137     std::string token = metadata->getToken (tag);
00138 
00139     coral.commit ();
00140 //     coral->disconnect ();
00141 
00142 //     pool->startTransaction (true);
00143   cond::PoolTransaction& pool = connection->poolTransaction() ;
00144     pool.start (true);
00145     cond::IOVService iov (pool);
00146     cond::IOVIterator * iovIt = iov.newIOVIterator (token);
00147 
00148     // If this is a key, then validity interval is important. But if this is a record
00149     // then I do not care about validity intervals. However, in case in the future they should be
00150     // extendet, this code should still work.
00151     while (iovIt->next () && payload.empty ())
00152         if (iovIt->validity ().first <= run && iovIt->validity ().second >= run)
00153             payload = iovIt->payloadToken ();
00154 
00155     delete iovIt;
00156     pool.commit ();
00157 
00158     //connection->disconnect ();
00159 
00160     if( payload.empty() )
00161       {
00162         throw cond::Exception( "DataReader: empty payload token." ) ;
00163       }
00164     //assert (!payload.empty ());
00165     return payload;
00166 }
00167 
00168 /* The following code is taken from PoolDBESSource */
00169 static std::string buildName( const std::string& iRecordName, const std::string& iTypeName )
00170 {
00171   return iRecordName+"@"+iTypeName+"@Proxy";
00172 }
00173 
00174 void DataReader::updateToken (const std::string & record, const std::string & type,
00175     const L1TriggerKey & key)
00176 {
00177      std::stringstream ss;
00178      ss << record << "@" << type;
00179 
00180      // find entry in the map and change its second argument - payload token
00181      // DataProxy will make sure to take into account this change
00182      std::map <std::string, std::string>::iterator it = typeToToken.find (ss.str ());
00183      if (it != typeToToken.end ())
00184          it->second = key.get (record, type);
00185 }
00186 
00187 void DataReader::updateToken (const std::string & tag, const edm::RunNumber_t run)
00188 {
00189      std::map <std::string, std::string>::iterator it = typeToToken.find ("L1TriggerKeyRcd@L1TriggerKey");
00190      if (it != typeToToken.end ())
00191          it->second = payloadToken (tag, run);
00192 }
00193 
00194 std::pair<boost::shared_ptr<edm::eventsetup::DataProxy>, edm::eventsetup::DataKey>
00195 DataReader::createPayload (const std::string & record, const std::string & type)
00196 {
00197 
00198   edm::LogWarning("L1DBESSource") << "DataReader::createPayload( " << record << " , " << type << " )";
00199 
00200      edm::eventsetup::TypeTag typeTag = findType (type);
00201      std::stringstream ss;
00202      ss << record << "@" << type;
00203 
00204      std::map <std::string, std::string>::iterator it = typeToToken.find (ss.str ());
00205 
00206      // we do not insert any valid token. Token will be updated when updateToken is called.
00207      // In other words, returned record is not valid until one calls updateToken for this type
00208      // record
00209      if (it == typeToToken.end ())
00210          it = typeToToken.insert (std::make_pair (ss.str (), "")).first;
00211 
00212      boost::shared_ptr<edm::eventsetup::DataProxy> proxy(
00213 //              cond::ProxyFactory::get()->create(buildName(record, type), pool, it));
00214              cond::ProxyFactory::get()->create(buildName(record, type), connection, it));
00215 
00216      if(0 == proxy.get())
00217         throw cond::Exception ("DataReader::createPayload") << "Proxy for type " << type
00218             << " and record " << record << " returned null";
00219 
00220      edm::eventsetup::DataKey dataKey (typeTag, "");
00221      return std::make_pair (proxy, dataKey);
00222 }
00223 
00224 std::pair<boost::shared_ptr<edm::eventsetup::DataProxy>, edm::eventsetup::DataKey>
00225 DataReader::payload (const std::string & record, const std::string & type)
00226 {
00227     return createPayload (record, type);
00228 }
00229 
00230 std::pair<boost::shared_ptr<edm::eventsetup::DataProxy>, edm::eventsetup::DataKey>
00231 DataReader::key ()
00232 {
00233     return createPayload ("L1TriggerKeyRcd", "L1TriggerKey");
00234 }
00235 }

Generated on Tue Jun 9 17:26:56 2009 for CMSSW by  doxygen 1.5.4