-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorbit.py
1356 lines (1175 loc) · 53.4 KB
/
orbit.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Sergey Tomin. XFEL/DESY, 2017.
"""
import logging
from threading import Thread, Event
import pyqtgraph as pg
from PyQt5 import QtGui, QtCore
from PyQt5 import QtWidgets
import numpy as np
from ocelot import *
from ocelot.cpbd.track import *
import time
from ocelot.cpbd.orbit_correction import Orbit, OrbitSVD, MICADO
from ocelot.cpbd.response_matrix import *
from golden_orbit import GoldenOrbit
from adaptive_feedback import UIAFeedBack
from ocelot.cpbd import match
import matplotlib.pyplot as plt
import seaborn as sns
logger = logging.getLogger(__name__)
try:
from bpm_api import bpm_api
except Exception as e:
logger.warning("Import bpm_api: " + str(e))
class ResponseMatrixCalculator(Thread):
"""
Wrap for ResponseMatrix class. Allow to calculate response matrices (ORM and DRM) in different thread
"""
def __init__(self, rm, drm):
super(ResponseMatrixCalculator, self).__init__()
self.rm = rm
self.drm = drm
self.do_DRM_calc = True
self.tw_init = None
self.rm_filename = None
self.drm_filename = None
def run(self):
self.rm.calculate(tw_init=self.tw_init)
cor_names = self.rm.cor_names
bpm_names = self.rm.bpm_names
inj_matrix = self.rm.matrix
try:
self.rm.load(self.rm_filename)
except:
logger.warning("ResponseMatrixCalculator: Could not load RM.")
if self.rm_filename != None:
logger.warning("ResponseMatrixCalculator: Dumping RM >" + str(self.rm_filename))
self.rm.dump(filename=self.rm_filename)
return False
if len(cor_names) > len(self.rm.cor_names) or len(bpm_names) > len(self.rm.bpm_names):
logger.info("ResponseMatrixCalculator: dump calculated ORM")
self.rm.cor_names = cor_names
self.rm.bpm_names = bpm_names
self.rm.matrix = inj_matrix
self.rm.dump(filename=self.rm_filename)
else:
logger.info("ResponseMatrixCalculator: inject calculated ORM")
self.rm.inject(cor_names, bpm_names, inj_matrix)
logger.warning("ResponseMatrixCalculator: Dumping RM >" + str(self.rm_filename))
self.rm.dump(filename=self.rm_filename)
#print(np.shape(self.rm.matrix))
#if self.rm_filename != None:
# self.rm.dump(filename=self.rm_filename)
if self.do_DRM_calc:
if self.drm != None:
logger.info("ResponseMatrixCalculator: DRM calculation ... ")
self.drm.calculate(tw_init=self.tw_init)
if self.drm_filename != None:
self.rm.dump(filename=self.drm_filename)
logger.info("ResponseMatrixCalculator: DRM dumping > " + self.drm_filename)
class OrbitKeeper(Thread):
def __init__(self, orbit_class):
super(OrbitKeeper, self).__init__()
self.orbit_class = orbit_class
self._stop_event = Event()
self.do = None
self.delay = 1
self.stop_event = False
def one_correction(self, reset):
beam_on = self.orbit_class.read_orbit()
time.sleep(0.01)
if beam_on:
self.orbit_class.correct(reset=reset)
time.sleep(0.01)
self.orbit_class.apply_kicks()
time.sleep(0.5)
else:
logger.debug("AutoCorrection->one_correction: no beam")
time.sleep(1)
def run(self):
reset = True
while not self.stop_event:
self.one_correction(reset)
logger.debug("AutoCorrection->run: correct")
time.sleep(self.delay)
reset = False
def stop(self):
self._stop_event.set()
class OrbitInterface:
"""
Main class for orbit correction
"""
def __init__(self, parent):
self.parent = parent
self.bpms4remove = self.parent.uncheck_bpms #["BPMS.99.I1", "BPMS.192.B1"]
self.corrs4remove = self.parent.uncheck_corrs #["CBB.98.I1", "CBB.100.I1", "CBB.101.I1","CBB.191.B1", "CBB.193.B1", "CBB.202.B1",
# 'CBL.73.I1', 'CBL.78.I1', 'CBL.83.I1', 'CBL.88.I1', 'CBL.90.I1', 'CBB.403.B2', 'CBB.405.B2', 'CBB.414.B2']
#print("corrs unchecked:", self.corrs4remove)
self.svd_epsilon_x = self.parent.svd_epsilon_x
self.svd_epsilon_y = self.parent.svd_epsilon_y
self.ui = parent.ui
self.online_calc = True
self.reset_undo_database()
self.corrs = []
self.hcors = []
self.vcors = []
self.s_bpm = []
self.x_bpm = []
self.y_bpm = []
#self.mi_orbit = MIOrbit(server=self.parent.server, subtrain=self.parent.subtrain)
#self.mi_orbit.mi = self.parent.mi
#
#self.xfel_mps = MPS(server=self.parent.server, subtrain=self.parent.subtrain)
#self.xfel_mps.mi = self.parent.mi
self.update_machine_interface()
self.calc_correction = {}
self.p_init = None
self.orbit = None
self.add_orbit_plot()
self.ui.pb_check.clicked.connect(lambda: self.getRows(2, self.ui.table_cor))
self.ui.pb_uncheck.clicked.connect(lambda: self.getRows(0, self.ui.table_cor))
self.ui.pb_bpm_uncheck.clicked.connect(lambda: self.getRows(0, self.ui.table_bpm))
self.ui.pb_bpm_check.clicked.connect(lambda: self.getRows(2, self.ui.table_bpm))
self.ui.actionRead_BPMs_Corrs.triggered.connect(self.read_orbit)
self.ui.pb_apply_kicks.clicked.connect(self.apply_kicks)
#self.ui.pb_calc_RM.clicked.connect(self.calc_response_matrix)
self.ui.actionCalculate_RM.triggered.connect(lambda: self.calc_response_matrix(do_DRM_calc=True))
self.ui.actionCalculate_ORM.triggered.connect(lambda: self.calc_response_matrix(do_DRM_calc=False))
self.ui.actionShow_ORM.triggered.connect(self.show_orm)
self.ui.actionAnalyse_Corrections.triggered.connect(self.analyse_corrections)
#self.ui.pb_correct_orbit.clicked.connect(self.correct)
self.ui.pb_correct_orbit.clicked.connect(self.read_and_correct)
self.ui.pb_read_orbit.clicked.connect(self.read_bpms)
self.ui.pb_calculate.clicked.connect(self.calculate_correction)
self.ui.pb_reset_all.clicked.connect(self.undo)
self.ui.cb_x_cors.stateChanged.connect(self.choose_plane)
self.ui.cb_y_cors.stateChanged.connect(self.choose_plane)
self.ui.actionUpdate_Lattice_from_DOOCS.triggered.connect(self.parent.read_quads)
self.ui.cb_cbxy.stateChanged.connect(self.uncheck_aircols)
self.ui.cb_caxy.stateChanged.connect(self.uncheck_aircols)
#self.ui.pb_uncheck_red.clicked.connect(self.uncheck_red)
self.ui.actionUncheck_Red.triggered.connect(self.uncheck_red)
self.ui.pb_online_orbit.clicked.connect(self.start_stop_live_orbit)
self.ui.pb_calc_orb.clicked.connect(self.start_stop_calc_orbit)
self.ui.pb_ref_orb.clicked.connect(self.start_stop_ref_orbit)
#self.cavity = CavityA1(eid="CTRL.A1.I1")
#self.cavity.mi = self.parent.mi
self.ui.pb_feedback.clicked.connect(self.start_stop_feedback)
self.rm_calc = pg.QtCore.QTimer()
self.rm_calc.timeout.connect(self.is_rm_calc_alive)
self.golden_orbit = GoldenOrbit(parent=self)
self.ui.sb_apply_fraction.valueChanged.connect(self.set_values2correctors)
#self.ui.cb_correction_result.stateChanged.connect(self.update_plot)
self.orbit_keeper = OrbitKeeper(orbit_class=self)
self.ui.actionAdaptive_Feedback.triggered.connect(self.run_awindow)
self.adaptive_feedback = None
#self.adaptive_feedback = None
self.dev_mode = self.parent.dev_mode
self.ui.actionSave_corrs.triggered.connect(self.save_correctors)
self.ui.actionLoad_corrs.triggered.connect(self.restore_correctors)
self.button_bpm = None
self.cavity_bpm = None
try:
self.button_bpm = bpm_api.ButtonBPM()
self.cavity_bpm = bpm_api.CavityBPM()
except Exception as e:
logger.warning("Initialization of bpm_api.ButtonBPM and bpm_api.CavityBPM: " + str(e))
self.ui.cb_freeze_bpms.stateChanged.connect(self.freeze_bpms)
def freeze_bpms(self):
# switched off freeze and unfreeze functionality.
if 1:
return
if self.ui.cb_freeze_bpms.isChecked():
logger.info("Freeze BPMs")
self.xfel_mps.beam_off()
self.xfel_mps.num_bunches_requested(num_bunches=1)
charge = self.ui.sb_bpm_charge.value() # in pC
amplitude = self.ui.sp_orbit_ampl.value() # in mm
attenuation = self.ui.sb_attenuation.value() # attenuation
self.button_bpm.activate(max_charge_value=charge, max_pos_value=amplitude)
self.cavity_bpm.activate(attenuation=attenuation)
else:
logger.info("Unfreeze BPMs")
self.button_bpm.deactivate()
self.cavity_bpm.deactivate()
def show_orm(self):
if self.orbit is not None:
cor_list = [cor.id for cor in np.append(self.orbit.hcors, self.orbit.vcors)]
bpm_list = [bpm.id for bpm in self.orbit.bpms]
if self.orbit.response_matrix is None:
print("ORM is None in self.orbit")
return
df_slice = self.orbit.response_matrix.extract_df_slice(cor_list, bpm_list)
if df_slice is None:
print("df_slice is None. return")
return
shape = np.array(df_slice.shape)
print("ORM shape: " + str(shape))
if any(shape > 100):
self.parent.error_box("ORM is too large. Shape: " + str(shape))
return
ax = sns.heatmap(df_slice, annot=True)
ax.set_title("Orbit response matrix")
plt.show()
def analyse_corrections(self):
if self.parent.cor_analysis is not None:
if self.parent.cor_analysis.df is not None:
self.parent.cor_analysis.calculate_orm()
def update_machine_interface(self):
self.mi_orbit = self.parent.mi.devices.MIOrbit(server=self.parent.server, subtrain=self.parent.subtrain)
self.mi_orbit.mi = self.parent.mi
self.mi_orbit.bpm_server = self.parent.bpm_server
#self.mi_orbit.start()
self.xfel_mps = self.parent.mi.devices.MPS(server=self.parent.server, subtrain=self.parent.subtrain)
self.xfel_mps.mi = self.parent.mi
self.mi_charge_doocs = self.parent.mi.devices.ChargeDoocs(server=self.parent.server, subtrain=self.parent.subtrain)
self.mi_charge_doocs.mi = self.parent.mi
def reset_undo_database(self):
self.undo_data_base = []
def run_awindow(self):
if self.adaptive_feedback is None:
self.adaptive_feedback = UIAFeedBack(orbit=self)
self.adaptive_feedback.show()
def uncheck_red(self):
"""
Method to uncheck correctors if they are out of limits (red color in the GUI)
:return:
"""
corrs = self.get_dev_from_cb_state(self.corrs)
for cor in corrs:
if cor.ui.alarm:
cor.ui.uncheck()
bpms = self.get_dev_from_cb_state(self.bpms)
for bpm in bpms:
if bpm.ui.alarm:
bpm.ui.uncheck()
def uncheck_aircols(self):
"""
Method checks and unchecks corresponding aircoils downstream or upstream
:return:
"""
upstream = self.ui.cb_caxy.isChecked()
downstream = self.ui.cb_cbxy.isChecked()
#corrs = self.get_dev_from_cb_state(self.corrs)
logger.debug("uncheck_aircoils: upstream / downstream: " + str(upstream) + "/" + str(downstream))
for cor in self.corrs:
#print(cor.id, upstream, downstream)
if ".SA1" in cor.id or (".SA3" in cor.id) or (".SA2" in cor.id):
if not upstream:
if ("CAX." in cor.id) or ("CAY." in cor.id):
cor.ui.uncheck()
if not downstream:
if ("CBX." in cor.id) or ("CBY." in cor.id):
cor.ui.uncheck()
if upstream:
if ("CAX." in cor.id) or ("CAY." in cor.id):
cor.ui.check()
if downstream:
if ("CBX." in cor.id) or ("CBY." in cor.id):
cor.ui.check()
def choose_plane(self):
"""
Method checks and unchecks corresponding correctors in horizontal or/and vertical planes
:return:
"""
x_plane = self.ui.cb_x_cors.isChecked()
y_plane = self.ui.cb_y_cors.isChecked()
if y_plane and not x_plane:
for cor in self.corrs:
if cor.__class__ == Hcor:
cor.ui.uncheck()
cor.ui.set_hide(True)
else:
cor.ui.check()
cor.ui.set_hide(False)
elif x_plane and not y_plane:
for cor in self.corrs:
if cor.__class__ == Hcor:
cor.ui.check()
cor.ui.set_hide(False)
else:
cor.ui.uncheck()
cor.ui.set_hide(True)
else:
for cor in self.corrs:
cor.ui.check()
cor.ui.set_hide(False)
# uncheck correctors from the ban list
self.uncheck_corrs(self.corrs, self.corrs4remove)
def reset_all(self):
"""
Method to reset initial values of the correctors
:return:
"""
corrs = self.get_dev_from_cb_state(self.corrs)
self.online_calc = False
for cor in corrs:
kick_mrad = cor.ui.get_init_value()
cor.ui.set_value(kick_mrad)
self.online_calc = True
def undo(self):
"""
Method to reset initial values of the correctors
:return:
"""
if len(self.undo_data_base) == 0:
return 0
self.online_calc = False
corrs_dict = self.undo_data_base[-1]
for cor in self.corrs:
if cor.id in corrs_dict.keys():
cor.ui.check()
kick_mrad = corrs_dict[cor.id]
cor.ui.set_init_value(kick_mrad)
cor.ui.set_value(kick_mrad)
else:
cor.ui.uncheck()
del self.undo_data_base[-1]
self.ui.pb_reset_all.setText("Undo (" + str(len(self.undo_data_base)) + ")")
self.online_calc = True
def save_correctors(self):
corrs_save = {}
for cor in self.corrs:
logger.debug("save correctors: " + cor.id + " " + str(cor.ui.get_init_value()))
corrs_save[cor.id] = cor.ui.get_init_value()
with open("corrs_save.json", 'w') as f:
json.dump(corrs_save, f)
def restore_correctors(self):
with open("corrs_save.json", 'r') as f:
table = json.load(f)
cor_ids = [cor.id for cor in self.corrs]
self.online_calc = False
for cor_id in table.keys():
if cor_id in cor_ids:
inx = cor_ids.index(cor_id)
cor = self.corrs[inx]
cor.ui.set_value(table[cor.id])
logger.debug("restore correctors:" + cor.id +" before %s after %s" % (cor.ui.get_value(), table[cor.id]))
self.online_calc = True
def apply_kicks(self):
"""
Methods sends correctors kicks to DOOCS, if strengths below the limits,
otherwise error box will appear
:return:
"""
logger.info("Apply Kicks")
corrs = self.get_dev_from_cb_state(self.corrs)
for cor in corrs:
if cor.ui.alarm:
logger.info("apply_kicks: kick exceeds limits. Stop applying")
self.parent.error_box("kick exceeds limits. Try 'Uncheck Red' and recalculate correction")
return 0
kicks = np.array([cor.ui.get_value() for cor in corrs])
if np.all(kicks == 0):
yes = self.parent.question_box("All kicks are zero. Apply?")
if not yes:
return
for cor in corrs:
kick_mrad = cor.ui.get_value()
logger.debug("Apply kicks: " + cor.id + " set: %s --> %s" % (cor.ui.get_init_value(), kick_mrad))
try:
cor.mi.set_value(kick_mrad)
except:
logger.error("Apply kick: mi.set_value cause error - corrector.id = "+ str(cor.id) + ", kick_mrad = " + str(kick_mrad))
self.parent.error_box("Error during writing in DOOCS. Repeat APPLY KICKS. Corrector: " + str(cor.id) + " kick = " + str(kick_mrad))
dict_delta_kicks_rad = self.write_old_kicks(corrs)
# if self.parent.cor_analysis is not None and self.parent.mi.analyse_correction is True:
# self.parent.cor_analysis.save_kicks(dict_delta_kicks_rad)
# #self.parent.cor_analysis.used_correctors(corrs)
# self.parent.cor_analysis.start()
def write_old_kicks(self, corrs):
"""
Method to store a history of the applied kicks in self.undo_data_base.
secondary functionality is to store correctors dictionary with delta kick for analysis.
:param corrs: list of correcors (classes)
:return: dict_delta_kicks_rad, dictionary {"cor_id": delta_kick_in_rad, ...}
"""
old_corrs_kicks = {}
dict_delta_kicks_rad = {}
save_flag = False
for cor in corrs:
# write to dict old kicker strengths
old_corrs_kicks[cor.id] = cor.ui.get_init_value()
dict_delta_kicks_rad[cor.id] = (cor.ui.get_value() - cor.ui.get_init_value())/1000 # mrad -> rad
if cor.ui.get_init_value() != cor.ui.get_value():
save_flag = True
if save_flag:
self.undo_data_base.append(old_corrs_kicks)
self.ui.pb_reset_all.setText("Undo (" + str(len(self.undo_data_base)) + ")")
return dict_delta_kicks_rad
def read_correctors_BESSY(self):
cor_names = [cor.id for cor in self.corrs]
self.parent.mi.corrector_data.connect(cor_names)
self.parent.mi.correctors_kick = self.parent.mi.corrector_data.get()
self.online_calc = False
for elem in self.corrs:
hw2phys = self.parent.mi.corr_conversion[elem.id][0]
elem.kick_mrad = elem.mi.get_value_from_dict()
elem.angle_read = elem.kick_mrad*hw2phys
elem.i_kick = elem.kick_mrad
elem.ui.set_init_value(elem.kick_mrad)
elem.ui.set_value(elem.kick_mrad)
self.online_calc = True
self.parent.lat.update_transfer_maps()
def read_correctors(self):
"""
Method to read from MI correctors (angles: mrad->rad)
self.online_calc = False - switch off recalculating of the calculated orbit
otherwise after every set in table orbit will be recalculated.
:return:
"""
self.online_calc = False
for elem in self.corrs:
elem.kick_mrad = elem.mi.get_value()
elem.angle_read = elem.mi.hw2phys(elem.kick_mrad)
elem.i_kick = elem.kick_mrad
elem.ui.set_init_value(elem.kick_mrad)
elem.ui.set_value(elem.kick_mrad)
self.online_calc = True
self.parent.lat.update_transfer_maps()
def read_orbit_one_by_one(self):
"""
Method to readw from MI: correctors (angles: mrad->rad) and
BPMs (X and Y: mm -> m) and checks charge on the BPMs
if the charge below charge_thresh return False
:return: bool, True if the charge on all BPMs >= charge_thresh otherwise False
"""
self.read_correctors()
charge_thresh = 0.005
bpms = self.get_dev_from_cb_state(self.bpms)
beam_on = True
for elem in bpms:
try:
x_mm, y_mm = elem.mi.get_pos()
if np.isnan(x_mm) or np.isnan(y_mm):
logger.warning("read bpm: " + elem.id + "NaN -> was unchecked")
elem.ui.uncheck()
charge = elem.mi.get_charge()
if charge < charge_thresh:
beam_on = False
if np.abs(charge/self.parent.bunch_charge) < self.parent.charge_tol/100:
logger.info(" BPM:" + elem.id + " unchecked -> " +str(np.round(charge, 2)) + "/" + str(np.round(self.parent.bunch_charge, 2)) + " < " + str(self.parent.charge_tol/100))
elem.ui.uncheck()
elem.x = x_mm/1000.
elem.y = y_mm/1000.
elem.ui.set_value((x_mm, y_mm))
except Exception as exc:
logger.error("read bpm: " + elem.id + " was unchecked. Error: " + str(exc))
elem.ui.uncheck()
self.update_plot()
return beam_on
def read_orbit_star(self):
"""
Method to readw from MI: correctors (angles: mrad->rad) and
BPMs (X and Y: mm -> m) and checks charge on the BPMs
if the charge below charge_thresh return False
:return: bool, True if the charge on all BPMs >= charge_thresh otherwise False
"""
self.mi_orbit.read_and_average(nreadings=1, take_last_n=1)
self.read_correctors()
charge_thresh = 0.005
bpms = self.get_dev_from_cb_state(self.bpms)
self.mi_orbit.get_bpms(bpms)
beam_on = True
for elem in bpms:
try:
if np.isnan(elem.x) or np.isnan(elem.y):
logger.warning("read bpm: " + elem.id + "NaN -> was unchecked")
elem.ui.uncheck()
charge = elem.charge
if charge < charge_thresh:
beam_on = False
if np.abs(charge/self.parent.bunch_charge) < self.parent.charge_tol/100:
logger.info(" BPM:" + elem.id + " unchecked -> " +str(np.round(charge, 2)) + "/" + str(np.round(self.parent.bunch_charge, 2)) + " < " + str(self.parent.charge_tol/100))
elem.ui.uncheck()
elem.ui.set_value((elem.x*1000, elem.y*1000))
except Exception as exc:
logger.error("read bpm: " + elem.id + " was unchecked. Error: " + str(exc))
elem.ui.uncheck()
self.update_plot()
return beam_on
def read_orbit(self):
if self.parent.mi.allow_star_operation is True:
return self.read_orbit_star()
else:
return self.read_orbit_one_by_one()
def calc_orbit(self):
"""
function calculates the orbit taking into account correctors strength
:return: None
"""
if self.online_calc == False:
return
for elem in self.parent.lat.sequence:
if elem.__class__ in [Hcor, Vcor] and hasattr(elem, 'ui'):
elem.kick_mrad = elem.ui.get_value()
kick_mrad_i = elem.ui.get_init_value()
warn = (np.abs(elem.kick_mrad) - np.abs(elem.ui.get_init_value())) > 0.5
elem.ui.check_values(elem.kick_mrad, elem.lims, warn=warn)
#angle = (elem.kick_mrad - kick_mrad_i)/1000.
kick_mrad = (elem.kick_mrad - kick_mrad_i)
elem.angle = elem.mi.hw2phys(kick_mrad)
#elem.transfer_map = self.parent.lat.method.create_tm(elem)
#self.update_cors_plot()
self.update_plot()
def create_Orbit_obj(self):
"""
function creates the Orbit object with correctors and bpms which are active in the GUI
Orbit - is object form ocelot.cpbd.orbit_correction
:return: Orbit
"""
self.orbit = Orbit(self.parent.lat, empty=True, rm_method=self.parent.mi.orm_method,
disp_rm_method=self.parent.mi.drm_method)
# setup correction method
if self.parent.solver_name == "SVD":
self.orbit.orbit_solver = OrbitSVD(epsilon_x=self.parent.svd_epsilon_x,
epsilon_y=self.parent.svd_epsilon_y)
else:
self.orbit.orbit_solver = MICADO(epsilon_x=self.parent.svd_epsilon_x,
epsilon_y=self.parent.svd_epsilon_y, epsilon_ksi=self.parent.epsilon_ksi)
# checking hardware of the correctors
self.check_hardware_status(self.corrs)
s_pos_min = np.min([cor.s for cor in self.corrs])
bpms = np.array(self.bpms)
corrs = np.array(self.corrs)
s_pos_max = np.max([bpm.s for bpm in bpms])
bpms_unch = bpms[np.array([bpm.s for bpm in self.bpms]) < s_pos_min]
corrs_unch = corrs[np.array([corr.s for corr in self.corrs]) > s_pos_max]
if self.parent.mi.uncheck_upstream_bpms:
[bpm.ui.uncheck() for bpm in bpms_unch]
[corr.ui.uncheck() for corr in corrs_unch]
bpms = self.get_dev_from_cb_state(bpms)
if len(bpms) == 0:
self.parent.error_box("No BPM. Check SUBTRAIN.")
return None
corrs = self.get_dev_from_cb_state(self.corrs)
if len(corrs) == 0:
self.parent.error_box("No correctors for correction")
return None
self.orbit.bpms = bpms
self.orbit.corrs = corrs
self.hcors = []
self.vcors = []
for cor in corrs:
if cor.__class__ == Hcor:
self.hcors.append(cor)
else:
self.vcors.append(cor)
self.orbit.hcors = self.hcors
self.orbit.vcors = self.vcors
self.orbit.setup_response_matrix()
self.orbit.setup_disp_response_matrix()
return self.orbit
def load_response_matrices(self):
"""
Method to load ORM and DRM from the file.
:return: True if the corresponding file exists and False if not
"""
logger.debug("load_response_matrices: path: " + self.parent.rm_files_dir + "RM_" + self.ui.cb_lattice.currentText() + ".p")
try:
self.orbit.response_matrix.load(self.parent.rm_files_dir + "RM_" + self.ui.cb_lattice.currentText() + ".p")
except:
logger.error("load_response_matrices: No Response Matrix")
return False
try:
self.orbit.disp_response_matrix.load(self.parent.rm_files_dir + "DRM_" + self.ui.cb_lattice.currentText() + ".p")
except:
logger.error("load_response_matrices: No Dispersion Response Matrix. Setting: self.orbit.disp_response_matrix = None")
self.orbit.disp_response_matrix = None
return True
return True
def is_rm_ok(self, orbit):
"""
Method to check and load if needed the RMs
:return: True - if shape of the ORM (!) is correct (shape of the DRM is not checked)
False - if the RM does not exist or RM load was failed
"""
#print(len(self.orbit.response_matrix.matrix))
if len(self.orbit.response_matrix.matrix) == 0:
is_ok = self.load_response_matrices()
logger.debug("is_rm_ok: tring to load response matrix ... Is OK? " + str(is_ok))
if not is_ok:
return is_ok
cor_list = [cor.id for cor in np.append(orbit.hcors, orbit.vcors)]
bpm_list = [bpm.id for bpm in orbit.bpms]
RM = None
try:
RM = self.orbit.response_matrix.extract(cor_list=cor_list, bpm_list=bpm_list)
except:
self.parent.error_box(message="Problem with RM. Recalcualte it. Load all section from min position to maximum and manually select all correctors and BPms.")
return False
if np.shape(RM)[0] != len(bpm_list)*2 or np.shape(RM)[1] != len(cor_list):
return False
else:
return True
def close_orbit(self):
"""
Method sets BPM.x_ref and BPM.y_ref from dictionary: self.golden_orbit
:return:
"""
n_bpms = len(self.orbit.bpms)
if n_bpms < 10:
self.ui.cb_close_orbit.setChecked(False)
for i, elem in enumerate(self.orbit.bpms[-self.parent.co_nlast_bpms:]):
elem.x_ref = elem.x
elem.y_ref = elem.y
logger.debug("close_orbit: set BPM to ref orbit: " + elem.id)
def set_values2correctors(self):
apply_fraction = self.ui.sb_apply_fraction.value()
self.online_calc = False
for cor in self.corrs:
kick_mrad_old = cor.ui.get_init_value()
if cor.id in self.calc_correction.keys():
cor.angle = self.calc_correction[cor.id]
delta_kick_mrad = cor.mi.phys2hw(cor.angle)*apply_fraction
#delta_kick_mrad = cor.angle*1000*apply_fraction
#print(cor.angle*1000, delta_kick_mrad)
new_kick_mrad = kick_mrad_old + delta_kick_mrad
cor.kick_mrad = new_kick_mrad
cor.ui.set_value(cor.kick_mrad)
if np.abs(delta_kick_mrad) > 0.001:
self.ui.table_cor.item(cor.row, 1).setForeground(QtGui.QColor(255, 101, 101)) # red
else:
self.ui.table_cor.item(cor.row, 1).setForeground(QtGui.QColor(255, 255, 255)) # white
warn = (np.abs(new_kick_mrad) - np.abs(kick_mrad_old)) > 0.5
cor.ui.check_values(cor.kick_mrad, cor.lims, warn)
self.online_calc = True
self.calc_orbit()
def single_shot_read_bpms(self):
# remove checking if the freeze checkBox is checked
# if not self.ui.cb_freeze_bpms.isChecked():
# self.parent.error_box("Freeze BPMs first")
# return
logger.info("Single Shot reading")
try:
self.read_correctors()
except Exception as e:
logger.critical("single_shot_read: read_correctors ERROR: " + str(e))
self.parent.error_box("Error in DOOCS during correctors reading")
self.xfel_mps.num_bunches_requested(num_bunches=1)
self.xfel_mps.beam_on()
time.sleep(0.2)
self.xfel_mps.beam_off()
time.sleep(0.5)
try:
self.mi_orbit.read_and_average(nreadings=1, take_last_n=1, reliable_reading=False, suffix=".HOLD")
except Exception as e:
logger.error("single_shot_orbit_read: mi_orbit.read_and_average()" + str(e))
raise
#time.sleep(0.1)
self.calculate_correction()
def multi_shot_read_bpms(self):
logger.info("Multi Shot reading")
try:
self.read_correctors()
except Exception as e:
logger.critical("multi_shot_read: read_correctors ERROR: " + str(e))
self.parent.error_box("Error in DOOCS during correctors reading")
self.xfel_mps.num_bunches_requested(num_bunches=1)
# first idle reading before real one.
#self.mi_orbit.read_and_average(nreadings=1, take_last_n=1)
self.xfel_mps.beam_on()
try:
self.mi_orbit.read_and_average(nreadings=self.parent.gc_nreadings, take_last_n=self.parent.gc_nlast, suffix="")
except Exception as e:
logger.error("read_bpms: mi_orbit.read_and_average() " + str(e))
raise
self.xfel_mps.beam_off()
self.calculate_correction()
def read_bpms(self):
if self.parent.single_shot_flag and (self.button_bpm != None and self.cavity_bpm != None):
self.single_shot_read_bpms()
else:
self.multi_shot_read_bpms()
def calculate_correction(self):
logger.debug("calculate_correction: .. ")
bpms = self.get_dev_from_cb_state(self.bpms)
checked_bpms_id = [bpm.id for bpm in bpms]
self.mi_orbit.get_bpms(bpms)
bpms = self.get_dev_from_cb_state(self.bpms)
charge_thresh = 0.005
for elem in bpms:
if np.isnan(elem.x) or np.isnan(elem.y):
logger.debug("calculate_correction: check BPM: " + elem.id + " NAN -> was unchecked")
elem.ui.uncheck()
if elem.charge < charge_thresh:
elem.ui.uncheck()
elem.ui.set_value((elem.x*1000, elem.y*1000))
self.update_plot()
self.uncheck_red()
self.correct()
for elem in bpms:
if elem.id in checked_bpms_id:
elem.ui.check()
logger.debug("calculate_correction: .. OK")
def read_and_correct(self):
logger.debug("read_and_correct ... ")
self.read_orbit()
self.uncheck_red()
self.correct()
logger.debug("read_and_correct ... OK")
def correct(self, reset=True):
"""
Method to the Orbit correction. Method calculate correctors strengths (kicks)
and call function to calculate (self.calc_orbit()) and draw orbit on the plot
but does not send it to the DOOCS server.
:return:
"""
logger.info("correct: ... ")
if reset:
self.orbit = self.create_Orbit_obj()
if self.orbit is None:
return
if not self.is_rm_ok(self.orbit):
self.parent.error_box("Calculate Response Matrix")
return 0
self.golden_orbit.dict2golden_orbit()
if self.ui.cb_close_orbit.isChecked():
self.close_orbit()
if self.parent.mi.analyse_correction is True:
self.parent.cor_analysis.initialization(mi_orbit=self.mi_orbit, orbit=self.orbit)
self.parent.cor_analysis.get_snapshot()
self.calc_correction = {}
for cor in self.corrs:
cor.angle = 0.
self.calc_correction[cor.id] = cor.angle
alpha = self.ui.sb_alpha.value()
# print("PARAMS: ", self.parent.svd_epsilon_x, self.parent.svd_epsilon_y, self.parent.svd_beta)
self.orbit.correction(alpha=alpha, p_init=None, beta=self.parent.svd_beta, print_log=False)
for cor in self.corrs:
self.calc_correction[cor.id] = cor.angle
self.set_values2correctors()
logger.info("correct: ... OK")
def start_stop_feedback(self):
"""
Method to start/stop feedback timer.
sb_feedback_sec - spinBox - set seconds for timer
pb_feedback - pushBatton Off/On
feedback_timer - timer
:return:
"""
delay = self.ui.sb_feedback_sec.value()*1000
if self.ui.pb_feedback.text() == "Orbit Keeper Off":
self.parent.feedback_timer.stop()
self.ui.pb_feedback.setStyleSheet("color: rgb(85, 255, 127);")
self.ui.pb_feedback.setText("Orbit Keeper On")
else:
self.parent.feedback_timer.start(delay)
self.ui.pb_feedback.setText("Orbit Keeper Off")
self.ui.pb_feedback.setStyleSheet("color: red")
def start_stop_feedback_new(self):
"""
Method to start/stop feedback timer.
sb_feedback_sec - spinBox - set seconds for timer
pb_feedback - pushBatton Off/On
feedback_timer - timer
:return:
"""
delay = self.ui.sb_feedback_sec.value()
if self.ui.pb_feedback.text() == "Orbit Keeper Off":
self.orbit_keeper.stop_event = True
self.orbit_keeper.stop()
self.ui.pb_feedback.setStyleSheet("color: rgb(85, 255, 127);")
self.ui.pb_feedback.setText("Orbit Keeper On")
else:
self.orbit_keeper.delay = delay
self.orbit_keeper.stop_event = False
self.orbit_keeper.start()
self.ui.pb_feedback.setText("Orbit Keeper Off")
self.ui.pb_feedback.setStyleSheet("color: red")
def auto_correction(self):
"""
Method repeats correction in a loop.
repetation rate is defined by spinBox - sb_feedback_sec
feedback_timer - QTimer to repats correction oin different thread
:return:
"""
beam_on = self.read_orbit()
time.sleep(0.01)
if beam_on:
self.correct()
time.sleep(0.01)
self.apply_kicks()
time.sleep(0.5)
else:
logger.info("auto_correction: no beam")
time.sleep(1)
def get_dev_from_cb_state(self, devs):
"""
Gets list of all pvs that have checked boxes.
Returns:
List of PV strings
"""
checked_devs = []
for dev in devs:
state = dev.ui.state()
#print(dev.id, state)
if state == 2:
checked_devs.append(dev)
return checked_devs
def check_hardware_status(self, devs):
"""
Check hardware status of the devices
Returns:
List of PV strings
"""
checked_devs = []
for dev in devs:
is_ok = dev.mi.is_ok()
if not is_ok and not self.dev_mode:
logger.warning(" harware_status fault: " + dev.id )
dev.ui.uncheck()
dev.ui.set_fault(True)
else:
dev.ui.set_fault(False)
def calc_response_matrix(self, do_DRM_calc=True):
"""
Method is connected to pushBatton pb_calc_RM and creates ResponseMatrixCalculator
which calculates ORM and DRM in different thread
self.parent.rm_files_dir - name of directory for RMs sore
self.ui.cb_lattice.currentText() - name of the sections (e.g. "I1D, L1, SASE1 and so on)
The method also launchs the Qtimer self.rm_calc to controle when the thread
ResponseMatrixCalculator finishs the calculations
:return:
"""
self.orbit = self.create_Orbit_obj()
if self.orbit == None:
return
self.RMs = ResponseMatrixCalculator(rm=self.orbit.response_matrix,
drm=self.orbit.disp_response_matrix)
self.RMs.do_DRM_calc = do_DRM_calc
self.ui.pb_correct_orbit.setText("RMs are calculated. Please Wait...")
self.ui.pb_correct_orbit.setStyleSheet("color: red")
self.RMs.tw_init = self.parent.tws0
self.RMs.rm_filename = self.parent.rm_files_dir + "RM_" + self.ui.cb_lattice.currentText() + ".p"
self.RMs.drm_filename = self.parent.rm_files_dir + "DRM_" + self.ui.cb_lattice.currentText() + ".p"
self.RMs.start()
self.rm_calc.start()
def is_rm_calc_alive(self):
"""
Method to check if the ResponseMatrixCalculator thread is alive.