Unexpected behavior of nested for loops when loading a macro with .L

You try to use a so-called ROOT’s unnamed macro. Your source code must be enclosed in curly braces (I somehow remember that the “{” needed to be the very first line in a ROOT’s unnamed macro file):

{
 // ... your source code ...
}

BTW. To load and execute a macro, you should use “.x”, not “.L”.