CbmRoot
CbmStsMC.cxx
Go to the documentation of this file.
1 
8 #include "CbmStsMC.h"
9 
10 #include "TGeoBBox.h"
11 #include "TGeoManager.h"
12 #include "TGeoPhysicalNode.h"
13 #include "TKey.h"
14 #include "TParticle.h"
15 #include "TVector3.h"
16 #include "TVirtualMC.h"
17 #include <TFile.h>
18 
19 #include "FairLogger.h"
20 
21 #include "CbmGeometryUtils.h"
22 #include "CbmStack.h"
23 #include "CbmStsElement.h"
24 #include "CbmStsPoint.h"
25 #include "CbmStsSetup.h"
26 
27 using std::map;
28 using std::pair;
29 
30 // ----- Constructor ---------------------------------------------------
31 CbmStsMC::CbmStsMC(Bool_t active, const char* name)
32  : FairDetector(name, active, ToIntegralType(ECbmModuleId::kSts))
33  , fStatusIn()
34  , fStatusOut()
35  , fEloss(0.)
36  , fAddressMap()
37  , fStsPoints(NULL)
38  , fSetup(NULL)
39  , fCombiTrans(NULL)
40  , fProcessNeutrals(kFALSE) {}
41 // -------------------------------------------------------------------------
42 
43 
44 // ----- Destructor ----------------------------------------------------
46  if (fStsPoints) {
47  fStsPoints->Delete();
48  delete fStsPoints;
49  }
50  if (fCombiTrans) { delete fCombiTrans; }
51 }
52 // -------------------------------------------------------------------------
53 
54 
55 // ----- ConstructGeometry -----------------------------------------------
57 
58  TString fileName = GetGeometryFileName();
59 
60  // --- Only ROOT geometries are supported
61  if (!fileName.EndsWith(".root")) {
62  LOG(fatal) << GetName() << ": Geometry format of file " << fileName.Data()
63  << " not supported.";
64  }
66 }
67 // -------------------------------------------------------------------------
68 
69 
70 // ----- End of event action -------------------------------------------
72  Print(); // Status output
73  Reset(); // Reset the track status parameters
74 }
75 // -------------------------------------------------------------------------
76 
77 
78 // ----- Initialise ----------------------------------------------------
80 
81  // --- Instantiate the output array
82  fStsPoints = new TClonesArray("CbmStsPoint");
83 
84  // --- Get the CbmStsSetup instance and construct a map from full path
85  // --- to address for each sensor. This is needed to store the unique x
86  // --- address of the activated sensor in the CbmStsPoint class.
87  // --- Unfortunately, the full geometry path (string) is the only way
88  // --- to unambiguously identify the current active node during the
89  // --- transport. It may seem that looking up a string in a map is not
90  // --- efficient. I checked however, that the performance penalty is very
91  // --- small.
92  fAddressMap.clear();
94  fSetup->Init();
95  Int_t nUnits = fSetup->GetNofDaughters();
96  for (Int_t iUnit = 0; iUnit < nUnits; iUnit++) {
97  CbmStsElement* unit = fSetup->GetDaughter(iUnit);
98  Int_t nLadd = unit->GetNofDaughters();
99  for (Int_t iLadd = 0; iLadd < nLadd; iLadd++) {
100  CbmStsElement* ladd = unit->GetDaughter(iLadd);
101  Int_t nHlad = ladd->GetNofDaughters();
102  for (Int_t iHlad = 0; iHlad < nHlad; iHlad++) {
103  CbmStsElement* hlad = ladd->GetDaughter(iHlad);
104  Int_t nModu = hlad->GetNofDaughters();
105  for (Int_t iModu = 0; iModu < nModu; iModu++) {
106  CbmStsElement* modu = hlad->GetDaughter(iModu);
107  Int_t nSens = modu->GetNofDaughters();
108  for (Int_t iSens = 0; iSens < nSens; iSens++) {
109  CbmStsElement* sensor = modu->GetDaughter(iSens);
110  TString path = sensor->GetPnode()->GetName();
111  if (!path.BeginsWith("/")) path.Prepend("/");
112  pair<TString, Int_t> a(path, sensor->GetAddress());
113  fAddressMap.insert(a);
114  TString test = sensor->GetPnode()->GetName();
115  }
116  }
117  }
118  }
119  }
120  LOG(info) << fName << ": Address map initialised with "
121  << Int_t(fAddressMap.size()) << " sensors. ";
122 
123  // --- Call the Initialise method of the mother class
124  FairDetector::Initialize();
125 }
126 // -------------------------------------------------------------------------
127 
128 
129 // ----- ProcessHits ----------------------------------------------------
130 Bool_t CbmStsMC::ProcessHits(FairVolume* /*vol*/) {
131 
132  // --- If this is the first step for the track in the sensor:
133  // Reset energy loss and store track parameters
134  if (gMC->IsTrackEntering()) {
135  fEloss = 0.;
136  fStatusIn.Reset();
137  fStatusOut.Reset();
139  }
140 
141  // --- For all steps within active volume: sum up differential energy loss
142  fEloss += gMC->Edep();
143 
144 
145  // --- If track is leaving: get track parameters and create CbmstsPoint
146  if (gMC->IsTrackExiting() || gMC->IsTrackStop()
147  || gMC->IsTrackDisappeared()) {
148 
150 
151  // --- No action if no energy loss (neutral particles)
152  if (fEloss == 0. && (!fProcessNeutrals)) return kFALSE;
153 
154  // --- Add a StsPoint to the output array. Increment stack counter.
155  CreatePoint();
156 
157  // --- Increment number of StsPoints for this track
158  CbmStack* stack = (CbmStack*) gMC->GetStack();
160 
161  } //? track is exiting or stopped
162 
163 
164  return kTRUE;
165 }
166 // -------------------------------------------------------------------------
167 
168 
169 // ----- Reset output array and track status ---------------------------
171  fStsPoints->Delete();
172  fStatusIn.Reset();
173  fStatusOut.Reset();
174  fEloss = 0.;
175 }
176 // -------------------------------------------------------------------------
177 
178 
179 // ----- Screen log ----------------------------------------------------
180 void CbmStsMC::Print(Option_t* /*opt*/) const {
181  //Int_t nHits = fStsPoints->GetEntriesFast();
182  LOG(info) << fName << ": " << fStsPoints->GetEntriesFast()
183  << " points registered in this event.";
184 }
185 // -------------------------------------------------------------------------
186 
187 
188 // =========================================================================
189 // Private methods
190 // =========================================================================
191 
192 
193 // ----- Create STS point ----------------------------------------------
195 
196  // --- Check detector address
198  LOG(error) << GetName() << ": inconsistent detector addresses "
199  << fStatusIn.fAddress << " " << fStatusOut.fAddress;
200  return NULL;
201  }
202 
203  // --- Check track Id
205  LOG(error) << GetName() << ": inconsistent track Id " << fStatusIn.fTrackId
206  << " " << fStatusOut.fTrackId;
207  return NULL;
208  }
209 
210  // --- Check track PID
211  if (fStatusIn.fPid != fStatusOut.fPid) {
212  LOG(error) << GetName() << ": inconsistent track PID " << fStatusIn.fPid
213  << " " << fStatusOut.fPid;
214  return NULL;
215  }
216 
217  // --- Entry position and momentum
218  TVector3 posIn(fStatusIn.fX, fStatusIn.fY, fStatusIn.fZ);
219  TVector3 momIn(fStatusIn.fPx, fStatusIn.fPy, fStatusIn.fPz);
220 
221  // --- Exit position and momentum
222  TVector3 posOut(fStatusOut.fX, fStatusOut.fY, fStatusOut.fZ);
223  TVector3 momOut(fStatusOut.fPx, fStatusOut.fPy, fStatusOut.fPz);
224 
225  // --- Time and track length (average between in and out)
226  Double_t time = 0.5 * (fStatusIn.fTime + fStatusOut.fTime);
227  Double_t length = 0.5 * (fStatusIn.fLength + fStatusOut.fLength);
228 
229  // --- Flag for entry/exit
230  Int_t flag = 0;
231  if (fStatusIn.fFlag) flag += 1; // first coordinate is entry step
232  if (fStatusOut.fFlag) flag += 2; // second coordinate is exit step
233 
234  // --- Debug output
235  LOG(debug2) << GetName() << ": Creating point from track "
236  << fStatusIn.fTrackId << " in sensor " << fStatusIn.fAddress
237  << ", position (" << posIn.X() << ", " << posIn.Y() << ", "
238  << posIn.Z() << "), energy loss " << fEloss;
239 
240  // --- Add new point to output array
241  Int_t newIndex = fStsPoints->GetEntriesFast();
242  return new ((*fStsPoints)[fStsPoints->GetEntriesFast()])
245  posIn,
246  posOut,
247  momIn,
248  momOut,
249  time,
250  length,
251  fEloss,
252  fStatusIn.fPid,
253  0,
254  newIndex,
255  flag);
256 }
257 // -------------------------------------------------------------------------
258 
259 
260 // ----- Set the current track status ----------------------------------
262 
263  // --- Check for TVirtualMC and TGeomanager
264  if (!(gMC && gGeoManager)) {
265  LOG(error) << fName << ": No TVirtualMC or TGeoManager instance!";
266  return;
267  }
268 
269  // --- Address of current sensor
270  // --- Use the geometry path from TVirtualMC; cannot rely on
271  // --- TGeoManager here.
272  TString path = gMC->CurrentVolPath();
273  auto it = fAddressMap.find(path);
274  if (it == fAddressMap.end()) {
275  LOG(fatal) << fName << ": Path not found in address map! "
276  << gGeoManager->GetPath();
277  status.fAddress = 0;
278  } else
279  status.fAddress = it->second;
280 
281  // --- Index and PID of current track
282  status.fTrackId = gMC->GetStack()->GetCurrentTrackNumber();
283  status.fPid = gMC->GetStack()->GetCurrentTrack()->GetPdgCode();
284 
285  // --- Position
286  gMC->TrackPosition(status.fX, status.fY, status.fZ);
287 
288  // --- Momentum
289  Double_t dummy;
290  gMC->TrackMomentum(status.fPx, status.fPy, status.fPz, dummy);
291 
292  // --- Time and track length
293  status.fTime = gMC->TrackTime() * 1.e9; // conversion into ns
294  status.fLength = gMC->TrackLength();
295 
296  // --- Status flag (entry/exit or new/stopped/disappeared)
297  if (gMC->IsTrackEntering()) {
298  if (gMC->IsNewTrack())
299  status.fFlag = kFALSE; // Track created in sensor
300  else
301  status.fFlag = kTRUE; // Track entering
302  } else { // exiting or stopped or disappeared
303  if (gMC->IsTrackDisappeared() || gMC->IsTrackStop())
304  status.fFlag = kFALSE; // Track stopped or disappeared in sensor
305  else
306  status.fFlag = kTRUE; // Track exiting
307  }
308 }
309 // -------------------------------------------------------------------------
310 
311 
312 //__________________________________________________________________________
315  LOG(info) << "Importing STS geometry from ROOT file " << fgeoName.Data();
317  } else {
318  LOG(info) << "Constructing STS geometry from ROOT file " << fgeoName.Data();
319  FairModule::ConstructRootGeometry();
320  }
321 }
322 
CbmStsMC
Class for the MC transport of the CBM-STS.
Definition: CbmStsMC.h:33
CbmStsMC::ProcessHits
virtual Bool_t ProcessHits(FairVolume *vol=0)
Action for a track step in a sensitive node of the STS.
Definition: CbmStsMC.cxx:130
CbmStsMC::EndOfEvent
virtual void EndOfEvent()
Action at end of event.
Definition: CbmStsMC.cxx:71
CbmStsElement::GetAddress
Int_t GetAddress() const
Definition: CbmStsElement.h:65
CbmStsTrackStatus::fAddress
Int_t fAddress
Unique address.
Definition: CbmStsTrackStatus.h:60
CbmStsMC::fProcessNeutrals
Bool_t fProcessNeutrals
Transformation matrix for geometry positioning.
Definition: CbmStsMC.h:162
CbmStsTrackStatus::fPid
Int_t fPid
MCTrack PID [PDG code].
Definition: CbmStsTrackStatus.h:62
CbmStsMC::CreatePoint
CbmStsPoint * CreatePoint()
Create a new StsPoint Creates a new CbmStsPoint using the current track status information and adds i...
Definition: CbmStsMC.cxx:194
CbmStsSetup.h
CbmStsTrackStatus::fPy
Double_t fPy
Momentum x component [GeV].
Definition: CbmStsTrackStatus.h:67
CbmStsTrackStatus::fZ
Double_t fZ
x position [cm]
Definition: CbmStsTrackStatus.h:65
CbmStsTrackStatus::fTime
Double_t fTime
Time since track creation [ns].
Definition: CbmStsTrackStatus.h:69
CbmStsSetup::Instance
static CbmStsSetup * Instance()
Definition: CbmStsSetup.cxx:293
CbmStsTrackStatus::fTrackId
Int_t fTrackId
MCTrack index.
Definition: CbmStsTrackStatus.h:61
ECbmModuleId
ECbmModuleId
Definition: CbmDefs.h:33
CbmStsTrackStatus::fX
Double_t fX
x position [cm]
Definition: CbmStsTrackStatus.h:63
CbmStsMC::SetStatus
void SetStatus(CbmStsTrackStatus &status)
Set the current track status Set the current track status (in or out) with parameters obtained from T...
Definition: CbmStsMC.cxx:261
CbmStsMC::fSetup
CbmStsSetup * fSetup
Output array (CbmStsPoint)
Definition: CbmStsMC.h:159
CbmStsPoint
Definition: CbmStsPoint.h:27
CbmStsTrackStatus::Reset
void Reset()
Definition: CbmStsTrackStatus.h:44
CbmStsMC::ConstructGeometry
virtual void ConstructGeometry()
Construct the STS geometry in the TGeoManager.
Definition: CbmStsMC.cxx:56
CbmStsMC::fStsPoints
TClonesArray * fStsPoints
Definition: CbmStsMC.h:158
CbmStsElement::GetPnode
TGeoPhysicalNode * GetPnode() const
Definition: CbmStsElement.h:106
CbmStack.h
CbmStsMC.h
CbmStsMC::Reset
virtual void Reset()
Clear output array and reset current track status.
Definition: CbmStsMC.cxx:170
CbmStack::AddPoint
void AddPoint(ECbmModuleId iDet)
Definition: CbmStack.cxx:383
CbmStsTrackStatus
Stores status of track during transport. Auxiliary for CbmSts.
Definition: CbmStsTrackStatus.h:20
Cbm::GeometryUtils::IsNewGeometryFile
Bool_t IsNewGeometryFile(TString &filename)
Definition: CbmGeometryUtils.cxx:133
CbmStsMC::fAddressMap
std::map< TString, Int_t > fAddressMap
Accumulated energy loss for current track.
Definition: CbmStsMC.h:157
CbmStsElement.h
CbmStsMC::fEloss
Double_t fEloss
Track status at exit of sensor.
Definition: CbmStsMC.h:155
ClassImp
ClassImp(CbmConverterManager) InitStatus CbmConverterManager
Definition: CbmConverterManager.cxx:12
CbmStsMC::fCombiTrans
TGeoCombiTrans * fCombiTrans
Pointer to static instance of CbmStsSetup.
Definition: CbmStsMC.h:161
CbmStsSetup::Init
Bool_t Init(const char *geometryFile=nullptr)
Initialise the setup.
Definition: CbmStsSetup.cxx:201
CbmStsTrackStatus::fY
Double_t fY
x position [cm]
Definition: CbmStsTrackStatus.h:64
CbmStsTrackStatus::fPz
Double_t fPz
Momentum x component [GeV].
Definition: CbmStsTrackStatus.h:68
CbmStsTrackStatus::fFlag
Bool_t fFlag
Status flag. TRUE if normal entry/exit, else FALSE.
Definition: CbmStsTrackStatus.h:71
CbmStsTrackStatus::fPx
Double_t fPx
Momentum x component [GeV].
Definition: CbmStsTrackStatus.h:66
CbmStsMC::CbmStsMC
CbmStsMC(Bool_t active=kTRUE, const char *name="STSMC")
Definition: CbmStsMC.cxx:31
CbmStsPoint.h
CbmStsMC::ConstructRootGeometry
virtual void ConstructRootGeometry(TGeoMatrix *shift=NULL)
Definition: CbmStsMC.cxx:313
ToIntegralType
constexpr auto ToIntegralType(T enumerator) -> typename std::underlying_type< T >::type
Definition: CbmDefs.h:24
CbmStsElement::GetDaughter
CbmStsElement * GetDaughter(Int_t index) const
Definition: CbmStsElement.cxx:120
CbmStsElement::GetNofDaughters
Int_t GetNofDaughters() const
Definition: CbmStsElement.h:95
CbmStsMC::Initialize
virtual void Initialize()
Initialisation.
Definition: CbmStsMC.cxx:79
Cbm::GeometryUtils::ImportRootGeometry
void ImportRootGeometry(TString &filename, FairModule *mod, TGeoMatrix *mat)
Definition: CbmGeometryUtils.cxx:140
CbmStsMC::~CbmStsMC
virtual ~CbmStsMC()
Definition: CbmStsMC.cxx:45
CbmStack
Definition: CbmStack.h:45
CbmStsMC::Print
virtual void Print(Option_t *opt="") const
Screen log Prints current number of StsPoints in array. Virtual from TObject.
Definition: CbmStsMC.cxx:180
ECbmModuleId::kSts
@ kSts
Silicon Tracking System.
CbmStsMC::fStatusOut
CbmStsTrackStatus fStatusOut
Track status at entry of sensor.
Definition: CbmStsMC.h:154
CbmStsElement
Class representing an element of the STS setup.
Definition: CbmStsElement.h:32
CbmStsMC::fStatusIn
CbmStsTrackStatus fStatusIn
Definition: CbmStsMC.h:153
CbmGeometryUtils.h
CbmStsTrackStatus::fLength
Double_t fLength
Length since track creation [cm].
Definition: CbmStsTrackStatus.h:70