Obtain TGeoManager from ROOT file


I have a ROOT file with a few geometry volumes. If I open file in TBrowser I can see that every TGeoVolumeAssembly object has actual non-null fGeoManager member. Please refer to the screenshot below:

When I Dump() the “BarrelEMC” object from TBrowser UI, it provides:

*fGeoManager                  ->402fc60

However, when I am trying to access same TGeoVolumeAssembly’s fGeoManager member from a ROOT script, I get NULL pointer:

TFile* f = new TFile("emc_module12_2018v1.root", "READ");
TGeoVolumeAssembly* assembly = (TGeoVolumeAssembly*)f->Get("BarrelEMC");

Code outputs *fGeoManager ->0. I do not understand why am I getting NULL instead of the real object? Script archive is attached below.

emc-panda-test.zip (269.5 KB)

P.S.: my ROOT file does not seem to have TGeoManager object explicitly saved in it. Is there still a way to extract it from one of the TGeoVolumes?

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.24.02
Platform: fedora 5.13.19-200.fc34.x86_64
Compiler: gcc (GCC) 11.2.1

When you write individual volumes to a file, the geometry manager is not saved as well, so you need to create it yourself before loading the volumes from file. The pointer inside is not persistent, it is set at construction time (when you read from file) to the current gGeoManager. In your macro loading the volume triggers an automatic creation of an empty TGeoManager object after the volume creation, so at construction time gGeoManager will be uninitialized. So just create the TGeoManager to start with:

TGeoManager *geom = new TGeoManager("EMC","BarrelEMC");
auto f = TFile::Open("emc_module12_2018v1.root");
TGeoVolumeAssembly* assembly = (TGeoVolumeAssembly*)f->Get("BarrelEMC");
TGeoMaterial *mat = new TGeoMaterial("Vacuum",0,0,0);
TGeoMedium   *med = new TGeoMedium("Vacuum",1,mat);
TGeoVolume *top=gGeoManager->MakeBox("Top",med,100,100,100);

Thank you @agheata. I will give it a try and reply soon!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.