forked from neumeist/twobody
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
1481 lines (1208 loc) · 47.3 KB
/
plotter.py
File metadata and controls
1481 lines (1208 loc) · 47.3 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
#!/usr/bin/env python
##########################################################################################
######
###### plotter module
######
###### adopted from a similar script for MET performance study
######
###### - containers for data and plots
###### - plotting routines
######
###### Use: from plotter import *
######
###### Author: Gena Kukartsev, October 2010
######
##########################################################################################
from __future__ import division
import sys, ROOT
from ROOT import TFile
from ROOT import TTree
from ROOT import TH1F
from ROOT import TH2F
from ROOT import TCanvas
from ROOT import TLegend
from ROOT import SetOwnership
from ROOT import THStack
from ROOT import TLatex
from ROOT import TStyle
from ROOT import TProfile
from ROOT import TBox
from ROOT import TCut
import operator
from array import array
from tdrStyle import *
from ROOT import gROOT
from ROOT import TGraph
from ROOT import TMultiGraph
from ROOT import TMath
from ROOT import TGraphErrors
from ROOT import TGraphAsymmErrors
from ROOT import TF1
import math
setlogscale = 1
def get_scale(scale_name, x):
if scale_name == 'cteq6l1_fit':
# 2010
a = 1.23524 - 0.132727*(x-1000.0)/1000.0
# 2011 1.1/fb
b = 1.23312 - 0.154901*(x-1000.0)/1000.0 +0.0516781*(x-1000.0)/1000.0*(x-1000.0)/1000.0
#print a,b,a-b
return b
elif scale_name == 'graviton':
return 1.6
elif scale_name == 'rs_acceptance_ee':
_a = 0.757
_b = -82.021
_c = 81.125
_a2 = 0.739
_b2 = -14.698
_c2 = -43.930
return (_a+_b/(x+_c)) / (_a2+_b2/(x+_c2))
elif scale_name == 'rs_acceptance_mumu':
_a = 0.878
_b = -50.1
_c = 22.8
_p0 = 0.901
_p1 = -830000.0
_p2 = -0.0000188
return (_a+_b/(x+_c)) / (_p0+_p1/x/x/x+_p2*x)
elif scale_name == 'unit':
return 1.0
else:
return None
def get_scale_err(scale_name, x):
if scale_name == 'cteq6l1_fit':
#return 0.0960395 + 0.0660697*math.log(x/1000.0)
return 0.0 + 0.00996035*math.log(x/250.0)
if scale_name == 'graviton':
return 0.10
else:
return None
# container for files and trees
class Data:
def __init__(self, lumi = None):
self.legend = '[Data]:'
self.lumi = lumi # /pb
self.cat=[] # master list of keys for all dictionaries
self.file = {}
self.x={} # dict of arrays
self.y={}
self.exh={}
self.exl={}
self.eyh={}
self.eyl={}
# errors for the 2sigma quantile
self.exh2={}
self.exl2={}
self.eyh2={}
self.eyl2={}
self.line_color={}
self.line_style={}
self.line_width={}
self.marker_color={}
self.marker_size={}
self.marker_style={}
self.fill_color={}
self.fill2_color={}
self.fill_style={}
self.fill2_style={}
self.tlegend={} # None if do not want to be added
self.legend_index={} # order in the legend
self.type={}
self.dofit={}
self.fit_min={}
self.fit_max={}
self.frame_fill_color = 19
def add(self, cat, filename, legend, scale = 1.0,
line_color = 1, line_style = 1, line_width = 1,
marker_color = 1, marker_size = 1.2, marker_style=8,
dofit=False, fit_min = 100, fit_max = 3000,
var_scale = None,
fill_style = 1002,
fill_color = None,
fill2_style = 1002,
fill2_color = ROOT.kRed,
extra_scale_name = 'unit',
legend_index = None):
if fill_color == None:
fill_color = line_color
self.type[cat] = 'observed'
self.dofit[cat] = dofit
self.fit_min[cat] = fit_min
self.fit_max[cat] = fit_max
self.cat.append(cat)
self.file = filename
self.tlegend[cat] = legend
#self.legend_index[cat] = legend_index
if legend_index:
self.legend_index[legend_index] = cat
else:
#self.legend_index[len(self.cat)+1] = cat
_keylist = self.legend_index.keys() # keys are indices
_keylist.sort()
print 'keylist:', _keylist
print 'keylist length:', len(_keylist)
if len(_keylist)>0:
_largest_index = _keylist[len(_keylist)-1]
else:
_largest_index = -1
print 'largest_index:', _largest_index
self.legend_index[_largest_index+1] = cat
self.line_color[cat] = line_color
self.line_style[cat] = line_style
self.line_width[cat] = line_width
self.marker_color[cat] = marker_color
self.marker_size[cat] = marker_size
self.marker_style[cat] = marker_style
self.fill_style[cat] = fill_style
self.fill2_style[cat] = fill2_style
self.fill_color[cat] = fill_color
self.fill2_color[cat] = fill2_color
_x = array('d')
_y = array('d')
_exh = array('d')
_exl = array('d')
_eyh = array('d')
_eyl = array('d')
with open(filename, 'r') as _file:
for line in _file:
#print self.legend, line
_buf = line.split()
if len(_buf) == 0:
continue
if _buf[0][0] == '#':
continue
_mass = float(_buf[0])
_scale = get_scale(var_scale, _mass)
# get SSM k-factor
#if cat=='SSM':
# print _mass, _scale,
_scale_err = get_scale_err(var_scale, _mass)
if _scale != None:
#print self.legend, 'mass =', _mass, 'k-factor =', _scale
_scale = _scale*scale
else:
_scale = scale
_xval = float(_buf[0])
_x.append(_xval)
# print SSM theory xsec
#if cat=='SSM':
# print ' ', float(_buf[1])
_yval = float(_buf[1])*_scale*get_scale(extra_scale_name, _xval)
_y.append(_yval)
# now fill errors (if any)
if _scale_err != None:
#print self.legend, 'mass =', _mass, 'k-factor =', _scale
_ey = _yval * _scale_err * get_scale(extra_scale_name, _xval)
else:
_ey = 0.0
_ex = 0
_eyh.append(_ey)
_eyl.append(_ey)
_exh.append(_ex)
_exl.append(_ex)
self.x[cat] = _x
self.y[cat] = _y
self.exh[cat] = _exh
self.exl[cat] = _exl
self.eyh[cat] = _eyh
self.eyl[cat] = _eyl
def add_function(self, cat, filename, legend, scale = 1.0,
line_color = 1, line_style = 1, line_width = 1,
marker_color = 1, marker_size = 1.2, marker_style=8,
fill_style = 1002,
fill_color = None,
fill2_style = 1002,
fill2_color = ROOT.kRed,
index_x = 0,
index_y = 1,
functor = None,
entry = None):
if fill_color == None:
fill_color = line_color
self.type[cat] = 'observed'
self.dofit[cat] = False
self.fit_min[cat] = None
self.fit_max[cat] = None
self.cat.append(cat)
self.file = filename
self.tlegend[cat] = legend
self.line_color[cat] = line_color
self.line_style[cat] = line_style
self.line_width[cat] = line_width
self.marker_color[cat] = marker_color
self.marker_size[cat] = marker_size
self.marker_style[cat] = marker_style
self.fill_style[cat] = fill_style
self.fill2_style[cat] = fill2_style
self.fill_color[cat] = fill_color
self.fill2_color[cat] = fill2_color
x_array = array('d')
y_array = array('d')
exh_array = array('d')
exl_array = array('d')
eyh_array = array('d')
eyl_array = array('d')
_nentries = {} # number of rows with a given mass (key:mass)
_ydict = {} # key: mass, value: list yval
_ydict_p = {}
_ydict_m = {}
with open(filename, 'r') as _file:
for line in _file:
#print self.legend, line
_buf = line.split()
if len(_buf) == 0:
continue
if _buf[0][0] == '#':
continue
_x_input = float( _buf[index_x].strip() )
_y_input = float( _buf[index_y].strip() )
_y_m = float( _buf[3].strip() )
_y_p = float( _buf[5].strip() )
_scale = scale
_xval = _x_input
_yval = _y_input * _scale
#print 'debug:', _mass
if _xval in _nentries:
_nentries[_xval] += 1
else:
_nentries[_xval] = 0
_ydict[_xval] = []
_ydict_m[_xval] = []
_ydict_p[_xval] = []
_ydict[_xval].append(_yval)
_ydict_m[_xval].append(_y_m)
_ydict_p[_xval].append(_y_p)
_keys = _nentries.keys()
_keys.sort()
#print _keys
#print _ydict
for _x in _keys:
x_array.append(_x)
if functor:
_y = functor(_x, _ydict)
elif entry:
# normalized residual
#_y = math.fabs(1.0 - _ydict[_x][entry]/_ydict[_x][0])
#residual as a fraction of expected band
_y = 2.0*( _ydict[_x][0] - _ydict[_x][entry]) / (_ydict_p[_x][entry] - _ydict_m[_x][entry])
#print _y
else:
_y = _ydict[_x][0]
y_array.append(_y)
eyh_array.append(0)
eyl_array.append(0)
exh_array.append(0)
exl_array.append(0)
self.x[cat] = x_array
self.y[cat] = y_array
self.exh[cat] = exh_array
self.exl[cat] = exl_array
self.eyh[cat] = eyh_array
self.eyl[cat] = eyl_array
def add_median(self, cat, filename, legend, scale = 1.0,
line_color = 1, line_style = 1, line_width = 1,
marker_color = 1, marker_size = 1.2, marker_style=8,
dofit=False, fit_min = 100, fit_max = 3000,
var_scale = None,
fill_style = 1002,
fill_color = None,
fill2_style = 1002,
fill2_color = ROOT.kRed,
extra_scale_name = 'unit',
value_type = 'median',
error_type = 'quantile',
legend_index = None):
#
# same as add() but for multiple values
#
if fill_color == None:
fill_color = line_color
self.type[cat] = 'observed'
self.dofit[cat] = dofit
self.fit_min[cat] = fit_min
self.fit_max[cat] = fit_max
self.cat.append(cat)
self.file = filename
self.tlegend[cat] = legend
#self.legend_index[cat] = legend_index
if legend_index:
self.legend_index[legend_index] = cat
else:
self.legend_index[len(self.cat)+1] = cat
self.line_color[cat] = line_color
self.line_style[cat] = line_style
self.line_width[cat] = line_width
self.marker_color[cat] = marker_color
self.marker_size[cat] = marker_size
self.marker_style[cat] = marker_style
self.fill_style[cat] = fill_style
self.fill2_style[cat] = fill2_style
self.fill_color[cat] = fill_color
self.fill2_color[cat] = fill2_color
_x = array('d')
_y = array('d')
_exh = array('d')
_exl = array('d')
_eyh = array('d')
_eyl = array('d')
_d = [] # list of tuples, one tuple per mass point
with open(filename, 'r') as _file:
_mass = None
_arr = array('d')
_prev_limit = 0.0
for line in _file:
#print self.legend, line
_buf = line.split()
if _buf[0][0] == '#':
continue
#_mass = float(_buf[0])
aMass = float(_buf[0])
_scale = get_scale(var_scale, aMass)
_scale_err = get_scale_err(var_scale, aMass)
if _scale != None:
_scale = _scale*scale
else:
_scale = scale
#_xval = float(_buf[0])
#_x.append(_xval)
#_yval = float(_buf[1])*_scale*get_scale(extra_scale_name, _xval)
aLim = float(_buf[1])*_scale*get_scale(extra_scale_name, aMass)
#_y.append(_yval)
if _mass == None:
_mass = aMass
if abs(_mass-aMass) > 0.1:
_d.append((_mass, _arr))
#print self.legend, cat, 'mass =', _mass, 'PEs per point:', len(_arr)
_mass = aMass
_arr=array('d')
#if abs(aLim - _prev_limit) < 0.000001:
# #print self.legend, 'WARNING: duplicate expected limit, ignoring...'
# continue
#_arr.append(float(_buf[1])*scale)
_arr.append(aLim)
_prev_limit = aLim
_d.append((_mass, _arr))
for t in _d:
_x.append(t[0])
_mean = TMath.Mean(len(t[1]), t[1])
_median = TMath.Median(len(t[1]), t[1])
_rms = TMath.RMS(len(t[1]), t[1])
_nrms = _rms/len(t[1])
# cross check
#CheckBimodal(t)
#print self.legend, t[0], _mean, _rms, _nrms
if value_type == 'mean':
_value = _mean
_y.append(_mean)
elif value_type == 'median':
_value = _median
_y.append(_median)
_exh.append(0)
_exl.append(0)
if error_type == 'rms':
_eyh.append(_rms/2)
_eyl.append(_rms/2)
elif error_type == 'quantile':
_prob = array('d')
_prob.append(0.021)
_prob.append(0.159)
_prob.append(0.841)
_prob.append(0.979)
_nprob = 4
_quantiles = array('d')
_quantiles.append(0)
_quantiles.append(0)
_quantiles.append(0)
_quantiles.append(0)
TMath.Quantiles(len(t[1]), _nprob, t[1], _quantiles, _prob)
#print self.legend, 'Quantiles:', _quantiles
#_eyh.append(abs(_value-_quantiles[2]))
#_eyl.append(abs(_value-_quantiles[1]))
_eyh.append(0)
_eyl.append(0)
# dumping to text file
#print t[0], _median
self.x[cat] = _x
self.y[cat] = _y
self.exh[cat] = _exh
self.exl[cat] = _exl
self.eyh[cat] = _eyh
self.eyl[cat] = _eyl
def add_with_errors(self, cat, filename, legend, scale = 1.0,
line_color = 1, line_style = 1, line_width = 1,
marker_color = 1, marker_size = 1.2, marker_style=8,
dofit=False, fit_min = 100, fit_max = 3000,
var_scale = None,
fill_style = 1002,
fill_color = None,
fill2_style = 1002,
fill2_color = ROOT.kRed,
extra_scale_name = 'unit',
index_x = 0,
index_y = 1,
index_ex_up = None,
index_ey_up = None,
index_ex_down = None,
index_ey_down = None,
n_entry = 1, # number of entry if multiple entries with the same value of x
legend_index = None
):
if fill_color == None:
fill_color = line_color
self.type[cat] = 'observed'
self.dofit[cat] = dofit
self.fit_min[cat] = fit_min
self.fit_max[cat] = fit_max
self.cat.append(cat)
self.file = filename
self.tlegend[cat] = legend
#self.legend_index[cat] = legend_index
if legend_index:
self.legend_index[legend_index] = cat
else:
self.legend_index[len(self.cat)+1] = cat
self.line_color[cat] = line_color
self.line_style[cat] = line_style
self.line_width[cat] = line_width
self.marker_color[cat] = marker_color
self.marker_size[cat] = marker_size
self.marker_style[cat] = marker_style
self.fill_style[cat] = fill_style
self.fill2_style[cat] = fill2_style
self.fill_color[cat] = fill_color
self.fill2_color[cat] = fill2_color
_x = array('d')
_y = array('d')
_exh = array('d')
_exl = array('d')
_eyh = array('d')
_eyl = array('d')
_nentries = {} # number of rows with a given mass
with open(filename, 'r') as _file:
for line in _file:
#print self.legend, line
_buf = line.split()
if _buf[0][0] == '#':
continue
_mass = float(_buf[index_x])
# only add if entry has the right number, .i.e. "third 600"
#print 'debug:', _mass
if _mass in _nentries:
_nentries[_mass] += 1
else:
_nentries[_mass] = 1
#print 'debug:', _nentries
if _nentries[_mass] != n_entry:
continue
_scale = None
_scale_err = None
if var_scale:
_scale = get_scale(var_scale, _mass)
_scale_err = get_scale_err(var_scale, _mass)
if _scale != None:
#print self.legend, 'mass =', _mass, 'k-factor =', _scale
_scale = _scale*scale
else:
_scale = scale
_xval = float(_buf[index_x])
_x.append(_xval)
_yval = float(_buf[index_y])*_scale*get_scale(extra_scale_name, _xval)
_y.append(_yval)
# now fill errors (if any)
if index_ex_up:
_ex_up = float(_buf[index_ex_up])
else:
_ex_up = 0.0
if index_ey_up:
_ey_up = float(_buf[index_ey_up])-_yval
else:
_ey_up = 0.0
if index_ex_down:
_ex_down = float(_buf[index_ex_down])
else:
_ex_down = 0.0
if index_ey_down:
_ey_down = _yval-float(_buf[index_ey_down])
else:
_ey_down = 0.0
# additional errors if any
if _scale_err != None:
#print self.legend, 'mass =', _mass, 'k-factor =', _scale
_ey_extra = _yval * _scale_err * get_scale(extra_scale_name, _xval)
_ey_up = math.sqrt(_ey_extra*_ey_extra + _ey_up*_ey_up)
_ey_down = math.sqrt(_ey_extra*_ey_extra + _ey_down*_ey_down)
_eyh.append(_ey_up)
_eyl.append(_ey_down)
_exh.append(_ex_up)
_exl.append(_ex_down)
self.x[cat] = _x
self.y[cat] = _y
self.exh[cat] = _exh
self.exl[cat] = _exl
self.eyh[cat] = _eyh
self.eyl[cat] = _eyl
def smooth_band(self, x, y, m, excl,
xmin = 300.0, xmax = 1000,
mode = 'low'):
#
# Smooth out a line defined by a set of points
# Returns an array of "y" for the same x
#
legend = '[smooth]:'
print legend, 'smoothing...'
_y = array('d')
print legend, y
_shifted = array('d') # actual line for smoothing
x_ = array('d') # x with excluded points
_c = 0
for y_ in y:
#print 300+100*_c,m[_c],y[_c]
if _c not in excl:
x_.append(x[_c])
if mode=='low':
_shifted.append(m[_c] - y[_c])
else:
_shifted.append(m[_c] + y[_c])
_c += 1
_g = TGraph(len(x_), x_, _shifted)
f1 = TF1("f1", "[0]+[1]*exp([2]*x+[3])", xmin, xmax)
_fr = _g.Fit("f1", "MEWS", "", xmin, xmax)
_fr.Print()
c = TCanvas()
_g.SetMarkerStyle(20)
_g.SetMarkerSize(1.5)
_g.Draw('AP')
raw_input('press <enter> to continue...')
_count = 0
for _x in x:
if _x > (xmin-0.1) and _x < (xmax+0.1):
print legend, y[_count], f1.Eval(_x)
if mode=='low':
_y.append( m[_count] - f1.Eval(_x) )
else:
_y.append( f1.Eval(_x) - m[_count] )
else:
_y.append( y[_count] )
_count += 1
return _y
def add_expected(self, cat, filename, legend, scale = 1.0,
line_color = 1, line_style = 1, line_width = 1,
marker_color = 1, marker_size = 1.2, marker_style=8,
value_type = 'median',
error_type = 'rms',
fill_style = 1002,
fill_color = ROOT.kYellow,
fill2_style = 1002,
fill2_color = ROOT.kRed,
extra_scale_name = 'unit',
smooth = None, # 'single' or 'comb' or None: special tricks for smoothing expected bands
legend_index = None):
self.type[cat] = 'expected'
#legend_index = None # safety - not implemented right yet
#self.cat.append(cat)
self.cat.append(cat)
#self.cat.append(cat+'1sig')
#self.cat.append(cat+'2sig')
self.file = filename
#self.tlegend[cat] = legend
self.tlegend[cat] = 'median expected'
self.tlegend[cat+'1sig'] = '68% expected'
self.tlegend[cat+'2sig'] = '95% expected'
#self.legend_index[cat] = legend_index
if legend_index:
self.legend_index[legend_index] = cat
self.legend_index[legend_index+1] = cat+'1sig'
self.legend_index[legend_index+2] = cat+'2sig'
#print len(self.legend_index), self.legend_index
else:
self.legend_index[len(self.cat)+1] = cat
self.legend_index[len(self.cat)+2] = cat+'1sig'
self.legend_index[len(self.cat)+3] = cat+'2sig'
self.line_color[cat] = line_color
self.line_style[cat] = line_style
self.line_width[cat] = line_width
self.marker_color[cat] = marker_color
self.marker_size[cat] = marker_size
self.marker_style[cat] = marker_style
self.fill_style[cat] = fill_style
self.fill2_style[cat] = fill2_style
self.fill_color[cat] = fill_color
self.fill2_color[cat] = fill2_color
_x = array('d')
_y = array('d')
_exh = array('d')
_exl = array('d')
_eyh = array('d')
_eyl = array('d')
_exh2 = array('d')
_exl2 = array('d')
_eyh2 = array('d')
_eyl2 = array('d')
_d = [] # list of tuples, one tuple per mass point
with open(filename, 'r') as _file:
_mass = None
_arr = array('d')
_prev_limit = 0.0
for line in _file:
#print self.legend, line
_buf = line.split()
aMass = float(_buf[0])
aLim = float(_buf[1])*scale*get_scale(extra_scale_name, aMass)
if _mass == None:
_mass = aMass
if abs(_mass-aMass) > 0.1:
_d.append((_mass, _arr))
#print self.legend, cat, 'mass =', _mass, 'PEs per point:', len(_arr)
_mass = aMass
_arr=array('d')
#if abs(aLim - _prev_limit) < 0.000001:
# #print self.legend, 'WARNING: duplicate expected limit, ignoring...'
# continue
#_arr.append(float(_buf[1])*scale)
_arr.append(aLim)
_prev_limit = aLim
_d.append((_mass, _arr))
#print self.legend, _d
for t in _d:
_x.append(t[0])
_mean = TMath.Mean(len(t[1]), t[1])
_median = TMath.Median(len(t[1]), t[1])
_rms = TMath.RMS(len(t[1]), t[1])
_nrms = _rms/len(t[1])
# cross check
#CheckBimodal(t, 'exp')
#print self.legend, t[0], _mean, _rms, _nrms
if value_type == 'mean':
_value = _mean
_y.append(_mean)
elif value_type == 'median':
_value = _median
_y.append(_median)
_exh.append(0)
_exl.append(0)
_exh2.append(0)
_exl2.append(0)
if error_type == 'rms':
_eyh.append(_rms/2)
_eyl.append(_rms/2)
_eyh2.append(_rms)
_eyl2.append(_rms)
elif error_type == 'quantile':
_prob = array('d')
_prob.append(0.021)
_prob.append(0.159)
_prob.append(0.841)
_prob.append(0.979)
_nprob = 4
_quantiles = array('d')
_quantiles.append(0)
_quantiles.append(0)
_quantiles.append(0)
_quantiles.append(0)
TMath.Quantiles(len(t[1]), _nprob, t[1], _quantiles, _prob)
#print self.legend, 'Quantiles:', _quantiles
_eyh.append(abs(_value-_quantiles[2]))
_eyl.append(abs(_value-_quantiles[1]))
_eyh2.append(abs(_value-_quantiles[3]))
_eyl2.append(abs(_value-_quantiles[0]))
# dumping to text file
#print t[0], _median
self.x[cat] = _x
self.y[cat] = _y
self.exh[cat] = _exh
self.exl[cat] = _exl
self.exh2[cat] = _exh2
self.exl2[cat] = _exl2
# smoothed bands
if (smooth == 'single'):
# ee, mumu
self.eyh[cat] = self.smooth_band(_x,_eyh,_y,[1],300,1500,'high')
self.eyh2[cat] = self.smooth_band(_x,_eyh2,_y,[1],300,1500,'high')
self.eyl[cat] = self.smooth_band(_x,_eyl,_y,[],300,1500)
self.eyl2[cat] = self.smooth_band(_x,_eyl2,_y,[1],300,1500)
elif (smooth == 'comb'):
# comb
self.eyh[cat] = self.smooth_band(_x,_eyh,_y,[],300,600,'high')
self.eyh2[cat] = self.smooth_band(_x,_eyh2,_y,[1],300,600,'high')
self.eyl[cat] = self.smooth_band(_x,_eyl,_y,[1],300,600)
self.eyl2[cat] = self.smooth_band(_x,_eyl2,_y,[1],300,600)
else:
# non-smoothed bands
self.eyh[cat] = _eyh
self.eyl[cat] = _eyl
self.eyh2[cat] = _eyh2
self.eyl2[cat] = _eyl2
class PlotGraphs:
def __init__(self, data,
xlow, xhigh, ylow, yhigh,
xlabel = "", ylabel = "",
xLegend = .45, yLegend = .60,
legendWidth = 0.20, legendHeight = 0.45,
fillStyle = 3395,
drawOption = 'APL3',
make_tfile = False):
self.graph_index = {}
self.ngraphs = 0
self.data = data
self.drawOption = drawOption
self.xlow=xlow
self.xhigh=xhigh
self.ylow=ylow
self.yhigh=yhigh
self.xlabel = xlabel
self.ylabel = ylabel
self.multigraph=TMultiGraph("MultiGraph", "")
self.legend_type = {}
if make_tfile:
self.tfile = TFile('tgraphs.root', 'recreate')
self.legend = TLegend(xLegend, yLegend,
xLegend + legendWidth, yLegend + legendHeight)
self.g_ = {}
self.g2_ = {}
self.g3_ = {}
for cat in data.cat:
if data.type[cat] == 'observed':
#self.g_[cat] = TGraph(len(data.x[cat]), data.x[cat], data.y[cat])
self.g_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl[cat], data.exh[cat], data.eyl[cat], data.eyh[cat])
self.g_[cat].SetName(cat)
if make_tfile:
_gcopy = self.g_[cat].Clone()
self.tfile.Append(_gcopy)
if data.dofit[cat]:
#self.g_[cat].Fit("pol3", "M", "", data.fit_min[cat], data.fit_max[cat])
f1 = TF1("f1", "[0]+[1]*(x-1000.0)/1000.0+[2]*(x-1000.0)*(x-1000.0)/1000000.0+[3]*(x-1000.0)*(x-1000.0)*(x-1000.0)/1000000000.0", data.fit_min[cat], data.fit_max[cat])
f2 = TF1("f2", "[0]+[1]*(x-1000.0)/1000.0+[2]*(x-1000.0)*(x-1000.0)/1000000.0", data.fit_min[cat], data.fit_max[cat])
f3 = TF1("f3", "[0]+[1]*(x-1000.0)/1000.0", data.fit_min[cat], data.fit_max[cat])
_fr = self.g_[cat].Fit("f2", "MEWS", "", data.fit_min[cat], data.fit_max[cat])
_fr.Print()
elif data.type[cat] == 'expected':
self.g_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl[cat], data.exh[cat], data.eyl[cat], data.eyh[cat])
self.g2_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl2[cat], data.exh2[cat], data.eyl2[cat], data.eyh2[cat])
self.g3_[cat] = TGraphAsymmErrors(len(data.x[cat]), data.x[cat], data.y[cat], data.exl2[cat], data.exh2[cat], data.y[cat], data.exl2[cat])
self.g3_[cat].SetFillStyle(1002)
#self.g3_[cat].SetFillStyle(3008)
# 95% quantile
self.g2_[cat].SetMarkerColor(data.fill2_color[cat])
self.g2_[cat].SetMarkerStyle(data.marker_style[cat])
self.g2_[cat].SetMarkerSize(data.marker_size[cat])
self.g2_[cat].SetLineColor(data.fill2_color[cat])
self.g2_[cat].SetLineStyle(data.line_style[cat])
self.g2_[cat].SetLineWidth(data.line_width[cat])
#self.g2_[cat].SetFillColor(data.marker_color[cat]+2)
self.g2_[cat].SetFillColor(data.fill2_color[cat])
#self.g2_[cat].SetFillStyle(3008)
#self.g2_[cat].SetFillStyle(3003)
self.g2_[cat].SetFillStyle(data.fill2_style[cat])
self.g_[cat].SetMarkerColor(data.marker_color[cat])
self.g_[cat].SetMarkerStyle(data.marker_style[cat])
self.g_[cat].SetMarkerSize(data.marker_size[cat])
self.g_[cat].SetLineColor(data.line_color[cat])
self.g_[cat].SetLineStyle(data.line_style[cat])
self.g_[cat].SetLineWidth(data.line_width[cat])
self.g_[cat].SetFillColor(data.fill_color[cat])
if data.fill_style[cat] == None:
self.g_[cat].SetFillStyle(fillStyle)
else:
self.g_[cat].SetFillStyle(data.fill_style[cat])
if data.type[cat] == 'observed':
# only the main observed limit is a line,
# everything else is a filled area
if cat[0:3] == 'obs':
_legend_type = 'lp'
if 'PC' in drawOption:
_draw_option = 'PC'
else:
_draw_option = 'PL'
self.multigraph.Add(self.g_[cat], _draw_option)
elif cat[0:3] == 'SSM':
_legend_type = 'lp'
_draw_option = '3'
self.multigraph.Add(self.g_[cat], _draw_option)
# print theory curve
#self.g_[cat].Print()
elif cat[0:3] == 'Psi':
_legend_type = 'lp'
_draw_option = '3'
self.multigraph.Add(self.g_[cat], _draw_option)
elif cat[0:3] == 'Stu':
_legend_type = 'lp'
_draw_option = '3'
self.multigraph.Add(self.g_[cat], _draw_option)
elif cat[0:2] == 'RS':