Can not view geometry through TCanvas

Dear Experts,

I want to simulate the electron drift, and set the geometry using GeometrySimple, but after I saved the canvas into root file and open the file run “canvas->Draw()”, I got sag fault. Can someone tell me where I did wrong? Here is the code:

void simXT::SetupGeo(std::string geoFile){
        fGeo= new GeometrySimple();

        TFile* f_geo = new TFile("/Users/siyuan/Physics/comet/xt_simulation/geofile/prototypeGeo_9anode.root","read");
        TTree* t_geo = (TTree*)f_geo->Get("t");

        t_geo->SetBranchAddress("x", &wire_x);
        t_geo->SetBranchAddress("y", &wire_y);
        t_geo->SetBranchAddress("type", &wire_type);

        SolidBox* box = new SolidBox(0, 0, 0, 10, 10, fSize_box/2);
        fGeo->AddSolid(box,fGas);

        std::cout<<"\033[32m";
        std::cout<<fGeo->GetSolid(0, 0, -73)<<std::endl;
        std::cout<<fGeo->IsInside(0, 0, -73)<<std::endl;
        std::cout<<"\033[0m";

        fCompE_field = new ComponentAnalyticField();
        fCompE_field->SetGeometry(fGeo);

        for(int i=0; i<t_geo->GetEntries(); i++){
                if(wire_type == 0) fCompE_field->AddWire(wire_x, wire_y, r_field, fHV*wire_type, "field");
                if(wire_type == 1) fCompE_field->AddWire(wire_x, wire_y, r_sense, fHV*wire_type, "field");
        }



        fCompE_field->SetMagneticField(0, 0, 0);

        std::cout<<"Set geometry for EM field"<<std::endl;
        //view geometry
        ViewGeometry* geoView = new ViewGeometry();
        geoView->SetCanvas(fMyCanvas);
        geoView->SetGeometry(fGeo);
        geoView->Plot3d();

        fFile_out->cd();
        fMyCanvas->Write();
        fFile_out->Close();
}

the wire_type is 1 for sense wire and 0 for field wire.

Hi,
I can’t run your code as is since I don’t have the class which this function is part of, but I ran a simple test program which you’ll find attached. Saving the canvas to a ROOT file and reopening seems to work fine with this program.

test.C (687 Bytes)

I tried your test program and can see the canvas after running the program, but when I open the root file and c->Draw() I still got sag fault:

siyuan@vpn28-48 test % root -l geo.root 
root [0] 
Attaching file geo.root as _file0...
(TFile *) 0x135210e70
root [1] .ls
TFile**		geo.root	
 TFile*		geo.root	
  KEY: TCanvas	c;1	
root [2] c->Draw()
Info in <TGeoManager::TGeoManager>: Geometry Geometry, default geometry created

 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGeom.so] TGeoVolume::Paint(char const*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGeom.so] TGeoVolume::Paint(char const*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGpad.so] TPad::PaintModified() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGpad.so] TCanvas::Update() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TRootCanvas::HandleContainerConfigure(Event_t*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TRootContainer::HandleConfigureNotify(Event_t*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TGFrame::HandleEvent(Event_t*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] ROOT::MacOSX::X11::EventTranslator::GenerateConfigureNotifyEvent(NSView<X11Window>*, CGRect const&) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzWindow mapRaised] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] TGCocoa::MapRaised(unsigned long) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TGFrame::MapRaised() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGpad.so] TCanvas::Draw(char const*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libRint.so] TRint::HandleTermInput() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TUnixSystem::CheckDescriptors() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TSystem::InnerLoop() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TSystem::Run() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TApplication::Run(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libRint.so] TRint::Run(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/bin/root.exe] main (no debug info)
[/usr/lib/dyld] start (no debug info)
Root > .q

Maybe I got some clue, the ROOT doesn’t know what is ViewGeometry class and can not write it into root file, then caused sag fault when I open it?

c->DrawClone()

If I use DrawClone() I got an empty Canvas (Nothing on the pad)

Not sure… It works for me when I run it on Centos7 and macOS.

From the error message above seems the geometry is not saved and automatically created when I draw the canvas through root file, but I don’t know how it works to save Garfield class into root file…

The code you sent and the test program do not save a Garfield class to a ROOT file. ViewGeometry uses the ROOT TGeo package to draw the geometry. What is saved to the ROOT file is a TCanvas and its contents (ROOT geometry).
As I said, I’m afraid I can’t reproduce the empty canvas problem. Does it work if you open it in a TBrowser?

The TBrowser also gives an empty Canvas like this:

Ah, I hadn’t realised you are using ROOT7.
I don’t think the issue is related to Garfield++ though. Can you try with the attached ROOT-only program?
test.C (961 Bytes)

Not sure why but I got those error messages… I should linked ROOT libs

siyuan@SiyuandeMacBook-Pro Downloads % `root-config --cxx --cflags --libs` -lMinuit -o test test.C 
Undefined symbols for architecture arm64:
  "TGeoMedium::TGeoMedium(char const*, int, TGeoMaterial const*, double*)", referenced from:
      _main in test-b03032.o
  "TGeoManager::SetTopVolume(TGeoVolume*)", referenced from:
      _main in test-b03032.o
  "TGeoManager::CloseGeometry(char const*)", referenced from:
      _main in test-b03032.o
  "TGeoManager::MakeBox(char const*, TGeoMedium*, double, double, double)", referenced from:
      _main in test-b03032.o
  "TGeoMaterial::TGeoMaterial(char const*, double, double, double, double, double)", referenced from:
      _main in test-b03032.o
  "_gGeoManager", referenced from:
      _main in test-b03032.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

You probably need to include -lGeom

`root-config --cxx --cflags --evelibs` -lMinuit ...

BTW. Are you sure you want to use “-lMinuit” and not “-lMinuit2”.

I successfully compiled and ran the test.C program, and still got sag fault when I open the root file and use

c->Draw()

error message:

siyuan@SiyuandeMacBook-Pro Downloads % `root-config --cxx --cflags --libs` -lMinuit2 -lGeom -o test test.C
siyuan@SiyuandeMacBook-Pro Downloads % ./test
Info in <TGeoManager::TGeoManager>: Geometry Geometry, default geometry created
Info in <TGeoManager::SetTopVolume>: Top volume is World. Master volume is World
Info in <TGeoNavigator::BuildCache>: --- Maximum geometry depth set to 100
Info in <TGeoManager::CheckGeometry>: Fixing runtime shapes...
Info in <TGeoManager::CheckGeometry>: ...Nothing to fix
Info in <TGeoManager::CloseGeometry>: Counting nodes...
Info in <TGeoManager::Voxelize>: Voxelizing...
Info in <TGeoManager::CloseGeometry>: Building cache...
Info in <TGeoManager::CountLevels>: max level = 1, max placements = 1
Info in <TGeoManager::CloseGeometry>: 2 nodes/ 2 volume UID's in default geometry
Info in <TGeoManager::CloseGeometry>: ----------------modeler ready----------------
siyuan@SiyuandeMacBook-Pro Downloads % root -l geo.root 
root [0] 
Attaching file geo.root as _file0...
(TFile *) 0x14773f3f0
root [1] .ls
TFile**		geo.root	
 TFile*		geo.root	
  KEY: TCanvas	c;1	
root [2] c->Draw()
Info in <TGeoManager::TGeoManager>: Geometry Geometry, default geometry created

 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGeom.so] TGeoVolume::Paint(char const*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGeom.so] TGeoVolume::Paint(char const*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGpad.so] TPad::PaintModified() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGpad.so] TCanvas::Update() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TRootCanvas::HandleContainerConfigure(Event_t*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TRootContainer::HandleConfigureNotify(Event_t*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TGFrame::HandleEvent(Event_t*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] ROOT::MacOSX::X11::EventTranslator::GenerateConfigureNotifyEvent(NSView<X11Window>*, CGRect const&) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzView configureNotifyTree] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] -[QuartzWindow mapRaised] (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGCocoa.so] TGCocoa::MapRaised(unsigned long) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGui.so] TGFrame::MapRaised() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libGpad.so] TCanvas::Draw(char const*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::Interpreter::process(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cling::Value*, cling::Transaction**, bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libRint.so] TRint::HandleTermInput() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TUnixSystem::CheckDescriptors() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TMacOSXSystem::DispatchOneEvent(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TSystem::InnerLoop() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TSystem::Run() (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libCore.so] TApplication::Run(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/lib/root/libRint.so] TRint::Run(bool) (no debug info)
[/opt/homebrew/Cellar/root/6.26.02/bin/root.exe] main (no debug info)
[/usr/lib/dyld] start (no debug info)

Ah…sorry I’m not sure, I’m a newbie and just been told to add such flags to compile…

I found that I get sag fault only when I save TGeo class into a canvas, if I only save a canvas the sag fault won’t happen…

Let’s see whether @agheata has an idea why this doesn’t work!

Hi, this won’t work, unfortunately. Geometry volumes are contextual objects that cannot be drawn in the absence of the geometry containing them, and this (TGeoManager class) is not written with the canvas. I’m afraid you need to load the geometry and replay the visualization settings rather than saving the canvas.

Eh…Sorry I don’t really understand, I’m actually a newbie of this, for example the macro written by @hschindl above, that should not work if I only draw the Canvas?
If so, is that normal I got a sag fault?