# Export magnetic field to root file

**URL:** <https://root-forum.cern.ch/t/export-magnetic-field-to-root-file/52844>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [December 21, 2022, 8:02am UTC](https://root-forum.cern.ch/t/export-magnetic-field-to-root-file/52844 "2022-12-21T08:02:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![arazquin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@arazquin](https://root-forum.cern.ch/u/arazquin)\
**Post date:** [December 21, 2022, 8:02am UTC](https://root-forum.cern.ch/t/export-magnetic-field-to-root-file/52844/1 "2022-12-21T08:02:36Z")

</div>

Hello experts,  
I have a magnetic fieldmap loaded using  
TGeoGlobalMagField  
How can I export it into a root file?  
Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Emil\_R](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/emil_r/32/31255_2.png) [@Emil\_R](https://root-forum.cern.ch/u/Emil_R)\
**Post date:** [December 21, 2022, 8:33am UTC](https://root-forum.cern.ch/t/export-magnetic-field-to-root-file/52844/2 "2022-12-21T08:33:14Z")

</div>

> TGeoGlobalMagField \*MagF = new TGeoGlobalMagField();  
> MagF-\>SaveAs(“TheRootFile.root”);

This should work.  
If you already have a root file, just use:

> MagF-\>Write();

After opening the file with:

> TFile \*f = TFile::Open(“ThisFile.root”,“UPDATE”);

Cheers,  
Emil

---

<div class="post-metadata">

**Author:** ![arazquin](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/df788c/32.png) [@arazquin](https://root-forum.cern.ch/u/arazquin)\
**Post date:** [December 22, 2022, 4:19am UTC](https://root-forum.cern.ch/t/export-magnetic-field-to-root-file/52844/3 "2022-12-22T04:19:19Z")

</div>

Thanks a lot!
