This will not always work. In case there is no geometry record, this throws an exception which needs to be catched in order to prevent cmsRun from crashing. You will need to make use of a try {} catch {} statement. See example here: https://github.com/cms-sw/cmssw/blob/CMSSW_6_2_X_SLHC/L1Trigger/CSCTriggerPrimitives/plugins/CSCTriggerPrimitivesProducer.cc#L80

In the try{} statement you put the setGEMGeometry() function. In setGEMGeometry() you remove the if-else statement and restore the code to the previous implementation