This file is indexed.

/usr/include/casacore/scimath/Mathematics/HistAcc.tcc is in casacore-dev 2.2.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//# HistAcc.cc: Statistics Accumulator
//# Copyright (C) 1996,1998,1999,2001,2003
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: aips2-request@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA
//#
//#
//# $Id$

#ifndef SCIMATH_HISTACC_TCC
#define SCIMATH_HISTACC_TCC


#include <casacore/scimath/Mathematics/HistAcc.h>
#include <casacore/casa/BasicMath/Math.h>
#include <casacore/casa/Arrays.h>
// #include <casacore/casa/Exceptions/Error.h>
#include <casacore/casa/iostream.h>
#include <casacore/casa/iomanip.h>

namespace casacore { //# NAMESPACE CASACORE - BEGIN

// Constructor: Fully automatic bin definition

template<class T> 
HistAcc<T>::HistAcc(const uInt n) {
    init();                         // bring into a known state
    initBuffer(n);                  // initialise temporary buffer
} 

// Constructor: Semi-automatic (give bin width only).  

template<class T> 
HistAcc<T>::HistAcc(const uInt n, const T width) {
    init();                         // bring into a known state
    itsUserDefinedBinWidth = width; // user-defined bin width
    initBuffer(n);                 // initialise temporary buffer
} 

// Constructor: Define bins fully:

template<class T> 
HistAcc<T>::HistAcc(const T low, const T high, const T width) {
    init();                        // bring into a known state
    defineBins(low,high,width);
    itsAutoDefineMode = False;
} 

// Constructor: copy an existing histogram (that):

template<class T> 
HistAcc<T>::HistAcc(const HistAcc<T>& that) {
    copy(that);
} 

// Initialise the histogram definition, i.e. bring it into a
// known state:

template<class T>
void HistAcc<T>::init ()
{
    reset();
    itsBinContents.resize(0); 
    itsBinHighLimit.resize(0);
    itsUserDefinedBinWidth = 0;
    initBuffer(0);
}

// Reset, i.e. set the contents of the histogram bins to zero
// (but do NOT change their definition!)

template<class T>
void HistAcc<T>::reset ()
{
    itsStatAcc.reset();
    for (uInt i=0; i<itsBinContents.nelements(); i++) {
	itsBinContents[i] = 0; 
    }
}

// Copy the attributes of the given accumulator:

template<class T>
void HistAcc<T>::copy (const HistAcc<T>& that)
{
    itsStatAcc = that.itsStatAcc;

    itsUserDefinedBinWidth = that.itsUserDefinedBinWidth;
    uInt n = that.itsBinContents.nelements();
    itsBinContents.resize(n); 
    itsBinHighLimit.resize(n);
    uInt i;
    for (i=0; i<n; i++) {
	itsBinContents[i] = that.itsBinContents[i];
	itsBinHighLimit[i] = that.itsBinHighLimit[i];
    }

    n = that.itsBuffer.nelements();
    initBuffer(n);
    itsBufferContents = that.itsBufferContents;
    for (i=0; i<itsBufferContents; i++) {
	itsBuffer[i] = that.itsBuffer[i];
    }
}

// Operator=: this = that

template<class T>
HistAcc<T>& HistAcc<T>::operator= (const HistAcc<T>& that)
{    
    copy(that);
    return *this;
}

//************************************************************************
// Initialise a temporary buffer to store input values from which the
// bin parameters can be determined automatically:

template<class T>
void HistAcc<T>::initBuffer (const uInt bufferLength)
{
    if (bufferLength > 0) {
	itsAutoDefineMode = True; 
	itsBuffer.resize(bufferLength);
    } else {
	itsAutoDefineMode = False; 
	itsBuffer.resize(0);
    }
    itsBufferContents = 0;
}

// Put a value into the temporary buffer. 
// If the buffer is full, define the histogram bins.

template<class T>
void HistAcc<T>::putBuffer (const T v)
{
    if (itsBufferContents < itsBuffer.nelements()) {
	itsBuffer[itsBufferContents] = v;
	itsBufferContents++;
    }

    if (itsBufferContents >= itsBuffer.nelements()) {
	autoDefineBins();     // define bin parameters
    }
}

// Transfer the values from the buffer to the (defined!) bins,
// and clear up the `automatic-mode' machinery.

template<class T>
void HistAcc<T>::clearBuffer ()
{
    itsAutoDefineMode = False;            // BEFORE put1!

    // Transfer values from buffer to histogram
    itsStatAcc.reset();
    for (uInt i=0; i<itsBufferContents; i++) {
	put1(itsBuffer[i]);
    }
    // Clear up the temporary buffer.
    initBuffer(0);
}

// Define the bin parameters `automatically', i.e. using the values in
// itsBuffer:

template<class T>
void HistAcc<T>::autoDefineBins ()
{
    if (itsBufferContents == 0) {         // no values in itsBuffer
	// ............?

    } else {
	// Calculate statistics of the values in the buffer 
	itsStatAcc.reset();            // reset statistics
	itsStatAcc.put(itsBuffer);     // accumulate buffer values

	// Calculate bin range from statistics:
	Double hw = itsStatAcc.getRms();  // w.r.t. mean
	hw *= 3;                          // 3 sigma?
	Double low = itsStatAcc.getMean();// lowest bin
	Double high = low;                // highest bin
	low -= hw;                        // mean - 3 rms
	high += hw;                       // mean + 3 rms

	// Calculate bin width:
	T width = itsUserDefinedBinWidth; // use if defined
	Int k = 0;
	if (width <= 0) {                 // if not defined
	    width = T((high - low)/11);   // default: 11 bins?     
	    if (width <= 0) {             // if still not OK
		width = 1;                // ....?
	    } else {
		// Truncate the width to decimal units (pretty):
		Double q = 10000000;
		Double q1;
		for (uInt i=0; i<15; i++) {
		    q /= 10;
		    q1 = q;
		    if (width < q1) {q1 = q/2;}
		    if (width < q1) {q1 = q/5;}
		    if (width >= q1) {
			k = Int((width+q1/2)/q1);      // truncate
			width = T(k * q1);
			break;                    // escape
		    }
		}
	    }
	}


	// Make bin centres multiples of the bin width (pretty):
	k = Int(high / width);            // nr of multiples  
	high = k * width;                 // adjust highest bin
	k = Int(low / width);             // nr of multiples  
	low = (k-1) * width;              // adjust highest bin
	
	// Go ahead:
	defineBins(T(low),T(high),width); // define the bins
	clearBuffer();                    // transfer values to bins
    }
}

// Define the bin parameters. Low and high represent the centres of the
// lowest and highest bins. The first and last bins contain nr of  
// `spurious' values, i.e. values that are either too small or too large
// for the defined bins.

template<class T> 
void HistAcc<T>::defineBins(const T low, const T high, const T width) {

    T v;
    uInt n = 0;
    for (v=low; v<high+width; v+=width) {
	n++;                              // count the bins
    }

    itsBinContents.resize(n+2);           // n regular + 2 spurious
    itsBinHighLimit.resize(itsBinContents.nelements());

    v = low - width/2;                    // low limit of lowest bin
    for (uInt i=0; i<n+2; i++) {
	itsBinHighLimit[i] = v;           // high limit of bin 
	itsBinContents[i] = 0;            // contents of bin (=0)
	v += width;
    } 
} 

//*************************************************************************
// Accumulate an Array of values (see ArrayMath.cc):

template<class T>
void HistAcc<T>::put(const Array<T>& v) {
    uInt ntotal = v.nelements();
    Bool vDelete;
    const T* vStorage = v.getStorage(vDelete);
    const T* vs = vStorage;
    while (ntotal--) {put1(*vs++);}
    v.freeStorage(vStorage, vDelete); 
}

// Accumulate a Block (simple vector) of values:

template<class T>
void HistAcc<T>::put(const Block<T>& v) {
    for (uInt i=0; i < v.nelements(); i++){
	put1(v[i]);
    }
}

// Accumulate a single value into the histogram:

template<class T>
void HistAcc<T>::put1(const T v)
{
    if (itsAutoDefineMode) {
	putBuffer(v);                // put into temporary buffer

    } else {                         // put into histogram bins
	itsStatAcc.put(v);           // accumulate statistics (all values!)
	for (uInt i=0; i<itsBinContents.nelements()-1; i++) {
	    if (v < itsBinHighLimit[i]) {
		itsBinContents[i]++; 
		return;              // escape
	    }
	}

	// If get to here, put in bin for high spurious values
	itsBinContents[itsBinContents.nelements()-1]++; 
    }
}
    
//**********************************************************************
// Result: get the internal Statistics accumulator, with the aim 
// of getting the histogram statistics. 
// Example: uInt count = h.getStatistics().getCount();

template<class T> 
const StatAcc<T>& HistAcc<T>::getStatistics() 
{
    if (itsAutoDefineMode) {
	itsStatAcc.reset();
	itsStatAcc.put(itsBuffer);
    }
    return itsStatAcc;            
}

// Get the nr of spurious values:

template<class T> 
uInt HistAcc<T>::getSpurious(uInt& nlow, uInt& nhigh)
{
    if (itsAutoDefineMode) {
	autoDefineBins();      
    }
    nlow = itsBinContents[0];                              // tooSmall
    nhigh = itsBinContents[itsBinContents.nelements()-1];  // tooLarge
    return nlow + nhigh;                             // total spurious
}

// Result: get the actual bin-width. Use the difference in high limit
// between the first two bins. 

template<class T> 
Fallible<T> HistAcc<T>::getBinWidth() const
{
    T width = itsBinHighLimit[1] - itsBinHighLimit[0];
    if (width <= 0) {
	return Fallible<T>();
    } else {
	return Fallible<T>(width);
    }
}

// Result: get the Median (= the 50-percentile) 

template<class T> 
Fallible<T> HistAcc<T>::getMedian ()
{
    return getPercentile(50.0);
}

// Result: get the n-percentile, i.e. the value which has n% of the
// input values below it. (the Median is the 50-percentile).

template<class T> 
Fallible<T> HistAcc<T>::getPercentile (const Float p)
{
    if (itsAutoDefineMode) {
	autoDefineBins();
    }
    Double target = itsStatAcc.getWtot() * p/100;  // target value

    uInt n1 = itsBinContents[0];          // spurious low
    if (n1 > target) {
	return Fallible<T>();             // not defined
    }
    // Go through the regular bins, excuding spurious high
    for (uInt i=1; i<itsBinContents.nelements()-1; i++) {
	n1 += itsBinContents[i];
	if (n1 > target) {
	    return getBinValue(i);        // OK
	} 
    }
    return Fallible<T>();             // none of the regular bins
}
 
// Result: get the centre value for the bin with the given index.
// For the bins for low and high `spurious' values, return a value
// that is a full histogram-width away from the extreme bins.

template<class T> 
Fallible<T> HistAcc<T>::getBinValue (const uInt index) const
{
    if (getBinWidth().isValid()) {
	T width = getBinWidth();
	if (index == 0) {
	    return Fallible<T>(itsBinHighLimit[0] - 
			       itsBinContents.nelements() * width);
	    
	} else if (index < itsBinContents.nelements()-1) { 
	    // Regular bins. The strange construct of using -1+0.5 for -0.5 
	    // is to get the correct result for integers.
	    return Fallible<T>(itsBinHighLimit[index] - width + width/2);
	    
	} else if (index == itsBinContents.nelements()-1) {
	    return Fallible<T>(itsBinHighLimit[0] + 
			       itsBinContents.nelements() * width * 2);
	    
	} else {
	    return Fallible<T>();
	}

    } else {
	return Fallible<T>();
    }
}

// Result: get the Histogram itself in two Blocks (simple vectors)
   template<class T> 
   Fallible<uInt> HistAcc<T>::getHistogram (Block<uInt>& binContents, 
    					    Block<T>& binValues) 
{    
    if (itsAutoDefineMode) {
	autoDefineBins();
    }
    uInt n = itsBinContents.nelements();      // nr of bins
    if (n>0) {
	binContents.resize(n-2);
	binValues.resize(n-2);
	for (uInt i=1; i<n-1; i++) {
	    binContents[i-1] = itsBinContents[i];
	    binValues[i-1] = getBinValue(i);
	}
        return Fallible<uInt>(n);
    } else {
	return Fallible<uInt>();               // error
    }
}


//*********************************************************************
// Remove bins with contents smaller than nmin.
// Remove all the `spurious' values, assuming that they are so widely 
// spaced that their average density is less than one/bin.

template<class T> 
void HistAcc<T>::emptyBinsWithLessThan (const uInt nmin) 
{
    if (itsAutoDefineMode) {
	autoDefineBins();
    }

    if (nmin > 1) {
	// Remove the spurious values:
	itsBinContents[0]=0;                          // low
	itsBinContents[itsBinContents.nelements()-1]=0; // high

	// Deal with the regular bins:
	itsStatAcc.reset();                    // reset accumulator
	Float wgt;
	for (uInt i=1; i<itsBinContents.nelements()-1; i++) {
	    if (itsBinContents[i] < nmin) {    // low-contents bin
		itsBinContents[i]=0;            // set to zero
	    } else {                            // above the limit
		// Re-calculate statistics:
		wgt = itsBinContents[i];       // weight
		itsStatAcc.put(getBinValue(i),wgt);
	    }
	}
    }
}


//**********************************************************************
// `Print' histogram to the given o-stream:

template<class T>
void HistAcc<T>::printHistogram (ostream& os, const String& caption)
{ 
    if (itsAutoDefineMode) {autoDefineBins();}

    ios::fmtflags flags = os.flags();         // save current setting
    os << " " << endl;               // skip line
    os << " Histogram: " << caption << endl; 
    uInt pv = 3;                     // precision for bin values
    uInt pc = 3;                     // precision for bin contents

    for (uInt i=1; i<itsBinContents.nelements()-1; i++) {
	setprecision(pv);
	os << "  bin value=" << setw(pv+4) << getBinValue(i);
	setprecision(pc);
	os << " : n=" << setw(pc+1) << itsBinContents[i];
	os << endl;
    }

    os << "  nTotal=" << itsStatAcc.getWtot();
    if (getBinWidth().isValid()) {
	os << "    binWidth=" << getBinWidth();
    } else {
	os << "    binWidth not defined";
    }
    os << endl;

    uInt nlow;
    uInt nhigh;
    uInt nsp = getSpurious(nlow, nhigh);
    os << "  nSpurious=" << nsp;
    os << "   nLow=" << nlow;
    if (itsStatAcc.getMin().isValid()) {
	os << "(vMin=" << itsStatAcc.getMin() << ")";
    } else {
	os << "(vMin= ..)";
    }
    os << "   nHigh=" << nhigh;
    if (itsStatAcc.getMax().isValid()) {
	os << "(vMax=" << itsStatAcc.getMax() << ")";
    } else {
	os << "(vMax= ..)";
    }
    os << endl;

    os << " ";
    if (itsStatAcc.getMean().isValid()) {
	os << " mean=" << itsStatAcc.getMean();
    } else {
	os << " mean= ..";
    }
    if (getMedian().isValid()) {
	os << "   median=" << getMedian();
    } else {
	os << "   median= ..";
    }
    if (itsStatAcc.getRms().isValid()) {
	os  << "  rms=" << itsStatAcc.getRms();
    } else {
	os << "   rms= ..";
    }
    os << endl;

    os.flags(flags);                  // restore original setting
}


//**********************************************************************






} //# NAMESPACE CASACORE - END


#endif