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 void 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 void CMSRunSummary2DB::retrieveData( unsigned int runnumber){
00120 cmsrunsum result;
00121 std::string runinfoschema("CMS_RUNINFO");
00122 std::string runsessionParamTable("RUNSESSION_PARAMETER");
00123 coral::ConnectionService* svc=new coral::ConnectionService;
00124 lumi::DBConfig dbconf(*svc);
00125 if(!m_authpath.empty()){
00126 dbconf.setAuthentication(m_authpath);
00127 }
00128
00129
00130 std::string::size_type cutpos=m_source.find(';');
00131 std::string dbsource=m_source;
00132 std::string csvsource("");
00133 if(cutpos!=std::string::npos){
00134 dbsource=m_source.substr(0,cutpos);
00135 csvsource=m_source.substr(cutpos+1);
00136 }
00137
00138 coral::ISessionProxy* runinfosession=svc->connect(dbsource,coral::ReadOnly);
00139 try{
00140 coral::ITypeConverter& tpc=runinfosession->typeConverter();
00141 tpc.setCppTypeForSqlType("unsigned int","NUMBER(38)");
00142 runinfosession->transaction().start(true);
00143 coral::ISchema& runinfoschemaHandle=runinfosession->schema(runinfoschema);
00144 if(!runinfoschemaHandle.existsTable(runsessionParamTable)){
00145 throw lumi::Exception(std::string("non-existing table "+runsessionParamTable),"CMSRunSummary2DB","retrieveData");
00146 }
00147 coral::IQuery* amodetagQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00148 coral::AttributeList amodetagOutput;
00149 amodetagOutput.extend("amodetag",typeid(std::string));
00150 coral::AttributeList amodetagCondition;
00151 amodetagCondition=coral::AttributeList();
00152 amodetagCondition.extend("name",typeid(std::string));
00153 amodetagCondition.extend("runnumber",typeid(unsigned int));
00154 amodetagCondition["name"].data<std::string>()=std::string("CMS.SCAL:AMODEtag");
00155 amodetagCondition["runnumber"].data<unsigned int>()=runnumber;
00156 amodetagQuery->addToOutputList("distinct(STRING_VALUE)");
00157 amodetagQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",amodetagCondition);
00158
00159 amodetagQuery->defineOutput(amodetagOutput);
00160 coral::ICursor& amodetagCursor=amodetagQuery->execute();
00161 std::vector<std::string> amodes;
00162 while (amodetagCursor.next()){
00163 const coral::AttributeList& row=amodetagCursor.currentRow();
00164 amodes.push_back(row["amodetag"].data<std::string>());
00165
00166 }
00167
00168
00169
00170 std::string amd;
00171 for(std::vector<std::string>::iterator it=amodes.begin();it!=amodes.end();++it){
00172 if(it->find("PHYS")==std::string::npos) continue;
00173 amd=*it;
00174 }
00175 if(amd.size()==0&&amodes.size()!=0){
00176 amd=*(amodes.begin());
00177 }
00178 if(amd.size()==0){
00179 amd=std::string("PROTPHYS");
00180 }
00181
00182 result.amodetag=amd;
00183 delete amodetagQuery;
00184
00185 coral::IQuery* egevQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00186 coral::AttributeList egevOutput;
00187 egevOutput.extend("egev",typeid(std::string));
00188 coral::AttributeList egevCondition;
00189 egevCondition=coral::AttributeList();
00190 egevCondition.extend("name",typeid(std::string));
00191 egevCondition.extend("runnumber",typeid(unsigned int));
00192 egevCondition["name"].data<std::string>()=std::string("CMS.SCAL:EGEV");
00193 egevCondition["runnumber"].data<unsigned int>()=runnumber;
00194 egevQuery->addToOutputList("distinct(STRING_VALUE)");
00195 egevQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",egevCondition);
00196 egevQuery->defineOutput(egevOutput);
00197 coral::ICursor& egevCursor=egevQuery->execute();
00198 result.egev=0;
00199 while (egevCursor.next()){
00200 const coral::AttributeList& row=egevCursor.currentRow();
00201 std::string egevstr=row["egev"].data<std::string>();
00202 int tmpgev=str2int(egevstr);
00203 if(tmpgev>result.egev){
00204 result.egev=tmpgev;
00205 }
00206 }
00207 if(result.egev==0){
00208 result.egev=3500;
00209 }
00210 delete egevQuery;
00211
00212 coral::IQuery* seqQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00213 coral::AttributeList seqBindVariableList;
00214 seqBindVariableList.extend("runnumber",typeid(unsigned int));
00215 seqBindVariableList.extend("name",typeid(std::string));
00216
00217 seqBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00218 seqBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:SEQ_NAME");
00219 seqQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",seqBindVariableList);
00220 seqQuery->addToOutputList("STRING_VALUE");
00221 coral::ICursor& seqCursor=seqQuery->execute();
00222
00223 while( seqCursor.next() ){
00224 const coral::AttributeList& row=seqCursor.currentRow();
00225 result.sequence=row["STRING_VALUE"].data<std::string>();
00226 }
00227 delete seqQuery;
00228
00229 coral::IQuery* hltkeyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00230 coral::AttributeList hltkeyBindVariableList;
00231 hltkeyBindVariableList.extend("runnumber",typeid(unsigned int));
00232 hltkeyBindVariableList.extend("name",typeid(std::string));
00233
00234 hltkeyBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00235 hltkeyBindVariableList["name"].data<std::string>()=std::string("CMS.LVL0:HLT_KEY_DESCRIPTION");
00236 hltkeyQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",hltkeyBindVariableList);
00237 hltkeyQuery->addToOutputList("STRING_VALUE");
00238 coral::ICursor& hltkeyCursor=hltkeyQuery->execute();
00239
00240 while( hltkeyCursor.next() ){
00241 const coral::AttributeList& row=hltkeyCursor.currentRow();
00242 result.hltkey=row["STRING_VALUE"].data<std::string>();
00243 }
00244 delete hltkeyQuery;
00245
00246 coral::IQuery* fillQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00247 coral::AttributeList fillBindVariableList;
00248 fillBindVariableList.extend("runnumber",typeid(unsigned int));
00249 fillBindVariableList.extend("name",typeid(std::string));
00250
00251 fillBindVariableList["runnumber"].data<unsigned int>()=runnumber;
00252 fillBindVariableList["name"].data<std::string>()=std::string("CMS.SCAL:FILLN");
00253 fillQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",fillBindVariableList);
00254 fillQuery->addToOutputList("STRING_VALUE");
00255 fillQuery->addToOrderList("TIME");
00256
00257 coral::ICursor& fillCursor=fillQuery->execute();
00258 unsigned int cc=0;
00259 while( fillCursor.next() ){
00260 const coral::AttributeList& row=fillCursor.currentRow();
00261 if (cc==0){
00262 result.fillnumber=row["STRING_VALUE"].data<std::string>();
00263 }
00264 ++cc;
00265 }
00266 delete fillQuery;
00267 if (result.fillnumber.empty()){
00268 throw nonCollisionException("retrieveData","CMSRunSummary2DB");
00269 }
00270 coral::IQuery* startTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00271 coral::AttributeList startTVariableList;
00272 startTVariableList.extend("runnumber",typeid(unsigned int));
00273 startTVariableList.extend("name",typeid(std::string));
00274
00275 startTVariableList["runnumber"].data<unsigned int>()=runnumber;
00276 startTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:START_TIME_T");
00277 startTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",startTVariableList);
00278 startTQuery->addToOutputList("TIME");
00279 coral::ICursor& startTCursor=startTQuery->execute();
00280
00281 while( startTCursor.next() ){
00282 const coral::AttributeList& row=startTCursor.currentRow();
00283 result.startT=row["TIME"].data<coral::TimeStamp>();
00284 }
00285 delete startTQuery;
00286 coral::IQuery* stopTQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00287 coral::AttributeList stopTVariableList;
00288 stopTVariableList.extend("runnumber",typeid(unsigned int));
00289 stopTVariableList.extend("name",typeid(std::string));
00290
00291 stopTVariableList["runnumber"].data<unsigned int>()=runnumber;
00292 stopTVariableList["name"].data<std::string>()=std::string("CMS.LVL0:STOP_TIME_T");
00293 stopTQuery->setCondition("RUNNUMBER =:runnumber AND NAME =:name",stopTVariableList);
00294 stopTQuery->addToOutputList("TIME");
00295 coral::ICursor& stopTCursor=stopTQuery->execute();
00296
00297 while( stopTCursor.next() ){
00298 const coral::AttributeList& row=stopTCursor.currentRow();
00299 result.stopT=row["TIME"].data<coral::TimeStamp>();
00300 }
00301 delete stopTQuery;
00302 coral::IQuery* l1keyQuery=runinfoschemaHandle.tableHandle(runsessionParamTable).newQuery();
00303 coral::AttributeList l1keyOutput;
00304 l1keyOutput.extend("l1key",typeid(std::string));
00305 coral::AttributeList l1keyCondition;
00306 l1keyCondition=coral::AttributeList();
00307 l1keyCondition.extend("name",typeid(std::string));
00308 l1keyCondition.extend("runnumber",typeid(unsigned int));
00309 l1keyCondition["name"].data<std::string>()=std::string("CMS.TRG:TSC_KEY");
00310 l1keyCondition["runnumber"].data<unsigned int>()=runnumber;
00311 l1keyQuery->addToOutputList("STRING_VALUE");
00312 l1keyQuery->setCondition("NAME=:name AND RUNNUMBER=:runnumber",l1keyCondition);
00313
00314 l1keyQuery->defineOutput(l1keyOutput);
00315 coral::ICursor& l1keyCursor=l1keyQuery->execute();
00316 while (l1keyCursor.next()){
00317 const coral::AttributeList& row=l1keyCursor.currentRow();
00318 result.l1key=row["l1key"].data<std::string>();
00319 }
00320 delete l1keyQuery;
00321 }catch( const coral::Exception& er){
00322 runinfosession->transaction().rollback();
00323 delete runinfosession;
00324 delete svc;
00325 throw er;
00326 }
00327 runinfosession->transaction().commit();
00328 delete runinfosession;
00329
00330 if(csvsource.size()!=0){
00331 parseFillCSV(csvsource,result);
00332 }else{
00333 result.fillscheme=std::string("");
00334 result.ncollidingbunches=0;
00335 }
00336 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;
00337
00338
00339 coral::ISessionProxy* destsession=svc->connect(m_dest,coral::Update);
00340
00341 coral::ITypeConverter& desttpc=destsession->typeConverter();
00342 desttpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00343 try{
00344 destsession->transaction().start(false);
00345 coral::ISchema& destschema=destsession->nominalSchema();
00346 coral::ITable& destruntable=destschema.tableHandle(LumiNames::cmsrunsummaryTableName());
00347 coral::AttributeList runData;
00348 destruntable.dataEditor().rowBuffer(runData);
00349 runData["RUNNUM"].data<unsigned int>()=runnumber;
00350 runData["FILLNUM"].data<unsigned int>()=str2int(result.fillnumber);
00351 runData["SEQUENCE"].data<std::string>()=result.sequence;
00352 runData["HLTKEY"].data<std::string>()=result.hltkey;
00353 runData["STARTTIME"].data<coral::TimeStamp>()=result.startT;
00354 runData["STOPTIME"].data<coral::TimeStamp>()=result.stopT;
00355 runData["AMODETAG"].data<std::string>()=result.amodetag;
00356 runData["EGEV"].data<unsigned int>()=(unsigned int)result.egev;
00357 runData["L1KEY"].data<std::string>()=result.l1key;
00358 runData["FILLSCHEME"].data<std::string>()=result.fillscheme;
00359 runData["NCOLLIDINGBUNCHES"].data<unsigned int>()=result.ncollidingbunches;
00360 destruntable.dataEditor().insertRow(runData);
00361 }catch( const coral::Exception& er){
00362 std::cout<<"database problem "<<er.what()<<std::endl;
00363 destsession->transaction().rollback();
00364 delete destsession;
00365 delete svc;
00366 throw er;
00367 }
00368 destsession->transaction().commit();
00369 delete svc;
00370 }
00371 const std::string CMSRunSummary2DB::dataType() const{
00372 return "CMSRUNSUMMARY";
00373 }
00374 const std::string CMSRunSummary2DB::sourceType() const{
00375 return "DB";
00376 }
00377 unsigned int CMSRunSummary2DB::str2int(const std::string& s) const{
00378 std::istringstream myStream(s);
00379 unsigned int i;
00380 if(myStream>>i){
00381 return i;
00382 }else{
00383 throw lumi::Exception(std::string("str2int error"),"str2int","CMSRunSummary2DB");
00384 }
00385 }
00386 CMSRunSummary2DB::~CMSRunSummary2DB(){}
00387 }
00388 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00389 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::CMSRunSummary2DB,"CMSRunSummary2DB");
00390 #endif