Fix writing output tree

Only write output if reading according input is enabled.