CMS 3D CMS Logo

EcalLaserCondTools.cc
Go to the documentation of this file.
1 //emacs settings:-*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  * $Id: EcalLaserCondTools.cc,v 1.2 2010/06/14 10:45:17 pgras Exp $
4  *
5  * author: Ph Gras. June, 2010
6  */
7 
9 
13 
16 
17 #include "hdf5.h"
18 
19 #include <string>
20 #include <fstream>
21 #include <algorithm>
22 #include <memory>
23 #include <cmath>
24 
26  : fout_(nullptr),
27  eventList_(nullptr),
28  eventListFileName_(ps.getParameter<std::string>("eventListFile")),
29  verb_(ps.getParameter<int>("verbosity")),
30  mode_(ps.getParameter<std::string>("mode")),
31  fnames_(ps.getParameter<std::vector<std::string> >("inputFiles")),
32  skipIov_(ps.getParameter<int>("skipIov")),
33  nIovs_(ps.getParameter<int>("nIovs")),
34  fromTime_(ps.getParameter<int>("fromTime")),
35  toTime_(ps.getParameter<int>("toTime")),
36  minP_(ps.getParameter<double>("transparencyMin")),
37  maxP_(ps.getParameter<double>("transparencyMax")) {
38  if (mode_ == "db_to_ascii_file") {
40  }
41 
42  ferr_ = fopen("corr_errors.txt", "w");
43  fprintf(ferr_, "#t1\tdetid\tp1\tp2\tp3");
44 
45  if (!eventListFileName_.empty()) {
46  eventList_ = fopen(eventListFileName_.c_str(), "r");
47  if (eventList_ == nullptr)
48  throw cms::Exception("User") << "Failed to open file " << eventListFileName_ << "\n";
49  }
50 }
51 
53  if (ferr_)
54  fclose(ferr_);
55  if (fout_)
56  fclose(fout_);
57 }
58 
60  if (mode_ == "ascii_file_to_db") {
61  if (verb_ > 2)
62  edm::LogPrint("EcalLaserCondTools") << "ascii_file_to_db mode\n";
63 
64  if (!db_.isAvailable()) {
65  throw cms::Exception("CondDBAccess") << "Failed to connect to PoolDBOutputService\n";
66  }
67  FileReader corrReader(fnames_);
68  corrReader.setVerbosity(verb_);
69  fillDb(corrReader);
70  } else if (mode_ == "hdf_file_to_db") {
72  } else if (mode_ == "db_to_ascii_file") {
73  dbToAscii(es);
74  } else {
75  cms::Exception("InvalidParam") << "Value of parameter mode is not valid. Expecting ascii_file_to_db or read";
76  }
77 }
78 
80  cond::Time_t iovStart = 0;
81 
82  hid_t file, space, memspace;
83  hid_t dset_rawid, dset_t2, dset;
84 
85  hsize_t dims[2] = {};
86 
87  for (unsigned int ifile = 0; ifile < fnames_.size(); ++ifile) {
88  if (verb_) {
89  edm::LogPrint("EcalLaserCondTools") << " - converting file: " << fnames_[ifile] << "\n";
90  }
91 
92  file = H5Fopen(fnames_[ifile].c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
93 
94  dset_rawid = H5Dopen(file, "cmssw_id", H5P_DEFAULT);
95  space = H5Dget_space(dset_rawid);
96  H5Sget_simple_extent_dims(space, dims, nullptr);
97 
98  unsigned int nCrystals = dims[0];
99  int rawid[nCrystals];
100  herr_t status;
101 
102  status = H5Dread(dset_rawid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rawid);
103  if (status < 0)
104  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
105 
106  H5Dclose(dset_rawid);
107  H5Sclose(space);
108 
109  dset_t2 = H5Dopen(file, "t2", H5P_DEFAULT);
110  space = H5Dget_space(dset_t2);
111  H5Sget_simple_extent_dims(space, dims, nullptr);
112 
113  unsigned int nIovs = dims[0];
114  unsigned int nLME = dims[1];
115 
116  if (verb_) {
117  edm::LogPrint("EcalLaserCondTools") << "Number of crystals: " << nCrystals << "\n";
118  edm::LogPrint("EcalLaserCondTools") << "Number of IOVs: " << nIovs << "\n";
119  edm::LogPrint("EcalLaserCondTools") << "Number of Monitoring regions: " << nLME << "\n";
120  }
121 
122  int t1[nIovs], t3[nIovs], t2[nIovs][nLME];
123 
124  // -- reading data (cmsswid, t2, t1, t3, p2, p1, p3
125  if (verb_ > 1)
126  edm::LogPrint("EcalLaserCondTools") << " * reading t2 table "
127  << "\n";
128  status = H5Dread(dset_t2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, t2[0]);
129  if (status < 0)
130  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
131 
132  H5Dclose(dset_t2);
133  //H5Sclose(space);
134 
135  if (verb_ > 1)
136  edm::LogPrint("EcalLaserCondTools") << " * reading t1 table "
137  << "\n";
138  dset = H5Dopen(file, "t1", H5P_DEFAULT);
139  status = H5Dread(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, t1);
140  if (status < 0)
141  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
142 
143  H5Dclose(dset);
144 
145  if (verb_ > 1)
146  edm::LogPrint("EcalLaserCondTools") << " * reading t3 table "
147  << "\n";
148  dset = H5Dopen(file, "t3", H5P_DEFAULT);
149  status = H5Dread(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, t3);
150  if (status < 0)
151  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
152 
153  H5Dclose(dset);
154 
156 
157  // read crystal info IOV by IOV (otherwise too large)
158  float p1[nCrystals], p2[nCrystals], p3[nCrystals];
159  hsize_t iov_dim[1] = {nCrystals};
160  memspace = H5Screate_simple(1, iov_dim, nullptr);
161 
162  EcalLaserAPDPNRatios corrSet;
163  for (unsigned int iIov = skipIov_; iIov < nIovs && iIov < unsigned(nIovs_); ++iIov) {
165  iovStart = uint64_t(t1[iIov]) << 32;
166  for (size_t iLme = 0; iLme < EcalLaserCondTools::nLmes; ++iLme) {
167  t.t1 = edm::Timestamp(uint64_t(t1[iIov]) << 32);
168  t.t2 = edm::Timestamp(uint64_t(t2[iIov][iLme]) << 32);
169  t.t3 = edm::Timestamp(uint64_t(t3[iIov]) << 32);
170  corrSet.setTime(iLme, t);
171  }
172 
173  hsize_t offset[2] = {iIov, 0}; // shift rows: iIov, columns: 0
174  hsize_t count[2] = {1, nCrystals}; // 1 row, nXtal columns
175 
176  dset = H5Dopen(file, "p1", H5P_DEFAULT);
177  space = H5Dget_space(dset);
178  status = H5Sselect_hyperslab(space, H5S_SELECT_SET, offset, nullptr, count, nullptr);
179  if (status < 0)
180  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
181 
182  status = H5Dread(dset, H5T_NATIVE_FLOAT, memspace, space, H5P_DEFAULT, p1);
183  if (status < 0)
184  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
185 
186  H5Dclose(dset);
187  //H5Sclose(space);
188 
189  dset = H5Dopen(file, "p2", H5P_DEFAULT);
190  space = H5Dget_space(dset);
191  status = H5Sselect_hyperslab(space, H5S_SELECT_SET, offset, nullptr, count, nullptr);
192  if (status < 0)
193  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
194 
195  status = H5Dread(dset, H5T_NATIVE_FLOAT, memspace, space, H5P_DEFAULT, p2);
196  if (status < 0)
197  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
198 
199  H5Dclose(dset);
200  // H5Sclose(space);
201 
202  dset = H5Dopen(file, "p3", H5P_DEFAULT);
203  space = H5Dget_space(dset);
204  status = H5Sselect_hyperslab(space, H5S_SELECT_SET, offset, nullptr, count, nullptr);
205  if (status < 0)
206  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
207 
208  status = H5Dread(dset, H5T_NATIVE_FLOAT, memspace, space, H5P_DEFAULT, p3);
209  if (status < 0)
210  throw cms::Exception("EcalLaserCondTool:HDF") << "Error while reading HD file.";
211  H5Dclose(dset);
212  H5Sclose(space);
213 
214  for (size_t iXtal = 0; iXtal < nCrystals; ++iXtal) {
215  DetId detid = rawid[iXtal];
216 
218  corr.p1 = p1[iXtal];
219  corr.p2 = p2[iXtal];
220  corr.p3 = p3[iXtal];
221 
222  if (!std::isfinite(corr.p1) || !std::isfinite(corr.p2) || !std::isfinite(corr.p3) || corr.p1 < minP_ ||
223  corr.p1 > maxP_ || corr.p2 < minP_ || corr.p2 > maxP_ || corr.p3 < minP_ || corr.p3 > maxP_) {
224  fprintf(ferr_, "%d %d %f %f %f\n", t1[iIov], (int)detid, corr.p1, corr.p2, corr.p3);
225  corr.p1 = corr.p2 = corr.p3 = 1;
226  }
227  corrSet.setValue((int)detid, corr);
228  }
229 
230  try {
231  //Write correction set in DB (one IOV):
232  //if (db_->isNewTagRequest("EcalLaserAPDPNRatiosRcd")) {
233  // if (verb_)
234  // edm::LogPrint("EcalLaserCondTools") << "First IOV, extending starting time.\n";
235  // iovStart = db_->beginOfTime();
236  //}
237  timeval t;
238  gettimeofday(&t, nullptr);
239  if (verb_ > 1)
240  edm::LogPrint("EcalLaserCondTools")
241  << "[" << timeToString(t.tv_sec) << "] "
242  << "Write IOV " << iIov << " starting from " << timeToString(iovStart >> 32) << "... ";
243  db_->writeOneIOV(corrSet, iovStart, "EcalLaserAPDPNRatiosRcd");
244  } catch (const cms::Exception& e) {
245  if (verb_ > 1)
246  edm::LogPrint("EcalLaserCondTools") << "Failed. ";
247  edm::LogPrint("EcalLaserCondTools") << "Exception catched while writting to cond DB" << e.what() << "\n";
248  }
249  if (verb_ > 1)
250  edm::LogPrint("EcalLaserCondTools") << "Suceeded.\n";
251 
252  } // loop over IOVs
253 
254  H5Sclose(memspace);
255  H5Fclose(file);
256  } // loop over input files
257 }
258 
260  int iIov = 0;
261  int processedIovs = 0;
262  if (verb_ > 2)
263  edm::LogPrint("EcalLaserCondTools") << "Starting filling DB...\n";
264  int t1 = 0;
265  int t3 = 0;
266  int t2[nLmes];
267 
268  int prevT1 = 0;
269  int prevT3 = 0;
270  int prevT = 0;
271  int t = 0;
272  if (eventList_) {
273  int iline = 0;
274  while (!feof(eventList_)) {
275  //skips comment lines:
276  char c[2];
277  while (fscanf(eventList_, " %1[#]%*[^\n]\n", &c[0]) == 1)
278  ++iline;
279 
280  int n = fscanf(eventList_, "%*d %*d %*d %d%*[^\n]\n", &t);
281  if (verb_ > 1)
282  edm::LogPrint("EcalLaserCondTools") << "Event time: t = " << t << ", " << timeToString(t) << "\n";
283  ++iline;
284  if (n != 1)
285  throw cms::Exception("User") << "Syntax error in event list file " << eventListFileName_ << " at line " << iline
286  << " " << n << " "
287  << ".\n";
288  if (t < prevT)
289  throw cms::Exception("User") << "Events in the event list file " << eventListFileName_
290  << " are not ordered in increased time as required! See line " << iline << "\n";
291  if (t == 0)
292  throw cms::Exception("User") << "Found an unexpected t = 0 time value "
293  "in the event list file"
294  << eventListFileName_ << " at line " << iline << "!\n";
295  //Look for IOV:
296  bool iovFound = true;
297  if (t <= t3) { //IOV already inserted for previous event.
298  if (verb_ > 1)
299  edm::LogPrint("EcalLaserCondTools") << "Event in same IOV than previous one.\n";
300  continue;
301  }
302 
303  while ((iovFound = r.readTime(t1, t2, t3)) && t3 < t) /*NOP*/
304  ;
305 
306  if (iovFound) {
307  if (t1 < prevT1 && t3 < prevT3)
308  throw cms::Exception("User")
309  << "IOVs in the correction ascii file are not ordered in increased time as required!\n";
310  else if (t1 < prevT1 || t3 < prevT3)
311  throw cms::Exception("User") << "Found interleaved IOVs in the correction ascii file!\n";
312  processIov(r, t1, t2, t3);
313  } else {
314  edm::LogPrint("EcalLaserCondTools") << "Warning: event beyond last IOV t3. Event time: " << timeToString(t)
315  << ". Last IOV t3: " << timeToString(t3) << "\n";
316  }
317  }
318  } else
319  while (r.readTime(t1, t2, t3)) {
320  ++iIov;
321  if (iIov <= skipIov_) {
322  edm::LogPrint("EcalLaserCondTools") << "Skipping IOV " << iIov << "\n";
323  continue;
324  } else if (processedIovs >= nIovs_ && nIovs_ >= 0) {
325  edm::LogPrint("EcalLaserCondTools") << "Requested number of IOVs, " << nIovs_ << ", processed.\n";
326  return;
327  } else {
328  processIov(r, t1, t2, t3);
329  ++processedIovs;
330  }
331  }
332 }
333 
335  static int iIov = 0;
336  ++iIov;
337 
338  // FILE* fdebug = 0;
339  // if(i==) fdebug = fopen("debug.txt", "w");
340 
341  // if(iIov <= skipIov_) { std::cout << "Skipping IOV " << iIov << "\n"; return; }
342 
343  cond::Time_t iovStart = 0;
344 
345  if (verb_ > 1) {
346  edm::LogPrint("EcalLaserCondTools") << "t1:" << t1 << "(" << timeToString(t1) << ") \n"
347  << "t3: " << t3 << "(" << timeToString(t3) << ")\nt2-t1: ";
348  for (int i = 0; i < EcalLaserCondTools::nLmes; ++i) {
349  edm::LogPrint("EcalLaserCondTools") << t2[i] - t1 << "\t";
350  }
351 
352  edm::LogPrint("EcalLaserCondTools") << "\n";
353  }
354  if (t1 < fromTime_) {
355  edm::LogPrint("EcalLaserCondTools") << "Skipping IOV " << iIov << ", "
356  << ", which is before 'fromTime'," << timeToString(fromTime_) << "("
357  << fromTime_ << ").\n";
358  return;
359  }
360 
361  if (toTime_ != -1 && t3 < toTime_) {
362  edm::LogPrint("EcalLaserCondTools") << "Skipping IOV " << iIov << ", "
363  << ", which is beyond 'toTime'," << timeToString(toTime_) << "(" << toTime_
364  << ").\n";
365  return;
366  }
367 
368  if (t1 == 0) {
369  edm::LogPrint("EcalLaserCondTools") << "Skipping IOV with t1 = 0"
370  << "\n";
371  return;
372  }
373 
374  EcalLaserAPDPNRatios corrSet;
375 
377  iovStart = uint64_t(t1) << 32;
378  for (size_t i = 0; i < EcalLaserCondTools::nLmes; ++i) {
379  t.t1 = edm::Timestamp(uint64_t(t1) << 32);
380  t.t2 = edm::Timestamp(uint64_t(t2[i]) << 32);
381  t.t3 = edm::Timestamp(uint64_t(t3) << 32);
382  corrSet.setTime(i, t);
383  }
384 
385  constexpr int ncrystals = 75848;
386  std::set<int> detidList;
387  for (int i = 0; i < ncrystals; ++i) {
388  DetId detid;
389  //EcalLaserAPDPNRatios::EcalLaserAPDPNpair corr = {0, 0, 0};
391  if (verb_ > 2)
392  edm::LogPrint("EcalLaserCondTools") << "Reading " << toNth(i + 1) << " crystal\n";
393  if (!r.readPs(detid, corr)) {
394  throw cms::Exception("LasCor") << "Failed to read " << toNth(i + 1) << " crystal correction.\n";
395  }
396 
397  std::pair<std::set<int>::iterator, bool> res = detidList.insert(int(detid));
398 
399  if (!res.second) { //detid already processed
400  edm::LogPrint("EcalLaserCondTools")
401  << "Duplicate det id, for IOV " << iIov << " t1 = " << t1 << " detid = " << int(detid) << "\n";
402  }
403 
404  if (!std::isfinite(corr.p1) || !std::isfinite(corr.p2) || !std::isfinite(corr.p3) || corr.p1 < minP_ ||
405  corr.p1 > maxP_ || corr.p2 < minP_ || corr.p2 > maxP_ || corr.p3 < minP_ || corr.p3 > maxP_) {
406  fprintf(ferr_, "%d %d %f %f %f\n", t1, (int)detid, corr.p1, corr.p2, corr.p3);
407  corr.p1 = corr.p2 = corr.p3 = 1;
408  }
409 
410  if (verb_ > 2) {
411  if (detid.subdetId() == EcalBarrel) {
412  edm::LogPrint("EcalLaserCondTools") << EBDetId(detid);
413  } else if (detid.subdetId() == EcalEndcap) {
414  edm::LogPrint("EcalLaserCondTools") << EEDetId(detid);
415  } else {
416  edm::LogPrint("EcalLaserCondTools") << (int)detid;
417  }
418  edm::LogPrint("EcalLaserCondTools") << ": "
419  << "p1 = " << corr.p1 << "\t"
420  << "p2 = " << corr.p2 << "\t"
421  << "p3 = " << corr.p3 << "\n";
422  }
423 
424  corrSet.setValue((int)detid, corr);
425  }
426 
427  try {
428  //Write correction set in DB (one IOV):
429  if (db_->isNewTagRequest("EcalLaserAPDPNRatiosRcd")) {
430  if (verb_)
431  edm::LogPrint("EcalLaserCondTools") << "First IOV, extending starting time.\n";
432 
433  iovStart = db_->beginOfTime();
434  }
435  timeval t;
436  gettimeofday(&t, nullptr);
437  if (verb_ > 1)
438  edm::LogPrint("EcalLaserCondTools")
439  << "[" << timeToString(t.tv_sec) << "] "
440  << "Write IOV " << iIov << " starting from " << timeToString(iovStart >> 32) << "... ";
441  db_->writeOneIOV(corrSet, iovStart, "EcalLaserAPDPNRatiosRcd");
442  } catch (const cms::Exception& e) {
443  edm::LogPrint("EcalLaserCondTools") << "Failed.\nException cathed while writting to cond DB" << e.what() << "\n";
444  }
445  edm::LogPrint("EcalLaserCondTools") << "Suceeded.\n";
446 }
447 
449  for (;;) {
450  ++ifile_;
451  if (ifile_ >= fnames_.size()) {
452  if (verb_ > 1)
453  edm::LogPrint("EcalLaserCondTools") << "No more correction files.\n";
454 
455  return false;
456  }
457  if (verb_ > 1)
458  edm::LogPrint("EcalLaserCondTools") << "Opening file " << fnames_[ifile_] << "\n";
459 
460  f_ = fopen(fnames_[ifile_].c_str(), "r");
461  iline_ = 0;
462  if (f_ == nullptr) {
463  std::cerr << "Failed to open file " << fnames_[ifile_] << ". File skipped!\n";
464  } else {
465  return true;
466  }
467  }
468 }
469 
471  trim();
472  if ((f_ == nullptr || feof(f_)) && !nextFile()) {
473  if (verb_ > 1)
474  edm::LogPrint("EcalLaserCondTools") << "No more record\n";
475 
476  return false;
477  }
478  int i;
479  char* buf = nullptr;
480  size_t s = 0;
481  while ((i = fgetc(f_)) != 'T' && i != 'L' && i >= 0)
482  getline(&buf, &s, f_);
483  if (buf)
484  free(buf);
485  buf = nullptr;
486 
487  if (i == 'L') { //last record put 3 consecutive times starting from end of prev. IOV
488  t1 = t3;
489  for (int i = 0; i < EcalLaserCondTools::nLmes; ++i)
490  t2[i] = t1 + 1;
491  t3 = t1 + 2;
492  return true;
493  }
494 
495  if (i != 'T') {
496  if (verb_ > 1)
497  edm::LogPrint("EcalLaserCondTools") << "No more record or bad line type/marker (getc returned " << i << ")\n";
498 
499  return false;
500  }
501 
503  int n = fscanf(f_, "%d %d", &t1, &t3);
504  for (int i = 0; i < EcalLaserCondTools::nLmes; ++i) {
505  int nn = fscanf(f_, "%d", &t2[i]);
506  if (nn != 1)
507  break;
508  n += nn;
509  }
510 
511  int nnn = fscanf(f_, " ");
512 
513  if (n != (2 + EcalLaserCondTools::nLmes) || nnn != 0)
514  throw cms::Exception("LasCorFile") << "File " << fnames_[ifile_] << " line " << iline_
515  << ": syntax error. Expecting 'T' marker followed by 94 values: "
516  << "t1 t2 t3(lme 1) t3(lme 2) ... t3(lme " << EcalLaserCondTools::nLmes << ")\n";
517 
518  return true;
519 }
520 
522  if (f_ == nullptr) {
523  if (verb_)
524  edm::LogPrint("EcalLaserCondTools") << "Requested to read p1..p3 parameter line while no file is closed.\n";
525 
526  return false;
527  }
528 
529  trim();
530  int i = fgetc(f_);
531 
532  if (i != 'P') {
533  if (verb_ && i >= 0)
534  edm::LogPrint("EcalLaserCondTools") << "File " << fnames_[ifile_] << " line " << iline_
535  << ": unexpected line type, '" << (char)i << "' while expecting 'P'\n";
536 
537  if (verb_ && i < 0)
538  edm::LogPrint("EcalLaserCondTools") << "Failed to read p1..p3 parameter line\n";
539 
540  return false;
541  }
542 
543  int rawdetid;
544  int n = fscanf(f_, "%d %f %f %f\n", &rawdetid, &corr.p1, &corr.p2, &corr.p3);
545  ++iline_;
546 
547  if (n != 4) {
548  // corr.p2=corr.p1;
549  // corr.p3=corr.p1;
550  throw cms::Exception("I/O") << "Syntax error at line " << iline_ << "of file " << fnames_[ifile_] << " read " << n
551  << " values,"
552  << " raw id" << rawdetid << ": " << corr.p1 << ", " << corr.p2;
553  }
554  detid = rawdetid;
555  constexpr int ECALID = 3;
556  if (detid.det() != ECALID)
557  throw cms::Exception("InvalidValue") << "Line " << iline_ << "of file " << fnames_[ifile_]
558  << " contains an invalid det ID (detector code is not ECAL!)\n";
559  if (detid.subdetId() == EcalBarrel) {
560  EBDetId ebDetId(detid);
561  if (!EBDetId::validDetId(ebDetId.ietaAbs(), ebDetId.iphi()))
562  throw cms::Exception("InvalidValue") << "Line " << iline_ << "of file " << fnames_[ifile_]
563  << " contains an invalid det ID (detector code is not ECAL!)\n";
564  }
565  if (detid.subdetId() == EcalEndcap) {
566  EEDetId eeDetId(detid);
567  if (!EEDetId::validDetId(eeDetId.ix(), eeDetId.iy(), eeDetId.zside()))
568  throw cms::Exception("InvalidValue") << "Line " << iline_ << "of file " << fnames_[ifile_]
569  << " contains an invalid det ID (detector code is not ECAL!)\n";
570  }
571  ++iline_;
572  return true;
573 }
574 
576  if (f_ == nullptr)
577  return;
578  bool skipLine = false;
579  int c;
580  while ((c = fgetc(f_)) >= 0 && (c == ' ' || c == '\t' || c == '\n' || c == '#' || skipLine)) {
581  if (c == '#')
582  skipLine = true;
583  if (c == '\n') {
584  ++iline_;
585  skipLine = false;
586  }
587  }
588  ungetc(c, f_);
589 }
590 
592  std::stringstream s;
593  s << n;
594  if (n % 100 < 10 || n % 100 > 20) {
595  switch (n % 10) {
596  case 1:
597  s << "st";
598  break;
599  case 2:
600  s << "nd";
601  break;
602  case 3:
603  s << "rd";
604  break;
605  default:
606  s << "th";
607  }
608  } else {
609  s << "th";
610  }
611  return s.str();
612 }
613 
615  char buf[256];
616  struct tm lt;
617  localtime_r(&t, &lt);
618  strftime(buf, sizeof(buf), "%F %R:%S", &lt);
619  buf[sizeof(buf) - 1] = 0;
620  return std::string(buf);
621 }
622 
624  const auto& laserAPDPNRatios = es.getData(laserAPDPNRatiosToken_);
625 
626  const EcalLaserAPDPNRatios::EcalLaserAPDPNRatiosMap& p = laserAPDPNRatios.getLaserMap();
627  const EcalLaserAPDPNRatios::EcalLaserTimeStampMap& t = laserAPDPNRatios.getTimeMap();
628 
629  if (t.size() != EcalLaserCondTools::nLmes)
630  throw cms::Exception("LasCor") << "Unexpected number time parameter triplets\n";
631 
632  if (fout_ == nullptr) {
633  fout_ = fopen("corr_dump.txt", "w");
634  if (fout_ == nullptr)
635  throw cms::Exception("LasCor") << "Failed to create file corr_dump.txt\n";
636  }
637 
638  unsigned t1 = t[0].t1.unixTime();
639  unsigned t3 = t[0].t3.unixTime();
640  fprintf(fout_, "T %d\t%d", t1, t3);
641 
642  if (verb_)
643  edm::LogPrint("EcalLaserCondTools") << "Processing IOV " << t1 << " - " << t3 << "(" << timeToString(t1) << " - "
644  << timeToString(t3) << "\n";
645 
646  for (unsigned i = 0; i < t.size(); ++i) {
647  if (t[i].t1.unixTime() != t1 || t[i].t3.unixTime() != t3) {
648  throw cms::Exception("LasCor") << "Inconsitency in t1, t3: "
649  << "t1(lme 1) =" << t1 << ", t1(lme " << (i + 1) << ") = " << t[i].t1.unixTime()
650  << ", t3(lme 1) =" << t3 << ", t3(lme " << (i + 1) << ") = " << t[i].t3.unixTime()
651  << "\n";
652  }
653  fprintf(fout_, "\t%d", t[i].t2.unixTime());
654  }
655  fputc('\n', fout_);
656  fflush(fout_);
657 
658  for (int ieta = -EBDetId::MAX_IETA; ieta <= EBDetId::MAX_IETA; ++ieta) {
659  if (ieta == 0)
660  continue;
661  for (int iphi = EBDetId::MIN_IPHI; iphi <= EBDetId::MAX_IPHI; ++iphi) {
662  if (EBDetId::validDetId(ieta, iphi)) {
664  EcalLaserAPDPNRatios::EcalLaserAPDPNpair corr = p.barrel(detId.hashedIndex());
665  fprintf(fout_, "P %d\t%f\t%f\t%f\n", (int)detId, corr.p1, corr.p2, corr.p3);
666  }
667  }
668  }
669 
670  for (int iZ = 1; iZ >= -1; --iZ) {
671  for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
672  for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
673  if (EEDetId::validDetId(iX, iY, iZ)) {
674  EEDetId detId(iX, iY, iZ);
675  EcalLaserAPDPNRatios::EcalLaserAPDPNpair corr = p.endcap(detId.hashedIndex());
676  fprintf(fout_, "P %d\t%f\t%f\t%f\n", (int)detId, corr.p1, corr.p2, corr.p3);
677  }
678  }
679  }
680  }
681 }
682 //DEFINE_FWK_MODULE(EcalLaserCondTools);
std::vector< std::string > fnames_
static const int MIN_IPHI
Definition: EBDetId.h:135
EcalLaserCondTools(const edm::ParameterSet &)
int ietaAbs() const
get the absolute value of the crystal ieta
Definition: EBDetId.h:47
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void fillDb(CorrReader &r)
~EcalLaserCondTools() override
int iphi() const
get the crystal iphi
Definition: EBDetId.h:51
static std::string timeToString(time_t t)
edm::Service< cond::service::PoolDBOutputService > db_
int ix() const
Definition: EEDetId.h:77
static void trim(std::string &s)
void processIov(CorrReader &r, int t1, int t2[nLmes], int t3)
assert(be >=bs)
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46
static const int IX_MIN
Definition: EEDetId.h:290
Definition: Electron.h:6
static const int IY_MIN
Definition: EEDetId.h:294
static bool validDetId(int i, int j)
check if a valid index combination
Definition: EBDetId.h:118
void dbToAscii(const edm::EventSetup &es)
bool readPs(DetId &rawdetid, EcalLaserAPDPNRatios::EcalLaserAPDPNpair &corr) override
unsigned nCrystals(unsigned)
bool readTime(int &t1, int t2[EcalLaserCondTools::nLmes], int &t3) override
void setTime(int hashedIndex, const EcalLaserTimeStamp &value)
unsigned long long Time_t
Definition: Time.h:14
dictionary corr
bool isNewTagRequest(const std::string &recordName)
static constexpr int nLmes
std::string eventListFileName_
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
void analyze(const edm::Event &evt, const edm::EventSetup &es) override
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
Log< level::Warning, true > LogPrint
static const int IX_MAX
Definition: EEDetId.h:298
Definition: DetId.h:17
static const int MAX_IPHI
Definition: EBDetId.h:137
int zside() const
Definition: EEDetId.h:71
unsigned long long uint64_t
Definition: Time.h:13
std::vector< EcalLaserTimeStamp > EcalLaserTimeStampMap
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
static const int MAX_IETA
Definition: EBDetId.h:136
void setValue(uint32_t rawId, const EcalLaserAPDPNpair &value)
static std::string toNth(int n)
edm::ESGetToken< EcalLaserAPDPNRatios, EcalLaserAPDPNRatiosRcd > laserAPDPNRatiosToken_
std::vector< std::string > fnames_
static const int IY_MAX
Definition: EEDetId.h:302
bool isAvailable() const
Definition: Service.h:40
Definition: event.py:1
int iy() const
Definition: EEDetId.h:83