# Generate OpenBUGS 'data' file with a sequence of triggers # given: 1) beam composition; 2) trigger efficiency for pi/mu generate.triggers <- function(N=1000, p.mu=0.10, p.trig=c(0.02, 0.95)) { # generate triggers by muons (with prob=p.trig[2]) # or by pions (with prob=p.trig[1]) # where a proportion of muon in the beam given by p.mu T <- numeric() for (i in 1:N) { part <- rbinom(1, 1, p.mu) T[i] <- rbinom(1, 1, p.trig[part+1]) } # write to file ------------------------------------------ # define file name file.name <- sprintf("data_pmu%.2f_N%d.txt",p.mu, N) cat(sprintf("list(N=%d,\n",N), file=file.name) cat(sprintf("p.trig=c(%.2f,%.2f),\n", p.trig[1], p.trig[2]), file=file.name, append=TRUE) cat(sprintf("T=c("), file=file.name, append=TRUE) cat(T, sep=",", file=file.name, append=TRUE) cat(sprintf(")\n)\n"), file=file.name, append=TRUE) return(T) # returns T for printouts } # example of instructions to call the above function # uncomment the following line to generate the file T <- generate.triggers(10000, 0.15)