CMS 3D CMS Logo

MESetTrend.cc
Go to the documentation of this file.
2 
3 #include <ctime>
4 
5 namespace ecaldqm {
6 
7  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*/) :
8  MESetEcal(_fullPath, _otype, _btype, _kind, 1, _xaxis, _yaxis),
9  minutely_(false),
10  shiftAxis_(false),
11  currentBin_(-1)
12  {
13  switch(kind_){
18  break;
19  default:
20  throw_("Unsupported MonitorElement kind");
21  }
22  }
23 
25  MESetEcal(_orig),
26  minutely_(_orig.minutely_),
27  shiftAxis_(_orig.shiftAxis_),
29  {
30  }
31 
32  MESet&
34  {
35  MESetTrend const* pRhs(dynamic_cast<MESetTrend const*>(&_rhs));
36  if(pRhs){
37  minutely_ = pRhs->minutely_;
38  shiftAxis_ = pRhs->shiftAxis_;
39  currentBin_ = pRhs->currentBin_;
40  }
41 
42  return MESetEcal::operator=(_rhs);
43  }
44 
45  MESet*
46  MESetTrend::clone(std::string const& _path/* = ""*/) const
47  {
49  if(_path != "") path_ = _path;
50  MESet* copy(new MESetTrend(*this));
51  path_ = path;
52  return copy;
53  }
54 
55  void
57  {
58  binning::AxisSpecs xaxis;
59  if(xaxis_) xaxis = *xaxis_;
60  else{
61  xaxis.nbins = 200;
62  xaxis.low = 0.;
63  xaxis.high = 2000.;
64  }
65 
66  if(minutely_){
67  time_t localTime(time(nullptr));
68  struct tm timeBuffer;
69  gmtime_r(&localTime, &timeBuffer); // gmtime() is not thread safe
70  unsigned utcTime(mktime(&timeBuffer));
71 
72  xaxis.low = utcTime;
73  if(xaxis_) xaxis.high = utcTime + xaxis_->high - xaxis_->low;
74  else xaxis.high = xaxis.low + 200 * 60.;
75  }
76 
77  binning::AxisSpecs const* xaxisTemp(xaxis_);
78  xaxis_ = &xaxis;
79 
80  MESetEcal::book(_ibooker);
81 
82  xaxis_ = xaxisTemp;
83 
84  if(minutely_){
85  for(unsigned iME(0); iME < mes_.size(); ++iME)
86  mes_[iME]->getTH1()->GetXaxis()->SetTimeDisplay(1);
87  setAxisTitle("UTC");
88  }
89  else
90  setAxisTitle("LumiSections");
91  }
92 
93  void
94  MESetTrend::fill(DetId const& _id, double _t, double _wy/* = 1.*/, double _w/* = 1.*/)
95  {
96  if(!active_) return;
97 
98  unsigned iME(binning::findPlotIndex(otype_, _id));
99  checkME_(iME);
100 
101  if(shift_(unsigned(_t)))
102  fill_(iME, _t + 0.5, _wy, _w);
103  }
104 
105  void
106  MESetTrend::fill(EcalElectronicsId const& _id, double _t, double _wy/* = 1.*/, double _w/* = 1.*/)
107  {
108  if(!active_) return;
109 
110  unsigned iME(binning::findPlotIndex(otype_, _id));
111  checkME_(iME);
112 
113  if(shift_(unsigned(_t)))
114  fill_(iME, _t + 0.5, _wy, _w);
115  }
116 
117  void
118  MESetTrend::fill(int _dcctccid, double _t, double _wy/* = 1.*/, double _w/* = 1.*/)
119  {
120  if(!active_) return;
121 
122  unsigned iME(binning::findPlotIndex(otype_, _dcctccid, btype_));
123  checkME_(iME);
124 
125  if(shift_(unsigned(_t)))
126  fill_(iME, _t + 0.5, _wy, _w);
127  }
128 
129  void
130  MESetTrend::fill(double _t, double _wy/* = 1.*/, double _w/* = 1.*/)
131  {
132  if(!active_) return;
133  if(mes_.size() != 1)
134  throw_("MESet type incompatible");
135 
136  if(shift_(unsigned(_t)))
137  fill_(0, _t + 0.5, _wy, _w);
138  }
139 
140  int
141  MESetTrend::findBin(DetId const& _id, double _t, double _y/* = 0.*/) const
142  {
143  if(!active_) return -1;
144 
145  unsigned iME(binning::findPlotIndex(otype_, _id));
146  checkME_(iME);
147 
148  return mes_[iME]->getTH1()->FindBin(_t + 0.5, _y);
149  }
150 
151  int
152  MESetTrend::findBin(EcalElectronicsId const& _id, double _t, double _y/* = 0.*/) const
153  {
154  if(!active_) return -1;
155 
156  unsigned iME(binning::findPlotIndex(otype_, _id));
157  checkME_(iME);
158 
159  return mes_[iME]->getTH1()->FindBin(_t + 0.5, _y);
160  }
161 
162  int
163  MESetTrend::findBin(int _dcctccid, double _t, double _y/* = 0.*/) const
164  {
165  if(!active_) return -1;
166 
167  unsigned iME(binning::findPlotIndex(otype_, _dcctccid, btype_));
168  checkME_(iME);
169 
170  return mes_[iME]->getTH1()->FindBin(_t + 0.5, _y);
171  }
172 
173  int
174  MESetTrend::findBin(double _t, double _y/* = 0.*/) const
175  {
176  if(!active_) return -1;
177  if(mes_.size() != 1)
178  throw_("MESet type incompatible");
179 
180  return mes_[0]->getTH1()->FindBin(_t + 0.5, _y);
181  }
182 
183  void
185  {
187  throw_("Cumulative flag set for a profile plot");
188  currentBin_ = 1;
189  }
190 
191  bool
192  MESetTrend::shift_(unsigned _t)
193  {
194  TAxis* tAxis(mes_[0]->getTH1()->GetXaxis());
195  int nbinsX(tAxis->GetNbins());
196  unsigned tLow(tAxis->GetBinLowEdge(1));
197  unsigned tHigh(tAxis->GetBinUpEdge(nbinsX));
198 
199  if(!shiftAxis_) return _t >= tLow && _t < tHigh;
200 
201  int dBin(0);
202  int unitsPerBin((tHigh - tLow) / nbinsX);
203 
204  if(_t >= tLow && _t < tHigh){
205  if(currentBin_ > 0){
206  int thisBin(tAxis->FindBin(_t + 0.5));
207  if(thisBin < currentBin_) return false;
208  else if(thisBin > currentBin_){
209  for(unsigned iME(0); iME < mes_.size(); iME++){
210  MonitorElement* me(mes_[iME]);
211  int nbinsY(me->getTH1()->GetNbinsY());
212  for(int iy(1); iy <= nbinsY; ++iy){
213  int orig(me->getTH1()->GetBin(currentBin_, iy));
214  double currentContent(me->getBinContent(orig));
215  double currentError(me->getBinError(orig));
216  for(int ix(currentBin_); ix <= thisBin; ++ix){
217  int dest(me->getTH1()->GetBin(ix, iy));
218  me->setBinContent(dest, currentContent);
219  me->setBinError(dest, currentError);
220  }
221  }
222  }
223  }
224  }
225 
226  return true;
227  }
228  else if(_t >= tHigh){
229  dBin = (_t - tHigh) / unitsPerBin + 1;
230  if(currentBin_ > 0) currentBin_ = nbinsX;
231  }
232  else if(_t < tLow){
233  if(currentBin_ > 0) return false; // no going back in time in case of cumulative history
234 
235  int maxBin(0);
236 
237  for(unsigned iME(0); iME < mes_.size(); iME++){
238  MonitorElement* me(mes_[iME]);
239 
240  bool filled(false);
241  int iMax(nbinsX + 1);
242  while(--iMax > 0 && !filled){
243  switch(kind_){
245  if(me->getBinContent(iMax) != 0) filled = true;
246  break;
248  if(me->getBinEntries(iMax) != 0) filled = true;
249  break;
251  for(int iy(1); iy <= me->getNbinsY(); iy++){
252  if(me->getBinContent(me->getTH1()->GetBin(iMax, iy)) != 0){
253  filled = true;
254  break;
255  }
256  }
257  break;
259  for(int iy(1); iy <= me->getNbinsY(); iy++){
260  if(me->getBinEntries(me->getTH1()->GetBin(iMax, iy)) != 0){
261  filled = true;
262  break;
263  }
264  }
265  break;
266  default:
267  break;
268  }
269  }
270 
271  if(iMax > maxBin) maxBin = iMax;
272  }
273 
274  if(_t < tLow - (nbinsX - maxBin) * unitsPerBin) return false;
275 
276  dBin = (_t - tLow) / unitsPerBin - 1;
277  }
278 
279  int start(dBin > 0 ? dBin + 1 : nbinsX + dBin);
280  int end(dBin > 0 ? nbinsX + 1 : 0);
281  int step(dBin > 0 ? 1 : -1);
282 
283  tLow += dBin * unitsPerBin;
284  tHigh += dBin * unitsPerBin;
285 
286  for(unsigned iME(0); iME < mes_.size(); iME++){
287  MonitorElement* me(mes_[iME]);
288 
289  me->getTH1()->GetXaxis()->SetLimits(tLow, tHigh);
290 
291  if((end - start) / step < 0){
292  me->Reset();
293  continue;
294  }
295 
296  me->setEntries(0.);
297  double entries(0.);
298 
299  switch(kind_){
301  {
302  int ix(start);
303  for(; ix != end; ix += step){
304  double binContent(me->getBinContent(ix));
305  entries += binContent;
306  me->setBinContent(ix - dBin, binContent);
307  me->setBinError(ix - dBin, me->getBinError(ix));
308  }
309  ix = end - dBin - 1 * step;
310  double lastContent(currentBin_ > 0 ? me->getBinContent(ix) : 0.);
311  double lastError(currentBin_ > 0 ? me->getBinContent(ix) : 0.);
312  for(ix += step; ix != end; ix += step){
313  me->setBinContent(ix, lastContent);
314  me->setBinError(ix, lastError);
315  }
316  }
317  break;
319  {
320  int ix(start);
321  for(; ix != end; ix += step){
322  double binEntries(me->getBinEntries(ix));
323  double binContent(me->getBinContent(ix));
324  entries += binEntries;
325  me->setBinEntries(ix - dBin, binEntries);
326  me->setBinContent(ix - dBin, binContent * binEntries);
327  if(binEntries > 0){
328  double rms(me->getBinError(ix) * std::sqrt(binEntries));
329  double sumw2((rms * rms + binContent * binContent) * binEntries);
330  me->setBinError(ix - dBin, std::sqrt(sumw2));
331  }
332  else
333  me->setBinError(ix - dBin, 0.);
334  }
335  ix = end - dBin;
336  for(; ix != end; ix += step){
337  me->setBinEntries(ix, 0.);
338  me->setBinContent(ix, 0.);
339  me->setBinError(ix, 0.);
340  }
341  }
342  break;
344  {
345  int ix(start);
346  int nbinsY(me->getNbinsY());
347  for(; ix != end; ix += step){
348  for(int iy(1); iy <= nbinsY; iy++){
349  int orig(me->getTH1()->GetBin(ix, iy));
350  int dest(me->getTH1()->GetBin(ix - dBin, iy));
351  double binContent(me->getBinContent(orig));
352  entries += binContent;
353  me->setBinContent(dest, binContent);
354  me->setBinError(dest, me->getBinError(orig));
355 
356  me->setBinContent(orig, 0.);
357  me->setBinError(orig, 0.);
358  }
359  }
360  ix = end - dBin - 1 * step;
361  std::vector<double> lastContent;
362  std::vector<double> lastError;
363  for(int iy(1); iy <= nbinsY; iy++){
364  lastContent.push_back(currentBin_ > 0 ? me->getBinContent(ix, iy) : 0.);
365  lastError.push_back(currentBin_ > 0 ? me->getBinError(ix, iy) : 0.);
366  }
367  for(ix += step; ix != end; ix += step){
368  for(int iy(1); iy <= nbinsY; iy++){
369  int bin(me->getTH1()->GetBin(ix, iy));
370  me->setBinContent(bin, lastContent[iy - 1]);
371  me->setBinError(bin, lastError[iy - 1]);
372  }
373  }
374  }
375  break;
377  {
378  int ix(start);
379  int nbinsY(me->getNbinsY());
380  for(; ix != end; ix += step){
381  for(int iy(1); iy <= nbinsY; iy++){
382  int orig(me->getTH1()->GetBin(ix, iy));
383  int dest(me->getTH1()->GetBin(ix - dBin, iy));
384  double binEntries(me->getBinEntries(orig));
385  double binContent(me->getBinContent(orig));
386  entries += binEntries;
387  me->setBinEntries(dest, binEntries);
388  me->setBinContent(dest, binContent * binEntries);
389  if(binEntries > 0){
390  double rms(me->getBinError(orig) * std::sqrt(binEntries));
391  double sumw2((rms * rms + binContent * binContent) * binEntries);
392  me->setBinError(dest, std::sqrt(sumw2));
393  }
394  else
395  me->setBinError(dest, 0.);
396  }
397  }
398  ix = end - dBin;
399  for(; ix != end; ix += step){
400  for(int iy(1); iy <= nbinsY; iy++){
401  int bin(me->getTH1()->GetBin(ix, iy));
402  me->setBinEntries(bin, 0.);
403  me->setBinContent(bin, 0.);
404  me->setBinError(bin, 0.);
405  }
406  }
407  }
408  break;
409  default:
410  break;
411  }
412 
413  me->setEntries(entries);
414  }
415 
416  return true;
417  }
418 
419 }
MonitorElement::Kind kind_
Definition: MESet.h:134
void setBinContent(int binx, double content)
set content of bin (1-D)
virtual void setAxisTitle(std::string const &, int=1)
Definition: MESet.cc:100
int findBin(DetId const &, double, double=0.) const override
Definition: MESetTrend.cc:141
MESet & operator=(MESet const &) override
Definition: MESetEcal.cc:39
MESetTrend(std::string const &, binning::ObjectType, binning::BinningType, MonitorElement::Kind, binning::AxisSpecs const *=0, binning::AxisSpecs const *=0)
Definition: MESetTrend.cc:7
def copy(args, dbName)
virtual void checkME_(unsigned _iME) const
Definition: MESet.h:115
Ecal readout channel identification [32:20] Unused (so far) [19:13] DCC id [12:6] tower [5:3] strip [...
TH1 * getTH1() const
bool shift_(unsigned)
Definition: MESetTrend.cc:192
void fill(DetId const &, double, double=1., double=1.) override
Definition: MESetTrend.cc:94
std::vector< MonitorElement * > mes_
Definition: MESet.h:129
void throw_(std::string const &_message) const
Definition: MESet.h:124
T sqrt(T t)
Definition: SSEVec.h:18
void setBinError(int binx, double error)
set uncertainty on content of bin (1-D)
void setEntries(double nentries)
set # of entries
void Reset()
reset ME (ie. contents, errors, etc)
virtual void fill_(unsigned, int, double)
Definition: MESet.cc:278
bin
set the eta bin as selection string.
Definition: DetId.h:18
MESet * clone(std::string const &="") const override
Definition: MESetTrend.cc:46
unsigned findPlotIndex(ObjectType, DetId const &)
double getBinError(int binx) const
get uncertainty on content of bin (1-D) - See TH1::GetBinError for details
int getNbinsY() const
get # of bins in Y-axis
bool active_
Definition: MESet.h:138
double getBinContent(int binx) const
get content of bin (1-D)
void book(DQMStore::IBooker &) override
Definition: MESetTrend.cc:56
double getBinEntries(int bin) const
get # of bin entries (for profiles)
MESet & operator=(MESet const &) override
Definition: MESetTrend.cc:33
binning::ObjectType otype_
Definition: MESet.h:132
virtual const_iterator end() const
Definition: MESet.h:286
step
binning::AxisSpecs const * xaxis_
Definition: MESetEcal.h:68
binning::BinningType btype_
Definition: MESet.h:133
std::string path_
Definition: MESet.h:131
void setBinEntries(int bin, double nentries)
set # of bin entries (to be used for profiles)
void book(DQMStore::IBooker &) override
Definition: MESetEcal.cc:69