fix for ROOT-6699: check that a class is valid before downcasting