CMS 3D CMS Logo

TCPReceiver.cc
Go to the documentation of this file.
1 
2 /*
3  Author: Adam Hunt
4  email: ahunt@princeton.edu
5  Date: 2007-08-24
6 */
7 
10 #include "RecoLuminosity/TCPReceiver/interface/LumiStructures.hh"
11 
12 #include <iostream>
13 
14 #include <unistd.h>
15 #include <sys/time.h>
16 
17 // srand rand
18 #include <cstdlib>
19 
20 // perror
21 #include <cstdio>
22 
23 // tcp
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 
30 #include <cstring>
31 
32 namespace HCAL_HLX {
33 
35 #ifdef DEBUG
36  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
37 #endif
38 
39  acquireMode = 0;
40  servPort = 0;
41  servIP = "127.0.0.1";
42  Connected = false;
43 
44 #ifdef DEBUG
45  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
46 #endif
47  }
48 
49  TCPReceiver::TCPReceiver(unsigned short int port, std::string IP, unsigned char mode = 0) {
50 #ifdef DEBUG
51  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
52 #endif
53  acquireMode = mode;
54  servPort = port;
55  servIP = IP;
56  Connected = false;
57 
58 #ifdef DEBUG
59  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
60 #endif
61  }
62 
64 
65  void SetupFDSets(fd_set &ReadFDs,
66  fd_set &WriteFDs,
67  fd_set &ExceptFDs,
68  int ListeningSocket = -1,
69  int connectSocket = -1) { //std::vector & gConnections) {
70  FD_ZERO(&ReadFDs);
71  FD_ZERO(&WriteFDs);
72  FD_ZERO(&ExceptFDs);
73 
74  // Add the listener socket to the read and except FD sets, if there
75  // is one.
76  if (ListeningSocket != -1) {
77  FD_SET(ListeningSocket, &ReadFDs);
78  FD_SET(ListeningSocket, &ExceptFDs);
79  }
80 
81  if (connectSocket != -1) {
82  FD_SET(connectSocket, &ReadFDs);
83  FD_SET(connectSocket, &ExceptFDs);
84  }
85  }
86 
88 #ifdef DEBUG
89  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
90 #endif
91 
92  int errorCode = 0;
93 
94  if (acquireMode == 0) { // real data
95  if (Connected == false) {
96  errorCode = 701; // not connected
97  } else {
98  unsigned int bytesRcvd, bytesToReceive, totalBytesRcvd;
99  const unsigned int Buffer_Size = 8192;
100  char *Buffer;
101  char *BigBuffer;
102 
103  // From John's code
104 
105  fd_set fdsRead, fdsWrite, fdsExcept;
106 
107  // int outputcode;
108  //int z = 0, localCount = 0;
109  time_t tempTime, curTime;
110  //int ret;
111 
112  time(&curTime);
113 
114  bytesToReceive = sizeof(localSection);
115  Buffer = new char[Buffer_Size];
116  BigBuffer = new char[bytesToReceive];
117  totalBytesRcvd = 0;
118 
119  memset(reinterpret_cast<char *>(&localSection), 0, Buffer_Size);
120  memset(Buffer, 0, Buffer_Size);
121  memset(BigBuffer, 0, bytesToReceive);
122 
123  usleep(10000);
124 
125  while ((totalBytesRcvd < bytesToReceive) && (errorCode == 0)) {
126  SetupFDSets(fdsRead, fdsWrite, fdsExcept, -1, tcpSocket);
127 
128  if (select(tcpSocket + 1, &fdsRead, nullptr, &fdsExcept, nullptr) > 0) {
129  if (FD_ISSET(tcpSocket, &fdsRead)) {
130  if ((bytesRcvd = recv(tcpSocket, Buffer, Buffer_Size, 0)) <= 0) {
131  perror("Recv Error");
132  errorCode = 501;
133  } else {
134  if ((totalBytesRcvd + bytesRcvd) <= bytesToReceive) {
135  memcpy(&BigBuffer[totalBytesRcvd], Buffer, bytesRcvd);
136  } else {
137  std::cout << "***** MEM OVER FLOW: Did someone forget to update LumiStructures.hh? *****"
138  << std::endl;
139  errorCode = 502;
140  }
141  totalBytesRcvd += bytesRcvd;
142  time(&tempTime);
143  }
144  }
145  }
146  }
147 
148  if (errorCode == 0) {
149  memcpy(reinterpret_cast<char *>(&localSection), BigBuffer, sizeof(localSection));
150  errorCode = 1; // success
151  }
152  delete[] Buffer;
153  delete[] BigBuffer;
154  }
155  } else if (acquireMode == 1) { // fill with fake data. Should be unique.
156  GenerateFakeData(localSection);
157  errorCode = 1;
158  } else if (acquireMode == 2) { // fill with random fake data.
159  GenerateRandomData(localSection);
160  errorCode = 1;
161  } else {
162  errorCode = 201;
163  }
164 
165 #ifdef DEBUG
166  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
167 #endif
168 
169  return errorCode;
170  }
171 
173 #ifdef DEBUG
174  std::cout << "Begin and End " << __PRETTY_FUNCTION__ << " " << Connected << std::endl;
175 #endif
176  return Connected;
177  }
178 
179  int TCPReceiver::SetPort(unsigned short int port) {
180 #ifdef DEBUG
181  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
182 #endif
183 
184  int errorCode;
185 
186  if (port < 1024) {
187  errorCode = 101;
188  } else {
189  servPort = port;
190  errorCode = 1;
191  }
192 
193 #ifdef DEBUG
194  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
195 #endif
196  return errorCode;
197  }
198 
199  int TCPReceiver::SetMode(unsigned char mode) {
200 #ifdef DEBUG
201  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
202 #endif
203 
204  int errorCode;
205 
206  if (mode > 2) {
207  errorCode = 201;
208  } else {
209  acquireMode = mode;
210  errorCode = 1;
211  }
212 
213 #ifdef DEBUG
214  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
215 #endif
216  return errorCode;
217  }
218 
220 #ifdef DEBUG
221  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
222 #endif
223  servIP = IP;
224 #ifdef DEBUG
225  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
226 #endif
227  }
228 
230 #ifdef DEBUG
231  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
232 #endif
233 
234  int errorCode;
235 
236  if (acquireMode == 0) {
237  struct hostent *hostInfo = gethostbyname(servIP.c_str());
238 
239  if (servPort < 1024) {
240  errorCode = 101; // Protected ports
241  } else {
242 #ifdef DEBUG
243  std::cout << "Requesting connection" << std::endl;
244 #endif
245  if ((tcpSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
246  perror("Socket Error");
247  errorCode = 301; // Socket failed
248  } else {
249  memset(&servAddr, 0, sizeof(servAddr));
250  servAddr.sin_family = hostInfo->h_addrtype;
251  memcpy((char *)&servAddr.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
252  // servAddr.sin_addr.s_addr = inet_addr(servIP.c_str());
253  servAddr.sin_port = htons(servPort);
254 #ifdef DEBUG
255  std::cout << "Connecting" << std::endl;
256 #endif
257  if (connect(tcpSocket, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0) {
258  perror("Connect Error");
259  errorCode = 302; // connect failed
260  close(tcpSocket);
261  } else {
262  Connected = true;
263  errorCode = 1; // Successful connection
264  }
265  }
266  }
267  } else if (acquireMode == 1) {
268  Connected = true;
269  errorCode = 1; // do nothing for fake data
270  } else if (acquireMode == 2) {
271  Connected = true;
272  errorCode = 1; // do nothing for random data
273  } else {
274  errorCode = 201; // Invalid acquire mode
275  }
276 
277 #ifdef DEBUG
278  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
279 #endif
280  return errorCode;
281  }
282 
284 #ifdef DEBUG
285  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
286 #endif
287 
288  int errorCode = 0;
289 
290  if (Connected) {
291  if (acquireMode == 0) {
292  if (shutdown(tcpSocket, SHUT_RDWR) < 0) {
293  perror("Shutdown Error");
294  errorCode = 601; // Disconnect Failed
295  } else {
296  errorCode = 1; // Successful Disconnect
297  }
298  }
299  Connected = false;
300  } else {
301  errorCode = 401; // Not Connected
302  }
303 
304 #ifdef DEBUG
305  std::cout << "End " << __PRETTY_FUNCTION__ << " " << errorCode << std::endl;
306 #endif
307  return errorCode;
308  }
309 
311 #ifdef DEBUG
312  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
313 #endif
314  int i, j, k;
315 
316  localSection.hdr.runNumber = 1;
317  localSection.hdr.startOrbit = 2;
318  localSection.hdr.numOrbits = 3;
319  localSection.hdr.numBunches = 4;
320  localSection.hdr.numHLXs = 5;
321  localSection.hdr.bCMSLive = true;
322  localSection.hdr.sectionNumber = 120;
323 
324  timeval tvTemp;
325  gettimeofday(&tvTemp, nullptr);
326  localSection.hdr.timestamp = tvTemp.tv_sec;
327  localSection.hdr.timestamp_micros = tvTemp.tv_usec;
328 
329  localSection.lumiSummary.DeadtimeNormalization = 0.7;
330  localSection.lumiSummary.LHCNormalization = 0.75;
331  localSection.lumiSummary.OccNormalization[0] = 0.8;
332  localSection.lumiSummary.OccNormalization[1] = 0.85;
333  localSection.lumiSummary.ETNormalization = 0.8;
334  localSection.lumiSummary.InstantLumi = 0.9;
335  localSection.lumiSummary.InstantLumiErr = 0.10;
336  localSection.lumiSummary.InstantLumiQlty = 11;
337  localSection.lumiSummary.InstantETLumi = 0.12;
338  localSection.lumiSummary.InstantETLumiErr = 0.13;
339  localSection.lumiSummary.InstantETLumiQlty = 14;
340  localSection.lumiSummary.InstantOccLumi[0] = 0.15;
341  localSection.lumiSummary.InstantOccLumiErr[0] = 0.16;
342  localSection.lumiSummary.InstantOccLumiQlty[0] = 17;
343  localSection.lumiSummary.lumiNoise[0] = 0.18;
344  localSection.lumiSummary.InstantOccLumi[1] = 0.19;
345  localSection.lumiSummary.InstantOccLumiErr[1] = 0.20;
346  localSection.lumiSummary.InstantOccLumiQlty[1] = 21;
347  localSection.lumiSummary.lumiNoise[1] = 0.22;
348 
349  for (j = 0; j < 3564; j++) {
350  localSection.lumiDetail.ETBXNormalization[j] = 0.25 * j / 35640.0;
351  localSection.lumiDetail.OccBXNormalization[0][j] = 0.5 * j / 35640.0;
352  localSection.lumiDetail.OccBXNormalization[1][j] = 0.75 * j / 35640.0;
353  localSection.lumiDetail.LHCLumi[j] = 1 * j / 35640.0;
354  localSection.lumiDetail.ETLumi[j] = 2 * j / 35640.0;
355  localSection.lumiDetail.ETLumiErr[j] = 3 * j / 35640.0;
356  localSection.lumiDetail.ETLumiQlty[j] = 4 * j;
357  localSection.lumiDetail.OccLumi[0][j] = 5 * j / 35640.0;
358  localSection.lumiDetail.OccLumiErr[0][j] = 6 * j / 35640.0;
359  localSection.lumiDetail.OccLumiQlty[0][j] = 7 * j;
360  localSection.lumiDetail.OccLumi[1][j] = 8 * j / 35640.0;
361  localSection.lumiDetail.OccLumiErr[1][j] = 9 * j / 35640.0;
362  localSection.lumiDetail.OccLumiQlty[1][j] = 10 * j;
363  }
364 
365  for (i = 0; i < 36; i++) {
366  localSection.etSum[i].hdr.numNibbles = 7;
367  localSection.occupancy[i].hdr.numNibbles = 8;
368  localSection.lhc[i].hdr.numNibbles = 9;
369 
370  localSection.etSum[i].hdr.bIsComplete = true;
371  localSection.occupancy[i].hdr.bIsComplete = true;
372  localSection.lhc[i].hdr.bIsComplete = true;
373 
374  for (j = 0; j < 3564; j++) {
375  localSection.etSum[i].data[j] = 6 * j + 10 * i;
376  for (k = 0; k < 6; k++) {
377  localSection.occupancy[i].data[k][j] = k * j + 11 * i;
378  }
379  localSection.lhc[i].data[j] = 7 * j + 12 * i;
380  }
381  }
382 
383 #ifdef DEBUG
384  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
385 #endif
386  }
387 
389 #ifdef DEBUG
390  std::cout << "Begin " << __PRETTY_FUNCTION__ << std::endl;
391 #endif
392  int i, j, k;
393 
394  srand(time(nullptr));
395  localSection.hdr.runNumber = 55; //(rand() % 100);
396  localSection.hdr.startOrbit = (rand() % 100);
397  localSection.hdr.numOrbits = (rand() % 100);
398  localSection.hdr.numBunches = (rand() % 100);
399  localSection.hdr.numHLXs = (rand() % 100);
400  localSection.hdr.bCMSLive = true;
401  localSection.hdr.sectionNumber = (rand() % 100);
402 
403  localSection.lumiSummary.DeadtimeNormalization = (float)(rand() % 100) / 100;
404  localSection.lumiSummary.LHCNormalization = (float)(rand() % 100) / 100;
405  localSection.lumiSummary.OccNormalization[0] = (float)(rand() % 100) / 100;
406  localSection.lumiSummary.OccNormalization[1] = (float)(rand() % 100) / 100;
407  localSection.lumiSummary.ETNormalization = (float)(rand() % 100) / 100;
408  localSection.lumiSummary.InstantLumi = (float)(rand() % 100) / 100;
409  localSection.lumiSummary.InstantLumiErr = (float)(rand() % 100) / 100;
410  localSection.lumiSummary.InstantLumiQlty = (rand() % 100);
411  localSection.lumiSummary.InstantETLumi = (float)(rand() % 100) / 100;
412  localSection.lumiSummary.InstantETLumiErr = (float)(rand() % 100) / 100;
413  localSection.lumiSummary.InstantETLumiQlty = (rand() % 100);
414  localSection.lumiSummary.InstantOccLumi[0] = (float)(rand() % 100) / 100;
415  localSection.lumiSummary.InstantOccLumiErr[0] = (float)(rand() % 100) / 100;
416  localSection.lumiSummary.InstantOccLumiQlty[0] = (rand() % 100);
417  localSection.lumiSummary.lumiNoise[0] = (float)(rand() % 100) / 100;
418  localSection.lumiSummary.InstantOccLumi[1] = (float)(rand() % 100) / 100;
419  localSection.lumiSummary.InstantOccLumiErr[1] = (float)(rand() % 100) / 100;
420  localSection.lumiSummary.InstantOccLumiQlty[1] = (rand() % 100);
421  localSection.lumiSummary.lumiNoise[1] = (float)(rand() % 100) / 100;
422 
423  for (j = 0; j < 3564; j++) {
424  localSection.lumiDetail.ETBXNormalization[j] = 0.25 * j / 35640.0;
425  localSection.lumiDetail.OccBXNormalization[0][j] = 0.5 * j / 35640.0;
426  localSection.lumiDetail.OccBXNormalization[1][j] = 0.75 * j / 35640.0;
427  localSection.lumiDetail.LHCLumi[j] = (float)(rand() % 100) / 100.0;
428  localSection.lumiDetail.ETLumi[j] = (float)(rand() % 100) / 100.0;
429  localSection.lumiDetail.ETLumiErr[j] = (float)(rand() % 100) / 100.0;
430  localSection.lumiDetail.ETLumiQlty[j] = (rand() % 100);
431  localSection.lumiDetail.OccLumi[0][j] = (float)(rand() % 100) / 100.0;
432  localSection.lumiDetail.OccLumiErr[0][j] = (float)(rand() % 100) / 100.0;
433  localSection.lumiDetail.OccLumiQlty[0][j] = (rand() % 100);
434  localSection.lumiDetail.OccLumi[1][j] = (float)(rand() % 100) / 100.0;
435  localSection.lumiDetail.OccLumiErr[1][j] = (float)(rand() % 100) / 100.0;
436  localSection.lumiDetail.OccLumiQlty[1][j] = (rand() % 100);
437  }
438 
439  for (i = 0; i < 36; i++) {
440  localSection.etSum[i].hdr.numNibbles = (rand() % 100);
441  localSection.occupancy[i].hdr.numNibbles = 8 * (rand() % 100);
442  localSection.lhc[i].hdr.numNibbles = 9 * (rand() % 100);
443 
444  localSection.etSum[i].hdr.bIsComplete = true;
445  localSection.occupancy[i].hdr.bIsComplete = true;
446  localSection.lhc[i].hdr.bIsComplete = true;
447 
448  for (j = 0; j < 3564; j++) {
449  localSection.etSum[i].data[j] = 6 * (rand() % 3564);
450  for (k = 0; k < 6; k++) {
451  localSection.occupancy[i].data[k][j] = k * (rand() % 3564);
452  }
453  localSection.lhc[i].data[j] = 7 * (rand() % 3564);
454  }
455  }
456 
457 #ifdef DEBUG
458  std::cout << "End " << __PRETTY_FUNCTION__ << std::endl;
459 #endif
460  }
461 
464  GenerateFakeData(L);
465  return !(memcmp(&L, &localSection, sizeof(L)));
466  }
467 
468 } // namespace HCAL_HLX
void SetupFDSets(fd_set &ReadFDs, fd_set &WriteFDs, fd_set &ExceptFDs, int ListeningSocket=-1, int connectSocket=-1)
Definition: TCPReceiver.cc:65
unsigned char acquireMode
Definition: TCPReceiver.h:53
int SetPort(unsigned short int)
Definition: TCPReceiver.cc:179
port
Definition: query.py:116
void SetIP(std::string IP)
Definition: TCPReceiver.cc:219
void GenerateRandomData(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:388
LUMI_SECTION_SUB_HEADER hdr
OCCUPANCY_SECTION occupancy[36]
void GenerateFakeData(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:310
int ReceiveLumiSection(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:87
std::string servIP
Definition: TCPReceiver.h:57
select
when omitted electron plots will be filled w/o cut on electronId electronId = cms.PSet( src = cms.InputTag("mvaTrigV0"), cutValue = cms.double(0.5) ), when omitted electron plots will be filled w/o additional pre- selection of the electron candidates
int SetMode(unsigned char)
Definition: TCPReceiver.cc:199
LUMI_SECTION_SUB_HEADER hdr
Signal rand(Signal arg)
Definition: vlib.cc:379
unsigned short servPort
Definition: TCPReceiver.h:56
struct sockaddr_in servAddr
Definition: TCPReceiver.h:59
bool VerifyFakeData(HCAL_HLX::LUMI_SECTION &localSection)
Definition: TCPReceiver.cc:462