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