Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef RecoLuminosity_LumiProducer_TRGWBM2DB_h
0002 #define RecoLuminosity_LumiProducer_TRGWBM2DB_h
0003 #include "CoralBase/AttributeList.h"
0004 #include "CoralBase/Attribute.h"
0005 #include "CoralBase/AttributeSpecification.h"
0006 #include "CoralBase/Exception.h"
0007 #include "RelationalAccess/ConnectionService.h"
0008 #include "RelationalAccess/ISessionProxy.h"
0009 #include "RelationalAccess/ITransaction.h"
0010 #include "RelationalAccess/ITypeConverter.h"
0011 #include "RelationalAccess/IQuery.h"
0012 #include "RelationalAccess/ICursor.h"
0013 #include "RelationalAccess/ISchema.h"
0014 #include "RelationalAccess/IView.h"
0015 #include "RelationalAccess/ITable.h"
0016 #include "RelationalAccess/ITableDataEditor.h"
0017 #include "RelationalAccess/IBulkOperation.h"
0018 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
0019 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
0020 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
0021 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
0022 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
0023 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
0024 #include <iostream>
0025 #include <sstream>
0026 #include <map>
0027 namespace lumi {
0028   class TRGWBM2DB : public DataPipe {
0029   public:
0030     const static unsigned int COMMITLSINTERVAL = 20;  //commit interval in LS,totalrow=nsl*192
0031     explicit TRGWBM2DB(const std::string& dest);
0032     unsigned long long retrieveData(unsigned int runnumber) override;
0033     const std::string dataType() const override;
0034     const std::string sourceType() const override;
0035     ~TRGWBM2DB() override;
0036 
0037   private:
0038     std::string int2str(unsigned int t, unsigned int width);
0039     unsigned int str2int(const std::string& s);
0040 
0041   private:
0042     //per run information
0043     typedef std::vector<std::string> TriggerNameResult_Algo;
0044     typedef std::vector<std::string> TriggerNameResult_Tech;
0045     typedef std::vector<unsigned int> PrescaleResult_Algo;
0046     typedef std::vector<unsigned int> PrescaleResult_Tech;
0047     //per lumisection information
0048     typedef unsigned long long DEADCOUNT;
0049     typedef std::vector<DEADCOUNT> TriggerDeadCountResult;
0050     typedef std::vector<unsigned int> BITCOUNT;
0051     typedef std::vector<BITCOUNT> TriggerCountResult_Algo;
0052     typedef std::vector<BITCOUNT> TriggerCountResult_Tech;
0053   };  //cl TRGWBM2DB
0054       //
0055       //implementation
0056       //
0057   TRGWBM2DB::TRGWBM2DB(const std::string& dest) : DataPipe(dest) {}
0058   unsigned long long TRGWBM2DB::retrieveData(unsigned int runnumber) {
0059     std::string runnumberstr = int2str(runnumber, 6);
0060     //query source GT database
0061     coral::ConnectionService* svc = new coral::ConnectionService;
0062     lumi::DBConfig dbconf(*svc);
0063     if (!m_authpath.empty()) {
0064       dbconf.setAuthentication(m_authpath);
0065     }
0066     //std::cout<<"m_source "<<m_source<<std::endl;
0067     coral::ISessionProxy* trgsession = svc->connect(m_source, coral::ReadOnly);
0068     coral::ITypeConverter& tpc = trgsession->typeConverter();
0069 
0070     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(7)");
0071     tpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0072     tpc.setCppTypeForSqlType("unsigned long long", "NUMBER(20)");
0073 
0074     coral::AttributeList bindVariableList;
0075     bindVariableList.extend("runnumber", typeid(unsigned int));
0076     bindVariableList["runnumber"].data<unsigned int>() = runnumber;
0077     std::string wbmschema("CMS_WBM");
0078     std::string algoname("LEVEL1_TRIGGER_ALGO_CONDITIONS");
0079     std::string techname("LEVEL1_TRIGGER_TECH_CONDITIONS");
0080     std::string deadname("LEVEL1_TRIGGER_CONDITIONS");
0081 
0082     std::string gtschema("CMS_GT");
0083     std::string runtechviewname("GT_RUN_TECH_VIEW");
0084     std::string runalgoviewname("GT_RUN_ALGO_VIEW");
0085     std::string runprescalgoviewname("GT_RUN_PRESC_ALGO_VIEW");
0086     std::string runpresctechviewname("GT_RUN_PRESC_TECH_VIEW");
0087 
0088     //data exchange format
0089     lumi::TRGWBM2DB::BITCOUNT mybitcount_algo;
0090     mybitcount_algo.reserve(lumi::N_TRGALGOBIT);
0091     lumi::TRGWBM2DB::BITCOUNT mybitcount_tech;
0092     mybitcount_tech.reserve(lumi::N_TRGTECHBIT);
0093     lumi::TRGWBM2DB::TriggerNameResult_Algo algonames;
0094     algonames.reserve(lumi::N_TRGALGOBIT);
0095     lumi::TRGWBM2DB::TriggerNameResult_Tech technames;
0096     technames.reserve(lumi::N_TRGTECHBIT);
0097     lumi::TRGWBM2DB::PrescaleResult_Algo algoprescale;
0098     algoprescale.reserve(lumi::N_TRGALGOBIT);
0099     lumi::TRGWBM2DB::PrescaleResult_Tech techprescale;
0100     techprescale.reserve(lumi::N_TRGTECHBIT);
0101     lumi::TRGWBM2DB::TriggerCountResult_Algo algocount;
0102     algocount.reserve(400);
0103     lumi::TRGWBM2DB::TriggerCountResult_Tech techcount;
0104     techcount.reserve(400);
0105     lumi::TRGWBM2DB::TriggerDeadCountResult deadtimeresult;
0106     deadtimeresult.reserve(400);
0107     coral::ITransaction& transaction = trgsession->transaction();
0108     transaction.start(true);
0109     //uncomment if you want to see all the visible views
0110     /**
0111        transaction.start(true); //true means readonly transaction
0112        std::cout<<"schema name "<<session->schema(gtmonschema).schemaName()<<std::endl;
0113        std::set<std::string> listofviews;
0114        listofviews=session->schema(gtmonschema).listViews();
0115        for( std::set<std::string>::iterator it=listofviews.begin(); it!=listofviews.end();++it ){
0116        std::cout<<"view: "<<*it<<std::endl;
0117        } 
0118        std::cout<<"schema name "<<session->schema(gtschema).schemaName()<<std::endl;
0119        listofviews.clear();
0120        listofviews=session->schema(gtschema).listViews();
0121        for( std::set<std::string>::iterator it=listofviews.begin(); it!=listofviews.end();++it ){
0122        std::cout<<"view: "<<*it<<std::endl;
0123        } 
0124        std::cout<<"commit transaction"<<std::endl;
0125        transaction.commit();
0126     **/
0127     /**
0128        Part I
0129        query tables in schema cms_wbm
0130     **/
0131 
0132     coral::ISchema& wbmschemaHandle = trgsession->schema(wbmschema);
0133     if (!wbmschemaHandle.existsTable(algoname)) {
0134       throw lumi::Exception(std::string("non-existing table ") + algoname, "retrieveData", "TRGWBM2DB");
0135     }
0136     if (!wbmschemaHandle.existsTable(techname)) {
0137       throw lumi::Exception(std::string("non-existing table ") + techname, "retrieveData", "TRGWBM2DB");
0138     }
0139     if (!wbmschemaHandle.existsTable(deadname)) {
0140       throw lumi::Exception(std::string("non-existing table ") + deadname, "retrieveData", "TRGWBM2DB");
0141     }
0142     //
0143     //select LUMISEGMENTNR,GTALGOCOUNTS,BIT from cms_wbm.LEVEL1_TRIGGER_ALGO_CONDITIONS where RUNNUMBER=133881 order by LUMISEGMENTNR,BIT;
0144     //note: LUMISEGMENTNR count from 1
0145     //note: BIT count from 0-127
0146     //
0147     coral::IQuery* Queryalgo = wbmschemaHandle.newQuery();
0148     Queryalgo->addToTableList(algoname);
0149     coral::AttributeList qalgoOutput;
0150     qalgoOutput.extend("counts", typeid(unsigned int));
0151     qalgoOutput.extend("lsnr", typeid(unsigned int));
0152     qalgoOutput.extend("algobit", typeid(unsigned int));
0153     Queryalgo->addToOutputList("GTALGOCOUNTS", "counts");
0154     Queryalgo->addToOutputList("LUMISEGMENTNR", "lsnr");
0155     Queryalgo->addToOutputList("BIT", "algobit");
0156     Queryalgo->setCondition("RUNNUMBER =:runnumber", bindVariableList);
0157     Queryalgo->addToOrderList("LUMISEGMENTNR");
0158     Queryalgo->addToOrderList("BIT");
0159     Queryalgo->defineOutput(qalgoOutput);
0160     coral::ICursor& c = Queryalgo->execute();
0161     unsigned int s = 0;
0162     while (c.next()) {
0163       const coral::AttributeList& row = c.currentRow();
0164       //row.toOutputStream( std::cout ) << std::endl;
0165       unsigned int lsnr = row["lsnr"].data<unsigned int>();
0166       unsigned int count = row["counts"].data<unsigned int>();
0167       mybitcount_algo.push_back(count);
0168       unsigned int algobit = row["algobit"].data<unsigned int>();
0169       if (algobit == (lumi::N_TRGALGOBIT - 1)) {
0170         ++s;
0171         while (s != lsnr) {
0172           std::cout << "ALGO COUNT alert: found hole in LS range" << std::endl;
0173           std::cout << "    fill all algocount 0 for LS " << s << std::endl;
0174           std::vector<unsigned int> tmpzero(lumi::N_TRGALGOBIT, 0);
0175           algocount.push_back(tmpzero);
0176           ++s;
0177         }
0178         algocount.push_back(mybitcount_algo);
0179         mybitcount_algo.clear();
0180       }
0181     }
0182     if (s == 0) {
0183       c.close();
0184       delete Queryalgo;
0185       transaction.commit();
0186       throw lumi::Exception(std::string("requested run ") + runnumberstr + std::string(" doesn't exist for algocounts"),
0187                             "retrieveData",
0188                             "TRGWBM2DB");
0189     }
0190     delete Queryalgo;
0191     transaction.commit();
0192     //std::cout<<"read algo counts"<<std::endl;
0193     //
0194     //select LUMISEGMENTNR,GTTECHCOUNTS,BIT from cms_wbm.LEVEL1_TRIGGER_TECH_CONDITIONS where RUNNUMBER=133881 order by LUMISEGMENTNR,BIT;
0195     //note: LUMISEGMENTNR count from 1
0196     //note: BIT count from 0-63
0197     //
0198     transaction.start(true);
0199     coral::IQuery* Querytech = wbmschemaHandle.newQuery();
0200     Querytech->addToTableList(techname);
0201     coral::AttributeList qtechOutput;
0202     qtechOutput.extend("counts", typeid(unsigned int));
0203     qtechOutput.extend("lsnr", typeid(unsigned int));
0204     qtechOutput.extend("techbit", typeid(unsigned int));
0205     Querytech->addToOutputList("GTTECHCOUNTS", "counts");
0206     Querytech->addToOutputList("LUMISEGMENTNR", "lsnr");
0207     Querytech->addToOutputList("BIT", "techbit");
0208     Querytech->setCondition("RUNNUMBER=:runnumber", bindVariableList);
0209     Querytech->addToOrderList("LUMISEGMENTNR");
0210     Querytech->addToOrderList("BIT");
0211     Querytech->defineOutput(qtechOutput);
0212     coral::ICursor& techcursor = Querytech->execute();
0213 
0214     s = 0;
0215     while (techcursor.next()) {
0216       const coral::AttributeList& row = techcursor.currentRow();
0217       //row.toOutputStream( std::cout ) << std::endl;
0218       unsigned int lsnr = row["lsnr"].data<unsigned int>();
0219       unsigned int count = row["counts"].data<unsigned int>();
0220       unsigned int techbit = row["techbit"].data<unsigned int>();
0221       mybitcount_tech.push_back(count);
0222       if (techbit == (lumi::N_TRGTECHBIT - 1)) {
0223         ++s;
0224         while (s != lsnr) {
0225           std::cout << "TECH COUNT alert: found hole in LS range" << std::endl;
0226           std::cout << "     fill all techcount with 0 for LS " << s << std::endl;
0227           std::vector<unsigned int> tmpzero(lumi::N_TRGTECHBIT, 0);
0228           techcount.push_back(tmpzero);
0229           ++s;
0230         }
0231         techcount.push_back(mybitcount_tech);
0232         mybitcount_tech.clear();
0233       }
0234     }
0235     if (s == 0) {
0236       techcursor.close();
0237       delete Querytech;
0238       transaction.commit();
0239       throw lumi::Exception(std::string("requested run ") + runnumberstr + std::string(" doesn't exist for techcounts"),
0240                             "retrieveData",
0241                             "TRGWBM2DB");
0242     }
0243     delete Querytech;
0244     transaction.commit();
0245     //std::cout<<"read tech counts"<<std::endl;
0246     //
0247     //select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=133881 order by LUMISEGMENTNR;
0248     //Note: LUMISEGMENTNR counts from 1
0249     //
0250     transaction.start(true);
0251     coral::IQuery* Querydead = wbmschemaHandle.newQuery();
0252     Querydead->addToTableList(deadname);
0253     coral::AttributeList qdeadOutput;
0254     qdeadOutput.extend("counts", typeid(unsigned int));
0255     qdeadOutput.extend("lsnr", typeid(unsigned int));
0256     Querydead->addToOutputList("DEADTIMEBEAMACTIVE", "counts");
0257     Querydead->addToOutputList("LUMISEGMENTNR", "lsnr");
0258     coral::AttributeList bindVariablesDead;
0259     bindVariablesDead.extend("runnumber", typeid(int));
0260     bindVariablesDead["runnumber"].data<int>() = runnumber;
0261     Querydead->setCondition("RUNNUMBER=:runnumber", bindVariablesDead);
0262     Querydead->addToOrderList("LUMISEGMENTNR");
0263     Querydead->defineOutput(qdeadOutput);
0264     coral::ICursor& deadcursor = Querydead->execute();
0265     s = 0;
0266     while (deadcursor.next()) {
0267       const coral::AttributeList& row = deadcursor.currentRow();
0268       //row.toOutputStream( std::cout ) << std::endl;
0269       ++s;
0270       unsigned int lsnr = row["lsnr"].data<unsigned int>();
0271       while (s != lsnr) {
0272         std::cout << "DEADTIME alert: found hole in LS range" << std::endl;
0273         std::cout << "         fill deadtimebeamactive 0 for LS " << s << std::endl;
0274         deadtimeresult.push_back(0);
0275         ++s;
0276       }
0277       unsigned int count = row["counts"].data<unsigned int>();
0278       deadtimeresult.push_back(count);
0279     }
0280     //std::cout<<"deadtimeresult raw "<<deadtimeresult.size()<<std::endl;
0281     if (s == 0) {
0282       deadcursor.close();
0283       delete Querydead;
0284       transaction.commit();
0285       throw lumi::Exception(std::string("requested run ") + runnumberstr + std::string(" doesn't exist for deadcounts"),
0286                             "retrieveData",
0287                             "TRGWBM2DB");
0288       return 0;
0289     }
0290     delete Querydead;
0291     transaction.commit();
0292     //std::cout<<"read dead counts"<<std::endl;
0293     /**
0294        Part II
0295        query tables in schema cms_gt
0296     **/
0297     transaction.start(true);
0298     coral::ISchema& gtschemaHandle = trgsession->schema(gtschema);
0299     if (!gtschemaHandle.existsView(runtechviewname)) {
0300       throw lumi::Exception(std::string("non-existing view ") + runtechviewname, "str2int", "TRGWBM2DB");
0301     }
0302     if (!gtschemaHandle.existsView(runalgoviewname)) {
0303       throw lumi::Exception(std::string("non-existing view ") + runalgoviewname, "str2int", "TRGWBM2DB");
0304     }
0305     if (!gtschemaHandle.existsView(runprescalgoviewname)) {
0306       throw lumi::Exception(std::string("non-existing view ") + runprescalgoviewname, "str2int", "TRGWBM2DB");
0307     }
0308     if (!gtschemaHandle.existsView(runpresctechviewname)) {
0309       throw lumi::Exception(std::string("non-existing view ") + runpresctechviewname, "str2int", "TRGWBM2DB");
0310     }
0311     //
0312     //select algo_index,alias from cms_gt.gt_run_algo_view where runnumber=:runnumber order by algo_index;
0313     //
0314     std::map<unsigned int, std::string> triggernamemap;
0315     coral::IQuery* QueryName = gtschemaHandle.newQuery();
0316     QueryName->addToTableList(runalgoviewname);
0317     coral::AttributeList qAlgoNameOutput;
0318     qAlgoNameOutput.extend("algo_index", typeid(unsigned int));
0319     qAlgoNameOutput.extend("alias", typeid(std::string));
0320     QueryName->addToOutputList("algo_index");
0321     QueryName->addToOutputList("alias");
0322     QueryName->setCondition("runnumber =:runnumber", bindVariableList);
0323     QueryName->addToOrderList("algo_index");
0324     QueryName->defineOutput(qAlgoNameOutput);
0325     coral::ICursor& algonamecursor = QueryName->execute();
0326     while (algonamecursor.next()) {
0327       const coral::AttributeList& row = algonamecursor.currentRow();
0328       //row.toOutputStream( std::cout ) << std::endl;
0329       unsigned int algo_index = row["algo_index"].data<unsigned int>();
0330       std::string algo_name = row["alias"].data<std::string>();
0331       triggernamemap.insert(std::make_pair(algo_index, algo_name));
0332     }
0333     delete QueryName;
0334     //std::cout<<"read alias"<<std::endl;
0335     transaction.commit();
0336 
0337     //
0338     //select techtrig_index,name from cms_gt.gt_run_tech_view where runnumber=:runnumber order by techtrig_index;
0339     //
0340     transaction.start(true);
0341     std::map<unsigned int, std::string> techtriggernamemap;
0342     coral::IQuery* QueryTechName = gtschemaHandle.newQuery();
0343     QueryTechName->addToTableList(runtechviewname);
0344     coral::AttributeList qTechNameOutput;
0345     qTechNameOutput.extend("techtrig_index", typeid(unsigned int));
0346     qTechNameOutput.extend("name", typeid(std::string));
0347     QueryTechName->addToOutputList("techtrig_index");
0348     QueryTechName->addToOutputList("name");
0349     QueryTechName->setCondition("runnumber =:runnumber", bindVariableList);
0350     QueryTechName->addToOrderList("techtrig_index");
0351     QueryTechName->defineOutput(qTechNameOutput);
0352     coral::ICursor& technamecursor = QueryTechName->execute();
0353     while (technamecursor.next()) {
0354       const coral::AttributeList& row = technamecursor.currentRow();
0355       //row.toOutputStream( std::cout ) << std::endl;
0356       unsigned int tech_index = row["techtrig_index"].data<unsigned int>();
0357       std::string tech_name = row["name"].data<std::string>();
0358       techtriggernamemap.insert(std::make_pair(tech_index, tech_name));
0359     }
0360     delete QueryTechName;
0361     transaction.commit();
0362     //
0363     //select prescale_factor_algo_000,prescale_factor_algo_001..._127 from cms_gt.gt_run_presc_algo_view where runr=:runnumber and prescale_index=0;
0364     //
0365     transaction.start(true);
0366     coral::IQuery* QueryAlgoPresc = gtschemaHandle.newQuery();
0367     QueryAlgoPresc->addToTableList(runprescalgoviewname);
0368     coral::AttributeList qAlgoPrescOutput;
0369     std::string algoprescBase("PRESCALE_FACTOR_ALGO_");
0370     for (unsigned int bitidx = 0; bitidx < lumi::N_TRGALGOBIT; ++bitidx) {
0371       std::string algopresc = algoprescBase + int2str(bitidx, 3);
0372       qAlgoPrescOutput.extend(algopresc, typeid(unsigned int));
0373     }
0374     for (unsigned int bitidx = 0; bitidx < lumi::N_TRGALGOBIT; ++bitidx) {
0375       std::string algopresc = algoprescBase + int2str(bitidx, 3);
0376       QueryAlgoPresc->addToOutputList(algopresc);
0377     }
0378     coral::AttributeList PrescbindVariable;
0379     PrescbindVariable.extend("runnumber", typeid(int));
0380     PrescbindVariable.extend("prescaleindex", typeid(int));
0381     PrescbindVariable["runnumber"].data<int>() = runnumber;
0382     PrescbindVariable["prescaleindex"].data<int>() = 0;
0383     QueryAlgoPresc->setCondition("runnr =:runnumber AND prescale_index =:prescaleindex", PrescbindVariable);
0384     QueryAlgoPresc->defineOutput(qAlgoPrescOutput);
0385     coral::ICursor& algopresccursor = QueryAlgoPresc->execute();
0386     while (algopresccursor.next()) {
0387       const coral::AttributeList& row = algopresccursor.currentRow();
0388       //row.toOutputStream( std::cout ) << std::endl;
0389       for (unsigned int bitidx = 0; bitidx < 128; ++bitidx) {
0390         std::string algopresc = algoprescBase + int2str(bitidx, 3);
0391         algoprescale.push_back(row[algopresc].data<unsigned int>());
0392       }
0393     }
0394     delete QueryAlgoPresc;
0395     transaction.commit();
0396 
0397     //
0398     //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;
0399     //
0400     transaction.start(true);
0401     coral::IQuery* QueryTechPresc = gtschemaHandle.newQuery();
0402     QueryTechPresc->addToTableList(runpresctechviewname);
0403     coral::AttributeList qTechPrescOutput;
0404     std::string techprescBase("PRESCALE_FACTOR_TT_");
0405     for (unsigned int bitidx = 0; bitidx < lumi::N_TRGTECHBIT; ++bitidx) {
0406       std::string techpresc = techprescBase + this->int2str(bitidx, 3);
0407       qTechPrescOutput.extend(techpresc, typeid(unsigned int));
0408     }
0409     for (unsigned int bitidx = 0; bitidx < lumi::N_TRGTECHBIT; ++bitidx) {
0410       std::string techpresc = techprescBase + int2str(bitidx, 3);
0411       QueryTechPresc->addToOutputList(techpresc);
0412     }
0413     coral::AttributeList TechPrescbindVariable;
0414     TechPrescbindVariable.extend("runnumber", typeid(int));
0415     TechPrescbindVariable.extend("prescaleindex", typeid(int));
0416     TechPrescbindVariable["runnumber"].data<int>() = runnumber;
0417     TechPrescbindVariable["prescaleindex"].data<int>() = 0;
0418     QueryTechPresc->setCondition("runnr =:runnumber AND prescale_index =:prescaleindex", TechPrescbindVariable);
0419     QueryTechPresc->defineOutput(qTechPrescOutput);
0420     coral::ICursor& techpresccursor = QueryTechPresc->execute();
0421     while (techpresccursor.next()) {
0422       const coral::AttributeList& row = techpresccursor.currentRow();
0423       //row.toOutputStream( std::cout ) << std::endl;
0424       for (unsigned int bitidx = 0; bitidx < lumi::N_TRGTECHBIT; ++bitidx) {
0425         std::string techpresc = techprescBase + int2str(bitidx, 3);
0426         techprescale.push_back(row[techpresc].data<unsigned int>());
0427       }
0428     }
0429     delete QueryTechPresc;
0430     transaction.commit();
0431     transaction.commit();
0432     //std::cout<<"tech prescale ok"<<std::endl;
0433     delete trgsession;
0434 
0435     //
0436     //reprocess Algo name result filling unallocated trigger bit with string "False"
0437     //
0438     for (size_t algoidx = 0; algoidx < lumi::N_TRGALGOBIT; ++algoidx) {
0439       std::map<unsigned int, std::string>::iterator pos = triggernamemap.find(algoidx);
0440       if (pos != triggernamemap.end()) {
0441         algonames.push_back(pos->second);
0442       } else {
0443         algonames.push_back("False");
0444       }
0445     }
0446     //
0447     //reprocess Tech name result filling unallocated trigger bit with string "False"
0448     //
0449     std::stringstream ss;
0450     for (size_t techidx = 0; techidx < lumi::N_TRGTECHBIT; ++techidx) {
0451       ss << techidx;
0452       technames.push_back(ss.str());
0453       ss.str("");  //clear the string buffer after usage
0454     }
0455     //
0456     //cross check result size
0457     //
0458     if (algonames.size() != lumi::N_TRGALGOBIT || technames.size() != lumi::N_TRGTECHBIT) {
0459       throw lumi::Exception("wrong number of bits", "retrieveData", "TRGWBM2DB");
0460     }
0461     if (algoprescale.size() != lumi::N_TRGALGOBIT || techprescale.size() != lumi::N_TRGTECHBIT) {
0462       throw lumi::Exception("wrong number of prescale", "retrieveData", "TRGWBM2DB");
0463     }
0464     if (deadtimeresult.size() != algocount.size() || deadtimeresult.size() != techcount.size()) {
0465       throw lumi::Exception("inconsistent number of LS", "retrieveData", "TRGWBM2DB");
0466     }
0467     //
0468     //write data into lumi db
0469     //
0470     coral::ISessionProxy* lumisession = svc->connect(m_dest, coral::Update);
0471     coral::ITypeConverter& lumitpc = lumisession->typeConverter();
0472     lumitpc.setCppTypeForSqlType("unsigned int", "NUMBER(7)");
0473     lumitpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0474     lumitpc.setCppTypeForSqlType("unsigned long long", "NUMBER(20)");
0475 
0476     TriggerDeadCountResult::const_iterator deadIt;
0477     TriggerDeadCountResult::const_iterator deadBeg = deadtimeresult.begin();
0478     TriggerDeadCountResult::const_iterator deadEnd = deadtimeresult.end();
0479 
0480     unsigned int totalcmsls = deadtimeresult.size();
0481     std::cout << "inserting totalcmsls " << totalcmsls << std::endl;
0482     std::map<unsigned long long, std::vector<unsigned long long> > idallocationtable;
0483     try {
0484       std::cout << "\t allocating total ids " << totalcmsls * lumi::N_TRGBIT << std::endl;
0485       lumisession->transaction().start(false);
0486       lumi::idDealer idg(lumisession->nominalSchema());
0487       unsigned long long trgID = idg.generateNextIDForTable(LumiNames::trgTableName(), totalcmsls * lumi::N_TRGBIT) -
0488                                  totalcmsls * lumi::N_TRGBIT;
0489       lumisession->transaction().commit();
0490       unsigned int trglscount = 0;
0491       for (deadIt = deadBeg; deadIt != deadEnd; ++deadIt, ++trglscount) {
0492         std::vector<unsigned long long> bitvec;
0493         bitvec.reserve(lumi::N_TRGBIT);
0494         BITCOUNT& algoinbits = algocount[trglscount];
0495         BITCOUNT& techinbits = techcount[trglscount];
0496         BITCOUNT::const_iterator algoBitIt;
0497         BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
0498         BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
0499         for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgID) {
0500           bitvec.push_back(trgID);
0501         }
0502         BITCOUNT::const_iterator techBitIt;
0503         BITCOUNT::const_iterator techBitBeg = techinbits.begin();
0504         BITCOUNT::const_iterator techBitEnd = techinbits.end();
0505         for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgID) {
0506           bitvec.push_back(trgID);
0507         }
0508         idallocationtable.insert(std::make_pair(trglscount, bitvec));
0509       }
0510       std::cout << "\t all ids allocated" << std::endl;
0511       coral::AttributeList trgData;
0512       trgData.extend<unsigned long long>("TRG_ID");
0513       trgData.extend<unsigned int>("RUNNUM");
0514       trgData.extend<unsigned int>("CMSLSNUM");
0515       trgData.extend<unsigned int>("BITNUM");
0516       trgData.extend<std::string>("BITNAME");
0517       trgData.extend<unsigned int>("TRGCOUNT");
0518       trgData.extend<unsigned long long>("DEADTIME");
0519       trgData.extend<unsigned int>("PRESCALE");
0520 
0521       unsigned long long& trg_id = trgData["TRG_ID"].data<unsigned long long>();
0522       unsigned int& trgrunnum = trgData["RUNNUM"].data<unsigned int>();
0523       unsigned int& cmslsnum = trgData["CMSLSNUM"].data<unsigned int>();
0524       unsigned int& bitnum = trgData["BITNUM"].data<unsigned int>();
0525       std::string& bitname = trgData["BITNAME"].data<std::string>();
0526       unsigned int& count = trgData["TRGCOUNT"].data<unsigned int>();
0527       unsigned long long& deadtime = trgData["DEADTIME"].data<unsigned long long>();
0528       unsigned int& prescale = trgData["PRESCALE"].data<unsigned int>();
0529 
0530       trglscount = 0;
0531       coral::IBulkOperation* trgInserter = nullptr;
0532       unsigned int comittedls = 0;
0533       for (deadIt = deadBeg; deadIt != deadEnd; ++deadIt, ++trglscount) {
0534         unsigned int cmslscount = trglscount + 1;
0535         BITCOUNT& algoinbits = algocount[trglscount];
0536         BITCOUNT& techinbits = techcount[trglscount];
0537         unsigned int trgbitcount = 0;
0538         BITCOUNT::const_iterator algoBitIt;
0539         BITCOUNT::const_iterator algoBitBeg = algoinbits.begin();
0540         BITCOUNT::const_iterator algoBitEnd = algoinbits.end();
0541         if (!lumisession->transaction().isActive()) {
0542           lumisession->transaction().start(false);
0543           coral::ITable& trgtable = lumisession->nominalSchema().tableHandle(LumiNames::trgTableName());
0544           trgInserter = trgtable.dataEditor().bulkInsert(trgData, 2048);
0545         }
0546         for (algoBitIt = algoBitBeg; algoBitIt != algoBitEnd; ++algoBitIt, ++trgbitcount) {
0547           trg_id = idallocationtable[trglscount].at(trgbitcount);
0548           deadtime = *deadIt;
0549           trgrunnum = runnumber;
0550           cmslsnum = cmslscount;
0551           bitnum = trgbitcount;
0552           bitname = algonames[trgbitcount];
0553           count = *algoBitIt;
0554           prescale = algoprescale[trgbitcount];
0555 
0556           //coral::AttributeList rowbuf;
0557           //trgtable.dataEditor().rowBuffer(rowbuf);
0558           //rowbuf["TRG_ID"].data<unsigned long long>()=idg.generateNextIDForTable(LumiNames::trgTableName());
0559           //rowbuf["RUNNUM"].data<unsigned int>()=runnumber;
0560           //rowbuf["CMSLSNUM"].data<unsigned int>()=cmslscount;
0561           //rowbuf["BITNUM"].data<unsigned int>()=trgbitcount;
0562           //rowbuf["BITNAME"].data<std::string>()=algonames[trgbitcount];
0563           //rowbuf["TRGCOUNT"].data<unsigned int>()=*algoBitIt;
0564           //rowbuf["DEADTIME"].data<unsigned long long>()=*deadIt;
0565           //rowbuf["PRESCALE"].data<unsigned int>()=algoprescale[trgbitcount];
0566           //trgtable.dataEditor().insertRow(rowbuf);
0567           trgInserter->processNextIteration();
0568           //std::cout<<"tot "<<tot<<std::endl;
0569         }
0570         BITCOUNT::const_iterator techBitIt;
0571         BITCOUNT::const_iterator techBitBeg = techinbits.begin();
0572         BITCOUNT::const_iterator techBitEnd = techinbits.end();
0573         for (techBitIt = techBitBeg; techBitIt != techBitEnd; ++techBitIt, ++trgbitcount) {
0574           trg_id = idallocationtable[trglscount].at(trgbitcount);
0575           deadtime = *deadIt;
0576           trgrunnum = runnumber;
0577           cmslsnum = cmslscount;
0578           bitnum = trgbitcount;
0579           bitname = technames[trgbitcount - lumi::N_TRGALGOBIT];
0580           count = *techBitIt;
0581           prescale = techprescale[trgbitcount - lumi::N_TRGALGOBIT];
0582           trgInserter->processNextIteration();
0583           //coral::AttributeList rowbuf;
0584           //trgtable.dataEditor().rowBuffer(rowbuf);
0585           //rowbuf["TRG_ID"].data<unsigned long long>()=idg.generateNextIDForTable(LumiNames::trgTableName());
0586           //rowbuf["RUNNUM"].data<unsigned int>()=runnumber;
0587           //rowbuf["CMSLSNUM"].data<unsigned int>()=cmslscount;
0588           //rowbuf["BITNUM"].data<unsigned int>()=trgbitcount;
0589           //rowbuf["BITNAME"].data<std::string>()=technames[trgbitcount-lumi::N_TRGALGOBIT];
0590           //rowbuf["TRGCOUNT"].data<unsigned int>()=*techBitIt;
0591           //rowbuf["DEADTIME"].data<unsigned long long>()=*deadIt;
0592           //rowbuf["PRESCALE"].data<unsigned int>()=techprescale[trgbitcount-lumi::N_TRGALGOBIT];
0593           //trgtable.dataEditor().insertRow(rowbuf);
0594         }
0595         trgInserter->flush();  //flush every ls
0596         ++comittedls;
0597         if (comittedls == TRGWBM2DB::COMMITLSINTERVAL) {
0598           std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0599           delete trgInserter;
0600           trgInserter = nullptr;
0601           lumisession->transaction().commit();
0602           comittedls = 0;
0603           std::cout << "\t committed " << std::endl;
0604         } else if (trglscount == (totalcmsls - 1)) {
0605           std::cout << "\t committing at the end" << std::endl;
0606           delete trgInserter;
0607           trgInserter = nullptr;
0608           lumisession->transaction().commit();
0609           std::cout << "\t done" << std::endl;
0610         }
0611       }
0612     } catch (const coral::Exception& er) {
0613       lumisession->transaction().rollback();
0614       delete lumisession;
0615       delete svc;
0616       throw er;
0617     }
0618     //std::cout<<"about to commit "<<std::endl;
0619     //lumisession->transaction().commit();
0620     delete lumisession;
0621     delete svc;
0622     return 0;
0623   }
0624   const std::string TRGWBM2DB::dataType() const { return "TRG"; }
0625   const std::string TRGWBM2DB::sourceType() const { return "DB"; }
0626   //utilities
0627   std::string TRGWBM2DB::int2str(unsigned int t, unsigned int width) {
0628     std::stringstream ss;
0629     ss.width(width);
0630     ss.fill('0');
0631     ss << t;
0632     return ss.str();
0633   }
0634   unsigned int TRGWBM2DB::str2int(const std::string& s) {
0635     std::istringstream myStream(s);
0636     unsigned int i;
0637     if (myStream >> i) {
0638       return i;
0639     } else {
0640       throw lumi::Exception(std::string("str2int error"), "str2int", "TRGWBM2DB");
0641     }
0642   }
0643   TRGWBM2DB::~TRGWBM2DB() {}
0644 }  // namespace lumi
0645 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
0646 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory, lumi::TRGWBM2DB, "TRGWBM2DB");
0647 #endif