VMC Examples Version 6.6
Loading...
Searching...
No Matches
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
36using namespace std;
37
38/// \cond CLASSIMP
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
68
69 // Create a calorimeter SD
70 fSensitiveDetector = new SensitiveDetector("Calorimeter");
71
72 // Create a primary generator
74 }
75
76 //_____________________________________________________________________________
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
96
97 // Create a primary generator
100 }
101
102 //_____________________________________________________________________________
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 //_____________________________________________________________________________
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 //_____________________________________________________________________________
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
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 //_____________________________________________________________________________
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 //_____________________________________________________________________________
217 {
218 return new MCApplication(*this);
219 }
220
221 //_____________________________________________________________________________
223 {
224 // cout << "MCApplication::InitForWorker " << this << endl;
225
226 // Create Root manager
227 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
228 // fRootManager->SetDebug(true);
229
230 // Set data to MC
231 gMC->SetStack(fStack);
232
234 }
235
236 //_____________________________________________________________________________
238 {
239 // cout << "MCApplication::FinishWorkerRun: " << endl;
240 if (fRootManager) {
241 fRootManager->WriteAll();
242 fRootManager->Close();
243 }
244 }
245
246 //_____________________________________________________________________________
248 {
249 /// Read \em i -th event and prints hits.
250 /// \param i The number of event to be read
251
254 fRootManager->ReadEvent(i);
255 }
256
257 //_____________________________________________________________________________
259 {
260 /// Construct geometry using detector contruction class.
261 /// The detector contruction class is using TGeo functions or
262 /// TVirtualMC functions (if oldGeometry is selected)
263
264 fVerbose.ConstructGeometry();
265
267 }
268
269 //_____________________________________________________________________________
271 {
272 /// Initialize geometry
273
274 fVerbose.InitGeometry();
275
277 }
278
279 //_____________________________________________________________________________
281 {
282 /// Fill the user stack (derived from TVirtualMCStack) with primary
283 /// particles.
284
285 fVerbose.GeneratePrimaries();
286
287 TVector3 origin;
289 }
290
291 //_____________________________________________________________________________
293 {
294 /// User actions at beginning of event
295
296 fVerbose.BeginEvent();
297
298 // Clear TGeo tracks (if filled)
299 if (TString(gMC->GetName()) == "TGeant3TGeo" &&
300 gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
301 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
302
303 gGeoManager->ClearTracks();
304 // if (gPad) gPad->Clear();
305 }
306
307 fEventNo++;
308 cout << " Start generating event Nr " << fEventNo << endl;
309 }
310
311 //_____________________________________________________________________________
313 {
314 /// User actions at beginning of a primary track.
315 /// If test for user defined decay is activated,
316 /// the primary track ID is printed on the screen.
317
318 fVerbose.BeginPrimary();
319 }
320
321 //_____________________________________________________________________________
323 {
324 /// User actions at beginning of each track
325 /// If test for user defined decay is activated,
326 /// the decay products of the primary track (K0Short)
327 /// are printed on the screen.
328
329 fVerbose.PreTrack();
330 }
331
332 //_____________________________________________________________________________
334 {
335 /// User actions at each step
336
337 // Work around for Fluka VMC, which does not call
338 // MCApplication::PreTrack()
339 //
340 // cout << "MCApplication::Stepping" << this << endl;
341 static Int_t trackId = 0;
342 if (TString(gMC->GetName()) == "TFluka" &&
343 gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
344 fVerbose.PreTrack();
345 trackId = gMC->GetStack()->GetCurrentTrackNumber();
346 }
347
348 fVerbose.Stepping();
349
351 }
352
353 //_____________________________________________________________________________
355 {
356 /// User actions after finishing of each track
357
358 fVerbose.PostTrack();
359 }
360
361 //_____________________________________________________________________________
363 {
364 /// User actions after finishing of a primary track
365
366 fVerbose.FinishPrimary();
367 }
368
369 //_____________________________________________________________________________
371 {
372 /// User actions after finishing of an event
373
374 fVerbose.FinishEvent();
375
376 // update hit info from Garfield
378 fRootManager->Fill();
379
380 // reset data
382 fStack->Reset();
383 }
384
385 } // namespace ExGarfield
386}
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
The detector construction (via TGeo )
Implementation of the TVirtualMCApplication.
Int_t fEventNo
Event counter.
Bool_t fIsMaster
If is on master thread.
void InitMC(const char *setup)
virtual TVirtualMCApplication * CloneForWorker() const
DetectorConstruction * fDetConstruction
Dector construction.
TMCRootManager * fRootManager
Root manager.
SensitiveDetector * fSensitiveDetector
Calorimeter SD.
TMCVerbose fVerbose
VMC verbose helper.
Ex03MCStack * fStack
VMC stack.
PrimaryGenerator * fPrimaryGenerator
Primary generator.
virtual void GeneratePrimaries(const TVector3 &worldSize)
The calorimeter sensitive detector.