Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:06:51

0001 #include "DQM/BeamMonitor/plugins/BeamSpotDipServer.h"
0002 
0003 #include "FWCore/Framework/interface/MakerMacros.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/Framework/interface/LuminosityBlock.h"
0006 #include "FWCore/Framework/interface/Event.h"
0007 #include "FWCore/Framework/interface/ConsumesCollector.h"
0008 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0009 
0010 #include <fstream>
0011 #include <vector>
0012 #include <ctime>
0013 #include <sys/stat.h>
0014 
0015 #include "Dip.h"
0016 #include "DipFactory.h"
0017 #include "DipPublication.h"
0018 #include "DipTimestamp.h"
0019 
0020 using namespace std;
0021 
0022 /*****************************************************************************/
0023 class ErrHandler : public DipPublicationErrorHandler {
0024 public:
0025   virtual ~ErrHandler() = default;
0026 
0027 private:
0028   void handleException(DipPublication* publication, DipException& e) override {
0029     edm::LogError("BeamSpotDipServer") << "exception (create): " << e.what();
0030   }
0031 };
0032 
0033 /*****************************************************************************/
0034 BeamSpotDipServer::BeamSpotDipServer(const edm::ParameterSet& ps) {
0035   //
0036   verbose = ps.getUntrackedParameter<bool>("verbose");
0037   testing = ps.getUntrackedParameter<bool>("testing");
0038 
0039   subjectCMS = ps.getUntrackedParameter<string>("subjectCMS");
0040   subjectLHC = ps.getUntrackedParameter<string>("subjectLHC");
0041   subjectPV = ps.getUntrackedParameter<string>("subjectPV");
0042 
0043   readFromNFS = ps.getUntrackedParameter<bool>("readFromNFS");
0044   // only if readFromNFS = true
0045   sourceFile = ps.getUntrackedParameter<string>("sourceFile");    // beamspot
0046   sourceFile1 = ps.getUntrackedParameter<string>("sourceFile1");  // tk status
0047 
0048   timeoutLS = ps.getUntrackedParameter<vector<int>>("timeoutLS");
0049 
0050   //
0051   bsLegacyToken_ = esConsumes<edm::Transition::EndLuminosityBlock>();
0052 
0053   dcsRecordInputTag_ = ps.getUntrackedParameter<edm::InputTag>("dcsRecordInputTag");
0054   dcsRecordToken_ = consumes<DCSRecord>(dcsRecordInputTag_);
0055 
0056   //
0057   dip = Dip::create("CmsBeamSpotServer");
0058 
0059   // Use both CMS-based DIM DNS server (https://its.cern.ch/jira/browse/CMSOMS-280)
0060   dip->setDNSNode("cmsdimns1.cern.ch,cmsdimns2.cern.ch");
0061 
0062   edm::LogInfo("BeamSpotDipServer") << "reading from " << (readFromNFS ? "file (NFS)" : "database");
0063 }
0064 
0065 /*****************************************************************************/
0066 void BeamSpotDipServer::bookHistograms(DQMStore::IBooker&, const edm::Run&, const edm::EventSetup&) {
0067   // do nothing
0068 }
0069 
0070 /*****************************************************************************/
0071 void BeamSpotDipServer::dqmBeginRun(const edm::Run& r, const edm::EventSetup&) {
0072   edm::LogInfo("BeamSpotDipServer") << "begin run " << r.run();
0073 
0074   try {
0075     ErrHandler errHandler;
0076 
0077     edm::LogInfo("BeamSpotDipServer") << "server started at " + getDateTime();
0078 
0079     edm::LogInfo("BeamSpotDipServer") << "creating publication " + subjectCMS;
0080     publicationCMS = dip->createDipPublication(subjectCMS.c_str(), &errHandler);
0081     messageCMS = dip->createDipData();
0082 
0083     edm::LogInfo("BeamSpotDipServer") << "creating publication " + subjectLHC;
0084     publicationLHC = dip->createDipPublication(subjectLHC.c_str(), &errHandler);
0085     messageLHC = dip->createDipData();
0086 
0087     edm::LogInfo("BeamSpotDipServer") << "creating publication " + subjectPV;
0088     publicationPV = dip->createDipPublication(subjectPV.c_str(), &errHandler);
0089     messagePV = dip->createDipData();
0090 
0091     trueRcd();  // starts with all 0
0092     publishRcd("UNINITIALIZED", "", true, false);
0093   } catch (exception& e) {
0094     edm::LogError("BeamSpotDipServer") << "exception (start up): " << e.what();
0095   }
0096 
0097   quality = qualities[0];  // start with Uncertain
0098 }
0099 
0100 /*****************************************************************************/
0101 void BeamSpotDipServer::dqmBeginLuminosityBlock(const edm::LuminosityBlock&, const edm::EventSetup&) {
0102   // do nothing
0103 }
0104 
0105 /*****************************************************************************/
0106 void BeamSpotDipServer::analyze(const edm::Event& iEvent, const edm::EventSetup&) {
0107   if (!readFromNFS) {
0108     // get runnumber
0109     runnum = iEvent.run();
0110 
0111     // get tracker status if in a new lumisection
0112     int nthlumi = iEvent.luminosityBlock();
0113 
0114     if (nthlumi > lastlumi) {  // check every LS
0115       lastlumi = nthlumi;
0116 
0117       edm::Handle<DCSRecord> dcsRecord;
0118       iEvent.getByToken(dcsRecordToken_, dcsRecord);
0119 
0120       wholeTrackerOn =
0121           (*dcsRecord).highVoltageReady(DCSRecord::BPIX) && (*dcsRecord).highVoltageReady(DCSRecord::FPIX) &&
0122           (*dcsRecord).highVoltageReady(DCSRecord::TIBTID) && (*dcsRecord).highVoltageReady(DCSRecord::TOB) &&
0123           (*dcsRecord).highVoltageReady(DCSRecord::TECp) && (*dcsRecord).highVoltageReady(DCSRecord::TECm);
0124 
0125       if (verbose)
0126         edm::LogInfo("BeamSpotDipServer") << "whole tracker on? " << (wholeTrackerOn ? "yes" : "no");
0127     }
0128   }
0129 }
0130 
0131 /*****************************************************************************/
0132 void BeamSpotDipServer::dqmEndLuminosityBlock(const edm::LuminosityBlock& lumiSeg, const edm::EventSetup& iSetup) {
0133   edm::LogInfo("BeamSpotDipServer") << "--------------------- end of LS " << lumiSeg.luminosityBlock();
0134 
0135   try {
0136     if (readFromNFS) {
0137       ifstream logFile(sourceFile);
0138 
0139       if (!logFile.good()) {
0140         edm::LogWarning("BeamSpotDipServer") << "Source File: " + sourceFile + " doesn't exist!";
0141         problem();
0142       } else {
0143         lastModTime = getLastTime(sourceFile);
0144 
0145         if (lastFitTime == 0)
0146           lastFitTime = lastModTime;
0147 
0148         if (getFileSize(sourceFile) == 0) {
0149           // source file has zero length
0150           if (lastModTime > lastFitTime) {
0151             string tmp = tkStatus();
0152             edm::LogInfo("BeamSpotDipServer") << "New run starts. Run number: " << runnum;
0153             if (verbose)
0154               edm::LogInfo("BeamSpotDipServer") << "Initial lastModTime = " + getDateTime(lastModTime);
0155           }
0156           lastFitTime = lastModTime;
0157         }
0158 
0159         if (lastModTime > lastFitTime) {
0160           // source file modified
0161           if (verbose) {
0162             edm::LogInfo("BeamSpotDipServer") << "time of last fit    = " + getDateTime(lastFitTime);
0163             edm::LogInfo("BeamSpotDipServer") << "time of current fit = " + getDateTime(lastModTime);
0164           }
0165           lastFitTime = lastModTime;
0166 
0167           // source file length > 0
0168           if (getFileSize(sourceFile) > 0) {
0169             if (verbose)
0170               edm::LogInfo("BeamSpotDipServer") << "reading record from " + sourceFile;
0171 
0172             if (readRcd(logFile)) {
0173               if (verbose)
0174                 edm::LogInfo("BeamSpotDipServer") << "got new record from file";
0175 
0176               trueRcd();
0177               alive.reset();
0178               alive.flip(7);
0179             } else {
0180               if (verbose)
0181                 edm::LogInfo("BeamSpotDipServer") << "problem with new record";
0182               fakeRcd();
0183             }
0184 
0185             lsCount = 0;
0186           }
0187         } else {
0188           // source file not touched
0189           problem();
0190         }
0191       }
0192 
0193       logFile.close();
0194     } else {
0195       edm::ESHandle<BeamSpotOnlineObjects> bsLegacyHandle = iSetup.getHandle(bsLegacyToken_);
0196       auto const& bs = *bsLegacyHandle;
0197 
0198       // from database
0199       if (readRcd(bs)) {
0200         if (verbose)
0201           edm::LogInfo("BeamSpotDipServer") << "got new record from database";
0202         trueRcd();
0203         alive.reset();
0204         alive.flip(7);
0205       } else {
0206         if (verbose)
0207           edm::LogInfo("BeamSpotDipServer") << "problem with new record";
0208         fakeRcd();
0209       }
0210 
0211       lsCount = 0;
0212     }
0213 
0214     // quality of the publish results
0215     if (testing)
0216       publishRcd(qualities[0], "Testing", true, true);  // Uncertain
0217     else if (quality == qualities[1])                   // Bad
0218       publishRcd(quality, "No fit or fit fails", true, true);
0219     else
0220       publishRcd(quality, "", true, true);  // Good
0221   } catch (exception& e) {
0222     edm::LogWarning("BeamSpotDipServer") << "io exception (end of lumi): " << e.what();
0223   };
0224 }
0225 
0226 /*****************************************************************************/
0227 void BeamSpotDipServer::dqmEndRun(const edm::Run&, const edm::EventSetup&) {
0228   // destroy publications and data
0229   edm::LogInfo("BeamSpotDipServer") << "destroying publication " + subjectCMS;
0230   dip->destroyDipPublication(publicationCMS);
0231   delete messageCMS;
0232 
0233   edm::LogInfo("BeamSpotDipServer") << "destroying publication " + subjectLHC;
0234   dip->destroyDipPublication(publicationLHC);
0235   delete messageLHC;
0236 
0237   edm::LogInfo("BeamSpotDipServer") << "destroying publication " + subjectPV;
0238   dip->destroyDipPublication(publicationPV);
0239   delete messagePV;
0240 }
0241 
0242 /*****************************************************************************/
0243 long BeamSpotDipServer::getFileSize(string filename) {
0244   struct stat stat_buf;
0245   int rc = stat(filename.c_str(), &stat_buf);
0246   return (rc == 0 ? stat_buf.st_size : -1);
0247 }
0248 
0249 /*****************************************************************************/
0250 time_t BeamSpotDipServer::getLastTime(string filename) {
0251   struct stat stat_buf;
0252   int rc = stat(filename.c_str(), &stat_buf);
0253   return (rc == 0 ? stat_buf.st_mtime : -1);
0254 }
0255 
0256 /*****************************************************************************/
0257 vector<string> BeamSpotDipServer::parse(string line, const string& delimiter) {
0258   vector<string> list;
0259 
0260   size_t pos = 0;
0261   while ((pos = line.find(delimiter)) != string::npos) {
0262     string token = line.substr(0, pos);
0263 
0264     list.push_back(token);
0265 
0266     line.erase(0, pos + delimiter.length());
0267   }
0268 
0269   list.push_back(line);  // remainder
0270 
0271   return list;
0272 }
0273 
0274 /*****************************************************************************/
0275 string BeamSpotDipServer::tkStatus() {
0276   string outstr;
0277 
0278   if (readFromNFS) {  // get from file on /nfs
0279     ifstream logfile(sourceFile1);
0280 
0281     if (!logfile.good() || getFileSize(sourceFile1) == 0) {
0282       // file does not exist or has zero size
0283       outstr = "No CMS Tracker status available. No DAQ/DQM.";
0284     } else {
0285       int nthLnInRcd = 0;
0286       string record;
0287 
0288       try {
0289         string record;
0290 
0291         while (getline(logfile, record)) {
0292           nthLnInRcd++;
0293           vector<string> tmp = parse(record, " ");
0294 
0295           switch (nthLnInRcd) {
0296             case 7:
0297               if (tmp[1].find("Yes") == string::npos)
0298                 outstr = "CMS Tracker OFF.";
0299               else
0300                 outstr = "CMS not taking data or no beam.";
0301               break;
0302             case 8:
0303               runnum = stoi(tmp[1]);
0304               break;
0305             default:
0306               break;
0307           }
0308         }
0309       } catch (exception& e) {
0310         edm::LogWarning("BeamSpotDipServer") << "exception (tkStatus): " << e.what();
0311       }
0312     }
0313 
0314     logfile.close();
0315   } else {
0316     // get from DCS
0317     if (wholeTrackerOn)
0318       outstr = "CMS not taking data or no beam.";
0319     else
0320       outstr = "CMS Tracker OFF.";
0321   }
0322 
0323   return outstr;
0324 }
0325 
0326 /*****************************************************************************/
0327 void BeamSpotDipServer::problem() {
0328   if (verbose)
0329     edm::LogInfo("BeamSpotDipServer") << "no update | alive = " << alive;
0330 
0331   lsCount++;
0332 
0333   if ((lsCount % timeoutLS[0] == 0) && (lsCount % timeoutLS[1] != 0))  // first time out
0334   {
0335     if (!alive.test(1))
0336       alive.flip(1);
0337     if (!alive.test(2)) {
0338       if (!alive.test(7))
0339         fakeRcd();
0340       else
0341         trueRcd();
0342 
0343       stringstream warnMsg;
0344       warnMsg << "No new data for " << lsCount << " LS";
0345       publishRcd("Uncertain", warnMsg.str(), false, false);
0346     } else {
0347       fakeRcd();
0348 
0349       stringstream warnMsg;
0350       warnMsg << "No new data for " << lsCount << " LS: " << tkStatus();
0351       publishRcd("Bad", warnMsg.str(), false, false);
0352     }
0353   } else if (lsCount % timeoutLS[1] == 0)  // second time out
0354   {
0355     if (!alive.test(2))
0356       alive.flip(2);
0357     fakeRcd();
0358 
0359     stringstream warnMsg;
0360     warnMsg << "No new data for " << lsCount << " LS: " << tkStatus();
0361     publishRcd("Bad", warnMsg.str(), false, false);
0362   }
0363 }
0364 
0365 /*****************************************************************************/
0366 bool BeamSpotDipServer::readRcd(const BeamSpotOnlineObjects& bs)
0367 // read from database
0368 {
0369   runnum = bs.lastAnalyzedRun();
0370 
0371   // get from BeamSpotOnlineObject
0372 
0373   try {
0374     startTime = bs.startTime();
0375     startTimeStamp = bs.startTimeStamp();
0376     endTime = bs.endTime();
0377     endTimeStamp = bs.endTimeStamp();
0378   } catch (exception& e) {
0379     edm::LogWarning("BeamSpotDipServer") << "time variables are not available (readRcd): " << e.what();
0380 
0381     startTime = bs.creationTime();
0382     startTimeStamp = bs.creationTime();
0383     endTime = bs.creationTime();
0384     endTimeStamp = bs.creationTime();
0385   }
0386 
0387   try {
0388     lumiRange = bs.lumiRange();
0389   } catch (exception& e) {
0390     edm::LogWarning("BeamSpotDipServer") << "lumirange variable not avaialble (readRcd): " << e.what();
0391 
0392     lumiRange = to_string(bs.lastAnalyzedLumi());
0393   }
0394 
0395   currentLS = bs.lastAnalyzedLumi();
0396 
0397   type = bs.beamType();
0398 
0399   if (verbose)
0400     edm::LogInfo("BeamSpotDipServer") << "run: " << runnum << ", LS: " << currentLS << ", time: " << startTime << " "
0401                                       << startTimeStamp << ", type: " << type;
0402 
0403   if (testing)
0404     quality = qualities[0];  // Uncertain
0405   else if (type >= 2)
0406     quality = qualities[2];  // Good
0407   else
0408     quality = qualities[1];  // Bad
0409 
0410   x = bs.x();
0411   y = bs.y();
0412   z = bs.z();
0413 
0414   sigma_z = bs.sigmaZ();
0415   dxdz = bs.dxdz();
0416   dydz = bs.dydz();
0417   width_x = bs.beamWidthX();
0418   width_y = bs.beamWidthX();
0419 
0420   err_x = bs.xError();
0421   err_y = bs.yError();
0422   err_z = bs.zError();
0423   err_sigma_z = bs.sigmaZError();
0424   err_dxdz = bs.dxdzError();
0425   err_dydz = bs.dydzError();
0426   err_width_x = bs.beamWidthXError();
0427   err_width_y = bs.beamWidthYError();
0428 
0429   try {
0430     events = bs.usedEvents();
0431     meanPV = bs.meanPV();
0432     err_meanPV = bs.meanErrorPV();
0433     rmsPV = bs.rmsPV();
0434     err_rmsPV = bs.rmsErrorPV();
0435     maxPV = bs.maxPVs();
0436   } catch (exception& e) {
0437     edm::LogWarning("BeamSpotDipServer") << "PV variables are not available (readRcd): " << e.what();
0438 
0439     events = 0.;
0440     meanPV = 0.;
0441     err_meanPV = 0.;
0442     rmsPV = 0.;
0443     err_rmsPV = 0.;
0444     maxPV = 0.;
0445   }
0446 
0447   nPV = bs.numPVs();
0448 
0449   if (verbose)
0450     edm::LogInfo("BeamSpotDipServer") << "pos: (" << x << "," << y << "," << z << ")"
0451                                       << " nPV: " << nPV;
0452 
0453   return true;
0454 }
0455 
0456 /*****************************************************************************/
0457 bool BeamSpotDipServer::readRcd(ifstream& file)  // readFromNFS
0458 {
0459   int nthLnInRcd = 0;
0460   bool rcdQlty = false;
0461 
0462   try {
0463     string record;
0464     while (getline(file, record)) {
0465       nthLnInRcd++;
0466 
0467       vector<string> tmp = parse(record, " ");
0468 
0469       switch (nthLnInRcd) {
0470         case 1:
0471           if (record.rfind("Run", 0) != 0) {
0472             edm::LogError("BeamSpotDipServer") << "Reading of results text file interrupted. " + getDateTime();
0473             return false;
0474           }
0475           runnum = stoi(tmp[1]);
0476           break;
0477         case 2:
0478           startTime = tmp[1] + " " + tmp[2] + " " + tmp[3];
0479           startTimeStamp = stol(tmp[4]);
0480           break;
0481         case 3:
0482           endTime = tmp[1] + " " + tmp[2] + " " + tmp[3];
0483           endTimeStamp = stol(tmp[4]);
0484           break;
0485         case 4:
0486           lumiRange = record.substr(10);
0487           if (verbose)
0488             edm::LogInfo("BeamSpotDipServer") << "lumisection range: " + lumiRange;
0489           currentLS = stoi(tmp[3]);
0490           break;
0491         case 5:
0492           type = stoi(tmp[1]);
0493           if (testing)
0494             quality = qualities[0];  // Uncertain
0495           else if (type >= 2)
0496             quality = qualities[2];  // Good
0497           else
0498             quality = qualities[1];  // Bad
0499           break;
0500 
0501         case 6:
0502           x = stof(tmp[1]);
0503           break;
0504         case 7:
0505           y = stof(tmp[1]);
0506           break;
0507         case 8:
0508           z = stof(tmp[1]);
0509           break;
0510 
0511         case 9:
0512           sigma_z = stof(tmp[1]);
0513           break;
0514         case 10:
0515           dxdz = stof(tmp[1]);
0516           break;
0517         case 11:
0518           dydz = stof(tmp[1]);
0519           break;
0520         case 12:
0521           width_x = stof(tmp[1]);
0522           break;
0523         case 13:
0524           width_y = stof(tmp[1]);
0525           break;
0526 
0527         case 14:
0528           err_x = sqrt(stof(tmp[1]));
0529           break;
0530         case 15:
0531           err_y = sqrt(stof(tmp[2]));
0532           break;
0533         case 16:
0534           err_z = sqrt(stof(tmp[3]));
0535           break;
0536         case 17:
0537           err_sigma_z = sqrt(stof(tmp[4]));
0538           break;
0539         case 18:
0540           err_dxdz = sqrt(stof(tmp[5]));
0541           break;
0542         case 19:
0543           err_dydz = sqrt(stof(tmp[6]));
0544           break;
0545         case 20:
0546           err_width_x = sqrt(stof(tmp[7]));
0547           err_width_y = err_width_x;
0548           break;
0549         case 21:
0550           break;
0551         case 22:
0552           break;
0553         case 23:
0554           break;
0555         case 24:
0556           events = stoi(tmp[1]);
0557           break;
0558 
0559         case 25:
0560           meanPV = stof(tmp[1]);
0561           break;
0562         case 26:
0563           err_meanPV = stof(tmp[1]);
0564           break;
0565         case 27:
0566           rmsPV = stof(tmp[1]);
0567           break;
0568         case 28:
0569           err_rmsPV = stof(tmp[1]);
0570           break;
0571         case 29:
0572           maxPV = stoi(tmp[1]);
0573           break;
0574         case 30:
0575           nPV = stoi(tmp[1]);
0576           rcdQlty = true;
0577           break;
0578 
0579         default:
0580           break;
0581       }
0582     }
0583 
0584     file.close();
0585   } catch (exception& e) {
0586     edm::LogWarning("BeamSpotDipServer") << "io exception (readRcd): " << e.what();
0587   }
0588 
0589   return rcdQlty;
0590 }
0591 
0592 /*****************************************************************************/
0593 void BeamSpotDipServer::CMS2LHCRF_POS(float x, float y, float z) {
0594   if (x != 0) {  // Rotation + Translation + Inversion + Scaling
0595     double tmpx = x;
0596     // x*rotY[0]*rotZ[0] + y*rotY[0]*rotZ[1] - z*rotY[1] + trans[0];
0597     Centroid[0] = tmpx;
0598     Centroid[0] *= -1.0 * cm2um;
0599   } else
0600     Centroid[0] = x;
0601 
0602   if (y != 0) {  // Rotation + Translation + Scaling
0603     double tmpy = y;
0604     // x*(rotX[1]*rotY[1]*rotZ[0] - rotX[0]*rotZ[1]) +
0605     // y*(rotX[0]*rotZ[0] + rotX[1]*rotY[1]*rotZ[1]) +
0606     // z*rotX[1]*rotY[0] + trans[1];
0607     Centroid[1] = tmpy;
0608     Centroid[1] *= cm2um;
0609   } else
0610     Centroid[1] = y;
0611 
0612   if (z != 0) {  // Rotation + Translation + Inversion + Scaling
0613     double tmpz = z;
0614     // x*(rotX[0]*rotY[1]*rotZ[0] + rotX[1]*rotZ[1]) +
0615     // y*(rotX[0]*rotY[1]*rotZ[1] - rotX[1]*rotZ[0]) +
0616     // z*rotX[0]*rotY[0] + trans[2];
0617     Centroid[2] = tmpz;
0618     Centroid[2] *= -1.0 * cm2mm;
0619   } else
0620     Centroid[2] = z;
0621 }
0622 
0623 /*****************************************************************************/
0624 void BeamSpotDipServer::trueRcd() {
0625   try {
0626     // CMS to LHC RF
0627     CMS2LHCRF_POS(x, y, z);
0628 
0629     Tilt[0] = dxdz * rad2urad;
0630     Tilt[1] = (dydz != 0 ? (dydz * -1 * rad2urad) : 0);
0631 
0632     Size[0] = width_x * cm2um;
0633     Size[1] = width_y * cm2um;
0634     Size[2] = sigma_z * cm2mm;
0635 
0636     // CMS
0637     messageCMS->insert(runnum, "runnum");
0638     messageCMS->insert(startTime, "startTime");
0639     messageCMS->insert(endTime, "endTime");
0640     messageCMS->insert(startTimeStamp, "startTimeStamp");
0641     messageCMS->insert(endTimeStamp, "endTimeStamp");
0642     messageCMS->insert(lumiRange, "lumiRange");
0643     messageCMS->insert(quality, "quality");
0644     messageCMS->insert(type, "type");  // Unknown=-1, Fake=0, Tracker=2(Good)
0645     messageCMS->insert(x, "x");
0646     messageCMS->insert(y, "y");
0647     messageCMS->insert(z, "z");
0648     messageCMS->insert(dxdz, "dxdz");
0649     messageCMS->insert(dydz, "dydz");
0650     messageCMS->insert(width_x, "width_x");
0651     messageCMS->insert(width_y, "width_y");
0652     messageCMS->insert(sigma_z, "sigma_z");
0653 
0654     if (publishStatErrors) {
0655       messageCMS->insert(err_x, "err_x");
0656       messageCMS->insert(err_y, "err_y");
0657       messageCMS->insert(err_z, "err_z");
0658       messageCMS->insert(err_dxdz, "err_dxdz");
0659       messageCMS->insert(err_dydz, "err_dydz");
0660       messageCMS->insert(err_width_x, "err_width_x");
0661       messageCMS->insert(err_width_y, "err_width_y");
0662       messageCMS->insert(err_sigma_z, "err_sigma_z");
0663     }
0664 
0665     // LHC
0666     messageLHC->insert(Size, 3, "Size");
0667     messageLHC->insert(Centroid, 3, "Centroid");
0668     messageLHC->insert(Tilt, 2, "Tilt");
0669 
0670     // PV
0671     messagePV->insert(runnum, "runnum");
0672     messagePV->insert(startTime, "startTime");
0673     messagePV->insert(endTime, "endTime");
0674     messagePV->insert(startTimeStamp, "startTimeStamp");
0675     messagePV->insert(endTimeStamp, "endTimeStamp");
0676     messagePV->insert(lumiRange, "lumiRange");
0677     messagePV->insert(events, "events");
0678     messagePV->insert(meanPV, "meanPV");
0679     messagePV->insert(err_meanPV, "err_meanPV");
0680     messagePV->insert(rmsPV, "rmsPV");
0681     messagePV->insert(err_rmsPV, "err_rmsPV");
0682     messagePV->insert(maxPV, "maxPV");
0683     messagePV->insert(nPV, "nPV");
0684   } catch (exception& e) {
0685     edm::LogWarning("BeamSpotDipServer") << "exception (trueRcd): " << e.what();
0686   }
0687 }
0688 
0689 /*****************************************************************************/
0690 void BeamSpotDipServer::fakeRcd() {
0691   try {
0692     Centroid[0] = 0;
0693     Centroid[1] = 0;
0694     Centroid[2] = 0;
0695 
0696     Size[0] = 0;
0697     Size[1] = 0;
0698     Size[2] = 0;
0699 
0700     Tilt[0] = 0;
0701     Tilt[1] = 0;
0702 
0703     messageLHC->insert(Size, 3, "Size");
0704     messageLHC->insert(Centroid, 3, "Centroid");
0705     messageLHC->insert(Tilt, 2, "Tilt");
0706   } catch (exception& e) {
0707     edm::LogWarning("BeamSpotDipServer") << "exception (fakeRcd): " << e.what();
0708   }
0709 }
0710 
0711 /*****************************************************************************/
0712 void BeamSpotDipServer::publishRcd(string qlty, string err, bool pubCMS, bool fitTime) {
0713   try {
0714     bool updateCMS = pubCMS && (currentLS % intLS == 0);
0715 
0716     if (verbose) {
0717       edm::LogInfo("BeamSpotDipServer") << "sending (" << qlty << " | " << err << ")";
0718 
0719       if (alive.test(7)) {
0720         if (updateCMS)
0721           edm::LogInfo("BeamSpotDipServer") << " to CCC and CMS";
0722         else if (!alive.test(1) && !alive.test(2))
0723           edm::LogInfo("BeamSpotDipServer") << " to CCC only";
0724       }
0725     }
0726 
0727     DipTimestamp zeit;
0728     if (fitTime) {
0729       long epoch;
0730       epoch = endTimeStamp * 1000;  // convert to ms
0731       zeit = DipTimestamp(epoch);
0732     } else
0733       zeit = DipTimestamp();
0734 
0735     // send
0736     if (updateCMS)
0737       publicationCMS->send(*messageCMS, zeit);
0738 
0739     publicationLHC->send(*messageLHC, zeit);
0740     publicationPV->send(*messagePV, zeit);
0741 
0742     // set qualities
0743     if (qlty == qualities[0]) {  // Uncertain
0744       if (updateCMS)
0745         publicationCMS->setQualityUncertain(err.c_str());
0746 
0747       publicationLHC->setQualityUncertain(err.c_str());
0748     } else if (qlty == qualities[1]) {  // Bad
0749       if (updateCMS)
0750         publicationCMS->setQualityBad(err.c_str());
0751 
0752       publicationLHC->setQualityBad(err.c_str());
0753     } else if (qlty == "UNINITIALIZED") {
0754       if (updateCMS)
0755         publicationCMS->setQualityBad("UNINITIALIZED");
0756 
0757       publicationLHC->setQualityBad("UNINITIALIZED");
0758     }
0759   } catch (exception& e) {
0760     edm::LogWarning("BeamSpotDipServer") << "exception (publishRcd): " << e.what();
0761   }
0762 }
0763 
0764 /*****************************************************************************/
0765 string BeamSpotDipServer::getDateTime(time_t t) {
0766   char mbstr[100];
0767   strftime(mbstr, sizeof(mbstr), "%Y.%m.%d %H:%M:%S %z", std::localtime(&t));
0768 
0769   return mbstr;
0770 }
0771 
0772 //
0773 string BeamSpotDipServer::getDateTime() {
0774   time_t t = time(nullptr);
0775 
0776   return getDateTime(t);
0777 }
0778 
0779 DEFINE_FWK_MODULE(BeamSpotDipServer);