Now as I have to duplicate the same ~70 lines of code for TNtupleD,

I'd better make this function a template and move it to the TreeUtils.