Newer
Older
MS_cooling / Analyse / main.py
@Jihyun Bhom Jihyun Bhom on 23 Aug 2021 7 KB align commit before we move to TL scope
import sys
sys.argv.append( '-b-' )
from ROOT import *
import csv
import os
from array import array
import numpy as np
import math
import sys

from data_corrected import dict_data 
from ana import Reader

gROOT.SetBatch(True)

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

def get_dist(arr, name):
    
    if(len(arr)==0):
        return TH1D("EMPTY, NO DATA", "EMPTY, NO DATA", 10, 0,1)

    MIN=min(arr)
    MAX=max(arr)

    hist=TH1D(name, name, 100, MIN, MAX)
    for a in arr:
        hist.Fill(a)
        
    return hist

def mean(list):
    #print list
    if(len(list)==0):
        return -1.

    return sum(list)/len(list)

def main(argv):
    print 'Software to analysis Cooling results for Magnet Stations'
    print 'Author: Jihyun Bhom'

    scope="RS"

    RMS1_noise=[]
    V1_signal=[]
    RMS2_noise=[]
    V2_signal=[]
    RMS3_noise=[]
    V3_signal=[]
    

    #temperature=[]
    

    gr_noise1, gr_noise2,gr_noise3=array( 'd' ),array( 'd' ),array( 'd' )
    gr_sig1, gr_sig2, gr_sig3=array( 'd' ),array( 'd' ),array( 'd' )

    gr_noise1e, gr_noise2e,gr_noise3e=array( 'd' ),array( 'd' ),array( 'd' )
    gr_sig1e, gr_sig2e, gr_sig3e=array( 'd' ),array( 'd' ),array( 'd' )
    

    temperature1=array( 'd' )
    temperature2=array( 'd' )
    temperature3=array( 'd' )
    temperature1s=array( 'd' )
    temperature2s=array( 'd' )
    temperature3s=array( 'd' )
    
    temperature1e=array( 'd' )
    temperature2e=array( 'd' )
    temperature3e=array( 'd' )
    temperature1se=array( 'd' )
    temperature2se=array( 'd' )
    temperature3se=array( 'd' )
    
    
    

    reader=Reader(dict_data)
    
    for i in dict_data:
        if((i["scope"]==scope)):
            temp=i["temperature"]
            
            #for j in ["noise", "sig"]:
            j=i["type"]
            print "Trying", i
                
            #reader=Reader(dict_data)
            RMS1, RMS2, RMS3, V1, V2, V3=reader.read(i["scope"], temp, j ,300) 
                
            if(j=="noise"):

                
                
                if(mean(RMS1)>0):
                    hist1=get_dist(RMS1, "noise, temperature= "+str(temp)+" Channel 1")
                    gr_noise1.append(hist1.GetMean())
                    gr_noise1e.append(hist1.GetRMS())
                    temperature1.append(temp)
                    temperature1e.append(1.) 
                if(mean(RMS2)>0):
                    hist2=get_dist(RMS2, "noise, temperature= "+str(temp)+" Channel 2")
                    gr_noise2.append(hist2.GetMean())
                    gr_noise2e.append(hist2.GetRMS())
                    temperature2.append(temp)
                    temperature2e.append(1.) 
                if(mean(RMS3)>0):
                    hist3=get_dist(RMS3, "noise, temperature= "+str(temp)+" Channel 3")
                    gr_noise3.append(hist3.GetMean())
                    gr_noise3e.append(hist3.GetRMS())
                    temperature3.append(temp)
                    temperature3e.append(1.)
                    
                    
                

                
                hist1=get_dist(RMS1, "noise, temperature= "+str(temp)+" Channel 1")
                hist2=get_dist(RMS2, "noise, temperature= "+str(temp)+" Channel 2")
                hist3=get_dist(RMS3, "noise, temperature= "+str(temp)+" Channel 3")


                print "Check: ", mean(RMS1), RMS(RMS1), hist1.GetMean(), hist1.GetRMS()
                
                can1=TCanvas("c1", "c1",1920,1080)
                can1.Divide(2,2,0.01,0.01)
                can1.cd(1)
                hist1.Draw()
                can1.cd(2) 
                hist2.Draw()
                can1.cd(3) 
                hist3.Draw()

                can1.SaveAs("noise" +"_"+str(temp)+".pdf")
                    
            if(j=="sig"):


                if(mean(V1)>0):
                    gr_sig1.append(mean(V1))
                    gr_sig1e.append(RMS(V1))
                    temperature1s.append(temp)
                    temperature1se.append(1.)
                if(mean(V2)>0):
                    gr_sig2.append(mean(V2))
                    gr_sig2e.append(RMS(V2))
                    temperature2s.append(temp)
                    temperature2se.append(1.)
                if(mean(V3)>0):
                    gr_sig3.append(mean(V3))
                    gr_sig3e.append(RMS(V3))
                    temperature3s.append(temp)
                    temperature3se.append(1.)
                                                                            


                hist1=get_dist(V1, "sig" +"_"+str(temp)+" Channel 1")
                hist2=get_dist(V2, "sig" +"_"+str(temp)+" Channel 2") 
                hist3=get_dist(V3, "sig" +"_"+str(temp)+" Channel 3") 
                    
                can1=TCanvas("c1", "c1",1920,1080)
                can1.Divide(2,2,0.01,0.01)
                can1.cd(1)
                hist1.Draw()
                can1.cd(2)
                hist2.Draw()
                can1.cd(3)
                hist3.Draw()
                    
                can1.SaveAs("sig" +"_"+str(temp)+".pdf")

    can2=TCanvas("c1", "c1",1920,1080)
    can2.Divide(3,2,0.01,0.01)
    
    Gr_sig1=TGraphErrors(len(gr_sig1), temperature1s, gr_sig1, temperature1se, gr_sig1e)
    Gr_sig2=TGraphErrors(len(gr_sig2), temperature2s, gr_sig2, temperature2se, gr_sig2e)
    Gr_sig3=TGraphErrors(len(gr_sig3), temperature3s, gr_sig3, temperature3se, gr_sig3e)

    Gr_sig1.SetTitle("Signal Channel 1")
    Gr_sig1.GetXaxis().SetTitle("Temperature [C]")
    Gr_sig1.GetYaxis().SetTitle("V [mV]")

    Gr_sig2.SetTitle("Signal Channel 2")
    Gr_sig2.GetXaxis().SetTitle("Temperature [C]")
    Gr_sig2.GetYaxis().SetTitle("V [mV]")
    
    Gr_sig3.SetTitle("Signal Channel 3")
    Gr_sig3.GetXaxis().SetTitle("Temperature [C]")
    Gr_sig3.GetYaxis().SetTitle("V [mV]")
    


    
    Gr_noise1=TGraphErrors(len(gr_noise1), temperature1, gr_noise1, temperature1e, gr_noise1e)
    Gr_noise2=TGraphErrors(len(gr_noise2), temperature2, gr_noise2, temperature2e, gr_noise2e)
    Gr_noise3=TGraphErrors(len(gr_noise3), temperature3, gr_noise3, temperature3e, gr_noise3e)
    
    Gr_noise1.SetTitle("Noise Channel 1")
    Gr_noise1.GetXaxis().SetTitle("Temperature [C]")
    Gr_noise1.GetYaxis().SetTitle("V [mV]")

    Gr_noise2.SetTitle("Noise Channel 2")
    Gr_noise2.GetXaxis().SetTitle("Temperature [C]")
    Gr_noise2.GetYaxis().SetTitle("V [mV]")
    
    Gr_noise3.SetTitle("Noise Channel 3")
    Gr_noise3.GetXaxis().SetTitle("Temperature [C]")
    Gr_noise3.GetYaxis().SetTitle("V [mV]")
    

    
    can2.cd(1)
    Gr_sig1.Draw()
    can2.cd(2)
    Gr_sig2.Draw()
    can2.cd(3)
    Gr_sig3.Draw()


    can2.cd(4)  
    Gr_noise1.Draw()
    can2.cd(5) 
    Gr_noise2.Draw()
    can2.cd(6)
    Gr_noise3.Draw() 

    can2.SaveAs("result.pdf")

    print gr_sig1
    print gr_noise1

    
    
if __name__ == "__main__":
    sys.exit(main(sys.argv))