forked from pylhc/Python_Classes4MAD
/
rhicdata.py
61 lines (53 loc) · 1.73 KB
/
rhicdata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from Numeric import *
import sys
import pickle
from string import split
from math import acos
#########################
def list_to_float(arg):
#########################
vals = []
for token in arg:
vals.append(float(token))
return vals
#########################
class BPM:
#########################
"a class for BPMs"
def __init__(self, cutline):
self.name=cutline[1]
self.location=float(cutline[2])
self.data=array(list_to_float(cutline[3:]))
#########################
class rhicdata:
#########################
"Read the sdds asci RHIC data into an object"
def __init__(self, filename):
self.H=[]
self.V=[]
self.byname={}
for line in open(filename):
if ("#" in line) :
self.title=line
else:
cutline=split(line)
if (int(cutline[0])==0): #Horizontal BPM
self.H.append(BPM(cutline))
self.byname[cutline[1]]=self.H[-1].data
else: #Vertical BPM
self.V.append(BPM(cutline))
self.byname[cutline[1]]=self.H[-1].data
def correlation(self, istart=20, iend=2000, sigma=0):
self.phaseadv=[]
for i in range(1, len(self.H)):
s1=self.H[i-1].data[istart:iend]
s2=self.H[i].data[istart:iend]
s1=s1-average(s1)
s2=s2-average(s2)
sum1=sum(s1*s1)/len(s1)
sum2=sum(s2*s2)/len(s1)
sum12=sum(s2*s1)/len(s1)
if sum1*sum2 != 0:
self.phaseadv.append(acos(sum12/sqrt((sum1-sigma**2)*(sum2-sigma**2))))
else:
self.phaseadv.append('nan')