-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
1124 lines (961 loc) · 30.6 KB
/
utils.py
File metadata and controls
1124 lines (961 loc) · 30.6 KB
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
import pandas as pd
from cmsdials import Dials
import cmsdials
from cmsdials.auth.bearer import Credentials
from cmsdials.filters import (
LumisectionHistogram1DFilters,
FileIndexFilters,
RunFilters,
LumisectionHistogram2DFilters,
)
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display
import plotly.express as px
import plotly.graph_objects as go
import math
from statistics import mean
from statistics import stdev
import plotly.io as pio
import os
import json
import oms
creds = Credentials.from_creds_file()
dials = Dials(creds)
with open("../clientid.json", "r") as file:
secrets = json.load(file)
os.environ["API_CLIENT_ID"] = secrets["API_CLIENT_ID"]
os.environ["API_CLIENT_SECRET"] = secrets["API_CLIENT_SECRET"]
oms_fetch = oms.oms_fetch()
if not os.path.exists("plots"):
os.mkdir("plots")
def data_1d_me(run_number, me, regex):
"""
Filters data by parameters below, sorts by ls_number, and converts it into numpy arrays
Parameters:
run_number, me, regex
Returns:
data, ls, x_min, x_max, x_bin, y_min, y_max
"""
# do we want to keep saving runs? I am doing it to save time when testing things
if not os.path.exists("saved_runs"):
os.mkdir("saved_runs")
file_name = f'saved_runs/{me.replace("/", "_")}_{run_number}.pkl'
if os.path.exists(file_name):
sorted_df = pd.read_pickle(file_name)
else:
data = dials.h1d.list_all(
LumisectionHistogram1DFilters(
run_number=run_number,
dataset__regex=regex,
me=me,
)
)
df = pd.DataFrame([x.__dict__ for x in data.results])
sorted_df = df.sort_values(by="ls_number")
sorted_df.to_pickle(file_name)
ls = sorted_df["ls_number"]
histbins = sorted_df["data"].to_numpy(dtype=np.ndarray)
histbins = np.array([np.array(x) for x in histbins])
x_min = sorted_df["x_min"][0]
x_max = sorted_df["x_max"][0]
x_bin = sorted_df["x_bin"][0]
y_min = histbins.min()
y_max = histbins.max()
return histbins, ls, x_min, x_max, x_bin, y_min, y_max
def calc_peak(histbins):
"""
Find the maximum height of the histogram
"""
peak = max(np.max(np.histogram(hist)[1]) for hist in histbins)
return peak
def plot_1d_me(
histbins,
histbins_ref,
me_name,
run_number,
ref_run,
x_min,
x_max,
x_bin,
write=False,
):
"""
Plot the 1D ME , with a dropdown menu to switch between current and ref run
Parameters:
histbins: data array for current run
histbins_ref: data array for ref run
run_number: current run
ref_run: reference run
write: Boolean (choose to save the plot to an html file or not)
Returns:
plots figure
writes it to an html file if write=True
"""
max_peak = max(calc_peak(histbins), calc_peak(histbins_ref))
# Create initial figure
fig = go.Figure()
# Add initial traces
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins[0],
name="Current Run",
visible=True,
)
)
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins_ref[0],
name="Reference Run",
visible=False,
)
)
# Function to create slider steps
def create_steps(data):
steps = []
for i in range(len(data)):
step = dict(method="update", args=[{"y": [data[i]]}], label=str(i))
steps.append(step)
return steps
# Create sliders for both datasets
slider_histbins = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins),
)
]
slider_histbins_ref = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins_ref),
)
]
y_axis_range = [0, max_peak]
# Initial slider configuration
fig.update_layout(
sliders=slider_histbins,
title=me_name,
xaxis_title=me_name.split("/")[-1],
yaxis_title="",
yaxis_range=y_axis_range,
)
# Add dropdown to switch between Current Run and Reference Run
fig.update_layout(
updatemenus=[
dict(
buttons=list(
[
dict(
args=[
{"visible": [True, False]},
{"sliders": slider_histbins},
],
label="Current Run " + str(run_number),
method="update",
),
dict(
args=[
{"visible": [False, True]},
{"sliders": slider_histbins_ref},
],
label="Reference Run " + str(ref_run),
method="update",
),
]
),
direction="down",
showactive=True,
),
]
)
fig.show()
if write:
html_content = pio.to_html(fig, include_plotlyjs="cdn")
# Write the HTML content to a file
name_hist = "plots/" + str(run_number) + "_" + me_name.split("/")[-1] + ".html"
with open(name_hist, "w") as f:
f.write(html_content)
def plot_1d_me_ref_overlay(
histbins,
histbins_ref,
me_name,
run_number,
ref_run,
x_min,
x_max,
x_bin,
write=False,
):
"""
Plot the 1D ME with averaged ref run overlaid
Parameters:
histbins: data array for current run
histbins_ref: data array for ref run
run_number: current run
ref_run: reference run
write: Boolean (choose to save the plot to an html file or not)
Returns:
plots figure
writes it to an html file if write=True
"""
max_peak = max(calc_peak(histbins), calc_peak(histbins_ref))
# calculate average of the ref:
avg_histbins_ref = np.mean(histbins_ref, axis=0)
# Create initial figure
fig = go.Figure()
# Add initial traces
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins[0],
name="Current Run" + str(run_number),
visible=True,
)
)
# Add reference trace
fig.add_trace(
go.Scatter(
x=np.linspace(x_min, x_max, int(x_bin)),
y=avg_histbins_ref,
name="Averaged Reference Run" + str(ref_run),
visible=True,
line=dict(color="red", width=1),
)
)
# Function to create slider steps
def create_steps(data):
steps = []
for i in range(len(data)):
step = dict(
method="update", args=[{"y": [data[i], avg_histbins_ref]}], label=str(i)
)
steps.append(step)
return steps
# Create sliders for current dataset
slider_histbins = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins),
)
]
y_axis_range = [0, max_peak]
# Initial slider configuration
fig.update_layout(
sliders=slider_histbins,
title=me_name,
xaxis_title=me_name.split("/")[-1],
yaxis_title="",
yaxis_range=y_axis_range,
)
# overlay ref
fig.show()
if write:
html_content = pio.to_html(fig, include_plotlyjs="cdn")
# Write the HTML content to a file
name_hist = (
"plots/" + str(run_number) + "_" + me_name.split("/")[-1] + "_overlay.html"
)
with open(name_hist, "w") as f:
f.write(html_content)
def plot_1d_me_normTrig(
histbins,
histbins_ref,
trigger_rate,
trigger_rate_ref,
me_name,
run_number,
ref_run,
x_min,
x_max,
x_bin,
write=False,
):
"""
Plot the 1D ME , with a dropdown menu to switch between current and ref run
Normalised with trigger rate
Parameters:
histbins: data array for current run
histbins_ref: data array for ref run
run_number: current run
ref_run: reference run
write: Boolean (choose to save the plot to an html file or not)
Returns:
plots figure
writes it to an html file if write=True
"""
histbins_norm = [
np.array(hist) / rate for hist, rate in zip(histbins, trigger_rate)
]
histbins_norm_ref = [
np.array(hist) / rate for hist, rate in zip(histbins_ref, trigger_rate_ref)
]
# histbins_norm = [remove_infinities(hist) for hist in histbins_norm]
# histbins_norm_ref = [remove_infinities(hist) for hist in histbins_norm_ref]
# max_peak = max(calc_peak(histbins_norm), calc_peak(histbins_norm_ref))
# print(max_peak)
max_peak = 1000 # need a way to calculate this
# Create initial figure
fig = go.Figure()
# Add initial traces
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins_norm[0],
name="Current Run",
visible=True,
)
)
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins_norm_ref[0],
name="Reference Run",
visible=False,
)
)
# Function to create slider steps
def create_steps(data):
steps = []
for i in range(len(data)):
step = dict(method="update", args=[{"y": [data[i]]}], label=str(i))
steps.append(step)
return steps
# Create sliders for both datasets
slider_histbins = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins_norm),
)
]
slider_histbins_ref = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins_norm_ref),
)
]
y_axis_range = [0, max_peak]
# Initial slider configuration
fig.update_layout(
sliders=slider_histbins,
title=me_name,
xaxis_title=me_name.split("/")[-1],
yaxis_title="",
yaxis_range=y_axis_range,
)
# Add dropdown to switch between Current Run and Reference Run
fig.update_layout(
updatemenus=[
dict(
buttons=list(
[
dict(
args=[
{"visible": [True, False]},
{"sliders": slider_histbins},
],
label="Current Run " + str(run_number),
method="update",
),
dict(
args=[
{"visible": [False, True]},
{"sliders": slider_histbins_ref},
],
label="Reference Run " + str(ref_run),
method="update",
),
]
),
direction="down",
showactive=True,
),
]
)
fig.show()
if write:
html_content = pio.to_html(fig, include_plotlyjs="cdn")
# Write the HTML content to a file
name_hist = (
"plots/" + str(run_number) + "_" + me_name.split("/")[-1] + "_normTrig.html"
)
with open(name_hist, "w") as f:
f.write(html_content)
def plot_1d_me_normTrig_ref_overlay(
histbins,
histbins_ref,
trigger_rate,
trigger_rate_ref,
me_name,
run_number,
ref_run,
x_min,
x_max,
x_bin,
write=False,
):
"""
Plot the 1D ME , normalised by trigger rate
Parameters:
histbins: data array for current run
histbins_ref: data array for ref run
run_number: current run
ref_run: reference run
write: Boolean (choose to save the plot to an html file or not)
Returns:
plots figure
writes it to an html file if write=True
"""
histbins_norm = [
np.array(hist) / rate for hist, rate in zip(histbins, trigger_rate)
]
histbins_norm_ref = [
np.array(hist) / rate for hist, rate in zip(histbins_ref, trigger_rate_ref)
]
# histbins_norm = [remove_infinities(hist) for hist in histbins_norm]
# histbins_norm_ref = [remove_infinities(hist) for hist in histbins_norm_ref]
# calculate average of the normalised ref:
avg_histbins_ref = np.mean(histbins_norm_ref, axis=0)
# max_peak = max(calc_peak(histbins_norm), calc_peak(histbins_norm_ref))
# print(max_peak)
max_peak = 1000 # need a way to calculate this
# Create initial figure
fig = go.Figure()
# Add initial traces
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins_norm[0],
name="Current Run" + str(run_number),
visible=True,
)
)
# Add reference trace
fig.add_trace(
go.Scatter(
x=np.linspace(x_min, x_max, int(x_bin)),
y=avg_histbins_ref,
name="Averaged Reference Run" + str(ref_run),
visible=True,
line=dict(color="red", width=1),
)
)
# Function to create slider steps
def create_steps(data):
steps = []
for i in range(len(data)):
step = dict(
method="update", args=[{"y": [data[i], avg_histbins_ref]}], label=str(i)
)
steps.append(step)
return steps
# Create sliders for both datasets
slider_histbins = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins_norm),
)
]
y_axis_range = [0, max_peak]
# Initial slider configuration
fig.update_layout(
sliders=slider_histbins,
title=me_name + ", Run " + str(run_number) + ", Ref: " + str(ref_run),
xaxis_title=me_name.split("/")[-1],
yaxis_title="",
yaxis_range=y_axis_range,
)
fig.show()
if write:
html_content = pio.to_html(fig, include_plotlyjs="cdn")
# Write the HTML content to a file
name_hist = (
"plots/"
+ str(run_number)
+ "_"
+ me_name.split("/")[-1]
+ "_normTrig_overlay.html"
)
with open(name_hist, "w") as f:
f.write(html_content)
def plot_1d_me_normTrig_ref_overlay_approval(
histbins,
histbins_ref,
trigger_rate,
trigger_rate_ref,
me_name,
run_number,
ref_run,
x_min,
x_max,
x_bin,
write=False,
):
"""
Plot the 1D ME , normalised by trigger rate, with better labels
Parameters:
histbins: data array for current run
histbins_ref: data array for ref run
run_number: current run
ref_run: reference run
write: Boolean (choose to save the plot to an html file or not)
Returns:
plots figure
writes it to an html file if write=True
"""
histbins_norm = [
np.array(hist) / rate for hist, rate in zip(histbins, trigger_rate)
]
histbins_norm_ref = [
np.array(hist) / rate for hist, rate in zip(histbins_ref, trigger_rate_ref)
]
# histbins_norm = [remove_infinities(hist) for hist in histbins_norm]
# histbins_norm_ref = [remove_infinities(hist) for hist in histbins_norm_ref]
# calculate average of the normalised ref:
avg_histbins_ref = np.mean(histbins_norm_ref, axis=0)
# max_peak = max(calc_peak(histbins_norm), calc_peak(histbins_norm_ref))
# print(max_peak)
max_peak = 700 # need a way to calculate this
# Create initial figure
fig = go.Figure()
font_size = 24
# Add initial traces
fig.add_trace(
go.Bar(
x=np.linspace(x_min, x_max, int(x_bin)),
y=histbins_norm[0],
name="Current Run",
visible=True,
)
)
# Add reference trace
fig.add_trace(
go.Scatter(
x=np.linspace(x_min, x_max, int(x_bin)),
y=avg_histbins_ref,
name="Averaged Reference Run " + str(ref_run),
visible=True,
line=dict(color="black", width=1),
)
)
# Function to create slider steps
def create_steps(data):
steps = []
for i in range(len(data)):
step = dict(
method="update", args=[{"y": [data[i], avg_histbins_ref]}], label=str(i)
)
steps.append(step)
return steps
# Create sliders for both datasets
slider_histbins = [
dict(
active=0,
currentvalue={"prefix": "LS: "},
pad={"t": 50},
steps=create_steps(histbins_norm),
)
]
y_axis_range = [0, max_peak]
# Initial slider configuration
fig.update_layout(
sliders=slider_histbins,
# title='On-Track Cluster Charge (Normalized)',
xaxis_title="On-Track Cluster Charge (electrons)",
yaxis_title="A.U.",
yaxis_range=y_axis_range,
# for styling
legend=dict(
x=0.94, # X coordinate of the legend (from 0 to 1)
y=0.98, # Y coordinate of the legend (from 0 to 1)
xanchor="right", # Horizontal anchor point of the legend
yanchor="top", # Vertical anchor point of the legend
font=dict(size=font_size + 8),
),
annotations=[
dict(
x=0,
y=1.09,
xref="paper",
yref="paper",
text="<b>CMS</b> <i>Preliminary</i>",
showarrow=False,
font=dict(size=font_size + 10),
),
dict(
x=1,
y=1.09,
xref="paper",
yref="paper",
text="2024 (13.6 TeV)",
showarrow=False,
font=dict(size=font_size + 10),
),
dict(
x=0.72,
y=0.6,
xref="paper",
yref="paper",
text="BPIX L" + str(me_name[-1]),
showarrow=False,
font=dict(size=font_size + 8),
),
],
xaxis=dict(
# tickvals=tickvals,
# ticktext=ticktext,
title_font=dict(
size=font_size + 4
), # Adjust the font size for x-axis title
gridcolor="lightgray",
),
yaxis=dict(
title_font=dict(
size=font_size + 4
), # Adjust the font size for y-axis title
gridcolor="lightgray",
),
plot_bgcolor="white",
paper_bgcolor="white",
width=1400,
height=800,
)
fig.update_xaxes(showline=True, mirror=True, linewidth=2, linecolor="black")
fig.update_yaxes(showline=True, mirror=True, linewidth=2, linecolor="black")
# end styling
fig.show()
if write:
html_content = f"""
<div style="text-align: left; font-size: 18px; width: 1400px; margin: auto;">
{pio.to_html(fig, include_plotlyjs='cdn')}
</div>
"""
# Write the HTML content to a file
name_hist = (
"/eos/user/s/sharmari/public/DIALS_Plots/approval/"
+ str(run_number)
+ "_"
+ me_name.split("/")[-1]
+ ".html"
)
with open(name_hist, "w") as f:
f.write(html_content)
def plot_rate(
hist, hist_ref, run_number, ref_run, x_axis, y_axis, title, me_name, write=False
):
plot = go.Figure()
custom_ticks = [x + 1 for x in range(len(hist))]
custom_ticks_ref = [x + 1 for x in range(len(hist_ref))]
plot.add_trace(go.Scatter(x=list(range(len(hist))), y=hist, visible=True))
plot.add_trace(go.Scatter(x=list(range(len(hist_ref))), y=hist_ref, visible=False))
tickvals, ticktext = get_x_labels(custom_ticks)
tickvals_ref, ticktext_ref = get_x_labels(custom_ticks_ref)
# Add dropdown
plot.update_layout(
title=title,
xaxis_title=x_axis,
yaxis_title=y_axis,
xaxis=dict(tickvals=tickvals, ticktext=ticktext),
updatemenus=[
dict(
buttons=list(
[
dict(
args=[
{"visible": [True, False]},
{
"xaxis": {
"tickvals": tickvals,
"ticktext": ticktext,
"title_text": x_axis,
}
},
],
label="Current Run " + str(run_number),
method="update",
),
dict(
args=[
{"visible": [False, True]},
{
"xaxis": {
"tickvals": tickvals_ref,
"ticktext": ticktext_ref,
"title_text": x_axis,
}
},
],
label="Reference Run " + str(ref_run),
method="update",
),
]
),
direction="down",
),
],
)
plot.show()
if write:
html_content = pio.to_html(plot, include_plotlyjs="cdn")
# Write the HTML content to a file
# name_hist = 'plots/'+str(run_number)+'_'+me_name.split('/')[-1]+'_'+title+'.html'
name_hist = "plots/" + str(run_number) + title + ".html"
with open(name_hist, "w") as f:
f.write(html_content)
# pio.write_html(fig, name_hist)
def get_oms_info(run_number):
"""
Get trigger rate from OMS as a df and convert it to numpy array
Parameters:
run_number
Returns:
trigger_rate: ZeroBias trigger rate
"""
extrafilter = dict(
attribute_name="dataset_name",
value="ZeroBias",
operator="EQ",
)
# attributes = [
# "start_time",
# "last_lumisection_number",
# "rate",
# "run_number",
# "last_lumisection_in_run",
# "first_lumisection_number",
# "dataset_name",
# "cms_active",
# "events",
# ]
omstrigger_json = oms.get_oms_data(
oms_fetch.omsapi,
"datasetrates",
run_number,
extrafilters=[extrafilter],
limit_entries=4000,
)
omstrigger_df = oms.oms_utils.makeDF(omstrigger_json)
trigger_rate = np.array(omstrigger_df["rate"])
return trigger_rate
def calc_trends(histbins, ls, max_value, trigger_rate, norm=False):
"""
Calculates ~peak value, std error on mean, and list of empty LS.
Also normalises by trigger rate if norm=True
Parameters:
histbins: data in the form of numpy array
ls: list of lumisections
max_value: maximum value of the quantity being measured along x-axis
trigger_rate: np array obtained from oms
norm: Boolean (default False)
Returns:
list_means: position of peak. It's not precise because the distributions are not Gaussian
list_std: standard error on mean
list_good_ls: non-zero LS
empty_ls:
"""
list_means = [] # peak
list_std = []
list_good_ls = []
empty_ls = []
good_trigger = []
for i in range(len(histbins)):
ls_num = ls[i]
# Calculate Mean and Std Error on Mean
if any(histbins[i]):
n_bins = len(histbins[i])
actual_values = np.linspace(0, max_value, n_bins, endpoint=True)
peak_value = np.average(
actual_values, weights=histbins[i]
) # needs modification to get actual peak?? Fit a Landau/Langaus and get the MPV
std_dev = stdev(histbins[i])
sem = std_dev / np.sqrt(
n_bins
) # standard error on mean = (standard deviation)/sqrt(n)
list_good_ls.append(ls_num)
list_means.append(peak_value)
list_std.append(sem)
good_trigger.append(trigger_rate[i])
else:
peak_value = 0
std_dev = 0
empty_ls.append(ls_num)
if norm:
list_means = np.divide(list_means, good_trigger)
list_std = np.divide(list_std, good_trigger)
return list_means, list_std, list_good_ls, empty_ls
def plot_trends(
list_raw, list_norm, list_good_ls, title, x_axis, me_name, run_number, write=False
):
plot = go.Figure()
custom_ticks = [x for x in list_good_ls]
plot.add_trace(go.Scatter(x=list(range(len(list_raw))), y=list_raw, visible=True))
plot.add_trace(
go.Scatter(x=list(range(len(list_norm))), y=list_norm, visible=False)
)
tickvals, ticktext = get_x_labels(custom_ticks)
# Add dropdown
plot.update_layout(
title=title + ", " + me_name + ", Run: " + str(run_number),
xaxis_title="LS",
xaxis=dict(tickvals=tickvals, ticktext=ticktext),
updatemenus=[
dict(
buttons=list(
[
dict(
args=[
{"visible": [True, False]},
{
"xaxis": {
"tickvals": tickvals,
"ticktext": ticktext,
"title_text": x_axis,
}
},
],
label="Raw",
method="restyle",
),
dict(
args=[
{"visible": [False, True]},
{
"xaxis": {
"tickvals": tickvals,
"ticktext": ticktext,
"title_text": x_axis,
}
},
],
label="Normalised",
method="restyle",
),
]
),
direction="down",
),
],
)
plot.show()
if write:
html_content = pio.to_html(plot, include_plotlyjs="cdn")