Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-02-14 03:16:55

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         if (dcnt >= deadfracresult.size()) {
0625           std::cout << "[WARNING] filling FAKE deadfrac=0.0 at LS " << dcnt << std::endl;
0626           deadfracresult.push_back(0.0);
0627         }
0628         if (dcnt >= algocount.size()) {
0629           std::cout << "[WARNING] filling FAKE algocount at LS " << dcnt << std::endl;
0630           algocount.emplace_back(lumi::N_TRGALGOBIT, 0);
0631         }
0632         if (dcnt >= techcount.size()) {
0633           std::cout << "[WARNING] filling FAKE techcount at LS " << dcnt << std::endl;
0634           techcount.emplace_back(lumi::N_TRGTECHBIT, 0);
0635         }
0636         if (algoprescale.find(dcnt + 1) == algoprescale.end()) {
0637           std::cout << "[WARNING] filling FAKE 1 algoprescale at LS " << dcnt + 1 << std::endl;
0638           algoprescale[dcnt + 1] = std::vector<unsigned int>(lumi::N_TRGALGOBIT, 1);
0639         }
0640         if (techprescale.find(dcnt + 1) == techprescale.end()) {
0641           std::cout << "[WARNING] filling FAKE 1 techprescale at LS " << dcnt + 1 << std::endl;
0642           techprescale[dcnt + 1] = std::vector<unsigned int>(lumi::N_TRGTECHBIT, 1);
0643         }
0644         ++dcnt;
0645       }
0646     }
0647     //
0648     //write data into lumi db
0649     //
0650     unsigned long long trgdataid = 0;
0651     coral::ISessionProxy* lumisession = svc->connect(m_dest, coral::Update);
0652     coral::ITypeConverter& lumitpc = lumisession->typeConverter();
0653     lumitpc.setCppTypeForSqlType("unsigned int", "NUMBER(7)");
0654     lumitpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0655     lumitpc.setCppTypeForSqlType("unsigned long long", "NUMBER(20)");
0656     try {
0657       if (m_mode == "loadoldschema") {
0658         std::cout << "writing trg data to old trg table " << std::endl;
0659         writeTrgData(lumisession,
0660                      runnumber,
0661                      m_source,
0662                      deadtimeresult.begin(),
0663                      deadtimeresult.end(),
0664                      deadfracresult,
0665                      algonames,
0666                      technames,
0667                      algocount,
0668                      techcount,
0669                      algoprescale,
0670                      techprescale,
0671                      COMMITLSINTERVAL);
0672         std::cout << "done" << std::endl;
0673       }
0674       std::cout << "writing trg data to new lstrg table " << std::endl;
0675       trgdataid = writeTrgDataToSchema2(lumisession,
0676                                         runnumber,
0677                                         m_source,
0678                                         deadtimeresult.begin(),
0679                                         deadtimeresult.end(),
0680                                         deadfracresult,
0681                                         algonames,
0682                                         technames,
0683                                         algocount,
0684                                         techcount,
0685                                         algoprescale,
0686                                         techprescale,
0687                                         COMMITLSTRGINTERVAL);
0688       std::cout << "done" << std::endl;
0689       delete lumisession;
0690       delete svc;
0691     } catch (const coral::Exception& er) {
0692       std::cout << "database error " << er.what() << std::endl;
0693       lumisession->transaction().rollback();
0694       delete lumisession;
0695       delete svc;
0696       throw er;
0697     }
0698     return trgdataid;
0699   }
0700   void TRGScalers2DB::writeTrgData(coral::ISessionProxy* lumisession,
0701                                    unsigned int runnumber,
0702                                    const std::string& source,
0703                                    TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesBeg,
0704                                    TRGScalers2DB::TriggerDeadCountResult::iterator deadtimesEnd,
0705                                    TRGScalers2DB::TriggerDeadFracResult& deadfracs,
0706                                    TRGScalers2DB::TriggerNameResult_Algo& algonames,
0707                                    TRGScalers2DB::TriggerNameResult_Tech& technames,
0708                                    TRGScalers2DB::TriggerCountResult_Algo& algocounts,
0709                                    TRGScalers2DB::TriggerCountResult_Tech& techcounts,
0710                                    TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
0711                                    TRGScalers2DB::PrescaleResult_Tech& prescaletech,
0712                                    unsigned int commitintv) {
0713     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
0714     //unsigned int totalcmsls=deadtimes.size();
0715     unsigned int totalcmsls = std::distance(deadtimesBeg, deadtimesEnd);
0716     std::cout << "inserting totalcmsls " << totalcmsls << std::endl;
0717     std::map<unsigned long long, std::vector<unsigned long long> > idallocationtable;
0718     std::cout << "\t allocating total ids " << totalcmsls * lumi::N_TRGBIT << std::endl;
0719     lumisession->transaction().start(false);
0720     lumi::idDealer idg(lumisession->nominalSchema());
0721     unsigned long long trgID = idg.generateNextIDForTable(LumiNames::trgTableName(), totalcmsls * lumi::N_TRGBIT) -
0722                                totalcmsls * lumi::N_TRGBIT;
0723     //lumisession->transaction().commit();
0724     unsigned int trglscount = 0;
0725     for (deadIt = deadtimesBeg; deadIt != deadtimesEnd; ++deadIt, ++trglscount) {
0726       std::vector<unsigned long long> bitvec;
0727       bitvec.reserve(lumi::N_TRGBIT);
0728       const BITCOUNT& algoinbits = algocounts[trglscount];
0729       const BITCOUNT& techinbits = techcounts[trglscount];
0730       BITCOUNT::const_iterator algoBitIt;
0731       BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
0732       BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
0733       for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgID) {
0734         bitvec.push_back(trgID);
0735       }
0736       BITCOUNT::const_iterator techBitIt;
0737       BITCOUNT::const_iterator techBitBeg = techinbits.begin();
0738       BITCOUNT::const_iterator techBitEnd = techinbits.end();
0739       for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgID) {
0740         bitvec.push_back(trgID);
0741       }
0742       idallocationtable.insert(std::make_pair(trglscount, bitvec));
0743     }
0744     std::cout << "\t all ids allocated" << std::endl;
0745     coral::AttributeList trgData;
0746     trgData.extend<unsigned long long>("TRG_ID");
0747     trgData.extend<unsigned int>("RUNNUM");
0748     trgData.extend<unsigned int>("CMSLSNUM");
0749     trgData.extend<unsigned int>("BITNUM");
0750     trgData.extend<std::string>("BITNAME");
0751     trgData.extend<unsigned int>("TRGCOUNT");
0752     trgData.extend<unsigned long long>("DEADTIME");
0753     trgData.extend<float>("DEADFRAC");
0754     trgData.extend<unsigned int>("PRESCALE");
0755 
0756     unsigned long long& trg_id = trgData["TRG_ID"].data<unsigned long long>();
0757     unsigned int& trgrunnum = trgData["RUNNUM"].data<unsigned int>();
0758     unsigned int& cmslsnum = trgData["CMSLSNUM"].data<unsigned int>();
0759     unsigned int& bitnum = trgData["BITNUM"].data<unsigned int>();
0760     std::string& bitname = trgData["BITNAME"].data<std::string>();
0761     unsigned int& count = trgData["TRGCOUNT"].data<unsigned int>();
0762     unsigned long long& deadtime = trgData["DEADTIME"].data<unsigned long long>();
0763     float& deadfrac = trgData["DEADFRAC"].data<float>();
0764     unsigned int& prescale = trgData["PRESCALE"].data<unsigned int>();
0765 
0766     trglscount = 0;
0767     coral::IBulkOperation* trgInserter = nullptr;
0768     unsigned int comittedls = 0;
0769     for (deadIt = deadtimesBeg; deadIt != deadtimesEnd; ++deadIt, ++trglscount) {
0770       unsigned int cmslscount = trglscount + 1;
0771       float dfra = deadfracs[trglscount];
0772       const BITCOUNT& algoinbits = algocounts[trglscount];
0773       const BITCOUNT& techinbits = techcounts[trglscount];
0774       unsigned int trgbitcount = 0;
0775       BITCOUNT::const_iterator algoBitIt;
0776       BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
0777       BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
0778       if (!lumisession->transaction().isActive()) {
0779         lumisession->transaction().start(false);
0780         coral::ITable& trgtable = lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
0781         trgInserter = trgtable.dataEditor().bulkInsert(trgData, 2048);
0782       } else {
0783         if (deadIt == deadtimesBeg) {
0784           coral::ITable& trgtable = lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
0785           trgInserter = trgtable.dataEditor().bulkInsert(trgData, 2048);
0786         }
0787       }
0788       for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgbitcount) {
0789         trg_id = idallocationtable[trglscount].at(trgbitcount);
0790         deadtime = *deadIt;
0791         deadfrac = dfra;
0792         trgrunnum = runnumber;
0793         cmslsnum = cmslscount;
0794         bitnum = trgbitcount;
0795         bitname = algonames[trgbitcount];
0796         count = *algoBitIt;
0797         prescale = prescalealgo[cmslscount].at(trgbitcount);
0798         //std::cout<<"cmslsnum "<<cmslsnum<<" bitnum "<<bitnum<<" bitname "<<bitname<<" prescale "<< prescale<<" count "<<count<<std::endl;
0799         trgInserter->processNextIteration();
0800       }
0801       BITCOUNT::const_iterator techBitIt;
0802       BITCOUNT::const_iterator techBitBeg = techinbits.begin();
0803       BITCOUNT::const_iterator techBitEnd = techinbits.end();
0804       for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgbitcount) {
0805         trg_id = idallocationtable[trglscount].at(trgbitcount);
0806         deadtime = *deadIt;
0807         deadfrac = dfra;
0808         trgrunnum = runnumber;
0809         cmslsnum = cmslscount;
0810         bitnum = trgbitcount;
0811         bitname = technames[trgbitcount - lumi::N_TRGALGOBIT];
0812         count = *techBitIt;
0813         prescale = prescaletech[cmslsnum][trgbitcount - lumi::N_TRGALGOBIT];
0814         trgInserter->processNextIteration();
0815       }
0816       trgInserter->flush();
0817       ++comittedls;
0818       if (comittedls == commitintv) {
0819         std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0820         delete trgInserter;
0821         trgInserter = nullptr;
0822         lumisession->transaction().commit();
0823         comittedls = 0;
0824         std::cout << "\t committed " << std::endl;
0825       } else if (trglscount == (totalcmsls - 1)) {
0826         std::cout << "\t committing at the end" << std::endl;
0827         delete trgInserter;
0828         trgInserter = nullptr;
0829         lumisession->transaction().commit();
0830         std::cout << "\t done" << std::endl;
0831       }
0832     }
0833   }
0834   unsigned long long TRGScalers2DB::writeTrgDataToSchema2(coral::ISessionProxy* lumisession,
0835                                                           unsigned int irunnumber,
0836                                                           const std::string& source,
0837                                                           TriggerDeadCountResult::iterator deadtimesBeg,
0838                                                           TriggerDeadCountResult::iterator deadtimesEnd,
0839                                                           TRGScalers2DB::TriggerDeadFracResult& deadfracs,
0840                                                           TRGScalers2DB::TriggerNameResult_Algo& algonames,
0841                                                           TRGScalers2DB::TriggerNameResult_Tech& technames,
0842                                                           TRGScalers2DB::TriggerCountResult_Algo& algocounts,
0843                                                           TRGScalers2DB::TriggerCountResult_Tech& techcounts,
0844                                                           TRGScalers2DB::PrescaleResult_Algo& prescalealgo,
0845                                                           TRGScalers2DB::PrescaleResult_Tech& prescaletech,
0846                                                           unsigned int commitintv) {
0847     TRGScalers2DB::TriggerDeadCountResult::iterator deadIt;
0848     unsigned int totalcmsls = std::distance(deadtimesBeg, deadtimesEnd);
0849     std::cout << "inserting totalcmsls " << totalcmsls << std::endl;
0850     coral::AttributeList lstrgData;
0851     lstrgData.extend<unsigned long long>("DATA_ID");
0852     lstrgData.extend<unsigned int>("RUNNUM");
0853     lstrgData.extend<unsigned int>("CMSLSNUM");
0854     lstrgData.extend<unsigned long long>("DEADTIMECOUNT");
0855     lstrgData.extend<unsigned int>("BITZEROCOUNT");
0856     lstrgData.extend<unsigned int>("BITZEROPRESCALE");
0857     lstrgData.extend<float>("DEADFRAC");
0858     lstrgData.extend<coral::Blob>("PRESCALEBLOB");
0859     lstrgData.extend<coral::Blob>("TRGCOUNTBLOB");
0860 
0861     unsigned long long& data_id = lstrgData["DATA_ID"].data<unsigned long long>();
0862     unsigned int& trgrunnum = lstrgData["RUNNUM"].data<unsigned int>();
0863     unsigned int& cmslsnum = lstrgData["CMSLSNUM"].data<unsigned int>();
0864     unsigned long long& deadtime = lstrgData["DEADTIMECOUNT"].data<unsigned long long>();
0865     unsigned int& bitzerocount = lstrgData["BITZEROCOUNT"].data<unsigned int>();
0866     unsigned int& bitzeroprescale = lstrgData["BITZEROPRESCALE"].data<unsigned int>();
0867     float& deadfrac = lstrgData["DEADFRAC"].data<float>();
0868     coral::Blob& prescaleblob = lstrgData["PRESCALEBLOB"].data<coral::Blob>();
0869     coral::Blob& trgcountblob = lstrgData["TRGCOUNTBLOB"].data<coral::Blob>();
0870 
0871     unsigned long long branch_id = 3;
0872     std::string branch_name("DATA");
0873     lumi::RevisionDML revisionDML;
0874     lumi::RevisionDML::TrgEntry trgrundata;
0875     std::stringstream op;
0876     op << irunnumber;
0877     std::string runnumberStr = op.str();
0878     lumisession->transaction().start(false);
0879     trgrundata.entry_name = runnumberStr;
0880     trgrundata.source = source;
0881     trgrundata.runnumber = irunnumber;
0882     std::string bitnames;
0883     TriggerNameResult_Algo::iterator bitnameIt;
0884     TriggerNameResult_Algo::iterator bitnameItBeg = algonames.begin();
0885     TriggerNameResult_Algo::iterator bitnameItEnd = algonames.end();
0886     for (bitnameIt = bitnameItBeg; bitnameIt != bitnameItEnd; ++bitnameIt) {
0887       if (bitnameIt != bitnameItBeg) {
0888         bitnames += std::string(",");
0889       }
0890       bitnames += *bitnameIt;
0891     }
0892     TriggerNameResult_Tech::iterator techbitnameIt;
0893     TriggerNameResult_Tech::iterator techbitnameItBeg = technames.begin();
0894     TriggerNameResult_Tech::iterator techbitnameItEnd = technames.end();
0895     for (techbitnameIt = techbitnameItBeg; techbitnameIt != techbitnameItEnd; ++techbitnameIt) {
0896       bitnames += std::string(",");
0897       bitnames += *techbitnameIt;
0898     }
0899     std::cout << "\tbitnames " << bitnames << std::endl;
0900     trgrundata.bitzeroname = technames[4];
0901     trgrundata.bitnames = bitnames;
0902     trgrundata.entry_id = revisionDML.getEntryInBranchByName(
0903         lumisession->nominalSchema(), lumi::LumiNames::trgdataTableName(), runnumberStr, branch_name);
0904     if (trgrundata.entry_id == 0) {
0905       revisionDML.bookNewEntry(lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata);
0906       std::cout << "trgrundata revision_id " << trgrundata.revision_id << " entry_id " << trgrundata.entry_id
0907                 << " data_id " << trgrundata.data_id << std::endl;
0908       revisionDML.addEntry(
0909           lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata, branch_id, branch_name);
0910     } else {
0911       revisionDML.bookNewRevision(lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata);
0912       std::cout << "trgrundata revision_id " << trgrundata.revision_id << " entry_id " << trgrundata.entry_id
0913                 << " data_id " << trgrundata.data_id << std::endl;
0914       revisionDML.addRevision(
0915           lumisession->nominalSchema(), LumiNames::trgdataTableName(), trgrundata, branch_id, branch_name);
0916     }
0917     std::cout << "inserting trgrundata " << std::endl;
0918     revisionDML.insertTrgRunData(lumisession->nominalSchema(), trgrundata);
0919     std::cout << "inserting lstrg data" << std::endl;
0920 
0921     unsigned int trglscount = 0;
0922     // trglscount=0;
0923     coral::IBulkOperation* lstrgInserter = nullptr;
0924     unsigned int comittedls = 0;
0925     for (deadIt = deadtimesBeg; deadIt != deadtimesEnd; ++deadIt, ++trglscount) {
0926       unsigned int cmslscount = trglscount + 1;
0927       if (!lumisession->transaction().isActive()) {
0928         lumisession->transaction().start(false);
0929         coral::ITable& lstrgtable = lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
0930         lstrgInserter = lstrgtable.dataEditor().bulkInsert(lstrgData, 2048);
0931       } else {
0932         if (deadIt == deadtimesBeg) {
0933           coral::ITable& lstrgtable = lumisession->nominalSchema().tableHandle(LumiNames::lstrgTableName());
0934           lstrgInserter = lstrgtable.dataEditor().bulkInsert(lstrgData, 2048);
0935         }
0936       }
0937       data_id = trgrundata.data_id;
0938       trgrunnum = irunnumber;
0939       cmslsnum = cmslscount;
0940       deadtime = *deadIt;
0941       deadfrac = deadfracs[trglscount];
0942       //bitzerocount = algocounts[trglscount][0];//use algobit_0
0943       //bitzeroprescale = prescalealgo[cmslsnum][0];
0944       bitzerocount = techcounts[trglscount][4];  //use techbit_4
0945       bitzeroprescale = prescaletech[cmslsnum][4];
0946       std::vector<unsigned int> fullprescales;
0947       fullprescales.reserve(prescalealgo[cmslsnum].size() + prescaletech[cmslsnum].size());
0948       fullprescales.insert(fullprescales.end(), prescalealgo[cmslsnum].begin(), prescalealgo[cmslsnum].end());
0949       fullprescales.insert(fullprescales.end(), prescaletech[cmslsnum].begin(), prescaletech[cmslsnum].end());
0950 
0951       prescaleblob.resize(sizeof(unsigned int) * (fullprescales.size()));
0952       void* prescaleblob_StartAddress = prescaleblob.startingAddress();
0953       std::memmove(prescaleblob_StartAddress, &fullprescales[0], sizeof(unsigned int) * (fullprescales.size()));
0954 
0955       std::vector<unsigned int> fullcounts;
0956       fullcounts.reserve(algocounts[trglscount].size() + techcounts[trglscount].size());
0957       fullcounts.insert(fullcounts.end(), algocounts[trglscount].begin(), algocounts[trglscount].end());
0958       fullcounts.insert(fullcounts.end(), techcounts[trglscount].begin(), techcounts[trglscount].end());
0959       trgcountblob.resize(sizeof(unsigned int) * (fullcounts.size()));
0960       void* trgcountblob_StartAddress = trgcountblob.startingAddress();
0961       std::memmove(trgcountblob_StartAddress, &fullcounts[0], sizeof(unsigned int) * (fullcounts.size()));
0962 
0963       lstrgInserter->processNextIteration();
0964       lstrgInserter->flush();
0965       ++comittedls;
0966       if (comittedls == commitintv) {
0967         std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0968         delete lstrgInserter;
0969         lstrgInserter = nullptr;
0970         lumisession->transaction().commit();
0971         comittedls = 0;
0972         std::cout << "\t committed " << std::endl;
0973       } else if (trglscount == (totalcmsls - 1)) {
0974         std::cout << "\t committing at the end" << std::endl;
0975         delete lstrgInserter;
0976         lstrgInserter = nullptr;
0977         lumisession->transaction().commit();
0978         std::cout << "\t done" << std::endl;
0979       }
0980     }
0981 
0982     return trgrundata.data_id;
0983   }
0984   const std::string TRGScalers2DB::dataType() const { return "TRG"; }
0985   const std::string TRGScalers2DB::sourceType() const { return "DB"; }
0986   //utilities
0987   std::string TRGScalers2DB::int2str(unsigned int t, unsigned int width) {
0988     std::stringstream ss;
0989     ss.width(width);
0990     ss.fill('0');
0991     ss << t;
0992     return ss.str();
0993   }
0994   unsigned int TRGScalers2DB::str2int(const std::string& s) {
0995     std::istringstream myStream(s);
0996     unsigned int i;
0997     if (myStream >> i) {
0998       return i;
0999     } else {
1000       throw lumi::Exception(std::string("str2int error"), "str2int", "TRGScalers2DB");
1001     }
1002   }
1003   TRGScalers2DB::~TRGScalers2DB() {}
1004 }  // namespace lumi
1005 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
1006 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory, lumi::TRGScalers2DB, "TRGScalers2DB");
1007 #endif