CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
UtilFunctions.cc
Go to the documentation of this file.
1 
10 #include <cmath>
11 #include <iostream>
12 
13 #include "TH1F.h"
14 #include "TProfile.h"
15 #include "TH2.h"
16 #include "TClass.h"
17 #include "TAxis.h"
19 
20 
21 namespace ecaldqm {
22 
23  // functions implemented here are not universal in the sense that
24  // the input variables are changed due to efficiency of memory usage.
25 
26 
27  // calculated time intervals and bin locations for time varing profiles
28 
29  void calcBins(int binWidth, int divisor, long int start_time, long int last_time, long int current_time,
30  long int & binDiff, long int & diff) {
31 
32  // changing arguments : binDiff, diff
33 
34  // binWidth : time interval
35  // divisor : time unit - for minute case divisor is 60 and for hour case 3600
36  // start_time : initial time when the job started
37  // last_time : the last updated time before calling the current "analyze" function
38  // current_time : the current time inside "analyze" fucntion
39  // binDiff : the bin difference for the current time compared to the bin location of the last time
40  // diff : time difference between the current time and the last time
41 
42  long int diff_current_start = current_time - start_time;
43  long int diff_last_start = last_time - start_time;
44 
45  // --------------------------------------------------
46  // Calculate time interval and bin width
47  // --------------------------------------------------
48 
49  binDiff = diff_current_start/divisor/binWidth - diff_last_start/divisor/binWidth;
50  diff = (current_time - last_time)/divisor;
51 
52  if(diff >= binWidth) {
53  while(diff >= binWidth) diff -= binWidth;
54  }
55 
56  } // calcBins
57 
58  void shift(TH1 *h, Directions d, int bins)
59  {
60  if(!bins || !h) return;
61  if(h->GetXaxis()->IsVariableBinSize()) return;
62 
63  if(bins < 0){
64  bins = -bins;
65  d = d==kRight ? kLeft : kRight;
66  }
67 
68  if(!h->GetSumw2()) h->Sumw2();
69  int nBins = h->GetXaxis()->GetNbins();
70  if(bins >= nBins){
71  h->Reset();
72  return;
73  }
74 
75  // the first bin goes to underflow
76  // each bin moves to the right
77 
78  int firstBin, lastBin, bound, increment;
79  switch(d){
80  case kRight:
81  firstBin = nBins + 1;
82  lastBin = 0;
83  bound = bins;
84  increment = -1;
85  break;
86  case kLeft:
87  firstBin = 0;
88  lastBin = nBins + 1;
89  bound = nBins - bins + 1;
90  increment = 1;
91  break;
92  default:
93  return;
94  }
95 
96  int shift = increment * bins;
97 
98  if( h->IsA() == TClass::GetClass("TProfile") ){
99 
100  TProfile *p = static_cast<TProfile *>(h);
101 
102  // by shifting n bin to the left, the number of entries are
103  // reduced by the number in n bins including the underflow bin.
104  double nentries = p->GetEntries();
105  for(int i = firstBin; i != firstBin + shift; i += increment) nentries -= p->GetBinEntries(i);
106  p->SetEntries(nentries);
107 
108  TArrayD* sumw2 = p->GetSumw2();
109 
110  for(int i = firstBin; i != bound; i += increment){
111  // GetBinContent returns binContent/binEntries
112  p->SetBinContent( i, p->GetBinContent( i+shift ) * p->GetBinEntries( i+shift ) );
113  p->SetBinEntries( i, p->GetBinEntries( i+shift ) );
114  sumw2->SetAt( sumw2->GetAt( i+shift ), i );
115  }
116 
117  for(int i = bound; i != lastBin + increment; i += increment){
118  p->SetBinContent( i, 0 );
119  p->SetBinEntries( i, 0 );
120  sumw2->SetAt( 0., i );
121  }
122 
123  }else if( h->InheritsFrom("TH2") ){
124 
125  TH2 *h2 = static_cast<TH2 *>(h);
126  int nBinsY = h2->GetYaxis()->GetNbins();
127 
128  // assumes sum(binContent) == entries
129  double nentries = h2->GetEntries();
130  for(int i = firstBin; i != firstBin + shift; i += increment)
131  for(int j=0 ; j<=nBinsY+1 ; j++) nentries -= h2->GetBinContent(i,j);
132 
133  h2->SetEntries(nentries);
134 
135  for(int i = firstBin; i != bound; i += increment)
136  for(int j = 0; j <= nBinsY + 1; j++)
137  h2->SetBinContent( i, j, h2->GetBinContent(i+shift, j) );
138 
139  for(int i = bound; i != lastBin + increment; i += increment)
140  for(int j = 0; j <= nBinsY + 1; j++)
141  h2->SetBinContent( i, j, 0 );
142 
143  }else if( h->InheritsFrom("TH1") ){ // any other histogram class
144 
145  // assumes sum(binContent) == entries
146  double nentries = h->GetEntries();
147  for(int i = firstBin; i != firstBin + shift; i += increment) nentries -= h->GetBinContent(i);
148 
149  h->SetEntries(nentries);
150 
151  for(int i = firstBin; i != bound; i += increment)
152  h->SetBinContent( i, h->GetBinContent(i+shift) );
153 
154  for(int i = bound; i != lastBin + increment; i += increment)
155  h->SetBinContent( i, 0 );
156  }
157 
158 
159  }
160 
161  void shift2Right(TH1* h, int bins)
162  {
163  shift(h, kRight, bins);
164  }
165 
166  void shift2Left(TH1* h, int bins)
167  {
168  shift(h, kLeft, bins);
169  }
170 
171  // shift axis of histograms keeping the bin contents
172 
173  void shiftAxis(TH1 *h, Directions d, double shift)
174  {
175  if( !h ) return;
176  TAxis *xax = h->GetXaxis();
177  if( h->GetXaxis()->IsVariableBinSize() ) return;
178 
179  double xmax = xax->GetXmax();
180  double xmin = xax->GetXmin();
181  int nbins = xax->GetNbins();
182 
183  if(d == kRight)
184  xax->Set(nbins, xmin - shift, xmax - shift);
185  else if(d == kLeft)
186  xax->Set(nbins, xmin + shift, xmax + shift);
187  }
188 
189  // get mean and rms of Y values from TProfile
190 
191  void getAverageFromTProfile(TProfile* p, double& mean, double& rms) {
192 
193  // changing arguments : mean, rms
194  mean = rms = 0.0;
195 
196  if(!p) return;
197 
198  int nbins = p->GetXaxis()->GetNbins();
199  double y = 0.0;
200  double y2 = 0.0;
201  for(int i=1; i<=nbins; i++){
202  y += p->GetBinContent(i);
203  y2 += y*y;
204  }
205  mean = y/nbins;
206  rms = std::sqrt(y2/nbins - mean*mean);
207 
208  } // getAverageFromTProfile
209 
210 
211  // get mean and rms based on two histograms' difference
212 
213  void getMeanRms(TObject* pre, TObject* cur, double& mean, double& rms) {
214 
215  // changing arguments : mean, rms
216 
217  mean = rms = 0.0;
218 
219  if(!cur) return;
220 
221  TString name(cur->IsA()->GetName());
222 
223  if(name.Contains("TProfile")) {
224  getAverageFromTProfile((TProfile*)cur,mean,rms);
225  }
226  else if(name.Contains("TH2")) {
227  if(pre) {
228  mean = ((TH2F*)cur)->GetEntries() - ((TH2F*)pre)->GetEntries();
229  if(mean < 0) return;
230  rms = std::sqrt(mean);
231  }
232  else {
233  mean = ((TH2F*)cur)->GetEntries();
234  if(mean < 0) return;
235  rms = std::sqrt(mean);
236  }
237  float nxybins = ((TH2F*)cur)->GetNbinsX()*((TH2F*)cur)->GetNbinsY();
238  if(nxybins < 1.) nxybins = 1.;
239  mean /= nxybins;
240  rms /= nxybins;
241  }
242  else if(name.Contains("TH1")) {
243  if(pre) {
244  ((TH1F*)pre)->Sumw2();
245  ((TH1F*)pre)->Add((TH1F*)pre,(TH1F*)cur,-1,1);
246  mean = ((TH1F*)pre)->GetMean();
247  rms = ((TH1F*)pre)->GetRMS();
248  }
249  else {
250  mean = ((TH1F*)cur)->GetMean();
251  rms = ((TH1F*)cur)->GetRMS();
252  }
253  }
254 
255  } // getMeanRms
256 
258 
259  // The cloned object, ret should be deleted after using it.
260 
261  TObject* ret = 0;
262 
263  if(!me) return ret;
264 
265  std::string title = histo + " Clone";
266  ret = (TObject*) (me->getRootObject()->Clone(title.c_str()));
267 
268  return ret;
269  }
270 
271 
272 }
273 
int i
Definition: DBlmapReader.cc:9
void getAverageFromTProfile(TProfile *p, double &mean, double &rms)
unsigned long long start_time
Definition: procUtils.cc:98
void shiftAxis(TH1 *h, Directions d, double shift)
void shift2Left(TH1 *h, int bins)
T sqrt(T t)
Definition: SSEVec.h:48
int j
Definition: DBlmapReader.cc:9
void shift(TH1 *h, Directions d, int bins)
Ecal Monitor Utility functions.
TObject * getRootObject(void) const
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
TObject * cloneIt(MonitorElement *me, std::string histo)
void calcBins(int binWidth, int divisor, long int start_time, long int last_time, long int current_time, long int &binDiff, long int &diff)
void shift2Right(TH1 *h, int bins)
void getMeanRms(TObject *pre, TObject *cur, double &mean, double &rms)