forked from amissert/atmFitTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistoCompare.cxx
1720 lines (1524 loc) · 52.3 KB
/
histoCompare.cxx
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
#ifndef HISTOCOMPARE_C
#define HISTOCOMPARE_C
#include "histoCompare.h"
#include "markovTools.h"
#include <time.h>
histoCompare* histoCompare::staticthis;
/////////////////////////////////////////////
//get a rough estimation (fron 1D likelihood proflie)
//of the uncertainty of each fit parameter
//Requires a guess of parUnc[] in atmFitPars initialization
//This should be run before running MCMC, since these
//rough uncertainties are used to proposes MCMC steps
void histoCompare::calcRoughParErr(){
cout<<"histoCompare: "<<"calculating rough uncertainties"<<endl;
//print final results
for (int ipar=0;ipar<thePars->nTotPars;ipar++){
// thePars->setParameter(ipar,fit->GetParameter(ipar));
errParLo[ipar]=getErrLo(ipar);
errParHi[ipar]=getErrHi(ipar);
thePars->parUnc[ipar]=(errParHi[ipar]-errParLo[ipar]);
cout<<" PAR "<<ipar<<" FIT RESULT: "<<thePars->pars[ipar]<<" +/- : "<<thePars->parUnc[ipar]<<endl;
}
}
void histoCompare::readFitPars(const char* filename){
thePars->readPars(filename);
return;
}
void histoCompare::timetest(int ntry){
clock_t t1,t2;
double par[100];
int parindex = 0;
for (int ibin=0;ibin<nBin;ibin++){
for (int icomp=0;icomp<nComp;icomp++){
for (int iatt=0;iatt<nAtt;iatt++){
for (int imod=0;imod<2;imod++){
par[parindex]=Par[ibin][icomp][iatt][imod];
parindex++;
}
}
}
}
double diff;
int itry=0;
t1=clock();
while (itry<ntry){
//getTotLnL1D(result,par);
getTotLnL();
itry++;
}
t2=clock();
diff = ((double)t2-(double)t1)/(double)ntry;
double diff1 = diff;
cout<<"time 1: "<<diff<<endl;
itry=0;
t1=clock();
while (itry<ntry){
//getTotLnL1D(result,par);
getTotLnL();
itry++;
}
t2=clock();
diff = ((double)t2-(double)t1)/(double)ntry;
cout<<"time 2: "<<diff<<endl;
cout<<"changes: "<<diff1-diff<<endl;
return;
}
void histoCompare::tuneMCMC(int ncycles,int nsteps,double goal){
// //setup mc mcmctools
// const int npars = thePars->nTotPars; //< total number of parameters in fit
// double par[npars]; //< container for parameters
// int parindex = 0;
double result = 0.;
// markovTools* mc = new markovTools(npars); //< create markovTools object
markovTools* mc = new markovTools(thePars); //< create markovTools object
mc->setTuneParameter(tunePar);
//fill parameter array and set uncertainties
for (int ipar=0;ipar<thePars->nTotPars;ipar++){
// par[ipar]=thePars->getParameter(ipar); //< parameter array
mc->setParVar(ipar,thePars->parUnc[ipar]); //< set parameter variance
mc->setFixPar(ipar,thePars->fixPar[ipar]); //< set parameter fix flag
}
//set initial state
result = getTotLnL();
// getTotLnL1D(result,npars, par);//< get total likelihood from 1D array
mc->setL(result);//< sets the initial likelihood
double Linit = result;
//run tuning
for (int icycle=0;icycle<ncycles;icycle++){
double xaccepted=0.;
int istep=0;
while (istep<nsteps){
istep = mc->iStep;
mc->proposeStep(); //< propose a new step
//getTotLnL1D(result, npars,par); //< get likelihood of new step
result = getTotLnL();
cout<<result-Linit<<endl;
if (mc->acceptStepLnL(result)){ //< check if new step is accepted
xaccepted++;
}
}
double rate = xaccepted/(double)nsteps;
cout<<"acceptance rate: "<<rate<<endl;
cout<<"tune parameter: "<<tunePar<<endl;
tunePar*=(rate/goal);
cout<<"new tune parameter: "<<tunePar<<endl;
}
return;
}
void histoCompare::tuneMCMCOld(int ncycles,int nsteps,double goal){
// //setup mc mcmctools
const int npars = thePars->nTotPars; //< total number of parameters in fit
double par[npars]; //< container for parameters
int parindex = 0;
double result = 0.;
markovTools* mc = new markovTools(npars); //< create markovTools object
// markovTools* mc = new markovTools(thePars); //< create markovTools object
mc->setTuneParameter(tunePar);
//fill parameter array and set uncertainties
for (int ipar=0;ipar<thePars->nTotPars;ipar++){
par[ipar]=thePars->getParameter(ipar); //< parameter array
mc->setParVar(ipar,thePars->parUnc[ipar]); //< set parameter variance
mc->setFixPar(ipar,thePars->fixPar[ipar]); //< set parameter fix flag
}
//set initial state
// result = getTotLnL();
getTotLnL1D(result,npars, par);//< get total likelihood from 1D array
mc->setL(result);//< sets the initial likelihood
double Linit = result;
//run tuning
for (int icycle=0;icycle<ncycles;icycle++){
double xaccepted=0.;
int istep=0;
while (istep<nsteps){
istep = mc->iStep;
mc->proposeStep(par); //< propose a new step
getTotLnL1D(result, npars,par); //< get likelihood of new step
//result = getTotLnL();
cout<<result-Linit<<endl;
if (mc->acceptStepLnL(result,par)){ //< check if new step is accepted
xaccepted++;
}
}
double rate = xaccepted/(double)nsteps;
cout<<"acceptance rate: "<<rate<<endl;
cout<<"tune parameter: "<<tunePar<<endl;
tunePar*=(rate/goal);
cout<<"new tune parameter: "<<tunePar<<endl;
}
delete mc;
}
///////////////////////////////////////////////
//Run a MCMC of length nsteps
void histoCompare::runMCMC(int nsteps){
///////////////////////////////////////
//setup mcmc tools
markovTools* mc = new markovTools(thePars);
mc->setTuneParameter(tunePar);
// const int npars = thePars->nTotPars; //< total number of parameters in fit
// double par[npars]; //< container for parameters
// int parindex = 0;
double result = 0.;
// markovTools* mc = new markovTools(npars); //< create markovTools object
// mc->setTuneParameter(tunePar);
///////////////////////////////////////////////
//fill parameter array and set uncertainties
for (int ipar=0;ipar<thePars->nTotPars;ipar++){
//par[ipar]=thePars->getParameter(ipar);
mc->setParVar(ipar,thePars->parUnc[ipar]);
}
///////////////////////////////////////////////////
//set initial state
// getTotLnL1D(result,npars, par);//< get total likelihood from 1D array
result = getTotLnL();
mc->setL(result);//< sets the initial likelihood
//loop through steps
int currentstep=0;
while (currentstep<nsteps){
currentstep = mc->iStep;
//mc->proposeStep(par); //< fills par[] with proposed params
mc->proposeStep();
//getTotLnL1D(result, npars,par);
result = getTotLnL();
//mc->acceptStepLnL(result,par); //< if step is accepted, istep++, and written to histo
mc->acceptStepLnL(result);
}
///////////////////////
//save results
mc->savePath();
//////////////////////////
return;
}
/*
double histoCompare::getErrHi(int ibin,int icomp,int iatt,int imod){
if (fixPar[ibin][icomp][iatt][imod]==1) return 0.;
double thresh = 1.0;
double Ldiff = 0.;
double Lbest = getTotLnL();
double parbest = Par[ibin][icomp][iatt][imod];
double parval = 0.;
double dpar = 1.;
double loerr;
int ntry = 0;
int ntrymax=1000;
if (imod==0) dpar = 0.005;
if (imod==1) dpar = 10.;
//course search
while ((Ldiff<1)&&(ntry<ntrymax)){
Par[ibin][icomp][iatt][imod]+=dpar; //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
}
// cout<<"ntry: "<<ntry<<endl;
Par[ibin][icomp][iatt][imod]-=dpar;
Ldiff = 0;
dpar*=0.2;
ntry=0;
while ((Ldiff<1)&&(ntry<ntrymax)){
Par[ibin][icomp][iatt][imod]+=dpar; //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
}
Par[ibin][icomp][iatt][imod]-=dpar;
Ldiff = 0;
dpar*=0.1;
ntry=0;
while ((Ldiff<1)&&(ntry<ntrymax)){
Par[ibin][icomp][iatt][imod]+=dpar; //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
}
loerr = Par[ibin][icomp][iatt][imod]-parbest;
Par[ibin][icomp][iatt][imod] = parbest;
return loerr;
}
*/
void histoCompare::saveFitPars(const char* filename){
thePars->savePars(filename);
return;
}
double histoCompare::getErrHi(int ipar){
//scan through log likelihood by decreasing parameter until threshold is reached
// double thresh = 1.0; //likelihood threshold
double Ldiff = 0.; //difference in likelihood from current value
double Lbest = getTotLnL(); //current likelihood value
// cout<<"lbest: "<<Lbest<<endl;
double parbest = thePars->pars[ipar];
double parval = thePars->pars[ipar];
double dpar = thePars->parUnc[ipar]/5.;
double hierr;
int ntry = 0;
int ntrymax=100;
//coarse search
while (Ldiff<1){
// cout<<"oldpar: "<<parval<<endl;
parval+=dpar;
// cout<<"newpar: "<<parval<<endl;
thePars->setParameter(ipar,parval); //modify parameter
// double Lnew = getTotLnL();
// cout<<"Lnew: "<<Lnew<<endl;
Ldiff = TMath::Abs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
if (ntry>ntrymax) break;
}
// cout<<"ntry: "<<ntry<<endl;
parval-=dpar;
thePars->setParameter(ipar,parval);
Ldiff = 0;
dpar*=0.10;
ntry=0;
//fine search
while ((Ldiff<1)){
parval+=dpar;
thePars->setParameter(ipar,parval); //modify paramete
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
ntry++;
if (ntry>ntrymax) break;
}
parval-=dpar;
thePars->setParameter(ipar,parval);
Ldiff = 0;
dpar*=0.10;
ntry=0;
//very fine search
// while ((Ldiff<1)&&(ntry<ntrymax)){
// parval+=dpar;
// thePars->setParameter(ipar,parval); //modify paramete
// Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// ntry++;
// }
// cout<<"ntry: "<<ntry<<endl;
hierr = thePars->pars[ipar];
thePars->setParameter(ipar,parbest);
return hierr;
}
double histoCompare::getErrLo(int ipar){
//scan through log likelihood by decreasing parameter until threshold is reached
//double thresh = 1.0; //likelihood threshold
double Ldiff = 0.; //difference in likelihood from current value
double Lbest = getTotLnL(); //current likelihood value
double parbest = thePars->pars[ipar];
double parval = thePars->pars[ipar];
double dpar = thePars->parUnc[ipar]/5.;
// cout<<"dpar: "<<dpar<<endl;
double loerr;
int ntry = 0;
int ntrymax=100;
//coarse search
while (Ldiff<1){
parval-=dpar;
thePars->setParameter(ipar,parval); //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"ntry: "<<ntry<<endl;
// cout<<"Ldiff: "<<Ldiff<<endl;
// cout<<"par: "<<thePars->pars[ipar]<<endl;
ntry++;
if (ntry>ntrymax) break;
}
// cout<<"ntry: "<<ntry<<endl;
parval+=dpar;
thePars->setParameter(ipar,parval);
Ldiff = 0;
dpar*=0.10;
ntry=0;
//fine search
while ((Ldiff<1)){
parval-=dpar;
thePars->setParameter(ipar,parval); //modify paramete
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
ntry++;
if (ntry>ntrymax) break;
}
// cout<<"ntry: "<<ntry<<endl;
parval+=dpar;
thePars->setParameter(ipar,parval);
Ldiff = 0;
dpar*=0.10;
ntry=0;
//very fine search
// while ((Ldiff<1)&&(ntry<ntrymax)){
// parval-=dpar;
// thePars->setParameter(ipar,parval); //modify paramete
// Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// ntry++;
// if (ntry>ntrymax) break;
// }
// cout<<"ntry: "<<ntry<<endl;
loerr = thePars->pars[ipar];
thePars->setParameter(ipar,parbest);
return loerr;
}
/*
double histoCompare::getErrLo(int ibin,int icomp,int iatt,int imod){
//scan through log likelihood by decreasing parameter
if (fixPar[ibin][icomp][iatt][imod]==1) return 0.;
double thresh = 1.0;
double Ldiff = 0.;
double Lbest = getTotLnL();
double parbest = Par[ibin][icomp][iatt][imod];
double parval = 0.;
double dpar = 1.;
double loerr;
int ntry = 0;
int ntrymax=1000;
if (imod==0) dpar = 0.005;
if (imod==1) dpar = 10.;
//course search
while (Ldiff<1){
Par[ibin][icomp][iatt][imod]-=dpar; //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
}
// cout<<"ntry: "<<ntry<<endl;
Par[ibin][icomp][iatt][imod]+=dpar;
Ldiff = 0;
dpar*=0.2;
ntry=0;
while ((Ldiff<1)&&(ntry<ntrymax)){
Par[ibin][icomp][iatt][imod]-=dpar; //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
}
Par[ibin][icomp][iatt][imod]+=dpar;
Ldiff = 0;
dpar*=0.1;
ntry=0;
while ((Ldiff<1)&&(ntry<ntrymax)){
Par[ibin][icomp][iatt][imod]-=dpar; //modify parameter
Ldiff = fabs(Lbest-getTotLnL()); //check L difference
// cout<<"Ldiff: "<<Ldiff<<endl;
ntry++;
}
loerr = parbest-Par[ibin][icomp][iatt][imod];
Par[ibin][icomp][iatt][imod] = parbest;
return loerr;
}
*/
void histoCompare::profileL(int ipar, double range, int npts, int sameflg){
TString pname = "p";
pname.Append("_profile.png");
int iprofile =0;
if (sameflg) iprofile = 1;
double bestpoint = thePars->pars[ipar];
cout<<"Fitted value: "<<bestpoint<<endl;
double dx = range/(double)npts;
double xx = bestpoint - (range/2.);
double ll;
double lbest = getTotLnL();
if (hProf[iprofile]) hProf[iprofile]->Delete();
hProf[iprofile] = new TH1D("hprof","hprof",npts,xx,(xx+range));
for (int ipoint=0;ipoint<npts;ipoint++){
// cout<<"filling point" <<ipoint<<endl;
//Par[ibin][icomp][iatt][imod] = xx;
thePars->setParameter(ipar,xx);
ll = getTotLnL();
hProf[iprofile]->SetBinContent(ipoint+1,ll-lbest);
// hProf[0]->SetBinContent(ipoint+1,ll);
xx+=dx;
}
//set parameter back to initial value
thePars->setParameter(ipar,bestpoint);
hProf[iprofile]->SetLineColor(9);
hProf[iprofile]->GetYaxis()->SetTitle("#Delta #chi^{2}");
TString xname = "parameter ";
xname.Append(Form("%d",ipar));
hProf[iprofile]->GetXaxis()->SetTitle(xname.Data());
hProf[iprofile]->Draw("c");
if (sameflg) hProf[iprofile]->SetLineColor(kRed);
if (sameflg){
hProf[0]->Draw("c");
hProf[iprofile]->Draw("samec");
}
else{
hProf[0]->Draw("c");
}
cc->Print(pname.Data());
return;
}
void histoCompare::profileL(int ibin, int icomp, int iatt, int imod, double range, int npts){
TString pname = "p";
pname.Append("_profile.png");
//double bestpoint = Par[ibin][icomp][iatt][imod];
double bestpoint = thePars->histoPar[ibin][icomp][iatt][imod];
cout<<"Fitted value: "<<bestpoint<<endl;
double dx = range/(double)npts;
double xx = bestpoint - (range/2.);
double ll;
double lbest = getTotLnL();
if (hProf[0]) hProf[0]->Delete();
hProf[0] = new TH1D("hprof","hprof",npts,xx,(xx+range));
for (int ipoint=0;ipoint<npts;ipoint++){
// cout<<"filling point" <<ipoint<<endl;
//Par[ibin][icomp][iatt][imod] = xx;
thePars->setParameter(ibin,icomp,iatt,imod,xx);
ll = getTotLnL();
hProf[0]->SetBinContent(ipoint+1,ll-lbest);
// hProf[0]->SetBinContent(ipoint+1,ll);
xx+=dx;
}
//set parameter back to initial value
thePars->setParameter(ibin,icomp,iatt,imod,bestpoint);
hProf[0]->SetLineColor(9);
hProf[0]->Draw("c");
cc->Print(pname.Data());
return;
}
void histoCompare::showFitPars(int ibin,int iatt,int imod){
const int nbinstot = nComp;
double X[nbinstot];
double EXL[nbinstot];
double EXH[nbinstot];
double EYL[nbinstot];
double EYH[nbinstot];
double Y[nbinstot];
int parindex;
if (gPar) gPar->Delete();
hPar = new TH1D("hpar","hpar",nbinstot,0,nbinstot);
hParErrLo = new TH1D("hparerrlo","hparerrlo",nbinstot,0,nbinstot);
hParErrHi = new TH1D("hparerrhi","hparerrhi",nbinstot,0,nbinstot);
for (int icomp=0;icomp<nComp;icomp++){
parindex=thePars->getParIndex(ibin,icomp,iatt,imod);
hPar->SetBinContent(icomp+1,thePars->getParameter(parindex));
X[icomp]=(double)hPar->GetBinCenter(icomp+1);
Y[icomp]=(double)thePars->getParameter(parindex);
// hPar->GetXaxis()->SetBinLabel(icomp+1,parName[ibin][icomp][iatt][imod].Data());
EYL[icomp]=(double)thePars->getParameter(parindex) -errParLo[parindex];
EYH[icomp]=errParHi[parindex] - (double)thePars->getParameter(parindex);
hParErrLo->SetBinContent(icomp+1,(double)EYL[icomp]);
hParErrHi->SetBinContent(icomp+1,(double)EYH[icomp]);
EXL[icomp]=0.4;
EXH[icomp]=0.4;
}
gPar = new TGraphAsymmErrors(nbinstot,X,Y,EXL,EXH,EYL,EYH);
gPar->SetFillColor(6);
gPar->SetLineColor(6);
gPar->SetMarkerStyle(8);
gPar->Draw("a2");
gPar->Draw("p");
return;
}
void histoCompare::showModHiso(int isamp,int ibin, int icomp, int iatt, double smear, double bias){
double biastmp = thePars->histoPar[ibin][icomp][iatt][1];
double smeartmp = thePars->histoPar[ibin][icomp][iatt][1];
thePars->setParameter(ibin,icomp,iatt,0,smear);
thePars->setParameter(ibin,icomp,iatt,1,bias);
hMod = hManager->getModHistogram(isamp,ibin,icomp,iatt); //gets the modified histogram
hTmp = hManager->getHistogram(isamp,ibin,icomp,iatt);
// if (hMod) hMod->Delete();
// if (hTmp) hTmp->Delete();
// if (hTot) hTot->Delete();
// hMod = (TH1D*) hManager->hMC[isamp][ibin][icomp][iatt]->Clone("hclonetmp");
// hTot = (TH1D*) hManager->hMC[isamp][ibin][icomp][iatt]->Clone("htottmp");
// hTmp = (TH1D*) hManager->hMC[isamp][ibin][icomp][iatt]->Clone("htmp");
// smearHisto((*hManager->hMC[isamp][ibin][icomp][iatt]),(*hMod),smear,bias);
// for (int icomp=1;i<nComp;icomp++){
// smearHisto((*hManager->hMC[isamp][ibin][icomp][iatt]),(*hTmp),smear,bias);
// hMod->Add(hTmp);
// hTot->Add(hManager->hMC[isamp][ibin][icomp][iatt]);
// }
// hMod->Rebin(rebinFactor);
// hTot->Rebin(rebinFactor);
hMod->SetLineColor(kRed);
hTmp->Draw();
hMod->Draw("same");
//set parameter back to original
thePars->setParameter(ibin,icomp,iatt,0,smeartmp);
thePars->setParameter(ibin,icomp,iatt,1,biastmp);
return;
}
TH1D* histoCompare::showSmear(TH1D* h, double smear, double bias){
// TH1D* hh = smearIt(h,smear,bias);
return h;
}
void histoCompare::showFitResult(int isamp,int ibin,int iatt){
//sum up mc components
//get MC prediction
hMod = (TH1D*)hManager->getSumHistogramMod(isamp,ibin,iatt)->Clone("hmod");
hTmp = hManager->getSumHistogram(isamp,ibin,iatt);
// hMod = smearIt(hManager->hMC[isamp][ibin][0][iatt],smear,bias);
// hTot = (TH1D*)hManager->hMC[isamp][ibin][0][iatt]->Clone("htot");
// for (int jcomp=1;jcomp<nComp;jcomp++){
// hTot->Add(hManager->hMC[isamp][ibin][jcomp][iatt]);
// smear = bestPar[ibin][jcomp][iatt][0];
// bias = bestPar[ibin][jcomp][iatt][1];
// hMod->Add(smearIt(hManager->hMC[isamp][ibin][jcomp][iatt],smear,bias));
// }
// hTot->SetLineColor(kRed);
// hMod->Scale(Norm);
// hTmp->Scale(Norm);
hMod->SetLineColor(kRed);
hTmp->SetLineColor(kBlue);
// hTot->Rebin(rebinFactor);
// hMod->Rebin(rebinFactor);
hTmp->Draw();
hMod->Draw("samee");
// hTmp->Draw("samee");
hManager->hData[isamp][ibin][iatt]->SetMarkerStyle(8);
hManager->hData[isamp][ibin][iatt]->Draw("samee");
return;
}
//Show the effect of varying a single bias or smear parameter
void histoCompare::showFitEffect(int isamp,int ibin,int icomp,int iatt){
//sum up mc components
hMod = hManager->getModHistogram(isamp,ibin,icomp,iatt); //gets the modified histogram
hTmp = hManager->getSumHistogram(isamp,ibin,iatt); //get the sum histogram
// double smear = bestPar[ibin][icomp][iatt][0];
// double bias = bestPar[ibin][icomp][iatt][1];
// cout<<"SMEAR: "<<smear<<endl;
// cout<<"BIAS: "<<bias<<endl;
// hMod = smearIt(hManager->hMC[isamp][ibin][icomp][iatt],smear,bias);
// hTot = (TH1D*)hManager->hMC[isamp][ibin][icomp][iatt]->Clone("htot");
for (int jcomp=0;jcomp<nComp;jcomp++){
if (jcomp!=icomp){
hMod->Add(hManager->hMC[isamp][ibin][jcomp][iatt]);
}
}
hMod->SetLineColor(kRed);
// hTmp->Scale(Norm);
hTmp->SetLineColor(kBlue);
// hMod->Scale(Norm);
// hTot->Rebin(rebinFactor);
// hMod->Rebin(rebinFactor);
// hTmp->Draw();
hMod->Draw();
hTmp->Draw("same");
hManager->hData[isamp][ibin][iatt]->SetMarkerStyle(8);
hManager->hData[isamp][ibin][iatt]->Draw("samee");
return;
}
///////////////////////////////////////////////////////////////////////////////////
//Plots the specified histogram before and after modifications, as well as the data
void histoCompare::showFitHisto(int isamp,int ibin,int icomp,int iatt){
double smear = thePars->histoPar[ibin][icomp][iatt][0];
double bias = thePars->histoPar[ibin][icomp][iatt][1];
cout<<"SMEAR: "<<smear<<endl;
cout<<"BIAS: "<<bias<<endl;
hMod = hManager->getModHistogram(isamp,ibin,icomp,iatt);
hMod->SetLineColor(kRed);
hMod->Draw("eh");
hTmp = hManager->getHistogram(isamp,ibin,icomp,iatt);
hTmp->SetLineColor(kBlue);
// hMod->Smooth(1);
// convolveThisHisto(*hMod,hMod->GetBinWidth(2)*0.5,0.);
hTmp->Draw("sameeh");
return;
}
void histoCompare::LnLPreFit(){
//setup static this so wrapper doesn't segfault
staticthis = this;
//threshold to determine if a parameter is fit or not
//if the MC histograms for this parameter have a size less than this value,
//don't bother fitting them!
double nthresh = 100.;
//sets the precision of the fits
double parerr = 0.005;
//individually fit each parameter
int parindex =0;
//int parindextmp;
//parameter name container
TString parnametmp;
// double parinit; //container to temporarily store initial values
//total number of parameters to be fit
int npars = thePars->nTotPars;
cout<<"$$$$$$$$$$$$$$$ LNL PRE FIT $$$$$$$$$$$$$$$"<<endl;
cout<<" ---------------------------------------- "<<endl;
cout<<" NUMBER OF PARAMETERS: "<<npars<<endl;
cout<<" PRECISION: "<<parerr<<endl;
cout<<" --------------------------------------- "<<endl;
//fix parameters with too few events to be fit
parindex = 0;
for (int ibin=0;ibin<nBin;ibin++){
for (int iatt=0;iatt<nAtt;iatt++){
for (int icomp=0;icomp<nComp;icomp++){
//name parameters
parnametmp = Form("par_%d_",parindex);
parnametmp.Append(binName[ibin].Data());
parnametmp.Append("_");
parnametmp.Append(compName[icomp].Data());
parnametmp.Append("_");
parnametmp.Append(attName[iatt].Data());
parnametmp.Append("_");
parnametmp.Append("smear");
parName[ibin][icomp][iatt][0]=parnametmp.Data();
parnametmp = Form("par_%d_",(parindex+1));
parnametmp.Append(binName[ibin].Data());
parnametmp.Append("_");
parnametmp.Append(compName[icomp].Data());
parnametmp.Append("_");
parnametmp.Append(attName[iatt].Data());
parnametmp.Append("_");
parnametmp.Append("bias");
parName[ibin][icomp][iatt][1]=parnametmp.Data();
//get summed histogram
hTot = (TH1D*)hManager->hMC[0][ibin][icomp][iatt]->Clone("htot");
for (int isamp=1;isamp<nSamp;isamp++){
hTot->Add(hManager->hMC[isamp][ibin][icomp][iatt]);
}
cout<<"total entries: "<<hTot->GetEntries()<<endl;
//check to make sure histogram is above fitting threshold
if (hTot->GetEntries()<nthresh){
cout<<" FIXING PARAMETER: "<<parName[ibin][icomp][iatt][0].Data()<<" (ENTRIES TOO LOW!) "<<endl;
fixPar[ibin][icomp][iatt][0]=1;
thePars->fixParameter(ibin,icomp,iatt,0);
thePars->fixParameter(ibin,icomp,iatt,1);
cout<<" FIXING PARAMETER: "<<parName[ibin][icomp][iatt][1].Data()<<" (ENTRIES TOO LOW!) "<<endl;
}
hTot->Delete();
parindex+=2;
}
}
}
cout<<" ---------------------------------------- "<<endl;
//setup the fitter!
TFitter* fit = new TFitter(npars);
//shut fitter up
{
double pp = -1;
fit->ExecuteCommand("SET PRINTOUT",&pp,1);
}
//specify function to be fit
fit->SetFCN(lnLWrapper);
//set parameters to inital values
TString pname;
for (int ipar=0;ipar<thePars->nTotPars;ipar++){
pname = Form("parameter%d",ipar);
fit->SetParameter(ipar,pname.Data(),thePars->pars[ipar],parerr,0,0);
}
//fix all parameters
for (int jpar=0;jpar<npars;jpar++){
fit->FixParameter(jpar);
}
//release and fit flux and xsec parameters
parindex = thePars->nTotPars-thePars->nSysPars;
for (int isyspar=0;isyspar<thePars->nSysPars;isyspar++){
fit->ReleaseParameter(parindex);
parindex++;
}
fit->ExecuteCommand("SIMPLEX",0,0);
parindex = 0;
for (int jpar=0;jpar<npars;jpar++){
fit->FixParameter(jpar);
}
//run individual bias fits
for (int jbin=0;jbin<nBin;jbin++){
for (int jatt=0;jatt<nAtt;jatt++){
for (int jcomp=0;jcomp<nComp;jcomp++){
parindex++; //< starts on odd parameter (bias only)
//release biasparameter to be fit
if (thePars->checkFixFlg(jbin,jcomp,jatt,1)==1){
parindex++; //< do nothing if parameter is fixed
continue;
}
fit->ReleaseParameter(parindex);
cout<<"fitting "<<jbin<<jcomp<<jatt<<1<<" # "<<thePars->getParIndex(jbin,jcomp,jatt,1)<<endl;
fit->ExecuteCommand("SIMPLEX",0,0);
fit->FixParameter(parindex);
parindex++;
}
}
}
parindex=0;
//run individual smear fits
for (int jbin=0;jbin<nBin;jbin++){
for (int jatt=0;jatt<nAtt;jatt++){
for (int jcomp=0;jcomp<nComp;jcomp++){
//starts on even parameter (smear only)
//release smear parameter to be fit
if (thePars->checkFixFlg(jbin,jcomp,jatt,0)==1){
parindex++; //<do nothing if parameter is fixed
continue;
}
fit->ReleaseParameter(parindex);
cout<<"fitting "<<jbin<<jcomp<<jatt<<0<<" # "<<thePars->getParIndex(jbin,jcomp,jatt,0)<<endl;
fit->ExecuteCommand("SIMPLEX",0,0);
fit->FixParameter(parindex);
// bestPar[jbin][jcomp][jatt][0] = fit->GetParameter(parindex);
parindex+=2;
}
}
}
//print final results
for (int ipar=0;ipar<npars;ipar++){
thePars->setParameter(ipar,fit->GetParameter(ipar));
cout<<" PAR "<<ipar<<" FIT RESULT: "<<thePars->pars[ipar]<<endl;
}
cout<<"$$$$$$$$$$$$$ END LNL PRE FIT $$$$$$$$$$$$$"<<endl;
return;
}
void histoCompare::sysParFit(){
//setup static this so wrapper doesn't segfault
staticthis = this;
//sets the precision of the fits
double parerr = 0.001;
cout<<"$$$$$$$$$$$$$$$$$ LNL SINLGE PAR FIT $$$$$$$$$$$$$$$$$"<<endl;
cout<<" ---------------------------------------- "<<endl;
cout<<" PRECISION: "<<parerr<<endl;
cout<<" --------------------------------------- "<<endl;
//setup the fitter!
//total number of parameters to be fit
int npars = thePars->nTotPars;
TFitter* fit = new TFitter(npars);
//shut fitter up
{
double pp = 0;
fit->ExecuteCommand("SET PRINTOUT",&pp,1);
}
//specify function to be fit
fit->SetFCN(lnLWrapper);
//setup parameters
TString aname;
for (int ipar=0;ipar<npars;ipar++){
aname = "parameter";
aname.Append(Form("_%d",ipar));
fit->SetParameter(ipar,aname.Data(),thePars->pars[ipar],parerr,0,0);
}
//fix all params
for (int jpar=0;jpar<npars;jpar++){
fit->FixParameter(jpar);
}
//release single parameter to fit
for (int jpar=(thePars->nTotPars-thePars->nSysPars);jpar<thePars->nTotPars;jpar++){
fit->ReleaseParameter(jpar);
}
//fit that thang
fit->ExecuteCommand("SIMPLEX",0,0);
fit->ExecuteCommand("MIGRAD",0,0);
//print results
for (int jpar=(thePars->nTotPars-thePars->nSysPars);jpar<thePars->nTotPars;jpar++){
cout<<"PAR: "<<jpar<<" "<<fit->GetParameter(jpar)<<endl;
}
return;
}
void histoCompare::singleParFit(int ipar){
//setup static this so wrapper doesn't segfault
staticthis = this;
//sets the precision of the fits
double parerr = 0.001;
cout<<"$$$$$$$$$$$$$$$$$ LNL SINLGE PAR FIT $$$$$$$$$$$$$$$$$"<<endl;
cout<<" ---------------------------------------- "<<endl;
cout<<" PARAMETER: "<<ipar<<endl;
cout<<" PRECISION: "<<parerr<<endl;
cout<<" --------------------------------------- "<<endl;
//setup the fitter!
//total number of parameters to be fit
int npars = thePars->nTotPars;
TFitter* fit = new TFitter(npars);
//shut fitter up
{
double pp = 0;
fit->ExecuteCommand("SET PRINTOUT",&pp,1);
}
//specify function to be fit
fit->SetFCN(lnLWrapper);
//setup parameters
TString aname;
for (int kpar=0;kpar<npars;kpar++){
aname = "parameter";
aname.Append(Form("_%d",kpar));
fit->SetParameter(kpar,aname.Data(),thePars->pars[kpar],parerr,0,0);
}
//fix all params
for (int jpar=0;jpar<npars;jpar++){
fit->FixParameter(jpar);
}
//release single parameter to fit
fit->ReleaseParameter(ipar);
//fit that thang
fit->ExecuteCommand("SIMPLEX",0,0);
fit->ExecuteCommand("MIGRAD",0,0);
//print results
cout<<"RESULT: "<<fit->GetParameter(ipar)<<endl;
return;
}
void histoCompare::LnLFit(){
//setup static this so wrapper doesn't segfault
staticthis = this;
//threshold to determine if a parameter is fit or not
//if the MC histograms for this parameter have a size less than this value,
//don't bother fitting them!
// double nthresh = 50.;
//sets the precision of the fits
double parerr = 0.001;
//individually fit each parameter
int parindex =0;
//parameter name container
TString parnametmp;
//will we fix all the smearing parameters?
if (flgFixAllSmearPars){
thePars->fixAllSmearPars();
}
// double parinit; //container to temporarily store initial values
//total number of parameters to be fit
int npars = thePars->nTotPars;
//run the prefit
LnLPreFit();
cout<<"$$$$$$$$$$$$$$$$$ LNL FIT $$$$$$$$$$$$$$$$$"<<endl;
cout<<" ---------------------------------------- "<<endl;
cout<<" NUMBER OF PARAMETERS: "<<npars<<endl;
cout<<" PRECISION: "<<parerr<<endl;
cout<<" --------------------------------------- "<<endl;
////////////////////////////////////////////////////////
//setup the fitter!
TFitter* fit = new TFitter(npars);
//shut fitter up
{
double pp = 0;
fit->ExecuteCommand("SET PRINTOUT",&pp,1);
}
//specify function to be fit
fit->SetFCN(lnLWrapper);
//setup parameters
TString aname;
for (int ipar=0;ipar<npars;ipar++){
// int kbin=thePars->binOfPar[ipar];
// int kcomp=thePars->compOfPar[ipar];
// int katt = thePars->attOfPar[ipar];
aname = "parameter_";
aname.Append(ipar);
fit->SetParameter(ipar,aname.Data(),thePars->pars[ipar],parerr,0,0);
}
parindex = 0;
//int parindextmp = 0;
///////////////////////////////////////////////////////////////////
//do individual fits
for (int jbin=0;jbin<nBin;jbin++){
for (int jatt=0;jatt<nAtt;jatt++){
//start of fit block//
//fix all parameters
for (int jpar=0;jpar<npars;jpar++){
fit->FixParameter(jpar);
}
//for bin 0, fit the xsec and flux parameters as well//
if (jbin==0){
//release all flux and xsec pars
for (int isyspar=(thePars->nTotPars-thePars->nSysPars);isyspar<thePars->nSysPars;isyspar++){