CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoLuminosity/LumiProducer/plugins/TRG2DB.cc

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