-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_GAN.py
2278 lines (1753 loc) · 93.8 KB
/
test_GAN.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Import trained generators and produce plots
#Files assumed:
#Data files for kaon and pion tracks (mod refers to additonal variables added):
# '../../data/mod-PID-train-data-KAONS.hdf'
# '../../data/mod-PID-train-data-PIONS.hdf'
#csv files for normalisation of data (shifts and divisors) calc from datafiles
# '../../data/KAON_norm.csv'
# '../../data/PION_norm.csv'
#May also use mask files to select test data:
#'../../GAN_training/GAN_7DLL/setx/unused_data_mask.csv'
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde
from keras.models import load_model
import math
from matplotlib.ticker import AutoMinorLocator
import time
import os
from matplotlib import gridspec
#Originally: set13.1 generator - works reasonably well for efficiency plot and DLL correlations to each other (both generated)
#set15 for PION
#Now with set17 for KAON (same as 13.1 but with 6DLLs)
#TrackP not correlated with input TrackP
#Time total run
t_init = time.time()
plt.rcParams['agg.path.chunksize'] = 10000 #Needed for plotting lots of data?
ref_particle = 'pi'
particle_source_1 = 'KAON'
particle_source_2 = 'PION'
train_frac = 0.7
examples=2000000
set_text = ""
unused_mask_loc_k = ''
unused_mask_loc_p = ''
RNN = False
sort_var = 'RICH1EntryDist0'
unused_mask_k = False
unused_mask_p = False
generate_P = False
alt_model_k = False
alt_model_p = False
gen_av = False
concat = False
subset=False
sub_var = 'RICH1ExitDist0'
sub_min = None
sub_max = 10
if subset:
subset_text = '_' + sub_var + '_' + str(sub_min) + '-' + str(sub_max)
else:
subset_text = ''
print("Loading generators...")
###############################################################################
#epochs = 250 Generate P, Pt
#generator_k = load_model('../../GAN_training/GAN_7DLL/set17/trained_gan.h5')
#set_text += "set17"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set15/trained_gan.h5')
#set_text += "set15"
#
#frac=0.025
#input_physical_vars = ['TrackP', 'TrackPt']
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs']
#generate_P = True
###############################################################################
#epochs = 500
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set18/trained_gan.h5')
#set_text += "set18"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set19/trained_gan.h5')
#set_text += "set19"
#
#frac=0.1
#input_physical_vars = ['TrackP', 'TrackPt']
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs']
#generate_P = True
###############################################################################
#epochs = 100
#generator_k = load_model('../../GAN_training/GAN_7DLL/set20/trained_gan.h5')
#set_text += "set20"
#frac=0.025
#input_physical_vars = ['TrackP', 'TrackPt']
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs']
###############################################################################
#epochs = 500
#
#batch_size = 128
#generator_k = load_model('../../GAN_training/GAN_7DLL/set22/trained_gan.h5')
#set_text += "set22"
#batch_size = 128
#generator_p = load_model('../../GAN_training/GAN_7DLL/set23/trained_gan.h5')
#set_text += "set23"
#batch_size = 1024
#generator_p = load_model('../../GAN_training/GAN_7DLL/set24/trained_gan.h5')
#set_text += "set24"
#batch_size = 32
#generator_p = load_model('../../GAN_training/GAN_7DLL/set25/trained_gan.h5')
#set_text += "set25"
#batch_size = 4096
#generator_p = load_model('../../GAN_training/GAN_7DLL/set26/trained_gan.h5')
#set_text += "set26"
#
#frac = 0.1
#input_physical_vars = ['TrackP', 'TrackPt']
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs']
###############################################################################
#epochs = 500
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set28/trained_gan.h5')
#set_text += "set28"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set27/trained_gan.h5')
#set_text += "set27"
#
#frac = 0.1
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs']
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#physical_vars = input_physical_vars
###############################################################################
#New data
#epochs = 500
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set31/trained_gan.h5')
#set_text += "set31"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set30/trained_gan.h5')
#set_text += "set30"
#
#frac = 0.1
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs']
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#physical_vars = input_physical_vars
###############################################################################
#epochs = 500
#generator_k = load_model('../../GAN_training/GAN_7DLL/set34/trained_gan.h5')
#set_text += "set34"
#
##generator_k = load_model('../../GAN_training/GAN_7DLL/set37/trained_gan.h5')
##set_text += "set37"
##alt_model_k = True
generator_k = load_model('../../GAN_training/GAN_7DLL/set44/trained_wgan.h5')
set_text += "set44"
alt_model_k = True
generator_p = load_model('../../GAN_training/GAN_7DLL/set35/trained_gan.h5')
set_text += "set35"
frac = 0.1
input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
'TrackRich1ExitZ']
physical_vars = input_physical_vars
DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
###############################################################################
#Added RICH2
#epochs = 500
#frac = frac = 1 #0.1
#generator_k = load_model('../../GAN_training/GAN_7DLL/set43/trained_gan.h5')
#set_text += "set43"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set45/trained_gan.h5')
#set_text += "set45"
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set58/trained_gan.h5')
#set_text += "set58"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set61/trained_gan.h5')
#set_text += "set61"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set63/trained_gan.h5')
#set_text += "set63"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set66/trained_gan.h5')
#set_text += "set66"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set64/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set64/penult_trained_gan.h5')
#set_text += "set64"
#set_text += "av"
#set_text += "alt"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set67/trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set67/penult_trained_gan.h5')
#set_text += "set67"
#set_text += "av"
#set_text += "alt"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set65/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set65/penult_trained_gan.h5')
#set_text += "set65"
#set_text += "av"
#set_text += "alt"
#generator_p = load_model('../../GAN_training/GAN_7DLL/set68/trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set68/penult_trained_gan.h5')
#set_text += "set68"
#set_text += "av"
#set_text += "alt"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set69/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set69/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set69/unused_data_mask.csv'
#set_text += "set69"
#set_text += "av"
##set_text += "alt"
#set_text += "new"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set79/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set79/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set79/unused_data_mask.csv'
#set_text += "set79"
#set_text += "av"
##set_text += "alt"
#set_text += "new"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set72/trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set72/penult_trained_gan.h5')
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set72/unused_data_mask.csv'
#set_text += "set72"
#set_text += "av"
##set_text += "alt"
#set_text += "new"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set70/trained_gan.h5')
##generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set70/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set70/unused_data_mask.csv'
#set_text += "set70"
##set_text += "av"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set73/trained_gan.h5')
##generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set73/penult_trained_gan.h5')
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set73/unused_data_mask.csv'
#set_text += "set73"
##set_text += "av"
##set_text += "alt"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set75/trained_gan.h5')
##generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set75/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set75/unused_data_mask.csv'
#set_text += "set75"
##set_text += "av"
##set_text += "alt"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set76/trained_gan.h5')
##generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set76/penult_trained_gan.h5')
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set76/unused_data_mask.csv'
#set_text += "set76"
##set_text += "av"
##set_text += "alt"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set88/half_trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set88/penult_half_trained_gan.h5')
#set_text += "set88"
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set88/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set88/penult_trained_gan.h5')
#set_text += "set88-1"
#
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set88/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set113/half_trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set113/penult_half_trained_gan.h5')
#set_text += "set113"
#generator_k = load_model('../../GAN_training/GAN_7DLL/set113/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set113/penult_trained_gan.h5')
#set_text += "set113-1"
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set113/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#alt_model_k = True
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set98/half_trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set98/penult_half_trained_gan.h5')
#set_text += "set98"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set98/trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set98/penult_trained_gan.h5')
#set_text += "set98-1"
#
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set98/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set117/trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set117/penult_trained_gan.h5')
#set_text += "set117"
#
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set117/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#alt_model_p = True
#gen_av = True
#concat = True
#unused_mask_k = True
#unused_mask_p = True
##
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
##
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ']
#
##physical_vars = input_physical_vars
#
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2', 'RICH1ConeNum',
# 'RICH2ConeNum']
###############################################################################
#All old and new data expect removed spacial coods
#epochs = 500
#frac = frac = 1 #0.1
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set59/trained_gan.h5')
#set_text += "set59"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set62/trained_gan.h5')
#set_text += "set62"
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set71/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set71/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set71/unused_data_mask.csv'
#set_text += "set71"
#set_text += "av"
##set_text += "alt"
#set_text += "test"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set74/trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set74/penult_trained_gan.h5')
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set74/unused_data_mask.csv'
#set_text += "set74"
#set_text += "av"
##set_text += "alt"
#set_text += "test"
#
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set118/half_trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set118/penult_half_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set118/unused_data_mask.csv'
#alt_model_k = True
#set_text += "set118"
#set_text += "av"
##set_text += "alt"
##set_text += "test"
##
#generator_p = load_model('../../GAN_training/GAN_7DLL/set119/half_trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set119/penult_half_trained_gan.h5')
#alt_model_p = True
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set119/unused_data_mask.csv'
#set_text += "set119"
#set_text += "av"
##set_text += "alt"
##set_text += "test"
#gen_av = True
#concat = True
#
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'RICH1EntryDist0', 'RICH1ExitDist0',
# 'RICH2EntryDist0', 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1',
# 'RICH2ExitDist1', 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2',
# 'RICH1ConeNum', 'RICH2ConeNum']
#
##physical_vars = input_physical_vars
#
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2', 'RICH1ConeNum',
# 'RICH2ConeNum']
###############################################################################
#Added corr info
#epochs = 500
#frac = 1 #0.1
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set47/trained_gan.h5')
#set_text += "set47"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set46/trained_gan.h5')
#set_text += "set46"
#
#
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2', 'RICH1ConeNum',
# 'RICH2ConeNum']
#
#physical_vars = input_physical_vars
#
###############################################################################
#Added corr info but only used NN data
#epochs = 500
#frac = 1 #0.1
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set48/trained_gan.h5')
#set_text += "set48"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set49/trained_gan.h5')
#set_text += "set49"
#
#Reduced inner layers to 4
#generator_k = load_model('../../GAN_training/GAN_7DLL/set50/trained_gan.h5')
#set_text += "set50"
#Reduced inner layers to 4
#generator_p = load_model('../../GAN_training/GAN_7DLL/set51/trained_gan.h5')
#set_text += "set51"
#Reduced inner layers to 6
#generator_k = load_model('../../GAN_training/GAN_7DLL/set52/trained_gan.h5')
#set_text += "set52"
#
#Reduced inner layers to 6
#generator_p = load_model('../../GAN_training/GAN_7DLL/set53/trained_gan.h5')
#set_text += "set53"
#
##Reduced inner layers to 4, frac = 0.2
#generator_k = load_model('../../GAN_training/GAN_7DLL/set54/trained_gan.h5')
#set_text += "set54"
#
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2']
#
#physical_vars = input_physical_vars
#physical_vars= ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2', 'RICH1ConeNum',
# 'RICH2ConeNum']
#
###############################################################################
#8 layers again, all data inc all NN data
#epochs = 500 #(1000 for full gen)
#frac = 1 #0.2
#generator_k = load_model('../../GAN_training/GAN_7DLL/set55/half_trained_gan.h5')
#set_text += "set55"
##generator_k = load_model('../../GAN_training/GAN_7DLL/set55/trained_gan.h5')
##set_text += "set55-1"
#
#set_text += "new"
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set56/half_trained_gan.h5')
#set_text += "set56"
#
##generator_p = load_model('../../GAN_training/GAN_7DLL/set56/trained_gan.h5')
##set_text += "set56-1"
#set_text += "new"
#
##gen_av = True
##concat = True
#
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2', 'RICH1ConeNum',
# 'RICH2ConeNum']
#
#physical_vars = input_physical_vars
###############################################################################
#RNN. Noise=250
##Choose GPU to use
#os.environ["CUDA_VISIBLE_DEVICES"]="1"
#
#RNN=True
#seq_length = 4 #Rows, default 32 (//4)
#batch_size = 128
#apparent_batch_size = batch_size - seq_length + 1
#sort_var = 'RICH1EntryDist0'
#
#generator_k = load_model('../../GAN_training/set90/trained_gan.h5')
##generator_k_2 = load_model('../../GAN_training/set90/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/set90/unused_data_mask.csv'
#set_text += "set90"
##set_text += "av"
###set_text += "alt"
#
#
#generator_p = load_model('../../GAN_training/set93/trained_gan.h5')
##generator_p_2 = load_model('../../GAN_training/set93/penult_trained_gan.h5')
#unused_mask_loc_p = '../../GAN_training/set93/unused_data_mask.csv'
#set_text += "set93"
##set_text += "av"
###set_text += "alt"
#generator_k = load_model('../../GAN_training/set96/trained_gan.h5')
##generator_k_2 = load_model('../../GAN_training/set96/penult_trained_gan.h5')
#unused_mask_loc_k = '../../GAN_training/set96/unused_data_mask.csv'
#set_text += "set96"
##set_text += "av"
###set_text += "alt"
#
#
#generator_p = load_model('../../GAN_training/set97/trained_gan.h5')
##generator_p_2 = load_model('../../GAN_training/set97/penult_trained_gan.h5')
#unused_mask_loc_p = '../../GAN_training/set97/unused_data_mask.csv'
#set_text += "set97"
##set_text += "av"
###set_text += "alt"
#
#
#unused_mask_k = True
#unused_mask_p = True
#gen_av = False
#concat = True
#
#
#input_physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ']
#
##physical_vars = input_physical_vars
#
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#
#physical_vars = ['TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX', 'TrackVertexY', 'TrackVertexZ',
# 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ', 'TrackRich1ExitX', 'TrackRich1ExitY',
# 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY', 'TrackRich2EntryZ', 'TrackRich2ExitX',
# 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0', 'RICH1ExitDist0', 'RICH2EntryDist0',
# 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1', 'RICH2ExitDist1',
# 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2', 'RICH1ConeNum',
# 'RICH2ConeNum']
###############################################################################
#All orig data and new data inc run/even num, alt models:
#generator_k = load_model('../../GAN_training/GAN_7DLL/set114/half_trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set114/penult_half_trained_gan.h5')
#set_text += "set114"
##
##generator_k = load_model('../../GAN_training/GAN_7DLL/set114/trained_gan.h5')
##generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set114/penult_trained_gan.h5')
##set_text += "set114-1"
#
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set114/unused_data_mask.csv'
#set_text += "av"
###set_text += "alt"
#alt_model_k = True
#
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set116/half_trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set116/penult_half_trained_gan.h5')
#set_text += "set116"
#
##generator_p = load_model('../../GAN_training/GAN_7DLL/set116/trained_gan.h5')
##generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set116/penult_trained_gan.h5')
##set_text += "set116-1"
#
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set116/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#alt_model_p = True
#
#
#generator_k = load_model('../../GAN_training/GAN_7DLL/set115/trained_gan.h5')
#generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set115/penult_trained_gan.h5')
#set_text += "set115"
#unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set115/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#alt_model_k = True
#unused_mask_k = True
#unused_mask_p = True
#gen_av = True
#concat = True
#
#input_physical_vars = ['RunNumber', 'EventNumber', 'TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX',
# 'TrackVertexY', 'TrackVertexZ', 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ',
# 'TrackRich1ExitX', 'TrackRich1ExitY', 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY',
# 'TrackRich2EntryZ', 'TrackRich2ExitX', 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RICH1EntryDist0',
# 'RICH1ExitDist0', 'RICH2EntryDist0', 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1',
# 'RICH2EntryDist1', 'RICH2ExitDist1', 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2',
# 'RICH2ExitDist2', 'RICH1ConeNum', 'RICH2ConeNum']
#
#DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
#
#physical_vars = input_physical_vars
#
###############################################################################
#WGAN testing... Need alt PION stuff
generator_k = load_model('../../GAN_training/GAN_7DLL/set103/trained_wgan.h5')
generator_k_2 = load_model('../../GAN_training/GAN_7DLL/set103/penult_trained_wgan.h5')
set_text += "set103"
unused_mask_loc_k = '../../GAN_training/GAN_7DLL/set103/unused_data_mask.csv'
set_text += "av"
##set_text += "alt"
alt_model_k = True
#
#
#generator_p = load_model('../../GAN_training/GAN_7DLL/set116/half_trained_gan.h5')
#generator_p_2 = load_model('../../GAN_training/GAN_7DLL/set116/penult_half_trained_gan.h5')
#set_text += "set116"
#
#unused_mask_loc_p = '../../GAN_training/GAN_7DLL/set116/unused_data_mask.csv'
#set_text += "av"
#set_text += "alt"
#alt_model_p = True
#
unused_mask_k = True
unused_mask_p = True
gen_av = True
concat = True
input_physical_vars = ['RunNumber', 'EventNumber', 'TrackP', 'TrackPt', 'NumLongTracks', 'NumPVs', 'TrackVertexX',
'TrackVertexY', 'TrackVertexZ', 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ',
'TrackRich1ExitX', 'TrackRich1ExitY', 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY',
'TrackRich2EntryZ', 'TrackRich2ExitX', 'TrackRich2ExitY', 'TrackRich2ExitZ']
DLLs = ['e', 'mu', 'k', 'p', 'd', 'bt']
physical_vars = input_physical_vars
###############################################################################
print("Generators loaded")
#Old dimensional stuff
#gen_input_dim = 100 #Dimension of random noise vector.
#phys_dim = len(physical_vars)
#input_phys_dim = len(input_physical_vars)
#DLLs_dim = len(DLLs)
#data_dim = DLLs_dim + phys_dim
#noise_dim = gen_input_dim - input_phys_dim
#New dim stuff
noise_dim = 100 #Dimension of random noise vector.
input_phys_dim = len(input_physical_vars)
phys_dim = len(physical_vars)
DLLs_dim = len(DLLs)
data_dim = DLLs_dim + phys_dim
gen_input_dim = noise_dim + input_phys_dim
input_phys_index = []
for i in range(input_phys_dim):
for k in range(phys_dim):
if input_physical_vars[i] == physical_vars[k]:
input_phys_index.append(k)
break
#If averaging, half examples each.
if gen_av:
if not concat:
examples = examples//2
# =============================================================================
# Functions
# =============================================================================
#Import all data via pandas from data files
#Inputs: Particle source e.g. KAONS corresponding to the datafile from which
# data will be imported
#Returns: pandas structure containing all variables from the source
def import_all_var(particle_source):
#Import data from kaons and pions
if(particle_source == 'KAON'):
datafile = '../../data/mod-PID-train-data-KAONS.hdf'
elif(particle_source == 'PION'):
datafile = '../../data/mod-PID-train-data-PIONS.hdf'
else:
print("Please select either kaon or pion as particle source")
data = pd.read_hdf(datafile, particle_source + 'S')
if subset:
if sub_min is not None:
if sub_max is not None:
bool_mask = (data[sub_var] >= sub_min & data[sub_var] <= sub_max)
else:
bool_mask = (data[sub_var] >= sub_min)
elif sub_max is not None:
bool_mask = (data[sub_var] <= sub_max)
else:
print("Subset set to true but no limits given!")
data = data[bool_mask]
return data
#Create sequences of data needed for RNN
#Inputs: data array to be made into sequences, length of sequences (=look_back)
#Returns: arrays of sequenced data, array containing the final row of each seq
#Note: Number of sequences = original number of rows - look_back + 1
# e.g. input 10 rows, lookback = 4 -> 7 output
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back+1):
#Extract [look_back] data rows starting from the ith row
a = dataset[i:(i+look_back), :]
dataX.append(a)
dataY.append(dataset[i + look_back - 1, :])
return np.array(dataX), np.array(dataY)
#Change DLLs e.g. from (K-pi) and (p-pi) to p-K
#Input: Two DLL arrays w.r.t. pi, to be changed s.t. the new DLL is w.r.t. the first particle in each DLL
#Returns: New DLL array e.g. DLL(p-K)
def change_DLL(DLL1, DLL2):
if(not np.array_equal(DLL1, DLL2)):
DLL3 = np.subtract(DLL1, DLL2)
else:
print("DLLs are the same!")
DLL3 = DLL1
return DLL3
#Import information needed to normalise all data to between -1 and 1
#Input: particle source i.e. either KAON or PION so read in respective csv file with vales
#Returns: div_num and shift needed to normalise all relevant data (DLLs and input physics)
def import_norm_info(particle_source):
#Read in csv datafile
data_norm = np.array(pd.read_csv('../../data/' + particle_source + '_norm.csv'))
#shift = [0,x], div_num = [1,x], where x starts at 1 for meaningful data
#Order of variables:
columns = ['RunNumber', 'EventNumber', 'MCPDGCode', 'NumPVs', 'NumLongTracks', 'NumRich1Hits',
'NumRich2Hits', 'TrackP', 'TrackPt', 'TrackChi2PerDof', 'TrackNumDof', 'TrackVertexX',
'TrackVertexY', 'TrackVertexZ', 'TrackRich1EntryX', 'TrackRich1EntryY', 'TrackRich1EntryZ',
'TrackRich1ExitX', 'TrackRich1ExitY', 'TrackRich1ExitZ', 'TrackRich2EntryX', 'TrackRich2EntryY',
'TrackRich2EntryZ','TrackRich2ExitX', 'TrackRich2ExitY', 'TrackRich2ExitZ', 'RichDLLe',
'RichDLLmu', 'RichDLLk', 'RichDLLp', 'RichDLLd', 'RichDLLbt', 'RICH1EntryDist0', 'RICH1ExitDist0',
'RICH2EntryDist0', 'RICH2ExitDist0', 'RICH1EntryDist1', 'RICH1ExitDist1', 'RICH2EntryDist1',
'RICH2ExitDist1', 'RICH1EntryDist2', 'RICH1ExitDist2', 'RICH2EntryDist2', 'RICH2ExitDist2',
'RICH1ConeNum', 'RICH2ConeNum']
#Create arrays for shift and div_num to be stored in. Only need to save DLLs and physics input values (data_dim)
shift = np.zeros(data_dim)
div_num = np.zeros(data_dim)
#Loop over all DLLs and input physics
for i in range(data_dim):
#First values correspond to DLLs
if i < DLLs_dim:
for j in range(len(columns)):
if columns[j] == 'RichDLL' + DLLs[i]:
shift[i] = data_norm[0,j+1]
div_num[i] = data_norm[1,j+1]
break
#Next set correspond to physics inputs
else:
for k in range(len(columns)):
if columns[k] == physical_vars[i-DLLs_dim]:
shift[i] = data_norm[0,k+1]
div_num[i] = data_norm[1,k+1]
break
return shift, div_num
#Normalise relevant data via dividing centre on zero and divide by max s.t. range=[-1,1]
#Input: Data array to be normalised (x) and particle source, so know which set of normalisation values to use
#Returns: Normalised data array (x) and shift/div_num used to do so (so can unnormalise later)
def norm(x, particle_source):
#Import normalistion arrays (shift and div_number) from csv file
shift, div_num, = import_norm_info(particle_source)
#For each column in input data array, normalise by shifting and dividing
for i in range(x.shape[1]):
x[:,i] = np.subtract(x[:,i], shift[i])
x[:,i] = np.divide(x[:,i], div_num[i])
return x, shift, div_num
#Get all relevant test data
#Inputs: List of DLLs of interest, list of physical vars of interest, particle source for data e.g. KAONS
#Returns: test data, as well as values used to normalise the data
def get_x_data(DLLs, ref_particle, physical_vars, particle_source, examples, unused_mask, unused_mask_loc):
#Get all data from particle source
all_data = import_all_var(particle_source)
#If using RNN, will need data to be sorted e.g. by TrackP
if RNN:
all_data = all_data.sort_values(by=sort_var,ascending=True)
#Total number of data rows
data_length = all_data.shape[0]
#Get first set of DLL data
DLL_data_1 = np.array(all_data.loc[:, 'RichDLL' + DLLs[0]])
#Create array to store all relevant data, starting with first DLL
x_data_dim = (data_length, data_dim)
x_data = np.zeros((x_data_dim))
x_data[:,0] = DLL_data_1
#Get other DLL data
for i in range(1, DLLs_dim):
x_data[:,i] = np.array(all_data.loc[:, 'RichDLL' + DLLs[i]])
#Get physics data
for i in range(DLLs_dim, data_dim):
phys_vars_index = i - DLLs_dim
x_data[:,i] = np.array(all_data.loc[:, physical_vars[phys_vars_index]])
#Have all data at this point, potentially sorted (if RNN)
#Now default to selecting [examples] number of rows for test data
#If have masks from training run, using inverse
if unused_mask:
unused_data_mask = np.array(pd.read_csv(unused_mask_loc))
unused_data_mask = np.array(unused_data_mask[:,1], dtype=bool)
#Apply this mask to x_data, leaving (10000000 * (1- frac * train_frac)) points remaining e.g. 9300000 for frac = 0.1, train_frac = 0.7
x_data_testable = x_data[unused_data_mask]
#Now reduce this down to [examples] through random selection
zero_arr =np.zeros(x_data_testable.shape[0] - examples, dtype=bool)
ones_arr = np.ones(examples, dtype=bool)
examples_mask = np.concatenate((zero_arr,ones_arr))
np.random.shuffle(examples_mask)
#Apply boolean mask
x_test = x_data_testable[examples_mask]
else:
zero_arr =np.zeros(data_length - examples, dtype=bool)
ones_arr = np.ones(examples, dtype=bool)
combined_01_arr = np.concatenate((zero_arr,ones_arr))
np.random.shuffle(combined_01_arr)
#Apply boolean mask
x_test = x_data[combined_01_arr]
#Normalise data. Shuffle not needed as all x_test used and order doesn't matter
x_test, shift, div_num = norm(x_test, particle_source)
return x_test, shift, div_num
# =============================================================================
# Plotting
# =============================================================================
#Make scatter plot w/ colour of correlations between two variables (e.g. DLLs)
def col_scatt(var1, var2, part_source_1, DLL_particle_1, ref_particle_1, part_source_2, DLL_particle_2, ref_particle_2, max_var_index, real_gen_text, x_range=None, y_range=None, zero_lines=0, save_index=0, size=1):
title = "./plots/" + set_text + "_" + part_source_1 + DLL_particle_1 + "_" + part_source_1 + DLL_particle_2 + "_" + real_gen_text + subset_text + "_colour.eps"
if real_gen_text == 'gen':
part_source_1_text = "Generated "
part_source_2_text = "Generated "
elif real_gen_text == 'real':
part_source_1_text = "Real "
part_source_2_text = "Real "
elif real_gen_text == 'real_gen':
part_source_1_text = "Generated "
part_source_2_text = "Real "
if part_source_1 == 'KAON':
part_source_1_text += 'Kaon'
elif part_source_1 == 'PION':
part_source_1_text += 'Pion'
if part_source_2 == 'KAON':
part_source_2_text += 'Kaon'
elif part_source_2 == 'PION':
part_source_2_text += 'Pion'
if ref_particle_1 == 'pi':
ref_particle_1_text = r'$\pi ) $'
elif ref_particle_1 == 'mu':
ref_particle_1_text = r'$\mu ) $'
elif ref_particle_1 == 'k':
ref_particle_1_text = 'K)'