Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:25:21

0001 #ifndef RecoLuminosity_LumiProducer_Lumi2DB_H
0002 #define RecoLuminosity_LumiProducer_Lumi2DB_H
0003 #include "RelationalAccess/ConnectionService.h"
0004 #include "CoralBase/AttributeList.h"
0005 #include "CoralBase/Attribute.h"
0006 #include "CoralBase/AttributeSpecification.h"
0007 #include "CoralBase/Blob.h"
0008 #include "CoralBase/Exception.h"
0009 #include "RelationalAccess/ISessionProxy.h"
0010 #include "RelationalAccess/ITransaction.h"
0011 #include "RelationalAccess/ITypeConverter.h"
0012 #include "RelationalAccess/ISchema.h"
0013 #include "RelationalAccess/ITable.h"
0014 #include "RelationalAccess/ITableDataEditor.h"
0015 #include "RelationalAccess/IBulkOperation.h"
0016 #include "RecoLuminosity/LumiProducer/interface/LumiRawDataStructures.h"
0017 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
0018 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.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/RevisionDML.h"
0024 #include <iostream>
0025 #include <cstring>
0026 #include <cstdlib>
0027 #include <memory>
0028 #include <algorithm>
0029 #include <map>
0030 #include "TFile.h"
0031 #include "TTree.h"
0032 namespace lumi {
0033   class Lumi2DB : public DataPipe {
0034   public:
0035     const static unsigned int COMMITLSINTERVAL = 500;  //commit interval in LS,totalrow=nls*(1+nalgo)
0036     Lumi2DB(const std::string& dest);
0037     unsigned long long retrieveData(unsigned int) override;
0038     const std::string dataType() const override;
0039     const std::string sourceType() const override;
0040     ~Lumi2DB() override;
0041 
0042     struct LumiSource {
0043       unsigned int run;
0044       unsigned int firstsection;
0045       char version[8];
0046       char datestr[9];
0047     };
0048     struct PerBXData {
0049       //int idx;
0050       float lumivalue;
0051       float lumierr;
0052       short lumiquality;
0053     };
0054     struct PerLumiData {
0055       float dtnorm;
0056       float lhcnorm;
0057       float instlumi;
0058       float instlumierror;
0059       short instlumiquality;
0060       short lumisectionquality;
0061       short cmsalive;
0062       std::string beammode;
0063       float beamenergy;
0064       short nlivebx;  //how much is in the beamintensity vector
0065       short* bxindex;
0066       float* beamintensity_1;
0067       float* beamintensity_2;
0068       unsigned int cmslsnr;
0069       unsigned int lumilsnr;
0070       unsigned int startorbit;
0071       unsigned int numorbit;
0072       std::vector<PerBXData> bxET;
0073       std::vector<PerBXData> bxOCC1;
0074       std::vector<PerBXData> bxOCC2;
0075     };
0076     struct beamData {
0077       float energy;
0078       std::string mode;
0079       short nlivebx;
0080       short* bxindex;
0081       float* beamintensity_1;
0082       float* beamintensity_2;
0083     };
0084     typedef std::vector<PerLumiData> LumiResult;
0085     bool hasStableBeam(lumi::Lumi2DB::LumiResult::iterator lumiBeg, lumi::Lumi2DB::LumiResult::iterator lumiEnd);
0086 
0087   private:
0088     void parseSourceString(lumi::Lumi2DB::LumiSource& result) const;
0089     void retrieveBeamIntensity(HCAL_HLX::DIP_COMBINED_DATA* dataPtr, Lumi2DB::beamData& b) const;
0090     void writeAllLumiData(coral::ISessionProxy* session,
0091                           unsigned int irunnumber,
0092                           const std::string& ilumiversion,
0093                           LumiResult::iterator lumiBeg,
0094                           LumiResult::iterator lumiEnd);
0095     unsigned int writeAllLumiDataToSchema2(coral::ISessionProxy* session,
0096                                            const std::string& source,
0097                                            unsigned int runnumber,
0098                                            float bgev,
0099                                            unsigned int ncollidingbunches,
0100                                            LumiResult::iterator lumiBeg,
0101                                            LumiResult::iterator lumiEnd);
0102     void writeBeamIntensityOnly(coral::ISessionProxy* session,
0103                                 unsigned int irunnumber,
0104                                 const std::string& ilumiversion,
0105                                 LumiResult::iterator lumiBeg,
0106                                 LumiResult::iterator lumiEnd);
0107     bool isLumiDataValid(LumiResult::iterator lumiBeg, LumiResult::iterator lumiEnd);
0108     float applyCalibration(float varToCalibrate) const;
0109     void cleanTemporaryMemory(lumi::Lumi2DB::LumiResult::iterator lumiBeg, lumi::Lumi2DB::LumiResult::iterator lumiEnd);
0110   };  //cl Lumi2DB
0111 }  // namespace lumi
0112 
0113 //
0114 //implementation
0115 //
0116 float lumi::Lumi2DB::applyCalibration(float varToCalibrate) const {  //#only used for writing into schema_v1
0117   return float(varToCalibrate) * m_norm;
0118 }
0119 bool lumi::Lumi2DB::hasStableBeam(lumi::Lumi2DB::LumiResult::iterator lumiBeg,
0120                                   lumi::Lumi2DB::LumiResult::iterator lumiEnd) {
0121   //
0122   // the run has at least 1 stable beams LS
0123   //
0124   lumi::Lumi2DB::LumiResult::iterator lumiIt;
0125   int nStable = 0;
0126   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt) {
0127     if (lumiIt->beammode == "STABLE BEAMS") {
0128       ++nStable;
0129     }
0130   }
0131   if (nStable == 0) {
0132     return false;
0133   }
0134   return true;
0135 }
0136 bool lumi::Lumi2DB::isLumiDataValid(lumi::Lumi2DB::LumiResult::iterator lumiBeg,
0137                                     lumi::Lumi2DB::LumiResult::iterator lumiEnd) {
0138   //
0139   // validate lumidata: all ls has lumi less than 0.5e-08 before calibration, then invalid data
0140   //
0141   lumi::Lumi2DB::LumiResult::iterator lumiIt;
0142   int nBad = 0;
0143   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt) {
0144     //std::cout<<"instlumi before calib "<<lumiIt->instlumi<<std::endl;
0145     if (lumiIt->instlumi <= 0.5e-8) {  //cut before calib
0146       ++nBad;
0147     }
0148   }
0149   if (nBad == std::distance(lumiBeg, lumiEnd)) {
0150     return false;
0151   }
0152   return true;
0153 }
0154 void lumi::Lumi2DB::writeBeamIntensityOnly(coral::ISessionProxy* session,
0155                                            unsigned int irunnumber,
0156                                            const std::string& ilumiversion,
0157                                            lumi::Lumi2DB::LumiResult::iterator lumiBeg,
0158                                            lumi::Lumi2DB::LumiResult::iterator lumiEnd) {
0159   coral::AttributeList inputData;
0160   inputData.extend("bxindex", typeid(coral::Blob));
0161   inputData.extend("beamintensity_1", typeid(coral::Blob));
0162   inputData.extend("beamintensity_2", typeid(coral::Blob));
0163   inputData.extend("runnum", typeid(unsigned int));
0164   inputData.extend("startorbit", typeid(unsigned int));
0165   inputData.extend("lumiversion", typeid(std::string));
0166   coral::Blob& bxindex = inputData["bxindex"].data<coral::Blob>();
0167   coral::Blob& beamintensity_1 = inputData["beamintensity_1"].data<coral::Blob>();
0168   coral::Blob& beamintensity_2 = inputData["beamintensity_2"].data<coral::Blob>();
0169   unsigned int& runnumber = inputData["runnum"].data<unsigned int>();
0170   unsigned int& startorbit = inputData["startorbit"].data<unsigned int>();
0171   std::string& lumiversion = inputData["lumiversion"].data<std::string>();
0172 
0173   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
0174   coral::IBulkOperation* summaryUpdater = nullptr;
0175   unsigned int totallumils = std::distance(lumiBeg, lumiEnd);
0176   unsigned int lumiindx = 0;
0177   unsigned int comittedls = 0;
0178   std::string setClause(
0179       "CMSBXINDEXBLOB=:bxindex,BEAMINTENSITYBLOB_1=:beamintensity_1,BEAMINTENSITYBLOB_2=:beamintensity_2");
0180   std::string condition("RUNNUM=:runnum AND STARTORBIT=:startorbit AND LUMIVERSION=:lumiversion");
0181   runnumber = irunnumber;
0182   lumiversion = ilumiversion;
0183   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt, ++lumiindx) {
0184     if (!session->transaction().isActive()) {
0185       session->transaction().start(false);
0186     }
0187     startorbit = lumiIt->startorbit;
0188     //std::cout<<"runnumber "<<irunnumber<<" starorbit "<<startorbit<<" lumiversion "<<lumiversion<<" totallumils "<<totallumils<<std::endl;
0189     short nlivebx = lumiIt->nlivebx;
0190     if (nlivebx != 0) {
0191       bxindex.resize(sizeof(short) * nlivebx);
0192       beamintensity_1.resize(sizeof(float) * nlivebx);
0193       beamintensity_2.resize(sizeof(float) * nlivebx);
0194       void* bxindex_StartAddress = bxindex.startingAddress();
0195       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
0196       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
0197       std::memmove(bxindex_StartAddress, lumiIt->bxindex, sizeof(short) * nlivebx);
0198       std::memmove(beamIntensity1_StartAddress, lumiIt->beamintensity_1, sizeof(float) * nlivebx);
0199       std::memmove(beamIntensity2_StartAddress, lumiIt->beamintensity_2, sizeof(float) * nlivebx);
0200       ::free(lumiIt->bxindex);
0201       ::free(lumiIt->beamintensity_1);
0202       ::free(lumiIt->beamintensity_2);
0203     } else {
0204       bxindex.resize(0);
0205       beamintensity_1.resize(0);
0206       beamintensity_2.resize(0);
0207     }
0208     coral::ITable& summarytable = session->nominalSchema().tableHandle(LumiNames::lumisummaryTableName());
0209     summaryUpdater = summarytable.dataEditor().bulkUpdateRows(setClause, condition, inputData, totallumils);
0210     summaryUpdater->processNextIteration();
0211     summaryUpdater->flush();
0212     ++comittedls;
0213     if (comittedls == Lumi2DB::COMMITLSINTERVAL) {
0214       std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0215       delete summaryUpdater;
0216       summaryUpdater = nullptr;
0217       session->transaction().commit();
0218       comittedls = 0;
0219       std::cout << "\t committed " << std::endl;
0220     } else if (lumiindx == (totallumils - 1)) {
0221       std::cout << "\t committing at the end" << std::endl;
0222       delete summaryUpdater;
0223       summaryUpdater = nullptr;
0224       session->transaction().commit();
0225       std::cout << "\t done" << std::endl;
0226     }
0227   }
0228 }
0229 void lumi::Lumi2DB::writeAllLumiData(coral::ISessionProxy* session,
0230                                      unsigned int irunnumber,
0231                                      const std::string& ilumiversion,
0232                                      lumi::Lumi2DB::LumiResult::iterator lumiBeg,
0233                                      lumi::Lumi2DB::LumiResult::iterator lumiEnd) {
0234   coral::AttributeList summaryData;
0235   coral::AttributeList detailData;
0236   summaryData.extend("LUMISUMMARY_ID", typeid(unsigned long long));
0237   summaryData.extend("RUNNUM", typeid(unsigned int));
0238   summaryData.extend("CMSLSNUM", typeid(unsigned int));
0239   summaryData.extend("LUMILSNUM", typeid(unsigned int));
0240   summaryData.extend("LUMIVERSION", typeid(std::string));
0241   summaryData.extend("DTNORM", typeid(float));
0242   summaryData.extend("LHCNORM", typeid(float));
0243   summaryData.extend("INSTLUMI", typeid(float));
0244   summaryData.extend("INSTLUMIERROR", typeid(float));
0245   summaryData.extend("INSTLUMIQUALITY", typeid(short));
0246   summaryData.extend("LUMISECTIONQUALITY", typeid(short));
0247   summaryData.extend("CMSALIVE", typeid(short));
0248   summaryData.extend("STARTORBIT", typeid(unsigned int));
0249   summaryData.extend("NUMORBIT", typeid(unsigned int));
0250   summaryData.extend("BEAMENERGY", typeid(float));
0251   summaryData.extend("BEAMSTATUS", typeid(std::string));
0252   summaryData.extend("CMSBXINDEXBLOB", typeid(coral::Blob));
0253   summaryData.extend("BEAMINTENSITYBLOB_1", typeid(coral::Blob));
0254   summaryData.extend("BEAMINTENSITYBLOB_2", typeid(coral::Blob));
0255 
0256   detailData.extend("LUMIDETAIL_ID", typeid(unsigned long long));
0257   detailData.extend("LUMISUMMARY_ID", typeid(unsigned long long));
0258   detailData.extend("BXLUMIVALUE", typeid(coral::Blob));
0259   detailData.extend("BXLUMIERROR", typeid(coral::Blob));
0260   detailData.extend("BXLUMIQUALITY", typeid(coral::Blob));
0261   detailData.extend("ALGONAME", typeid(std::string));
0262 
0263   unsigned long long& lumisummary_id = summaryData["LUMISUMMARY_ID"].data<unsigned long long>();
0264   unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
0265   std::string& lumiversion = summaryData["LUMIVERSION"].data<std::string>();
0266   float& dtnorm = summaryData["DTNORM"].data<float>();
0267   float& lhcnorm = summaryData["LHCNORM"].data<float>();
0268   float& instlumi = summaryData["INSTLUMI"].data<float>();
0269   float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
0270   short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
0271   short& lumisectionquality = summaryData["LUMISECTIONQUALITY"].data<short>();
0272   short& alive = summaryData["CMSALIVE"].data<short>();
0273   unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
0274   unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
0275   unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
0276   unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
0277   float& beamenergy = summaryData["BEAMENERGY"].data<float>();
0278   std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
0279   coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
0280   coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
0281   coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
0282 
0283   unsigned long long& lumidetail_id = detailData["LUMIDETAIL_ID"].data<unsigned long long>();
0284   unsigned long long& d2lumisummary_id = detailData["LUMISUMMARY_ID"].data<unsigned long long>();
0285   coral::Blob& bxlumivalue = detailData["BXLUMIVALUE"].data<coral::Blob>();
0286   coral::Blob& bxlumierror = detailData["BXLUMIERROR"].data<coral::Blob>();
0287   coral::Blob& bxlumiquality = detailData["BXLUMIQUALITY"].data<coral::Blob>();
0288   std::string& algoname = detailData["ALGONAME"].data<std::string>();
0289 
0290   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
0291   coral::IBulkOperation* summaryInserter = nullptr;
0292   coral::IBulkOperation* detailInserter = nullptr;
0293   //one loop for ids
0294   //nested transaction doesn't work with bulk inserter
0295   unsigned int totallumils = std::distance(lumiBeg, lumiEnd);
0296   unsigned int lumiindx = 0;
0297   std::map<unsigned long long, std::vector<unsigned long long> > idallocationtable;
0298   std::cout << "\t allocating total lumisummary ids " << totallumils << std::endl;
0299   std::cout << "\t allocating total lumidetail ids " << totallumils * lumi::N_LUMIALGO << std::endl;
0300 
0301   session->transaction().start(false);
0302   lumi::idDealer idg(session->nominalSchema());
0303   unsigned long long lumisummaryID =
0304       idg.generateNextIDForTable(LumiNames::lumisummaryTableName(), totallumils) - totallumils;
0305   unsigned long long lumidetailID =
0306       idg.generateNextIDForTable(LumiNames::lumidetailTableName(), totallumils * lumi::N_LUMIALGO) -
0307       totallumils * lumi::N_LUMIALGO;
0308   session->transaction().commit();
0309   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt, ++lumiindx, ++lumisummaryID) {
0310     std::vector<unsigned long long> allIDs;
0311     allIDs.reserve(1 + lumi::N_LUMIALGO);
0312     allIDs.push_back(lumisummaryID);
0313     for (unsigned int j = 0; j < lumi::N_LUMIALGO; ++j, ++lumidetailID) {
0314       allIDs.push_back(lumidetailID);
0315     }
0316     idallocationtable.insert(std::make_pair(lumiindx, allIDs));
0317   }
0318   std::cout << "\t all ids allocated" << std::endl;
0319   lumiindx = 0;
0320   unsigned int comittedls = 0;
0321   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt, ++lumiindx) {
0322     if (!session->transaction().isActive()) {
0323       session->transaction().start(false);
0324       coral::ITable& summarytable = session->nominalSchema().tableHandle(LumiNames::lumisummaryTableName());
0325       summaryInserter = summarytable.dataEditor().bulkInsert(summaryData, totallumils);
0326       coral::ITable& detailtable = session->nominalSchema().tableHandle(LumiNames::lumidetailTableName());
0327       detailInserter = detailtable.dataEditor().bulkInsert(detailData, totallumils * lumi::N_LUMIALGO);
0328     }
0329     lumisummary_id = idallocationtable[lumiindx][0];
0330     lumirunnum = irunnumber;
0331     lumiversion = ilumiversion;
0332     dtnorm = lumiIt->dtnorm;
0333     lhcnorm = lumiIt->lhcnorm;
0334     //instlumi = lumiIt->instlumi;
0335     //instlumierror = lumiIt->instlumierror;
0336     instlumi = applyCalibration(lumiIt->instlumi);
0337     instlumierror = applyCalibration(lumiIt->instlumierror);
0338     instlumiquality = lumiIt->instlumiquality;
0339     lumisectionquality = lumiIt->lumisectionquality;
0340     alive = lumiIt->cmsalive;
0341     cmslsnr = lumiIt->cmslsnr;
0342 
0343     lumilsnr = lumiIt->lumilsnr;
0344     startorbit = lumiIt->startorbit;
0345     numorbit = lumiIt->numorbit;
0346     beamenergy = lumiIt->beamenergy;
0347     beamstatus = lumiIt->beammode;
0348     short nlivebx = lumiIt->nlivebx;
0349     //std::cout<<"nlivebx "<<nlivebx<<std::endl;
0350     if (nlivebx != 0) {
0351       bxindex.resize(sizeof(short) * nlivebx);
0352       beamintensity_1.resize(sizeof(float) * nlivebx);
0353       beamintensity_2.resize(sizeof(float) * nlivebx);
0354       void* bxindex_StartAddress = bxindex.startingAddress();
0355       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
0356       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
0357       std::memmove(bxindex_StartAddress, lumiIt->bxindex, sizeof(short) * nlivebx);
0358       std::memmove(beamIntensity1_StartAddress, lumiIt->beamintensity_1, sizeof(float) * nlivebx);
0359       std::memmove(beamIntensity2_StartAddress, lumiIt->beamintensity_2, sizeof(float) * nlivebx);
0360       //::free(lumiIt->bxindex);
0361       //::free(lumiIt->beamintensity_1);
0362       //::free(lumiIt->beamintensity_2);
0363     } else {
0364       bxindex.resize(0);
0365       beamintensity_1.resize(0);
0366       beamintensity_2.resize(0);
0367     }
0368     //insert the new row
0369     summaryInserter->processNextIteration();
0370     summaryInserter->flush();
0371     unsigned int algoindx = 1;
0372     for (unsigned int j = 0; j < lumi::N_LUMIALGO; ++j, ++algoindx) {
0373       d2lumisummary_id = idallocationtable[lumiindx].at(0);
0374       lumidetail_id = idallocationtable[lumiindx].at(algoindx);
0375       std::vector<PerBXData>::const_iterator bxIt;
0376       std::vector<PerBXData>::const_iterator bxBeg;
0377       std::vector<PerBXData>::const_iterator bxEnd;
0378       if (j == 0) {
0379         algoname = std::string("ET");
0380         bxBeg = lumiIt->bxET.begin();
0381         bxEnd = lumiIt->bxET.end();
0382       }
0383       if (j == 1) {
0384         algoname = std::string("OCC1");
0385         bxBeg = lumiIt->bxOCC1.begin();
0386         bxEnd = lumiIt->bxOCC1.end();
0387       }
0388       if (j == 2) {
0389         algoname = std::string("OCC2");
0390         bxBeg = lumiIt->bxOCC2.begin();
0391         bxEnd = lumiIt->bxOCC2.end();
0392       }
0393       float lumivalue[lumi::N_BX] = {0.0};
0394       float lumierror[lumi::N_BX] = {0.0};
0395       int lumiquality[lumi::N_BX] = {0};
0396       bxlumivalue.resize(sizeof(float) * lumi::N_BX);
0397       bxlumierror.resize(sizeof(float) * lumi::N_BX);
0398       bxlumiquality.resize(sizeof(short) * lumi::N_BX);
0399       void* bxlumivalueStartAddress = bxlumivalue.startingAddress();
0400       void* bxlumierrorStartAddress = bxlumierror.startingAddress();
0401       void* bxlumiqualityStartAddress = bxlumiquality.startingAddress();
0402       unsigned int k = 0;
0403       for (bxIt = bxBeg; bxIt != bxEnd; ++bxIt, ++k) {
0404         lumivalue[k] = bxIt->lumivalue;
0405         lumierror[k] = bxIt->lumierr;
0406         lumiquality[k] = bxIt->lumiquality;
0407       }
0408       std::memmove(bxlumivalueStartAddress, lumivalue, sizeof(float) * lumi::N_BX);
0409       std::memmove(bxlumierrorStartAddress, lumierror, sizeof(float) * lumi::N_BX);
0410       std::memmove(bxlumiqualityStartAddress, lumiquality, sizeof(short) * lumi::N_BX);
0411       detailInserter->processNextIteration();
0412     }
0413     detailInserter->flush();
0414     ++comittedls;
0415     if (comittedls == Lumi2DB::COMMITLSINTERVAL) {
0416       std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0417       delete summaryInserter;
0418       summaryInserter = nullptr;
0419       delete detailInserter;
0420       detailInserter = nullptr;
0421       session->transaction().commit();
0422       comittedls = 0;
0423       std::cout << "\t committed " << std::endl;
0424     } else if (lumiindx == (totallumils - 1)) {
0425       std::cout << "\t committing at the end" << std::endl;
0426       delete summaryInserter;
0427       summaryInserter = nullptr;
0428       delete detailInserter;
0429       detailInserter = nullptr;
0430       session->transaction().commit();
0431       std::cout << "\t done" << std::endl;
0432     }
0433   }
0434 }
0435 
0436 unsigned int lumi::Lumi2DB::writeAllLumiDataToSchema2(coral::ISessionProxy* session,
0437                                                       const std::string& source,
0438                                                       unsigned int irunnumber,
0439                                                       float bgev,
0440                                                       unsigned int ncollidingbunches,
0441                                                       lumi::Lumi2DB::LumiResult::iterator lumiBeg,
0442                                                       lumi::Lumi2DB::LumiResult::iterator lumiEnd) {
0443   ///
0444   //output: lumi data id
0445   ///
0446   std::cout << "writeAllLumiDataToSchema2" << std::endl;
0447   coral::AttributeList summaryData;
0448   summaryData.extend("DATA_ID", typeid(unsigned long long));
0449   summaryData.extend("RUNNUM", typeid(unsigned int));
0450   summaryData.extend("LUMILSNUM", typeid(unsigned int));
0451   summaryData.extend("CMSLSNUM", typeid(unsigned int));
0452   summaryData.extend("INSTLUMI", typeid(float));
0453   summaryData.extend("INSTLUMIERROR", typeid(float));
0454   summaryData.extend("INSTLUMIQUALITY", typeid(short));
0455   summaryData.extend("BEAMSTATUS", typeid(std::string));
0456   summaryData.extend("BEAMENERGY", typeid(float));
0457   summaryData.extend("NUMORBIT", typeid(unsigned int));
0458   summaryData.extend("STARTORBIT", typeid(unsigned int));
0459   summaryData.extend("CMSBXINDEXBLOB", typeid(coral::Blob));
0460   summaryData.extend("BEAMINTENSITYBLOB_1", typeid(coral::Blob));
0461   summaryData.extend("BEAMINTENSITYBLOB_2", typeid(coral::Blob));
0462   summaryData.extend("BXLUMIVALUE_OCC1", typeid(coral::Blob));
0463   summaryData.extend("BXLUMIERROR_OCC1", typeid(coral::Blob));
0464   summaryData.extend("BXLUMIQUALITY_OCC1", typeid(coral::Blob));
0465   summaryData.extend("BXLUMIVALUE_OCC2", typeid(coral::Blob));
0466   summaryData.extend("BXLUMIERROR_OCC2", typeid(coral::Blob));
0467   summaryData.extend("BXLUMIQUALITY_OCC2", typeid(coral::Blob));
0468   summaryData.extend("BXLUMIVALUE_ET", typeid(coral::Blob));
0469   summaryData.extend("BXLUMIERROR_ET", typeid(coral::Blob));
0470   summaryData.extend("BXLUMIQUALITY_ET", typeid(coral::Blob));
0471 
0472   unsigned long long& data_id = summaryData["DATA_ID"].data<unsigned long long>();
0473   unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
0474   unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
0475   unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
0476   float& instlumi = summaryData["INSTLUMI"].data<float>();
0477   float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
0478   short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
0479   std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
0480   float& beamenergy = summaryData["BEAMENERGY"].data<float>();
0481   unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
0482   unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
0483   coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
0484   coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
0485   coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
0486   coral::Blob& bxlumivalue_et = summaryData["BXLUMIVALUE_ET"].data<coral::Blob>();
0487   coral::Blob& bxlumierror_et = summaryData["BXLUMIERROR_ET"].data<coral::Blob>();
0488   coral::Blob& bxlumiquality_et = summaryData["BXLUMIQUALITY_ET"].data<coral::Blob>();
0489   coral::Blob& bxlumivalue_occ1 = summaryData["BXLUMIVALUE_OCC1"].data<coral::Blob>();
0490   coral::Blob& bxlumierror_occ1 = summaryData["BXLUMIERROR_OCC1"].data<coral::Blob>();
0491   coral::Blob& bxlumiquality_occ1 = summaryData["BXLUMIQUALITY_OCC1"].data<coral::Blob>();
0492   coral::Blob& bxlumivalue_occ2 = summaryData["BXLUMIVALUE_OCC2"].data<coral::Blob>();
0493   coral::Blob& bxlumierror_occ2 = summaryData["BXLUMIERROR_OCC2"].data<coral::Blob>();
0494   coral::Blob& bxlumiquality_occ2 = summaryData["BXLUMIQUALITY_OCC2"].data<coral::Blob>();
0495 
0496   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
0497   coral::IBulkOperation* summaryInserter = nullptr;
0498 
0499   unsigned int totallumils = std::distance(lumiBeg, lumiEnd);
0500   unsigned int lumiindx = 0;
0501   unsigned int comittedls = 0;
0502 
0503   unsigned long long branch_id = 3;
0504   std::string branch_name("DATA");
0505   lumi::RevisionDML revisionDML;
0506   lumi::RevisionDML::LumiEntry lumirundata;
0507   std::stringstream op;
0508   op << irunnumber;
0509   std::string runnumberStr = op.str();
0510   session->transaction().start(false);
0511   lumirundata.entry_name = runnumberStr;
0512   lumirundata.source = source;
0513   lumirundata.runnumber = irunnumber;
0514   lumirundata.bgev = bgev;
0515   lumirundata.ncollidingbunches = ncollidingbunches;
0516   lumirundata.data_id = 0;
0517   lumirundata.entry_id = revisionDML.getEntryInBranchByName(
0518       session->nominalSchema(), lumi::LumiNames::lumidataTableName(), runnumberStr, branch_name);
0519   //std::cout<<"entry_id "<<lumirundata.entry_id<<std::endl;
0520   if (lumirundata.entry_id == 0) {
0521     revisionDML.bookNewEntry(session->nominalSchema(), LumiNames::lumidataTableName(), lumirundata);
0522     std::cout << "it's a new run lumirundata revision_id " << lumirundata.revision_id << " entry_id "
0523               << lumirundata.entry_id << " data_id " << lumirundata.data_id << std::endl;
0524     revisionDML.addEntry(session->nominalSchema(), LumiNames::lumidataTableName(), lumirundata, branch_id, branch_name);
0525     std::cout << "added entry " << std::endl;
0526   } else {
0527     revisionDML.bookNewRevision(session->nominalSchema(), LumiNames::lumidataTableName(), lumirundata);
0528     std::cout << "lumirundata revision_id " << lumirundata.revision_id << " entry_id " << lumirundata.entry_id
0529               << " data_id " << lumirundata.data_id << std::endl;
0530     revisionDML.addRevision(
0531         session->nominalSchema(), LumiNames::lumidataTableName(), lumirundata, branch_id, branch_name);
0532   }
0533   revisionDML.insertLumiRunData(session->nominalSchema(), lumirundata);
0534   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt, ++lumiindx) {
0535     if (!session->transaction().isActive()) {
0536       session->transaction().start(false);
0537       coral::ITable& summarytable = session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
0538       summaryInserter = summarytable.dataEditor().bulkInsert(summaryData, totallumils);
0539     } else {
0540       if (lumiIt == lumiBeg) {
0541         coral::ITable& summarytable = session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
0542         summaryInserter = summarytable.dataEditor().bulkInsert(summaryData, totallumils);
0543       }
0544     }
0545     data_id = lumirundata.data_id;
0546     lumirunnum = irunnumber;
0547     lumilsnr = lumiIt->lumilsnr;
0548     cmslsnr = lumiIt->cmslsnr;
0549     instlumi = lumiIt->instlumi;            // not calibrated!
0550     instlumierror = lumiIt->instlumierror;  // not calibrated!
0551     instlumiquality = lumiIt->instlumiquality;
0552     beamstatus = lumiIt->beammode;
0553     beamenergy = lumiIt->beamenergy;
0554     numorbit = lumiIt->numorbit;
0555     startorbit = lumiIt->startorbit;
0556     short nlivebx = lumiIt->nlivebx;
0557     //std::cout<<"nlivebx "<<nlivebx<<std::endl;
0558     if (nlivebx != 0) {
0559       bxindex.resize(sizeof(short) * nlivebx);
0560       beamintensity_1.resize(sizeof(float) * nlivebx);
0561       beamintensity_2.resize(sizeof(float) * nlivebx);
0562       void* bxindex_StartAddress = bxindex.startingAddress();
0563       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
0564       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
0565       std::memmove(bxindex_StartAddress, lumiIt->bxindex, sizeof(short) * nlivebx);
0566       std::memmove(beamIntensity1_StartAddress, lumiIt->beamintensity_1, sizeof(float) * nlivebx);
0567       std::memmove(beamIntensity2_StartAddress, lumiIt->beamintensity_2, sizeof(float) * nlivebx);
0568       //::free(lumiIt->bxindex);
0569       //::free(lumiIt->beamintensity_1);
0570       //::free(lumiIt->beamintensity_2);
0571     } else {
0572       bxindex.resize(0);
0573       beamintensity_1.resize(0);
0574       beamintensity_2.resize(0);
0575     }
0576     for (unsigned int j = 0; j < lumi::N_LUMIALGO; ++j) {
0577       std::vector<PerBXData>::const_iterator bxIt;
0578       std::vector<PerBXData>::const_iterator bxBeg;
0579       std::vector<PerBXData>::const_iterator bxEnd;
0580       if (j == 0) {  //the push_back order in the input data is ET,OCC1,OCC2
0581         //algoname=std::string("ET");
0582         bxBeg = lumiIt->bxET.begin();
0583         bxEnd = lumiIt->bxET.end();
0584         float lumivalue[lumi::N_BX] = {0.0};
0585         float lumierror[lumi::N_BX] = {0.0};
0586         int lumiquality[lumi::N_BX] = {0};
0587         bxlumivalue_et.resize(sizeof(float) * lumi::N_BX);
0588         bxlumierror_et.resize(sizeof(float) * lumi::N_BX);
0589         bxlumiquality_et.resize(sizeof(short) * lumi::N_BX);
0590         void* bxlumivalueStartAddress = bxlumivalue_et.startingAddress();
0591         void* bxlumierrorStartAddress = bxlumierror_et.startingAddress();
0592         void* bxlumiqualityStartAddress = bxlumiquality_et.startingAddress();
0593         unsigned int k = 0;
0594         for (bxIt = bxBeg; bxIt != bxEnd; ++bxIt, ++k) {
0595           lumivalue[k] = bxIt->lumivalue;
0596           lumierror[k] = bxIt->lumierr;
0597           lumiquality[k] = bxIt->lumiquality;
0598         }
0599         std::memmove(bxlumivalueStartAddress, lumivalue, sizeof(float) * lumi::N_BX);
0600         std::memmove(bxlumierrorStartAddress, lumierror, sizeof(float) * lumi::N_BX);
0601         std::memmove(bxlumiqualityStartAddress, lumiquality, sizeof(short) * lumi::N_BX);
0602       }
0603       if (j == 1) {
0604         //algoname=std::string("OCC1");
0605         bxBeg = lumiIt->bxOCC1.begin();
0606         bxEnd = lumiIt->bxOCC1.end();
0607         float lumivalue[lumi::N_BX] = {0.0};
0608         float lumierror[lumi::N_BX] = {0.0};
0609         int lumiquality[lumi::N_BX] = {0};
0610         bxlumivalue_occ1.resize(sizeof(float) * lumi::N_BX);
0611         bxlumierror_occ1.resize(sizeof(float) * lumi::N_BX);
0612         bxlumiquality_occ1.resize(sizeof(short) * lumi::N_BX);
0613         void* bxlumivalueStartAddress = bxlumivalue_occ1.startingAddress();
0614         void* bxlumierrorStartAddress = bxlumierror_occ1.startingAddress();
0615         void* bxlumiqualityStartAddress = bxlumiquality_occ1.startingAddress();
0616         unsigned int k = 0;
0617         for (bxIt = bxBeg; bxIt != bxEnd; ++bxIt, ++k) {
0618           lumivalue[k] = bxIt->lumivalue;
0619           lumierror[k] = bxIt->lumierr;
0620           lumiquality[k] = bxIt->lumiquality;
0621         }
0622         std::memmove(bxlumivalueStartAddress, lumivalue, sizeof(float) * lumi::N_BX);
0623         std::memmove(bxlumierrorStartAddress, lumierror, sizeof(float) * lumi::N_BX);
0624         std::memmove(bxlumiqualityStartAddress, lumiquality, sizeof(short) * lumi::N_BX);
0625       }
0626       if (j == 2) {
0627         //algoname=std::string("OCC2");
0628         bxBeg = lumiIt->bxOCC2.begin();
0629         bxEnd = lumiIt->bxOCC2.end();
0630         float lumivalue[lumi::N_BX] = {0.0};
0631         float lumierror[lumi::N_BX] = {0.0};
0632         int lumiquality[lumi::N_BX] = {0};
0633         bxlumivalue_occ2.resize(sizeof(float) * lumi::N_BX);
0634         bxlumierror_occ2.resize(sizeof(float) * lumi::N_BX);
0635         bxlumiquality_occ2.resize(sizeof(short) * lumi::N_BX);
0636         void* bxlumivalueStartAddress = bxlumivalue_occ2.startingAddress();
0637         void* bxlumierrorStartAddress = bxlumierror_occ2.startingAddress();
0638         void* bxlumiqualityStartAddress = bxlumiquality_occ2.startingAddress();
0639         unsigned int k = 0;
0640         for (bxIt = bxBeg; bxIt != bxEnd; ++bxIt, ++k) {
0641           lumivalue[k] = bxIt->lumivalue;
0642           lumierror[k] = bxIt->lumierr;
0643           lumiquality[k] = bxIt->lumiquality;
0644         }
0645         std::memmove(bxlumivalueStartAddress, lumivalue, sizeof(float) * lumi::N_BX);
0646         std::memmove(bxlumierrorStartAddress, lumierror, sizeof(float) * lumi::N_BX);
0647         std::memmove(bxlumiqualityStartAddress, lumiquality, sizeof(short) * lumi::N_BX);
0648       }
0649     }
0650     summaryInserter->processNextIteration();
0651     summaryInserter->flush();
0652     ++comittedls;
0653     if (comittedls == Lumi2DB::COMMITLSINTERVAL) {
0654       std::cout << "\t committing in LS chunck " << comittedls << std::endl;
0655       delete summaryInserter;
0656       summaryInserter = nullptr;
0657       session->transaction().commit();
0658       comittedls = 0;
0659       std::cout << "\t committed " << std::endl;
0660     } else if (lumiindx == (totallumils - 1)) {
0661       std::cout << "\t committing at the end" << std::endl;
0662       delete summaryInserter;
0663       summaryInserter = nullptr;
0664       session->transaction().commit();
0665       std::cout << "\t done" << std::endl;
0666     }
0667   }
0668   return lumirundata.data_id;
0669 }
0670 
0671 void lumi::Lumi2DB::cleanTemporaryMemory(lumi::Lumi2DB::LumiResult::iterator lumiBeg,
0672                                          lumi::Lumi2DB::LumiResult::iterator lumiEnd) {
0673   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
0674   for (lumiIt = lumiBeg; lumiIt != lumiEnd; ++lumiIt) {
0675     ::free(lumiIt->bxindex);
0676     ::free(lumiIt->beamintensity_1);
0677     ::free(lumiIt->beamintensity_2);
0678   }
0679 }
0680 lumi::Lumi2DB::Lumi2DB(const std::string& dest) : DataPipe(dest) {}
0681 void lumi::Lumi2DB::parseSourceString(lumi::Lumi2DB::LumiSource& result) const {
0682   //parse lumi source file name
0683   if (m_source.length() == 0)
0684     throw lumi::Exception("lumi source is not set", "parseSourceString", "Lumi2DB");
0685   //std::cout<<"source "<<m_source<<std::endl;
0686   size_t tempIndex = m_source.find_last_of('.');
0687   size_t nameLength = m_source.length();
0688   std::string FileSuffix = m_source.substr(tempIndex + 1, nameLength - tempIndex);
0689   std::string::size_type lastPos = m_source.find_first_not_of('_', 0);
0690   std::string::size_type pos = m_source.find_first_of('_', lastPos);
0691   std::vector<std::string> pieces;
0692   while (std::string::npos != pos || std::string::npos != lastPos) {
0693     pieces.push_back(m_source.substr(lastPos, pos - lastPos));
0694     lastPos = m_source.find_first_not_of('_', pos);
0695     pos = m_source.find_first_of('_', lastPos);
0696   }
0697   if (pieces[1] != "LUMI" || pieces[2] != "RAW" || FileSuffix != "root") {
0698     throw lumi::Exception("not lumi raw data file CMS_LUMI_RAW", "parseSourceString", "Lumi2DB");
0699   }
0700   std::strcpy(result.datestr, pieces[3].c_str());
0701   std::strcpy(result.version, pieces[5].c_str());
0702   //std::cout<<"version : "<<result.version<<std::endl;
0703   result.run = atoi(pieces[4].c_str());
0704   //std::cout<<"run : "<<result.run<<std::endl;
0705   result.firstsection = atoi(pieces[5].c_str());
0706   //std::cout<<"first section : "<< result.firstsection<<std::endl;
0707 }
0708 
0709 void lumi::Lumi2DB::retrieveBeamIntensity(HCAL_HLX::DIP_COMBINED_DATA* dataPtr, Lumi2DB::beamData& b) const {
0710   if (dataPtr == nullptr) {
0711     std::cout << "HCAL_HLX::DIP_COMBINED_DATA* dataPtr=0" << std::endl;
0712     b.bxindex = nullptr;
0713     b.beamintensity_1 = nullptr;
0714     b.beamintensity_2 = nullptr;
0715     b.nlivebx = 0;
0716   } else {
0717     b.bxindex = (short*)::malloc(sizeof(short) * lumi::N_BX);
0718     b.beamintensity_1 = (float*)::malloc(sizeof(float) * lumi::N_BX);
0719     b.beamintensity_2 = (float*)::malloc(sizeof(float) * lumi::N_BX);
0720 
0721     short a = 0;  //a is position in lumidetail array
0722     for (unsigned int i = 0; i < lumi::N_BX; ++i) {
0723       if (i == 0) {
0724         if (dataPtr->Beam[0].averageBunchIntensities[0] > 0 || dataPtr->Beam[1].averageBunchIntensities[0] > 0) {
0725           b.bxindex[a] = 0;
0726           b.beamintensity_1[a] = dataPtr->Beam[0].averageBunchIntensities[0];
0727           b.beamintensity_2[a] = dataPtr->Beam[1].averageBunchIntensities[0];
0728           ++a;
0729         }
0730         continue;
0731       }
0732       if (dataPtr->Beam[0].averageBunchIntensities[i - 1] > 0 || dataPtr->Beam[1].averageBunchIntensities[i - 1] > 0) {
0733         b.bxindex[a] = i;
0734         b.beamintensity_1[a] = dataPtr->Beam[0].averageBunchIntensities[i - 1];
0735         b.beamintensity_2[a] = dataPtr->Beam[1].averageBunchIntensities[i - 1];
0736         ++a;
0737         //if(i!=0){
0738         // std::cout<<"beam intensity "<<dataPtr->sectionNumber<<" "<<dataPtr->timestamp-1262300400<<" "<<(i-1)*10+1<<" "<<b.beamintensity_1[a]<<" "<<b.beamintensity_2[a]<<std::endl;
0739         //}
0740       }
0741     }
0742     b.nlivebx = a;
0743   }
0744 }
0745 /**
0746    retrieve lumi per ls data from root file
0747  **/
0748 unsigned long long lumi::Lumi2DB::retrieveData(unsigned int runnumber) {
0749   lumi::Lumi2DB::LumiResult lumiresult;
0750   //check filename is in  lumiraw format
0751   lumi::Lumi2DB::LumiSource filenamecontent;
0752   unsigned int lumidataid = 0;
0753   try {
0754     parseSourceString(filenamecontent);
0755   } catch (const lumi::Exception& er) {
0756     std::cout << er.what() << std::endl;
0757     throw er;
0758   }
0759   if (filenamecontent.run != runnumber) {
0760     throw lumi::Exception("runnumber in file name does not match requested run number", "retrieveData", "Lumi2DB");
0761   }
0762   TFile* source = TFile::Open(m_source.c_str(), "READ");
0763   TTree* hlxtree = (TTree*)source->Get("HLXData");
0764   if (!hlxtree) {
0765     throw lumi::Exception(std::string("non-existing HLXData "), "retrieveData", "Lumi2DB");
0766   }
0767   //hlxtree->Print();
0768   std::unique_ptr<HCAL_HLX::LUMI_SECTION> localSection(new HCAL_HLX::LUMI_SECTION);
0769   HCAL_HLX::LUMI_SECTION_HEADER* lumiheader = &(localSection->hdr);
0770   HCAL_HLX::LUMI_SUMMARY* lumisummary = &(localSection->lumiSummary);
0771   HCAL_HLX::LUMI_DETAIL* lumidetail = &(localSection->lumiDetail);
0772 
0773   hlxtree->SetBranchAddress("Header.", &lumiheader);
0774   hlxtree->SetBranchAddress("Summary.", &lumisummary);
0775   hlxtree->SetBranchAddress("Detail.", &lumidetail);
0776 
0777   size_t nentries = hlxtree->GetEntries();
0778   unsigned int nstablebeam = 0;
0779   float bgev = 0.0;
0780   //source->GetListOfKeys()->Print();
0781   std::map<unsigned int, Lumi2DB::beamData> dipmap;
0782   TTree* diptree = (TTree*)source->Get("DIPCombined");
0783   if (diptree) {
0784     //throw lumi::Exception(std::string("non-existing DIPData "),"retrieveData","Lumi2DB");
0785     std::unique_ptr<HCAL_HLX::DIP_COMBINED_DATA> dipdata(new HCAL_HLX::DIP_COMBINED_DATA);
0786     diptree->SetBranchAddress("DIPCombined.", &dipdata);
0787     size_t ndipentries = diptree->GetEntries();
0788     for (size_t i = 0; i < ndipentries; ++i) {
0789       diptree->GetEntry(i);
0790       //unsigned int fillnumber=dipdata->FillNumber;
0791       //std::vector<short> collidingidx;collidingidx.reserve(LUMI::N_BX);
0792       //for(unsigned int i=0;i<lumi::N_BX;++i){
0793       //int isb1colliding=dipdata->beam[0].beamConfig[i];
0794       //int isb2colliding=dipdata->beam[1].beamConfig[i];
0795       //if(isb1colliding && isb2colliding&&isb1colliding==1&&isb2colliding==1){
0796       //  collidingidx.push_back(i);
0797       //    }
0798       //}
0799       beamData b;
0800       //std::cout<<"Beam Mode : "<<dipdata->beamMode<<"\n";
0801       //std::cout<<"Beam Energy : "<<dipdata->Energy<<"\n";
0802       //std::cout<<"sectionUmber : "<<dipdata->sectionNumber<<"\n";
0803       unsigned int dipls = dipdata->sectionNumber;
0804       if (std::string(dipdata->beamMode).empty()) {
0805         b.mode = "N/A";
0806       } else {
0807         b.mode = dipdata->beamMode;
0808       }
0809       b.energy = dipdata->Energy;
0810       if (b.mode == "STABLE BEAMS") {
0811         ++nstablebeam;
0812         bgev += b.energy;
0813       }
0814       this->retrieveBeamIntensity(dipdata.get(), b);
0815       dipmap.insert(std::make_pair(dipls, b));
0816     }
0817   } else {
0818     for (size_t i = 0; i < nentries; ++i) {
0819       beamData b;
0820       b.mode = "N/A";
0821       b.energy = 0.0;
0822       this->retrieveBeamIntensity(nullptr, b);
0823       dipmap.insert(std::make_pair(i, b));
0824     }
0825   }
0826   //diptree->Print();
0827 
0828   size_t ncmslumi = 0;
0829   std::cout << "processing total lumi lumisection " << nentries << std::endl;
0830   //size_t lumisecid=0;
0831   //unsigned int lumilumisecid=0;
0832   //runnumber=lumiheader->runNumber;
0833   //
0834   //hardcode the first LS is always alive
0835   //
0836   unsigned int ncollidingbunches = 0;
0837   for (size_t i = 0; i < nentries; ++i) {
0838     lumi::Lumi2DB::PerLumiData h;
0839     h.cmsalive = 1;
0840     hlxtree->GetEntry(i);
0841     //std::cout<<"live flag "<<lumiheader->bCMSLive <<std::endl;
0842     if (!lumiheader->bCMSLive && i != 0) {
0843       std::cout << "\t non-CMS LS " << lumiheader->sectionNumber << " ";
0844       h.cmsalive = 0;
0845     }
0846     ++ncmslumi;
0847     if (ncmslumi == 1) {  //just take the first ls
0848       ncollidingbunches = lumiheader->numBunches;
0849     }
0850     h.bxET.reserve(lumi::N_BX);
0851     h.bxOCC1.reserve(lumi::N_BX);
0852     h.bxOCC2.reserve(lumi::N_BX);
0853 
0854     //runnumber=lumiheader->runNumber;
0855     //if(runnumber!=m_run) throw std::runtime_error(std::string("requested run ")+this->int2str(m_run)+" does not match runnumber in the data header "+this->int2str(runnumber));
0856     h.lumilsnr = lumiheader->sectionNumber;
0857     std::map<unsigned int, Lumi2DB::beamData>::iterator beamIt = dipmap.find(h.lumilsnr);
0858     if (beamIt != dipmap.end()) {
0859       h.beammode = beamIt->second.mode;
0860       h.beamenergy = beamIt->second.energy;
0861       h.nlivebx = beamIt->second.nlivebx;
0862       if (h.nlivebx != 0) {
0863         h.bxindex = (short*)malloc(sizeof(short) * h.nlivebx);
0864         h.beamintensity_1 = (float*)malloc(sizeof(float) * h.nlivebx);
0865         h.beamintensity_2 = (float*)malloc(sizeof(float) * h.nlivebx);
0866         if (h.bxindex == nullptr || h.beamintensity_1 == nullptr || h.beamintensity_2 == nullptr) {
0867           std::cout << "malloc failed" << std::endl;
0868         }
0869         //std::cout<<"h.bxindex size "<<sizeof(short)*h.nlivebx<<std::endl;
0870         //std::cout<<"h.beamintensity_1 size "<<sizeof(float)*h.nlivebx<<std::endl;
0871         //std::cout<<"h.beamintensity_2 size "<<sizeof(float)*h.nlivebx<<std::endl;
0872 
0873         std::memmove(h.bxindex, beamIt->second.bxindex, sizeof(short) * h.nlivebx);
0874         std::memmove(h.beamintensity_1, beamIt->second.beamintensity_1, sizeof(float) * h.nlivebx);
0875         std::memmove(h.beamintensity_2, beamIt->second.beamintensity_2, sizeof(float) * h.nlivebx);
0876 
0877         ::free(beamIt->second.bxindex);
0878         beamIt->second.bxindex = nullptr;
0879         ::free(beamIt->second.beamintensity_1);
0880         beamIt->second.beamintensity_1 = nullptr;
0881         ::free(beamIt->second.beamintensity_2);
0882         beamIt->second.beamintensity_2 = nullptr;
0883       } else {
0884         //std::cout<<"h.nlivebx is zero"<<std::endl;
0885         h.bxindex = nullptr;
0886         h.beamintensity_1 = nullptr;
0887         h.beamintensity_2 = nullptr;
0888       }
0889     } else {
0890       h.beammode = "N/A";
0891       h.beamenergy = 0.0;
0892       h.nlivebx = 0;
0893       h.bxindex = nullptr;
0894       h.beamintensity_1 = nullptr;
0895       h.beamintensity_2 = nullptr;
0896     }
0897     h.startorbit = lumiheader->startOrbit;
0898     h.numorbit = lumiheader->numOrbits;
0899     if (h.cmsalive == 0) {
0900       h.cmslsnr = 0;  //the dead ls has cmsls number=0
0901     } else {
0902       h.cmslsnr = ncmslumi;  //we guess cms lumils
0903     }
0904     h.instlumi = lumisummary->InstantLumi;
0905     //std::cout<<"instant lumi "<<lumisummary->InstantLumi<<std::endl;
0906     h.instlumierror = lumisummary->InstantLumiErr;
0907     h.lumisectionquality = lumisummary->InstantLumiQlty;
0908     h.dtnorm = lumisummary->DeadTimeNormalization;
0909     h.lhcnorm = lumisummary->LHCNormalization;
0910     //unsigned int timestp=lumiheader->timestamp;
0911     //std::cout<<"cmslsnum "<<ncmslumi<<"timestp "<<timestp<<std::endl;
0912     for (size_t i = 0; i < lumi::N_BX; ++i) {
0913       lumi::Lumi2DB::PerBXData bET;
0914       lumi::Lumi2DB::PerBXData bOCC1;
0915       lumi::Lumi2DB::PerBXData bOCC2;
0916       //bET.idx=i+1;
0917       bET.lumivalue = lumidetail->ETLumi[i];
0918       bET.lumierr = lumidetail->ETLumiErr[i];
0919       bET.lumiquality = lumidetail->ETLumiQlty[i];
0920       h.bxET.push_back(bET);
0921 
0922       //bOCC1.idx=i+1;
0923 
0924       bOCC1.lumivalue = lumidetail->OccLumi[0][i];
0925       bOCC1.lumierr = lumidetail->OccLumiErr[0][i];
0926       /**if(bOCC1.lumivalue*6.370>1.0e-04){
0927     if(i!=0){
0928       std::cout<<i<<" detail "<<(i-1)*10+1<<" "<<(timestp-1262300400)<<" "<<bOCC1.lumivalue*6.37<<" "<<bOCC1.lumierr*6.37<<std::endl;
0929     }
0930       }
0931       **/
0932       bOCC1.lumiquality = lumidetail->OccLumiQlty[0][i];
0933       h.bxOCC1.push_back(bOCC1);
0934 
0935       //bOCC2.idx=i+1;
0936       bOCC2.lumivalue = lumidetail->OccLumi[1][i];
0937       bOCC2.lumierr = lumidetail->OccLumiErr[1][i];
0938       bOCC2.lumiquality = lumidetail->OccLumiQlty[1][i];
0939       h.bxOCC2.push_back(bOCC2);
0940     }
0941     lumiresult.push_back(h);
0942   }
0943   std::cout << std::endl;
0944   if (nstablebeam != 0) {
0945     bgev = bgev / nstablebeam;  //nominal beam energy=sum(energy)/nstablebeams
0946   }
0947   std::cout << "avg stable beam energy " << bgev << std::endl;
0948   if (!m_novalidate && !isLumiDataValid(lumiresult.begin(), lumiresult.end())) {
0949     throw lumi::invalidDataException("all lumi values are <0.5e-08", "isLumiDataValid", "Lumi2DB");
0950   }
0951   if (!m_nocheckingstablebeam && !hasStableBeam(lumiresult.begin(), lumiresult.end())) {
0952     throw lumi::noStableBeamException("no LS has STABLE BEAMS", "hasStableBeam", "Lumi2DB");
0953   }
0954   coral::ConnectionService* svc = new coral::ConnectionService;
0955   lumi::DBConfig dbconf(*svc);
0956   if (!m_authpath.empty()) {
0957     dbconf.setAuthentication(m_authpath);
0958   }
0959   coral::ISessionProxy* session = svc->connect(m_dest, coral::Update);
0960   coral::ITypeConverter& tpc = session->typeConverter();
0961   tpc.setCppTypeForSqlType("unsigned int", "NUMBER(10)");
0962   //
0963   //write to old lumisummary
0964   //
0965   try {
0966     const std::string lversion(filenamecontent.version);
0967     if (m_mode == std::string("beamintensity_only")) {
0968       std::cout << "writing beam intensity only to old lumisummary table " << std::endl;
0969       writeBeamIntensityOnly(session, runnumber, lversion, lumiresult.begin(), lumiresult.end());
0970       std::cout << "done" << std::endl;
0971     } else {
0972       if (m_mode == "loadoldschema") {
0973         std::cout << "writing all lumi data to old lumisummary table " << std::endl;
0974         writeAllLumiData(session, runnumber, lversion, lumiresult.begin(), lumiresult.end());
0975         std::cout << "done" << std::endl;
0976       }
0977     }
0978     std::cout << "writing all lumi data to lumisummary_V2 table " << std::endl;
0979     lumidataid = writeAllLumiDataToSchema2(
0980         session, m_source, runnumber, bgev, ncollidingbunches, lumiresult.begin(), lumiresult.end());
0981     std::cout << "done" << std::endl;
0982     cleanTemporaryMemory(lumiresult.begin(), lumiresult.end());
0983     delete session;
0984     delete svc;
0985   } catch (const coral::Exception& er) {
0986     std::cout << "database error " << er.what() << std::endl;
0987     session->transaction().rollback();
0988     delete session;
0989     delete svc;
0990     throw er;
0991   }
0992   return lumidataid;
0993 }
0994 const std::string lumi::Lumi2DB::dataType() const { return "LUMI"; }
0995 const std::string lumi::Lumi2DB::sourceType() const { return "DB"; }
0996 lumi::Lumi2DB::~Lumi2DB() {}
0997 
0998 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
0999 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory, lumi::Lumi2DB, "Lumi2DB");
1000 #endif