It's a good thing that somebody had the presence of mind to close the door before the dog got out.
Ability to think clearly and act appropriately, cool under pressure. - "When the bear charged the car, Jill had the presence of mind to sound the burglar alarm."
The ability to think and act calmly and efficiently, especially in an emergency. the ability to deal with a dangerous situation calmly and quickly have the presence of mind to do sth