Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ExampleWidget.cpp
Go to the documentation of this file.
1// Author: Sergey Linev, GSI 13/01/2021
2
3/*************************************************************************
4 * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#include "ExampleWidget.h"
12
13#include "TCanvas.h"
14#include "TH1.h"
15#include "TH2.h"
16#include "TROOT.h"
17#include "TMath.h"
18#include "TFile.h"
19#include "TGeoManager.h"
20#include "TGeoVolume.h"
21#include "TGeoMatrix.h"
22#include "TGeoArb8.h"
23#include "TPolyLine3D.h"
24#include "TPolyMarker3D.h"
25
26#include <ROOT/RCanvas.hxx>
28#include <ROOT/RGeomViewer.hxx>
29
30#include <QMessageBox>
31#include <QPushButton>
32
34 QWidget(parent)
35{
36 setupUi(this);
37
38 QObject::connect(InfoBtn, &QPushButton::clicked, this, &ExampleWidget::InfoButton_clicked);
39 QObject::connect(CmsBtn, &QPushButton::clicked, this, &ExampleWidget::CmsButton_clicked);
40 QObject::connect(GeoCanvasBtn, &QPushButton::clicked, this, &ExampleWidget::GeoCanvasButton_clicked);
41 QObject::connect(StandaloneBtn, &QPushButton::clicked, this, &ExampleWidget::StandaloneBtn_clicked);
42 QObject::connect(ExitBtn, &QPushButton::clicked, this, &ExampleWidget::ExitButton_clicked);
43
44
45 setAttribute(Qt::WA_DeleteOnClose);
46
47 setObjectName(name);
48
49 // create sample histogram
50
51 fHisto = new TH1F("gaus1","Example of TH1 drawing in TCanvas", 100, -5, 5);
52 fHisto->FillRandom("gaus", 10000);
53 fHisto->SetDirectory(nullptr);
54
55 gPad = fxTCanvasWidget->getCanvas();
56 fHisto->Draw();
57
58 static constexpr int nth2points = 40;
59 fHisto2 = std::make_shared<TH2I>("gaus2", "Example of TH2 drawing in RCanvas", nth2points, -5, 5, nth2points, -5, 5);
60 fHisto2->SetDirectory(nullptr);
61 for (int n=0;n<nth2points;++n) {
62 for (int k=0;k<nth2points;++k) {
63 double x = 10.*n/nth2points-5.;
64 double y = 10.*k/nth2points-5.;
65 fHisto2->SetBinContent(fHisto2->GetBin(n+1, k+1), (int) (1000*TMath::Gaus(x)*TMath::Gaus(y)));
66 }
67 }
68
69 fxRCanvasWidget->getCanvas()->Draw<ROOT::Experimental::TObjectDrawable>(fHisto2, "col");
70
72}
73
75{
76}
77
78
80{
81 auto viewer = fxGeomViewerWidget->getGeomViewer();
82
83 new TGeoManager("tubeseg", "poza3");
84 TGeoMaterial *mat = new TGeoMaterial("Al", 26.98,13,2.7);
85 TGeoMedium *med = new TGeoMedium("MED",1,mat);
86 TGeoVolume *top = gGeoManager->MakeBox("TOP",med,100,100,100);
88 TGeoVolume *vol = gGeoManager->MakeTubs("TUBESEG",med, 20,30,40,-30,270);
89 vol->SetLineColor(kRed);
90 vol->SetLineWidth(2);
91 top->AddNode(vol,1);
93// gGeoManager->SetNsegments(40);
95
96 viewer->SetGeometry(gGeoManager);
97}
98
100{
102
103 if (!TGeoManager::Import("https://root.cern/files/cms.root")) {
105 return;
106 }
107
108 auto viewer = fxGeomViewerWidget->getGeomViewer();
109
126
127 // select geometry and volume to draw
128 viewer->SetGeometry(gGeoManager, "CMSE");
129
130 // specify JSROOT draw options - here clipping on X,Y,Z axes
131 viewer->SetDrawOptions("clipxyz");
132
133 // set default limits for number of visible nodes and faces
134 // when viewer created, initial values exported from TGeoManager
135 viewer->SetLimits();
136
137 viewer->Update();
138}
139
141{
142 TGeoManager *geom = new TGeoManager("simple1", "Simple geometry");
143
144 //--- define some materials
145 TGeoMaterial *matVacuum = new TGeoMaterial("Vacuum", 0,0,0);
146 TGeoMaterial *matAl = new TGeoMaterial("Al", 26.98,13,2.7);
147// //--- define some media
148 TGeoMedium *Vacuum = new TGeoMedium("Vacuum",1, matVacuum);
149 TGeoMedium *Al = new TGeoMedium("Root Material",2, matAl);
150
151 //--- define the transformations
152 TGeoTranslation *tr1 = new TGeoTranslation(20., 0, 0.);
153 TGeoTranslation *tr2 = new TGeoTranslation(10., 0., 0.);
154 TGeoTranslation *tr3 = new TGeoTranslation(10., 20., 0.);
155 TGeoTranslation *tr4 = new TGeoTranslation(5., 10., 0.);
156 TGeoTranslation *tr5 = new TGeoTranslation(20., 0., 0.);
157 TGeoTranslation *tr6 = new TGeoTranslation(-5., 0., 0.);
158 TGeoTranslation *tr7 = new TGeoTranslation(7.5, 7.5, 0.);
159 TGeoRotation *rot1 = new TGeoRotation("rot1", 90., 0., 90., 270., 0., 0.);
160 TGeoCombiTrans *combi1 = new TGeoCombiTrans(7.5, -7.5, 0., rot1);
161 TGeoTranslation *tr8 = new TGeoTranslation(7.5, -5., 0.);
162 TGeoTranslation *tr9 = new TGeoTranslation(7.5, 20., 0.);
163 TGeoTranslation *tr10 = new TGeoTranslation(85., 0., 0.);
164 TGeoTranslation *tr11 = new TGeoTranslation(35., 0., 0.);
165 TGeoTranslation *tr12 = new TGeoTranslation(-15., 0., 0.);
166 TGeoTranslation *tr13 = new TGeoTranslation(-65., 0., 0.);
167
168 TGeoTranslation *tr14 = new TGeoTranslation(0,0,-100);
169 TGeoCombiTrans *combi2 = new TGeoCombiTrans(0,0,100,
170 new TGeoRotation("rot2",90,180,90,90,180,0));
171 TGeoCombiTrans *combi3 = new TGeoCombiTrans(100,0,0,
172 new TGeoRotation("rot3",90,270,0,0,90,180));
173 TGeoCombiTrans *combi4 = new TGeoCombiTrans(-100,0,0,
174 new TGeoRotation("rot4",90,90,0,0,90,0));
175 TGeoCombiTrans *combi5 = new TGeoCombiTrans(0,100,0,
176 new TGeoRotation("rot5",0,0,90,180,90,270));
177 TGeoCombiTrans *combi6 = new TGeoCombiTrans(0,-100,0,
178 new TGeoRotation("rot6",180,0,90,180,90,90));
179
180 //--- make the top container volume
181 Double_t worldx = 110.;
182 Double_t worldy = 50.;
183 Double_t worldz = 5.;
184 TGeoVolume *top = geom->MakeBox("TOP", Vacuum, 270., 270., 120.);
185 geom->SetTopVolume(top);
186
187 TGeoVolume *rootbox = geom->MakeBox("ROOT", Vacuum, 110., 50., 5.);
188 rootbox->SetVisibility(kFALSE);
189
190 //--- make letter 'R'
191 TGeoVolume *R = geom->MakeBox("R", Vacuum, 25., 25., 5.);
192 R->SetVisibility(kFALSE);
193 TGeoVolume *bar1 = geom->MakeBox("bar1", Al, 5., 25, 5.);
194 bar1->SetLineColor(kRed);
195 R->AddNode(bar1, 1, tr1);
196 TGeoVolume *bar2 = geom->MakeBox("bar2", Al, 5., 5., 5.);
197 bar2->SetLineColor(kRed);
198 R->AddNode(bar2, 1, tr2);
199 R->AddNode(bar2, 2, tr3);
200 TGeoVolume *tub1 = geom->MakeTubs("tub1", Al, 5., 15., 5., 90., 270.);
201 tub1->SetLineColor(kRed);
202 R->AddNode(tub1, 1, tr4);
203 TGeoVolume *bar3 = geom->MakeArb8("bar3", Al, 5.);
204 bar3->SetLineColor(kRed);
205 TGeoArb8 *arb = (TGeoArb8*)bar3->GetShape();
206 arb->SetVertex(0, 15., -5.);
207 arb->SetVertex(1, 0., -25.);
208 arb->SetVertex(2, -10., -25.);
209 arb->SetVertex(3, 5., -5.);
210 arb->SetVertex(4, 15., -5.);
211 arb->SetVertex(5, 0., -25.);
212 arb->SetVertex(6, -10., -25.);
213 arb->SetVertex(7, 5., -5.);
214 R->AddNode(bar3, 1, gGeoIdentity);
215
216 //--- make letter 'O'
217 TGeoVolume *O = geom->MakeBox("O", Vacuum, 25., 25., 5.);
219 TGeoVolume *bar4 = geom->MakeBox("bar4", Al, 5., 7.5, 5.);
220 bar4->SetLineColor(kYellow);
221 O->AddNode(bar4, 1, tr5);
222 O->AddNode(bar4, 2, tr6);
223 TGeoVolume *tub2 = geom->MakeTubs("tub1", Al, 7.5, 17.5, 5., 0., 180.);
224 tub2->SetLineColor(kYellow);
225 O->AddNode(tub2, 1, tr7);
226 O->AddNode(tub2, 2, combi1);
227
228 //--- make letter 'T'
229 TGeoVolume *T = geom->MakeBox("T", Vacuum, 25., 25., 5.);
230 T->SetVisibility(kFALSE);
231 TGeoVolume *bar5 = geom->MakeBox("bar5", Al, 5., 20., 5.);
232 bar5->SetLineColor(kBlue);
233 T->AddNode(bar5, 1, tr8);
234 TGeoVolume *bar6 = geom->MakeBox("bar6", Al, 17.5, 5., 5.);
235 bar6->SetLineColor(kBlue);
236 T->AddNode(bar6, 1, tr9);
237
238 rootbox->AddNode(R, 1, tr10);
239 rootbox->AddNode(O, 1, tr11);
240 rootbox->AddNode(O, 2, tr12);
241 rootbox->AddNode(T, 1, tr13);
242
243 top->AddNode(rootbox, 1, new TGeoRotation("rot7",180,90,0));
244
245 //--- close the geometry
246 geom->CloseGeometry();
247
248 geom->SetVisLevel(3);
249
250
251 auto canv = fxTCanvasWidget->getCanvas();
252 canv->Clear();
253
254 canv->GetListOfPrimitives()->Add(geom,"");
255
256 // create a first PolyMarker3D
257 TPolyMarker3D *pm = new TPolyMarker3D(21);
258
259 for (int n=0;n<21;n++)
260 pm->SetPoint(n, -120 + n*10, -20, 25);
261
262 // set marker size, color & style
263 pm->SetMarkerSize(2);
264 pm->SetMarkerColor(4);
265 pm->SetMarkerStyle(2);
266
267 canv->GetListOfPrimitives()->Add(pm,"");
268
269 TPolyLine3D *pl = new TPolyLine3D(5);
270
271 // set points
272 pl->SetPoint(0, -120, -20, -30);
273 pl->SetPoint(1, 80, -20, -30);
274 pl->SetPoint(2, 80, 20, -30);
275 pl->SetPoint(3, -120, 20, -30);
276 pl->SetPoint(4, -120, -20, -30);
277 // set attributes
278 pl->SetLineWidth(3);
279 pl->SetLineColor(2);
280
281 canv->GetListOfPrimitives()->Add(pl,"");
282
283 canv->Modified();
284 canv->Update();
285}
286
288{
289 QMessageBox::information(this,"QtRoot standalone example","Demo how QRootCanvas can be inserted into the QWidget");
290}
291
293{
294 if (fxTabWidget->count() > 3) {
296 fxTabWidget->setCurrentIndex(3);
297 }
298}
299
301{
302 // close and delete geo painter -
303 // not possible to have two TGeoManager at the same time
304 auto widget = fxTabWidget->widget(3);
305 fxTabWidget->removeTab(3);
306 delete widget;
307
308 fxTabWidget->setCurrentIndex(2);
310 fxTabWidget->setCurrentIndex(2);
311}
312
314{
315#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
316 gROOT->SetWebDisplay("qt5");
317#else
318 gROOT->SetWebDisplay("qt6");
319#endif
320
321 auto h1 = new TH1F("gaus_standalone","Example of standalone TCanvas", 100, -5, 5);
322 h1->FillRandom("gaus", 10000);
323 h1->SetDirectory(nullptr);
324
325 auto c = new TCanvas("standalone", "Standalone canvas");
326 h1->Draw("hist");
327
328 c->Modified();
329 c->Update();
330}
331
333{
334 // when widget closed, application automatically exit
335 close();
336}
#define c(i)
Definition RSha256.hxx:101
constexpr Bool_t kFALSE
Definition RtypesCore.h:101
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
@ kYellow
Definition Rtypes.h:66
char name[80]
Definition TGX11.cxx:110
R__EXTERN TGeoManager * gGeoManager
R__EXTERN TGeoIdentity * gGeoIdentity
Definition TGeoMatrix.h:537
#define gROOT
Definition TROOT.h:406
#define gPad
void DrawGeometryInCanvas()
void StandaloneBtn_clicked()
void InfoButton_clicked()
TH1F * fHisto
histogram for display in TCanvas
void ExitButton_clicked()
void CreateDummyGeometry()
ExampleWidget(QWidget *parent=nullptr, const char *name=nullptr)
void GeoCanvasButton_clicked()
std::shared_ptr< TH2I > fHisto2
histogram for display in RCanvas
virtual ~ExampleWidget()
void ImportCmsGeometry()
Provides v7 drawing facilities for TObject types (TGraph, TH1, TH2, etc).
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition TAttMarker.h:38
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition TAttMarker.h:40
virtual void SetMarkerSize(Size_t msize=1)
Set the marker size.
Definition TAttMarker.h:45
The Canvas class.
Definition TCanvas.h:23
static Bool_t SetCacheFileDir(std::string_view cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Sets the directory where to locally stage/cache remote files.
Definition TFile.cxx:4618
An arbitrary trapezoid with less than 8 vertices standing on two parallel planes perpendicular to Z a...
Definition TGeoArb8.h:17
virtual void SetVertex(Int_t vnum, Double_t x, Double_t y)
Set values for a given vertex.
Class describing rotation + translation.
Definition TGeoMatrix.h:317
The manager class for any TGeo geometry.
Definition TGeoManager.h:44
TGeoVolume * MakeArb8(const char *name, TGeoMedium *medium, Double_t dz, Double_t *vertices=nullptr)
Make an TGeoArb8 volume.
void SetVisLevel(Int_t level=3)
set default level down to which visualization is performed
static TGeoManager * Import(const char *filename, const char *name="", Option_t *option="")
static function Import a geometry from a gdml or ROOT file
void CloseGeometry(Option_t *option="d")
Closing geometry implies checking the geometry validity, fixing shapes with negative parameters (run-...
TGeoVolume * GetVolume(const char *name) const
Search for a named volume. All trailing blanks stripped.
void DefaultColors()
Set default volume colors according to A of material.
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 * MakeTubs(const char *name, TGeoMedium *medium, Double_t rmin, Double_t rmax, Double_t dz, Double_t phi1, Double_t phi2)
Make in one step a volume pointing to a tube segment shape with given medium.
Base class describing materials.
Media are used to store properties related to tracking and which are useful only when using geometry ...
Definition TGeoMedium.h:23
Class describing rotations.
Definition TGeoMatrix.h:168
Class describing translations.
Definition TGeoMatrix.h:116
TGeoVolume, TGeoVolumeMulti, TGeoVolumeAssembly are the volume classes.
Definition TGeoVolume.h:43
void SetVisibility(Bool_t vis=kTRUE) override
set visibility of this volume
void SetLineWidth(Width_t lwidth) override
Set the line width.
virtual TGeoNode * AddNode(TGeoVolume *vol, Int_t copy_no, TGeoMatrix *mat=nullptr, Option_t *option="")
Add a TGeoNode to the list of nodes.
void SetTransparency(Char_t transparency=0)
Definition TGeoVolume.h:376
void InvisibleAll(Bool_t flag=kTRUE)
Make volume and each of it daughters (in)visible.
void SetLineColor(Color_t lcolor) override
Set the line color.
TGeoShape * GetShape() const
Definition TGeoVolume.h:190
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:621
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:8905
virtual void FillRandom(const char *fname, Int_t ntimes=5000, TRandom *rng=nullptr)
Fill histogram following distribution in function fname.
Definition TH1.cxx:3519
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
A 3-dimensional polyline.
Definition TPolyLine3D.h:33
virtual void SetPoint(Int_t point, Double_t x, Double_t y, Double_t z)
Set point n to x, y, z.
A 3D polymarker.
void SetPoint(Int_t n, Double_t x, Double_t y, Double_t z)
Set point n to x, y, z.
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
Double_t Gaus(Double_t x, Double_t mean=0, Double_t sigma=1, Bool_t norm=kFALSE)
Calculates a gaussian function with mean and sigma.
Definition TMath.cxx:471