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]
      TimesB <- TimesB[TimesB != 0] + 1  ## shift for Bob's data
      NeventsA <- NeventsA + length(TimesA)
      NeventsB <- NeventsB + length(TimesB)
      Diffs <- outer(TimesA, TimesB, "-")
      Rows <- as.vector(row(Diffs))
      Cols <- as.vector(col(Diffs))
      dim(Diffs) <- NULL
      Keep <- (abs(Diffs) <= 4)
      Diffs <- Diffs[Keep]
      Rows <- Rows[Keep]
      Cols <- Cols[Keep]
      for (Offset in c(0, 1, -1, 2, -2, 3, -3, 4, -4)) {
        Pairs <- (Diffs == Offset)
        Npairs <- Npairs + sum(Pairs)      
        Remove <- Rows %in% Rows[Pairs] | Cols %in% Cols[Pairs]
        Rows <- Rows[!Remove]
        Cols <- Cols[!Remove]
        Diffs <- Diffs[!Remove]
      }
    }
    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 35580 51036 56647"
## [1] "s1_a1b1_180236.txt s2_a1b1_180236.txt"
## [1] "2 71653 102501 114176"
## [1] "s1_a1b1_180254.txt s2_a1b1_180254.txt"
## [1] "3 108014 154289 171595"
## [1] "s1_a1b1_180313.txt s2_a1b1_180313.txt"
## [1] "4 143364 205154 227734"
## [1] "s1_a1b1_180331.txt s2_a1b1_180331.txt"
## [1] "5 179445 256792 284871"
## [1] "s1_a1b1_180350.txt s2_a1b1_180350.txt"
## [1] "6 215282 308131 341484"
## [1] "s1_a1b1_181832.txt s2_a1b1_181832.txt"
## [1] "7 250828 358668 398284"
## [1] "s1_a1b1_181850.txt s2_a1b1_181850.txt"
## [1] "8 286084 409130 454593"
## [1] "s1_a1b1_181910.txt s2_a1b1_181910.txt"
## [1] "9 321617 459914 511103"
## [1] "s1_a1b1_181928.txt s2_a1b1_181928.txt"
## [1] "10 357086 510577 567755"
## [1] "s1_a1b1_181947.txt s2_a1b1_181947.txt"
## [1] "11 392567 561189 623910"
## [1] "s1_a1b1_182005.txt s2_a1b1_182005.txt"
## [1] "12 428235 612119 680413"
## [1] "s1_a1b1_183219.txt s2_a1b1_183219.txt"
## [1] "13 463791 662809 737321"
## [1] "s1_a1b1_183238.txt s2_a1b1_183238.txt"
## [1] "14 499643 713946 794285"
## [1] "s1_a1b1_183257.txt s2_a1b1_183257.txt"
## [1] "15 535475 764845 851218"
## [1] "s1_a1b1_183315.txt s2_a1b1_183315.txt"
## [1] "16 571201 815912 908155"
## [1] "s1_a1b1_183334.txt s2_a1b1_183334.txt"
## [1] "17 607123 866982 965057"
## [1] "s1_a1b1_183353.txt s2_a1b1_183353.txt"
## [1] "18 642780 917889 1021859"
## [1] "s1_a1b1_185001.txt s2_a1b1_185001.txt"
## [1] "19 678693 969346 1078974"
## [1] "s1_a1b1_185020.txt s2_a1b1_185020.txt"
## [1] "20 714471 1020473 1136199"
## [1] "s1_a1b1_185039.txt s2_a1b1_185039.txt"
## [1] "21 750366 1071847 1193194"
## [1] "s1_a1b1_185057.txt s2_a1b1_185057.txt"
## [1] "22 786103 1123005 1250380"
## [1] "s1_a1b1_185116.txt s2_a1b1_185116.txt"
## [1] "23 821959 1174474 1307629"
## [1] "s1_a1b1_185135.txt s2_a1b1_185135.txt"
## [1] "24 857633 1225679 1364358"
## [1] "s1_a1b1_190557.txt s2_a1b1_190557.txt"
## [1] "25 893157 1276187 1420416"
## [1] "s1_a1b1_190615.txt s2_a1b1_190615.txt"
## [1] "26 928246 1325960 1476070"
## [1] "s1_a1b1_190634.txt s2_a1b1_190634.txt"
## [1] "27 963455 1375893 1532204"
## [1] "s1_a1b1_190653.txt s2_a1b1_190653.txt"
## [1] "28 999038 1426437 1588333"
## [1] "s1_a1b1_190712.txt s2_a1b1_190712.txt"
## [1] "29 1033974 1476271 1644049"
## [1] "s1_a1b1_190731.txt s2_a1b1_190731.txt"
## [1] "30 1069306 1526617 1699881"
results11
## $Npairs
## [1] 1069306
## 
## $NeventsA
## [1] 1526617
## 
## $NeventsB
## [1] 1699881
setwd("../a1b2")
results12 <- count()
## [1] "s1_a1b2_180527.txt s2_a1b2_180527.txt"
## [1] "1 38300 50666 149968"
## [1] "s1_a1b2_180546.txt s2_a1b2_180546.txt"
## [1] "2 76280 100762 299364"
## [1] "s1_a1b2_180604.txt s2_a1b2_180604.txt"
## [1] "3 114046 150997 448372"
## [1] "s1_a1b2_180622.txt s2_a1b2_180622.txt"
## [1] "4 152120 201345 597562"
## [1] "s1_a1b2_180641.txt s2_a1b2_180641.txt"
## [1] "5 190348 251982 747952"
## [1] "s1_a1b2_180659.txt s2_a1b2_180659.txt"
## [1] "6 228605 302394 897934"
## [1] "s1_a1b2_182151.txt s2_a1b2_182151.txt"
## [1] "7 266956 353139 1047875"
## [1] "s1_a1b2_182210.txt s2_a1b2_182210.txt"
## [1] "8 305339 403696 1197665"
## [1] "s1_a1b2_182229.txt s2_a1b2_182229.txt"
## [1] "9 343667 454426 1347791"
## [1] "s1_a1b2_182247.txt s2_a1b2_182247.txt"
## [1] "10 382422 505386 1498511"
## [1] "s1_a1b2_182305.txt s2_a1b2_182305.txt"
## [1] "11 421039 556227 1648481"
## [1] "s1_a1b2_182324.txt s2_a1b2_182324.txt"
## [1] "12 459773 606987 1798580"
## [1] "s1_a1b2_183829.txt s2_a1b2_183829.txt"
## [1] "13 498046 657895 1949414"
## [1] "s1_a1b2_183847.txt s2_a1b2_183847.txt"
## [1] "14 536609 708899 2100634"
## [1] "s1_a1b2_183906.txt s2_a1b2_183906.txt"
## [1] "15 575286 760128 2252059"
## [1] "s1_a1b2_183925.txt s2_a1b2_183925.txt"
## [1] "16 613738 810942 2403909"
## [1] "s1_a1b2_183944.txt s2_a1b2_183944.txt"
## [1] "17 652415 862020 2555919"
## [1] "s1_a1b2_184003.txt s2_a1b2_184003.txt"
## [1] "18 691260 913543 2707658"
## [1] "s1_a1b2_185407.txt s2_a1b2_185407.txt"
## [1] "19 729881 964791 2859629"
## [1] "s1_a1b2_185427.txt s2_a1b2_185427.txt"
## [1] "20 768589 1015993 3011825"
## [1] "s1_a1b2_185446.txt s2_a1b2_185446.txt"
## [1] "21 807048 1067165 3162832"
## [1] "s1_a1b2_185505.txt s2_a1b2_185505.txt"
## [1] "22 845839 1118503 3314721"
## [1] "s1_a1b2_185526.txt s2_a1b2_185526.txt"
## [1] "23 884558 1169731 3466530"
## [1] "s1_a1b2_185545.txt s2_a1b2_185545.txt"
## [1] "24 923131 1220883 3618228"
## [1] "s1_a1b2_190926.txt s2_a1b2_190926.txt"
## [1] "25 961432 1271157 3767930"
## [1] "s1_a1b2_190945.txt s2_a1b2_190945.txt"
## [1] "26 999555 1321285 3917610"
## [1] "s1_a1b2_191004.txt s2_a1b2_191004.txt"
## [1] "27 1037984 1371811 4067119"
## [1] "s1_a1b2_191023.txt s2_a1b2_191023.txt"
## [1] "28 1076377 1422622 4217065"
## [1] "s1_a1b2_191041.txt s2_a1b2_191041.txt"
## [1] "29 1114310 1472680 4366185"
## [1] "s1_a1b2_191100.txt s2_a1b2_191100.txt"
## [1] "30 1152595 1522865 4515782"
results12
## $Npairs
## [1] 1152595
## 
## $NeventsA
## [1] 1522865
## 
## $NeventsB
## [1] 4515782
setwd("../a2b1")

results21 <- count()
## [1] "s1_a2b1_181411.txt s2_a2b1_181411.txt"
## [1] "1 39788 158414 56506"
## [1] "s1_a2b1_181429.txt s2_a2b1_181429.txt"
## [1] "2 79710 315690 113136"
## [1] "s1_a2b1_181448.txt s2_a2b1_181448.txt"
## [1] "3 119742 473458 169579"
## [1] "s1_a2b1_181506.txt s2_a2b1_181506.txt"
## [1] "4 159067 630270 225165"
## [1] "s1_a2b1_181525.txt s2_a2b1_181525.txt"
## [1] "5 198478 787672 281086"
## [1] "s1_a2b1_181543.txt s2_a2b1_181543.txt"
## [1] "6 238151 945152 337158"
## [1] "s1_a2b1_182840.txt s2_a2b1_182840.txt"
## [1] "7 278011 1102639 393732"
## [1] "s1_a2b1_182859.txt s2_a2b1_182859.txt"
## [1] "8 318183 1261258 450790"
## [1] "s1_a2b1_182918.txt s2_a2b1_182918.txt"
## [1] "9 357901 1418777 507258"
## [1] "s1_a2b1_182937.txt s2_a2b1_182937.txt"
## [1] "10 397582 1576905 563679"
## [1] "s1_a2b1_182955.txt s2_a2b1_182955.txt"
## [1] "11 437329 1733611 620113"
## [1] "s1_a2b1_183014.txt s2_a2b1_183014.txt"
## [1] "12 477512 1891659 677154"
## [1] "s1_a2b1_184612.txt s2_a2b1_184612.txt"
## [1] "13 517484 2050442 733957"
## [1] "s1_a2b1_184632.txt s2_a2b1_184632.txt"
## [1] "14 557620 2209923 790948"
## [1] "s1_a2b1_184650.txt s2_a2b1_184650.txt"
## [1] "15 597363 2369249 847730"
## [1] "s1_a2b1_184709.txt s2_a2b1_184709.txt"
## [1] "16 637290 2528541 904633"
## [1] "s1_a2b1_184728.txt s2_a2b1_184728.txt"
## [1] "17 677292 2687996 961663"
## [1] "s1_a2b1_184747.txt s2_a2b1_184747.txt"
## [1] "18 717401 2847753 1018702"
## [1] "s1_a2b1_190132.txt s2_a2b1_190132.txt"
## [1] "19 756430 3004912 1074301"
## [1] "s1_a2b1_190151.txt s2_a2b1_190151.txt"
## [1] "20 795769 3162417 1130382"
## [1] "s1_a2b1_190210.txt s2_a2b1_190210.txt"
## [1] "21 835179 3319330 1186657"
## [1] "s1_a2b1_190229.txt s2_a2b1_190229.txt"
## [1] "22 874701 3476261 1243043"
## [1] "s1_a2b1_190248.txt s2_a2b1_190248.txt"
## [1] "23 914310 3634102 1299357"
## [1] "s1_a2b1_190306.txt s2_a2b1_190306.txt"
## [1] "24 953510 3791000 1355361"
## [1] "s1_a2b1_191606.txt s2_a2b1_191606.txt"
## [1] "25 993091 3948445 1411631"
## [1] "s1_a2b1_191625.txt s2_a2b1_191625.txt"
## [1] "26 1032580 4105687 1467824"
## [1] "s1_a2b1_191645.txt s2_a2b1_191645.txt"
## [1] "27 1072160 4262989 1524020"
## [1] "s1_a2b1_191704.txt s2_a2b1_191704.txt"
## [1] "28 1112040 4420718 1580570"
## [1] "s1_a2b1_191723.txt s2_a2b1_191723.txt"
## [1] "29 1151624 4577844 1637191"
## [1] "s1_a2b1_191743.txt s2_a2b1_191743.txt"
## [1] "30 1191146 4735046 1693718"
results21
## $Npairs
## [1] 1191146
## 
## $NeventsA
## [1] 4735046
## 
## $NeventsB
## [1] 1693718
setwd("../a2b2")

results22 <- count()
## [1] "s1_a2b2_181028.txt s2_a2b2_181028.txt"
## [1] "1 2178 156664 149245"
## [1] "s1_a2b2_181047.txt s2_a2b2_181047.txt"
## [1] "2 4394 313475 298562"
## [1] "s1_a2b2_181105.txt s2_a2b2_181105.txt"
## [1] "3 7332 470210 447707"
## [1] "s1_a2b2_181123.txt s2_a2b2_181123.txt"
## [1] "4 9779 627258 597336"
## [1] "s1_a2b2_181141.txt s2_a2b2_181141.txt"
## [1] "5 12063 783687 746270"
## [1] "s1_a2b2_181200.txt s2_a2b2_181200.txt"
## [1] "6 14501 940904 896442"
## [1] "s1_a2b2_182504.txt s2_a2b2_182504.txt"
## [1] "7 16832 1098033 1046673"
## [1] "s1_a2b2_182523.txt s2_a2b2_182523.txt"
## [1] "8 19235 1254900 1196666"
## [1] "s1_a2b2_182541.txt s2_a2b2_182541.txt"
## [1] "9 21412 1412806 1346262"
## [1] "s1_a2b2_182600.txt s2_a2b2_182600.txt"
## [1] "10 23595 1569899 1496482"
## [1] "s1_a2b2_182619.txt s2_a2b2_182619.txt"
## [1] "11 26124 1727228 1646646"
## [1] "s1_a2b2_182637.txt s2_a2b2_182637.txt"
## [1] "12 28362 1884680 1797340"
## [1] "s1_a2b2_184153.txt s2_a2b2_184153.txt"
## [1] "13 30521 2043780 1948299"
## [1] "s1_a2b2_184212.txt s2_a2b2_184212.txt"
## [1] "14 33088 2202741 2099022"
## [1] "s1_a2b2_184231.txt s2_a2b2_184231.txt"
## [1] "15 35236 2362088 2250425"
## [1] "s1_a2b2_184250.txt s2_a2b2_184250.txt"
## [1] "16 37404 2521200 2401356"
## [1] "s1_a2b2_184308.txt s2_a2b2_184308.txt"
## [1] "17 39683 2679900 2552457"
## [1] "s1_a2b2_184327.txt s2_a2b2_184327.txt"
## [1] "18 41900 2838957 2704499"
## [1] "s1_a2b2_185731.txt s2_a2b2_185731.txt"
## [1] "19 44206 2998185 2855971"
## [1] "s1_a2b2_185750.txt s2_a2b2_185750.txt"
## [1] "20 46745 3157932 3007486"
## [1] "s1_a2b2_185808.txt s2_a2b2_185808.txt"
## [1] "21 49031 3316588 3158087"
## [1] "s1_a2b2_185827.txt s2_a2b2_185827.txt"
## [1] "22 51416 3474103 3309092"
## [1] "s1_a2b2_185846.txt s2_a2b2_185846.txt"
## [1] "23 53705 3631540 3459677"
## [1] "s1_a2b2_185905.txt s2_a2b2_185905.txt"
## [1] "24 56137 3788565 3610475"
## [1] "s1_a2b2_191234.txt s2_a2b2_191234.txt"
## [1] "25 58335 3944766 3760278"
## [1] "s1_a2b2_191253.txt s2_a2b2_191253.txt"
## [1] "26 60640 4101332 3910181"
## [1] "s1_a2b2_191312.txt s2_a2b2_191312.txt"
## [1] "27 62944 4259202 4059533"
## [1] "s1_a2b2_191331.txt s2_a2b2_191331.txt"
## [1] "28 65291 4415703 4209049"
## [1] "s1_a2b2_191350.txt s2_a2b2_191350.txt"
## [1] "29 67491 4573061 4358268"
## [1] "s1_a2b2_191409.txt s2_a2b2_191409.txt"
## [1] "30 69749 4729369 4507497"
results22
## $Npairs
## [1] 69749
## 
## $NeventsA
## [1] 4729369
## 
## $NeventsB
## [1] 4507497
setwd("..")