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 - 2015 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 TR/src/MCApplication.cxx
11/// \brief Implementation of the MCApplication class
12///
13/// Geant4 TestEm10 adapted to Virtual Monte Carlo.
14///
15/// \date 18/12/2015
16/// \author I. Hrivnacova; IPN, Orsay
17
18#include "MCApplication.h"
19#include "DetectorConstruction.h"
20#include "Ex03MCStack.h"
21#include "PrimaryGenerator.h"
22#include "SensitiveDetector.h"
23
24#include <TMCRootManager.h>
25
26#include <Riostream.h>
27#include <TGeoManager.h>
28#include <TGeoUniformMagField.h>
29#include <TH1.h>
30#include <TInterpreter.h>
31#include <TPDGCode.h>
32#include <TParticle.h>
33#include <TROOT.h>
34#include <TVirtualGeoTrack.h>
35#include <TVirtualMC.h>
36
37using namespace std;
38
39/// \cond CLASSIMP
41 /// \endcond
42
43 namespace VMC
44{
45 namespace TR
46 {
47
48 std::vector<TH1D*> fHistograms;
49
50 //_____________________________________________________________________________
51 MCApplication::MCApplication(const char* name, const char* title)
52 : TVirtualMCApplication(name, title),
53 fRootManager(0),
54 fPrintModulo(1),
55 fEventNo(0),
56 fVerbose(0),
57 fStack(0),
58 fDetConstruction(0),
59 fSensitiveDetector(0),
60 fPrimaryGenerator(0),
61 fMagField(0),
62 fOldGeometry(kFALSE),
63 fIsControls(kFALSE),
64 fIsMaster(kTRUE)
65 {
66 /// Standard constructor
67 /// \param name The MC application name
68 /// \param title The MC application description
69
70 // Create a user stack
71 fStack = new Ex03MCStack(1000);
72
73 // Create detector construction
75
76 // Create a calorimeter SD
77 fSensitiveDetector = new SensitiveDetector("Absorber");
78
79 // Create a primary generator
81
82 // Constant magnetic field (in kiloGauss)
83 fMagField = new TGeoUniformMagField();
84 }
85
86 //_____________________________________________________________________________
88 : TVirtualMCApplication(origin.GetName(), origin.GetTitle()),
89 fRootManager(0),
90 fPrintModulo(origin.fPrintModulo),
91 fEventNo(0),
92 fVerbose(origin.fVerbose),
93 fStack(0),
94 fDetConstruction(origin.fDetConstruction),
95 fSensitiveDetector(0),
96 fPrimaryGenerator(0),
97 fMagField(0),
98 fOldGeometry(origin.fOldGeometry),
99 fIsMaster(kFALSE)
100 {
101 /// Copy constructor for cloning application on workers (in multithreading
102 /// mode) \param origin The source MC application
103
104 // Create new user stack
105 fStack = new Ex03MCStack(1000);
106
107 // 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],
117 origin.fMagField->GetFieldValue()[2]);
118 }
119
120 //_____________________________________________________________________________
123 fRootManager(0),
124 fPrintModulo(1),
125 fEventNo(0),
126 fStack(0),
127 fDetConstruction(0),
128 fSensitiveDetector(0),
129 fPrimaryGenerator(0),
130 fMagField(0),
131 fOldGeometry(kFALSE),
132 fIsControls(kFALSE),
133 fIsMaster(kTRUE)
134 {
135 /// Default constructor
136 }
137
138 //_____________________________________________________________________________
140 {
141 /// Destructor
142
143 // cout << "MCApplication::~MCApplication " << this << endl;
144
145 delete fRootManager;
146 delete fStack;
147 if (fIsMaster) delete fDetConstruction;
148 delete fSensitiveDetector;
149 delete fPrimaryGenerator;
150 delete fMagField;
151 delete gMC;
152
153 // cout << "Done MCApplication::~MCApplication " << this << endl;
154 }
155
156 //
157 // private methods
158 //
159
160 //_____________________________________________________________________________
162 {
163 /// Register stack in the Root manager.
164
165 if (fRootManager) {
166 // cout << "MCApplication::RegisterStack: " << endl;
167 fRootManager->Register("stack", "Ex03MCStack", &fStack);
168 }
169 }
170
171 //_____________________________________________________________________________
173 {
174 /// Create histograms
175
176 fHistograms.push_back(new TH1D("1", "Edep", 100, 0., 0.1));
177 fHistograms.push_back(new TH1D("2", "XTR Gamma spectrum", 100, 0., 0.1));
178 fHistograms.push_back(
179 new TH1D("3", "Secondary Gamma spectrum", 100, 0., 0.1));
180 fHistograms.push_back(new TH1D("4", "Secondary e- spectrum", 100, 0., 0.1));
181 }
182
183 //
184 // public methods
185 //
186
187 //_____________________________________________________________________________
188 void MCApplication::InitMC(const char* setup)
189 {
190 /// Initialize MC.
191 /// The selection of the concrete MC is done in the macro.
192 /// \param setup The name of the configuration macro
193
194 fVerbose.InitMC();
195
196 if (TString(setup) != "") {
197 gROOT->LoadMacro(setup);
198 gInterpreter->ProcessLine("Config()");
199 if (!gMC) {
200 Fatal(
201 "InitMC", "Processing Config() has failed. (No MC is instantiated.)");
202 }
203 }
204
205// MT support available from root v 5.34/18
206#if ROOT_VERSION_CODE >= 336402
207 // Create Root manager
208 if (!gMC->IsMT()) {
209 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
210 // fRootManager->SetDebug(true);
211 }
212#else
213 // Create Root manager
214 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
215 // fRootManager->SetDebug(true);
216#endif
217 // Cretae histograms
218 BookHisto();
219
220 gMC->SetStack(fStack);
221 gMC->SetMagField(fMagField);
222 gMC->Init();
223 gMC->BuildPhysics();
224
226 }
227
228 //_____________________________________________________________________________
229 void MCApplication::RunMC(Int_t nofEvents)
230 {
231 /// Run MC.
232 /// \param nofEvents Number of events to be processed
233
234 fVerbose.RunMC(nofEvents);
235
236 gMC->ProcessRun(nofEvents);
237 FinishRun();
238 }
239
240 //_____________________________________________________________________________
242 {
243 /// Finish MC run.
244
245 fVerbose.FinishRun();
246 // cout << "MCApplication::FinishRun: " << endl;
247
248 // print run statisctics
249 cout << " ================== run summary =====================" << endl;
250 cout << " End of Run TotNbofEvents = " << fEventNo << endl;
251
252 cout << " Mean energy deposit in absorber = " << fHistograms[0]->GetMean()
253 << " +-" << fHistograms[0]->GetRMS() << " MeV " << endl;
254
255 cout << " Total number of XTR gammas = " << fHistograms[1]->GetEntries()
256 << endl;
257
258 cout << " Total number of all gammas = " << fHistograms[2]->GetEntries()
259 << endl;
260
261 if (fRootManager) {
262 fRootManager->WriteAll();
263 fRootManager->Close();
264 }
265 }
266
267 //_____________________________________________________________________________
269 {
270 return new MCApplication(*this);
271 }
272
273 //_____________________________________________________________________________
275 {
276 // cout << "MCApplication::InitForWorker " << this << endl;
277
278 // Create Root manager
279 fRootManager = new TMCRootManager(GetName(), TMCRootManager::kWrite);
280 // fRootManager->SetDebug(true);
281
282 // Set data to MC
283 gMC->SetStack(fStack);
284 gMC->SetMagField(fMagField);
285
287 }
288
289 //_____________________________________________________________________________
291 {
292 // cout << "MCApplication::FinishWorkerRun: " << endl;
293 if (fRootManager) {
294 fRootManager->WriteAll();
295 fRootManager->Close();
296 }
297 }
298
299 //_____________________________________________________________________________
301 {
302 /// Construct geometry using detector contruction class.
303 /// The detector contruction class is using TGeo functions or
304 /// TVirtualMC functions (if oldGeometry is selected)
305
306 fVerbose.ConstructGeometry();
307
309 }
310
311 //_____________________________________________________________________________
313 {
314 /// Initialize geometry
315
316 fVerbose.InitGeometry();
317
319 }
320
321 //_____________________________________________________________________________
323 {
324 /// Fill the user stack (derived from TVirtualMCStack) with primary
325 /// particles.
326
327 fVerbose.GeneratePrimaries();
328
330 }
331
332 //_____________________________________________________________________________
334 {
335 /// User actions at beginning of event
336
337 fVerbose.BeginEvent();
338
339 // Clear TGeo tracks (if filled)
340 if (TString(gMC->GetName()) == "TGeant3TGeo" &&
341 gGeoManager->GetListOfTracks() && gGeoManager->GetTrack(0) &&
342 ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints()) {
343
344 gGeoManager->ClearTracks();
345 // if (gPad) gPad->Clear();
346 }
347
348 fEventNo++;
349 if (fEventNo % fPrintModulo == 0) {
350 cout << "\n---> Begin of event: " << fEventNo << endl;
351 }
352 }
353
354 //_____________________________________________________________________________
356 {
357 /// User actions at beginning of a primary track.
358
359 fVerbose.BeginPrimary();
360 }
361
362 //_____________________________________________________________________________
364 {
365 /// User actions at beginning of each track.
366 /// Fill spectra.
367
368 fVerbose.PreTrack();
369
370 // Gamma
371 if (gMC->TrackPid() == kGamma) {
372 // XTR gammas
373 Int_t creatorProcess = fStack->GetCurrentTrack()->GetUniqueID();
374
375// Available since 6.07/03 and 5.34/35
376// kpTransitionRadiation = 49
377#if ((ROOT_VERSION_CODE >= ROOT_VERSION(6, 7, 3)) || \
378 ((ROOT_VERSION_CODE <= ROOT_VERSION(6, 0, 0)) && \
379 (ROOT_VERSION_CODE >= ROOT_VERSION(5, 34, 35))))
380 if (creatorProcess == kPTransitionRadiation) {
381#else
382 if (creatorProcess == kPNull) {
383#endif
384 fHistograms[1]->Fill(gMC->Etot() * 1e+03);
385 }
386 fHistograms[2]->Fill(gMC->Etot() * 1e+03);
387 }
388
389 // Secondary e-
390 if (gMC->TrackPid() == kElectron &&
392
393 fHistograms[3]->Fill((gMC->Etot() - gMC->TrackMass()) * 1e+03);
394 }
395 }
396
397 //_____________________________________________________________________________
399 {
400 /// User actions at each step
401
402 // Work around for Fluka VMC, which does not call
403 // MCApplication::PreTrack()
404 //
405 // cout << "MCApplication::Stepping" << this << endl;
406 static Int_t trackId = 0;
407 if (TString(gMC->GetName()) == "TFluka" &&
408 gMC->GetStack()->GetCurrentTrackNumber() != trackId) {
409 fVerbose.PreTrack();
410 trackId = gMC->GetStack()->GetCurrentTrackNumber();
411 }
412
413 fVerbose.Stepping();
414
416 }
417
418 //_____________________________________________________________________________
420 {
421 /// User actions after finishing of each track
422
423 fVerbose.PostTrack();
424 }
425
426 //_____________________________________________________________________________
428 {
429 /// User actions after finishing of a primary track
430
431 fVerbose.FinishPrimary();
432 }
433
434 //_____________________________________________________________________________
436 {
437 /// User actions after finishing of an event
438
439 fVerbose.FinishEvent();
440
441 // Geant3 + TGeo
442 // (use TGeo functions for visualization)
443 // if ( TString(gMC->GetName()) == "TGeant3TGeo") {
444
445 // // Draw volume
446 // gGeoManager->SetVisOption(0);
447 // gGeoManager->SetTopVisible();
448 // gGeoManager->GetTopVolume()->Draw();
449
450 // // Draw tracks (if filled)
451 // // Available when this feature is activated via
452 // // gMC->SetCollectTracks(kTRUE);
453 // if ( gGeoManager->GetListOfTracks() &&
454 // gGeoManager->GetTrack(0) &&
455 // ((TVirtualGeoTrack*)gGeoManager->GetTrack(0))->HasPoints() ) {
456
457 // gGeoManager->DrawTracks("/*"); // this means all tracks
458 // }
459 // }
460
461 // Fill Edep histogram in MeV
462 // cout << "Filling in h0 " <<
463 // fSensitiveDetector->GetHit(0)->GetEdep()*1e+03 << endl;
464 fHistograms[0]->Fill(fSensitiveDetector->GetEdep() * 1e+03);
465
466 fRootManager->Fill();
467
468 if (fEventNo % fPrintModulo == 0) {
470 }
471
473 fStack->Reset();
474 }
475
476 } // namespace TR
477}
Implementation of the TVirtualMCStack interface.
Definition Ex03MCStack.h:36
virtual TParticle * GetCurrentTrack() const
virtual Int_t GetCurrentParentTrackNumber() const
The detector construction (via TGeo )
Implementation of the TVirtualMCApplication.
virtual void InitGeometry()
virtual void FinishPrimary()
virtual void GeneratePrimaries()
Ex03MCStack * fStack
VMC stack.
virtual void ConstructGeometry()
Int_t fPrintModulo
The event modulus number to be printed.
Bool_t fIsMaster
If is on master thread.
SensitiveDetector * fSensitiveDetector
Absorber SD.
PrimaryGenerator * fPrimaryGenerator
Primary generator.
TMCVerbose fVerbose
VMC verbose helper.
virtual TVirtualMCApplication * CloneForWorker() const
virtual void FinishRunOnWorker()
virtual void BeginPrimary()
void RunMC(Int_t nofEvents)
TGeoUniformMagField * fMagField
Magnetic field.
TMCRootManager * fRootManager
Root manager.
Int_t fEventNo
Event counter.
virtual void InitOnWorker()
void InitMC(const char *setup)
DetectorConstruction * fDetConstruction
Dector construction.
The primary generator.
The absorber sensitive detector.
Double_t GetEdep() const
Return energy deposit.
virtual void Print(Option_t *option="") const
std::vector< TH1D * > fHistograms