-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstants_fitter.C
1227 lines (965 loc) · 51 KB
/
constants_fitter.C
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
// Stand alone code to fit for alignment parameters A,e,M from mass fits
#include "Minuit2/FunctionMinimum.h"
#include "Minuit2/MnMinimize.h"
#include "Minuit2/MnMigrad.h"
#include "Minuit2/MnPrint.h"
#include "Minuit2/MnUserParameterState.h"
#include "Minuit2/FCNGradientBase.h"
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Dense>
#include <sys/time.h>
using Eigen::MatrixXd;
using Eigen::VectorXd;
using namespace ROOT::Minuit2;
using namespace std;
const int n_eta_bins = 24;
const int n_pt_bins = 5;
class TheoryFcn : public FCNGradientBase {
//class TheoryFcn : public FCNBase {
public:
TheoryFcn(const vector<double> &meas, const vector<double> &err, const vector<string> &bin_labels, const TMatrixD &V_phys_to_int, const TMatrixD &V_int_to_phys) : scaleSquared(meas), scaleSquaredError(err), binLabels(bin_labels), VPhysToInt(V_phys_to_int), VIntToPhys(V_int_to_phys), errorDef(1.0) {}
~TheoryFcn() {}
virtual double Up() const {return errorDef;}
virtual void SetErrorDef(double def) {errorDef = def;}
virtual double operator()(const vector<double>&) const;
virtual vector<double> Gradient(const vector<double>& ) const;
virtual bool CheckGradient() const {return true;}
// virtual std::vector< double > ROOT::Minuit2::FCNGradientBase::Hessian(const std::vector< double > & )const -> allows to do Hessian analytically?
vector<int> getIndices(string bin_label) const;
double getK(const int pT_index) const;
const vector<double> pT_binning {25.0, 33.2011, 38.3067, 42.2411, 46.055, 55.0}; // pT binning goes here, this is for 2016
static constexpr double scaling_A = 0.001, scaling_e = 0.001 * 40.0, scaling_M = 0.001 / 40.0, scaling_e_prime = 0.001 / 0.01; // this scaling makes fitter parameters of order 1
vector<string> binLabels; // made public for the closure test
TMatrixD VPhysToInt, VIntToPhys;
private:
vector<double> scaleSquared;
vector<double> scaleSquaredError;
double errorDef;
};
//-----------------------------------------------
class TheoryFcn2 : public TheoryFcn {
public:
TheoryFcn2(const vector<double> &meas, const vector<double> &err, const vector<string> &bin_labels, const TMatrixD &V_phys_to_int, const TMatrixD &V_int_to_phys) : TheoryFcn(meas, err, bin_labels, V_phys_to_int, V_int_to_phys) {}
~TheoryFcn2() {}
vector<vector<double>> DummyData(const vector<double> &dummy_par_val, const int n_data_points, const double width, TRandom3 random) const; //TODO vector<vector<double>> might be slow
};
//-----------------------------------------------
// Function to get bin index from label name
vector<int> TheoryFcn::getIndices(string bin_label) const{
vector<int> indices;
int pos = 0;
for (int i=0; i<4-1;i++){ //4 for 4D binning
pos = bin_label.find("_");
indices.push_back(stoi(bin_label.substr(0,pos)));
bin_label.erase(0,pos+1); // 1 is the length of the delimiter, "_"
}
indices.push_back(stoi(bin_label));
return indices;
}
//-----------------------------------------------
// Function to get average k from pT bin index
double TheoryFcn::getK(const int pT_index) const{
// TODO what about errors on k?
// TODO could add the pT histo per eta,pt,eta,pt to get more accurate average pT
return 2.0 / (pT_binning[pT_index] + pT_binning[pT_index+1]); // k = 1 / avg_pT
}
//-----------------------------------------------
// Function to build reduced chi2
double TheoryFcn::operator()(const vector<double>& par) const {
// par has size n_parameters = 3*number_eta_bins, idices from 0 to n-1 contain A, from n to 2n-1 epsilon, from 2n to 3n-1 M
assert(par.size() == 3*n_eta_bins);
double chi2(0.0);
double diff(0.0);
double k_plus, k_minus, term_pos, term_neg;
double my_func;
int eta_pos_index, eta_neg_index;
vector<int> bin_indices(4); // for 4D binning
double k_middle = (1.0/pT_binning[n_pt_bins] + 1.0/pT_binning[0])/2.0;
vector<double> physical_M_pars(n_eta_bins);
TMatrixD internal_M_matrix(n_eta_bins,1), physical_M_matrix(n_eta_bins,1);
TArrayD internal_M_elements(n_eta_bins);
for(int i=0; i<n_eta_bins; i++){
internal_M_elements[i] = par[2*n_eta_bins + i];
}
internal_M_matrix.SetMatrixArray(internal_M_elements.GetArray());
physical_M_matrix = TMatrixD(VIntToPhys,TMatrixD::kMult,internal_M_matrix);
//std::cout<< "\n" << "physical M array"<< "\n";
for(int i=0; i<n_eta_bins; i++){
physical_M_pars[i] = TMatrixDColumn(physical_M_matrix,0)(i);
//std::cout<< " "<< physical_M_pars[i];
}
//std::cout<<"\n";
for(unsigned int n(0); n < scaleSquared.size() ; n++) {
bin_indices = getIndices(binLabels[n]);
eta_pos_index = bin_indices[0];
eta_neg_index = bin_indices[2];
// get k value and TODO error from pT bin index
k_plus = getK(bin_indices[1]);
k_minus = getK(bin_indices[3]);
// (1 + A(+) - e(+)k + M(+)/k)(1 + A(-) - e(-)k - M(-)/k) and scaling of A,e,M
//term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e*par[eta_pos_index + n_eta_bins]*k_plus + scaling_M*par[eta_pos_index + 2*n_eta_bins]/k_plus);
//term_neg = (1. + scaling_A*par[eta_neg_index] - scaling_e*par[eta_neg_index + n_eta_bins]*k_minus - scaling_M*par[eta_neg_index + 2*n_eta_bins]/k_minus);
// decorrelate A, e by shifting origin of k
// (1 + A'(+) - e'(+)k' + M(+)/k)(1 + A'(-) - e'(-)k' - M(-)/k) and scaling of A,e,M
//par[eta_pos_index] now has the meaning of A' rather than A
//par[eta_pos_index + n_eta_bins] now has the meaning of e' rather than e
//term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e_prime*par[eta_pos_index + n_eta_bins]*(k_plus - k_middle) + scaling_M*par[eta_pos_index + 2*n_eta_bins]/k_plus);
//term_neg = (1. + scaling_A*par[eta_neg_index] - scaling_e_prime*par[eta_neg_index + n_eta_bins]*(k_minus - k_middle) - scaling_M*par[eta_neg_index + 2*n_eta_bins]/k_minus);
// decorrelate A, e by shifting origin of k and decorrelate M by fitting a linear combination of physical parameters
term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e_prime*par[eta_pos_index + n_eta_bins]*(k_plus - k_middle) + scaling_M*physical_M_pars[eta_pos_index]/k_plus);
term_neg = (1. + scaling_A*par[eta_neg_index] - scaling_e_prime*par[eta_neg_index + n_eta_bins]*(k_minus - k_middle) - scaling_M*physical_M_pars[eta_neg_index]/k_minus);
my_func = term_pos*term_neg;
diff = my_func - scaleSquared[n];
chi2 += diff*diff/(scaleSquaredError[n]*scaleSquaredError[n]);
}
int ndof = scaleSquared.size() - par.size();
return (chi2/ndof - 1.0); // minimise reduced chi2 directly
}
//-----------------------------------------------
// Function to build gradient of reduced chi2 analytically
vector<double> TheoryFcn::Gradient(const vector<double> &par ) const {
assert(par.size() == 3*n_eta_bins);
vector<double> grad(par.size(),0.0);
TArrayD grad_wrt_M_physical_array(n_eta_bins), grad_wrt_M_internal_array(n_eta_bins); // TODO and if not eta 24 bins?
TMatrixD grad_wrt_M_physical_matrix(n_eta_bins,1), grad_wrt_M_internal_matrix(n_eta_bins,1);
double temp(0.0), local_func(0.0), local_grad(0.0);
double k_plus, k_minus, term_pos, term_neg;
double k_middle = (1.0/pT_binning[n_pt_bins] + 1.0/pT_binning[0])/2.0;
int eta_pos_index, eta_neg_index;
vector<int> bin_indices(4); // for 4D binning
int ndof = scaleSquared.size() - par.size();
vector<double> physical_M_pars(n_eta_bins);
TMatrixD internal_M_matrix(n_eta_bins,1), physical_M_matrix(n_eta_bins,1);
TArrayD internal_M_elements(n_eta_bins);
for(int i=0; i<n_eta_bins; i++){
internal_M_elements[i] = par[2*n_eta_bins + i];
}
internal_M_matrix.SetMatrixArray(internal_M_elements.GetArray());
physical_M_matrix = TMatrixD(VIntToPhys,TMatrixD::kMult,internal_M_matrix);
for(int i=0; i<n_eta_bins; i++){
physical_M_pars[i] = TMatrixDColumn(physical_M_matrix,0)(i);
}
for(unsigned int n(0); n < scaleSquared.size() ; n++) { // loops over measurements
// TODO I need everything from the chi2 function, maybe better solution to this
bin_indices = getIndices(binLabels[n]);
eta_pos_index = bin_indices[0];
eta_neg_index = bin_indices[2];
// get k value and TODO error from pT bin index
k_plus = getK(bin_indices[1]);
k_minus = getK(bin_indices[3]);
// (1 + A(+) - e(+)k + M(+)/k)(1 + A(-) - e(-)k - M(-)/k) and scaling of e,M
//term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e*par[eta_pos_index + n_eta_bins]*k_plus + scaling_M*par[eta_pos_index + 2*n_eta_bins]/k_plus);
//term_neg = (1. + scaling_A*par[eta_neg_index] - scaling_e*par[eta_neg_index + n_eta_bins]*k_minus - scaling_M*par[eta_neg_index + 2*n_eta_bins]/k_minus);
// decorrelate A, e by shifting origin of k
// (1 + A'(+) - e'(+)k' + M(+)/k)(1 + A'(-) - e'(-)k' - M(-)/k) and scaling of A,e,M
//par[eta_pos_index] now has the meaning of A' rather than A
//par[eta_pos_index + n_eta_bins] now has the meaning of e' rather than e
//term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e_prime*par[eta_pos_index + n_eta_bins]*(k_plus - k_middle) + scaling_M*par[eta_pos_index + 2*n_eta_bins]/k_plus);
//term_neg = (1. + scaling_A*par[eta_neg_index] - scaling_e_prime*par[eta_neg_index + n_eta_bins]*(k_minus - k_middle) - scaling_M*par[eta_neg_index + 2*n_eta_bins]/k_minus);
// decorrelate A, e by shifting origin of k and decorrelate M by fitting a linear combination of physical parameters
term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e_prime*par[eta_pos_index + n_eta_bins]*(k_plus - k_middle) + scaling_M*physical_M_pars[eta_pos_index]/k_plus);
term_neg = (1. + scaling_A*par[eta_neg_index] - scaling_e_prime*par[eta_neg_index + n_eta_bins]*(k_minus - k_middle) - scaling_M*physical_M_pars[eta_neg_index]/k_minus);
local_func = term_pos*term_neg;
temp=2.0*(local_func - scaleSquared[n])/(scaleSquaredError[n]*scaleSquaredError[n])/ndof;
//TODO not calculate terms before checking if needed, ahm, not needed
//for (unsigned int i : {eta_pos_index, eta_pos_index + n_eta_bins, eta_pos_index + 2*n_eta_bins, eta_neg_index, eta_neg_index + n_eta_bins, eta_neg_index + 2*n_eta_bins}) { // loops over parameters
// if (i == eta_pos_index) { // derivative wrt A(+), which is fpar[eta_pos_index]
// local_grad = scaling_A*term_neg;
// } else if (i == eta_pos_index + n_eta_bins) { // derivative wrt e(+)
// local_grad = -scaling_e*k_plus*term_neg;
// } else if (i == eta_pos_index + 2*n_eta_bins) { // derivative wrt M(+)
// local_grad = scaling_M/k_plus*term_neg;
// } else if (i == eta_neg_index){ // derivative wrt A(-)
// local_grad = scaling_A*term_pos;
// } else if (i == eta_neg_index + n_eta_bins) { // derivative wrt e(-)
// local_grad = -scaling_e*k_minus*term_pos;
// } else if (i == eta_neg_index + 2*n_eta_bins){ // derivative wrt M(-)
// local_grad = -scaling_M/k_minus*term_pos;
// } else {
// cout<<"\n"<<"ERROR: indices in grad don't match"<<"\n"; //TOOD raise a proper error
// }
//
//grad[i] += temp*local_grad;
//}
// INCORRECT
// decorrelate A, e by shifting origin of k and decorrelate M by fitting a linear combination of physical parameters
// for (unsigned int i : {eta_pos_index, eta_pos_index + n_eta_bins, eta_pos_index + 2*n_eta_bins, eta_neg_index, eta_neg_index + n_eta_bins, eta_neg_index + 2*n_eta_bins}) { // loops over parameters
// if (i == eta_pos_index) { // derivative wrt A'(+), which is fpar[eta_pos_index]
// local_grad = scaling_A*term_neg;
// } else if (i == eta_pos_index + n_eta_bins) { // derivative wrt e'(+)
// local_grad = -scaling_e_prime*(k_plus-k_middle)*term_neg;
// } else if (i == eta_pos_index + 2*n_eta_bins) { // derivative wrt M(+), next transform to derivative wrt M_internal(+)
// local_grad = scaling_M/k_plus*term_neg;
// } else if (i == eta_neg_index){ // derivative wrt A'(-)
// local_grad = scaling_A*term_pos;
// } else if (i == eta_neg_index + n_eta_bins) { // derivative wrt e'(-)
// local_grad = -scaling_e_prime*(k_minus-k_middle)*term_pos;
// } else if (i == eta_neg_index + 2*n_eta_bins){ // derivative wrt M(-), next transform to derivative wrt M_internal(-)
// local_grad = -scaling_M/k_minus*term_pos;
// } else {
// cout<<"\n"<<"ERROR: indices in grad don't match"<<"\n"; //TOOD raise a proper error
// }
// grad[i] += temp*local_grad;
//}
// Correct derivatives
// derivative wrt A'(+), which is fpar[eta_pos_index]
local_grad = scaling_A*term_neg;
grad[eta_pos_index] += temp*local_grad;
// derivative wrt e'(+)
local_grad = -scaling_e_prime*(k_plus-k_middle)*term_neg;
grad[eta_pos_index + n_eta_bins] += temp*local_grad;
// derivative wrt M(+), next transform to derivative wrt M_internal(+)
local_grad = scaling_M/k_plus*term_neg;
grad[eta_pos_index + 2*n_eta_bins] += temp*local_grad;
// derivative wrt A'(-)
local_grad = scaling_A*term_pos;
grad[eta_neg_index] += temp*local_grad;
// derivative wrt e'(-)
local_grad = -scaling_e_prime*(k_minus-k_middle)*term_pos;
grad[eta_neg_index + n_eta_bins] += temp*local_grad;
// derivative wrt M(-), next transform to derivative wrt M_internal(-)
local_grad = -scaling_M/k_minus*term_pos;
grad[eta_neg_index + 2*n_eta_bins] += temp*local_grad;
}
// -----------------------------------------------
// Transform M terms to derivatives wrt M_internal
for(int i=0; i<n_eta_bins; i++){
grad_wrt_M_physical_array[i] = grad[2*n_eta_bins + i];
}
grad_wrt_M_physical_matrix.SetMatrixArray(grad_wrt_M_physical_array.GetArray());
grad_wrt_M_internal_matrix = TMatrixD(VPhysToInt,TMatrixD::kMult,grad_wrt_M_physical_matrix);
for(int i=0; i<n_eta_bins; i++){
grad[2*n_eta_bins + i] = TMatrixDColumn(grad_wrt_M_internal_matrix,0)(i);
}
// -----------------------------------------------
return grad;
}
//-----------------------------------------------
// Function to generate dummy data for closure test
vector<vector<double>> TheoryFcn2::DummyData(const vector<double> &dummy_par_val, const int n_data_points, const double width, TRandom3 random) const {
// par has size n_parameters = 3*number_eta_bins, idices from 0 to n-1 contain A, from n to 2n-1 epsilon, from 2n to 3n-1 M
vector<vector<double>> res(2);
vector<double> test_data(n_data_points, 0.0), test_error(n_data_points, 0.0);
double k_plus, k_minus, mean, width_rand, term_pos, term_neg;
int eta_pos_index, eta_neg_index;
vector<double> DummyParVal(dummy_par_val);
vector<int> bin_indices(4); // for 4D binning
for(unsigned int n(0); n <n_data_points ; n++) {
bin_indices = getIndices(binLabels[n]);
eta_pos_index = bin_indices[0];
eta_neg_index = bin_indices[2];
// get k value and TODO error from pT bin index
k_plus = getK(bin_indices[1]);
k_minus = getK(bin_indices[3]);
// (1 + A(+) - e(+)k + M(+)/k)(1 + A(-) - e(-)k - M(-)/k) and scaling of e,M
term_pos = (1. + DummyParVal[eta_pos_index] - DummyParVal[eta_pos_index + n_eta_bins]*k_plus + DummyParVal[eta_pos_index + 2*n_eta_bins]/k_plus);
term_neg = (1. + DummyParVal[eta_neg_index] - DummyParVal[eta_neg_index + n_eta_bins]*k_minus - DummyParVal[eta_neg_index + 2*n_eta_bins]/k_minus);
mean = term_pos*term_neg;
width_rand = random.Gaus(width, width/10.0); // error on scale
width_rand = abs(2*mean*width_rand); // error on scale**2
test_error[n] = width_rand;
test_data[n] = random.Gaus(mean, width_rand);
if (n<10) {std::cout << "\n" << n << ": dummy scale_squared = " << test_data[n] << "; dummy scale_squared_error = " << test_error[n] << "\n";}
}
res[0] = test_data;
res[1] = test_error;
return res;
}
//-----------------------------------------------
// Function to get parameter name (A0, e1, etc.) and appropriate scaling from index in parameters array
tuple<string,double,string> getParameterNameAndScaling(int index){
int whole = index / n_eta_bins;
int rest = index % n_eta_bins;
double scaling;
string name, physical_name;
if (whole == 0) {
name = "A_prime" + to_string(rest);
physical_name = "A" + to_string(rest);
scaling = TheoryFcn::scaling_A;
}
else if (whole == 1) {
name = "e_prime" + to_string(rest);
physical_name = "e" + to_string(rest);
scaling = TheoryFcn::scaling_e_prime;
}
else if (whole == 2) {
name = "M_internal" + to_string(rest);
physical_name = "M" + to_string(rest);
scaling = TheoryFcn::scaling_M;
}
else {
cout<<"\n"<<"ERROR counting parameters"<<"\n";
}
return make_tuple(name, scaling, physical_name);
}
//-----------------------------------------------
// Function to generate dummy labels for closure test
vector<string> generateLabels (const int n_eta_bins, const int n_pt_bins){
vector<string> labels;
for (int i=0; i<n_eta_bins; i++){
for (int j=0; j<n_pt_bins; j++){
for (int k=0; k<n_eta_bins; k++){
for (int l=0; l<n_pt_bins; l++){
labels.push_back(to_string(i) + "_" + to_string(j) + "_" + to_string(k) + "_" + to_string(l));
}
}
}
}
return labels;
}
//-----------------------------------------------
// Function to generate dummy parameters for closure test
vector<double> generateParameters (const int n_parameters){ //TODO might be slow to pass the vector by value
vector<double> res(n_parameters); // has size n_parameters = 3*number_eta_bins, idices from 0 to n-1 contain A, from n to 2n-1 epsilon, from 2n to 3n-1 M
for (int i=0; i<n_parameters/3; i++){ // A from -0.0002 to 0.0005 and back
//res[i] = (-7.0*36.0/n_parameters/n_parameters*(i - n_parameters/6)*(i - n_parameters/6) + 5.0)*0.0001;
res[i] = 0.0004; // A set constant for now
}
for (int i=n_parameters/3; i<2*n_parameters/3; i++){ // e from 0.01 to 0.001 and back
//res[i] = (9.0*36.0/n_parameters/n_parameters*((i-n_parameters/3) - n_parameters/6)*((i-n_parameters/3) - n_parameters/6) + 1.0)*0.001;
res[i] = 0.002; // e set constant for now
}
for (int i=2*n_parameters/3; i<n_parameters; i++){ // M from 4*10^-5 to -2*10^-5 and back
//res[i] = ( 6.0*36.0/n_parameters/n_parameters*((i-2*n_parameters/3) - n_parameters/6)*((i-2*n_parameters/3) - n_parameters/6) - 2.0)*0.00001;
res[i] = 0.00001; // M set constant for now
}
return res;
}
//-----------------------------------------------
// Overload << operator
template <typename S>
ostream& operator<<(ostream& os,
const vector<S>& vector)
{
for (auto element : vector) {
os << element << " ";
}
return os;
}
//-----------------------------------------------
int constants_fitter() {
//-------------------------------------------------
// Get start time
double t1(0.);
struct timeval tv_start, tv_stop;
gettimeofday(&tv_start, 0);
//-------------------------------------------------
// Choose closure_test/analysis mode
string mode_option("analysis"); //TODO pass as command line argument
// Set verbosity
int verbosity = 2;
ROOT::Minuit2::MnPrint::SetGlobalLevel(verbosity);
//-------------------------------------------------
// Internal to physical parameter transformation
TMatrixD V_internal_to_physical(n_eta_bins, n_eta_bins);
TArrayD V_internal_to_physical_elements(n_eta_bins*n_eta_bins);
/*
//---------------------------------------------------------------------
// inverse of {(1, 1, 1, 1, ...),(1, -1, 0, 0, ...),(0, 1, -1, 0, ...)}
for(int j=0; j<n_eta_bins; j++){ // counts rows
for(int i=0; i<n_eta_bins; i++){ // counts collumns
if (i == 0){
V_internal_to_physical_elements[j*n_eta_bins+i] = 1.0/n_eta_bins;
} else if (j >= i) {
V_internal_to_physical_elements[j*n_eta_bins+i] = (-1.0)*i/n_eta_bins;
} else if (j < i){
V_internal_to_physical_elements[j*n_eta_bins+i] = double(n_eta_bins - i)/double(n_eta_bins);
} else {std::cout << "\n" << "Miscounted V decorrelating M";}
}
}
*/
/*
//---------------------------------------------------------------------
// inverse of {(1, 1, 1, 1, ...),(-1, 1, 0, 0, ...),(-1, 0, 1, 0, ...)}
for(int j=0; j<n_eta_bins; j++){
for(int i=0; i<n_eta_bins; i++){
if (i == 0){
V_internal_to_physical_elements[j*n_eta_bins+i] = 1.0/n_eta_bins;
} else if (j == i) {
V_internal_to_physical_elements[j*n_eta_bins+i] = double(n_eta_bins - 1)/double(n_eta_bins);
} else {
V_internal_to_physical_elements[j*n_eta_bins+i] = (-1.0)/n_eta_bins;
}
}
}
*/
/*
//-------------------------------------------------------------------
// inverse of {(1, 1, 1, 1, ...),(0, 1, 0, 0, ...),(0, 0, 1, 0, ...)}
V_internal_to_physical_elements[0] = 1.0;
for(int i=1; i<n_eta_bins; i++){ // j = 0
V_internal_to_physical_elements[i] = -1.0;
}
for(int j=1; j<n_eta_bins; j++){ // j starts from 1
for(int i=0; i<n_eta_bins; i++){
if (i == j){
V_internal_to_physical_elements[j*n_eta_bins+i] = 1.0;
} else {
V_internal_to_physical_elements[j*n_eta_bins+i] = 0.0;
}
}
}
*/
//identity e.g. M not decorrelated
for(int j=0; j<n_eta_bins; j++){
for(int i=0; i<n_eta_bins; i++){
if (j == i){
V_internal_to_physical_elements[j*n_eta_bins+i] = 1.0;
} else {
V_internal_to_physical_elements[j*n_eta_bins+i] = 0.0;
}
}
}
V_internal_to_physical.SetMatrixArray(V_internal_to_physical_elements.GetArray());
//std::cout<< "\n" << "VIntToPhys"<< "\n";
//V_internal_to_physical.Print();
//-------------------------------------------------
// Physical to internal parameter transformation
TMatrixD V_physical_to_internal(n_eta_bins, n_eta_bins);
TArrayD V_physical_to_internal_elements(n_eta_bins*n_eta_bins);
//for(int i=0; i<n_eta_bins; i++){ // j = 0 <- row 0
// V_physical_to_internal_elements[i] = 1.0;
//}
//for(int j=1; j<n_eta_bins; j++){ // counts rows, j starts from 1
// for(int i=0; i<n_eta_bins; i++){ // counts collumns
/*
//----------------------------------------------------------
// {(1, 1, 1, 1, ...),(1, -1, 0, 0, ...),(0, 1, -1, 0, ...)}
if (i == j){
V_physical_to_internal_elements[j*n_eta_bins+i] = -1.0;
} else if ( i == j-1) {
V_physical_to_internal_elements[j*n_eta_bins+i] = 1.0;
} else {
V_physical_to_internal_elements[j*n_eta_bins+i] = 0.0;
}
*/
/*
//----------------------------------------------------------
// {(1, 1, 1, 1, ...),(-1, 1, 0, 0, ...),(-1, 0, 1, 0, ...)}
if (i == j){
V_physical_to_internal_elements[j*n_eta_bins+i] = 1.0;
} else if ( i == 0) {
V_physical_to_internal_elements[j*n_eta_bins+i] = -1.0;
} else {
V_physical_to_internal_elements[j*n_eta_bins+i] = 0.0;
}
*/
//--------------------------------------------------------
// {(1, 1, 1, 1, ...),(0, 1, 0, 0, ...),(0, 0, 1, 0, ...)}
//if (i == j){
// V_physical_to_internal_elements[j*n_eta_bins+i] = 1.0;
//} else {
// V_physical_to_internal_elements[j*n_eta_bins+i] = 0.0;
//}
//}
//}
//identity e.g. M not decorrelated
for(int j=0; j<n_eta_bins; j++){
for(int i=0; i<n_eta_bins; i++){
if (j == i){
V_physical_to_internal_elements[j*n_eta_bins+i] = 1.0;
} else {
V_physical_to_internal_elements[j*n_eta_bins+i] = 0.0;
}
}
}
V_physical_to_internal.SetMatrixArray(V_physical_to_internal_elements.GetArray());
//std::cout<< "\n" << "VPhysToInt"<< "\n";
//V_physical_to_internal.Print();
//-------------------------------------------------
// (Optional) Run over many toys
int n_toys = 1;
if ( n_toys != 1 && mode_option.compare("analysis") == 0 ){
cout << "Can't run toys over data, will quit";
return 0;
}
if ( n_toys != 1 ) { verbosity = 0; }
auto track_tree = std::make_unique<TTree>("track_tree", "track_tree");
double red_chi2, edm;
double* red_chi2_ptr = &red_chi2;
double* edm_ptr = &edm;
vector<double> params_tmp(3*n_eta_bins); // 3 for A,e,M model
track_tree->Branch("red_chi2", red_chi2_ptr);
track_tree->Branch("edm", edm_ptr);
for(int w = 0; w<3*n_eta_bins; w++){ // 3 for A,e,M model
int rest_tmp = w % n_eta_bins;
if( w / n_eta_bins == 0) track_tree->Branch(Form("pull_A%d",rest_tmp), ¶ms_tmp[w]);
if( w / n_eta_bins == 1) track_tree->Branch(Form("pull_e%d",rest_tmp), ¶ms_tmp[w]);
if( w / n_eta_bins == 2) track_tree->Branch(Form("pull_M%d",rest_tmp), ¶ms_tmp[w]);
}
int z = 0;
//for(int z = 0; z<n_toys; z++){ // <- Loop over toys starts
TRandom3 iter_random = TRandom3(4357 + z);
//-------------------------------------------------
unsigned int n_data_points, n_parameters;
vector<string> labels;
vector<double> dummy_parameters(0.0);
vector<double> scale_squared_values, scale_squared_error_values;
if (mode_option.compare("closure_test") == 0) {
//-----------------------------------------------
// Get dummy data for closure test
labels = generateLabels(n_eta_bins,n_pt_bins);
n_data_points = labels.size();
n_parameters = 3*n_eta_bins; // 3 for A,e,M model
vector<double> empty_data(n_data_points, 0.0), empty_error(n_data_points, 0.0);
double error_start = 0.001;
dummy_parameters = generateParameters(n_parameters);
TheoryFcn2 f_dummy(empty_data, empty_error, labels, V_physical_to_internal, V_internal_to_physical);
vector<vector<double>> dummy_call = f_dummy.DummyData(dummy_parameters, n_data_points, error_start, iter_random);
scale_squared_values = dummy_call[0];
scale_squared_error_values = dummy_call[1];
} else if (mode_option.compare("analysis") == 0){
//-----------------------------------------------
// Get data
std::unique_ptr<TFile> inputFile( TFile::Open("InOutputFiles/mass_fits_control_histos_smear_beta_val.root") );
std::unique_ptr<TH1D> beta(inputFile->Get<TH1D>("beta"));
std::unique_ptr<TH1D> bin_occupancy(inputFile->Get<TH1D>("bin_occupancy"));
std::unique_ptr<TH1D> gaus_integral(inputFile->Get<TH1D>("gaus_integral"));
int n_data_points_initial = beta->GetEntries();
n_data_points = 0;
std::cout << "\n" << n_data_points_initial <<" entries initially in beta histogram";
for(int i=0; i<n_data_points_initial; i++){
if( bin_occupancy->GetBinContent(i+1) > 300.0 && gaus_integral->GetBinContent(i+1) > 0.8 ){
scale_squared_values.push_back((1.0 + beta->GetBinContent(i+1))*(1.0 + beta->GetBinContent(i+1)));
scale_squared_error_values.push_back(2*abs(1.0 + beta->GetBinContent(i+1))*(beta->GetBinError(i+1)));
labels.push_back(beta->GetXaxis()->GetLabels()->At(i)->GetName());
n_data_points += 1;
}
}
for(int i=0; i<10; i++){
std::cout << "\n" << labels[i] << ": beta = " << beta->GetBinContent(i+1) << "; scale_squared = " << scale_squared_values[i] << "; scale_squared_error = " << scale_squared_error_values[i] << "\n";
}
std::cout << "\n" << n_data_points << "entries remain from beta histogram";
std::cout << "\n" << "scale_squared_values.size() = " << scale_squared_values.size() << "; scale_squared_error_values.size() = " << scale_squared_error_values.size() << "labels.size() = " << labels.size();
n_parameters = 3*n_eta_bins; // 3 for A,e,M model
}
//-------------------------------------------------
// Use data
// TheoryFcn(const vector<double> &meas, const vector<double> &err, const vector<string> &bin_labels, const TMatrixD &V_phys_to_int, const TMatrixD &V_int_to_phys)
TheoryFcn fFCN(scale_squared_values, scale_squared_error_values, labels, V_physical_to_internal, V_internal_to_physical);
fFCN.SetErrorDef(1.0/(n_data_points - n_parameters)); // new error definition when minimising reduced chi2
// Create parameters with initial starting values
double start=0.0, par_error=0.01; // will store error on parameter before taking scaling into account
MnUserParameters upar;
// for(unsigned int i=0 ; i<n_parameters; ++i){
// upar.Add(Form("param%d",i), start, par_error);
//}
// for closure mode, translate dummy parameters to internal parameters and set as starting value
if (mode_option.compare("closure_test") == 0) {
// Translate dummy parameters to internal parameters and set as starting value
//TMatrixD internal_M_matrix(n_eta_bins,1), dummy_M_matrix(n_eta_bins,1);
//TArrayD dummy_M_elements(n_eta_bins);
//for(int i=0; i<n_eta_bins; i++){
// dummy_M_elements[i] = dummy_parameters[2*n_eta_bins + i] / (0.001 / 40.0); // divided by scaling_M = 0.001 / 40.0
//}
//dummy_M_matrix.SetMatrixArray(dummy_M_elements.GetArray());
//internal_M_matrix = TMatrixD(V_physical_to_internal,TMatrixD::kMult,dummy_M_matrix);
for (int i=0; i<n_parameters/3; i++){
//upar.Add(Form("param%d",i), dummy_parameters[i] / 0.001, par_error); // divided by scaling_A = 0.001
upar.Add(Form("param%d",i), start, par_error); // A_internal starts from 0 for now
//upar.Add(Form("param%d",i), start); // A_internal set constant to 0
}
for (int i=n_parameters/3; i<2*n_parameters/3; i++){
//upar.Add(Form("param%d",i), dummy_parameters[i] / (0.001 / 0.01), par_error); // divided by scaling_e_prime = 0.001 / 0.01
upar.Add(Form("param%d",i), start, par_error); // e_internal starts from 0 for now
//upar.Add(Form("param%d",i), start); // e_internal set constant to 0
}
for (int i=2*n_parameters/3; i<n_parameters; i++){
//upar.Add(Form("param%d",i), TMatrixDColumn(internal_M_matrix,0)(i-2*n_eta_bins), par_error);
upar.Add(Form("param%d",i), start, par_error); // M_internal starts from 0 for now
}
} else if (mode_option.compare("analysis") == 0){
for (int i=0; i<n_parameters/3; i++){
upar.Add(Form("param%d",i), start, par_error); // A_internal
//upar.Add(Form("param%d",i), start); // A_internal const
}
for (int i=n_parameters/3; i<2*n_parameters/3; i++){ // e_internal
upar.Add(Form("param%d",i), start, par_error);
//upar.Add(Form("param%d",i), start); // e_internal set constant to 0
}
for (int i=2*n_parameters/3; i<n_parameters; i++){ // M_internal
upar.Add(Form("param%d",i), start, par_error); //upar.Add(Form("param%d",i), double((i-2*n_eta_bins)/10.0), par_error);
}
}
//for (unsigned int i=0; i<upar.Params().size(); i++) {
//cout <<"par[" << i << "] = " << get<0>(getParameterNameAndScaling(i)) << ": " << upar.Params()[i] << "\n";
//}
// create Migrad minimizer
MnMigrad minimize(fFCN, upar, 1); //TODO strategy is 1, check others too
// ... and Minimize
unsigned int maxfcn(numeric_limits<unsigned int>::max());
double tolerance(0.001); //MIGRAD will stop iterating when edm : 0.002 * tolerance * UPERROR
//fFCN.SetErrorDef(1.0/(n_data_points - n_parameters));
//3829.158
FunctionMinimum min = minimize(maxfcn, tolerance);
// save results
double k_middle = (1.0/55.0 + 1.0/25.0)/2.0; //TODO change to not input by hand
vector<double> A_e_M_values(n_parameters), A_e_M_errors(n_parameters);
double cov_prime;
TMatrixD internal_M_matrix(n_eta_bins,1), internal_M_err_matrix(n_eta_bins,1), physical_M_matrix(n_eta_bins,1), physical_M_err_matrix(n_eta_bins,1);
TArrayD internal_M_data(n_eta_bins), internal_M_error_data(n_eta_bins);
vector<double> physical_M_parameters(n_eta_bins), physical_M_errors(n_eta_bins);
for(int i=0; i<n_eta_bins; i++){
internal_M_data[i] = min.UserState().Value(2*n_eta_bins + i);
internal_M_error_data[i] = min.UserState().Error(2*n_eta_bins + i);
}
internal_M_matrix.SetMatrixArray(internal_M_data.GetArray());
physical_M_matrix = TMatrixD(V_internal_to_physical,TMatrixD::kMult,internal_M_matrix);
internal_M_err_matrix.SetMatrixArray(internal_M_error_data.GetArray());
physical_M_err_matrix = TMatrixD(V_internal_to_physical,TMatrixD::kMult,internal_M_err_matrix);
for(int i=0; i<n_eta_bins; i++){
physical_M_parameters[i] = TMatrixDColumn(physical_M_matrix,0)(i);
physical_M_errors[i] =TMatrixDColumn(physical_M_err_matrix,0)(i);
}
for (int i=0; i<n_parameters; i++){
int whole = i / n_eta_bins;
//term_pos = (1. + scaling_A*par[eta_pos_index] - scaling_e_prime*par[eta_pos_index + n_eta_bins]*(k_plus - k_middle) + scaling_M*physical_M_pars[eta_pos_index]/k_plus);
if (whole == 0) { // A = scaling_A_prime*par[A_prime] + scaling_e_prime*par[e_prime]*k_middle
cov_prime = min.UserState().Covariance().Data()[(i+n_eta_bins)*(i+n_eta_bins+1)/2.0 + i];
//corr_hist->SetBinContent(bin, covariance[(i_temp-1)*(i_temp)/2+(j_temp-1)] / min.UserState().Error(i-1) / min.UserState().Error(j-1));
A_e_M_values[i] = min.UserState().Value(i) * get<1>(getParameterNameAndScaling(i)) + min.UserState().Value(i+n_eta_bins) * get<1>(getParameterNameAndScaling(i+n_eta_bins)) * k_middle;
A_e_M_errors[i] = pow(pow(get<1>(getParameterNameAndScaling(i)),2)*pow(min.UserState().Error(i),2)+pow(get<1>(getParameterNameAndScaling(i+n_eta_bins))*k_middle,2)*pow(min.UserState().Error(i+n_eta_bins),2)+2.0*get<1>(getParameterNameAndScaling(i))*get<1>(getParameterNameAndScaling(i+n_eta_bins))*k_middle*cov_prime, 0.5); // with cov[A',e']
}
else if (whole == 1) { // e = scaling_e_prime*par[e_prime]
A_e_M_values[i] = min.UserState().Value(i) * get<1>(getParameterNameAndScaling(i));
A_e_M_errors[i] = min.UserState().Error(i) * abs(get<1>(getParameterNameAndScaling(i)));
}
else if (whole == 2) { // M
//cout << i<<" " << physical_M_parameters[i - 2*n_eta_bins] * get<1>(getParameterNameAndScaling(i)) << "\n";
A_e_M_values[i] = physical_M_parameters[i - 2*n_eta_bins] * get<1>(getParameterNameAndScaling(i)); //min.UserState().Value(i) * get<1>(getParameterNameAndScaling(i));
//cout << i<<" " << A_e_M_values[i] << "\n";
A_e_M_errors[i] = physical_M_errors[i - 2*n_eta_bins] * abs(get<1>(getParameterNameAndScaling(i))); //min.UserState().Error(i) *get<1>(getParameterNameAndScaling(i));
}
}
if ( n_toys != 1 ) {
red_chi2 = min.Fval();
edm = min.Edm();
for (int i=0; i<n_parameters; i++){
params_tmp[i] = (A_e_M_values[i] - dummy_parameters[i]) / A_e_M_errors[i];
}
track_tree->Fill();
}
//} // <- Loop over toys finishes
if ( n_toys != 1 ) {
std::unique_ptr<TFile> f_control_iter( TFile::Open("track_params.root", "RECREATE") );
track_tree->Write();
cout<<"\n"<<"done running over toys"<<"\n";
f_control_iter->Close();
}
gettimeofday(&tv_stop, 0);
t1 = (tv_stop.tv_sec - tv_start.tv_sec)*1000.0 + (tv_stop.tv_usec - tv_start.tv_usec)/1000.0;
cout << "# Calculation time: " << t1/60000.0 << " min" << "\n";
if ( n_toys != 1) {
return 0;
}
cout << "chi^2/ndf: " << min.Fval() + 1. << "\n" << "\n";
cout << "min is valid: " << min.IsValid() << std::endl;
cout << "HesseFailed: " << min.HesseFailed() << std::endl;
cout << "HasCovariance: " << min.HasCovariance() << std::endl;
cout << "HasValidCovariance: " << min.HasValidCovariance() << std::endl;
cout << "HasValidParameters: " << min.HasValidParameters() << std::endl;
cout << "IsAboveMaxEdm: " << min.IsAboveMaxEdm() << std::endl;
cout << "HasReachedCallLimit: " << min.HasReachedCallLimit() << std::endl;
cout << "HasAccurateCovar: " << min.HasAccurateCovar() << std::endl;
cout << "HasPosDefCovar : " << min.HasPosDefCovar() << std::endl;
cout << "HasMadePosDefCovar : " << min.HasMadePosDefCovar() << std::endl;
cout << min << "\n";
cout << "\tHesse..." << endl;
MnHesse hesse(1);
hesse(fFCN, min);
vector<double> hessian = min.UserState().Hessian().Data();
vector<double> covariance = min.UserState().Covariance().Data();
TH2D *hessian_hist = new TH2D("hessian_hist", "Hessian", n_parameters, 0, n_parameters, n_parameters, 0, n_parameters);
TH2D *covariance_hist = new TH2D("covariance_hist", "Covariance", n_parameters, 0, n_parameters, 0, n_parameters);
TH2D *corr_hist = new TH2D("corr_hist", "Correlation", n_parameters, 0, n_parameters, n_parameters, 0, n_parameters);
TH2D *corr_physical_hist = new TH2D("corr_physical_hist", "Correlation physical parameters", n_parameters, 0, n_parameters, n_parameters, 0, n_parameters);
int bin, i_temp, j_temp, bin_x, bin_y;
for (int i=1; i<=n_parameters; i++){ // counts x axis bins left to right
hessian_hist->GetXaxis()->SetBinLabel(i,get<0>(getParameterNameAndScaling(i-1)).c_str());
hessian_hist->GetYaxis()->SetBinLabel(i,get<0>(getParameterNameAndScaling(n_parameters-i)).c_str());
covariance_hist->GetXaxis()->SetBinLabel(i,get<0>(getParameterNameAndScaling(i-1)).c_str());
covariance_hist->GetYaxis()->SetBinLabel(i,get<0>(getParameterNameAndScaling(n_parameters-i)).c_str());
corr_hist->GetXaxis()->SetBinLabel(i,get<0>(getParameterNameAndScaling(i-1)).c_str());
corr_hist->GetYaxis()->SetBinLabel(i,get<0>(getParameterNameAndScaling(n_parameters-i)).c_str());
for (int j=1; j<=i; j++){ // counts y axis bins, n_parameters+1-j counts bins up to down
//if ((i<=n_eta_bins || i>n_eta_bins*2) && (j<=n_eta_bins || j>n_eta_bins*2)){ //TODO remove if and else block when fitting 3 pars
bin = hessian_hist->GetBin(i, n_parameters+1-j);
i_temp = i; // i_temp = (i > n_eta_bins*2) ? i - n_eta_bins : i; //TODO when fitting 3 pars i_temp can be just i
j_temp = j; // j_temp = (j > n_eta_bins*2) ? j - n_eta_bins : j; //TODO when fitting 3 pars j_temp can be just j
hessian_hist->SetBinContent(bin, hessian[(i_temp-1)*(i_temp)/2+(j_temp-1)]);
covariance_hist->SetBinContent(bin, covariance[(i_temp-1)*(i_temp)/2+(j_temp-1)]);
corr_hist->SetBinContent(bin, covariance[(i_temp-1)*(i_temp)/2+(j_temp-1)] / min.UserState().Error(i-1) / min.UserState().Error(j-1)); // TODO when fitting 2 pars is i i_temp, j j_temp?
bin = hessian_hist->GetBin(j, n_parameters+1-i);
hessian_hist->SetBinContent(bin, hessian[(i_temp-1)*(i_temp)/2+(j_temp-1)]);
covariance_hist->SetBinContent(bin, covariance[(i_temp-1)*(i_temp)/2+(j_temp-1)]);
corr_hist->SetBinContent(bin, covariance[(i_temp-1)*(i_temp)/2+(j_temp-1)] / min.UserState().Error(i-1) / min.UserState().Error(j-1)); // TODO when fitting 2 pars is i i_temp, j j_temp?
//} else {
//bin = hessian_hist->GetBin(i, n_parameters+1-j);
//hessian_hist->SetBinContent(bin, 0.0);
//covariance_hist->SetBinContent(bin, 0.0);
//corr_hist->SetBinContent(bin, 0.0);
//bin = hessian_hist->GetBin(j, n_parameters+1-i);
//hessian_hist->SetBinContent(bin, 0.0);
//covariance_hist->SetBinContent(bin, 0.0);
//corr_hist->SetBinContent(bin, 0.0);
// }
}
}
double hessian_ar[n_parameters*n_parameters];
double covariance_ar[n_parameters*n_parameters];
double corr_ar[n_parameters*n_parameters];
for (int i=1; i<=n_parameters; i++){
for (int j=1; j<=n_parameters; j++){
// define arrays collumn wise
hessian_ar[(i-1)*n_parameters+(j-1)] = hessian_hist->GetBinContent(hessian_hist->GetBin(i, n_parameters+1-j));
covariance_ar[(i-1)*n_parameters+(j-1)] = covariance_hist->GetBinContent(covariance_hist->GetBin(i, n_parameters+1-j));
corr_ar[(i-1)*n_parameters+(j-1)] = corr_hist->GetBinContent(corr_hist->GetBin(i, n_parameters+1-j));
//TODO scaling???
}
}
//cout << "Hessian: " << hessian <<"\n"<< "Hessian: " <<"\n";
TMatrixTSym<double> Hessian_matrix(n_parameters, hessian_ar, "F"); //need option F to unroll collumn wise //TODO pass by ptr
for(int i=0; i<n_parameters; i++){
for(int j=0; j<n_parameters; j++){
//cout << Hessian_matrix(i,j) << " ";
}
//cout << "\n";
}
//cout << "\n";
//cout << "Covariance: " << covariance << "\n" << "Covariance: " << "\n";
TMatrixTSym<double> Covariance_matrix(n_parameters, covariance_ar, "F"); //need option F to unroll collumn wise //TODO pass by ptr
for(int i=0; i<n_parameters; i++){
for(int j=0; j<n_parameters; j++){
//cout << Covariance_matrix(i,j) << " ";
}
//cout << "\n";
}
TMatrixD U_internal_to_physical(n_parameters, n_parameters); // TODO what if n_parameters != 3*netabins?
TArrayD U_internal_to_physical_elements(n_parameters*n_parameters);
for(int j=0; j<n_eta_bins*3; j++){ // counts rows
if (j<n_eta_bins){ // A rows
for(int i=0; i<n_eta_bins*3; i++){
if(i == j){
U_internal_to_physical_elements[j*n_eta_bins*3+i] = get<1>(getParameterNameAndScaling(i)); // A collumns non empty
} else if (i >= n_eta_bins && i == j + n_eta_bins && i < n_eta_bins*2){
U_internal_to_physical_elements[j*n_eta_bins*3+i] = get<1>(getParameterNameAndScaling(i))*k_middle; // e collumns non empty
} else {
U_internal_to_physical_elements[j*n_eta_bins*3+i] = 0.0; // M collumns and the rest
}
}
} else if(j<2*n_eta_bins){ // e rows
for(int i=0; i<n_eta_bins*3; i++){
if(i == j){
U_internal_to_physical_elements[j*n_eta_bins*3+i] = get<1>(getParameterNameAndScaling(i)); // e collumns non empty
} else {
U_internal_to_physical_elements[j*n_eta_bins*3+i] = 0.0; // the rest
}
}
} else if(j<3*n_eta_bins){ // M rows
for(int i=0; i<n_eta_bins*3; i++){
if(i == j){
U_internal_to_physical_elements[j*n_eta_bins*3+i] = get<1>(getParameterNameAndScaling(i)); // M collumns non empty // TODO this is insufficient if you decorrelate M
} else {
U_internal_to_physical_elements[j*n_eta_bins*3+i] = 0.0; // the rest
}
}
} else {std::cout << "\n" << "Miscounted U";}
}
U_internal_to_physical.SetMatrixArray(U_internal_to_physical_elements.GetArray());
//U_internal_to_physical.Print();
TMatrixD left_term(n_parameters, n_parameters), covariance_matrix_physical(n_parameters, n_parameters);
left_term = TMatrixD(U_internal_to_physical,TMatrixD::kMult,Covariance_matrix);
covariance_matrix_physical = TMatrixD(left_term, TMatrixD::kMultTranspose,U_internal_to_physical);
//TMatrixD U_internal_to_physical_transpose(TMatrixD::kTransposed,U_internal_to_physical);
//left_term = TMatrixD(U_internal_to_physical_transpose, TMatrixD::kMult, Covariance_matrix);
//covariance_matrix_physical = TMatrixD(left_term, TMatrixD::kMult,U_internal_to_physical);
for(int i=1; i<=n_parameters; i++){
corr_physical_hist->GetXaxis()->SetBinLabel(i,get<2>(getParameterNameAndScaling(i-1)).c_str());
corr_physical_hist->GetYaxis()->SetBinLabel(i,get<2>(getParameterNameAndScaling(n_parameters-i)).c_str());
for(int j=1; j<=n_parameters; j++){
bin = corr_physical_hist->GetBin(i, n_parameters-j+1);
corr_physical_hist->SetBinContent(bin, covariance_matrix_physical(i-1,j-1) / A_e_M_errors[i-1] / A_e_M_errors[j-1]);
}
}
//cout << "Correlation: " << "\n";
TMatrixTSym<double> Corr_matrix(n_parameters, corr_ar, "F"); //need option F to unroll collumn wise //TODO pass by ptr
for(int i=0; i<n_parameters; i++){
for(int j=0; j<n_parameters; j++){
//cout << Corr_matrix(i,j) << " ";