VMC Examples
Version 6.8
Toggle main menu visibility
Loading...
Searching...
No Matches
examples
E06
src
Ex06MCApplication.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 Ex06MCApplication.cxx
11
/// \brief Implementation of the Ex06MCApplication class
12
///
13
/// Geant4 ExampleN06 adapted to Virtual Monte Carlo
14
///
15
/// \date 16/05/2005
16
/// \author I. Hrivnacova; IPN, Orsay
17
18
#include <Riostream.h>
19
#include <TGeoManager.h>
20
#include <TGeoUniformMagField.h>
21
#include <TInterpreter.h>
22
#include <TLorentzVector.h>
23
#include <TROOT.h>
24
#include <TVirtualGeoTrack.h>
25
#include <TVirtualMC.h>
26
27
#include "Ex03MCStack.h"
28
#include "
Ex06DetectorConstruction.h
"
29
#include "
Ex06DetectorConstructionOld.h
"
30
#include "
Ex06MCApplication.h
"
31
#include "
Ex06PrimaryGenerator.h
"
32
33
using namespace
std;
34
35
/// \cond CLASSIMP
36
ClassImp(
Ex06MCApplication
)
37
/// \endcond
38
39
//_____________________________________________________________________________
40
Ex06MCApplication
::
Ex06MCApplication
(const
char
* name, const
char
* title)
41
:
TVirtualMCApplication
(name, title),
42
fGammaCounter
(0),
43
fFeedbackCounter
(0),
44
fRunGammaCounter
(0),
45
fRunFeedbackCounter
(0),
46
fVerbose
(0),
47
fStack
(0),
48
fMagField
(0),
49
fDetConstruction
(0),
50
fPrimaryGenerator
(0),
51
fOldGeometry
(kFALSE),
52
fTestStackPopper
(kFALSE),
53
fIsMaster
(kTRUE)
54
{
55
/// Standard constructor
56
/// \param name The MC application name
57
/// \param title The MC application description
58
59
// Create a user stack
60
fStack
=
new
Ex03MCStack
(1000);
61
62
// create magnetic field (with zero value)
63
fMagField
=
new
TGeoUniformMagField();
64
65
// Create detector construction
66
fDetConstruction
=
new
Ex06DetectorConstruction
();
67
68
// Create a primary generator
69
fPrimaryGenerator
=
new
Ex06PrimaryGenerator
(
fStack
);
70
}
71
72
//_____________________________________________________________________________
73
Ex06MCApplication::Ex06MCApplication
(
const
Ex06MCApplication
& origin)
74
:
TVirtualMCApplication
(origin.GetName(), origin.GetTitle()),
75
fGammaCounter
(0),
76
fFeedbackCounter
(0),
77
fRunGammaCounter
(0),
78
fRunFeedbackCounter
(0),
79
fVerbose
(origin.
fVerbose
),
80
fStack
(0),
81
fMagField
(0),
82
fDetConstruction
(origin.
fDetConstruction
),
83
fPrimaryGenerator
(0),
84
fOldGeometry
(origin.
fOldGeometry
),
85
fTestStackPopper
(origin.
fTestStackPopper
),
86
fIsMaster
(kFALSE)
87
{
88
/// Copy constructor (for clonig on worker thread in MT mode).
89
/// \param origin The source object (on master).
90
91
// Create a user stack
92
fStack
=
new
Ex03MCStack
(1000);
93
94
// create magnetic field (with zero value)
95
// TODO: check copying
96
fMagField
=
new
TGeoUniformMagField();
97
98
// Create a primary generator
99
fPrimaryGenerator
=
100
new
Ex06PrimaryGenerator
(*(origin.
fPrimaryGenerator
),
fStack
);
101
}
102
103
//_____________________________________________________________________________
104
Ex06MCApplication::Ex06MCApplication
()
105
:
TVirtualMCApplication
(),
106
fGammaCounter
(0),
107
fFeedbackCounter
(0),
108
fRunGammaCounter
(0),
109
fRunFeedbackCounter
(0),
110
fVerbose
(0),
111
fStack
(0),
112
fMagField
(0),
113
fDetConstruction
(0),
114
fPrimaryGenerator
(0),
115
fOldGeometry
(kFALSE),
116
fTestStackPopper
(kFALSE),
117
fIsMaster
(kTRUE)
118
{
119
/// Default constructor
120
}
121
122
//_____________________________________________________________________________
123
Ex06MCApplication::~Ex06MCApplication
()
124
{
125
/// Destructor
126
127
// cout << "Ex06MCApplication::~Ex06MCApplication " << this << endl;
128
129
delete
fStack
;
130
delete
fMagField
;
131
if
(
fIsMaster
)
delete
fDetConstruction
;
132
delete
fPrimaryGenerator
;
133
delete
gMC;
134
135
// cout << "Done Ex06MCApplication::~Ex06MCApplication " << this << endl;
136
}
137
138
//
139
// public methods
140
//
141
142
//_____________________________________________________________________________
143
void
Ex06MCApplication::InitMC
(
const
char
* setup)
144
{
145
/// Initialize MC.
146
/// The selection of the concrete MC is done in the macro.
147
/// \param setup The name of the configuration macro
148
149
fVerbose
.InitMC();
150
151
if
(TString(setup) !=
""
) {
152
gROOT->LoadMacro(setup);
153
gInterpreter->ProcessLine(
"Config()"
);
154
if
(!gMC) {
155
Fatal(
156
"InitMC"
,
"Processing Config() has failed. (No MC is instantiated.)"
);
157
}
158
}
159
160
gMC->SetStack(
fStack
);
161
gMC->SetMagField(
fMagField
);
162
gMC->Init();
163
gMC->BuildPhysics();
164
}
165
166
//_____________________________________________________________________________
167
void
Ex06MCApplication::RunMC
(Int_t nofEvents)
168
{
169
/// Run MC.
170
/// \param nofEvents Number of events to be processed
171
172
fVerbose
.RunMC(nofEvents);
173
174
gMC->ProcessRun(nofEvents);
175
176
FinishRun
();
177
}
178
179
//_____________________________________________________________________________
180
TVirtualMCApplication
*
Ex06MCApplication::CloneForWorker
()
const
181
{
182
return
new
Ex06MCApplication
(*
this
);
183
}
184
185
//_____________________________________________________________________________
186
void
Ex06MCApplication::InitOnWorker
()
187
{
188
// cout << "Ex06MCApplication::InitForWorker " << this << endl;
189
190
// Set data to MC
191
gMC->SetStack(
fStack
);
192
gMC->SetMagField(
fMagField
);
193
}
194
195
//_____________________________________________________________________________
196
void
Ex06MCApplication::Merge
(
TVirtualMCApplication
* localMCApplication)
197
{
198
// cout << "Ex06MCApplication::Merge " << this << endl;
199
200
Ex06MCApplication
* ex06LocalMCApplication =
201
static_cast<
Ex06MCApplication
*
>
(localMCApplication);
202
203
fRunGammaCounter
+= ex06LocalMCApplication->
fRunGammaCounter
;
204
fRunFeedbackCounter
+= ex06LocalMCApplication->
fRunFeedbackCounter
;
205
}
206
207
//_____________________________________________________________________________
208
void
Ex06MCApplication::ConstructGeometry
()
209
{
210
/// Construct geometry using detector contruction class.
211
/// The detector contruction class is using TGeo functions or
212
/// TVirtualMC functions (if oldGeometry is selected)
213
214
fVerbose
.ConstructGeometry();
215
216
// Cannot use Root geometry if not supported with
217
// selected MC
218
if
(!
fOldGeometry
&& !gMC->IsRootGeometrySupported()) {
219
cerr <<
"Selected MC does not support TGeo geometry"
<< endl;
220
cerr <<
"Exiting program"
<< endl;
221
exit(1);
222
}
223
224
if
(!
fOldGeometry
) {
225
cout <<
"Geometry will be defined via TGeo"
<< endl;
226
fDetConstruction
->ConstructMaterials();
227
fDetConstruction
->ConstructGeometry();
228
}
229
else
{
230
cout <<
"Geometry will be defined via VMC"
<< endl;
231
Ex06DetectorConstructionOld
detConstructionOld;
232
detConstructionOld.
ConstructMaterials
();
233
detConstructionOld.
ConstructGeometry
();
234
}
235
}
236
237
//_____________________________________________________________________________
238
void
Ex06MCApplication::ConstructOpGeometry
()
239
{
240
/// Define material optical properties
241
242
fVerbose
.ConstructGeometry();
243
244
fDetConstruction
->ConstructOpGeometry();
245
}
246
247
//_____________________________________________________________________________
248
void
Ex06MCApplication::InitGeometry
()
249
{
250
/// Initialize geometry
251
252
fVerbose
.InitGeometry();
253
}
254
255
//_____________________________________________________________________________
256
void
Ex06MCApplication::GeneratePrimaries
()
257
{
258
/// Fill the user stack (derived from TVirtualMCStack) with primary particles.
259
260
fVerbose
.GeneratePrimaries();
261
262
fPrimaryGenerator
->GeneratePrimaries();
263
}
264
265
//_____________________________________________________________________________
266
void
Ex06MCApplication::BeginEvent
()
267
{
268
/// User actions at beginning of event
269
270
fVerbose
.BeginEvent();
271
272
fGammaCounter
= 0;
273
fFeedbackCounter
= 0;
274
}
275
276
//_____________________________________________________________________________
277
void
Ex06MCApplication::BeginPrimary
()
278
{
279
/// User actions at beginning of a primary track
280
281
fVerbose
.BeginPrimary();
282
}
283
284
//_____________________________________________________________________________
285
void
Ex06MCApplication::PreTrack
()
286
{
287
/// User actions at beginning of each track
288
289
fVerbose
.PreTrack();
290
291
if
(gMC->TrackPid() == 50000050) {
292
fGammaCounter
++;
293
fRunGammaCounter
++;
294
}
295
if
(gMC->TrackPid() == 50000051) {
296
fFeedbackCounter
++;
297
fRunFeedbackCounter
++;
298
}
299
}
300
301
//_____________________________________________________________________________
302
void
Ex06MCApplication::Stepping
()
303
{
304
/// User actions at each step
305
306
fVerbose
.Stepping();
307
308
// Work around for Fluka VMC, which does not call
309
// MCApplication::PreTrack()
310
//
311
static
Int_t trackId = 0;
312
if
(TString(gMC->GetName()) ==
"TFluka"
&&
313
gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
314
315
fVerbose
.PreTrack();
316
trackId = gMC->GetStack()->GetCurrentTrackNumber();
317
if
(gMC->TrackPid() == 50000050)
fGammaCounter
++;
318
if
(gMC->TrackPid() == 50000051)
fFeedbackCounter
++;
319
}
320
321
if
(!
fTestStackPopper
)
return
;
322
323
// Stack popper test
324
// Add 1 feedback photon (50000051, fixed momentum) when a charged track
325
// enters TANK and 3 feedback photons (momentum in opposite direction to
326
// parent photon)
327
// when a photon is stopped in TANK
328
329
// Charged particles entering in TANK
330
TString volName = gMC->CurrentVolName();
331
if
(volName !=
"TANK"
)
return
;
332
333
if
((gMC->TrackCharge() != 0.) && (gMC->IsTrackEntering())) {
334
// 1 keV
335
Double_t energy = 1e-06;
336
// TLorentzVector momentum(energy, 0., 0., energy);
337
// workaround for a problem in Geant4 11 in G4OpBoundaryProcess
338
TLorentzVector momentum(
339
energy, 0.1 * energy, 0.1 * energy, 0.98994949 * energy);
340
GenerateFeedback
(1, momentum);
341
}
342
343
if
((gMC->TrackPid() == 50000050) && (gMC->Edep() > 0.)) {
344
TLorentzVector momentum;
345
gMC->TrackMomentum(momentum);
346
momentum = -1. * momentum;
347
GenerateFeedback
(3, momentum);
348
}
349
}
350
351
//_____________________________________________________________________________
352
void
Ex06MCApplication::GenerateFeedback
(
353
Int_t nofPhotons, TLorentzVector momentum)
354
{
355
/// Generate FeedBack photons for the current particle.
356
357
for
(Int_t i = 0; i < nofPhotons; ++i) {
358
// same position as the parent track
359
TLorentzVector position;
360
gMC->TrackPosition(position);
361
// Feedback photon
362
Int_t pdgEncoding = 50000051;
363
// Fixed polarization
364
Double_t polarization[3];
365
polarization[0] = 0.3;
366
polarization[1] = 0.4;
367
polarization[2] = 0.866025403784438597;
368
369
Int_t ntrack;
370
gMC->GetStack()->PushTrack(1, gMC->GetStack()->GetCurrentTrackNumber(),
371
pdgEncoding, momentum.X(), momentum.Y(), momentum.Z(), momentum.T(),
372
position.X(), position.Y(), position.Z(), position.T(), polarization[0],
373
polarization[1], polarization[2], kPFeedBackPhoton, ntrack, 1.0, 0);
374
}
375
}
376
377
//_____________________________________________________________________________
378
void
Ex06MCApplication::PostTrack
()
379
{
380
/// User actions after finishing of each track
381
382
fVerbose
.PostTrack();
383
}
384
385
//_____________________________________________________________________________
386
void
Ex06MCApplication::FinishPrimary
()
387
{
388
/// User actions after finishing of a primary track
389
390
fVerbose
.FinishPrimary();
391
}
392
393
//_____________________________________________________________________________
394
void
Ex06MCApplication::FinishEvent
()
395
{
396
/// User actions after finishing of an event
397
398
fVerbose
.FinishEvent();
399
400
// Geant3 + TGeo
401
// (use TGeo functions for visualization)
402
if
(TString(gMC->GetName()) ==
"TGeant3TGeo"
) {
403
404
// Draw volume
405
gGeoManager->SetVisOption(0);
406
gGeoManager->SetTopVisible();
407
gGeoManager->GetTopVolume()->Draw();
408
409
// Draw tracks (if filled)
410
// Available when this feature is activated via
411
// gMC->SetCollectTracks(kTRUE);
412
if
(gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
413
((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
414
415
gGeoManager->DrawTracks(
"/*"
);
// this means all tracks
416
}
417
}
418
419
cout <<
"Number of optical photons produced in this event : "
<<
fGammaCounter
420
<< endl;
421
422
if
(
fTestStackPopper
) {
423
cout <<
"Number of feedback photons produced in this event : "
424
<<
fFeedbackCounter
<< endl;
425
}
426
fStack
->Reset();
427
}
428
429
//_____________________________________________________________________________
430
void
Ex06MCApplication::FinishRun
()
431
{
432
/// User actions after finishing of a run
433
434
fVerbose
.FinishRun();
435
436
cout <<
"Number of optical photons produced in this run : "
437
<<
fRunGammaCounter
<< endl;
438
439
if
(
fTestStackPopper
) {
440
cout <<
"Number of feedback photons produced in this run : "
441
<<
fRunFeedbackCounter
<< endl;
442
}
443
}
Ex06DetectorConstructionOld.h
Definition of the Ex06DetectorConstructionOld class.
Ex06DetectorConstruction.h
Definition of the Ex06DetectorConstruction class.
Ex06MCApplication.h
Definition of the Ex06MCApplication class.
Ex06PrimaryGenerator.h
Definition of the Ex06PrimaryGenerator class.
Ex03MCStack
Implementation of the TVirtualMCStack interface.
Definition
Ex03MCStack.h:36
Ex06DetectorConstructionOld
The detector construction (via TGeo ).
Definition
Ex06DetectorConstructionOld.h:35
Ex06DetectorConstructionOld::ConstructMaterials
void ConstructMaterials()
Definition
Ex06DetectorConstructionOld.cxx:54
Ex06DetectorConstructionOld::ConstructGeometry
void ConstructGeometry()
Definition
Ex06DetectorConstructionOld.cxx:91
Ex06DetectorConstruction
The detector construction (via TGeo ).
Definition
Ex06DetectorConstruction.h:35
Ex06MCApplication
Implementation of the TVirtualMCApplication.
Definition
Ex06MCApplication.h:38
Ex06MCApplication::fTestStackPopper
Bool_t fTestStackPopper
Option for stack popper test.
Definition
Ex06MCApplication.h:96
Ex06MCApplication::RunMC
void RunMC(Int_t nofEvents)
Definition
Ex06MCApplication.cxx:167
Ex06MCApplication::GeneratePrimaries
virtual void GeneratePrimaries()
Definition
Ex06MCApplication.cxx:256
Ex06MCApplication::Ex06MCApplication
Ex06MCApplication(const char *name, const char *title)
Definition
Ex06MCApplication.cxx:40
Ex06MCApplication::PreTrack
virtual void PreTrack()
Definition
Ex06MCApplication.cxx:285
Ex06MCApplication::fFeedbackCounter
Int_t fFeedbackCounter
Feedback photons counter.
Definition
Ex06MCApplication.h:87
Ex06MCApplication::fMagField
TVirtualMagField * fMagField
The magnetic field.
Definition
Ex06MCApplication.h:92
Ex06MCApplication::GenerateFeedback
void GenerateFeedback(Int_t nofPhotons, TLorentzVector momentum)
Definition
Ex06MCApplication.cxx:352
Ex06MCApplication::fRunGammaCounter
Int_t fRunGammaCounter
Optical photons counter2.
Definition
Ex06MCApplication.h:88
Ex06MCApplication::fRunFeedbackCounter
Int_t fRunFeedbackCounter
Feedback photons counter2.
Definition
Ex06MCApplication.h:89
Ex06MCApplication::BeginPrimary
virtual void BeginPrimary()
Definition
Ex06MCApplication.cxx:277
Ex06MCApplication::Stepping
virtual void Stepping()
Definition
Ex06MCApplication.cxx:302
Ex06MCApplication::fIsMaster
Bool_t fIsMaster
If is on master thread.
Definition
Ex06MCApplication.h:97
Ex06MCApplication::~Ex06MCApplication
virtual ~Ex06MCApplication()
Definition
Ex06MCApplication.cxx:123
Ex06MCApplication::InitMC
void InitMC(const char *setup)
Definition
Ex06MCApplication.cxx:143
Ex06MCApplication::fGammaCounter
Int_t fGammaCounter
Optical photons counter.
Definition
Ex06MCApplication.h:86
Ex06MCApplication::fDetConstruction
Ex06DetectorConstruction * fDetConstruction
Dector construction.
Definition
Ex06MCApplication.h:93
Ex06MCApplication::PostTrack
virtual void PostTrack()
Definition
Ex06MCApplication.cxx:378
Ex06MCApplication::InitGeometry
virtual void InitGeometry()
Definition
Ex06MCApplication.cxx:248
Ex06MCApplication::fOldGeometry
Bool_t fOldGeometry
Option for geometry definition.
Definition
Ex06MCApplication.h:95
Ex06MCApplication::Ex06MCApplication
Ex06MCApplication()
Definition
Ex06MCApplication.cxx:104
Ex06MCApplication::BeginEvent
virtual void BeginEvent()
Definition
Ex06MCApplication.cxx:266
Ex06MCApplication::ConstructGeometry
virtual void ConstructGeometry()
Definition
Ex06MCApplication.cxx:208
Ex06MCApplication::CloneForWorker
virtual TVirtualMCApplication * CloneForWorker() const
Definition
Ex06MCApplication.cxx:180
Ex06MCApplication::fStack
Ex03MCStack * fStack
VMC stack.
Definition
Ex06MCApplication.h:91
Ex06MCApplication::InitOnWorker
virtual void InitOnWorker()
Definition
Ex06MCApplication.cxx:186
Ex06MCApplication::FinishRun
void FinishRun()
Definition
Ex06MCApplication.cxx:430
Ex06MCApplication::fPrimaryGenerator
Ex06PrimaryGenerator * fPrimaryGenerator
Primary generator.
Definition
Ex06MCApplication.h:94
Ex06MCApplication::fVerbose
TMCVerbose fVerbose
VMC verbose helper.
Definition
Ex06MCApplication.h:90
Ex06MCApplication::FinishEvent
virtual void FinishEvent()
Definition
Ex06MCApplication.cxx:394
Ex06MCApplication::FinishPrimary
virtual void FinishPrimary()
Definition
Ex06MCApplication.cxx:386
Ex06MCApplication::Merge
virtual void Merge(TVirtualMCApplication *localMCApplication)
Definition
Ex06MCApplication.cxx:196
Ex06MCApplication::ConstructOpGeometry
virtual void ConstructOpGeometry()
Definition
Ex06MCApplication.cxx:238
Ex06PrimaryGenerator
The primary generator.
Definition
Ex06PrimaryGenerator.h:34
TVirtualMCApplication
Generated on
for VMC Examples by
1.17.0