Almost certainly, it's a variation on 'to a tittle' - one meaning of �tittle' being the cross-mark on the letter 't'. There have been many suggestions that it might be a 'tee', as in golf and curling, or even a T-square. However, the Oxford English Dictionary rejects all of them as
untenable. When writing in a hurry, one often omits the cross-stroke of a 't' or the dot above a �j' or �i', so 'to a T' suggests something is exactly right.