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