CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Lumi2DB.cc
Go to the documentation of this file.
1 #ifndef RecoLuminosity_LumiProducer_Lumi2DB_H
2 #define RecoLuminosity_LumiProducer_Lumi2DB_H
3 #include "RelationalAccess/ConnectionService.h"
4 #include "CoralBase/AttributeList.h"
5 #include "CoralBase/Attribute.h"
6 #include "CoralBase/AttributeSpecification.h"
7 #include "CoralBase/Blob.h"
8 #include "CoralBase/Exception.h"
9 #include "RelationalAccess/ISessionProxy.h"
10 #include "RelationalAccess/ITransaction.h"
11 #include "RelationalAccess/ITypeConverter.h"
12 #include "RelationalAccess/ISchema.h"
13 #include "RelationalAccess/ITable.h"
14 #include "RelationalAccess/ITableDataEditor.h"
15 #include "RelationalAccess/IBulkOperation.h"
24 #include <iostream>
25 #include <cstring>
26 #include <cstdlib>
27 #include <memory>
28 #include <algorithm>
29 #include <map>
30 #include "TFile.h"
31 #include "TTree.h"
32 namespace lumi{
33  class Lumi2DB : public DataPipe{
34  public:
35  const static unsigned int COMMITLSINTERVAL=500; //commit interval in LS,totalrow=nls*(1+nalgo)
36  Lumi2DB(const std::string& dest);
37  virtual void retrieveData( unsigned int );
38  virtual const std::string dataType() const;
39  virtual const std::string sourceType() const;
40  virtual ~Lumi2DB();
41 
42  struct LumiSource{
43  unsigned int run;
44  unsigned int firstsection;
45  char version[8];
46  char datestr[9];
47  };
48  struct PerBXData{
49  //int idx;
50  float lumivalue;
51  float lumierr;
52  short lumiquality;
53  };
54  struct PerLumiData{
55  float dtnorm;
56  float lhcnorm;
57  float instlumi;
61  short cmsalive;
62  std::string beammode;
63  float beamenergy;
64  short nlivebx;//how much is in the beamintensity vector
65  short* bxindex;
68  unsigned int cmslsnr;
69  unsigned int lumilsnr;
70  unsigned int startorbit;
71  unsigned int numorbit;
72  std::vector<PerBXData> bxET;
73  std::vector<PerBXData> bxOCC1;
74  std::vector<PerBXData> bxOCC2;
75  };
76  struct beamData{
77  float energy;
78  std::string mode;
79  short nlivebx;
80  short* bxindex;
83  };
84  typedef std::vector<PerLumiData> LumiResult;
85  bool hasStableBeam( lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd );
86  private:
89  void writeAllLumiData(coral::ISessionProxy* session,unsigned int irunnumber,const std::string& ilumiversion,LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
90  void writeAllLumiDataToSchema2(coral::ISessionProxy* session,const std::string& source,unsigned int runnumber,float bgev,unsigned int ncollidingbunches,LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
91  void writeBeamIntensityOnly(coral::ISessionProxy* session,unsigned int irunnumber,const std::string& ilumiversion,LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
92  bool isLumiDataValid(LumiResult::iterator lumiBeg,LumiResult::iterator lumiEnd);
93  float applyCalibration(float varToCalibrate) const;
94  void cleanTemporaryMemory( lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd);
95  };//cl Lumi2DB
96 }//ns lumi
97 
98 //
99 //implementation
100 //
101 float
102 lumi::Lumi2DB::applyCalibration(float varToCalibrate)const{ //#only used for writing into schema_v1
103  return float(varToCalibrate)*m_norm;
104 }
105 bool
106 lumi::Lumi2DB::hasStableBeam( lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
107  //
108  // the run has at least 1 stable beams LS
109  //
110  lumi::Lumi2DB::LumiResult::iterator lumiIt;
111  int nStable=0;
112  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
113  if(lumiIt->beammode=="STABLE BEAMS"){
114  ++nStable;
115  }
116  }
117  if(nStable==0){
118  return false;
119  }
120  return true;
121 }
122 bool
123 lumi::Lumi2DB::isLumiDataValid(lumi::Lumi2DB::LumiResult::iterator lumiBeg,lumi::Lumi2DB::LumiResult::iterator lumiEnd){
124  //
125  // validate lumidata: all ls has lumi less than 0.5e-08 before calibration, then invalid data
126  //
127  lumi::Lumi2DB::LumiResult::iterator lumiIt;
128  int nBad=0;
129  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
130  //std::cout<<"instlumi before calib "<<lumiIt->instlumi<<std::endl;
131  if(lumiIt->instlumi<=0.5e-8){//cut before calib
132  ++nBad;
133  }
134  }
135  if(nBad==std::distance(lumiBeg,lumiEnd)){
136  return false;
137  }
138  return true;
139 }
140 void
142  coral::ISessionProxy* session,
143  unsigned int irunnumber,
144  const std::string& ilumiversion,
145  lumi::Lumi2DB::LumiResult::iterator lumiBeg,
146  lumi::Lumi2DB::LumiResult::iterator lumiEnd
147  ){
148  coral::AttributeList inputData;
149  inputData.extend("bxindex",typeid(coral::Blob));
150  inputData.extend("beamintensity_1",typeid(coral::Blob));
151  inputData.extend("beamintensity_2",typeid(coral::Blob));
152  inputData.extend("runnum",typeid(unsigned int));
153  inputData.extend("startorbit",typeid(unsigned int));
154  inputData.extend("lumiversion",typeid(std::string));
155  coral::Blob& bxindex = inputData["bxindex"].data<coral::Blob>();
156  coral::Blob& beamintensity_1 = inputData["beamintensity_1"].data<coral::Blob>();
157  coral::Blob& beamintensity_2 = inputData["beamintensity_2"].data<coral::Blob>();
158  unsigned int& runnumber = inputData["runnum"].data<unsigned int>();
159  unsigned int& startorbit = inputData["startorbit"].data<unsigned int>();
160  std::string& lumiversion = inputData["lumiversion"].data<std::string>();
161 
162  lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
163  coral::IBulkOperation* summaryUpdater=0;
164  unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
165  unsigned int lumiindx=0;
166  unsigned int comittedls=0;
167  std::string setClause("CMSBXINDEXBLOB=:bxindex,BEAMINTENSITYBLOB_1=:beamintensity_1,BEAMINTENSITYBLOB_2=:beamintensity_2");
168  std::string condition("RUNNUM=:runnum AND STARTORBIT=:startorbit AND LUMIVERSION=:lumiversion");
169  runnumber=irunnumber;
170  lumiversion=ilumiversion;
171  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
172  if(!session->transaction().isActive()){
173  session->transaction().start(false);
174  }
175  startorbit=lumiIt->startorbit;
176  //std::cout<<"runnumber "<<irunnumber<<" starorbit "<<startorbit<<" lumiversion "<<lumiversion<<" totallumils "<<totallumils<<std::endl;
177  short nlivebx=lumiIt->nlivebx;
178  if(nlivebx!=0){
179  bxindex.resize(sizeof(short)*nlivebx);
180  beamintensity_1.resize(sizeof(float)*nlivebx);
181  beamintensity_2.resize(sizeof(float)*nlivebx);
182  void* bxindex_StartAddress = bxindex.startingAddress();
183  void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
184  void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
185  std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
186  std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
187  std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
188  ::free(lumiIt->bxindex);
189  ::free(lumiIt->beamintensity_1);
190  ::free(lumiIt->beamintensity_2);
191  }else{
192  bxindex.resize(0);
193  beamintensity_1.resize(0);
194  beamintensity_2.resize(0);
195  }
196  coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryTableName());
197  summaryUpdater=summarytable.dataEditor().bulkUpdateRows(setClause,condition,inputData,totallumils);
198  summaryUpdater->processNextIteration();
199  summaryUpdater->flush();
200  ++comittedls;
201  if(comittedls==Lumi2DB::COMMITLSINTERVAL){
202  std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl;
203  delete summaryUpdater;
204  summaryUpdater=0;
205  session->transaction().commit();
206  comittedls=0;
207  std::cout<<"\t committed "<<std::endl;
208  }else if( lumiindx==(totallumils-1) ){
209  std::cout<<"\t committing at the end"<<std::endl;
210  delete summaryUpdater; summaryUpdater=0;
211  session->transaction().commit();
212  std::cout<<"\t done"<<std::endl;
213  }
214  }
215 }
216 void
218  coral::ISessionProxy* session,
219  unsigned int irunnumber,
220  const std::string& ilumiversion,
221  lumi::Lumi2DB::LumiResult::iterator lumiBeg,
222  lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
223  coral::AttributeList summaryData;
224  coral::AttributeList detailData;
225  summaryData.extend("LUMISUMMARY_ID",typeid(unsigned long long));
226  summaryData.extend("RUNNUM",typeid(unsigned int));
227  summaryData.extend("CMSLSNUM",typeid(unsigned int));
228  summaryData.extend("LUMILSNUM",typeid(unsigned int));
229  summaryData.extend("LUMIVERSION",typeid(std::string));
230  summaryData.extend("DTNORM",typeid(float));
231  summaryData.extend("LHCNORM",typeid(float));
232  summaryData.extend("INSTLUMI",typeid(float));
233  summaryData.extend("INSTLUMIERROR",typeid(float));
234  summaryData.extend("INSTLUMIQUALITY",typeid(short));
235  summaryData.extend("LUMISECTIONQUALITY",typeid(short));
236  summaryData.extend("CMSALIVE",typeid(short));
237  summaryData.extend("STARTORBIT",typeid(unsigned int));
238  summaryData.extend("NUMORBIT",typeid(unsigned int));
239  summaryData.extend("BEAMENERGY",typeid(float));
240  summaryData.extend("BEAMSTATUS",typeid(std::string));
241  summaryData.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
242  summaryData.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
243  summaryData.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
244 
245  detailData.extend("LUMIDETAIL_ID",typeid(unsigned long long));
246  detailData.extend("LUMISUMMARY_ID",typeid(unsigned long long));
247  detailData.extend("BXLUMIVALUE",typeid(coral::Blob));
248  detailData.extend("BXLUMIERROR",typeid(coral::Blob));
249  detailData.extend("BXLUMIQUALITY",typeid(coral::Blob));
250  detailData.extend("ALGONAME",typeid(std::string));
251 
252  unsigned long long& lumisummary_id=summaryData["LUMISUMMARY_ID"].data<unsigned long long>();
253  unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
254  std::string& lumiversion = summaryData["LUMIVERSION"].data<std::string>();
255  float& dtnorm = summaryData["DTNORM"].data<float>();
256  float& lhcnorm = summaryData["LHCNORM"].data<float>();
257  float& instlumi = summaryData["INSTLUMI"].data<float>();
258  float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
259  short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
260  short& lumisectionquality = summaryData["LUMISECTIONQUALITY"].data<short>();
261  short& alive = summaryData["CMSALIVE"].data<short>();
262  unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
263  unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
264  unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
265  unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
266  float& beamenergy = summaryData["BEAMENERGY"].data<float>();
267  std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
268  coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
269  coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
270  coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
271 
272  unsigned long long& lumidetail_id=detailData["LUMIDETAIL_ID"].data<unsigned long long>();
273  unsigned long long& d2lumisummary_id=detailData["LUMISUMMARY_ID"].data<unsigned long long>();
274  coral::Blob& bxlumivalue=detailData["BXLUMIVALUE"].data<coral::Blob>();
275  coral::Blob& bxlumierror=detailData["BXLUMIERROR"].data<coral::Blob>();
276  coral::Blob& bxlumiquality=detailData["BXLUMIQUALITY"].data<coral::Blob>();
277  std::string& algoname=detailData["ALGONAME"].data<std::string>();
278 
279  lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
280  coral::IBulkOperation* summaryInserter=0;
281  coral::IBulkOperation* detailInserter=0;
282  //one loop for ids
283  //nested transaction doesn't work with bulk inserter
284  unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
285  unsigned int lumiindx=0;
286  std::map< unsigned long long,std::vector<unsigned long long> > idallocationtable;
287  std::cout<<"\t allocating total lumisummary ids "<<totallumils<<std::endl;
288  std::cout<<"\t allocating total lumidetail ids "<<totallumils*lumi::N_LUMIALGO<<std::endl;
289 
290  session->transaction().start(false);
291  lumi::idDealer idg(session->nominalSchema());
292  unsigned long long lumisummaryID = idg.generateNextIDForTable(LumiNames::lumisummaryTableName(),totallumils)-totallumils;
293  unsigned long long lumidetailID=idg.generateNextIDForTable(LumiNames::lumidetailTableName(),totallumils*lumi::N_LUMIALGO)-totallumils*lumi::N_LUMIALGO;
294  session->transaction().commit();
295  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx,++lumisummaryID){
296  std::vector< unsigned long long > allIDs;
297  allIDs.reserve(1+lumi::N_LUMIALGO);
298  allIDs.push_back(lumisummaryID);
299  for( unsigned int j=0; j<lumi::N_LUMIALGO; ++j, ++lumidetailID){
300  allIDs.push_back(lumidetailID);
301  }
302  idallocationtable.insert(std::make_pair(lumiindx,allIDs));
303  }
304  std::cout<<"\t all ids allocated"<<std::endl;
305  lumiindx=0;
306  unsigned int comittedls=0;
307  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
308  if(!session->transaction().isActive()){
309  session->transaction().start(false);
310  coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryTableName());
311  summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
312  coral::ITable& detailtable=session->nominalSchema().tableHandle(LumiNames::lumidetailTableName());
313  detailInserter=detailtable.dataEditor().bulkInsert(detailData,totallumils*lumi::N_LUMIALGO);
314  }
315  lumisummary_id=idallocationtable[lumiindx][0];
316  lumirunnum = irunnumber;
317  lumiversion = ilumiversion;
318  dtnorm = lumiIt->dtnorm;
319  lhcnorm = lumiIt->lhcnorm;
320  //instlumi = lumiIt->instlumi;
321  //instlumierror = lumiIt->instlumierror;
322  instlumi = applyCalibration(lumiIt->instlumi);
323  instlumierror = applyCalibration(lumiIt->instlumierror);
324  instlumiquality = lumiIt->instlumiquality;
325  lumisectionquality = lumiIt->lumisectionquality;
326  alive = lumiIt->cmsalive;
327  cmslsnr = lumiIt->cmslsnr;
328 
329  lumilsnr = lumiIt->lumilsnr;
330  startorbit = lumiIt->startorbit;
331  numorbit = lumiIt->numorbit;
332  beamenergy = lumiIt->beamenergy;
333  beamstatus = lumiIt->beammode;
334  short nlivebx=lumiIt->nlivebx;
335  //std::cout<<"nlivebx "<<nlivebx<<std::endl;
336  if(nlivebx!=0){
337  bxindex.resize(sizeof(short)*nlivebx);
338  beamintensity_1.resize(sizeof(float)*nlivebx);
339  beamintensity_2.resize(sizeof(float)*nlivebx);
340  void* bxindex_StartAddress = bxindex.startingAddress();
341  void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
342  void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
343  std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
344  std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
345  std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
346  //::free(lumiIt->bxindex);
347  //::free(lumiIt->beamintensity_1);
348  //::free(lumiIt->beamintensity_2);
349  }else{
350  bxindex.resize(0);
351  beamintensity_1.resize(0);
352  beamintensity_2.resize(0);
353  }
354  //insert the new row
355  summaryInserter->processNextIteration();
356  summaryInserter->flush();
357  unsigned int algoindx=1;
358  for( unsigned int j=0; j<lumi:: N_LUMIALGO; ++j,++algoindx ){
359  d2lumisummary_id=idallocationtable[lumiindx].at(0);
360  lumidetail_id=idallocationtable[lumiindx].at(algoindx);
361  std::vector<PerBXData>::const_iterator bxIt;
362  std::vector<PerBXData>::const_iterator bxBeg;
363  std::vector<PerBXData>::const_iterator bxEnd;
364  if(j==0) {
365  algoname=std::string("ET");
366  bxBeg=lumiIt->bxET.begin();
367  bxEnd=lumiIt->bxET.end();
368  }
369  if(j==1) {
370  algoname=std::string("OCC1");
371  bxBeg=lumiIt->bxOCC1.begin();
372  bxEnd=lumiIt->bxOCC1.end();
373  }
374  if(j==2) {
375  algoname=std::string("OCC2");
376  bxBeg=lumiIt->bxOCC2.begin();
377  bxEnd=lumiIt->bxOCC2.end();
378  }
379  float lumivalue[lumi::N_BX]={0.0};
380  float lumierror[lumi::N_BX]={0.0};
381  int lumiquality[lumi::N_BX]={0};
382  bxlumivalue.resize(sizeof(float)*lumi::N_BX);
383  bxlumierror.resize(sizeof(float)*lumi::N_BX);
384  bxlumiquality.resize(sizeof(short)*lumi::N_BX);
385  void* bxlumivalueStartAddress=bxlumivalue.startingAddress();
386  void* bxlumierrorStartAddress=bxlumierror.startingAddress();
387  void* bxlumiqualityStartAddress=bxlumiquality.startingAddress();
388  unsigned int k=0;
389  for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k ){
390  lumivalue[k]=bxIt->lumivalue;
391  lumierror[k]=bxIt->lumierr;
392  lumiquality[k]=bxIt->lumiquality;
393  }
394  std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
395  std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
396  std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
397  detailInserter->processNextIteration();
398  }
399  detailInserter->flush();
400  ++comittedls;
401  if(comittedls==Lumi2DB::COMMITLSINTERVAL){
402  std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl;
403  delete summaryInserter;
404  summaryInserter=0;
405  delete detailInserter;
406  detailInserter=0;
407  session->transaction().commit();
408  comittedls=0;
409  std::cout<<"\t committed "<<std::endl;
410  }else if( lumiindx==(totallumils-1) ){
411  std::cout<<"\t committing at the end"<<std::endl;
412  delete summaryInserter; summaryInserter=0;
413  delete detailInserter; detailInserter=0;
414  session->transaction().commit();
415  std::cout<<"\t done"<<std::endl;
416  }
417  }
418 }
419 
420 void
422  coral::ISessionProxy* session,
423  const std::string& source,
424  unsigned int irunnumber,
425  float bgev,
426  unsigned int ncollidingbunches,
427  lumi::Lumi2DB::LumiResult::iterator lumiBeg,
428  lumi::Lumi2DB::LumiResult::iterator lumiEnd ){
429  std::cout<<"writeAllLumiDataToSchema2"<<std::endl;
430  coral::AttributeList summaryData;
431  summaryData.extend("DATA_ID",typeid(unsigned long long));
432  summaryData.extend("RUNNUM",typeid(unsigned int));
433  summaryData.extend("LUMILSNUM",typeid(unsigned int));
434  summaryData.extend("CMSLSNUM",typeid(unsigned int));
435  summaryData.extend("INSTLUMI",typeid(float));
436  summaryData.extend("INSTLUMIERROR",typeid(float));
437  summaryData.extend("INSTLUMIQUALITY",typeid(short));
438  summaryData.extend("BEAMSTATUS",typeid(std::string));
439  summaryData.extend("BEAMENERGY",typeid(float));
440  summaryData.extend("NUMORBIT",typeid(unsigned int));
441  summaryData.extend("STARTORBIT",typeid(unsigned int));
442  summaryData.extend("CMSBXINDEXBLOB",typeid(coral::Blob));
443  summaryData.extend("BEAMINTENSITYBLOB_1",typeid(coral::Blob));
444  summaryData.extend("BEAMINTENSITYBLOB_2",typeid(coral::Blob));
445  summaryData.extend("BXLUMIVALUE_OCC1",typeid(coral::Blob));
446  summaryData.extend("BXLUMIERROR_OCC1",typeid(coral::Blob));
447  summaryData.extend("BXLUMIQUALITY_OCC1",typeid(coral::Blob));
448  summaryData.extend("BXLUMIVALUE_OCC2",typeid(coral::Blob));
449  summaryData.extend("BXLUMIERROR_OCC2",typeid(coral::Blob));
450  summaryData.extend("BXLUMIQUALITY_OCC2",typeid(coral::Blob));
451  summaryData.extend("BXLUMIVALUE_ET",typeid(coral::Blob));
452  summaryData.extend("BXLUMIERROR_ET",typeid(coral::Blob));
453  summaryData.extend("BXLUMIQUALITY_ET",typeid(coral::Blob));
454 
455  unsigned long long& data_id=summaryData["DATA_ID"].data<unsigned long long>();
456  unsigned int& lumirunnum = summaryData["RUNNUM"].data<unsigned int>();
457  unsigned int& lumilsnr = summaryData["LUMILSNUM"].data<unsigned int>();
458  unsigned int& cmslsnr = summaryData["CMSLSNUM"].data<unsigned int>();
459  float& instlumi = summaryData["INSTLUMI"].data<float>();
460  float& instlumierror = summaryData["INSTLUMIERROR"].data<float>();
461  short& instlumiquality = summaryData["INSTLUMIQUALITY"].data<short>();
462  std::string& beamstatus = summaryData["BEAMSTATUS"].data<std::string>();
463  float& beamenergy = summaryData["BEAMENERGY"].data<float>();
464  unsigned int& numorbit = summaryData["NUMORBIT"].data<unsigned int>();
465  unsigned int& startorbit = summaryData["STARTORBIT"].data<unsigned int>();
466  coral::Blob& bxindex = summaryData["CMSBXINDEXBLOB"].data<coral::Blob>();
467  coral::Blob& beamintensity_1 = summaryData["BEAMINTENSITYBLOB_1"].data<coral::Blob>();
468  coral::Blob& beamintensity_2 = summaryData["BEAMINTENSITYBLOB_2"].data<coral::Blob>();
469  coral::Blob& bxlumivalue_et=summaryData["BXLUMIVALUE_ET"].data<coral::Blob>();
470  coral::Blob& bxlumierror_et=summaryData["BXLUMIERROR_ET"].data<coral::Blob>();
471  coral::Blob& bxlumiquality_et=summaryData["BXLUMIQUALITY_ET"].data<coral::Blob>();
472  coral::Blob& bxlumivalue_occ1=summaryData["BXLUMIVALUE_OCC1"].data<coral::Blob>();
473  coral::Blob& bxlumierror_occ1=summaryData["BXLUMIERROR_OCC1"].data<coral::Blob>();
474  coral::Blob& bxlumiquality_occ1=summaryData["BXLUMIQUALITY_OCC1"].data<coral::Blob>();
475  coral::Blob& bxlumivalue_occ2=summaryData["BXLUMIVALUE_OCC2"].data<coral::Blob>();
476  coral::Blob& bxlumierror_occ2=summaryData["BXLUMIERROR_OCC2"].data<coral::Blob>();
477  coral::Blob& bxlumiquality_occ2=summaryData["BXLUMIQUALITY_OCC2"].data<coral::Blob>();
478 
479  lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
480  coral::IBulkOperation* summaryInserter=0;
481 
482  unsigned int totallumils=std::distance(lumiBeg,lumiEnd);
483  unsigned int lumiindx=0;
484  unsigned int comittedls=0;
485 
486  unsigned long long branch_id=3;
487  std::string branch_name("DATA");
488  lumi::RevisionDML revisionDML;
490  std::stringstream op;
491  op<<irunnumber;
492  std::string runnumberStr=op.str();
493  session->transaction().start(false);
494  lumirundata.entry_name=runnumberStr;
495  lumirundata.source=source;
496  lumirundata.runnumber=irunnumber;
497  lumirundata.bgev=bgev;
498  lumirundata.ncollidingbunches=ncollidingbunches;
499  lumirundata.entry_id=revisionDML.getEntryInBranchByName(session->nominalSchema(),lumi::LumiNames::lumidataTableName(),runnumberStr,branch_name);
500  //std::cout<<"entry_id "<<lumirundata.entry_id<<std::endl;
501  if(lumirundata.entry_id==0){
502  revisionDML.bookNewEntry(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata);
503  //std::cout<<"lumirundata revision_id "<<lumirundata.revision_id<<" entry_id "<<lumirundata.entry_id<<" data_id "<<lumirundata.data_id<<std::endl;
504  revisionDML.addEntry(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata,branch_id,branch_name);
505  }else{
506  revisionDML.bookNewRevision(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata);
507  //std::cout<<"lumirundata revision_id "<<lumirundata.revision_id<<" entry_id "<<lumirundata.entry_id<<" data_id "<<lumirundata.data_id<<std::endl;
508  revisionDML.addRevision(session->nominalSchema(),LumiNames::lumidataTableName(),lumirundata,branch_id,branch_name);
509  }
510  revisionDML.insertLumiRunData(session->nominalSchema(),lumirundata);
511  //summaryData.toOutputStream(std::cout);
512  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt,++lumiindx){
513  if(!session->transaction().isActive()){
514  session->transaction().start(false);
515  coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
516  summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
517  }else{
518  if(lumiIt==lumiBeg){
519  coral::ITable& summarytable=session->nominalSchema().tableHandle(LumiNames::lumisummaryv2TableName());
520  summaryInserter=summarytable.dataEditor().bulkInsert(summaryData,totallumils);
521  }
522  }
523  data_id = lumirundata.data_id;
524  lumirunnum = irunnumber;
525  lumilsnr = lumiIt->lumilsnr;
526  cmslsnr = lumiIt->cmslsnr;
527  instlumi = lumiIt->instlumi; // not calibrated!
528  instlumierror = lumiIt->instlumierror; // not calibrated!
529  instlumiquality = lumiIt->instlumiquality;
530  beamstatus = lumiIt->beammode;
531  beamenergy = lumiIt->beamenergy;
532  numorbit = lumiIt->numorbit;
533  startorbit = lumiIt->startorbit;
534  short nlivebx=lumiIt->nlivebx;
535  //std::cout<<"nlivebx "<<nlivebx<<std::endl;
536  if(nlivebx!=0){
537  bxindex.resize(sizeof(short)*nlivebx);
538  beamintensity_1.resize(sizeof(float)*nlivebx);
539  beamintensity_2.resize(sizeof(float)*nlivebx);
540  void* bxindex_StartAddress = bxindex.startingAddress();
541  void* beamIntensity1_StartAddress = beamintensity_1.startingAddress();
542  void* beamIntensity2_StartAddress = beamintensity_2.startingAddress();
543  std::memmove(bxindex_StartAddress,lumiIt->bxindex,sizeof(short)*nlivebx);
544  std::memmove(beamIntensity1_StartAddress,lumiIt->beamintensity_1,sizeof(float)*nlivebx);
545  std::memmove(beamIntensity2_StartAddress,lumiIt->beamintensity_2,sizeof(float)*nlivebx);
546  //::free(lumiIt->bxindex);
547  //::free(lumiIt->beamintensity_1);
548  //::free(lumiIt->beamintensity_2);
549  }else{
550  bxindex.resize(0);
551  beamintensity_1.resize(0);
552  beamintensity_2.resize(0);
553  }
554  for( unsigned int j=0; j<lumi:: N_LUMIALGO; ++j ){
555  std::vector<PerBXData>::const_iterator bxIt;
556  std::vector<PerBXData>::const_iterator bxBeg;
557  std::vector<PerBXData>::const_iterator bxEnd;
558  if(j==0) {//the push_back order in the input data is ET,OCC1,OCC2
559  //algoname=std::string("ET");
560  bxBeg=lumiIt->bxET.begin();
561  bxEnd=lumiIt->bxET.end();
562  float lumivalue[lumi::N_BX]={0.0};
563  float lumierror[lumi::N_BX]={0.0};
564  int lumiquality[lumi::N_BX]={0};
565  bxlumivalue_et.resize(sizeof(float)*lumi::N_BX);
566  bxlumierror_et.resize(sizeof(float)*lumi::N_BX);
567  bxlumiquality_et.resize(sizeof(short)*lumi::N_BX);
568  void* bxlumivalueStartAddress=bxlumivalue_et.startingAddress();
569  void* bxlumierrorStartAddress=bxlumierror_et.startingAddress();
570  void* bxlumiqualityStartAddress=bxlumiquality_et.startingAddress();
571  unsigned int k=0;
572  for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k ){
573  lumivalue[k]=bxIt->lumivalue;
574  lumierror[k]=bxIt->lumierr;
575  lumiquality[k]=bxIt->lumiquality;
576  }
577  std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
578  std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
579  std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
580  }
581  if(j==1) {
582  //algoname=std::string("OCC1");
583  bxBeg=lumiIt->bxOCC1.begin();
584  bxEnd=lumiIt->bxOCC1.end();
585  float lumivalue[lumi::N_BX]={0.0};
586  float lumierror[lumi::N_BX]={0.0};
587  int lumiquality[lumi::N_BX]={0};
588  bxlumivalue_occ1.resize(sizeof(float)*lumi::N_BX);
589  bxlumierror_occ1.resize(sizeof(float)*lumi::N_BX);
590  bxlumiquality_occ1.resize(sizeof(short)*lumi::N_BX);
591  void* bxlumivalueStartAddress=bxlumivalue_occ1.startingAddress();
592  void* bxlumierrorStartAddress=bxlumierror_occ1.startingAddress();
593  void* bxlumiqualityStartAddress=bxlumiquality_occ1.startingAddress();
594  unsigned int k=0;
595  for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k ){
596  lumivalue[k]=bxIt->lumivalue;
597  lumierror[k]=bxIt->lumierr;
598  lumiquality[k]=bxIt->lumiquality;
599  }
600  std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
601  std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
602  std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
603  }
604  if(j==2) {
605  //algoname=std::string("OCC2");
606  bxBeg=lumiIt->bxOCC2.begin();
607  bxEnd=lumiIt->bxOCC2.end();
608  float lumivalue[lumi::N_BX]={0.0};
609  float lumierror[lumi::N_BX]={0.0};
610  int lumiquality[lumi::N_BX]={0};
611  bxlumivalue_occ2.resize(sizeof(float)*lumi::N_BX);
612  bxlumierror_occ2.resize(sizeof(float)*lumi::N_BX);
613  bxlumiquality_occ2.resize(sizeof(short)*lumi::N_BX);
614  void* bxlumivalueStartAddress=bxlumivalue_occ2.startingAddress();
615  void* bxlumierrorStartAddress=bxlumierror_occ2.startingAddress();
616  void* bxlumiqualityStartAddress=bxlumiquality_occ2.startingAddress();
617  unsigned int k=0;
618  for( bxIt=bxBeg;bxIt!=bxEnd;++bxIt,++k ){
619  lumivalue[k]=bxIt->lumivalue;
620  lumierror[k]=bxIt->lumierr;
621  lumiquality[k]=bxIt->lumiquality;
622  }
623  std::memmove(bxlumivalueStartAddress,lumivalue,sizeof(float)*lumi::N_BX);
624  std::memmove(bxlumierrorStartAddress,lumierror,sizeof(float)*lumi::N_BX);
625  std::memmove(bxlumiqualityStartAddress,lumiquality,sizeof(short)*lumi::N_BX);
626  }
627  }
628  summaryInserter->processNextIteration();
629  summaryInserter->flush();
630  ++comittedls;
631  if(comittedls==Lumi2DB::COMMITLSINTERVAL){
632  std::cout<<"\t committing in LS chunck "<<comittedls<<std::endl;
633  delete summaryInserter;
634  summaryInserter=0;
635  session->transaction().commit();
636  comittedls=0;
637  std::cout<<"\t committed "<<std::endl;
638  }else if( lumiindx==(totallumils-1) ){
639  std::cout<<"\t committing at the end"<<std::endl;
640  delete summaryInserter; summaryInserter=0;
641  session->transaction().commit();
642  std::cout<<"\t done"<<std::endl;
643  }
644  }
645 }
646 
647 void lumi::Lumi2DB::cleanTemporaryMemory( lumi::Lumi2DB::LumiResult::iterator lumiBeg,
648  lumi::Lumi2DB::LumiResult::iterator lumiEnd){
649  lumi::Lumi2DB::LumiResult::const_iterator lumiIt;
650  for(lumiIt=lumiBeg;lumiIt!=lumiEnd;++lumiIt){
651  ::free(lumiIt->bxindex);
652  ::free(lumiIt->beamintensity_1);
653  ::free(lumiIt->beamintensity_2);
654  }
655 
656 }
657 lumi::Lumi2DB::Lumi2DB(const std::string& dest):DataPipe(dest){}
658 void
660  //parse lumi source file name
661  if(m_source.length()==0) throw lumi::Exception("lumi source is not set","parseSourceString","Lumi2DB");
662  //std::cout<<"source "<<m_source<<std::endl;
663  size_t tempIndex = m_source.find_last_of(".");
664  size_t nameLength = m_source.length();
665  std::string FileSuffix= m_source.substr(tempIndex+1,nameLength - tempIndex);
666  std::string::size_type lastPos=m_source.find_first_not_of("_",0);
667  std::string::size_type pos = m_source.find_first_of("_",lastPos);
668  std::vector<std::string> pieces;
669  while( std::string::npos != pos || std::string::npos != lastPos){
670  pieces.push_back(m_source.substr(lastPos,pos-lastPos));
671  lastPos=m_source.find_first_not_of("_",pos);
672  pos=m_source.find_first_of("_",lastPos);
673  }
674  if( pieces[1]!="LUMI" || pieces[2]!="RAW" || FileSuffix!="root"){
675  throw lumi::Exception("not lumi raw data file CMS_LUMI_RAW","parseSourceString","Lumi2DB");
676  }
677  std::strcpy(result.datestr,pieces[3].c_str());
678  std::strcpy(result.version,pieces[5].c_str());
679  //std::cout<<"version : "<<result.version<<std::endl;
680  result.run = atoi(pieces[4].c_str());
681  //std::cout<<"run : "<<result.run<<std::endl;
682  result.firstsection = atoi(pieces[5].c_str());
683  //std::cout<<"first section : "<< result.firstsection<<std::endl;
684 }
685 
686 void
688  if(dataPtr==0){
689  std::cout<<"HCAL_HLX::DIP_COMBINED_DATA* dataPtr=0"<<std::endl;
690  b.bxindex=0;
691  b.beamintensity_1=0;
692  b.beamintensity_2=0;
693  b.nlivebx=0;
694  }else{
695  b.bxindex=(short*)::malloc(sizeof(short)*lumi::N_BX);
696  b.beamintensity_1=(float*)::malloc(sizeof(float)*lumi::N_BX);
697  b.beamintensity_2=(float*)::malloc(sizeof(float)*lumi::N_BX);
698 
699  short a=0;//a is position in lumidetail array
700  for(unsigned int i=0;i<lumi::N_BX;++i){
701  if( i==0 ){
702  if(dataPtr->Beam[0].averageBunchIntensities[0]>0 || dataPtr->Beam[1].averageBunchIntensities[0]>0 ){
703  b.bxindex[a]=0;
704  b.beamintensity_1[a]=dataPtr->Beam[0].averageBunchIntensities[0];
705  b.beamintensity_2[a]=dataPtr->Beam[1].averageBunchIntensities[0];
706  ++a;
707  }
708  continue;
709  }
710  if(dataPtr->Beam[0].averageBunchIntensities[i-1]>0 || dataPtr->Beam[1].averageBunchIntensities[i-1]>0){
711  b.bxindex[a]=i;
712  b.beamintensity_1[a]=dataPtr->Beam[0].averageBunchIntensities[i-1];
713  b.beamintensity_2[a]=dataPtr->Beam[1].averageBunchIntensities[i-1];
714  ++a;
715  //if(i!=0){
716  // std::cout<<"beam intensity "<<dataPtr->sectionNumber<<" "<<dataPtr->timestamp-1262300400<<" "<<(i-1)*10+1<<" "<<b.beamintensity_1[a]<<" "<<b.beamintensity_2[a]<<std::endl;
717  //}
718  }
719  }
720  b.nlivebx=a;
721  }
722 }
726 void
728  lumi::Lumi2DB::LumiResult lumiresult;
729  //check filename is in lumiraw format
730  lumi::Lumi2DB::LumiSource filenamecontent;
731  try{
732  parseSourceString(filenamecontent);
733  }catch(const lumi::Exception& er){
734  std::cout<<er.what()<<std::endl;
735  throw er;
736  }
737  if( filenamecontent.run!=runnumber ){
738  throw lumi::Exception("runnumber in file name does not match requested run number","retrieveData","Lumi2DB");
739  }
740  TFile* source=TFile::Open(m_source.c_str(),"READ");
741  TTree *hlxtree = (TTree*)source->Get("HLXData");
742  if(!hlxtree){
743  throw lumi::Exception(std::string("non-existing HLXData "),"retrieveData","Lumi2DB");
744  }
745  //hlxtree->Print();
746  std::auto_ptr<HCAL_HLX::LUMI_SECTION> localSection(new HCAL_HLX::LUMI_SECTION);
747  HCAL_HLX::LUMI_SECTION_HEADER* lumiheader = &(localSection->hdr);
748  HCAL_HLX::LUMI_SUMMARY* lumisummary = &(localSection->lumiSummary);
749  HCAL_HLX::LUMI_DETAIL* lumidetail = &(localSection->lumiDetail);
750 
751  hlxtree->SetBranchAddress("Header.",&lumiheader);
752  hlxtree->SetBranchAddress("Summary.",&lumisummary);
753  hlxtree->SetBranchAddress("Detail.",&lumidetail);
754 
755  size_t nentries=hlxtree->GetEntries();
756  unsigned int nstablebeam=0;
757  float bgev=0.0;
758  //source->GetListOfKeys()->Print();
759  std::map<unsigned int, Lumi2DB::beamData> dipmap;
760  TTree *diptree= (TTree*)source->Get("DIPCombined");
761  if(diptree){
762  //throw lumi::Exception(std::string("non-existing DIPData "),"retrieveData","Lumi2DB");
763  std::auto_ptr<HCAL_HLX::DIP_COMBINED_DATA> dipdata(new HCAL_HLX::DIP_COMBINED_DATA);
764  diptree->SetBranchAddress("DIPCombined.",&dipdata);
765  size_t ndipentries=diptree->GetEntries();
766  for(size_t i=0;i<ndipentries;++i){
767  diptree->GetEntry(i);
768  //unsigned int fillnumber=dipdata->FillNumber;
769  //std::vector<short> collidingidx;collidingidx.reserve(LUMI::N_BX);
770  //for(unsigned int i=0;i<lumi::N_BX;++i){
771  //int isb1colliding=dipdata->beam[0].beamConfig[i];
772  //int isb2colliding=dipdata->beam[1].beamConfig[i];
773  //if(isb1colliding && isb2colliding&&isb1colliding==1&&isb2colliding==1){
774  // collidingidx.push_back(i);
775  // }
776  //}
777  beamData b;
778  //std::cout<<"Beam Mode : "<<dipdata->beamMode<<"\n";
779  //std::cout<<"Beam Energy : "<<dipdata->Energy<<"\n";
780  //std::cout<<"sectionUmber : "<<dipdata->sectionNumber<<"\n";
781  unsigned int dipls=dipdata->sectionNumber;
782  if (std::string(dipdata->beamMode).empty()){
783  b.mode="N/A";
784  }else{
785  b.mode=dipdata->beamMode;
786  }
787  b.energy=dipdata->Energy;
788  if(b.mode=="STABLE BEAMS"){
789  ++nstablebeam;
790  bgev+=b.energy;
791  }
792  this->retrieveBeamIntensity(dipdata.get(),b);
793  dipmap.insert(std::make_pair(dipls,b));
794  }
795  }else{
796  for(size_t i=0;i<nentries;++i){
797  beamData b;
798  b.mode="N/A";
799  b.energy=0.0;
800  this->retrieveBeamIntensity(0,b);
801  dipmap.insert(std::make_pair(i,b));
802  }
803  }
804  //diptree->Print();
805 
806  size_t ncmslumi=0;
807  std::cout<<"processing total lumi lumisection "<<nentries<<std::endl;
808  //size_t lumisecid=0;
809  //unsigned int lumilumisecid=0;
810  //runnumber=lumiheader->runNumber;
811  //
812  //hardcode the first LS is always alive
813  //
814  unsigned int ncollidingbunches=0;
815  for(size_t i=0;i<nentries;++i){
817  h.cmsalive=1;
818  hlxtree->GetEntry(i);
819  //std::cout<<"live flag "<<lumiheader->bCMSLive <<std::endl;
820  if( !lumiheader->bCMSLive && i!=0){
821  std::cout<<"\t non-CMS LS "<<lumiheader->sectionNumber<<" ";
822  h.cmsalive=0;
823  }
824  ++ncmslumi;
825  if(ncmslumi==1){//just take the first ls
826  ncollidingbunches=lumiheader->numBunches;
827  }
828  h.bxET.reserve(lumi::N_BX);
829  h.bxOCC1.reserve(lumi::N_BX);
830  h.bxOCC2.reserve(lumi::N_BX);
831 
832  //runnumber=lumiheader->runNumber;
833  //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));
834  h.lumilsnr=lumiheader->sectionNumber;
835  std::map<unsigned int , Lumi2DB::beamData >::iterator beamIt=dipmap.find(h.lumilsnr);
836  if ( beamIt!=dipmap.end() ){
837  h.beammode=beamIt->second.mode;
838  h.beamenergy=beamIt->second.energy;
839  h.nlivebx=beamIt->second.nlivebx;
840  if(h.nlivebx!=0){
841  h.bxindex=(short*)malloc(sizeof(short)*h.nlivebx);
842  h.beamintensity_1=(float*)malloc(sizeof(float)*h.nlivebx);
843  h.beamintensity_2=(float*)malloc(sizeof(float)*h.nlivebx);
844  if(h.bxindex==0 || h.beamintensity_1==0 || h.beamintensity_2==0){
845  std::cout<<"malloc failed"<<std::endl;
846  }
847  //std::cout<<"h.bxindex size "<<sizeof(short)*h.nlivebx<<std::endl;
848  //std::cout<<"h.beamintensity_1 size "<<sizeof(float)*h.nlivebx<<std::endl;
849  //std::cout<<"h.beamintensity_2 size "<<sizeof(float)*h.nlivebx<<std::endl;
850 
851  std::memmove(h.bxindex,beamIt->second.bxindex,sizeof(short)*h.nlivebx);
852  std::memmove(h.beamintensity_1,beamIt->second.beamintensity_1,sizeof(float)*h.nlivebx);
853  std::memmove(h.beamintensity_2,beamIt->second.beamintensity_2,sizeof(float)*h.nlivebx);
854 
855  ::free(beamIt->second.bxindex);beamIt->second.bxindex=0;
856  ::free(beamIt->second.beamintensity_1);beamIt->second.beamintensity_1=0;
857  ::free(beamIt->second.beamintensity_2);beamIt->second.beamintensity_2=0;
858  }else{
859  //std::cout<<"h.nlivebx is zero"<<std::endl;
860  h.bxindex=0;
861  h.beamintensity_1=0;
862  h.beamintensity_2=0;
863  }
864  }else{
865  h.beammode="N/A";
866  h.beamenergy=0.0;
867  h.nlivebx=0;
868  h.bxindex=0;
869  h.beamintensity_1=0;
870  h.beamintensity_2=0;
871  }
872  h.startorbit=lumiheader->startOrbit;
873  h.numorbit=lumiheader->numOrbits;
874  if(h.cmsalive==0){
875  h.cmslsnr=0; //the dead ls has cmsls number=0
876  }else{
877  h.cmslsnr=ncmslumi;//we guess cms lumils
878  }
879  h.instlumi=lumisummary->InstantLumi;
880  //std::cout<<"instant lumi "<<lumisummary->InstantLumi<<std::endl;
881  h.instlumierror=lumisummary->InstantLumiErr;
882  h.lumisectionquality=lumisummary->InstantLumiQlty;
883  h.dtnorm=lumisummary->DeadTimeNormalization;
884  h.lhcnorm=lumisummary->LHCNormalization;
885  //unsigned int timestp=lumiheader->timestamp;
886  //std::cout<<"cmslsnum "<<ncmslumi<<"timestp "<<timestp<<std::endl;
887  for(size_t i=0;i<lumi::N_BX;++i){
891  //bET.idx=i+1;
892  bET.lumivalue=lumidetail->ETLumi[i];
893  bET.lumierr=lumidetail->ETLumiErr[i];
894  bET.lumiquality=lumidetail->ETLumiQlty[i];
895  h.bxET.push_back(bET);
896 
897  //bOCC1.idx=i+1;
898 
899  bOCC1.lumivalue=lumidetail->OccLumi[0][i];
900  bOCC1.lumierr=lumidetail->OccLumiErr[0][i];
907  bOCC1.lumiquality=lumidetail->OccLumiQlty[0][i];
908  h.bxOCC1.push_back(bOCC1);
909 
910  //bOCC2.idx=i+1;
911  bOCC2.lumivalue=lumidetail->OccLumi[1][i];
912  bOCC2.lumierr=lumidetail->OccLumiErr[1][i];
913  bOCC2.lumiquality=lumidetail->OccLumiQlty[1][i];
914  h.bxOCC2.push_back(bOCC2);
915  }
916  lumiresult.push_back(h);
917  }
918  std::cout<<std::endl;
919  if(nstablebeam!=0){
920  bgev=bgev/nstablebeam;//nominal beam energy=sum(energy)/nstablebeams
921  }
922  std::cout<<"avg stable beam energy "<<bgev<<std::endl;
923  if( !m_novalidate && !isLumiDataValid(lumiresult.begin(),lumiresult.end()) ){
924  throw lumi::invalidDataException("all lumi values are <0.5e-08","isLumiDataValid","Lumi2DB");
925  }
926  if( !m_nocheckingstablebeam && !hasStableBeam(lumiresult.begin(),lumiresult.end()) ){
927  throw lumi::noStableBeamException("no LS has STABLE BEAMS","hasStableBeam","Lumi2DB");
928  }
929  coral::ConnectionService* svc=new coral::ConnectionService;
930  lumi::DBConfig dbconf(*svc);
931  if(!m_authpath.empty()){
932  dbconf.setAuthentication(m_authpath);
933  }
934  coral::ISessionProxy* session=svc->connect(m_dest,coral::Update);
935  coral::ITypeConverter& tpc=session->typeConverter();
936  tpc.setCppTypeForSqlType("unsigned int","NUMBER(10)");
937  //
938  //write to old lumisummary
939  //
940  try{
941  const std::string lversion(filenamecontent.version);
942  if(m_mode==std::string("beamintensity_only")){
943  std::cout<<"writing beam intensity only to old lumisummary table "<<std::endl;
944  writeBeamIntensityOnly(session,runnumber,lversion,lumiresult.begin(),lumiresult.end());
945  std::cout<<"done"<<std::endl;
946  }else{
947  if(m_mode=="loadoldschema"){
948  std::cout<<"writing all lumi data to old lumisummary table "<<std::endl;
949  writeAllLumiData(session,runnumber,lversion,lumiresult.begin(),lumiresult.end());
950  std::cout<<"done"<<std::endl;
951  }
952  }
953  std::cout<<"writing all lumi data to lumisummary_V2 table "<<std::endl;
954  writeAllLumiDataToSchema2(session,m_source,runnumber,bgev,ncollidingbunches,lumiresult.begin(),lumiresult.end());
955  std::cout<<"done"<<std::endl;
956  cleanTemporaryMemory(lumiresult.begin(),lumiresult.end());
957  delete session;
958  delete svc;
959  }catch( const coral::Exception& er){
960  std::cout<<"database error "<<er.what()<<std::endl;
961  session->transaction().rollback();
962  delete session;
963  delete svc;
964  throw er;
965  }
966 }
967 const std::string lumi::Lumi2DB::dataType() const{
968  return "LUMI";
969 }
970 const std::string lumi::Lumi2DB::sourceType() const{
971  return "DB";
972 }
974 
977 #endif
int i
Definition: DBlmapReader.cc:9
std::vector< PerLumiData > LumiResult
Definition: Lumi2DB.cc:84
void parseSourceString(lumi::Lumi2DB::LumiSource &result) const
Definition: Lumi2DB.cc:659
void writeBeamIntensityOnly(coral::ISessionProxy *session, unsigned int irunnumber, const std::string &ilumiversion, LumiResult::iterator lumiBeg, LumiResult::iterator lumiEnd)
Definition: Lumi2DB.cc:141
unsigned int lumilsnr
Definition: Lumi2DB.cc:69
list lumirundata
Definition: dataDML.py:2353
void addEntry(coral::ISchema &schema, const std::string &datatableName, const Entry &entry, unsigned long long branch_id, const std::string &branchname)
Definition: RevisionDML.cc:61
tuple pieces
Definition: csv2json.py:31
tuple lumi
Definition: fjr2json.py:35
bool hasStableBeam(lumi::Lumi2DB::LumiResult::iterator lumiBeg, lumi::Lumi2DB::LumiResult::iterator lumiEnd)
Definition: Lumi2DB.cc:106
const unsigned int N_LUMIALGO
Definition: ConstantDef.h:4
uint16_t size_type
virtual const std::string sourceType() const
Definition: Lumi2DB.cc:970
unsigned int numorbit
Definition: Lumi2DB.cc:71
static const unsigned int COMMITLSINTERVAL
Definition: Lumi2DB.cc:35
std::vector< PerBXData > bxOCC2
Definition: Lumi2DB.cc:74
unsigned int firstsection
Definition: Lumi2DB.cc:44
void insertLumiRunData(coral::ISchema &schema, const LumiEntry &lumientry)
Definition: RevisionDML.cc:123
unsigned long long data_id
Definition: RevisionDML.h:22
virtual void retrieveData(unsigned int)
Definition: Lumi2DB.cc:727
tuple result
Definition: query.py:137
virtual char const * what() const
Definition: Exception.h:12
void setAuthentication(const std::string &authPath)
Definition: DBConfig.cc:15
unsigned long long getEntryInBranchByName(coral::ISchema &schema, const std::string &datatableName, const std::string &entryname, const std::string &branchname)
Definition: RevisionDML.cc:14
static const std::string lumisummaryTableName()
Definition: LumiNames.cc:8
int j
Definition: DBlmapReader.cc:9
float applyCalibration(float varToCalibrate) const
Definition: Lumi2DB.cc:102
Lumi2DB(const std::string &dest)
Definition: Lumi2DB.cc:657
unsigned int startorbit
Definition: Lumi2DB.cc:70
void cleanTemporaryMemory(lumi::Lumi2DB::LumiResult::iterator lumiBeg, lumi::Lumi2DB::LumiResult::iterator lumiEnd)
Definition: Lumi2DB.cc:647
void retrieveBeamIntensity(HCAL_HLX::DIP_COMBINED_DATA *dataPtr, Lumi2DB::beamData &b) const
Definition: Lumi2DB.cc:687
int k[5][pyjets_maxn]
void writeAllLumiDataToSchema2(coral::ISessionProxy *session, const std::string &source, unsigned int runnumber, float bgev, unsigned int ncollidingbunches, LumiResult::iterator lumiBeg, LumiResult::iterator lumiEnd)
Definition: Lumi2DB.cc:421
static const std::string lumidataTableName()
Definition: LumiNames.cc:41
unsigned long long entry_id
Definition: RevisionDML.h:21
std::string mode
Definition: Lumi2DB.cc:78
void bookNewEntry(coral::ISchema &schema, const std::string &datatableName, Entry &entry)
Definition: RevisionDML.cc:43
unsigned int cmslsnr
Definition: Lumi2DB.cc:68
float m_norm
Definition: DataPipe.h:32
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
const unsigned int N_BX
Definition: ConstantDef.h:5
static const std::string lumidetailTableName()
Definition: LumiNames.cc:11
double b
Definition: hdecay.h:120
unsigned int ncollidingbunches
Definition: RevisionDML.h:31
static const std::string lumisummaryv2TableName()
Definition: LumiNames.cc:44
unsigned long long generateNextIDForTable(const std::string &tableName, unsigned int interval=1)
Definition: idDealer.cc:31
std::vector< PerBXData > bxOCC1
Definition: Lumi2DB.cc:73
virtual const std::string dataType() const
Definition: Lumi2DB.cc:967
double a
Definition: hdecay.h:121
bool isLumiDataValid(LumiResult::iterator lumiBeg, LumiResult::iterator lumiEnd)
Definition: Lumi2DB.cc:123
void writeAllLumiData(coral::ISessionProxy *session, unsigned int irunnumber, const std::string &ilumiversion, LumiResult::iterator lumiBeg, LumiResult::iterator lumiEnd)
Definition: Lumi2DB.cc:217
tuple cout
Definition: gather_cfg.py:121
#define DEFINE_EDM_PLUGIN(factory, type, name)
void addRevision(coral::ISchema &schema, const std::string &datatableName, const Entry &revision, unsigned long long branch_id, std::string &branchname)
Definition: RevisionDML.cc:99
virtual ~Lumi2DB()
Definition: Lumi2DB.cc:973
std::vector< PerBXData > bxET
Definition: Lumi2DB.cc:72
tuple inputData
Definition: idDealer.py:72
void bookNewRevision(coral::ISchema &schema, const std::string &datatableName, Entry &revision)
Definition: RevisionDML.cc:53