#!/usr/bin/env python # --------------------------------------------------------- # # Simple python script for reading ASCII data file # # Author: Troels C. Petersen (Niels Bohr Institute) # Date: 1st of September 2014 # # Run by: ./ReadingAsciiFile.py # --------------------------------------------------------- # from ROOT import * import math # Open file f = open('randomnumbers.dat', 'r') Nread = 0 verbose = True # Read and ignore the header line header = f.readline() # Loop over lines and extract variables of interest for line in f: line = line.strip() # Strip the line read for space characters columns = line.split() # Split the line read into numbers (thus "columns" is a list) n1 = float(columns[0]) # Make "n1" the float (i.e. decimal number) value of the first string in "columns" n2 = float(columns[1]) # ... n3 = float(columns[2]) n4 = float(columns[3]) n5 = float(columns[4]) if (verbose and Nread < 10) : print ' %2d: n1 = %6.3f n2 = %6.3f n3 = %6.3f n4 = %6.3f n5 = %6.3f'%(Nread, n1, n2, n3, n4, n5) Nread = Nread+1 f.close() # Tell python to wait here untill you do something (so that you can get to the what is plotted!) raw_input( ' ... Press enter to exit ... ' ) #---------------------------------------------------------------------------------- # # As always, read through this (very short) program, and make sure that you # understand what is going on. # # Questions: # ---------- # 1) What is the mean and RMS/width/sigma of the first column? If you are in doubt how to calculate # this, then try to make the three sums I introduced on slide 9 of the "Mean and Width" # lecture: http://www.nbi.dk/~petersen/Teaching/Stat2014/Week1/AS2014_0901_MeanWidth.pdf # # 2) Try to make plots of the five distributions, and see if you from these plots # (and perhaps fits) can extract the parameters of each of the five distribuitons. # #----------------------------------------------------------------------------------