CbmRoot
CbmPsdMC.cxx
Go to the documentation of this file.
1 
11 #include "CbmPsdMC.h"
12 
13 #include "TGeoManager.h"
14 #include "TKey.h"
15 #include "TVirtualMC.h"
16 #include <cassert>
17 #include <string>
18 
19 #include "CbmGeometryUtils.h"
20 #include "CbmModuleList.h"
21 #include "CbmPsdPoint.h"
22 #include "CbmStack.h"
23 
24 
25 // ----- Default constructor -------------------------------------------
26 CbmPsdMC::CbmPsdMC(Bool_t active, const char* name)
27  : FairDetector(name, active, ToIntegralType(ECbmModuleId::kPsd))
28  , fPosX(0.)
29  , fPosZ(0.)
30  , fRotY(0.)
31  , fUserPlacement(kFALSE)
32  , fPsdPoints(new TClonesArray("CbmPsdPoint"))
33  , fTrackID(-3)
34  , fAddress(-3)
35  , fPos()
36  , fMom()
37  , fTime(-1.)
38  , fLength(-1.)
39  , fEloss(-1.)
40  , fLayerID(-1)
41  , fModuleID(-1) {}
42 // -------------------------------------------------------------------------
43 
44 
45 // ----- Destructor ----------------------------------------------------
47  if (fPsdPoints) {
48  fPsdPoints->Delete();
49  delete fPsdPoints;
50  }
51 }
52 // -------------------------------------------------------------------------
53 
54 
55 // ----- Construct the geometry from file ------------------------------
57 
58 
59  /*
60  LOG(info) << GetName() << ": Constructing geometry from file "
61  << fgeoName;
62 
63  // --- A TGeoManager should be present
64  assert(gGeoManager);
65 
66  // --- Only ROOT geometries are supported
67  if ( ! GetGeometryFileName().EndsWith(".root") ) {
68  LOG(fatal) << GetName() << ": Geometry format of file "
69  << GetGeometryFileName() << " not supported.";
70  return;
71  }
72 
73  // --- Look for PSD volume and transformation matrix in geometry file
74  TFile* geoFile = new TFile(fgeoName);
75  assert ( geoFile );
76  TKey* key = nullptr;
77  TIter keyIter(geoFile->GetListOfKeys());
78  Bool_t foundVolume = kFALSE;
79  Bool_t foundMatrix = kFALSE;
80  std::string volumeName = "";
81  TGeoMatrix* transformation = nullptr;
82 
83  while ( (key = (TKey*)keyIter() ) ) {
84 
85  if ( key->ReadObj()->InheritsFrom("TGeoVolume") ) {
86  if ( foundVolume) {
87  LOG(fatal) << GetName() << ": More than one TGeoVolume in file! "
88  << volumeName << " " << key->GetName();
89  break;
90  } //? already found a volume
91  volumeName = key->GetName();
92  foundVolume = kTRUE;
93  continue;
94  } //? key inherits from TGeoVolume
95 
96  if ( key->ReadObj()->InheritsFrom("TGeoMatrix") ) {
97  if ( foundMatrix ) {
98  LOG(fatal) << GetName() << ": More than one TGeoMatrix in file! ";
99  break;
100  } //? already found a matrix
101  transformation = dynamic_cast<TGeoMatrix*>(key->ReadObj());
102  foundMatrix = kTRUE;
103  continue;
104  } //? key inherits from TGeoMatrix
105 
106  } //# keys in file
107 
108  if ( ! foundVolume ) LOG(fatal) << GetName() << ": No TGeoVolume in file " << fgeoName;
109  if ( ! foundMatrix ) LOG(fatal) << GetName() << ": No TGeoMatrix in file " << fgeoName;
110 
111  // --- Import PSD volume
112  TGeoVolume* psdVolume = TGeoVolume::Import(fgeoName, volumeName.c_str());
113 
114  // Add PSD to the geometry
115  gGeoManager->GetTopVolume()->AddNode(psdVolume, 0, transformation);
116  if ( gLogger->IsLogNeeded(fair::Severity::debug) ) {
117  psdVolume->Print();
118  transformation->Print();
119  }
120 
121  // Register all sensitive volumes
122  for (Int_t i=0; i<psdVolume->GetNdaughters(); ++i)
123  RegisterSensitiveVolumes(psdVolume->GetNode(i));
124 // RegisterSensitiveVolumes(psdVolume->GetNode(0));
125 
126  LOG(debug) << GetName() << ": " << fNbOfSensitiveVol
127  << " sensitive volumes";
128 */
129 
130  LOG(info) << "Importing PSD geometry from ROOT file " << fgeoName.Data();
132 }
133 // -------------------------------------------------------------------------
134 
135 
136 // ----- End of event action -------------------------------------------
138  Print(); // Status output
139  fPsdPoints->Delete();
140 }
141 // -------------------------------------------------------------------------
142 
143 
144 // ----- Print ---------------------------------------------------------
145 void CbmPsdMC::Print(Option_t*) const {
146  LOG(info) << fName << ": " << fPsdPoints->GetEntriesFast()
147  << " points registered in this event.";
148 }
149 // -------------------------------------------------------------------------
150 
151 
152 // ----- Public method ProcessHits --------------------------------------
153 Bool_t CbmPsdMC::ProcessHits(FairVolume*) {
154 
155  // No action for neutral particles
156  if (TMath::Abs(gMC->TrackCharge()) <= 0) return kFALSE;
157 
158  // --- If this is the first step for the track in the volume:
159  // Reset energy loss and store track parameters
160  if (gMC->IsTrackEntering()) {
161  fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
162 
163  gMC->CurrentVolOffID(1, fLayerID);
164  gMC->CurrentVolOffID(3, fModuleID);
165 
166  fAddress = fLayerID;
167  gMC->TrackPosition(fPos);
168  gMC->TrackMomentum(fMom);
169  fTime = gMC->TrackTime() * 1.0e09;
170  fLength = gMC->TrackLength();
171  fEloss = 0.;
172  } //? track entering
173 
174  // --- For all steps within active volume: sum up differential energy loss
175  fEloss += gMC->Edep();
176 
177  // --- If track is leaving: get track parameters and create CbmstsPoint
178  if (gMC->IsTrackExiting() || gMC->IsTrackStop()
179  || gMC->IsTrackDisappeared()) {
180 
181  // Create CbmPsdPoint
182  Int_t size = fPsdPoints->GetEntriesFast();
183  CbmPsdPoint* psdPoint = new ((*fPsdPoints)[size]) CbmPsdPoint(
184  fTrackID, fAddress, fPos.Vect(), fMom.Vect(), fTime, fLength, fEloss);
185  psdPoint->SetModuleID(fModuleID + 1);
186 
187  // --- Increment number of PsdPoints for this track in the stack
188  CbmStack* stack = dynamic_cast<CbmStack*>(gMC->GetStack());
189  assert(stack);
191 
192  } //? track is exiting or stopped
193 
194  return kTRUE;
195 }
196 // -------------------------------------------------------------------------
197 
198 
199 // ----- Register the sensitive volumes --------------------------------
201 
202  TObjArray* daughters = node->GetVolume()->GetNodes();
203  for (Int_t iDaughter = 0; iDaughter < daughters->GetEntriesFast();
204  iDaughter++) {
205  TGeoNode* daughter = dynamic_cast<TGeoNode*>(daughters->At(iDaughter));
206  assert(daughter);
207  if (daughter->GetNdaughters() > 0) RegisterSensitiveVolumes(daughter);
208  TGeoVolume* daughterVolume = daughter->GetVolume();
209  if (CheckIfSensitive(daughterVolume->GetName())) {
210  AddSensitiveVolume(daughterVolume);
211  } //? Sensitive volume
212  } //# Daughter nodes
213 }
214 // -------------------------------------------------------------------------
215 
216 
CbmPsdMC::Print
virtual void Print(Option_t *opt="") const
Screen log Prints current number of StsPoints in array. Virtual from TObject.
Definition: CbmPsdMC.cxx:145
CbmPsdMC::fEloss
Double_t fEloss
length
Definition: CbmPsdMC.h:151
CbmPsdPoint
Definition: CbmPsdPoint.h:24
CbmPsdMC::ProcessHits
virtual Bool_t ProcessHits(FairVolume *volume=0)
Stepping action.
Definition: CbmPsdMC.cxx:153
CbmPsdMC::EndOfEvent
virtual void EndOfEvent()
Action at end of event.
Definition: CbmPsdMC.cxx:137
CbmPsdMC::ConstructGeometry
virtual void ConstructGeometry()
Construct the PSD geometry in the TGeoManager.
Definition: CbmPsdMC.cxx:56
CbmPsdMC::CbmPsdMC
CbmPsdMC(Bool_t active=kTRUE, const char *name="PSDMC")
Constructor.
Definition: CbmPsdMC.cxx:26
ECbmModuleId
ECbmModuleId
Definition: CbmDefs.h:33
CbmPsdMC::fTime
Double_t fTime
momentum
Definition: CbmPsdMC.h:149
CbmStack.h
CbmStack::AddPoint
void AddPoint(ECbmModuleId iDet)
Definition: CbmStack.cxx:383
CbmPsdMC.h
CbmPsdMC::CheckIfSensitive
virtual Bool_t CheckIfSensitive(std::string name)
Check whether a volume is sensitive.
Definition: CbmPsdMC.h:50
CbmPsdMC::fModuleID
Int_t fModuleID
layer ID
Definition: CbmPsdMC.h:153
CbmPsdMC::fLength
Double_t fLength
time
Definition: CbmPsdMC.h:150
CbmPsdMC::~CbmPsdMC
virtual ~CbmPsdMC()
Definition: CbmPsdMC.cxx:46
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmModuleList.h
CbmPsdMC::RegisterSensitiveVolumes
void RegisterSensitiveVolumes(TGeoNode *node)
module ID
Definition: CbmPsdMC.cxx:200
CbmPsdMC
Class for the MC transport of the CBM-PSD.
Definition: CbmPsdMC.h:29
ToIntegralType
constexpr auto ToIntegralType(T enumerator) -> typename std::underlying_type< T >::type
Definition: CbmDefs.h:24
CbmPsdMC::fLayerID
Int_t fLayerID
energy loss
Definition: CbmPsdMC.h:152
Cbm::GeometryUtils::ImportRootGeometry
void ImportRootGeometry(TString &filename, FairModule *mod, TGeoMatrix *mat)
Definition: CbmGeometryUtils.cxx:140
CbmPsdMC::fMom
TLorentzVector fMom
position
Definition: CbmPsdMC.h:148
ECbmModuleId::kPsd
@ kPsd
Projectile spectator detector.
CbmPsdMC::fPos
TLorentzVector fPos
address (module and layer)
Definition: CbmPsdMC.h:147
CbmStack
Definition: CbmStack.h:45
CbmPsdMC::fAddress
Int_t fAddress
track index
Definition: CbmPsdMC.h:146
CbmPsdPoint::SetModuleID
void SetModuleID(Int_t mod)
Definition: CbmPsdPoint.h:61
CbmPsdMC::fTrackID
Int_t fTrackID
Output array.
Definition: CbmPsdMC.h:145
CbmGeometryUtils.h
CbmPsdPoint.h
CbmPsdMC::fPsdPoints
TClonesArray * fPsdPoints
Definition: CbmPsdMC.h:142