CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/RecoLuminosity/LumiProducer/plugins/TRGScalers2DB.cc

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_TRGScalers2DB_h 
00002 #define RecoLuminosity_LumiProducer_TRGScalers2DB_h 
00003 #include "CoralBase/AttributeList.h"
00004 #include "CoralBase/Attribute.h"
00005 #include "CoralBase/AttributeSpecification.h"
00006 #include "CoralBase/Blob.h"
00007 #include "CoralBase/Exception.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/RevisionDML.h"
00026 #include <cstring>
00027 #include <cstdlib>
00028 #include <iostream>
00029 #include <sstream>
00030 #include <stdexcept>
00031 #include <map>
00032 namespace lumi{
00033   class TRGScalers2DB : public DataPipe{
00034   public:
00035     const static unsigned int COMMITLSINTERVAL=150; //commit interval in LS, totalrow=nsl*192
00036     const static unsigned int COMMITLSTRGINTERVAL=550; //commit interval in LS of schema2
00037     explicit TRGScalers2DB(const std::string& dest);
00038     virtual void retrieveData( unsigned int runnumber);
00039     virtual const std::string dataType() const;
00040     virtual const std::string sourceType() const;
00041     virtual ~TRGScalers2DB();                                                                                                     
00042 
00043     //per run information
00044     typedef std::vector<std::string> TriggerNameResult_Algo;
00045     typedef std::vector<std::string> TriggerNameResult_Tech;
00046     //per lumisection information
00047     typedef unsigned long long DEADCOUNT;
00048     typedef float DEADFRAC;
00049     typedef std::vector<DEADCOUNT> TriggerDeadCountResult;
00050     typedef std::vector<DEADFRAC> TriggerDeadFracResult;
00051     typedef std::vector<unsigned int> BITCOUNT;
00052     typedef std::vector<BITCOUNT> TriggerCountResult_Algo;
00053     typedef std::vector<BITCOUNT> TriggerCountResult_Tech;
00054     typedef std::map< unsigned int, std::vector<unsigned int> > PrescaleResult_Algo;
00055     typedef std::map< unsigned int, std::vector<unsigned int> > PrescaleResult_Tech;
00056 
00057     std::string int2str(unsigned int t,unsigned int width);
00058     unsigned int str2int(const std::string& s);
00059     void writeTrgData(coral::ISessionProxy* session,
00060                       unsigned int runnumber,
00061                       const std::string& source,
00062                       TriggerDeadCountResult::iterator deadtimesBeg,
00063                       TriggerDeadCountResult::iterator deadtimesEnd,
00064                       TRGScalers2DB::TriggerDeadFracResult& deadfracs,
00065                       TriggerNameResult_Algo& algonames, 
00066                       TriggerNameResult_Tech& technames,
00067                       TriggerCountResult_Algo& algocounts,
00068                       TriggerCountResult_Tech& techcounts,
00069                       PrescaleResult_Algo& prescalealgo,
00070                       PrescaleResult_Tech& prescaletech,
00071                       unsigned int commitintv);
00072     void writeTrgDataToSchema2(coral::ISessionProxy* session,
00073                                unsigned int runnumber,
00074                                const std::string& source,
00075                                TriggerDeadCountResult::iterator deadtimesBeg,
00076                                TriggerDeadCountResult::iterator deadtimesEnd,
00077                                TRGScalers2DB::TriggerDeadFracResult& deadfracs,       
00078                                TriggerNameResult_Algo& algonames,
00079                                TriggerNameResult_Tech& technames,
00080                                TriggerCountResult_Algo& algocounts,
00081                                TriggerCountResult_Tech& techcounts,
00082                                PrescaleResult_Algo& prescalealgo, 
00083                                PrescaleResult_Tech& prescaletech,
00084                                unsigned int commitintv);
00085   };//cl TRGScalers2DB
00086   //
00087   //implementation
00088   //
00089   //deadtime fraction query
00090   //
00091   //select fraction,lumi_section,count_bx from cms_gt_mon.v_scalers_tcs_deadtime where run_number=:runnum and scaler_name='DeadtimeBeamActive' 
00092   //
00093   //select count_bx,lumi_section,scaler_index from cms_gt_mon.v_scalers_fdl_algo where run_number=:runnum order by scaler_index;
00094   //
00095   //
00096   //select count_bx,lumi_section,scaler_index from cms_gt_mon.v_scalers_fdl_tech where run_number=:runnum order by scaler_index;
00097   //
00098   //select algo_index,alias from cms_gt.gt_run_algo_view where runnumber=:runnumber //order by algo_index;
00099   //
00100   //select techtrig_index,name from cms_gt.gt_run_tech_view where runnumber=:runnumber //order by techtrig_index;
00101   //
00102   //select distinct(prescale_index) from cms_gt_mon.lumi_sections where run_number=:runnumber;
00103   //
00104   //select lumi_section,prescale_index from cms_gt_mon.lumi_sections where run_number=:runnumber;
00105   //
00106   //select prescale_factor_algo_000,prescale_factor_algo_001..._127 from cms_gt.gt_run_presc_algo_view where runr=:runnumber and prescale_index=:prescale_index;
00107   // {prescale_index:[pres]}
00108   //
00109   //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;
00110   //    
00111   TRGScalers2DB::TRGScalers2DB(const std::string& dest):DataPipe(dest){}
00112   void TRGScalers2DB::retrieveData( unsigned int runnumber){
00113     std::string runnumberstr=int2str(runnumber,6);
00114     //query source GT database
00115     coral::ConnectionService* svc=new coral::ConnectionService;
00116     lumi::DBConfig dbconf(*svc);
00117     if(!m_authpath.empty()){
00118       dbconf.setAuthentication(m_authpath);
00119     }
00120     //std::cout<<"m_source "<<m_source<<std::endl;
00121     coral::ISessionProxy* trgsession=svc->connect(m_source, coral::ReadOnly);
00122     coral::ITypeConverter& tpc=trgsession->typeConverter();
00123     tpc.setCppTypeForSqlType("unsigned int","NUMBER(7)");
00124     tpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00125     tpc.setCppTypeForSqlType("unsigned long long","NUMBER(20)");
00126     std::string gtmonschema("CMS_GT_MON");
00127     std::string algoviewname("V_SCALERS_FDL_ALGO");
00128     std::string techviewname("V_SCALERS_FDL_TECH");
00129     std::string deadviewname("V_SCALERS_TCS_DEADTIME");
00130     std::string lstablename("LUMI_SECTIONS");
00131 
00132     std::string gtschema("CMS_GT");
00133     std::string runtechviewname("GT_RUN_TECH_VIEW");
00134     std::string runalgoviewname("GT_RUN_ALGO_VIEW");
00135     std::string runprescalgoviewname("GT_RUN_PRESC_ALGO_VIEW");
00136     std::string runpresctechviewname("GT_RUN_PRESC_TECH_VIEW");
00137 
00138     //data exchange format
00139     lumi::TRGScalers2DB::PrescaleResult_Algo algoprescale;
00140     lumi::TRGScalers2DB::PrescaleResult_Tech techprescale;
00141     lumi::TRGScalers2DB::BITCOUNT mybitcount_algo;
00142     mybitcount_algo.reserve(lumi::N_TRGALGOBIT);
00143     lumi::TRGScalers2DB::BITCOUNT mybitcount_tech; 
00144     mybitcount_tech.reserve(lumi::N_TRGTECHBIT);
00145     lumi::TRGScalers2DB::TriggerNameResult_Algo algonames;
00146     algonames.reserve(lumi::N_TRGALGOBIT);
00147     lumi::TRGScalers2DB::TriggerNameResult_Tech technames;
00148     technames.reserve(lumi::N_TRGTECHBIT);
00149     lumi::TRGScalers2DB::TriggerCountResult_Algo algocount;
00150     //algocount.reserve(400);
00151     lumi::TRGScalers2DB::TriggerCountResult_Tech techcount;
00152     //techcount.reserve(400);
00153     lumi::TRGScalers2DB::TriggerDeadFracResult deadfracresult;
00154     deadfracresult.reserve(500);
00155     lumi::TRGScalers2DB::TriggerDeadCountResult deadtimeresult;
00156     deadtimeresult.reserve(500);
00157     coral::ITransaction& transaction=trgsession->transaction();
00158     transaction.start(true);
00163     coral::ISchema& gtmonschemaHandle=trgsession->schema(gtmonschema);    
00164     if(!gtmonschemaHandle.existsView(algoviewname)){
00165       throw lumi::Exception(std::string("non-existing view ")+algoviewname,"retrieveData","TRGScalers2DB");
00166     }
00167     if(!gtmonschemaHandle.existsView(techviewname)){
00168       throw lumi::Exception(std::string("non-existing view ")+techviewname,"retrieveData","TRGScalers2DB");
00169     }
00170     if(!gtmonschemaHandle.existsView(deadviewname)){
00171       throw lumi::Exception(std::string("non-existing view ")+deadviewname,"retrieveData","TRGScalers2DB");
00172     }
00173     
00174     //
00175     //select count_bx,lumi_section,scaler_index from cms_gt_mon.v_scalers_fdl_algo where run_number=:runnum order by lumi_section,scaler_index;
00176     //note: scaler_index  0-127
00177     //note: lumi_section count from 1 
00178     //
00179     coral::AttributeList bindVariableList;
00180     bindVariableList.extend("runnumber",typeid(unsigned int));
00181     bindVariableList["runnumber"].data<unsigned int>()=runnumber;
00182 
00183     coral::IQuery* Queryalgoview=gtmonschemaHandle.newQuery();
00184     Queryalgoview->addToTableList(algoviewname);
00185     coral::AttributeList qalgoOutput;
00186     qalgoOutput.extend("count_bx",typeid(unsigned int));
00187     qalgoOutput.extend("lumi_section",typeid(unsigned int));
00188     qalgoOutput.extend("scaler_index",typeid(unsigned int));
00189     Queryalgoview->addToOutputList("count_bx");
00190     Queryalgoview->addToOutputList("lumi_section");
00191     Queryalgoview->addToOutputList("scaler_index");
00192     Queryalgoview->setCondition("RUN_NUMBER=:runnumber",bindVariableList);
00193     Queryalgoview->addToOrderList("lumi_section");
00194     Queryalgoview->addToOrderList("scaler_index");
00195     Queryalgoview->defineOutput(qalgoOutput);
00196     coral::ICursor& c=Queryalgoview->execute();
00197     
00198     unsigned int s=0;
00199     while( c.next() ){
00200       const coral::AttributeList& row = c.currentRow();     
00201       //row.toOutputStream( std::cout ) << std::endl;
00202       unsigned int lsnr=row["lumi_section"].data<unsigned int>();
00203       unsigned int count=row["count_bx"].data<unsigned int>();
00204       unsigned int algobit=row["scaler_index"].data<unsigned int>();
00205       mybitcount_algo.push_back(count);
00206       if(algobit==(lumi::N_TRGALGOBIT-1)){
00207         ++s;
00208         while(s!=lsnr){
00209           std::cout<<"ALGO COUNT alert: found hole in LS range "<<s<<std::endl;
00210           std::cout<<"    fill all algocount 0 for LS "<<s<<std::endl;
00211           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT,0);
00212           algocount.push_back(tmpzero);
00213           ++s;
00214         }
00215         algocount.push_back(mybitcount_algo);
00216         mybitcount_algo.clear();
00217       }
00218     }
00219     if(s==0){
00220       c.close();
00221       delete Queryalgoview;
00222       transaction.commit();
00223       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for algocounts"),"retrieveData","TRGScalers2DB");
00224     }
00225     delete Queryalgoview;
00226     //
00227     //select count_bx,lumi_section,scaler_index from cms_gt_mon.v_scalers_fdl_tech where run_number=:runnum order by lumi_section,scaler_index;
00228     //
00229     //note: techobit 0-63
00230     //note: lsnr count from 1 
00231     //
00232     coral::IQuery* Querytechview=gtmonschemaHandle.newQuery();
00233     Querytechview->addToTableList(techviewname);
00234     coral::AttributeList qtechOutput;
00235     qtechOutput.extend("count_bx",typeid(unsigned int));
00236     qtechOutput.extend("lumi_section",typeid(unsigned int));
00237     qtechOutput.extend("scaler_index",typeid(unsigned int));
00238     Querytechview->addToOutputList("count_bx");
00239     Querytechview->addToOutputList("lumi_section");
00240     Querytechview->addToOutputList("scaler_index");
00241     Querytechview->setCondition("RUN_NUMBER=:runnumber",bindVariableList);
00242     Querytechview->addToOrderList("lumi_section");
00243     Querytechview->addToOrderList("scaler_index");
00244     Querytechview->defineOutput(qtechOutput);
00245     coral::ICursor& techcursor=Querytechview->execute();    
00246     s=0;
00247     while( techcursor.next() ){
00248       const coral::AttributeList& row = techcursor.currentRow();     
00249       //row.toOutputStream( std::cout #include <stdexcept>) << std::endl;
00250       unsigned int lsnr=row["lumi_section"].data<unsigned int>();
00251       unsigned int count=row["count_bx"].data<unsigned int>();
00252       unsigned int techbit=row["scaler_index"].data<unsigned int>();
00253       mybitcount_tech.push_back(count);
00254       if(techbit==(lumi::N_TRGTECHBIT-1)){
00255         ++s;
00256         while(s!=lsnr){
00257           std::cout<<"TECH COUNT alert: found hole in LS range "<<s<<std::endl;
00258           std::cout<<"     fill all techcount with 0 for LS "<<s<<std::endl;
00259           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT,0);
00260           techcount.push_back(tmpzero);
00261           ++s;
00262         }
00263         techcount.push_back(mybitcount_tech);
00264         mybitcount_tech.clear();
00265       }
00266     }
00267     if(s==0){
00268       techcursor.close();
00269       delete Querytechview;
00270       transaction.commit();
00271       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for techcounts"),"retrieveData","TRGScalers2DB");
00272     }
00273     delete Querytechview;
00274 
00275     //
00276     //select fraction,lumi_section,count_bx from cms_gt_mon.v_scalers_tcs_deadtime where run_number=:runnum and scaler_name='DeadtimeBeamActive' order by lumi_section' 
00277     //
00278     //note: lsnr count from 1 
00279     //
00280     coral::IQuery* Querydeadview=gtmonschemaHandle.newQuery();
00281     Querydeadview->addToTableList(deadviewname);
00282     coral::AttributeList qdeadOutput;
00283     qdeadOutput.extend("fraction",typeid(float));
00284     qdeadOutput.extend("lumi_section",typeid(unsigned int));
00285     qdeadOutput.extend("count_bx",typeid(unsigned int));
00286     Querydeadview->addToOutputList("fraction");
00287     Querydeadview->addToOutputList("lumi_section");
00288     Querydeadview->addToOutputList("count_bx");
00289     coral::AttributeList bindVariablesDead;
00290     bindVariablesDead.extend("runnumber",typeid(int));
00291     bindVariablesDead.extend("scalername",typeid(std::string));
00292     bindVariablesDead["runnumber"].data<int>()=runnumber;
00293     bindVariablesDead["scalername"].data<std::string>()=std::string("DeadtimeBeamActive");
00294     Querydeadview->setCondition("RUN_NUMBER=:runnumber AND SCALER_NAME=:scalername",bindVariablesDead);
00295     Querydeadview->addToOrderList("lumi_section");
00296     Querydeadview->defineOutput(qdeadOutput);
00297     coral::ICursor& deadcursor=Querydeadview->execute();
00298     s=0;
00299     while( deadcursor.next() ){
00300       const coral::AttributeList& row = deadcursor.currentRow();     
00301       //row.toOutputStream( std::cout ) << std::endl;
00302       ++s;
00303       unsigned int lsnr=row["lumi_section"].data<unsigned int>();
00304       float dfrac=1.0;
00305       unsigned int count=0;
00306       while(s!=lsnr){
00307         std::cout<<"DEADTIME alert: found hole in LS range"<<s<<std::endl;        
00308         std::cout<<"         fill deadtimefraction 0%, deadtimebeamactive 0 for LS "<<s<<std::endl;
00309         deadfracresult.push_back(dfrac);
00310         deadtimeresult.push_back(count);
00311         ++s;
00312       }
00313       if(!row["fraction"].isNull()){
00314         dfrac=row["fraction"].data<float>(); //deadfraction is null from trigger means "undefined", but insert 1.0...
00315       }else{
00316         std::cout<<"DEADTIME fraction alert: undefined fraction , assume 100% , LS "<<lsnr<<std::endl;
00317       }
00318       if(dfrac>1.0){
00319         std::cout<<"DEADTIME fraction alert: overflow dead fraction , force to 100% , LS "<<lsnr<<std::endl;
00320         dfrac=1.0;
00321       }
00322       deadfracresult.push_back(dfrac);
00323       count=row["count_bx"].data<unsigned int>();
00324       deadtimeresult.push_back(count);
00325     }
00326     if(s==0){
00327       deadcursor.close();
00328       delete Querydeadview;
00329       transaction.commit();
00330       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for deadcounts"),"retrieveData","TRGScalers2DB");
00331       return;
00332     }
00333     delete Querydeadview;
00334     
00335     //
00336     //select distinct(prescale_index) from cms_gt_mon.lumi_sections where run_number=:runnumber;
00337     //
00338     std::vector< int > prescidx;
00339     coral::IQuery* allpsidxQuery=gtmonschemaHandle.newQuery();
00340     allpsidxQuery->addToTableList(lstablename);
00341     coral::AttributeList allpsidxOutput;
00342     allpsidxOutput.extend("psidx",typeid(int));
00343     allpsidxQuery->addToOutputList("distinct PRESCALE_INDEX","psidx");
00344     coral::AttributeList bindVariablesAllpsidx;
00345     bindVariablesAllpsidx.extend("runnumber",typeid(int));
00346     bindVariablesAllpsidx["runnumber"].data<int>()=runnumber;
00347     allpsidxQuery->setCondition("RUN_NUMBER =:runnumber",bindVariablesAllpsidx);
00348     allpsidxQuery->defineOutput(allpsidxOutput);
00349     coral::ICursor& allpsidxCursor=allpsidxQuery->execute();
00350     while( allpsidxCursor.next() ){
00351       const coral::AttributeList& row = allpsidxCursor.currentRow();     
00352       int psidx=row["psidx"].data<int>();
00353       prescidx.push_back(psidx);
00354     }
00355     delete allpsidxQuery;
00356     std::map< int, std::vector<unsigned int> > algoprescMap;
00357     std::map< int, std::vector<unsigned int> > techprescMap;
00358     std::vector< int >::iterator prescidxIt;
00359     std::vector< int >::iterator prescidxItBeg=prescidx.begin();
00360     std::vector< int >::iterator prescidxItEnd=prescidx.end();
00361     for(prescidxIt=prescidxItBeg;prescidxIt!=prescidxItEnd;++prescidxIt){
00362       std::vector<unsigned int> algopres; algopres.reserve(lumi::N_TRGALGOBIT);
00363       std::vector<unsigned int> techpres; techpres.reserve(lumi::N_TRGTECHBIT);
00364       algoprescMap.insert(std::make_pair(*prescidxIt,algopres));
00365       techprescMap.insert(std::make_pair(*prescidxIt,techpres));
00366     }
00367     //
00368     //select lumi_section,prescale_index from cms_gt_mon.lumi_sections where run_number=:runnumber
00369     // {ls:prescale_index}
00370     //
00371     std::map< unsigned int, int > lsprescmap;
00372     coral::IQuery* lstoprescQuery=gtmonschemaHandle.newQuery();
00373     lstoprescQuery->addToTableList(lstablename);
00374     coral::AttributeList lstoprescOutput;
00375     lstoprescOutput.extend("lumisection",typeid(unsigned int));
00376     lstoprescOutput.extend("psidx",typeid(int));
00377     lstoprescQuery->addToOutputList("LUMI_SECTION","lumisection");
00378     lstoprescQuery->addToOutputList("PRESCALE_INDEX","psidx");
00379     coral::AttributeList bindVariablesLstopresc;
00380     bindVariablesLstopresc.extend("runnumber",typeid(int));
00381     bindVariablesLstopresc["runnumber"].data<int>()=runnumber;
00382     lstoprescQuery->setCondition("RUN_NUMBER =:runnumber",bindVariablesLstopresc);
00383     lstoprescQuery->defineOutput(lstoprescOutput);
00384     unsigned int lsprescount=0;
00385     unsigned int lastpresc=0;
00386     coral::ICursor& lstoprescCursor=lstoprescQuery->execute();
00387     while( lstoprescCursor.next() ){
00388       ++lsprescount;
00389       const coral::AttributeList& row = lstoprescCursor.currentRow();     
00390       unsigned int lumisection=row["lumisection"].data< unsigned int>();
00391       while(lsprescount!=lumisection){
00392           std::cout<<"PRESCALE_INDEX COUNT alert: found hole in LS range "<<lsprescount<<std::endl;
00393           std::cout<<"     fill this prescale from last availabl prescale "<<lastpresc<<std::endl;
00394           unsigned int guesspsidx=lastpresc;
00395           lsprescmap.insert(std::make_pair(lsprescount,guesspsidx));
00396           ++lsprescount;
00397       }
00398       int psidx=row["psidx"].data< int>();
00399       lsprescmap.insert(std::make_pair(lumisection,psidx));
00400       lastpresc=psidx;
00401     }
00402     if(lsprescount==0){
00403       lstoprescCursor.close();
00404       delete lstoprescQuery ;
00405       transaction.commit();
00406       throw lumi::Exception(std::string("requested run ")+runnumberstr+std::string(" doesn't exist for prescale_index"),"retrieveData","TRG2DB");
00407     }
00408     delete lstoprescQuery ;
00409 
00410     for(prescidxIt=prescidxItBeg;prescidxIt!=prescidxItEnd;++prescidxIt){
00411       std::vector<unsigned int> algopres; algopres.reserve(lumi::N_TRGALGOBIT);
00412       std::vector<unsigned int> techpres; techpres.reserve(lumi::N_TRGTECHBIT);
00413       algoprescMap.insert(std::make_pair(*prescidxIt,algopres));
00414       techprescMap.insert(std::make_pair(*prescidxIt,techpres));
00415     }
00416     //prefill lsprescmap
00417     //transaction.commit();
00422     coral::ISchema& gtschemaHandle=trgsession->schema(gtschema);
00423     if(!gtschemaHandle.existsView(runtechviewname)){
00424       throw lumi::Exception(std::string("non-existing view ")+runtechviewname,"str2int","TRG2DB");
00425     }
00426     if(!gtschemaHandle.existsView(runalgoviewname)){
00427       throw lumi::Exception(std::string("non-existing view ")+runalgoviewname,"str2int","TRG2DB");
00428     }
00429     if(!gtschemaHandle.existsView(runprescalgoviewname)){
00430       throw lumi::Exception(std::string("non-existing view ")+runprescalgoviewname,"str2int","TRG2DB");
00431     }
00432     if(!gtschemaHandle.existsView(runpresctechviewname)){
00433       throw lumi::Exception(std::string("non-existing view ")+runpresctechviewname,"str2int","TRG2DB");
00434     }
00435     //
00436     //select algo_index,alias from cms_gt.gt_run_algo_view where runnumber=:runnumber //order by algo_index;
00437     //
00438     std::map<unsigned int,std::string> triggernamemap;
00439     coral::IQuery* QueryName=gtschemaHandle.newQuery();
00440     QueryName->addToTableList(runalgoviewname);
00441     coral::AttributeList qAlgoNameOutput;
00442     qAlgoNameOutput.extend("algo_index",typeid(unsigned int));
00443     qAlgoNameOutput.extend("alias",typeid(std::string));
00444     QueryName->addToOutputList("algo_index");
00445     QueryName->addToOutputList("alias");
00446     QueryName->setCondition("runnumber =:runnumber",bindVariableList);
00447     //QueryName->addToOrderList("algo_index");
00448     QueryName->defineOutput(qAlgoNameOutput);
00449     coral::ICursor& algonamecursor=QueryName->execute();
00450     while( algonamecursor.next() ){
00451       const coral::AttributeList& row = algonamecursor.currentRow();     
00452       //row.toOutputStream( std::cout ) << std::endl;
00453       unsigned int algo_index=row["algo_index"].data<unsigned int>();
00454       std::string algo_name=row["alias"].data<std::string>();
00455       triggernamemap.insert(std::make_pair(algo_index,algo_name));
00456     }
00457     delete QueryName;
00458     
00459     //
00460     //select techtrig_index,name from cms_gt.gt_run_tech_view where runnumber=:runnumber //order by techtrig_index;
00461     //
00462     std::map<unsigned int,std::string> techtriggernamemap;
00463     coral::IQuery* QueryTechName=gtschemaHandle.newQuery();
00464     QueryTechName->addToTableList(runtechviewname);
00465     coral::AttributeList qTechNameOutput;
00466     qTechNameOutput.extend("techtrig_index",typeid(unsigned int));
00467     qTechNameOutput.extend("name",typeid(std::string));
00468     QueryTechName->addToOutputList("techtrig_index");
00469     QueryTechName->addToOutputList("name");
00470     QueryTechName->setCondition("runnumber =:runnumber",bindVariableList);
00471     //QueryTechName->addToOrderList("techtrig_index");
00472     QueryTechName->defineOutput(qTechNameOutput);
00473     coral::ICursor& technamecursor=QueryTechName->execute();
00474     while( technamecursor.next() ){
00475       const coral::AttributeList& row = technamecursor.currentRow();     
00476       //row.toOutputStream( std::cout ) << std::endl;
00477       unsigned int tech_index=row["techtrig_index"].data<unsigned int>();
00478       std::string tech_name=row["name"].data<std::string>();
00479       techtriggernamemap.insert(std::make_pair(tech_index,tech_name));
00480     }
00481     delete QueryTechName;
00482     //
00483     //loop over all prescale_index
00484     //
00485     //select prescale_factor_algo_000,prescale_factor_algo_001..._127 from cms_gt.gt_run_presc_algo_view where runr=:runnumber and prescale_index=:prescale_index;
00486     // {prescale_index:[pres]}
00487     //
00488     std::vector< int >::iterator presIt;
00489     std::vector< int >::iterator presItBeg=prescidx.begin();
00490     std::vector< int >::iterator presItEnd=prescidx.end();
00491     for( presIt=presItBeg; presIt!=presItEnd; ++presIt ){
00492       coral::IQuery* QueryAlgoPresc=gtschemaHandle.newQuery();
00493       QueryAlgoPresc->addToTableList(runprescalgoviewname);
00494       coral::AttributeList qAlgoPrescOutput;
00495       std::string algoprescBase("PRESCALE_FACTOR_ALGO_");
00496       for(unsigned int bitidx=0;bitidx<lumi::N_TRGALGOBIT;++bitidx){
00497         std::string algopresc=algoprescBase+int2str(bitidx,3);
00498         qAlgoPrescOutput.extend(algopresc,typeid(unsigned int));
00499       }
00500       for(unsigned int bitidx=0;bitidx<lumi::N_TRGALGOBIT;++bitidx){
00501         std::string algopresc=algoprescBase+int2str(bitidx,3);
00502         QueryAlgoPresc->addToOutputList(algopresc);
00503       }
00504       coral::AttributeList PrescbindVariable;
00505       PrescbindVariable.extend("runnumber",typeid(int));
00506       PrescbindVariable.extend("prescaleindex",typeid(int));
00507       PrescbindVariable["runnumber"].data<int>()=runnumber;
00508       PrescbindVariable["prescaleindex"].data<int>()=*presIt;
00509       QueryAlgoPresc->setCondition("runnr =:runnumber AND prescale_index =:prescaleindex",PrescbindVariable);
00510       QueryAlgoPresc->defineOutput(qAlgoPrescOutput);
00511       coral::ICursor& algopresccursor=QueryAlgoPresc->execute();
00512       while( algopresccursor.next() ){
00513         const coral::AttributeList& row = algopresccursor.currentRow();     
00514         for(unsigned int bitidx=0;bitidx<lumi::N_TRGALGOBIT;++bitidx){
00515           std::string algopresc=algoprescBase+int2str(bitidx,3);
00516           algoprescMap[*presIt].push_back(row[algopresc].data<unsigned int>());
00517         }
00518       }
00519       delete QueryAlgoPresc;
00520     }
00521     //
00522     //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;
00523     //    
00524     for( presIt=presItBeg; presIt!=presItEnd; ++presIt ){
00525       coral::IQuery* QueryTechPresc=gtschemaHandle.newQuery();
00526       QueryTechPresc->addToTableList(runpresctechviewname);
00527       coral::AttributeList qTechPrescOutput;
00528       std::string techprescBase("PRESCALE_FACTOR_TT_");
00529       for(unsigned int bitidx=0;bitidx<lumi::N_TRGTECHBIT;++bitidx){
00530         std::string techpresc=techprescBase+this->int2str(bitidx,3);
00531         qTechPrescOutput.extend(techpresc,typeid(unsigned int));
00532       }
00533       for(unsigned int bitidx=0;bitidx<lumi::N_TRGTECHBIT;++bitidx){
00534         std::string techpresc=techprescBase+int2str(bitidx,3);
00535         QueryTechPresc->addToOutputList(techpresc);
00536       }
00537       coral::AttributeList TechPrescbindVariable;
00538       TechPrescbindVariable.extend("runnumber",typeid(int));
00539       TechPrescbindVariable.extend("prescaleindex",typeid(int));
00540       TechPrescbindVariable["runnumber"].data<int>()=runnumber;
00541       TechPrescbindVariable["prescaleindex"].data<int>()=*presIt;
00542       QueryTechPresc->setCondition("runnr =:runnumber AND prescale_index =:prescaleindex",TechPrescbindVariable);
00543       QueryTechPresc->defineOutput(qTechPrescOutput);
00544       coral::ICursor& techpresccursor=QueryTechPresc->execute();
00545       while( techpresccursor.next() ){
00546         const coral::AttributeList& row = techpresccursor.currentRow();     
00547         //row.toOutputStream( std::cout ) << std::endl;
00548         for(unsigned int bitidx=0;bitidx<lumi::N_TRGTECHBIT;++bitidx){
00549           std::string techpresc=techprescBase+int2str(bitidx,3);
00550           techprescMap[*presIt].push_back(row[techpresc].data<unsigned int>());
00551         }
00552       }
00553       delete QueryTechPresc;
00554     }
00555     transaction.commit();
00556     delete trgsession;
00557 
00558     std::map< unsigned int, int >::iterator lsprescmapIt;
00559     std::map< unsigned int, int >::iterator lsprescmapItBeg=lsprescmap.begin();
00560     std::map< unsigned int, int >::iterator lsprescmapItEnd=lsprescmap.end();
00561     for( lsprescmapIt=lsprescmapItBeg; lsprescmapIt!=lsprescmapItEnd; ++lsprescmapIt ){
00562       unsigned int ls=lsprescmapIt->first;
00563       int preidx=lsprescmapIt->second;
00564       algoprescale.insert(std::make_pair(ls,algoprescMap[preidx]));
00565       techprescale.insert(std::make_pair(ls,techprescMap[preidx]));
00566     }
00567     algoprescMap.clear();
00568     techprescMap.clear();
00569     //
00570     //reprocess Algo name result filling unallocated trigger bit with string "False"
00571     //
00572     for(size_t algoidx=0;algoidx<lumi::N_TRGALGOBIT;++algoidx){
00573       std::map<unsigned int,std::string>::iterator pos=triggernamemap.find(algoidx);
00574       if(pos!=triggernamemap.end()){
00575         algonames.push_back(pos->second);
00576       }else{
00577         algonames.push_back("False");
00578       }
00579     }
00580     //
00581     //reprocess Tech name result filling unallocated trigger bit with string "False"  
00582     //
00583     std::stringstream ss;
00584     for(size_t techidx=0;techidx<lumi::N_TRGTECHBIT;++techidx){
00585       ss<<techidx;
00586       technames.push_back(ss.str());
00587       ss.str(""); //clear the string buffer after usage
00588     }
00589     //
00590     //cross check result size
00591     //
00592     if(algonames.size()!=lumi::N_TRGALGOBIT || technames.size()!=lumi::N_TRGTECHBIT){
00593       throw lumi::Exception("wrong number of bits","retrieveData","TRG2DB");
00594     }
00595     //if(algoprescale.size()!=lumi::N_TRGALGOBIT || techprescale.size()!=lumi::N_TRGTECHBIT){
00596     //  throw lumi::Exception("wrong number of prescale","retrieveData","TRG2DB");
00597     //}
00598     if(deadtimeresult.size()!=deadfracresult.size()|| deadtimeresult.size()!=algocount.size() || deadtimeresult.size()!=techcount.size() || deadtimeresult.size()!=algoprescale.size() || deadtimeresult.size()!=techprescale.size() ){
00599       //throw lumi::Exception("inconsistent number of LS","retrieveData","TRG2DB");
00600       std::cout<<"[WARNING] inconsistent number of LS of deadtimecounter,deadfrac,algo,tech,prescalealgo,prescaletech "<<deadtimeresult.size()<<" "<<deadfracresult.size()<<" "<<algocount.size()<<" "<<techcount.size()<<" "<<algoprescale.size()<<" "<<techprescale.size()<<std::endl;
00601       TRGScalers2DB::TriggerDeadCountResult::iterator dIt;
00602       TRGScalers2DB::TriggerDeadCountResult::iterator dBeg=deadtimeresult.begin();
00603       TRGScalers2DB::TriggerDeadCountResult::iterator dEnd=deadtimeresult.end();
00604       unsigned int dcnt=0;
00605       for(dIt=dBeg;dIt!=dEnd;++dIt){
00606         try{
00607           deadfracresult.at(dcnt);
00608         }catch(std::out_of_range& er){
00609           std::cout<<"[WARNING] filling FAKE deadfrac=0.0 at LS "<<dcnt<<std::endl;
00610           deadfracresult[dcnt]=0.0;
00611         }
00612         try{
00613           algocount.at(dcnt);
00614         }catch(std::out_of_range& er){
00615           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT,0);
00616           std::cout<<"[WARNING] filling FAKE algocount at LS "<<dcnt<<std::endl;
00617           algocount[dcnt]=tmpzero;
00618         }
00619         try{
00620           techcount.at(dcnt);
00621         }catch(std::out_of_range& er){
00622           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT,0);
00623           std::cout<<"[WARNING] filling FAKE techcount at LS "<<dcnt<<std::endl;
00624           techcount[dcnt]=tmpzero;
00625         }      
00626         if(algoprescale.find(dcnt+1)==algoprescale.end()){
00627           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT,1);
00628           std::cout<<"[WARNING] filling FAKE 1 algoprescale at LS "<<dcnt+1<<std::endl;
00629           algoprescale[dcnt+1]=tmpzero;
00630         }
00631         if(techprescale.find(dcnt+1)==techprescale.end()){
00632           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT,1);
00633           std::cout<<"[WARNING] filling FAKE 1 techprescale at LS "<<dcnt+1<<std::endl;
00634           techprescale[dcnt+1]=tmpzero;
00635         }
00636         ++dcnt; 
00637       }
00638     }
00639  //    
00640     //write data into lumi db
00641     //
00642     
00643     coral::ISessionProxy* lumisession=svc->connect(m_dest,coral::Update);
00644     coral::ITypeConverter& lumitpc=lumisession->typeConverter();
00645     lumitpc.setCppTypeForSqlType("unsigned int","NUMBER(7)");
00646     lumitpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00647     lumitpc.setCppTypeForSqlType("unsigned long long","NUMBER(20)");
00648     try{
00649       if(m_mode=="loadoldschema"){ 
00650          std::cout<<"writing trg data to old trg table "<<std::endl;
00651          writeTrgData(lumisession,runnumber,m_source,deadtimeresult.begin(),deadtimeresult.end(),deadfracresult,algonames,technames,algocount,techcount,algoprescale,techprescale,COMMITLSINTERVAL);
00652          std::cout<<"done"<<std::endl;
00653       }
00654       std::cout<<"writing trg data to new lstrg table "<<std::endl;
00655       writeTrgDataToSchema2(lumisession,runnumber,m_source,deadtimeresult.begin(),deadtimeresult.end(),deadfracresult,algonames,technames,algocount,techcount,algoprescale,techprescale,COMMITLSTRGINTERVAL);
00656       std::cout<<"done"<<std::endl;
00657       delete lumisession;
00658       delete svc;
00659     }catch( const coral::Exception& er){
00660       std::cout<<"database error "<<er.what()<<std::endl;
00661       lumisession->transaction().rollback();
00662       delete lumisession;
00663       delete svc;
00664       throw er;
00665     }
00666   }
00667   void  
00668   TRGScalers2DB::writeTrgData(coral::ISessionProxy* lumisession,
00669                        unsigned int runnumber,
00670                        const std::string& source,
00671                        TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesBeg,TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesEnd,
00672                        TRGScalers2DB::TriggerDeadFracResult& deadfracs,
00673                        TRGScalers2DB::TriggerNameResult_Algo& algonames,
00674                        TRGScalers2DB::TriggerNameResult_Tech& technames,
00675                        TRGScalers2DB::TriggerCountResult_Algo& algocounts,
00676                        TRGScalers2DB::TriggerCountResult_Tech& techcounts,
00677                        TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
00678                        TRGScalers2DB::PrescaleResult_Tech& prescaletech,
00679                        unsigned int commitintv){    
00680     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
00681     //unsigned int totalcmsls=deadtimes.size();
00682     unsigned int totalcmsls=std::distance(deadtimesBeg,deadtimesEnd);
00683     std::cout<<"inserting totalcmsls "<<totalcmsls<<std::endl;
00684     std::map< unsigned long long, std::vector<unsigned long long> > idallocationtable;
00685     std::cout<<"\t allocating total ids "<<totalcmsls*lumi::N_TRGBIT<<std::endl; 
00686     lumisession->transaction().start(false);
00687     lumi::idDealer idg(lumisession->nominalSchema());
00688     unsigned long long trgID = idg.generateNextIDForTable(LumiNames::trgTableName(),totalcmsls*lumi::N_TRGBIT)-totalcmsls*lumi::N_TRGBIT;
00689     //lumisession->transaction().commit();
00690     unsigned int trglscount=0;
00691     for(deadIt=deadtimesBeg;deadIt!=deadtimesEnd;++deadIt,++trglscount){
00692       std::vector<unsigned long long> bitvec;
00693       bitvec.reserve(lumi::N_TRGBIT);
00694       const BITCOUNT& algoinbits=algocounts[trglscount];
00695       const BITCOUNT& techinbits=techcounts[trglscount];
00696       BITCOUNT::const_iterator algoBitIt;
00697       BITCOUNT::const_iterator algoBitBeg=algoinbits.begin();
00698       BITCOUNT::const_iterator algoBitEnd=algoinbits.end();     
00699       for(algoBitIt=algoBitBeg;algoBitIt!=algoBitEnd;++algoBitIt,++trgID){
00700         bitvec.push_back(trgID);
00701       }
00702       BITCOUNT::const_iterator techBitIt;
00703       BITCOUNT::const_iterator techBitBeg=techinbits.begin();
00704       BITCOUNT::const_iterator techBitEnd=techinbits.end();
00705       for(techBitIt=techBitBeg;techBitIt!=techBitEnd;++techBitIt,++trgID){
00706         bitvec.push_back(trgID);
00707       }
00708       idallocationtable.insert(std::make_pair(trglscount,bitvec));
00709     }
00710     std::cout<<"\t all ids allocated"<<std::endl; 
00711     coral::AttributeList trgData;
00712     trgData.extend<unsigned long long>("TRG_ID");
00713     trgData.extend<unsigned int>("RUNNUM");
00714     trgData.extend<unsigned int>("CMSLSNUM");
00715     trgData.extend<unsigned int>("BITNUM");
00716     trgData.extend<std::string>("BITNAME");
00717     trgData.extend<unsigned int>("TRGCOUNT");
00718     trgData.extend<unsigned long long>("DEADTIME");
00719     trgData.extend<float>("DEADFRAC");
00720     trgData.extend<unsigned int>("PRESCALE");
00721     
00722     unsigned long long& trg_id=trgData["TRG_ID"].data<unsigned long long>();
00723     unsigned int& trgrunnum=trgData["RUNNUM"].data<unsigned int>();
00724     unsigned int& cmslsnum=trgData["CMSLSNUM"].data<unsigned int>();
00725     unsigned int& bitnum=trgData["BITNUM"].data<unsigned int>();
00726     std::string& bitname=trgData["BITNAME"].data<std::string>();
00727     unsigned int& count=trgData["TRGCOUNT"].data<unsigned int>();
00728     unsigned long long& deadtime=trgData["DEADTIME"].data<unsigned long long>();
00729     float& deadfrac=trgData["DEADFRAC"].data<float>();
00730     unsigned int& prescale=trgData["PRESCALE"].data<unsigned int>();
00731                       
00732     trglscount=0;    
00733     coral::IBulkOperation* trgInserter=0; 
00734     unsigned int comittedls=0;
00735     for(deadIt=deadtimesBeg;deadIt!=deadtimesEnd;++deadIt,++trglscount ){
00736       unsigned int cmslscount=trglscount+1;
00737       float dfra=deadfracs[trglscount];
00738       const BITCOUNT& algoinbits=algocounts[trglscount];
00739       const BITCOUNT& techinbits=techcounts[trglscount];
00740       unsigned int trgbitcount=0;
00741       BITCOUNT::const_iterator algoBitIt;
00742       BITCOUNT::const_iterator algoBitBeg=algoinbits.begin();
00743       BITCOUNT::const_iterator algoBitEnd=algoinbits.end();
00744       if(!lumisession->transaction().isActive()){ 
00745         lumisession->transaction().start(false);
00746         coral::ITable& trgtable=lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
00747         trgInserter=trgtable.dataEditor().bulkInsert(trgData,2048);
00748       }else{
00749         if(deadIt==deadtimesBeg){
00750           coral::ITable& trgtable=lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
00751           trgInserter=trgtable.dataEditor().bulkInsert(trgData,2048);
00752         }
00753       }
00754       for(algoBitIt=algoBitBeg;algoBitIt!=algoBitEnd;++algoBitIt,++trgbitcount){
00755         trg_id = idallocationtable[trglscount].at(trgbitcount);
00756         deadtime=*deadIt;
00757         deadfrac=dfra;
00758         trgrunnum = runnumber;
00759         cmslsnum = cmslscount;
00760         bitnum=trgbitcount;
00761         bitname=algonames[trgbitcount];
00762         count=*algoBitIt;
00763         prescale=prescalealgo[cmslscount].at(trgbitcount);
00764         //std::cout<<"cmslsnum "<<cmslsnum<<" bitnum "<<bitnum<<" bitname "<<bitname<<" prescale "<< prescale<<" count "<<count<<std::endl;
00765         trgInserter->processNextIteration();    
00766       }
00767       BITCOUNT::const_iterator techBitIt;
00768       BITCOUNT::const_iterator techBitBeg=techinbits.begin();
00769       BITCOUNT::const_iterator techBitEnd=techinbits.end();
00770       for(techBitIt=techBitBeg;techBitIt!=techBitEnd;++techBitIt,++trgbitcount){
00771         trg_id = idallocationtable[trglscount].at(trgbitcount);
00772         deadtime=*deadIt;
00773         deadfrac=dfra;
00774         trgrunnum = runnumber;
00775         cmslsnum = cmslscount;
00776         bitnum=trgbitcount;
00777         bitname=technames[trgbitcount-lumi::N_TRGALGOBIT];
00778         count=*techBitIt;
00779         prescale=prescaletech[cmslsnum][trgbitcount-lumi::N_TRGALGOBIT];
00780         trgInserter->processNextIteration();    
00781       }
00782       trgInserter->flush();
00783       ++comittedls;
00784       if(comittedls==commitintv){
00785         std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00786         delete trgInserter; trgInserter=0;
00787         lumisession->transaction().commit();
00788         comittedls=0;
00789         std::cout<<"\t committed "<<std::endl; 
00790       }else if( trglscount==( totalcmsls-1) ){
00791         std::cout<<"\t committing at the end"<<std::endl; 
00792         delete trgInserter; trgInserter=0;
00793         lumisession->transaction().commit();
00794         std::cout<<"\t done"<<std::endl; 
00795       }
00796     }
00797   }
00798   void  
00799   TRGScalers2DB::writeTrgDataToSchema2(coral::ISessionProxy* lumisession,
00800                                 unsigned int irunnumber,
00801                                 const std::string& source,
00802                                 TriggerDeadCountResult::iterator deadtimesBeg,
00803                                 TriggerDeadCountResult::iterator deadtimesEnd,
00804                                 TRGScalers2DB::TriggerDeadFracResult& deadfracs,       
00805                                 TRGScalers2DB::TriggerNameResult_Algo& algonames, 
00806                                 TRGScalers2DB::TriggerNameResult_Tech& technames,
00807                                 TRGScalers2DB::TriggerCountResult_Algo& algocounts,
00808                                 TRGScalers2DB::TriggerCountResult_Tech& techcounts,
00809                                 TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
00810                                 TRGScalers2DB::PrescaleResult_Tech& prescaletech,
00811                                 unsigned int commitintv){
00812     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
00813     unsigned int totalcmsls=std::distance(deadtimesBeg,deadtimesEnd);
00814     std::cout<<"inserting totalcmsls "<<totalcmsls<<std::endl;
00815     coral::AttributeList lstrgData;
00816     lstrgData.extend<unsigned long long>("DATA_ID");
00817     lstrgData.extend<unsigned int>("RUNNUM");
00818     lstrgData.extend<unsigned int>("CMSLSNUM");
00819     lstrgData.extend<unsigned long long>("DEADTIMECOUNT");
00820     lstrgData.extend<unsigned int>("BITZEROCOUNT");
00821     lstrgData.extend<unsigned int>("BITZEROPRESCALE");
00822     lstrgData.extend<float>("DEADFRAC");
00823     lstrgData.extend<coral::Blob>("PRESCALEBLOB");
00824     lstrgData.extend<coral::Blob>("TRGCOUNTBLOB");
00825 
00826     unsigned long long& data_id=lstrgData["DATA_ID"].data<unsigned long long>();
00827     unsigned int& trgrunnum=lstrgData["RUNNUM"].data<unsigned int>();
00828     unsigned int& cmslsnum=lstrgData["CMSLSNUM"].data<unsigned int>();
00829     unsigned long long& deadtime=lstrgData["DEADTIMECOUNT"].data<unsigned long long>();
00830     unsigned int& bitzerocount=lstrgData["BITZEROCOUNT"].data<unsigned int>();
00831     unsigned int& bitzeroprescale=lstrgData["BITZEROPRESCALE"].data<unsigned int>();
00832     float& deadfrac=lstrgData["DEADFRAC"].data<float>();
00833     coral::Blob& prescaleblob=lstrgData["PRESCALEBLOB"].data<coral::Blob>();
00834     coral::Blob& trgcountblob=lstrgData["TRGCOUNTBLOB"].data<coral::Blob>();
00835 
00836     unsigned long long branch_id=3;
00837     std::string branch_name("DATA");
00838     lumi::RevisionDML revisionDML;
00839     lumi::RevisionDML::TrgEntry trgrundata;
00840     std::stringstream op;
00841     op<<irunnumber;
00842     std::string runnumberStr=op.str();
00843     lumisession->transaction().start(false);
00844     trgrundata.entry_name=runnumberStr;
00845     trgrundata.source=source;
00846     trgrundata.runnumber=irunnumber;
00847     std::string bitnames;
00848     TriggerNameResult_Algo::iterator bitnameIt;
00849     TriggerNameResult_Algo::iterator bitnameItBeg=algonames.begin();
00850     TriggerNameResult_Algo::iterator bitnameItEnd=algonames.end();
00851     for (bitnameIt=bitnameItBeg;bitnameIt!=bitnameItEnd;++bitnameIt){
00852       if(bitnameIt!=bitnameItBeg){
00853         bitnames+=std::string(",");
00854       }
00855       bitnames+=*bitnameIt;
00856     }
00857     TriggerNameResult_Tech::iterator techbitnameIt;
00858     TriggerNameResult_Tech::iterator techbitnameItBeg=technames.begin();
00859     TriggerNameResult_Tech::iterator techbitnameItEnd=technames.end();
00860     for(techbitnameIt=techbitnameItBeg;techbitnameIt!=techbitnameItEnd;++techbitnameIt){
00861       bitnames+=std::string(",");
00862       bitnames+=*techbitnameIt;
00863     }
00864     std::cout<<"\tbitnames "<<bitnames<<std::endl;
00865     trgrundata.bitzeroname=technames[4];
00866     trgrundata.bitnames=bitnames;
00867     trgrundata.entry_id=revisionDML.getEntryInBranchByName(lumisession->nominalSchema(),lumi::LumiNames::trgdataTableName(),runnumberStr,branch_name);
00868     if(trgrundata.entry_id==0){
00869       revisionDML.bookNewEntry(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata);
00870       std::cout<<"trgrundata revision_id "<<trgrundata.revision_id<<" entry_id "<<trgrundata.entry_id<<" data_id "<<trgrundata.data_id<<std::endl;
00871       revisionDML.addEntry(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata,branch_id,branch_name);
00872   }else{
00873       revisionDML.bookNewRevision(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata);
00874       std::cout<<"trgrundata revision_id "<<trgrundata.revision_id<<" entry_id "<<trgrundata.entry_id<<" data_id "<<trgrundata.data_id<<std::endl;
00875       revisionDML.addRevision(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata,branch_id,branch_name);
00876     }
00877     std::cout<<"inserting trgrundata "<<std::endl;
00878     revisionDML.insertTrgRunData(lumisession->nominalSchema(),trgrundata);
00879     std::cout<<"inserting lstrg data"<<std::endl;
00880  
00881     unsigned int trglscount=0;   
00882     // trglscount=0;
00883     coral::IBulkOperation* lstrgInserter=0; 
00884     unsigned int comittedls=0;
00885     for(deadIt=deadtimesBeg;deadIt!=deadtimesEnd;++deadIt,++trglscount ){
00886       unsigned int cmslscount=trglscount+1;
00887       if(!lumisession->transaction().isActive()){ 
00888         lumisession->transaction().start(false);
00889         coral::ITable& lstrgtable=lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
00890         lstrgInserter=lstrgtable.dataEditor().bulkInsert(lstrgData,2048);
00891       }else{
00892         if(deadIt==deadtimesBeg){
00893           coral::ITable& lstrgtable=lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
00894           lstrgInserter=lstrgtable.dataEditor().bulkInsert(lstrgData,2048);
00895         }
00896       }
00897       data_id = trgrundata.data_id;
00898       trgrunnum = irunnumber;
00899       cmslsnum = cmslscount;
00900       deadtime = *deadIt;
00901       deadfrac = deadfracs[trglscount];
00902       //bitzerocount = algocounts[trglscount][0];//use algobit_0
00903       //bitzeroprescale = prescalealgo[cmslsnum][0];
00904       bitzerocount=techcounts[trglscount][4]; //use techbit_4
00905       bitzeroprescale=prescaletech[cmslsnum][4];
00906       std::vector<unsigned int> fullprescales;
00907       fullprescales.reserve(prescalealgo[cmslsnum].size()+prescaletech[cmslsnum].size());
00908       fullprescales.insert(fullprescales.end(),prescalealgo[cmslsnum].begin(),prescalealgo[cmslsnum].end());
00909       fullprescales.insert(fullprescales.end(),prescaletech[cmslsnum].begin(),prescaletech[cmslsnum].end());
00910 
00911       prescaleblob.resize(sizeof(unsigned int)*(fullprescales.size()));
00912       void* prescaleblob_StartAddress = prescaleblob.startingAddress();
00913       std::memmove(prescaleblob_StartAddress,&fullprescales[0],sizeof(unsigned int)*(fullprescales.size()));
00914       
00915       std::vector<unsigned int> fullcounts;
00916       fullcounts.reserve(algocounts[trglscount].size()+techcounts[trglscount].size());
00917       fullcounts.insert(fullcounts.end(),algocounts[trglscount].begin(),algocounts[trglscount].end());
00918       fullcounts.insert(fullcounts.end(),techcounts[trglscount].begin(),techcounts[trglscount].end());
00919       trgcountblob.resize(sizeof(unsigned int)*(fullcounts.size()));
00920       void* trgcountblob_StartAddress = trgcountblob.startingAddress();
00921       std::memmove(trgcountblob_StartAddress,&fullcounts[0],sizeof(unsigned int)*(fullcounts.size()));
00922 
00923       lstrgInserter->processNextIteration();    
00924       lstrgInserter->flush();
00925       ++comittedls;
00926       if(comittedls==commitintv){
00927         std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00928         delete lstrgInserter; lstrgInserter=0;
00929         lumisession->transaction().commit();
00930         comittedls=0;
00931         std::cout<<"\t committed "<<std::endl; 
00932       }else if( trglscount==( totalcmsls-1) ){
00933         std::cout<<"\t committing at the end"<<std::endl; 
00934         delete lstrgInserter; lstrgInserter=0;
00935         lumisession->transaction().commit();
00936         std::cout<<"\t done"<<std::endl; 
00937       }
00938     }
00939 
00940 
00941   }
00942   const std::string TRGScalers2DB::dataType() const{
00943     return "TRG";
00944   }
00945   const std::string TRGScalers2DB::sourceType() const{
00946     return "DB";
00947   }
00948   //utilities
00949   std::string TRGScalers2DB::int2str(unsigned int t, unsigned int width){
00950     std::stringstream ss;
00951     ss.width(width);
00952     ss.fill('0');
00953     ss<<t;
00954     return ss.str();
00955   }
00956   unsigned int TRGScalers2DB::str2int(const std::string& s){
00957     std::istringstream myStream(s);
00958     unsigned int i;
00959     if(myStream>>i){
00960       return i;
00961     }else{
00962       throw lumi::Exception(std::string("str2int error"),"str2int","TRGScalers2DB");
00963     }
00964   }
00965   TRGScalers2DB::~TRGScalers2DB(){}
00966 }//ns lumi
00967 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00968 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::TRGScalers2DB,"TRGScalers2DB");
00969 #endif