Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-10-10 23:05:27

0001 
0002 /*
0003   Author: Adam Hunt
0004   email:  ahunt@princeton.edu
0005   Date:   2007-08-24
0006 */
0007 
0008 #include "RecoLuminosity/TCPReceiver/interface/TCPReceiver.h"
0009 #include "RecoLuminosity/TCPReceiver/interface/TimeStamp.h"
0010 #include "RecoLuminosity/TCPReceiver/interface/LumiStructures.hh"
0011 
0012 #include <iostream>
0013 
0014 #include <unistd.h>
0015 #include <sys/time.h>
0016 
0017 // srand rand
0018 #include <cstdlib>
0019 
0020 // perror
0021 #include <cstdio>
0022 
0023 // tcp
0024 #include <sys/types.h>
0025 #include <sys/socket.h>
0026 #include <arpa/inet.h>
0027 #include <netinet/in.h>
0028 #include <netdb.h>
0029 
0030 #include <cstring>
0031 #include <cassert>
0032 
0033 namespace HCAL_HLX {
0034 
0035   TCPReceiver::TCPReceiver() {
0036 #ifdef DEBUG
0037     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0038 #endif
0039 
0040     acquireMode = 0;
0041     servPort = 0;
0042     servIP = "127.0.0.1";
0043     Connected = false;
0044 
0045 #ifdef DEBUG
0046     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
0047 #endif
0048   }
0049 
0050   TCPReceiver::TCPReceiver(unsigned short int port, std::string IP, unsigned char mode = 0) {
0051 #ifdef DEBUG
0052     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0053 #endif
0054     acquireMode = mode;
0055     servPort = port;
0056     servIP = IP;
0057     Connected = false;
0058 
0059 #ifdef DEBUG
0060     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0061 #endif
0062   }
0063 
0064   TCPReceiver::~TCPReceiver() { Disconnect(); }
0065 
0066   void SetupFDSets(fd_set &ReadFDs,
0067                    fd_set &WriteFDs,
0068                    fd_set &ExceptFDs,
0069                    int ListeningSocket = -1,
0070                    int connectSocket = -1) {  //std::vector & gConnections) {
0071     FD_ZERO(&ReadFDs);
0072     FD_ZERO(&WriteFDs);
0073     FD_ZERO(&ExceptFDs);
0074 
0075     // Add the listener socket to the read and except FD sets, if there
0076     // is one.
0077     if (ListeningSocket != -1) {
0078       FD_SET(ListeningSocket, &ReadFDs);
0079       FD_SET(ListeningSocket, &ExceptFDs);
0080     }
0081 
0082     if (connectSocket != -1) {
0083       FD_SET(connectSocket, &ReadFDs);
0084       FD_SET(connectSocket, &ExceptFDs);
0085     }
0086   }
0087 
0088   int TCPReceiver::ReceiveLumiSection(HCAL_HLX::LUMI_SECTION &localSection) {
0089 #ifdef DEBUG
0090     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0091 #endif
0092 
0093     int errorCode = 0;
0094 
0095     if (acquireMode == 0) {  // real data
0096       if (Connected == false) {
0097         errorCode = 701;  // not connected
0098       } else {
0099         unsigned int bytesRcvd, bytesToReceive, totalBytesRcvd;
0100         const unsigned int Buffer_Size = 8192;
0101         char *Buffer;
0102         char *BigBuffer;
0103 
0104         // From John's code
0105 
0106         fd_set fdsRead, fdsWrite, fdsExcept;
0107 
0108         //  int outputcode;
0109         //int z = 0, localCount = 0;
0110         time_t tempTime, curTime;
0111         //int ret;
0112 
0113         time(&curTime);
0114 
0115         bytesToReceive = sizeof(localSection);
0116         Buffer = new char[Buffer_Size];
0117         BigBuffer = new char[bytesToReceive];
0118         totalBytesRcvd = 0;
0119 
0120         memset(reinterpret_cast<char *>(&localSection), 0, Buffer_Size);
0121         memset(Buffer, 0, Buffer_Size);
0122         memset(BigBuffer, 0, bytesToReceive);
0123 
0124         usleep(10000);
0125         assert(tcpSocket >= 0);
0126         while ((totalBytesRcvd < bytesToReceive) && (errorCode == 0)) {
0127           SetupFDSets(fdsRead, fdsWrite, fdsExcept, -1, tcpSocket);
0128 
0129           if (select(tcpSocket + 1, &fdsRead, nullptr, &fdsExcept, nullptr) > 0) {
0130             if (FD_ISSET(tcpSocket, &fdsRead)) {
0131               if ((bytesRcvd = recv(tcpSocket, Buffer, Buffer_Size, 0)) <= 0) {
0132                 perror("Recv Error");
0133                 errorCode = 501;
0134               } else {
0135                 if ((totalBytesRcvd + bytesRcvd) <= bytesToReceive) {
0136                   memcpy(&BigBuffer[totalBytesRcvd], Buffer, bytesRcvd);
0137                 } else {
0138                   std::cout << "***** MEM OVER FLOW: Did someone forget to update LumiStructures.hh? *****"
0139                             << std::endl;
0140                   errorCode = 502;
0141                 }
0142                 totalBytesRcvd += bytesRcvd;
0143                 time(&tempTime);
0144               }
0145             }
0146           }
0147         }
0148 
0149         if (errorCode == 0) {
0150           memcpy(reinterpret_cast<char *>(&localSection), BigBuffer, sizeof(localSection));
0151           errorCode = 1;  // success
0152         }
0153         delete[] Buffer;
0154         delete[] BigBuffer;
0155       }
0156     } else if (acquireMode == 1) {  // fill with fake data. Should be unique.
0157       GenerateFakeData(localSection);
0158       errorCode = 1;
0159     } else if (acquireMode == 2) {  // fill with random fake data.
0160       GenerateRandomData(localSection);
0161       errorCode = 1;
0162     } else {
0163       errorCode = 201;
0164     }
0165 
0166 #ifdef DEBUG
0167     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
0168 #endif
0169 
0170     return errorCode;
0171   }
0172 
0173   bool TCPReceiver::IsConnected() {
0174 #ifdef DEBUG
0175     std::cout << "Begin and End  " << __PRETTY_FUNCTION__ << " " << Connected << std::endl;
0176 #endif
0177     return Connected;
0178   }
0179 
0180   int TCPReceiver::SetPort(unsigned short int port) {
0181 #ifdef DEBUG
0182     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0183 #endif
0184 
0185     int errorCode;
0186 
0187     if (port < 1024) {
0188       errorCode = 101;
0189     } else {
0190       servPort = port;
0191       errorCode = 1;
0192     }
0193 
0194 #ifdef DEBUG
0195     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
0196 #endif
0197     return errorCode;
0198   }
0199 
0200   int TCPReceiver::SetMode(unsigned char mode) {
0201 #ifdef DEBUG
0202     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0203 #endif
0204 
0205     int errorCode;
0206 
0207     if (mode > 2) {
0208       errorCode = 201;
0209     } else {
0210       acquireMode = mode;
0211       errorCode = 1;
0212     }
0213 
0214 #ifdef DEBUG
0215     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
0216 #endif
0217     return errorCode;
0218   }
0219 
0220   void TCPReceiver::SetIP(std::string IP) {
0221 #ifdef DEBUG
0222     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0223 #endif
0224     servIP = IP;
0225 #ifdef DEBUG
0226     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
0227 #endif
0228   }
0229 
0230   int TCPReceiver::Connect() {
0231 #ifdef DEBUG
0232     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0233 #endif
0234 
0235     int errorCode;
0236 
0237     if (acquireMode == 0) {
0238       struct hostent *hostInfo = gethostbyname(servIP.c_str());
0239 
0240       if (servPort < 1024) {
0241         errorCode = 101;  // Protected ports
0242       } else {
0243 #ifdef DEBUG
0244         std::cout << "Requesting connection" << std::endl;
0245 #endif
0246         if ((tcpSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
0247           perror("Socket Error");
0248           errorCode = 301;  // Socket failed
0249         } else {
0250           memset(&servAddr, 0, sizeof(servAddr));
0251           servAddr.sin_family = hostInfo->h_addrtype;
0252           memcpy((char *)&servAddr.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
0253           //  servAddr.sin_addr.s_addr = inet_addr(servIP.c_str());
0254           servAddr.sin_port = htons(servPort);
0255 #ifdef DEBUG
0256           std::cout << "Connecting" << std::endl;
0257 #endif
0258           if (connect(tcpSocket, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
0259             perror("Connect Error");
0260             errorCode = 302;  // connect failed
0261             close(tcpSocket);
0262           } else {
0263             Connected = true;
0264             errorCode = 1;  // Successful connection
0265           }
0266         }
0267       }
0268     } else if (acquireMode == 1) {
0269       Connected = true;
0270       errorCode = 1;  // do nothing for fake data
0271     } else if (acquireMode == 2) {
0272       Connected = true;
0273       errorCode = 1;  // do nothing for random data
0274     } else {
0275       errorCode = 201;  // Invalid acquire mode
0276     }
0277 
0278 #ifdef DEBUG
0279     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
0280 #endif
0281     return errorCode;
0282   }
0283 
0284   int TCPReceiver::Disconnect() {
0285 #ifdef DEBUG
0286     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0287 #endif
0288 
0289     int errorCode = 0;
0290 
0291     if (Connected) {
0292       if (acquireMode == 0) {
0293         if (shutdown(tcpSocket, SHUT_RDWR) < 0) {
0294           perror("Shutdown Error");
0295           errorCode = 601;  // Disconnect Failed
0296         } else {
0297           errorCode = 1;  // Successful Disconnect
0298         }
0299       }
0300       Connected = false;
0301     } else {
0302       errorCode = 401;  // Not Connected
0303     }
0304 
0305 #ifdef DEBUG
0306     std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
0307 #endif
0308     return errorCode;
0309   }
0310 
0311   void TCPReceiver::GenerateFakeData(HCAL_HLX::LUMI_SECTION &localSection) {
0312 #ifdef DEBUG
0313     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0314 #endif
0315     int i, j, k;
0316 
0317     localSection.hdr.runNumber = 1;
0318     localSection.hdr.startOrbit = 2;
0319     localSection.hdr.numOrbits = 3;
0320     localSection.hdr.numBunches = 4;
0321     localSection.hdr.numHLXs = 5;
0322     localSection.hdr.bCMSLive = true;
0323     localSection.hdr.sectionNumber = 120;
0324 
0325     timeval tvTemp;
0326     gettimeofday(&tvTemp, nullptr);
0327     localSection.hdr.timestamp = tvTemp.tv_sec;
0328     localSection.hdr.timestamp_micros = tvTemp.tv_usec;
0329 
0330     localSection.lumiSummary.DeadtimeNormalization = 0.7;
0331     localSection.lumiSummary.LHCNormalization = 0.75;
0332     localSection.lumiSummary.OccNormalization[0] = 0.8;
0333     localSection.lumiSummary.OccNormalization[1] = 0.85;
0334     localSection.lumiSummary.ETNormalization = 0.8;
0335     localSection.lumiSummary.InstantLumi = 0.9;
0336     localSection.lumiSummary.InstantLumiErr = 0.10;
0337     localSection.lumiSummary.InstantLumiQlty = 11;
0338     localSection.lumiSummary.InstantETLumi = 0.12;
0339     localSection.lumiSummary.InstantETLumiErr = 0.13;
0340     localSection.lumiSummary.InstantETLumiQlty = 14;
0341     localSection.lumiSummary.InstantOccLumi[0] = 0.15;
0342     localSection.lumiSummary.InstantOccLumiErr[0] = 0.16;
0343     localSection.lumiSummary.InstantOccLumiQlty[0] = 17;
0344     localSection.lumiSummary.lumiNoise[0] = 0.18;
0345     localSection.lumiSummary.InstantOccLumi[1] = 0.19;
0346     localSection.lumiSummary.InstantOccLumiErr[1] = 0.20;
0347     localSection.lumiSummary.InstantOccLumiQlty[1] = 21;
0348     localSection.lumiSummary.lumiNoise[1] = 0.22;
0349 
0350     for (j = 0; j < 3564; j++) {
0351       localSection.lumiDetail.ETBXNormalization[j] = 0.25 * j / 35640.0;
0352       localSection.lumiDetail.OccBXNormalization[0][j] = 0.5 * j / 35640.0;
0353       localSection.lumiDetail.OccBXNormalization[1][j] = 0.75 * j / 35640.0;
0354       localSection.lumiDetail.LHCLumi[j] = 1 * j / 35640.0;
0355       localSection.lumiDetail.ETLumi[j] = 2 * j / 35640.0;
0356       localSection.lumiDetail.ETLumiErr[j] = 3 * j / 35640.0;
0357       localSection.lumiDetail.ETLumiQlty[j] = 4 * j;
0358       localSection.lumiDetail.OccLumi[0][j] = 5 * j / 35640.0;
0359       localSection.lumiDetail.OccLumiErr[0][j] = 6 * j / 35640.0;
0360       localSection.lumiDetail.OccLumiQlty[0][j] = 7 * j;
0361       localSection.lumiDetail.OccLumi[1][j] = 8 * j / 35640.0;
0362       localSection.lumiDetail.OccLumiErr[1][j] = 9 * j / 35640.0;
0363       localSection.lumiDetail.OccLumiQlty[1][j] = 10 * j;
0364     }
0365 
0366     for (i = 0; i < 36; i++) {
0367       localSection.etSum[i].hdr.numNibbles = 7;
0368       localSection.occupancy[i].hdr.numNibbles = 8;
0369       localSection.lhc[i].hdr.numNibbles = 9;
0370 
0371       localSection.etSum[i].hdr.bIsComplete = true;
0372       localSection.occupancy[i].hdr.bIsComplete = true;
0373       localSection.lhc[i].hdr.bIsComplete = true;
0374 
0375       for (j = 0; j < 3564; j++) {
0376         localSection.etSum[i].data[j] = 6 * j + 10 * i;
0377         for (k = 0; k < 6; k++) {
0378           localSection.occupancy[i].data[k][j] = k * j + 11 * i;
0379         }
0380         localSection.lhc[i].data[j] = 7 * j + 12 * i;
0381       }
0382     }
0383 
0384 #ifdef DEBUG
0385     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
0386 #endif
0387   }
0388 
0389   void TCPReceiver::GenerateRandomData(HCAL_HLX::LUMI_SECTION &localSection) {
0390 #ifdef DEBUG
0391     std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
0392 #endif
0393     int i, j, k;
0394 
0395     srand(time(nullptr));
0396     localSection.hdr.runNumber = 55;  //(rand() % 100);
0397     localSection.hdr.startOrbit = (rand() % 100);
0398     localSection.hdr.numOrbits = (rand() % 100);
0399     localSection.hdr.numBunches = (rand() % 100);
0400     localSection.hdr.numHLXs = (rand() % 100);
0401     localSection.hdr.bCMSLive = true;
0402     localSection.hdr.sectionNumber = (rand() % 100);
0403 
0404     localSection.lumiSummary.DeadtimeNormalization = (float)(rand() % 100) / 100;
0405     localSection.lumiSummary.LHCNormalization = (float)(rand() % 100) / 100;
0406     localSection.lumiSummary.OccNormalization[0] = (float)(rand() % 100) / 100;
0407     localSection.lumiSummary.OccNormalization[1] = (float)(rand() % 100) / 100;
0408     localSection.lumiSummary.ETNormalization = (float)(rand() % 100) / 100;
0409     localSection.lumiSummary.InstantLumi = (float)(rand() % 100) / 100;
0410     localSection.lumiSummary.InstantLumiErr = (float)(rand() % 100) / 100;
0411     localSection.lumiSummary.InstantLumiQlty = (rand() % 100);
0412     localSection.lumiSummary.InstantETLumi = (float)(rand() % 100) / 100;
0413     localSection.lumiSummary.InstantETLumiErr = (float)(rand() % 100) / 100;
0414     localSection.lumiSummary.InstantETLumiQlty = (rand() % 100);
0415     localSection.lumiSummary.InstantOccLumi[0] = (float)(rand() % 100) / 100;
0416     localSection.lumiSummary.InstantOccLumiErr[0] = (float)(rand() % 100) / 100;
0417     localSection.lumiSummary.InstantOccLumiQlty[0] = (rand() % 100);
0418     localSection.lumiSummary.lumiNoise[0] = (float)(rand() % 100) / 100;
0419     localSection.lumiSummary.InstantOccLumi[1] = (float)(rand() % 100) / 100;
0420     localSection.lumiSummary.InstantOccLumiErr[1] = (float)(rand() % 100) / 100;
0421     localSection.lumiSummary.InstantOccLumiQlty[1] = (rand() % 100);
0422     localSection.lumiSummary.lumiNoise[1] = (float)(rand() % 100) / 100;
0423 
0424     for (j = 0; j < 3564; j++) {
0425       localSection.lumiDetail.ETBXNormalization[j] = 0.25 * j / 35640.0;
0426       localSection.lumiDetail.OccBXNormalization[0][j] = 0.5 * j / 35640.0;
0427       localSection.lumiDetail.OccBXNormalization[1][j] = 0.75 * j / 35640.0;
0428       localSection.lumiDetail.LHCLumi[j] = (float)(rand() % 100) / 100.0;
0429       localSection.lumiDetail.ETLumi[j] = (float)(rand() % 100) / 100.0;
0430       localSection.lumiDetail.ETLumiErr[j] = (float)(rand() % 100) / 100.0;
0431       localSection.lumiDetail.ETLumiQlty[j] = (rand() % 100);
0432       localSection.lumiDetail.OccLumi[0][j] = (float)(rand() % 100) / 100.0;
0433       localSection.lumiDetail.OccLumiErr[0][j] = (float)(rand() % 100) / 100.0;
0434       localSection.lumiDetail.OccLumiQlty[0][j] = (rand() % 100);
0435       localSection.lumiDetail.OccLumi[1][j] = (float)(rand() % 100) / 100.0;
0436       localSection.lumiDetail.OccLumiErr[1][j] = (float)(rand() % 100) / 100.0;
0437       localSection.lumiDetail.OccLumiQlty[1][j] = (rand() % 100);
0438     }
0439 
0440     for (i = 0; i < 36; i++) {
0441       localSection.etSum[i].hdr.numNibbles = (rand() % 100);
0442       localSection.occupancy[i].hdr.numNibbles = 8 * (rand() % 100);
0443       localSection.lhc[i].hdr.numNibbles = 9 * (rand() % 100);
0444 
0445       localSection.etSum[i].hdr.bIsComplete = true;
0446       localSection.occupancy[i].hdr.bIsComplete = true;
0447       localSection.lhc[i].hdr.bIsComplete = true;
0448 
0449       for (j = 0; j < 3564; j++) {
0450         localSection.etSum[i].data[j] = 6 * (rand() % 3564);
0451         for (k = 0; k < 6; k++) {
0452           localSection.occupancy[i].data[k][j] = k * (rand() % 3564);
0453         }
0454         localSection.lhc[i].data[j] = 7 * (rand() % 3564);
0455       }
0456     }
0457 
0458 #ifdef DEBUG
0459     std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
0460 #endif
0461   }
0462 
0463   bool TCPReceiver::VerifyFakeData(HCAL_HLX::LUMI_SECTION &localSection) {
0464     HCAL_HLX::LUMI_SECTION L;
0465     GenerateFakeData(L);
0466     return !(memcmp(&L, &localSection, sizeof(L)));
0467   }
0468 
0469 }  // namespace HCAL_HLX