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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
|
/***********************************
Table Of Contents
0. Track Split Plot
1. Misalignment Dependence
2. Make Plots
3. Axis Label
4. Axis Limits
5. Place Legend
***********************************/
using namespace std;
#include "trackSplitPlot.h"
#include "Alignment/OfflineValidation/interface/TkAlStyle.h"
//===================
//0. Track Split Plot
//===================
TCanvas *trackSplitPlot(Int_t nFiles,
TString *files,
TString *names,
TString xvar,
TString yvar,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas,
ostream &summaryfile) {
if (TkAlStyle::status() == NO_STATUS)
TkAlStyle::set(INTERNAL);
TString legendOptions = TkAlStyle::legendoptions;
legendOptions.ReplaceAll("all", "meanerror,rmserror").ToLower();
if (outliercut < 0)
outliercut = -1;
gStyle->SetMarkerSize(1.5);
setupcolors();
stufftodelete->SetOwner(true);
cout << xvar << " " << yvar << endl;
if (xvar == "" && yvar == "")
return nullptr;
PlotType type;
if (xvar == "")
type = Histogram;
else if (yvar == "")
type = OrgHistogram;
else if (resolution)
type = Resolution;
else if (nFiles < 1)
type = ScatterPlot;
else
type = Profile;
if (nFiles < 1)
nFiles = 1;
const Int_t n = nFiles;
vector<TH1 *> p;
Int_t lengths[n];
stringstream sx, sy, srel, ssigma1, ssigma2, ssigmaorg;
sx << xvar << "_org";
TString xvariable = sx.str();
TString xvariable2 = "";
if (xvar == "runNumber")
xvariable = "runNumber";
if (xvar.BeginsWith("nHits")) {
xvariable = xvar;
xvariable2 = xvar;
xvariable.Append("1_spl");
xvariable2.Append("2_spl");
}
sy << "Delta_" << yvar;
TString yvariable = sy.str();
TString relvariable = "1";
if (relative) {
srel << yvar << "_org";
relvariable = srel.str();
}
TString sigma1variable = "", sigma2variable = "";
if (pull) {
ssigma1 << yvar << "1Err_spl";
ssigma2 << yvar << "2Err_spl";
}
sigma1variable = ssigma1.str();
sigma2variable = ssigma2.str();
TString sigmaorgvariable = "";
if (pull && relative)
ssigmaorg << yvar << "Err_org";
sigmaorgvariable = ssigmaorg.str();
Double_t xmin = -1, xmax = 1, ymin = -1, ymax = 1, xbins = -1, ybins;
if (type == Profile || type == ScatterPlot || type == OrgHistogram || type == Resolution)
axislimits(nFiles, files, xvar, 'x', relative, pull, xmin, xmax, xbins);
if (type == Profile || type == ScatterPlot || type == Histogram || type == Resolution)
axislimits(nFiles, files, yvar, 'y', relative, pull, ymin, ymax, ybins);
std::vector<TString> meansrmss(n);
std::vector<double> means(n);
std::vector<double> rmss(n);
//a file is not "used" if it's MC data and the x variable is run number, or if the filename is blank
std::vector<bool> used(n);
for (Int_t i = 0; i < n; i++) {
stringstream sid;
sid << "p" << i;
TString id = sid.str();
//for a profile or resolution, it fills a histogram, q[j], for each bin, then gets the mean and width from there.
vector<TH1F *> q;
if (type == ScatterPlot)
p.push_back(new TH2F(id, "", xbins, xmin, xmax, ybins, ymin, ymax));
if (type == Histogram)
p.push_back(new TH1F(id, "", ybins, ymin, ymax));
if (type == OrgHistogram)
p.push_back(new TH1F(id, "", xbins, xmin, xmax));
if (type == Resolution || type == Profile) {
p.push_back(new TH1F(id, "", xbins, xmin, xmax));
for (Int_t j = 0; j < xbins; j++) {
stringstream sid2;
sid2 << "q" << i << j;
TString id2 = sid2.str();
q.push_back(new TH1F(id2, "", 1000, ymin * 10, ymax * 10));
}
}
p[i]->SetLineColor(colors[i]);
if (type == Resolution || type == Profile) {
p[i]->SetMarkerStyle(styles[i] / 100);
p[i]->SetMarkerColor(colors[i]);
p[i]->SetLineStyle(styles[i] % 100);
} else {
if (styles[i] >= 100) {
p[i]->SetMarkerStyle(styles[i] / 100);
p[i]->SetMarkerColor(colors[i]);
p[i]->Sumw2();
}
p[i]->SetLineStyle(styles[i] % 100);
}
stufftodelete->Add(p[i]);
p[i]->SetBit(kCanDelete, true);
used[i] = true;
//if it's MC data (run 1), the run number is meaningless
if ((xvar == "runNumber" && findMax(files[i], "runNumber", 'x') < 2) || files[i] == "") {
used[i] = false;
p[i]->SetLineColor(kWhite);
p[i]->SetMarkerColor(kWhite);
for (unsigned int j = 0; j < q.size(); j++)
delete q[j];
continue;
}
TFile *f = TFile::Open(files[i]);
TTree *tree = (TTree *)f->Get("cosmicValidation/splitterTree");
if (tree == nullptr)
tree = (TTree *)f->Get("splitterTree");
lengths[i] = tree->GetEntries();
Double_t x = 0, y = 0, rel = 1, sigma1 = 1;
Double_t sigma2 = 1; //if !pull, we want to divide by sqrt(2) because we want the error from 1 track
Double_t sigmaorg = 0;
Int_t xint = 0, xint2 = 0;
Int_t runNumber = 0;
double pt1 = 0, maxpt1 = 0;
if (!relative && !pull && (yvar == "dz" || yvar == "dxy"))
rel = 1e-4; //it's in cm but we want it in um, so divide by 1e-4
if (!relative && !pull && (yvar == "phi" || yvar == "theta" || yvar == "qoverpt"))
rel = 1e-3; //make the axis labels manageable
tree->SetBranchAddress("runNumber", &runNumber);
if (type == Profile || type == ScatterPlot || type == Resolution || type == OrgHistogram) {
if (xvar == "runNumber")
tree->SetBranchAddress(xvariable, &xint);
else if (xvar.BeginsWith("nHits")) {
tree->SetBranchAddress(xvariable, &xint);
tree->SetBranchAddress(xvariable2, &xint2);
} else
tree->SetBranchAddress(xvariable, &x);
}
if (type == Profile || type == ScatterPlot || type == Resolution || type == Histogram) {
int branchexists = tree->SetBranchAddress(yvariable, &y);
if (branchexists == -5) //i.e. it doesn't exist
{
yvariable.ReplaceAll("Delta_", "d");
yvariable.Append("_spl");
tree->SetBranchAddress(yvariable, &y);
}
}
if (relative && xvar != yvar) //if xvar == yvar, setting the branch here will undo setting it to x 2 lines earlier
tree->SetBranchAddress(relvariable, &rel); //setting the value of rel is then taken care of later: rel = x
if (pull) {
tree->SetBranchAddress(sigma1variable, &sigma1);
tree->SetBranchAddress(sigma2variable, &sigma2);
}
if (relative && pull)
tree->SetBranchAddress(sigmaorgvariable, &sigmaorg);
if (xvar == "pt" || yvar == "pt" || xvar == "qoverpt" || yvar == "qoverpt") {
tree->SetBranchAddress("pt1_spl", &pt1);
} else {
maxpt1 = 999;
}
Int_t notincluded = 0; //this counts the number that aren't in the right run range.
//it's subtracted from lengths[i] in order to normalize the histograms
for (Int_t j = 0; j < lengths[i]; j++) {
tree->GetEntry(j);
if (xvar == "runNumber" || xvar.BeginsWith("nHits"))
x = xint;
if (xvar == "runNumber")
runNumber = x;
if (yvar == "phi" && y >= pi)
y -= 2 * pi;
if (yvar == "phi" && y <= -pi)
y += 2 * pi;
if ((runNumber < minrun && runNumber > 1) ||
(runNumber > maxrun && maxrun > 0)) //minrun and maxrun are global variables.
{
notincluded++;
continue;
}
if (relative && xvar == yvar)
rel = x;
Double_t error = 0;
if (relative && pull)
error = sqrt((sigma1 / rel) * (sigma1 / rel) + (sigma2 / rel) * (sigma2 / rel) +
(sigmaorg * y / (rel * rel)) * (sigmaorg * x / (rel * rel)));
else
error = sqrt(sigma1 * sigma1 +
sigma2 * sigma2); // = sqrt(2) if !pull; this divides by sqrt(2) to get the error in 1 track
y /= (rel * error);
if (pt1 > maxpt1)
maxpt1 = pt1;
if (ymin <= y && y < ymax && xmin <= x && x < xmax) {
if (type == Histogram)
p[i]->Fill(y);
if (type == ScatterPlot)
p[i]->Fill(x, y);
if (type == Resolution || type == Profile) {
int which = (p[i]->Fill(x, 0)) - 1;
//get which q[j] by filling p[i] with nothing. (TH1F::Fill returns the bin number)
//p[i]'s actual contents are set later.
if (which >= 0 && (unsigned)which < q.size())
q[which]->Fill(y);
}
if (type == OrgHistogram)
p[i]->Fill(x);
}
if (xvar.BeginsWith("nHits")) {
x = xint2;
if (ymin <= y && y < ymax && xmin <= x && x < xmax) {
if (type == Histogram)
p[i]->Fill(y);
if (type == ScatterPlot)
p[i]->Fill(x, y);
if (type == Resolution || type == Profile) {
int which = (p[i]->Fill(x, 0)) - 1;
if (which >= 0)
q[which]->Fill(y); //get which q[j] by filling p[i] (with nothing), which returns the bin number
}
if (type == OrgHistogram)
p[i]->Fill(x);
}
}
if (lengths[i] < 10 ? true
: (((j + 1) / (int)(pow(10, (int)(log10(lengths[i])) - 1))) *
(int)(pow(10, (int)(log10(lengths[i])) - 1)) ==
j + 1 ||
j + 1 == lengths[i]))
//print when j+1 is a multiple of 10^x, where 10^x has 1 less digit than lengths[i]
// and when it's finished
//For example, if lengths[i] = 123456, it will print this when j+1 = 10000, 20000, ..., 120000, 123456
//So it will print between 10 and 100 times: 10 when lengths[i] = 10^x and 100 when lengths[i] = 10^x - 1
{
cout << j + 1 << "/" << lengths[i] << ": ";
if (type == Profile || type == ScatterPlot || type == Resolution)
cout << x << ", " << y << endl;
if (type == OrgHistogram)
cout << x << endl;
if (type == Histogram)
cout << y << endl;
}
}
lengths[i] -= notincluded;
if (maxpt1 < 6) { //0T
used[i] = false;
p[i]->SetLineColor(kWhite);
p[i]->SetMarkerColor(kWhite);
for (unsigned int j = 0; j < q.size(); j++)
delete q[j];
continue;
}
meansrmss[i] = "";
if (type == Histogram || type == OrgHistogram) {
stringstream meanrms;
meanrms.precision(3);
double average = -1e99;
double rms = -1e99;
TString var = (type == Histogram ? yvar : xvar);
char axis = (type == Histogram ? 'y' : 'x');
TString varunits = "";
if (!relative && !pull)
varunits = units(var, axis);
if (legendOptions.Contains("mean")) {
if (outliercut < 0)
average = p[i]->GetMean();
else
average = findAverage(files[i], var, axis, relative, pull);
cout << "Average = " << average;
meanrms << "#mu = " << average;
means[i] = average;
if (legendOptions.Contains("meanerror")) {
if (outliercut < 0)
rms = p[i]->GetRMS();
else
rms = findRMS(files[i], var, axis, relative, pull);
meanrms << " #pm " << rms / TMath::Sqrt(lengths[i] * abs(outliercut));
cout << " +/- " << rms / TMath::Sqrt(lengths[i] * abs(outliercut));
}
if (varunits != "") {
meanrms << " " << varunits;
cout << " " << varunits;
}
cout << endl;
if (legendOptions.Contains("rms"))
meanrms << ", ";
}
if (legendOptions.Contains("rms")) {
if (rms < -1e98) {
if (outliercut < 0)
rms = p[i]->GetRMS();
else
rms = findRMS(files[i], var, axis, relative, pull);
}
cout << "RMS = " << rms;
meanrms << "rms = " << rms;
rmss[i] = rms;
if (legendOptions.Contains("rmserror")) {
//https://root.cern.ch/root/html/src/TH1.cxx.html#7076
meanrms << " #pm " << rms / TMath::Sqrt(2 * lengths[i] * abs(outliercut));
cout << " +/- " << rms / TMath::Sqrt(2 * lengths[i] * abs(outliercut));
}
if (varunits != "") {
meanrms << " " << varunits;
cout << " " << varunits;
}
cout << endl;
}
meansrmss[i] = meanrms.str();
}
if (type == Resolution) {
for (Int_t j = 0; j < xbins; j++) {
p[i]->SetBinContent(j + 1, q[j]->GetRMS());
p[i]->SetBinError(j + 1, q[j]->GetRMSError());
delete q[j];
}
}
if (type == Profile) {
for (Int_t j = 0; j < xbins; j++) {
p[i]->SetBinContent(j + 1, q[j]->GetMean());
p[i]->SetBinError(j + 1, q[j]->GetMeanError());
delete q[j];
}
}
setAxisLabels(p[i], type, xvar, yvar, relative, pull);
}
if (type == Histogram && !pull && any_of(begin(used), end(used), ::identity<bool>)) {
if (legendOptions.Contains("mean")) {
summaryfile << " mu_Delta" << yvar;
if (relative)
summaryfile << "/" << yvar;
if (pull)
summaryfile << "_pull";
if (!pull && !relative && plainunits(yvar, 'y') != "")
summaryfile << " (" << plainunits(yvar, 'y') << ")";
summaryfile << "\t"
<< "latexname=$\\mu_{" << latexlabel(yvar, 'y', relative, resolution, pull) << "}$";
if (!pull && !relative && plainunits(yvar, 'y') != "")
summaryfile << " (" << latexunits(yvar, 'y') << ")";
summaryfile << "\t"
<< "format={:.3g}\t"
<< "latexformat=${:.3g}$";
for (int i = 0; i < n; i++) {
if (used[i]) {
summaryfile << "\t" << means[i];
} else {
summaryfile << "\t" << nan("");
}
}
summaryfile << "\n";
}
if (legendOptions.Contains("rms")) {
summaryfile << "sigma_Delta" << yvar;
if (relative)
summaryfile << "/" << yvar;
if (pull)
summaryfile << "_pull";
if (!pull && !relative && plainunits(yvar, 'y') != "")
summaryfile << " (" << plainunits(yvar, 'y') << ")";
summaryfile << "\t"
<< "latexname=$\\sigma_{" << latexlabel(yvar, 'y', relative, resolution, pull) << "}$";
if (!pull && !relative && latexunits(yvar, 'y') != "")
summaryfile << " (" << latexunits(yvar, 'y') << ")";
summaryfile << "\t"
<< "format={:.3g}\t"
<< "latexformat=${:.3g}$";
for (int i = 0; i < n; i++) {
if (used[i]) {
summaryfile << "\t" << rmss[i];
} else {
summaryfile << "\t" << nan("");
}
}
summaryfile << "\n";
}
}
TH1 *firstp = nullptr;
for (int i = 0; i < n; i++) {
if (used[i]) {
firstp = p[i];
break;
}
}
if (firstp == nullptr) {
stufftodelete->Clear();
return nullptr;
}
TCanvas *c1 = TCanvas::MakeDefCanvas();
TH1 *maxp = firstp;
if (type == ScatterPlot)
firstp->Draw("COLZ");
else if (type == Resolution || type == Profile) {
vector<TGraphErrors *> g;
TMultiGraph *list = new TMultiGraph();
for (Int_t i = 0, ii = 0; i < n; i++, ii++) {
if (!used[i]) {
ii--;
continue;
}
g.push_back(new TGraphErrors(p[i]));
for (Int_t j = 0; j < g[ii]->GetN(); j++) {
if (g[ii]->GetY()[j] == 0 && g[ii]->GetEY()[j] == 0) {
g[ii]->RemovePoint(j);
j--;
}
}
list->Add(g[ii]);
}
list->Draw("AP");
Double_t yaxismax = list->GetYaxis()->GetXmax();
Double_t yaxismin = list->GetYaxis()->GetXmin();
delete list; //automatically deletes g[i]
if (yaxismin > 0) {
yaxismax += yaxismin;
yaxismin = 0;
}
firstp->GetYaxis()->SetRangeUser(yaxismin, yaxismax);
if (xvar == "runNumber")
firstp->GetXaxis()->SetNdivisions(505);
} else if (type == Histogram || type == OrgHistogram) {
Bool_t allthesame = true;
for (Int_t i = 1; i < n && allthesame; i++) {
if (lengths[i] != lengths[0])
allthesame = false;
}
if (!allthesame && xvar != "runNumber")
for (Int_t i = 0; i < n; i++) {
//This does NOT include events that are out of the run number range (minrun and maxrun).
//It DOES include events that are out of the histogram range.
p[i]->Scale(1.0 / lengths[i]);
}
maxp = (TH1F *)firstp->Clone("maxp");
stufftodelete->Add(maxp);
maxp->SetBit(kCanDelete, true);
maxp->SetLineColor(kWhite);
for (Int_t i = 1; i <= maxp->GetNbinsX(); i++) {
for (Int_t j = 0; j < n; j++) {
if (!used[j])
continue;
maxp->SetBinContent(i, TMath::Max(maxp->GetBinContent(i), p[j]->GetBinContent(i)));
}
}
maxp->SetMarkerStyle(0);
maxp->SetMinimum(0);
maxp->Draw("");
if (xvar == "runNumber") {
maxp->GetXaxis()->SetNdivisions(505);
maxp->Draw("");
}
}
int nEntries = 0;
for (int i = 0; i < n; i++)
if (used[i])
nEntries++;
double width = 0.5;
if (type == Histogram || type == OrgHistogram)
width *= 2;
TLegend *legend = TkAlStyle::legend(nEntries, width);
legend->SetTextSize(0);
if (type == Histogram || type == OrgHistogram)
legend->SetNColumns(2);
stufftodelete->Add(legend);
legend->SetBit(kCanDelete, true);
for (Int_t i = 0; i < n; i++) {
if (!used[i])
continue;
if (type == Resolution || type == Profile) {
if (p[i] == firstp)
p[i]->Draw("P");
else
p[i]->Draw("same P");
legend->AddEntry(p[i], names[i], "pl");
} else if (type == Histogram || type == OrgHistogram) {
if (styles[i] >= 100) {
p[i]->Draw("same P0E");
legend->AddEntry(p[i], names[i], "pl");
} else {
p[i]->Draw("same hist");
legend->AddEntry(p[i], names[i], "l");
}
legend->AddEntry((TObject *)nullptr, meansrmss[i], "");
}
}
if (legend->GetListOfPrimitives()->At(0) == nullptr) {
stufftodelete->Clear();
deleteCanvas(c1);
return nullptr;
}
c1->Update();
legend->Draw();
double legendfraction =
legend->GetY2() -
legend->GetY1(); //apparently GetY1 and GetY2 give NDC coordinates. This is not a mistake on my part
double padheight = gPad->GetUymax() - gPad->GetUymin();
//legendfraction = legendheight / padheight = newlegendheight / newpadheight
//newpadheight = padheight + x
//newlegendheight = newpadheight - padheight = x so it doesn't cover anything
//==>legendfraction = x/(padheight+x)
/* ==> */ double x = padheight * legendfraction / (1 - legendfraction) * 1.5; //1.5 to give extra room
maxp->GetYaxis()->SetRangeUser(gPad->GetUymin(), gPad->GetUymax() + x);
TkAlStyle::drawStandardTitle();
c1->Update();
if (saveas != "")
saveplot(c1, saveas);
return c1;
}
//make a 1D histogram of Delta_yvar
TCanvas *trackSplitPlot(Int_t nFiles,
TString *files,
TString *names,
TString var,
Bool_t relative,
Bool_t pull,
TString saveas,
ostream &summaryfile) {
return trackSplitPlot(nFiles, files, names, "", var, relative, false, pull, saveas, summaryfile);
}
//For 1 file
TCanvas *trackSplitPlot(TString file,
TString xvar,
TString yvar,
Bool_t profile,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas,
ostream &summaryfile) {
Int_t nFiles = 0;
if (profile) //it interprets nFiles < 1 as 1 file, make a scatterplot
nFiles = 1;
TString *files = &file;
TString name = "";
TString *names = &name;
return trackSplitPlot(nFiles, files, names, xvar, yvar, relative, resolution, pull, saveas, summaryfile);
}
//make a 1D histogram of Delta_yvar
TCanvas *trackSplitPlot(TString file, TString var, Bool_t relative, Bool_t pull, TString saveas, ostream &summaryfile) {
Int_t nFiles = 1;
TString *files = &file;
TString name = "";
TString *names = &name;
return trackSplitPlot(nFiles, files, names, var, relative, pull, saveas, summaryfile);
}
void saveplot(TCanvas *c1, TString saveas) {
if (saveas == "")
return;
TString saveas2 = saveas, saveas3 = saveas;
saveas2.ReplaceAll(".pngepsroot", "");
saveas3.Remove(saveas3.Length() - 11);
if (saveas2 == saveas3) {
c1->SaveAs(saveas.ReplaceAll(".pngepsroot", ".png"));
c1->SaveAs(saveas.ReplaceAll(".png", ".eps"));
c1->SaveAs(saveas.ReplaceAll(".eps", ".root"));
c1->SaveAs(saveas.ReplaceAll(".root", ".pdf"));
} else {
c1->SaveAs(saveas);
}
}
void deleteCanvas(TObject *canvas) {
if (canvas == nullptr)
return;
if (!canvas->InheritsFrom("TCanvas")) {
delete canvas;
return;
}
TCanvas *c1 = (TCanvas *)canvas;
delete c1;
}
void setupcolors() {
if (colorsset)
return;
colorsset = true;
colors.clear();
styles.clear();
Color_t array[15] = {
1, 2, 3, 4, 6, 7, 8, 9, kYellow + 3, kOrange + 10, kPink - 2, kTeal + 9, kAzure - 8, kViolet - 6, kSpring - 1};
for (int i = 0; i < 15; i++) {
colors.push_back(array[i]);
styles.push_back(1); //Set the default to 1
//This is to be consistent with the other validation
}
}
//This makes a plot, of Delta_yvar vs. runNumber, zoomed in to between firstrun and lastrun.
//Each bin contains 1 run.
//Before interpreting the results, make sure to look at the histogram of run number (using yvar = "")
//There might be bins with very few events => big error bars,
//or just 1 event => no error bar
void runNumberZoomed(Int_t nFiles,
TString *files,
TString *names,
TString yvar,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
Int_t firstRun,
Int_t lastRun,
TString saveas) {
Int_t tempminrun = minrun;
Int_t tempmaxrun = maxrun;
minrun = firstRun;
maxrun = lastRun;
trackSplitPlot(nFiles, files, names, "runNumber", yvar, relative, resolution, pull, saveas);
minrun = tempminrun;
maxrun = tempmaxrun;
}
//==========================
//1. Misalignment Dependence
//==========================
//This can do three different things:
// (1) if xvar == "", it will plot the mean (if !resolution) or width (if resolution) of Delta_yvar as a function
// of the misalignment values, as given in values. misalignment (e.g. sagitta, elliptical) will be used as the
// x axis label.
// (2) if xvar != "", it will fit the profile/resolution to a function. If parameter > 0, it will plot the parameter given by parameter as
// a function of the misalignment. parametername is used as the y axis label. You can put a semicolon in parametername
// to separate the name from the units. Functionname describes the funciton, and is put in brackets in the y axis label.
// For example, to fit to Delta_pt = [0]*(eta_org-[1]), you could use functionname = "#Deltap_{T} = A(#eta-B)",
// parameter = 0, and parametername = "A;GeV".
// (3) if parameter < 0, it will draw the profile/resolution along with the fitted functions.
// The parameter of interest is still indicated by parameter, which is transformed to -parameter - 1.
// For example, -1 --> 0, -2 --> 1, -3 --> 2, ...
// This parameter's value and error will be in the legend. You still need to enter parametername and functionname,
// because they will be used for labels.
//The best way to run misalignmentDependence is through makePlots. If you want to run misalignmentDependence directly,
//the LAST function, all the way at the bottom of this file, is probably the most practical to use (for all three of these).
// The first function takes a canvas as its argument. This canvas needs to have been produced with trackSplitPlot using
// the same values of xvar, yvar, relative, resolution, and pull or something strange could happen.
void misalignmentDependence(TCanvas *c1old,
Int_t nFiles,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TF1 *function,
Int_t parameter,
TString parametername,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
if (c1old == nullptr)
return;
c1old = (TCanvas *)c1old->Clone("c1old");
if (misalignment == "" || yvar == "")
return;
Bool_t drawfits = (parameter < 0);
if (parameter < 0)
parameter = -parameter - 1; //-1 --> 0, -2 --> 1, -3 --> 2, ...
TString yaxislabel = nPart(1, parametername);
TString parameterunits = nPart(2, parametername);
if (parameterunits != "")
yaxislabel.Append(" (").Append(parameterunits).Append(")");
TList *list = c1old->GetListOfPrimitives();
//const int n = list->GetEntries() - 2 - (xvar == "");
const int n = nFiles;
gStyle->SetOptStat(0);
gStyle->SetOptFit(0);
gStyle->SetFitFormat("5.4g");
gStyle->SetFuncColor(2);
gStyle->SetFuncStyle(1);
gStyle->SetFuncWidth(1);
TH1 **p = new TH1 *[n];
TF1 **f = new TF1 *[n];
bool used[n];
for (Int_t i = 0; i < n; i++) {
stringstream s0;
s0 << "p" << i;
TString pname = s0.str();
p[i] = (TH1 *)list->/*At(i+1+(xvar == ""))*/ FindObject(pname);
used[i] = (p[i] != nullptr);
if (used[i])
p[i]->SetDirectory(nullptr);
if (xvar == "")
continue;
stringstream s;
s << function->GetName() << i;
TString newname = s.str();
f[i] = (TF1 *)function->Clone(newname);
stufftodelete->Add(f[i]);
}
Double_t *result = new Double_t[nFiles];
Double_t *error = new Double_t[nFiles];
if (xvar == "") {
yaxislabel = axislabel(yvar, 'y', relative, resolution, pull);
for (Int_t i = 0; i < nFiles; i++) {
if (!used[i])
continue;
if (!resolution) {
result[i] = p[i]->GetMean();
error[i] = p[i]->GetMeanError();
} else {
result[i] = p[i]->GetRMS();
error[i] = p[i]->GetRMSError();
}
cout << result[i] << " +/- " << error[i] << endl;
}
} else {
for (int i = 0; i < n; i++) {
if (!used[i])
continue;
f[i]->SetLineColor(colors[i]);
f[i]->SetLineStyle(styles[i]);
f[i]->SetLineWidth(1);
p[i]->SetMarkerColor(colors[i]);
p[i]->SetMarkerStyle(20 + i);
p[i]->SetLineColor(colors[i]);
p[i]->SetLineStyle(styles[i]);
p[i]->Fit(f[i], "IM");
error[i] = f[i]->GetParError(parameter);
//the fits sometimes don't work if the parameters are constrained.
//take care of the constraining here.
//for sine, make the amplitude positive and the phase between 0 and 2pi.
//unless the amplitude is the only parameter (eg sagitta theta theta)
if (function->GetName() == TString("sine") && function->GetNumberFreeParameters() >= 2) {
if (f[i]->GetParameter(0) < 0) {
f[i]->SetParameter(0, -f[i]->GetParameter(0));
f[i]->SetParameter(2, f[i]->GetParameter(2) + pi);
}
while (f[i]->GetParameter(2) >= 2 * pi)
f[i]->SetParameter(2, f[i]->GetParameter(2) - 2 * pi);
while (f[i]->GetParameter(2) < 0)
f[i]->SetParameter(2, f[i]->GetParameter(2) + 2 * pi);
}
result[i] = f[i]->GetParameter(parameter);
}
}
TCanvas *c1 = TCanvas::MakeDefCanvas();
if (drawfits && xvar != "" && yvar != "") {
TString legendtitle = "[";
legendtitle.Append(functionname);
legendtitle.Append("]");
TLegend *legend = new TLegend(.7, .7, .9, .9, legendtitle, "br");
stufftodelete->Add(legend);
TString drawoption = "";
for (int i = 0; i < n; i++) {
if (!used[i])
continue;
p[i]->Draw(drawoption);
f[i]->Draw("same");
drawoption = "same";
stringstream s;
s.precision(3);
s << nPart(1, parametername) << " = " << result[i] << " #pm " << error[i];
if (parameterunits != "")
s << " " << parameterunits;
TString str = s.str();
legend->AddEntry(p[i], names[i], "pl");
legend->AddEntry(f[i], str, "l");
}
c1->Update();
Double_t x1min = .98 * gPad->GetUxmin() + .02 * gPad->GetUxmax();
Double_t x2max = .02 * gPad->GetUxmin() + .98 * gPad->GetUxmax();
Double_t y1min = .98 * gPad->GetUymin() + .02 * gPad->GetUymax();
Double_t y2max = .02 * gPad->GetUymin() + .98 * gPad->GetUymax();
Double_t width = .4 * (x2max - x1min);
Double_t height = (1. / 20) * legend->GetListOfPrimitives()->GetEntries() * (y2max - y1min);
width *= 2;
height /= 2;
legend->SetNColumns(2);
Double_t newy2max = placeLegend(legend, width, height, x1min, y1min, x2max, y2max);
p[0]->GetYaxis()->SetRangeUser(gPad->GetUymin(), (newy2max - .02 * gPad->GetUymin()) / .98);
legend->SetFillStyle(0);
legend->Draw();
} else {
if (values == nullptr)
return;
Bool_t phasesmatter = false;
if (misalignment == "elliptical" || misalignment == "sagitta" || misalignment == "skew") {
if (phases == nullptr) {
cout << "This misalignment has a phase, but you didn't supply the phases!" << endl
<< "Can't produce plots depending on the misalignment value." << endl;
return;
}
int firstnonzero = -1;
for (Int_t i = 0; i < nFiles; i++) {
if (values[i] == 0)
continue; //if the amplitude is 0 the phase is arbitrary
if (firstnonzero == -1)
firstnonzero = i;
if (phases[i] != phases[firstnonzero])
phasesmatter = true;
}
}
if (!phasesmatter) {
TGraphErrors *g = new TGraphErrors(nFiles, values, result, (Double_t *)nullptr, error);
g->SetName("");
stufftodelete->Add(g);
TString xaxislabel = "#epsilon_{";
xaxislabel.Append(misalignment);
xaxislabel.Append("}");
g->GetXaxis()->SetTitle(xaxislabel);
if (xvar != "") {
yaxislabel.Append(" [");
yaxislabel.Append(functionname);
yaxislabel.Append("]");
}
g->GetYaxis()->SetTitle(yaxislabel);
g->SetMarkerColor(colors[0]);
g->SetMarkerStyle(20);
g->Draw("AP");
Double_t yaxismax = g->GetYaxis()->GetXmax();
Double_t yaxismin = g->GetYaxis()->GetXmin();
if (yaxismin > 0) {
yaxismax += yaxismin;
yaxismin = 0;
}
g->GetYaxis()->SetRangeUser(yaxismin, yaxismax);
g->Draw("AP");
} else {
double *xvalues = new double[nFiles];
double *yvalues = new double[nFiles]; //these are not physically x and y (except in the case of skew)
for (int i = 0; i < nFiles; i++) {
xvalues[i] = values[i] * cos(phases[i]);
yvalues[i] = values[i] * sin(phases[i]);
}
TGraph2DErrors *g =
new TGraph2DErrors(nFiles, xvalues, yvalues, result, (Double_t *)nullptr, (Double_t *)nullptr, error);
g->SetName("");
stufftodelete->Add(g);
delete[] xvalues; //A TGraph2DErrors has its own copy of xvalues and yvalues, so it's ok to delete these copies.
delete[] yvalues;
TString xaxislabel = "#epsilon_{";
xaxislabel.Append(misalignment);
xaxislabel.Append("}cos(#delta)");
TString realyaxislabel = xaxislabel;
realyaxislabel.ReplaceAll("cos(#delta)", "sin(#delta)");
g->GetXaxis()->SetTitle(xaxislabel);
g->GetYaxis()->SetTitle(realyaxislabel);
TString zaxislabel = /*"fake"*/ yaxislabel; //yaxislabel is defined earlier
if (xvar != "") {
zaxislabel.Append(" [");
zaxislabel.Append(functionname);
zaxislabel.Append("]");
}
g->GetZaxis()->SetTitle(zaxislabel);
g->SetMarkerStyle(20);
g->Draw("pcolerr");
}
}
if (saveas != "") {
saveplot(c1, saveas);
delete[] p;
delete[] f;
delete[] result;
delete[] error;
delete c1old;
}
}
//This version allows you to show multiple parameters. It runs the previous version multiple times, once for each parameter.
//saveas will be modified to indicate which parameter is being used each time.
void misalignmentDependence(TCanvas *c1old,
Int_t nFiles,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TF1 *function,
Int_t nParameters,
Int_t *parameters,
TString *parameternames,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
for (int i = 0; i < nParameters; i++) {
TString saveasi = saveas;
TString insert = nPart(1, parameternames[i]);
insert.Prepend(".");
saveasi.Insert(saveasi.Last('.'), insert); //insert the parameter name before the file extension
misalignmentDependence(c1old,
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
function,
parameters[i],
parameternames[i],
functionname,
relative,
resolution,
pull,
saveasi);
}
}
//This version does not take a canvas as its argument. It runs trackSplitPlot to produce the canvas.
void misalignmentDependence(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TF1 *function,
Int_t parameter,
TString parametername,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
misalignmentDependence(trackSplitPlot(nFiles, files, names, xvar, yvar, relative, resolution, pull, ""),
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
function,
parameter,
parametername,
functionname,
relative,
resolution,
pull,
saveas);
}
void misalignmentDependence(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TF1 *function,
Int_t nParameters,
Int_t *parameters,
TString *parameternames,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
for (int i = 0; i < nParameters; i++) {
TString saveasi = saveas;
TString insert = nPart(1, parameternames[i]);
insert.Prepend(".");
saveasi.Insert(saveasi.Last('.'), insert); //insert the parameter name before the file extension
misalignmentDependence(nFiles,
files,
names,
misalignment,
values,
phases,
xvar,
yvar,
function,
parameters[i],
parameternames[i],
functionname,
relative,
resolution,
pull,
saveasi);
}
}
// This version allows you to use a string for the function. It creates a TF1 using this string and uses this TF1
void misalignmentDependence(TCanvas *c1old,
Int_t nFiles,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TString function,
Int_t parameter,
TString parametername,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
TF1 *f = new TF1("func", function);
misalignmentDependence(c1old,
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
f,
parameter,
parametername,
functionname,
relative,
resolution,
pull,
saveas);
delete f;
}
void misalignmentDependence(TCanvas *c1old,
Int_t nFiles,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TString function,
Int_t nParameters,
Int_t *parameters,
TString *parameternames,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
for (int i = 0; i < nParameters; i++) {
TString saveasi = saveas;
TString insert = nPart(1, parameternames[i]);
insert.Prepend(".");
saveasi.Insert(saveasi.Last('.'), insert); //insert the parameter name before the file extension
misalignmentDependence(c1old,
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
function,
parameters[i],
parameternames[i],
functionname,
relative,
resolution,
pull,
saveasi);
}
}
void misalignmentDependence(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TString function,
Int_t parameter,
TString parametername,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
TF1 *f = new TF1("func", function);
misalignmentDependence(nFiles,
files,
names,
misalignment,
values,
phases,
xvar,
yvar,
f,
parameter,
parametername,
functionname,
relative,
resolution,
pull,
saveas);
delete f;
}
void misalignmentDependence(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
TString function,
Int_t nParameters,
Int_t *parameters,
TString *parameternames,
TString functionname,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
for (int i = 0; i < nParameters; i++) {
TString saveasi = saveas;
TString insert = nPart(1, parameternames[i]);
insert.Prepend(".");
saveasi.Insert(saveasi.Last('.'), insert); //insert the parameter name before the file extension
misalignmentDependence(nFiles,
files,
names,
misalignment,
values,
phases,
xvar,
yvar,
function,
parameters[i],
parameternames[i],
functionname,
relative,
resolution,
pull,
saveasi);
}
}
//This version does not take a function as its argument. It automatically determines what function, parameter,
//functionname, and parametername to use based on misalignment, xvar, yvar, relative, resolution, and pull.
//However, you have to manually put into the function which plots to fit to what shapes.
//The 2012A data, using the prompt geometry, is a nice example if you want to see an elliptical misalignment.
//If drawfits is true, it draws the fits; otherwise it plots the parameter as a function of misalignment as given by values.
//If the combination of misalignment, xvar, yvar, relative, resolution, pull has a default function to use, it returns true,
// otherwise it returns false.
//This is the version called by makeThesePlots.C
Bool_t misalignmentDependence(TCanvas *c1old,
Int_t nFiles,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
Bool_t drawfits,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
if (xvar == "") {
if (c1old == nullptr || misalignment == "" || values == nullptr)
return false;
misalignmentDependence(c1old,
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
(TF1 *)nullptr,
0,
"",
"",
relative,
resolution,
pull,
saveas);
return true;
}
TF1 *f = nullptr;
TString functionname = "";
//if only one parameter is of interest
TString parametername = "";
Int_t parameter = 9999;
//if multiple parameters are of interest
Int_t nParameters = -1;
TString *parameternames = nullptr;
Int_t *parameters = nullptr;
if (misalignment == "sagitta") {
if (xvar == "phi" && yvar == "phi" && !resolution && !pull) {
f = new TF1("sine", "-[0]*cos([1]*x+[2])");
f->FixParameter(1, 1);
f->SetParameter(0, 6e-4);
nParameters = 2;
Int_t tempParameters[2] = {0, 2};
TString tempParameterNames[2] = {"A;mrad", "B"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#Delta#phi=-Acos(#phi+B)";
}
if (xvar == "theta" && yvar == "theta" && !resolution && pull) {
f = new TF1("line", "-[0]*(x+[1])");
f->FixParameter(1, -pi / 2);
parametername = "A";
functionname = "#Delta#theta/#delta(#Delta#theta)=-A(#theta-#pi/2)";
parameter = 0;
}
if (xvar == "theta" && yvar == "theta" && !resolution && !pull) {
f = new TF1("sine", "[0]*sin([1]*x+[2])");
f->FixParameter(1, 2);
f->FixParameter(2, 0);
parametername = "A;mrad";
functionname = "#Delta#theta=-Asin(2#theta)";
parameter = 0;
}
}
if (misalignment == "elliptical") {
if (xvar == "phi" && yvar == "dxy" && !resolution && !pull) {
f = new TF1("sine", "[0]*sin([1]*x-[2])");
//f = new TF1("sine","[0]*sin([1]*x-[2]) + [3]");
f->FixParameter(1, -2);
f->SetParameter(0, 5e-4);
nParameters = 2;
Int_t tempParameters[2] = {0, 2};
TString tempParameterNames[2] = {"A;#mum", "B"};
//nParameters = 3;
//Int_t tempParameters[3] = {0,2,3};
//TString tempParameterNames[3] = {"A;#mum","B","C;#mum"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#Deltad_{xy}=-Asin(2#phi+B)";
//functionname = "#Deltad_{xy}=-Asin(2#phi+B)+C";
}
if (xvar == "phi" && yvar == "dxy" && !resolution && pull) {
f = new TF1("sine", "[0]*sin([1]*x-[2])");
//f = new TF1("sine","[0]*sin([1]*x-[2]) + [3]");
f->FixParameter(1, -2);
nParameters = 2;
Int_t tempParameters[2] = {0, 2};
TString tempParameterNames[2] = {"A", "B"};
//nParameters = 3;
//Int_t tempParameters[3] = {0,2,3};
//TString tempParameterNames[3] = {"A","B","C"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#Deltad_{xy}/#delta(#Deltad_{xy})=-Asin(2#phi+B)";
//functionname = "#Deltad_{xy}/#delta(#Deltad_{xy})=-Asin(2#phi+B)+C";
}
if (xvar == "theta" && yvar == "dz" && !resolution && !pull) {
f = new TF1("line", "-[0]*(x-[1])");
f->FixParameter(1, pi / 2);
parametername = "A;#mum";
functionname = "#Deltad_{z}=-A(#theta-#pi/2)";
parameter = 0;
}
/*
This fit doesn't work
if (xvar == "theta" && yvar == "dz" && !resolution && pull)
{
f = new TF1("sine","[0]*sin([1]*x+[2])");
f->FixParameter(2,-pi/2);
f->FixParameter(1,1);
parametername = "A";
functionname = "#Deltad_{z}/#delta(#Deltad_{z})=Acos(#theta)";
parameter = 0;
}
*/
if (xvar == "dxy" && yvar == "phi" && !resolution && !pull) {
f = new TF1("line", "-[0]*(x-[1])");
f->FixParameter(1, 0);
parametername = "A;mrad/cm";
functionname = "#Delta#phi=-A(d_{xy})";
parameter = 0;
}
if (xvar == "dxy" && yvar == "phi" && !resolution && pull) {
f = new TF1("line", "-[0]*(x-[1])");
f->FixParameter(1, 0);
parametername = "A;cm^{-1}";
functionname = "#Delta#phi/#delta(#Delta#phi)=-A(d_{xy})";
parameter = 0;
}
}
if (misalignment == "skew") {
if (xvar == "phi" && yvar == "theta" && resolution && !pull) {
f = new TF1("sine", "[0]*sin([1]*x+[2])+[3]");
f->FixParameter(1, 2);
nParameters = 3;
Int_t tempParameters[3] = {0, 2, 3};
TString tempParameterNames[3] = {"A;mrad", "B", "C;mrad"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#sigma(#Delta#theta)=Asin(2#phi+B)+C";
}
if (xvar == "phi" && yvar == "eta" && resolution && !pull) {
f = new TF1("sine", "[0]*sin([1]*x+[2])+[3]");
f->FixParameter(1, 2);
nParameters = 3;
Int_t tempParameters[3] = {0, 2, 3};
TString tempParameterNames[3] = {"A;mrad", "B", "C;mrad"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#sigma(#Delta#eta)=Asin(2#phi+B)+C";
}
if (xvar == "phi" && yvar == "theta" && resolution && pull) {
f = new TF1("sine", "[0]*sin([1]*x+[2])+[3]");
f->FixParameter(1, 2);
nParameters = 3;
Int_t tempParameters[3] = {0, 2, 3};
TString tempParameterNames[3] = {"A", "B", "C"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#sigma(#Delta#theta/#delta(#Delta#theta))=Asin(2#phi+B)+C";
}
if (xvar == "phi" && yvar == "eta" && resolution && pull) {
f = new TF1("sine", "[0]*sin([1]*x+[2])+[3]");
f->FixParameter(1, 2);
nParameters = 3;
Int_t tempParameters[3] = {0, 2, 3};
TString tempParameterNames[3] = {"A", "B", "C"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#sigma(#Delta#eta/#delta(#Delta#eta))=Asin(2#phi+B)+C";
}
if (xvar == "phi" && yvar == "dz" && !resolution && !pull) {
f = new TF1("tanh", "[0]*(tanh([1]*(x+[2])) )"); // - tanh(([3]-[1])*x+[2]) + 1)");
//f = new TF1("tanh","[0]*(tanh([1]*(x+[2])) + tanh([1]*([3]-[2]-x)) - 1)");
f->SetParameter(0, 100);
f->SetParLimits(1, -20, 20);
f->SetParLimits(2, 0, pi);
f->FixParameter(3, pi);
nParameters = 3;
Int_t tempParameters[3] = {0, 1, 2};
TString tempParameterNames[3] = {"A;#mum", "B", "C"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#Deltad_{z}=Atanh(B(#phi+C))";
//functionname = "#Deltad_{z}=A(tanh(B(#phi+C)) + tanh(B(#pi-#phi-C)) - 1";
}
}
if (misalignment == "layerRot") {
if (xvar == "qoverpt" && yvar == "qoverpt" && !relative && !resolution && !pull) {
f = new TF1("sech", "[0]/cosh([1]*(x+[2]))+[3]");
//f = new TF1("gauss","[0]/exp(([1]*(x+[2]))^2)+[3]"); //sech works better than a gaussian
f->SetParameter(0, 1);
f->SetParameter(1, 1);
f->SetParLimits(1, 0, 10);
f->FixParameter(2, 0);
f->FixParameter(3, 0);
nParameters = 2;
Int_t tempParameters[2] = {0, 1};
TString tempParameterNames[2] = {"A;10^{-3}e/GeV", "B;GeV/e"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#Delta(q/p_{T})=Asech(B(q/p_{T}))";
}
}
if (misalignment == "telescope") {
if (xvar == "theta" && yvar == "theta" && !relative && !resolution && !pull) {
f = new TF1("gauss", "[0]/exp(([1]*(x+[2]))^2)+[3]");
f->SetParameter(0, 1);
f->SetParameter(1, 1);
f->SetParLimits(1, 0, 10);
f->FixParameter(2, -pi / 2);
f->FixParameter(3, 0);
nParameters = 2;
Int_t tempParameters[2] = {0, 1};
TString tempParameterNames[2] = {"A;mrad", "B"};
parameters = tempParameters;
parameternames = tempParameterNames;
functionname = "#Delta#theta=Aexp(-(B(#theta-#pi/2))^{2})";
}
}
if (functionname == "")
return false;
if (drawfits) {
parameter = -parameter - 1;
for (int i = 0; i < nParameters; i++)
parameters[i] = -parameters[i] - 1;
}
if (nParameters > 0)
misalignmentDependence(c1old,
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
f,
nParameters,
parameters,
parameternames,
functionname,
relative,
resolution,
pull,
saveas);
else
misalignmentDependence(c1old,
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
f,
parameter,
parametername,
functionname,
relative,
resolution,
pull,
saveas);
delete f;
return true;
}
//This is the most practically useful version. It does not take a canvas, but produces it automatically and then determines what
//function to fit it to.
Bool_t misalignmentDependence(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString xvar,
TString yvar,
Bool_t drawfits,
Bool_t relative,
Bool_t resolution,
Bool_t pull,
TString saveas) {
return misalignmentDependence(trackSplitPlot(nFiles, files, names, xvar, yvar, relative, resolution, pull, ""),
nFiles,
names,
misalignment,
values,
phases,
xvar,
yvar,
drawfits,
relative,
resolution,
pull,
saveas);
}
Bool_t hasFit(TString misalignment, TString xvar, TString yvar, Bool_t relative, Bool_t resolution, Bool_t pull) {
return misalignmentDependence((TCanvas *)nullptr,
0,
(TString *)nullptr,
misalignment,
(Double_t *)nullptr,
(Double_t *)nullptr,
xvar,
yvar,
false,
relative,
resolution,
pull,
TString(""));
}
//=============
//2. Make Plots
//=============
void makePlots(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString directory,
Bool_t matrix[xsize][ysize]) {
stufftodelete->SetOwner(true);
for (Int_t i = 0, totaltime = 0; i < nFiles; i++) {
TFile *f = nullptr;
bool exists = false;
if (files[i] == "")
exists = true;
for (int j = 1; j <= 60 * 24 && !exists; j++, totaltime++) //wait up to 1 day for the validation to be finished
{
f = TFile::Open(files[i]);
if (f != nullptr)
exists = f->IsOpen();
delete f;
if (exists)
continue;
gSystem->Sleep(60000);
cout << "It's been ";
if (j >= 60)
cout << j / 60 << " hour";
if (j >= 120)
cout << "s";
if (j % 60 != 0 && j >= 60)
cout << " and ";
if (j % 60 != 0)
cout << j % 60 << " minute";
if (j % 60 >= 2)
cout << "s";
cout << endl;
}
if (!exists)
return;
if (i == nFiles - 1 && totaltime > nFiles)
gSystem->Sleep(60000);
}
TString directorytomake = directory;
gSystem->mkdir(directorytomake, true);
ofstream summaryfile(directorytomake + "/TrackSplittingValidationSummary.txt");
for (int i = 0; i < nFiles; i++) {
summaryfile << "\t" << TString(names[i]).ReplaceAll("#", "\\");
}
summaryfile << "\tformat={}\tlatexformat={}\n";
if (misalignment != "") {
directorytomake.Append("/fits");
gSystem->mkdir(directorytomake);
}
for (Int_t x = 0; x < xsize; x++) {
for (Int_t y = 0; y < ysize; y++) {
for (Int_t pull = 0; pull == 0 || (pull == 1 && yvariables[y] != ""); pull++) {
if (false)
continue; //this line is to make it easier to do e.g. all plots involving Delta eta
//(replace false with yvariables[y] != "eta")
if (!matrix[x][y])
continue;
if (xvariables[x] == "" && yvariables[y] == "")
continue;
Int_t nPlots =
nFiles + 4; //scatterplot for each (if you uncomment it), profile, resolution, and fits for each.
vector<TString> s;
TString slashstring = "";
if (directory.Last('/') != directory.Length() - 1)
slashstring = "/";
vector<TString> plotnames;
for (Int_t i = 0; i < nFiles; i++) {
plotnames.push_back(names[i]); //this is plotnames[i]
plotnames[i].ReplaceAll(" ", "");
}
plotnames.push_back(""); //this is plotnames[nFiles], but gets changed
if (yvariables[y] == "")
plotnames[nFiles] = "orghist";
else if (xvariables[x] == "")
plotnames[nFiles] = "hist";
else
plotnames[nFiles] = "profile";
plotnames.push_back("resolution"); //this is plotnames[nFiles+1]
plotnames.push_back(""); //this is plotnames[nFiles+2]
plotnames.push_back(""); //this is plotnames[nFiles+3]
if (plotnames[nFiles] == "profile") {
plotnames[nFiles + 2] = ".profile";
plotnames[nFiles + 2].Prepend(misalignment);
plotnames[nFiles + 3] = ".resolution";
plotnames[nFiles + 3].Prepend(misalignment);
plotnames[nFiles + 2].Prepend("fits/");
plotnames[nFiles + 3].Prepend("fits/");
} else {
plotnames[nFiles + 2] = "profile.";
plotnames[nFiles + 2].Append(misalignment);
plotnames[nFiles + 3] = "resolution.";
plotnames[nFiles + 3].Append(misalignment);
}
TString pullstring = "";
if (pull)
pullstring = "pull.";
TString xvarstring = xvariables[x];
if (xvariables[x] != "runNumber" && !xvariables[x].BeginsWith("nHits") && xvariables[x] != "")
xvarstring.Append("_org");
if (xvariables[x] != "" && yvariables[y] != "")
xvarstring.Append(".");
TString yvarstring = yvariables[y];
if (yvariables[y] != "")
yvarstring.Prepend("Delta_");
TString relativestring = "";
if (relativearray[y])
relativestring = ".relative";
for (Int_t i = 0; i < nPlots; i++) {
stringstream ss;
ss << directory << slashstring << plotnames[i] << "." << pullstring << xvarstring << yvarstring
<< relativestring << ".pngepsroot";
s.push_back(ss.str());
if (misalignment != "") {
TString wrongway = misalignment;
TString rightway = misalignment;
wrongway.Append(".pull");
rightway.Prepend("pull.");
s[i].ReplaceAll(wrongway, rightway);
}
}
Int_t i;
for (i = 0; i < nFiles; i++) {
if (xvariables[x] == "" || yvariables[y] == "")
continue;
//uncomment this section to make scatterplots
/*
trackSplitPlot(files[i],xvariables[x],yvariables[y],false,relativearray[y],false,(bool)pull,s[i]);
stufftodelete->Clear();
for ( ; gROOT->GetListOfCanvases()->GetEntries() > 0; )
deleteCanvas( gROOT->GetListOfCanvases()->Last());
for ( ; gROOT->GetListOfFiles()->GetEntries() > 0; )
delete (TFile*)gROOT->GetListOfFiles()->Last();
*/
}
if (xvariables[x] != "" && yvariables[y] != "") {
//make profile
TCanvas *c1 = trackSplitPlot(
nFiles, files, names, xvariables[x], yvariables[y], relativearray[y], false, (bool)pull, s[i], summaryfile);
if (misalignmentDependence(c1,
nFiles,
names,
misalignment,
values,
phases,
xvariables[x],
yvariables[y],
true,
relativearray[y],
false,
(bool)pull,
s[i + 2])) {
s[i + 2].ReplaceAll(".png", ".parameter.png");
misalignmentDependence(c1,
nFiles,
names,
misalignment,
values,
phases,
xvariables[x],
yvariables[y],
false,
relativearray[y],
false,
(bool)pull,
s[i + 2]);
}
stufftodelete->Clear();
for (; gROOT->GetListOfCanvases()->GetEntries() > 0;)
deleteCanvas(gROOT->GetListOfCanvases()->Last());
for (; gROOT->GetListOfFiles()->GetEntries() > 0;)
delete (TFile *)gROOT->GetListOfFiles()->Last();
//make resolution plot
TCanvas *c2 = trackSplitPlot(nFiles,
files,
names,
xvariables[x],
yvariables[y],
relativearray[y],
true,
(bool)pull,
s[i + 1],
summaryfile);
if (misalignmentDependence(c2,
nFiles,
names,
misalignment,
values,
phases,
xvariables[x],
yvariables[y],
true,
relativearray[y],
true,
(bool)pull,
s[i + 3])) {
s[i + 3].ReplaceAll(".png", ".parameter.png");
misalignmentDependence(c2,
nFiles,
names,
misalignment,
values,
phases,
xvariables[x],
yvariables[y],
false,
relativearray[y],
true,
(bool)pull,
s[i + 3]);
}
stufftodelete->Clear();
for (; gROOT->GetListOfCanvases()->GetEntries() > 0;)
deleteCanvas(gROOT->GetListOfCanvases()->Last());
for (; gROOT->GetListOfFiles()->GetEntries() > 0;)
delete (TFile *)gROOT->GetListOfFiles()->Last();
} else {
//make histogram
TCanvas *c1 = trackSplitPlot(
nFiles, files, names, xvariables[x], yvariables[y], relativearray[y], false, (bool)pull, s[i], summaryfile);
if (misalignmentDependence(c1,
nFiles,
names,
misalignment,
values,
phases,
xvariables[x],
yvariables[y],
true,
relativearray[y],
false,
(bool)pull,
s[i + 2])) {
misalignmentDependence(c1,
nFiles,
names,
misalignment,
values,
phases,
xvariables[x],
yvariables[y],
true,
relativearray[y],
true,
(bool)pull,
s[i + 3]);
}
stufftodelete->Clear();
for (; gROOT->GetListOfCanvases()->GetEntries() > 0;)
deleteCanvas(gROOT->GetListOfCanvases()->Last());
for (; gROOT->GetListOfFiles()->GetEntries() > 0;)
delete (TFile *)gROOT->GetListOfFiles()->Last();
}
}
cout << y + ysize * x + 1 << "/" << xsize * ysize << endl;
}
}
}
void makePlots(Int_t nFiles, TString *files, TString *names, TString directory, Bool_t matrix[xsize][ysize]) {
makePlots(nFiles, files, names, "", (Double_t *)nullptr, (Double_t *)nullptr, directory, matrix);
}
void makePlots(TString file,
TString misalignment,
Double_t *values,
Double_t *phases,
TString directory,
Bool_t matrix[xsize][ysize]) {
setupcolors();
file.Remove(TString::kTrailing, ',');
unsigned int n = file.CountChar(',') + 1;
TString *files = new TString[n];
TString *names = new TString[n];
vector<Color_t> tempcolors = colors;
vector<Style_t> tempstyles = styles;
for (unsigned int i = 0; i < n; i++) {
TString thisfile = nPart(i + 1, file, ",");
int numberofpipes = thisfile.CountChar('|');
if (numberofpipes >= 0 && nPart(numberofpipes + 1, thisfile, "|").IsDigit()) {
if (numberofpipes >= 1 && nPart(numberofpipes, thisfile, "|").IsDigit()) {
colors[i] = nPart(numberofpipes, thisfile, "|").Atoi();
styles[i] = nPart(numberofpipes + 1, thisfile, "|").Atoi();
thisfile.Remove(thisfile.Length() - nPart(numberofpipes, thisfile, "|").Length() -
nPart(numberofpipes + 1, thisfile, "|").Length() - 2);
} else {
colors[i] = nPart(numberofpipes + 1, thisfile, "|").Atoi();
thisfile.Remove(thisfile.Length() - nPart(numberofpipes + 1, thisfile, "|").Length() - 2);
}
}
files[i] = nPart(1, thisfile, "=", true);
names[i] = nPart(2, thisfile, "=", false);
}
if (n == 1 && names[0] == "")
names[0] =
"scatterplot"; //With 1 file there's no legend, so this is only used in the filename of the scatterplots, if made
makePlots(n, files, names, misalignment, values, phases, directory, matrix);
delete[] files;
delete[] names;
colors = tempcolors;
styles = tempstyles;
}
void makePlots(TString file, TString directory, Bool_t matrix[xsize][ysize]) {
makePlots(file, "", (Double_t *)nullptr, (Double_t *)nullptr, directory, matrix);
}
//***************************************************************************
//functions to make plots for 1 row, column, or cell of the matrix
//examples:
// xvar = "nHits", yvar = "ptrel" - makes plots of nHits vs Delta_pt/pt_org
// xvar = "all", yvar = "pt" - makes all plots involving Delta_pt
// (not Delta_pt/pt_org)
// xvar = "", yvar = "all" - makes all histograms of Delta_???
// (including Delta_pt/pt_org)
//***************************************************************************
void makePlots(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString directory,
TString xvar,
TString yvar) {
Bool_t matrix[xsize][ysize];
for (int x = 0; x < xsize; x++)
for (int y = 0; y < ysize; y++) {
bool xmatch = (xvar == "all" || xvar == xvariables[x]);
bool ymatch = (yvar == "all" || yvar == yvariables[y]);
if (yvar == "pt" && yvariables[y] == "pt" && relativearray[y] == true)
ymatch = false;
if (yvar == "ptrel" && yvariables[y] == "pt" && relativearray[y] == true)
ymatch = true;
matrix[x][y] = (xmatch && ymatch);
}
makePlots(nFiles, files, names, misalignment, values, phases, directory, matrix);
}
void makePlots(Int_t nFiles, TString *files, TString *names, TString directory, TString xvar, TString yvar) {
makePlots(nFiles, files, names, "", (Double_t *)nullptr, (Double_t *)nullptr, directory, xvar, yvar);
}
void makePlots(TString file,
TString misalignment,
Double_t *values,
Double_t *phases,
TString directory,
TString xvar,
TString yvar) {
setupcolors();
file.Remove(TString::kTrailing, ',');
unsigned int n = file.CountChar(',') + 1;
TString *files = new TString[n];
TString *names = new TString[n];
vector<Color_t> tempcolors = colors;
vector<Style_t> tempstyles = styles;
for (unsigned int i = 0; i < n; i++) {
TString thisfile = nPart(i + 1, file, ",");
int numberofpipes = thisfile.CountChar('|');
if (numberofpipes >= 0 && nPart(numberofpipes + 1, thisfile, "|").IsDigit()) {
if (numberofpipes >= 1 && nPart(numberofpipes, thisfile, "|").IsDigit()) {
colors[i] = nPart(numberofpipes, thisfile, "|").Atoi();
styles[i] = nPart(numberofpipes + 1, thisfile, "|").Atoi();
thisfile.Remove(thisfile.Length() - nPart(numberofpipes, thisfile, "|").Length() -
nPart(numberofpipes + 1, thisfile, "|").Length() - 2);
} else {
colors[i] = nPart(numberofpipes + 1, thisfile, "|").Atoi();
thisfile.Remove(thisfile.Length() - nPart(numberofpipes + 1, thisfile, "|").Length() - 2);
}
}
files[i] = nPart(1, thisfile, "=", true);
names[i] = nPart(2, thisfile, "=", false);
}
if (n == 1 && names[0] == "")
names[0] =
"scatterplot"; //With 1 file there's no legend, so this is only used in the filename of the scatterplots, if made
makePlots(n, files, names, misalignment, values, phases, directory, xvar, yvar);
delete[] files;
delete[] names;
colors = tempcolors;
styles = tempstyles;
}
void makePlots(TString file, TString directory, TString xvar, TString yvar) {
makePlots(file, "", (Double_t *)nullptr, (Double_t *)nullptr, directory, xvar, yvar);
}
//***************************
//functions to make all plots
//***************************
void makePlots(Int_t nFiles,
TString *files,
TString *names,
TString misalignment,
Double_t *values,
Double_t *phases,
TString directory) {
makePlots(nFiles, files, names, misalignment, values, phases, directory, "all", "all");
}
void makePlots(Int_t nFiles, TString *files, TString *names, TString directory) {
makePlots(nFiles, files, names, "", (Double_t *)nullptr, (Double_t *)nullptr, directory);
}
void makePlots(TString file, TString misalignment, Double_t *values, Double_t *phases, TString directory) {
setupcolors();
file.Remove(TString::kTrailing, ',');
unsigned int n = file.CountChar(',') + 1;
TString *files = new TString[n];
TString *names = new TString[n];
vector<Color_t> tempcolors = colors;
vector<Style_t> tempstyles = styles;
for (unsigned int i = 0; i < n; i++) {
TString thisfile = nPart(i + 1, file, ",");
int numberofpipes = thisfile.CountChar('|');
if (numberofpipes >= 0 && nPart(numberofpipes + 1, thisfile, "|").IsDigit()) {
if (numberofpipes >= 1 && nPart(numberofpipes, thisfile, "|").IsDigit()) {
colors[i] = nPart(numberofpipes, thisfile, "|").Atoi();
styles[i] = nPart(numberofpipes + 1, thisfile, "|").Atoi();
thisfile.Remove(thisfile.Length() - nPart(numberofpipes, thisfile, "|").Length() -
nPart(numberofpipes + 1, thisfile, "|").Length() - 2);
} else {
colors[i] = nPart(numberofpipes + 1, thisfile, "|").Atoi();
thisfile.Remove(thisfile.Length() - nPart(numberofpipes + 1, thisfile, "|").Length() - 2);
}
}
files[i] = nPart(1, thisfile, "=", true);
names[i] = nPart(2, thisfile, "=", false);
}
if (n == 1 && names[0] == "")
names[0] =
"scatterplot"; //With 1 file there's no legend, so this is only used in the filename of the scatterplots, if made
makePlots(n, files, names, misalignment, values, phases, directory);
delete[] files;
delete[] names;
colors = tempcolors;
styles = tempstyles;
}
void makePlots(TString file, TString directory) {
makePlots(file, "", (Double_t *)nullptr, (Double_t *)nullptr, directory);
}
//=============
//3. Axis Label
//=============
TString fancyname(TString variable) {
if (variable == "pt")
return "p_{T}";
else if (variable == "phi")
return "#phi";
else if (variable == "eta")
return "#eta";
else if (variable == "theta")
return "#theta";
else if (variable == "qoverpt")
return "q/p_{T}";
else if (variable == "runNumber")
return "run number";
else if (variable == "dxy" || variable == "dz")
return variable.ReplaceAll("d", "d_{").Append("}");
else
return variable;
}
//this gives the units, to be put in the axis label
TString units(TString variable, Char_t axis) {
if (variable == "pt")
return "GeV";
if (variable == "dxy" || variable == "dz") {
if (axis == 'y')
return "#mum"; //in the tree, it's listed in centimeters, but in trackSplitPlot the value is divided by 1e4
if (axis == 'x')
return "cm";
}
if (variable == "qoverpt") {
if (axis == 'y')
return "#times10^{-3}e/GeV"; //e/TeV is not particularly intuitive
if (axis == 'x')
return "e/GeV";
}
if (axis == 'y' && (variable == "phi" || variable == "theta"))
return "mrad";
return "";
}
TString plainunits(TString variable, char axis) {
TString result = units(variable, axis);
result.ReplaceAll("#mu", "u");
result.ReplaceAll("#times10^{-3}", "* 1e-3 ");
return result;
}
TString latexunits(TString variable, char axis) {
TString result = units(variable, axis);
result.ReplaceAll("#", "\\")
.ReplaceAll("{", "{{")
.ReplaceAll("}", "}}")
.ReplaceAll("\\mum", "$\\mu$m")
.ReplaceAll("\\times10^{{-3}}", "$\\times10^{{-3}}$");
return result;
}
//this gives the full axis label, including units. It can handle any combination of relative, resolution, and pull.
TString axislabel(TString variable, Char_t axis, Bool_t relative, Bool_t resolution, Bool_t pull) {
if (axis == 'X' || axis == 'Y') {
double min, max, bins;
axislimits(0, nullptr, variable, tolower(axis), relative, pull, min, max, bins);
if (variable.BeginsWith("nHits"))
return "fraction of tracks";
if (variable == "runNumber")
return "number of tracks";
stringstream s;
s << "fraction of tracks / " << (max - min) / bins;
if (!pull && !relative) {
TString varunits = units(variable, tolower(axis));
if (varunits != "")
s << " " << varunits;
}
TString result = s.str();
result.ReplaceAll(" #times", "#times");
return result;
}
stringstream s;
if (resolution && axis == 'y')
s << "#sigma(";
if (axis == 'y')
s << "#Delta";
s << fancyname(variable);
if (relative && axis == 'y') {
s << " / ";
if (!pull)
s << "(";
s << fancyname(variable);
}
if (axis == 'y') {
if (pull) {
s << " / #delta(#Delta" << fancyname(variable);
if (relative)
s << " / " << fancyname(variable);
s << ")";
} else {
if (!relative)
s << " / ";
s << "#sqrt{2}";
if (relative)
s << ")";
}
}
if (resolution && axis == 'y')
s << ")";
if (((!relative && !pull) || axis == 'x') && units(variable, axis) != "")
s << " (" << units(variable, axis) << ")";
TString result = s.str();
result.ReplaceAll("#Deltaq/p_{T}", "#Delta(q/p_{T})");
return result;
}
TString latexlabel(TString variable, Char_t axis, Bool_t relative, Bool_t resolution, Bool_t pull) {
TString result = axislabel(variable, axis, relative, resolution, pull);
result.ReplaceAll(" (" + units(variable, axis) + ")", "");
result.ReplaceAll("#", "\\").ReplaceAll("\\Delta", "\\Delta ");
return result;
}
void setAxisLabels(TH1 *p, PlotType type, TString xvar, TString yvar, Bool_t relative, Bool_t pull) {
if (type == Histogram)
p->SetXTitle(axislabel(yvar, 'y', relative, false, pull));
if (type == ScatterPlot || type == Profile || type == Resolution || type == OrgHistogram)
p->SetXTitle(axislabel(xvar, 'x'));
if (type == Histogram)
p->SetYTitle(axislabel(yvar, 'Y', relative, false, pull));
if (type == OrgHistogram)
p->SetYTitle(axislabel(xvar, 'X', relative, false, pull));
if (type == ScatterPlot || type == Profile)
p->SetYTitle(axislabel(yvar, 'y', relative, false, pull));
if (type == Resolution)
p->SetYTitle(axislabel(yvar, 'y', relative, true, pull));
}
void setAxisLabels(TMultiGraph *p, PlotType type, TString xvar, TString yvar, Bool_t relative, Bool_t pull) {
if (type == Histogram)
p->GetXaxis()->SetTitle(axislabel(yvar, 'y', relative, false, pull));
if (type == ScatterPlot || type == Profile || type == Resolution || type == OrgHistogram)
p->GetXaxis()->SetTitle(axislabel(xvar, 'x'));
if (type == Histogram)
p->GetYaxis()->SetTitle(axislabel(yvar, 'Y', relative, false, pull));
if (type == OrgHistogram)
p->GetYaxis()->SetTitle(axislabel(xvar, 'X', relative, false, pull));
if (type == ScatterPlot || type == Profile)
p->GetYaxis()->SetTitle(axislabel(yvar, 'y', relative, false, pull));
if (type == Resolution)
p->GetYaxis()->SetTitle(axislabel(yvar, 'y', relative, true, pull));
}
TString nPart(Int_t part, TString string, TString delimit, Bool_t removerest) {
if (part <= 0)
return "";
for (int i = 1; i < part; i++) //part-1 times
{
if (string.Index(delimit) < 0)
return "";
string.Replace(0, string.Index(delimit) + 1, "", 0);
}
if (string.Index(delimit) >= 0 && removerest)
string.Remove(string.Index(delimit));
return string;
}
//==============
//4. Axis Limits
//==============
Double_t findStatistic(
Statistic what, Int_t nFiles, TString *files, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
Double_t x = 0, //if axis == 'x', var_org goes in x; if axis == 'y', Delta_var goes in x
rel = 1, //if relative, var_org goes in rel. x is divided by rel, so you get Delta_var/var_org
sigma1 = 1, //if pull, the error for split track 1 goes in sigma1 and the error for split track 2 goes in sigma2.
sigma2 = 1, //x is divided by sqrt(sigma1^2+sigma2^2). If !pull && axis == 'y', this divides by sqrt(2)
sigmaorg = 0; // because we want the error in one track. sigmaorg is used when relative && pull
Int_t xint = 0,
xint2 = 0; //xint is used for run number and nHits. xint2 is used for nHits because each event has 2 values.
Int_t runNumber = 0; //this is used to make sure the run number is between minrun and maxrun
if (axis == 'x') {
sigma1 = 1 / sqrt(2); //if axis == 'x' don't divide by sqrt(2)
sigma2 = 1 / sqrt(2);
}
Double_t totallength = 0;
vector<double> xvect;
Double_t result = 0;
if (what == Minimum)
result = 1e100;
if (what == Maximum)
result = -1e100;
stringstream sx, srel, ssigma1, ssigma2, ssigmaorg;
if (axis == 'y')
sx << "Delta_";
sx << var;
if (axis == 'x' && var != "runNumber" && !var.BeginsWith("nHits"))
sx << "_org";
if (axis == 'x' && var.BeginsWith("nHits"))
sx << "1_spl";
TString variable = sx.str(), variable2 = variable;
variable2.ReplaceAll("1_spl", "2_spl");
TString relvariable = "1";
if (relative) {
srel << var << "_org";
relvariable = srel.str();
}
if (pull) {
ssigma1 << var << "1Err_spl";
ssigma2 << var << "2Err_spl";
}
TString sigma1variable = ssigma1.str();
TString sigma2variable = ssigma2.str();
if (pull && relative)
ssigmaorg << var << "Err_org";
TString sigmaorgvariable = ssigmaorg.str();
if (!relative && !pull && (variable == "Delta_dxy" || variable == "Delta_dz"))
rel = 1e-4; //it's in cm but we want um
if (!relative && !pull && (variable == "Delta_phi" || variable == "Delta_theta" || variable == "Delta_qoverpt"))
rel = 1e-3; //make the axis labels manageable
for (Int_t j = 0; j < nFiles; j++) {
if (((var == "runNumber" && what != Maximum) ? findMax(files[j], "runNumber", 'x') < 2 : false) ||
files[j] == "") //if it's MC data (run 1), the run number is meaningless
continue;
TFile *f = TFile::Open(files[j]);
TTree *tree = (TTree *)f->Get("cosmicValidation/splitterTree");
if (tree == nullptr)
tree = (TTree *)f->Get("splitterTree");
Int_t length = tree->GetEntries();
tree->SetBranchAddress("runNumber", &runNumber);
if (var == "runNumber")
tree->SetBranchAddress(variable, &xint);
else if (var.BeginsWith("nHits")) {
tree->SetBranchAddress(variable, &xint);
tree->SetBranchAddress(variable2, &xint2);
} else
tree->SetBranchAddress(variable, &x);
if (relative)
tree->SetBranchAddress(relvariable, &rel);
if (pull) {
tree->SetBranchAddress(sigma1variable, &sigma1);
tree->SetBranchAddress(sigma2variable, &sigma2);
}
if (relative && pull)
tree->SetBranchAddress(sigmaorgvariable, &sigmaorg);
for (Int_t i = 0; i < length; i++) {
tree->GetEntry(i);
if (var == "runNumber" || var.BeginsWith("nHits"))
x = xint;
if (var == "runNumber")
runNumber = x;
if (var == "phi" && x >= pi)
x -= 2 * pi;
if (var == "phi" && x <= -pi)
x += 2 * pi;
if ((runNumber < minrun && runNumber > 1) || (runNumber > maxrun && maxrun > 0))
continue;
totallength++;
Double_t error;
if (relative && pull)
error = sqrt((sigma1 / rel) * (sigma1 / rel) + (sigma2 / rel) * (sigma2 / rel) +
(sigmaorg * x / (rel * rel)) * (sigmaorg * x / (rel * rel)));
else
error = sqrt(sigma1 * sigma1 + sigma2 * sigma2); // = 1 if axis == 'x' && !pull
// = sqrt(2) if axis == 'y' && !pull, so that you get the error in 1 track
// when you divide by it
x /= (rel * error);
if (!std::isfinite(x)) //e.g. in data with no pixels, the error occasionally comes out to be NaN
continue; //Filling a histogram with NaN is irrelevant, but here it would cause the whole result to be NaN
if (what == Minimum && x < result)
result = x;
if (what == Maximum && x > result)
result = x;
xvect.push_back(x);
if (var.BeginsWith("nHits")) {
x = xint2;
if (what == Minimum && x < result)
result = x;
if (what == Maximum && x > result)
result = x;
xvect.push_back(x);
}
}
delete f; //automatically closes the file
}
if (what == Minimum || what == Maximum)
return result;
sort(xvect.begin(), xvect.end());
for (unsigned int i = (unsigned int)(xvect.size() * (1 - outliercut) / 2);
i <= (unsigned int)(xvect.size() * (1 + outliercut) / 2 + .999);
i++, totallength++)
result += xvect[i];
result /= totallength;
if (what == RMS) {
double average = result;
result = 0;
for (unsigned int i = (unsigned int)(xvect.size() * (1 - outliercut) / 2);
i <= (unsigned int)(xvect.size() * (1 + outliercut) / 2 + .999);
i++)
result += (x - average) * (x - average);
result = sqrt(result / (totallength - 1));
}
return result;
}
Double_t findAverage(Int_t nFiles, TString *files, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(Average, nFiles, files, var, axis, relative, pull);
}
Double_t findMin(Int_t nFiles, TString *files, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(Minimum, nFiles, files, var, axis, relative, pull);
}
Double_t findMax(Int_t nFiles, TString *files, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(Maximum, nFiles, files, var, axis, relative, pull);
}
Double_t findRMS(Int_t nFiles, TString *files, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(RMS, nFiles, files, var, axis, relative, pull);
}
//These functions are for 1 file
Double_t findStatistic(Statistic what, TString file, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(what, 1, &file, var, axis, relative, pull);
}
Double_t findAverage(TString file, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(Average, file, var, axis, relative, pull);
}
Double_t findMin(TString file, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(Minimum, file, var, axis, relative, pull);
}
Double_t findMax(TString file, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(Maximum, file, var, axis, relative, pull);
}
Double_t findRMS(TString file, TString var, Char_t axis, Bool_t relative, Bool_t pull) {
return findStatistic(RMS, file, var, axis, relative, pull);
}
//This puts the axis limits that should be used for trackSplitPlot in min and max.
//Default axis limits are defined for pt, qoverpt, dxy, dz, theta, eta, and phi.
//For run number and nHits, the minimum and maximum are used.
//For any other variable, average +/- 5*rms are used.
//To use this instead of the default values, just comment out the part that says [else] if (var == "?") {min = ?; max = ?;}
void axislimits(Int_t nFiles,
TString *files,
TString var,
Char_t axis,
Bool_t relative,
Bool_t pull,
Double_t &min,
Double_t &max,
Double_t &bins) {
bool pixel = subdetector.Contains("PIX");
if (axis == 'x') {
if (var == "pt") {
min = 5;
max = 100;
bins = 38;
} else if (var == "qoverpt") {
min = -.35;
max = .35;
bins = 35;
} else if (var == "dxy") {
min = -100;
max = 100;
if (pixel) {
min = -10;
max = 10;
}
bins = 20;
} else if (var == "dz") {
min = -250;
max = 250;
if (pixel) {
min = -25;
max = 25;
}
bins = 25;
} else if (var == "theta") {
min = .5;
max = 2.5;
bins = 40;
} else if (var == "eta") {
min = -1.2;
max = 1.2;
bins = 40;
} else if (var == "phi") {
min = -3;
max = 0;
bins = 30;
} else if (var == "runNumber" || var.BeginsWith("nHits")) {
min = findMin(nFiles, files, var, 'x') - .5;
max = findMax(nFiles, files, var, 'x') + .5;
bins = max - min;
} else {
cout << "No x axis limits for " << var << ". Using average +/- 5*rms" << endl;
Double_t average = findAverage(nFiles, files, var, 'x');
Double_t rms = findRMS(nFiles, files, var, 'x');
max = TMath::Min(average + 5 * rms, findMax(nFiles, files, var, 'x'));
min = TMath::Max(average - 5 * rms, findMin(nFiles, files, var, 'x'));
bins = 50;
}
}
if (axis == 'y') {
if (pull) {
min = -5;
max = 5;
bins = 40;
} else if (var == "pt" && relative) {
min = -.06;
max = .06;
bins = 30;
} else if (var == "pt" && !relative) {
min = -.8;
max = .8;
bins = 40;
} else if (var == "qoverpt") {
min = -2.5;
max = 2.5;
bins = 50;
} else if (var == "dxy") {
min = -1250;
max = 1250;
if (pixel) {
min = -125;
max = 125;
}
bins = 50;
} else if (var == "dz") {
min = -2000;
max = 2000;
if (pixel) {
min = -200;
max = 200;
}
bins = 40;
} else if (var == "theta") {
min = -10;
max = 10;
if (pixel) {
min = -5;
max = 5;
}
bins = 50;
} else if (var == "eta") {
min = -.007;
max = .007;
if (pixel) {
min = -.003;
max = .003;
}
bins = 30;
} else if (var == "phi") {
min = -2;
max = 2;
bins = 40;
} else {
cout << "No y axis limits for " << var << ". Using average +/- 5 * rms." << endl;
Double_t average = 0 /*findAverage(nFiles,files,var,'y',relative,pull)*/;
Double_t rms = findRMS(nFiles, files, var, 'y', relative, pull);
min = TMath::Max(TMath::Max(-TMath::Abs(average) - 5 * rms, findMin(nFiles, files, var, 'y', relative, pull)),
-findMax(nFiles, files, var, 'y', relative, pull));
max = -min;
bins = 50;
}
}
}
//===============
//5. Place Legend
//===============
Double_t placeLegend(
TLegend *l, Double_t width, Double_t height, Double_t x1min, Double_t y1min, Double_t x2max, Double_t y2max) {
for (int i = legendGrid; i >= 0; i--) {
for (int j = legendGrid; j >= 0; j--) {
Double_t x1 = x1min * (1 - (double)i / legendGrid) + (x2max - width) * (double)i / legendGrid - margin * width;
Double_t y1 = y1min * (1 - (double)j / legendGrid) + (y2max - height) * (double)j / legendGrid - margin * height;
Double_t x2 = x1 + (1 + 2 * margin) * width;
Double_t y2 = y1 + (1 + 2 * margin) * height;
if (fitsHere(l, x1, y1, x2, y2)) {
x1 += margin * width;
y1 += margin * height;
x2 -= margin * width;
y2 -= margin * height;
l->SetX1(x1);
l->SetY1(y1);
l->SetX2(x2);
l->SetY2(y2);
return y2max;
}
}
}
Double_t newy2max = y2max + increaseby * (y2max - y1min);
Double_t newheight = height * (newy2max - y1min) / (y2max - y1min);
return placeLegend(l, width, newheight, x1min, y1min, x2max, newy2max);
}
Bool_t fitsHere(TLegend *l, Double_t x1, Double_t y1, Double_t x2, Double_t y2) {
Bool_t fits = true;
TList *list = l->GetListOfPrimitives();
for (Int_t k = 0; list->At(k) != nullptr && fits; k++) {
TObject *obj = ((TLegendEntry *)(list->At(k)))->GetObject();
if (obj == nullptr)
continue;
TClass *cl = obj->IsA();
//Histogram, drawn as a histogram
if (cl->InheritsFrom("TH1") && !cl->InheritsFrom("TH2") && !cl->InheritsFrom("TH3") && cl != TProfile::Class() &&
((TH1 *)obj)->GetMarkerColor() == kWhite) {
Int_t where = 0;
TH1 *h = (TH1 *)obj;
for (Int_t i = 1; i <= h->GetNbinsX() && fits; i++) {
if (h->GetBinLowEdge(i) + h->GetBinWidth(i) < x1)
continue; //to the left of the legend
if (h->GetBinLowEdge(i) > x2)
continue; //to the right of the legend
if (h->GetBinContent(i) > y1 && h->GetBinContent(i) < y2)
fits = false; //inside the legend
if (h->GetBinContent(i) < y1) {
if (where == 0)
where = -1; //below the legend
if (where == 1)
fits = false; //a previous bin was above it so there's a vertical line through it
}
if (h->GetBinContent(i) > y2) {
if (where == 0)
where = 1; //above the legend
if (where == -1)
fits = false; //a previous bin was below it so there's a vertical line through it
}
}
continue;
}
//Histogram, drawn with Draw("P")
else if (cl->InheritsFrom("TH1") && !cl->InheritsFrom("TH2") && !cl->InheritsFrom("TH3") && cl != TProfile::Class())
//Probably TProfile would be the same but I haven't tested it
{
TH1 *h = (TH1 *)obj;
for (Int_t i = 1; i <= h->GetNbinsX() && fits; i++) {
if (h->GetBinLowEdge(i) + h->GetBinWidth(i) / 2 < x1)
continue;
if (h->GetBinLowEdge(i) > x2)
continue;
if (h->GetBinContent(i) > y1 && h->GetBinContent(i) < y2)
fits = false;
if (h->GetBinContent(i) + h->GetBinError(i) > y2 && h->GetBinContent(i) - h->GetBinError(i) < y2)
fits = false;
if (h->GetBinContent(i) + h->GetBinError(i) > y1 && h->GetBinContent(i) - h->GetBinError(i) < y1)
fits = false;
}
} else if (cl->InheritsFrom("TF1") && !cl->InheritsFrom("TF2")) {
TF1 *f = (TF1 *)obj;
Double_t max = f->GetMaximum(x1, x2);
Double_t min = f->GetMinimum(x1, x2);
if (min < y2 && max > y1)
fits = false;
}
// else if (cl->InheritsFrom(...... add more objects here
else {
cout << "Don't know how to place the legend around objects of type " << obj->ClassName() << "." << endl
<< "Add this class into fitsHere() if you want it to work properly." << endl
<< "The legend will still be placed around any other objects." << endl;
}
}
return fits;
}
|