Tg calculation

Ihave atrajectory withe 40frames .  When I want to calculate tg using perl scripting. By calculating the  density at different temperatures,

I notice that it does the calculation in all frames 
How can I specify one frame to calculate in? 

Best regards ​​​​​​​