Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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"
//______________________________________________________________________________
TCanvas *create_canvas(const char *title, bool divide = true)
{
auto c = (TCanvas *)gROOT->GetListOfCanvases()->FindObject("csg_canvas");
if (c) {
c->Clear();
c->Update();
c->SetTitle(title);
} else {
c = new TCanvas("csg_canvas", title, 700, 1000);
}
if (divide) {
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);
}
return c;
}
//______________________________________________________________________________
void MakePicture()
{
if (is_raytracing != raytracing) {
gPad->Modified();
gPad->Update();
}
}
//______________________________________________________________________________
void s_union()
{
auto c = create_canvas("Union boolean operation");
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);
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->SetTextSize(.044);
pt->Draw();
c->cd(1);
}
//______________________________________________________________________________
void s_intersection()
{
auto c = create_canvas("Intersection boolean operation");
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);
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->SetTextSize(0.044);
pt->Draw();
c->cd(1);
}
//______________________________________________________________________________
void s_difference()
{
auto c = create_canvas("Difference boolean operation");
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);
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->SetTextSize(0.044);
pt->Draw();
c->cd(1);
}
//______________________________________________________________________________
void s_complex()
{
auto c = create_canvas("A * B - C");
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);
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->SetTextSize(0.044);
pt->Draw();
c->cd(1);
}
//______________________________________________________________________________
void raytrace()
{
if (gGeoManager && gPad) {
auto top = gGeoManager->GetTopVolume();
bool drawn = gPad->GetListOfPrimitives()->FindObject(top);
if (drawn)
top->SetVisRaytrace(raytracing);
printf("raytrace %d\n", raytracing);
gPad->Modified();
gPad->Update();
}
}
//______________________________________________________________________________
void help()
{
auto c = create_canvas("Help to run demos", false);
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->AddText(" .... s_union() : Union boolean operation");
hdemo->AddText(" .... s_difference() : Difference boolean operation");
hdemo->AddText(" .... s_intersection() : Intersection boolean operation");
hdemo->AddText(" .... s_complex() : Combination of (A * B) + (C - D)");
hdemo->AddText(" ");
hdemo->SetAllWith("....", "color", 2);
hdemo->SetAllWith("....", "font", 72);
hdemo->SetAllWith("....", "size", 0.03);
hdemo->Draw();
}
//______________________________________________________________________________
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 ");
bar->AddButton("Complex composite", "s_complex()", "(A * B) + (C - D)");
bar->AddButton("RAY-TRACE ON/OFF", "raytrace()", "Toggle ray-tracing mode");
bar->Show();
}
#define c(i)
Definition RSha256.hxx:101
bool Bool_t
Definition RtypesCore.h:63
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
Option_t Option_t TPoint TPoint const char text
R__EXTERN TGeoManager * gGeoManager
#define gROOT
Definition TROOT.h:406
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
#define gPad
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition TAttText.h:42
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
The Canvas class.
Definition TCanvas.h:23
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition TControlBar.h:26
void Show()
Show control bar.
void AddButton(TControlBarButton *button)
Add button.
void SetVisRaytrace(Bool_t flag=kTRUE)
Definition TGeoAtt.h:66
Composite shapes are Boolean combinations of two or more shape components.
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
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.
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
TGeoVolume * GetTopVolume() const
Base class describing materials.
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition TGeoMedium.h:23
virtual void DefineSection(Int_t snum, Double_t z, Double_t rmin, Double_t rmax)
Class describing translations.
Definition TGeoMatrix.h:116
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
virtual TGeoNode * AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=nullptr, Option_t *option="")
Add a TGeoNode to the list of nodes.
void Draw(Option_t *option="") override
draw top volume according to option
Bool_t IsRaytracing() const
Check if the painter is currently ray-tracing the content of this volume.
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
virtual void Clear(Option_t *="")
Definition TObject.h:119
virtual TObject * FindObject(const char *name) const
Must be redefined in derived classes.
Definition TObject.cxx:420
TObject * FindObject(const char *name) const override
Search if object named name is inside this pad or in pads inside this pad.
Definition TPad.cxx:2700
A Pave (see TPave) with text, lines or/and boxes inside.
Definition TPaveText.h:21
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
virtual void SetAllWith(const char *text, Option_t *option, Double_t value)
Set attribute option for all lines containing string text.
void Draw(Option_t *option="") override
Draw this pavetext with its current attributes.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1857
Base class for several text objects.
Definition TText.h:22
TPaveText * pt
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition fillpatterns.C:1
Author
Andrei Gheata

Definition in file csgdemo.C.