CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
beamSpotDipStandalone.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <sstream>
4 #include <vector>
5 #include <cmath>
6 #include <cstring>
7 #include <ctime>
8 #include <sys/stat.h>
9 #include <bitset>
10 #include <unistd.h>
11 
12 //
13 #include "Dip.h"
14 #include "DipFactory.h"
15 #include "DipPublication.h"
16 #include "DipTimestamp.h"
17 
18 using namespace std;
19 
20 // constants
21 const char* qualities[3] = {"Uncertain", "Bad", "Good"};
22 const bool publishStatErrors = true;
23 
24 const int secPerLS = 23;
25 const int rad2urad = 1000000;
26 const int cm2um = 10000;
27 const int cm2mm = 10;
28 const int intLS = 1; // for CMS scaler
29 
30 // variables
31 long lastFitTime = 0;
32 long lastModTime = 0;
33 std::bitset<8> alive;
34 int lsCount = 0;
35 int currentLS = 0;
36 
37 // DIP objects
38 DipFactory* dip;
39 DipData* messageCMS;
40 DipData* messageLHC;
41 DipData* messagePV;
42 DipPublication* publicationCMS;
43 DipPublication* publicationLHC;
44 DipPublication* publicationPV;
45 
46 // initial values of beamspot object
47 int runnum;
48 string startTime;
49 string endTime;
50 time_t startTimeStamp = 0;
51 time_t endTimeStamp = 0;
52 string lumiRange = "0 - 0";
53 string quality = "Uncertain";
54 int type = -1;
55 float x = 0;
56 float y = 0;
57 float z = 0;
58 float dxdz = 0;
59 float dydz = 0;
60 float err_x = 0;
61 float err_y = 0;
62 float err_z = 0;
63 float err_dxdz = 0;
64 float err_dydz = 0;
65 float width_x = 0;
66 float width_y = 0;
67 float sigma_z = 0;
68 float err_width_x = 0;
69 float err_width_y = 0;
70 float err_sigma_z = 0;
71 
72 //
73 int events = 0;
74 float meanPV = 0;
75 float err_meanPV = 0;
76 float rmsPV = 0;
77 float err_rmsPV = 0;
78 int maxPV = 0;
79 int nPV = 0;
80 
81 //
82 float Size[3];
83 float Centroid[3];
84 float Tilt[2];
85 
86 //
87 bool verbose = false;
88 bool testing = false;
89 
90 const string subjectCMS = "dip/CMS/Tracker/BeamSpot";
91 const string subjectLHC = "dip/CMS/LHC/LuminousRegion";
92 const string subjectPV = "dip/CMS/Tracker/PrimaryVertices";
93 
94 string sourceFile = "/nfshome0/dqmpro/BeamMonitorDQM/BeamFitResultsForDIP.txt";
95 string sourceFile1 = "/nfshome0/dqmpro/BeamMonitorDQM/BeamFitResultsOld_TkStatus.txt";
96 
97 const int timeoutLS[2] = {1, 2};
98 
99 //
100 
101 /*****************************************************************************/
102 string getDateTime(time_t t) {
103  char mbstr[100];
104  strftime(mbstr, sizeof(mbstr), "%Y.%m.%d %H:%M:%S %z", std::localtime(&t));
105 
106  return mbstr;
107 }
108 
109 string getDateTime() {
110  time_t t = time(nullptr);
111 
112  return getDateTime(t);
113 }
114 
115 /*****************************************************************************/
116 class ErrHandler : public DipPublicationErrorHandler {
117 public:
118  virtual ~ErrHandler() = default;
119 
120 private:
121  void handleException(DipPublication* publication, DipException& e) override {
122  cerr << "exception (create): " << e.what() << endl;
123  }
124 };
125 
126 /*****************************************************************************/
127 long getFileSize(string filename) {
128  struct stat stat_buf;
129  int rc = stat(filename.c_str(), &stat_buf);
130  return (rc == 0 ? stat_buf.st_size : -1);
131 }
132 
133 /*****************************************************************************/
134 time_t getLastTime(string filename) {
135  struct stat stat_buf;
136  int rc = stat(filename.c_str(), &stat_buf);
137  return (rc == 0 ? stat_buf.st_mtime : -1);
138 }
139 
140 /*****************************************************************************/
141 vector<string> parse(string line, const string& delimiter) {
142  vector<string> list;
143 
144  size_t pos = 0;
145  while ((pos = line.find(delimiter)) != string::npos) {
146  string token = line.substr(0, pos);
147 
148  list.push_back(token);
149 
150  line.erase(0, pos + delimiter.length());
151  }
152 
153  list.push_back(line); // remainder
154 
155  return list;
156 }
157 
158 /*****************************************************************************/
159 void CMS2LHCRF_POS(float x, float y, float z) {
160  if (x != 0) { // Rotation + Translation + Inversion + Scaling
161  double tmpx = x;
162  // x*rotY[0]*rotZ[0] + y*rotY[0]*rotZ[1] - z*rotY[1] + trans[0];
163  Centroid[0] = tmpx;
164  Centroid[0] *= -1.0 * cm2um;
165  } else
166  Centroid[0] = x;
167 
168  if (y != 0) { // Rotation + Translation + Scaling
169  double tmpy = y;
170  // x*(rotX[1]*rotY[1]*rotZ[0] - rotX[0]*rotZ[1]) +
171  // y*(rotX[0]*rotZ[0] + rotX[1]*rotY[1]*rotZ[1]) +
172  // z*rotX[1]*rotY[0] + trans[1];
173  Centroid[1] = tmpy;
174  Centroid[1] *= cm2um;
175  } else
176  Centroid[1] = y;
177 
178  if (z != 0) { // Rotation + Translation + Inversion + Scaling
179  double tmpz = z;
180  // x*(rotX[0]*rotY[1]*rotZ[0] + rotX[1]*rotZ[1]) +
181  // y*(rotX[0]*rotY[1]*rotZ[1] - rotX[1]*rotZ[0]) +
182  // z*rotX[0]*rotY[0] + trans[2];
183  Centroid[2] = tmpz;
184  Centroid[2] *= -1.0 * cm2mm;
185  } else
186  Centroid[2] = z;
187 }
188 
189 /*****************************************************************************/
190 void trueRcd() {
191  try {
192  // CMS to LHC RF
193  CMS2LHCRF_POS(x, y, z);
194 
195  Tilt[0] = dxdz * rad2urad;
196  Tilt[1] = (dydz != 0 ? (dydz * -1 * rad2urad) : 0);
197 
198  Size[0] = width_x * cm2um;
199  Size[1] = width_y * cm2um;
200  Size[2] = sigma_z * cm2mm;
201 
202  // CMS
203  messageCMS->insert(runnum, "runnum");
204  messageCMS->insert(startTime, "startTime");
205  messageCMS->insert(endTime, "endTime");
206  messageCMS->insert(startTimeStamp, "startTimeStamp");
207  messageCMS->insert(endTimeStamp, "endTimeStamp");
208  messageCMS->insert(lumiRange, "lumiRange");
209  messageCMS->insert(quality, "quality");
210  messageCMS->insert(type, "type"); // Unknown=-1, Fake=0, Tracker=2(Good)
211  messageCMS->insert(x, "x");
212  messageCMS->insert(y, "y");
213  messageCMS->insert(z, "z");
214  messageCMS->insert(dxdz, "dxdz");
215  messageCMS->insert(dydz, "dydz");
216  messageCMS->insert(width_x, "width_x");
217  messageCMS->insert(width_y, "width_y");
218  messageCMS->insert(sigma_z, "sigma_z");
219 
220  if (publishStatErrors) {
221  messageCMS->insert(err_x, "err_x");
222  messageCMS->insert(err_y, "err_y");
223  messageCMS->insert(err_z, "err_z");
224  messageCMS->insert(err_dxdz, "err_dxdz");
225  messageCMS->insert(err_dydz, "err_dydz");
226  messageCMS->insert(err_width_x, "err_width_x");
227  messageCMS->insert(err_width_y, "err_width_y");
228  messageCMS->insert(err_sigma_z, "err_sigma_z");
229  }
230 
231  // LHC
232  messageLHC->insert(Size, 3, "Size");
233  messageLHC->insert(Centroid, 3, "Centroid");
234  messageLHC->insert(Tilt, 2, "Tilt");
235 
236  // PV
237  messagePV->insert(runnum, "runnum");
238  messagePV->insert(startTime, "startTime");
239  messagePV->insert(endTime, "endTime");
240  messagePV->insert(startTimeStamp, "startTimeStamp");
241  messagePV->insert(endTimeStamp, "endTimeStamp");
242  messagePV->insert(lumiRange, "lumiRange");
243  messagePV->insert(events, "events");
244  messagePV->insert(meanPV, "meanPV");
245  messagePV->insert(err_meanPV, "err_meanPV");
246  messagePV->insert(rmsPV, "rmsPV");
247  messagePV->insert(err_rmsPV, "err_rmsPV");
248  messagePV->insert(maxPV, "maxPV");
249  messagePV->insert(nPV, "nPV");
250  } catch (exception& e) {
251  cerr << "exception (trueRcd): " << e.what() << endl;
252  }
253 }
254 
255 /*****************************************************************************/
256 void fakeRcd() {
257  try {
258  Centroid[0] = 0;
259  Centroid[1] = 0;
260  Centroid[2] = 0;
261 
262  Size[0] = 0;
263  Size[1] = 0;
264  Size[2] = 0;
265 
266  Tilt[0] = 0;
267  Tilt[1] = 0;
268 
269  messageLHC->insert(Size, 3, "Size");
270  messageLHC->insert(Centroid, 3, "Centroid");
271  messageLHC->insert(Tilt, 2, "Tilt");
272  } catch (exception& e) {
273  cerr << "exception (fakeRcd): " << e.what() << endl;
274  }
275 }
276 
277 /*****************************************************************************/
278 void publishRcd(string qlty, string err, bool pubCMS, bool fitTime) {
279  try {
280  bool updateCMS = pubCMS && (currentLS % intLS == 0);
281 
282  if (verbose) {
283  cerr << "sending (" << qlty << " | " << err << ")";
284 
285  if (alive.test(7)) {
286  if (updateCMS)
287  cerr << " to CCC and CMS";
288  else if (!alive.test(1) && !alive.test(2))
289  cerr << " to CCC only";
290  }
291 
292  cerr << endl;
293  }
294 
295  DipTimestamp zeit;
296  if (fitTime) {
297  long epoch;
298  epoch = endTimeStamp * 1000; // convert to ms
299  zeit = DipTimestamp(epoch);
300  } else
301  zeit = DipTimestamp();
302 
303  // send
304  if (updateCMS)
305  publicationCMS->send(*messageCMS, zeit);
306 
307  publicationLHC->send(*messageLHC, zeit);
308  publicationPV->send(*messagePV, zeit);
309 
310  // set qualities
311 
312  if (qlty == qualities[0]) { // Uncertain
313  if (updateCMS)
314  publicationCMS->setQualityUncertain(err.c_str());
315 
316  publicationLHC->setQualityUncertain(err.c_str());
317  } else if (qlty == qualities[1]) { // Bad
318  if (updateCMS)
319  publicationCMS->setQualityBad(err.c_str());
320 
321  publicationLHC->setQualityBad(err.c_str());
322  } else if (qlty == "UNINITIALIZED") {
323  if (updateCMS)
324  publicationCMS->setQualityBad("UNINITIALIZED");
325 
326  publicationLHC->setQualityBad("UNINITIALIZED");
327  }
328  } catch (exception& e) {
329  cerr << "exception (publishRcd): " << e.what() << endl;
330  }
331 }
332 
333 /*****************************************************************************/
334 bool readRcd(ifstream& file) {
335  int nthLnInRcd = 0;
336  bool rcdQlty = false;
337 
338  try {
339  string record;
340  while (getline(file, record)) {
341  nthLnInRcd++;
342 
343  vector<string> tmp = parse(record, " ");
344 
345  switch (nthLnInRcd) {
346  case 1:
347  if (record.rfind("Run", 0) != 0) {
348  cerr << "Reading of results text file interrupted. " + getDateTime() << endl;
349  return false;
350  }
351  runnum = stoi(tmp[1]);
352  break;
353  case 2:
354  startTime = tmp[1] + " " + tmp[2] + " " + tmp[3];
355  startTimeStamp = stol(tmp[4]);
356  break;
357  case 3:
358  endTime = tmp[1] + " " + tmp[2] + " " + tmp[3];
359  endTimeStamp = stol(tmp[4]);
360  break;
361  case 4:
362  lumiRange = record.substr(10);
363  if (verbose)
364  cerr << "lumisection range: " + lumiRange << endl;
365  currentLS = stoi(tmp[3]);
366  break;
367  case 5:
368  type = stoi(tmp[1]);
369  if (testing)
370  quality = qualities[0]; // Uncertain
371  else if (type >= 2)
372  quality = qualities[2]; // Good
373  else
374  quality = qualities[1]; // Bad
375  break;
376 
377  case 6:
378  x = stof(tmp[1]);
379  break;
380  case 7:
381  y = stof(tmp[1]);
382  break;
383  case 8:
384  z = stof(tmp[1]);
385  break;
386 
387  case 9:
388  sigma_z = stof(tmp[1]);
389  break;
390  case 10:
391  dxdz = stof(tmp[1]);
392  break;
393  case 11:
394  dydz = stof(tmp[1]);
395  break;
396  case 12:
397  width_x = stof(tmp[1]);
398  break;
399  case 13:
400  width_y = stof(tmp[1]);
401  break;
402 
403  case 14:
404  err_x = sqrt(stof(tmp[1]));
405  break;
406  case 15:
407  err_y = sqrt(stof(tmp[2]));
408  break;
409  case 16:
410  err_z = sqrt(stof(tmp[3]));
411  break;
412  case 17:
413  err_sigma_z = sqrt(stof(tmp[4]));
414  break;
415  case 18:
416  err_dxdz = sqrt(stof(tmp[5]));
417  break;
418  case 19:
419  err_dydz = sqrt(stof(tmp[6]));
420  break;
421  case 20:
422  err_width_x = sqrt(stof(tmp[7]));
424  break;
425  case 21:
426  break;
427  case 22:
428  break;
429  case 23:
430  break;
431  case 24:
432  events = stoi(tmp[1]);
433  break;
434 
435  case 25:
436  meanPV = stof(tmp[1]);
437  break;
438  case 26:
439  err_meanPV = stof(tmp[1]);
440  break;
441  case 27:
442  rmsPV = stof(tmp[1]);
443  break;
444  case 28:
445  err_rmsPV = stof(tmp[1]);
446  break;
447  case 29:
448  maxPV = stoi(tmp[1]);
449  break;
450  case 30:
451  nPV = stoi(tmp[1]);
452  rcdQlty = true;
453  break;
454 
455  default:
456  break;
457  }
458  }
459 
460  file.close();
461  } catch (exception& e) {
462  cerr << "io exception (readRcd): " << e.what() << endl;
463  }
464 
465  return rcdQlty;
466 }
467 
468 /*****************************************************************************/
469 string tkStatus() {
470  string outstr;
471 
472  ifstream logfile(sourceFile1);
473 
474  if (!logfile.good() || getFileSize(sourceFile1) == 0) {
475  // file does not exist or has zero size
476  outstr = "No CMS Tracker status available. No DAQ/DQM.";
477  } else {
478  int nthLnInRcd = 0;
479  string record;
480 
481  try {
482  string record;
483 
484  while (getline(logfile, record)) {
485  nthLnInRcd++;
486  vector<string> tmp = parse(record, " ");
487 
488  switch (nthLnInRcd) {
489  case 7:
490  if (tmp[1].find("Yes") == string::npos)
491  outstr = "CMS Tracker OFF.";
492  else
493  outstr = "CMS not taking data or no beam.";
494  break;
495  case 8:
496  runnum = stoi(tmp[1]);
497  break;
498  default:
499  break;
500  }
501  }
502  } catch (exception& e) {
503  cerr << "exception (tkStatus): " << e.what() << endl;
504  }
505  }
506 
507  logfile.close();
508 
509  return outstr;
510 }
511 
512 /*****************************************************************************/
513 void problem() {
514  if (verbose)
515  cerr << "no update | alive = " << alive << endl;
516 
517  lsCount++;
518 
519  if ((lsCount % (timeoutLS[0] * secPerLS) == 0) && (lsCount % (timeoutLS[1] * secPerLS) != 0)) // first timeout
520  {
521  if (!alive.test(1))
522  alive.flip(1);
523  if (!alive.test(2)) {
524  if (!alive.test(7))
525  fakeRcd();
526  else
527  trueRcd();
528 
529  stringstream warnMsg;
530  warnMsg << "No new data for " << lsCount << " LS";
531  publishRcd("Uncertain", warnMsg.str(), false, false);
532  } else {
533  fakeRcd();
534 
535  stringstream warnMsg;
536  warnMsg << "No new data for " << lsCount << " LS: " << tkStatus();
537  publishRcd("Bad", warnMsg.str(), false, false);
538  }
539  } else if (lsCount % (timeoutLS[1] * secPerLS) == 0) { // second timeout
540  if (!alive.test(2))
541  alive.flip(2);
542  fakeRcd();
543 
544  stringstream warnMsg;
545  warnMsg << "No new data for " << lsCount << " LS: " << tkStatus();
546  publishRcd("Bad", warnMsg.str(), false, false);
547  }
548 }
549 
550 /*****************************************************************************/
551 void polling() {
552  try {
553  ifstream logFile(sourceFile);
554 
555  if (!logFile.good()) {
556  cerr << "Source File: " + sourceFile + " doesn't exist!" << endl;
557  problem();
558  } else {
560 
561  if (lastFitTime == 0)
563 
564  if (getFileSize(sourceFile) == 0) {
565  // source file has zero length
566  if (lastModTime > lastFitTime) {
567  string tmp = tkStatus();
568  cerr << "New run starts. Run number: " << runnum << endl;
569  if (verbose)
570  cerr << "Initial lastModTime = " + getDateTime(lastModTime) << endl;
571  }
573  }
574 
575  if (lastModTime > lastFitTime) {
576  // source file modified
577  if (verbose) {
578  cerr << "time of last fit = " + getDateTime(lastFitTime) << endl;
579  cerr << "time of current fit = " + getDateTime(lastModTime) << endl;
580  }
582 
583  // source file length > 0
584  if (getFileSize(sourceFile) > 0) {
585  if (verbose)
586  cerr << "reading record from " + sourceFile << endl;
587 
588  if (readRcd(logFile)) {
589  if (verbose)
590  cerr << "got new record from file" << endl;
591 
592  trueRcd();
593  alive.reset();
594  alive.flip(7);
595  } else {
596  if (verbose)
597  cerr << "problem with new record" << endl;
598  fakeRcd();
599  }
600 
601  lsCount = 0;
602  }
603  } else {
604  // source file not touched
605  problem();
606  }
607  }
608 
609  logFile.close();
610 
611  } catch (exception& e) {
612  cerr << "io exception (end of lumi): " << e.what() << endl;
613  };
614 }
615 
616 /*****************************************************************************/
617 void beginServer() {
618  try {
619  ErrHandler errHandler;
620 
621  cerr << "server started at " + getDateTime() << endl;
622 
623  cerr << "creating publication " + subjectCMS << endl;
624  publicationCMS = dip->createDipPublication(subjectCMS.c_str(), &errHandler);
625  messageCMS = dip->createDipData();
626 
627  cerr << "creating publication " + subjectLHC << endl;
628  publicationLHC = dip->createDipPublication(subjectLHC.c_str(), &errHandler);
629  messageLHC = dip->createDipData();
630 
631  cerr << "creating publication " + subjectPV << endl;
632  publicationPV = dip->createDipPublication(subjectPV.c_str(), &errHandler);
633  messagePV = dip->createDipData();
634 
635  trueRcd(); // starts with all 0
636  publishRcd("UNINITIALIZED", "", true, false);
637  } catch (exception& e) {
638  cerr << "exception (start up): " << e.what() << endl;
639  }
640 
641  quality = qualities[0]; // start with Uncertain
642 }
643 
644 /*****************************************************************************/
645 void endServer() {
646  // destroy publications and data
647  cerr << "destroying publication " + subjectCMS << endl;
648  dip->destroyDipPublication(publicationCMS);
649  delete messageCMS;
650 
651  cerr << "destroying publication " + subjectLHC << endl;
652  dip->destroyDipPublication(publicationLHC);
653  delete messageLHC;
654 
655  cerr << "destroying publication " + subjectPV << endl;
656  dip->destroyDipPublication(publicationPV);
657  delete messagePV;
658 }
659 
660 /*****************************************************************************/
661 int main(int narg, char* args[]) {
662  // options
663  verbose = strcmp(args[1], "true");
664  testing = strcmp(args[2], "true");
665 
666  sourceFile = args[3];
667  sourceFile1 = args[4];
668 
669  //
671  endTime = getDateTime();
672 
673  dip = Dip::create("CmsBeamSpotServer");
674  // dip->setDNSNode("cmsdimns1.cern.ch");
675  dip->setDNSNode("cmsdimns2.cern.ch");
676 
677  cerr << "reading from file (NFS)" << endl;
678 
679  //
680  beginServer();
681 
682  cerr << "entering polling loop" << endl;
683 
684  while (true) {
685  polling();
686  sleep(1);
687  }
688 
689  cerr << "[done]" << endl;
690 
691  //
692  endServer();
693 
694  return 0;
695 }
const int rad2urad
float dydz
float width_x
float err_dydz
vector< string > parse(string line, const string &delimiter)
const string subjectLHC
string getDateTime(time_t t)
bool testing
int runnum
float dxdz
float width_y
bool verbose
const int cm2mm
DipPublication * publicationPV
string quality
void handleException(DipPublication *publication, DipException &e) override
void publishRcd(string qlty, string err, bool pubCMS, bool fitTime)
float rmsPV
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
long getFileSize(string filename)
string tkStatus()
void trueRcd()
const int intLS
DipData * messageCMS
bool readRcd(ifstream &file)
string startTime
long lastFitTime
const string subjectCMS
DipPublication * publicationCMS
void beginServer()
T sqrt(T t)
Definition: SSEVec.h:19
void polling()
const bool publishStatErrors
long lastModTime
float err_x
time_t startTimeStamp
int lsCount
float err_rmsPV
time_t getLastTime(string filename)
const int timeoutLS[2]
float err_z
const string subjectPV
const char * qualities[3]
float Centroid[3]
string sourceFile1
DipData * messageLHC
DipPublication * publicationLHC
float Tilt[2]
time_t endTimeStamp
tuple logFile
Definition: heppy_check.py:40
float err_width_x
DipData * messagePV
DipFactory * dip
float err_dxdz
float sigma_z
const int cm2um
string sourceFile
void fakeRcd()
tuple filename
Definition: lut2db_cfg.py:20
const int secPerLS
float Size[3]
float x
float err_meanPV
string lumiRange
std::bitset< 8 > alive
void problem()
float err_y
float err_sigma_z
tmp
align.sh
Definition: createJobs.py:716
void endServer()
int currentLS
float err_width_y
void CMS2LHCRF_POS(float x, float y, float z)
int events
string endTime
float meanPV