VMC Examples
Version 6.8
Toggle main menu visibility
Loading...
Searching...
No Matches
examples
ExGarfield
src
MCApplication.cxx
Go to the documentation of this file.
1
//------------------------------------------------
2
// The Virtual Monte Carlo examples
3
// Copyright (C) 2007 - 2016 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 ExGarfield/src/MCApplication.cxx
11
/// \brief Implementation of the ExGarfield::MCApplication class
12
///
13
/// Geant4 ExampleN03 adapted to Virtual Monte Carlo
14
///
15
/// \date 28/10/2015
16
/// \author I. Hrivnacova; IPN, Orsay
17
18
#include "MCApplication.h"
19
#include "Ex03MCStack.h"
20
#include "Hit.h"
21
#include "PrimaryGenerator.h"
22
23
#include <TMCRootManager.h>
24
25
#include <Riostream.h>
26
#include <TGeoManager.h>
27
#include <TInterpreter.h>
28
#include <TPDGCode.h>
29
#include <TParticle.h>
30
#include <TROOT.h>
31
#include <TRandom.h>
32
#include <TVector3.h>
33
#include <TVirtualGeoTrack.h>
34
#include <TVirtualMC.h>
35
36
using namespace
std;
37
38
/// \cond CLASSIMP
39
ClassImp(
VMC::ExGarfield::MCApplication
)
40
/// \endcond
41
42
namespace
VMC
43
{
44
namespace
ExGarfield
45
{
46
47
//_____________________________________________________________________________
48
MCApplication::MCApplication
(
const
char
* name,
const
char
* title)
49
:
TVirtualMCApplication
(name, title),
50
fRootManager
(0),
51
fEventNo
(0),
52
fVerbose
(0),
53
fStack
(0),
54
fDetConstruction
(0),
55
fSensitiveDetector
(0),
56
fPrimaryGenerator
(0),
57
fIsMaster
(kTRUE)
58
{
59
/// Standard constructor
60
/// \param name The MC application name
61
/// \param title The MC application description
62
63
// Create a user stack
64
fStack
=
new
Ex03MCStack
(1000);
65
66
// Create detector construction
67
fDetConstruction
=
new
DetectorConstruction
();
68
69
// Create a calorimeter SD
70
fSensitiveDetector
=
new
SensitiveDetector
(
"Calorimeter"
);
71
72
// Create a primary generator
73
fPrimaryGenerator
=
new
PrimaryGenerator
(
fStack
);
74
}
75
76
//_____________________________________________________________________________
77
MCApplication::MCApplication
(
const
MCApplication
& origin)
78
:
TVirtualMCApplication
(origin.GetName(), origin.GetTitle()),
79
fRootManager
(0),
80
fEventNo
(0),
81
fVerbose
(origin.
fVerbose
),
82
fStack
(0),
83
fDetConstruction
(origin.
fDetConstruction
),
84
fSensitiveDetector
(0),
85
fPrimaryGenerator
(0),
86
fIsMaster
(kFALSE)
87
{
88
/// Copy constructor for cloning application on workers (in multithreading
89
/// mode) \param origin The source MC application
90
91
// Create new user stack
92
fStack
=
new
Ex03MCStack
(1000);
93
94
// Create a calorimeter SD
95
fSensitiveDetector
=
new
SensitiveDetector
(*(origin.
fSensitiveDetector
));
96
97
// Create a primary generator
98
fPrimaryGenerator
=
99
new
PrimaryGenerator
(*(origin.
fPrimaryGenerator
),
fStack
);
100
}
101
102
//_____________________________________________________________________________
103
MCApplication::MCApplication
()
104
:
TVirtualMCApplication
(),
105
fRootManager
(0),
106
fEventNo
(0),
107
fStack
(0),
108
fDetConstruction
(0),
109
fSensitiveDetector
(0),
110
fPrimaryGenerator
(0),
111
fIsMaster
(kTRUE)
112
{
113
/// Default constructor
114
}
115
116
//_____________________________________________________________________________
117
MCApplication::~MCApplication
()
118
{
119
/// Destructor
120
121
// cout << "MCApplication::~MCApplication " << this << endl;
122
123
delete
fRootManager
;
124
delete
fStack
;
125
if
(
fIsMaster
)
delete
fDetConstruction
;
126
delete
fSensitiveDetector
;
127
delete
fPrimaryGenerator
;
128
delete
gMC;
129
130
// cout << "Done MCApplication::~MCApplication " << this << endl;
131
}
132
133
//
134
// private methods
135
//
136
137
//_____________________________________________________________________________
138
void
MCApplication::RegisterStack
()
const
139
{
140
/// Register stack in the Root manager.
141
142
if
(
fRootManager
) {
143
// cout << "MCApplication::RegisterStack: " << endl;
144
fRootManager
->Register(
"stack"
,
"Ex03MCStack"
, &
fStack
);
145
}
146
}
147
148
//
149
// public methods
150
//
151
152
//_____________________________________________________________________________
153
void
MCApplication::InitMC
(
const
char
* setup)
154
{
155
/// Initialize MC.
156
/// The selection of the concrete MC is done in the macro.
157
/// \param setup The name of the configuration macro
158
159
fVerbose
.InitMC();
160
161
if
(TString(setup) !=
""
) {
162
gROOT->LoadMacro(setup);
163
gInterpreter->ProcessLine(
"Config()"
);
164
if
(!gMC) {
165
Fatal(
166
"InitMC"
,
"Processing Config() has failed. (No MC is instantiated.)"
);
167
}
168
}
169
170
// MT support available from root v 5.34/18
171
#if ROOT_VERSION_CODE >= 336402
172
// Create Root manager
173
if
(!gMC->IsMT()) {
174
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite);
175
// fRootManager->SetDebug(true);
176
}
177
#else
178
// Create Root manager
179
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite);
180
// fRootManager->SetDebug(true);
181
#endif
182
183
gMC->SetStack(
fStack
);
184
gMC->Init();
185
gMC->BuildPhysics();
186
187
RegisterStack
();
188
}
189
190
//_____________________________________________________________________________
191
void
MCApplication::RunMC
(Int_t nofEvents)
192
{
193
/// Run MC.
194
/// \param nofEvents Number of events to be processed
195
196
fVerbose
.RunMC(nofEvents);
197
198
gMC->ProcessRun(nofEvents);
199
FinishRun
();
200
}
201
202
//_____________________________________________________________________________
203
void
MCApplication::FinishRun
()
204
{
205
/// Finish MC run.
206
207
fVerbose
.FinishRun();
208
// cout << "MCApplication::FinishRun: " << endl;
209
if
(
fRootManager
) {
210
fRootManager
->WriteAll();
211
fRootManager
->Close();
212
}
213
}
214
215
//_____________________________________________________________________________
216
TVirtualMCApplication
*
MCApplication::CloneForWorker
()
const
217
{
218
return
new
MCApplication
(*
this
);
219
}
220
221
//_____________________________________________________________________________
222
void
MCApplication::InitOnWorker
()
223
{
224
// cout << "MCApplication::InitForWorker " << this << endl;
225
226
// Create Root manager
227
Int_t threadRank = 1;
228
// The real thread rank will be set in MCRootManager
229
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kWrite, threadRank);
230
// fRootManager->SetDebug(true);
231
232
// Set data to MC
233
gMC->SetStack(
fStack
);
234
235
RegisterStack
();
236
}
237
238
//_____________________________________________________________________________
239
void
MCApplication::FinishRunOnWorker
()
240
{
241
// cout << "MCApplication::FinishWorkerRun: " << endl;
242
if
(
fRootManager
) {
243
fRootManager
->WriteAll();
244
fRootManager
->Close();
245
}
246
}
247
248
//_____________________________________________________________________________
249
void
MCApplication::ReadEvent
(Int_t i)
250
{
251
/// Read \em i -th event and prints hits.
252
/// \param i The number of event to be read
253
254
if
( !
fRootManager
) {
255
fRootManager
=
new
TMCRootManager(GetName(), TMCRootManager::kRead);
256
}
257
258
fSensitiveDetector
->Register();
259
RegisterStack
();
260
fRootManager
->ReadEvent(i);
261
}
262
263
//_____________________________________________________________________________
264
void
MCApplication::ConstructGeometry
()
265
{
266
/// Construct geometry using detector contruction class.
267
/// The detector contruction class is using TGeo functions or
268
/// TVirtualMC functions (if oldGeometry is selected)
269
270
fVerbose
.ConstructGeometry();
271
272
fDetConstruction
->Construct();
273
}
274
275
//_____________________________________________________________________________
276
void
MCApplication::InitGeometry
()
277
{
278
/// Initialize geometry
279
280
fVerbose
.InitGeometry();
281
282
fSensitiveDetector
->Initialize();
283
}
284
285
//_____________________________________________________________________________
286
void
MCApplication::GeneratePrimaries
()
287
{
288
/// Fill the user stack (derived from TVirtualMCStack) with primary
289
/// particles.
290
291
fVerbose
.GeneratePrimaries();
292
293
TVector3 origin;
294
fPrimaryGenerator
->GeneratePrimaries(origin);
295
}
296
297
//_____________________________________________________________________________
298
void
MCApplication::BeginEvent
()
299
{
300
/// User actions at beginning of event
301
302
fVerbose
.BeginEvent();
303
304
// Clear TGeo tracks (if filled)
305
if
(TString(gMC->GetName()) ==
"TGeant3TGeo"
&&
306
gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
307
((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
308
309
gGeoManager->ClearTracks();
310
// if (gPad) gPad->Clear();
311
}
312
313
fEventNo
++;
314
cout <<
" Start generating event Nr "
<<
fEventNo
<< endl;
315
}
316
317
//_____________________________________________________________________________
318
void
MCApplication::BeginPrimary
()
319
{
320
/// User actions at beginning of a primary track.
321
/// If test for user defined decay is activated,
322
/// the primary track ID is printed on the screen.
323
324
fVerbose
.BeginPrimary();
325
}
326
327
//_____________________________________________________________________________
328
void
MCApplication::PreTrack
()
329
{
330
/// User actions at beginning of each track
331
/// If test for user defined decay is activated,
332
/// the decay products of the primary track (K0Short)
333
/// are printed on the screen.
334
335
fVerbose
.PreTrack();
336
}
337
338
//_____________________________________________________________________________
339
void
MCApplication::Stepping
()
340
{
341
/// User actions at each step
342
343
// Work around for Fluka VMC, which does not call
344
// MCApplication::PreTrack()
345
//
346
// cout << "MCApplication::Stepping" << this << endl;
347
static
Int_t trackId = 0;
348
if
(TString(gMC->GetName()) ==
"TFluka"
&&
349
gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
350
fVerbose
.PreTrack();
351
trackId = gMC->GetStack()->GetCurrentTrackNumber();
352
}
353
354
fVerbose
.Stepping();
355
356
fSensitiveDetector
->ProcessHits();
357
}
358
359
//_____________________________________________________________________________
360
void
MCApplication::PostTrack
()
361
{
362
/// User actions after finishing of each track
363
364
fVerbose
.PostTrack();
365
}
366
367
//_____________________________________________________________________________
368
void
MCApplication::FinishPrimary
()
369
{
370
/// User actions after finishing of a primary track
371
372
fVerbose
.FinishPrimary();
373
}
374
375
//_____________________________________________________________________________
376
void
MCApplication::FinishEvent
()
377
{
378
/// User actions after finishing of an event
379
380
fVerbose
.FinishEvent();
381
382
// update hit info from Garfield
383
fSensitiveDetector
->UpdateFromGarfield();
384
fRootManager
->Fill();
385
386
// reset data
387
fSensitiveDetector
->EndOfEvent();
388
fStack
->Reset();
389
}
390
391
}
// namespace ExGarfield
392
}
Ex03MCStack
Implementation of the TVirtualMCStack interface.
Definition
Ex03MCStack.h:36
TVirtualMCApplication
VMC::ExGarfield::DetectorConstruction
The detector construction (via TGeo ).
Definition
DetectorConstruction.h:40
VMC::ExGarfield::MCApplication
Implementation of the TVirtualMCApplication.
Definition
MCApplication.h:48
VMC::ExGarfield::MCApplication::fEventNo
Int_t fEventNo
Event counter.
Definition
MCApplication.h:90
VMC::ExGarfield::MCApplication::fIsMaster
Bool_t fIsMaster
If is on master thread.
Definition
MCApplication.h:96
VMC::ExGarfield::MCApplication::FinishEvent
virtual void FinishEvent()
Definition
MCApplication.cxx:376
VMC::ExGarfield::MCApplication::Stepping
virtual void Stepping()
Definition
MCApplication.cxx:339
VMC::ExGarfield::MCApplication::PostTrack
virtual void PostTrack()
Definition
MCApplication.cxx:360
VMC::ExGarfield::MCApplication::InitMC
void InitMC(const char *setup)
Definition
MCApplication.cxx:153
VMC::ExGarfield::MCApplication::MCApplication
MCApplication(const char *name, const char *title)
Definition
MCApplication.cxx:48
VMC::ExGarfield::MCApplication::MCApplication
MCApplication()
Definition
MCApplication.cxx:103
VMC::ExGarfield::MCApplication::CloneForWorker
virtual TVirtualMCApplication * CloneForWorker() const
Definition
MCApplication.cxx:216
VMC::ExGarfield::MCApplication::FinishRunOnWorker
virtual void FinishRunOnWorker()
Definition
MCApplication.cxx:239
VMC::ExGarfield::MCApplication::PreTrack
virtual void PreTrack()
Definition
MCApplication.cxx:328
VMC::ExGarfield::MCApplication::ReadEvent
void ReadEvent(Int_t i)
Definition
MCApplication.cxx:249
VMC::ExGarfield::MCApplication::~MCApplication
virtual ~MCApplication()
Definition
MCApplication.cxx:117
VMC::ExGarfield::MCApplication::GeneratePrimaries
virtual void GeneratePrimaries()
Definition
MCApplication.cxx:286
VMC::ExGarfield::MCApplication::fDetConstruction
DetectorConstruction * fDetConstruction
Dector construction.
Definition
MCApplication.h:93
VMC::ExGarfield::MCApplication::InitGeometry
virtual void InitGeometry()
Definition
MCApplication.cxx:276
VMC::ExGarfield::MCApplication::fRootManager
TMCRootManager * fRootManager
Root manager.
Definition
MCApplication.h:89
VMC::ExGarfield::MCApplication::BeginPrimary
virtual void BeginPrimary()
Definition
MCApplication.cxx:318
VMC::ExGarfield::MCApplication::RunMC
void RunMC(Int_t nofEvents)
Definition
MCApplication.cxx:191
VMC::ExGarfield::MCApplication::fSensitiveDetector
SensitiveDetector * fSensitiveDetector
Calorimeter SD.
Definition
MCApplication.h:94
VMC::ExGarfield::MCApplication::RegisterStack
void RegisterStack() const
Definition
MCApplication.cxx:138
VMC::ExGarfield::MCApplication::fVerbose
TMCVerbose fVerbose
VMC verbose helper.
Definition
MCApplication.h:91
VMC::ExGarfield::MCApplication::BeginEvent
virtual void BeginEvent()
Definition
MCApplication.cxx:298
VMC::ExGarfield::MCApplication::FinishRun
void FinishRun()
Definition
MCApplication.cxx:203
VMC::ExGarfield::MCApplication::InitOnWorker
virtual void InitOnWorker()
Definition
MCApplication.cxx:222
VMC::ExGarfield::MCApplication::fStack
Ex03MCStack * fStack
VMC stack.
Definition
MCApplication.h:92
VMC::ExGarfield::MCApplication::ConstructGeometry
virtual void ConstructGeometry()
Definition
MCApplication.cxx:264
VMC::ExGarfield::MCApplication::fPrimaryGenerator
PrimaryGenerator * fPrimaryGenerator
Primary generator.
Definition
MCApplication.h:95
VMC::ExGarfield::MCApplication::FinishPrimary
virtual void FinishPrimary()
Definition
MCApplication.cxx:368
VMC::ExGarfield::PrimaryGenerator
The primary generator.
Definition
PrimaryGenerator.h:41
VMC::ExGarfield::SensitiveDetector
The calorimeter sensitive detector.
Definition
SensitiveDetector.h:39
VMC::ExGarfield
Definition
FastSimulation.h:28
VMC
Definition
FastSimulation.h:26
Generated on
for VMC Examples by
1.17.0