After saying hamapil at night can someome think in Torah untill falling asleep? If someone thinks in Torah before saying Hamapil, he might fall asleep without saying it?
One may certainly think in learning after falling asleep, and this is recommended practice. Aside from the value of Torah study, it is a sure way to fall asleep quickly!
You are right that thinking in learning while laying down is a recipe for sleep, and it is better to say hamapil first.