VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex03MCApplication.cxx
Go to the documentation of this file.
1//------------------------------------------------
2// The Virtual Monte Carlo examples
3// Copyright (C) 2014 - 2018 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 Ex03MCApplication.cxx
11/// \brief Implementation of the Ex03MCApplication class
12///
13/// Geant4 ExampleN03 adapted to Virtual Monte Carlo
14///
15/// \date 06/03/2002
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include "Ex03MCApplication.h"
19#include "Ex03DetectorConstructionOld.h"
20#include "Ex03MCStack.h"
21#include "Ex03PrimaryGenerator.h"
22
23#include <TMCRootManager.h>
24
25#include <Riostream.h>
26#include <TGeoManager.h>
27#include <TGeoUniformMagField.h>
28#include <TInterpreter.h>
29#include <TPDGCode.h>
30#include <TParticle.h>
31#include <TROOT.h>
32#include <TRandom.h>
33#include <TVector3.h>
34#include <TVirtualGeoTrack.h>
35#include <TVirtualMC.h>
36
37using namespace std;
38
39/// \cond CLASSIMP
40ClassImp(Ex03MCApplication)
41 /// \endcond
42
43 //_____________________________________________________________________________
44 Ex03MCApplication::Ex03MCApplication(const char* name, const char* title)
45 : TVirtualMCApplication(name, title),
46 fRootManager(0),
47 fPrintModulo(1),
48 fEventNo(0),
49 fVerbose(0),
50 fStack(0),
51 fDetConstruction(0),
52 fCalorimeterSD(0),
53 fPrimaryGenerator(0),
54 fMagField(0),
55 fOldGeometry(kFALSE),
56 fIsControls(kFALSE),
57 fIsMaster(kTRUE)
58{
59 /// Standard constructor
60 /// \param name The MC application name
61 /// \param title The MC application description
62
63 cout << "--------------------------------------------------------------"
64 << endl;
65 cout << " VMC Example E03" << endl;
66 cout << "--------------------------------------------------------------"
67 << endl;
68
69 // Create a user stack
70 fStack = new Ex03MCStack(1000);
71
72 // Create detector construction
73 fDetConstruction = new Ex03DetectorConstruction();
74
75 // Create a calorimeter SD
76 fCalorimeterSD = new Ex03CalorimeterSD("Calorimeter", fDetConstruction);
77
78 // Create a primary generator
79 fPrimaryGenerator = new Ex03PrimaryGenerator(fStack);
80
81 // Constant magnetic field (in kiloGauss)
82 fMagField = new TGeoUniformMagField();
83}
84
85//_____________________________________________________________________________
87 : TVirtualMCApplication(origin.GetName(), origin.GetTitle()),
88 fRootManager(0),
89 fPrintModulo(origin.fPrintModulo),
90 fEventNo(0),
91 fVerbose(origin.fVerbose),
92 fStack(0),
93 fDetConstruction(origin.fDetConstruction),
94 fCalorimeterSD(0),
95 fPrimaryGenerator(0),
96 fMagField(0),
97 fOldGeometry(origin.fOldGeometry),
98 fIsMaster(kFALSE)
99{
100 /// Copy constructor for cloning application on workers (in multithreading
101 /// mode) \param origin The source MC application
102
103 // Create new user stack
104 fStack = new Ex03MCStack(1000);
105
106 // Create a calorimeter SD
109
110 // Create a primary generator
113
114 // Constant magnetic field (in kiloGauss)
115 fMagField = new TGeoUniformMagField(origin.fMagField->GetFieldValue()[0],
116 origin.fMagField->GetFieldValue()[1], origin.fMagField->GetFieldValue()[2]);
117}
118
119//_____________________________________________________________________________
122 fRootManager(0),
123 fPrintModulo(1),
124 fEventNo(0),
125 fStack(0),
126 fDetConstruction(0),
127 fCalorimeterSD(0),
128 fPrimaryGenerator(0),
129 fMagField(0),
130 fOldGeometry(kFALSE),
131 fIsControls(kFALSE),
132 fIsMaster(kTRUE)
133{
134 /// Default constructor
135}
136
137//_____________________________________________________________________________
139{
140 /// Destructor
141
142 // cout << "Ex03MCApplication::~Ex03MCApplication " << this << endl;
143
144 delete fRootManager;
145 delete fStack;
146 if (fIsMaster) delete fDetConstruction;
147 delete fCalorimeterSD;
148 delete fPrimaryGenerator;
149 delete fMagField;
150 delete gMC;
151
152 // cout << "Done Ex03MCApplication::~Ex03MCApplication " << this << endl;
153}
154
155//
156// private methods
157//
158
159//_____________________________________________________________________________
161{
162 /// Register stack in the Root manager.
163
164 if (fRootManager) {
165 // cout << "Ex03MCApplication::RegisterStack: " << endl;
166 fRootManager->Register("stack", "Ex03MCStack", &fStack);
167 }
168}
169
170//
171// public methods
172//
173
174//_____________________________________________________________________________
175void Ex03MCApplication::InitMC(const char* setup)
176{
177 /// Initialize MC.
178 /// The selection of the concrete MC is done in the macro.
179 /// \param setup The name of the configuration macro
180
181 fVerbose.InitMC();
182
183 if (TString(setup) != "") {
184 gROOT->LoadMacro(setup);
185 gInterpreter->ProcessLine("Config()");
186 if (!gMC) {
187 Fatal(
188 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
189 }
190 }
191
192// MT support available from root v 5.34/18
193#if ROOT_VERSION_CODE >= 336402
194 // Create Root manager
195 if (!gMC->IsMT()) {
196 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
197 // fRootManager->SetDebug(true);
198 }
199#else
200 // Create Root manager
201 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
202 // fRootManager->SetDebug(true);
203#endif
204
205 gMC->SetStack(fStack);
206 gMC->SetMagField(fMagField);
207 gMC->Init();
208 gMC->BuildPhysics();
209
211}
212
213//_____________________________________________________________________________
214void Ex03MCApplication::RunMC(Int_t nofEvents)
215{
216 /// Run MC.
217 /// \param nofEvents Number of events to be processed
218
219 fVerbose.RunMC(nofEvents);
220
221 gMC->ProcessRun(nofEvents);
222 FinishRun();
223}
224
225//_____________________________________________________________________________
227{
228 /// Finish MC run.
229
230 fVerbose.FinishRun();
231 // cout << "Ex03MCApplication::FinishRun: " << endl;
232 if (fRootManager) {
233 fRootManager->WriteAll();
234 fRootManager->Close();
235 }
236}
237
238//_____________________________________________________________________________
243
244//_____________________________________________________________________________
246{
247 // cout << "Ex03MCApplication::InitForWorker " << this << endl;
248
249 // Create Root manager
250 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
251 // fRootManager->SetDebug(true);
252
253 // Set data to MC
254 gMC->SetStack(fStack);
255 gMC->SetMagField(fMagField);
256
258}
259
260//_____________________________________________________________________________
262{
263 // cout << "Ex03MCApplication::FinishWorkerRun: " << endl;
264 if (fRootManager) {
265 fRootManager->WriteAll();
266 fRootManager->Close();
267 }
268}
269
270//_____________________________________________________________________________
272{
273 /// Read \em i -th event and prints hits.
274 /// \param i The number of event to be read
275
278 fRootManager->ReadEvent(i);
279}
280
281//_____________________________________________________________________________
283{
284 /// Construct geometry using detector contruction class.
285 /// The detector contruction class is using TGeo functions or
286 /// TVirtualMC functions (if oldGeometry is selected)
287
288 fVerbose.ConstructGeometry();
289
290 if (!fOldGeometry) {
293 // TGeoManager::Import("geometry.root");
294 // gMC->SetRootGeometry();
295 }
296 else {
297 Ex03DetectorConstructionOld detConstructionOld;
298 detConstructionOld.ConstructMaterials();
299 detConstructionOld.ConstructGeometry();
300 }
301}
302
303//_____________________________________________________________________________
305{
306 /// Initialize geometry
307
308 fVerbose.InitGeometry();
309
311
313
315}
316
317//_____________________________________________________________________________
319{
320 /// Example of user defined particle with user defined decay mode
321
322 fVerbose.AddParticles();
323
324 // Define particle
325 gMC->DefineParticle(1000020050, "He5", kPTHadron, 5.03427, 2.0, 0.002, "Ion",
326 0.0, 0, 1, 0, 0, 0, 0, 0, 5, kFALSE);
327
328 // Define the 2 body phase space decay for He5
329 Int_t mode[6][3];
330 Float_t bratio[6];
331
332 for (Int_t kz = 0; kz < 6; kz++) {
333 bratio[kz] = 0.;
334 mode[kz][0] = 0;
335 mode[kz][1] = 0;
336 mode[kz][2] = 0;
337 }
338 bratio[0] = 100.;
339 mode[0][0] = kNeutron; // neutron (2112)
340 mode[0][1] = 1000020040; // alpha
341
342 gMC->SetDecayMode(1000020050, bratio, mode);
343
344 // Overwrite a decay mode already defined in MCs
345 // Kaon Short: 310 normally decays in two modes
346 // pi+, pi- 68.61 %
347 // pi0, pi0 31.39 %
348 // and we force only the mode pi0, pi0
349
350 Int_t mode2[6][3];
351 Float_t bratio2[6];
352
353 for (Int_t kz = 0; kz < 6; kz++) {
354 bratio2[kz] = 0.;
355 mode2[kz][0] = 0;
356 mode2[kz][1] = 0;
357 mode2[kz][2] = 0;
358 }
359 bratio2[0] = 100.;
360 mode2[0][0] = kPi0; // pi0 (111)
361 mode2[0][1] = kPi0; // pi0 (111)
362
363 gMC->SetDecayMode(kK0Short, bratio2, mode2);
364}
365
366//_____________________________________________________________________________
368{
369 /// Example of user defined ion
370
371 fVerbose.AddIons();
372
373 gMC->DefineIon("MyIon", 34, 70, 12, 0.);
374}
375
376//_____________________________________________________________________________
378{
379 /// Fill the user stack (derived from TVirtualMCStack) with primary particles.
380
381 fVerbose.GeneratePrimaries();
382
383 TVector3 origin(fDetConstruction->GetWorldSizeX(),
385
387}
388
389//_____________________________________________________________________________
391{
392 /// User actions at beginning of event
393
394 fVerbose.BeginEvent();
395
396 // Clear TGeo tracks (if filled)
397 if (TString(gMC->GetName()) == "TGeant3TGeo" &&
398 gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
399 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
400
401 gGeoManager->ClearTracks();
402 // if (gPad) gPad->Clear();
403 }
404
405 fEventNo++;
406 if (fEventNo % fPrintModulo == 0) {
407 cout << "\n---> Begin of event: " << fEventNo << endl;
408 // ??? How to do this in VMC
409 // HepRandom::showEngineStatus();
410 }
411}
412
413//_____________________________________________________________________________
415{
416 /// User actions at beginning of a primary track.
417 /// If test for user defined decay is activated,
418 /// the primary track ID is printed on the screen.
419
420 fVerbose.BeginPrimary();
421
423 cout << " Primary track ID = " << fStack->GetCurrentTrackNumber() << endl;
424 }
425}
426
427//_____________________________________________________________________________
429{
430 /// User actions at beginning of each track
431 /// If test for user defined decay is activated,
432 /// the decay products of the primary track (K0Short)
433 /// are printed on the screen.
434
435 fVerbose.PreTrack();
436
437 // print info about K0Short decay products
439 Int_t parentID = fStack->GetCurrentParentTrackNumber();
440
441 if (parentID >= 0 &&
442 fStack->GetParticle(parentID)->GetPdgCode() == kK0Short &&
443 fStack->GetCurrentTrack()->GetUniqueID() == kPDecay) {
444 // The production process is saved as TParticle unique ID
445 // via Ex03MCStack
446
447 cout << " Current track " << fStack->GetCurrentTrack()->GetName()
448 << " is a decay product of Parent ID = "
450 }
451 }
452}
453
454//_____________________________________________________________________________
456{
457 /// User actions at each step
458
459 // Work around for Fluka VMC, which does not call
460 // MCApplication::PreTrack()
461 //
462
463 static Int_t trackId = 0;
464 if (TString(gMC->GetName()) == "TFluka" &&
465 gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
466 fVerbose.PreTrack();
467 trackId = gMC->GetStack()->GetCurrentTrackNumber();
468 }
469
470 fVerbose.Stepping();
471
473}
474
475//_____________________________________________________________________________
477{
478 /// User actions after finishing of each track
479
480 fVerbose.PostTrack();
481}
482
483//_____________________________________________________________________________
485{
486 /// User actions after finishing of a primary track
487
488 fVerbose.FinishPrimary();
489
491 cout << endl;
492 }
493}
494
495//_____________________________________________________________________________
497{
498 /// User actions after finishing of an event
499
500 fVerbose.FinishEvent();
501
502 // Geant3 + TGeo
503 // (use TGeo functions for visualization)
504 if (TString(gMC->GetName()) == "TGeant3TGeo") {
505
506 // Draw volume
507 gGeoManager->SetVisOption(0);
508 gGeoManager->SetTopVisible();
509 gGeoManager->GetTopVolume()->Draw();
510
511 // Draw tracks (if filled)
512 // Available when this feature is activated via
513 // gMC->SetCollectTracks(kTRUE);
514 if (gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
515 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
516
517 gGeoManager->DrawTracks("/*"); // this means all tracks
518 }
519 }
520
521 fRootManager->Fill();
522
524
526
527 fStack->Reset();
528}
Definition of the Ex03MCApplication class.
The calorimeter sensitive detector.
The old detector construction (via VMC functions)
The detector construction (via TGeo )
Implementation of the TVirtualMCApplication.
Int_t fPrintModulo
The event modulus number to be printed.
TGeoUniformMagField * fMagField
Magnetic field.
Bool_t fIsControls
Option to activate special controls.
TMCVerbose fVerbose
VMC verbose helper.
Bool_t fIsMaster
If is on master thread.
Ex03PrimaryGenerator * fPrimaryGenerator
Primary generator.
Ex03MCStack * fStack
VMC stack.
Ex03CalorimeterSD * fCalorimeterSD
Calorimeter SD.
Ex03MCApplication(const char *name, const char *title)
Ex03DetectorConstruction * fDetConstruction
Dector construction.
Bool_t fOldGeometry
Option for geometry definition.
Int_t fEventNo
Event counter.
TMCRootManager * fRootManager
Root manager.
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
virtual TParticle * GetCurrentTrack() const
virtual Int_t GetCurrentTrackNumber() const
virtual Int_t GetCurrentParentTrackNumber() const
TParticle * GetParticle(Int_t id) const
The primary generator.
virtual void GeneratePrimaries(const TVector3 &worldSize)
Bool_t GetUserDecay() const
Return true if particle with user decay is activated.
virtual void GeneratePrimaries()
virtual TVirtualMCApplication * CloneForWorker() const
void InitMC(const char *setup)
void RunMC(Int_t nofEvents)
virtual void FinishRunOnWorker()
virtual void ConstructGeometry()