VMC Examples Version 6.6
Loading...
Searching...
No Matches
Ex03cCalorimeterSD.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 Ex03cCalorimeterSD.cxx
11/// \brief Implementation of the Ex03cCalorimeterSD class
12///
13/// Geant4 ExampleN03 adapted to Virtual Monte Carlo \n
14/// Id: ExN03CalorimeterSD.cc,v 1.6 2002/01/09 17:24:12 ranjard Exp \n
15/// GEANT4 tag $Name: $
16///
17/// \date 21/08/2019
18/// \author Benedikt Volkel, CERN
19
20#include "Ex03cCalorimeterSD.h"
21#include "Ex03CalorHit.h"
23
24#include <Riostream.h>
25#include <TLorentzVector.h>
26#include <TMCManager.h>
27#include <TMCRootManager.h>
28#include <TTree.h>
29#include <TVirtualMC.h>
30
31/// \cond CLASSIMP
32ClassImp(Ex03cCalorimeterSD)
33 /// \endcond
34
35 using namespace std;
36
37//_____________________________________________________________________________
39 const char* name, Ex03cDetectorConstruction* detector)
40 : TNamed(name, ""),
41 fMC(0),
42 fDetector(detector),
43 fCalCollection(0),
44 fAbsorberVolId(0),
45 fGapVolId(0),
46 fVerboseLevel(1)
47{
48 /// Standard constructor.
49 /// Create hits collection and an empty hit for each layer
50 /// As the copy numbers may start from 0 or 1 (depending on
51 /// geometry model, we create one more layer for this case.)
52 /// \param name The calorimeter hits collection name
53 /// \param detector The detector construction
54
55 fCalCollection = new TClonesArray("Ex03CalorHit", 500);
56 for (Int_t i = 0; i < fDetector->GetNbOfLayers() + 1; i++)
57 new ((*fCalCollection)[i]) Ex03CalorHit();
58}
59
60//_____________________________________________________________________________
62 const Ex03cCalorimeterSD& origin, Ex03cDetectorConstruction* detector)
63 : TNamed(origin),
64 fMC(0),
65 fDetector(detector),
66 fCalCollection(0),
67 fAbsorberVolId(origin.fAbsorberVolId),
68 fGapVolId(origin.fGapVolId),
69 fVerboseLevel(origin.fVerboseLevel)
70{
71 /// Copy constructor (for clonig on worker thread in MT mode).
72 /// Create hits collection and an empty hit for each layer
73 /// As the copy numbers may start from 0 or 1 (depending on
74 /// geometry model, we create one more layer for this case.)
75 /// \param origin The source object (on master).
76 /// \param detector The detector construction
77
78 fCalCollection = new TClonesArray("Ex03CalorHit", 500);
79 for (Int_t i = 0; i < fDetector->GetNbOfLayers() + 1; i++)
80 new ((*fCalCollection)[i]) Ex03CalorHit();
81}
82
83//_____________________________________________________________________________
85 : TNamed(),
86 fDetector(0),
87 fCalCollection(0),
88 fAbsorberVolId(0),
89 fGapVolId(0),
90 fVerboseLevel(1)
91{
92 /// Default constructor
93}
94
95//_____________________________________________________________________________
97{
98 /// Destructor
99
100 if (fCalCollection) fCalCollection->Delete();
101 delete fCalCollection;
102}
103
104//
105// private methods
106//
107
108//_____________________________________________________________________________
110{
111 /// \return The hit for the specified layer.
112 /// \param i The layer number
113
114 return (Ex03CalorHit*)fCalCollection->At(i);
115}
116
117//_____________________________________________________________________________
119{
120 /// Reset all hits in the hits collection.
121
122 for (Int_t i = 0; i < fCalCollection->GetEntriesFast(); i++)
123 GetHit(i)->Reset();
124}
125
126//
127// public methods
128//
129
130//_____________________________________________________________________________
132{
133 /// Register hits collection in the Root manager;
134 /// set sensitive volumes.
135
136 if (TMCRootManager::Instance()) Register();
137
138 // Keep the pointer to TVirtualMC object as a data member
139 // to avoid a possible performance penalty due to a frequent retrieval
140 // from the thread-local storage
141 if (TMCManager::Instance()) {
142 TMCManager::Instance()->ConnectEnginePointer(fMC);
143 }
144 else {
145 fMC = gMC;
146 }
147
148 fAbsorberVolId = fMC->VolId("ABSO");
149 fGapVolId = fMC->VolId("GAPX");
150}
151
152//_____________________________________________________________________________
154{
155 /// Account energy deposit and track lengths for each layer in its hit.
156
157 Int_t copyNo;
158 Int_t id = fMC->CurrentVolID(copyNo);
159
160 if (id != fAbsorberVolId && id != fGapVolId) return false;
161
162 fMC->CurrentVolOffID(2, copyNo);
163 // cout << "Got copyNo "<< copyNo << " " << fMC->CurrentVolPath() << endl;
164
165 Double_t edep = fMC->Edep();
166
167 Double_t step = 0.;
168 if (fMC->TrackCharge() != 0.) step = fMC->TrackStep();
169
170 if (!GetHit(copyNo)) {
171 std::cerr << "No hit found for layer with copyNo = " << copyNo << endl;
172 return false;
173 }
174
175 if (id == fAbsorberVolId) {
176 GetHit(copyNo)->AddAbs(edep, step);
177 }
178
179 if (id == fGapVolId) {
180 GetHit(copyNo)->AddGap(edep, step);
181 }
182
183 return true;
184}
185
186//_____________________________________________________________________________
188{
189 /// Print hits collection (if verbose) and reset hits afterwards.
190
191 if (fVerboseLevel > 1) Print();
192
193 // Reset hits collection
194 ResetHits();
195}
196
197//_____________________________________________________________________________
199{
200 /// Register the hits collection in Root manager.
201
202 TMCRootManager::Instance()->Register("hits", "TClonesArray", &fCalCollection);
203}
204
205//_____________________________________________________________________________
206void Ex03cCalorimeterSD::Print(Option_t* /*option*/) const
207{
208 /// Print the hits collection.
209
210 Int_t nofHits = fCalCollection->GetEntriesFast();
211
212 cout << "\n-------->Hits Collection: in this event: " << endl;
213
214 for (Int_t i = 0; i < nofHits; i++) (*fCalCollection)[i]->Print();
215}
216
217//_____________________________________________________________________________
219{
220 /// Print the total values for all layers.
221
222 Double_t totEAbs = 0.;
223 Double_t totLAbs = 0.;
224 Double_t totEGap = 0.;
225 Double_t totLGap = 0.;
226
227 Int_t nofHits = fCalCollection->GetEntriesFast();
228 for (Int_t i = 0; i < nofHits; i++) {
229 totEAbs += GetHit(i)->GetEdepAbs();
230 totLAbs += GetHit(i)->GetTrakAbs();
231 totEGap += GetHit(i)->GetEdepGap();
232 totLGap += GetHit(i)->GetTrakGap();
233 }
234
235 cout << " Absorber: total energy (MeV): " << setw(7) << totEAbs * 1.0e03
236 << " total track length (cm): " << setw(7) << totLAbs << endl
237 << " Gap: total energy (MeV): " << setw(7) << totEGap * 1.0e03
238 << " total track length (cm): " << setw(7) << totLGap << endl;
239}
Definition of the Ex03cCalorimeterSD class.
Definition of the Ex03cDetectorConstruction class.
The calorimeter hit.
Double_t GetEdepGap()
Double_t GetTrakGap()
void AddAbs(Double_t de, Double_t dl)
Double_t GetEdepAbs()
Double_t GetTrakAbs()
void AddGap(Double_t de, Double_t dl)
The calorimeter sensitive detector.
Int_t fAbsorberVolId
The absorber volume Id.
Int_t fVerboseLevel
Verbosity level.
Int_t fGapVolId
The gap volume Id.
Ex03CalorHit * GetHit(Int_t i) const
TVirtualMC * fMC
The VMC implementation.
virtual void Print(Option_t *option="") const
Ex03cDetectorConstruction * fDetector
Detector construction.
TClonesArray * fCalCollection
Hits collection.
The detector construction (via TGeo )