TGeoManager and TThread

Hi,

when I run the following code and rotate the object with the mouse the program crashes from time to time.

I work on windows (.NET 2003) and root 5.08/00.

#include <TRint.h>
#include <TGeoManager.h>
#include <TGeoVolume.h>
#include <TThread.h>
#include “Riostream.h”

TRint *app;

void* guiThread(void arg)
{
TGeoManager
fGeoManager = new TGeoManager(“MEGDetector”, “MEG detector”);

TGeoVolume* fTop = fGeoManager->MakeBox(“TOPV”, new TGeoMedium(“TOP VOLUME”,1,new TGeoMaterial(“AIR”,14.61 ,7.3,0.1205000E-02)), 150, 150, 565);
fTop->SetVisibility(0);

TGeoVolume* DVC3 = fGeoManager->MakeCtub(“DVC3”,new TGeoMedium(“CATHODE”,2,new TGeoMaterial(“ALUMINIUM”,26.98 ,13,2.7)),1,1,43,0,180,0,-1,-0.3,0,-1,0.3);
DVC3->SetLineColor(4);

TGeoRotation *rot100 = new TGeoRotation(“rot100”,90,0,90,90,0,0);

for (int i=0;i<100;i++)
fTop->AddNode(DVC3,i+1,new TGeoCombiTrans(i*20,0,0,rot100));

fGeoManager->SetTopVolume(fTop);

fGeoManager->CloseGeometry();

fTop->Draw("");

app->Run();
return NULL;
}

int main(int argc, char *argv[])
{
int argn = 1;
char arg[1][100];
char *argp = &arg[0][0];
strcpy(arg[0],argv[0]);

app = new TRint(“App”, &argn, &argp,NULL,0,true);

TThread *thread = new TThread(“guiThread”, guiThread);
thread->Run();
gSystem->Sleep(1000);

while (1) {
gSystem->ProcessEvents();
gSystem->Sleep(10);
}

return 1;
}

Thanks for help,

Matthias

Hi Matthias,

I don’t know exactly what you want to do with your code, but here is an example working on windows :

[code]#include <TRint.h>
#include <TGeoManager.h>
#include <TGeoVolume.h>
#include <TThread.h>
#include <TPad.h>
#include <TView.h>
#include “Riostream.h”

Float_t theta = 0;
Float_t phi = 30;

void* guiThread(void arg)
{
TGeoManager
fGeoManager = new TGeoManager(“MEGDetector”, “MEG detector”);

TGeoVolume* fTop = fGeoManager->MakeBox(“TOPV”, new TGeoMedium(“TOP VOLUME”,1,new TGeoMaterial(“AIR”,14.61 ,7.3,0.1205000E-02)), 150, 150, 565);
fTop->SetVisibility(0);

TGeoVolume* DVC3 = fGeoManager->MakeCtub(“DVC3”,new TGeoMedium(“CATHODE”,2,new TGeoMaterial(“ALUMINIUM”,26.98 ,13,2.7)),1,1,43,0,180,0,-1,-0.3,0,-1,0.3);
DVC3->SetLineColor(4);

TGeoRotation *rot100 = new TGeoRotation(“rot100”,90,0,90,90,0,0);

for (int i=0;i<100;i++)
fTop->AddNode(DVC3,i+1,new TGeoCombiTrans(i*20,0,0,rot100));

fGeoManager->SetTopVolume(fTop);

fGeoManager->CloseGeometry();

fTop->Draw("");

while (1) {
theta += 2;
phi += 2;
gPad->GetView()->RotateView(theta,phi);
gPad->Modified();
gPad->Update();
gSystem->Sleep(500);
}

return NULL;
}

int main(int argc, char *argv[])
{
TRint *app = new TRint(“App”, &argc, argv, NULL, 0, true);

TThread *thread = new TThread(“guiThread”, guiThread);
thread->Run();
gSystem->Sleep(1000);

app->Run(kTRUE);
thread->Kill();

return 0;
}
[/code]
Several problems in your code :

  1. app->Run() must be the main thread.
  2. calling gSystem->ProcessEvents(); in an infinite loop is doing almost the same than app->Run()… so having two event handling mechanisms in two separate threads (not protected) is not good at all.
    Please take a look in $(ROOTSYS)/tutorials and $(ROOTSYS)/test directories to see correct usage of threads in ROOT.

Cheers,
Bertrand.