setwd("/Users/richard/Desktop/Bell/ACOnet FileSender_files/Giustina_Data+CoincCode_Eberhard_2013")

count <- function(){
  Names <- system("ls", intern = TRUE)
  NeventsA <- 0
  NeventsB <- 0
  Npairs <- 0
  for(Piece in 1:30){
    BobData <- read.table(Names[Piece])
    AliceData <- read.table(Names[Piece + 30])
    print(paste(Names[Piece], Names[Piece + 30]))
    BobData <- BobData$V1
    BobMaxRun <- BobData[1]
    BobData <- BobData[-1]
    dim(BobData) <- c(BobMaxRun, 100)
    AliceData <- AliceData$V1
    AliceMaxRun <- AliceData[1]
    AliceData <- AliceData[-1]
    dim(AliceData) <- c(AliceMaxRun, 100)
    for (Block in 1:100) {
      TimesA <- AliceData[ , Block]
      TimesB <- BobData[ , Block]
      TimesA <- TimesA[TimesA != 0] - 1
      TimesB <- TimesB[TimesB != 0]
      TimesA <- TimesA%/%50
      TimesB <- TimesB%/%50
      TimesA <- unique(TimesA)
      TimesB <- unique(TimesB)
      NeventsA <- NeventsA + length(TimesA)
      NeventsB <- NeventsB + length(TimesB)
      Diffs <- outer(TimesA, TimesB, "-")
      Npairs <- Npairs + sum(Diffs == 0)
    }
    print(paste(Piece, Npairs, NeventsA, NeventsB))
  }
  return(list(Npairs = Npairs, NeventsA = NeventsA, NeventsB = NeventsB))
}

setwd("a1b1")

results11 <- count()
## [1] "s1_a1b1_180217.txt s2_a1b1_180217.txt"
## [1] "1 35087 50862 56429"
## [1] "s1_a1b1_180236.txt s2_a1b1_180236.txt"
## [1] "2 70664 102143 113752"
## [1] "s1_a1b1_180254.txt s2_a1b1_180254.txt"
## [1] "3 106465 153751 170951"
## [1] "s1_a1b1_180313.txt s2_a1b1_180313.txt"
## [1] "4 141346 204441 226892"
## [1] "s1_a1b1_180331.txt s2_a1b1_180331.txt"
## [1] "5 176900 255910 283816"
## [1] "s1_a1b1_180350.txt s2_a1b1_180350.txt"
## [1] "6 212246 307066 340221"
## [1] "s1_a1b1_181832.txt s2_a1b1_181832.txt"
## [1] "7 247270 357443 396826"
## [1] "s1_a1b1_181850.txt s2_a1b1_181850.txt"
## [1] "8 281996 407724 452925"
## [1] "s1_a1b1_181910.txt s2_a1b1_181910.txt"
## [1] "9 317017 458330 509201"
## [1] "s1_a1b1_181928.txt s2_a1b1_181928.txt"
## [1] "10 351981 508829 565631"
## [1] "s1_a1b1_181947.txt s2_a1b1_181947.txt"
## [1] "11 386980 559266 621568"
## [1] "s1_a1b1_182005.txt s2_a1b1_182005.txt"
## [1] "12 422130 610008 677846"
## [1] "s1_a1b1_183219.txt s2_a1b1_183219.txt"
## [1] "13 457173 660513 734525"
## [1] "s1_a1b1_183238.txt s2_a1b1_183238.txt"
## [1] "14 492496 711479 791267"
## [1] "s1_a1b1_183257.txt s2_a1b1_183257.txt"
## [1] "15 527807 762209 847959"
## [1] "s1_a1b1_183315.txt s2_a1b1_183315.txt"
## [1] "16 563008 813097 904688"
## [1] "s1_a1b1_183334.txt s2_a1b1_183334.txt"
## [1] "17 598401 863994 961362"
## [1] "s1_a1b1_183353.txt s2_a1b1_183353.txt"
## [1] "18 633586 914755 1017948"
## [1] "s1_a1b1_185001.txt s2_a1b1_185001.txt"
## [1] "19 668959 966038 1074855"
## [1] "s1_a1b1_185020.txt s2_a1b1_185020.txt"
## [1] "20 704199 1016976 1131860"
## [1] "s1_a1b1_185039.txt s2_a1b1_185039.txt"
## [1] "21 739612 1068184 1188638"
## [1] "s1_a1b1_185057.txt s2_a1b1_185057.txt"
## [1] "22 774845 1119164 1245600"
## [1] "s1_a1b1_185116.txt s2_a1b1_185116.txt"
## [1] "23 810172 1170443 1302623"
## [1] "s1_a1b1_185135.txt s2_a1b1_185135.txt"
## [1] "24 845312 1221466 1359149"
## [1] "s1_a1b1_190557.txt s2_a1b1_190557.txt"
## [1] "25 880370 1271811 1415003"
## [1] "s1_a1b1_190615.txt s2_a1b1_190615.txt"
## [1] "26 914981 1321414 1470455"
## [1] "s1_a1b1_190634.txt s2_a1b1_190634.txt"
## [1] "27 949686 1371172 1526395"
## [1] "s1_a1b1_190653.txt s2_a1b1_190653.txt"
## [1] "28 984768 1421546 1582294"
## [1] "s1_a1b1_190712.txt s2_a1b1_190712.txt"
## [1] "29 1019185 1471242 1637825"
## [1] "s1_a1b1_190731.txt s2_a1b1_190731.txt"
## [1] "30 1054015 1521403 1693449"
results11
## $Npairs
## [1] 1054015
## 
## $NeventsA
## [1] 1521403
## 
## $NeventsB
## [1] 1693449
setwd("../a1b2")
results12 <- count()
## [1] "s1_a1b2_180527.txt s2_a1b2_180527.txt"
## [1] "1 37855 50496 148649"
## [1] "s1_a1b2_180546.txt s2_a1b2_180546.txt"
## [1] "2 75470 100423 296802"
## [1] "s1_a1b2_180604.txt s2_a1b2_180604.txt"
## [1] "3 112834 150477 444594"
## [1] "s1_a1b2_180622.txt s2_a1b2_180622.txt"
## [1] "4 150512 200638 592495"
## [1] "s1_a1b2_180641.txt s2_a1b2_180641.txt"
## [1] "5 188393 251131 741624"
## [1] "s1_a1b2_180659.txt s2_a1b2_180659.txt"
## [1] "6 226209 301370 890250"
## [1] "s1_a1b2_182151.txt s2_a1b2_182151.txt"
## [1] "7 264110 351929 1038890"
## [1] "s1_a1b2_182210.txt s2_a1b2_182210.txt"
## [1] "8 302172 402328 1187399"
## [1] "s1_a1b2_182229.txt s2_a1b2_182229.txt"
## [1] "9 340096 452875 1336247"
## [1] "s1_a1b2_182247.txt s2_a1b2_182247.txt"
## [1] "10 378477 503672 1485657"
## [1] "s1_a1b2_182305.txt s2_a1b2_182305.txt"
## [1] "11 416710 554365 1634352"
## [1] "s1_a1b2_182324.txt s2_a1b2_182324.txt"
## [1] "12 455030 604959 1783157"
## [1] "s1_a1b2_183829.txt s2_a1b2_183829.txt"
## [1] "13 492875 655696 1932711"
## [1] "s1_a1b2_183847.txt s2_a1b2_183847.txt"
## [1] "14 531037 706541 2082649"
## [1] "s1_a1b2_183906.txt s2_a1b2_183906.txt"
## [1] "15 569281 757601 2232766"
## [1] "s1_a1b2_183925.txt s2_a1b2_183925.txt"
## [1] "16 607369 808243 2383351"
## [1] "s1_a1b2_183944.txt s2_a1b2_183944.txt"
## [1] "17 645685 859180 2534082"
## [1] "s1_a1b2_184003.txt s2_a1b2_184003.txt"
## [1] "18 684141 910538 2684492"
## [1] "s1_a1b2_185407.txt s2_a1b2_185407.txt"
## [1] "19 722344 961615 2835143"
## [1] "s1_a1b2_185427.txt s2_a1b2_185427.txt"
## [1] "20 760635 1012654 2986010"
## [1] "s1_a1b2_185446.txt s2_a1b2_185446.txt"
## [1] "21 798701 1063651 3135735"
## [1] "s1_a1b2_185505.txt s2_a1b2_185505.txt"
## [1] "22 837130 1114817 3286371"
## [1] "s1_a1b2_185526.txt s2_a1b2_185526.txt"
## [1] "23 875474 1165861 3436941"
## [1] "s1_a1b2_185545.txt s2_a1b2_185545.txt"
## [1] "24 913639 1216846 3587356"
## [1] "s1_a1b2_190926.txt s2_a1b2_190926.txt"
## [1] "25 951548 1266970 3735775"
## [1] "s1_a1b2_190945.txt s2_a1b2_190945.txt"
## [1] "26 989256 1316941 3884197"
## [1] "s1_a1b2_191004.txt s2_a1b2_191004.txt"
## [1] "27 1027219 1367311 4032428"
## [1] "s1_a1b2_191023.txt s2_a1b2_191023.txt"
## [1] "28 1065224 1417935 4181117"
## [1] "s1_a1b2_191041.txt s2_a1b2_191041.txt"
## [1] "29 1102822 1467832 4328975"
## [1] "s1_a1b2_191100.txt s2_a1b2_191100.txt"
## [1] "30 1140690 1517837 4477285"
results12
## $Npairs
## [1] 1140690
## 
## $NeventsA
## [1] 1517837
## 
## $NeventsB
## [1] 4477285
setwd("../a2b1")

results21 <- count()
## [1] "s1_a2b1_181411.txt s2_a2b1_181411.txt"
## [1] "1 39364 156860 56271"
## [1] "s1_a2b1_181429.txt s2_a2b1_181429.txt"
## [1] "2 78863 312597 112692"
## [1] "s1_a2b1_181448.txt s2_a2b1_181448.txt"
## [1] "3 118442 468769 168924"
## [1] "s1_a2b1_181506.txt s2_a2b1_181506.txt"
## [1] "4 157340 623997 224287"
## [1] "s1_a2b1_181525.txt s2_a2b1_181525.txt"
## [1] "5 196346 779771 279980"
## [1] "s1_a2b1_181543.txt s2_a2b1_181543.txt"
## [1] "6 235562 935727 335804"
## [1] "s1_a2b1_182840.txt s2_a2b1_182840.txt"
## [1] "7 274997 1091599 392181"
## [1] "s1_a2b1_182859.txt s2_a2b1_182859.txt"
## [1] "8 314740 1248636 449048"
## [1] "s1_a2b1_182918.txt s2_a2b1_182918.txt"
## [1] "9 354040 1404588 505314"
## [1] "s1_a2b1_182937.txt s2_a2b1_182937.txt"
## [1] "10 393329 1561065 561542"
## [1] "s1_a2b1_182955.txt s2_a2b1_182955.txt"
## [1] "11 432678 1716250 617752"
## [1] "s1_a2b1_183014.txt s2_a2b1_183014.txt"
## [1] "12 472429 1872665 674570"
## [1] "s1_a2b1_184612.txt s2_a2b1_184612.txt"
## [1] "13 512034 2029813 731148"
## [1] "s1_a2b1_184632.txt s2_a2b1_184632.txt"
## [1] "14 551707 2187698 787931"
## [1] "s1_a2b1_184650.txt s2_a2b1_184650.txt"
## [1] "15 590983 2345417 844507"
## [1] "s1_a2b1_184709.txt s2_a2b1_184709.txt"
## [1] "16 630515 2503165 901203"
## [1] "s1_a2b1_184728.txt s2_a2b1_184728.txt"
## [1] "17 670107 2660994 958014"
## [1] "s1_a2b1_184747.txt s2_a2b1_184747.txt"
## [1] "18 709816 2819136 1014834"
## [1] "s1_a2b1_190132.txt s2_a2b1_190132.txt"
## [1] "19 748448 2974764 1070222"
## [1] "s1_a2b1_190151.txt s2_a2b1_190151.txt"
## [1] "20 787406 3130696 1126093"
## [1] "s1_a2b1_190210.txt s2_a2b1_190210.txt"
## [1] "21 826394 3286101 1182163"
## [1] "s1_a2b1_190229.txt s2_a2b1_190229.txt"
## [1] "22 865508 3441518 1238326"
## [1] "s1_a2b1_190248.txt s2_a2b1_190248.txt"
## [1] "23 904677 3597767 1294437"
## [1] "s1_a2b1_190306.txt s2_a2b1_190306.txt"
## [1] "24 943437 3753053 1350209"
## [1] "s1_a2b1_191606.txt s2_a2b1_191606.txt"
## [1] "25 982595 3908922 1406270"
## [1] "s1_a2b1_191625.txt s2_a2b1_191625.txt"
## [1] "26 1021702 4064553 1462243"
## [1] "s1_a2b1_191645.txt s2_a2b1_191645.txt"
## [1] "27 1060876 4220272 1518260"
## [1] "s1_a2b1_191704.txt s2_a2b1_191704.txt"
## [1] "28 1100343 4376437 1574578"
## [1] "s1_a2b1_191723.txt s2_a2b1_191723.txt"
## [1] "29 1139462 4531982 1630961"
## [1] "s1_a2b1_191743.txt s2_a2b1_191743.txt"
## [1] "30 1178589 4687681 1687285"
results21
## $Npairs
## [1] 1178589
## 
## $NeventsA
## [1] 4687681
## 
## $NeventsB
## [1] 1687285
setwd("../a2b2")

results22 <- count()
## [1] "s1_a2b2_181028.txt s2_a2b2_181028.txt"
## [1] "1 3938 155075 148070"
## [1] "s1_a2b2_181047.txt s2_a2b2_181047.txt"
## [1] "2 8010 310370 296016"
## [1] "s1_a2b2_181105.txt s2_a2b2_181105.txt"
## [1] "3 12729 465586 443924"
## [1] "s1_a2b2_181123.txt s2_a2b2_181123.txt"
## [1] "4 16982 620982 592234"
## [1] "s1_a2b2_181141.txt s2_a2b2_181141.txt"
## [1] "5 21101 775870 739938"
## [1] "s1_a2b2_181200.txt s2_a2b2_181200.txt"
## [1] "6 25377 931575 888778"
## [1] "s1_a2b2_182504.txt s2_a2b2_182504.txt"
## [1] "7 29562 1087097 1037731"
## [1] "s1_a2b2_182523.txt s2_a2b2_182523.txt"
## [1] "8 33811 1242404 1186488"
## [1] "s1_a2b2_182541.txt s2_a2b2_182541.txt"
## [1] "9 37767 1398762 1334777"
## [1] "s1_a2b2_182600.txt s2_a2b2_182600.txt"
## [1] "10 41829 1554301 1483665"
## [1] "s1_a2b2_182619.txt s2_a2b2_182619.txt"
## [1] "11 46157 1710072 1632545"
## [1] "s1_a2b2_182637.txt s2_a2b2_182637.txt"
## [1] "12 50271 1866005 1781944"
## [1] "s1_a2b2_184153.txt s2_a2b2_184153.txt"
## [1] "13 54258 2023558 1931600"
## [1] "s1_a2b2_184212.txt s2_a2b2_184212.txt"
## [1] "14 58706 2180879 2081045"
## [1] "s1_a2b2_184231.txt s2_a2b2_184231.txt"
## [1] "15 62724 2338609 2231083"
## [1] "s1_a2b2_184250.txt s2_a2b2_184250.txt"
## [1] "16 66758 2496167 2380655"
## [1] "s1_a2b2_184308.txt s2_a2b2_184308.txt"
## [1] "17 70886 2653230 2530470"
## [1] "s1_a2b2_184327.txt s2_a2b2_184327.txt"
## [1] "18 74973 2810660 2681148"
## [1] "s1_a2b2_185731.txt s2_a2b2_185731.txt"
## [1] "19 79136 2968228 2831286"
## [1] "s1_a2b2_185750.txt s2_a2b2_185750.txt"
## [1] "20 83463 3126433 2981531"
## [1] "s1_a2b2_185808.txt s2_a2b2_185808.txt"
## [1] "21 87557 3283552 3130813"
## [1] "s1_a2b2_185827.txt s2_a2b2_185827.txt"
## [1] "22 91867 3439435 3280543"
## [1] "s1_a2b2_185846.txt s2_a2b2_185846.txt"
## [1] "23 95970 3595318 3429797"
## [1] "s1_a2b2_185905.txt s2_a2b2_185905.txt"
## [1] "24 100283 3750773 3579309"
## [1] "s1_a2b2_191234.txt s2_a2b2_191234.txt"
## [1] "25 104264 3905411 3727806"
## [1] "s1_a2b2_191253.txt s2_a2b2_191253.txt"
## [1] "26 108337 4060372 3876456"
## [1] "s1_a2b2_191312.txt s2_a2b2_191312.txt"
## [1] "27 112387 4216590 4024572"
## [1] "s1_a2b2_191331.txt s2_a2b2_191331.txt"
## [1] "28 116589 4371566 4172890"
## [1] "s1_a2b2_191350.txt s2_a2b2_191350.txt"
## [1] "29 120579 4527338 4320858"
## [1] "s1_a2b2_191409.txt s2_a2b2_191409.txt"
## [1] "30 124696 4682139 4468842"
results22
## $Npairs
## [1] 124696
## 
## $NeventsA
## [1] 4682139
## 
## $NeventsB
## [1] 4468842
setwd("..")

N <- 30 * 100 * ceiling((0.1 / (20 * 10^{-9}) - 600)/50)

corr <- function(results) {
  Npairs <- results$Npairs
  NeventsA <- results$NeventsA
  NeventsB <- results$NeventsB
  Npp <- Npairs
  Npm <- NeventsA - Npairs
  Nmp <- NeventsB - Npairs
  Nmm <- N - Npp - Npm - Nmp
  (Npp + Nmm - Npm - Nmp) / N
}


corr11 <- corr(results11)
corr12 <- corr(results12)
corr21 <- corr(results21)
corr22 <- corr(results22)
corrs <- c(corr11, corr12, corr21, corr22)
corrs
## [1] 0.9926203 0.9752387 0.9732115 0.9406490
CHSH <- corr11 + corr12 + corr21 - corr22
seCHSH <- sqrt((4 - sum(corrs^2)) / N)

CHSH; seCHSH; (CHSH - 2) / seCHSH
## [1] 2.000422
## [1] 2.778975e-05
## [1] 15.17216
C11 <- results11$Npairs
S12A <- results12$NeventsA
C12 <- results12$Npairs
S21B <- results21$NeventsB
C21 <- results21$Npairs
C22 <- results22$Npairs

J <- (S12A - C12) + (S21B - C21) + C22 - C11
seJ <- sqrt((S12A - C12) + (S21B - C21) + C22 + C11)

J; seJ; (-J) / seJ
## [1] -43476
## [1] 1436.856
## [1] 30.25774
S11A <- results11$NeventsA
S11B <- results11$NeventsB

Jalt <- (S11A + S11B - C11) - C12 - C21 + C22
seJalt <- sqrt((S11A + S11B - C11) + C12 + C21 + C22)
Jalt; seJalt; (-Jalt) / seJalt
## [1] -33746
## [1] 2145.883
## [1] 15.72593
save(results11, results12, results21, results22, file = "results50.RData")