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
|
#include "DQM/EcalCommon/interface/MESetTrend.h"
#include <ctime>
namespace ecaldqm {
MESetTrend::MESetTrend(std::string const &_fullPath,
binning::ObjectType _otype,
binning::BinningType _btype,
MonitorElement::Kind _kind,
binning::AxisSpecs const *_xaxis /* = 0*/,
binning::AxisSpecs const *_yaxis /* = 0*/)
: MESetEcal(_fullPath, _otype, _btype, _kind, 1, _xaxis, _yaxis),
minutely_(false),
shiftAxis_(false),
currentBin_(-1) {
switch (kind_) {
case MonitorElement::Kind::TH1F:
case MonitorElement::Kind::TH2F:
case MonitorElement::Kind::TPROFILE:
case MonitorElement::Kind::TPROFILE2D:
break;
default:
throw_("Unsupported MonitorElement kind");
}
}
MESetTrend::MESetTrend(MESetTrend const &_orig)
: MESetEcal(_orig), minutely_(_orig.minutely_), shiftAxis_(_orig.shiftAxis_), currentBin_(_orig.currentBin_) {}
MESet &MESetTrend::operator=(MESet const &_rhs) {
MESetTrend const *pRhs(dynamic_cast<MESetTrend const *>(&_rhs));
if (pRhs) {
minutely_ = pRhs->minutely_;
shiftAxis_ = pRhs->shiftAxis_;
currentBin_ = pRhs->currentBin_;
}
return MESetEcal::operator=(_rhs);
}
MESet *MESetTrend::clone(std::string const &_path /* = ""*/) const {
std::string path(path_);
if (!_path.empty())
path_ = _path;
MESet *copy(new MESetTrend(*this));
path_ = path;
return copy;
}
void MESetTrend::book(DQMStore::IBooker &_ibooker, EcalElectronicsMapping const *electronicsMap) {
binning::AxisSpecs xaxis;
if (xaxis_)
xaxis = *xaxis_;
else {
xaxis.nbins = 200;
xaxis.low = 0.;
xaxis.high = 2000.;
}
if (minutely_) {
time_t localTime(time(nullptr));
struct tm timeBuffer;
gmtime_r(&localTime, &timeBuffer); // gmtime() is not thread safe
unsigned utcTime(mktime(&timeBuffer));
xaxis.low = utcTime;
if (xaxis_)
xaxis.high = utcTime + xaxis_->high - xaxis_->low;
else
xaxis.high = xaxis.low + 200 * 60.;
}
binning::AxisSpecs const *xaxisTemp(xaxis_);
xaxis_ = &xaxis;
MESetEcal::book(_ibooker, electronicsMap);
xaxis_ = xaxisTemp;
if (minutely_) {
for (unsigned iME(0); iME < mes_.size(); ++iME)
mes_[iME]->getTH1()->GetXaxis()->SetTimeDisplay(1);
setAxisTitle("UTC");
} else
setAxisTitle("LumiSections");
}
void MESetTrend::fill(
EcalDQMSetupObjects const edso, DetId const &_id, double _t, double _wy /* = 1.*/, double _w /* = 1.*/) {
if (!active_)
return;
unsigned iME(binning::findPlotIndex(edso.electronicsMap, otype_, _id));
checkME_(iME);
if (shift_(unsigned(_t)))
fill_(iME, _t + 0.5, _wy, _w);
}
void MESetTrend::fill(EcalDQMSetupObjects const edso,
EcalElectronicsId const &_id,
double _t,
double _wy /* = 1.*/,
double _w /* = 1.*/) {
if (!active_)
return;
unsigned iME(binning::findPlotIndex(edso.electronicsMap, otype_, _id));
checkME_(iME);
if (shift_(unsigned(_t)))
fill_(iME, _t + 0.5, _wy, _w);
}
void MESetTrend::fill(
EcalDQMSetupObjects const edso, int _dcctccid, double _t, double _wy /* = 1.*/, double _w /* = 1.*/) {
if (!active_)
return;
unsigned iME(binning::findPlotIndex(edso.electronicsMap, otype_, _dcctccid, btype_));
checkME_(iME);
if (shift_(unsigned(_t)))
fill_(iME, _t + 0.5, _wy, _w);
}
void MESetTrend::fill(EcalDQMSetupObjects const edso, double _t, double _wy /* = 1.*/, double _w /* = 1.*/) {
if (!active_)
return;
if (mes_.size() != 1)
throw_("MESet type incompatible");
if (shift_(unsigned(_t)))
fill_(0, _t + 0.5, _wy, _w);
}
int MESetTrend::findBin(EcalDQMSetupObjects const edso, DetId const &_id, double _t, double _y /* = 0.*/) const {
if (!active_)
return -1;
unsigned iME(binning::findPlotIndex(edso.electronicsMap, otype_, _id));
checkME_(iME);
return mes_[iME]->getTH1()->FindBin(_t + 0.5, _y);
}
int MESetTrend::findBin(EcalDQMSetupObjects const edso,
EcalElectronicsId const &_id,
double _t,
double _y /* = 0.*/) const {
if (!active_)
return -1;
unsigned iME(binning::findPlotIndex(edso.electronicsMap, otype_, _id));
checkME_(iME);
return mes_[iME]->getTH1()->FindBin(_t + 0.5, _y);
}
int MESetTrend::findBin(EcalDQMSetupObjects const edso, int _dcctccid, double _t, double _y /* = 0.*/) const {
if (!active_)
return -1;
unsigned iME(binning::findPlotIndex(edso.electronicsMap, otype_, _dcctccid, btype_));
checkME_(iME);
return mes_[iME]->getTH1()->FindBin(_t + 0.5, _y);
}
int MESetTrend::findBin(EcalDQMSetupObjects const edso, double _t, double _y /* = 0.*/) const {
if (!active_)
return -1;
if (mes_.size() != 1)
throw_("MESet type incompatible");
return mes_[0]->getTH1()->FindBin(_t + 0.5, _y);
}
void MESetTrend::setCumulative() {
if (kind_ == MonitorElement::Kind::TPROFILE || kind_ == MonitorElement::Kind::TPROFILE2D)
throw_("Cumulative flag set for a profile plot");
currentBin_ = 1;
}
bool MESetTrend::shift_(unsigned _t) {
TAxis *tAxis(mes_[0]->getTH1()->GetXaxis());
int nbinsX(tAxis->GetNbins());
unsigned tLow(tAxis->GetBinLowEdge(1));
unsigned tHigh(tAxis->GetBinUpEdge(nbinsX));
if (!shiftAxis_)
return _t >= tLow && _t < tHigh;
int dBin(0);
int unitsPerBin((tHigh - tLow) / nbinsX);
if (_t >= tLow && _t < tHigh) {
if (currentBin_ > 0) {
int thisBin(tAxis->FindBin(_t + 0.5));
if (thisBin < currentBin_)
return false;
else if (thisBin > currentBin_) {
for (unsigned iME(0); iME < mes_.size(); iME++) {
MonitorElement *me(mes_[iME]);
int nbinsY(me->getTH1()->GetNbinsY());
for (int iy(1); iy <= nbinsY; ++iy) {
int orig(me->getTH1()->GetBin(currentBin_, iy));
double currentContent(me->getBinContent(orig));
double currentError(me->getBinError(orig));
for (int ix(currentBin_); ix <= thisBin; ++ix) {
int dest(me->getTH1()->GetBin(ix, iy));
me->setBinContent(dest, currentContent);
me->setBinError(dest, currentError);
}
}
}
}
}
return true;
} else if (_t >= tHigh) {
dBin = (_t - tHigh) / unitsPerBin + 1;
if (currentBin_ > 0)
currentBin_ = nbinsX;
} else if (_t < tLow) {
if (currentBin_ > 0)
return false; // no going back in time in case of cumulative history
int maxBin(0);
for (unsigned iME(0); iME < mes_.size(); iME++) {
MonitorElement *me(mes_[iME]);
bool filled(false);
int iMax(nbinsX + 1);
while (--iMax > 0 && !filled) {
switch (kind_) {
case MonitorElement::Kind::TH1F:
if (me->getBinContent(iMax) != 0)
filled = true;
break;
case MonitorElement::Kind::TPROFILE:
if (me->getBinEntries(iMax) != 0)
filled = true;
break;
case MonitorElement::Kind::TH2F:
for (int iy(1); iy <= me->getNbinsY(); iy++) {
if (me->getBinContent(me->getTH1()->GetBin(iMax, iy)) != 0) {
filled = true;
break;
}
}
break;
case MonitorElement::Kind::TPROFILE2D:
for (int iy(1); iy <= me->getNbinsY(); iy++) {
if (me->getBinEntries(me->getTH1()->GetBin(iMax, iy)) != 0) {
filled = true;
break;
}
}
break;
default:
break;
}
}
if (iMax > maxBin)
maxBin = iMax;
}
if (_t < tLow - (nbinsX - maxBin) * unitsPerBin)
return false;
dBin = (_t - tLow) / unitsPerBin - 1;
}
int start(dBin > 0 ? dBin + 1 : nbinsX + dBin);
int end(dBin > 0 ? nbinsX + 1 : 0);
int step(dBin > 0 ? 1 : -1);
tLow += dBin * unitsPerBin;
tHigh += dBin * unitsPerBin;
for (unsigned iME(0); iME < mes_.size(); iME++) {
MonitorElement *me(mes_[iME]);
me->getTH1()->GetXaxis()->SetLimits(tLow, tHigh);
if ((end - start) / step < 0) {
me->Reset();
continue;
}
me->setEntries(0.);
double entries(0.);
switch (kind_) {
case MonitorElement::Kind::TH1F: {
int ix(start);
for (; ix != end; ix += step) {
double binContent(me->getBinContent(ix));
entries += binContent;
me->setBinContent(ix - dBin, binContent);
me->setBinError(ix - dBin, me->getBinError(ix));
}
ix = end - dBin - 1 * step;
double lastContent(currentBin_ > 0 ? me->getBinContent(ix) : 0.);
double lastError(currentBin_ > 0 ? me->getBinContent(ix) : 0.);
for (ix += step; ix != end; ix += step) {
me->setBinContent(ix, lastContent);
me->setBinError(ix, lastError);
}
} break;
case MonitorElement::Kind::TPROFILE: {
int ix(start);
for (; ix != end; ix += step) {
double binEntries(me->getBinEntries(ix));
double binContent(me->getBinContent(ix));
entries += binEntries;
me->setBinEntries(ix - dBin, binEntries);
me->setBinContent(ix - dBin, binContent * binEntries);
if (binEntries > 0) {
double rms(me->getBinError(ix) * std::sqrt(binEntries));
double sumw2((rms * rms + binContent * binContent) * binEntries);
me->setBinError(ix - dBin, std::sqrt(sumw2));
} else
me->setBinError(ix - dBin, 0.);
}
ix = end - dBin;
for (; ix != end; ix += step) {
me->setBinEntries(ix, 0.);
me->setBinContent(ix, 0.);
me->setBinError(ix, 0.);
}
} break;
case MonitorElement::Kind::TH2F: {
int ix(start);
int nbinsY(me->getNbinsY());
for (; ix != end; ix += step) {
for (int iy(1); iy <= nbinsY; iy++) {
int orig(me->getTH1()->GetBin(ix, iy));
int dest(me->getTH1()->GetBin(ix - dBin, iy));
double binContent(me->getBinContent(orig));
entries += binContent;
me->setBinContent(dest, binContent);
me->setBinError(dest, me->getBinError(orig));
me->setBinContent(orig, 0.);
me->setBinError(orig, 0.);
}
}
ix = end - dBin - 1 * step;
std::vector<double> lastContent;
std::vector<double> lastError;
for (int iy(1); iy <= nbinsY; iy++) {
lastContent.push_back(currentBin_ > 0 ? me->getBinContent(ix, iy) : 0.);
lastError.push_back(currentBin_ > 0 ? me->getBinError(ix, iy) : 0.);
}
for (ix += step; ix != end; ix += step) {
for (int iy(1); iy <= nbinsY; iy++) {
int bin(me->getTH1()->GetBin(ix, iy));
me->setBinContent(bin, lastContent[iy - 1]);
me->setBinError(bin, lastError[iy - 1]);
}
}
} break;
case MonitorElement::Kind::TPROFILE2D: {
int ix(start);
int nbinsY(me->getNbinsY());
for (; ix != end; ix += step) {
for (int iy(1); iy <= nbinsY; iy++) {
int orig(me->getTH1()->GetBin(ix, iy));
int dest(me->getTH1()->GetBin(ix - dBin, iy));
double binEntries(me->getBinEntries(orig));
double binContent(me->getBinContent(orig));
entries += binEntries;
me->setBinEntries(dest, binEntries);
me->setBinContent(dest, binContent * binEntries);
if (binEntries > 0) {
double rms(me->getBinError(orig) * std::sqrt(binEntries));
double sumw2((rms * rms + binContent * binContent) * binEntries);
me->setBinError(dest, std::sqrt(sumw2));
} else
me->setBinError(dest, 0.);
}
}
ix = end - dBin;
for (; ix != end; ix += step) {
for (int iy(1); iy <= nbinsY; iy++) {
int bin(me->getTH1()->GetBin(ix, iy));
me->setBinEntries(bin, 0.);
me->setBinContent(bin, 0.);
me->setBinError(bin, 0.);
}
}
} break;
default:
break;
}
me->setEntries(entries);
}
return true;
}
} // namespace ecaldqm
|