VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex01MCApplication.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2007 - 2014 Ivana Hrivnacova
4// All rights reserved.
5//
6// For the licensing terms see geant4_vmc/LICENSE.
7// Contact: root-vmc@cern.ch
8//-------------------------------------------------
9
10/// \file Ex01MCApplication.cxx
11/// \brief Implementation of the Ex01MCApplication class
12///
13/// Geant4 ExampleN01 adapted to Virtual Monte Carlo \n
14///
15/// \date 05/04/2002
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include "Ex01MCApplication.h"
20#include "Ex01MCStack.h"
21
22#include <Riostream.h>
23#include <TArrayD.h>
24#include <TGeoManager.h>
25#include <TGeoMaterial.h>
26#include <TGeoMatrix.h>
27#include <TInterpreter.h>
28#include <TLorentzVector.h>
29#include <TROOT.h>
30#include <TThread.h>
31#include <TVirtualMC.h>
32
33using namespace std;
34
35/// \cond CLASSIMP
36ClassImp(Ex01MCApplication)
37 /// \endcond
38
39 //_____________________________________________________________________________
40 Ex01MCApplication::Ex01MCApplication(const char* name, const char* title)
41 : TVirtualMCApplication(name, title),
42 fStack(0),
43 fMagField(0),
44 fImedAr(0),
45 fImedAl(0),
46 fImedPb(0),
47 fOldGeometry(kFALSE)
48{
49 /// Standard constructor
50 /// \param name The MC application name
51 /// \param title The MC application description
52
53 // create a user stack
54 fStack = new Ex01MCStack(100);
55
56 // create magnetic field (with zero value)
57 fMagField = new TGeoUniformMagField();
58}
59
60//_____________________________________________________________________________
63 fStack(0),
64 fMagField(0),
65 fImedAr(0),
66 fImedAl(0),
67 fImedPb(0),
68 fOldGeometry(kFALSE)
69{
70 /// Default constructor
71}
72
73//_____________________________________________________________________________
75{
76 /// Destructor
77
78 delete fStack;
79 delete fMagField;
80 delete gMC;
81}
82
83//
84// private methods
85//
86
87//_____________________________________________________________________________
89{
90 /// Construct materials using TGeo modeller
91
92 // Create Root geometry manager
93 new TGeoManager("E01_geometry", "E01 VMC example geometry");
94
95 Double_t a; // Mass of a mole in g/mole
96 Double_t z; // Atomic number
97 Double_t density; // Material density in g/cm3
98
99 a = 39.95;
100 z = 18.;
101 density = 1.782e-03;
102 TGeoMaterial* matAr = new TGeoMaterial("ArgonGas", a, z, density);
103
104 a = 26.98;
105 z = 13.;
106 density = 2.7;
107 TGeoMaterial* matAl = new TGeoMaterial("Aluminium", a, z, density);
108
109 a = 207.19;
110 z = 82.;
111 density = 11.35;
112 TGeoMaterial* matLead = new TGeoMaterial("Lead", a, z, density);
113
114 /*
115 // Set material IDs
116 // This step is needed, only if user wants to use the material Ids
117 // in his application. Be aware that the material Ids vary
118 // with each concrete MC.
119 // It is recommended to use Media Ids instead, which values
120 // set by user are preserved in all MCs
121 Int_t imat = 0;
122 matAr->SetUniqueID(imat++);
123 matAl->SetUniqueID(imat++);
124 matLead->SetUniqueID(imat++);
125 */
126
127 //
128 // Tracking medias
129 //
130
131 Double_t param[20];
132 param[0] = 0; // isvol - Not used
133 param[1] = 2; // ifield - User defined magnetic field
134 param[2] = 10.; // fieldm - Maximum field value (in kiloGauss)
135 param[3] = -20.; // tmaxfd - Maximum angle due to field deflection
136 param[4] = -0.01; // stemax - Maximum displacement for multiple scat
137 param[5] = -.3; // deemax - Maximum fractional energy loss, DLS
138 param[6] = .001; // epsil - Tracking precision
139 param[7] = -.8; // stmin
140 for (Int_t i = 8; i < 20; ++i) param[i] = 0.;
141
142 fImedAr = 1;
143 new TGeoMedium("ArgonGas", fImedAr, matAr, param);
144
145 fImedAl = 2;
146 new TGeoMedium("Aluminium", fImedAl, matAl, param);
147
148 fImedPb = 3;
149 new TGeoMedium("Lead", fImedPb, matLead, param);
150}
151
152//_____________________________________________________________________________
154{
155 /// Contruct volumes using TGeo modeller
156
157 //------------------------------ experimental hall (world volume)
158 //------------------------------ beam line along x axis
159
160 Double_t* ubuf = 0;
161
162 Double_t expHall[3];
163 expHall[0] = 300.;
164 expHall[1] = 100.;
165 expHall[2] = 100.;
166 TGeoVolume* top = gGeoManager->Volume("EXPH", "BOX", fImedAr, expHall, 3);
167 gGeoManager->SetTopVolume(top);
168
169 //------------------------------ a tracker tube
170
171 Double_t trackerTube[3];
172 trackerTube[0] = 0.;
173 trackerTube[1] = 60.;
174 trackerTube[2] = 50.;
175 gGeoManager->Volume("TRTU", "TUBE", fImedAl, trackerTube, 3);
176
177 Double_t posX = -100.;
178 Double_t posY = 0.;
179 Double_t posZ = 0.;
180 gGeoManager->Node("TRTU", 1, "EXPH", posX, posY, posZ, 0, kTRUE, ubuf);
181
182 //------------------------------ a calorimeter block
183
184 Double_t calBox[3];
185 calBox[0] = 100.;
186 calBox[1] = 50.;
187 calBox[2] = 50.;
188 gGeoManager->Volume("CALB", "BOX", fImedPb, calBox, 3);
189
190 posX = 100.;
191 posY = 0.;
192 posZ = 0.;
193 gGeoManager->Node("CALB", 1, "EXPH", posX, posY, posZ, 0, kTRUE, ubuf);
194
195 //------------------------------ calorimeter layers
196
197 Double_t layerBox[3];
198 layerBox[0] = 1.;
199 layerBox[1] = 40.;
200 layerBox[2] = 40.;
201 gGeoManager->Volume("LAYB", "BOX", fImedAl, layerBox, 3);
202
203 for (Int_t i = 0; i < 19; i++) {
204 posX = (i - 9) * 10.;
205 posY = 0.;
206 posZ = 0.;
207 gGeoManager->Node("LAYB", i, "CALB", posX, posY, posZ, 0, kTRUE, ubuf);
208 }
209
210 // close geometry
211 gGeoManager->CloseGeometry();
212
213 // notify VMC about Root geometry
214 gMC->SetRootGeometry();
215}
216
217//
218// public methods
219//
220
221//_____________________________________________________________________________
222void Ex01MCApplication::InitMC(const char* setup)
223{
224 /// Initialize MC.
225 /// The selection of the concrete MC is done in the macro.
226 /// \param setup The name of the configuration macro
227
228 if (TString(setup) != "") {
229 gROOT->LoadMacro(setup);
230 gInterpreter->ProcessLine("Config()");
231 if (!gMC) {
232 Fatal(
233 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
234 }
235 }
236
237 gMC->SetStack(fStack);
238 gMC->SetMagField(fMagField);
239 gMC->Init();
240 gMC->BuildPhysics();
241}
242
243//__________________________________________________________________________
244void Ex01MCApplication::RunMC(Int_t nofEvents)
245{
246 /// Run MC.
247 /// \param nofEvents Number of events to be processed
248
249 gMC->ProcessRun(nofEvents);
250 FinishRun();
251}
252
253//_____________________________________________________________________________
255{
256 /// Finish MC run.
257}
258
259//_____________________________________________________________________________
261{
262 return new Ex01MCApplication(GetName(), GetTitle());
263}
264
265//_____________________________________________________________________________
267{
268 gMC->SetStack(fStack);
269 gMC->SetMagField(fMagField);
270}
271
272//_____________________________________________________________________________
274{
275 /// Construct geometry using TGeo functions or
276 /// TVirtualMC functions (if oldGeometry is selected)
277
278 // Cannot use Root geometry if not supported with
279 // selected MC
280 if (!fOldGeometry && !gMC->IsRootGeometrySupported()) {
281 cerr << "Selected MC does not support TGeo geometry" << endl;
282 cerr << "Exiting program" << endl;
283 exit(1);
284 }
285
286 if (!fOldGeometry) {
287 cout << "Geometry will be defined via TGeo" << endl;
290 }
291 else {
292 cout << "Geometry will be defined via VMC" << endl;
293 Ex01DetectorConstructionOld detConstructionOld;
294 detConstructionOld.ConstructMaterials();
295 detConstructionOld.ConstructVolumes();
296 }
297}
298
299//_____________________________________________________________________________
301{
302 /// Initialize geometry.
303
304 fImedAr = gMC->MediumId("ArgonGas");
305 fImedAl = gMC->MediumId("Aluminium");
306 fImedPb = gMC->MediumId("Lead");
307}
308
309//_____________________________________________________________________________
311{
312 /// Fill the user stack (derived from TVirtualMCStack) with primary particles.
313
314 // Track ID (filled by stack)
315 Int_t ntr;
316
317 // Option: to be tracked
318 Int_t toBeDone = 1;
319
320 // Geantino
321 Int_t pdg = 0;
322
323 // Polarization
324 Double_t polx = 0.;
325 Double_t poly = 0.;
326 Double_t polz = 0.;
327
328 // Position
329 Double_t vx = -200.;
330 Double_t vy = 0.;
331 Double_t vz = 0.;
332 Double_t tof = 0.;
333
334 // Momentum
335 Double_t px, py, pz, e;
336 px = 1.;
337 py = 0.;
338 pz = 0.;
339 e = 1.;
340
341 // Add particle to stack
342 fStack->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
343 poly, polz, kPPrimary, ntr, 1., 0);
344
345 // Change direction and add particle to stack
346 px = 1.;
347 py = 0.1;
348 pz = 0.;
349 fStack->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
350 poly, polz, kPPrimary, ntr, 1., 0);
351
352 // Change direction and add particle to stack
353 px = 1.;
354 py = 0.;
355 pz = 0.1;
356 fStack->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
357 poly, polz, kPPrimary, ntr, 1., 0);
358}
359
360//_____________________________________________________________________________
362{
363 /// User actions at beginning of event.
364 /// Nothing to be done this example
365}
366
367//_____________________________________________________________________________
369{
370 /// User actions at beginning of a primary track.
371 /// Nothing to be done this example
372}
373
374//_____________________________________________________________________________
376{
377 /// User actions at beginning of each track.
378 /// Print info message.
379
380 cout << endl;
381 cout << "Starting new track" << endl;
382}
383
384//_____________________________________________________________________________
386{
387 /// User actions at each step.
388 /// Print track position, the current volume and current medium names.
389
390 TLorentzVector position;
391 gMC->TrackPosition(position);
392
393 cout << "Track " << position.X() << " " << position.Y() << " " << position.Z()
394 << " in " << gMC->CurrentVolName() << " ";
395
396 if (gMC->CurrentMedium() == fImedAr) cout << "ArgonGas";
397 if (gMC->CurrentMedium() == fImedAl) cout << "Aluminium";
398 if (gMC->CurrentMedium() == fImedPb) cout << "Lead";
399
400 cout << endl;
401
402 // // Test other TVirtualMC::TrackPosition() functions
403
404 // Double_t dx, dy, dz;
405 // gMC->TrackPosition(dx, dy, dz);
406
407 // Float_t x, y, z;
408 // gMC->TrackPosition(x, y, z);
409
410 // cout << "Track position (double): " << dx << " " << dy << " " << dz
411 // << " (float): " << x << " " << y << " " << z << endl;
412}
413
414//_____________________________________________________________________________
416{
417 /// User actions after finishing of each track
418 /// Nothing to be done this example
419}
420
421//_____________________________________________________________________________
423{
424 /// User actions after finishing of a primary track.
425 /// Nothing to be done this example
426}
427
428//_____________________________________________________________________________
430{
431 /// User actions after finishing of an event
432 /// Nothing to be done this example
433}
434
435//_____________________________________________________________________________
437{
438 /// Test (new) TVirtualMC functions:
439 /// GetTransform(), GetShape(), GetMaterial(), GetMedium()
440
441 // Get transformation of 10th layer
442 //
443 TString volPath = "/EXPH_1/CALB_1/LAYB_9";
444 TGeoHMatrix matrix;
445 Bool_t result = gMC->GetTransformation(volPath, matrix);
446 if (result) {
447 cout << "Transformation for " << volPath.Data() << ": " << endl;
448 matrix.Print();
449 }
450 else {
451 cerr << "Volume path " << volPath.Data() << " not found" << endl;
452 }
453 cout << endl;
454
455 volPath = "/EXPH_1/CALB_1/LAYB_100";
456 result = gMC->GetTransformation(volPath, matrix);
457 if (result) {
458 cout << "Transformation for " << volPath.Data() << ": " << endl;
459 matrix.Print();
460 }
461 else {
462 cerr << "Volume path " << volPath.Data() << " not found" << endl;
463 }
464 cout << endl;
465
466 volPath = "/EXPH_1/CALB_1/LAYB_9";
467 result = gMC->GetTransformation(volPath, matrix);
468 if (result) {
469 cout << "Transformation for " << volPath.Data() << ": " << endl;
470 matrix.Print();
471 }
472 else {
473 cerr << "Volume path " << volPath.Data() << " not found" << endl;
474 }
475 cout << endl;
476
477 // Get shape
478 //
479 volPath = "/EXPH_1/CALB_1/LAYB_9";
480 TString shapeType;
481 TArrayD par;
482 result = gMC->GetShape(volPath, shapeType, par);
483 if (result) {
484 cout << "Shape for " << volPath.Data() << ": " << endl;
485 cout << shapeType.Data() << " parameters: ";
486 for (Int_t ipar = 0; ipar < par.GetSize(); ipar++)
487 cout << par.At(ipar) << ", ";
488 cout << endl;
489 }
490 else {
491 cerr << "Volume path " << volPath.Data() << " not found" << endl;
492 }
493 cout << endl;
494
495 // Get material by material ID
496 //
497 TString matName;
498 Int_t imat = 2;
499 Double_t a, z, density, radl, inter;
500 TArrayD mpar;
501#if ROOT_VERSION_CODE >= ROOT_VERSION(5, 30, 0)
502 result = gMC->GetMaterial(imat, matName, a, z, density, radl, inter, mpar);
503 if (result) {
504 cout << "Material with ID " << imat << ": " << endl;
505 cout << matName.Data() << " Aeff = " << a << " Zeff = " << z
506 << " density = " << density << " radl = " << radl
507 << " inter = " << inter << endl;
508 if (mpar.GetSize() > 0) {
509 cout << " User defined parameters: ";
510 for (Int_t ipar = 0; ipar < par.GetSize(); ipar++)
511 cout << mpar.At(ipar) << ", ";
512 cout << endl;
513 }
514 }
515 else {
516 cerr << "Material with ID " << imat << " not found" << endl;
517 }
518 cout << endl;
519#endif
520
521 // Get material by volume name
522 //
523 TString volName = "LAYB";
524 mpar.Set(0);
525 result =
526 gMC->GetMaterial(volName, matName, imat, a, z, density, radl, inter, mpar);
527 if (result) {
528 cout << "Material for " << volName.Data() << " volume: " << endl;
529 cout << matName.Data() << " " << imat << " Aeff = " << a
530 << " Zeff = " << z << " density = " << density << " radl = " << radl
531 << " inter = " << inter << endl;
532 if (mpar.GetSize() > 0) {
533 cout << " User defined parameters: ";
534 for (Int_t ipar = 0; ipar < par.GetSize(); ipar++)
535 cout << mpar.At(ipar) << ", ";
536 cout << endl;
537 }
538 }
539 else {
540 cerr << "Volume " << volName.Data() << " not found" << endl;
541 }
542 cout << endl;
543
544 // Get medium
545 //
546 TString medName;
547 Int_t imed, nmat, isvol, ifield;
548 Double_t fieldm, tmaxfd, stemax, deemax, epsil, stmin;
549 result = gMC->GetMedium(volName, medName, imed, nmat, isvol, ifield, fieldm,
550 tmaxfd, stemax, deemax, epsil, stmin, mpar);
551 if (result) {
552 cout << "Medium for " << volName.Data() << " volume: " << endl;
553 cout << medName.Data() << " " << imed << " nmat = " << nmat
554 << " isvol = " << isvol << " ifield = " << ifield
555 << " fieldm = " << fieldm << " tmaxfd = " << tmaxfd
556 << " stemax = " << stemax << " deemax = " << deemax
557 << " epsil = " << epsil << " stmin = " << stmin << endl;
558 if (mpar.GetSize() > 0) {
559 cout << " User defined parameters: ";
560 for (Int_t ipar = 0; ipar < par.GetSize(); ipar++)
561 cout << mpar.At(ipar) << ", ";
562 cout << endl;
563 }
564 }
565 else {
566 cerr << "Volume " << volName.Data() << " not found" << endl;
567 }
568 cout << endl;
569
570 // Test getters non-existing position/volume
571 //
572
573 // Transformation
574 volPath = "/EXPH_1/CALB_1/LAYB_100";
575 result = gMC->GetTransformation(volPath, matrix);
576 cout << "GetTransformation: Volume path " << volPath.Data();
577 if (!result)
578 cout << " not found" << endl;
579 else
580 cout << " found" << endl;
581
582 // Shape
583 result = gMC->GetShape(volPath, shapeType, par);
584 cout << "GetShape: Volume path " << volPath.Data();
585 if (!result)
586 cout << " not found" << endl;
587 else
588 cout << " found" << endl;
589
590 // Material
591 volName = "XYZ";
592 result =
593 gMC->GetMaterial(volName, matName, imat, a, z, density, radl, inter, mpar);
594 cout << "GetMaterial: Volume name " << volName.Data();
595 if (!result)
596 cout << " not found" << endl;
597 else
598 cout << " found" << endl;
599
600 // Medium
601 result = gMC->GetMedium(volName, medName, imed, nmat, isvol, ifield, fieldm,
602 tmaxfd, stemax, deemax, epsil, stmin, mpar);
603 cout << "GetMedium: Volume name " << volName.Data();
604 if (!result)
605 cout << " not found" << endl;
606 else
607 cout << " found" << endl;
608}
Definition of the Ex01DetectorConstructionOld class.
Definition of the Ex01MCApplication class.
Definition of the Ex01MCStack class.
The old (deprecated) detector construction class.
Implementation of the TVirtualMCApplication.
Int_t fImedAl
The Aluminium medium Id.
virtual void BeginPrimary()
virtual void InitGeometry()
virtual void InitOnWorker()
Int_t fImedAr
The Argon gas medium Id.
Int_t fImedPb
The Lead medium Id.
virtual TVirtualMCApplication * CloneForWorker() const
virtual void ConstructGeometry()
virtual void FinishEvent()
TVirtualMCStack * fStack
The VMC stack.
virtual void FinishPrimary()
virtual void BeginEvent()
TVirtualMagField * fMagField
The magnetic field.
void InitMC(const char *setup)
void RunMC(Int_t nofEvents)
Bool_t fOldGeometry
Option for geometry definition.
virtual void GeneratePrimaries()
Implementation of the TVirtualMCStack interface.
Definition Ex01MCStack.h:33