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
|
/*
* compare_hkl.c
*
* Compare reflection lists
*
* Copyright © 2012-2020 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
*
* Authors:
* 2010-2017 Thomas White <taw@physics.org>
* 2013 Lorenzo Galli <lorenzo.galli@desy.de>
*
* This file is part of CrystFEL.
*
* CrystFEL is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CrystFEL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CrystFEL. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <assert.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_statistics.h>
#include <gsl/gsl_fit.h>
#include <utils.h>
#include <symmetry.h>
#include <reflist-utils.h>
#include <cell-utils.h>
#include "version.h"
enum fom
{
FOM_R1I,
FOM_R1F,
FOM_R2,
FOM_RSPLIT,
FOM_CC,
FOM_CCSTAR,
FOM_CCANO,
FOM_CRDANO,
FOM_RANO,
FOM_RANORSPLIT,
FOM_D1SIG,
FOM_D2SIG
};
static enum fom get_fom(const char *s)
{
if ( strcasecmp(s, "r1i") == 0 ) return FOM_R1I;
if ( strcasecmp(s, "r1f") == 0 ) return FOM_R1F;
if ( strcasecmp(s, "r2") == 0 ) return FOM_R2;
if ( strcasecmp(s, "rsplit") == 0 ) return FOM_RSPLIT;
if ( strcasecmp(s, "cc") == 0 ) return FOM_CC;
if ( strcasecmp(s, "ccstar") == 0 ) return FOM_CCSTAR;
if ( strcasecmp(s, "ccano") == 0 ) return FOM_CCANO;
if ( strcasecmp(s, "crdano") == 0 ) return FOM_CRDANO;
if ( strcasecmp(s, "rano") == 0 ) return FOM_RANO;
if ( strcasecmp(s, "rano/rsplit") == 0 ) return FOM_RANORSPLIT;
if ( strcasecmp(s, "d1sig") == 0 ) return FOM_D1SIG;
if ( strcasecmp(s, "d2sig") == 0 ) return FOM_D2SIG;
ERROR("Unknown figure of merit '%s'.\n", s);
exit(1);
}
static void show_help(const char *s)
{
printf("Syntax: %s [options] <file1.hkl> <file2.hkl>\n\n", s);
printf(
"Compare intensity lists.\n"
"\n"
" -y, --symmetry=<sym> The symmetry of both the input files.\n"
" -p, --pdb=<filename> Unit cell file to use.\n"
" --fom=<FoM> Calculate this figure of merit Choose from:\n"
" R1I, R1F, R2, Rsplit, CC, CCstar,\n"
" CCano, CRDano, Rano, Rano/Rsplit, d1sig,\n"
" d2sig\n"
" --nshells=<n> Use <n> resolution shells.\n"
" -u Force scale factor to 1.\n"
" --shell-file=<file> Write resolution shells to <file>.\n"
"\n"
"You can control which reflections are included in the calculation:\n"
"\n"
" --ignore-negs Ignore reflections with negative intensities.\n"
" --zero-negs Set negative intensities to zero.\n"
" --sigma-cutoff=<n> Discard reflections with I/sigma(I) < n.\n"
" --rmin=<res> Low resolution cutoff (1/d in m^-1).\n"
" --rmax=<res> High resolution cutoff (1/d in m^-1).\n"
" --lowres=<n> Low resolution cutoff in (d in A).\n"
" --highres=<n> High resolution cutoff in (d in A).\n"
" --intensity-shells Use shells of intensity instead of resolution.\n"
"\n"
" -h, --help Display this help message.\n"
" --version Print CrystFEL version number and exit.\n"
);
}
struct fom_context
{
enum fom fom;
int nshells;
int *cts;
/* For R-factors */
double *num;
double *den;
/* For "double" R-factors */
double *num2;
double *den2;
/* For CCs */
double **vec1;
double **vec2;
int *n;
int nmax;
/* For "counting" things e.g. d1sig or d2sig */
int *n_within;
};
static struct fom_context *init_fom(enum fom fom, int nmax, int nshells)
{
struct fom_context *fctx;
int i;
fctx = malloc(sizeof(struct fom_context));
if ( fctx == NULL ) return NULL;
fctx->fom = fom;
fctx->nshells = nshells;
fctx->cts = malloc(nshells*sizeof(int));
for ( i=0; i<nshells; i++ ) {
fctx->cts[i] = 0;
}
switch ( fctx->fom ) {
case FOM_RANORSPLIT :
fctx->num2 = malloc(nshells*sizeof(double));
fctx->den2 = malloc(nshells*sizeof(double));
if ( (fctx->num2 == NULL) || (fctx->den2 == NULL) ) return NULL;
for ( i=0; i<nshells; i++ ) {
fctx->num2[i] = 0.0;
fctx->den2[i] = 0.0;
}
/* Intentional fall-through (no break) */
case FOM_R1I :
case FOM_R1F :
case FOM_R2 :
case FOM_RSPLIT :
case FOM_RANO :
fctx->num = malloc(nshells*sizeof(double));
fctx->den = malloc(nshells*sizeof(double));
if ( (fctx->num == NULL) || (fctx->den == NULL) ) return NULL;
for ( i=0; i<nshells; i++ ) {
fctx->num[i] = 0.0;
fctx->den[i] = 0.0;
}
break;
case FOM_CC :
case FOM_CCSTAR :
case FOM_CCANO :
case FOM_CRDANO :
fctx->vec1 = malloc(nshells*sizeof(double *));
fctx->vec2 = malloc(nshells*sizeof(double *));
if ( (fctx->vec1 == NULL) || (fctx->vec2 == NULL) ) return NULL;
for ( i=0; i<nshells; i++ ) {
fctx->vec1[i] = malloc(nmax*sizeof(double));
if ( fctx->vec1[i] == NULL ) return NULL;
fctx->vec2[i] = malloc(nmax*sizeof(double));
if ( fctx->vec2[i] == NULL ) return NULL;
fctx->n = malloc(nshells*sizeof(int));
if ( fctx->n == NULL ) return NULL;
}
for ( i=0; i<nshells; i++ ) {
fctx->n[i] = 0;
}
fctx->nmax = nmax;
break;
case FOM_D1SIG :
case FOM_D2SIG :
fctx->n_within = malloc(nshells*sizeof(int));
if ( fctx->n_within == NULL ) return NULL;
for ( i=0; i<nshells; i++ ) {
fctx->n_within[i] = 0;
}
break;
}
return fctx;
}
static void add_to_fom(struct fom_context *fctx, double i1, double i2,
double i1bij, double i2bij, double sig1, double sig2,
int bin)
{
double f1, f2;
double im, imbij;
fctx->cts[bin]++;
/* Negative intensities have already been weeded out. */
f1 = sqrt(i1);
f2 = sqrt(i2);
switch ( fctx->fom ) {
case FOM_R1I :
fctx->num[bin] += fabs(i1 - i2);
fctx->den[bin] += i1;
break;
case FOM_R1F :
fctx->num[bin] += fabs(f1 - f2);
fctx->den[bin] += f1;
break;
case FOM_R2 :
fctx->num[bin] += pow(i1 - i2, 2.0);
fctx->den[bin] += pow(i1, 2.0);
break;
case FOM_RSPLIT :
fctx->num[bin] += fabs(i1 - i2);
fctx->den[bin] += i1 + i2;
break;
case FOM_CC :
case FOM_CCSTAR :
assert(fctx->n[bin] < fctx->nmax);
fctx->vec1[bin][fctx->n[bin]] = i1;
fctx->vec2[bin][fctx->n[bin]] = i2;
fctx->n[bin]++;
break;
case FOM_CCANO :
case FOM_CRDANO :
assert(fctx->n[bin] < fctx->nmax);
fctx->vec1[bin][fctx->n[bin]] = i1 - i1bij;
fctx->vec2[bin][fctx->n[bin]] = i2 - i2bij;
fctx->n[bin]++;
break;
case FOM_RANORSPLIT :
fctx->num2[bin] += fabs(i1 - i2);
fctx->den2[bin] += i1 + i2;
/* Intentional fall-through (no break) */
case FOM_RANO :
im = (i1 + i2)/2.0;
imbij = (i1bij + i2bij)/2.0;
fctx->num[bin] += fabs(im - imbij);
fctx->den[bin] += im + imbij;
break;
case FOM_D1SIG :
if ( fabs(i1-i2) < sqrt(sig1*sig1 + sig2*sig2) ) {
fctx->n_within[bin]++;
}
break;
case FOM_D2SIG :
if ( fabs(i1-i2) < 2.0*sqrt(sig1*sig1 + sig2*sig2) ) {
fctx->n_within[bin]++;
}
break;
}
}
static double fom_overall(struct fom_context *fctx)
{
double overall_num = INFINITY;
double overall_den = 0.0;
double overall_num2 = INFINITY;
double overall_den2 = 0.0;
int i;
double *overall_vec1;
double *overall_vec2;
int overall_n;
double *overall_along_diagonal;
double *overall_perpend_diagonal;
double variance_signal;
double variance_error;
double cc = INFINITY;
switch ( fctx->fom ) {
case FOM_R1I :
case FOM_R1F :
case FOM_R2 :
case FOM_RSPLIT :
case FOM_RANO :
overall_num = 0.0;
overall_den = 0.0;
for ( i=0; i<fctx->nshells; i++ ) {
overall_num += fctx->num[i];
overall_den += fctx->den[i];
}
break;
case FOM_RANORSPLIT :
overall_num = 0.0;
overall_den = 0.0;
for ( i=0; i<fctx->nshells; i++ ) {
overall_num += fctx->num[i];
overall_den += fctx->den[i];
}
overall_num2 = 0.0;
overall_den2 = 0.0;
for ( i=0; i<fctx->nshells; i++ ) {
overall_num2 += fctx->num2[i];
overall_den2 += fctx->den2[i];
}
break;
case FOM_CC :
case FOM_CCSTAR :
case FOM_CCANO :
overall_vec1 = malloc(fctx->nmax*sizeof(double));
overall_vec2 = malloc(fctx->nmax*sizeof(double));
overall_n = 0;
for ( i=0; i<fctx->nshells; i++ ) {
int j;
for ( j=0; j<fctx->n[i]; j++ ) {
overall_vec1[overall_n] = fctx->vec1[i][j];
overall_vec2[overall_n] = fctx->vec2[i][j];
overall_n++;
}
}
cc = gsl_stats_correlation(overall_vec1, 1, overall_vec2, 1,
overall_n);
free(overall_vec1);
free(overall_vec2);
break;
case FOM_CRDANO :
overall_along_diagonal = malloc(fctx->nmax*sizeof(double));
overall_perpend_diagonal = malloc(fctx->nmax*sizeof(double));
overall_n = 0;
for ( i=0; i<fctx->nshells; i++ ) {
int j;
for ( j=0; j<fctx->n[i]; j++ ) {
overall_along_diagonal[overall_n] =
( fctx->vec1[i][j] + fctx->vec2[i][j] )
/ sqrt(2.0);
overall_perpend_diagonal[overall_n] =
( fctx->vec1[i][j] - fctx->vec2[i][j] )
/ sqrt(2.0);
overall_n++;
}
}
variance_signal = gsl_stats_variance_m(overall_along_diagonal,
1, overall_n, 0.0);
variance_error = gsl_stats_variance_m(overall_perpend_diagonal,
1, overall_n, 0.0);
cc = sqrt(variance_signal / variance_error );
free(overall_along_diagonal);
free(overall_perpend_diagonal);
break;
case FOM_D1SIG :
case FOM_D2SIG :
overall_num = 0.0;
overall_den = 0.0;
for ( i=0; i<fctx->nshells; i++ ) {
overall_num += fctx->n_within[i];
overall_den += fctx->cts[i];
}
break;
}
switch ( fctx->fom ) {
case FOM_R1I :
case FOM_R1F :
return overall_num/overall_den;
case FOM_R2 :
return sqrt(overall_num/overall_den);
case FOM_RSPLIT :
return 2.0*(overall_num/overall_den) / sqrt(2.0);
case FOM_CC :
case FOM_CCANO :
case FOM_CRDANO :
return cc;
case FOM_CCSTAR :
return sqrt((2.0*cc)/(1.0+cc));
case FOM_RANO :
return 2.0*(overall_num/overall_den);
case FOM_RANORSPLIT :
return (2.0*(overall_num/overall_den)) /
(2.0*(overall_num2/overall_den2) / sqrt(2.0));
case FOM_D1SIG :
case FOM_D2SIG :
return overall_num/overall_den;
}
ERROR("This point is never reached.\n");
abort();
}
static double fom_shell(struct fom_context *fctx, int i)
{
double cc;
int j;
double variance_signal;
double variance_error;
double *along_diagonal;
double *perpend_diagonal;
switch ( fctx->fom ) {
case FOM_R1I :
case FOM_R1F :
return fctx->num[i]/fctx->den[i];
case FOM_R2 :
return sqrt(fctx->num[i]/fctx->den[i]);
case FOM_RSPLIT :
return 2.0*(fctx->num[i]/fctx->den[i]) / sqrt(2.0);
case FOM_CC :
case FOM_CCANO :
return gsl_stats_correlation(fctx->vec1[i], 1, fctx->vec2[i], 1,
fctx->n[i]);
case FOM_CCSTAR :
cc = gsl_stats_correlation(fctx->vec1[i], 1, fctx->vec2[i], 1,
fctx->n[i]);
return sqrt((2.0*cc)/(1.0+cc));
case FOM_RANO :
return 2.0 * fctx->num[i]/fctx->den[i];
case FOM_RANORSPLIT :
return (2.0*fctx->num[i]/fctx->den[i]) /
(2.0*(fctx->num2[i]/fctx->den2[i]) / sqrt(2.0));
case FOM_CRDANO :
along_diagonal = malloc(fctx->n[i] * sizeof(double));
perpend_diagonal = malloc(fctx->n[i] * sizeof(double));
for ( j=0; j<fctx->n[i]; j++ ) {
along_diagonal[j] = ( fctx->vec1[i][j] +
fctx->vec2[i][j] ) / sqrt(2.0);
perpend_diagonal[j] = ( fctx->vec1[i][j] -
fctx->vec2[i][j] ) / sqrt(2.0);
}
variance_signal = gsl_stats_variance_m(along_diagonal, 1,
fctx->n[i], 0.0);
variance_error = gsl_stats_variance_m(perpend_diagonal, 1,
fctx->n[i], 0.0);
free(along_diagonal);
free(perpend_diagonal);
return sqrt(variance_signal / variance_error);
case FOM_D1SIG :
case FOM_D2SIG :
return (double)fctx->n_within[i] / fctx->cts[i];
}
ERROR("This point is never reached.\n");
abort();
}
struct shells
{
int config_intshells;
int nshells;
double *rmins;
double *rmaxs;
};
static struct shells *set_intensity_shells(double min_I, double max_I,
int nshells)
{
struct shells *s;
int i;
if ( min_I >= max_I ) {
ERROR("Invalid intensity range.\n");
return NULL;
}
/* Adjust minimum and maximum intensities to get the most densely
* populated part of the reflections */
max_I = min_I + (max_I-min_I)/5000.0;
s = malloc(sizeof(struct shells));
if ( s == NULL ) return NULL;
s->rmins = malloc(nshells*sizeof(double));
s->rmaxs = malloc(nshells*sizeof(double));
if ( (s->rmins==NULL) || (s->rmaxs==NULL) ) {
ERROR("Couldn't allocate memory for shells.\n");
free(s);
return NULL;
}
s->config_intshells = 1;
s->nshells = nshells;
for ( i=0; i<nshells; i++ ) {
s->rmins[i] = min_I + i*(max_I - min_I)/nshells;;
s->rmaxs[i] = min_I + (i+1)*(max_I - min_I)/nshells;;
}
return s;
}
static struct shells *set_resolution_shells(double rmin, double rmax,
int nshells)
{
struct shells *s;
double total_vol, vol_per_shell;
int i;
s = malloc(sizeof(struct shells));
if ( s == NULL ) return NULL;
s->rmins = malloc(nshells*sizeof(double));
s->rmaxs = malloc(nshells*sizeof(double));
if ( (s->rmins==NULL) || (s->rmaxs==NULL) ) {
ERROR("Couldn't allocate memory for resolution shells.\n");
free(s);
return NULL;
}
s->config_intshells = 0;
s->nshells = nshells;
total_vol = pow(rmax, 3.0) - pow(rmin, 3.0);
vol_per_shell = total_vol / nshells;
s->rmins[0] = rmin;
for ( i=1; i<nshells; i++ ) {
double r;
r = vol_per_shell + pow(s->rmins[i-1], 3.0);
r = pow(r, 1.0/3.0);
/* Shells of constant volume */
s->rmaxs[i-1] = r;
s->rmins[i] = r;
}
s->rmaxs[nshells-1] = rmax;
return s;
}
static double shell_label(struct shells *s, int i)
{
if ( s->config_intshells ) {
return (i+0.5) / s->nshells;
} else {
return s->rmins[i] + (s->rmaxs[i] - s->rmins[i])/2.0;
}
}
static int get_bin(struct shells *s, Reflection *refl, UnitCell *cell)
{
if ( s->config_intshells ) {
double intensity;
int bin, j;
intensity = get_intensity(refl);
bin = -1;
for ( j=0; j<s->nshells; j++ ) {
if ( (intensity>s->rmins[j])
&& (intensity<=s->rmaxs[j]) )
{
bin = j;
break;
}
}
return bin;
} else {
double d;
int bin, j;
signed int h, k, l;
get_indices(refl, &h, &k, &l);
d = 2.0 * resolution(cell, h, k, l);
bin = -1;
for ( j=0; j<s->nshells; j++ ) {
if ( (d>s->rmins[j]) && (d<=s->rmaxs[j]) ) {
bin = j;
break;
}
}
/* Allow for slight rounding errors */
if ( (bin == -1) && (d <= s->rmins[0]) ) bin = 0;
if ( (bin == -1) && (d >= s->rmaxs[s->nshells-1]) ) bin = 0;
assert(bin != -1);
return bin;
}
}
static int wilson_scale(RefList *list1, RefList *list2, UnitCell *cell)
{
Reflection *refl1;
Reflection *refl2;
RefListIterator *iter;
int max_n = 256;
int n = 0;
double *x;
double *y;
int r;
double G, B;
double c0, c1, cov00, cov01, cov11, chisq;
x = malloc(max_n*sizeof(double));
y = malloc(max_n*sizeof(double));
if ( (x==NULL) || (y==NULL) ) {
ERROR("Failed to allocate memory for scaling.\n");
return 1;
}
for ( refl1 = first_refl(list1, &iter);
refl1 != NULL;
refl1 = next_refl(refl1, iter) )
{
signed int h, k, l;
double Ih1, Ih2;
double res;
get_indices(refl1, &h, &k, &l);
res = resolution(cell, h, k, l);
refl2 = find_refl(list2, h, k, l);
assert(refl2 != NULL);
Ih1 = get_intensity(refl1);
Ih2 = get_intensity(refl2);
if ( (Ih1 <= 0.0) || (Ih2 <= 0.0) ) continue;
if ( isnan(Ih1) || isinf(Ih1) ) continue;
if ( isnan(Ih2) || isinf(Ih2) ) continue;
if ( n == max_n ) {
max_n *= 2;
x = realloc(x, max_n*sizeof(double));
y = realloc(y, max_n*sizeof(double));
if ( (x==NULL) || (y==NULL) ) {
ERROR("Failed to allocate memory for scaling.\n");
return 1;
}
}
x[n] = res*res;
y[n] = log(Ih1/Ih2);
n++;
}
if ( n < 2 ) {
ERROR("Not enough reflections for scaling\n");
return 1;
}
r = gsl_fit_linear(x, 1, y, 1, n, &c0, &c1,
&cov00, &cov01, &cov11, &chisq);
if ( r ) {
ERROR("Scaling failed.\n");
return 1;
}
G = exp(c0);
B = c1/2.0;
STATUS("Relative scale factor = %f, relative B factor = %f A^2\n",
G, B*1e20);
STATUS("A scale factor greater than 1 means that the second reflection "
"list is weaker than the first.\n");
STATUS("A positive relative B factor means that the second reflection "
"list falls off with resolution more quickly than the first.\n");
free(x);
free(y);
/* Apply the scaling factor */
for ( refl2 = first_refl(list2, &iter);
refl2 != NULL;
refl2 = next_refl(refl2, iter) )
{
signed int h, k, l;
double res;
double corr;
get_indices(refl2, &h, &k, &l);
res = resolution(cell, h, k, l);
corr = G * exp(2.0*B*res*res);
set_intensity(refl2, get_intensity(refl2)*corr);
set_esd_intensity(refl2, get_esd_intensity(refl2)*corr);
}
return 0;
}
static void do_fom(RefList *list1, RefList *list2, UnitCell *cell,
double rmin, double rmax, enum fom fom,
int config_unity, int nshells, const char *filename,
int config_intshells, double min_I, double max_I,
SymOpList *sym)
{
int i;
Reflection *refl1;
RefListIterator *iter;
FILE *fh;
struct fom_context *fctx;
struct shells *shells;
const char *t1, *t2;
int n_out;
fctx = init_fom(fom, num_reflections(list1), nshells);
if ( fctx==NULL ) {
ERROR("Couldn't allocate memory for resolution shells.\n");
return;
}
if ( !config_unity && wilson_scale(list1, list2, cell) ) {
ERROR("Error with scaling.\n");
return;
}
/* Calculate the bins */
if ( config_intshells ) {
shells = set_intensity_shells(min_I, max_I, nshells);
} else {
shells = set_resolution_shells(rmin, rmax, nshells);
}
if ( shells == NULL ) {
ERROR("Failed to set up shells.\n");
return;
}
for ( refl1 = first_refl(list1, &iter);
refl1 != NULL;
refl1 = next_refl(refl1, iter) )
{
Reflection *refl2;
signed int h, k, l;
set_flag(refl1, 0);
get_indices(refl1, &h, &k, &l);
refl2 = find_refl(list2, h, k, l);
assert(refl2 != NULL);
set_flag(refl2, 0);
}
n_out = 0;
for ( refl1 = first_refl(list1, &iter);
refl1 != NULL;
refl1 = next_refl(refl1, iter) )
{
signed int h, k, l;
int bin;
double i1, i2;
double i1bij, i2bij;
double sig1, sig2;
Reflection *refl2;
get_indices(refl1, &h, &k, &l);
refl2 = find_refl(list2, h, k, l);
if ( refl2 == NULL ) continue;
bin = get_bin(shells, refl1, cell);
if ( bin == -1 ) {
n_out++;
continue;
}
i1 = get_intensity(refl1);
i2 = get_intensity(refl2);
sig1 = get_esd_intensity(refl1);
sig2 = get_esd_intensity(refl2);
if ( (fom == FOM_CCANO) || (fom == FOM_CRDANO)
|| (fom == FOM_RANO) || (fom == FOM_RANORSPLIT) )
{
Reflection *refl1_bij = NULL;
Reflection *refl2_bij = NULL;
signed int hb, kb, lb;
if ( find_equiv_in_list(list1, -h, -k, -l, sym,
&hb, &kb, &lb) )
{
refl1_bij = find_refl(list1, hb, kb, lb);
}
if ( find_equiv_in_list(list2, -h, -k, -l, sym,
&hb, &kb, &lb) )
{
refl2_bij = find_refl(list2, hb, kb, lb);
}
/* Each reflection must only be counted once, whether
* we are visiting it now as "normal" or "bij" */
if ( get_flag(refl1) ) continue;
assert(!get_flag(refl2));
set_flag(refl1, 1);
set_flag(refl1_bij, 1);
set_flag(refl2, 1);
set_flag(refl2_bij, 1);
assert(refl1_bij != NULL);
assert(refl2_bij != NULL);
i1bij = get_intensity(refl1_bij);
i2bij = get_intensity(refl2_bij);
} else {
/* Make it obvious if these get used by mistake */
i1bij = +INFINITY;
i2bij = +INFINITY;
}
add_to_fom(fctx, i1, i2, i1bij, i2bij, sig1, sig2, bin);
}
if ( n_out) {
ERROR("WARNING: %i reflection pairs outside range.\n", n_out);
}
switch ( fom ) {
case FOM_R1I :
STATUS("Overall R1(I) = %.2f %%\n", 100.0*fom_overall(fctx));
break;
case FOM_R1F :
STATUS("Overall R1(F) = %.2f %%\n", 100.0*fom_overall(fctx));
break;
case FOM_R2 :
STATUS("Overall R(2) = %.2f %%\n", 100.0*fom_overall(fctx));
break;
case FOM_RSPLIT :
STATUS("Overall Rsplit = %.2f %%\n", 100.0*fom_overall(fctx));
break;
case FOM_CC :
STATUS("Overall CC = %.7f\n", fom_overall(fctx));
break;
case FOM_CCSTAR :
STATUS("Overall CC* = %.7f\n", fom_overall(fctx));
break;
case FOM_CCANO :
STATUS("Overall CCano = %.7f\n", fom_overall(fctx));
break;
case FOM_CRDANO :
STATUS("Overall CRDano = %.7f\n", fom_overall(fctx));
break;
case FOM_RANO :
STATUS("Overall Rano = %.2f %%\n", 100.0*fom_overall(fctx));
break;
case FOM_RANORSPLIT :
STATUS("Overall Rano/Rsplit = %.7f\n", fom_overall(fctx));
break;
case FOM_D1SIG :
STATUS("Fraction of differences less than 1 sigma = %.7f %%\n",
100.0*fom_overall(fctx));
break;
case FOM_D2SIG :
STATUS("Fraction of differences less than 2 sigma = %.7f %%\n",
100.0*fom_overall(fctx));
break;
}
fh = fopen(filename, "w");
if ( fh == NULL ) {
ERROR("Couldn't open '%s'\n", filename);
return;
}
if ( config_intshells ) {
t1 = "Relative I ";
t2 = "";
} else {
t1 = " 1/d centre";
t2 = " d / A Min 1/nm Max 1/nm";
}
switch ( fom ) {
case FOM_R1I :
fprintf(fh, "%s R1(I)/%% nref%s\n", t1, t2);
break;
case FOM_R1F :
fprintf(fh, "%s R1(F)/%% nref%s\n", t1, t2);
break;
case FOM_R2 :
fprintf(fh, "%s R2/%% nref%s\n", t1, t2);
break;
case FOM_RSPLIT :
fprintf(fh, "%s Rsplit/%% nref%s\n", t1, t2);
break;
case FOM_CC :
fprintf(fh, "%s CC nref%s\n", t1, t2);
break;
case FOM_CCSTAR :
fprintf(fh, "%s CC* nref%s\n", t1, t2);
break;
case FOM_CCANO :
fprintf(fh, "%s CCano nref%s\n", t1, t2);
break;
case FOM_CRDANO :
fprintf(fh, "%s CRDano nref%s\n", t1, t2);
break;
case FOM_RANO :
fprintf(fh, "%s Rano/%% nref%s\n", t1, t2);
break;
case FOM_RANORSPLIT :
fprintf(fh, "%s Rano/Rsplit nref%s\n", t1, t2);
break;
case FOM_D1SIG :
fprintf(fh, "%s D<1sigma/%% nref%s\n", t1, t2);
break;
case FOM_D2SIG :
fprintf(fh, "%s D<2sigma/%% nref%s\n", t1, t2);
break;
}
for ( i=0; i<nshells; i++ ) {
double r, cen;
cen = shell_label(shells, i);
r = fom_shell(fctx, i);
switch ( fom ) {
case FOM_R1I :
case FOM_R1F :
case FOM_R2 :
case FOM_RSPLIT :
case FOM_RANO :
if ( config_intshells ) {
fprintf(fh, "%10.3f %10.2f %10i\n",
cen, r*100.0, fctx->cts[i]);
} else {
fprintf(fh, "%10.3f %10.2f %10i %10.2f "
"%10.3f %10.3f\n",
cen*1.0e-9, r*100.0, fctx->cts[i],
(1.0/cen)*1e10,
shells->rmins[i]*1.0e-9,
shells->rmaxs[i]*1.0e-9);
}
break;
case FOM_CC :
case FOM_CCSTAR :
case FOM_CCANO :
case FOM_CRDANO :
if ( config_intshells ) {
fprintf(fh, "%10.3f %10.7f %10i\n",
cen, r, fctx->cts[i]);
} else {
fprintf(fh, "%10.3f %10.7f %10i %10.2f "
"%10.3f %10.3f\n",
cen*1.0e-9, r, fctx->cts[i], (1.0/cen)*1e10,
shells->rmins[i]*1.0e-9,
shells->rmaxs[i]*1.0e-9);
}
break;
case FOM_RANORSPLIT :
if ( config_intshells ) {
fprintf(fh, "%10.3f %10.7f %10i\n",
cen, r, fctx->cts[i]);
} else {
fprintf(fh, "%10.3f %10.7f %10i %10.2f "
"%10.3f %10.3f\n",
cen*1.0e-9, r, fctx->cts[i], (1.0/cen)*1e10,
shells->rmins[i]*1.0e-9,
shells->rmaxs[i]*1.0e-9);
}
break;
case FOM_D1SIG :
case FOM_D2SIG :
if ( config_intshells ) {
fprintf(fh, "%10.3f %10.2f %10i\n",
cen, r*100.0, fctx->cts[i]);
} else {
fprintf(fh, "%10.3f %10.2f %10i %10.2f "
"%10.3f %10.3f\n",
cen*1.0e-9, r*100.0, fctx->cts[i],
(1.0/cen)*1e10,
shells->rmins[i]*1.0e-9,
shells->rmaxs[i]*1.0e-9);
}
break;
}
}
fclose(fh);
}
static void check_highres()
{
static int have = 0;
if ( have ) {
ERROR("You cannot use --rmax and --highres at the same time.\n");
exit(1);
}
have = 1;
}
static void check_lowres()
{
static int have = 0;
if ( have ) {
ERROR("You cannot use --rmin and --lowres at the same time.\n");
exit(1);
}
have = 1;
}
int main(int argc, char *argv[])
{
int c;
UnitCell *cell;
char *afile = NULL;
char *bfile = NULL;
char *sym_str = NULL;
char *sym_str_fromfile = NULL;
char *sym_str_fromfile1 = NULL;
char *sym_str_fromfile2 = NULL;
SymOpList *sym;
int ncom, nrej, nmul, nneg, nres, nbij, ncen;
RefList *list1_acc;
RefList *list2_acc;
RefList *list1;
RefList *list2;
RefList *list1_raw;
RefList *list2_raw;
enum fom fom = FOM_R1I;
char *cellfile = NULL;
float rmin_fix = -1.0;
float rmax_fix = -1.0;
double rmin, rmax;
Reflection *refl1;
RefListIterator *iter;
float sigma_cutoff = -INFINITY;
int config_ignorenegs = 0;
int config_zeronegs = 0;
int config_unity = 0;
int config_intshells = 0;
int nshells = 10;
char *shell_file = NULL;
double min_I = +INFINITY;
double max_I = -INFINITY;
float highres, lowres;
int mul_cutoff = 0;
/* Long options */
const struct option longopts[] = {
{"help", 0, NULL, 'h'},
{"version", 0, NULL, 10 },
{"symmetry", 1, NULL, 'y'},
{"pdb", 1, NULL, 'p'},
{"rmin", 1, NULL, 2},
{"rmax", 1, NULL, 3},
{"fom", 1, NULL, 4},
{"sigma-cutoff", 1, NULL, 5},
{"nshells", 1, NULL, 6},
{"shell-file", 1, NULL, 7},
{"highres", 1, NULL, 8},
{"lowres", 1, NULL, 9},
{"min-measurements", 1, NULL, 11},
{"ignore-negs", 0, &config_ignorenegs, 1},
{"zero-negs", 0, &config_zeronegs, 1},
{"intensity-shells", 0, &config_intshells, 1},
{0, 0, NULL, 0}
};
/* Short options */
while ((c = getopt_long(argc, argv, "hy:p:u",
longopts, NULL)) != -1)
{
switch (c) {
case 'h' :
show_help(argv[0]);
return 0;
case 10 :
printf("CrystFEL: %s\n",
crystfel_version_string());
printf("%s\n",
crystfel_licence_string());
return 0;
case 'y' :
sym_str = strdup(optarg);
break;
case 'p' :
cellfile = strdup(optarg);
break;
case 'u' :
config_unity = 1;
break;
case 0 :
break;
case 2 :
check_lowres();
if ( sscanf(optarg, "%e", &rmin_fix) != 1 ) {
ERROR("Invalid value for --rmin\n");
return 1;
}
break;
case 3 :
check_highres();
if ( sscanf(optarg, "%e", &rmax_fix) != 1 ) {
ERROR("Invalid value for --rmax\n");
return 1;
}
break;
case 4 :
fom = get_fom(optarg);
break;
case 5 :
if ( sscanf(optarg, "%f", &sigma_cutoff) != 1 ) {
ERROR("Invalid value for --sigma-cutoff\n");
return 1;
}
STATUS("WARNING: You are using --sigma-cutoff. "
"Be aware that the figures of merit will not "
"reflect the entire data set!\n");
break;
case 6 :
if ( sscanf(optarg, "%i", &nshells) != 1 ) {
ERROR("Invalid value for --nshells\n");
return 1;
}
break;
case 7 :
shell_file = strdup(optarg);
break;
case 8 :
check_highres();
if ( sscanf(optarg, "%e", &highres) != 1 ) {
ERROR("Invalid value for --highres\n");
return 1;
}
rmax_fix = 1.0 / (highres/1e10);
break;
case 9 :
check_lowres();
if ( sscanf(optarg, "%e", &lowres) != 1 ) {
ERROR("Invalid value for --lowres\n");
return 1;
}
rmin_fix = 1.0 / (lowres/1e10);
break;
case 11 :
if ( sscanf(optarg, "%i", &mul_cutoff) != 1 ) {
ERROR("Invalid value for --min-measurements\n");
return 1;
}
break;
case '?' :
break;
default :
ERROR("Unhandled option '%c'\n", c);
break;
}
}
if ( argc != (optind+2) ) {
ERROR("Please provide exactly two HKL files to compare.\n");
return 1;
}
if ( !config_ignorenegs && !config_zeronegs ) {
switch ( fom )
{
case FOM_R1F :
ERROR("Your chosen figure of merit involves converting"
" intensities to structure factors, but you have"
" not specified how to handle negative"
" intensities.\n");
ERROR("Please try again with --ignore-negs or"
" --zero-negs.\n");
exit(1);
case FOM_R2 :
case FOM_R1I :
case FOM_RSPLIT :
case FOM_CC :
case FOM_CCSTAR :
case FOM_CCANO :
case FOM_CRDANO :
case FOM_RANO :
case FOM_RANORSPLIT :
case FOM_D1SIG :
case FOM_D2SIG :
break;
}
}
if ( (fom != FOM_R1F) && (config_ignorenegs || config_zeronegs) ) {
ERROR("WARNING: You are using --zero-negs or --ignore-negs "
"even though your chosen figure of merit does not "
"require it.\n");
ERROR("The figures of merit will not reflect the entire data "
"set!\n");
}
afile = strdup(argv[optind++]);
bfile = strdup(argv[optind]);
if ( shell_file == NULL ) shell_file = strdup("shells.dat");
cell = load_cell_from_file(cellfile);
if ( cellfile == NULL ) {
ERROR("You must provide a unit cell.\n");
exit(1);
}
if ( cell == NULL ) {
ERROR("Failed to load cell.\n");
return 1;
}
free(cellfile);
list1_raw = read_reflections_2(afile, &sym_str_fromfile1);
if ( list1_raw == NULL ) {
ERROR("Couldn't read file '%s'\n", afile);
return 1;
}
list2_raw = read_reflections_2(bfile, &sym_str_fromfile2);
if ( list2_raw == NULL ) {
ERROR("Couldn't read file '%s'\n", bfile);
return 1;
}
if ( (sym_str_fromfile1 != NULL) && (sym_str_fromfile2 != NULL) ) {
if ( strcmp(sym_str_fromfile1, sym_str_fromfile2) != 0 ) {
ERROR("The symmetries of the two list do not match:\n");
ERROR(" %s: %s\n", afile, sym_str_fromfile1);
ERROR(" %s: %s\n", bfile, sym_str_fromfile2);
return 1;
}
sym_str_fromfile = sym_str_fromfile1;
free(sym_str_fromfile2);
}
if ( sym_str == NULL ) {
if ( sym_str_fromfile != NULL ) {
STATUS("Using symmetry from reflection files: %s\n",
sym_str_fromfile);
sym_str = sym_str_fromfile;
} else {
sym_str = strdup("1");
}
}
sym = get_pointgroup(sym_str);
free(sym_str);
if ( is_centrosymmetric(sym) ) {
switch ( fom )
{
case FOM_R1F :
case FOM_R2 :
case FOM_R1I :
case FOM_RSPLIT :
case FOM_CC :
case FOM_CCSTAR :
case FOM_D1SIG :
case FOM_D2SIG :
break;
case FOM_CCANO :
case FOM_CRDANO :
case FOM_RANO :
case FOM_RANORSPLIT :
ERROR("You are trying to measure an anomalous signal in"
" a centrosymmetric point group.\n");
ERROR("This is a silly thing to do, and I'm refusing to"
" help you do it.\n");
ERROR("Please review your earlier processing steps and"
" try again using a non-centrosymmetric point"
" group for '-y'.\n");
return 1;
}
}
/* Check that the intensities have the correct symmetry */
if ( check_list_symmetry(list1_raw, sym) ) {
ERROR("The first input reflection list does not appear to"
" have symmetry %s\n", symmetry_name(sym));
if ( cell_get_lattice_type(cell) == L_MONOCLINIC ) {
ERROR("You may need to specify the unique axis in your "
"point group. The default is unique axis c.\n");
ERROR("See 'man crystfel' for more details.\n");
}
return 1;
}
if ( check_list_symmetry(list2_raw, sym) ) {
ERROR("The second input reflection list does not appear to"
" have symmetry %s\n", symmetry_name(sym));
if ( cell_get_lattice_type(cell) == L_MONOCLINIC ) {
ERROR("You may need to specify the unique axis in your "
"point group. The default is unique axis c.\n");
ERROR("See 'man crystfel' for more details.\n");
}
return 1;
}
resolution_limits(list1_raw, cell, &rmin, &rmax);
STATUS("%s: %i reflections, resolution range %.2f to %.2f Angstroms"
" (%.5f to %.5f nm^-1).\n", afile,
num_reflections(list1_raw),
1e10/rmin, 1e10/rmax, rmin/1e9, rmax/1e9);
resolution_limits(list2_raw, cell, &rmin, &rmax);
STATUS("%s: %i reflections, resolution range %.2f to %.2f Angstroms"
" (%.5f to %.5f nm^-1).\n", bfile,
num_reflections(list2_raw),
1e10/rmin, 1e10/rmax, rmin/1e9, rmax/1e9);
list1 = asymmetric_indices(list1_raw, sym);
list2 = asymmetric_indices(list2_raw, sym);
reflist_free(list1_raw);
reflist_free(list2_raw);
/* Select reflections to be used */
ncom = 0;
nrej = 0;
nmul = 0;
nneg = 0;
nres = 0;
nbij = 0;
ncen = 0;
list1_acc = reflist_new();
list2_acc = reflist_new();
for ( refl1 = first_refl(list1, &iter);
refl1 != NULL;
refl1 = next_refl(refl1, iter) )
{
signed int h, k, l;
double val1, val2;
double esd1, esd2;
int mul1, mul2;
Reflection *refl2;
Reflection *refl1_acc;
Reflection *refl2_acc;
get_indices(refl1, &h, &k, &l);
refl2 = find_refl(list2, h, k, l);
if ( refl2 == NULL ) continue;
val1 = get_intensity(refl1);
val2 = get_intensity(refl2);
esd1 = get_esd_intensity(refl1);
esd2 = get_esd_intensity(refl2);
mul1 = get_redundancy(refl1);
mul2 = get_redundancy(refl2);
if ( (val1 < sigma_cutoff * esd1)
|| (val2 < sigma_cutoff * esd2) )
{
nrej++;
continue;
}
if ( config_ignorenegs && ((val1 < 0.0) || (val2 < 0.0)) ) {
nneg++;
continue;
}
if ( (mul1 < mul_cutoff) || (mul2 < mul_cutoff) ) {
nmul++;
continue;
}
if ( config_zeronegs ) {
int d = 0;
if ( val1 < 0.0 ) {
val1 = 0.0;
d = 1;
}
if ( val2 < 0.0 ) {
val2 = 0.0;
d = 1;
}
if ( d ) nneg++;
}
if ( rmin_fix > 0.0 ) {
double res = 2.0*resolution(cell, h, k, l);
if ( res < rmin_fix ) {
nres++;
continue;
}
}
if ( rmax_fix > 0.0 ) {
double res = 2.0*resolution(cell, h, k, l);
if ( res > rmax_fix ) {
nres++;
continue;
}
}
refl1_acc = add_refl(list1_acc, h, k, l);
copy_data(refl1_acc, refl1);
set_intensity(refl1_acc, val1);
refl2_acc = add_refl(list2_acc, h, k, l);
copy_data(refl2_acc, refl2);
set_intensity(refl2_acc, val2);
if ( val1 > max_I ) max_I = val1;
if ( val1 < min_I ) min_I = val1;
ncom++;
}
reflist_free(list1);
reflist_free(list2);
/* For anomalous figures of merit, we additionally require that we have
* all the Bijvoet pairs after the above rejection tests */
if ( (fom == FOM_CCANO) || (fom == FOM_CRDANO)
|| (fom == FOM_RANO) || (fom == FOM_RANORSPLIT) )
{
list1 = list1_acc;
list2 = list2_acc;
list1_acc = reflist_new();
list2_acc = reflist_new();
min_I = +INFINITY;
max_I = -INFINITY;
ncom = 0;
for ( refl1 = first_refl(list1, &iter);
refl1 != NULL;
refl1 = next_refl(refl1, iter) )
{
Reflection *refl1_bij = NULL;
Reflection *refl2_bij = NULL;
signed int h, k, l;
signed int hb, kb, lb;
Reflection *refl1_acc;
Reflection *refl2_acc;
Reflection *refl2;
double val1, val2;
get_indices(refl1, &h, &k, &l);
refl2 = find_refl(list2, h, k, l);
assert(refl2 != NULL);
val1 = get_intensity(refl1);
val2 = get_intensity(refl2);
if ( is_centric(h, k, l, sym) ) {
ncen++;
continue;
}
if ( find_equiv_in_list(list1, -h, -k, -l, sym,
&hb, &kb, &lb) )
{
refl1_bij = find_refl(list1, hb, kb, lb);
}
if ( find_equiv_in_list(list2, -h, -k, -l, sym,
&hb, &kb, &lb) )
{
refl2_bij = find_refl(list2, hb, kb, lb);
}
if ( (refl1_bij == NULL) || (refl2_bij == NULL) ) {
nbij++;
continue;
}
refl1_acc = add_refl(list1_acc, h, k, l);
copy_data(refl1_acc, refl1);
set_intensity(refl1_acc, val1);
refl2_acc = add_refl(list2_acc, h, k, l);
copy_data(refl2_acc, refl2);
set_intensity(refl2_acc, val2);
if ( val1 > max_I ) max_I = val1;
if ( val1 < min_I ) min_I = val1;
ncom++;
}
}
gsl_set_error_handler_off();
if ( nrej > 0 ) {
STATUS("Discarded %i reflection pairs because either or both"
" versions had I/sigma(I) < %f.\n", nrej, sigma_cutoff);
}
if ( config_ignorenegs && (nneg > 0) ) {
STATUS("Discarded %i reflection pairs because either or both"
" versions had negative intensities.\n", nneg);
}
if ( config_zeronegs && (nneg > 0) ) {
STATUS("For %i reflection pairs, either or both versions had"
" negative intensities which were set to zero.\n", nneg);
}
if ( nmul > 0 ) {
STATUS("%i reflection pairs rejected because either or both"
" versions had too few measurements.\n", nmul);
}
if ( nres > 0 ) {
STATUS("%i reflection pairs rejected because either or both"
" versions were outside the resolution range.\n", nres);
}
if ( nbij > 0 ) {
STATUS("%i reflection pairs rejected because either or both"
" versions did not have Bijvoet partners.\n", nres);
}
if ( ncen > 0 ) {
STATUS("%i reflection pairs rejected because they were"
" centric.\n", ncen);
}
STATUS("%i reflection pairs accepted.\n", ncom);
resolution_limits(list1_acc, cell, &rmin, &rmax);
resolution_limits(list2_acc, cell, &rmin, &rmax);
STATUS("Accepted resolution range: %f to %f nm^-1"
" (%.2f to %.2f Angstroms).\n",
rmin/1e9, rmax/1e9, 1e10/rmin, 1e10/rmax);
if ( rmin_fix >= 0.0 ) {
rmin = rmin_fix;
}
if ( rmax_fix >= 0.0 ) {
rmax = rmax_fix;
}
if ( (rmin_fix>=0.0) || (rmax_fix>=0.0) ) {
STATUS("Fixed resolution range: %f to %f nm^-1"
" (%.2f to %.2f Angstroms).\n",
rmin/1e9, rmax/1e9, 1e10/rmin, 1e10/rmax);
}
do_fom(list1_acc, list2_acc, cell, rmin, rmax, fom, config_unity,
nshells, shell_file, config_intshells, min_I, max_I, sym);
free(shell_file);
reflist_free(list1_acc);
reflist_free(list2_acc);
return 0;
}
|