CMS 3D CMS Logo

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