CMS 3D CMS Logo

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