Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:20:01

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