""" A class for building histograms incrementally. """
import numpy as np
from collections import defaultdict


class RHist():
    """ 
    A class for calculating histograms where the bin size 
    and location is set by rounding the input (i.e. use <decimals>) but 
    where the number and range of bins is determined by the data. 
    
    As a result, you need only know in advance the approximate scale 
    your data will take, i.e. the precision you're interested in.

    There are a few methods that return useful statistics.

    <name> is a unique identifier for this histogram.

    <decimals> is an integer specifying the number of decimal places.
    Negative numbers behave as expected.  
    """

    def __init__(self,name,decimals=1):
        self.decimals = decimals
        self.name = name

        self.h = defaultdict(int)
        self.h_norm = None


    def add(self,x):
        """ Add <x>, a data point, to the histogram """

        # Do type checking here?
        self.h[np.round(x,self.decimals)] += 1


    def norm(self):