-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.C
1591 lines (1416 loc) · 53.1 KB
/
Graphics.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
#include "MixingFit.h"
#include "TGraph.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TROOT.h"
#include "TF1.h"
#include "TPolyLine.h"
#include "TEllipse.h"
#include "TBox.h"
#include "TAxis.h"
#include "TColor.h"
#include "TGaxis.h"
#include "TLine.h"
#include "TLegend.h"
#include "TLatex.h"
#include "TMath.h"
#include "TPaveText.h"
#include <cmath>
#include <vector>
#include <map>
int MixDrawer::pointsPerContour = 3600;
int MixDrawer::graphicsXIndex = 0;
int MixDrawer::graphicsYIndex = 1;
int MixDrawer::ymult = 1;
MixDrawer::MixDrawer ()
: directory("testing")
, xmin(-0.02)
, xmax(0.02)
, ymin(0)
, ymax(0.045)
, xaxisTitle("x")
, yaxisTitle("y")
, extraXMultiplier(-1)
, extraYMultiplier(-1)
{}
void MixDrawer::draw () {
//gROOT->ProcessLine(".L ./lhcb_style.cc");
// Use times new roman, precision 2
Int_t lhcbFont = 132; // Old LHCb style: 62;
// Line thickness
Double_t lhcbWidth = 2.00; // Old LHCb style: 3.00;
// Text size
Double_t lhcbTSize = 0.06;
// use plain black on white colors
gROOT->SetStyle("Plain");
TStyle *lhcbStyle= new TStyle("lhcbStyle","LHCb plots style");
//lhcbStyle->SetErrorX(0); // don't suppress the error bar along X
lhcbStyle->SetFillColor(1);
lhcbStyle->SetFillStyle(1001); // solid
lhcbStyle->SetFrameFillColor(0);
lhcbStyle->SetFrameBorderMode(0);
lhcbStyle->SetPadBorderMode(0);
lhcbStyle->SetPadColor(0);
lhcbStyle->SetCanvasBorderMode(0);
lhcbStyle->SetCanvasColor(0);
lhcbStyle->SetStatColor(0);
lhcbStyle->SetLegendBorderSize(0);
// If you want the usual gradient palette (blue -> red)
lhcbStyle->SetPalette(1);
// If you want colors that correspond to gray scale in black and white:
int colors[8] = {0,5,7,3,6,2,4,1};
lhcbStyle->SetPalette(8,colors);
// set the paper & margin sizes
lhcbStyle->SetPaperSize(20,26);
lhcbStyle->SetPadTopMargin(0.05);
lhcbStyle->SetPadRightMargin(0.09); // increase for colz plots
lhcbStyle->SetPadBottomMargin(0.16);
lhcbStyle->SetPadLeftMargin(0.20);
// use large fonts
lhcbStyle->SetTextFont(lhcbFont);
lhcbStyle->SetTextSize(lhcbTSize);
lhcbStyle->SetLabelFont(lhcbFont,"x");
lhcbStyle->SetLabelFont(lhcbFont,"y");
lhcbStyle->SetLabelFont(lhcbFont,"z");
lhcbStyle->SetLabelSize(lhcbTSize,"x");
lhcbStyle->SetLabelSize(lhcbTSize,"y");
lhcbStyle->SetLabelSize(lhcbTSize,"z");
lhcbStyle->SetTitleFont(lhcbFont);
lhcbStyle->SetTitleFont(lhcbFont,"x");
lhcbStyle->SetTitleFont(lhcbFont,"y");
lhcbStyle->SetTitleFont(lhcbFont,"z");
lhcbStyle->SetTitleSize(1.2*lhcbTSize,"x");
lhcbStyle->SetTitleSize(1.2*lhcbTSize,"y");
lhcbStyle->SetTitleSize(1.2*lhcbTSize,"z");
// use medium bold lines and thick markers
lhcbStyle->SetLineWidth(lhcbWidth);
lhcbStyle->SetFrameLineWidth(lhcbWidth);
lhcbStyle->SetHistLineWidth(lhcbWidth);
lhcbStyle->SetFuncWidth(lhcbWidth);
lhcbStyle->SetGridWidth(lhcbWidth);
lhcbStyle->SetLineStyleString(2,"[12 12]"); // postscript dashes
lhcbStyle->SetMarkerStyle(20);
lhcbStyle->SetMarkerSize(1.0);
// label offsets
lhcbStyle->SetLabelOffset(0.010,"X");
lhcbStyle->SetLabelOffset(0.010,"Y");
// by default, do not display histogram decorations:
lhcbStyle->SetOptStat(0);
//lhcbStyle->SetOptStat("emr"); // show only nent -e , mean - m , rms -r
// full opts at http://root.cern.ch/root/html/TStyle.html#TStyle:SetOptStat
lhcbStyle->SetStatFormat("6.3g"); // specified as c printf options
lhcbStyle->SetOptTitle(0);
lhcbStyle->SetOptFit(0);
//lhcbStyle->SetOptFit(1011); // order is probability, Chi2, errors, parameters
//titles
lhcbStyle->SetTitleOffset(1.05,"X");
lhcbStyle->SetTitleOffset(1.55,"Y");
lhcbStyle->SetTitleOffset(1.2,"Z");
lhcbStyle->SetTitleFillColor(0);
lhcbStyle->SetTitleStyle(0);
lhcbStyle->SetTitleBorderSize(0);
lhcbStyle->SetTitleFont(lhcbFont,"title");
lhcbStyle->SetTitleX(0.0);
lhcbStyle->SetTitleY(1.0);
lhcbStyle->SetTitleW(1.0);
lhcbStyle->SetTitleH(0.05);
// look of the statistics box:
lhcbStyle->SetStatBorderSize(0);
lhcbStyle->SetStatFont(lhcbFont);
lhcbStyle->SetStatFontSize(0.05);
lhcbStyle->SetStatX(0.9);
lhcbStyle->SetStatY(0.9);
lhcbStyle->SetStatW(0.25);
lhcbStyle->SetStatH(0.15);
// put tick marks on top and RHS of plots
lhcbStyle->SetPadTickX(1);
lhcbStyle->SetPadTickY(1);
// histogram divisions: only 5 in x to avoid label overlaps
lhcbStyle->SetNdivisions(505,"x");
lhcbStyle->SetNdivisions(510,"y");
gROOT->SetStyle("lhcbStyle");
gROOT->ForceStyle();
// add LHCb label
TPaveText *lhcbName = new TPaveText(gStyle->GetPadLeftMargin() + 0.05,
0.87 - gStyle->GetPadTopMargin(),
gStyle->GetPadLeftMargin() + 0.20,
0.95 - gStyle->GetPadTopMargin(),
"BRNDC");
lhcbName->AddText("LHCb");
lhcbName->SetFillColor(0);
lhcbName->SetTextAlign(12);
lhcbName->SetBorderSize(0);
TText *lhcbLabel = new TText();
lhcbLabel->SetTextFont(lhcbFont);
lhcbLabel->SetTextColor(1);
lhcbLabel->SetTextSize(lhcbTSize);
lhcbLabel->SetTextAlign(12);
TLatex *lhcbLatex = new TLatex();
lhcbLatex->SetTextFont(lhcbFont);
lhcbLatex->SetTextColor(1);
lhcbLatex->SetTextSize(lhcbTSize);
lhcbLatex->SetTextAlign(12);
std::cout << "-------------------------" << std::endl;
std::cout << "Set LHCb Style - Feb 2012" << std::endl;
std::cout << "-------------------------" << std::endl;
/*
gStyle->SetCanvasBorderMode(0);
gStyle->SetCanvasColor(10);
gStyle->SetFrameFillColor(10);
gStyle->SetFrameBorderMode(0);
gStyle->SetPadColor(0);
gStyle->SetTitleColor(1);
gStyle->SetStatColor(0);
gStyle->SetFillColor(0);
gStyle->SetFuncWidth(1);
gStyle->SetLineWidth(1);
gStyle->SetLineColor(1);
gStyle->SetPalette(1, 0);
gStyle->SetOptStat(0);
gStyle->SetTitleBorderSize(0);
gStyle->SetTitleSize(0.04);
gStyle->SetPadLeftMargin(0.13);
gStyle->SetPadBottomMargin(0.13);
gStyle->SetPadRightMargin(0.13);
gStyle->SetPadTopMargin(0.13);
gStyle->SetLineStyleString(11, "0 5000");
gStyle->SetLineStyleString(12, "100 50");
TGaxis::SetMaxDigits(2);*/
TCanvas* foo = new TCanvas();
foo->Size(40,40);
foo->SetFillColor(0);
foo->SetBorderMode(0);
foo->SetFrameBorderMode(0);
// Kludge to get nice axis endpoints.
// Taking weighted averages of xmin and xmax
// to get *graph* points that are a little
// pulled in from the endpoints we want for
// the *axis*; then ROOT will give you a little
// bit of space around the *graph* points.
double XMIN = (xmax+11*xmin)/12;
double XMAX = (xmin+11*xmax)/12;
double YMIN = (ymax+11*ymin)/12;
double YMAX = (ymin+11*ymax)/12;
double xpo[4] = {XMIN, XMIN, XMAX, XMAX};
double ypo[4] = {YMIN, YMAX, YMIN, YMAX};
TGraph* gr23 = new TGraph(4, xpo, ypo);
gr23->SetTitle("");
gr23->SetFillColor(38+150);
XMIN = gr23->GetXaxis()->GetXmin();
XMAX = gr23->GetXaxis()->GetXmax();
YMIN = gr23->GetYaxis()->GetXmin();
YMAX = gr23->GetYaxis()->GetXmax();
gr23->GetXaxis()->SetTitle(xaxisTitle.c_str());
gr23->GetYaxis()->SetTitle(yaxisTitle.c_str());
gr23->Draw("alf");
TF1* multXBy1k = new TF1("multXBy1k", "x", xmin, xmax);
TF1* multYBy1k = new TF1("multYBy1k", "1000*x", ymin*ymult, ymax*ymult);
/* TGaxis* xaxis = new TGaxis(xmin,ymin,xmax,ymin,"multXBy1k",510,"+");
TGaxis* yaxis = new TGaxis(xmin,ymin,xmin,ymax,"multYBy1k",510,"-");
xaxis->SetTitle((xaxisTitle+ " #times 10^{3}").c_str());
xaxis->CenterTitle();
xaxis->SetTitleSize(0.06);
xaxis->SetLabelSize(0.06);
if (1000 == ymult) yaxis->SetTitle((yaxisTitle + " #times 10^{3}").c_str());
else yaxis->SetTitle(yaxisTitle.c_str());
yaxis->CenterTitle();
yaxis->SetTitleSize(0.06);
yaxis->SetLabelSize(0.06);
yaxis->SetTitleOffset(1.1);
xaxis->SetTitleOffset(0.9);
gr23->GetXaxis()->SetLabelColor(kWhite);
gr23->GetXaxis()->SetTitleColor(kWhite);
gr23->GetXaxis()->SetAxisColor(kWhite);
gr23->GetYaxis()->SetLabelColor(kWhite);
gr23->GetYaxis()->SetTitleColor(kWhite);
gr23->GetYaxis()->SetAxisColor(kWhite);
xaxis->Draw();
yaxis->Draw();*/
foo->SaveAs((directory + "/axisplot.png").c_str());
delete multXBy1k;
delete multYBy1k;
for (std::map<MixingResult*, DrawOptions*>::iterator i = drawmap.begin(); i != drawmap.end(); ++i) {
if ((*i).first == MixingResult::fitResult) continue;
drawResult((*i).first, (*i).second, foo);
}
if (drawmap[MixingResult::fitResult]) {
if (MixingResult::isSensitive[MixingResult::WYE]) {
if (MixingResult::isSensitive[MixingResult::EKS]) {
//drawEllipse3(drawmap[MixingResult::fitResult], foo);
// drawEllipseForce(drawmap[MixingResult::fitResult], foo);
//if(graphicsYIndex==4){drawEllipse3(drawmap[MixingResult::fitResult], foo); }
/*else*/ drawEllipseForce_Adam(drawmap[MixingResult::fitResult], foo);
//drawEllipse(drawmap[MixingResult::fitResult], foo);
}
else {
MixingResult::minuit->GetParameter(MixingResult::WYE, MixingResult::fitResult->measurement, MixingResult::fitResult->error);
drawYbar(MixingResult::fitResult, drawmap[MixingResult::fitResult], foo);
}
}
}
if (0 < extraXMultiplier) {
TF1* multXBy = new TF1("multXBy", "x", xmin, xmax);
TF1* multYBy1k = new TF1("multYBy1k", "1000*x", ymin*ymult, ymax*ymult);
TGaxis* xaxis = new TGaxis(xmin,ymin,xmax,ymin,"multXBy1k",510,"+");
TGaxis* yaxis = new TGaxis(xmin,ymin,xmin,ymax,"multYBy1k",510,"-");
}
foo->SaveAs((directory + "/finalplot.png").c_str());
foo->SaveAs((directory + "/finalplot.pdf").c_str());
delete foo;
}
void MixDrawer::drawAnnulus (MixingResult* dat, DrawOptions* dis, TCanvas* foo) {
assert(1 == dis->drawWith.size());
MixingResult* xres = dat;
MixingResult* yres = dis->drawWith[0];
switch (xres->myType) {
case MixingResult::XPRIME:
break;
case MixingResult::YPRIME:
xres = yres;
yres = dat;
break;
default:
std::cout << "Error: Cannot draw annulus with non-prime result " << dat->getName() << std::endl;
assert(false);
}
if (yres->myType != MixingResult::YPRIME) {
std::cout << "Error: Cannot draw annulus with non-prime result " << dat->getName() << std::endl;
assert(false);
}
// Use separate fitter to avoid side effects on main result.
static TMinuit* annuMin = 0;
if (0 == annuMin) {
annuMin = new TMinuit(MixingResult::nParams);
annuMin->DefineParameter(0, "special_x", 0.005, 0.0001, -0.10, 0.1);
annuMin->DefineParameter(1, "special_y", 0.005, 0.0001, -0.10, 0.1);
annuMin->DefineParameter(2, "special_dkpi", 0.800, 0.0100, -0.05, 6.5);
annuMin->DefineParameter(3, "special_dkpipi", 0.800, 0.0100, -0.05, 6.5);
annuMin->SetFCN(MixChisqFcn);
}
//annuMin->SetPrintLevel(-1);
char cmdbuf[1000];
int cmdres = 0;
int didx = -1;
switch (xres->myPrime) {
case MixingResult::KPI: didx = 3; break;
case MixingResult::KPIPI0: didx = 4; break; // NB, these are for use with MINUIT which is Fortran indexed.
default: break;
}
if (-1 == didx) {
std::cout << "Error: Cannot draw annulus with non-prime result " << xres->getName() << " " << xres->myPrime << std::endl;
assert(false);
}
annuMin->FixParameter(2);
annuMin->FixParameter(3);
TMinuit* temp = MixingResult::minuit;
MixingResult::minuit = annuMin;
double anglestep = 0.001;
for (double delta = 0; delta < 6.281; delta += anglestep) {
// For this value of delta, what (x, y) are compatible with
// the measured x', y'?
sprintf(cmdbuf, "SET PAR %i %f", didx, delta);
annuMin->mncomd(cmdbuf, cmdres);
if (0 != cmdres) std::cout << "Problem with command, returned " << cmdres << std::endl;
TGraph* curr = getEllipse(2.3);
curr->SetFillColor(dis->colour);
curr->Draw("if");
}
if (DrawOptions::AnnuKidney == dis->drawType) {
double delta = 0;
double delerr = 0;
MixingResult::minuit->GetParameter(didx-1, delta, delerr); // Back to C indexing
sprintf(cmdbuf, "SET PAR %i %f", didx, delta);
annuMin->mncomd(cmdbuf, cmdres);
if (0 != cmdres) std::cout << "Problem with command, returned " << cmdres << std::endl;
annuMin->mnfree(didx);
TGraph* curr = getEllipse(2.3);
curr->SetFillColor(dis->colour+4);
curr->Draw("if");
}
MixingResult::minuit = temp;
}
/*
void MixDrawer::drawAnnulus (MixingResult* dat, DrawOptions* dis, TCanvas* foo) {
assert(1 == dis->drawWith.size());
MixingResult* xres = dat;
MixingResult* yres = dis->drawWith[0];
switch (xres->myType) {
case MixingResult::XPRIME:
break;
case MixingResult::YPRIME:
xres = yres;
yres = dat;
break;
default:
std::cout << "Error: Cannot draw annulus with non-prime result " << dat->getName() << std::endl;
assert(false);
}
if (yres->myType != MixingResult::YPRIME) {
std::cout << "Error: Cannot draw annulus with non-prime result " << dat->getName() << std::endl;
assert(false);
}
double bestx = xres->measurement;
double besty = yres->measurement;
if ((MixingResult::KPI == xres->myPrime) || (bestx < 0)) {
// Move result into physically allowed region
double sigmas = -1*(bestx / xres->error);
bestx = 0;
besty += sigmas * yres->error * yres->correlation(xres);
}
if (MixingResult::KPI == xres->myPrime) bestx = sqrt(bestx);
double fitpar[MixingResult::nParams];
double fiterr[MixingResult::nParams];
double chisq;
int npars = MixingResult::nParams;
fitpar[0] = bestx;
fitpar[1] = besty;
for (int i = 0; i < MixingResult::nParams; ++i) {
MixingResult::minuit->GetParameter(i, fitpar[i], fiterr[i]);
}
MixChisqFcn(npars, 0, chisq, fitpar, 4);
double minimumChisq = chisq;
double normDist = sqrt(pow(bestx, 2) + pow(besty, 2));
double maxLoDist = 1;
double minLoDist = 0;
double minHiDist = 1;
double maxHiDist = 1000;
fitpar[0] = 0;
fitpar[1] = 0;
MixChisqFcn(npars, 0, chisq, fitpar, 4);
const double tolerance = 0.01;
if (chisq - minimumChisq < 2.3) {
maxLoDist = 0;
}
else {
for (int i = 0; i < 1000; ++i) {
double currDist = minLoDist + 0.5*(maxLoDist - minLoDist);
fitpar[0] = bestx * currDist;
fitpar[1] = besty * currDist;
MixChisqFcn(npars, 0, chisq, fitpar, 4);
if (chisq - minimumChisq < 2.3) maxLoDist = currDist;
else minLoDist = currDist;
std::cout << "Annulus minimum [" << minLoDist << ", " << maxLoDist << "] " << (chisq - minimumChisq) << std::endl;
if (maxLoDist - minLoDist < tolerance) break;
}
}
for (int i = 0; i < 1000; ++i) {
double currDist = minHiDist + 0.5*(maxHiDist - minHiDist);
fitpar[0] = bestx * currDist;
fitpar[1] = besty * currDist;
MixChisqFcn(npars, 0, chisq, fitpar, 4);
if (chisq - minimumChisq < 2.3) minHiDist = currDist;
else maxHiDist = currDist;
std::cout << "Annulus maximum [" << minHiDist << ", " << maxHiDist << "] " << (chisq - minimumChisq) << std::endl;
if (maxHiDist - minHiDist < tolerance) break;
}
double minRadius = 0.5*(minLoDist + maxLoDist)*normDist;
double maxRadius = 0.5*(minHiDist + maxHiDist)*normDist;
std::cout << normDist << " " << bestx << " " << besty << " " << minRadius << " " << maxRadius << std::endl;
double step = (maxRadius - minRadius)*0.005;
for (double rad = maxRadius; rad >= minRadius; rad -= step) {
std::cout << "Ellipse radius " << rad << std::endl;
TEllipse* currEllipse = new TEllipse(0, 0, rad);
currEllipse->SetLineColor(dis->colour);
currEllipse->Draw();
}
}
*/
void MixDrawer::drawResult (MixingResult* dat, DrawOptions* dis, TCanvas* foo) {
std::vector<MixingResult*> actives;
for (MixingResult::ResultIterator i = MixingResult::begin(); i != MixingResult::end(); ++i) {
if((*i)->isActive()){ actives.push_back(*i);
std::cout<<"i->getName() = "<<(*i)->getName()<<std::endl;//ad 8/19/13
}
(*i)->setActive(false);
}
dat->setActive(true);
for (std::vector<MixingResult*>::iterator i = dis->drawWith.begin(); i != dis->drawWith.end(); ++i) {
(*i)->setActive(true);
}
switch (dis->drawType) {
case DrawOptions::Ellipse:
{
assert(dis->drawWith.size() > 0);
//std::pair<TGraph*, TGraph*> ells = drawEllipse(dis, foo);
std::vector<TGraph*> ells = drawEllipse3(dis, foo);
}
break;
case DrawOptions::Ybar:
drawYbar(dat, dis, foo);
break;
case DrawOptions::Xbar:
case DrawOptions::Band:
drawBand(dat, dis, foo);
break;
case DrawOptions::Annulus:
case DrawOptions::AnnuKidney:
drawAnnulus(dat, dis, foo);
break;
default:
case DrawOptions::Unknown:
std::cout << "Warning: Could not draw result " << dat->name << "; unknown drawing type." << std::endl;
break;
}
for (std::vector<MixingResult*>::iterator i = actives.begin(); i != actives.end(); ++i) {
(*i)->setActive(true);
}
}
void MixDrawer::findPoint (TGraph* ret, int idx, double angle, double errorDef, int par1, int par2) {
//we're finding a point at a certain angle here, but we don't know where R is, so we have to iterate to find it.
double fitpar[MixingResult::nParams];//first get the parameters
double fiterr[MixingResult::nParams];//next get the error
double chisq;//initialize the chi2
int npars = MixingResult::nParams;//get the number of parameters
for (int i = 0; i < MixingResult::nParams; ++i) {
MixingResult::minuit->GetParameter(i, fitpar[i], fiterr[i]);//get all the results
//std::cout<<"i = "<<i<<", fitpar["<<i<<"] = "<<fitpar[i]<<", fiterr["<<i<<"] = "<<fiterr[i]<<std::endl;
}
MixChisqFcn(npars, 0, chisq, fitpar, 4);//get the chi2. Derivative (0) isn't used in the FCN, so it doesn't matter. What is flag=4?
double initX = fitpar[par1];//this is the initial x position
double initY = fitpar[par2]; //this is the initial y position
double xincrement = cos(angle);//this is the cos of the angle we want
double yincrement = sin(angle); //sin of the angle we want
double minimum = 0;//minimum chi2
double chiAtMinimum = chisq; //chi2 at the minimum
double chiAtSolution = chisq; //chi2 at the current solution
// std::cout<<"initX = "<<initX<<", initY = "<<initY<<", xincrement = "<<xincrement<<", yincrement = "<<yincrement<<std::endl;//ad 8/22/13
double maximum = 0.001; //this is the radius that we want to test
double chiAtMaximum = chisq;
while (chiAtMaximum < chiAtSolution + errorDef) {//break out of the loop as soon as we exceed the chi2. The error def is then the delta chi2
maximum *= 2;
fitpar[0] = initX + maximum*xincrement;//x+(r cos(angle))
fitpar[1] = initY + maximum*yincrement;//y+(r sin(angle)
MixChisqFcn(npars, 0, chisq, fitpar, 4);//check the fit
chiAtMaximum = chisq;
}
//we have the maximum
//ret->SetPoint(idx, fitpar[0],fitpar[1]);
// zero in for higher tolerance
static const double tolerance = 0.0001; //what is this?
for (int i = 0; i < 1000; ++i) {//iterate 1000 times
double currDist = minimum + (maximum - minimum)*0.5; //take mean of max and min
fitpar[0] = initX + currDist*xincrement;//x= x_0 +currDist*cos(angle)
fitpar[1] = initY + currDist*yincrement;//y= y_0+ currDist*sin(angle)
MixChisqFcn(npars, 0, chisq, fitpar, 4);
if (chisq > chiAtSolution + errorDef) {//if we found a better delta chi2
maximum = currDist;
chiAtMaximum = chisq;
}
else {//otherwise update the minimum
minimum = currDist;
chiAtMinimum = chisq;
}
if (1 == idx) std::cout << "Scan: currDist = " << currDist << ", delta Chi2 = " << (chisq - chiAtSolution) << ",chi2 at solution = " << chiAtSolution << std::endl;
assert(chiAtMaximum > chiAtMinimum);
if (chiAtMaximum - chiAtMinimum < tolerance) break;
}
double dist = minimum + 0.5*(maximum-minimum);
std::cout << "Found point " << initX + dist*xincrement << ", " << initY + dist*yincrement << std::endl;
ret->SetPoint(idx, initX + dist*xincrement, initY + dist*yincrement);
}
bool intersect (TLine& one, TLine& two) {
// Here I have special-case knowledge that one is always horizontal.
if ((two.IsHorizontal()) && (one.GetY1() != two.GetY1())) return false;
// So now they cannot be parallel, they must meet somewhere.
double x1 = one.GetX1();
double x2 = one.GetX2();
double x3 = two.GetX1();
double x4 = two.GetX2();
double y1 = one.GetY1();
double y2 = one.GetY2();
double y3 = two.GetY1();
double y4 = two.GetY2();
double det = (x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4);
if (fabs(det) < 1e-12) return false; // Almost parallel
double xInter = (x1*y2 - y1*x2)*(x3 - x4) - (x1 - x2)*(x3*y4 - y3*x4);
double yInter = (x1*y2 - y1*x2)*(y3 - y4) - (y1 - y2)*(x3*y4 - y3*x4);
xInter /= det;
yInter /= det;
// Check that intersection occurs on line segment one.
if (xInter < x1) return false;
if (xInter > x2) return false;
if (yInter < y1) return false;
if (yInter > y2) return false;
// And segment two.
if (xInter < x3) return false;
if (xInter > x4) return false;
if (yInter < y3) return false;
if (yInter > y4) return false;
/*
std::cout << "Intersect of ("
<< x1 << ", " << y1 << ") to (" << x2 << ", " << y2 << ") with ("
<< x3 << ", " << y3 << ") to (" << x4 << ", " << y4 << ") at point ("
<< xInter << ", " << yInter << ")\n";
*/
return true;
}
bool isWithin (double xp, double yp, TGraph* poly, int npoints) {
// Returns true if the point (xp, yp) is within the polygon
// described by the first npoints points of poly.
if (npoints < 3) return false;
int intersects = 0;
TLine one(xp, yp, xp+100000, yp);
for (int i = 1; i < npoints; ++i) {
double p1, p2, p3, p4;
poly->GetPoint(i-1, p1, p2);
poly->GetPoint(i, p3, p4);
TLine two(p1, p2, p3, p4);
if (intersect(one, two)) intersects++;
}
return (1 == intersects % 2);
}
TGraph* MixDrawer::getEllipse (double errorDef) {
MixingResult::minuit->SetErrorDef(errorDef);
TGraph* ret = (TGraph*) MixingResult::minuit->Contour(pointsPerContour, graphicsXIndex, graphicsYIndex);
if (!ret) return 0;
std::cout<<"calling rolf's find point with errordef " << errorDef << std::endl;
if(graphicsXIndex==0){
if ((!ret) || (ret->GetN() < pointsPerContour)) {
double xp = 0;
double yp = 0;
// Discard doubly-wrapped points.
bool endOfLine = false;
for (int i = 1; i < ret->GetN(); ++i) {
if (endOfLine) {
ret->RemovePoint(i);
i--;
continue;
}
ret->GetPoint(i, xp, yp);
//std::cout << "Testing point " << i << " (" << xp << ", " << yp << ") ... ";
if (isWithin(xp, yp, ret, i-1)) {
//std::cout << " discarding\n";
ret->RemovePoint(i);
i--;
endOfLine = true;
}
//else std::cout << " keeping\n";
}
}
}
return ret;
}
std::pair<TGraph*, TGraph*> MixDrawer::drawEllipse (DrawOptions* dis, TCanvas* foo) {
std::pair<TGraph*, TGraph*> ret(0, 0);
/*
for (int i = 2; i < MixingResult::NUMSENSE; ++i) {
bool sensitive = false;
for (MixingResult::ResultIterator m = MixingResult::begin(); m != MixingResult::end(); ++m) {
if (!(*m)->isActive()) continue;
if (!(*m)->isSensitiveTo(i)) continue;
sensitive = true;
break;
}
if (!sensitive) MixingResult::minuit->FixParameter(i);
}
MixingResult::minuit->FixParameter(2);
MixingResult::minuit->FixParameter(3);
*/
MixingResult::initialised = false;
std::cout << "Drawing " << dis->numContours << " ellipses\n";
// In 2 dimensions, an error ellipse nominally
// providing 68.27% coverage [equivalent to
// 1 sigma in 1 dimension] has chisq=2.30
MixingResult::minuit->Migrad();
ret.second = getEllipse(6.18);
if (ret.second) ret.second->SetFillColor(dis->colour + 2);
ret.first = getEllipse(2.30);
if (ret.first) ret.first->SetFillColor(dis->colour);
if ((dis->numContours > 1) && (ret.second)) {
ret.second->Draw("if9");
//std::cout << "Outer ellipse drawn\n";
}
if (ret.first) {
ret.first->Draw("if9");
//std::cout << "Inner ellipse drawn\n";
}
MixingResult::minuit->SetErrorDef(1.00);
return ret;
}
double MixDrawer::runFit () {
static double fitpar[10];
static double fiterr[10];
static int npar = MixingResult::nParams;
MixingResult::initialised = false;
MixingResult::minuit->Migrad();
for (int p = 0; p < MixingResult::nParams; ++p) {
MixingResult::minuit->GetParameter(p, fitpar[p], fiterr[p]);
}
double chisq = 0;
MixChisqFcn(npar, 0, chisq, fitpar, 4);
return chisq;
}
void MixDrawer::drawEllipseForce (DrawOptions* dis, TCanvas* foo) {
double fitpar[MixingResult::nParams];
double orgpar[MixingResult::nParams];
double wrkpar[MixingResult::nParams];
double fiterr[MixingResult::nParams];
for (int p = 0; p < MixingResult::nParams; ++p) {
MixingResult::minuit->GetParameter(p, fitpar[p], fiterr[p]);
orgpar[p] = fitpar[p];
wrkpar[p] = fitpar[p];
}
double XMIN = (xmax+11*xmin)/12; // To get nice axis labels.
double XMAX = (xmin+11*xmax)/12;
double YMIN = (ymax+11*ymin)/12;
double YMAX = (ymin+11*ymax)/12;
TH2F* histogram = new TH2F("hist", "", 1000, XMIN, XMAX, 1000, YMIN, YMAX);
histogram->SetStats(false);
TH2F* edmhist = new TH2F("edmhist", "", 1000, XMIN, XMAX, 1000, YMIN, YMAX);
edmhist->SetStats(false);
TH1F edm1d("edm1d", "", 1000, 0, 0.00001);
char strbuf[100];
int dummy = MixingResult::nParams;
double chisq = 0;
double maxEdm = 0;
MixChisqFcn(dummy, 0, chisq, fitpar, 4);
double centralChisq = chisq;
MixingResult::minuit->FixParameter(0);
MixingResult::minuit->FixParameter(1);
MixingResult::minuit->SetErrorDef(1);
MixingResult::minuit->SetPrintLevel(-1);
for (int i = 1; i <= 1000; ++i) {
//for (int i = 100; i <= 400; ++i) {
double prevChisq = 1000;
double xval = xmin + (i + 0.5)*(xmax - xmin)*0.001;
sprintf(strbuf, "SET PAR %i %f", graphicsXIndex+1, xval);
MixingResult::minuit->mncomd(strbuf, dummy);
assert(0 == dummy);
if (0 == i%25) std::cout << "Fitting line " << i << std::endl;
//for (int j = 200; j <= 1000; ++j) {
for (int j = 2; j <= 1000; ++j) {
double yval = ymin + (j + 0.5)*(ymax - ymin)*0.001;
sprintf(strbuf, "SET PAR %i %f", graphicsYIndex+1, yval);
MixingResult::minuit->mncomd(strbuf, dummy);
assert(0 == dummy);
chisq = runFit() - centralChisq;
if (chisq > fiveSigma) {
//std::cout << "Bad fit for (" << i << ", " << j << ") " << chisq << ", retrying with last working set.\n";
for (int p = 3; p <= 8; ++p) {
sprintf(strbuf, "SET PAR %i %f", p, wrkpar[p-1]);
MixingResult::minuit->mncomd(strbuf, dummy);
assert(0 == dummy);
}
chisq = runFit() - centralChisq;
if (chisq > 2*fiveSigma) {
//std::cout << " Still bad, " << chisq << ", retrying with central values.\n";
for (int p = 3; p <= 8; ++p) {
sprintf(strbuf, "SET PAR %i %f", p, orgpar[p-1]);
MixingResult::minuit->mncomd(strbuf, dummy);
assert(0 == dummy);
}
chisq = runFit() - centralChisq;
if (chisq > 3*fiveSigma) {
//std::cout << " Final fit is " << chisq << ", giving up.\n";
chisq = 0; // Give up
}
else for (int p = 0; p < MixingResult::nParams; ++p) MixingResult::minuit->GetParameter(p, wrkpar[p], fiterr[p]);
}
else for (int p = 0; p < MixingResult::nParams; ++p) MixingResult::minuit->GetParameter(p, wrkpar[p], fiterr[p]);
}
else for (int p = 0; p < MixingResult::nParams; ++p) MixingResult::minuit->GetParameter(p, wrkpar[p], fiterr[p]);
double fmin, fedm, errdef;
int nparx, istat;
MixingResult::minuit->mnstat(fmin, fedm, errdef, dummy, nparx, istat);
if (3 > istat) chisq = 0;
//if (fedm > 0.001) chisq = 0;
//if (chisq >= 1.5*prevChisq) chisq = 0;
if (chisq > 0) prevChisq = chisq;
histogram->SetBinContent(i, j, chisq);
edmhist->SetBinContent(i, j, fedm);
edm1d.Fill(fedm);
maxEdm = (fedm > maxEdm ? fedm : maxEdm);
//break;
}
//break;
}
/*
for (int i = 2; i < 1000; ++i) {
for (int j = 2; j < 1000; ++j) {
if (0 < histogram->GetBinContent(i, j)) continue;
double avg = 0;
//avg += histogram->GetBinContent(i+0, j+1);
//avg += histogram->GetBinContent(i+0, j-1);
avg += histogram->GetBinContent(i+1, j+0);
avg += histogram->GetBinContent(i-1, j+0);
avg *= 0.5;
histogram->SetBinContent(i, j, avg);
}
}
*/
for (int i = 2; i < 1000; ++i) {
for (int j = 2; j < 1000; ++j) {
double curr = histogram->GetBinContent(i, j);
if (0 == curr) continue;
// Values chosen for five-colour plot.
if (curr < oneSigma) curr = 10;
//else if (curr < twoSigma) curr = 7;
else if (curr < twoSigma) curr = 21;
//else if (curr < threeSigma) curr = 15;
else if (curr < threeSigma) curr = 31;
//else if (curr < fourSigma) curr = 20;
else if (curr < fourSigma) curr = 41;
//else if (curr < fiveSigma) curr = fiveSigma-1;
else if (curr < fiveSigma) curr = 51;
else curr = 0;
histogram->SetBinContent(i, j, curr);
}
}
// Search for bad fits no found by earlier methods. For each bin, is it an outlier in its region?
/*
for (int i = 2; i < 1000; ++i) {
for (int j = 2; j < 1000; ++j) {
double curr = histogram->GetBinContent(i, j);
if (0 == curr) continue;
std::map<double, int> surrounds;
for (int xp = -1; xp <= 1; ++xp) {
for (int yp = -1; yp <= 1; ++yp) {
if ((0 == xp) && (0 == yp)) continue;
double area = histogram->GetBinContent(i+xp, j+yp);
if (0 == area) continue;
surrounds[area]++;
}
}
if (0 < surrounds[curr]) continue;
histogram->SetBinContent(i, j, 0);
}
}*/
// Fix areas of bad fits. Take modal value of surrounding bins.
for (int i = 2; i < 1000; ++i) {
for (int j = 2; j < 1000; ++j) {
if (0 < histogram->GetBinContent(i, j)) continue;
// Right edge detection
int numNonzero = 0;
for (int xp = 1; xp <= 10; ++xp) {
if (0 == histogram->GetBinContent(i+xp, j)) continue;
numNonzero++;
}
if (0 == numNonzero) continue; // We're at a right edge.
std::map<double, int> surrounds;
for (int xp = -1; xp <= 1; ++xp) {
for (int yp = -1; yp <= 1; ++yp) {
if ((0 == xp) && (0 == yp)) continue;
double area = histogram->GetBinContent(i+xp, j+yp);
if (0 == area) continue;
surrounds[area]++;
}
}
std::map<double, int>::iterator best = surrounds.begin();
for (std::map<double, int>::iterator c = surrounds.begin(); c != surrounds.end(); ++c) {
if ((*c).second < (*best).second) continue;
best = c;
}
histogram->SetBinContent(i, j, (*best).first);
}
}
edmhist->GetZaxis()->SetRangeUser(0, 0.00001);
edmhist->Draw("colz");
foo->SaveAs("edms.png");
edm1d.Draw();
foo->SaveAs("edm1d.png");
int colors_new[5];
colors_new[0] = kBlue;
colors_new[1] = kCyan-7;
colors_new[2] = 8;
colors_new[3] = 42;
colors_new[4] = 46;
TColor::SetPalette(5, colors_new);
histogram->Draw("col");
}
void MixDrawer::drawEllipseForce_Adam (DrawOptions* dis, TCanvas* foo) {
int nbins(100);//changeable number of bins
double fitpar[MixingResult::nParams];
double orgpar[MixingResult::nParams];
double wrkpar[MixingResult::nParams];
double fiterr[MixingResult::nParams];
for (int p = 0; p < MixingResult::nParams; ++p) {//Get all the parameters
MixingResult::minuit->GetParameter(p, fitpar[p], fiterr[p]);
orgpar[p] = fitpar[p];
wrkpar[p] = fitpar[p];
}
TGraph* the_central_val = new TGraph(1);
std::cout<<"set central val graph to ("<<xaxisTitle<<","<<yaxisTitle <<") = ("<<orgpar[graphicsXIndex]<<","<<orgpar[graphicsYIndex]<<")"<<std::endl;
the_central_val->SetPoint(0,orgpar[graphicsXIndex],orgpar[graphicsYIndex]);
the_central_val->SetMarkerStyle(kFullDotLarge);
the_central_val->SetMarkerColor(kMagenta);
/*
double XMIN = (xmax+11*xmin)/12; // Nice axis endpoints.
double XMAX = (xmin+11*xmax)/12;
double YMIN = (ymax+11*ymin)/12;
double YMAX = (ymin+11*ymax)/12;
*/
double XMIN = xmin-(xmin/(double)nbins*0.5);//shift by half a bin to make the bin centers correct
double XMAX = xmax+(xmax/(double)nbins*0.5);
double YMIN = ymin-(ymin/(double)nbins*0.5);
double YMAX = ymax+(ymax/(double)nbins*0.5);
TH2F* histogram = new TH2F("hist", "", nbins, XMIN, XMAX, nbins, YMIN, YMAX);
//this is the final histogram we want to draw, so that'll be ok.
histogram->SetStats(false);
TH2F* edmhist = new TH2F("edmhist", "", nbins, XMIN, XMAX, nbins, YMIN, YMAX); //this is a graphical view of the estimated distance to minimum
edmhist->SetStats(false);
TH1F edm1d("edm1d", "", nbins, 0, 0.00001); //This is the projection
char strbuf[100];
int dummy = MixingResult::nParams;
double chisq = 0;
double maxEdm = 0;
MixChisqFcn(dummy, 0, chisq, fitpar, 4);
double centralChisq = chisq;
std::cout<<"centralChisq="<<centralChisq<<std::endl;
MixingResult::minuit->FixParameter(graphicsXIndex);
MixingResult::minuit->FixParameter(graphicsYIndex);
MixingResult::minuit->SetErrorDef(1); //Reset Error Definition
MixingResult::minuit->SetPrintLevel(-1); //let's not spit out all the shit.
for (int i = 2; i < nbins; ++i) {
//for (int i = 100; i <= 400; ++i) {
double prevChisq = 1000;
double xval = xmin + (i + 0.5)*(xmax - xmin)/((double) nbins);//this is the grid position in x
sprintf(strbuf, "SET PAR %i %f", graphicsXIndex+1, xval);
MixingResult::minuit->mncomd(strbuf, dummy);
assert(0 == dummy);
if (0 == i%25) std::cout << "Fitting line " << i << std::endl;
//for (int j = 200; j <= 1000; ++j) {
for (int j = 2; j < nbins; ++j) {
double yval = ymin + (j + 0.5)*(ymax - ymin)/((double) nbins);//this is the grid position in y
sprintf(strbuf, "SET PAR %i %f", graphicsYIndex+1, yval);
MixingResult::minuit->mncomd(strbuf, dummy);
assert(0 == dummy);
chisq = runFit() - centralChisq;
//std::cout << "Found chisq " << chisq << " " << centralChisq << std::endl;
if (chisq > fiveSigma) {
//std::cout << "Bad fit for (" << i << ", " << j << ") " << chisq << ", retrying with last working set.\n";
for (int p = 1; p <= 8; ++p) {
if (p == graphicsXIndex+1) continue;
if (p == graphicsYIndex+1) continue;