-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
1619 lines (1513 loc) · 75.5 KB
/
tools.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 csv
import copy
import array
import datetime
import pickle
from math import floor, ceil
## scale used ##
# Delta #
#confirmes_scale=1
#deaths_scale=100
#deathIstatExcess_scale=20
#intensivas_scale=150
#ricoveratis_scale=20
#tests_scale=0.10
# Omicron #
confirmes_scale=1
deaths_scale=500
deathIstatExcess_scale=200
intensivas_scale=1500
ricoveratis_scale=200
tests_scale=0.25
rnd = ROOT.TRandom3()
scuola7=[
"Bolzano",
]
scuola14=[
"EmiliaRomagna",
"Lazio",
"Liguria",
"Lombardia",
"Marche",
"Piemonte",
"Toscana",
"Umbria",
"Valled'Aosta",
"Veneto",
"Sicilia",
"Molise",
]
scuola24=[
"Abruzzo",
"Calabria",
"Campania",
"Puglia",
"Sardegna",
]
#useLog = False
useLog = True
fixSigma = 8
#maxPar3 = 1E4
maxPar3 = 1
#minPar2,maxPar2 = 7.9, 8.1
#minPar2,maxPar2 = 7, 9
#minPar2,maxPar2 = 8, 8
#minPar2,maxPar2 = 7.5, 8.5
#minPar2,maxPar2 = 7, 9
#minPar2,maxPar2 = 8, 8
minPar2,maxPar2 = 5.5, 100
#minPar2,maxPar2 = 5.5, 7.5
#minPar2,maxPar2 = 5, 11
#minPar2,maxPar2 = 6.5-3, 6.5+3
maxConstExp = 1
colors = [
ROOT.kBlack,
ROOT.kYellow+1,
ROOT.kRed,
ROOT.kMagenta,
ROOT.kBlue,
ROOT.kCyan+1,
ROOT.kGray+1,
ROOT.kOrange,
ROOT.kPink,
ROOT.kViolet,
ROOT.kAzure,
ROOT.kTeal,
ROOT.kSpring,
ROOT.kGray,
]
colors = colors *1000
maps = {
"America" : ["US","Canada","Ecuador"],
"Africa" : ["Algeria"],
"Europe" : ["Austria", "Belarus", "Belgium", "Croatia", "Czech Republic", "Denmark", "Finland", "France", "Germany", "Greece", "Iceland", "Ireland", "Italy", "Netherlands", "Norway", "Spain", "Sweden", "Switzerland", "UK", "Romania", "San Marino", "Portugal"],
"MiddleEast" : ["Azerbaijan", "Bahrain", "Iran", "Iraq", "Israel", "Kuwait", "Lebanon", "Qatar", "Oman", "United Arab Emirates",],
"FarEast" : ["Hong Kong", "Japan", "Malaysia", "Macau", "Singapore", "South Korea", "Taiwan", "India", "Thailand", "Vietnam",],
"Nord" : ["Valle d'Aosta",'FriuliVeneziaGiulia' , 'Bolzano', 'Veneto', 'Liguria', 'Piemonte', 'Lombardia', 'Emilia Romagna', 'Trento'],
"Centro" : ['Umbria', 'Marche', 'Toscana', 'Lazio', 'Abruzzo'],
"Sud" : ['Calabria', 'Molise', 'Campania', 'Sardegna', 'Sicilia', 'Basilicata', 'Puglia'],
}
#maps["Centro e Sud"] = maps["Centro"] + maps["Sud"]
positivi = ROOT.kMagenta+1
contagiati = ROOT.kBlue
ricoverati = ROOT.kGreen+3
guariti = ROOT.kRed
intensiva = ROOT.kGreen+1
test = ROOT.kGray+2
decessi = ROOT.kBlack
storico = ROOT.kMagenta
prediction = ROOT.kMagenta+2
istat = ROOT.kMagenta+1
funcExp = ROOT.kMagenta
colorMap = {
"casiSintomatici" : guariti,
"prelievi": positivi ,
"casi": test ,
"positives": positivi ,
"histo_confirmes": contagiati ,
"recoveres": guariti,
"deaths": decessi,
"positiveToTestRatio": funcExp,
"deathToRecoverRatio": contagiati,
"deathDailyToRecoverRatio": decessi,
"newConfirmes": contagiati,
"new18Days": ROOT.kBlue+2,
"positives18Days": ROOT.kBlue+2,
"newRecoveres": guariti,
"newDeaths": decessi,
"storico": storico,
"prediction": prediction,
"functionOneExp": ROOT.kRed+2,
"functionExp": funcExp,
"functionExp_newConfirmes": funcExp,
"functionExp_confirmes": funcExp,
"intensiva": intensiva,
"ricoverati": ricoverati,
"test": test,
"newIntensiva": intensiva,
"newRicoverati": ricoverati,
"newTest": test,
"Decessi": decessi,
"decessi": decessi,
"ISTAT": istat,
"predictionConfirmes": contagiati,
"predictionRecoveres": guariti,
"predictionDeaths": decessi,
"predictionIntensiva": intensiva,
"predictionRicoverati": ricoverati,
"predictionTest": test,
}
labelMap = {
"casiSintomatici": "Casi sint",
"casi": "Casi totali",
"prelievi": "Prelievo",
"positives": "Positivi",
"confirmes": "Casi totali",
"18Days": "Casi totali",
"recoveres": "Guariti",
"deaths": "Decessi",
"positiveToTestRatio": "Tasso positivita'",
"deathToRecoverRatio": "Decessi/Guariti (cumulato)",
"deathDailyToRecoverRatio": "Decessi/Guariti (giornaliero)",
"prediction": "Prediction",
"intensiva": "Terapia Intensiva",
"ricoverati": "Ricoverati",
"test": "Tamponi",
"newConfirmes": "Casi totali",
"new18Days": "Casi totali (18 giorni rit.)",
"positives18Days": "Positivi (18 giorni rit.)",
"newRecoveres": "Guariti",
"newDeaths": "Decessi",
"newIntensiva": "Terapia Intensiva",
"newRicoverati": "Ricoverati",
"newTest": "Tamponi",
"Decessi": "Decessi",
"decessi": "Decessi",
"ISTAT": "#splitline{Decessi totali}{(eccesso ISTAT)}",
"storico": "#splitline{Media 2015-19}{(riscalata)}",
"predictionConfirmes": "Casi totali (prev.)",
"predictionRecoveres": "Guariti (prev.)",
"predictionDeaths": "Decessi (prev.)",
"predictionIntensiva": "Terapia Intensiva (prev.)",
"predictionRicoverati": "Ricoverati (prev.)",
"predictionTest": "Tamponi (prev.)",
}
def makeCompatible(dataISTAT, firstDateDay=24, firstDateMonth=2):
for place in dataISTAT.keys()[:]:
## remove 0 as first digit (eg. 04/02/20 -> 4/2/20)
for date in dataISTAT[place].keys()[:]:
mm, dd, yy = date.split("/")
mm, dd, yy = int(mm), int(dd), int(yy)
if mm<firstDateMonth:
dd=firstDateDay
mm=firstDateMonth
if mm==firstDateMonth and dd<firstDateDay: dd=firstDateDay
newDate = "%s/%s/%s"%(mm,dd,yy)
if newDate!= date:
if not newDate in dataISTAT[place]: dataISTAT[place][newDate] = {}
for age in dataISTAT[place][date]:
if not age in dataISTAT[place][newDate]: dataISTAT[place][newDate][age] = [0] * len(dataISTAT[place][date][age])
for i in range(len(dataISTAT[place][date][age])):
# print dataISTAT[place][newDate]
# print dataISTAT[place][date]
dataISTAT[place][newDate][age][i] += dataISTAT[place][date][age][i]
del dataISTAT[place][date] ## delete old date
## cancella i dati dei singoli comuni
if len(place.split("_"))==3: del dataISTAT[place]
# elif place=='Italia': del dataISTAT[place] ## avoid double counting
elif len(place.split("_"))==2: dataISTAT[place.split("_")[1]] = dataISTAT.pop(place)
elif place=='TrentinoAltoAdige/S\xfcdtirol': dataISTAT["TrentinoAltoAdige"] = dataISTAT.pop(place)
return dataISTAT
def getGeneric(name, dictionary):
for k in sorted(dictionary.keys(), key=len, reverse=True):
if k in name:
return dictionary[k]
import pprint
pprint.pprint(dictionary)
print name
raise Exception("Error getGeneric, not found %s"%name)
return None
def getColor(name):
out = getGeneric (name, colorMap)
assert(type(out)==type(ROOT.kBlue))
# print("getColor(%s) = %s"%(name, out))
return out
def getLabel(name):
out = getGeneric (name, labelMap)
assert(type(out)==str)
return out
def getData(row, i):
try:
value = int(row[i+4])
except:
print("WARNING: problems with '%s' '%s' at %i. The numeber is '%s'. I wil use 0."%(row[0],row[1],i+4,row[i+4]))
value = 0
state = row[0]
country = row[1]
if "Hubei" == state:
if i < 22:
value = value*1.2
# if i < 30:
# value = value*1.1
elif "Shandong" == state:
if i < 30:
value = value*1.35
return value
def regions(state, country, default = ["World"]):
state = state.replace(",","")
country = country.replace(",","")
regions = set(default)
# if state: regions.add(state)
if country: regions.add(country)
for zone in maps:
if country in maps[zone]:
regions.add(zone)
if zone=="Europe" and country!="Italy": regions.add("Rest of Europe")
if country=="Mainland China" and state!="Hubei": regions.add("Rest of China")
if country!="Mainland China" and default[0]=="World": regions.add("Rest of World")
if country!="Lombardia" and default[0]=="Italia": regions.add("FuoriLombardia")
if country in scuola7: regions.add("scuola7")
if country in scuola14: regions.add("scuola14")
if country in scuola24: regions.add("scuola24")
if country!="Lombardia" and country!="Emilia Romagna" and country!="Veneto" and default[0]=="Italia": regions.add("FuoriLombardiaEmiliaVeneto")
# print (state, country, regions)
return regions
def fillData(fileName):
data = {}
dates = []
with open(fileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
dates = row[4:]
else:
for place in regions(row[0], row[1]):
if not place in data: data[place]={}
for i, date in enumerate(dates):
if not date in data[place]: data[place][date] = 0
data[place][date] += getData(row, i)
line_count += 1
return data, dates
def fillDataISTATpickle(fileName, zerosuppression=0, pickleFileName="temp.pkl", writePickle=True):
if writePickle:
print "Writing pickle file"
dataISTAT, dates = fillDataISTAT(fileName, zerosuppression)
output = open(pickleFileName,'wb')
pickle.dump(dataISTAT, output)
pickle.dump(dates, output)
output.close()
else:
print "Loading pickle file"
inp = open(pickleFileName,'rb')
dataISTAT = pickle.load(inp)
dates = pickle.load(inp)
inp.close()
return dataISTAT, dates
maschiLab = "M_" # "MASCHI_"
femmLab = "F_" # "FEMMINE_"
def fillDataISTAT(fileName, zerosuppression=0, pickleFileName="temp.pkl", writePickle=True):
data = {}
dates = []
total = {}
with open(fileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count ==0:
labels = row[:]
else:
date = row[labels.index("GE")]
# date = (datetime.date(year, int(date[0:2]), int(date[2:4])) - datetime.date(year, 3, 1)).days
date = "%s/%s/%s"%(date[0:2], date[2:4], "20")
age = int(row[labels.index("CL_ETA")])
# if not row[labels.index("NOME_REGIONE")]=="Lombardia": continue
# if not row[labels.index("NOME_PROVINCIA")]=="Bergamo": continue
if not date in dates: dates.append(date)
# place = int(row[labels.index("COD_PROVCOM")])
# if not place in placeMap:
# placeMap[place] = (row[labels.index("NOME_COMUNE")], row[labels.index("NOME_PROVINCIA")], row[labels.index("NOME_REGIONE")])
(regione, provincia, comune) = (row[labels.index("NOME_REGIONE")], row[labels.index("NOME_PROVINCIA")], row[labels.index("NOME_COMUNE")])
regione = regione.replace("-","").replace(" ","")
provincia = provincia.replace("-","").replace(" ","")
comune = comune.replace("-","").replace(" ","")
for place in ["%s_%s_%s"%(regione,provincia,comune),"%s_%s"%(regione,provincia),"%s"%(regione),"Italia"]:
if not place in data: data[place] = {}
if not place in total: total[place] = 0
if not date in data[place]: data[place][date] = {}
# if date in data[place] and age in data[place][date] and len(place.split("_"))>=3: print "WARNING: OVERWRITING DATA: %d %s %d"%(place, date, age)
if not (age in data[place][date]): data[place][date][age] = [0,0,0,0]
vals = (9999,9999)
if row[labels.index("%s20"%maschiLab)] != "n.d.":
vals = (int(row[labels.index("%s20"%maschiLab)]), int(row[labels.index("%s20"%femmLab)])) # M, F
if vals!=(9999,9999):
data[place][date][age][0] += vals[0] #M 2020
data[place][date][age][1] += vals[1] #F 2020
total[place] += vals[0]
total[place] += vals[1]
for year in ["19","18","17","16","15"]:
data[place][date][age][2] += int(row[labels.index(maschiLab+year) ]) #sum M 15-19
data[place][date][age][3] += int(row[labels.index(femmLab +year)]) #sum F 15-19
line_count+=1
dates=sorted(dates)
for place in total:
if total[place]<=zerosuppression:
del data[place]
print "Deleting "+place
return data, dates
(regione, provincia, comune) = place.split("_")
provincia = regione+"_"+provincia
if not provincia in provinceMap: provinceMap[provincia] = set()
if not regione in regioniMap: regioniMap[regione] = set()
provinceMap[provincia].add(place)
regioniMap[regione].add(place)
def fillDataISS(fileName):
data = {}
dates = []
regione = "Italia"
indexData = 1
data[regione] = {}
with open(fileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
# print "XXX",row
if line_count ==0:
labels = row[:]
else:
# index = labels.index("iss_date")
date = row[indexData]
if ("/" in date) and not("01/2020" in date) and not("02/2020" in date):
dd, mm, yyyy = date.split("/")
dd = str(int(dd))
mm = str(int(mm))
# if len(dd)<2: dd = "0"+dd
# if len(mm)<2: mm = "0"+mm
date = "%s/%s/%s"%(mm,dd,yyyy.replace("202","2"))
if not date in dates: dates.append(date)
data[regione][date] = {}
for i,label in enumerate(labels):
val = row[i].replace("<","")
data[regione][date][label] = val
# print date, label, val
line_count+=1
return data, dates
def fillDataRegioni(fileName, column_regione = "denominazione_regione"):
data = {}
dates = []
with open(fileName) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
# print "XXX",row
if line_count ==0:
labels = row[:]
labels = [label.replace("\xef","").replace("\xbb","").replace("\xbf","") for label in labels]
else:
date = row[labels.index("data")].split(" ")[0].split("T")[0].replace("-0","-").replace("2020-","").replace("2021-","").replace("2022-","").replace("-","/").replace("/0","/")
if "2020" in row[labels.index("data")]: date = date+"/20"
if "2021" in row[labels.index("data")]: date = date+"/21"
if "2022" in row[labels.index("data")]: date = date+"/22"
if not date in dates: dates.append(date)
regione = row[labels.index(column_regione)]
if regione == "Fuori Regione / Provincia Autonoma": continue
if regione == "In fase di definizione/aggiornamento": continue
if regione == "Friuli V. G. ": regione = "Friuli Venezia Giulia"
regione = regione.replace(" ","")
regione = regione.replace("-","")
regione = regione.replace("P.A.","")
if not regione in data: data[regione] = {}
# if date in data[regione]: print "WARNING: OVERWRITING DATA: %s %s"%(regione, date)
data[regione][date] = {}
for i,label in enumerate(labels):
data[regione][date][label] = row[i]
# print regione, date, label, row[i]
line_count+=1
return data, dates
def selectComuniDatesAgeGender(dataISTAT, dates, places=None, ages=[], genders=[]):
## places == None -> do no merge places
data = {}
for place in dataISTAT:
if places==None:
data[place] = {}
for place_ in regions("", place, ["Italia"]):
if place_ in dataISTAT: continue #skip if already existing (no double counting italia)
data[place_] = {}
if places!=None and len(places)>0 and not place in places: continue
for date in dates:
if not date in dataISTAT[place]:
if places==None: data[place][date]=0
continue
for age in dataISTAT[place][date]:
if len(ages)>0 and not age in ages: continue
for gender in [0,1,2,3]:
if len(genders)>0 and not gender in genders: continue
if not places == None:
data[date] = data[date] + dataISTAT[place][date][age][gender] if date in data else dataISTAT[place][date][age][gender]
else:
data[place][date] = data[place][date] + dataISTAT[place][date][age][gender] if date in data[place] else dataISTAT[place][date][age][gender]
for place_ in regions("", place, ["Italia"]):
if place_ in dataISTAT: continue #skip if already existing (no double counting italia)
data[place_][date] = data[place_][date] + dataISTAT[place][date][age][gender] if date in data[place_] else dataISTAT[place][date][age][gender]
return data
def getColumn(dataRegioni_, label, scaleFactor=1):
data = {}
for regione in dataRegioni_:
for place in regions("", regione, ["Italia"]):
if not place in data: data[place] = {}
for date in dataRegioni_[regione]:
# print(regione, date, label)
# print(dataRegioni_[regione][date][label])
if not date in data[place]: data[place][date] = 0
if dataRegioni_[regione][date][label] == "": dataRegioni_[regione][date][label] = 0
data[place][date] += int(dataRegioni_[regione][date][label])*scaleFactor
return data
def newCases(cases, dates, toDebug = []):
newCases = {}
for place in cases:
newCases[place] = {}
newCases[place][dates[0]] = 0
for i in range(1, len(cases[place])):
newCases[place][dates[i]] = cases[place][dates[i]] - cases[place][dates[i-1]]
if place in toDebug:
print place, i, dates[i]
print cases[place][dates[i]], cases[place][dates[i-1]], newCases[place][dates[i]]
return newCases
def shiftHisto(histo, shift):
shiftedHisto = histo.Clone(histo.GetName()+"shifted")
shiftedHisto.Reset()
loop = range(len(shiftedHisto))
if shift < 0:
loop = reversed(loop)
for i in loop:
for j in range(len(histo)):
if i+shift>0 and i+shift<len(shiftedHisto):
shiftedHisto.SetBinContent(i, histo.GetBinContent(i-shift))
shiftedHisto.SetBinError(i, histo.GetBinError(i-shift))
return shiftedHisto
def getRatio(numerators, denominators):
ratios = {}
for place in numerators:
ratios[place] = numerators[place].Clone("ratio"+place)
ratios[place].Divide(numerators[place],denominators[place])
return ratios
def smearData(dataUnsmeared, dates, daysSmearing):
if daysSmearing>2:
data = {}
for place in dataUnsmeared:
data[place] = {}
for date in dates:
if date in dataUnsmeared[place]:
data[place][date] = 0
count = 0
for j in range(daysSmearing):
idx = dates.index(date) - j
if idx>0:
data[place][date] += dataUnsmeared[place][dates[idx]]
count += 1
if count>0:
data[place][date] = data[place][date] / count
else:
data = copy.copy(dataUnsmeared)
return data
def makeHistos(prefix, dataUnsmeared, dates, places, firstDate, lastDate, predictionDate, threshold=-1E30, cutTails=False, errorType=None, lineWidth=3, daysSmearing=1, toDebug = []):
data = smearData(dataUnsmeared, dates, daysSmearing)
histos = {}
for place in places:
if not place in data: continue
histos[place] = copy.copy(ROOT.TH1F(prefix+"_"+str(place)+str(rnd.Rndm()), str(place), predictionDate-firstDate+1, firstDate-0.5, predictionDate+0.5))
if histos[place].GetXaxis().GetBinWidth(1)!=1.0:
print str(place)
print histos[place].GetXaxis().GetBinWidth(1)
print firstDate, predictionDate
print predictionDate-firstDate+1
raise Exception("histos[place].GetXaxis().GetBinWidth(1)!=1.0")
stop = False
start = False
histos[place].GetXaxis().SetNdivisions(7)
for i in reversed(range(firstDate, predictionDate)):
binx = histos[place].FindBin(i)
date = dates[i]
# print("date",date)
# print(binx,date)
if type(date)==str and i%7==0: histos[place].GetXaxis().SetBinLabel( binx, date[:-3] )
error = 0.
# print(date, data.values()[0])
# print(date in data.values()[0])
if date in data.values()[0]:
# print("EEE",date)
if not date in data[place] or data[place][date]==0:
# histos[place].SetBinContent(binx, 0)
# histos[place].SetBinError(binx, 0)
continue
# print("AAAA")
if errorType=='dictionary': ## if dictionary, data is (value, error)
value = data[place][date][0]
error = data[place][date][1]
else:
# print place, date
value = data[place][date]
valueM1 = data[place][dates[i-1]] if i>=1 else value
valueP1 = data[place][dates[i+1]] if i<=lastDate else value
valueM1 = max(valueM1,0)
valueP1 = max(valueP1,0)
average = ((valueM1+valueP1)/2)
if errorType=='cumulative':
error = 1.+(data[place][dates[lastDate]] - value)**0.5 if (data[place][dates[lastDate]] - value) >=0 else 0
elif errorType=='3sqrtN':
error = 0.05*abs(value)+ 3*abs(value)**0.5
error = 3*(value)**0.5 if (value>=9 and (value-average)<=0.5*average) else abs(value-average)*2
elif errorType=='sqrtN':
error = (value)**0.5 if (value>=9 and (value-average)<=0.5*average) else abs(value-average)*2
else:
error = 9.+(value)**0.5+0*0.25*(value) if value>=9 else 12.+abs(value-9.) ## error 10 + sqrt(N) + 0*25% N
if i>=1: error = max(error, abs(value-valueM1))
if i<=lastDate: error = max(error, abs(value-valueP1))
# print(binx,value)
if True or value>threshold:
if True or not stop:
# print(binx,value)
histos[place].SetBinContent(binx, value)
histos[place].SetBinError(binx, error)
start = True
if cutTails: ### remove tail, if there are 2 days without new cases from the peak
maxBin = histos[place].GetMaximumBin()
for i in range(maxBin, predictionDate):
if histos[place].GetBinContent(i-1)==0 and histos[place].GetBinContent(i-2)==0:
histos[place].SetBinContent(i, 0)
histos[place].SetBinError(i, 0)
for i in reversed(range(firstDate, maxBin)):
if histos[place].GetBinContent(i+1)==0 and histos[place].GetBinContent(i+2)==0:
histos[place].SetBinContent(i, 0)
histos[place].SetBinError(i, 0)
color = colors[places.index(place)]
histos[place].SetLineWidth(lineWidth)
histos[place].SetLineColor(color)
return histos
def fitErf(h, places, firstDate, lastDate, predictionDate, fitOption="0SEQ"):
functs = {}
functs_res = {}
functs_err = {}
for place in places:
functs[place] = copy.copy(ROOT.TF1("functionErf"+place,"[0]*(1+TMath::Erf((x-[1])/[2])) + [3]",0,predictionDate))
functs[place].SetParLimits(3,0,100)
functs[place].SetParLimits(2,2,20)
functs[place].SetParLimits(1,0,100)
functs[place].SetParameters(6.60369e+02, 25, fixSigma, 0)
functs[place].FixParameter(2, fixSigma)
functs[place].FixParameter(3, 0)
# h[place].Fit(functs[place],"0W","",0,lastDate)
functs_res[place] = h[place].Fit(functs[place],"0S","",0,lastDate+1.5)
functs[place].ReleaseParameter(3)
functs[place].SetParLimits(3,0,maxPar3)
functs_res[place] = h[place].Fit(functs[place],"0S","",0,lastDate+1.5)
if minPar2 != maxPar2:
functs[place].ReleaseParameter(2)
functs[place].SetParLimits(2,minPar2,maxPar2)
functs_res[place] = h[place].Fit(functs[place],"0S","",0,lastDate+1.5)
color = colors[places.index(place)]
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("errErf"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionErf_")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
from math import log
def fitExpGauss(h, places, firstDate, lastDate, predictionDate, fitOption="0SE", maxPar3=maxPar3):
functs = {}
functs_res = {}
functs_err = {}
for place in places:
print "### Fit fitExpGauss %s - %s. %s ###"%(place,h[place].GetName(),fitOption)
functs[place] = copy.copy(ROOT.TF1("function"+place,"gaus + exp(+x/[4]-[3])",firstDate,predictionDate))
functs[place].SetParameters(h[place].GetMaximum(), lastDate, fixSigma, 10, 1000)
##### Fit Exp then Gaus
functs[place].FixParameter(3, functs[place].GetParameter(3))
functs[place].FixParameter(4, functs[place].GetParameter(4))
# functs[place].SetParameter(3, h[place].GetXaxis().GetXmin())
# functs[place].SetParameter(4, h[place].GetMaximum())
# print h[place]
# print functs[place]
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5 + (lastDate - firstDate)/2, lastDate+1.5 )
# functs[place].FixParameter(0, functs[place].GetParameter(0))
# functs[place].FixParameter(1, functs[place].GetParameter(1))
# functs[place].FixParameter(2, functs[place].GetParameter(2))
functs[place].ReleaseParameter(3)
functs[place].ReleaseParameter(4)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].ReleaseParameter(4)
# functs[place].ReleaseParameter(0)
# functs[place].ReleaseParameter(1)
# functs[place].ReleaseParameter(2)
# functs[place].SetParameter(0, 0.01*h[place].GetMaximum())
# functs[place].SetParameter(1, h[place].GetXaxis().GetXmax())
# functs[place].SetParameter(2, fixSigma*10)
# functs[place].SetParameter(4, functs[place].GetParameter(4)*10)
# functs[place].SetParLimits(3,0,maxPar3)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].ReleaseParameter(4)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# if minPar2 != maxPar2:
# functs[place].ReleaseParameter(2)
# functs[place].SetParLimits(2,minPar2,maxPar2)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# ################## TEST INITIAL FUNCTIO WITH NO FIT ############################
# functs[place] = copy.copy(ROOT.TF1("function"+place,"gaus + exp(+x/[4]-[3])",firstDate,predictionDate))
# params = h[place].GetMaximum(), lastDate, fixSigma, 10, 1000
# print("PARAMS=", params)
# functs[place].SetParameters(*params)
# functs[place].FixParameter(0, functs[place].GetParameter(0))
# functs[place].FixParameter(1, functs[place].GetParameter(1))
# functs[place].FixParameter(2, functs[place].GetParameter(2))
# functs[place].FixParameter(3, functs[place].GetParameter(3))
# functs[place].ReleaseParameter(4)
# functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# ##############################################
color = colors[places.index(place)]
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("err"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionGaus_")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
def fitTwoGaussDiff(h, places, firstDate, lastDate, predictionDate, fitOption="0SEQ", maxPar3=maxPar3):
functs = {}
functs_res = {}
functs_err = {}
fixSigma=20
for place in places:
print "### Fit fitTwoGaussDiff %s - %s ###"%(place,h[place].GetName())
functs[place] = copy.copy(ROOT.TF1("function"+place,"gaus(0) + exp(+x/[4]*0-[3]) + gaus(5)",firstDate,predictionDate))
functs[place].SetParLimits(0, 0, h[place].GetMaximum()*2)
functs[place].SetParameters(h[place].GetMaximum(), h[place].GetMean(), fixSigma, 1, 1000, 0, 0, 0)
##### Fit Exp then Gaus
# functs[place].FixParameter(0, functs[place].GetParameter(0))
# functs[place].FixParameter(1, functs[place].GetParameter(1))
# functs[place].FixParameter(2, functs[place].GetParameter(2))
# functs[place].FixParameter(3, functs[place].GetParameter(3))
functs[place].FixParameter(4, functs[place].GetParameter(4))
functs[place].FixParameter(5, functs[place].GetParameter(5))
functs[place].FixParameter(6, functs[place].GetParameter(6))
functs[place].FixParameter(7, functs[place].GetParameter(7))
# functs[place].SetParameter(3, h[place].GetXaxis().GetXmin())
# functs[place].SetParameter(4, h[place].GetMaximum())
# print h[place]
# print functs[place]
# functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5 + (lastDate - firstDate)/2, lastDate+1.5 )
# functs[place].FixParameter(0, functs[place].GetParameter(0))
# functs[place].FixParameter(1, functs[place].GetParameter(1))
# functs[place].FixParameter(2, functs[place].GetParameter(2))
functs[place].ReleaseParameter(3)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].ReleaseParameter(4)
# functs[place].ReleaseParameter(0)
# functs[place].ReleaseParameter(1)
# functs[place].ReleaseParameter(2)
# functs[place].SetParameter(0, 0.01*h[place].GetMaximum())
# functs[place].SetParameter(1, h[place].GetXaxis().GetXmax())
# functs[place].SetParameter(2, fixSigma*10)
# functs[place].SetParameter(4, functs[place].GetParameter(4)*10)
# functs[place].SetParLimits(3,0,maxPar3)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].ReleaseParameter(5)
functs[place].ReleaseParameter(6)
functs[place].ReleaseParameter(7)
functs[place].SetParLimits(5, -functs[place].GetParameter(0)*2, 0)
functs[place].SetParameter(5, -functs[place].GetParameter(0)*0.5)
functs[place].SetParameter(6, functs[place].GetParameter(1)+20)
# functs[place].SetParLimits(7, functs[place].GetParameter(2)/3, functs[place].GetParameter(2)*3)
functs[place].SetParameter(7, functs[place].GetParameter(2))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
## if minPar2 != maxPar2:
## functs[place].ReleaseParameter(2)
## functs[place].SetParLimits(2,minPar2,maxPar2)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].SetParLimits(7, functs[place].GetParameter(2)/3, functs[place].GetParameter(2)*3)
# functs[place].SetParameter(7, functs[place].GetParameter(2))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
color = colors[places.index(place)]
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("err"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionGaus_")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
def fitGauss(h, places, firstDate, lastDate, predictionDate, fitOption="0SEQ", maxPar3=maxPar3):
functs = {}
functs_res = {}
functs_err = {}
for place in places:
print "### Fit fitGauss %s - %s ###"%(place,h[place].GetName())
functs[place] = copy.copy(ROOT.TF1("function"+place,"gaus + [3]",firstDate,predictionDate))
functs[place].SetParameters(h[place].GetBinContent(h[place].GetMaximumBin()), h[place].GetMean(), fixSigma)
functs[place].FixParameter(2, fixSigma)
functs[place].FixParameter(3, 1)
# print h[place]
# print functs[place]
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].ReleaseParameter(3)
functs[place].SetParLimits(3,0,maxPar3)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
if minPar2 != maxPar2:
functs[place].ReleaseParameter(2)
functs[place].SetParLimits(2,minPar2,maxPar2)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
color = colors[places.index(place)]
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("err"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionGaus_")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
def fitGaussAsymmetric(h, places, firstDate, lastDate, predictionDate, fitOption="0SE", maxPar3=maxPar3):
functs = {}
functs_res = {}
functs_err = {}
for place in places:
print "### Fit fitGaussAsymmetric %s - %s ###"%(place,h[place].GetName())
functs[place] = copy.copy(ROOT.TF1("function"+place,"[0]*exp(-0.5*( (x<=[5])*(x-[1])/[2] + [4]/[2]*(x>[5])*(x-[1])/[4] )**2) + [3]",firstDate,predictionDate))
# functs[place] = copy.copy(ROOT.TF1("function"+place,"[0]*exp(-0.5*( (x<=[1])*(x-[1])/[2] + (x>[1])*(x-[1])/[4] )**2) + [3]",firstDate,predictionDate))
fixSigma = 20
functs[place].SetParameters(h[place].GetBinContent(h[place].GetMaximumBin()), h[place].GetMean(), fixSigma, 0, fixSigma)
# functs[place].SetParameters(h[place].GetBinContent(h[place].GetMaximumBin()), h[place].GetMean(), fixSigma, 0, fixSigma)
# functs[place].FixParameter(5, 100000)
# functs[place].FixParameter(3, 0)
# print h[place]
# print functs[place]
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].SetParameter(5, functs[place].GetParameter(1))
# functs[place].FixParameter(5, h[place].GetMean())
functs[place].FixParameter(4, functs[place].GetParameter(2))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].FixParameter(5, h[place].GetMean())
functs[place].ReleaseParameter(4)
functs[place].SetParameter(4, functs[place].GetParameter(2))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].ReleaseParameter(5)
functs[place].SetParameter(4, functs[place].GetParameter(2))
functs[place].SetParameter(5, h[place].GetMean())
functs[place].ReleaseParameter(4)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].ReleaseParameter(3)
functs[place].ReleaseParameter(4)
functs[place].SetParameter(4, functs[place].GetParameter(2))
# functs[place].FixParameter(5, functs[place].GetParameter(5))
functs[place].FixParameter(4, functs[place].GetParameter(4))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
color = colors[places.index(place)]
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("err"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionGaus_")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
def fitGaussExp(h, places, firstDate, lastDate, predictionDate, fitOption="0SE", maxPar3=maxPar3):
functs = {}
functs_res = {}
functs_err = {}
for place in places:
print "### Fit fitGaussExp %s - %s ###"%(place,h[place].GetName())
# functs[place] = copy.copy(ROOT.TF1("function"+place,"[0]*exp( (x<=[5])*(-0.5*(x-[1])**2/[2]) - (x>[5])*(0.5*([5]-[1])**2/[2])/(1+[4]*[5])*(1+[4]*x) ) + [3]",firstDate,predictionDate))
functs[place] = copy.copy(ROOT.TF1("function"+place,"[0]*exp( (x<=[5])*(-0.5*(x-[1])**2/[2]) - (x>[5])*(0.5*([5]-[1])**2/[2])/(1+-2/([1] + [5])*[5])*(1+-2/([1] + [5])*x) ) + [3]",firstDate,predictionDate)) ###FUNZIONA! DERIVATA CONTINUA
#()-[2]*((0.5 *[4]* ([0] - [5])**2)/([4] *[5]* [2] + [2]) - [0]/[2]))
fixSigma = 20
functs[place].SetParameters(h[place].GetBinContent(h[place].GetMaximumBin()), h[place].GetMean(), fixSigma, 0, fixSigma)
print("FitInitValue:",h[place].GetBinContent(h[place].GetMaximumBin()), h[place].GetMean(), fixSigma, 0, fixSigma)
functs[place].FixParameter(5, 100000)
# functs[place].FixParameter(3, 0)
# print h[place]
# print functs[place]
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].SetParameter(5, functs[place].GetParameter(1))
# functs[place].FixParameter(5, h[place].GetMean())
# functs[place].FixParameter(4, 0)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].FixParameter(5, h[place].GetMean())
# functs[place].ReleaseParameter(4)
functs[place].SetParameter(4, 0)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].ReleaseParameter(5)
functs[place].SetParameter(5, h[place].GetMean())
functs[place].ReleaseParameter(4)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
functs[place].ReleaseParameter(3)
functs[place].ReleaseParameter(4)
# functs[place].FixParameter(5, functs[place].GetParameter(5))
# functs[place].FixParameter(0, h[place].GetBinContent(h[place].GetMaximumBin()))
# functs[place].FixParameter(1, h[place].GetMean())
# functs[place].FixParameter(2, fixSigma)
# functs[place].FixParameter(3, 0)
# functs[place].FixParameter(4, fixSigma)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
color = colors[places.index(place)]
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("err"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionGaus_")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
# EXT PARAMETER STEP FIRST
# NO. NAME VALUE ERROR SIZE DERIVATIVE
# 1 p0 7.93712e+05 nan 1.37999e+06 -7.80844e+28
# 2 p1 1.32749e+04 nan 1.37999e+04 -inf
# 3 p2 6.89555e+02 nan 3.15875e-03 1.26635e-03
# 4 p3 2.81559e+01 nan 3.45093e-04 -1.21856e-02
def fitTwoExp(h, places, firstDate, lastDate, predictionDate, fitOption="0SEQ", maxPar3=maxPar3, oneExp=False):
functs = {}
functs_res = {}
functs_err = {}
for place in places:
print "### Fit fitTwoExp %s - %s ###"%(place,h[place].GetName())
functs[place] = copy.copy(ROOT.TF1("function"+place + ("One" if oneExp else "Two"),"exp((x-[0])/[1]) + exp(-(x-[2])/[3])",firstDate,predictionDate))
if not oneExp: #allow negative/positive expo if only one exp
functs[place].SetParLimits(2,0,h[place].GetMean()*h[place].GetMean())
functs[place].SetParLimits(3,0,h[place].GetMean()*h[place].GetMean())
functs[place].SetParameters(h[place].GetMean(), h[place].GetMean(), h[place].GetMean(), h[place].GetMean())
functs[place].FixParameter(0, h[place].GetMean()*10)
functs[place].FixParameter(1, 1)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# functs[place].FixParameter(2, functs[place].GetParameter(0))
# functs[place].FixParameter(3, functs[place].GetParameter(1))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
if not oneExp:
functs[place].ReleaseParameter(0)
functs[place].ReleaseParameter(1)
functs[place].SetParLimits(0,0,h[place].GetMean()*h[place].GetMean())
functs[place].SetParLimits(1,0,h[place].GetMean()*h[place].GetMean())
functs[place].SetParameter(0, h[place].GetMean())
functs[place].SetParameter(1, h[place].GetMean())
# if minPar2 != maxPar2:
# functs[place].ReleaseParameter(2)
# functs[place].SetParLimits(2,minPar2,maxPar2)
# functs[place].ReleaseParameter(4)
# functs[place].SetParLimits(4,minPar2,maxPar2)
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
# for i in [0,1,2,3]:
# functs[place].SetParLimits(i,functs[place].GetParameter(i)*(1-0.5),functs[place].GetParameter(i)*(1+0.5))
functs_res[place] = h[place].Fit(functs[place], fitOption,"",firstDate-0.5,lastDate+1.5)
color = colors[places.index(place)]
if oneExp:
color = colors[places.index(place)]+3
functs[place].SetLineColor(color)
functs_err[place] = copy.copy(h[place].Clone(("err"+h[place].GetName())))
functs_err[place].Reset()
ROOT.TVirtualFitter.GetFitter().GetConfidenceIntervals(functs_err[place], 0.68)
functs_err[place].SetStats(ROOT.kFALSE)
functs_err[place].SetLineColor(color)
functs_err[place].SetFillColor(color)
name = h[place].GetName().replace("histo_","functionTwoExp_")
if oneExp: name = name.replace("Two","One")
functs[place].SetName(name+"_centralValue")
if functs_res[place].Get(): functs_res[place].SetName(name+"_fitResult")
functs_err[place].SetName(name+"_errorBand")
return functs, functs_res, functs_err
def fitDecessi(h, places, firstDate, lastDate, predictionDate, fitOption="0SEQ"):
functs = {}
functs_res = {}