VMC Examples
Version 6.8
Toggle main menu visibility
Loading...
Searching...
No Matches
examples
E02
src
Ex02MCApplication.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 Ex02MCApplication.cxx
11
/// \brief Implementation of the Ex02MCApplication class
12
///
13
/// Geant4 ExampleN02 adapted to Virtual Monte Carlo
14
///
15
/// \date 21/04/2002
16
/// \author I. Hrivnacova; IPN, Orsay
17
18
#include "
Ex02MCApplication.h
"
19
#include "
Ex02DetectorConstructionOld.h
"
20
#include "
Ex02MCStack.h
"
21
#include "
Ex02MagField.h
"
22
23
#include <Riostream.h>
24
#include <TGeoManager.h>
25
#include <TInterpreter.h>
26
#include <TMCRootManager.h>
27
#include <TPDGCode.h>
28
#include <TROOT.h>
29
#include <TVirtualGeoTrack.h>
30
#include <TVirtualMC.h>
31
32
using namespace
std;
33
34
/// \cond CLASSIMP
35
ClassImp(
Ex02MCApplication
)
36
/// \endcond
37
38
//_____________________________________________________________________________
39
Ex02MCApplication
::
Ex02MCApplication
(const
char
* name, const
char
* title)
40
:
TVirtualMCApplication
(name, title),
41
fRootManager
(0),
42
fStack
(0),
43
fDetConstruction
(),
44
fTrackerSD
(0),
45
fMagField
(0),
46
fOldGeometry
(kFALSE)
47
{
48
/// Standard constructor
49
/// \param name The MC application name
50
/// \param title The MC application description
51
52
// cout << "Ex02MCApplication::Ex02MCApplication " << this << endl;
53
54
// Create application data
55
56
// Create SD
57
fTrackerSD
=
new
Ex02TrackerSD
(
"Tracker Chamber"
);
58
// Create a user stack
59
fStack
=
new
Ex02MCStack
(100);
60
// Constant magnetic field (in kiloGauss)
61
fMagField
=
new
Ex02MagField
(20., 0., 0.);
62
// It si also possible to use TGeoUniformMagField class:
63
// fMagField = new TGeoUniformMagField(20., 0., 0.);
64
}
65
66
//_____________________________________________________________________________
67
Ex02MCApplication::Ex02MCApplication
(
const
Ex02MCApplication
& origin)
68
:
TVirtualMCApplication
(origin.GetName(), origin.GetTitle()),
69
fRootManager
(0),
70
fStack
(0),
71
fDetConstruction
(origin.
fDetConstruction
),
72
fTrackerSD
(0),
73
fMagField
(0),
74
fOldGeometry
(kFALSE)
75
{
76
/// Copy constructor (for clonig on worker thread in MT mode).
77
/// \param origin The source object (on master).
78
79
// cout << "Ex02MCApplication::Ex02MCApplication " << this << endl;
80
81
// Create application data
82
83
// Create SD
84
fTrackerSD
=
new
Ex02TrackerSD
(*(origin.
fTrackerSD
));
85
// Create a user stack
86
fStack
=
new
Ex02MCStack
(100);
87
// Constant magnetic field (in kiloGauss)
88
fMagField
=
new
Ex02MagField
(20., 0., 0.);
89
// It si also possible to use TGeoUniformMagField class:
90
// fMagField = new TGeoUniformMagField(20., 0., 0.);
91
}
92
93
//_____________________________________________________________________________
94
Ex02MCApplication::Ex02MCApplication
()
95
:
TVirtualMCApplication
(),
96
fRootManager
(0),
97
fStack
(0),
98
fDetConstruction
(),
99
fTrackerSD
(),
100
fMagField
(0),
101
fOldGeometry
(kFALSE)
102
{
103
/// Default constructor
104
}
105
106
//_____________________________________________________________________________
107
Ex02MCApplication::~Ex02MCApplication
()
108
{
109
/// Destructor
110
111
// cout << "Ex02MCApplication::~Ex02MCApplication " << this << endl;
112
113
delete
fRootManager
;
114
delete
fStack
;
115
delete
fTrackerSD
;
116
delete
fMagField
;
117
delete
gMC;
118
119
// cout << "Done Ex02MCApplication::~Ex02MCApplication " << this << endl;
120
}
121
122
//
123
// private methods
124
//
125
//_____________________________________________________________________________
126
void
Ex02MCApplication::RegisterStack
()
const
127
{
128
/// Register stack in the Root manager.
129
130
if
(
fRootManager
) {
131
// cout << "Ex02MCApplication::RegisterStack: " << endl;
132
fRootManager
->Register(
"stack"
,
"Ex02MCStack"
, &
fStack
);
133
}
134
}
135
136
//
137
// public methods
138
//
139
140
//_____________________________________________________________________________
141
void
Ex02MCApplication::InitMC
(
const
char
* setup)
142
{
143
/// Initialize MC from Config.C macro
144
/// The selection of the concrete MC is done in the macro.
145
/// \param setup The name of the configuration macro
146
147
if
(TString(setup) !=
""
) {
148
gROOT->LoadMacro(setup);
149
gInterpreter->ProcessLine(
"Config()"
);
150
if
(!gMC) {
151
Fatal(
152
"InitMC"
,
"Processing Config() has failed. (No MC is instantiated.)"
);
153
}
154
}
155
156
// MT support available from root v 5.34/18
157
#if ROOT_VERSION_CODE >= 336402
158
// Create Root manager
159
if
(!gMC->IsMT()) {
160
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite);
161
// fRootManager->SetDebug(true);
162
}
163
#else
164
// Create Root manager
165
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite);
166
// fRootManager->SetDebug(true);
167
#endif
168
169
// Set data to MC
170
gMC->SetStack(
fStack
);
171
gMC->SetMagField(
fMagField
);
172
173
// Init MC
174
gMC->Init();
175
gMC->BuildPhysics();
176
177
RegisterStack
();
178
}
179
180
//_____________________________________________________________________________
181
void
Ex02MCApplication::RunMC
(Int_t nofEvents)
182
{
183
/// Run MC.
184
/// \param nofEvents Number of events to be processed
185
186
gMC->ProcessRun(nofEvents);
187
FinishRun
();
188
}
189
190
//_____________________________________________________________________________
191
void
Ex02MCApplication::FinishRun
()
192
{
193
/// Finish MC run.
194
195
// cout << "Ex02MCApplication::FinishRun: " << endl;
196
if
(
fRootManager
) {
197
// fRootManager->WriteAndClose();
198
fRootManager
->WriteAll();
199
fRootManager
->Close();
200
}
201
}
202
203
//_____________________________________________________________________________
204
TVirtualMCApplication
*
Ex02MCApplication::CloneForWorker
()
const
205
{
206
return
new
Ex02MCApplication
(*
this
);
207
}
208
209
//_____________________________________________________________________________
210
void
Ex02MCApplication::InitOnWorker
()
211
{
212
// cout << "Ex02MCApplication::InitForWorker " << this << endl;
213
214
// Create Root manager
215
Int_t threadRank = 1;
216
// The real thread rank will be set in MCRootManager
217
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite, threadRank);
218
// fRootManager->SetDebug(true);
219
220
// Set data to MC
221
gMC->SetStack(
fStack
);
222
gMC->SetMagField(
fMagField
);
223
224
RegisterStack
();
225
}
226
227
//_____________________________________________________________________________
228
void
Ex02MCApplication::FinishRunOnWorker
()
229
{
230
// cout << "Ex02MCApplication::FinishWorkerRun: " << endl;
231
if
(
fRootManager
) {
232
fRootManager
->WriteAll();
233
fRootManager
->Close();
234
}
235
}
236
237
//_____________________________________________________________________________
238
void
Ex02MCApplication::ConstructGeometry
()
239
{
240
/// Construct geometry using detector contruction class.
241
/// The detector contruction class is using TGeo functions or
242
/// TVirtualMC functions (if oldGeometry is selected)
243
244
// Cannot use Root geometry if not supported with
245
// selected MC
246
if
(!
fOldGeometry
&& !gMC->IsRootGeometrySupported()) {
247
cerr <<
"Selected MC does not support TGeo geometry"
<< endl;
248
cerr <<
"Exiting program"
<< endl;
249
exit(1);
250
}
251
252
if
(!
fOldGeometry
) {
253
cout <<
"Geometry will be defined via TGeo"
<< endl;
254
fDetConstruction
.ConstructMaterials();
255
fDetConstruction
.ConstructGeometry();
256
}
257
else
{
258
cout <<
"Geometry will be defined via VMC"
<< endl;
259
Ex02DetectorConstructionOld
detConstructionOld;
260
detConstructionOld.
ConstructMaterials
();
261
detConstructionOld.
ConstructGeometry
();
262
}
263
}
264
265
//_____________________________________________________________________________
266
void
Ex02MCApplication::InitGeometry
()
267
{
268
/// Initialize geometry
269
270
fTrackerSD
->Initialize();
271
}
272
273
//_____________________________________________________________________________
274
void
Ex02MCApplication::GeneratePrimaries
()
275
{
276
/// Fill the user stack (derived from TVirtualMCStack) with primary particles.
277
278
// Track ID (filled by stack)
279
Int_t ntr;
280
281
// Option: to be tracked
282
Int_t toBeDone = 1;
283
284
// Particle type
285
// Int_t pdg = 0; // geantino
286
Int_t pdg = kProton;
287
288
// Polarization
289
Double_t polx = 0.;
290
Double_t poly = 0.;
291
Double_t polz = 0.;
292
293
// Position
294
Double_t vx = 0.;
295
Double_t vy = 0.;
296
Double_t vz = -0.5 * (
fDetConstruction
.GetWorldFullLength());
297
Double_t tof = 0.;
298
299
// Energy
300
Double_t kinEnergy = 3.0;
301
Double_t mass = 0.9382723;
302
Double_t e = mass + kinEnergy;
303
304
// Momentum
305
Double_t px, py, pz;
306
px = 0.;
307
py = 0.;
308
pz = sqrt(e * e - mass * mass);
309
310
// Add particle to stack
311
fStack
->PushTrack(toBeDone, -1, pdg, px, py, pz, e, vx, vy, vz, tof, polx,
312
poly, polz, kPPrimary, ntr, 1., 0);
313
}
314
315
//_____________________________________________________________________________
316
void
Ex02MCApplication::BeginEvent
()
317
{
318
/// User actions at beginning of event.
319
/// Nothing to be done this example
320
321
fStack
->SetObjectNumber();
322
}
323
324
//_____________________________________________________________________________
325
void
Ex02MCApplication::BeginPrimary
()
326
{
327
/// User actions at beginning of a primary track.
328
/// Nothing to be done this example
329
}
330
331
//_____________________________________________________________________________
332
void
Ex02MCApplication::PreTrack
()
333
{
334
/// User actions at beginning of each track.
335
/// Nothing to be done this example
336
}
337
338
//_____________________________________________________________________________
339
void
Ex02MCApplication::Stepping
()
340
{
341
/// User actions at each step
342
343
fTrackerSD
->ProcessHits();
344
}
345
346
//_____________________________________________________________________________
347
void
Ex02MCApplication::PostTrack
()
348
{
349
/// User actions at each step.
350
/// Nothing to be done this example
351
}
352
353
//_____________________________________________________________________________
354
void
Ex02MCApplication::FinishPrimary
()
355
{
356
/// User actions after finishing of a primary track.
357
/// Nothing to be done this example
358
}
359
360
//_____________________________________________________________________________
361
void
Ex02MCApplication::FinishEvent
()
362
{
363
/// User actions after finishing of an event
364
/// Nothing to be done this example
365
366
// Geant4 own visualization is activated via G4 macro (g4config.in)
367
368
// TGeo visualization
369
if
(gGeoManager && gGeoManager->GetListOfTracks() &&
370
gGeoManager->GetTrack(0) &&
371
((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
372
373
gGeoManager->SetVisOption(0);
374
gGeoManager->SetTopVisible();
375
gGeoManager->DrawTracks(
"/*"
);
// this means all tracks
376
}
377
378
fRootManager
->Fill();
379
380
fTrackerSD
->EndOfEvent();
381
382
fStack
->Print();
383
fStack
->Reset();
384
}
385
386
//_____________________________________________________________________________
387
void
Ex02MCApplication::ReadEvent
(Int_t i)
388
{
389
/// Read \em i -th event and prints hits.
390
/// \param i The number of event to be read
391
392
if
(!
fRootManager
) {
393
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kRead);
394
}
395
396
fTrackerSD
->Register();
397
RegisterStack
();
398
fRootManager
->ReadEvent(i);
399
400
fStack
->Print();
401
fTrackerSD
->Print();
402
}
Ex02DetectorConstructionOld.h
Definition of the Ex02DetectorConstructionOld class.
Ex02MCApplication.h
Definition of the Ex02MCApplication class.
Ex02MCStack.h
Definition of the Ex02MCStack class.
Ex02MagField.h
Definition of the Ex02MagField class.
Ex02DetectorConstructionOld
The old detector construction (via VMC functions).
Definition
Ex02DetectorConstructionOld.h:32
Ex02DetectorConstructionOld::ConstructMaterials
void ConstructMaterials()
Definition
Ex02DetectorConstructionOld.cxx:72
Ex02DetectorConstructionOld::ConstructGeometry
void ConstructGeometry()
Definition
Ex02DetectorConstructionOld.cxx:135
Ex02MCApplication
Implementation of the TVirtualMCApplication.
Definition
Ex02MCApplication.h:38
Ex02MCApplication::FinishPrimary
virtual void FinishPrimary()
Definition
Ex02MCApplication.cxx:354
Ex02MCApplication::Stepping
virtual void Stepping()
Definition
Ex02MCApplication.cxx:339
Ex02MCApplication::GeneratePrimaries
virtual void GeneratePrimaries()
Definition
Ex02MCApplication.cxx:274
Ex02MCApplication::ReadEvent
void ReadEvent(Int_t i)
Definition
Ex02MCApplication.cxx:387
Ex02MCApplication::RegisterStack
void RegisterStack() const
Definition
Ex02MCApplication.cxx:126
Ex02MCApplication::fMagField
TVirtualMagField * fMagField
Magnetic field.
Definition
Ex02MCApplication.h:79
Ex02MCApplication::fOldGeometry
Bool_t fOldGeometry
Option for geometry definition.
Definition
Ex02MCApplication.h:80
Ex02MCApplication::CloneForWorker
virtual TVirtualMCApplication * CloneForWorker() const
Definition
Ex02MCApplication.cxx:204
Ex02MCApplication::RunMC
void RunMC(Int_t nofEvents)
Definition
Ex02MCApplication.cxx:181
Ex02MCApplication::FinishRunOnWorker
virtual void FinishRunOnWorker()
Definition
Ex02MCApplication.cxx:228
Ex02MCApplication::BeginPrimary
virtual void BeginPrimary()
Definition
Ex02MCApplication.cxx:325
Ex02MCApplication::InitOnWorker
virtual void InitOnWorker()
Definition
Ex02MCApplication.cxx:210
Ex02MCApplication::FinishRun
void FinishRun()
Definition
Ex02MCApplication.cxx:191
Ex02MCApplication::fRootManager
TMCRootManager * fRootManager
Root manager.
Definition
Ex02MCApplication.h:75
Ex02MCApplication::Ex02MCApplication
Ex02MCApplication()
Definition
Ex02MCApplication.cxx:94
Ex02MCApplication::FinishEvent
virtual void FinishEvent()
Definition
Ex02MCApplication.cxx:361
Ex02MCApplication::InitMC
void InitMC(const char *setup)
Definition
Ex02MCApplication.cxx:141
Ex02MCApplication::PreTrack
virtual void PreTrack()
Definition
Ex02MCApplication.cxx:332
Ex02MCApplication::InitGeometry
virtual void InitGeometry()
Definition
Ex02MCApplication.cxx:266
Ex02MCApplication::fStack
Ex02MCStack * fStack
VMC stack.
Definition
Ex02MCApplication.h:76
Ex02MCApplication::ConstructGeometry
virtual void ConstructGeometry()
Definition
Ex02MCApplication.cxx:238
Ex02MCApplication::fDetConstruction
Ex02DetectorConstruction fDetConstruction
Dector construction.
Definition
Ex02MCApplication.h:77
Ex02MCApplication::PostTrack
virtual void PostTrack()
Definition
Ex02MCApplication.cxx:347
Ex02MCApplication::~Ex02MCApplication
virtual ~Ex02MCApplication()
Definition
Ex02MCApplication.cxx:107
Ex02MCApplication::Ex02MCApplication
Ex02MCApplication(const char *name, const char *title)
Definition
Ex02MCApplication.cxx:39
Ex02MCApplication::fTrackerSD
Ex02TrackerSD * fTrackerSD
Tracker SD.
Definition
Ex02MCApplication.h:78
Ex02MCApplication::BeginEvent
virtual void BeginEvent()
Definition
Ex02MCApplication.cxx:316
Ex02MCStack
Implementation of the TVirtualMCStack interface.
Definition
Ex02MCStack.h:33
Ex02MagField
Definition of a uniform magnetic field.
Definition
Ex02MagField.h:35
Ex02TrackerSD
The tracker sensitive detector.
Definition
Ex02TrackerSD.h:34
TVirtualMCApplication
Generated on
for VMC Examples by
1.17.0