#!/usr/bin/env python # --------------------------------------------------------- # # Simple python script for reading ASCII data file # --------------------------------------------------------- # from ROOT import * import math # Next, we inform ROOT what plotting style it should use for statistics and fitting box: gStyle.SetOptStat("emr") # Options for statistics box: "e" = entries, "m" = mean, "r" = RMS gStyle.SetOptFit(1111) # Options for fitting box. # Open file f = open('datafile.txt', 'r') Nread = 0 verbose = True # Read and ignore the header line header = f.readline() V_low = -0.56 V_high = 3.62 isLow = True Hist_V = TH1F("Hist_V", "Hist_V", 160, -0.6, -0.5) Hist_Vt = TH1F("Hist_Vt", "Hist_Vt", 80000, 0.0, 2.0) # 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) t = 0.0 V = 0.0 if (len(columns) == 2) : t = float(columns[0]) # Make "n1" the float (i.e. decimal number) value of the first string in "columns" V = float(columns[1]) # Voltage... Hist_V.Fill(V) Hist_Vt.SetBinContent(Nread+1, V) Hist_Vt.SetBinError(Nread+1, 0.0015) # Consider graph!!! if (isLow and V > V_low + 0.5*(V_high-V_low)) : isLow = False print t if (not isLow and V < V_low + 0.5*(V_high-V_low)) : isLow = True print t Nread += 1 if (t < -1.0) : print "Somthing wrong!" if (V < -10.0) : print "Somthing also wrong!" if (verbose and Nread < 10) : print " %2d: t = %8.6f V = %9.6f "%(Nread, t, V) f.close() print " Total number of lines: ", Nread # Plot result: # ------------ canvas = TCanvas( "canvas", "canvas", 50, 50, 1100, 500 ) Hist_Vt.SetTitle("Distribution of V vs. t") Hist_Vt.GetXaxis().SetRangeUser(0.8, 1.2) # Hist_Vt.GetXaxis().SetRangeUser(0.859, 0.863) Hist_Vt.GetXaxis().SetTitle("t") Hist_Vt.GetYaxis().SetTitle("V") Hist_Vt.SetLineColor(kBlue) Hist_Vt.SetLineWidth(2) Hist_Vt.Draw("") # Fitting histogram (with predefined function): fit_Vt = TF1("fit_Vt", "[0] + [1]*(x-0.86) + [2]*(x-0.86)*(x-0.86) + [3]*(x-0.86)*(x-0.86)*(x-0.86)", 0.8603, 0.8613) fit_Vt.SetLineColor(kRed) fit_Vt.SetLineWidth(3) Hist_Vt.Fit("fit_Vt", "R") canvas.Update() # Tell python to wait here until you do something... raw_input( ' ... Press enter to exit ... ' )