VMC Examples Version 6.6
Loading...
Searching...
No Matches
A01MCApplication.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 A01MCApplication.cxx
11/// \brief Implementation of the A01MCApplication class
12///
13/// Geant4 example A01 adapted to Virtual Monte Carlo
14///
15/// \date 12/05/2012
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 <TPDGCode.h>
23#include <TParticle.h>
24#include <TParticlePDG.h>
25#include <TROOT.h>
26#include <TRandom.h>
27#include <TVector3.h>
28#include <TVirtualGeoTrack.h>
29#include <TVirtualMC.h>
30
31#include <TMCRootManager.h>
32
33#include "A01DriftChamberSD.h"
34#include "A01EmCalorimeterSD.h"
35#include "A01HadCalorimeterSD.h"
36#include "A01HodoscopeSD.h"
37#include "A01MCApplication.h"
38#include "A01MagField.h"
39#include "A01PrimaryGenerator.h"
41#include "Ex03MCStack.h"
42
43using namespace std;
44
45/// \cond CLASSIMP
46ClassImp(A01MCApplication)
47 /// \endcond
48
49 //_____________________________________________________________________________
50 A01MCApplication::A01MCApplication(const char* name, const char* title)
51 : TVirtualMCApplication(name, title),
52 fRootManager(0),
53 fWriteStack(true),
54 fWriteHits(true),
55 fUseLocalMagField(false),
56 fVerbose(0),
57 fStack(0),
58 fDetConstruction(0),
59 fDriftChamberSD1(0),
60 fDriftChamberSD2(0),
61 fEmCalorimeterSD(0),
62 fHadCalorimeterSD(0),
63 fHodoscopeSD1(0),
64 fHodoscopeSD2(0),
65 fPrimaryGenerator(0),
66 fMagField(0),
67 fIsMaster(kTRUE)
68{
69 /// Standard constructor
70 /// \param name The MC application name
71 /// \param title The MC application description
72
73 // Create a user stack
74 fStack = new Ex03MCStack(1000);
75
76 // Create detector construction
77 fDetConstruction = new A01RootDetectorConstruction("A01geometry.root");
78
79 // Create SDs
80 fDriftChamberSD1 = new A01DriftChamberSD("Chamber1", "wirePlane1Logical");
81 fDriftChamberSD2 = new A01DriftChamberSD("Chamber2", "wirePlane2Logical");
82 fEmCalorimeterSD = new A01EmCalorimeterSD("EmCalorimeter");
83 fHadCalorimeterSD = new A01HadCalorimeterSD("HadCalorimeter");
84 fHodoscopeSD1 = new A01HodoscopeSD("Hodoscope1", "hodoscope1Logical");
85 fHodoscopeSD2 = new A01HodoscopeSD("Hodoscope2", "hodoscope2Logical");
86
87 // Create a primary generator
88 fPrimaryGenerator = new A01PrimaryGenerator(fStack);
89
90 // Constant magnetic field (in kiloGauss)
91 // field value: 1.0*tesla (= 10.0 kiloGauss) in y
92 if (!fUseLocalMagField) {
93 fMagField = new A01MagField(0, 10.0, 0);
94 }
95}
96
97//_____________________________________________________________________________
99 : TVirtualMCApplication(origin.GetName(), origin.GetTitle()),
100 fRootManager(0),
101 fWriteStack(origin.fWriteStack),
102 fWriteHits(origin.fWriteHits),
103 fUseLocalMagField(origin.fUseLocalMagField),
104 fVerbose(origin.fVerbose),
105 fStack(0),
106 fDetConstruction(origin.fDetConstruction),
107 fDriftChamberSD1(0),
108 fDriftChamberSD2(0),
109 fEmCalorimeterSD(0),
110 fHadCalorimeterSD(0),
111 fHodoscopeSD1(0),
112 fHodoscopeSD2(0),
113 fPrimaryGenerator(0),
114 fMagField(0),
115 fIsMaster(kFALSE)
116{
117 /// Copy constructor (for clonig on worker thread in MT mode).
118 /// \param origin The source object (on master).
119
120 // Create a user stack
121 fStack = new Ex03MCStack(1000);
122
123 // Create SDs
130
131 // Create a primary generator
134
135 // Constant magnetic field (in kiloGauss)
136 // field value: 1.0*tesla (= 10.0 kiloGauss) in y
137 if (!fUseLocalMagField) {
138 fMagField = new A01MagField(0, 10.0, 0);
139 }
140}
141
142//_____________________________________________________________________________
145 fRootManager(0),
146 fWriteStack(true),
147 fWriteHits(true),
148 fUseLocalMagField(false),
149 fStack(0),
150 fDetConstruction(0),
151 fDriftChamberSD1(0),
152 fDriftChamberSD2(0),
153 fEmCalorimeterSD(0),
154 fHadCalorimeterSD(0),
155 fHodoscopeSD1(0),
156 fHodoscopeSD2(0),
157 fPrimaryGenerator(0),
158 fMagField(0),
159 fIsMaster(kTRUE)
160{
161 /// Default constructor
162}
163
164//_____________________________________________________________________________
166{
167 /// Destructor
168
169 // cout << "A01MCApplication::~A01MCApplication " << this << endl;
170
171 delete fRootManager;
172 delete fStack;
173 if (fIsMaster) delete fDetConstruction;
174 delete fDriftChamberSD1;
175 delete fDriftChamberSD2;
176 delete fEmCalorimeterSD;
177 delete fHadCalorimeterSD;
178 delete fHodoscopeSD1;
179 delete fHodoscopeSD2;
180 delete fPrimaryGenerator;
181 delete fMagField;
182 delete gMC;
183
184 // cout << "Done A01MCApplication::~A01MCApplication " << this << endl;
185}
186
187//
188// private methods
189//
190
191//_____________________________________________________________________________
193{
194 /// Register stack in the Root manager.
195
196 if (fWriteStack && fRootManager) {
197 // cout << "A01MCApplication::RegisterStack: " << endl;
198 fRootManager->Register("stack", "Ex03MCStack", &fStack);
199 }
200}
201
202//
203// public methods
204//
205
206//_____________________________________________________________________________
207void A01MCApplication::InitMC(const char* setup)
208{
209 /// Initialize MC.
210 /// The selection of the concrete MC is done in the macro.
211 /// \param setup The name of the configuration macro
212
213 fVerbose.InitMC();
214
215 if (TString(setup) != "") {
216 gROOT->LoadMacro(setup);
217 gInterpreter->ProcessLine("Config()");
218 if (!gMC) {
219 Fatal(
220 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
221 }
222 }
223
224// MT support available from root v 5.34/18
225#if ROOT_VERSION_CODE >= 336402
226 // Create Root manager
227 if (!gMC->IsMT()) {
228 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
229 // fRootManager->SetDebug(true);
230 }
231#else
232 // Create Root manager
233 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
234 // fRootManager->SetDebug(true);
235#endif
236
237 // Set data to MC
238 gMC->SetStack(fStack);
239 gMC->SetMagField(fMagField);
240
241 // Init MC
242 gMC->Init();
243 gMC->BuildPhysics();
244
246}
247
248//_____________________________________________________________________________
249void A01MCApplication::RunMC(Int_t nofEvents)
250{
251 /// Run MC.
252 /// \param nofEvents Number of events to be processed
253
254 fVerbose.RunMC(nofEvents);
255
256 gMC->ProcessRun(nofEvents);
257 FinishRun();
258}
259
260//_____________________________________________________________________________
262{
263 /// Finish MC run.
264
265 fVerbose.FinishRun();
266
267 if (fRootManager && (fWriteStack || fWriteHits)) {
268 fRootManager->WriteAll();
269 fRootManager->Close();
270 }
271}
272
273//_____________________________________________________________________________
275{
276 // cout << "A01MCApplication::CloneForWorker " << this << endl;
277 return new A01MCApplication(*this);
278}
279
280//_____________________________________________________________________________
282{
283 // cout << "A01MCApplication::InitForWorker " << this << endl;
284
285 // Create Root manager
286 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
287 // fRootManager->SetDebug(true);
288
289 // Set data to MC
290 gMC->SetStack(fStack);
291 gMC->SetMagField(fMagField);
292
294}
295
296//_____________________________________________________________________________
298{
299 // cout << "A01MCApplication::FinishWorkerRun: " << endl;
300 if (fRootManager) {
301 fRootManager->WriteAll();
302 fRootManager->Close();
303 }
304}
305
306//_____________________________________________________________________________
308{
309 /// Read \em i -th event and print hits.
310 /// \param i The number of event to be read
311
319 fRootManager->ReadEvent(i);
320}
321
322//_____________________________________________________________________________
324{
325 /// Construct geometry using detector contruction class.
326
327 fVerbose.ConstructGeometry();
329}
330
331//_____________________________________________________________________________
333{
334 /// Initialize geometry
335
336 fVerbose.InitGeometry();
337
338 // Set cuts in G3 equivalent to 1mm cut in G4
339 if (TString(gMC->GetName()) == "TGeant3TGeo") SetCuts();
340
347}
348
349//_____________________________________________________________________________
351{
352 /// Nothing to be done
353
354 fVerbose.AddParticles();
355}
356
357//_____________________________________________________________________________
359{
360 /// Nothing to be done
361
362 fVerbose.AddIons();
363}
364
365//_____________________________________________________________________________
367{
368 /// Fill the user stack (derived from TVirtualMCStack) with primary particles.
369
370 fVerbose.GeneratePrimaries();
372}
373
374//_____________________________________________________________________________
376{
377 /// User actions at beginning of event
378
379 fVerbose.BeginEvent();
380
381 // Clear TGeo tracks (if filled)
382 if (TString(gMC->GetName()) == "TGeant3TGeo" &&
383 gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
384 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
385
386 gGeoManager->ClearTracks();
387 // if (gPad) gPad->Clear();
388 }
389}
390
391//_____________________________________________________________________________
393{
394 /// User actions at beginning of a primary track.
395
396 fVerbose.BeginPrimary();
397}
398
399//_____________________________________________________________________________
401{
402 /// User actions at beginning of each track
403
404 fVerbose.PreTrack();
405}
406
407//_____________________________________________________________________________
409{
410 /// User actions at each step
411
412 fVerbose.Stepping();
419}
420
421//_____________________________________________________________________________
423{
424 /// User actions after finishing each track
425
426 fVerbose.PostTrack();
427}
428
429//_____________________________________________________________________________
431{
432 /// User actions after finishing each primary track
433
434 fVerbose.FinishPrimary();
435}
436
437//_____________________________________________________________________________
439{
440 /// User actions after finishing an event
441
442 fVerbose.FinishEvent();
443
444 // Geant3 + TGeo
445 // (use TGeo functions for visualization)
446 if (TString(gMC->GetName()) == "TGeant3TGeo") {
447
448 // Draw volume
449 gGeoManager->SetVisOption(0);
450 gGeoManager->SetTopVisible();
451 gGeoManager->GetTopVolume()->Draw();
452
453 // Draw tracks (if filled)
454 // Available when this feature is activated via
455 // gMC->SetCollectTracks(kTRUE);
456 if (gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
457 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
458
459 gGeoManager->DrawTracks("/*"); // this means all tracks
460 }
461 }
462
463 if (fWriteStack || fWriteHits) {
464 fRootManager->Fill();
465 }
466
467 // Print info about primary particle
468 TParticle* primary = fStack->GetParticle(0);
469 cout << endl
470 << ">>> Event " << gMC->CurrentEvent()
471 << " >>> Simulation truth : " << primary->GetPDG()->GetName() << " ("
472 << primary->Px() * 1e03 << ", " << primary->Py() * 1e03 << ", "
473 << primary->Pz() * 1e03 << ") MeV" << endl;
474
475 // Call detectors
482
483 fStack->Reset();
484}
485
486//_____________________________________________________________________________
487void A01MCApplication::SetWriteHits(Bool_t writeHits)
488{
489 /// (In)Activate writing hits on file and propagate this option
490 /// to all SDs
491 /// \param writeHits The new value of the option
492
493 fWriteHits = writeHits;
494 fHodoscopeSD1->SetWriteHits(writeHits);
495 fHodoscopeSD2->SetWriteHits(writeHits);
496 fDriftChamberSD1->SetWriteHits(writeHits);
497 fDriftChamberSD2->SetWriteHits(writeHits);
498 fEmCalorimeterSD->SetWriteHits(writeHits);
500}
501
502//_____________________________________________________________________________
504{
505 /// Set the option to use local magnetic field (working only with Geant4 !)
506 /// \param localMagField The new value of the option
507
508 fUseLocalMagField = localMagField;
510
511 // delete existing global field
512 delete fMagField;
513 fMagField = 0;
514}
515
516//_____________________________________________________________________________
518{
519 /// Set cuts for e-, gamma equivalent to 1mm cut in G4.
520
521 Int_t mediumId = gMC->MediumId("Air");
522 if (mediumId) {
523 gMC->Gstpar(mediumId, "CUTGAM", 990.e-09);
524 gMC->Gstpar(mediumId, "BCUTE", 990.e-09);
525 gMC->Gstpar(mediumId, "CUTELE", 990.e-09);
526 gMC->Gstpar(mediumId, "DCUTE", 990.e-09);
527 }
528
529 mediumId = gMC->MediumId("G4_Galactic");
530 if (mediumId) {
531 gMC->Gstpar(mediumId, "CUTGAM", 990.e-09);
532 gMC->Gstpar(mediumId, "BCUTE", 990.e-09);
533 gMC->Gstpar(mediumId, "CUTELE", 990.e-09);
534 gMC->Gstpar(mediumId, "DCUTE", 990.e-09);
535 }
536
537 mediumId = gMC->MediumId("Scintillator");
538 if (mediumId) {
539 gMC->Gstpar(mediumId, "CUTGAM", 2.40367 - 06);
540 gMC->Gstpar(mediumId, "BCUTE", 356.639e-06);
541 gMC->Gstpar(mediumId, "CUTELE", 356.639e-06);
542 gMC->Gstpar(mediumId, "DCUTE", 356.639e-06);
543 }
544
545 mediumId = gMC->MediumId("ArgonGas");
546 if (mediumId) {
547 gMC->Gstpar(mediumId, "CUTGAM", 990.e-09);
548 gMC->Gstpar(mediumId, "BCUTE", 990.e-09);
549 gMC->Gstpar(mediumId, "CUTELE", 990.e-09);
550 gMC->Gstpar(mediumId, "DCUTE", 990.e-09);
551 }
552
553 mediumId = gMC->MediumId("CsI");
554 if (mediumId) {
555 gMC->Gstpar(mediumId, "CUTGAM", 38.5665e-06);
556 gMC->Gstpar(mediumId, "BCUTE", 689.033e-06);
557 gMC->Gstpar(mediumId, "CUTELE", 689.033e-06);
558 gMC->Gstpar(mediumId, "DCUTE", 689.033e-06);
559 }
560
561 mediumId = gMC->MediumId("Lead");
562 if (mediumId) {
563 gMC->Gstpar(mediumId, "CUTGAM", 101.843e-06);
564 gMC->Gstpar(mediumId, "BCUTE", 1.36749e-03);
565 gMC->Gstpar(mediumId, "CUTELE", 1.36749e-03);
566 gMC->Gstpar(mediumId, "DCUTE", 1.36749e-03);
567 }
568}
Definition of the A01DriftChamberSD class.
Definition of the A01EmCalorimeterSD class.
Definition of the A01HadCalorimeterSD class.
Definition of the A01HodoscopeSD class.
Definition of the A01MCApplication class.
Definition of the A01MagField class.
Definition of the A01PrimaryGenerator class.
Definition of the A01RootDetectorConstruction class.
The calorimeter sensitive detector.
void SetWriteHits(Bool_t writeHits)
The EM calorimeter sensitive detector.
void SetWriteHits(Bool_t writeHits)
The hadron calorimeter sensitive detector.
void SetWriteHits(Bool_t writeHits)
The calorimeter sensitive detector.
void SetWriteHits(Bool_t writeHits)
Implementation of the TVirtualMCApplication.
void ReadEvent(Int_t i)
void SetWriteHits(Bool_t writeHits)
void RunMC(Int_t nofEvents)
A01HodoscopeSD * fHodoscopeSD2
Hodoscope 2 SD.
A01MagField * fMagField
Magnetic field.
virtual void GeneratePrimaries()
A01DriftChamberSD * fDriftChamberSD1
Drift Chamber 1 SD.
virtual void Stepping()
virtual void BeginPrimary()
Bool_t fIsMaster
If is on master thread.
A01RootDetectorConstruction * fDetConstruction
Detector construction.
virtual void PreTrack()
virtual void AddParticles()
virtual void FinishRunOnWorker()
virtual void ConstructGeometry()
virtual void FinishEvent()
virtual void FinishPrimary()
A01DriftChamberSD * fDriftChamberSD2
Drift Chamber 2 SD.
void InitMC(const char *setup)
virtual void InitGeometry()
TMCVerbose fVerbose
VMC verbose helper.
A01HadCalorimeterSD * fHadCalorimeterSD
Had Calorimeter SD.
Bool_t fWriteStack
Option to write stack.
Bool_t fUseLocalMagField
Option to use local magnetic field (working only with Geant4 !)
Ex03MCStack * fStack
VMC stack.
virtual void BeginEvent()
A01PrimaryGenerator * fPrimaryGenerator
Primary generator.
void SetUseLocalMagField(Bool_t localMagField)
virtual void InitOnWorker()
virtual void AddIons()
virtual TVirtualMCApplication * CloneForWorker() const
virtual void PostTrack()
TMCRootManager * fRootManager
Root manager.
void RegisterStack() const
A01EmCalorimeterSD * fEmCalorimeterSD
Em Calorimeter SD.
A01HodoscopeSD * fHodoscopeSD1
Hodoscope 1 SD.
Definition of a uniform magnetic field within a given region.
Definition A01MagField.h:30
The primary generator.
virtual void GeneratePrimaries()
The detector construction (via TGeo )
void SetUseLocalMagField(Bool_t localMagField)
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
TParticle * GetParticle(Int_t id) const