ATLAS Offline Software
rmsFrac.cxx
Go to the documentation of this file.
1 
12 #include <cmath>
13 
14 #include "rmsFrac.h"
15 
16 namespace generate {
17 
18 
19 
20 double GetEntries(TH1D* h, int ilow, int ihi) {
21  double sum = 0;
22  for ( int i=ilow ; i<=ihi ; i++ ) sum += h->GetBinContent(i);
23  return sum;
24 }
25 
26 double GetEntries(TH1D* h) {
27  return GetEntries(h,1,h->GetNbinsX());
28 }
29 
30 
31 
32 void getRange(TH1D* s, int imax, double frac,
33  int& lowerbin, int& upperbin, double& lowerfrac, double& upperfrac ) {
34 
35  upperbin = imax;
36  lowerbin = imax;
37 
38  upperfrac = 0;
39  lowerfrac = 0;
40 
41  double entries = GetEntries(s,0,int(s->GetNbinsX())+1);
42 
43  if ( entries>0 ) {
44 
45  double sumn = s->GetBinContent(imax);
46  double tsum = s->GetBinContent(imax);
47 
48  int i=1;
49  while ( true ) {
50 
51  const int upperbin_i = imax+i;
52  const int lowerbin_i = imax-i;
53 
54  if ( upperbin_i>s->GetNbinsX() || lowerbin_i<1 ) break;
55 
56  tsum += s->GetBinContent(upperbin_i) + s->GetBinContent(lowerbin_i);
57 
58  // std::cout << i << " frac: " << lowersum
59  // << "\tx " << s->GetBinCenter(lowerbin)
60  // << "\t " << s->GetBinCenter(upperbin)
61  // << "\ttsum " << tsum
62  // << "\tentries " << entries
63  // << std::endl;
64 
65  if ( tsum>=entries*frac ) break;
66 
67  sumn = tsum;
68 
69  lowerfrac = sumn/entries;
70 
71  upperbin = upperbin_i;
72  lowerbin = lowerbin_i;
73 
74  i++;
75  }
76 
77  upperfrac = tsum/entries;
78  }
79 
80 }
81 
82 
83 
84 double findMean(TH1D* s, double frac=0.95) {
85 
86  const bool interpolate_flag = true;
87 
88  // double entries = GetEntries(s,0,int(s->GetNbinsX())+1);
89  double entries = s->GetEffectiveEntries(); // s,0,int(s->GetNbinsX())+1);
90 
91  // std::cout << "\nfindMean() " << s->GetName() << "\tentries: " << entries << std::endl;
92 
96  if ( (1-frac)*entries<1 ) return 0;
97 
98  s->GetXaxis()->SetRange(1,s->GetNbinsX());
99 
100  double mean = s->GetMean();
101  double meane = s->GetMeanError();
102 
103  int upperbin = 0;
104  int lowerbin = 0;
105 
106  double upperfrac = 0;
107  double lowerfrac = 0;
108 
109  int imax = 0;
110 
111  int it=0;
112  constexpr int it_max=20;
113  constexpr int it_max_report=20;
115  for ( ; it<it_max ; it++ ) {
116 
117 
118  // std::cout << it << "\tmean " << mean << " +- " << meane << std::endl;
119 
120  imax = s->GetXaxis()->FindBin(mean);
121 
122  upperbin = imax;
123  lowerbin = imax;
124 
125  upperfrac = 0;
126  lowerfrac = 0;
127 
128  getRange( s, imax, frac, lowerbin, upperbin, lowerfrac, upperfrac );
129 
130  s->GetXaxis()->SetRange(lowerbin, upperbin);
131 
132  double m = s->GetMean();
133  double me = s->GetMeanError();
134 
135  if ( it>0 ) {
136  if ( mean==m ||
137  std::fabs(mean-m)<me*1e-5 ||
138  std::fabs(mean-m)<meane*1e-5 ) {
139  mean = m;
140  meane = me;
141  // std::cout << "break after " << it << " iterations" << std::endl;
142  break;
143  }
144  }
145  //cppcheck-suppress oppositeInnerCondition
146  if ( it>=it_max_report ) {
147  std::cerr << s->GetName() << "\tMax iterations " << it << " reached" << std::endl;
148  }
149 
150  mean = m;
151  meane = me;
152 
153  }
154 
155  // std::cout << s->GetName() << "\tmean " << mean << " +- " << meane << std::endl;
156 
157  if ( interpolate_flag ) {
158 
159  s->GetXaxis()->SetRange(lowerbin-1, upperbin+1);
160 
161  double m = s->GetMean();
162  // double me = s->GetMeanError();
163 
164  s->GetXaxis()->SetRange(1,s->GetNbinsX());
165 
166  if ( upperfrac==lowerfrac ) return 0;
167 
168  double inter_mean = mean + (frac-lowerfrac)*(m-mean)/(upperfrac-lowerfrac);
169 
170  return inter_mean;
171  }
172  else {
173  s->GetXaxis()->SetRange(1,s->GetNbinsX());
174  return mean;
175  }
176 
177 }
178 
179 
180 
181 int findMax(TH1D* s) {
182  int imax = 1;
183  for ( int i=2 ; i<=s->GetNbinsX() ; i++ ) {
185  if ( s->GetBinContent(i)>s->GetBinContent(imax) ) imax = i;
186  }
187  return imax;
188 }
189 
190 
191 
192 
193 double rmsFrac(TH1D* s, double frac, double mean) {
194 
195  double rms = 0;
196  double erms = 0;
197 
198  const bool interpolate_flag = true;
199 
200  if ( s==0 ) { std::cerr << "rmsFrac() histogram s = " << s << std::endl; return 0; }
201  if ( s->GetXaxis()==0) { std::cerr << "rmsFrac() histogram s->GetXaxis() not definied for histogram " << s->GetName() << std::endl; return 0; }
202 
203  // std::cout << "rmsFrac() " << s->GetName() << " " << GetEntries(s) << " " << GetEntries(s, 0, s->GetNbinsX()+1) << std::endl;
204 
205  // double entries = GetEntries(s,0,int(s->GetNbinsX())+1);
206  double entries = s->GetEffectiveEntries(); // s,0,int(s->GetNbinsX())+1);
207 
211  if ( (1-frac)*entries<1 ) return 0;
212 
213  s->GetXaxis()->SetRange(1,s->GetNbinsX());
214 
215  double m = mean;
216 
217  int imax = s->GetXaxis()->FindBin(m);
218 
219  // std::cout << "rmsFrac() mean " << m << " " << imax << " max " << s->GetBinCenter(findMax(s)) << " " << findMax(s) << std::endl;
220  // std::cout << "rmsFrac() entries: " << entries << "\t" << GetEntries(s) << "\t" << GetEntries(s,0,s->GetNbinsX()+1) << std::endl;
221 
222  int upperbin = imax;
223  int lowerbin = imax;
224 
225  double upperfrac = 0;
226  double lowerfrac = 0;
227 
228  if ( entries>0 ) {
229  getRange( s, imax, frac, lowerbin, upperbin, lowerfrac, upperfrac );
230  }
231 
232  // std::cout << "rmsFrac() " << s->GetName() << "\tlower bin " << lowerbin << "\t upper bin " << upperbin << std::endl;
233 
234  // if ( upperbin!=lowerbin ) {
235  if ( true ) {
236 
237  std::vector<double> stats;
238 
239  // std::cout << "rmsFrac() GetEntries " << s->GetName() << " " << GetEntries(s) << std::endl;
240 
241  if ( interpolate_flag ) { // && upperfrac!=lowerfrac ) {
242 
243  double rlower = 0;
244  double erlower = 0;
245  s->GetXaxis()->SetRange(lowerbin, upperbin);
246  rlower = s->GetRMS();
247  // std::cout << "rmsFrac() GetEntries " << s->GetName() << " " << GetEntries(s,lowerbin,upperbin)/sentries << "\trlower " << rlower << std::endl;
248 
249  double rupper = 0;
250  double erupper = 0;
251  s->GetXaxis()->SetRange(lowerbin-1, upperbin+1);
252  rupper = s->GetRMS();
253  // std::cout << "rmsFrac() GetEntries " << s->GetName() << " " << GetEntries(s,lowerbin-1,upperbin+1)/sentries << "\trupper " << rupper << std::endl;
254 
255  // std::cout << "rmsFrac() Range " << s->GetBinLowEdge(lowerbin) << " - " << s->GetBinLowEdge(upperbin+1) << std::endl;
256 
257  if ( upperfrac!=lowerfrac ) {
258  rms = rlower + (frac-lowerfrac)*(rupper-rlower)/(upperfrac-lowerfrac);
259  erms = erlower + (frac-lowerfrac)*(erupper-erlower)/(upperfrac-lowerfrac);
260  }
261  else {
262  rms = erms = 0;
263  }
264 
265  }
266  else {
267  s->GetXaxis()->SetRange(lowerbin, upperbin);
268  rms = s->GetRMS();
269  }
270  }
271 
272 
273  s->GetXaxis()->SetRange(1,s->GetNbinsX());
274 
275  return rms;
276 
277 }
278 
279 
280 double rmsFrac(TH1D* s, double frac) {
281  return rmsFrac(s, frac, findMean( s, frac ) );
282 }
283 
284 
285 double rms95(TH1D* s, double mean) {
286  double rms = rmsFrac(s, 0.95, mean);
287  // printf("rms95 %12.10lf %12.10lf inv %12.10lf\n", rms, rms/0.8711151572, rms*1.1479538518 );
288  // rms *= 1.1479538518;
289  s->GetXaxis()->SetRange(1,s->GetNbinsX());
290  return rms;
291 }
292 
293 double rms95(TH1D* s) {
294  double rms = rmsFrac(s, 0.95);
295  // printf("rms95 %12.10lf %12.10lf inv %12.10lf\n", rms, rms/0.8711151572, rms*1.1479538518 );
296  // rms *= 1.1479538518;
297  s->GetXaxis()->SetRange(1,s->GetNbinsX());
298  return rms;
299 }
300 
301 }
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
mean
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:254
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
skel.it
it
Definition: skel.GENtoEVGEN.py:396
generate::frac
const double frac
Definition: generate.cxx:295
trigbs_dumpHLTContentInBS.stats
stats
Definition: trigbs_dumpHLTContentInBS.py:91
generate::rms95
double rms95(TH1D *s, double mean)
get the fraction of the rms95 value with respect to the rms95 value of a gaussian
Definition: rmsFrac.cxx:285
generate::getRange
void getRange(TH1D *s, int imax, double frac, int &lowerbin, int &upperbin, double &lowerfrac, double &upperfrac)
Definition: rmsFrac.cxx:32
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
lumiFormat.i
int i
Definition: lumiFormat.py:85
imax
int imax(int i, int j)
Definition: TileLaserTimingTool.cxx:33
generate
Definition: generate.cxx:28
h
generate::GetEntries
double GetEntries(TH1D *h, int ilow, int ihi)
Definition: rmsFrac.cxx:20
beamspotnt.rms
rms
Definition: bin/beamspotnt.py:1266
entries
double entries
Definition: listroot.cxx:49
generate::findMax
int findMax(TH1D *s)
Definition: rmsFrac.cxx:181
generate::rmsFrac
double rmsFrac(TH1D *s, double frac, double mean)
Definition: rmsFrac.cxx:193
generate::findMean
double findMean(TH1D *s, double frac=0.95)
Definition: rmsFrac.cxx:84
rmsFrac.h