Logo ROOT   6.14/05
Reference Guide
csgdemo.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_geom
3 /// Combinatorial Solid Geometry example.
4 ///
5 /// \macro_code
6 ///
7 /// \author Andrei Gheata
8 
9 Bool_t raytracing = kTRUE;
10 
11 #include "TGeoManager.h"
12 //______________________________________________________________________________
13 void csgdemo ()
14 {
15  gSystem->Load("libGeom");
16  TControlBar* bar = new TControlBar("vertical", "TGeo composite shapes",20,20);
17  bar->AddButton("How to run ","help()","Instructions ");
18  bar->AddButton("Union ", "s_union()", "A + B ");
19  bar->AddButton("Intersection ", "s_intersection()","A * B ");
20  bar->AddButton("Difference ", "s_difference()","A - B ");
21  //These two buttons are disabled cause of bugs
22  bar->AddButton("Complex composite", "complex_1()","(A * B) + (C - D)");
23  bar->Show();
24 
25  gROOT->SaveContext();
26 }
27 
28 //______________________________________________________________________________
29 void MakePicture()
30 {
32  gPad->Modified();
33  gPad->Update();
34 }
35 
36 
37 void s_union()
38 {
39  gROOT->GetListOfCanvases()->Delete();
40  TCanvas *c = new TCanvas("composite shape", "Union boolean operation", 700, 1000);
41 
42  c->Divide(1,2,0,0);
43  c->cd(2);
44  gPad->SetPad(0,0,1,0.4);
45  c->cd(1);
46  gPad->SetPad(0,0.4,1,1);
47 
48  if (gGeoManager) delete gGeoManager;
49 
50  new TGeoManager("xtru", "poza12");
51  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
52  TGeoMedium *med = new TGeoMedium("MED",1,mat);
53  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
55 
56  // define shape components with names
57  TGeoPgon *pgon = new TGeoPgon("pg",0.,360.,6,2);
58  pgon->DefineSection(0,0,0,20);
59  pgon->DefineSection(1, 30,0,20);
60 
61  TGeoSphere *sph = new TGeoSphere("sph", 40., 45., 5., 175., 0., 340.);
62  // define named geometrical transformations with names
63  TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
64  tr->SetName("tr");
65  // register all used transformations
66  tr->RegisterYourself();
67  // create the composite shape based on a Boolean expression
68  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr + pg");
69 
70  TGeoVolume *vol = new TGeoVolume("COMP1",cs);
71  top->AddNode(vol,1);
74  top->Draw();
75  MakePicture();
76 
77  c->cd(2);
78  TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
79  pt->SetLineColor(1);
80  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
81  text->SetTextColor(2);
82  pt->AddText("----- It's an example of boolean union operation : A + B");
83  pt->AddText("----- A == part of sphere (5-175, 0-340), B == pgon");
84  pt->AddText(" ");
85  pt->SetAllWith("-----","color",4);
86  pt->SetAllWith("-----","font",72);
87  pt->SetAllWith("-----","size",0.04);
88  pt->SetTextAlign(12);
89  pt->SetTextSize(.044);
90  pt->Draw();
91  c->cd(1);
92 }
93 
94 void s_intersection()
95 {
96  gROOT->GetListOfCanvases()->Delete();
97  TCanvas *c = new TCanvas("composite shape", "Intersection boolean operation", 700, 1000);
98 
99  c->Divide(1,2,0,0);
100  c->cd(2);
101  gPad->SetPad(0,0,1,0.4);
102  c->cd(1);
103  gPad->SetPad(0,0.4,1,1);
104 
105  if (gGeoManager) delete gGeoManager;
106 
107  new TGeoManager("xtru", "poza12");
108  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
109  TGeoMedium *med = new TGeoMedium("MED",1,mat);
110  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
112 
113  // define shape components with names
114  TGeoBBox *box = new TGeoBBox("bx", 40., 40., 40.);
115  TGeoSphere *sph = new TGeoSphere("sph", 40., 45.);
116  // define named geometrical transformations with names
117  TGeoTranslation *tr = new TGeoTranslation(0., 0., 45.);
118  tr->SetName("tr");
119  // register all used transformations
120  tr->RegisterYourself();
121  // create the composite shape based on a Boolean expression
122  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph:tr * bx");
123 
124  TGeoVolume *vol = new TGeoVolume("COMP2",cs);
125  top->AddNode(vol,1);
128  top->Draw();
129  MakePicture();
130 
131  c->cd(2);
132 
133  TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
134 
135  pt->SetLineColor(1);
136 
137  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
138 
139  text->SetTextColor(2);
140  pt->AddText("----- Here is an example of boolean intersection operation : A * B");
141  pt->AddText("----- A == sphere (with inner radius non-zero), B == box");
142  pt->AddText(" ");
143  pt->SetAllWith("-----","color",4);
144  pt->SetAllWith("-----","font",72);
145  pt->SetAllWith("-----","size",0.04);
146  pt->SetTextAlign(12);
147  pt->SetTextSize(0.044);
148  pt->Draw();
149  c->cd(1);
150 }
151 
152 void s_difference()
153 {
154  gROOT->GetListOfCanvases()->Delete();
155  TCanvas *c = new TCanvas("composite shape", "Difference boolean operation", 700, 1000);
156 
157  c->Divide(1,2,0,0);
158  c->cd(2);
159  gPad->SetPad(0,0,1,0.4);
160  c->cd(1);
161  gPad->SetPad(0,0.4,1,1);
162 
163  if (gGeoManager) delete gGeoManager;
164 
165  new TGeoManager("xtru", "poza12");
166  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
167  TGeoMedium *med = new TGeoMedium("MED",1,mat);
168  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
170 
171  // define shape components with names
172  TGeoTorus *tor = new TGeoTorus("tor", 45., 15., 20., 45., 145.);
173  TGeoSphere *sph = new TGeoSphere("sph", 20., 45., 0., 180., 0., 270.);
174  // create the composite shape based on a Boolean expression
175  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "sph - tor");
176 
177  TGeoVolume *vol = new TGeoVolume("COMP3",cs);
178  top->AddNode(vol,1);
181  top->Draw();
182  MakePicture();
183 
184  c->cd(2);
185 
186  TPaveText *pt = new TPaveText(.01, .01, .99, .99);
187 
188  pt->SetLineColor(1);
189 
190  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
191 
192  text->SetTextColor(2);
193 
194  pt->AddText("----- It's an example of boolean difference: A - B");
195  pt->AddText("----- A == part of sphere (0-180, 0-270), B == partial torus (45-145)");
196  pt->AddText(" ");
197  pt->SetAllWith("-----","color",4);
198  pt->SetAllWith("-----","font",72);
199  pt->SetAllWith("-----","size",0.04);
200  pt->SetTextAlign(12);
201  pt->SetTextSize(0.044);
202  pt->Draw();
203  c->cd(1);
204 }
205 
206 void complex_1()
207 {
208  gROOT->GetListOfCanvases()->Delete();
209  TCanvas *c = new TCanvas("composite shape", "A * B - C", 700, 1000);
210 
211  c->Divide(1,2,0,0);
212  c->cd(2);
213  gPad->SetPad(0,0,1,0.4);
214  c->cd(1);
215  gPad->SetPad(0,0.4,1,1);
216 
217  if (gGeoManager) delete gGeoManager;
218 
219  new TGeoManager("xtru", "poza12");
220  TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
221  TGeoMedium *med = new TGeoMedium("MED",1,mat);
222  TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
224 
225  // define shape components with names
226  TGeoBBox *box = new TGeoBBox("box", 20., 20., 20.);
227  TGeoBBox *box1 = new TGeoBBox("box1", 5., 5., 5.);
228  TGeoSphere *sph = new TGeoSphere("sph", 5., 25.);
229  TGeoSphere *sph1 = new TGeoSphere("sph1", 1., 15.);
230  // create the composite shape based on a Boolean expression
231  TGeoTranslation *tr = new TGeoTranslation(0., 30., 0.);
232  TGeoTranslation *tr1 = new TGeoTranslation(0., 40., 0.);
233  TGeoTranslation *tr2 = new TGeoTranslation(0., 30., 0.);
234  TGeoTranslation *tr3 = new TGeoTranslation(0., 30., 0.);
235  tr->SetName("tr");
236  tr1->SetName("tr1");
237  tr2->SetName("tr2");
238  tr3->SetName("tr3");
239  // register all used transformations
240  tr->RegisterYourself();
241  tr1->RegisterYourself();
242  tr2->RegisterYourself();
243  tr3->RegisterYourself();
244 
245  TGeoCompositeShape *cs = new TGeoCompositeShape("mir", "(sph * box) + (sph1:tr - box1:tr1)");
246 
247  TGeoVolume *vol = new TGeoVolume("COMP4",cs);
248 // vol->SetLineColor(randomColor());
249  top->AddNode(vol,1);
252  top->Draw();
253  MakePicture();
254 
255  c->cd(2);
256  TPaveText *pt = new TPaveText(0.01,0.01,0.99,0.99);
257  pt->SetLineColor(1);
258  TText *text = pt->AddText("TGeoCompositeShape - composite shape class");
259  text->SetTextColor(2);
260  pt->AddText("----- (sphere * box) + (sphere - box) ");
261 
262  pt->AddText(" ");
263  pt->SetAllWith("-----","color",4);
264  pt->SetAllWith("-----","font",72);
265  pt->SetAllWith("-----","size",0.04);
266  pt->SetTextAlign(12);
267  pt->SetTextSize(0.044);
268  pt->Draw();
269  c->cd(1);
270 
271 }
272 
273 //______________________________________________________________________________
274 void AddText(TPaveText *pave, const char *datamember, Double_t value, const char *comment)
275 {
276  char line[128];
277  for (Int_t i=0; i<128; i++) line[i] = ' ';
278  memcpy(&line[0], datamember, strlen(datamember));
279  line[10] = '=';
280  char number[20];
281  sprintf(number, "%5.2f", value);
282  memcpy(&line[12], number, strlen(number));
283  line[26] = '=';
284  line[27] = '>';
285  sprintf(&line[30], "%s",comment);
286  TText *text = pave->AddText(line);
287  text->SetTextAlign(12);
288 }
289 
290 //______________________________________________________________________________
291 void AddText(TPaveText *pave, const char *datamember, Int_t value, const char *comment)
292 {
293  char line[128];
294  for (Int_t i=0; i<128; i++) line[i] = ' ';
295  memcpy(&line[0], datamember, strlen(datamember));
296  line[10] = '=';
297  char number[20];
298  sprintf(number, "%5i", value);
299  memcpy(&line[12], number, strlen(number));
300  line[26] = '=';
301  line[27] = '>';
302  sprintf(&line[30], "%s",comment);
303  TText *text = pave->AddText(line);
304  text->SetTextAlign(12);
305 }
306 
307 //______________________________________________________________________________
308 void AddText(TPaveText *pave, TObject *pf, Int_t iaxis)
309 {
310  char line[128];
311  TGeoPatternFinder *finder = (TGeoPatternFinder*)pf;
312  if (!pave || !pf) return;
313  for (Int_t i=0; i<128; i++) line[i] = ' ';
314  TGeoVolume *volume = finder->GetVolume();
315  TGeoShape *sh = volume->GetShape();
316  sprintf(line, "Division of %s on axis %d (%s)", volume->GetName(), iaxis,sh->GetAxisName(iaxis));
317  TText *text = pave->AddText(line);
318  text->SetTextColor(3);
319  text->SetTextAlign(12);
320  AddText(pave, "fNdiv",finder->GetNdiv(),"number of divisions");
321  AddText(pave, "fStart",finder->GetStart(),"start divisioning position");
322  AddText(pave, "fStep",finder->GetStep(),"division step");
323 }
324 
325 //______________________________________________________________________________
326 void raytrace() {
327  if (!gGeoManager) return;
328 
330 
331  if (!painter) return;
332 
333  painter->SetRaytracing(raytracing);
334 
335  if (!gPad) return;
336 
337  gPad->Modified();
338  gPad->Update();
339 }
340 
341 //______________________________________________________________________________
342 void help() {
343  //
344 
345  new TCanvas("chelp","Help to run demos",200,10,700,600);
346 
347  TPaveText* welcome = new TPaveText(.1,.8,.9,.97);
348  welcome->AddText("Welcome to the new geometry package");
349  welcome->SetTextFont(32);
350  welcome->SetTextColor(4);
351  welcome->SetFillColor(24);
352  welcome->Draw();
353 
354  TPaveText* hdemo = new TPaveText(.05,.05,.95,.7);
355  hdemo->SetTextAlign(12);
356  hdemo->SetTextFont(52);
357  hdemo->AddText("- Demo for building TGeo composite shapes");
358  hdemo->AddText(" ");
359  hdemo->SetAllWith("....","color",2);
360  hdemo->SetAllWith("....","font",72);
361  hdemo->SetAllWith("....","size",0.03);
362 
363  hdemo->Draw();
364 }
365 
void Show()
Show control bar.
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
void AddButton(TControlBarButton *button)
Add button.
Spherical shell class.
Definition: TGeoSphere.h:17
A Control Bar is a fully user configurable tool which provides fast access to frequently used operati...
Definition: TControlBar.h:22
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:233
The manager class for any TGeo geometry.
Definition: TGeoManager.h:38
Box class.
Definition: TGeoBBox.h:17
TLine * line
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
A polygone.
Definition: TGeoPgon.h:19
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition: TGeoVolume.h:48
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
virtual void Draw(Option_t *option="")
draw top volume according to option
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:182
Torus segment class.
Definition: TGeoTorus.h:17
Class describing translations.
Definition: TGeoMatrix.h:121
#define gROOT
Definition: TROOT.h:410
static constexpr double bar
void SetTopVolume(TGeoVolume *vol)
Set the top volume and corresponding node as starting point of the geometry.
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1829
Base class describing materials.
Definition: TGeoMaterial.h:29
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
Int_t GetNdiv() const
void box(Int_t pat, Double_t x1, Double_t y1, Double_t x2, Double_t y2)
Definition: fillpatterns.C:1
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
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.
Base class for several text objects.
Definition: TText.h:23
TGeoVolume * GetVolume() const
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
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:984
Base finder class for patterns.
Class handling Boolean composition of shapes.
Double_t GetStep() const
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
Base abstract class for all shapes.
Definition: TGeoShape.h:25
TPaveText * pt
R__EXTERN TSystem * gSystem
Definition: TSystem.h:540
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void SetRaytracing(Bool_t flag=kTRUE)=0
Double_t GetStart() const
virtual void RegisterYourself()
Register the matrix in the current manager, which will become the owner.
Definition: TGeoMatrix.cxx:526
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
virtual const char * GetAxisName(Int_t iaxis) const =0
const Bool_t kFALSE
Definition: RtypesCore.h:88
The Canvas class.
Definition: TCanvas.h:31
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:21
R__EXTERN TGeoManager * gGeoManager
Definition: TGeoManager.h:562
double Double_t
Definition: RtypesCore.h:55
TText * text
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition: TGeoMedium.h:23
Mother of all ROOT objects.
Definition: TObject.h:37
Abstract class for geometry painters.
void SetNsegments(Int_t nseg)
Set number of segments for approximating circles in drawing.
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1162
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:618
#define gPad
Definition: TVirtualPad.h:285
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
#define c(i)
Definition: RSha256.hxx:101
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
TGeoShape * GetShape() const
Definition: TGeoVolume.h:191
const Bool_t kTRUE
Definition: RtypesCore.h:87
TVirtualGeoPainter * GetGeomPainter()
Make a default painter if none present. Returns pointer to it.
virtual void SetAllWith(const char *text, Option_t *option, Double_t value)
Set attribute option for all lines containing string text.
Definition: TPaveText.cxx:879