00001 #ifndef RecoLuminosity_LumiProducer_CMSRunSummary2DB_h
00002 #define RecoLuminosity_LumiProducer_CMSRunSummary2DB_h
00003 #include "CoralBase/AttributeList.h"
00004 #include "CoralBase/Attribute.h"
00005 #include "CoralBase/AttributeSpecification.h"
00006 #include "CoralBase/Exception.h"
00007 #include "CoralBase/TimeStamp.h"
00008 #include "RelationalAccess/ConnectionService.h"
00009 #include "RelationalAccess/ISessionProxy.h"
00010 #include "RelationalAccess/ITransaction.h"
00011 #include "RelationalAccess/ITypeConverter.h"
00012 #include "RelationalAccess/IQuery.h"
00013 #include "RelationalAccess/ICursor.h"
00014 #include "RelationalAccess/ISchema.h"
00015 #include "RelationalAccess/IView.h"
00016 #include "RelationalAccess/ITable.h"
00017 #include "RelationalAccess/ITableDataEditor.h"
00018 #include "RelationalAccess/IBulkOperation.h"
00019 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00020 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00021 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00022 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00023 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00024 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00025 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00026 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00027 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00028 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00029 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00030 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00031 #include <iostream>
00032 #include <sstream>
00033 #include <fstream>
00034 #include <vector>
00035 #include <string>
00036 #include <boost/regex.hpp>
00037 #include <boost/tokenizer.hpp>
00038
00039 namespace lumi{
00040 class CMSRunSummary2DB : public DataPipe{
00041 public:
00042 CMSRunSummary2DB( const std::string& dest);
00043 virtual unsigned long long retrieveData( unsigned int runnumber );
00044 virtual const std::string dataType() const;
00045 virtual const std::string sourceType() const;
00046 unsigned int str2int(const std::string& s) const;
00047 virtual ~CMSRunSummary2DB();
00048 private:
00049 struct cmsrunsum{
00050 std::string l1key;
00051 std::string amodetag;
00052 int egev;
00053 std::string hltkey;
00054 std::string fillnumber;
00055 std::string sequence;
00056 std::string fillscheme;
00057 int ncollidingbunches;
00058 coral::TimeStamp startT;
00059 coral::TimeStamp stopT;
00060 };
00061 bool isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum& rundata);
00062 void parseFillCSV(const std::string& csvsource, cmsrunsum& result);
00063 };
00064
00065
00066
00067 void
00068 CMSRunSummary2DB::parseFillCSV(const std::string& csvsource, lumi::CMSRunSummary2DB::cmsrunsum& result){
00069 result.fillscheme=std::string("");
00070 result.ncollidingbunches=0;
00071 std::ifstream csvfile;
00072 csvfile.open(csvsource.c_str());
00073 if(!csvfile){
00074 std::cout<<"[warning] unable to open file: "<<csvsource<<std::endl;
00075 return;
00076 }
00077 typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer;
00078 std::vector<std::string> record;
00079 std::string line;
00080 while(std::getline(csvfile,line)){
00081 Tokenizer tok(line);
00082 record.assign(tok.begin(),tok.end());
00083 if(record.size()<3) continue;
00084 std::string fillnum=record[0];
00085 if(fillnum==result.fillnumber){
00086 result.fillscheme=record[1];
00087 std::string ncollidingbunchesStr=record[2];
00088 result.ncollidingbunches=str2int(ncollidingbunchesStr);
00089 break;
00090 }
00091 }
00092 }
00093 CMSRunSummary2DB::CMSRunSummary2DB(const std::string& dest):DataPipe(dest){}
00094 bool CMSRunSummary2DB::isCollisionRun(const lumi::CMSRunSummary2DB::cmsrunsum& rundata){
00095 bool isCollision=false;
00096 bool isPhysics=false;
00097 std::string hk=rundata.hltkey;
00098 std::string lk=rundata.l1key;
00099 boost::match_results<std::string::const_iterator> what;
00100 const boost::regex lexpr("^TSC_.+_collisions_.+");
00101 boost::regex_match(lk,what,lexpr,boost::match_default);
00102 if(what[0].matched) isCollision=true;
00103 const boost::regex hexpr("^/cdaq/physics/.+");
00104 boost::regex_match(hk,what,hexpr,boost::match_default);
00105 if(what[0].matched) isPhysics=true;
00106 return (isCollision&&isPhysics);
00107 }
00108 unsigned long long
00109 CMSRunSummary2DB::retrieveData( unsigned int runnumber){
00121 cmsrunsum result;
00122 std::string runinfoschema("CMS_RUNINFO");
00123 std::string runsessionParamTable("RUNSESSION_PARAMETER");
00124 coral::ConnectionService* svc=new coral::ConnectionService;
00125 lumi::DBConfig dbconf(*svc);
00126 if(!m_authpath.empty()){
00127 dbconf.setAuthentication(m_authpath);
00128 }
00129
00130
00131 std::string::size_type cutpos=m_source.find(';');
00132 std::string dbsource=m_source;
00133 std::string csvsource("");
00134 if(cutpos!=std::string::npos){
00135 dbsource=m_source.substr(0,cutpos);
00136 csvsource=m_source.substr(cutpos+1);
00137 }
00138
00139 coral::ISessionProxy* runinfosession=svc->connect(dbsource,coral::ReadOnly);
00140 try{
00141 coral::ITypeConverter& tpc=runinfosession->typeConverter();
00142 tpc.setCppTypeForSqlType("unsigned int","NUMBER(38)");
00143 runinfosession->transaction().start(true);
00144 coral::ISchema& runinfoschemaHandle=runinfosession->schema(runinfoschema);
00145 if(!runinfoschemaHandle.existsTable(runsessionParamTable)){
00146 throw lumi::Exception(std::string("non-existing table "+runsessionParamTable),"CMSRunSummary2DB","retrieveData");
00147 }
00148 coral::IQuery* amodetagQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00149 coral::AttributeList amodetagOutput;
00150 amodetagOutput.extend("amodetag",typeid(std::string));
00151 coral::AttributeList amodetagCondition;
00152 amodetagCondition=coral::AttributeList();
00153 amodetagCondition.extend("name",typeid(std::string));
00154 amodetagCondition.extend("runnumber",typeid(unsigned int));
00155 amodetagCondition["name"].data<std::string>()=std::string("CMS.SCAL:AMODEtag");
00156 amodetagCondition["runnumber"].data<unsigned int>()=runnumber;
00157 amodetagQuery->addToOutputList("distinct(STRING_VALUE)");
00158 amodetagQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",amodetagCondition);
00159
00160 amodetagQuery->defineOutput(amodetagOutput);
00161 coral::ICursor& amodetagCursor=amodetagQuery->execute();
00162 std::vector<std::string> amodes;
00163 while (amodetagCursor.next()){
00164 const coral::AttributeList& row=amodetagCursor.currentRow();
00165 amodes.push_back(row["amodetag"].data<std::string>());
00166
00167 }
00168
00169
00170
00171 std::string amd;
00172 for(std::vector<std::string>::iterator it=amodes.begin();it!=amodes.end();++it){
00173 if(it->find("PHYS")==std::string::npos) continue;
00174 amd=*it;
00175 }
00176 if(amd.size()==0&&amodes.size()!=0){
00177 amd=*(amodes.begin());
00178 }
00179 if(amd.size()==0){
00180 amd=std::string("PROTPHYS");
00181 }
00182
00183 result.amodetag=amd;
00184 delete amodetagQuery;
00185
00186 coral::IQuery* egevQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00187 coral::AttributeList egevOutput;
00188 egevOutput.extend("egev",typeid(std::string));
00189 coral::AttributeList egevCondition;
00190 egevCondition=coral::AttributeList();
00191 egevCondition.extend("name",typeid(std::string));
00192 egevCondition.extend("runnumber",typeid(unsigned int));
00193 egevCondition["name"].data<std::string>()=std::string("CMS.SCAL:EGEV");
00194 egevCondition["runnumber"].data<unsigned int>()=runnumber;
00195 egevQuery->addToOutputList("distinct(STRING_VALUE)");
00196 egevQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",egevCondition);
00197 egevQuery->defineOutput(egevOutput);
00198 coral::ICursor& egevCursor=egevQuery->execute();
00199 result.egev=0;
00200 while (egevCursor.next()){
00201 const coral::AttributeList& row=egevCursor.currentRow();
00202 std::string egevstr=row["egev"].data<std::string>();
00203 int tmpgev=str2int(egevstr);
00204 if(tmpgev>result.egev){
00205 result.egev=tmpgev;
00206 }
00207 }
00208 if(result.egev==0){
00209 result.egev=3500;
00210 }
00211 delete egevQuery;
00212
00213 coral::IQuery* seqQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00214 coral::AttributeList seqBindVariableList;
00215 seqBindVariableList.extend("runnumber",typeid(unsigned int));
00216 seqBindVariableList.extend("name",typeid(std::string));
00217
00218 seqBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00219 seqBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:SEQ_NAME");
00220 seqQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",seqBindVariableList);
00221 seqQuery->addToOutputList("STRING_VALUE");
00222 coral::ICursor& seqCursor=seqQuery->execute();
00223
00224 while( seqCursor.next() ){
00225 const coral::AttributeList& row=seqCursor.currentRow();
00226 result.sequence=row["STRING_VALUE"].data<std::string>();
00227 }
00228 delete seqQuery;
00229
00230 coral::IQuery* hltkeyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00231 coral::AttributeList hltkeyBindVariableList;
00232 hltkeyBindVariableList.extend("runnumber",typeid(unsigned int));
00233 hltkeyBindVariableList.extend("name",typeid(std::string));
00234
00235 hltkeyBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00236 hltkeyBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
00237 hltkeyQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",hltkeyBindVariableList);
00238 hltkeyQuery->addToOutputList("STRING_VALUE");
00239 coral::ICursor& hltkeyCursor=hltkeyQuery->execute();
00240
00241 while( hltkeyCursor.next() ){
00242 const coral::AttributeList& row=hltkeyCursor.currentRow();
00243 result.hltkey=row["STRING_VALUE"].data<std::string>();
00244 }
00245 delete hltkeyQuery;
00246
00247 coral::IQuery* fillQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00248 coral::AttributeList fillBindVariableList;
00249 fillBindVariableList.extend("runnumber",typeid(unsigned int));
00250 fillBindVariableList.extend("name",typeid(std::string));
00251
00252 fillBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00253 fillBindVariableList["name"].data<std::string>()=std::string("CMS.SCAL:FILLN");
00254 fillQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",fillBindVariableList);
00255 fillQuery->addToOutputList("STRING_VALUE");
00256 fillQuery->addToOrderList("TIME");
00257
00258 coral::ICursor& fillCursor=fillQuery->execute();
00259 unsigned int cc=0;
00260 while( fillCursor.next() ){
00261 const coral::AttributeList& row=fillCursor.currentRow();
00262 if (cc==0){
00263 result.fillnumber=row["STRING_VALUE"].data<std::string>();
00264 }
00265 ++cc;
00266 }
00267 delete fillQuery;
00268 if (result.fillnumber.empty()){
00269 throw nonCollisionException("retrieveData","CMSRunSummary2DB");
00270 }
00271 coral::IQuery* startTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00272 coral::AttributeList startTVariableList;
00273 startTVariableList.extend("runnumber",typeid(unsigned int));
00274 startTVariableList.extend("name",typeid(std::string));
00275
00276 startTVariableList["runnumber"].data<unsigned int>()=runnumber;
00277 startTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:START_TIME_T");
00278 startTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",startTVariableList);
00279 startTQuery->addToOutputList("TIME");
00280 coral::ICursor& startTCursor=startTQuery->execute();
00281
00282 while( startTCursor.next() ){
00283 const coral::AttributeList& row=startTCursor.currentRow();
00284 result.startT=row["TIME"].data<coral::TimeStamp>();
00285 }
00286 delete startTQuery;
00287 coral::IQuery* stopTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00288 coral::AttributeList stopTVariableList;
00289 stopTVariableList.extend("runnumber",typeid(unsigned int));
00290 stopTVariableList.extend("name",typeid(std::string));
00291
00292 stopTVariableList["runnumber"].data<unsigned int>()=runnumber;
00293 stopTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:STOP_TIME_T");
00294 stopTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",stopTVariableList);
00295 stopTQuery->addToOutputList("TIME");
00296 coral::ICursor& stopTCursor=stopTQuery->execute();
00297
00298 while( stopTCursor.next() ){
00299 const coral::AttributeList& row=stopTCursor.currentRow();
00300 result.stopT=row["TIME"].data<coral::TimeStamp>();
00301 }
00302 delete stopTQuery;
00303 coral::IQuery* l1keyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00304 coral::AttributeList l1keyOutput;
00305 l1keyOutput.extend("l1key",typeid(std::string));
00306 coral::AttributeList l1keyCondition;
00307 l1keyCondition=coral::AttributeList();
00308 l1keyCondition.extend("name",typeid(std::string));
00309 l1keyCondition.extend("runnumber",typeid(unsigned int));
00310 l1keyCondition["name"].data<std::string>()=std::string("CMS.TRG:TSC_KEY");
00311 l1keyCondition["runnumber"].data<unsigned int>()=runnumber;
00312 l1keyQuery->addToOutputList("STRING_VALUE");
00313 l1keyQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",l1keyCondition);
00314
00315 l1keyQuery->defineOutput(l1keyOutput);
00316 coral::ICursor& l1keyCursor=l1keyQuery->execute();
00317 while (l1keyCursor.next()){
00318 const coral::AttributeList& row=l1keyCursor.currentRow();
00319 result.l1key=row["l1key"].data<std::string>();
00320 }
00321 delete l1keyQuery;
00322 }catch( const coral::Exception& er){
00323 runinfosession->transaction().rollback();
00324 delete runinfosession;
00325 delete svc;
00326 throw er;
00327 }
00328 runinfosession->transaction().commit();
00329 delete runinfosession;
00330
00331 if(csvsource.size()!=0){
00332 parseFillCSV(csvsource,result);
00333 }else{
00334 result.fillscheme=std::string("");
00335 result.ncollidingbunches=0;
00336 }
00337 std::cout<<"result for run "<<runnumber<<" : sequence : "<<result.sequence<<" : hltkey : "<<result.hltkey<<" : fillnumber : "<<result.fillnumber<<" : l1key : "<<result.l1key<<" : amodetag :"<<result.amodetag<<" : egev : "<<result.egev<<" : fillscheme "<<result.fillscheme<<" : ncollidingbunches : "<<result.ncollidingbunches<<std::endl;
00338
00339
00340 coral::ISessionProxy* destsession=svc->connect(m_dest,coral::Update);
00341
00342 coral::ITypeConverter& desttpc=destsession->typeConverter();
00343 desttpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00344 try{
00345 destsession->transaction().start(false);
00346 coral::ISchema& destschema=destsession->nominalSchema();
00347 coral::ITable& destruntable=destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
00348 coral::AttributeList runData;
00349 destruntable.dataEditor().rowBuffer(runData);
00350 runData["RUNNUM"].data<unsigned int>()=runnumber;
00351 runData["FILLNUM"].data<unsigned int>()=str2int(result.fillnumber);
00352 runData["SEQUENCE"].data<std::string>()=result.sequence;
00353 runData["HLTKEY"].data<std::string>()=result.hltkey;
00354 runData["STARTTIME"].data<coral::TimeStamp>()=result.startT;
00355 runData["STOPTIME"].data<coral::TimeStamp>()=result.stopT;
00356 runData["AMODETAG"].data<std::string>()=result.amodetag;
00357 runData["EGEV"].data<unsigned int>()=(unsigned int)result.egev;
00358 runData["L1KEY"].data<std::string>()=result.l1key;
00359 runData["FILLSCHEME"].data<std::string>()=result.fillscheme;
00360 runData["NCOLLIDINGBUNCHES"].data<unsigned int>()=result.ncollidingbunches;
00361 destruntable.dataEditor().insertRow(runData);
00362 }catch( const coral::Exception& er){
00363 std::cout<<"database problem "<<er.what()<<std::endl;
00364 destsession->transaction().rollback();
00365 delete destsession;
00366 delete svc;
00367 throw er;
00368 }
00369 destsession->transaction().commit();
00370 delete svc;
00371 return 0;
00372 }
00373 const std::string CMSRunSummary2DB::dataType() const{
00374 return "CMSRUNSUMMARY";
00375 }
00376 const std::string CMSRunSummary2DB::sourceType() const{
00377 return "DB";
00378 }
00379 unsigned int CMSRunSummary2DB::str2int(const std::string& s) const{
00380 std::istringstream myStream(s);
00381 unsigned int i;
00382 if(myStream>>i){
00383 return i;
00384 }else{
00385 throw lumi::Exception(std::string("str2int error"),"str2int","CMSRunSummary2DB");
00386 }
00387 }
00388 CMSRunSummary2DB::~CMSRunSummary2DB(){}
00389 }
00390 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00391 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::CMSRunSummary2DB,"CMSRunSummary2DB");
00392 #endif