CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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 unsigned long long 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     unsigned long long 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   unsigned long long 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 0;
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     unsigned long long trgdataid=0;
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       trgdataid=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     return trgdataid;
00667   }
00668   void  
00669   TRGScalers2DB::writeTrgData(coral::ISessionProxy* lumisession,
00670                        unsigned int runnumber,
00671                        const std::string& source,
00672                        TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesBeg,TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesEnd,
00673                        TRGScalers2DB::TriggerDeadFracResult& deadfracs,
00674                        TRGScalers2DB::TriggerNameResult_Algo& algonames,
00675                        TRGScalers2DB::TriggerNameResult_Tech& technames,
00676                        TRGScalers2DB::TriggerCountResult_Algo& algocounts,
00677                        TRGScalers2DB::TriggerCountResult_Tech& techcounts,
00678                        TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
00679                        TRGScalers2DB::PrescaleResult_Tech& prescaletech,
00680                        unsigned int commitintv){    
00681     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
00682     //unsigned int totalcmsls=deadtimes.size();
00683     unsigned int totalcmsls=std::distance(deadtimesBeg,deadtimesEnd);
00684     std::cout<<"inserting totalcmsls "<<totalcmsls<<std::endl;
00685     std::map< unsigned long long, std::vector<unsigned long long> > idallocationtable;
00686     std::cout<<"\t allocating total ids "<<totalcmsls*lumi::N_TRGBIT<<std::endl; 
00687     lumisession->transaction().start(false);
00688     lumi::idDealer idg(lumisession->nominalSchema());
00689     unsigned long long trgID = idg.generateNextIDForTable(LumiNames::trgTableName(),totalcmsls*lumi::N_TRGBIT)-totalcmsls*lumi::N_TRGBIT;
00690     //lumisession->transaction().commit();
00691     unsigned int trglscount=0;
00692     for(deadIt=deadtimesBeg;deadIt!=deadtimesEnd;++deadIt,++trglscount){
00693       std::vector<unsigned long long> bitvec;
00694       bitvec.reserve(lumi::N_TRGBIT);
00695       const BITCOUNT& algoinbits=algocounts[trglscount];
00696       const BITCOUNT& techinbits=techcounts[trglscount];
00697       BITCOUNT::const_iterator algoBitIt;
00698       BITCOUNT::const_iterator algoBitBeg=algoinbits.begin();
00699       BITCOUNT::const_iterator algoBitEnd=algoinbits.end();     
00700       for(algoBitIt=algoBitBeg;algoBitIt!=algoBitEnd;++algoBitIt,++trgID){
00701         bitvec.push_back(trgID);
00702       }
00703       BITCOUNT::const_iterator techBitIt;
00704       BITCOUNT::const_iterator techBitBeg=techinbits.begin();
00705       BITCOUNT::const_iterator techBitEnd=techinbits.end();
00706       for(techBitIt=techBitBeg;techBitIt!=techBitEnd;++techBitIt,++trgID){
00707         bitvec.push_back(trgID);
00708       }
00709       idallocationtable.insert(std::make_pair(trglscount,bitvec));
00710     }
00711     std::cout<<"\t all ids allocated"<<std::endl; 
00712     coral::AttributeList trgData;
00713     trgData.extend<unsigned long long>("TRG_ID");
00714     trgData.extend<unsigned int>("RUNNUM");
00715     trgData.extend<unsigned int>("CMSLSNUM");
00716     trgData.extend<unsigned int>("BITNUM");
00717     trgData.extend<std::string>("BITNAME");
00718     trgData.extend<unsigned int>("TRGCOUNT");
00719     trgData.extend<unsigned long long>("DEADTIME");
00720     trgData.extend<float>("DEADFRAC");
00721     trgData.extend<unsigned int>("PRESCALE");
00722     
00723     unsigned long long& trg_id=trgData["TRG_ID"].data<unsigned long long>();
00724     unsigned int& trgrunnum=trgData["RUNNUM"].data<unsigned int>();
00725     unsigned int& cmslsnum=trgData["CMSLSNUM"].data<unsigned int>();
00726     unsigned int& bitnum=trgData["BITNUM"].data<unsigned int>();
00727     std::string& bitname=trgData["BITNAME"].data<std::string>();
00728     unsigned int& count=trgData["TRGCOUNT"].data<unsigned int>();
00729     unsigned long long& deadtime=trgData["DEADTIME"].data<unsigned long long>();
00730     float& deadfrac=trgData["DEADFRAC"].data<float>();
00731     unsigned int& prescale=trgData["PRESCALE"].data<unsigned int>();
00732                       
00733     trglscount=0;    
00734     coral::IBulkOperation* trgInserter=0; 
00735     unsigned int comittedls=0;
00736     for(deadIt=deadtimesBeg;deadIt!=deadtimesEnd;++deadIt,++trglscount ){
00737       unsigned int cmslscount=trglscount+1;
00738       float dfra=deadfracs[trglscount];
00739       const BITCOUNT& algoinbits=algocounts[trglscount];
00740       const BITCOUNT& techinbits=techcounts[trglscount];
00741       unsigned int trgbitcount=0;
00742       BITCOUNT::const_iterator algoBitIt;
00743       BITCOUNT::const_iterator algoBitBeg=algoinbits.begin();
00744       BITCOUNT::const_iterator algoBitEnd=algoinbits.end();
00745       if(!lumisession->transaction().isActive()){ 
00746         lumisession->transaction().start(false);
00747         coral::ITable& trgtable=lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
00748         trgInserter=trgtable.dataEditor().bulkInsert(trgData,2048);
00749       }else{
00750         if(deadIt==deadtimesBeg){
00751           coral::ITable& trgtable=lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
00752           trgInserter=trgtable.dataEditor().bulkInsert(trgData,2048);
00753         }
00754       }
00755       for(algoBitIt=algoBitBeg;algoBitIt!=algoBitEnd;++algoBitIt,++trgbitcount){
00756         trg_id = idallocationtable[trglscount].at(trgbitcount);
00757         deadtime=*deadIt;
00758         deadfrac=dfra;
00759         trgrunnum = runnumber;
00760         cmslsnum = cmslscount;
00761         bitnum=trgbitcount;
00762         bitname=algonames[trgbitcount];
00763         count=*algoBitIt;
00764         prescale=prescalealgo[cmslscount].at(trgbitcount);
00765         //std::cout<<"cmslsnum "<<cmslsnum<<" bitnum "<<bitnum<<" bitname "<<bitname<<" prescale "<< prescale<<" count "<<count<<std::endl;
00766         trgInserter->processNextIteration();    
00767       }
00768       BITCOUNT::const_iterator techBitIt;
00769       BITCOUNT::const_iterator techBitBeg=techinbits.begin();
00770       BITCOUNT::const_iterator techBitEnd=techinbits.end();
00771       for(techBitIt=techBitBeg;techBitIt!=techBitEnd;++techBitIt,++trgbitcount){
00772         trg_id = idallocationtable[trglscount].at(trgbitcount);
00773         deadtime=*deadIt;
00774         deadfrac=dfra;
00775         trgrunnum = runnumber;
00776         cmslsnum = cmslscount;
00777         bitnum=trgbitcount;
00778         bitname=technames[trgbitcount-lumi::N_TRGALGOBIT];
00779         count=*techBitIt;
00780         prescale=prescaletech[cmslsnum][trgbitcount-lumi::N_TRGALGOBIT];
00781         trgInserter->processNextIteration();    
00782       }
00783       trgInserter->flush();
00784       ++comittedls;
00785       if(comittedls==commitintv){
00786         std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00787         delete trgInserter; trgInserter=0;
00788         lumisession->transaction().commit();
00789         comittedls=0;
00790         std::cout<<"\t committed "<<std::endl; 
00791       }else if( trglscount==( totalcmsls-1) ){
00792         std::cout<<"\t committing at the end"<<std::endl; 
00793         delete trgInserter; trgInserter=0;
00794         lumisession->transaction().commit();
00795         std::cout<<"\t done"<<std::endl; 
00796       }
00797     }
00798   }
00799   unsigned long long
00800   TRGScalers2DB::writeTrgDataToSchema2(coral::ISessionProxy* lumisession,
00801                                 unsigned int irunnumber,
00802                                 const std::string& source,
00803                                 TriggerDeadCountResult::iterator deadtimesBeg,
00804                                 TriggerDeadCountResult::iterator deadtimesEnd,
00805                                 TRGScalers2DB::TriggerDeadFracResult& deadfracs,       
00806                                 TRGScalers2DB::TriggerNameResult_Algo& algonames, 
00807                                 TRGScalers2DB::TriggerNameResult_Tech& technames,
00808                                 TRGScalers2DB::TriggerCountResult_Algo& algocounts,
00809                                 TRGScalers2DB::TriggerCountResult_Tech& techcounts,
00810                                 TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
00811                                 TRGScalers2DB::PrescaleResult_Tech& prescaletech,
00812                                 unsigned int commitintv){
00813     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
00814     unsigned int totalcmsls=std::distance(deadtimesBeg,deadtimesEnd);
00815     std::cout<<"inserting totalcmsls "<<totalcmsls<<std::endl;
00816     coral::AttributeList lstrgData;
00817     lstrgData.extend<unsigned long long>("DATA_ID");
00818     lstrgData.extend<unsigned int>("RUNNUM");
00819     lstrgData.extend<unsigned int>("CMSLSNUM");
00820     lstrgData.extend<unsigned long long>("DEADTIMECOUNT");
00821     lstrgData.extend<unsigned int>("BITZEROCOUNT");
00822     lstrgData.extend<unsigned int>("BITZEROPRESCALE");
00823     lstrgData.extend<float>("DEADFRAC");
00824     lstrgData.extend<coral::Blob>("PRESCALEBLOB");
00825     lstrgData.extend<coral::Blob>("TRGCOUNTBLOB");
00826 
00827     unsigned long long& data_id=lstrgData["DATA_ID"].data<unsigned long long>();
00828     unsigned int& trgrunnum=lstrgData["RUNNUM"].data<unsigned int>();
00829     unsigned int& cmslsnum=lstrgData["CMSLSNUM"].data<unsigned int>();
00830     unsigned long long& deadtime=lstrgData["DEADTIMECOUNT"].data<unsigned long long>();
00831     unsigned int& bitzerocount=lstrgData["BITZEROCOUNT"].data<unsigned int>();
00832     unsigned int& bitzeroprescale=lstrgData["BITZEROPRESCALE"].data<unsigned int>();
00833     float& deadfrac=lstrgData["DEADFRAC"].data<float>();
00834     coral::Blob& prescaleblob=lstrgData["PRESCALEBLOB"].data<coral::Blob>();
00835     coral::Blob& trgcountblob=lstrgData["TRGCOUNTBLOB"].data<coral::Blob>();
00836 
00837     unsigned long long branch_id=3;
00838     std::string branch_name("DATA");
00839     lumi::RevisionDML revisionDML;
00840     lumi::RevisionDML::TrgEntry trgrundata;
00841     std::stringstream op;
00842     op<<irunnumber;
00843     std::string runnumberStr=op.str();
00844     lumisession->transaction().start(false);
00845     trgrundata.entry_name=runnumberStr;
00846     trgrundata.source=source;
00847     trgrundata.runnumber=irunnumber;
00848     std::string bitnames;
00849     TriggerNameResult_Algo::iterator bitnameIt;
00850     TriggerNameResult_Algo::iterator bitnameItBeg=algonames.begin();
00851     TriggerNameResult_Algo::iterator bitnameItEnd=algonames.end();
00852     for (bitnameIt=bitnameItBeg;bitnameIt!=bitnameItEnd;++bitnameIt){
00853       if(bitnameIt!=bitnameItBeg){
00854         bitnames+=std::string(",");
00855       }
00856       bitnames+=*bitnameIt;
00857     }
00858     TriggerNameResult_Tech::iterator techbitnameIt;
00859     TriggerNameResult_Tech::iterator techbitnameItBeg=technames.begin();
00860     TriggerNameResult_Tech::iterator techbitnameItEnd=technames.end();
00861     for(techbitnameIt=techbitnameItBeg;techbitnameIt!=techbitnameItEnd;++techbitnameIt){
00862       bitnames+=std::string(",");
00863       bitnames+=*techbitnameIt;
00864     }
00865     std::cout<<"\tbitnames "<<bitnames<<std::endl;
00866     trgrundata.bitzeroname=technames[4];
00867     trgrundata.bitnames=bitnames;
00868     trgrundata.entry_id=revisionDML.getEntryInBranchByName(lumisession->nominalSchema(),lumi::LumiNames::trgdataTableName(),runnumberStr,branch_name);
00869     if(trgrundata.entry_id==0){
00870       revisionDML.bookNewEntry(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata);
00871       std::cout<<"trgrundata revision_id "<<trgrundata.revision_id<<" entry_id "<<trgrundata.entry_id<<" data_id "<<trgrundata.data_id<<std::endl;
00872       revisionDML.addEntry(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata,branch_id,branch_name);
00873   }else{
00874       revisionDML.bookNewRevision(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata);
00875       std::cout<<"trgrundata revision_id "<<trgrundata.revision_id<<" entry_id "<<trgrundata.entry_id<<" data_id "<<trgrundata.data_id<<std::endl;
00876       revisionDML.addRevision(lumisession->nominalSchema(),LumiNames::trgdataTableName(),trgrundata,branch_id,branch_name);
00877     }
00878     std::cout<<"inserting trgrundata "<<std::endl;
00879     revisionDML.insertTrgRunData(lumisession->nominalSchema(),trgrundata);
00880     std::cout<<"inserting lstrg data"<<std::endl;
00881  
00882     unsigned int trglscount=0;   
00883     // trglscount=0;
00884     coral::IBulkOperation* lstrgInserter=0; 
00885     unsigned int comittedls=0;
00886     for(deadIt=deadtimesBeg;deadIt!=deadtimesEnd;++deadIt,++trglscount ){
00887       unsigned int cmslscount=trglscount+1;
00888       if(!lumisession->transaction().isActive()){ 
00889         lumisession->transaction().start(false);
00890         coral::ITable& lstrgtable=lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
00891         lstrgInserter=lstrgtable.dataEditor().bulkInsert(lstrgData,2048);
00892       }else{
00893         if(deadIt==deadtimesBeg){
00894           coral::ITable& lstrgtable=lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
00895           lstrgInserter=lstrgtable.dataEditor().bulkInsert(lstrgData,2048);
00896         }
00897       }
00898       data_id = trgrundata.data_id;
00899       trgrunnum = irunnumber;
00900       cmslsnum = cmslscount;
00901       deadtime = *deadIt;
00902       deadfrac = deadfracs[trglscount];
00903       //bitzerocount = algocounts[trglscount][0];//use algobit_0
00904       //bitzeroprescale = prescalealgo[cmslsnum][0];
00905       bitzerocount=techcounts[trglscount][4]; //use techbit_4
00906       bitzeroprescale=prescaletech[cmslsnum][4];
00907       std::vector<unsigned int> fullprescales;
00908       fullprescales.reserve(prescalealgo[cmslsnum].size()+prescaletech[cmslsnum].size());
00909       fullprescales.insert(fullprescales.end(),prescalealgo[cmslsnum].begin(),prescalealgo[cmslsnum].end());
00910       fullprescales.insert(fullprescales.end(),prescaletech[cmslsnum].begin(),prescaletech[cmslsnum].end());
00911 
00912       prescaleblob.resize(sizeof(unsigned int)*(fullprescales.size()));
00913       void* prescaleblob_StartAddress = prescaleblob.startingAddress();
00914       std::memmove(prescaleblob_StartAddress,&fullprescales[0],sizeof(unsigned int)*(fullprescales.size()));
00915       
00916       std::vector<unsigned int> fullcounts;
00917       fullcounts.reserve(algocounts[trglscount].size()+techcounts[trglscount].size());
00918       fullcounts.insert(fullcounts.end(),algocounts[trglscount].begin(),algocounts[trglscount].end());
00919       fullcounts.insert(fullcounts.end(),techcounts[trglscount].begin(),techcounts[trglscount].end());
00920       trgcountblob.resize(sizeof(unsigned int)*(fullcounts.size()));
00921       void* trgcountblob_StartAddress = trgcountblob.startingAddress();
00922       std::memmove(trgcountblob_StartAddress,&fullcounts[0],sizeof(unsigned int)*(fullcounts.size()));
00923 
00924       lstrgInserter->processNextIteration();    
00925       lstrgInserter->flush();
00926       ++comittedls;
00927       if(comittedls==commitintv){
00928         std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00929         delete lstrgInserter; lstrgInserter=0;
00930         lumisession->transaction().commit();
00931         comittedls=0;
00932         std::cout<<"\t committed "<<std::endl; 
00933       }else if( trglscount==( totalcmsls-1) ){
00934         std::cout<<"\t committing at the end"<<std::endl; 
00935         delete lstrgInserter; lstrgInserter=0;
00936         lumisession->transaction().commit();
00937         std::cout<<"\t done"<<std::endl; 
00938       }
00939     }
00940 
00941     return trgrundata.data_id;
00942   }
00943   const std::string TRGScalers2DB::dataType() const{
00944     return "TRG";
00945   }
00946   const std::string TRGScalers2DB::sourceType() const{
00947     return "DB";
00948   }
00949   //utilities
00950   std::string TRGScalers2DB::int2str(unsigned int t, unsigned int width){
00951     std::stringstream ss;
00952     ss.width(width);
00953     ss.fill('0');
00954     ss<<t;
00955     return ss.str();
00956   }
00957   unsigned int TRGScalers2DB::str2int(const std::string& s){
00958     std::istringstream myStream(s);
00959     unsigned int i;
00960     if(myStream>>i){
00961       return i;
00962     }else{
00963       throw lumi::Exception(std::string("str2int error"),"str2int","TRGScalers2DB");
00964     }
00965   }
00966   TRGScalers2DB::~TRGScalers2DB(){}
00967 }//ns lumi
00968 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00969 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::TRGScalers2DB,"TRGScalers2DB");
00970 #endif