-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFemtoAnalysis.py
1237 lines (1086 loc) · 51.2 KB
/
FemtoAnalysis.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
import ROOT
import FileUtils as FU
import FemtoDreamSaver as FDS
import FemtoDreamReader as FDR
import CorrelationHandler as CH
import CombinedTemplateFit as TF
def UFFA(settings):
conf = config(settings)
if conf['function'] == 'cf':
UFFA_cf(conf)
elif conf['function'] == 'tf':
UFFA_tf(conf)
elif conf['function'] == 'tf2d':
UFFA_tf2d(conf)
elif conf['function'] == 'ctf':
UFFA_ctf(conf)
elif conf['function'] == 'syst':
if conf['htype'] in ['mtmult', 'rew3d', '4d', 'rew4d']:
UFFA_syst_3d(conf)
else:
UFFA_syst(conf)
# correlation function
def UFFA_cf(settings):
conf = config(settings)
fdr = FDR.FemtoDreamReader(conf['fullpath'], conf['fileTDir'])
ch = cf_handler(fdr, conf)
fds = FDS.FemtoDreamSaver(conf, ch.get_histos())
# template fits
def UFFA_tf(settings):
conf = config(settings)
if conf['file']:
fdr1 = FDR.FemtoDreamReader(conf['fullpath'], conf['fileTDir'])
dca_data = fdr1.get_dca()
elif conf['data']:
dca_data = conf['data']
else:
print('UFFA_tf: Missing input data!')
if conf['templates']:
if type(conf['templates']) == str:
fdr2 = FDR.FemtoDreamReader(conf['templates'], conf['mcTDir'])
dca_mcplots = fdr2.get_dca_mc()
else:
dca_mcplots = conf['templates']
else:
dca_mcplots = fdr1.get_dca_mc()
fds = FDS.FemtoDreamSaver(settings)
ofile = fds.getFile()
TF.TemplateFit(ofile, dca_data, dca_mcplots, conf['tftype'], conf['namelist'], conf['fitrange'], conf['signalrange'], conf['bins'], conf['rebin'], conf['outDir'], conf['temp_init'], conf['temp_limits'], conf['temp_fraction'], conf['print'])
# template fits 2d
def UFFA_tf2d(settings):
conf = config(settings)
dca_data = conf['data']
dca_mcplots = conf['templates']
fds = FDS.FemtoDreamSaver(settings)
ofile = fds.getFile()
TF.TemplateFit2D(ofile, dca_data, dca_mcplots, conf['namelist'], conf['fitrange'], conf['signalrange'], conf['bins'], conf['rebin'], conf['outDir'], conf['temp_init'], conf['temp_limits'], conf['temp_fraction'], conf['print'], conf['debug'])
# combined template fits
def UFFA_ctf(settings):
conf = config(settings)
if conf['file']:
fdr1 = FDR.FemtoDreamReader(conf['fullpath'], conf['fileTDir'])
dca_data = fdr1.get_dca()
elif conf['data']:
dca_data = conf['data']
else:
print('UFFA_tf: Missing input data!')
if conf['templates']:
if type(conf['templates']) == str:
fdr2 = FDR.FemtoDreamReader(conf['templates'], conf['mcTDir'])
dca_mcplots = fdr2.get_dca_mc()
else:
dca_mcplots = conf['templates']
else:
dca_mcplots = fdr1.get_dca_mc()
fds = FDS.FemtoDreamSaver(settings)
ofile = fds.getFile()
TF.CombinedFit(ofile, conf['outDir'], dca_data, dca_mcplots, conf['namelist'], conf['fitrange'], conf['signalrange'], conf['bins'], conf['rebin'], conf['temp_init'], conf['temp_limits'], conf['temp_fraction'], conf['print'])
# systematics
def UFFA_syst(settings):
conf = config(settings)
fdr = FDR.FemtoDreamReader(conf['fullpath'], conf['fileTDir'])
# default cf
ch = cf_handler(fdr, conf)
cf, cf_unw = ch.get_cf() # [[cf, [rebins]], [bin2...], ...], [[cf unw, [rebins]], [bin2...], ...]
# input same event for yield filtering
if conf['yield']:
se = fdr.get_se()
pair_num_se = se.Integral(se.FindBin(0), se.FindBin(conf['yield'][0]))
if conf['debug']:
se_all = ch.get_se()
cf_list = []
if conf['rebin']:
len_rebin = len(conf['rebin'])
if conf['atype'] == 'int': # integrated
ck, ck_rebin = cf[0]
cf_list.append([ck, ck_rebin])
syst = [[Systematics(ck), []]] # [[syst cf, [rebins]]]
if conf['rebin']:
for i in range(len_rebin):
syst[0][1].append(Systematics(ck_rebin[i]))
elif conf['atype'] == 'dif': # differential
syst = []
for n, [ck, ck_rebin] in enumerate(cf):
cf_list.append([ck, ck_rebin])
syst.append([Systematics(ck), []]) # [[syst cf, [rebins]], [bin2...], ...]
if conf['rebin']:
for i in range(len_rebin):
syst[n][1].append(Systematics(ck_rebin[i]))
# loop over data variations in file and calculate the cf for each
# which is then saved in a th2 from which the systematic error is computed and saved in a th1
file_dir = fdr.get_dir();
fdr.cd(0) # class method of FileSaver to return to root of file
folders = fdr.get_folder_names()
for folder in folders:
fdr.cd(folder)
# allows to include/exclude specific variations
if conf['exclude'] and folder in conf['exclude']:
continue
elif conf['include']:
if folder in conf['include']:
pass
else:
continue
elif folder.rsplit('_')[-1][:3] != "Var":
continue
ch_var = cf_handler(fdr, conf)
cf_var, cf_var_unw = ch_var.get_cf()
if conf['debug']:
print("Variation: \"" + folder + "\"")
if conf['yield']:
se_var = fdr.get_se()
pair_num_var = se_var.Integral(se_var.FindBin(0), se_var.FindBin(conf['yield'][0]))
deviation = abs(pair_num_se - pair_num_var) / pair_num_se
if deviation > conf['yield'][1]:
if conf['debug']:
dev = deviation * 100
print("Integrated yield k*: [0, " + str(conf['yield'][0]) + ") differs by " + f"{dev:.1f} %")
if deviation > conf['yield'][1]:
print("Variation: Excluded!\n")
continue
if conf['debug'] and conf['htype'] != 'k':
se_var_all = ch_var.get_se()
tab = '\t'
print("Differential yield:")
for n, bin1 in enumerate(se_var_all):
yield_all = se_all[n][0].Integral()
yield_all_var = se_var_all[n][0].Integral()
deviation = (abs(yield_all - yield_all_var) / yield_all) * 100
print(f"{tab}{conf['htype']:s}: [{conf['bins'][n]:.2f}, {conf['bins'][n + 1]:.2f}) {tab} {deviation:5.2f} %")
print()
for n, [ck_var, ck_var_rebin] in enumerate(cf_var):
syst[n][0].AddVar(ck_var)
if conf['rebin']:
for i in range(len_rebin):
syst[n][1][i].AddVar(ck_var_rebin[i])
del ch_var
# generate th2 plots for systematics
for n in range(len(syst)):
syst[n][0].GenSyst()
if conf['rebin']:
for i in range(len_rebin):
syst[n][1][i].GenSyst()
syst_plots = [] # [[[cf, diff, syst, dev], [rebins]], [bin2...], ...]
for n in range(len(syst)):
syst_plots.append([syst[n][0].GetAll(), []])
if conf['rebin']:
for i in range(len_rebin):
syst_plots[n][1].append(syst[n][1][i].GetAll())
# generates the graphs with the systematic errors for the cf and the rebinned entries
tgraphs = []
for n, (hist, hist_rebin) in enumerate(cf_list):
tgraphs.append([ROOT.TGraphErrors(), []])
for i in range(1, hist.GetNbinsX() + 1):
tgraphs[n][0].SetName("CF_syst_graph")
tgraphs[n][0].SetPoint(i - 1, hist.GetBinCenter(i), hist.GetBinContent(i))
tgraphs[n][0].SetPointError(i - 1, 0, syst_plots[n][0][2].GetBinContent(i))
if conf['rebin']:
for i in range(len_rebin):
tgraphs[n][1].append(ROOT.TGraphErrors())
for j in range(1, hist.GetNbinsX() + 1):
tgraphs[n][1][i].SetName("CF_syst_graph")
tgraphs[n][1][i].SetPoint(j - 1, hist_rebin[i].GetBinCenter(j), hist_rebin[i].GetBinContent(j))
tgraphs[n][1][i].SetPointError(j - 1, 0, syst_plots[n][1][i][2].GetBinContent(j))
histos = (cf_list, syst_plots, tgraphs)
fds = FDS.FemtoDreamSaver(conf, histos)
# systematics
def UFFA_syst_3d(settings):
conf = config(settings)
fdr = FDR.FemtoDreamReader(conf['fullpath'], conf['fileTDir'])
# default cf
ch = cf_handler(fdr, conf)
histos = ch.get_cf_3d() # [[cf, [rebins]], [bin2...], ...], [[cf unw, [rebins]], [bin2...], ...]
# input same event for yield filtering
if conf['yield']:
se = fdr.get_se()
pair_num_se = se.Integral(se.FindBin(0), se.FindBin(conf['yield'][0]))
if conf['debug']:
se_all = ch.get_se_3d()
syst = []
syst_plots = []
cf_raw = []
if conf['rebin']:
len_rebin = len(conf['rebin'])
# create systematic object for all entries
for n, bin1 in enumerate(histos):
syst.append([])
for nn, [cf, cf_rebin] in enumerate(bin1):
syst[n].append([Systematics(cf), []])
if conf['rebin']:
for nnn in range(len_rebin):
syst[n][nn][1].append(Systematics(cf_rebin[nnn]))
# loop over data variations in file and calculate the cf for each
# which is then saved in a th2 from which the systematic error is computed and saved in a th1
file_dir = fdr.get_dir();
fdr.cd(0) # class method of FileSaver to return to root of file
folders = fdr.get_folder_names()
folder_counter = -1
for folder in folders:
fdr.cd(folder)
# include/exclude specific variations
if conf['exclude'] and folder in conf['exclude']:
continue
elif conf['include']:
if folder in conf['include']:
pass
else:
continue
elif folder.rsplit('_')[-1][:3] != "Var":
continue
ch_var = cf_handler(fdr, conf)
histos_var = ch_var.get_cf_3d()
if conf['debug']:
print("Variation: \"" + folder + "\"")
# compare integrated yields in given range
if conf['yield']:
se_var = fdr.get_se()
pair_num_var = se_var.Integral(se_var.FindBin(0), se_var.FindBin(conf['yield'][0]))
deviation = abs(pair_num_se - pair_num_var) / pair_num_se
if deviation > conf['yield'][1]:
if conf['debug']:
dev = deviation * 100
print("Integrated yield k*: [0, " + str(conf['yield'][0]) + ") differs by " + f"{dev:.1f} %")
if deviation > conf['yield'][1]:
print("Variation: Excluded!\n")
continue
if conf['debug']:
se_var_all = ch_var.get_se_3d()
tab = '\t'
for n, bin1 in enumerate(se_var_all):
print(f"Differential yield {conf['diff3d']:s}: [{conf['bins3d'][n]:.2f}, {conf['bins3d'][n + 1]:.2f})")
for nn, bin2 in enumerate(bin1):
yield_all = se_all[n][nn][0].Integral()
yield_all_var = se_var_all[n][nn][0].Integral()
deviation = (abs(yield_all - yield_all_var) / yield_all) * 100
print(f"{tab}{conf['diff3d2']:s}: [{conf['bins'][nn]:.2f}, {conf['bins'][nn + 1]:.2f}) {tab} {deviation:5.2f} %")
print()
if conf['interactive']:
option = input("Include [Y/n] ")
if option and option.lower()[0] == 'n':
print("\"" + folder + "\" excluded!\n")
continue
folder_counter += 1
cf_raw.append([]) # add entry for folder
# add rebinned variations
for n, bin1 in enumerate(histos_var):
cf_raw[folder_counter].append([])
for nn, [cf, cf_rebin] in enumerate(bin1):
cf_raw[folder_counter][n].append([cf.Clone("CF_" + folder.rsplit('_')[-1]), []])
syst[n][nn][0].AddVar(cf)
if conf['rebin']:
for nnn in range(len_rebin):
cf_raw[folder_counter][n][nn][1].append(cf_rebin[nnn].Clone("CF_" + folder.rsplit('_')[-1]))
syst[n][nn][1][nnn].AddVar(cf_rebin[nnn])
del ch_var
# generate th2 plots for systematics
for n, bin1 in enumerate(syst):
syst_plots.append([])
for nn, bin2 in enumerate(bin1):
syst[n][nn][0].GenSyst()
syst_plots[n].append([syst[n][nn][0].GetAll(), []])
if conf['rebin']:
for nnn in range(len_rebin):
syst[n][nn][1][nnn].GenSyst()
syst_plots[n][nn][1].append(syst[n][nn][1][nnn].GetAll())
# generates the graphs with the systematic errors for the cf and the rebinned entries
tgraphs = []
for n, bin1 in enumerate(histos):
tgraphs.append([])
for nn, [hist, hist_rebin] in enumerate(bin1):
tgraphs[n].append([ROOT.TGraphErrors(), []])
for nnn in range(1, hist.GetNbinsX() + 1):
tgraphs[n][nn][0].SetName("CF syst graph")
tgraphs[n][nn][0].SetPoint(nnn - 1, hist.GetBinCenter(nnn), hist.GetBinContent(nnn))
tgraphs[n][nn][0].SetPointError(nnn - 1, 0, syst_plots[n][nn][0][2].GetBinContent(nnn))
if conf['rebin']:
for nnn in range(len_rebin):
tgraphs[n][nn][1].append(ROOT.TGraphErrors())
for nnnn in range(1, hist.GetNbinsX() + 1):
tgraphs[n][nn][1][nnn].SetName("CF syst graph")
tgraphs[n][nn][1][nnn].SetPoint(nnnn - 1, hist_rebin[nnn].GetBinCenter(nnnn), hist_rebin[nnn].GetBinContent(nnnn))
tgraphs[n][nn][1][nnn].SetPointError(nnnn - 1, 0, syst_plots[n][nn][1][nnn][2].GetBinContent(nnnn))
all_histos = (histos, syst_plots, tgraphs, cf_raw)
fds = FDS.FemtoDreamSaver(conf, all_histos)
# class that returns the systematics of a cf
# add variations with AddVar(var) before calling GenSyst()
# GetAll() returns [th2 cf, th2 difference, th1 systematics, th1 std dev]
class Systematics():
counter = 0
ybins = 1200
def __init__(self, cf):
self._cf = cf
self._xaxis = cf.GetXaxis()
self._xbins = cf.GetNbinsX()
self._var = ROOT.TH2D("CF_th2", "CF_th2", self._xbins, self._xaxis.GetXmin(), self._xaxis.GetXmax(), Systematics.ybins, 0, 10)
self._dif = ROOT.TH2D("diff_th2", "diff_th2", self._xbins, self._xaxis.GetXmin(), self._xaxis.GetXmax(), Systematics.ybins, -5, 5)
self._sys = ROOT.TH1D("syst_th1", "syst_th1", self._xbins, self._xaxis.GetXmin(), self._xaxis.GetXmax())
self._dev = ROOT.TH1D("dev_th1", "dev_th1", self._xbins, self._xaxis.GetXmin(), self._xaxis.GetXmax())
Systematics.counter = Systematics.counter + 1
def AddVar(self, cf_var):
for i in range(1, self._xbins + 1):
if self._cf.GetBinCenter(i) > 3: # break over 3GeV
break
self._var.Fill(cf_var.GetBinCenter(i), cf_var.GetBinContent(i)) # fill th2 cf histo with variation
def GenSyst(self):
for i in range(1, self._xbins + 1):
if self._cf.GetBinCenter(i) > 3: # break over 3GeV
break
cf_proj = self._var.ProjectionY("cf_xbin" + str(i), i, i)
dev = cf_proj.GetStdDev()
self._dev.SetBinContent(i, dev)
cont_def = self._cf.GetBinContent(i)
var_min = cf_proj.GetBinCenter(cf_proj.FindFirstBinAbove(0))
var_max = cf_proj.GetBinCenter(cf_proj.FindLastBinAbove(0))
self._dif.SetBinContent(i, self._dif.GetYaxis().FindBin(var_min), 1)
self._dif.SetBinContent(i, self._dif.GetYaxis().FindBin(var_max), 1)
dif_proj = self._dif.ProjectionY("diff_xbin" + str(i), i, i)
proj_min = dif_proj.GetBinCenter(dif_proj.FindFirstBinAbove(0)) # minimum value of difference
proj_max = dif_proj.GetBinCenter(dif_proj.FindLastBinAbove(0)) # maximum value of difference
self._sys.SetBinContent(i, (proj_max - proj_min) / (12**0.5)) # assume a square distribution
self._var.SetDirectory(0)
self._dif.SetDirectory(0)
self._sys.SetDirectory(0)
self._dev.SetDirectory(0)
def SetBinning(self, n):
Systematics.ybins = n
def GetVar(self):
return self._var
def GetDiff(self):
return self._dif
def GetSyst(self):
return self._sys
def GetDev(self):
return self._dev
def GetAll(self):
return [self._var, self._dif, self._sys, self._dev]
# class that handles the retrieving of histos and computing of correlation functions
class cf_handler():
def __init__(self, FileReader, conf):
self._file = FileReader
self._pair = conf['pair']
self._atype = conf['atype'] # analysis type
self._htype = conf['htype'] # histo type
self._mc = conf['mc'] # bool monte carlo data
self._bins = conf['bins'] # bin range for differential
self._diff3d = conf['diff3d'] # which axis to split first in a 3D analysis
self._bins3d = conf['bins3d'] # bin range for the first differential split in case of a 3D analysis
self._rebin = conf['rebin'] # rebin factors for all se, me, cf plots
self._norm = conf['normalize'] # normalization range
self._perc = conf['percentile'] # percentile range
self._rew_range = conf['rewrange'] # reweighting range
self._name_se = conf['nameSE']
self._name_me = conf['nameME']
self._se = None
self._me = None
self._se_mc = None
self._me_mc = None
self._event = None
self._tracks = None
self._tracks_mc = None
self._v0 = None
self._get_histos()
# retrieves histos from the provided file reader
def _get_histos(self):
if self._name_se and self._name_me:
self._se = self._file.get_histo(self._name_se)
self._me = self._file.get_histo(self._name_me)
elif self._htype == 'k': # TH1 kstar
self._se, self._me = self._file.get_kstar()
if self._mc:
self._se_mc, self._me_mc = self._file.get_kstar_mc()
elif self._htype == 'mult': # TH2 k-mult
self._se, self._me = self._file.get_kmult()
if self._mc:
self._se_mc, self._me_mc = self._file.get_kmult_mc()
elif self._htype == 'mult3d': # TH3 k-mult
se, me = self._file.get_kmtmult()
self._se = se.Project3D("zx").Clone()
self._me = me.Project3D("zx").Clone()
if self._mc:
self._se_mc, self._me_mc = self._file.get_kmult_mc()
elif self._htype == 'mt': # TH2 k-mt
self._se, self._me = self._file.get_kmt()
if self._mc:
self._se_mc, self._me_mc = self._file.get_kmt_mc()
elif self._htype == 'mt3d':
se, me = self._file.get_kmtmult()
self._se = se.Project3D("yx").Clone()
self._me = me.Project3D("yx").Clone()
if self._mc:
self._se_mc, self._me_mc = self._file.get_kmt_mc()
elif self._htype in ['mtmult', 'rew3d']:
self._se, self._me = self._file.get_kmtmult()
if self._mc:
self._se_mc, self._me_mc = self._file.get_kmtmult_mc()
elif self._htype in ['4d', 'rew4d']:
self._se, self._me = self._file.get_4d()
if self._name_se and not self._name_me:
self._se = self._file.get_histo(self._name_se)
if self._name_me and not self._name_se:
self._me = self._file.get_histo(self._name_me)
if self._mc:
self._tracks_mc = self._file.get_tracks_mc()
if self._pair == 'pp':
self._event = self._file.get_event()
self._tracks = self._file.get_tracks()
elif self._pair == 'pl':
self._v0 = self._file.get_v0()
# computes the cf for integrated or differential analysis and for mc data
# and returns the histos for all the different options
# [histos, histos_unw, histos_mc, histos_unw_mc, self._event, self._tracks, self._tracks_mc]
def get_histos(self):
histos = []
histos_mc = []
histos_unw = []
histos_unw_mc = []
if self._atype == 'int': # integrated analysis
histos, histos_unw = getIntegrated(self._se, self._me, self._htype, self._rebin, self._norm, self._rew_range)
if self._mc:
histos_mc, histos_unw_mc = getIntegrated(self._se_mc, self._me_mc, self._htype, self._rebin, self._norm, self._rew_range)
elif self._atype == 'dif': # differential analysis
if self._htype == 'mtmult': # 3D differantial analysis
histos = getDifferential3D(self._se, self._me, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm)
elif self._htype == 'rew3d':
histos, histos_unw = getDiffReweight3D(self._se, self._me, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm, self._rew_range)
elif self._htype in ['4d', 'rew4d']:
se3d, me3d = getProj4d(self._se, self._me, self._perc)
if self._htype == '4d':
histos = getDifferential3D(se3d, me3d, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm)
else:
histos, histos_unw = getDiffReweight3D(se3d, me3d, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm, self._rew_range)
else:
histos = getDifferential(self._se, self._me, self._htype, self._bins, self._rebin, self._norm)
if self._mc:
histos_mc = getDifferential(self._se_mc, self._me_mc, self._htype, self._bins, self._rebin, self._norm)
return [histos, histos_unw, histos_mc, histos_unw_mc, self._event, self._tracks, self._tracks_mc, self._v0]
# returns a list of cf and their rebinned version
# [[cf, [rebin 1, rebin 2, ...]], [bin2...], ...] same for unweighted if integrated analysis
def get_cf(self):
histos = []
histos_unw = []
cf_list = []
cf_list_unw = []
# integrated analysis
if self._atype == 'int':
histos, histos_unw = getIntegrated(self._se, self._me, self._htype, self._rebin, self._norm)
if self._htype == 'mult':
cf_list_unw.append(histos_unw[1])
cf_list_unw.append([])
# differential analysis
elif self._atype == 'dif':
histos = getDifferential(self._se, self._me, self._htype, self._bins, self._rebin, self._norm)
cf_list.append([histos[1][2], []]) # cf, for differential 1st bin
# rebinned entries appended to the empty list for the first bin
if self._rebin:
for n in range(len(self._rebin)):
cf_list[0][1].append(histos[1][3][n][2]) # rebinned cf
if self._atype == 'int' and self._htype == 'mult':
cf_list_unw[1].append(histos_unw[2][n][1]) # rebinned unw cf for integrated
# repeat for the rest of the bins in case of differential analysis
if self._atype == 'dif':
for n in range(2, len(self._bins)):
cf_list.append([histos[n][2], []])
if self._rebin:
for nn in range(len(self._rebin)):
cf_list[n][1].append(histos[n][3][nn][2]) # rebinned cf appended to rebin list
return [cf_list, cf_list_unw]
# returns a list of se and their rebinned version
def get_se(self):
histos = []
se_list = []
# integrated analysis
if self._atype == 'int':
histos, histos_unw = getIntegrated(self._se, self._me, self._htype, self._rebin, self._norm)
# differential analysis
elif self._atype == 'dif':
histos = getDifferential(self._se, self._me, self._htype, self._bins, self._rebin, self._norm)
se_list.append([histos[1][0], []]) # se for differential 1st bin
# rebinned entries appended to the empty list for the first bin
if self._rebin:
for n in range(len(self._rebin)):
se_list[0][1].append(histos[1][3][n][0]) # rebinned se
# repeat for the rest of the bins in case of differential analysis
if self._atype == 'dif':
for n in range(1, len(self._bins) - 1):
se_list.append([histos[n][0], []])
if self._rebin:
for nn in range(len(self._rebin)):
se_list[n][1].append(histos[n][3][nn][0]) # rebinned se appended to rebin list
return se_list
# returns all the cf's for a 3D mt/mult histo
# [[[bin1-1 cf, [rebin cf]], [bin1-2 cf, [rebin cf]], ...], [[bin2-1 cf, [rebin cf]], [bin2-2 cf, [rebin cf]], ...], ...]
def get_cf_3d(self):
cf_list = []
se = self._se
me = self._me
if self._htype in ['4d', 'rew4d']:
se, me = getProj4d(self._se, self._me, self._perc)
if self._htype in ['rew3d', 'rew4d']:
histos, histos_unw = getDiffReweight3D(se, me, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm, self._rew_range)
else:
histos = getDifferential3D(se, me, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm)
histos = histos[1:] # remove TH3 histos
for n, bin1 in enumerate(histos):
cf_list.append([])
bin1 = bin1[1:] # remove TH2 histos
for nn, th1 in enumerate(bin1):
cf_list[n].append([th1[2], []])
if self._rebin:
for nnn in range(len(self._rebin)):
cf_list[n][nn][1].append(th1[3][nnn][2])
return cf_list
# returns all the cf's for a 3D mt/mult histo
def get_se_3d(self):
se_list = []
se = self._se
me = self._me
if self._htype in ['4d', 'rew4d']:
se, me = getProj4d(self._se, self._me, self._perc)
if self._htype in ['rew3d', 'rew4d']:
histos, histos_unw = getDiffReweight3D(se, me, self._diff3d, self._bins3d, self.bins, self._rebin, self._nrm, self._rew_range)
else:
histos = getDifferential3D(se, me, self._diff3d, self._bins3d, self._bins, self._rebin, self._norm)
histos = histos[1:] # remove TH3 histos
for n, bin1 in enumerate(histos):
se_list.append([])
bin1 = bin1[1:] # remove TH2 histos
for nn, th1 in enumerate(bin1):
se_list[n].append([th1[0], []])
if self._rebin:
for nnn in range(len(self._rebin)):
se_list[n][nn][1].append(th1[3][nnn][0])
return se_list
# splits th2 in section based on provided bins
def getBinRangeHistos(iSE, iME, bins):
"""
This function splits 2D histograms in the ranges
defined in the option 'bins'.
The output is a list of ["range", SE, ME] for each bin:
[[name, SE, ME], [bin2], ...]
where the name is a string containing the limits.
"""
yAxis = iSE.GetYaxis()
if type(bins) == list:
limits = []
for n in range(len(bins) - 1):
bin_low = yAxis.FindBin(bins[n])
bin_up = FU.find_bin_reduce_on_lower_edge(yAxis, bins[n + 1])
limits.append((bin_low, bin_up))
else:
print("Error in getBinRangeHistos: bin input \"" + str(bins) + "\" not a list of ranges!")
exit()
histos = []
for n in range(len(limits)):
name = "[%.2f-%.2f)" % (bins[n], bins[n + 1])
se = iSE.ProjectionX("se_k", limits[n][0], limits[n][1])
me = iME.ProjectionX("me_k", limits[n][0], limits[n][1])
histos.append([name, se.Clone(), me.Clone()])
return histos
# splits th3 in section based on provided bins
def getBinRangeHistos3D(iSE, iME, diff3d, bins3d):
"""
This function takes as input 3D SE and ME plots
and splits them into 2D plots in mt/mult according to 'diff3d'
in the ranges defined in 'bins3d'.
The output is a list of ["range", SE mt/mult vs k*, ME mt/mult vs k*] for each bin:
[[name, SE, ME], [bin2], ...]
where the name is a string containing the limits and SE, ME are 2D plots.
"""
if diff3d == 'mt':
diffAxisSE = iSE.GetYaxis()
diffAxisME = iME.GetYaxis()
projOpt = "zx"
elif diff3d == 'mult':
diffAxisSE = iSE.GetZaxis()
diffAxisME = iME.GetZaxis()
projOpt = "yx"
else:
print("Error in getBinRangeHistos: diff3d axis not known. Please choose either 'mt' or 'mult'")
exit()
if type(bins3d) == list:
limits = []
for n in range(len(bins3d) - 1):
bin_low = diffAxisSE.FindBin(bins3d[n])
bin_up = FU.find_bin_reduce_on_lower_edge(diffAxisSE, bins3d[n + 1])
limits.append((bin_low, bin_up))
else:
print("Error in getBinRangeHistos: bin input \"" + str(bins3d) + "\" not a list of ranges!")
exit()
histos = []
for n in range(len(limits)):
name = diff3d + ": [%.2f-%.2f)" % (bins3d[n], bins3d[n + 1])
diffAxisSE.SetRange(limits[n][0], limits[n][1])
diffAxisME.SetRange(limits[n][0], limits[n][1])
se = iSE.Project3D(projOpt)
me = iME.Project3D(projOpt)
histos.append([name, se.Clone(f"SE_{projOpt}_{name}"), me.Clone(f"ME_{projOpt}_{name}")])
return histos
# helper function for the CF
# output: [se, me, cf]
def getCorrelation(se, me, name, conf, norm = None):
ch = CH.CorrelationHandler(name, se, me)
#ch.normalize()
ch.make_cf()
minmax = norm if norm else [0.24, 0.34]
ch.normalize_cf(minmax[0], minmax[1])
se = ch.get_se().Clone("SE")
me = ch.get_me().Clone("ME")
cf = ch.get_cf().Clone("CF")
se.SetTitle(conf)
me.SetTitle(conf)
cf.SetTitle(conf)
del ch
return [se, me, cf]
# returns [[iSE, iME], [se, me, cf]] for a list of mt or mult ranges
# [[iSE, iME], [se, me, cf, [rebin: [...], [...], ...], [bin 2 [rebin]], ...]
def getDifferential(iSE, iME, htype, bins, rebin, norm, title = None):
histos = []
conf = "" if not title else title + " " # append to given name
if htype == 'mult':
conf += "mult: "
histos.append([iSE.Clone("SE_kmult"), iME.Clone("ME_kmult")])
elif htype == 'mt':
conf += "mt: "
histos.append([iSE.Clone("SE_kmT"), iME.Clone("ME_kmT")])
else:
print("getDifferential: no kmT or kmult input!")
exit()
mt_histos = getBinRangeHistos(iSE, iME, bins)
for n, [name, se, me] in enumerate(mt_histos, 1):
histos.append(getCorrelation(se, me, name, conf + name, norm))
histos[n].append([])
if rebin: # append a list of rebinned [se, me, cf] in the original [se, me, cf, []]
for factor in rebin:
se_rebin = rebin_hist(se, factor)
me_rebin = rebin_hist(me, factor)
rebin_conf = " rebin: " + str(factor)
histos[n][3].append(getCorrelation(se_rebin, me_rebin, name, conf + name + rebin_conf, norm))
return histos
# [[iSE, iME], [[1st proj SE, 1st proj ME], [se, me, cf, [rebin], [bin 2 [rebin]]]], ...]
def getDifferential3D(iSE, iME, diff3d, bins3d, bins, rebin, norm):
"""
This function takes as input 3D mult-mt-k* plots
and splits them first in mt/mult according to 'diff3d' in the limits defined in 'bins3d'.
The 2D mt/mult-k* plots are then projected in k* in the limits defined in 'bins'.
The output is a list of the mt bins, each bin is then a list of the mult bins
which include the SE, ME, CF and the rebinned plots:
[ [ [SE, ME, CF, [rebinned SE, ME, CF]], [mult/mt bin2], ... ], [mt/mult bin2], ... ]
"""
histos = []
histos.append([iSE.Clone("SE_kmTmult"), iME.Clone("ME_kmTmult")])
diff3d_histos = getBinRangeHistos3D(iSE, iME, diff3d, bins3d)
diff_2 = 'mult'
if (diff3d == "mult"):
diff_2 = 'mt'
for title, se, me in diff3d_histos:
histos.append(getDifferential(se, me, diff_2, bins, rebin, norm, title))
return histos
# [[iSE, iME], [[1st proj SE, 1st proj ME], [se, me, cf, [rebin], [bin 2 [rebin]]]], ...]
def getDiffReweight3D(iSE, iME, diff3d, bins3d, bins, rebin, norm, rew_range):
"""
This function takes as input 3D mult-mt-k* plots
and splits them first in mt according to the limits defined in 'bins3d'.
The 2D mult-k* plots are then reweighted in mult and splits according
to the limits defined in 'bins'.
The output is a list of the mt bins, each bin is then a list of the mult bins
which include the SE, ME, CF and the rebinned plots:
[ [ [SE, ME, CF, [rebinned SE, ME, CF]], [mult bin2], ... ], [mt bin2], ... ]
"""
histos = []
histos.append([iSE.Clone("SE_kmTmult"), iME.Clone("ME_kmTmult")])
diff_2 = 'mult'
if (diff3d == "mult"):
diff_2 = 'mt'
histos_unw = getDifferential3D(iSE, iME, diff3d, bins3d, bins, rebin, norm)
histos_diff3d = reweight3D(iSE, iME, diff3d, bins3d, rew_range)
for title, se, me in histos_diff3d:
histos.append(getDifferential(se, me, diff_2, bins, rebin, norm, title))
return histos, histos_unw
# returns a list of [[iSE, iME], [se, me, cf]] for rel pair k* input
# or reweights and returns ([[iSE, iME], [se, me, cf, [rebin]]], [me_unw, cf_unw, [rebin]]) for kmult
# and [[iSE, iME], [se, me, cf, [rebin]]] for kmT
def getIntegrated(iSE, iME, htype, rebin, norm, rew_range):
histos = []
histos_unw = []
if htype == 'k': # k* input
histos.append([iSE.Clone("SE_kstar"), iME.Clone("ME_kstar")])
se = iSE
me = iME
elif htype == 'mult': # kmult input
histos.append([iSE.Clone("SE_kmult"), iME.Clone("ME_kmult")])
hReweight = reweight(iSE, iME, rew_range)
se = hReweight[0]
me = hReweight[1]
me_unw = hReweight[2]
se_mult = hReweight[4]
me_mult = hReweight[5]
me_mult_unw = hReweight[6]
histos[0].append(se_mult.Clone("SE_mult"))
histos[0].append(me_mult.Clone("ME_mult"))
histos[0].append(me_mult_unw.Clone("ME_mult unw"))
elif htype == 'mt': # kmT input
histos.append([iSE.Clone("SE_kmT"), iME.Clone("ME_kmT")])
hReweight = reweight(iSE, iME, rew_range)
se = hReweight[0]
me = hReweight[2] # unweighted me, i.e. normal me projection of the kmT histo
histos.append(getCorrelation(se, me, "cf", "", norm))
if rebin: # append rebinned histos to list of histos
histos_rebin = []
for factor in rebin:
se_rebin = rebin_hist(se, factor)
me_rebin = rebin_hist(me, factor)
rebin_conf = " rebin: " + str(factor)
histos_rebin.append(getCorrelation(se_rebin, me_rebin, "rebin: " + str(factor), rebin_conf, norm))
histos[1].append(histos_rebin)
if htype == 'mult': # 2nd list with unweighted histos
se, me, cf = getCorrelation(se, me_unw, "cf_unw", "unweighted", norm)
histos_unw.append(me.Clone("ME_unw"))
histos_unw.append(cf.Clone("CF unw"))
histos_unw.append([])
if rebin: # append rebinned histos to list of histos
histos_rebin = []
for factor in rebin:
se_rebin = rebin_hist(se, factor)
me_rebin = rebin_hist(me, factor)
rebin_conf = " rebin: " + str(factor)
se_rebin, me_rebin, cf_rebin = getCorrelation(se_rebin, me_rebin, "rebin: " + str(factor), rebin_conf, norm)
histos_unw[2].append([me_rebin.Clone("ME_unw"), cf_rebin.Clone("CF_unw")])
return histos, histos_unw
# projects and reweights se and me from kmult histos
# returns [se, me, me unweighted, se mult, me mult, me mult unweighted]
def reweight(iSE, iME, rew_range):
"""
This function takes as input a 2D mult vs k* SE and ME distribution
and reweights the ME distribution in each bin projection of mt/mult.
The output is a list that includes all plots that can be generated:
[0] SE 1D k*
[1] ME 1D k* reweighted
[2] ME 1D k* unweighted
[3] ME 2D mt/mult vs k* reweighted
[4] SE 1D mt/mult
[5] ME 1D mt/mult reweighted
[6] ME 1D mt/mult unweighted
"""
me = iME.Clone("ME_kmult_reweighted")
me.Reset("ICESM")
me_axis = me.GetYaxis()
se_k = iSE.ProjectionX("se_k")
me_k = iME.ProjectionX("me_k")
int_min = 0
int_max = se_k.GetNbinsX()
if rew_range:
int_min = se_k.FindBin(rew_range[0])
int_max = se_k.FindBin(FU.get_bin_reduce_on_lower_edge(iME.GetXaxis(), rew_range[1]))
se_mult = iSE.ProjectionY("se_mult")
me_mult = iME.ProjectionY("me_mult")
me_k_unw = iME.ProjectionX("me_k_unw")
me_mult_unw = iME.ProjectionY("me_mult_unw")
me_k.Reset("ICESM")
me_mult.Reset("ICESM")
# loop for the projection of each multiplicity slice
for ybin in range(1, iSE.GetNbinsY()):
se_n = iSE.ProjectionX("se_bin", ybin, ybin)
me_n = iME.ProjectionX("me_bin", ybin, ybin)
se_int = se_n.Integral(int_min, int_max)
me_int = me_n.Integral(int_min, int_max)
if me_int > 0. and se_int > 0.:
me_n.Scale(se_int / me_int)
me_mult.SetBinContent(ybin, me_n.Integral(int_min, int_max))
me_k.Add(me_n)
for xbin in range(1, me_n.GetNbinsX() + 1): # fill th2 reweighted ME
#me.Fill(me_n.GetBinContent(xbin), me_axis.GetBinCenter(ybin))
#me.Fill(me_axis.GetBinCenter(ybin), me_n.GetBinContent(xbin))
me.SetBinContent(xbin, ybin, me_n.GetBinContent(xbin))
return [se_k, me_k, me_k_unw, me, se_mult, me_mult, me_mult_unw]
# split th3 in mt range and reweight each slice in multiplicity
# output: [[name, mult-k SE, reweighted mult-k ME], [bin 2], ...]
def reweight3D(iSE, iME, diff3d, bins3d, rew_range):
"""
This function takes as input a 3D SE and ME distribution
and splits them in mt by the provided binning.
The resulting 2D mult/k* plots are then reweighted.
The output is a list for the individual mt bins with the name (mt limits), SE, ME:
[[name, SE mult/k*, ME mult/k* reweighted], [bin 2], ...]
"""
histos = getBinRangeHistos3D(iSE, iME, diff3d, bins3d) # split th3 in mt and get a list of mult-k histos
out = []
for hist in histos:
out.append([hist[0], hist[1], reweight(hist[1], hist[2], rew_range)[3].Clone(hist[0])]) # append the reweighted th2 ME distribution
return out
# 4d percentile histos
def getProj4d(iSE, iME, perc_range):
se4d = iSE.Clone("4d_perc_se")
me4d = iME.Clone("4d_perc_me")
axis = se4d.GetAxis(3)
bin_low = axis.FindBin(perc_range[0])
bin_up = FU.find_bin_reduce_on_lower_edge(axis, perc_range[1])
se4d.GetAxis(3).SetRange(bin_low, bin_up)
me4d.GetAxis(3).SetRange(bin_low, bin_up)
se = se4d.Projection(0, 1, 2)
me = me4d.Projection(0, 1, 2)
return [se, me]
# returns rebinned copy of histo
def rebin_hist(input_histo, binning):
histo = input_histo.Clone()
histo = histo.Rebin(binning)
return histo
# generates list with rebin factors
def bin2list(rebin):
rebin_list = []
if type(rebin) == int or type(rebin) == str:
rebin = [rebin]
elif type(rebin) != list:
return None
rebin_list.extend(rebin)
return rebin_list
# generates the proper settings dictionary
def config(dic_conf):
"""
This function sets up all the configurable options
so that it is consitent and expandable.
Current options include:
"function": 'cf', 'syst', 'tf' -> for correlation function, systematics, template fits
"pair": 'pp', 'pl' -> for q&a plots relevant for individual analyses
"path": "string" -> full path to the root file, might include ~/ for home directory
"file": "string" -> name of the root file
"fullpath": "string" -> full path and file name equal to "path" + "file"
"outDir": "string" -> output directory
"rename": "string" -> rename output file
"fileTDir": "string" -> root file directory: path to directory inside the root file
"nameSE": "string" -> path + name of the se plot inside the provided "fileTDir" if given
"nameME": "string" -> same as nameSE but for the ME distribution
"newfile": 'new', 'recreate', 'update' -> same option as in ROOT, 'new' will rename if file already exists
"mc": 'true', 'false' -> save monte carlo data from provided root file
"mcTDir": "string" -> root file directory for the monte carlo data
"bins": [list of floats] -> binning for differential analysis
"diff3d": 'mt', 'mult' -> project 3D plots first in mt/mult 2D and after in mult/mt 1D or vice versa
"bins3d": [list of floats] -> binning for 3D plots to 2D plots
"yield": [GeV, Deviation] -> integrated analysis: include systematics inside deviation for the GeV range
"rebin": int or [list of ints] -> rebin output plots
for tf: int -> all dca/cpa rebinned with int
or [list of ints] -> each int will correspond to one range of the binning if provided
"atype": 'int', 'dif' -> integrated analysis or differential analysis
"htype": 'k', 'mt', 'mult', 'mt3d', 'mult3d', 'mtmult', 'rew3d'
'k' -> k* - relative pair momentum distribution
'mt' -> mt vs k* distribution
'mult' -> multiplicity vs k* distribution
'mt3d' -> mt vs k* from 3D distribution but integrated in mult
'mult3d' -> mult vs k* from 3D distribution but integrated in mt
'mtmult' -> mult vs mt vs k* 3D distribution
'rew3d' -> mult vs mt vs k* 3D differentially in mt and reweighted in mult
"tftype": 'dca', 'cpa' -> option for the template fit plots
"templates": [list of th1 plots] -> list of dca/cpa plots for fitting
"temp_init": list of values to initialize fitting parameters