PAASS
Software suite to Acquire and Analyze Data from Pixie16
poll2_stats.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <string.h>
4 #include <unistd.h>
5 
6 #include "poll2_stats.h"
7 #include "poll2_socket.h"
8 
9 StatsHandler::StatsHandler(const size_t nCards){
10 
11  numCards = nCards;
12 
13  // Define all the 2d arrays
14  nEventsDelta = new unsigned int*[numCards];
15  nEventsTotal = new unsigned int*[numCards];
16  calcEventRate = new double*[numCards];
17  inputCountRate = new double*[numCards];
18  outputCountRate = new double*[numCards];
19  for(unsigned int i = 0; i < numCards; i++){
20  nEventsDelta[i] = new unsigned int[NUM_CHAN_PER_MOD];
21  nEventsTotal[i] = new unsigned int[NUM_CHAN_PER_MOD];
22  calcEventRate[i] = new double[NUM_CHAN_PER_MOD];
23  inputCountRate[i] = new double[NUM_CHAN_PER_MOD];
24  outputCountRate[i] = new double[NUM_CHAN_PER_MOD];
25  }
26 
27  for(unsigned int i = 0; i < numCards; i++){
28  for(unsigned int j = 0; j < NUM_CHAN_PER_MOD; j++){
29  nEventsDelta[i][j] = 0;
30  nEventsTotal[i][j] = 0;
31  calcEventRate[i][j] = 0.0;
32  inputCountRate[i][j] = 0.0;
33  outputCountRate[i][j] = 0.0;
34  }
35  }
36 
37  // Define all the 1d arrays
38  dataDelta = new size_t[numCards];
39  dataTotal = new size_t[numCards];
40 
41  timeElapsed = 0.0;
42  totalTime = 0.0;
43  dumpTime = 3.0; // Minimum of 2 seconds between updates
44 
45  is_able_to_send = true;
46 
47  client = new Client();
48  if(!client->Init("127.0.0.1", 5556)){
49  is_able_to_send = false;
50  }
51 
52  Clear();
53 }
54 
56  client->SendMessage((char *)"$KILL_SOCKET", 13);
57  client->Close();
58 
59  // De-allocate the 2d arrays
60  for(unsigned int i = 0; i < numCards; i++){
61  delete[] nEventsDelta[i];
62  delete[] nEventsTotal[i];
63  delete[] calcEventRate[i];
64  delete[] inputCountRate[i];
65  delete[] outputCountRate[i];
66  }
67  delete[] nEventsDelta;
68  delete[] nEventsTotal;
69  delete[] calcEventRate;
70  delete[] inputCountRate;
71  delete[] outputCountRate;
72 
73  // De-allocate the 1d arrays
74  delete[] dataDelta;
75  delete[] dataTotal;
76 }
77 
78 void StatsHandler::AddEvent(unsigned int mod, unsigned int ch, size_t size, int delta_/*=1*/){
79  if(mod >= numCards){
80  std::cout << "Bad module " << mod << ", numCards = " << numCards << std::endl;
81  return;
82  }
83  if(ch >= (unsigned)NUM_CHAN_PER_MOD){
84  std::cout << "Bad channel " << ch << std::endl;
85  return;
86  }
87  nEventsDelta[mod][ch] += delta_;
88  nEventsTotal[mod][ch] += delta_;
89  dataDelta[mod] += size;
90  dataTotal[mod] += size;
91 }
92 
96 bool StatsHandler::AddTime(double dtime) {
97  timeElapsed += dtime;
98  totalTime += dtime;
99 
100  return (timeElapsed >= dumpTime);
101 
102 }
103 
105  if(!is_able_to_send){ return; }
106 
107  double dataRate = GetTotalDataRate();
108 
109  // Below is the stats packet structure (for N modules)
110  // ---------------------------------------------------
111  // 4 byte total number of pixie modules (N)
112  // 8 byte total time of run (in seconds)
113  // 8 byte total data rate (in B/s)
114  // channel 0, 0 rate
115  // channel 0, 0 total
116  // channel 0, 1 rate
117  // channel 0, 1 total
118  // ...
119  // channel 0, 15 rate
120  // channel 0, 15 total
121  // channel 1, 0 rate
122  // channel 1, 0 total
123  // ...
124  // channel N-1, 15 rate
125  // channel N-1, 15 total
126  //msg_size = 20 fixed bytes + 4 words/card/ch * numCards * 16 ch * 8 bytes/ word
127  size_t msg_size = sizeof(numCards) + sizeof(totalTime) + sizeof(dataRate) + numCards*16*(sizeof(inputCountRate[0][0])+sizeof(outputCountRate[0][0])+sizeof(calcEventRate[0][0])+sizeof(nEventsTotal[0][0]));
128  char *message = new char[msg_size];
129  char *ptr = message;
130 
131  // Construct the rate message
132  memcpy(ptr, &numCards, 4); ptr += 4;
133  memcpy(ptr, &totalTime, 8); ptr += 8;
134  memcpy(ptr, &dataRate, 8); ptr += 8;
135  for (unsigned int i=0; i < numCards; i++) {
136  for (unsigned int j=0; j < NUM_CHAN_PER_MOD; j++) {
137  calcEventRate[i][j] = nEventsDelta[i][j] / timeElapsed;
138  if (timeElapsed<=0)
139  calcEventRate[i][j] = 0;
140  memcpy(ptr, &inputCountRate[i][j], sizeof(inputCountRate[i][j])); ptr += sizeof(inputCountRate[i][j]);
141  memcpy(ptr, &outputCountRate[i][j], sizeof(outputCountRate[i][j])); ptr += sizeof(outputCountRate[i][j]);
142  memcpy(ptr, &calcEventRate[i][j], sizeof(calcEventRate[i][j])); ptr += sizeof(calcEventRate[i][j]);
143  memcpy(ptr, &nEventsTotal[i][j], sizeof(nEventsTotal[i][j])); ptr += sizeof(nEventsTotal[i][j]);
144  } //Update the status bar
145  }
146 
147  client->SendMessage(message, msg_size);
148 
149  delete[] message;
150 }
151 
153  if(timeElapsed<=0) return 0;
154  return dataDelta[mod] / timeElapsed;
155 }
156 
158  double rate = 0;
159  for (unsigned int i=0; i < numCards; i++) rate += GetDataRate(i);
160  return rate;
161 }
162 
164  double output = 0.0;
165  for(unsigned int i = 0; i < numCards; i++){
166  output += calcEventRate[mod][i];
167  }
168  return output;
169 }
170 
172  return totalTime;
173 }
174 
176  timeElapsed = 0;
177 
178  for(size_t i=0; i < numCards; i++){
179  for(size_t j = 0; j < NUM_CHAN_PER_MOD; j++){
180  nEventsDelta[i][j] = 0;
181  }
182  dataDelta[i] = 0;
183  }
184 }
185 
186 void StatsHandler::SetXiaRates(int mod, std::vector<std::pair<double, double> > *xiaRates) {
187  for (int ch = 0; ch < NUM_CHAN_PER_MOD; ch++) {
188  inputCountRate[mod][ch] = xiaRates->at(ch).first;
189  outputCountRate[mod][ch] = xiaRates->at(ch).second;
190  }
191 }
193  totalTime = 0;
194  for(size_t i=0; i < numCards; i++){
195  for(size_t j = 0; j < NUM_CHAN_PER_MOD; j++){
196  nEventsTotal[i][j] = 0;
197  }
198  }
199 }
200 
202  ClearRates();
203  ClearTotals();
204 }
void Close()
Close the socket.
double ** outputCountRate
The XIA Module output count rate.
Definition: poll2_stats.h:63
double ** inputCountRate
The XIA Module input count rate.
Definition: poll2_stats.h:62
bool is_able_to_send
Definition: poll2_stats.h:80
#define NUM_CHAN_PER_MOD
Definition: poll2_stats.h:6
unsigned int numCards
Definition: poll2_stats.h:78
Provides network connectivity for poll2.
void ClearTotals()
double ** calcEventRate
Definition: poll2_stats.h:60
double GetDataRate(size_t mod)
double GetTotalDataRate()
double dumpTime
Definition: poll2_stats.h:75
unsigned int ** nEventsTotal
Definition: poll2_stats.h:51
void ClearRates()
void SetXiaRates(int mod, std::vector< std::pair< double, double >> *xiaRates)
Set the ICR and OCR from the XIA module.
bool Init(const char *address_, int port_)
size_t * dataTotal
Definition: poll2_stats.h:57
StatsHandler(size_t nCards=1)
Definition: poll2_stats.cpp:9
double GetEventRate(size_t mod)
int SendMessage(char *message_, size_t length_)
double totalTime
Definition: poll2_stats.h:72
void Clear()
Clear the stats.
unsigned int ** nEventsDelta
Definition: poll2_stats.h:48
double timeElapsed
Definition: poll2_stats.h:69
Client * client
Definition: poll2_stats.h:45
void AddEvent(unsigned int mod, unsigned int ch, size_t size, int delta_=1)
Definition: poll2_stats.cpp:78
bool AddTime(double dtime)
Definition: poll2_stats.cpp:96
double GetTotalTime()
Return the total run time.
size_t * dataDelta
Definition: poll2_stats.h:54