1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/*
* DataPoint.cc
*
* Created on: Sep 24, 2012
* Author: aspataru
*/
#include "EventFilter/Utilities/interface/DataPoint.h"
#include <algorithm>
#include <cassert>
#include <cstring>
//max collected updates per lumi
#define MAXUPDATES 0xffffffff
#define MAXBINS
using namespace jsoncollector;
namespace jsoncollector {
template class HistoJ<unsigned int>;
template class HistoJ<double>;
} // namespace jsoncollector
const std::string DataPoint::SOURCE = "source";
const std::string DataPoint::DEFINITION = "definition";
const std::string DataPoint::DATA = "data";
DataPoint::~DataPoint() {
if (buf_)
delete[] buf_;
}
/*
*
* Method implementation for simple DataPoint usage
*
*/
void DataPoint::serialize(Json::Value &root) const {
if (!source_.empty()) {
root[SOURCE] = source_;
}
if (!definition_.empty()) {
root[DEFINITION] = definition_;
}
for (unsigned int i = 0; i < data_.size(); i++)
root[DATA].append(data_[i]);
}
void DataPoint::deserialize(Json::Value &root) {
source_ = root.get(SOURCE, "").asString();
definition_ = root.get(DEFINITION, "").asString();
if (root.get(DATA, "").isArray()) {
unsigned int size = root.get(DATA, "").size();
for (unsigned int i = 0; i < size; i++) {
data_.push_back(root.get(DATA, "")[i].asString());
}
}
}
/*
*
* Method implementation for the new multi-threaded model
*
* */
void DataPoint::trackMonitorable(JsonMonitorable const *monitorable, bool NAifZeroUpdates) {
name_ = monitorable->getName();
tracked_ = (void const *)monitorable;
if (dynamic_cast<IntJ const *>(monitorable))
monType_ = TYPEINT;
else if (dynamic_cast<DoubleJ const *>(monitorable))
monType_ = TYPEDOUBLE;
else if (dynamic_cast<StringJ const *>(monitorable))
monType_ = TYPESTRING;
else
assert(0);
NAifZeroUpdates_ = NAifZeroUpdates;
}
void DataPoint::trackVectorUInt(std::string const &name,
std::vector<unsigned int> const *monvec,
bool NAifZeroUpdates) {
name_ = name;
tracked_ = (void const *)monvec;
isStream_ = true;
monType_ = TYPEUINT;
NAifZeroUpdates_ = NAifZeroUpdates;
makeStreamLumiMap(monvec->size());
}
void DataPoint::trackVectorUIntAtomic(std::string const &name,
std::vector<AtomicMonUInt *> const *monvec,
bool NAifZeroUpdates) {
name_ = name;
tracked_ = (void const *)monvec;
isStream_ = true;
isAtomic_ = true;
monType_ = TYPEUINT;
NAifZeroUpdates_ = NAifZeroUpdates;
makeStreamLumiMap(monvec->size());
}
void DataPoint::makeStreamLumiMap(unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
streamDataMaps_.push_back(MonPtrMap());
}
}
void DataPoint::serialize(Json::Value &root, bool rootInit, std::string const &input) const {
if (rootInit) {
if (!source_.empty())
root[SOURCE] = source_;
if (!definition_.empty())
root[DEFINITION] = definition_;
}
root[DATA].append(input);
}
void DataPoint::snap(unsigned int lumi) {
isCached_ = false;
if (isStream_) {
if (monType_ == TYPEUINT) {
for (unsigned int i = 0; i < streamDataMaps_.size(); i++) {
unsigned int streamLumi_ = streamLumisPtr_->at(i); //get currently processed stream lumi
unsigned int monVal;
#if ATOMIC_LEVEL > 0
if (isAtomic_)
monVal =
(static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->at(i)->load(std::memory_order_relaxed);
#else
if (isAtomic_)
monVal = *((static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->at(i));
#endif
else
monVal = (static_cast<std::vector<unsigned int> const *>(tracked_))->at(i);
auto itr = streamDataMaps_[i].find(streamLumi_);
if (itr == streamDataMaps_[i].end()) {
if (opType_ == OPHISTO) {
if (*nBinsPtr_) {
HistoJ<unsigned int> *nh = new HistoJ<unsigned int>(1, MAXUPDATES);
nh->update(monVal);
streamDataMaps_[i][streamLumi_] = nh;
}
} else { //default to SUM
IntJ *nj = new IntJ;
nj->update(monVal);
streamDataMaps_[i][streamLumi_] = nj;
}
} else {
if (opType_ == OPHISTO) {
if (*nBinsPtr_) {
(static_cast<HistoJ<unsigned int> *>(itr->second.get()))->update(monVal);
}
} else {
*(static_cast<IntJ *>(itr->second.get())) = monVal;
}
}
}
} else
assert(monType_ != TYPEINT); //not yet implemented, application error
} else
snapGlobal(lumi);
}
void DataPoint::snapGlobal(unsigned int lumi) {
isCached_ = false;
if (isStream_)
return;
auto itr = globalDataMap_.find(lumi);
if (itr == globalDataMap_.end()) {
if (monType_ == TYPEINT) {
IntJ *ij = new IntJ;
ij->update((static_cast<IntJ const *>(tracked_))->value());
globalDataMap_[lumi] = ij;
}
if (monType_ == TYPEDOUBLE) {
DoubleJ *dj = new DoubleJ;
dj->update((static_cast<DoubleJ const *>(tracked_))->value());
globalDataMap_[lumi] = dj;
}
if (monType_ == TYPESTRING) {
StringJ *sj = new StringJ;
sj->update((static_cast<StringJ const *>(tracked_))->value());
globalDataMap_[lumi] = sj;
}
} else {
if (monType_ == TYPEINT)
static_cast<IntJ *>(itr->second.get())->update((static_cast<IntJ const *>(tracked_))->value());
else if (monType_ == TYPEDOUBLE)
static_cast<DoubleJ *>(itr->second.get())->update((static_cast<DoubleJ const *>(tracked_))->value());
else if (monType_ == TYPESTRING)
static_cast<StringJ *>(itr->second.get())->concatenate((static_cast<StringJ const *>(tracked_))->value());
}
}
void DataPoint::snapStreamAtomic(unsigned int lumi, unsigned int streamID) {
if (!isStream_ || !isAtomic_)
return;
isCached_ = false;
if (monType_ == TYPEUINT) {
unsigned int monVal;
#if ATOMIC_LEVEL > 0
if (isAtomic_)
monVal =
(static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->at(streamID)->load(std::memory_order_relaxed);
#else
if (isAtomic_)
monVal = *((static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->at(streamID));
#endif
else
monVal = (static_cast<std::vector<unsigned int> const *>(tracked_))->at(streamID);
auto itr = streamDataMaps_[streamID].find(lumi);
if (itr == streamDataMaps_[streamID].end()) //insert
{
if (opType_ == OPHISTO) {
if (*nBinsPtr_) {
HistoJ<unsigned int> *h = new HistoJ<unsigned int>(1, MAXUPDATES);
h->update(monVal);
streamDataMaps_[streamID][lumi] = h;
}
} else { //default to SUM
IntJ *h = new IntJ;
h->update(monVal);
streamDataMaps_[streamID][lumi] = h;
}
} else {
if (opType_ == OPHISTO) {
if (*nBinsPtr_) {
static_cast<HistoJ<unsigned int> *>(itr->second.get())->update(monVal);
}
} else
*(static_cast<IntJ *>(itr->second.get())) = monVal;
}
} else
assert(monType_ != TYPEINT); //not yet implemented
}
std::string DataPoint::fastOutCSV(int sid) {
if (tracked_) {
if (isStream_) {
std::stringstream ss;
if (sid < 0) {
if (isAtomic_) {
// if ATOMIC_LEVEL>0
// ss << (unsigned int) (static_cast<std::vector<AtomicMonUInt*>*>(tracked_))->at(fastIndex_)->load(std::memory_order_relaxed);
ss << (unsigned int)*((static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->at(fastIndex_));
fastIndex_ = (fastIndex_ + 1) % (static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->size();
} else {
ss << (static_cast<std::vector<unsigned int> const *>(tracked_))->at(fastIndex_);
fastIndex_ = (fastIndex_ + 1) % (static_cast<std::vector<unsigned int> const *>(tracked_))->size();
}
} else {
if (isAtomic_)
ss << (unsigned int)*((static_cast<std::vector<AtomicMonUInt *> const *>(tracked_))->at(unsigned(sid)));
else
ss << (static_cast<std::vector<unsigned int> const *>(tracked_))->at(unsigned(sid));
}
return ss.str();
}
return (static_cast<JsonMonitorable const *>(tracked_))->toString();
}
return std::string("");
}
JsonMonitorable *DataPoint::mergeAndRetrieveValue(unsigned int lumi) {
assert(monType_ == TYPEUINT && isStream_); //for now only support UINT and SUM for stream variables
IntJ *newJ = new IntJ;
for (unsigned int i = 0; i < streamDataMaps_.size(); i++) {
auto itr = streamDataMaps_[i].find(lumi);
if (itr != streamDataMaps_[i].end()) {
newJ->add(static_cast<IntJ *>(itr->second.get())->value());
}
}
cacheI_ = newJ->value();
isCached_ = true;
return newJ; //assume the caller takes care of deleting the object
}
void DataPoint::mergeAndSerialize(Json::Value &root, unsigned int lumi, bool initJsonValue, int sid) {
if (initJsonValue) {
root[SOURCE] = source_;
root[DEFINITION] = definition_;
}
if (isDummy_) {
root[DATA].append("N/A");
return;
}
if (!isStream_) {
auto itr = globalDataMap_.find(lumi);
if (itr != globalDataMap_.end()) {
root[DATA].append(itr->second.get()->toString());
} else {
if (NAifZeroUpdates_)
root[DATA].append("N/A");
else if (monType_ == TYPESTRING)
root[DATA].append("");
else
root[DATA].append("0");
}
return;
} else {
assert(monType_ == TYPEUINT);
if (isCached_) {
std::stringstream ss;
ss << cacheI_;
root[DATA].append(ss.str());
return;
}
if (opType_ != OPHISTO) { //sum is default
std::stringstream ss;
unsigned int updates = 0;
unsigned int sum = 0;
if (sid < 1)
for (unsigned int i = 0; i < streamDataMaps_.size(); i++) {
auto itr = streamDataMaps_[i].find(lumi);
if (itr != streamDataMaps_[i].end()) {
sum += static_cast<IntJ *>(itr->second.get())->value();
updates++;
}
}
else {
auto itr = streamDataMaps_[unsigned(sid)].find(lumi);
if (itr != streamDataMaps_[unsigned(sid)].end()) {
sum += static_cast<IntJ *>(itr->second.get())->value();
updates++;
}
}
if (!updates && NAifZeroUpdates_)
ss << "N/A";
ss << sum;
root[DATA].append(ss.str());
return;
}
if (opType_ == OPHISTO) {
if (nBinsPtr_ == nullptr) {
root[DATA].append("N/A");
return;
}
if (*nBinsPtr_ > bufLen_) {
if (buf_)
delete[] buf_;
bufLen_ = *nBinsPtr_;
buf_ = new uint32_t[bufLen_];
}
memset(buf_, 0, bufLen_ * sizeof(uint32_t));
unsigned int updates = 0;
if (sid < 1)
for (unsigned int i = 0; i < streamDataMaps_.size(); i++) {
auto itr = streamDataMaps_[i].find(lumi);
if (itr != streamDataMaps_[i].end()) {
HistoJ<unsigned int> *monObj = static_cast<HistoJ<unsigned int> *>(itr->second.get());
updates += monObj->getUpdates();
auto &hvec = monObj->value();
for (unsigned int j = 0; j < hvec.size(); j++) {
unsigned int thisbin = (unsigned int)hvec[j];
if (thisbin < *nBinsPtr_) {
buf_[thisbin]++;
}
}
}
}
else {
auto itr = streamDataMaps_[unsigned(sid)].find(lumi);
if (itr != streamDataMaps_[unsigned(sid)].end()) {
HistoJ<unsigned int> *monObj = static_cast<HistoJ<unsigned int> *>(itr->second.get());
updates += monObj->getUpdates();
auto &hvec = monObj->value();
for (unsigned int j = 0; j < hvec.size(); j++) {
unsigned int thisbin = (unsigned int)hvec[j];
if (thisbin < *nBinsPtr_) {
buf_[thisbin]++;
}
}
}
}
std::stringstream ss;
if (!*nBinsPtr_ || (!updates && NAifZeroUpdates_))
ss << "N/A";
else {
ss << "[";
if (*nBinsPtr_) {
for (unsigned int i = 0; i < *nBinsPtr_ - 1; i++) {
ss << buf_[i] << ",";
}
ss << buf_[*nBinsPtr_ - 1];
}
ss << "]";
}
root[DATA].append(ss.str());
return;
}
}
}
//wipe out data that will no longer be used
void DataPoint::discardCollected(unsigned int lumi) {
for (unsigned int i = 0; i < streamDataMaps_.size(); i++) {
auto itr = streamDataMaps_[i].find(lumi);
if (itr != streamDataMaps_[i].end())
streamDataMaps_[i].erase(lumi);
}
auto itr = globalDataMap_.find(lumi);
if (itr != globalDataMap_.end())
globalDataMap_.erase(lumi);
}
|