ATLAS Offline Software
TBProfiler.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 //#include <iostream>
7 
8 template<typename T>
9 TBProfiler<T>::TBProfiler()
10  : m_channelFill(false)
11  , m_skipFlag(true)
12  , m_deltaBin()
13 { }
14 
15 template<typename T>
16 TBProfiler<T>::TBProfiler(size_t theSize)
17  : m_channelFill(true)
18  , m_deltaBin()
19 {
20  m_skipFlag = ! this->setupAction(theSize);
21 }
22 
23 template<typename T>
24 TBProfiler<T>::TBProfiler(size_t storeSize, const T& lowerLimit,
25  const T& upperLimit)
26  : m_channelFill(false),
27  m_deltaBin()
28 {
29  if ( storeSize > 0 && upperLimit > lowerLimit )
30  {
31  m_binLimits.resize(storeSize+1,0);
32  m_binLimits[0] = lowerLimit;
33  m_deltaBin = ( upperLimit - lowerLimit ) / ((T)storeSize);
34  for ( unsigned int i=1; i<m_binLimits.size(); i++ )
35  {
36  m_binLimits[i] = m_binLimits[i-1] + m_deltaBin;
37  }
38  m_skipFlag = ! this->setupAction(storeSize);
39  }
40  else
41  {
42  m_skipFlag = true;
43  }
44 }
45 
46 template<typename T>
47 TBProfiler<T>::~TBProfiler()
48 { }
49 
50 template<typename T>
51 void TBProfiler<T>::addData(const T& theChannel, const T& theData)
52 {
53  if ( ! m_skipFlag && ! m_channelFill )
54  {
55  size_t theIndex;
56  if ( ( theIndex = this->getIndex(theChannel) ) != size_t(-1) )
57  {
58  this->addData(theIndex,theData);
59  }
60  }
61 }
62 
63 template<typename T>
64 void TBProfiler<T>::addData(const std::vector<T>& theData)
65 {
66  if ( m_skipFlag ) return;
67 
68  for ( size_t i=0; i<std::min(theData.size(),m_sum.size()); i++ )
69  {
70  m_sum[i] += theData[i];
71  m_sumOfSquares[i] += theData[i] * theData[i];
72  m_entries[i]++;
73  }
74 }
75 
76 template<typename T>
77 
78 void TBProfiler<T>::addData(size_t theIndex,const T& theData)
79 {
80  if ( m_skipFlag ) return;
81 
82  if ( theIndex < m_sum.size() )
83  {
84  m_sum[theIndex] += theData;
85  m_sumOfSquares[theIndex] += theData * theData;
86  m_entries[theIndex]++;
87  }
88 }
89 
90 template<typename T>
91 bool TBProfiler<T>::getAverages(std::vector<T>& theAverages) const
92 {
93  if ( m_skipFlag ) return false;
94 
95  size_t changeCtr = 0;
96  for ( size_t i=0; i<std::min(theAverages.size(),m_sum.size()); i++ )
97  {
98  if ( m_entries[i] > 0 )
99  {
100  changeCtr++;
101  theAverages[i] = m_sum[i] / ((T)m_entries[i]);
102  }
103  }
104  return changeCtr > 0;
105 }
106 
107 template<typename T>
108 bool TBProfiler<T>::getAverages(size_t theIndex, T& theAverage) const
109 {
110  if ( m_skipFlag ) return false;
111 
112  bool checkOut;
113  if ( (checkOut = ( theIndex < m_sum.size() && m_entries[theIndex] > 0 ) ) )
114  {
115  theAverage = m_sum[theIndex]/((T)m_entries[theIndex]);
116  }
117  return checkOut;
118 }
119 
120 template<typename T>
121 bool TBProfiler<T>::getStandardDevs(std::vector<T>& theVariances) const
122 {
123  if ( m_skipFlag ) return false;
124 
125  size_t changeCtr = 0;
126  for ( size_t i=0; i<std::min(theVariances.size(),m_sum.size()); i++ )
127  {
128  if ( m_entries[i] > 0 )
129  {
130  changeCtr++;
131  T theDiff = m_sumOfSquares[i] - m_sum[i] * m_sum[i];
132  theVariances[i] = theDiff >=0
133  ? sqrt(theDiff) : -sqrt(fabs(theDiff));
134  }
135  }
136  return changeCtr > 0;
137 }
138 
139 template<typename T>
140 bool TBProfiler<T>::getStandardDevs(size_t theIndex,T& theVariance) const
141 {
142  if ( m_skipFlag ) return false;
143 
144  bool checkOut;
145  if (( checkOut = ( theIndex < m_sum.size() && m_entries[theIndex] > 0 ) ))
146  {
147  T theDev = m_sumOfSquares[theIndex] - m_sum[theIndex] * m_sum[theIndex];
148  theVariance = theDev > 0 ? sqrt(theDev) : -sqrt(fabs(theDev));
149  }
150  return checkOut;
151 }
152 
153 template<typename T>
154 bool TBProfiler<T>::getErrors(std::vector<T>& theErrors) const
155 {
156  if ( m_skipFlag ) return false;
157 
158  size_t changeCtr = 0;
159  std::vector<T> theDevs;
160  if ( this->getStandardDevs(theDevs) )
161  {
162  for ( size_t i=0; i<std::min(theErrors.size(),m_sum.size()); i++ )
163  {
164  if ( m_entries[i] > 0 )
165  {
166  changeCtr++;
167  theErrors[i] = theDevs[i] / sqrt((T)m_entries[i]);
168  }
169  }
170  }
171  return changeCtr > 0;
172 }
173 
174 template<typename T>
175 bool TBProfiler<T>::getErrors(size_t theIndex, T& theError) const
176 {
177  if ( m_skipFlag ) return false;
178 
179  bool checkOut;
180  if (( checkOut = this->getStandardDevs(theIndex,theError) ))
181  {
182  theError /= sqrt((T)m_entries[theIndex]);
183  }
184  return checkOut;
185 }
186 
187 template<typename T>
188 bool TBProfiler<T>::getBinEntries(std::vector<size_t>& theEntries) const
189 {
190  if ( m_skipFlag ) return false;
191 
192  theEntries.clear();
193  theEntries.insert(++theEntries.begin(),m_entries.begin(),m_entries.end());
194 
195  return theEntries.size() > 0;
196 }
197 
198 template<typename T>
199 bool TBProfiler<T>::getBinEntries(size_t theIndex,size_t& theEntries) const
200 {
201  if ( m_skipFlag ) return false;
202 
203  if ( theIndex < m_entries.size() )
204  {
205  theEntries = m_entries[theIndex];
206  return true;
207  }
208  else
209  {
210  return false;
211  }
212 }
213 
214 
215 template<typename T>
216 const std::vector<T>& TBProfiler<T>::getSums() const
217 {
218  return m_sum;
219 }
220 
221 template<typename T>
222 const std::vector<T>& TBProfiler<T>::getSumOfSquares() const
223 {
224  return m_sumOfSquares;
225 }
226 
227 template<typename T>
228 const std::vector<size_t>& TBProfiler<T>::getEntries() const
229 {
230  return m_entries;
231 }
232 
233 template<typename T>
234 size_t TBProfiler<T>::getNumberOfChannels() const
235 {
236  return m_binLimits.size()-1;
237 }
238 
239 template<typename T>
240 T TBProfiler<T>::getChannelWidth() const
241 {
242  return m_deltaBin;
243 }
244 
245 template<typename T>
246 T TBProfiler<T>::getMinChannel() const
247 {
248  return ! m_channelFill && ! m_skipFlag
249  ? m_binLimits[0]
250  : 0;
251 }
252 
253 template<typename T>
254 T TBProfiler<T>::getMaxChannel() const
255 {
256  return ! m_channelFill && ! m_skipFlag
257  ? m_binLimits[m_binLimits.size()-1]
258  : 0;
259 }
260 
261 template<typename T>
262 size_t TBProfiler<T>::getIndex(const T& theChannel) const
263 {
264  if ( m_skipFlag ) return size_t(-1);
265 
266  return
267  ( theChannel >= m_binLimits[0] ||
268  theChannel < m_binLimits[m_binLimits.size()-1] )
269  ? (size_t)floor(( theChannel - m_binLimits[0] ) / m_deltaBin)
270  : size_t(-1);
271 }
272 
273 template<typename T>
274 bool TBProfiler<T>::setupAction(size_t theSize)
275 {
276  if ( theSize > 0 )
277  {
278  m_sum.resize(theSize,0);
279  m_sumOfSquares.resize(theSize,0);
280  m_entries.resize(theSize,0);
281  return true;
282  }
283  else
284  {
285  return false;
286  }
287 }