Logo ROOT  
Reference Guide
csgdemo.C File Reference

Detailed Description

Combinatorial Solid Geometry example

Stripped down to demonstrate EVE shape-extracts.

  1. Run root csgdemo.C This will produce csg.root containing the extract.
  2. Display the assebly as: root show_extract.C("csg.root")
Bool_t raytracing = kTRUE;
#include "TGeoManager.h"
//______________________________________________________________________________
void csgdemo ()
{
gSystem->Load("libGeom");
TControlBar* bar = new TControlBar("vertical", "TGeo composite shapes",20,20);
bar->AddButton("How to run ","help()","Instructions ");
bar->AddButton("Union ", "s_union()", "A + B ");
bar->AddButton("Intersection ", "s_intersection()","A * B ");
bar->AddButton("Difference ", "s_difference()","A - B ");
//These two buttons are disabled cause of bugs
bar->AddButton("Complex composite", "complex_1()","(A * B) + (C - D)");
bar->Show();
gROOT->SaveContext();
}
//______________________________________________________________________________
void MakePicture()
{
gPad->Modified();
gPad->Update();
}
void s_union()
{
gROOT->GetListOfCanvases()->Delete();
TCanvas *c = new TCanvas("composite shape", "Union boolean operation", 700, 1000);
c->Divide(1,2,0,0);
c->cd(2);
gPad->SetPad(0,0,1,0.4);
c->cd(1);
gPad->SetPad(0,0.4,1,1);
if (gGeoManager) delete gGeoManager;
new TGeoManager("xtru", "poza12");
TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
TGeoMedium *med = new TGeoMedium("MED",1,mat);
TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
// define shape components with names
TGeoPgon *pgon = new TGeoPgon("pg",0.,360.,6,2);
pgon->DefineSection(0,0,0,20);
pgon->DefineSection(1, 30,0,20);
TGeoSphere *sph = new TGeoSphere("sph", 40., 45., 5., 175., 0., 340.);
// define named geometrical transformations with names
TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
tr->SetName("tr");
// register all used transformations
// create the composite shape based on a Boolean expression
TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr + pg");
TGeoVolume *vol = new TGeoVolume("COMP1",cs);
top->AddNode(vol,1);
top->Draw();
MakePicture();
c->cd(2);
TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
pt->SetLineColor(1);
TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
text->SetTextColor(2);
pt->AddText("----- It's an example of boolean union operation : A + B");
pt->AddText("----- A == part of sphere (5-175, 0-340), B == pgon");
pt->AddText(" ");
pt->SetAllWith("-----","color",4);
pt->SetAllWith("-----","font",72);
pt->SetAllWith("-----","size",0.04);
pt->SetTextAlign(12);
pt->SetTextSize(.044);
pt->Draw();
c->cd(1);
}
void s_intersection()
{
gROOT->GetListOfCanvases()->Delete();
TCanvas *c = new TCanvas("composite shape", "Intersection boolean operation", 700, 1000);
c->Divide(1,2,0,0);
c->cd(2);
gPad->SetPad(0,0,1,0.4);
c->cd(1);
gPad->SetPad(0,0.4,1,1);
if (gGeoManager) delete gGeoManager;
new TGeoManager("xtru", "poza12");
TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
TGeoMedium *med = new TGeoMedium("MED",1,mat);
TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
// define shape components with names
TGeoBBox *box = new TGeoBBox("bx", 40., 40., 40.);
TGeoSphere *sph = new TGeoSphere("sph", 40., 45.);
// define named geometrical transformations with names
TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
tr->SetName("tr");
// register all used transformations
// create the composite shape based on a Boolean expression
TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr * bx");
TGeoVolume *vol = new TGeoVolume("COMP2",cs);
top->AddNode(vol,1);
top->Draw();
MakePicture();
c->cd(2);
TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
pt->SetLineColor(1);
TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
text->SetTextColor(2);
pt->AddText("----- Here is an example of boolean intersection operation : A * B");
pt->AddText("----- A == sphere (with inner radius non-zero), B == box");
pt->AddText(" ");
pt->SetAllWith("-----","color",4);
pt->SetAllWith("-----","font",72);
pt->SetAllWith("-----","size",0.04);
pt->SetTextAlign(12);
pt->SetTextSize(0.044);
pt->Draw();
c->cd(1);
}
void s_difference()
{
gROOT->GetListOfCanvases()->Delete();
TCanvas *c = new TCanvas("composite shape", "Difference boolean operation", 700, 1000);
c->Divide(1,2,0,0);
c->cd(2);
gPad->SetPad(0,0,1,0.4);
c->cd(1);
gPad->SetPad(0,0.4,1,1);
if (gGeoManager) delete gGeoManager;
new TGeoManager("xtru", "poza12");
TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
TGeoMedium *med = new TGeoMedium("MED",1,mat);
TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
// define shape components with names
TGeoTorus *tor = new TGeoTorus("tor", 45., 15., 20., 45., 145.);
TGeoSphere *sph = new TGeoSphere("sph", 20., 45., 0., 180., 0., 270.);
// create the composite shape based on a Boolean expression
TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph - tor");
TGeoVolume *vol = new TGeoVolume("COMP3",cs);
top->AddNode(vol,1);
top->Draw();
MakePicture();
c->cd(2);
TPaveText *pt = new TPaveText(.01, .01, .99, .99);
pt->SetLineColor(1);
TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
text->SetTextColor(2);
pt->AddText("----- It's an example of boolean difference: A - B");
pt->AddText("----- A == part of sphere (0-180, 0-270), B == partial torus (45-145)");
pt->AddText(" ");
pt->SetAllWith("-----","color",4);
pt->SetAllWith("-----","font",72);
pt->SetAllWith("-----","size",0.04);
pt->SetTextAlign(12);
pt->SetTextSize(0.044);
pt->Draw();
c->cd(1);
}
void complex_1()
{
gROOT->GetListOfCanvases()->Delete();
TCanvas *c = new TCanvas("composite shape", "A * B - C", 700, 1000);
c->Divide(1,2,0,0);
c->cd(2);
gPad->SetPad(0,0,1,0.4);
c->cd(1);
gPad->SetPad(0,0.4,1,1);
if (gGeoManager) delete gGeoManager;
new TGeoManager("xtru", "poza12");
TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
TGeoMedium *med = new TGeoMedium("MED",1,mat);
TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
// define shape components with names
TGeoBBox *box = new TGeoBBox("box", 20., 20., 20.);
TGeoBBox *box1 = new TGeoBBox("box1", 5., 5., 5.);
TGeoSphere *sph = new TGeoSphere("sph", 5., 25.);
TGeoSphere *sph1 = new TGeoSphere("sph1", 1., 15.);
// create the composite shape based on a Boolean expression
TGeoTranslation *tr = new TGeoTranslation(0., 30., 0.);
TGeoTranslation *tr1 = new TGeoTranslation(0., 40., 0.);
TGeoTranslation *tr2 = new TGeoTranslation(0., 30., 0.);
TGeoTranslation *tr3 = new TGeoTranslation(0., 30., 0.);
tr->SetName("tr");
tr1->SetName("tr1");
tr2->SetName("tr2");
tr3->SetName("tr3");
// register all used transformations
TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "(sph * box) + (sph1:tr - box1:tr1)");
TGeoVolume *vol = new TGeoVolume("COMP4",cs);
// vol->SetLineColor(randomColor());
top->AddNode(vol,1);
top->Draw();
MakePicture();
c->cd(2);
TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
pt->SetLineColor(1);
TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
text->SetTextColor(2);
pt->AddText("----- (sphere * box) + (sphere - box) ");
pt->AddText(" ");
pt->SetAllWith("-----","color",4);
pt->SetAllWith("-----","font",72);
pt->SetAllWith("-----","size",0.04);
pt->SetTextAlign(12);
pt->SetTextSize(0.044);
pt->Draw();
c->cd(1);
}
//______________________________________________________________________________
void AddText(TPaveText *pave, const char *datamember, Double_t value, const char *comment)
{
char line[128];
for (Int_t i=0; i<128; i++) line[i] = ' ';
memcpy(&line[0], datamember, strlen(datamember));
line[10] = '=';
char number[20];
sprintf(number, "%5.2f", value);
memcpy(&line[12], number, strlen(number));
line[26] = '=';
line[27] = '>';
sprintf(&line[30], "%s",comment);
TText *text = pave->AddText(line);
text->SetTextAlign(12);
}
//______________________________________________________________________________
void AddText(TPaveText *pave, const char *datamember, Int_t value, const char *comment)
{
char line[128];
for (Int_t i=0; i<128; i++) line[i] = ' ';
memcpy(&line[0], datamember, strlen(datamember));
line[10] = '=';
char number[20];
sprintf(number, "%5i", value);
memcpy(&line[12], number, strlen(number));
line[26] = '=';
line[27] = '>';
sprintf(&line[30], "%s",comment);
TText *text = pave->AddText(line);
text->SetTextAlign(12);
}
//______________________________________________________________________________
void AddText(TPaveText *pave, TObject *pf, Int_t iaxis)
{
char line[128];
if (!pave || !pf) return;
for (Int_t i=0; i<128; i++) line[i] = ' ';
TGeoVolume *volume = finder->GetVolume();
TGeoShape *sh = volume->GetShape();
sprintf(line, "Division of %s on axis %d (%s)", volume->GetName(), iaxis,sh->GetAxisName(iaxis));
TText *text = pave->AddText(line);
text->SetTextColor(3);
text->SetTextAlign(12);
AddText(pave, "fNdiv",finder->GetNdiv(),"number of divisions");
AddText(pave, "fStart",finder->GetStart(),"start divisioning position");
AddText(pave, "fStep",finder->GetStep(),"division step");
}
//______________________________________________________________________________
void raytrace() {
if (!gGeoManager) return;
if (!painter) return;
painter->SetRaytracing(raytracing);
if (!gPad) return;
gPad->Modified();
gPad->Update();
}
//______________________________________________________________________________
void help() {
//
new TCanvas("chelp","Help to run demos",200,10,700,600);
TPaveText* welcome = new TPaveText(.1,.8,.9,.97);
welcome->AddText("Welcome to the new geometry package");
welcome->SetTextFont(32);
welcome->SetTextColor(4);
welcome->SetFillColor(24);
welcome->Draw();
TPaveText* hdemo = new TPaveText(.05,.05,.95,.7);
hdemo->SetTextAlign(12);
hdemo->SetTextFont(52);
hdemo->AddText("- Demo for building TGeo composite shapes");
hdemo->AddText(" ");
hdemo->SetAllWith("....","color",2);
hdemo->SetAllWith("....","font",72);
hdemo->SetAllWith("....","size",0.03);
hdemo->Draw();
}
Author
Andrei Gheata

Definition in file csgdemo.C.

c
#define c(i)
Definition: RSha256.hxx:119
TVirtualGeoPainter::SetRaytracing
virtual void SetRaytracing(Bool_t flag=kTRUE)=0
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
TGeoVolume::GetShape
TGeoShape * GetShape() const
Definition: TGeoVolume.h:188
TGeoPatternFinder::GetNdiv
Int_t GetNdiv() const
Definition: TGeoPatternFinder.h:86
TNamed::SetName
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
TGeoManager::MakeBox
TGeoVolume * MakeBox(const char *name, TGeoMedium *medium, Double_t dx, Double_t dy, Double_t dz)
Make in one step a volume pointing to a box shape with given medium.
Definition: TGeoManager.cxx:2918
gGeoManager
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:602
TGeoTorus
Definition: TGeoTorus.h:17
TGeoManager::CloseGeometry
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
Definition: TGeoManager.cxx:1359
TPaveText::SetAllWith
virtual void SetAllWith(const char *text, Option_t *option, Double_t value)
Set attribute option for all lines containing string text.
Definition: TPaveText.cxx:880
Int_t
int Int_t
Definition: RtypesCore.h:45
box
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
TGeoVolume::AddNode
virtual void AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=0, Option_t *option="")
Add a TGeoNode to the list of nodes.
Definition: TGeoVolume.cxx:932
TGeoPatternFinder::GetVolume
TGeoVolume * GetVolume() const
Definition: TGeoPatternFinder.h:92
TSystem::Load
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1852
TGeant4Unit::bar
static constexpr double bar
Definition: TGeant4SystemOfUnits.h:227
Bool_t
bool Bool_t
Definition: RtypesCore.h:63
TGeoTranslation
Definition: TGeoMatrix.h:121
text
TText * text
Definition: entrylist_figure1.C:10
TControlBar
Definition: TControlBar.h:26
TGeoMaterial
Definition: TGeoMaterial.h:34
TGeoPatternFinder
Definition: TGeoPatternFinder.h:31
TGeoCompositeShape
Definition: TGeoCompositeShape.h:27
TGeoManager::SetNsegments
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
Definition: TGeoManager.cxx:3339
TPaveText::Draw
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:234
TGeoManager::SetTopVolume
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
Definition: TGeoManager.cxx:3397
TGeoShape
Definition: TGeoShape.h:25
TGeoManager::GetGeomPainter
TVirtualGeoPainter * GetGeomPainter()
Make a default painter if none present. Returns pointer to it.
Definition: TGeoManager.cxx:2685
TGeoSphere
Definition: TGeoSphere.h:17
TGeoVolume::Draw
virtual void Draw(Option_t *option="")
draw top volume according to option
Definition: TGeoVolume.cxx:1146
TGeoPatternFinder::GetStep
Double_t GetStep() const
Definition: TGeoPatternFinder.h:90
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TGeoPgon
Definition: TGeoPgon.h:20
TGeoBBox
Definition: TGeoBBox.h:17
line
TLine * line
Definition: entrylistblock_figure1.C:235
TPaveText::AddText
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:183
TGeoPcon::DefineSection
virtual void DefineSection(Int_t snum, Double_t z, Double_t rmin, Double_t rmax)
Defines z position of a section plane, rmin and rmax at this z.
Definition: TGeoPcon.cxx:571
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
TGeoManager.h
TGeoShape::GetAxisName
virtual const char * GetAxisName(Int_t iaxis) const =0
TText
Definition: TText.h:22
TGeoPatternFinder::GetStart
Double_t GetStart() const
Definition: TGeoPatternFinder.h:89
TVirtualGeoPainter
Definition: TVirtualGeoPainter.h:32
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
Definition: TCanvas.h:23
TObject
Definition: TObject.h:37
gPad
#define gPad
Definition: TVirtualPad.h:287
TPaveText
Definition: TPaveText.h:21
TGeoMatrix::RegisterYourself
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Definition: TGeoMatrix.cxx:526
ROOT::TMetaUtils::propNames::comment
static const std::string comment("comment")
TGeoMedium
Definition: TGeoMedium.h:23
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
pt
TPaveText * pt
Definition: entrylist_figure1.C:7
TGeoManager
Definition: TGeoManager.h:44
TGeoVolume
Definition: TGeoVolume.h:44
gROOT
#define gROOT
Definition: TROOT.h:406