Newer
Older
MS_cooling / Analyse / ana.py
@Jihyun Bhom Jihyun Bhom on 23 Aug 2021 11 KB align commit before we move to TL scope
import os
import csv
import math
from ROOT import TH1D, TH2D, TCanvas, TGraph, TLine, kRed, kBlue
from array import array



'''
def CanvasPartition(C, Nx, Ny,
                    lMargin, rMargin,
                    bMargin, tMargin):
    if (!C):
        return
    # Setup Pad layout:
    vSpacing = 0.0
    vStep  = (1.- bMargin - tMargin - (Ny-1) * vSpacing) / Ny
    hSpacing = 0.0
    hStep  = (1.- lMargin - rMargin - (Nx-1) * hSpacing) / Nx
    vposd,vposu,vmard,vmaru,vfactor
    hposl,hposr,hmarl,hmarr,hfactor
    for i in xrange( 0, Nx):
        if (i==0): 
                hposl = 0.0
                hposr = lMargin + hStep
                hfactor = hposr-hposl
                hmarl = lMargin / hfactor
                hmarr = 0.0
        else if (i == Nx-1):
            hposl = hposr + hSpacing
            hposr = hposl + hStep + rMargin
            hfactor = hposr-hposl
            hmarl = 0.0
            hmarr = rMargin / (hposr-hposl)
        else: 
            hposl = hposr + hSpacing
            hposr = hposl + hStep
            hfactor = hposr-hposl
            hmarl = 0.0
            hmarr = 0.0
            
        for j in xrange(0,Ny):
            if (j==0):
                vposd = 0.0
                vposu = bMargin + vStep
                vfactor = vposu-vposd
                vmard = bMargin / vfactor
                vmaru = 0.0
                else if (j == Ny-1):
                    vposd = vposu + vSpacing
                vposu = vposd + vStep + tMargin
                vfactor = vposu-vposd
                vmard = 0.0
                vmaru = tMargin / (vposu-vposd)
                else:
                    vposd = vposu + vSpacing
                    vposu = vposd + vStep
                    vfactor = vposu-vposd
                    vmard = 0.0
                    vmaru = 0.0
                    
                    C.cd(0)
                    
                    pad = new TPad(string(i)+strting(j),"",hposl,vposd,hposr,vposu)
                    pad.SetLeftMargin(hmarl)
                    pad.SetRightMargin(hmarr)
                    pad.SetBottomMargin(vmard)
                    pad.SetTopMargin(vmaru)
                    pad.SetFrameBorderMode(0)
                    pad.SetBorderMode(0)
                    pad.SetBorderSize(0)
                    pad.Draw()
'''                    
                
def MAX(arr, m,M):
    max=-1e10
    for i in xrange(m,M):
        if (arr[i] > max):
            max=arr[i]
    return max



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)
    arr2=[]
    for i in xrange(0,n):
        arr2.append(arr[i]-mean)
    return arr2

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, n_samples):
        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

        counter=0

        RMS1=[]
        RMS2=[]
        RMS3=[]
        V1=[]
        V2=[]
        V3=[]
        
        if(scope == "RS"):   
            for filename in os.listdir(data_file):
                time=[]
                V=[]
                V_trig=[]
                print filename
                if(filename=="INDEX.CSV"):
                    continue
                counter+=1
                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.e9)
                        #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")
                
                mkdir=scope+"_"+str(temperature)+"_"+sig
                if not os.path.exists(mkdir):
                    os.makedirs(mkdir)
                
                
                v1,v2,v3,rms1,rms2,rms3=ana.ana(mkdir+"/"+f_out)
                if(v1>5.*rms1):
                    V1.append(v1)
                    #print "Appending: ", v1
                if(v2>5.*rms2):
                    V2.append(v2)
                if(v3>5.*rms3):
                    V3.append(v3)
                RMS1.append(rms1)
                RMS2.append(rms2)
                RMS3.append(rms3) 

                print v1, rms1, v2, rms2, v3, rms3
                if(counter>=n_samples):
                    break
        if(scope == "RS"):   
            for filename in os.listdir(data_file):
                time=[]
                V=[]
                V_trig=[]
                print filename

                counter+=1
                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.e9)
                        #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")
                
                mkdir=scope+"_"+str(temperature)+"_"+sig
                if not os.path.exists(mkdir):
                    os.makedirs(mkdir)
                
                
                v1,v2,v3,rms1,rms2,rms3=ana.ana(mkdir+"/"+f_out)
                if(v1>5.*rms1):
                    V1.append(v1)
                    #print "Appending: ", v1
                if(v2>5.*rms2):
                    V2.append(v2)
                if(v3>5.*rms3):
                    V3.append(v3)
                RMS1.append(rms1)
                RMS2.append(rms2)
                RMS3.append(rms3) 

                print v1, rms1, v2, rms2, v3, rms3
                if(counter>=n_samples):
                    break

        if(scope=="TL"):
            for filename in os.listdir(data_file):
                print filename
                
                





                
        

        return  RMS1, RMS2, RMS3, V1, V2, V3
                     
    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]*1.e3)
            c2.append(self.V[i][1]*1.e3)
            c3.append(self.V[i][2]*1.e3) 
            c_trig.append(self.V_trig[i]*1.e3)
            if(float(self.time[i])*1e9<-20.):
                t0=t0+1

        t0=t0-10
        gr1 = TGraph( lenght, t, c1 )
        mean_V1=mean(self.V[0:t0][0])
        c1c=correct_mean(c1,mean_V1)
        rms_v1=RMS(c1c[0:t0])
        gr1.SetTitle("Channel 1")
        gr1.GetXaxis().SetTitle("t [ns]")
        gr1.GetYaxis().SetTitle("U [mV]")
        l11=TLine(t[0], mean_V1-rms_v1, t[t0], mean_V1-rms_v1)
        l12=TLine(t[0], mean_V1+rms_v1, t[t0], mean_V1+rms_v1)
        l11.SetLineColor(kRed-3)
        l12.SetLineColor(kRed-3)
        
        gr2 = TGraph( lenght, t, c2 )
        mean_V2=mean(self.V[0:t0][1])        
        c2c=correct_mean(c2,mean_V2)  
        rms_v2=RMS(c2c[0:t0])
        gr2.SetTitle("Channel 2")
        gr2.GetXaxis().SetTitle("t [ns]")
        gr2.GetYaxis().SetTitle("U [mV]")
        l21=TLine(t[0], mean_V2-rms_v2, t[t0], mean_V2-rms_v2)
        l22=TLine(t[0], mean_V2+rms_v2, t[t0], mean_V2+rms_v2)
        l21.SetLineColor(kRed-3)
        l22.SetLineColor(kRed-3)
        

        
        gr3 = TGraph( lenght, t, c3 )
        mean_V3=mean(self.V[0:t0][2])        
        c3c=correct_mean(c3,mean_V3)  
        rms_v3=RMS(c3c[0:t0])
        gr3.SetTitle("Channel 3")
        gr3.GetXaxis().SetTitle("t [ns]")
        gr3.GetYaxis().SetTitle("U [mV]")
        l31=TLine(t[0], mean_V3-rms_v3, t[t0], mean_V3-rms_v3)
        l32=TLine(t[0], mean_V3+rms_v3, t[t0], mean_V3+rms_v3)
        l31.SetLineColor(kRed-3)
        l32.SetLineColor(kRed-3)
        
        
        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])
        gr_trig.SetTitle("Triggering Channel")
        gr_trig.GetXaxis().SetTitle("t [ns]")
        gr_trig.GetYaxis().SetTitle("U [mV]")

        lt=TLine(t[t0-10], -6., t[t0+30], -6.)
        lt.SetLineColor(kBlue-3)
        lt.SetLineWidth(3)
        
        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.01,0.01)
            can1.cd(1)
            gr1.Draw()
            l11.Draw()
            l12.Draw()
            
            can1.cd(2)
            gr2.Draw()
            l21.Draw()
            l22.Draw()

            can1.cd(3)
            gr3.Draw()
            l31.Draw()
            l32.Draw()
            
            can1.cd(4)
            gr_trig.Draw()
            lt.Draw()
            
            can1.SaveAs(out)
            print "saved as", out
        MAXC1=MAX(c1,t0,t0+40)
        MAXC2=MAX(c2,t0,t0+40)
        MAXC3=MAX(c3,t0,t0+40)
        #print c1[t0:t0+20]
        #print MAXC1
        return MAXC1, MAXC2, MAXC3, rms_v1, rms_v2, rms_v3