Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:26:37

0001 #ifndef RecoLuminosity_LumiProducer_TRGScalers2DB_h
0002 #define RecoLuminosity_LumiProducer_TRGScalers2DB_h
0003 #include "CoralBase/AttributeList.h"
0004 #include "CoralBase/Attribute.h"
0005 #include "CoralBase/AttributeSpecification.h"
0006 #include "CoralBase/Blob.h"
0007 #include "CoralBase/Exception.h"
0008 #include "RelationalAccess/ConnectionService.h"
0009 #include "RelationalAccess/ISessionProxy.h"
0010 #include "RelationalAccess/ITransaction.h"
0011 #include "RelationalAccess/ITypeConverter.h"
0012 #include "RelationalAccess/IQuery.h"
0013 #include "RelationalAccess/ICursor.h"
0014 #include "RelationalAccess/ISchema.h"
0015 #include "RelationalAccess/IView.h"
0016 #include "RelationalAccess/ITable.h"
0017 #include "RelationalAccess/ITableDataEditor.h"
0018 #include "RelationalAccess/IBulkOperation.h"
0019 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
0020 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0021 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
0022 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0023 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
0024 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
0025 #include "RecoLuminosity/LumiProducer/interface/RevisionDML.h"
0026 #include <cstring>
0027 #include <cstdlib>
0028 #include <iostream>
0029 #include <sstream>
0030 #include <stdexcept>
0031 #include <map>
0032 namespace lumi {
0033   class TRGScalers2DB : public DataPipe {
0034   public:
0035     const static unsigned int COMMITLSINTERVAL = 150;     //commit interval in LS, totalrow=nsl*192
0036     const static unsigned int COMMITLSTRGINTERVAL = 550;  //commit interval in LS of schema2
0037     explicit TRGScalers2DB(const std::string& dest);
0038     unsigned long long retrieveData(unsigned int runnumber) override;
0039     const std::string dataType() const override;
0040     const std::string sourceType() const override;
0041     ~TRGScalers2DB() override;
0042 
0043     //per run information
0044     typedef std::vector<std::string> TriggerNameResult_Algo;
0045     typedef std::vector<std::string> TriggerNameResult_Tech;
0046     //per lumisection information
0047     typedef unsigned long long DEADCOUNT;
0048     typedef float DEADFRAC;
0049     typedef std::vector<DEADCOUNT> TriggerDeadCountResult;
0050     typedef std::vector<DEADFRAC> TriggerDeadFracResult;
0051     typedef std::vector<unsigned int> BITCOUNT;
0052     typedef std::vector<BITCOUNT> TriggerCountResult_Algo;
0053     typedef std::vector<BITCOUNT> TriggerCountResult_Tech;
0054     typedef std::map<unsigned int, std::vector<unsigned int> > PrescaleResult_Algo;
0055     typedef std::map<unsigned int, std::vector<unsigned int> > PrescaleResult_Tech;
0056 
0057     std::string int2str(unsigned int t, unsigned int width);
0058     unsigned int str2int(const std::string& s);
0059     void writeTrgData(coral::ISessionProxy* session,
0060                       unsigned int runnumber,
0061                       const std::string& source,
0062                       TriggerDeadCountResult::iterator deadtimesBeg,
0063                       TriggerDeadCountResult::iterator deadtimesEnd,
0064                       TRGScalers2DB::TriggerDeadFracResult& deadfracs,
0065                       TriggerNameResult_Algo& algonames,
0066                       TriggerNameResult_Tech& technames,
0067                       TriggerCountResult_Algo& algocounts,
0068                       TriggerCountResult_Tech& techcounts,
0069                       PrescaleResult_Algo& prescalealgo,
0070                       PrescaleResult_Tech& prescaletech,
0071                       unsigned int commitintv);
0072     unsigned long long writeTrgDataToSchema2(coral::ISessionProxy* session,
0073                                              unsigned int runnumber,
0074                                              const std::string& source,
0075                                              TriggerDeadCountResult::iterator deadtimesBeg,
0076                                              TriggerDeadCountResult::iterator deadtimesEnd,
0077                                              TRGScalers2DB::TriggerDeadFracResult& deadfracs,
0078                                              TriggerNameResult_Algo& algonames,
0079                                              TriggerNameResult_Tech& technames,
0080                                              TriggerCountResult_Algo& algocounts,
0081                                              TriggerCountResult_Tech& techcounts,
0082                                              PrescaleResult_Algo& prescalealgo,
0083                                              PrescaleResult_Tech& prescaletech,
0084                                              unsigned int commitintv);
0085   };  //cl TRGScalers2DB
0086   //
0087   //implementation
0088   //
0089   //deadtime fraction query
0090   //
0091   //select fraction,lumi_section,count_bx from cms_gt_mon.v_scalers_tcs_deadtime where run_number=:runnum and scaler_name='DeadtimeBeamActive'
0092   //
0093   //select count_bx,lumi_section,scaler_index from cms_gt_mon.v_scalers_fdl_algo where run_number=:runnum order by scaler_index;
0094   //
0095   //
0096   //select count_bx,lumi_section,scaler_index from cms_gt_mon.v_scalers_fdl_tech where run_number=:runnum order by scaler_index;
0097   //
0098   //select algo_index,alias from cms_gt.gt_run_algo_view where runnumber=:runnumber //order by algo_index;
0099   //
0100   //select techtrig_index,name from cms_gt.gt_run_tech_view where runnumber=:runnumber //order by techtrig_index;
0101   //
0102   //select distinct(prescale_index) from cms_gt_mon.lumi_sections where run_number=:runnumber;
0103   //
0104   //select lumi_section,prescale_index from cms_gt_mon.lumi_sections where run_number=:runnumber;
0105   //
0106   //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;
0107   // {prescale_index:[pres]}
0108   //
0109   //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;
0110   //
0111   TRGScalers2DB::TRGScalers2DB(const std::string& dest) : DataPipe(dest) {}
0112   unsigned long long TRGScalers2DB::retrieveData(unsigned int runnumber) {
0113     std::string runnumberstr = int2str(runnumber, 6);
0114     //query source GT database
0115     coral::ConnectionService* svc = new coral::ConnectionService;
0116     lumi::DBConfig dbconf(*svc);
0117     if (!m_authpath.empty()) {
0118       dbconf.setAuthentication(m_authpath);
0119     }
0120     //std::cout<<"m_source "<<m_source<<std::endl;
0121     coral::ISessionProxy* trgsession = svc->connect(m_source, coral::ReadOnly);
0122     coral::ITypeConverter& tpc = trgsession->typeConverter();
0123     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(7)");
0124     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0125     tpc.setCppTypeForSqlType("unsigned long long", "NUMBER(20)");
0126     std::string gtmonschema("CMS_GT_MON");
0127     std::string algoviewname("V_SCALERS_FDL_ALGO");
0128     std::string techviewname("V_SCALERS_FDL_TECH");
0129     std::string deadviewname("V_SCALERS_TCS_DEADTIME");
0130     std::string lstablename("LUMI_SECTIONS");
0131 
0132     std::string gtschema("CMS_GT");
0133     std::string runtechviewname("GT_RUN_TECH_VIEW");
0134     std::string runalgoviewname("GT_RUN_ALGO_VIEW");
0135     std::string runprescalgoviewname("GT_RUN_PRESC_ALGO_VIEW");
0136     std::string runpresctechviewname("GT_RUN_PRESC_TECH_VIEW");
0137 
0138     //data exchange format
0139     lumi::TRGScalers2DB::PrescaleResult_Algo algoprescale;
0140     lumi::TRGScalers2DB::PrescaleResult_Tech techprescale;
0141     lumi::TRGScalers2DB::BITCOUNT mybitcount_algo;
0142     mybitcount_algo.reserve(lumi::N_TRGALGOBIT);
0143     lumi::TRGScalers2DB::BITCOUNT mybitcount_tech;
0144     mybitcount_tech.reserve(lumi::N_TRGTECHBIT);
0145     lumi::TRGScalers2DB::TriggerNameResult_Algo algonames;
0146     algonames.reserve(lumi::N_TRGALGOBIT);
0147     lumi::TRGScalers2DB::TriggerNameResult_Tech technames;
0148     technames.reserve(lumi::N_TRGTECHBIT);
0149     lumi::TRGScalers2DB::TriggerCountResult_Algo algocount;
0150     //algocount.reserve(400);
0151     lumi::TRGScalers2DB::TriggerCountResult_Tech techcount;
0152     //techcount.reserve(400);
0153     lumi::TRGScalers2DB::TriggerDeadFracResult deadfracresult;
0154     deadfracresult.reserve(500);
0155     lumi::TRGScalers2DB::TriggerDeadCountResult deadtimeresult;
0156     deadtimeresult.reserve(500);
0157     coral::ITransaction& transaction = trgsession->transaction();
0158     transaction.start(true);
0159     /**
0160        Part I
0161        query tables in schema cms_gt_mon
0162     **/
0163     coral::ISchema& gtmonschemaHandle = trgsession->schema(gtmonschema);
0164     if (!gtmonschemaHandle.existsView(algoviewname)) {
0165       throw lumi::Exception(std::string("non-existing view ") + algoviewname, "retrieveData", "TRGScalers2DB");
0166     }
0167     if (!gtmonschemaHandle.existsView(techviewname)) {
0168       throw lumi::Exception(std::string("non-existing view ") + techviewname, "retrieveData", "TRGScalers2DB");
0169     }
0170     if (!gtmonschemaHandle.existsView(deadviewname)) {
0171       throw lumi::Exception(std::string("non-existing view ") + deadviewname, "retrieveData", "TRGScalers2DB");
0172     }
0173 
0174     //
0175     //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;
0176     //note: scaler_index  0-127
0177     //note: lumi_section count from 1
0178     //
0179     coral::AttributeList bindVariableList;
0180     bindVariableList.extend("runnumber", typeid(unsigned int));
0181     bindVariableList["runnumber"].data<unsigned int>() = runnumber;
0182 
0183     coral::IQuery* Queryalgoview = gtmonschemaHandle.newQuery();
0184     Queryalgoview->addToTableList(algoviewname);
0185     coral::AttributeList qalgoOutput;
0186     qalgoOutput.extend("COUNT_BX", typeid(unsigned int));
0187     qalgoOutput.extend("LUMI_SECTION", typeid(unsigned int));
0188     qalgoOutput.extend("SCALER_INDEX", typeid(unsigned int));
0189     Queryalgoview->addToOutputList("COUNT_BX");
0190     Queryalgoview->addToOutputList("LUMI_SECTION");
0191     Queryalgoview->addToOutputList("SCALER_INDEX");
0192     Queryalgoview->setCondition("RUN_NUMBER=:runnumber", bindVariableList);
0193     Queryalgoview->addToOrderList("LUMI_SECTION");
0194     Queryalgoview->addToOrderList("SCALER_INDEX");
0195     Queryalgoview->defineOutput(qalgoOutput);
0196     coral::ICursor& c = Queryalgoview->execute();
0197 
0198     unsigned int s = 0;
0199     while (c.next()) {
0200       const coral::AttributeList& row = c.currentRow();
0201       //row.toOutputStream( std::cout ) << std::endl;
0202       unsigned int lsnr = row["LUMI_SECTION"].data<unsigned int>();
0203       unsigned int count = row["COUNT_BX"].data<unsigned int>();
0204       unsigned int algobit = row["SCALER_INDEX"].data<unsigned int>();
0205       mybitcount_algo.push_back(count);
0206       if (algobit == (lumi::N_TRGALGOBIT - 1)) {
0207         ++s;
0208         while (s != lsnr) {
0209           std::cout << "ALGO COUNT alert: found hole in LS range " << s << std::endl;
0210           std::cout << "    fill all algocount 0 for LS " << s << std::endl;
0211           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT, 0);
0212           algocount.push_back(tmpzero);
0213           ++s;
0214         }
0215         algocount.push_back(mybitcount_algo);
0216         mybitcount_algo.clear();
0217       }
0218     }
0219     if (s == 0) {
0220       c.close();
0221       delete Queryalgoview;
0222       transaction.commit();
0223       throw lumi::Exception(std::string("requested run ") + runnumberstr + std::string(" doesn't exist for algocounts"),
0224                             "retrieveData",
0225                             "TRGScalers2DB");
0226     }
0227     delete Queryalgoview;
0228     //
0229     //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;
0230     //
0231     //note: techobit 0-63
0232     //note: lsnr count from 1
0233     //
0234     coral::IQuery* Querytechview = gtmonschemaHandle.newQuery();
0235     Querytechview->addToTableList(techviewname);
0236     coral::AttributeList qtechOutput;
0237     qtechOutput.extend("COUNT_BX", typeid(unsigned int));
0238     qtechOutput.extend("LUMI_SECTION", typeid(unsigned int));
0239     qtechOutput.extend("SCALER_INDEX", typeid(unsigned int));
0240     Querytechview->addToOutputList("COUNT_BX");
0241     Querytechview->addToOutputList("LUMI_SECTION");
0242     Querytechview->addToOutputList("SCALER_INDEX");
0243     Querytechview->setCondition("RUN_NUMBER=:runnumber", bindVariableList);
0244     Querytechview->addToOrderList("LUMI_SECTION");
0245     Querytechview->addToOrderList("SCALER_INDEX");
0246     Querytechview->defineOutput(qtechOutput);
0247     coral::ICursor& techcursor = Querytechview->execute();
0248     s = 0;
0249     while (techcursor.next()) {
0250       const coral::AttributeList& row = techcursor.currentRow();
0251       //row.toOutputStream( std::cout #include <stdexcept>) << std::endl;
0252       unsigned int lsnr = row["LUMI_SECTION"].data<unsigned int>();
0253       unsigned int count = row["COUNT_BX"].data<unsigned int>();
0254       unsigned int techbit = row["SCALER_INDEX"].data<unsigned int>();
0255       mybitcount_tech.push_back(count);
0256       if (techbit == (lumi::N_TRGTECHBIT - 1)) {
0257         ++s;
0258         while (s != lsnr) {
0259           std::cout << "TECH COUNT alert: found hole in LS range " << s << std::endl;
0260           std::cout << "     fill all techcount with 0 for LS " << s << std::endl;
0261           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT, 0);
0262           techcount.push_back(tmpzero);
0263           ++s;
0264         }
0265         techcount.push_back(mybitcount_tech);
0266         mybitcount_tech.clear();
0267       }
0268     }
0269     if (s == 0) {
0270       techcursor.close();
0271       delete Querytechview;
0272       transaction.commit();
0273       throw lumi::Exception(std::string("requested run ") + runnumberstr + std::string(" doesn't exist for techcounts"),
0274                             "retrieveData",
0275                             "TRGScalers2DB");
0276     }
0277     delete Querytechview;
0278 
0279     //
0280     //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'
0281     //
0282     //note: lsnr count from 1
0283     //
0284     coral::IQuery* Querydeadview = gtmonschemaHandle.newQuery();
0285     Querydeadview->addToTableList(deadviewname);
0286     coral::AttributeList qdeadOutput;
0287     qdeadOutput.extend("FRACTION", typeid(float));
0288     qdeadOutput.extend("LUMI_SECTION", typeid(unsigned int));
0289     qdeadOutput.extend("COUNT_BX", typeid(unsigned int));
0290     Querydeadview->addToOutputList("FRACTION");
0291     Querydeadview->addToOutputList("LUMI_SECTION");
0292     Querydeadview->addToOutputList("COUNT_BX");
0293     coral::AttributeList bindVariablesDead;
0294     bindVariablesDead.extend("runnumber", typeid(int));
0295     bindVariablesDead.extend("scalername", typeid(std::string));
0296     bindVariablesDead["runnumber"].data<int>() = runnumber;
0297     bindVariablesDead["scalername"].data<std::string>() = std::string("DeadtimeBeamActive");
0298     Querydeadview->setCondition("RUN_NUMBER=:runnumber AND SCALER_NAME=:scalername", bindVariablesDead);
0299     Querydeadview->addToOrderList("LUMI_SECTION");
0300     Querydeadview->defineOutput(qdeadOutput);
0301     coral::ICursor& deadcursor = Querydeadview->execute();
0302     s = 0;
0303     while (deadcursor.next()) {
0304       const coral::AttributeList& row = deadcursor.currentRow();
0305       //row.toOutputStream( std::cout ) << std::endl;
0306       ++s;
0307       unsigned int lsnr = row["LUMI_SECTION"].data<unsigned int>();
0308       float dfrac = 1.0;
0309       unsigned int count = 0;
0310       while (s != lsnr) {
0311         std::cout << "DEADTIME alert: found hole in LS range" << s << std::endl;
0312         std::cout << "         fill deadtimefraction 0%, deadtimebeamactive 0 for LS " << s << std::endl;
0313         deadfracresult.push_back(dfrac);
0314         deadtimeresult.push_back(count);
0315         ++s;
0316       }
0317       if (!row["FRACTION"].isNull()) {
0318         dfrac = row["FRACTION"].data<float>();  //deadfraction is null from trigger means "undefined", but insert 1.0...
0319       } else {
0320         std::cout << "DEADTIME fraction alert: undefined fraction , assume 100% , LS " << lsnr << std::endl;
0321       }
0322       if (dfrac > 1.0) {
0323         std::cout << "DEADTIME fraction alert: overflow dead fraction , force to 100% , LS " << lsnr << std::endl;
0324         dfrac = 1.0;
0325       }
0326       deadfracresult.push_back(dfrac);
0327       count = row["COUNT_BX"].data<unsigned int>();
0328       deadtimeresult.push_back(count);
0329     }
0330     if (s == 0) {
0331       deadcursor.close();
0332       delete Querydeadview;
0333       transaction.commit();
0334       throw lumi::Exception(std::string("requested run ") + runnumberstr + std::string(" doesn't exist for deadcounts"),
0335                             "retrieveData",
0336                             "TRGScalers2DB");
0337       return 0;
0338     }
0339     delete Querydeadview;
0340 
0341     //
0342     //select distinct(prescale_index) from cms_gt_mon.lumi_sections where run_number=:runnumber;
0343     //
0344     std::vector<int> prescidx;
0345     coral::IQuery* allpsidxQuery = gtmonschemaHandle.newQuery();
0346     allpsidxQuery->addToTableList(lstablename);
0347     coral::AttributeList allpsidxOutput;
0348     allpsidxOutput.extend("psidx", typeid(int));
0349     allpsidxQuery->addToOutputList("distinct(PRESCALE_INDEX)", "psidx");
0350     coral::AttributeList bindVariablesAllpsidx;
0351     bindVariablesAllpsidx.extend("runnumber", typeid(int));
0352     bindVariablesAllpsidx["runnumber"].data<int>() = runnumber;
0353     allpsidxQuery->setCondition("RUN_NUMBER =:runnumber", bindVariablesAllpsidx);
0354     allpsidxQuery->defineOutput(allpsidxOutput);
0355     coral::ICursor& allpsidxCursor = allpsidxQuery->execute();
0356     while (allpsidxCursor.next()) {
0357       const coral::AttributeList& row = allpsidxCursor.currentRow();
0358       int psidx = row["psidx"].data<int>();
0359       prescidx.push_back(psidx);
0360     }
0361     delete allpsidxQuery;
0362     std::map<int, std::vector<unsigned int> > algoprescMap;
0363     std::map<int, std::vector<unsigned int> > techprescMap;
0364     std::vector<int>::iterator prescidxIt;
0365     std::vector<int>::iterator prescidxItBeg = prescidx.begin();
0366     std::vector<int>::iterator prescidxItEnd = prescidx.end();
0367     for (prescidxIt = prescidxItBeg; prescidxIt != prescidxItEnd; ++prescidxIt) {
0368       std::vector<unsigned int> algopres;
0369       algopres.reserve(lumi::N_TRGALGOBIT);
0370       std::vector<unsigned int> techpres;
0371       techpres.reserve(lumi::N_TRGTECHBIT);
0372       algoprescMap.insert(std::make_pair(*prescidxIt, algopres));
0373       techprescMap.insert(std::make_pair(*prescidxIt, techpres));
0374     }
0375     //
0376     //select lumi_section,prescale_index from cms_gt_mon.lumi_sections where run_number=:runnumber
0377     // {ls:prescale_index}
0378     //
0379     std::map<unsigned int, int> lsprescmap;
0380     coral::IQuery* lstoprescQuery = gtmonschemaHandle.newQuery();
0381     lstoprescQuery->addToTableList(lstablename);
0382     coral::AttributeList lstoprescOutput;
0383     lstoprescOutput.extend("lumisection", typeid(unsigned int));
0384     lstoprescOutput.extend("psidx", typeid(int));
0385     lstoprescQuery->addToOutputList("LUMI_SECTION", "lumisection");
0386     lstoprescQuery->addToOutputList("PRESCALE_INDEX", "psidx");
0387     coral::AttributeList bindVariablesLstopresc;
0388     bindVariablesLstopresc.extend("runnumber", typeid(int));
0389     bindVariablesLstopresc["runnumber"].data<int>() = runnumber;
0390     lstoprescQuery->setCondition("RUN_NUMBER =:runnumber", bindVariablesLstopresc);
0391     lstoprescQuery->defineOutput(lstoprescOutput);
0392     unsigned int lsprescount = 0;
0393     unsigned int lastpresc = 0;
0394     coral::ICursor& lstoprescCursor = lstoprescQuery->execute();
0395     while (lstoprescCursor.next()) {
0396       ++lsprescount;
0397       const coral::AttributeList& row = lstoprescCursor.currentRow();
0398       unsigned int lumisection = row["lumisection"].data<unsigned int>();
0399       while (lsprescount != lumisection) {
0400         std::cout << "PRESCALE_INDEX COUNT alert: found hole in LS range " << lsprescount << std::endl;
0401         std::cout << "     fill this prescale from last availabl prescale " << lastpresc << std::endl;
0402         unsigned int guesspsidx = lastpresc;
0403         lsprescmap.insert(std::make_pair(lsprescount, guesspsidx));
0404         ++lsprescount;
0405       }
0406       int psidx = row["psidx"].data<int>();
0407       lsprescmap.insert(std::make_pair(lumisection, psidx));
0408       lastpresc = psidx;
0409     }
0410     if (lsprescount == 0) {
0411       lstoprescCursor.close();
0412       delete lstoprescQuery;
0413       transaction.commit();
0414       throw lumi::Exception(
0415           std::string("requested run ") + runnumberstr + std::string(" doesn't exist for prescale_index"),
0416           "retrieveData",
0417           "TRG2DB");
0418     }
0419     delete lstoprescQuery;
0420 
0421     for (prescidxIt = prescidxItBeg; prescidxIt != prescidxItEnd; ++prescidxIt) {
0422       std::vector<unsigned int> algopres;
0423       algopres.reserve(lumi::N_TRGALGOBIT);
0424       std::vector<unsigned int> techpres;
0425       techpres.reserve(lumi::N_TRGTECHBIT);
0426       algoprescMap.insert(std::make_pair(*prescidxIt, algopres));
0427       techprescMap.insert(std::make_pair(*prescidxIt, techpres));
0428     }
0429     //prefill lsprescmap
0430     //transaction.commit();
0431     /**
0432        Part II
0433        query tables in schema cms_gt
0434     **/
0435     coral::ISchema& gtschemaHandle = trgsession->schema(gtschema);
0436     if (!gtschemaHandle.existsView(runtechviewname)) {
0437       throw lumi::Exception(std::string("non-existing view ") + runtechviewname, "str2int", "TRG2DB");
0438     }
0439     if (!gtschemaHandle.existsView(runalgoviewname)) {
0440       throw lumi::Exception(std::string("non-existing view ") + runalgoviewname, "str2int", "TRG2DB");
0441     }
0442     if (!gtschemaHandle.existsView(runprescalgoviewname)) {
0443       throw lumi::Exception(std::string("non-existing view ") + runprescalgoviewname, "str2int", "TRG2DB");
0444     }
0445     if (!gtschemaHandle.existsView(runpresctechviewname)) {
0446       throw lumi::Exception(std::string("non-existing view ") + runpresctechviewname, "str2int", "TRG2DB");
0447     }
0448     //
0449     //select algo_index,alias from cms_gt.gt_run_algo_view where runnumber=:runnumber //order by algo_index;
0450     //
0451     std::map<unsigned int, std::string> triggernamemap;
0452     coral::IQuery* QueryName = gtschemaHandle.newQuery();
0453     QueryName->addToTableList(runalgoviewname);
0454     coral::AttributeList qAlgoNameOutput;
0455     qAlgoNameOutput.extend("ALGO_INDEX", typeid(unsigned int));
0456     qAlgoNameOutput.extend("ALIAS", typeid(std::string));
0457     QueryName->addToOutputList("ALGO_INDEX");
0458     QueryName->addToOutputList("ALIAS");
0459     QueryName->setCondition("RUNNUMBER=:runnumber", bindVariableList);
0460     //QueryName->addToOrderList("algo_index");
0461     QueryName->defineOutput(qAlgoNameOutput);
0462     coral::ICursor& algonamecursor = QueryName->execute();
0463     while (algonamecursor.next()) {
0464       const coral::AttributeList& row = algonamecursor.currentRow();
0465       //row.toOutputStream( std::cout ) << std::endl;
0466       unsigned int algo_index = row["ALGO_INDEX"].data<unsigned int>();
0467       std::string algo_name = row["ALIAS"].data<std::string>();
0468       triggernamemap.insert(std::make_pair(algo_index, algo_name));
0469     }
0470     delete QueryName;
0471 
0472     //
0473     //select techtrig_index,name from cms_gt.gt_run_tech_view where runnumber=:runnumber //order by techtrig_index;
0474     //
0475     std::map<unsigned int, std::string> techtriggernamemap;
0476     coral::IQuery* QueryTechName = gtschemaHandle.newQuery();
0477     QueryTechName->addToTableList(runtechviewname);
0478     coral::AttributeList qTechNameOutput;
0479     qTechNameOutput.extend("TECHTRIG_INDEX", typeid(unsigned int));
0480     qTechNameOutput.extend("NAME", typeid(std::string));
0481     QueryTechName->addToOutputList("TECHTRIG_INDEX");
0482     QueryTechName->addToOutputList("NAME");
0483     QueryTechName->setCondition("RUNNUMBER=:runnumber", bindVariableList);
0484     //QueryTechName->addToOrderList("techtrig_index");
0485     QueryTechName->defineOutput(qTechNameOutput);
0486     coral::ICursor& technamecursor = QueryTechName->execute();
0487     while (technamecursor.next()) {
0488       const coral::AttributeList& row = technamecursor.currentRow();
0489       //row.toOutputStream( std::cout ) << std::endl;
0490       unsigned int tech_index = row["TECHTRIG_INDEX"].data<unsigned int>();
0491       std::string tech_name = row["NAME"].data<std::string>();
0492       techtriggernamemap.insert(std::make_pair(tech_index, tech_name));
0493     }
0494     delete QueryTechName;
0495     //
0496     //loop over all prescale_index
0497     //
0498     //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;
0499     // {prescale_index:[pres]}
0500     //
0501     std::vector<int>::iterator presIt;
0502     std::vector<int>::iterator presItBeg = prescidx.begin();
0503     std::vector<int>::iterator presItEnd = prescidx.end();
0504     for (presIt = presItBeg; presIt != presItEnd; ++presIt) {
0505       coral::IQuery* QueryAlgoPresc = gtschemaHandle.newQuery();
0506       QueryAlgoPresc->addToTableList(runprescalgoviewname);
0507       coral::AttributeList qAlgoPrescOutput;
0508       std::string algoprescBase("PRESCALE_FACTOR_ALGO_");
0509       for (unsigned int bitidx = 0; bitidx < lumi::N_TRGALGOBIT; ++bitidx) {
0510         std::string algopresc = algoprescBase + int2str(bitidx, 3);
0511         qAlgoPrescOutput.extend(algopresc, typeid(unsigned int));
0512       }
0513       for (unsigned int bitidx = 0; bitidx < lumi::N_TRGALGOBIT; ++bitidx) {
0514         std::string algopresc = algoprescBase + int2str(bitidx, 3);
0515         QueryAlgoPresc->addToOutputList(algopresc);
0516       }
0517       coral::AttributeList PrescbindVariable;
0518       PrescbindVariable.extend("runnumber", typeid(int));
0519       PrescbindVariable.extend("prescaleindex", typeid(int));
0520       PrescbindVariable["runnumber"].data<int>() = runnumber;
0521       PrescbindVariable["prescaleindex"].data<int>() = *presIt;
0522       QueryAlgoPresc->setCondition("RUNNR=:runnumber AND PRESCALE_INDEX=:prescaleindex", PrescbindVariable);
0523       QueryAlgoPresc->defineOutput(qAlgoPrescOutput);
0524       coral::ICursor& algopresccursor = QueryAlgoPresc->execute();
0525       while (algopresccursor.next()) {
0526         const coral::AttributeList& row = algopresccursor.currentRow();
0527         for (unsigned int bitidx = 0; bitidx < lumi::N_TRGALGOBIT; ++bitidx) {
0528           std::string algopresc = algoprescBase + int2str(bitidx, 3);
0529           algoprescMap[*presIt].push_back(row[algopresc].data<unsigned int>());
0530         }
0531       }
0532       delete QueryAlgoPresc;
0533     }
0534     //
0535     //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;
0536     //
0537     for (presIt = presItBeg; presIt != presItEnd; ++presIt) {
0538       coral::IQuery* QueryTechPresc = gtschemaHandle.newQuery();
0539       QueryTechPresc->addToTableList(runpresctechviewname);
0540       coral::AttributeList qTechPrescOutput;
0541       std::string techprescBase("PRESCALE_FACTOR_TT_");
0542       for (unsigned int bitidx = 0; bitidx < lumi::N_TRGTECHBIT; ++bitidx) {
0543         std::string techpresc = techprescBase + this->int2str(bitidx, 3);
0544         qTechPrescOutput.extend(techpresc, typeid(unsigned int));
0545       }
0546       for (unsigned int bitidx = 0; bitidx < lumi::N_TRGTECHBIT; ++bitidx) {
0547         std::string techpresc = techprescBase + int2str(bitidx, 3);
0548         QueryTechPresc->addToOutputList(techpresc);
0549       }
0550       coral::AttributeList TechPrescbindVariable;
0551       TechPrescbindVariable.extend("runnumber", typeid(int));
0552       TechPrescbindVariable.extend("prescaleindex", typeid(int));
0553       TechPrescbindVariable["runnumber"].data<int>() = runnumber;
0554       TechPrescbindVariable["prescaleindex"].data<int>() = *presIt;
0555       QueryTechPresc->setCondition("RUNNR=:runnumber AND PRESCALE_INDEX=:prescaleindex", TechPrescbindVariable);
0556       QueryTechPresc->defineOutput(qTechPrescOutput);
0557       coral::ICursor& techpresccursor = QueryTechPresc->execute();
0558       while (techpresccursor.next()) {
0559         const coral::AttributeList& row = techpresccursor.currentRow();
0560         //row.toOutputStream( std::cout ) << std::endl;
0561         for (unsigned int bitidx = 0; bitidx < lumi::N_TRGTECHBIT; ++bitidx) {
0562           std::string techpresc = techprescBase + int2str(bitidx, 3);
0563           techprescMap[*presIt].push_back(row[techpresc].data<unsigned int>());
0564         }
0565       }
0566       delete QueryTechPresc;
0567     }
0568     transaction.commit();
0569     delete trgsession;
0570 
0571     std::map<unsigned int, int>::iterator lsprescmapIt;
0572     std::map<unsigned int, int>::iterator lsprescmapItBeg = lsprescmap.begin();
0573     std::map<unsigned int, int>::iterator lsprescmapItEnd = lsprescmap.end();
0574     for (lsprescmapIt = lsprescmapItBeg; lsprescmapIt != lsprescmapItEnd; ++lsprescmapIt) {
0575       unsigned int ls = lsprescmapIt->first;
0576       int preidx = lsprescmapIt->second;
0577       algoprescale.insert(std::make_pair(ls, algoprescMap[preidx]));
0578       techprescale.insert(std::make_pair(ls, techprescMap[preidx]));
0579     }
0580     algoprescMap.clear();
0581     techprescMap.clear();
0582     //
0583     //reprocess Algo name result filling unallocated trigger bit with string "False"
0584     //
0585     for (size_t algoidx = 0; algoidx < lumi::N_TRGALGOBIT; ++algoidx) {
0586       std::map<unsigned int, std::string>::iterator pos = triggernamemap.find(algoidx);
0587       if (pos != triggernamemap.end()) {
0588         algonames.push_back(pos->second);
0589       } else {
0590         algonames.push_back("False");
0591       }
0592     }
0593     //
0594     //reprocess Tech name result filling unallocated trigger bit with string "False"
0595     //
0596     std::stringstream ss;
0597     for (size_t techidx = 0; techidx < lumi::N_TRGTECHBIT; ++techidx) {
0598       ss << techidx;
0599       technames.push_back(ss.str());
0600       ss.str("");  //clear the string buffer after usage
0601     }
0602     //
0603     //cross check result size
0604     //
0605     if (algonames.size() != lumi::N_TRGALGOBIT || technames.size() != lumi::N_TRGTECHBIT) {
0606       throw lumi::Exception("wrong number of bits", "retrieveData", "TRG2DB");
0607     }
0608     //if(algoprescale.size()!=lumi::N_TRGALGOBIT || techprescale.size()!=lumi::N_TRGTECHBIT){
0609     //  throw lumi::Exception("wrong number of prescale","retrieveData","TRG2DB");
0610     //}
0611     if (deadtimeresult.size() != deadfracresult.size() || deadtimeresult.size() != algocount.size() ||
0612         deadtimeresult.size() != techcount.size() || deadtimeresult.size() != algoprescale.size() ||
0613         deadtimeresult.size() != techprescale.size()) {
0614       //throw lumi::Exception("inconsistent number of LS","retrieveData","TRG2DB");
0615       std::cout
0616           << "[WARNING] inconsistent number of LS of deadtimecounter,deadfrac,algo,tech,prescalealgo,prescaletech "
0617           << deadtimeresult.size() << " " << deadfracresult.size() << " " << algocount.size() << " " << techcount.size()
0618           << " " << algoprescale.size() << " " << techprescale.size() << std::endl;
0619       TRGScalers2DB::TriggerDeadCountResult::iterator dIt;
0620       TRGScalers2DB::TriggerDeadCountResult::iterator dBeg = deadtimeresult.begin();
0621       TRGScalers2DB::TriggerDeadCountResult::iterator dEnd = deadtimeresult.end();
0622       unsigned int dcnt = 0;
0623       for (dIt = dBeg; dIt != dEnd; ++dIt) {
0624         try {
0625           deadfracresult.at(dcnt);
0626         } catch (std::out_of_range& er) {
0627           std::cout << "[WARNING] filling FAKE deadfrac=0.0 at LS " << dcnt << std::endl;
0628           deadfracresult[dcnt] = 0.0;
0629         }
0630         try {
0631           algocount.at(dcnt);
0632         } catch (std::out_of_range& er) {
0633           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT, 0);
0634           std::cout << "[WARNING] filling FAKE algocount at LS " << dcnt << std::endl;
0635           algocount[dcnt] = tmpzero;
0636         }
0637         try {
0638           techcount.at(dcnt);
0639         } catch (std::out_of_range& er) {
0640           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT, 0);
0641           std::cout << "[WARNING] filling FAKE techcount at LS " << dcnt << std::endl;
0642           techcount[dcnt] = tmpzero;
0643         }
0644         if (algoprescale.find(dcnt + 1) == algoprescale.end()) {
0645           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT, 1);
0646           std::cout << "[WARNING] filling FAKE 1 algoprescale at LS " << dcnt + 1 << std::endl;
0647           algoprescale[dcnt + 1] = tmpzero;
0648         }
0649         if (techprescale.find(dcnt + 1) == techprescale.end()) {
0650           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT, 1);
0651           std::cout << "[WARNING] filling FAKE 1 techprescale at LS " << dcnt + 1 << std::endl;
0652           techprescale[dcnt + 1] = tmpzero;
0653         }
0654         ++dcnt;
0655       }
0656     }
0657     //
0658     //write data into lumi db
0659     //
0660     unsigned long long trgdataid = 0;
0661     coral::ISessionProxy* lumisession = svc->connect(m_dest, coral::Update);
0662     coral::ITypeConverter& lumitpc = lumisession->typeConverter();
0663     lumitpc.setCppTypeForSqlType("unsigned int", "NUMBER(7)");
0664     lumitpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0665     lumitpc.setCppTypeForSqlType("unsigned long long", "NUMBER(20)");
0666     try {
0667       if (m_mode == "loadoldschema") {
0668         std::cout << "writing trg data to old trg table " << std::endl;
0669         writeTrgData(lumisession,
0670                      runnumber,
0671                      m_source,
0672                      deadtimeresult.begin(),
0673                      deadtimeresult.end(),
0674                      deadfracresult,
0675                      algonames,
0676                      technames,
0677                      algocount,
0678                      techcount,
0679                      algoprescale,
0680                      techprescale,
0681                      COMMITLSINTERVAL);
0682         std::cout << "done" << std::endl;
0683       }
0684       std::cout << "writing trg data to new lstrg table " << std::endl;
0685       trgdataid = writeTrgDataToSchema2(lumisession,
0686                                         runnumber,
0687                                         m_source,
0688                                         deadtimeresult.begin(),
0689                                         deadtimeresult.end(),
0690                                         deadfracresult,
0691                                         algonames,
0692                                         technames,
0693                                         algocount,
0694                                         techcount,
0695                                         algoprescale,
0696                                         techprescale,
0697                                         COMMITLSTRGINTERVAL);
0698       std::cout << "done" << std::endl;
0699       delete lumisession;
0700       delete svc;
0701     } catch (const coral::Exception& er) {
0702       std::cout << "database error " << er.what() << std::endl;
0703       lumisession->transaction().rollback();
0704       delete lumisession;
0705       delete svc;
0706       throw er;
0707     }
0708     return trgdataid;
0709   }
0710   void TRGScalers2DB::writeTrgData(coral::ISessionProxy* lumisession,
0711                                    unsigned int runnumber,
0712                                    const std::string& source,
0713                                    TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesBeg,
0714                                    TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesEnd,
0715                                    TRGScalers2DB::TriggerDeadFracResult& deadfracs,
0716                                    TRGScalers2DB::TriggerNameResult_Algo& algonames,
0717                                    TRGScalers2DB::TriggerNameResult_Tech& technames,
0718                                    TRGScalers2DB::TriggerCountResult_Algo& algocounts,
0719                                    TRGScalers2DB::TriggerCountResult_Tech& techcounts,
0720                                    TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
0721                                    TRGScalers2DB::PrescaleResult_Tech& prescaletech,
0722                                    unsigned int commitintv) {
0723     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
0724     //unsigned int totalcmsls=deadtimes.size();
0725     unsigned int totalcmsls = std::distance(deadtimesBeg, deadtimesEnd);
0726     std::cout << "inserting totalcmsls " << totalcmsls << std::endl;
0727     std::map<unsigned long long, std::vector<unsigned long long> > idallocationtable;
0728     std::cout << "\t allocating total ids " << totalcmsls * lumi::N_TRGBIT << std::endl;
0729     lumisession->transaction().start(false);
0730     lumi::idDealer idg(lumisession->nominalSchema());
0731     unsigned long long trgID = idg.generateNextIDForTable(LumiNames::trgTableName(), totalcmsls * lumi::N_TRGBIT) -
0732                                totalcmsls * lumi::N_TRGBIT;
0733     //lumisession->transaction().commit();
0734     unsigned int trglscount = 0;
0735     for (deadIt = deadtimesBeg; deadIt != deadtimesEnd; ++deadIt, ++trglscount) {
0736       std::vector<unsigned long long> bitvec;
0737       bitvec.reserve(lumi::N_TRGBIT);
0738       const BITCOUNT& algoinbits = algocounts[trglscount];
0739       const BITCOUNT& techinbits = techcounts[trglscount];
0740       BITCOUNT::const_iterator algoBitIt;
0741       BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
0742       BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
0743       for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgID) {
0744         bitvec.push_back(trgID);
0745       }
0746       BITCOUNT::const_iterator techBitIt;
0747       BITCOUNT::const_iterator techBitBeg = techinbits.begin();
0748       BITCOUNT::const_iterator techBitEnd = techinbits.end();
0749       for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgID) {
0750         bitvec.push_back(trgID);
0751       }
0752       idallocationtable.insert(std::make_pair(trglscount, bitvec));
0753     }
0754     std::cout << "\t all ids allocated" << std::endl;
0755     coral::AttributeList trgData;
0756     trgData.extend<unsigned long long>("TRG_ID");
0757     trgData.extend<unsigned int>("RUNNUM");
0758     trgData.extend<unsigned int>("CMSLSNUM");
0759     trgData.extend<unsigned int>("BITNUM");
0760     trgData.extend<std::string>("BITNAME");
0761     trgData.extend<unsigned int>("TRGCOUNT");
0762     trgData.extend<unsigned long long>("DEADTIME");
0763     trgData.extend<float>("DEADFRAC");
0764     trgData.extend<unsigned int>("PRESCALE");
0765 
0766     unsigned long long& trg_id = trgData["TRG_ID"].data<unsigned long long>();
0767     unsigned int& trgrunnum = trgData["RUNNUM"].data<unsigned int>();
0768     unsigned int& cmslsnum = trgData["CMSLSNUM"].data<unsigned int>();
0769     unsigned int& bitnum = trgData["BITNUM"].data<unsigned int>();
0770     std::string& bitname = trgData["BITNAME"].data<std::string>();
0771     unsigned int& count = trgData["TRGCOUNT"].data<unsigned int>();
0772     unsigned long long& deadtime = trgData["DEADTIME"].data<unsigned long long>();
0773     float& deadfrac = trgData["DEADFRAC"].data<float>();
0774     unsigned int& prescale = trgData["PRESCALE"].data<unsigned int>();
0775 
0776     trglscount = 0;
0777     coral::IBulkOperation* trgInserter = nullptr;
0778     unsigned int comittedls = 0;
0779     for (deadIt = deadtimesBeg; deadIt != deadtimesEnd; ++deadIt, ++trglscount) {
0780       unsigned int cmslscount = trglscount + 1;
0781       float dfra = deadfracs[trglscount];
0782       const BITCOUNT& algoinbits = algocounts[trglscount];
0783       const BITCOUNT& techinbits = techcounts[trglscount];
0784       unsigned int trgbitcount = 0;
0785       BITCOUNT::const_iterator algoBitIt;
0786       BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
0787       BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
0788       if (!lumisession->transaction().isActive()) {
0789         lumisession->transaction().start(false);
0790         coral::ITable& trgtable = lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
0791         trgInserter = trgtable.dataEditor().bulkInsert(trgData, 2048);
0792       } else {
0793         if (deadIt == deadtimesBeg) {
0794           coral::ITable& trgtable = lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
0795           trgInserter = trgtable.dataEditor().bulkInsert(trgData, 2048);
0796         }
0797       }
0798       for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgbitcount) {
0799         trg_id = idallocationtable[trglscount].at(trgbitcount);
0800         deadtime = *deadIt;
0801         deadfrac = dfra;
0802         trgrunnum = runnumber;
0803         cmslsnum = cmslscount;
0804         bitnum = trgbitcount;
0805         bitname = algonames[trgbitcount];
0806         count = *algoBitIt;
0807         prescale = prescalealgo[cmslscount].at(trgbitcount);
0808         //std::cout<<"cmslsnum "<<cmslsnum<<" bitnum "<<bitnum<<" bitname "<<bitname<<" prescale "<< prescale<<" count "<<count<<std::endl;
0809         trgInserter->processNextIteration();
0810       }
0811       BITCOUNT::const_iterator techBitIt;
0812       BITCOUNT::const_iterator techBitBeg = techinbits.begin();
0813       BITCOUNT::const_iterator techBitEnd = techinbits.end();
0814       for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgbitcount) {
0815         trg_id = idallocationtable[trglscount].at(trgbitcount);
0816         deadtime = *deadIt;
0817         deadfrac = dfra;
0818         trgrunnum = runnumber;
0819         cmslsnum = cmslscount;
0820         bitnum = trgbitcount;
0821         bitname = technames[trgbitcount - lumi::N_TRGALGOBIT];
0822         count = *techBitIt;
0823         prescale = prescaletech[cmslsnum][trgbitcount - lumi::N_TRGALGOBIT];
0824         trgInserter->processNextIteration();
0825       }
0826       trgInserter->flush();
0827       ++comittedls;
0828       if (comittedls == commitintv) {
0829         std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0830         delete trgInserter;
0831         trgInserter = nullptr;
0832         lumisession->transaction().commit();
0833         comittedls = 0;
0834         std::cout << "\t committed " << std::endl;
0835       } else if (trglscount == (totalcmsls - 1)) {
0836         std::cout << "\t committing at the end" << std::endl;
0837         delete trgInserter;
0838         trgInserter = nullptr;
0839         lumisession->transaction().commit();
0840         std::cout << "\t done" << std::endl;
0841       }
0842     }
0843   }
0844   unsigned long long TRGScalers2DB::writeTrgDataToSchema2(coral::ISessionProxy* lumisession,
0845                                                           unsigned int irunnumber,
0846                                                           const std::string& source,
0847                                                           TriggerDeadCountResult::iterator deadtimesBeg,
0848                                                           TriggerDeadCountResult::iterator deadtimesEnd,
0849                                                           TRGScalers2DB::TriggerDeadFracResult& deadfracs,
0850                                                           TRGScalers2DB::TriggerNameResult_Algo& algonames,
0851                                                           TRGScalers2DB::TriggerNameResult_Tech& technames,
0852                                                           TRGScalers2DB::TriggerCountResult_Algo& algocounts,
0853                                                           TRGScalers2DB::TriggerCountResult_Tech& techcounts,
0854                                                           TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
0855                                                           TRGScalers2DB::PrescaleResult_Tech& prescaletech,
0856                                                           unsigned int commitintv) {
0857     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
0858     unsigned int totalcmsls = std::distance(deadtimesBeg, deadtimesEnd);
0859     std::cout << "inserting totalcmsls " << totalcmsls << std::endl;
0860     coral::AttributeList lstrgData;
0861     lstrgData.extend<unsigned long long>("DATA_ID");
0862     lstrgData.extend<unsigned int>("RUNNUM");
0863     lstrgData.extend<unsigned int>("CMSLSNUM");
0864     lstrgData.extend<unsigned long long>("DEADTIMECOUNT");
0865     lstrgData.extend<unsigned int>("BITZEROCOUNT");
0866     lstrgData.extend<unsigned int>("BITZEROPRESCALE");
0867     lstrgData.extend<float>("DEADFRAC");
0868     lstrgData.extend<coral::Blob>("PRESCALEBLOB");
0869     lstrgData.extend<coral::Blob>("TRGCOUNTBLOB");
0870 
0871     unsigned long long& data_id = lstrgData["DATA_ID"].data<unsigned long long>();
0872     unsigned int& trgrunnum = lstrgData["RUNNUM"].data<unsigned int>();
0873     unsigned int& cmslsnum = lstrgData["CMSLSNUM"].data<unsigned int>();
0874     unsigned long long& deadtime = lstrgData["DEADTIMECOUNT"].data<unsigned long long>();
0875     unsigned int& bitzerocount = lstrgData["BITZEROCOUNT"].data<unsigned int>();
0876     unsigned int& bitzeroprescale = lstrgData["BITZEROPRESCALE"].data<unsigned int>();
0877     float& deadfrac = lstrgData["DEADFRAC"].data<float>();
0878     coral::Blob& prescaleblob = lstrgData["PRESCALEBLOB"].data<coral::Blob>();
0879     coral::Blob& trgcountblob = lstrgData["TRGCOUNTBLOB"].data<coral::Blob>();
0880 
0881     unsigned long long branch_id = 3;
0882     std::string branch_name("DATA");
0883     lumi::RevisionDML revisionDML;
0884     lumi::RevisionDML::TrgEntry trgrundata;
0885     std::stringstream op;
0886     op << irunnumber;
0887     std::string runnumberStr = op.str();
0888     lumisession->transaction().start(false);
0889     trgrundata.entry_name = runnumberStr;
0890     trgrundata.source = source;
0891     trgrundata.runnumber = irunnumber;
0892     std::string bitnames;
0893     TriggerNameResult_Algo::iterator bitnameIt;
0894     TriggerNameResult_Algo::iterator bitnameItBeg = algonames.begin();
0895     TriggerNameResult_Algo::iterator bitnameItEnd = algonames.end();
0896     for (bitnameIt = bitnameItBeg; bitnameIt != bitnameItEnd; ++bitnameIt) {
0897       if (bitnameIt != bitnameItBeg) {
0898         bitnames += std::string(",");
0899       }
0900       bitnames += *bitnameIt;
0901     }
0902     TriggerNameResult_Tech::iterator techbitnameIt;
0903     TriggerNameResult_Tech::iterator techbitnameItBeg = technames.begin();
0904     TriggerNameResult_Tech::iterator techbitnameItEnd = technames.end();
0905     for (techbitnameIt = techbitnameItBeg; techbitnameIt != techbitnameItEnd; ++techbitnameIt) {
0906       bitnames += std::string(",");
0907       bitnames += *techbitnameIt;
0908     }
0909     std::cout << "\tbitnames " << bitnames << std::endl;
0910     trgrundata.bitzeroname = technames[4];
0911     trgrundata.bitnames = bitnames;
0912     trgrundata.entry_id = revisionDML.getEntryInBranchByName(
0913         lumisession->nominalSchema(), lumi::LumiNames::trgdataTableName(), runnumberStr, branch_name);
0914     if (trgrundata.entry_id == 0) {
0915       revisionDML.bookNewEntry(lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata);
0916       std::cout << "trgrundata revision_id " << trgrundata.revision_id << " entry_id " << trgrundata.entry_id
0917                 << " data_id " << trgrundata.data_id << std::endl;
0918       revisionDML.addEntry(
0919           lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata, branch_id, branch_name);
0920     } else {
0921       revisionDML.bookNewRevision(lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata);
0922       std::cout << "trgrundata revision_id " << trgrundata.revision_id << " entry_id " << trgrundata.entry_id
0923                 << " data_id " << trgrundata.data_id << std::endl;
0924       revisionDML.addRevision(
0925           lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata, branch_id, branch_name);
0926     }
0927     std::cout << "inserting trgrundata " << std::endl;
0928     revisionDML.insertTrgRunData(lumisession->nominalSchema(), trgrundata);
0929     std::cout << "inserting lstrg data" << std::endl;
0930 
0931     unsigned int trglscount = 0;
0932     // trglscount=0;
0933     coral::IBulkOperation* lstrgInserter = nullptr;
0934     unsigned int comittedls = 0;
0935     for (deadIt = deadtimesBeg; deadIt != deadtimesEnd; ++deadIt, ++trglscount) {
0936       unsigned int cmslscount = trglscount + 1;
0937       if (!lumisession->transaction().isActive()) {
0938         lumisession->transaction().start(false);
0939         coral::ITable& lstrgtable = lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
0940         lstrgInserter = lstrgtable.dataEditor().bulkInsert(lstrgData, 2048);
0941       } else {
0942         if (deadIt == deadtimesBeg) {
0943           coral::ITable& lstrgtable = lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
0944           lstrgInserter = lstrgtable.dataEditor().bulkInsert(lstrgData, 2048);
0945         }
0946       }
0947       data_id = trgrundata.data_id;
0948       trgrunnum = irunnumber;
0949       cmslsnum = cmslscount;
0950       deadtime = *deadIt;
0951       deadfrac = deadfracs[trglscount];
0952       //bitzerocount = algocounts[trglscount][0];//use algobit_0
0953       //bitzeroprescale = prescalealgo[cmslsnum][0];
0954       bitzerocount = techcounts[trglscount][4];  //use techbit_4
0955       bitzeroprescale = prescaletech[cmslsnum][4];
0956       std::vector<unsigned int> fullprescales;
0957       fullprescales.reserve(prescalealgo[cmslsnum].size() + prescaletech[cmslsnum].size());
0958       fullprescales.insert(fullprescales.end(), prescalealgo[cmslsnum].begin(), prescalealgo[cmslsnum].end());
0959       fullprescales.insert(fullprescales.end(), prescaletech[cmslsnum].begin(), prescaletech[cmslsnum].end());
0960 
0961       prescaleblob.resize(sizeof(unsigned int) * (fullprescales.size()));
0962       void* prescaleblob_StartAddress = prescaleblob.startingAddress();
0963       std::memmove(prescaleblob_StartAddress, &fullprescales[0], sizeof(unsigned int) * (fullprescales.size()));
0964 
0965       std::vector<unsigned int> fullcounts;
0966       fullcounts.reserve(algocounts[trglscount].size() + techcounts[trglscount].size());
0967       fullcounts.insert(fullcounts.end(), algocounts[trglscount].begin(), algocounts[trglscount].end());
0968       fullcounts.insert(fullcounts.end(), techcounts[trglscount].begin(), techcounts[trglscount].end());
0969       trgcountblob.resize(sizeof(unsigned int) * (fullcounts.size()));
0970       void* trgcountblob_StartAddress = trgcountblob.startingAddress();
0971       std::memmove(trgcountblob_StartAddress, &fullcounts[0], sizeof(unsigned int) * (fullcounts.size()));
0972 
0973       lstrgInserter->processNextIteration();
0974       lstrgInserter->flush();
0975       ++comittedls;
0976       if (comittedls == commitintv) {
0977         std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0978         delete lstrgInserter;
0979         lstrgInserter = nullptr;
0980         lumisession->transaction().commit();
0981         comittedls = 0;
0982         std::cout << "\t committed " << std::endl;
0983       } else if (trglscount == (totalcmsls - 1)) {
0984         std::cout << "\t committing at the end" << std::endl;
0985         delete lstrgInserter;
0986         lstrgInserter = nullptr;
0987         lumisession->transaction().commit();
0988         std::cout << "\t done" << std::endl;
0989       }
0990     }
0991 
0992     return trgrundata.data_id;
0993   }
0994   const std::string TRGScalers2DB::dataType() const { return "TRG"; }
0995   const std::string TRGScalers2DB::sourceType() const { return "DB"; }
0996   //utilities
0997   std::string TRGScalers2DB::int2str(unsigned int t, unsigned int width) {
0998     std::stringstream ss;
0999     ss.width(width);
1000     ss.fill('0');
1001     ss << t;
1002     return ss.str();
1003   }
1004   unsigned int TRGScalers2DB::str2int(const std::string& s) {
1005     std::istringstream myStream(s);
1006     unsigned int i;
1007     if (myStream >> i) {
1008       return i;
1009     } else {
1010       throw lumi::Exception(std::string("str2int error"), "str2int", "TRGScalers2DB");
1011     }
1012   }
1013   TRGScalers2DB::~TRGScalers2DB() {}
1014 }  // namespace lumi
1015 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
1016 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory, lumi::TRGScalers2DB, "TRGScalers2DB");
1017 #endif