Newer
Older
MS_cooling / Analyse / ana.py
@Jihyun Bhom Jihyun Bhom on 13 Aug 2021 4 KB started
import os
import csv
import math
from ROOT import TH1D, TH2D, TCanvas, TGraph, TLine
from array import array


def RMS(arr):
    square = 0
    mean = 0.0
    root = 0.0
    n=len(arr)
    #Calculate square
    for i in range(0,n):
        square += (arr[i]**2)
        
    #Calculate Mean
    mean = (square / (float)(n))
    
    #Calculate Root
    root = math.sqrt(mean)
        
    return root

def mean(arr):
    return sum(arr)/float(len(arr))

def correct_mean(arr, mean):
    n=len(arr)
    for i in xrange(0,n):
        arr[i]=arr[i]-mean
    return arr

def integral(T,V):
    sum=0.
    n=len(T)
    for i in range(1,n):
        sum+= (T[i]-T[i-1])*(V[i]+V[i-1])/2.

    return sum

class Reader:
    def __init__(self, data):
        self.data=data
  
  
    def read(self,scope, temperature, sig):
        print 'Reading the data from Scope: ', scope, "temprature: ", temperature, "Signal/noise: ", sig
        data_file="data/"
        if(scope == "TL"):
            data_file+="lecroy_unpacked/"
        # now reading data
        dir,chan=self.get_dir(scope,temperature, sig)
        if(len(dir)>1):
            print 'WARNING, We have 2 entries for Scope: ', scope, "temprature: ", temperature, "Signal/noise: ", sig
        
        data_file+=dir[0]
        print data_file
        print chan

        
        if(scope == "RS"):   
            for filename in os.listdir(data_file):
                time=[]
                V=[]
                V_trig=[]
                print filename
                if(filename=="INDEX.CSV"):
                    continue
                
                with open(data_file+"/"+filename, 'r') as file:
                    
                    data_csv=csv.reader(file, delimiter=',')
                    first=True
                    for row in data_csv:
                        
                        if(first):
                            first=False
                            continue
                        time.append(float(row[0])*1.e6)
                        #print chan[0]
                        #print row
                        V_tmp=[]
                        for c in xrange(1,5):
                            if(c != chan[0]):
                              V_tmp.append(float(row[c]))
                              print 'Appendet channel ', c
                        V.append(V_tmp)
                        V_trig.append(float(row[chan[0]]))
                ana=Analysis(time,V,V_trig)
                f_out=filename
                f_out=f_out.replace("CSV", "pdf")
                int1,int2,int3=ana.ana(f_out)
                
    def get_dir(self,scope,temperature, sig):
        ret=[]
        t_chan=[]
        for i in self.data:
            if( (i["scope"]==scope) and (i["temperature"]>temperature-0.1 and i["temperature"]<temperature+0.1) and i["type"]==sig ):
                print "Found file with scope= ", scope, " temprature = ", temperature, "signal type= ", sig
                ret.append(i["dir"])
                t_chan.append(i["trig_channel"])

        return ret, t_chan
            
class Analysis:
    def __init__(self,time,V, V_trig):
        self.time=time
        self.V=V
        self.V_trig=V_trig
        self.save=False
    def ana(self, out):
        t0=0

       
        
        t,c1,c2,c3, c_trig=array( 'd' ), array( 'd' ),array( 'd' ),array( 'd' ),array( 'd' ) 
        lenght=len(self.time)
        for i in xrange(0,lenght):
            t.append(self.time[i])
            c1.append(self.V[i][0])
            c2.append(self.V[i][1])
            c3.append(self.V[i][2]) 
            c_trig.append(self.V_trig[i])
            if(float(self.time[i])*1e9<-20.):
                t0=t0+1

            
        gr1 = TGraph( lenght, t, c1 )
        mean_V1=mean(c1[0:t0])
        c1=correct_mean(c1,mean_V1)
        rms_v1=RMS(c1[0:t0])

        gr2 = TGraph( lenght, t, c2 )
        mean_V2=mean(c2[0:t0])
        c2=correct_mean(c2,mean_V2)  
        rms_v2=RMS(c2[0:t0])

        gr3 = TGraph( lenght, t, c3 )
        mean_V3=mean(c3[0:t0])
        c3=correct_mean(c3,mean_V3)  
        rms_v3=RMS(c3[0:t0])

        gr_trig = TGraph( lenght, t, c_trig )
        mean_trig=mean(c_trig[0:t0])
        c_trig=correct_mean(c_trig,mean_trig)
        rms_trig=RMS(c_trig[0:t0])

        n=len(t)
        
        gr1 = TGraph( n, t, c1 )
        gr2 = TGraph( n, t, c2 )
        gr3 = TGraph( n, t, c3 )
        gr_trig = TGraph( n, t, c_trig )
        
        if(out):
            can1=TCanvas("c1", "c1",1920,1080)
            can1.Divide(2,2,0,0)
            can1.cd(1)
            gr1.Draw()
            can1.cd(2)
            gr2.Draw()
            can1.cd(3)
            gr3.Draw()
            can1.cd(4)
            gr_trig.Draw()

            can1.SaveAs(out)
            print "saved as", out
        integral1=integral(t,c1)
        integral2=integral(t,c2)
        integral3=integral(t,c3)


        return integral1, integral2, integral3