+ All Categories
Home > Documents > Lab #1: Photon counting & the statistics of light

Lab #1: Photon counting & the statistics of light

Date post: 25-Nov-2021
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
12
AST325/326 (Fall 2016) Lab #1 Lab #1: Photon counting & the statistics of light Lab report is due Monday, 2016 October 3, before 4:09PM EDT “It is not knowledge, but the act of learning, not possession but the act of getting there, which grants the greatest enjoyment.” – Carl Friedrich Gauss (1777 – 1885) 1. Overview This handout provides a description of the activities that we will explore in the first lab. Because we use this first lab to introduce many new skills, especially Python programming, this handout is a straightforward guide that your lab group should follow sequentially. 1.1 Schedule This is a three-week lab with activities and lectures on September 12, 19 and 26. Your lab report is due on October 3, before 4:09 pm. For class on September 19 you should have progressed to steps 1 and 2 (see §1.4). On September 26 you should be prepared to discuss steps 3–5 during the “Group-Led Discussion”. 1.2 Goals Explore physical limitations on the detection of light. Investigate how precisely brightness can be specified, and what determines that precision. 1.3 Reading assignments (available on line on course website) Linux o To master the first lab you will need basic Linux system skills so that you can collect data and manipulate files. Python o Work through the Python/IPython tutorials to learn how to use this powerful programming language to compute statistical quantities and make plots. Check out the lab we page and “Getting started” at: http://www.scipy.org/Getting_Started Fundamental Python references are: Numerical computing: http://numpy.scipy.org/ Plotting: http://matplotlib.sourceforge.net/ Document preparation o You can use the TeX/LaTeX document preparation system to create publication quality reports. Download the examples and make sure that you can generate PDF output. You can then use this example as a template for your first report. Skim the statistics handout on the class web page. 1.4 Key steps You will execute six key steps in this lab: 1. Collect data from the photon counting experiment and use Python to make plots of the counts per sample versus time. 2. Plot histograms to visualize the statistical properties of different data of sets.
Transcript
Page 1: Lab #1: Photon counting & the statistics of light

AST325/326 (Fall 2016) Lab #1

Lab #1: Photon counting & the statistics of light Lab report is due Monday, 2016 October 3, before 4:09PM EDT

“It is not knowledge, but the act of learning, not possession but the act of getting there, which

grants the greatest enjoyment.” – Carl Friedrich Gauss (1777 – 1885) 1 . Overview This handout provides a description of the activities that we will explore in the first lab. Because we use this first lab to introduce many new skills, especially Python programming, this handout is a straightforward guide that your lab group should follow sequentially. 1.1 Schedule This is a three-week lab with activities and lectures on September 12, 19 and 26. Your lab report is due on October 3, before 4:09 pm. For class on September 19 you should have progressed to steps 1 and 2 (see §1.4). On September 26 you should be prepared to discuss steps 3–5 during the “Group-Led Discussion”. 1.2 Goals Explore physical limitations on the detection of light. Investigate how precisely brightness can be specified, and what determines that precision. 1.3 Reading assignments (available on line on course website)

• Linux o To master the first lab you will need basic Linux system skills so that you can

collect data and manipulate files. • Python

o Work through the Python/IPython tutorials to learn how to use this powerful programming language to compute statistical quantities and make plots. Check out the lab we page and “Getting started” at:

• http://www.scipy.org/Getting_Started Fundamental Python references are:

• Numerical computing: http://numpy.scipy.org/ • Plotting: http://matplotlib.sourceforge.net/

• Document preparation o You can use the TeX/LaTeX document preparation system to create publication

quality reports. Download the examples and make sure that you can generate PDF output. You can then use this example as a template for your first report.

• Skim the statistics handout on the class web page. 1.4 Key steps You will execute six key steps in this lab:

1. Collect data from the photon counting experiment and use Python to make plots of the counts per sample versus time.

2. Plot histograms to visualize the statistical properties of different data of sets.

Page 2: Lab #1: Photon counting & the statistics of light

2

AST325/326 (Fall 2016) Lab #1

3. Compute the mean and standard deviation for your samples and investigate the variability of the count rate. Explore how the mean and standard deviation vary as the mean counts per sample increases.

4. Compare the observed histograms with the theoretical Poisson probability distribution function.

5. For data sets comprising multiple sequences compute the mean of the mean (MOM) and the standard deviation of the mean (SDOM). Explore show the MOM and the SDOM vary with the length of the sequences.

6. Write your lab report. 2 . Getting started: collecting data from the photon counter This lab activity is based on a light detector that can record the arrival of individual photons. The detector, known as a photomultiplier tube (PMT), is very sensitive, and must be treated with care. Before you modify anything associated with the PMT make sure that you have read and understood the PMT Operation Note located in the Experiments section of the course web page:

http://www.astro.utoronto.ca/~astrolab/ Do not expose the PMT to direct room light! Do not expose to light so that the count rate is > 2 MHz (one million counts per second). To determine the count rate, divide the counts per sample by the sample time, e.g., 1000 counts per sample when the sample time is 0.001 corresponds to 1,000,000 counts per second or 1 MHz. The first step is to acquire a time sequence of digitized data and save the data as a file. Log into one of the Linux workstations. Make a new folder or sub-directory (using mkdir) for your project % mkdir lab1 % cd lab1

(Commands that you type are shown in Courier font. A % indicates the Linux prompt—do not type an extra %!) Now invoke IPython at the Unix prompt. (You can cut and paste from this PDF document to make sure that you don’t have any typos.)

% ipython Enthought Python Distribution -- www.enthought.com

Python 2.7.2 |EPD 7.1-2 (64-bit)| (default, Jul 27 2011, 14:50:45) Type "copyright", "credits" or "license" for more information.

IPython 0.11 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details.

In [1]: import PMT as pmt In [2]: x = pmt.photoncount(0.001, 100)

Page 3: Lab #1: Photon counting & the statistics of light

3

AST325/326 (Fall 2016) Lab #1

These commands tell Python to load the program photoncount from the file PMT.py and run the photon detector. In this example the photon counter records the number of photons detected in 1 ms (0.001 s) and repeat 100 times. The results are stored in a Python variable called x. A cartoon of the photon counting process is shown in Figure 1.

Since you asked for 100 samples, the variable x should contain 100 numbers. You can find out about the array x by typing its name, e.g.,

In [6]: x Out[6]: array([ 6, 9, 6, 4, 3, 5, 6, 2, 8, 2, 7, 7, 11, 5, 5, 7, 4,

2, 3, 4, 5, 7, 2, 5, 5, 2, 6, 4, 5, 4, 6, 6, 5, 10, 4, 2, 3, 2, 1, 3, 5, 6, 2, 3, 3, 6, 1, 2, 7, 5, 6, 4, 3, 6, 8, 2, 3, 4, 5, 6, 6, 9, 8, 5, 6, 6, 3, 4, 3, 5, 3, 7, 4, 5, 5, 5, 3, 5, 8, 4, 7, 5, 2, 0, 1, 3, 0, 4, 3, 6, 6, 3, 5, 4, 5, 3, 4, 11, 7, 3])

Figure 1: A cartoon of the samples generated by the counter. The bin width is constant, and for each interval of time, Δ t = 1/rate, counts (smiley faces) from the photon detector are accumulated. In this example the sequence of counts is 3, 1, 2, 4. Only four samples from a longer sequence are illustrated.

An alternative way to invoke the counter is to try

In [10]: nsamp = 100 In [11]: tsamp = 0.001 In [11]: x = pmt.photoncount(tsamp, nsamp)

Here nsamp and tsamp are variables used to refer to values that might be used again. For example, use Python arithmetic to compute the total time for which the counter counts photons.

Typically, at a sample time of a few ms you should get a handful of counts per sample. Play around with the sample time and convince yourself that the number of counts collected increased in direct proportional to this quantity sample.

The counter program will fail if you ask for a sample time shorter than 0.1 ms, i.e, if you set tsamp to a value less than 0.0001. There is a lower limit on the counting interval because the counter uses an oscillator or “clock” running at a finite frequency to trigger the start and end of each active counting interval. One of the consequences of a finite clock frequency is that the available sample times are discretized.

Page 4: Lab #1: Photon counting & the statistics of light

4

AST325/326 (Fall 2016) Lab #1

The predominant failure mode of this experiment occurs when the program on the PC attached to the counter fails. If you suspect that there is a problem, ask the instructor or TA for help. 3 . Reading & writing data You may want to save data for future analysis. This means reading and writing to a file. The simplest way to write a file is to use the python numpy package. Here’s an example:

In [12]: import numpy as np In [13]: np.savetxt('my-data.txt',x,fmt='%i')

This writes a file called my-data.txt. The file will be created if it does not exist; savetxt will overwrite the file if it does. The fmt='%i' statement says that you want to write integers. To read the file back into a new variable called y try:

In [18]: y = np.loadtxt('my-data.txt') 4 . Plotting data When you have the data available in a Python variable you examine it by typing its name. Better still, you can plot the values in the array x using functions from the matplotlib library: In [41]: import matplotlib.pyplot as plt # import if you haven’t already In [42]: plt.figure() # start a new figure

Then type: In [43]: plt.plot(x)

Which will produce the picture shown in Figure 2. When the plot function is given a single argument the x-axis represents the element number in the array.

Page 5: Lab #1: Photon counting & the statistics of light

5

AST325/326 (Fall 2016) Lab #1

Figure 2: The result of plotting x. This plot is an example of a default matplotlib output. This is a rudimentary plot lacking axis labels (or the quantities of the plotted quantity).

To make a more informative graph you need to plot your data as a function of time. Label the x- and y-axes with quantities plotted. Make sure that your labels include units. A plot title or legend is also helpful and can be used to record the sample rate or other pertinent information. Look up matplotlib documentation and figure out how to add a title and axes to your plot. Figure 3 gives an example of a figure that adds these features and improves the readability by using thicker line weights and larger fonts.

Figure 3: An improved version of Figure 2 using some of the capabilities of matplotlib.pyplot. The axes are labeled with the quantities and their units (counts are dimensionless), the font size is large enough to be legible, and thicker lines make the plot easier to read. The histogram line style helps to visualize the sample interval.

Page 6: Lab #1: Photon counting & the statistics of light

6

AST325/326 (Fall 2016) Lab #1

5 . Statistics—making a histogram First, review and understand basic descriptive statistics including mean, standard deviation, and histograms. IPython has all sorts of handy functions in matplotlib and numpy for statistical analysis, e.g., mean for computing mean and hist for plotting histograms. Do not use these! First, we want you understand the basic algorithms—you will not learn these by using a canned routine. Second, you should always exhibit a healthy skepticism about someone else’s program—if you can write the function yourself, you should. Third, we want you to learn Python. By using basic arithmetic operators (+, -, *, /) and primitive functions such as numpy.sum, you can compute these quantities with a few lines of code (and do this faster than it takes to read the appropriate documentation.) For example, to compute the mean try:

In [89]: import numpy as np In [90]: m1 = np.sum(x)/(np.size(x)) # first moment <x> = mean In [91]: m2 = np.sum(x*x)/(np.size(x)) # second moment <x^2> In [92]: std = np.sqrt(m2 - m1*m1) # std deviation (<x^2>-<x>^2)^1/2 In [93]: print 'Mean = ', m1 Mean = 4 In [94]: print 'Standard deviation = ',std Standard deviation = 3.60555127546

Although this looks right, something is seriously wrong! What? Making a histogram involves sorting the data into unique categories (binning), and counting the number of occurrences of each of those categories. The following example makes a histogram of the quantity x In [18]: hmin = 0 # lowest bin in histogram to plot In [19]: hmax = 12 # highest bin in histogram to plot In [20]: hr = np.arange(hmin,hmax+1) # list of bin values In [21]: hist = np.array([np.where(x == i)[0].size for i in hr] ) In [23]: plt.plot(hr,hist,drawstyle='steps-mid')

The result is shown in Figure 4. Comparing this figure with Figure 2 or Figure 3 shows that the histogram provides a very efficient overview of the data; a quick glance reveals the minimum (1) and maximum (11) as well as the most likely value (4).

Page 7: Lab #1: Photon counting & the statistics of light

7

AST325/326 (Fall 2016) Lab #1

Figure 4: Example histogram of the data plotted in Figure 2 and Figure 3, showing the frequency of occurrences of each count value. A quick inspection shows that the most common value is 4 counts, 1 count occurs four times, the maximum value is 11, and there are zero occurrences of 0.

The power of Python is apparent in two statements that make the histogram. First we decide the highest and lowest bins we want to sample by setting hmin and hmax and then construct a list of bin values using arange In [20]: hr = np.arange(hmin,hmax+1) In [21]: hr Out[21]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

The key step in making the histogram is counting the occurrences in each of our bin values, i.e, occurrences of the values in the list [0,1,2…12]. The function where does this as it finds where the array x has certain values, e.g., In [53]: np.where(x == 1) Out[53]: (array([20, 67, 71, 72]),)

shows that elements 20, 67, 71, and 72 of x equal 1. The round brackets or parenthesis denote that where returns a Python tuple which is the list of array indices where x==1. The syntax [0] extracts the list as the first element of the tuple, e.g., In [81]: np.where(x == 1)[0] Out[81]: array([20, 67, 71, 72])

We do not care where the instances of x==1 occur, only how many occurrences there are. We can count these occurrences by using the size attribute In [116]: np.where(x == 1)[0].size Out[116]: 4

Page 8: Lab #1: Photon counting & the statistics of light

8

AST325/326 (Fall 2016) Lab #1

Python often has multiple many ways to achieve the same thing; equally functional would be In [129]: np.size(np.where(x==1)) Out[129]: 4

Now we want to repeat this counting for every element of x. To do this we iterate x using a for loop, e.g.,

In [134]: for i in x: print i

2 5 4 3 . . .

Here i is a dummy variable which represents, in order, each element of x. Notice the colon, which indicates the beginning of commands included in the loop. We can use a for loop to iterate over each bin, e.g., In [14]: hmin = 0 In [15]: hmax = 12 In [16]: hr = np.arange(hmin,hmax+1,1) # make the list of bins In [17]: hist = np.zeros(hmax-hmin+1,dtype=np.int) # store the counts here In [18]: In [18]: for i in hr: hist[i] = np.where(x == i)[0].size # count In [19]: plt.plot(hr,hist,drawstyle='steps-mid')

Notice that the explicit declaration of the variable hist and the for loop replaces the enigmatic “list comprehension” construction: In [20]: hist = np.array([np.where(x == i)[0].size for i in hr] )

Either version works—there is no single “right answer.” The list comprehension construction may be significantly faster in some circumstances. Now check that the histogram plot actually reflects your data. Compare the list of counts in the data file and the plot. Do a “reality check” on a data set where you can rapidly compute the histogram with pencil and paper1. Once you can plot histograms with confidence, repeat the experiment, say, six times and compare the results of your experiments. Does the histogram change? Do you always get the same mean count rate? Become adept at inspecting the histogram plot and guessing the mean and standard deviations (see §7). Calculate the mean and standard deviation of the six sequences you just measured. Be sure to use a unique file name for each sequence of data, because you may

1 This is perhaps the most important programming lesson here. Always test your program on a problem where you know the answer. For example test your histogram program with x =[0,0,0,1,1,1] and x=[0,1,2,3,4,5,6].

Page 9: Lab #1: Photon counting & the statistics of light

9

AST325/326 (Fall 2016) Lab #1

want to go back and look at your data files again. If you do not change the name the file will be overwritten. To examine the data in Python it will be helpful to understand how to use "for" loops. A quick way to approach repetitive tasks involves writing the entire sequence of data acquisition in using for loops.

6 . Collecting data We have seen that Python for loops can be executed at the command line. Try this: In [74]: x = [1,3,5,7,9] In [75]: for i in x: print i 1 3 5 7 9

Here the for loop uses i to iterate over the contents of the list x. Suppose we want to take a sequence of data sets where the sample time increases by factors of two. The following construction would be useful: In [84]: x =np.arange(5) In [85]: for i in x: print 2**i 1 2 4 8 16 If you need multiple instructions “within” the loop, then it is convenient to write a program. Create a new file with emacs called, say, test1.py by cutting and pasting the following:

# Testing photoncount with a for loop. Each time photoncount is called # a unique file name is used.

import numpy as np import PMT as pmt

nsamp = 100 # Define the number of samples tsamp = 0.01 # Define the sample time (in s) nrep = 4 # Define the number of repetitions

k = np.arange(nrep) + 1

for i in k: #

x = pmt.photoncount(tsamp,nsamp) print x

# # construct the file name

Page 10: Lab #1: Photon counting & the statistics of light

10

AST325/326 (Fall 2016) Lab #1

#

myfilename = 'test_{:02d}_{:0.3f}_{:04d}.dat'.format(i,tsamp,nsamp) print 'Writing data to: ' + myfilename

# # write out the data #

np.savetxt(myfilename,x,fmt='%i') Note that # is a comment character—everything after a “#” is ignored. Make sure that the lines executed within the loop are indented. To execute this program in IPython, all you need to do is type

In [99] run test1

Notice that the file name is printed out, and each time that photoncount is invoked the new file name is used. After the program completes get a listing of the folder to see what files were created:

% ls -l The -l part tells the computer to give you more or “long” information about the files. When you have collected and saved several sequences of data try plotting them and comparing their properties. When you are plotting multiple sequences of data investigate what happens when you use the Pyplot function

In [33] plt.subplot(3,2,1)

Now repeat with a bigger sample of data, e.g., increase the number of samples by a factor of 4, but keep the sample rate the same as before. Again, take six sets of data and repeat the exercise of calculating the mean and standard deviation for each longer set of data. Calculate the means and standard deviations of the six count rates you just measured. What do you notice about the mean count rate and the standard deviation for these sequences? 7. Mean and standard deviation Let's try and get to the root of the variations present in the count data. First, we will explore the relation between the mean number of counts and the standard deviation. Take sequences of data with increasingly long sample times (do you need to worry that the sample time is quantized?) Calculate the mean count for each sequence and also the standard deviation. Suppose xbar and s are the means and standard deviations, use the command In [34] plt.plot(xbar, s^2,’-o’) to make a plot of the mean versus the variance (the standard deviation squared). Now over plot a line with unit slope, i.e., x = y. What does this tell you about the relation between mean and variance for counting (Poisson) statistics?

Page 11: Lab #1: Photon counting & the statistics of light

11

AST325/326 (Fall 2016) Lab #1

x

Is the scatter about the line x = y uniform or does it vary with the mean count? Do Poisson statistics break down for large means? Try plotting your data on a log-log plot by using:

In [35] plt.yscale(‘log’) In [36] plt.xscale(‘log’)

8 . The Poisson distribution Plot a histogram for one of your sequences with a small count rate, e.g, 2-4 counts per sample and lots of samples, e.g., 1000 samples. Calculate the mean count rate and compare the resultant histogram with the theoretical Poisson probability distribution,

P ( x; µ ) = µ

exp (− µ ) , (1) x!

where µ is the mean. Use multiple calls to plot() to compare your data histogram with the theoretical predictions of the Poisson distribution. How do you compare a histogram, which represents a measured counts with a theoretical distribution!? The Poisson distribution gives a probability. You have measured counts. Explain how to choose the correct scaling factor (or normalization) to compare the measured and theoretical distributions. Can you use a theoretical probability to predict counts or can you express your measured counts as a probability? Does the Poisson distribution provide a good description of the data? Now arrange so that the counts per sample is increased (be careful that the count rate does not exceed 1 MHz.) Aim for several hundred counts per sample. Plot the histogram again. What has happened to the shape of the histogram—is it qualitatively different? Does the Poission distribution still apply? Calculate the mean and standard deviation and over-plot the corresponding Gaussian probability distribution,

Is a Gaussian curve a good approximation to the Poisson distribution? Under what conditions is the Gaussian probability distribution a good approximation?

9. Standard deviation of the mean The more events you count the more accurately you can measure the number of counts per sample (i.e., the mean count rate). To illustrate the effect, take ten sets of data with a given number of samples, say 16. Choose a fixed sample time, say, 1 ms. For each of these ten sets calculate the mean. Due to statistical variations the ten means will be different, so also calculate the mean of the means (MOM) and the standard deviation of the

Page 12: Lab #1: Photon counting & the statistics of light

12

AST325/326 (Fall 2016) Lab #1

means (SDOM). The MOM is the best estimate of the counts per sample and the SDOM is a measure of how precisely we know the average counts per sample. How does the SDOM vary with the number of samples in the individual sequences? Experience suggests that if we have more samples in each of our ten measurements the SDOM will be smaller. To quantify this effect, repeat with 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048 etc. samples per sequence. Do not vary the sample rate, and be sure that no one changes the brightness of the light source while you are collecting your data! For each sample size consider the ten data sets and calculate the mean of the means (MOM) and the standard deviation of the means (SDOM). Plot the MOM and the SDOM as a function of the number of samples. Describe how the MOM and SDOM vary as the sample size increases. Based on your knowledge of Poisson statistics and error propagation predict the SDOM given the measured mean count per sample and the sample size. Makes plots that compare your prediction with the data.

• If I want to improve the accuracy of a measurement of the mean by a factor of two, by what factor do I need to increase the number of samples?

• How accurate is your best estimate of the count rate, i.e., how accurate is the MOM? • Is it possible to construct a light source for the photometer experiment that would show

no variations in the count rate? Write up your lab report describing each of the above exercises. Describe and show your results by including Python plots in your report. Discuss your findings and implications of the experiment. Include the key portions of your code only in the Appendix of your lab report.


Recommended