Besides the @SQ header lines, the bam file header may have information on the reference used in the @PG lines. If this header line is present, you can parse it to determine the reference which was used for alignment. An example line from a BWA mem aligned bam file is below.
@PG ID:bwa PN:bwa VN:0.7.9a-r788-dirty CL:bwa mem -t 6 -R @RG\tID:foo\tSM:bar /mnt/data/reference/hs37d5.fa ../ERR194147_1.fastq.gz ../ERR194147_2.fastq.gz
Which shows that this bam file was aligned to hs37d5.fa