CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/RecoLuminosity/LumiProducer/plugins/Lumi2DB.cc

Go to the documentation of this file.
00001 #ifndef RecoLuminosity_LumiProducer_Lumi2DB_H 
00002 #define RecoLuminosity_LumiProducer_Lumi2DB_H
00003 #include "RelationalAccess/ConnectionService.h"
00004 #include "CoralBase/AttributeList.h"
00005 #include "CoralBase/Attribute.h"
00006 #include "CoralBase/AttributeSpecification.h"
00007 #include "CoralBase/Blob.h"
00008 #include "CoralBase/Exception.h"
00009 #include "RelationalAccess/ISessionProxy.h"
00010 #include "RelationalAccess/ITransaction.h"
00011 #include "RelationalAccess/ITypeConverter.h"
00012 #include "RelationalAccess/ISchema.h"
00013 #include "RelationalAccess/ITable.h"
00014 #include "RelationalAccess/ITableDataEditor.h"
00015 #include "RelationalAccess/IBulkOperation.h"
00016 #include "RecoLuminosity/LumiProducer/interface/LumiRawDataStructures.h"
00017 #include "RecoLuminosity/LumiProducer/interface/DataPipe.h"
00018 #include "RecoLuminosity/LumiProducer/interface/ConstantDef.h"
00019 #include "RecoLuminosity/LumiProducer/interface/LumiNames.h"
00020 #include "RecoLuminosity/LumiProducer/interface/idDealer.h"
00021 #include "RecoLuminosity/LumiProducer/interface/Exception.h"
00022 #include "RecoLuminosity/LumiProducer/interface/DBConfig.h"
00023 #include "RecoLuminosity/LumiProducer/interface/RevisionDML.h"
00024 #include <iostream>
00025 #include <cstring>
00026 #include <cstdlib>
00027 #include <memory>
00028 #include <algorithm>
00029 #include <map>
00030 #include "TFile.h"
00031 #include "TTree.h"
00032 namespace lumi{
00033   class Lumi2DB : public DataPipe{
00034   public:
00035     const static unsigned int COMMITLSINTERVAL=500; //commit interval in LS,totalrow=nls*(1+nalgo)
00036     Lumi2DB(const std::string& dest);
00037     virtual void retrieveData( unsigned int );
00038     virtual const std::string dataType() const;
00039     virtual const std::string sourceType() const;
00040     virtual ~Lumi2DB();
00041   
00042     struct LumiSource{
00043       unsigned int run;
00044       unsigned int firstsection;
00045       char version[8];
00046       char datestr[9];
00047     };
00048     struct PerBXData{
00049       //int idx;
00050       float lumivalue;
00051       float lumierr;
00052       short lumiquality;
00053     };
00054     struct PerLumiData{
00055       float dtnorm;
00056       float lhcnorm;
00057       float instlumi;
00058       float instlumierror;
00059       short instlumiquality;
00060       short lumisectionquality;
00061       short cmsalive;
00062       std::string beammode;
00063       float beamenergy;
00064       short nlivebx;
00065       short* bxindex;
00066       float* beamintensity_1;
00067       float* beamintensity_2;
00068       unsigned int cmslsnr;
00069       unsigned int lumilsnr;
00070       unsigned int startorbit;
00071       unsigned int numorbit;
00072       std::vector<PerBXData> bxET;
00073       std::vector<PerBXData> bxOCC1;
00074       std::vector<PerBXData> bxOCC2;
00075     };
00076     struct beamData{
00077       float energy;
00078       std::string mode;
00079       short nlivebx;
00080       short* bxindex;
00081       float* beamintensity_1;
00082       float* beamintensity_2;
00083     };
00084     typedef std::vector<PerLumiData> LumiResult;
00085     bool hasStableBeam( lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd );
00086   private:
00087     void parseSourceString(lumi::Lumi2DB::LumiSource& result)const;
00088     void retrieveBeamIntensity(HCAL_HLX::DIP_COMBINED_DATA* dataPtr, Lumi2DB::beamData&b)const;
00089     void writeAllLumiData(coral::ISessionProxy* session,unsigned int irunnumber,const std::string& ilumiversion,LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
00090     void writeAllLumiDataToSchema2(coral::ISessionProxy* session,const std::string& source,unsigned int runnumber,float bgev,LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
00091     void writeBeamIntensityOnly(coral::ISessionProxy* session,unsigned int irunnumber,const std::string& ilumiversion,LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
00092     bool isLumiDataValid(LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
00093     float applyCalibration(float varToCalibrate) const;
00094     void cleanTemporaryMemory( lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd);
00095   };//cl Lumi2DB
00096 }//ns lumi
00097 
00098 //
00099 //implementation
00100 //
00101 float
00102 lumi::Lumi2DB::applyCalibration(float varToCalibrate)const{ //#only used for writing into schema_v1
00103   return float(varToCalibrate)*m_norm;
00104 }
00105 bool
00106 lumi::Lumi2DB::hasStableBeam( lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
00107   //
00108   // the run has at least 1 stable beams LS
00109   //
00110   lumi::Lumi2DB::LumiResult::iterator lumiIt;  
00111   int nStable=0;
00112   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
00113     if(lumiIt->beammode=="STABLE BEAMS"){
00114       ++nStable;
00115     }
00116   }
00117   if(nStable==0){
00118     return false;
00119   }
00120   return true;
00121 }
00122 bool
00123 lumi::Lumi2DB::isLumiDataValid(lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd){
00124   //
00125   // validate lumidata: all ls has lumi less than 0.5e-08 before calibration, then invalid data
00126   //
00127   lumi::Lumi2DB::LumiResult::iterator lumiIt;
00128   int nBad=0;
00129   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
00130     //std::cout<<"instlumi before calib "<<lumiIt->instlumi<<std::endl;
00131     if(lumiIt->instlumi<=0.5e-8){//cut before calib
00132       ++nBad;
00133     }
00134   }
00135   if(nBad==std::distance(lumiBeg,lumiEnd)){
00136     return false;
00137   }
00138   return true;
00139 }
00140 void
00141 lumi::Lumi2DB::writeBeamIntensityOnly(
00142                             coral::ISessionProxy* session,
00143                             unsigned int irunnumber,
00144                             const std::string& ilumiversion,
00145                             lumi::Lumi2DB::LumiResult::iterator lumiBeg,
00146                             lumi::Lumi2DB::LumiResult::iterator lumiEnd
00147                             ){
00148   coral::AttributeList inputData;
00149   inputData.extend("bxindex",typeid(coral::Blob));
00150   inputData.extend("beamintensity_1",typeid(coral::Blob));
00151   inputData.extend("beamintensity_2",typeid(coral::Blob));
00152   inputData.extend("runnum",typeid(unsigned int));
00153   inputData.extend("startorbit",typeid(unsigned int));
00154   inputData.extend("lumiversion",typeid(std::string)); 
00155   coral::Blob& bxindex = inputData["bxindex"].data<coral::Blob>();
00156   coral::Blob& beamintensity_1 = inputData["beamintensity_1"].data<coral::Blob>();
00157   coral::Blob& beamintensity_2 = inputData["beamintensity_2"].data<coral::Blob>();
00158   unsigned int& runnumber = inputData["runnum"].data<unsigned int>();
00159   unsigned int& startorbit = inputData["startorbit"].data<unsigned int>();
00160   std::string& lumiversion = inputData["lumiversion"].data<std::string>();
00161 
00162   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
00163   coral::IBulkOperation* summaryUpdater=0;
00164   unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
00165   unsigned int lumiindx=0;
00166   unsigned int comittedls=0;
00167   std::string setClause("CMSBXINDEXBLOB=:bxindex,BEAMINTENSITYBLOB_1=:beamintensity_1,BEAMINTENSITYBLOB_2=:beamintensity_2");
00168   std::string condition("RUNNUM=:runnum AND STARTORBIT=:startorbit AND LUMIVERSION=:lumiversion");
00169   runnumber=irunnumber;
00170   lumiversion=ilumiversion;
00171   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
00172     if(!session->transaction().isActive()){ 
00173       session->transaction().start(false);
00174     }
00175     startorbit=lumiIt->startorbit;
00176     //std::cout<<"runnumber "<<irunnumber<<" starorbit "<<startorbit<<" lumiversion "<<lumiversion<<" totallumils "<<totallumils<<std::endl;
00177     short nlivebx=lumiIt->nlivebx;
00178     if(nlivebx!=0){
00179       bxindex.resize(sizeof(short)*nlivebx);
00180       beamintensity_1.resize(sizeof(float)*nlivebx);
00181       beamintensity_2.resize(sizeof(float)*nlivebx);
00182       void* bxindex_StartAddress = bxindex.startingAddress();      
00183       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
00184       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
00185       std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
00186       std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
00187       std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
00188       ::free(lumiIt->bxindex);
00189       ::free(lumiIt->beamintensity_1);
00190       ::free(lumiIt->beamintensity_2);
00191     }else{
00192       bxindex.resize(0);
00193       beamintensity_1.resize(0);
00194       beamintensity_2.resize(0);
00195     }
00196     coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryTableName());
00197     summaryUpdater=summarytable.dataEditor().bulkUpdateRows(setClause,condition,inputData,totallumils);
00198     summaryUpdater->processNextIteration();
00199     summaryUpdater->flush();
00200     ++comittedls;
00201     if(comittedls==Lumi2DB::COMMITLSINTERVAL){
00202       std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00203       delete summaryUpdater;
00204       summaryUpdater=0;
00205       session->transaction().commit();
00206       comittedls=0;
00207       std::cout<<"\t committed "<<std::endl; 
00208     }else if( lumiindx==(totallumils-1) ){
00209       std::cout<<"\t committing at the end"<<std::endl; 
00210       delete summaryUpdater; summaryUpdater=0;
00211       session->transaction().commit();
00212       std::cout<<"\t done"<<std::endl; 
00213     }
00214   }
00215 }
00216 void
00217 lumi::Lumi2DB::writeAllLumiData(
00218                             coral::ISessionProxy* session,
00219                             unsigned int irunnumber,
00220                             const std::string& ilumiversion,
00221                             lumi::Lumi2DB::LumiResult::iterator lumiBeg,
00222                             lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
00223   coral::AttributeList summaryData;
00224   coral::AttributeList detailData;
00225   summaryData.extend("LUMISUMMARY_ID",typeid(unsigned long long));
00226   summaryData.extend("RUNNUM",typeid(unsigned int));
00227   summaryData.extend("CMSLSNUM",typeid(unsigned int));
00228   summaryData.extend("LUMILSNUM",typeid(unsigned int));
00229   summaryData.extend("LUMIVERSION",typeid(std::string));
00230   summaryData.extend("DTNORM",typeid(float));
00231   summaryData.extend("LHCNORM",typeid(float));
00232   summaryData.extend("INSTLUMI",typeid(float));
00233   summaryData.extend("INSTLUMIERROR",typeid(float));
00234   summaryData.extend("INSTLUMIQUALITY",typeid(short));
00235   summaryData.extend("LUMISECTIONQUALITY",typeid(short));
00236   summaryData.extend("CMSALIVE",typeid(short));
00237   summaryData.extend("STARTORBIT",typeid(unsigned int));
00238   summaryData.extend("NUMORBIT",typeid(unsigned int));
00239   summaryData.extend("BEAMENERGY",typeid(float));
00240   summaryData.extend("BEAMSTATUS",typeid(std::string));
00241   summaryData.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
00242   summaryData.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
00243   summaryData.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
00244   
00245   detailData.extend("LUMIDETAIL_ID",typeid(unsigned long long));
00246   detailData.extend("LUMISUMMARY_ID",typeid(unsigned long long));
00247   detailData.extend("BXLUMIVALUE",typeid(coral::Blob));
00248   detailData.extend("BXLUMIERROR",typeid(coral::Blob));
00249   detailData.extend("BXLUMIQUALITY",typeid(coral::Blob));
00250   detailData.extend("ALGONAME",typeid(std::string));
00251   
00252   unsigned long long& lumisummary_id=summaryData["LUMISUMMARY_ID"].data<unsigned long long>();
00253   unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
00254   std::string& lumiversion = summaryData["LUMIVERSION"].data<std::string>();
00255   float& dtnorm = summaryData["DTNORM"].data<float>();
00256   float& lhcnorm = summaryData["LHCNORM"].data<float>();
00257   float& instlumi = summaryData["INSTLUMI"].data<float>();
00258   float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
00259   short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
00260   short& lumisectionquality = summaryData["LUMISECTIONQUALITY"].data<short>();
00261   short& alive = summaryData["CMSALIVE"].data<short>();
00262   unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
00263   unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
00264   unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
00265   unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
00266   float& beamenergy = summaryData["BEAMENERGY"].data<float>();
00267   std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
00268   coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
00269   coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
00270   coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
00271   
00272   unsigned long long& lumidetail_id=detailData["LUMIDETAIL_ID"].data<unsigned long long>();
00273   unsigned long long& d2lumisummary_id=detailData["LUMISUMMARY_ID"].data<unsigned long long>();
00274   coral::Blob& bxlumivalue=detailData["BXLUMIVALUE"].data<coral::Blob>();
00275   coral::Blob& bxlumierror=detailData["BXLUMIERROR"].data<coral::Blob>();
00276   coral::Blob& bxlumiquality=detailData["BXLUMIQUALITY"].data<coral::Blob>();
00277   std::string& algoname=detailData["ALGONAME"].data<std::string>();
00278     
00279   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
00280   coral::IBulkOperation* summaryInserter=0;
00281   coral::IBulkOperation* detailInserter=0;
00282   //one loop for ids
00283   //nested transaction doesn't work with bulk inserter
00284   unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
00285   unsigned int lumiindx=0;
00286   std::map< unsigned long long,std::vector<unsigned long long> > idallocationtable;
00287   std::cout<<"\t allocating total lumisummary ids "<<totallumils<<std::endl; 
00288   std::cout<<"\t allocating total lumidetail ids "<<totallumils*lumi::N_LUMIALGO<<std::endl; 
00289 
00290   session->transaction().start(false);
00291   lumi::idDealer idg(session->nominalSchema());
00292   unsigned long long lumisummaryID = idg.generateNextIDForTable(LumiNames::lumisummaryTableName(),totallumils)-totallumils;
00293   unsigned long long lumidetailID=idg.generateNextIDForTable(LumiNames::lumidetailTableName(),totallumils*lumi::N_LUMIALGO)-totallumils*lumi::N_LUMIALGO;
00294   session->transaction().commit();
00295   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx,++lumisummaryID){
00296     std::vector< unsigned long long > allIDs;
00297     allIDs.reserve(1+lumi::N_LUMIALGO);
00298     allIDs.push_back(lumisummaryID);
00299     for( unsigned int j=0; j<lumi::N_LUMIALGO; ++j, ++lumidetailID){
00300       allIDs.push_back(lumidetailID);
00301     }
00302     idallocationtable.insert(std::make_pair(lumiindx,allIDs));
00303   }
00304   std::cout<<"\t all ids allocated"<<std::endl; 
00305   lumiindx=0;
00306   unsigned int comittedls=0;
00307   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
00308     if(!session->transaction().isActive()){ 
00309       session->transaction().start(false);
00310       coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryTableName());
00311       summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
00312       coral::ITable& detailtable=session->nominalSchema().tableHandle(LumiNames::lumidetailTableName());
00313       detailInserter=detailtable.dataEditor().bulkInsert(detailData,totallumils*lumi::N_LUMIALGO);    
00314     }
00315     lumisummary_id=idallocationtable[lumiindx][0];
00316     lumirunnum = irunnumber;
00317     lumiversion = ilumiversion;
00318     dtnorm = lumiIt->dtnorm;
00319     lhcnorm = lumiIt->lhcnorm;
00320     //instlumi = lumiIt->instlumi;
00321     //instlumierror = lumiIt->instlumierror;
00322     instlumi = applyCalibration(lumiIt->instlumi);
00323     instlumierror = applyCalibration(lumiIt->instlumierror);
00324     instlumiquality = lumiIt->instlumiquality;
00325     lumisectionquality = lumiIt->lumisectionquality;
00326     alive = lumiIt->cmsalive;
00327     cmslsnr = lumiIt->cmslsnr;
00328       
00329     lumilsnr = lumiIt->lumilsnr;
00330     startorbit = lumiIt->startorbit;
00331     numorbit = lumiIt->numorbit;
00332     beamenergy = lumiIt->beamenergy;
00333     beamstatus = lumiIt->beammode;
00334     short nlivebx=lumiIt->nlivebx;
00335     //std::cout<<"nlivebx "<<nlivebx<<std::endl;
00336     if(nlivebx!=0){
00337       bxindex.resize(sizeof(short)*nlivebx);
00338       beamintensity_1.resize(sizeof(float)*nlivebx);
00339       beamintensity_2.resize(sizeof(float)*nlivebx);
00340       void* bxindex_StartAddress = bxindex.startingAddress();      
00341       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
00342       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
00343       std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
00344       std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
00345       std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
00346       //::free(lumiIt->bxindex);
00347       //::free(lumiIt->beamintensity_1);
00348       //::free(lumiIt->beamintensity_2);
00349     }else{
00350       bxindex.resize(0);
00351       beamintensity_1.resize(0);
00352       beamintensity_2.resize(0);
00353     }
00354     //insert the new row
00355     summaryInserter->processNextIteration();
00356     summaryInserter->flush();
00357     unsigned int algoindx=1;
00358     for( unsigned int j=0; j<lumi:: N_LUMIALGO; ++j,++algoindx ){
00359       d2lumisummary_id=idallocationtable[lumiindx].at(0);
00360       lumidetail_id=idallocationtable[lumiindx].at(algoindx);
00361       std::vector<PerBXData>::const_iterator bxIt;
00362       std::vector<PerBXData>::const_iterator bxBeg;
00363       std::vector<PerBXData>::const_iterator bxEnd;
00364       if(j==0) {
00365         algoname=std::string("ET");
00366         bxBeg=lumiIt->bxET.begin();
00367         bxEnd=lumiIt->bxET.end();
00368       }
00369       if(j==1) {
00370         algoname=std::string("OCC1");
00371         bxBeg=lumiIt->bxOCC1.begin();
00372         bxEnd=lumiIt->bxOCC1.end();
00373       }
00374       if(j==2) {
00375         algoname=std::string("OCC2");
00376         bxBeg=lumiIt->bxOCC2.begin();
00377         bxEnd=lumiIt->bxOCC2.end();
00378       }
00379       float lumivalue[lumi::N_BX]={0.0};
00380       float lumierror[lumi::N_BX]={0.0};
00381       int lumiquality[lumi::N_BX]={0};
00382       bxlumivalue.resize(sizeof(float)*lumi::N_BX);
00383       bxlumierror.resize(sizeof(float)*lumi::N_BX);
00384       bxlumiquality.resize(sizeof(short)*lumi::N_BX);
00385       void* bxlumivalueStartAddress=bxlumivalue.startingAddress();
00386       void* bxlumierrorStartAddress=bxlumierror.startingAddress();
00387       void* bxlumiqualityStartAddress=bxlumiquality.startingAddress();
00388       unsigned int k=0;
00389       for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){            
00390         lumivalue[k]=bxIt->lumivalue;
00391         lumierror[k]=bxIt->lumierr;
00392         lumiquality[k]=bxIt->lumiquality;
00393       }
00394       std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00395       std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00396       std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00397       detailInserter->processNextIteration();
00398     }
00399     detailInserter->flush();
00400     ++comittedls;
00401     if(comittedls==Lumi2DB::COMMITLSINTERVAL){
00402       std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00403       delete summaryInserter;
00404       summaryInserter=0;
00405       delete detailInserter;
00406       detailInserter=0;
00407       session->transaction().commit();
00408       comittedls=0;
00409       std::cout<<"\t committed "<<std::endl; 
00410     }else if( lumiindx==(totallumils-1) ){
00411       std::cout<<"\t committing at the end"<<std::endl; 
00412       delete summaryInserter; summaryInserter=0;
00413       delete detailInserter; detailInserter=0;
00414       session->transaction().commit();
00415       std::cout<<"\t done"<<std::endl; 
00416     }
00417   }
00418 }
00419 
00420 void
00421 lumi::Lumi2DB::writeAllLumiDataToSchema2(
00422                             coral::ISessionProxy* session,
00423                             const std::string& source,
00424                             unsigned int irunnumber,
00425                             float bgev,
00426                             lumi::Lumi2DB::LumiResult::iterator lumiBeg,
00427                             lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
00428 
00429   coral::AttributeList summaryData;
00430   summaryData.extend("DATA_ID",typeid(unsigned long long));
00431   summaryData.extend("RUNNUM",typeid(unsigned int));
00432   summaryData.extend("LUMILSNUM",typeid(unsigned int));
00433   summaryData.extend("CMSLSNUM",typeid(unsigned int));
00434   summaryData.extend("INSTLUMI",typeid(float));
00435   summaryData.extend("INSTLUMIERROR",typeid(float));
00436   summaryData.extend("INSTLUMIQUALITY",typeid(short));
00437   summaryData.extend("BEAMSTATUS",typeid(std::string));
00438   summaryData.extend("BEAMENERGY",typeid(float));
00439   summaryData.extend("NUMORBIT",typeid(unsigned int));
00440   summaryData.extend("STARTORBIT",typeid(unsigned int));
00441   summaryData.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
00442   summaryData.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
00443   summaryData.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
00444   summaryData.extend("BXLUMIVALUE_OCC1",typeid(coral::Blob));
00445   summaryData.extend("BXLUMIERROR_OCC1",typeid(coral::Blob));
00446   summaryData.extend("BXLUMIQUALITY_OCC1",typeid(coral::Blob));
00447   summaryData.extend("BXLUMIVALUE_OCC2",typeid(coral::Blob));
00448   summaryData.extend("BXLUMIERROR_OCC2",typeid(coral::Blob));
00449   summaryData.extend("BXLUMIQUALITY_OCC2",typeid(coral::Blob));
00450   summaryData.extend("BXLUMIVALUE_ET",typeid(coral::Blob));
00451   summaryData.extend("BXLUMIERROR_ET",typeid(coral::Blob));
00452   summaryData.extend("BXLUMIQUALITY_ET",typeid(coral::Blob));
00453 
00454   unsigned long long& data_id=summaryData["DATA_ID"].data<unsigned long long>();
00455   unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
00456   unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
00457   unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
00458   float& instlumi = summaryData["INSTLUMI"].data<float>();
00459   float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
00460   short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
00461   std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
00462   float& beamenergy = summaryData["BEAMENERGY"].data<float>(); 
00463   unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
00464   unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
00465   coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
00466   coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
00467   coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();  
00468   coral::Blob& bxlumivalue_et=summaryData["BXLUMIVALUE_ET"].data<coral::Blob>();
00469   coral::Blob& bxlumierror_et=summaryData["BXLUMIERROR_ET"].data<coral::Blob>();
00470   coral::Blob& bxlumiquality_et=summaryData["BXLUMIQUALITY_ET"].data<coral::Blob>();
00471   coral::Blob& bxlumivalue_occ1=summaryData["BXLUMIVALUE_OCC1"].data<coral::Blob>();
00472   coral::Blob& bxlumierror_occ1=summaryData["BXLUMIERROR_OCC1"].data<coral::Blob>();
00473   coral::Blob& bxlumiquality_occ1=summaryData["BXLUMIQUALITY_OCC1"].data<coral::Blob>();
00474   coral::Blob& bxlumivalue_occ2=summaryData["BXLUMIVALUE_OCC2"].data<coral::Blob>();
00475   coral::Blob& bxlumierror_occ2=summaryData["BXLUMIERROR_OCC2"].data<coral::Blob>();
00476   coral::Blob& bxlumiquality_occ2=summaryData["BXLUMIQUALITY_OCC2"].data<coral::Blob>();
00477 
00478   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
00479   coral::IBulkOperation* summaryInserter=0;
00480 
00481   unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
00482   unsigned int lumiindx=0;
00483   unsigned int comittedls=0;
00484   
00485   unsigned long long branch_id=3;
00486   std::string branch_name("DATA");
00487   lumi::RevisionDML revisionDML;
00488   lumi::RevisionDML::LumiEntry lumirundata;
00489   std::stringstream op;
00490   op<<irunnumber;
00491   std::string runnumberStr=op.str();
00492   session->transaction().start(false);
00493   lumirundata.entry_name=runnumberStr;
00494   lumirundata.source=source;
00495   lumirundata.runnumber=irunnumber;
00496   lumirundata.bgev=bgev;
00497   lumirundata.entry_id=revisionDML.getEntryInBranchByName(session->nominalSchema(),lumi::LumiNames::lumidataTableName(),runnumberStr,branch_name);
00498   std::cout<<"entry_id "<<lumirundata.entry_id<<std::endl;
00499   if(lumirundata.entry_id==0){
00500     revisionDML.bookNewEntry(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata);
00501     std::cout<<"lumirundata revision_id "<<lumirundata.revision_id<<" entry_id "<<lumirundata.entry_id<<" data_id "<<lumirundata.data_id<<std::endl;
00502     revisionDML.addEntry(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata,branch_id,branch_name);
00503   }else{
00504     revisionDML.bookNewRevision(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata);
00505     std::cout<<"lumirundata revision_id "<<lumirundata.revision_id<<" entry_id "<<lumirundata.entry_id<<" data_id "<<lumirundata.data_id<<std::endl;
00506     revisionDML.addRevision(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata,branch_id,branch_name);
00507   }
00508   std::cout<<"inserting lumirundata "<<std::endl;
00509   revisionDML.insertLumiRunData(session->nominalSchema(),lumirundata);
00510   std::cout<<"inserting lslumi data"<<std::endl;
00511   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
00512     if(!session->transaction().isActive()){ 
00513       session->transaction().start(false);
00514       coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
00515       summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
00516     }else{
00517       if(lumiIt==lumiBeg){
00518         coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
00519         summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
00520       }
00521     }
00522     data_id = lumirundata.data_id;
00523     lumirunnum = irunnumber;
00524     lumilsnr = lumiIt->lumilsnr;
00525     cmslsnr = lumiIt->cmslsnr;
00526     instlumi = lumiIt->instlumi; // not calibrated!
00527     instlumierror = lumiIt->instlumierror; // not calibrated!
00528     instlumiquality = lumiIt->instlumiquality;
00529     beamstatus = lumiIt->beammode;  
00530     beamenergy = lumiIt->beamenergy;
00531     numorbit = lumiIt->numorbit;
00532     startorbit = lumiIt->startorbit;
00533     short nlivebx=lumiIt->nlivebx;
00534     //std::cout<<"nlivebx "<<nlivebx<<std::endl;
00535     if(nlivebx!=0){
00536       bxindex.resize(sizeof(short)*nlivebx);
00537       beamintensity_1.resize(sizeof(float)*nlivebx);
00538       beamintensity_2.resize(sizeof(float)*nlivebx);
00539       void* bxindex_StartAddress = bxindex.startingAddress();      
00540       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
00541       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
00542       std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
00543       std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
00544       std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
00545       //::free(lumiIt->bxindex);
00546       //::free(lumiIt->beamintensity_1);
00547       //::free(lumiIt->beamintensity_2);
00548     }else{
00549       bxindex.resize(0);
00550       beamintensity_1.resize(0);
00551       beamintensity_2.resize(0);
00552     }    
00553     for( unsigned int j=0; j<lumi:: N_LUMIALGO; ++j ){
00554       std::vector<PerBXData>::const_iterator bxIt;
00555       std::vector<PerBXData>::const_iterator bxBeg;
00556       std::vector<PerBXData>::const_iterator bxEnd;
00557       if(j==0) {//the push_back order in the input data is ET,OCC1,OCC2
00558         //algoname=std::string("ET");
00559         bxBeg=lumiIt->bxET.begin();
00560         bxEnd=lumiIt->bxET.end();
00561         float lumivalue[lumi::N_BX]={0.0};
00562         float lumierror[lumi::N_BX]={0.0};
00563         int lumiquality[lumi::N_BX]={0};
00564         bxlumivalue_et.resize(sizeof(float)*lumi::N_BX);
00565         bxlumierror_et.resize(sizeof(float)*lumi::N_BX);
00566         bxlumiquality_et.resize(sizeof(short)*lumi::N_BX);
00567         void* bxlumivalueStartAddress=bxlumivalue_et.startingAddress();
00568         void* bxlumierrorStartAddress=bxlumierror_et.startingAddress();
00569         void* bxlumiqualityStartAddress=bxlumiquality_et.startingAddress();
00570         unsigned int k=0;
00571         for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){          
00572           lumivalue[k]=bxIt->lumivalue;
00573           lumierror[k]=bxIt->lumierr;
00574           lumiquality[k]=bxIt->lumiquality;
00575         }
00576         std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00577         std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00578         std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00579       }
00580       if(j==1) {
00581         //algoname=std::string("OCC1");
00582         bxBeg=lumiIt->bxOCC1.begin();
00583         bxEnd=lumiIt->bxOCC1.end();
00584         float lumivalue[lumi::N_BX]={0.0};
00585         float lumierror[lumi::N_BX]={0.0};
00586         int lumiquality[lumi::N_BX]={0};
00587         bxlumivalue_occ1.resize(sizeof(float)*lumi::N_BX);
00588         bxlumierror_occ1.resize(sizeof(float)*lumi::N_BX);
00589         bxlumiquality_occ1.resize(sizeof(short)*lumi::N_BX);
00590         void* bxlumivalueStartAddress=bxlumivalue_occ1.startingAddress();
00591         void* bxlumierrorStartAddress=bxlumierror_occ1.startingAddress();
00592         void* bxlumiqualityStartAddress=bxlumiquality_occ1.startingAddress();
00593         unsigned int k=0;
00594         for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){          
00595           lumivalue[k]=bxIt->lumivalue;
00596           lumierror[k]=bxIt->lumierr;
00597           lumiquality[k]=bxIt->lumiquality;
00598         }
00599         std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00600         std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00601         std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00602       }
00603       if(j==2) {
00604         //algoname=std::string("OCC2");
00605         bxBeg=lumiIt->bxOCC2.begin();
00606         bxEnd=lumiIt->bxOCC2.end();
00607         float lumivalue[lumi::N_BX]={0.0};
00608         float lumierror[lumi::N_BX]={0.0};
00609         int lumiquality[lumi::N_BX]={0};
00610         bxlumivalue_occ2.resize(sizeof(float)*lumi::N_BX);
00611         bxlumierror_occ2.resize(sizeof(float)*lumi::N_BX);
00612         bxlumiquality_occ2.resize(sizeof(short)*lumi::N_BX);
00613         void* bxlumivalueStartAddress=bxlumivalue_occ2.startingAddress();
00614         void* bxlumierrorStartAddress=bxlumierror_occ2.startingAddress();
00615         void* bxlumiqualityStartAddress=bxlumiquality_occ2.startingAddress();
00616         unsigned int k=0;
00617         for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){          
00618           lumivalue[k]=bxIt->lumivalue;
00619           lumierror[k]=bxIt->lumierr;
00620           lumiquality[k]=bxIt->lumiquality;
00621         }
00622         std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00623         std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00624         std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00625       }
00626     }
00627     summaryInserter->processNextIteration();
00628     summaryInserter->flush();
00629     ++comittedls;
00630     if(comittedls==Lumi2DB::COMMITLSINTERVAL){
00631       std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00632       delete summaryInserter;
00633       summaryInserter=0;
00634       session->transaction().commit();
00635       comittedls=0;
00636       std::cout<<"\t committed "<<std::endl; 
00637     }else if( lumiindx==(totallumils-1) ){
00638       std::cout<<"\t committing at the end"<<std::endl; 
00639       delete summaryInserter; summaryInserter=0;
00640       session->transaction().commit();
00641       std::cout<<"\t done"<<std::endl; 
00642     }
00643   }
00644 }
00645 
00646 void lumi::Lumi2DB::cleanTemporaryMemory( lumi::Lumi2DB::LumiResult::iterator lumiBeg,
00647                                           lumi::Lumi2DB::LumiResult::iterator lumiEnd){
00648   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
00649   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
00650     ::free(lumiIt->bxindex);
00651     ::free(lumiIt->beamintensity_1);
00652     ::free(lumiIt->beamintensity_2);
00653   }
00654   
00655 }
00656 lumi::Lumi2DB::Lumi2DB(const std::string& dest):DataPipe(dest){}
00657 void 
00658 lumi::Lumi2DB::parseSourceString(lumi::Lumi2DB::LumiSource& result)const{
00659   //parse lumi source file name
00660   if(m_source.length()==0) throw lumi::Exception("lumi source is not set","parseSourceString","Lumi2DB");
00661   //std::cout<<"source "<<m_source<<std::endl;
00662   size_t tempIndex = m_source.find_last_of(".");
00663   size_t nameLength = m_source.length();
00664   std::string FileSuffix= m_source.substr(tempIndex+1,nameLength - tempIndex);
00665   std::string::size_type lastPos=m_source.find_first_not_of("_",0);
00666   std::string::size_type pos = m_source.find_first_of("_",lastPos);
00667   std::vector<std::string> pieces;
00668   while( std::string::npos != pos || std::string::npos != lastPos){
00669     pieces.push_back(m_source.substr(lastPos,pos-lastPos));
00670     lastPos=m_source.find_first_not_of("_",pos);
00671     pos=m_source.find_first_of("_",lastPos);
00672   }
00673   if( pieces[1]!="LUMI" || pieces[2]!="RAW" || FileSuffix!="root"){
00674     throw lumi::Exception("not lumi raw data file CMS_LUMI_RAW","parseSourceString","Lumi2DB");
00675   }
00676   std::strcpy(result.datestr,pieces[3].c_str());
00677   std::strcpy(result.version,pieces[5].c_str());
00678   //std::cout<<"version : "<<result.version<<std::endl;
00679   result.run = atoi(pieces[4].c_str());
00680   //std::cout<<"run : "<<result.run<<std::endl;
00681   result.firstsection = atoi(pieces[5].c_str());
00682   //std::cout<<"first section : "<< result.firstsection<<std::endl;
00683 }
00684 
00685 void
00686 lumi::Lumi2DB::retrieveBeamIntensity(HCAL_HLX::DIP_COMBINED_DATA* dataPtr, Lumi2DB::beamData&b)const{
00687    if(dataPtr==0){
00688       std::cout<<"HCAL_HLX::DIP_COMBINED_DATA* dataPtr=0"<<std::endl;
00689       b.bxindex=0;
00690       b.beamintensity_1=0;
00691       b.beamintensity_2=0;
00692       b.nlivebx=0;
00693    }else{
00694       b.bxindex=(short*)::malloc(sizeof(short)*lumi::N_BX);
00695       b.beamintensity_1=(float*)::malloc(sizeof(float)*lumi::N_BX);
00696       b.beamintensity_2=(float*)::malloc(sizeof(float)*lumi::N_BX);
00697       
00698       short a=0;//a is position in lumidetail array
00699       for(unsigned int i=0;i<lumi::N_BX;++i){
00700          if( i==0 ){
00701             if(dataPtr->Beam[0].averageBunchIntensities[0]>0 || dataPtr->Beam[1].averageBunchIntensities[0]>0 ){
00702                b.bxindex[a]=0;
00703                b.beamintensity_1[a]=dataPtr->Beam[0].averageBunchIntensities[0];
00704                b.beamintensity_2[a]=dataPtr->Beam[1].averageBunchIntensities[0];
00705                ++a;
00706             }
00707             continue;
00708          }
00709          if(dataPtr->Beam[0].averageBunchIntensities[i-1]>0 || dataPtr->Beam[1].averageBunchIntensities[i-1]>0){
00710             b.bxindex[a]=i;
00711             b.beamintensity_1[a]=dataPtr->Beam[0].averageBunchIntensities[i-1];
00712             b.beamintensity_2[a]=dataPtr->Beam[1].averageBunchIntensities[i-1];
00713             ++a;
00714             //if(i!=0){
00715             // std::cout<<"beam intensity "<<dataPtr->sectionNumber<<" "<<dataPtr->timestamp-1262300400<<" "<<(i-1)*10+1<<" "<<b.beamintensity_1[a]<<" "<<b.beamintensity_2[a]<<std::endl;
00716             //}
00717          }
00718       }
00719       b.nlivebx=a;
00720    }
00721 }
00722 void 
00723 lumi::Lumi2DB::retrieveData( unsigned int runnumber){
00724   lumi::Lumi2DB::LumiResult lumiresult;
00725   //check filename is in  lumiraw format
00726   lumi::Lumi2DB::LumiSource filenamecontent;
00727   try{
00728     parseSourceString(filenamecontent);
00729   }catch(const lumi::Exception& er){
00730     std::cout<<er.what()<<std::endl;
00731     throw er;
00732   }
00733   if( filenamecontent.run!=runnumber ){
00734     throw lumi::Exception("runnumber in file name does not match requested run number","retrieveData","Lumi2DB");
00735   }
00736   TFile* source=TFile::Open(m_source.c_str(),"READ");
00737   TTree *hlxtree = (TTree*)source->Get("HLXData");
00738   if(!hlxtree){
00739     throw lumi::Exception(std::string("non-existing HLXData "),"retrieveData","Lumi2DB");
00740   }
00741   //hlxtree->Print();
00742   std::auto_ptr<HCAL_HLX::LUMI_SECTION> localSection(new HCAL_HLX::LUMI_SECTION);
00743   HCAL_HLX::LUMI_SECTION_HEADER* lumiheader = &(localSection->hdr);
00744   HCAL_HLX::LUMI_SUMMARY* lumisummary = &(localSection->lumiSummary);
00745   HCAL_HLX::LUMI_DETAIL* lumidetail = &(localSection->lumiDetail);
00746   
00747   hlxtree->SetBranchAddress("Header.",&lumiheader);
00748   hlxtree->SetBranchAddress("Summary.",&lumisummary);
00749   hlxtree->SetBranchAddress("Detail.",&lumidetail);
00750    
00751   size_t nentries=hlxtree->GetEntries();
00752   unsigned int nstablebeam=0;
00753   float bgev=0.0;
00754   //source->GetListOfKeys()->Print();
00755   std::map<unsigned int, Lumi2DB::beamData> dipmap;
00756   TTree *diptree= (TTree*)source->Get("DIPCombined");
00757   if(diptree){
00758     //throw lumi::Exception(std::string("non-existing DIPData "),"retrieveData","Lumi2DB");
00759     std::auto_ptr<HCAL_HLX::DIP_COMBINED_DATA> dipdata(new HCAL_HLX::DIP_COMBINED_DATA);
00760     diptree->SetBranchAddress("DIPCombined.",&dipdata);
00761     size_t ndipentries=diptree->GetEntries();
00762     for(size_t i=0;i<ndipentries;++i){
00763       diptree->GetEntry(i);
00764       //unsigned int fillnumber=dipdata->FillNumber;
00765       //std::vector<short> collidingidx;collidingidx.reserve(LUMI::N_BX);
00766       //for(unsigned int i=0;i<lumi::N_BX;++i){
00767       //int isb1colliding=dipdata->beam[0].beamConfig[i];
00768       //int isb2colliding=dipdata->beam[1].beamConfig[i];
00769       //if(isb1colliding && isb2colliding&&isb1colliding==1&&isb2colliding==1){
00770       //  collidingidx.push_back(i);
00771       //        }
00772       //}
00773       beamData b;
00774       //std::cout<<"Beam Mode : "<<dipdata->beamMode<<"\n";
00775       //std::cout<<"Beam Energy : "<<dipdata->Energy<<"\n";
00776       //std::cout<<"sectionUmber : "<<dipdata->sectionNumber<<"\n";
00777       unsigned int dipls=dipdata->sectionNumber;
00778       if (std::string(dipdata->beamMode).empty()){
00779         b.mode="N/A";
00780       }else{
00781         b.mode=dipdata->beamMode;
00782       }
00783       b.energy=dipdata->Energy;
00784       if(b.mode=="STABLE BEAMS"){
00785         ++nstablebeam;
00786         bgev+=b.energy;
00787       }
00788       this->retrieveBeamIntensity(dipdata.get(),b);
00789       dipmap.insert(std::make_pair(dipls,b));
00790     }
00791   }else{
00792     for(size_t i=0;i<nentries;++i){
00793       beamData b;
00794       b.mode="N/A";
00795       b.energy=0.0;
00796       this->retrieveBeamIntensity(0,b);
00797       dipmap.insert(std::make_pair(i,b));
00798     }
00799   }
00800   //diptree->Print();
00801  
00802   size_t ncmslumi=0;
00803   std::cout<<"processing total lumi lumisection "<<nentries<<std::endl;
00804   //size_t lumisecid=0;
00805   //unsigned int lumilumisecid=0;
00806   //runnumber=lumiheader->runNumber;
00807   //
00808   //hardcode the first LS is always alive
00809   //
00810   for(size_t i=0;i<nentries;++i){
00811     lumi::Lumi2DB::PerLumiData h;
00812     h.cmsalive=1;
00813     hlxtree->GetEntry(i);
00814     //std::cout<<"live flag "<<lumiheader->bCMSLive <<std::endl;
00815     if( !lumiheader->bCMSLive && i!=0){
00816       std::cout<<"\t non-CMS LS "<<lumiheader->sectionNumber<<" ";
00817       h.cmsalive=0;
00818     }
00819     ++ncmslumi;
00820     
00821     h.bxET.reserve(lumi::N_BX);
00822     h.bxOCC1.reserve(lumi::N_BX);
00823     h.bxOCC2.reserve(lumi::N_BX);
00824     
00825     //runnumber=lumiheader->runNumber;
00826     //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));
00827     h.lumilsnr=lumiheader->sectionNumber;
00828     std::map<unsigned int , Lumi2DB::beamData >::iterator beamIt=dipmap.find(h.lumilsnr);
00829     if ( beamIt!=dipmap.end() ){
00830       h.beammode=beamIt->second.mode;
00831       h.beamenergy=beamIt->second.energy;
00832       h.nlivebx=beamIt->second.nlivebx;
00833       if(h.nlivebx!=0){
00834         h.bxindex=(short*)malloc(sizeof(short)*h.nlivebx);
00835         h.beamintensity_1=(float*)malloc(sizeof(float)*h.nlivebx);
00836         h.beamintensity_2=(float*)malloc(sizeof(float)*h.nlivebx);
00837         if(h.bxindex==0 || h.beamintensity_1==0 || h.beamintensity_2==0){
00838           std::cout<<"malloc failed"<<std::endl;
00839         }
00840         //std::cout<<"h.bxindex size "<<sizeof(short)*h.nlivebx<<std::endl;
00841         //std::cout<<"h.beamintensity_1 size "<<sizeof(float)*h.nlivebx<<std::endl;
00842         //std::cout<<"h.beamintensity_2 size "<<sizeof(float)*h.nlivebx<<std::endl;
00843 
00844         std::memmove(h.bxindex,beamIt->second.bxindex,sizeof(short)*h.nlivebx);
00845         std::memmove(h.beamintensity_1,beamIt->second.beamintensity_1,sizeof(float)*h.nlivebx);
00846         std::memmove(h.beamintensity_2,beamIt->second.beamintensity_2,sizeof(float)*h.nlivebx);
00847 
00848         ::free(beamIt->second.bxindex);beamIt->second.bxindex=0;
00849         ::free(beamIt->second.beamintensity_1);beamIt->second.beamintensity_1=0;
00850         ::free(beamIt->second.beamintensity_2);beamIt->second.beamintensity_2=0;
00851       }else{
00852         //std::cout<<"h.nlivebx is zero"<<std::endl;
00853         h.bxindex=0;
00854         h.beamintensity_1=0;
00855         h.beamintensity_2=0;
00856       }
00857     }else{
00858       h.beammode="N/A";
00859       h.beamenergy=0.0;
00860       h.nlivebx=0;
00861       h.bxindex=0;
00862       h.beamintensity_1=0;
00863       h.beamintensity_2=0;
00864     }
00865     h.startorbit=lumiheader->startOrbit;
00866     h.numorbit=lumiheader->numOrbits;
00867     if(h.cmsalive==0){
00868       h.cmslsnr=0; //the dead ls has cmsls number=0
00869     }else{
00870       h.cmslsnr=ncmslumi;//we guess cms lumils
00871     }
00872     h.instlumi=lumisummary->InstantLumi;
00873     //std::cout<<"instant lumi "<<lumisummary->InstantLumi<<std::endl;
00874     h.instlumierror=lumisummary->InstantLumiErr;
00875     h.lumisectionquality=lumisummary->InstantLumiQlty;
00876     h.dtnorm=lumisummary->DeadTimeNormalization;
00877     h.lhcnorm=lumisummary->LHCNormalization;
00878     //unsigned int timestp=lumiheader->timestamp;
00879     //std::cout<<"cmslsnum "<<ncmslumi<<"timestp "<<timestp<<std::endl;
00880     for(size_t i=0;i<lumi::N_BX;++i){
00881       lumi::Lumi2DB::PerBXData bET;
00882       lumi::Lumi2DB::PerBXData bOCC1;
00883       lumi::Lumi2DB::PerBXData bOCC2;
00884       //bET.idx=i+1;
00885       bET.lumivalue=lumidetail->ETLumi[i];
00886       bET.lumierr=lumidetail->ETLumiErr[i];
00887       bET.lumiquality=lumidetail->ETLumiQlty[i];      
00888       h.bxET.push_back(bET);
00889 
00890       //bOCC1.idx=i+1;
00891 
00892       bOCC1.lumivalue=lumidetail->OccLumi[0][i];
00893       bOCC1.lumierr=lumidetail->OccLumiErr[0][i];
00900       bOCC1.lumiquality=lumidetail->OccLumiQlty[0][i]; 
00901       h.bxOCC1.push_back(bOCC1);
00902           
00903       //bOCC2.idx=i+1;
00904       bOCC2.lumivalue=lumidetail->OccLumi[1][i];
00905       bOCC2.lumierr=lumidetail->OccLumiErr[1][i];
00906       bOCC2.lumiquality=lumidetail->OccLumiQlty[1][i]; 
00907       h.bxOCC2.push_back(bOCC2);
00908     }
00909     lumiresult.push_back(h);
00910   }
00911   std::cout<<std::endl;
00912   if(nstablebeam!=0){
00913     bgev=bgev/nstablebeam;//nominal beam energy=sum(energy)/nstablebeams
00914   }
00915   std::cout<<"avg stable beam energy "<<bgev<<std::endl;
00916   if( !m_novalidate && !isLumiDataValid(lumiresult.begin(),lumiresult.end()) ){
00917     throw lumi::invalidDataException("all lumi values are <0.5e-08","isLumiDataValid","Lumi2DB");
00918   }
00919   if( !m_nocheckingstablebeam && !hasStableBeam(lumiresult.begin(),lumiresult.end()) ){
00920     throw lumi::noStableBeamException("no LS has STABLE BEAMS","hasStableBeam","Lumi2DB");
00921   }
00922   coral::ConnectionService* svc=new coral::ConnectionService;
00923   lumi::DBConfig dbconf(*svc);
00924   if(!m_authpath.empty()){
00925     dbconf.setAuthentication(m_authpath);
00926   }
00927   coral::ISessionProxy* session=svc->connect(m_dest,coral::Update);
00928   coral::ITypeConverter& tpc=session->typeConverter();
00929   tpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00930   //
00931   //write to old lumisummary
00932   //
00933   try{
00934     const std::string lversion(filenamecontent.version);
00935     if(m_mode==std::string("beamintensity_only")){
00936       std::cout<<"writing beam intensity only to old lumisummary table "<<std::endl;
00937       writeBeamIntensityOnly(session,runnumber,lversion,lumiresult.begin(),lumiresult.end());
00938       std::cout<<"done"<<std::endl;
00939     }else{
00940        if(m_mode=="loadoldschema"){
00941           std::cout<<"writing all lumi data to old lumisummary table "<<std::endl;
00942           writeAllLumiData(session,runnumber,lversion,lumiresult.begin(),lumiresult.end());     
00943           std::cout<<"done"<<std::endl;
00944        }
00945     }
00946     std::cout<<"writing all lumi data to lumisummary_V2 table "<<std::endl;
00947     writeAllLumiDataToSchema2(session,m_source,runnumber,bgev,lumiresult.begin(),lumiresult.end());
00948     std::cout<<"done"<<std::endl;
00949     cleanTemporaryMemory(lumiresult.begin(),lumiresult.end());
00950     delete session;
00951     delete svc;
00952   }catch( const coral::Exception& er){
00953     std::cout<<"database error "<<er.what()<<std::endl;
00954     session->transaction().rollback();
00955     delete session;
00956     delete svc;
00957     throw er;
00958   }
00959 }
00960 const std::string lumi::Lumi2DB::dataType() const{
00961   return "LUMI";
00962 }
00963 const std::string lumi::Lumi2DB::sourceType() const{
00964   return "DB";
00965 }
00966 lumi::Lumi2DB::~Lumi2DB(){}
00967 
00968 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00969 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::Lumi2DB,"Lumi2DB");
00970 #endif