-3

I have a python list which I use for storeing histograms. Each histogram is itself a list with n entries corresponding to the bins of the histogram. Each bin itself is again a list of three numbers and two list:

histograms = [ h1, h2 , h3, ... ],
h1 = [ "name", [ bin_0, bin_1, ..., bin_n ]],
bin_i = [ num_1, num_2, num_2, [ likelihood_sig ], [ likelihood_bkg ] ],
likelihood_x = [ bin_x_0 , ... , bin_x_10 ],

where bin_i/x and num_i are floats. In my code I scan over a data file and fill the histograms. Finding the right bins and filling the num_i's works. However, filling the bin_x's doesn't. Whatever I write into likelihood_sig is automatically (feels like magic to me :P) written into likelihood_bkg, too and vice versa.

Here is the part of the code where I fill the data into the list-structure (there are a lot of print statements as I tried to figure out whats going wrong...):

# 1d histograms
for histogram in self._histograms_1d:
    # definition is a string with the name of the histogram
    definition = histogram[0].strip().split()

    # particles is a list of a list with four floats
    # particles = [ p1, p2, ... ] , p1 = [ x0, x1, x2, x3 ]

    actual_bin = self.which_bin_1d(particles,definition)
    # add the data (dsig_s,dsig_b) to the correct bin (works)
    histogram[1][actual_bin][1]         = histogram[1][actual_bin][1]         + dsig_s
    histogram[1][actual_bin][2]         = histogram[1][actual_bin][2]         + dsig_b
    # dq_bin = int ( log ( 1 + dsig_s/dsig_b) // binning_size (=0.5) ) ... works
    # add data to likelihood (doesn't work ... )
    my_debug_temp2 =  histogram[1][actual_bin][4][dq_bin]
    my_debug_temp1 =  histogram[1][actual_bin][3][dq_bin] + dsig_s
    histogram[1][actual_bin][3][dq_bin] = my_debug_temp1
    histogram[1][actual_bin][4][dq_bin] = my_debug_temp2
    my_debug_temp1 =  histogram[1][actual_bin][3][dq_bin]
    my_debug_temp2 =  histogram[1][actual_bin][4][dq_bin] + dsig_b
    histogram[1][actual_bin][4][dq_bin] = my_debug_temp2
    histogram[1][actual_bin][3][dq_bin] = my_debug_temp1
    # in the following print statement I find histogram[1][actual_bin][4][dq_bin] = histogram[1][actual_bin][4][dq_bin] (??? independent of my_debug_temp2 and my_debug_temp1 !!)
    print histogram[1][actual_bin][3]
    print histogram[1][actual_bin][4]

Would be nice if somebody knows how to handle this or tell me if I mis-use some Python statements.

edit: cut down on print's

edit: init phase of the histograms

This is the part of the code where I define the histogram structure. Then in the above part the are filled. They are not used anywhere else.

#================================================================
# load cuts from cut file
def load_1d(self):
    for line in open(self._histogram_1d_file):
        if line.strip()[:1]!='#' and len(line.strip())>0:
            #print line.strip()
            words = line.strip().split()
            nbins = int(words[1])
            histogram = []
            dq_vec = []
            for i in range(0,self._num_dq_bins):
                dq_vec.append(0.0)
            for i in range(0,nbins):
                bin_i=[0,0,0,dq_vec,dq_vec]
                histogram.append(bin_i)
            self._histograms_1d.append([line.strip(),histogram])
    return True
#================================================================
  • 1
    Can you cut back on the `print` statements here? They are obscuring the actual code now. – Martijn Pieters Jan 27 '14 at 13:10
  • 3
    And most likely you inserted a list reference somewhere that is *not* a copy of the original, just another reference to the same list. – Martijn Pieters Jan 27 '14 at 13:10
  • 2
    python 2.7 bug, for sure. – wim Jan 27 '14 at 13:11
  • 1
    The bug is in code you haven't showed us, not in the mass of unreadable code you did show. – Wooble Jan 27 '14 at 13:11
  • hi there, thanks a lot for the comments. Martin, how would it look like if I mixed up the references? I dont' use this list in another context, except for initialization. I will add these lines, too – user3240157 Jan 27 '14 at 13:16

1 Answers1

1

I bet your likelihood_sig and likelihood_bkg point to the same list object under different names. You need to fix the initialization code to instantiate another list object for the latter.

a = [1,2,3]
b = a
b[1] = 20
a[1] #=> 20 because a and b point to the same list object

a = [1,2,3]
b = a[:]
b[1] = 20
a[1] #=> 2

Here a[:] is a shorthand for a[0:len(a)] which produces a (shallow) copy of a.

Community
  • 1
  • 1
nodakai
  • 7,773
  • 3
  • 30
  • 60