CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch1/src/RecoLuminosity/LumiProducer/plugins/TRGWBM2DB.cc

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_TRGWBM2DB_h 
00002 #define RecoLuminosity_LumiProducer_TRGWBM2DB_h 
00003 #include "CoralBase/AttributeList.h"
00004 #include "CoralBase/Attribute.h"
00005 #include "CoralBase/AttributeSpecification.h"
00006 #include "CoralBase/Exception.h"
00007 #include "RelationalAccess/ConnectionService.h"
00008 #include "RelationalAccess/ISessionProxy.h"
00009 #include "RelationalAccess/ITransaction.h"
00010 #include "RelationalAccess/ITypeConverter.h"
00011 #include "RelationalAccess/IQuery.h"
00012 #include "RelationalAccess/ICursor.h"
00013 #include "RelationalAccess/ISchema.h"
00014 #include "RelationalAccess/IView.h"
00015 #include "RelationalAccess/ITable.h"
00016 #include "RelationalAccess/ITableDataEditor.h"
00017 #include "RelationalAccess/IBulkOperation.h"
00018 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00019 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00020 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00021 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00022 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00023 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00024 #include <iostream>
00025 #include <sstream>
00026 #include <map>
00027 namespace lumi{
00028   class TRGWBM2DB : public DataPipe{
00029   public:
00030     const static unsigned int COMMITLSINTERVAL=20; //commit interval in LS,totalrow=nsl*192
00031     explicit TRGWBM2DB(const std::string& dest);
00032     virtual void retrieveData( unsigned int runnumber);
00033     virtual const std::string dataType() const;
00034     virtual const std::string sourceType() const;
00035     virtual ~TRGWBM2DB();
00036   private:
00037     std::string int2str(unsigned int t,unsigned int width);
00038     unsigned int str2int(const std::string& s);
00039   private:
00040     //per run information
00041     typedef std::vector<std::string> TriggerNameResult_Algo;
00042     typedef std::vector<std::string> TriggerNameResult_Tech;
00043     typedef std::vector<unsigned int> PrescaleResult_Algo;
00044     typedef std::vector<unsigned int> PrescaleResult_Tech;
00045     //per lumisection information
00046     typedef unsigned long long DEADCOUNT;
00047     typedef std::vector<DEADCOUNT> TriggerDeadCountResult;
00048     typedef std::vector<unsigned int> BITCOUNT;
00049     typedef std::vector<BITCOUNT> TriggerCountResult_Algo;
00050     typedef std::vector<BITCOUNT> TriggerCountResult_Tech;
00051   };//cl TRGWBM2DB
00052   //
00053   //implementation
00054   //
00055  TRGWBM2DB::TRGWBM2DB(const std::string& dest):DataPipe(dest){}
00056   void TRGWBM2DB::retrieveData( unsigned int runnumber){
00057     std::string runnumberstr=int2str(runnumber,6);
00058     //query source GT database
00059     coral::ConnectionService* svc=new coral::ConnectionService;
00060     lumi::DBConfig dbconf(*svc);
00061     if(!m_authpath.empty()){
00062       dbconf.setAuthentication(m_authpath);
00063     }
00064     //std::cout<<"m_source "<<m_source<<std::endl;
00065     coral::ISessionProxy* trgsession=svc->connect(m_source, coral::ReadOnly);
00066     coral::ITypeConverter& tpc=trgsession->typeConverter();
00067 
00068     tpc.setCppTypeForSqlType("unsigned int","NUMBER(7)");
00069     tpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00070     tpc.setCppTypeForSqlType("unsigned long long","NUMBER(20)");
00071     
00072     coral::AttributeList bindVariableList;
00073     bindVariableList.extend("runnumber",typeid(unsigned int));
00074     bindVariableList["runnumber"].data<unsigned int>()=runnumber;
00075     std::string wbmschema("CMS_WBM");
00076     std::string algoname("LEVEL1_TRIGGER_ALGO_CONDITIONS");
00077     std::string techname("LEVEL1_TRIGGER_TECH_CONDITIONS");
00078     std::string deadname("LEVEL1_TRIGGER_CONDITIONS");
00079     
00080     std::string gtschema("CMS_GT");
00081     std::string runtechviewname("GT_RUN_TECH_VIEW");
00082     std::string runalgoviewname("GT_RUN_ALGO_VIEW");
00083     std::string runprescalgoviewname("GT_RUN_PRESC_ALGO_VIEW");
00084     std::string runpresctechviewname("GT_RUN_PRESC_TECH_VIEW");
00085 
00086     //data exchange format
00087     lumi::TRGWBM2DB::BITCOUNT mybitcount_algo;
00088     mybitcount_algo.reserve(lumi::N_TRGALGOBIT);
00089     lumi::TRGWBM2DB::BITCOUNT mybitcount_tech; 
00090     mybitcount_tech.reserve(lumi::N_TRGTECHBIT);
00091     lumi::TRGWBM2DB::TriggerNameResult_Algo algonames;
00092     algonames.reserve(lumi::N_TRGALGOBIT);
00093     lumi::TRGWBM2DB::TriggerNameResult_Tech technames;
00094     technames.reserve(lumi::N_TRGTECHBIT);
00095     lumi::TRGWBM2DB::PrescaleResult_Algo algoprescale;
00096     algoprescale.reserve(lumi::N_TRGALGOBIT);
00097     lumi::TRGWBM2DB::PrescaleResult_Tech techprescale;
00098     techprescale.reserve(lumi::N_TRGTECHBIT);
00099     lumi::TRGWBM2DB::TriggerCountResult_Algo algocount;
00100     algocount.reserve(400);
00101     lumi::TRGWBM2DB::TriggerCountResult_Tech techcount;
00102     techcount.reserve(400);
00103     lumi::TRGWBM2DB::TriggerDeadCountResult deadtimeresult;
00104     deadtimeresult.reserve(400);
00105     coral::ITransaction& transaction=trgsession->transaction();
00106     transaction.start(true);
00107     //uncomment if you want to see all the visible views
00130     coral::ISchema& wbmschemaHandle=trgsession->schema(wbmschema);    
00131     if(!wbmschemaHandle.existsTable(algoname)){
00132       throw lumi::Exception(std::string("non-existing table ")+algoname,"retrieveData","TRGWBM2DB");
00133     }
00134     if(!wbmschemaHandle.existsTable(techname)){
00135       throw lumi::Exception(std::string("non-existing table ")+techname,"retrieveData","TRGWBM2DB");
00136     }
00137     if(!wbmschemaHandle.existsTable(deadname)){
00138       throw lumi::Exception(std::string("non-existing table ")+deadname,"retrieveData","TRGWBM2DB");
00139     }
00140     //
00141     //select LUMISEGMENTNR,GTALGOCOUNTS,BIT from cms_wbm.LEVEL1_TRIGGER_ALGO_CONDITIONS where RUNNUMBER=133881 order by LUMISEGMENTNR,BIT;
00142     //note: LUMISEGMENTNR count from 1
00143     //note: BIT count from 0-127
00144     //
00145     coral::IQuery* Queryalgo=wbmschemaHandle.newQuery();
00146     Queryalgo->addToTableList(algoname);
00147     coral::AttributeList qalgoOutput;
00148     qalgoOutput.extend("counts",typeid(unsigned int));
00149     qalgoOutput.extend("lsnr",typeid(unsigned int));
00150     qalgoOutput.extend("algobit",typeid(unsigned int));
00151     Queryalgo->addToOutputList("GTALGOCOUNTS","counts");
00152     Queryalgo->addToOutputList("LUMISEGMENTNR","lsnr");
00153     Queryalgo->addToOutputList("BIT","algobit");
00154     Queryalgo->setCondition("RUNNUMBER =:runnumber",bindVariableList);
00155     Queryalgo->addToOrderList("LUMISEGMENTNR");
00156     Queryalgo->addToOrderList("BIT");
00157     Queryalgo->defineOutput(qalgoOutput);
00158     coral::ICursor& c=Queryalgo->execute();
00159     unsigned int s=0;
00160     while( c.next() ){
00161       const coral::AttributeList& row = c.currentRow();     
00162       //row.toOutputStream( std::cout ) << std::endl;
00163       unsigned int lsnr=row["lsnr"].data<unsigned int>();
00164       unsigned int count=row["counts"].data<unsigned int>();
00165       mybitcount_algo.push_back(count);
00166       unsigned int algobit=row["algobit"].data<unsigned int>();
00167       if(algobit==(lumi::N_TRGALGOBIT-1)){
00168         ++s;
00169         while(s!=lsnr){
00170           std::cout<<"ALGO COUNT alert: found hole in LS range"<<std::endl;
00171           std::cout<<"    fill all algocount 0 for LS "<<s<<std::endl;
00172           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT,0);
00173           algocount.push_back(tmpzero);
00174           ++s;
00175         }
00176         algocount.push_back(mybitcount_algo);
00177         mybitcount_algo.clear();
00178       }
00179     }
00180     if(s==0){
00181       c.close();
00182       delete Queryalgo;
00183       transaction.commit();
00184       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for algocounts"),"retrieveData","TRGWBM2DB");
00185     }
00186     delete Queryalgo;
00187     transaction.commit();
00188     //std::cout<<"read algo counts"<<std::endl;
00189     //
00190     //select LUMISEGMENTNR,GTTECHCOUNTS,BIT from cms_wbm.LEVEL1_TRIGGER_TECH_CONDITIONS where RUNNUMBER=133881 order by LUMISEGMENTNR,BIT;
00191     //note: LUMISEGMENTNR count from 1
00192     //note: BIT count from 0-63
00193     //
00194     transaction.start(true);
00195     coral::IQuery* Querytech=wbmschemaHandle.newQuery();
00196     Querytech->addToTableList(techname);
00197     coral::AttributeList qtechOutput;
00198     qtechOutput.extend("counts",typeid(unsigned int));
00199     qtechOutput.extend("lsnr",typeid(unsigned int));
00200     qtechOutput.extend("techbit",typeid(unsigned int));
00201     Querytech->addToOutputList("GTTECHCOUNTS","counts");
00202     Querytech->addToOutputList("LUMISEGMENTNR","lsnr");
00203     Querytech->addToOutputList("BIT","techbit");
00204     Querytech->setCondition("RUNNUMBER=:runnumber",bindVariableList);
00205     Querytech->addToOrderList("LUMISEGMENTNR");
00206     Querytech->addToOrderList("BIT");
00207     Querytech->defineOutput(qtechOutput);
00208     coral::ICursor& techcursor=Querytech->execute();
00209     
00210     s=0;
00211     while( techcursor.next() ){
00212       const coral::AttributeList& row = techcursor.currentRow();     
00213       //row.toOutputStream( std::cout ) << std::endl;
00214       unsigned int lsnr=row["lsnr"].data<unsigned int>();
00215       unsigned int count=row["counts"].data<unsigned int>();
00216       unsigned int techbit=row["techbit"].data<unsigned int>();
00217       mybitcount_tech.push_back(count);
00218       if(techbit==(lumi::N_TRGTECHBIT-1)){
00219         ++s;
00220         while( s!=lsnr){
00221           std::cout<<"TECH COUNT alert: found hole in LS range"<<std::endl;
00222           std::cout<<"     fill all techcount with 0 for LS "<<s<<std::endl;
00223           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT,0);
00224           techcount.push_back(tmpzero);
00225           ++s;
00226         }
00227         techcount.push_back(mybitcount_tech);
00228         mybitcount_tech.clear();
00229       }
00230     }
00231     if(s==0){
00232       techcursor.close();
00233       delete Querytech;
00234       transaction.commit();
00235       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for techcounts"),"retrieveData","TRGWBM2DB");
00236     }
00237     delete Querytech;
00238     transaction.commit();
00239     //std::cout<<"read tech counts"<<std::endl;
00240     //
00241     //select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=133881 order by LUMISEGMENTNR;
00242     //Note: LUMISEGMENTNR counts from 1
00243     //
00244     transaction.start(true);
00245     coral::IQuery* Querydead=wbmschemaHandle.newQuery();
00246     Querydead->addToTableList(deadname);
00247     coral::AttributeList qdeadOutput;
00248     qdeadOutput.extend("counts",typeid(unsigned int));
00249     qdeadOutput.extend("lsnr",typeid(unsigned int));
00250     Querydead->addToOutputList("DEADTIMEBEAMACTIVE","counts");
00251     Querydead->addToOutputList("LUMISEGMENTNR","lsnr");
00252     coral::AttributeList bindVariablesDead;
00253     bindVariablesDead.extend("runnumber",typeid(int));
00254     bindVariablesDead["runnumber"].data<int>()=runnumber;
00255     Querydead->setCondition("RUNNUMBER=:runnumber",bindVariablesDead);
00256     Querydead->addToOrderList("LUMISEGMENTNR");
00257     Querydead->defineOutput(qdeadOutput);
00258     coral::ICursor& deadcursor=Querydead->execute();
00259     s=0;
00260     while( deadcursor.next() ){
00261       const coral::AttributeList& row = deadcursor.currentRow();     
00262       //row.toOutputStream( std::cout ) << std::endl;
00263       ++s;
00264       unsigned int lsnr=row["lsnr"].data<unsigned int>();
00265       while(s!=lsnr){
00266         std::cout<<"DEADTIME alert: found hole in LS range"<<std::endl;
00267         std::cout<<"         fill deadtimebeamactive 0 for LS "<<s<<std::endl;
00268         deadtimeresult.push_back(0);
00269         ++s;
00270       }
00271       unsigned int count=row["counts"].data<unsigned int>();
00272       deadtimeresult.push_back(count);
00273     }
00274     //std::cout<<"deadtimeresult raw "<<deadtimeresult.size()<<std::endl;
00275     if(s==0){
00276       deadcursor.close();
00277       delete Querydead;
00278       transaction.commit();
00279       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for deadcounts"),"retrieveData","TRGWBM2DB");
00280       return;
00281     }
00282     delete Querydead;
00283     transaction.commit();
00284     //std::cout<<"read dead counts"<<std::endl;
00289     transaction.start(true);
00290     coral::ISchema& gtschemaHandle=trgsession->schema(gtschema);
00291     if(!gtschemaHandle.existsView(runtechviewname)){
00292       throw lumi::Exception(std::string("non-existing view ")+runtechviewname,"str2int","TRGWBM2DB");
00293     }
00294     if(!gtschemaHandle.existsView(runalgoviewname)){
00295       throw lumi::Exception(std::string("non-existing view ")+runalgoviewname,"str2int","TRGWBM2DB");
00296     }
00297     if(!gtschemaHandle.existsView(runprescalgoviewname)){
00298       throw lumi::Exception(std::string("non-existing view ")+runprescalgoviewname,"str2int","TRGWBM2DB");
00299     }
00300     if(!gtschemaHandle.existsView(runpresctechviewname)){
00301       throw lumi::Exception(std::string("non-existing view ")+runpresctechviewname,"str2int","TRGWBM2DB");
00302     }
00303     //
00304     //select algo_index,alias from cms_gt.gt_run_algo_view where runnumber=:runnumber order by algo_index;
00305     //
00306     std::map<unsigned int,std::string> triggernamemap;
00307     coral::IQuery* QueryName=gtschemaHandle.newQuery();
00308     QueryName->addToTableList(runalgoviewname);
00309     coral::AttributeList qAlgoNameOutput;
00310     qAlgoNameOutput.extend("algo_index",typeid(unsigned int));
00311     qAlgoNameOutput.extend("alias",typeid(std::string));
00312     QueryName->addToOutputList("algo_index");
00313     QueryName->addToOutputList("alias");
00314     QueryName->setCondition("runnumber =:runnumber",bindVariableList);
00315     QueryName->addToOrderList("algo_index");
00316     QueryName->defineOutput(qAlgoNameOutput);
00317     coral::ICursor& algonamecursor=QueryName->execute();
00318     while( algonamecursor.next() ){
00319       const coral::AttributeList& row = algonamecursor.currentRow();     
00320       //row.toOutputStream( std::cout ) << std::endl;
00321       unsigned int algo_index=row["algo_index"].data<unsigned int>();
00322       std::string algo_name=row["alias"].data<std::string>();
00323       triggernamemap.insert(std::make_pair(algo_index,algo_name));
00324     }
00325     delete QueryName;
00326     //std::cout<<"read alias"<<std::endl;
00327     transaction.commit();
00328     
00329     //
00330     //select techtrig_index,name from cms_gt.gt_run_tech_view where runnumber=:runnumber order by techtrig_index;
00331     //
00332     transaction.start(true);
00333     std::map<unsigned int,std::string> techtriggernamemap;
00334     coral::IQuery* QueryTechName=gtschemaHandle.newQuery();
00335     QueryTechName->addToTableList(runtechviewname);
00336     coral::AttributeList qTechNameOutput;
00337     qTechNameOutput.extend("techtrig_index",typeid(unsigned int));
00338     qTechNameOutput.extend("name",typeid(std::string));
00339     QueryTechName->addToOutputList("techtrig_index");
00340     QueryTechName->addToOutputList("name");
00341     QueryTechName->setCondition("runnumber =:runnumber",bindVariableList);
00342     QueryTechName->addToOrderList("techtrig_index");
00343     QueryTechName->defineOutput(qTechNameOutput);
00344     coral::ICursor& technamecursor=QueryTechName->execute();
00345     while( technamecursor.next() ){
00346       const coral::AttributeList& row = technamecursor.currentRow();     
00347       //row.toOutputStream( std::cout ) << std::endl;
00348       unsigned int tech_index=row["techtrig_index"].data<unsigned int>();
00349       std::string tech_name=row["name"].data<std::string>();
00350       techtriggernamemap.insert(std::make_pair(tech_index,tech_name));
00351     }
00352     delete QueryTechName;
00353     transaction.commit();
00354     //
00355     //select prescale_factor_algo_000,prescale_factor_algo_001..._127 from cms_gt.gt_run_presc_algo_view where runr=:runnumber and prescale_index=0;
00356     //    
00357     transaction.start(true);
00358     coral::IQuery* QueryAlgoPresc=gtschemaHandle.newQuery();
00359     QueryAlgoPresc->addToTableList(runprescalgoviewname);
00360     coral::AttributeList qAlgoPrescOutput;
00361     std::string algoprescBase("PRESCALE_FACTOR_ALGO_");
00362     for(unsigned int bitidx=0;bitidx<lumi::N_TRGALGOBIT;++bitidx){
00363       std::string algopresc=algoprescBase+int2str(bitidx,3);
00364       qAlgoPrescOutput.extend(algopresc,typeid(unsigned int));
00365     }
00366     for(unsigned int bitidx=0;bitidx<lumi::N_TRGALGOBIT;++bitidx){
00367       std::string algopresc=algoprescBase+int2str(bitidx,3);
00368       QueryAlgoPresc->addToOutputList(algopresc);
00369     }
00370     coral::AttributeList PrescbindVariable;
00371     PrescbindVariable.extend("runnumber",typeid(int));
00372     PrescbindVariable.extend("prescaleindex",typeid(int));
00373     PrescbindVariable["runnumber"].data<int>()=runnumber;
00374     PrescbindVariable["prescaleindex"].data<int>()=0;
00375     QueryAlgoPresc->setCondition("runnr =:runnumber AND prescale_index =:prescaleindex",PrescbindVariable);
00376     QueryAlgoPresc->defineOutput(qAlgoPrescOutput);
00377     coral::ICursor& algopresccursor=QueryAlgoPresc->execute();
00378     while( algopresccursor.next() ){
00379       const coral::AttributeList& row = algopresccursor.currentRow();     
00380       //row.toOutputStream( std::cout ) << std::endl;  
00381       for(unsigned int bitidx=0;bitidx<128;++bitidx){
00382         std::string algopresc=algoprescBase+int2str(bitidx,3);
00383         algoprescale.push_back(row[algopresc].data<unsigned int>());
00384       }
00385     }
00386     delete QueryAlgoPresc;
00387     transaction.commit();
00388 
00389     //
00390     //select prescale_factor_tt_000,prescale_factor_tt_001..._63 from cms_gt.gt_run_presc_tech_view where runr=:runnumber and prescale_index=0;
00391     //    
00392     transaction.start(true);
00393     coral::IQuery* QueryTechPresc=gtschemaHandle.newQuery();
00394     QueryTechPresc->addToTableList(runpresctechviewname);
00395     coral::AttributeList qTechPrescOutput;
00396     std::string techprescBase("PRESCALE_FACTOR_TT_");
00397     for(unsigned int bitidx=0;bitidx<lumi::N_TRGTECHBIT;++bitidx){
00398       std::string techpresc=techprescBase+this->int2str(bitidx,3);
00399       qTechPrescOutput.extend(techpresc,typeid(unsigned int));
00400     }
00401     for(unsigned int bitidx=0;bitidx<lumi::N_TRGTECHBIT;++bitidx){
00402       std::string techpresc=techprescBase+int2str(bitidx,3);
00403       QueryTechPresc->addToOutputList(techpresc);
00404     }
00405     coral::AttributeList TechPrescbindVariable;
00406     TechPrescbindVariable.extend("runnumber",typeid(int));
00407     TechPrescbindVariable.extend("prescaleindex",typeid(int));
00408     TechPrescbindVariable["runnumber"].data<int>()=runnumber;
00409     TechPrescbindVariable["prescaleindex"].data<int>()=0;
00410     QueryTechPresc->setCondition("runnr =:runnumber AND prescale_index =:prescaleindex",TechPrescbindVariable);
00411     QueryTechPresc->defineOutput(qTechPrescOutput);
00412     coral::ICursor& techpresccursor=QueryTechPresc->execute();
00413     while( techpresccursor.next() ){
00414       const coral::AttributeList& row = techpresccursor.currentRow();     
00415       //row.toOutputStream( std::cout ) << std::endl;
00416       for(unsigned int bitidx=0;bitidx<lumi::N_TRGTECHBIT;++bitidx){
00417         std::string techpresc=techprescBase+int2str(bitidx,3);
00418         techprescale.push_back(row[techpresc].data<unsigned int>());
00419       }
00420     }
00421     delete QueryTechPresc;
00422     transaction.commit();
00423     transaction.commit();
00424     //std::cout<<"tech prescale ok"<<std::endl;
00425     delete trgsession;
00426 
00427     //
00428     //reprocess Algo name result filling unallocated trigger bit with string "False"
00429     //
00430     for(size_t algoidx=0;algoidx<lumi::N_TRGALGOBIT;++algoidx){
00431       std::map<unsigned int,std::string>::iterator pos=triggernamemap.find(algoidx);
00432       if(pos!=triggernamemap.end()){
00433         algonames.push_back(pos->second);
00434       }else{
00435         algonames.push_back("False");
00436       }
00437     }
00438     //
00439     //reprocess Tech name result filling unallocated trigger bit with string "False"  
00440     //
00441     std::stringstream ss;
00442     for(size_t techidx=0;techidx<lumi::N_TRGTECHBIT;++techidx){
00443       ss<<techidx;
00444       technames.push_back(ss.str());
00445       ss.str(""); //clear the string buffer after usage
00446     }
00447     //
00448     //cross check result size
00449     //
00450     if(algonames.size()!=lumi::N_TRGALGOBIT || technames.size()!=lumi::N_TRGTECHBIT){
00451       throw lumi::Exception("wrong number of bits","retrieveData","TRGWBM2DB");
00452     }
00453     if(algoprescale.size()!=lumi::N_TRGALGOBIT || techprescale.size()!=lumi::N_TRGTECHBIT){
00454       throw lumi::Exception("wrong number of prescale","retrieveData","TRGWBM2DB");
00455     }
00456     if(deadtimeresult.size()!=algocount.size() || deadtimeresult.size()!=techcount.size()){
00457       throw lumi::Exception("inconsistent number of LS","retrieveData","TRGWBM2DB");
00458     }
00459     //    
00460     //write data into lumi db
00461     //
00462     coral::ISessionProxy* lumisession=svc->connect(m_dest,coral::Update);
00463     coral::ITypeConverter& lumitpc=lumisession->typeConverter();
00464     lumitpc.setCppTypeForSqlType("unsigned int","NUMBER(7)");
00465     lumitpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00466     lumitpc.setCppTypeForSqlType("unsigned long long","NUMBER(20)");
00467     
00468     TriggerDeadCountResult::const_iterator deadIt;
00469     TriggerDeadCountResult::const_iterator deadBeg=deadtimeresult.begin();
00470     TriggerDeadCountResult::const_iterator deadEnd=deadtimeresult.end();
00471 
00472     unsigned int totalcmsls=deadtimeresult.size();
00473     std::cout<<"inserting totalcmsls "<<totalcmsls<<std::endl;
00474     std::map< unsigned long long, std::vector<unsigned long long> > idallocationtable;
00475     try{
00476       std::cout<<"\t allocating total ids "<<totalcmsls*lumi::N_TRGBIT<<std::endl; 
00477       lumisession->transaction().start(false);
00478       lumi::idDealer idg(lumisession->nominalSchema());
00479       unsigned long long trgID = idg.generateNextIDForTable(LumiNames::trgTableName(),totalcmsls*lumi::N_TRGBIT)-totalcmsls*lumi::N_TRGBIT;
00480       lumisession->transaction().commit();
00481       unsigned int trglscount=0;
00482       for(deadIt=deadBeg;deadIt!=deadEnd;++deadIt,++trglscount){
00483         std::vector<unsigned long long> bitvec;
00484         bitvec.reserve(lumi::N_TRGBIT);
00485         BITCOUNT& algoinbits=algocount[trglscount];
00486         BITCOUNT& techinbits=techcount[trglscount];
00487         BITCOUNT::const_iterator algoBitIt;
00488         BITCOUNT::const_iterator algoBitBeg=algoinbits.begin();
00489         BITCOUNT::const_iterator algoBitEnd=algoinbits.end();   
00490         for(algoBitIt=algoBitBeg;algoBitIt!=algoBitEnd;++algoBitIt,++trgID){
00491           bitvec.push_back(trgID);
00492         }
00493         BITCOUNT::const_iterator techBitIt;
00494         BITCOUNT::const_iterator techBitBeg=techinbits.begin();
00495         BITCOUNT::const_iterator techBitEnd=techinbits.end();
00496         for(techBitIt=techBitBeg;techBitIt!=techBitEnd;++techBitIt,++trgID){
00497           bitvec.push_back(trgID);
00498         }
00499         idallocationtable.insert(std::make_pair(trglscount,bitvec));
00500       }
00501       std::cout<<"\t all ids allocated"<<std::endl; 
00502       coral::AttributeList trgData;
00503       trgData.extend<unsigned long long>("TRG_ID");
00504       trgData.extend<unsigned int>("RUNNUM");
00505       trgData.extend<unsigned int>("CMSLSNUM");
00506       trgData.extend<unsigned int>("BITNUM");
00507       trgData.extend<std::string>("BITNAME");
00508       trgData.extend<unsigned int>("TRGCOUNT");
00509       trgData.extend<unsigned long long>("DEADTIME");
00510       trgData.extend<unsigned int>("PRESCALE");
00511 
00512       unsigned long long& trg_id=trgData["TRG_ID"].data<unsigned long long>();
00513       unsigned int& trgrunnum=trgData["RUNNUM"].data<unsigned int>();
00514       unsigned int& cmslsnum=trgData["CMSLSNUM"].data<unsigned int>();
00515       unsigned int& bitnum=trgData["BITNUM"].data<unsigned int>();
00516       std::string& bitname=trgData["BITNAME"].data<std::string>();
00517       unsigned int& count=trgData["TRGCOUNT"].data<unsigned int>();
00518       unsigned long long& deadtime=trgData["DEADTIME"].data<unsigned long long>();
00519       unsigned int& prescale=trgData["PRESCALE"].data<unsigned int>();
00520      
00521       trglscount=0;    
00522       coral::IBulkOperation* trgInserter=0; 
00523       unsigned int comittedls=0; 
00524       for(deadIt=deadBeg;deadIt!=deadEnd;++deadIt,++trglscount ){
00525         unsigned int cmslscount=trglscount+1;
00526         BITCOUNT& algoinbits=algocount[trglscount];
00527         BITCOUNT& techinbits=techcount[trglscount];
00528         unsigned int trgbitcount=0;
00529         BITCOUNT::const_iterator algoBitIt;
00530         BITCOUNT::const_iterator algoBitBeg=algoinbits.begin();
00531         BITCOUNT::const_iterator algoBitEnd=algoinbits.end();
00532         if(!lumisession->transaction().isActive()){ 
00533           lumisession->transaction().start(false);
00534           coral::ITable& trgtable=lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
00535           trgInserter=trgtable.dataEditor().bulkInsert(trgData,2048);
00536         }
00537         for(algoBitIt=algoBitBeg;algoBitIt!=algoBitEnd;++algoBitIt,++trgbitcount){
00538           trg_id = idallocationtable[trglscount].at(trgbitcount);
00539           deadtime=*deadIt;
00540           trgrunnum = runnumber;
00541           cmslsnum = cmslscount;
00542           bitnum=trgbitcount;
00543           bitname=algonames[trgbitcount];
00544           count=*algoBitIt;
00545           prescale=algoprescale[trgbitcount];
00546 
00547           //coral::AttributeList rowbuf;
00548           //trgtable.dataEditor().rowBuffer(rowbuf);
00549           //rowbuf["TRG_ID"].data<unsigned long long>()=idg.generateNextIDForTable(LumiNames::trgTableName());
00550           //rowbuf["RUNNUM"].data<unsigned int>()=runnumber;
00551           //rowbuf["CMSLSNUM"].data<unsigned int>()=cmslscount;
00552           //rowbuf["BITNUM"].data<unsigned int>()=trgbitcount;
00553           //rowbuf["BITNAME"].data<std::string>()=algonames[trgbitcount];
00554           //rowbuf["TRGCOUNT"].data<unsigned int>()=*algoBitIt;
00555           //rowbuf["DEADTIME"].data<unsigned long long>()=*deadIt;
00556           //rowbuf["PRESCALE"].data<unsigned int>()=algoprescale[trgbitcount];
00557           //trgtable.dataEditor().insertRow(rowbuf);
00558           trgInserter->processNextIteration();  
00559           //std::cout<<"tot "<<tot<<std::endl;
00560         }
00561         BITCOUNT::const_iterator techBitIt;
00562         BITCOUNT::const_iterator techBitBeg=techinbits.begin();
00563         BITCOUNT::const_iterator techBitEnd=techinbits.end();
00564         for(techBitIt=techBitBeg;techBitIt!=techBitEnd;++techBitIt,++trgbitcount){
00565           trg_id = idallocationtable[trglscount].at(trgbitcount);
00566           deadtime=*deadIt;
00567           trgrunnum = runnumber;
00568           cmslsnum = cmslscount;
00569           bitnum=trgbitcount;
00570           bitname=technames[trgbitcount-lumi::N_TRGALGOBIT];
00571           count=*techBitIt;
00572           prescale=techprescale[trgbitcount-lumi::N_TRGALGOBIT];
00573           trgInserter->processNextIteration();  
00574           //coral::AttributeList rowbuf;
00575           //trgtable.dataEditor().rowBuffer(rowbuf);
00576           //rowbuf["TRG_ID"].data<unsigned long long>()=idg.generateNextIDForTable(LumiNames::trgTableName());
00577           //rowbuf["RUNNUM"].data<unsigned int>()=runnumber;
00578           //rowbuf["CMSLSNUM"].data<unsigned int>()=cmslscount;
00579           //rowbuf["BITNUM"].data<unsigned int>()=trgbitcount;
00580           //rowbuf["BITNAME"].data<std::string>()=technames[trgbitcount-lumi::N_TRGALGOBIT];
00581           //rowbuf["TRGCOUNT"].data<unsigned int>()=*techBitIt;
00582           //rowbuf["DEADTIME"].data<unsigned long long>()=*deadIt;
00583           //rowbuf["PRESCALE"].data<unsigned int>()=techprescale[trgbitcount-lumi::N_TRGALGOBIT];
00584           //trgtable.dataEditor().insertRow(rowbuf);
00585         }
00586         trgInserter->flush();//flush every ls
00587         ++comittedls;
00588         if(comittedls==TRGWBM2DB::COMMITLSINTERVAL){
00589           std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00590           delete trgInserter; trgInserter=0;
00591           lumisession->transaction().commit();
00592           comittedls=0;
00593           std::cout<<"\t committed "<<std::endl; 
00594         }else if( trglscount==( totalcmsls-1) ){
00595           std::cout<<"\t committing at the end"<<std::endl; 
00596           delete trgInserter; trgInserter=0;
00597           lumisession->transaction().commit();
00598           std::cout<<"\t done"<<std::endl; 
00599         }
00600       }
00601     }catch( const coral::Exception& er){
00602       lumisession->transaction().rollback();
00603       delete lumisession;
00604       delete svc;
00605       throw er;
00606     }
00607     //std::cout<<"about to commit "<<std::endl;
00608     //lumisession->transaction().commit();
00609     delete lumisession;
00610     delete svc;
00611   }
00612   const std::string TRGWBM2DB::dataType() const{
00613     return "TRG";
00614   }
00615   const std::string TRGWBM2DB::sourceType() const{
00616     return "DB";
00617   }
00618   //utilities
00619   std::string TRGWBM2DB::int2str(unsigned int t, unsigned int width){
00620     std::stringstream ss;
00621     ss.width(width);
00622     ss.fill('0');
00623     ss<<t;
00624     return ss.str();
00625   }
00626   unsigned int TRGWBM2DB::str2int(const std::string& s){
00627     std::istringstream myStream(s);
00628     unsigned int i;
00629     if(myStream>>i){
00630       return i;
00631     }else{
00632       throw lumi::Exception(std::string("str2int error"),"str2int","TRGWBM2DB");
00633     }
00634   }
00635   TRGWBM2DB::~TRGWBM2DB(){}
00636 }//ns lumi
00637 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00638 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::TRGWBM2DB,"TRGWBM2DB");
00639 #endif