BmnRoot
Loading...
Searching...
No Matches
BmnVSP.cxx
Go to the documentation of this file.
1#include "BmnVSP.h"
2
3#include "BmnVSPPoint.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 "TParticle.h"
13#include "TParticlePDG.h"
14#include "TRegexp.h"
15#include "TVirtualMC.h"
16
17#include <iostream>
18
19using namespace std;
20
22 : FairDetector("VSP", kTRUE)
23{
24 fPointCollection = new TClonesArray("BmnVSPPoint");
25 fPosIndex = 0;
26 fVerboseLevel = 1;
27 ResetParameters();
28}
29
30BmnVSP::BmnVSP(const char* name, Bool_t active)
31 : FairDetector(name, active)
32{
33 fPointCollection = new TClonesArray("BmnVSPPoint");
34 fPosIndex = 0;
35 fVerboseLevel = 1;
36 ResetParameters();
37}
38
40{
41 if (fPointCollection) {
42 fPointCollection->Delete();
43 delete fPointCollection;
44 }
45}
46
47//------------------------------------------------------------------------------
48
49Bool_t BmnVSP::ProcessHits(FairVolume* vol)
50{
51
52 // Set parameters at entrance of volume. Reset ELoss.
53 if (gMC->IsTrackEntering()) {
54
55 ResetParameters();
56 fELoss = 0.;
57 fTime = gMC->TrackTime() * 1.0e09;
58 fLength = gMC->TrackLength();
59 fIsPrimary = 0;
60 fCharge = -1;
61 fPdgId = 0;
62
63 TLorentzVector PosIn;
64 gMC->TrackPosition(PosIn);
65 fPosIn.SetXYZ(PosIn.X(), PosIn.Y(), PosIn.Z());
66
67 TLorentzVector MomIn;
68 gMC->TrackMomentum(MomIn);
69 fMomIn.SetXYZ(MomIn.X(), MomIn.Y(), MomIn.Z());
70
71 TParticle* part = 0;
72 part = gMC->GetStack()->GetCurrentTrack();
73 if (part) {
74 fIsPrimary = (Int_t)part->IsPrimary();
75 fCharge = (Int_t)part->GetPDG()->Charge();
76 fPdgId = (Int_t)part->GetPdgCode();
77 }
78
79 fVolumeID = vol->getMCid();
80 fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
81 }
82
83 // Sum energy loss for all steps in the active volume
84 fELoss += gMC->Edep();
85
86 // Create BmnVSPPoint at EXIT of active volume;
87 if ((gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()) && fELoss > 0) {
88
89 TLorentzVector PosOut;
90 gMC->TrackPosition(PosOut);
91 fPosOut.SetXYZ(PosOut.X(), PosOut.Y(), PosOut.Z());
92
93 TLorentzVector MomOut;
94 gMC->TrackMomentum(MomOut);
95 fMomOut.SetXYZ(MomOut.X(), MomOut.Y(), MomOut.Z());
96
97 // correction step to avoid the seg. violation error due to invalid memory access
98 TVector3 diff_pos = fPosIn - fPosOut;
99
100 if (diff_pos.Mag() < 0.001)
101 return kFALSE; // ignore points produced by tracks with zero length inside the current sens. volume
102 if (fMomOut.Mag() == 0)
103 return kFALSE; // ignore points produced by tracks with zero momentum inside the current sens. volume
104
105 TVector3 corr_step = fMomOut;
106 corr_step.SetMag(0.001); // 10 um
107 TVector3 pos = fPosOut;
108 fPosOut = pos - corr_step;
109 gGeoManager->FindNode(fPosOut[0], fPosOut[1], fPosOut[2]);
110
111 if (gGeoManager->GetCurrentNode()->GetMotherVolume() == 0)
112 return kFALSE; // check if the current node has its mother vol.
113
114 // Determine station and module numbers for the current hit ------------
115 Int_t stationNum = -1; // current station number (default)
116 Int_t moduleNum = -1; // current module number (default)
117
118 TGeoVolume* motherVolume = gGeoManager->GetCurrentNode()->GetMotherVolume();
119 TString moduleVolumeName = motherVolume->GetName();
120
121 TRegexp expr = "^moduleV_module[0-9]+_station[0-9]+_VSP$";
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
132 BmnVSPPoint* p = AddHit(fTrackID, fVolumeID, fPosIn, fPosOut, fMomIn, fMomOut, fTime, fLength, fELoss,
133 fIsPrimary, fCharge, fPdgId);
134
135 // if (stationNum == -1) {
136 // cout << "BmnVSP.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(kVSP);
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("VSPPoint", "VSP", fPointCollection, kTRUE);
164}
165
166TClonesArray* BmnVSP::GetCollection(Int_t iColl) const
167{
168 if (iColl == 0)
169 return fPointCollection;
170 return NULL;
171}
172
173void BmnVSP::Print(Option_t*) const
174{
175 Int_t nHits = fPointCollection->GetEntriesFast();
176 cout << "-I- BmnVSP: " << 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();
188 ResetParameters();
189}
190
191void BmnVSP::CopyClones(TClonesArray* cl1, TClonesArray* cl2, Int_t offset)
192{
193 Int_t nEntries = cl1->GetEntriesFast();
194 cout << "-I- BmnVSP: " << nEntries << " entries to add." << endl;
195 TClonesArray& clref = *cl2;
196 BmnVSPPoint* oldpoint = NULL;
197
198 for (Int_t i = 0; i < nEntries; i++) {
199 oldpoint = (BmnVSPPoint*)cl1->At(i);
200 Int_t index = oldpoint->GetTrackID() + offset;
201 oldpoint->SetTrackID(index);
202 new (clref[fPosIndex]) BmnVSPPoint(*oldpoint);
203 fPosIndex++;
204 }
205
206 cout << "-I- BmnVSP: " << cl2->GetEntriesFast() << " merged entries." << endl;
207}
208
210{
211 TString fileName = GetGeometryFileName();
212
213 if (fileName.EndsWith(".root")) {
214 LOG(info) << "Constructing VSP geometry from ROOT file " << fileName.Data();
215 ConstructRootGeometry();
216 }
217
218 else if (fileName.EndsWith(".gdml"))
219 {
220 LOG(info) << "Constructing VSP geometry from GDML file " << fileName.Data();
221 ConstructGDMLGeometry(nullptr);
222 }
223
224 else
225 {
226 LOG(fatal) << "Geometry format of VSP 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
276void BmnVSP::ExpandNodeForGdml(TGeoNode* node)
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;
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
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 -------------------------------------------------------
387Bool_t BmnVSP::CheckIfSensitive(std::string name)
388{
389 TString tsname = name;
390 if (tsname.Contains("sensor")) {
391 return kTRUE;
392 }
393 return kFALSE;
394}
395//------------------------------------------------------------------------------
396
397//------------------------------------------------------------------------------
398BmnVSPPoint* BmnVSP::AddHit(Int_t trackID,
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
412 TClonesArray& clref = *fPointCollection;
413 Int_t size = clref.GetEntriesFast();
414 // std::cout << "ELoss: " << eLoss << "\n";
415 return new (clref[size])
416 BmnVSPPoint(trackID, detID, posIn, posOut, momIn, momOut, time, length, eLoss, isPrimary, charge, pdgId);
417}
418//------------------------------------------------------------------------------
int i
Definition P4_F32vec4.h:22
__m128 m
Definition P4_F32vec4.h:27
@ kVSP
void SetModule(Int_t module_num)
Definition BmnVSPPoint.h:53
void SetStation(Int_t station_num)
Definition BmnVSPPoint.h:52
virtual TClonesArray * GetCollection(Int_t iColl) const
Definition BmnVSP.cxx:166
virtual void Print(Option_t *) const
Definition BmnVSP.cxx:173
virtual void Reset()
Definition BmnVSP.cxx:185
BmnVSP()
Definition BmnVSP.cxx:21
virtual void CopyClones(TClonesArray *cl1, TClonesArray *cl2, Int_t offset)
Definition BmnVSP.cxx:191
void ExpandNodeForGdml(TGeoNode *node)
Definition BmnVSP.cxx:276
virtual void ConstructGDMLGeometry(TGeoMatrix *)
Definition BmnVSP.cxx:231
virtual void EndOfEvent()
Definition BmnVSP.cxx:153
virtual Bool_t CheckIfSensitive(std::string name)
Definition BmnVSP.cxx:387
virtual ~BmnVSP()
Definition BmnVSP.cxx:39
virtual Bool_t ProcessHits(FairVolume *vol=0)
Definition BmnVSP.cxx:49
virtual void Register()
Definition BmnVSP.cxx:161
virtual void ConstructGeometry()
Definition BmnVSP.cxx:209
STL namespace.