CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/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;//how much is in the beamintensity vector
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,unsigned int ncollidingbunches,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                             unsigned int ncollidingbunches,
00427                             lumi::Lumi2DB::LumiResult::iterator lumiBeg,
00428                             lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
00429   std::cout<<"writeAllLumiDataToSchema2"<<std::endl;
00430   coral::AttributeList summaryData;
00431   summaryData.extend("DATA_ID",typeid(unsigned long long));
00432   summaryData.extend("RUNNUM",typeid(unsigned int));
00433   summaryData.extend("LUMILSNUM",typeid(unsigned int));
00434   summaryData.extend("CMSLSNUM",typeid(unsigned int));
00435   summaryData.extend("INSTLUMI",typeid(float));
00436   summaryData.extend("INSTLUMIERROR",typeid(float));
00437   summaryData.extend("INSTLUMIQUALITY",typeid(short));
00438   summaryData.extend("BEAMSTATUS",typeid(std::string));
00439   summaryData.extend("BEAMENERGY",typeid(float));
00440   summaryData.extend("NUMORBIT",typeid(unsigned int));
00441   summaryData.extend("STARTORBIT",typeid(unsigned int));
00442   summaryData.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
00443   summaryData.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
00444   summaryData.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
00445   summaryData.extend("BXLUMIVALUE_OCC1",typeid(coral::Blob));
00446   summaryData.extend("BXLUMIERROR_OCC1",typeid(coral::Blob));
00447   summaryData.extend("BXLUMIQUALITY_OCC1",typeid(coral::Blob));
00448   summaryData.extend("BXLUMIVALUE_OCC2",typeid(coral::Blob));
00449   summaryData.extend("BXLUMIERROR_OCC2",typeid(coral::Blob));
00450   summaryData.extend("BXLUMIQUALITY_OCC2",typeid(coral::Blob));
00451   summaryData.extend("BXLUMIVALUE_ET",typeid(coral::Blob));
00452   summaryData.extend("BXLUMIERROR_ET",typeid(coral::Blob));
00453   summaryData.extend("BXLUMIQUALITY_ET",typeid(coral::Blob));
00454 
00455   unsigned long long& data_id=summaryData["DATA_ID"].data<unsigned long long>();
00456   unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
00457   unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
00458   unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
00459   float& instlumi = summaryData["INSTLUMI"].data<float>();
00460   float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
00461   short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
00462   std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
00463   float& beamenergy = summaryData["BEAMENERGY"].data<float>(); 
00464   unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
00465   unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
00466   coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
00467   coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
00468   coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();  
00469   coral::Blob& bxlumivalue_et=summaryData["BXLUMIVALUE_ET"].data<coral::Blob>();
00470   coral::Blob& bxlumierror_et=summaryData["BXLUMIERROR_ET"].data<coral::Blob>();
00471   coral::Blob& bxlumiquality_et=summaryData["BXLUMIQUALITY_ET"].data<coral::Blob>();
00472   coral::Blob& bxlumivalue_occ1=summaryData["BXLUMIVALUE_OCC1"].data<coral::Blob>();
00473   coral::Blob& bxlumierror_occ1=summaryData["BXLUMIERROR_OCC1"].data<coral::Blob>();
00474   coral::Blob& bxlumiquality_occ1=summaryData["BXLUMIQUALITY_OCC1"].data<coral::Blob>();
00475   coral::Blob& bxlumivalue_occ2=summaryData["BXLUMIVALUE_OCC2"].data<coral::Blob>();
00476   coral::Blob& bxlumierror_occ2=summaryData["BXLUMIERROR_OCC2"].data<coral::Blob>();
00477   coral::Blob& bxlumiquality_occ2=summaryData["BXLUMIQUALITY_OCC2"].data<coral::Blob>();
00478 
00479   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
00480   coral::IBulkOperation* summaryInserter=0;
00481 
00482   unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
00483   unsigned int lumiindx=0;
00484   unsigned int comittedls=0;
00485   
00486   unsigned long long branch_id=3;
00487   std::string branch_name("DATA");
00488   lumi::RevisionDML revisionDML;
00489   lumi::RevisionDML::LumiEntry lumirundata;
00490   std::stringstream op;
00491   op<<irunnumber;
00492   std::string runnumberStr=op.str();
00493   session->transaction().start(false);
00494   lumirundata.entry_name=runnumberStr;
00495   lumirundata.source=source;
00496   lumirundata.runnumber=irunnumber;
00497   lumirundata.bgev=bgev;
00498   lumirundata.ncollidingbunches=ncollidingbunches;
00499   lumirundata.entry_id=revisionDML.getEntryInBranchByName(session->nominalSchema(),lumi::LumiNames::lumidataTableName(),runnumberStr,branch_name);
00500   //std::cout<<"entry_id "<<lumirundata.entry_id<<std::endl;
00501   if(lumirundata.entry_id==0){
00502     revisionDML.bookNewEntry(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata);
00503     //std::cout<<"lumirundata revision_id "<<lumirundata.revision_id<<" entry_id "<<lumirundata.entry_id<<" data_id "<<lumirundata.data_id<<std::endl;
00504     revisionDML.addEntry(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata,branch_id,branch_name);
00505   }else{
00506     revisionDML.bookNewRevision(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata);
00507     //std::cout<<"lumirundata revision_id "<<lumirundata.revision_id<<" entry_id "<<lumirundata.entry_id<<" data_id "<<lumirundata.data_id<<std::endl;
00508     revisionDML.addRevision(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata,branch_id,branch_name);
00509   }
00510   revisionDML.insertLumiRunData(session->nominalSchema(),lumirundata);
00511   //summaryData.toOutputStream(std::cout);
00512   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
00513     if(!session->transaction().isActive()){ 
00514       session->transaction().start(false);
00515       coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
00516       summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
00517     }else{
00518       if(lumiIt==lumiBeg){
00519         coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
00520         summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
00521       }
00522     }
00523     data_id = lumirundata.data_id;
00524     lumirunnum = irunnumber;
00525     lumilsnr = lumiIt->lumilsnr;
00526     cmslsnr = lumiIt->cmslsnr;
00527     instlumi = lumiIt->instlumi; // not calibrated!
00528     instlumierror = lumiIt->instlumierror; // not calibrated!
00529     instlumiquality = lumiIt->instlumiquality;
00530     beamstatus = lumiIt->beammode;  
00531     beamenergy = lumiIt->beamenergy;
00532     numorbit = lumiIt->numorbit;
00533     startorbit = lumiIt->startorbit;
00534     short nlivebx=lumiIt->nlivebx;
00535     //std::cout<<"nlivebx "<<nlivebx<<std::endl;
00536     if(nlivebx!=0){
00537       bxindex.resize(sizeof(short)*nlivebx);
00538       beamintensity_1.resize(sizeof(float)*nlivebx);
00539       beamintensity_2.resize(sizeof(float)*nlivebx);
00540       void* bxindex_StartAddress = bxindex.startingAddress();      
00541       void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
00542       void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
00543       std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
00544       std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
00545       std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
00546       //::free(lumiIt->bxindex);
00547       //::free(lumiIt->beamintensity_1);
00548       //::free(lumiIt->beamintensity_2);
00549     }else{
00550       bxindex.resize(0);
00551       beamintensity_1.resize(0);
00552       beamintensity_2.resize(0);
00553     }    
00554     for( unsigned int j=0; j<lumi:: N_LUMIALGO; ++j ){
00555       std::vector<PerBXData>::const_iterator bxIt;
00556       std::vector<PerBXData>::const_iterator bxBeg;
00557       std::vector<PerBXData>::const_iterator bxEnd;
00558       if(j==0) {//the push_back order in the input data is ET,OCC1,OCC2
00559         //algoname=std::string("ET");
00560         bxBeg=lumiIt->bxET.begin();
00561         bxEnd=lumiIt->bxET.end();
00562         float lumivalue[lumi::N_BX]={0.0};
00563         float lumierror[lumi::N_BX]={0.0};
00564         int lumiquality[lumi::N_BX]={0};
00565         bxlumivalue_et.resize(sizeof(float)*lumi::N_BX);
00566         bxlumierror_et.resize(sizeof(float)*lumi::N_BX);
00567         bxlumiquality_et.resize(sizeof(short)*lumi::N_BX);
00568         void* bxlumivalueStartAddress=bxlumivalue_et.startingAddress();
00569         void* bxlumierrorStartAddress=bxlumierror_et.startingAddress();
00570         void* bxlumiqualityStartAddress=bxlumiquality_et.startingAddress();
00571         unsigned int k=0;
00572         for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){          
00573           lumivalue[k]=bxIt->lumivalue;
00574           lumierror[k]=bxIt->lumierr;
00575           lumiquality[k]=bxIt->lumiquality;
00576         }
00577         std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00578         std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00579         std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00580       }
00581       if(j==1) {
00582         //algoname=std::string("OCC1");
00583         bxBeg=lumiIt->bxOCC1.begin();
00584         bxEnd=lumiIt->bxOCC1.end();
00585         float lumivalue[lumi::N_BX]={0.0};
00586         float lumierror[lumi::N_BX]={0.0};
00587         int lumiquality[lumi::N_BX]={0};
00588         bxlumivalue_occ1.resize(sizeof(float)*lumi::N_BX);
00589         bxlumierror_occ1.resize(sizeof(float)*lumi::N_BX);
00590         bxlumiquality_occ1.resize(sizeof(short)*lumi::N_BX);
00591         void* bxlumivalueStartAddress=bxlumivalue_occ1.startingAddress();
00592         void* bxlumierrorStartAddress=bxlumierror_occ1.startingAddress();
00593         void* bxlumiqualityStartAddress=bxlumiquality_occ1.startingAddress();
00594         unsigned int k=0;
00595         for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){          
00596           lumivalue[k]=bxIt->lumivalue;
00597           lumierror[k]=bxIt->lumierr;
00598           lumiquality[k]=bxIt->lumiquality;
00599         }
00600         std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00601         std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00602         std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00603       }
00604       if(j==2) {
00605         //algoname=std::string("OCC2");
00606         bxBeg=lumiIt->bxOCC2.begin();
00607         bxEnd=lumiIt->bxOCC2.end();
00608         float lumivalue[lumi::N_BX]={0.0};
00609         float lumierror[lumi::N_BX]={0.0};
00610         int lumiquality[lumi::N_BX]={0};
00611         bxlumivalue_occ2.resize(sizeof(float)*lumi::N_BX);
00612         bxlumierror_occ2.resize(sizeof(float)*lumi::N_BX);
00613         bxlumiquality_occ2.resize(sizeof(short)*lumi::N_BX);
00614         void* bxlumivalueStartAddress=bxlumivalue_occ2.startingAddress();
00615         void* bxlumierrorStartAddress=bxlumierror_occ2.startingAddress();
00616         void* bxlumiqualityStartAddress=bxlumiquality_occ2.startingAddress();
00617         unsigned int k=0;
00618         for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k  ){          
00619           lumivalue[k]=bxIt->lumivalue;
00620           lumierror[k]=bxIt->lumierr;
00621           lumiquality[k]=bxIt->lumiquality;
00622         }
00623         std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
00624         std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
00625         std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
00626       }
00627     }
00628     summaryInserter->processNextIteration();
00629     summaryInserter->flush();
00630     ++comittedls;
00631     if(comittedls==Lumi2DB::COMMITLSINTERVAL){
00632       std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl; 
00633       delete summaryInserter;
00634       summaryInserter=0;
00635       session->transaction().commit();
00636       comittedls=0;
00637       std::cout<<"\t committed "<<std::endl; 
00638     }else if( lumiindx==(totallumils-1) ){
00639       std::cout<<"\t committing at the end"<<std::endl; 
00640       delete summaryInserter; summaryInserter=0;
00641       session->transaction().commit();
00642       std::cout<<"\t done"<<std::endl; 
00643     }
00644   }
00645 }
00646 
00647 void lumi::Lumi2DB::cleanTemporaryMemory( lumi::Lumi2DB::LumiResult::iterator lumiBeg,
00648                                           lumi::Lumi2DB::LumiResult::iterator lumiEnd){
00649   lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
00650   for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
00651     ::free(lumiIt->bxindex);
00652     ::free(lumiIt->beamintensity_1);
00653     ::free(lumiIt->beamintensity_2);
00654   }
00655   
00656 }
00657 lumi::Lumi2DB::Lumi2DB(const std::string& dest):DataPipe(dest){}
00658 void 
00659 lumi::Lumi2DB::parseSourceString(lumi::Lumi2DB::LumiSource& result)const{
00660   //parse lumi source file name
00661   if(m_source.length()==0) throw lumi::Exception("lumi source is not set","parseSourceString","Lumi2DB");
00662   //std::cout<<"source "<<m_source<<std::endl;
00663   size_t tempIndex = m_source.find_last_of(".");
00664   size_t nameLength = m_source.length();
00665   std::string FileSuffix= m_source.substr(tempIndex+1,nameLength - tempIndex);
00666   std::string::size_type lastPos=m_source.find_first_not_of("_",0);
00667   std::string::size_type pos = m_source.find_first_of("_",lastPos);
00668   std::vector<std::string> pieces;
00669   while( std::string::npos != pos || std::string::npos != lastPos){
00670     pieces.push_back(m_source.substr(lastPos,pos-lastPos));
00671     lastPos=m_source.find_first_not_of("_",pos);
00672     pos=m_source.find_first_of("_",lastPos);
00673   }
00674   if( pieces[1]!="LUMI" || pieces[2]!="RAW" || FileSuffix!="root"){
00675     throw lumi::Exception("not lumi raw data file CMS_LUMI_RAW","parseSourceString","Lumi2DB");
00676   }
00677   std::strcpy(result.datestr,pieces[3].c_str());
00678   std::strcpy(result.version,pieces[5].c_str());
00679   //std::cout<<"version : "<<result.version<<std::endl;
00680   result.run = atoi(pieces[4].c_str());
00681   //std::cout<<"run : "<<result.run<<std::endl;
00682   result.firstsection = atoi(pieces[5].c_str());
00683   //std::cout<<"first section : "<< result.firstsection<<std::endl;
00684 }
00685 
00686 void
00687 lumi::Lumi2DB::retrieveBeamIntensity(HCAL_HLX::DIP_COMBINED_DATA* dataPtr, Lumi2DB::beamData&b)const{
00688    if(dataPtr==0){
00689       std::cout<<"HCAL_HLX::DIP_COMBINED_DATA* dataPtr=0"<<std::endl;
00690       b.bxindex=0;
00691       b.beamintensity_1=0;
00692       b.beamintensity_2=0;
00693       b.nlivebx=0;
00694    }else{
00695       b.bxindex=(short*)::malloc(sizeof(short)*lumi::N_BX);
00696       b.beamintensity_1=(float*)::malloc(sizeof(float)*lumi::N_BX);
00697       b.beamintensity_2=(float*)::malloc(sizeof(float)*lumi::N_BX);
00698       
00699       short a=0;//a is position in lumidetail array
00700       for(unsigned int i=0;i<lumi::N_BX;++i){
00701          if( i==0 ){
00702             if(dataPtr->Beam[0].averageBunchIntensities[0]>0 || dataPtr->Beam[1].averageBunchIntensities[0]>0 ){
00703                b.bxindex[a]=0;
00704                b.beamintensity_1[a]=dataPtr->Beam[0].averageBunchIntensities[0];
00705                b.beamintensity_2[a]=dataPtr->Beam[1].averageBunchIntensities[0];
00706                ++a;
00707             }
00708             continue;
00709          }
00710          if(dataPtr->Beam[0].averageBunchIntensities[i-1]>0 || dataPtr->Beam[1].averageBunchIntensities[i-1]>0){
00711             b.bxindex[a]=i;
00712             b.beamintensity_1[a]=dataPtr->Beam[0].averageBunchIntensities[i-1];
00713             b.beamintensity_2[a]=dataPtr->Beam[1].averageBunchIntensities[i-1];
00714             ++a;
00715             //if(i!=0){
00716             // std::cout<<"beam intensity "<<dataPtr->sectionNumber<<" "<<dataPtr->timestamp-1262300400<<" "<<(i-1)*10+1<<" "<<b.beamintensity_1[a]<<" "<<b.beamintensity_2[a]<<std::endl;
00717             //}
00718          }
00719       }
00720       b.nlivebx=a;
00721    }
00722 }
00726 void 
00727 lumi::Lumi2DB::retrieveData( unsigned int runnumber){
00728   lumi::Lumi2DB::LumiResult lumiresult;
00729   //check filename is in  lumiraw format
00730   lumi::Lumi2DB::LumiSource filenamecontent;
00731   try{
00732     parseSourceString(filenamecontent);
00733   }catch(const lumi::Exception& er){
00734     std::cout<<er.what()<<std::endl;
00735     throw er;
00736   }
00737   if( filenamecontent.run!=runnumber ){
00738     throw lumi::Exception("runnumber in file name does not match requested run number","retrieveData","Lumi2DB");
00739   }
00740   TFile* source=TFile::Open(m_source.c_str(),"READ");
00741   TTree *hlxtree = (TTree*)source->Get("HLXData");
00742   if(!hlxtree){
00743     throw lumi::Exception(std::string("non-existing HLXData "),"retrieveData","Lumi2DB");
00744   }
00745   //hlxtree->Print();
00746   std::auto_ptr<HCAL_HLX::LUMI_SECTION> localSection(new HCAL_HLX::LUMI_SECTION);
00747   HCAL_HLX::LUMI_SECTION_HEADER* lumiheader = &(localSection->hdr);
00748   HCAL_HLX::LUMI_SUMMARY* lumisummary = &(localSection->lumiSummary);
00749   HCAL_HLX::LUMI_DETAIL* lumidetail = &(localSection->lumiDetail);
00750   
00751   hlxtree->SetBranchAddress("Header.",&lumiheader);
00752   hlxtree->SetBranchAddress("Summary.",&lumisummary);
00753   hlxtree->SetBranchAddress("Detail.",&lumidetail);
00754    
00755   size_t nentries=hlxtree->GetEntries();
00756   unsigned int nstablebeam=0;
00757   float bgev=0.0;
00758   //source->GetListOfKeys()->Print();
00759   std::map<unsigned int, Lumi2DB::beamData> dipmap;
00760   TTree *diptree= (TTree*)source->Get("DIPCombined");
00761   if(diptree){
00762     //throw lumi::Exception(std::string("non-existing DIPData "),"retrieveData","Lumi2DB");
00763     std::auto_ptr<HCAL_HLX::DIP_COMBINED_DATA> dipdata(new HCAL_HLX::DIP_COMBINED_DATA);
00764     diptree->SetBranchAddress("DIPCombined.",&dipdata);
00765     size_t ndipentries=diptree->GetEntries();
00766     for(size_t i=0;i<ndipentries;++i){
00767       diptree->GetEntry(i);
00768       //unsigned int fillnumber=dipdata->FillNumber;
00769       //std::vector<short> collidingidx;collidingidx.reserve(LUMI::N_BX);
00770       //for(unsigned int i=0;i<lumi::N_BX;++i){
00771       //int isb1colliding=dipdata->beam[0].beamConfig[i];
00772       //int isb2colliding=dipdata->beam[1].beamConfig[i];
00773       //if(isb1colliding && isb2colliding&&isb1colliding==1&&isb2colliding==1){
00774       //  collidingidx.push_back(i);
00775       //        }
00776       //}
00777       beamData b;
00778       //std::cout<<"Beam Mode : "<<dipdata->beamMode<<"\n";
00779       //std::cout<<"Beam Energy : "<<dipdata->Energy<<"\n";
00780       //std::cout<<"sectionUmber : "<<dipdata->sectionNumber<<"\n";
00781       unsigned int dipls=dipdata->sectionNumber;
00782       if (std::string(dipdata->beamMode).empty()){
00783         b.mode="N/A";
00784       }else{
00785         b.mode=dipdata->beamMode;
00786       }
00787       b.energy=dipdata->Energy;
00788       if(b.mode=="STABLE BEAMS"){
00789         ++nstablebeam;
00790         bgev+=b.energy;
00791       }
00792       this->retrieveBeamIntensity(dipdata.get(),b);
00793       dipmap.insert(std::make_pair(dipls,b));
00794     }
00795   }else{
00796     for(size_t i=0;i<nentries;++i){
00797       beamData b;
00798       b.mode="N/A";
00799       b.energy=0.0;
00800       this->retrieveBeamIntensity(0,b);
00801       dipmap.insert(std::make_pair(i,b));
00802     }
00803   }
00804   //diptree->Print();
00805  
00806   size_t ncmslumi=0;
00807   std::cout<<"processing total lumi lumisection "<<nentries<<std::endl;
00808   //size_t lumisecid=0;
00809   //unsigned int lumilumisecid=0;
00810   //runnumber=lumiheader->runNumber;
00811   //
00812   //hardcode the first LS is always alive
00813   //
00814   unsigned int ncollidingbunches=0;
00815   for(size_t i=0;i<nentries;++i){
00816     lumi::Lumi2DB::PerLumiData h;
00817     h.cmsalive=1;
00818     hlxtree->GetEntry(i);
00819     //std::cout<<"live flag "<<lumiheader->bCMSLive <<std::endl;
00820     if( !lumiheader->bCMSLive && i!=0){
00821       std::cout<<"\t non-CMS LS "<<lumiheader->sectionNumber<<" ";
00822       h.cmsalive=0;
00823     }
00824     ++ncmslumi;
00825     if(ncmslumi==1){//just take the first ls
00826       ncollidingbunches=lumiheader->numBunches;
00827     }
00828     h.bxET.reserve(lumi::N_BX);
00829     h.bxOCC1.reserve(lumi::N_BX);
00830     h.bxOCC2.reserve(lumi::N_BX);
00831     
00832     //runnumber=lumiheader->runNumber;
00833     //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));
00834     h.lumilsnr=lumiheader->sectionNumber;
00835     std::map<unsigned int , Lumi2DB::beamData >::iterator beamIt=dipmap.find(h.lumilsnr);
00836     if ( beamIt!=dipmap.end() ){
00837       h.beammode=beamIt->second.mode;
00838       h.beamenergy=beamIt->second.energy;
00839       h.nlivebx=beamIt->second.nlivebx;
00840       if(h.nlivebx!=0){
00841         h.bxindex=(short*)malloc(sizeof(short)*h.nlivebx);
00842         h.beamintensity_1=(float*)malloc(sizeof(float)*h.nlivebx);
00843         h.beamintensity_2=(float*)malloc(sizeof(float)*h.nlivebx);
00844         if(h.bxindex==0 || h.beamintensity_1==0 || h.beamintensity_2==0){
00845           std::cout<<"malloc failed"<<std::endl;
00846         }
00847         //std::cout<<"h.bxindex size "<<sizeof(short)*h.nlivebx<<std::endl;
00848         //std::cout<<"h.beamintensity_1 size "<<sizeof(float)*h.nlivebx<<std::endl;
00849         //std::cout<<"h.beamintensity_2 size "<<sizeof(float)*h.nlivebx<<std::endl;
00850 
00851         std::memmove(h.bxindex,beamIt->second.bxindex,sizeof(short)*h.nlivebx);
00852         std::memmove(h.beamintensity_1,beamIt->second.beamintensity_1,sizeof(float)*h.nlivebx);
00853         std::memmove(h.beamintensity_2,beamIt->second.beamintensity_2,sizeof(float)*h.nlivebx);
00854 
00855         ::free(beamIt->second.bxindex);beamIt->second.bxindex=0;
00856         ::free(beamIt->second.beamintensity_1);beamIt->second.beamintensity_1=0;
00857         ::free(beamIt->second.beamintensity_2);beamIt->second.beamintensity_2=0;
00858       }else{
00859         //std::cout<<"h.nlivebx is zero"<<std::endl;
00860         h.bxindex=0;
00861         h.beamintensity_1=0;
00862         h.beamintensity_2=0;
00863       }
00864     }else{
00865       h.beammode="N/A";
00866       h.beamenergy=0.0;
00867       h.nlivebx=0;
00868       h.bxindex=0;
00869       h.beamintensity_1=0;
00870       h.beamintensity_2=0;
00871     }
00872     h.startorbit=lumiheader->startOrbit;
00873     h.numorbit=lumiheader->numOrbits;
00874     if(h.cmsalive==0){
00875       h.cmslsnr=0; //the dead ls has cmsls number=0
00876     }else{
00877       h.cmslsnr=ncmslumi;//we guess cms lumils
00878     }
00879     h.instlumi=lumisummary->InstantLumi;
00880     //std::cout<<"instant lumi "<<lumisummary->InstantLumi<<std::endl;
00881     h.instlumierror=lumisummary->InstantLumiErr;
00882     h.lumisectionquality=lumisummary->InstantLumiQlty;
00883     h.dtnorm=lumisummary->DeadTimeNormalization;
00884     h.lhcnorm=lumisummary->LHCNormalization;
00885     //unsigned int timestp=lumiheader->timestamp;
00886     //std::cout<<"cmslsnum "<<ncmslumi<<"timestp "<<timestp<<std::endl;
00887     for(size_t i=0;i<lumi::N_BX;++i){
00888       lumi::Lumi2DB::PerBXData bET;
00889       lumi::Lumi2DB::PerBXData bOCC1;
00890       lumi::Lumi2DB::PerBXData bOCC2;
00891       //bET.idx=i+1;
00892       bET.lumivalue=lumidetail->ETLumi[i];
00893       bET.lumierr=lumidetail->ETLumiErr[i];
00894       bET.lumiquality=lumidetail->ETLumiQlty[i];      
00895       h.bxET.push_back(bET);
00896 
00897       //bOCC1.idx=i+1;
00898 
00899       bOCC1.lumivalue=lumidetail->OccLumi[0][i];
00900       bOCC1.lumierr=lumidetail->OccLumiErr[0][i];
00907       bOCC1.lumiquality=lumidetail->OccLumiQlty[0][i]; 
00908       h.bxOCC1.push_back(bOCC1);
00909           
00910       //bOCC2.idx=i+1;
00911       bOCC2.lumivalue=lumidetail->OccLumi[1][i];
00912       bOCC2.lumierr=lumidetail->OccLumiErr[1][i];
00913       bOCC2.lumiquality=lumidetail->OccLumiQlty[1][i]; 
00914       h.bxOCC2.push_back(bOCC2);
00915     }
00916     lumiresult.push_back(h);
00917   }
00918   std::cout<<std::endl;
00919   if(nstablebeam!=0){
00920     bgev=bgev/nstablebeam;//nominal beam energy=sum(energy)/nstablebeams
00921   }
00922   std::cout<<"avg stable beam energy "<<bgev<<std::endl;
00923   if( !m_novalidate && !isLumiDataValid(lumiresult.begin(),lumiresult.end()) ){
00924     throw lumi::invalidDataException("all lumi values are <0.5e-08","isLumiDataValid","Lumi2DB");
00925   }
00926   if( !m_nocheckingstablebeam && !hasStableBeam(lumiresult.begin(),lumiresult.end()) ){
00927     throw lumi::noStableBeamException("no LS has STABLE BEAMS","hasStableBeam","Lumi2DB");
00928   }
00929   coral::ConnectionService* svc=new coral::ConnectionService;
00930   lumi::DBConfig dbconf(*svc);
00931   if(!m_authpath.empty()){
00932     dbconf.setAuthentication(m_authpath);
00933   }
00934   coral::ISessionProxy* session=svc->connect(m_dest,coral::Update);
00935   coral::ITypeConverter& tpc=session->typeConverter();
00936   tpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
00937   //
00938   //write to old lumisummary
00939   //
00940   try{
00941     const std::string lversion(filenamecontent.version);
00942     if(m_mode==std::string("beamintensity_only")){
00943       std::cout<<"writing beam intensity only to old lumisummary table "<<std::endl;
00944       writeBeamIntensityOnly(session,runnumber,lversion,lumiresult.begin(),lumiresult.end());
00945       std::cout<<"done"<<std::endl;
00946     }else{
00947        if(m_mode=="loadoldschema"){
00948           std::cout<<"writing all lumi data to old lumisummary table "<<std::endl;
00949           writeAllLumiData(session,runnumber,lversion,lumiresult.begin(),lumiresult.end());     
00950           std::cout<<"done"<<std::endl;
00951        }
00952     }
00953     std::cout<<"writing all lumi data to lumisummary_V2 table "<<std::endl;
00954     writeAllLumiDataToSchema2(session,m_source,runnumber,bgev,ncollidingbunches,lumiresult.begin(),lumiresult.end());
00955     std::cout<<"done"<<std::endl;
00956     cleanTemporaryMemory(lumiresult.begin(),lumiresult.end());
00957     delete session;
00958     delete svc;
00959   }catch( const coral::Exception& er){
00960     std::cout<<"database error "<<er.what()<<std::endl;
00961     session->transaction().rollback();
00962     delete session;
00963     delete svc;
00964     throw er;
00965   }
00966 }
00967 const std::string lumi::Lumi2DB::dataType() const{
00968   return "LUMI";
00969 }
00970 const std::string lumi::Lumi2DB::sourceType() const{
00971   return "DB";
00972 }
00973 lumi::Lumi2DB::~Lumi2DB(){}
00974 
00975 #include "RecoLuminosity/LumiProducer/interface/DataPipeFactory.h"
00976 DEFINE_EDM_PLUGIN(lumi::DataPipeFactory,lumi::Lumi2DB,"Lumi2DB");
00977 #endif