BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnSilicon.cxx
Go to the documentation of this file.
1#include "BmnSilicon.h"
2
3#include "BmnSiliconPoint.h"
4#include "CbmStack.h"
5#include "FairGeoMedia.h"
6#include "FairGeoRootBuilder.h"
7#include "FairMCPoint.h"
8#include "FairRootManager.h"
9#include "FairVolume.h"
10#include "TFile.h"
11#include "TGDMLParse.h"
12#include "TGeoManager.h"
13#include "TParticle.h"
14#include "TParticlePDG.h"
15#include "TRegexp.h"
16#include "TVirtualMC.h"
17
18#include <iostream>
19
20using namespace std;
21
23 : FairDetector("Silicon", kTRUE)
24{
25 fPointCollection = new TClonesArray("BmnSiliconPoint");
26 fPosIndex = 0;
27 fVerboseLevel = 1;
29}
30
31BmnSilicon::BmnSilicon(const char* name, Bool_t active)
32 : FairDetector(name, active)
33{
34 fPointCollection = new TClonesArray("BmnSiliconPoint");
35 fPosIndex = 0;
36 fVerboseLevel = 1;
38}
39
41{
42 if (fPointCollection) {
43 fPointCollection->Delete();
44 delete fPointCollection;
45 }
46}
47
48//------------------------------------------------------------------------------
49
51{
52
53 // Set parameters at entrance of volume. Reset ELoss.
54 if (gMC->IsTrackEntering()) {
55
57 fELoss = 0.;
58 fTime = gMC->TrackTime() * 1.0e09;
59 fLength = gMC->TrackLength();
60 fIsPrimary = 0;
61 fCharge = -1;
62 fPdgId = 0;
63
64 TLorentzVector PosIn;
65 gMC->TrackPosition(PosIn);
66 fPosIn.SetXYZ(PosIn.X(), PosIn.Y(), PosIn.Z());
67
68 TLorentzVector MomIn;
69 gMC->TrackMomentum(MomIn);
70 fMomIn.SetXYZ(MomIn.X(), MomIn.Y(), MomIn.Z());
71
72 TParticle* part = 0;
73 part = gMC->GetStack()->GetCurrentTrack();
74 if (part) {
75 fIsPrimary = (Int_t)part->IsPrimary();
76 fCharge = (Int_t)part->GetPDG()->Charge();
77 fPdgId = (Int_t)part->GetPdgCode();
78 }
79
80 fVolumeID = vol->getMCid();
81 fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
82 }
83
84 // Sum energy loss for all steps in the active volume
85 fELoss += gMC->Edep();
86
87 // Create BmnSiliconPoint at EXIT of active volume;
88 if ((gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()) && fELoss > 0) {
89
90 TLorentzVector PosOut;
91 gMC->TrackPosition(PosOut);
92 fPosOut.SetXYZ(PosOut.X(), PosOut.Y(), PosOut.Z());
93
94 TLorentzVector MomOut;
95 gMC->TrackMomentum(MomOut);
96 fMomOut.SetXYZ(MomOut.X(), MomOut.Y(), MomOut.Z());
97
98 // correction step to avoid the seg. violation error due to invalid memory access
99 TVector3 diff_pos = fPosIn - fPosOut;
100
101 if (diff_pos.Mag() < 0.001)
102 return kFALSE; // ignore points produced by tracks with zero length inside the current sens. volume
103 if (fMomOut.Mag() == 0)
104 return kFALSE; // ignore points produced by tracks with zero momentum inside the current sens. volume
105
106 TVector3 corr_step = fMomOut;
107 corr_step.SetMag(0.001); // 10 um
108 TVector3 pos = fPosOut;
109 fPosOut = pos - corr_step;
110 gGeoManager->FindNode(fPosOut[0], fPosOut[1], fPosOut[2]);
111
112 if (gGeoManager->GetCurrentNode()->GetMotherVolume() == 0)
113 return kFALSE; // check if the current node has its mother vol.
114
115 // Determine station and module numbers for the current hit ------------
116 Int_t stationNum = -1; // current station number (default)
117 Int_t moduleNum = -1; // current module number (default)
118
119 TGeoVolume* motherVolume = gGeoManager->GetCurrentNode()->GetMotherVolume();
120 TString moduleVolumeName = motherVolume->GetName();
121 TRegexp expr = "^module[0-9]+_station[0-9]+.*";
122 if (moduleVolumeName.Contains(expr)) {
123 TRegexp mod_expr = "module[0-9]+";
124 TRegexp stat_expr = "station[0-9]+";
125 moduleNum = TString(TString(moduleVolumeName(mod_expr))(TRegexp("[0-9]+"))).Atoi();
126 stationNum = TString(TString(moduleVolumeName(stat_expr))(TRegexp("[0-9]+"))).Atoi();
127 }
128
129 if (stationNum == -1 || moduleNum == -1)
130 return kFALSE;
131
134
135 // if (stationNum == -1) {
136 // cout << "BmnSilicon.cxx" << endl;
137 // cout << moduleVolumeName << endl;
138 // cout << "stationNum = " << stationNum << "\n";
139 // cout << "moduleNum = " << moduleNum << "\n";
140 // p->Print(nullptr);
141 // cout << "\n";
142 // }
143
144 p->SetStation(stationNum);
145 p->SetModule(moduleNum);
146
147 ((CbmStack*)gMC->GetStack())->AddPoint(kSILICON);
148 }
149
150 return kTRUE;
151}
152
154{
155 if (fVerboseLevel)
156 Print("");
157 fPointCollection->Clear();
158 fPosIndex = 0;
159}
160
162{
163 FairRootManager::Instance()->Register("SiliconPoint", "Silicon", fPointCollection, kTRUE);
164}
165
167{
168 if (iColl == 0)
169 return fPointCollection;
170 return NULL;
171}
172
174{
175 Int_t nHits = fPointCollection->GetEntriesFast();
176 cout << "-I- BmnSilicon: " << nHits << " points registered in this event." << endl;
177
178 if (fVerboseLevel > 1) {
179 for (Int_t i = 0; i < nHits; i++) {
180 (*fPointCollection)[i]->Print();
181 }
182 }
183}
184
186{
187 fPointCollection->Clear();
189}
190
192{
193 Int_t nEntries = cl1->GetEntriesFast();
194 cout << "-I- BmnSilicon: " << nEntries << " entries to add." << endl;
195 TClonesArray& clref = *cl2;
196 BmnSiliconPoint* oldpoint = NULL;
197
198 for (Int_t i = 0; i < nEntries; i++) {
199 oldpoint = (BmnSiliconPoint*)cl1->At(i);
200 Int_t index = oldpoint->GetTrackID() + offset;
201 oldpoint->SetTrackID(index);
202 new (clref[fPosIndex]) BmnSiliconPoint(*oldpoint);
203 fPosIndex++;
204 }
205
206 cout << "-I- BmnSilicon: " << cl2->GetEntriesFast() << " merged entries." << endl;
207}
208
210{
211 TString fileName = GetGeometryFileName();
212
213 if (fileName.EndsWith(".root")) {
214 LOG(info) << "Constructing Silicon geometry from ROOT file " << fileName.Data();
215 ConstructRootGeometry();
216 }
217
218 else if (fileName.EndsWith(".gdml"))
219 {
220 LOG(info) << "Constructing Silicon geometry from GDML file " << fileName.Data();
221 ConstructGDMLGeometry(nullptr);
222 }
223
224 else
225 {
226 LOG(fatal) << "Geometry format of Silicon file " << fileName.Data() << " not supported.";
227 }
228}
229
230// ----- ConstructGDMLGeometry ---------------------------------------------
232{
233 TFile* old = gFile;
234 TGDMLParse parser;
235 TGeoVolume* gdmlTop;
236
237 // Before importing GDML
238 Int_t maxInd = gGeoManager->GetListOfMedia()->GetEntries() - 1;
239
240 gdmlTop = parser.GDMLReadFile(GetGeometryFileName());
241
242 // Cheating - reassigning media indices after GDML import (need to fix this in TGDMLParse class!!!)
243 // for (Int_t i=0; i<gGeoManager->GetListOfMedia()->GetEntries(); i++)
244 // gGeoManager->GetListOfMedia()->At(i)->Dump();
245 // After importing GDML
246 Int_t j = gGeoManager->GetListOfMedia()->GetEntries() - 1;
247 Int_t curId;
248 TGeoMedium* m;
249 do {
250 m = (TGeoMedium*)gGeoManager->GetListOfMedia()->At(j);
251 curId = m->GetId();
252 m->SetId(curId + maxInd);
253 j--;
254 } while (curId > 1);
255
256 // LOG(debug) << "====================================================================";
257 // for (Int_t i=0; i<gGeoManager->GetListOfMedia()->GetEntries(); i++)
258 // gGeoManager->GetListOfMedia()->At(i)->Dump();
259
260 Int_t newMaxInd = gGeoManager->GetListOfMedia()->GetEntries() - 1;
261
262 gGeoManager->GetTopVolume()->AddNode(gdmlTop, 1, 0);
263 ExpandNodeForGdml(gGeoManager->GetTopVolume()->GetNode(gGeoManager->GetTopVolume()->GetNdaughters() - 1));
264
265 for (Int_t k = maxInd + 1; k < newMaxInd + 1; k++) {
266 TGeoMedium* medToDel = (TGeoMedium*)(gGeoManager->GetListOfMedia()->At(maxInd + 1));
267 LOG(debug) << " removing media " << medToDel->GetName() << " with id " << medToDel->GetId() << " (k=" << k
268 << ")";
269 gGeoManager->GetListOfMedia()->Remove(medToDel);
270 }
271 gGeoManager->SetAllIndex();
272
273 gFile = old;
274}
275
277{
278 LOG(debug) << "----------------------------------------- ExpandNodeForGdml for node " << node->GetName();
279
280 TGeoVolume* curVol = node->GetVolume();
281
282 LOG(debug) << " volume: " << curVol->GetName();
283
284 if (curVol->IsAssembly()) {
285 LOG(debug) << " skipping volume-assembly";
286 } else {
287 TGeoMedium* curMed = curVol->GetMedium();
288 TGeoMaterial* curMat = curVol->GetMaterial();
289 TGeoMedium* curMedInGeoManager = gGeoManager->GetMedium(curMed->GetName());
290 TGeoMaterial* curMatOfMedInGeoManager = curMedInGeoManager->GetMaterial();
291 TGeoMaterial* curMatInGeoManager = gGeoManager->GetMaterial(curMat->GetName());
292
293 // Current medium and material assigned to the volume from GDML
294 LOG(debug2) << " curMed\t\t\t\t" << curMed << "\t" << curMed->GetName() << "\t" << curMed->GetId();
295 LOG(debug2) << " curMat\t\t\t\t" << curMat << "\t" << curMat->GetName() << "\t" << curMat->GetIndex();
296
297 // Medium and material found in the gGeoManager - either the pre-loaded one or one from GDML
298 LOG(debug2) << " curMedInGeoManager\t\t" << curMedInGeoManager << "\t" << curMedInGeoManager->GetName()
299 << "\t" << curMedInGeoManager->GetId();
300 LOG(debug2) << " curMatOfMedInGeoManager\t\t" << curMatOfMedInGeoManager << "\t"
301 << curMatOfMedInGeoManager->GetName() << "\t" << curMatOfMedInGeoManager->GetIndex();
302 LOG(debug2) << " curMatInGeoManager\t\t" << curMatInGeoManager << "\t" << curMatInGeoManager->GetName()
303 << "\t" << curMatInGeoManager->GetIndex();
304
305 TString matName = curMat->GetName();
306 TString medName = curMed->GetName();
307
308 if (curMed->GetId() != curMedInGeoManager->GetId()) {
309 if (fFixedMedia.find(medName) == fFixedMedia.end()) {
310 LOG(debug) << " Medium needs to be fixed";
311 fFixedMedia[medName] = curMedInGeoManager;
312 Int_t ind = curMat->GetIndex();
313 gGeoManager->RemoveMaterial(ind);
314 LOG(debug) << " removing material " << curMat->GetName() << " with index " << ind;
315 for (Int_t i = ind; i < gGeoManager->GetListOfMaterials()->GetEntries(); i++) {
316 TGeoMaterial* m = (TGeoMaterial*)gGeoManager->GetListOfMaterials()->At(i);
317 m->SetIndex(m->GetIndex() - 1);
318 }
319
320 LOG(debug) << " Medium fixed";
321 } else {
322 LOG(debug) << " Already fixed medium found in the list ";
323 }
324 } else {
325 if (fFixedMedia.find(medName) == fFixedMedia.end()) {
326 LOG(debug) << " There is no correct medium in the memory yet";
327
328 FairGeoLoader* geoLoad = FairGeoLoader::Instance();
329 FairGeoInterface* geoFace = geoLoad->getGeoInterface();
330 FairGeoMedia* geoMediaBase = geoFace->getMedia();
331 FairGeoBuilder* geobuild = geoLoad->getGeoBuilder();
332
333 FairGeoMedium* curMedInGeo = geoMediaBase->getMedium(medName);
334 if (curMedInGeo == 0) {
335 LOG(fatal) << " Media not found in Geo file: " << medName;
336 //! This should not happen.
337 //! This means that somebody uses material in GDML that is not in the media.geo file.
338 //! Most probably this is the sign to the user to check materials' names in the CATIA model.
339 } else {
340 LOG(debug) << " Found media in Geo file" << medName;
341 /*Int_t nmed = */ geobuild->createMedium(curMedInGeo);
342 fFixedMedia[medName] = (TGeoMedium*)gGeoManager->GetListOfMedia()->Last();
343 gGeoManager->RemoveMaterial(curMatOfMedInGeoManager->GetIndex());
344 LOG(debug) << " removing material " << curMatOfMedInGeoManager->GetName() << " with index "
345 << curMatOfMedInGeoManager->GetIndex();
346 for (Int_t i = curMatOfMedInGeoManager->GetIndex();
347 i < gGeoManager->GetListOfMaterials()->GetEntries(); i++)
348 {
349 TGeoMaterial* m = (TGeoMaterial*)gGeoManager->GetListOfMaterials()->At(i);
350 m->SetIndex(m->GetIndex() - 1);
351 }
352 }
353
354 if (curMedInGeo->getSensitivityFlag()) {
355 LOG(debug) << " Adding sensitive " << curVol->GetName();
356 AddSensitiveVolume(curVol);
357 }
358 } else {
359 LOG(debug) << " Already fixed medium found in the list";
360 LOG(debug) << "!!! Sensitivity: " << fFixedMedia[medName]->GetParam(0);
361 if (fFixedMedia[medName]->GetParam(0) == 1) {
362 LOG(debug) << " Adding sensitive " << curVol->GetName();
363 AddSensitiveVolume(curVol);
364 }
365 }
366 }
367
368 curVol->SetMedium(fFixedMedia[medName]);
369 gGeoManager->SetAllIndex();
370
371 // gGeoManager->GetListOfMaterials()->Print();
372 // gGeoManager->GetListOfMedia()->Print();
373 }
374
375 //! Recursevly go down the tree of nodes
376 if (curVol->GetNdaughters() != 0) {
377 TObjArray* NodeChildList = curVol->GetNodes();
378 TGeoNode* curNodeChild;
379 for (Int_t j = 0; j < NodeChildList->GetEntriesFast(); j++) {
380 curNodeChild = (TGeoNode*)NodeChildList->At(j);
381 ExpandNodeForGdml(curNodeChild);
382 }
383 }
384}
385
386//--- Check if Sensitive -------------------------------------------------------
388{
389 TString tsname = name;
390 if (tsname.Contains("Sensor")) {
391 return kTRUE;
392 }
393 return kFALSE;
394}
395//------------------------------------------------------------------------------
396
397//------------------------------------------------------------------------------
399 Int_t detID,
400 TVector3 posIn,
401 TVector3 posOut,
402 TVector3 momIn,
403 TVector3 momOut,
404 Double_t time,
405 Double_t length,
406 Double_t eLoss,
407 Int_t isPrimary,
408 Double_t charge,
409 Int_t pdgId)
410{
411
413 Int_t size = clref.GetEntriesFast();
414 // std::cout << "ELoss: " << eLoss << "\n";
415 return new (clref[size])
416 BmnSiliconPoint(trackID, detID, posIn, posOut, momIn, momOut, time, length, eLoss, isPrimary, charge, pdgId);
417}
int i
Definition P4_F32vec4.h:22
__m128 m
Definition P4_F32vec4.h:27
bool Bool_t
int Int_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
#define gFile
R__EXTERN TGeoManager * gGeoManager
@ kSILICON
void SetStation(Int_t station_num)
void SetModule(Int_t module_num)
Double32_t fELoss
length
Definition BmnSilicon.h:75
virtual Bool_t CheckIfSensitive(std::string name)
Double_t fCharge
is track primary?
Definition BmnSilicon.h:77
virtual void Print(Option_t *) const
void ResetParameters()
Definition BmnSilicon.h:108
virtual void EndOfEvent()
virtual ~BmnSilicon()
virtual void Reset()
BmnSiliconPoint * AddHit(Int_t trackID, Int_t detID, TVector3 posIn, TVector3 posOut, TVector3 momIn, TVector3 momOut, Double_t time, Double_t length, Double_t eLoss, Int_t isPrimary, Double_t charge, Int_t pdgId)
Int_t fVolumeID
track index
Definition BmnSilicon.h:68
virtual void Register()
TVector3 fPosIn
volume id
Definition BmnSilicon.h:69
Int_t fTrackID
Definition BmnSilicon.h:67
Double32_t fLength
time
Definition BmnSilicon.h:74
Int_t fPosIndex
pdg id of particle
Definition BmnSilicon.h:80
Int_t fPdgId
track charge
Definition BmnSilicon.h:78
virtual void ConstructGeometry()
Double32_t fTime
momentum (out)
Definition BmnSilicon.h:73
TVector3 fMomOut
momentum (in)
Definition BmnSilicon.h:72
TClonesArray * fPointCollection
Definition BmnSilicon.h:81
virtual Bool_t ProcessHits(FairVolume *vol=0)
void ExpandNodeForGdml(TGeoNode *node)
std::map< TString, TGeoMedium * > fFixedMedia
Hit collection.
Definition BmnSilicon.h:83
virtual void CopyClones(TClonesArray *cl1, TClonesArray *cl2, Int_t offset)
TVector3 fPosOut
position (in)
Definition BmnSilicon.h:70
Int_t fIsPrimary
energy loss
Definition BmnSilicon.h:76
virtual void ConstructGDMLGeometry(TGeoMatrix *)
virtual TClonesArray * GetCollection(Int_t iColl) const
TVector3 fMomIn
position (out)
Definition BmnSilicon.h:71
CbmStack.h.
Definition CbmStack.h:43
TGeoVolume * GDMLReadFile(const char *filename="test.gdml")
Int_t GetIndex()
Int_t GetId() const
TGeoMaterial * GetMaterial() const
TGeoVolume * GetVolume() const
TGeoMedium * GetMedium() const
TGeoMaterial * GetMaterial() const
Int_t GetNdaughters() const
TObjArray * GetNodes()
virtual void SetMedium(TGeoMedium *medium)
virtual Bool_t IsAssembly() const
Double_t Y() const
Double_t X() const
Double_t Z() const
const char * GetName() const override
Int_t GetEntriesFast() const
TObject * At(Int_t idx) const override
Double_t Charge() const
Int_t GetPdgCode() const
TParticlePDG * GetPDG(Int_t mode=0) const
Bool_t IsPrimary() const
Int_t Atoi() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
const char * Data() const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Double_t Mag() const
void SetMag(Double_t)
Definition json.hpp:4446