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
00017
00018 DataReader::DataReader( const std::string& connectString,
00019 const std::string& authenticationPath )
00020 : DataManager( connectString, authenticationPath )
00021 {
00022
00023
00024
00025 }
00026
00027 DataReader::~DataReader ()
00028 {
00029
00030
00031 }
00032
00033 L1TriggerKey DataReader::readKey (const std::string & tag, const edm::RunNumber_t & run)
00034 {
00035
00036 cond::PoolTransaction& pool = connection->poolTransaction() ;
00037 pool.start (true);
00038
00039
00040 if (intervals.find (tag) == intervals.end ())
00041 intervals.insert (std::make_pair (tag, loadIntervals (tag)));
00042
00043
00044
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);
00052
00053 pool.commit ();
00054
00055 return ret;
00056 }
00057
00058 L1TriggerKey DataReader::readKey (const std::string & payloadToken)
00059 {
00060
00061 cond::PoolTransaction& pool = connection->poolTransaction() ;
00062 pool.start (true);
00063
00064 cond::TypedRef<L1TriggerKey> key (pool, payloadToken);
00065 L1TriggerKey ret (*key);
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
00075
00076
00077
00078
00079 cond::CoralTransaction& coral = connection->coralTransaction() ;
00080 coral.start (true);
00081 std::string tagToken = metadata->getToken (tag);
00082 coral.commit();
00083
00084
00085
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
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
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
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
00125 return invalid ();
00126 }
00127
00128 std::string DataReader::payloadToken (const std::string & tag, const edm::RunNumber_t run) const
00129 {
00130
00131
00132
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
00141
00142
00143 cond::PoolTransaction& pool = connection->poolTransaction() ;
00144 pool.start (true);
00145 cond::IOVService iov (pool);
00146 cond::IOVIterator * iovIt = iov.newIOVIterator (token);
00147
00148
00149
00150
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
00159
00160 if( payload.empty() )
00161 {
00162 throw cond::Exception( "DataReader: empty payload token." ) ;
00163 }
00164
00165 return payload;
00166 }
00167
00168
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
00181
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
00207
00208
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
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 }