Getting the Moodle base / root directory

The moodle root directory $CFG->dirroot is established in the file lib/setup.php along with other values in the global $CFG object which combines configuraton from config.php as well as the entries in the database table mdl_config.

The value is set as follows:

$CFG->dirroot = dirname(dirname(__FILE__));

And you can use it reliably establish the base/root moodle directory absolute path and use it for things like file requires in your plugin.

Some other useful entries in lib/setup.php from the code:

$CFG->wwwroot  - Path to moodle index directory in url format.
$CFG->dataroot - Path to moodle data files directory on server's filesystem.
$CFG->dirroot  - Path to moodle's library folder on server's filesystem.
$CFG->libdir   - Path to moodle's library folder on server's filesystem.
$CFG->tempdir  - Path to moodle's temp file directory on server's filesystem.
$CFG->cachedir - Path to moodle's cache directory on server's filesystem (shared by cluster nodes).
$CFG->localcachedir - Path to moodle's local cache directory (not shared by cluster nodes).

Lots more in there have a dig around in lib/setup.php.