BmnRoot
Loading...
Searching...
No Matches
CbmSts.cxx
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- CbmSts source file -----
3// ----- Created 26/07/04 by V. Friese -----
4// -------------------------------------------------------------------------
5#include "CbmSts.h"
6
7#include "CbmGeoSts.h"
8#include "CbmGeoStsPar.h"
9#include "CbmStack.h"
10#include "CbmStsDetectorId.h"
11#include "CbmStsPoint.h"
12#include "FairGeoInterface.h"
13#include "FairGeoLoader.h"
14#include "FairGeoMedia.h"
15#include "FairGeoNode.h"
16#include "FairGeoRootBuilder.h"
17#include "FairRootManager.h"
18#include "FairRun.h"
19#include "FairRuntimeDb.h"
20#include "FairVolume.h"
21#include "TClonesArray.h"
22#include "TFile.h"
23#include "TGDMLParse.h"
24#include "TGeoMCGeometry.h"
25#include "TGeoManager.h"
26#include "TGeoVoxelFinder.h"
27#include "TObjArray.h"
28#include "TParticle.h"
29#include "TRegexp.h"
30#include "TVirtualMC.h"
31
32#include <iostream>
33
34using std::cerr;
35using std::cout;
36using std::endl;
37
38// ----- Default constructor -------------------------------------------
40 : FairDetector("STS", kTRUE, kGEM)
41 , fTrackID(0)
42 , fVolumeID(0)
43 , fDetectorId(0)
44 , fPosIn(0., 0., 0.)
45 , fPosOut(0., 0., 0.)
46 , fMomIn(0., 0., 0.)
47 , fMomOut(0., 0., 0.)
48 , fTime(0.)
49 , fLength(0.)
50 , fELoss(0.)
51 , fPosIndex(0)
52 , fStsCollection(NULL)
53{
54 ResetParameters();
55 fStsCollection = new TClonesArray("CbmStsPoint");
56 fPosIndex = 0;
57 // kGeoSaved = kFALSE;
58 // flGeoPar = new TList();
59 // flGeoPar->SetName( GetName());
60 fVerboseLevel = 1;
61}
62// -------------------------------------------------------------------------
63
64// ----- Standard constructor ------------------------------------------
65CbmSts::CbmSts(const char* name, Bool_t active)
66 : FairDetector(name, active, kGEM)
67 , fTrackID(0)
68 , fVolumeID(0)
69 , fDetectorId(0)
70 , fPosIn(0., 0., 0.)
71 , fPosOut(0., 0., 0.)
72 , fMomIn(0., 0., 0.)
73 , fMomOut(0., 0., 0.)
74 , fTime(0.)
75 , fLength(0.)
76 , fELoss(0.)
77 , fPosIndex(0)
78 , fStsCollection(NULL)
79{
80 ResetParameters();
81 fStsCollection = new TClonesArray("CbmStsPoint");
82 fPosIndex = 0;
83 fVerboseLevel = 1;
84}
85// -------------------------------------------------------------------------
86
87// ----- Destructor ----------------------------------------------------
89{
90 if (fStsCollection) {
91 fStsCollection->Delete();
92 delete fStsCollection;
93 }
94}
95// -------------------------------------------------------------------------
96
97// ----- Public method ProcessHits --------------------------------------
98Bool_t CbmSts::ProcessHits(FairVolume* vol)
99{
100
101 // Set parameters at entrance of volume. Reset ELoss.
102 if (gMC->IsTrackEntering()) {
103 fELoss = 0.;
104 fTime = gMC->TrackTime() * 1.0e09;
105 fLength = gMC->TrackLength();
106
107 TLorentzVector PosIn;
108 gMC->TrackPosition(PosIn);
109 fPosIn.SetXYZ(PosIn.X(), PosIn.Y(), PosIn.Z());
110
111 TLorentzVector MomIn;
112 gMC->TrackMomentum(MomIn);
113 fMomIn.SetXYZ(MomIn.X(), MomIn.Y(), MomIn.Z());
114 }
115
116 // Sum energy loss for all steps in the active volume
117 fELoss += gMC->Edep();
118
119 // Set additional parameters at exit of active volume. Create CbmStsPoint.
120 if (gMC->IsTrackExiting() || gMC->IsTrackStop() || gMC->IsTrackDisappeared()) {
121
122 fTrackID = gMC->GetStack()->GetCurrentTrackNumber();
123 fDetectorId = vol->getMCid();
124
125 TLorentzVector PosOut;
126 gMC->TrackPosition(PosOut);
127 fPosOut.SetXYZ(PosOut.X(), PosOut.Y(), PosOut.Z());
128
129 TLorentzVector MomOut;
130 gMC->TrackMomentum(MomOut);
131 fMomOut.SetXYZ(MomOut.X(), MomOut.Y(), MomOut.Z());
132
133 if (fELoss == 0.)
134 return kFALSE;
135
136 // correction step to avoid the seg. violation error due to invalid memory access
137 TVector3 diff_pos = fPosIn - fPosOut;
138
139 if (diff_pos.Mag() < 0.001)
140 return kFALSE; // ignore points produced by tracks with zero length inside the current sens. volume
141 if (fMomOut.Mag() == 0)
142 return kFALSE; // ignore points produced by tracks with zero momentum inside the current sens. volume
143
144 TVector3 corr_step = fMomOut;
145 corr_step.SetMag(0.001); // 10 um
146 TVector3 pos = fPosOut;
147 fPosOut = pos - corr_step;
148 gGeoManager->FindNode(fPosOut[0], fPosOut[1], fPosOut[2]);
149
150 // Determine station and module numbers for the current hit ------------
151 Int_t stationNum = -1; // current station number (default)
152 Int_t moduleNum = -1; // current module number (default)
153
154 TGeoVolume* currentVolume = gGeoManager->GetCurrentVolume();
155 TString currentVolumeName = currentVolume->GetName();
156
157 TRegexp expr = "^Sensor_module[0-9]+_station[0-9]+_GEM$";
158 if (currentVolumeName.Contains(expr)) {
159 TRegexp mod_expr = "module[0-9]+";
160 TRegexp stat_expr = "station[0-9]+";
161
162 moduleNum = TString(TString(currentVolumeName(mod_expr))(TRegexp("[0-9]+"))).Atoi();
163 stationNum = TString(TString(currentVolumeName(stat_expr))(TRegexp("[0-9]+"))).Atoi();
164 }
165
166 // cout << "stationNum = " << stationNum << "\n";
167 // cout << "moduleNum = " << moduleNum << "\n";
168 // cout << "\n";
169
170 if (stationNum == -1 || moduleNum == -1)
171 return kFALSE; // check if the current point has incorrect indices
172 //----------------------------------------------------------------------
173
174 AddHit(fTrackID, fDetectorId, fPosIn, fPosOut, fMomIn, fMomOut, fTime, fLength, fELoss, stationNum, moduleNum);
175
176 // Increment number of StsPoints for this track
177 CbmStack* stack = (CbmStack*)gMC->GetStack();
178 stack->AddPoint(kGEM);
179
180 ResetParameters();
181 }
182
183 return kTRUE;
184}
185
186// ----- Public method EndOfEvent -----------------------------------------
188// ----- Public method EndOfEvent -----------------------------------------
190{
191
192 if (fVerboseLevel)
193 Print("");
194 // fStsCollection->Clear();
195 fStsCollection->Delete();
196
197 ResetParameters();
198}
199// ----------------------------------------------------------------------------
200
201// ----- Public method Register -------------------------------------------
203{
204 FairRootManager::Instance()->Register("StsPoint", GetName(), fStsCollection, kTRUE);
205}
206// ----------------------------------------------------------------------------
207
208// ----- Public method GetCollection --------------------------------------
209TClonesArray* CbmSts::GetCollection(Int_t iColl) const
210{
211 if (iColl == 0)
212 return fStsCollection;
213 else
214 return NULL;
215}
216// ----------------------------------------------------------------------------
217
218// ----- Public method Print ----------------------------------------------
219void CbmSts::Print(Option_t*) const
220{
221 Int_t nHits = fStsCollection->GetEntriesFast();
222 cout << "-I- CbmSts: " << nHits << " points registered in this event." << endl;
223}
224// ----------------------------------------------------------------------------
225
226// ----- Public method Reset ----------------------------------------------
228{
229 // fStsCollection->Clear();
230 fStsCollection->Delete();
231 ResetParameters();
232}
233// ----------------------------------------------------------------------------
234
235// ----- Public method CopyClones -----------------------------------------
236void CbmSts::CopyClones(TClonesArray* cl1, TClonesArray* cl2, Int_t offset)
237{
238 Int_t nEntries = cl1->GetEntriesFast();
239 cout << "-I- CbmSts: " << nEntries << " entries to add." << endl;
240 TClonesArray& clref = *cl2;
241 CbmStsPoint* oldpoint = NULL;
242 for (Int_t i = 0; i < nEntries; i++) {
243 oldpoint = (CbmStsPoint*)cl1->At(i);
244 Int_t index = oldpoint->GetTrackID() + offset;
245 oldpoint->SetTrackID(index);
246 new (clref[fPosIndex]) CbmStsPoint(*oldpoint);
247 fPosIndex++;
248 }
249 cout << " -I- CbmSts: " << cl2->GetEntriesFast() << " merged entries." << endl;
250}
251// ----------------------------------------------------------------------------
252
253// ----- ConstructGeometry --------------------------------------------------
255{
256 TString fileName = GetGeometryFileName();
257 if (fileName.EndsWith(".root")) {
258 LOG(info) << "Constructing STS geometry from ROOT file " << fileName.Data();
259 ConstructRootGeometry();
260 } else if (fileName.EndsWith(".geo")) {
261 LOG(info) << "Constructing STS geometry from ASCII file " << fileName.Data();
263 } else if (fileName.EndsWith(".gdml")) {
264 LOG(info) << "Constructing STS geometry from GDML file " << fileName.Data();
265 ConstructGDMLGeometry(nullptr);
266 } else
267 LOG(fatal) << "Geometry format of STS file " << fileName.Data() << " not supported";
268}
269// ----------------------------------------------------------------------------
270
271// ----- ConstructAsciiGeometry -------------------------------------------
273{
274
275 FairGeoLoader* geoLoad = FairGeoLoader::Instance();
276 FairGeoInterface* geoFace = geoLoad->getGeoInterface();
277 CbmGeoSts* stsGeo = new CbmGeoSts();
278 stsGeo->setGeomFile(GetGeometryFileName());
279 geoFace->addGeoModule(stsGeo);
280
281 Bool_t rc = geoFace->readSet(stsGeo);
282 if (rc)
283 stsGeo->create(geoLoad->getGeoBuilder());
284 TList* volList = stsGeo->getListOfVolumes();
285 // store geo parameter
286 FairRun* fRun = FairRun::Instance();
287 FairRuntimeDb* rtdb = FairRun::Instance()->GetRuntimeDb();
288 CbmGeoStsPar* par = (CbmGeoStsPar*)(rtdb->getContainer("CbmGeoStsPar"));
289 TObjArray* fSensNodes = par->GetGeoSensitiveNodes();
290 TObjArray* fPassNodes = par->GetGeoPassiveNodes();
291
292 TListIter iter(volList);
293 FairGeoNode* node = NULL;
294 FairGeoVolume* aVol = NULL;
295
296 while ((node = (FairGeoNode*)iter.Next())) {
297 aVol = dynamic_cast<FairGeoVolume*>(node);
298 if (node->isSensitive()) {
299 fSensNodes->AddLast(aVol);
300 } else {
301 fPassNodes->AddLast(aVol);
302 }
303 }
304 par->setChanged();
305 par->setInputVersion(fRun->GetRunId(), 1);
306 ProcessNodes(volList);
307}
308// ----------------------------------------------------------------------------
309
310// ----- ConstructGDMLGeometry -------------------------------------------
312{
313 TGDMLParse parser;
314 TGeoVolume* v1 = parser.GDMLReadFile(GetGeometryFileName());
315
316 if (v1 == 0)
317 LOG(fatal)
318 << "\033[5m\033[31mFairModule::ConstructGDMLGeometry(): could construct geometry from GDML File!! \033[0m"
319 << GetGeometryFileName().Data();
320
321 TGeoNode* n = v1->GetNode(0);
322
323 gGeoManager->cd();
324
325 /*add gdml volume to the simulation TGeoManager*/
326 gGeoManager->AddVolume(v1);
327
328 /*force rebuilding of voxels */
329 TGeoVoxelFinder* voxels = v1->GetVoxels();
330 if (voxels)
331 voxels->SetNeedRebuild();
332
336 TGeoMatrix* M = n->GetMatrix();
337 M->SetDefaultName();
338
339 /*Now we can remove the matrix so that the new geomanager will rebuild it properly*/
340 gGeoManager->GetListOfMatrices()->Remove(M);
341 TGeoHMatrix* global = gGeoManager->GetHMatrix();
342 gGeoManager->GetListOfMatrices()->Remove(global); // Remove the Identity matrix
343
345 TGeoVolume* Cave = gGeoManager->GetTopVolume();
346 Cave->AddNode(v1, 0, 0);
347
348 /*
349 // correction from O. Merle: in case of a TGeoVolume (v1) set the material properly
350 // Assign medium to the the volume v, this has to be done in all cases:
351 // case 1: For CAD converted volumes they have no mediums (only names)
352 // case 2: TGeoVolumes, we need to be sure that the material is defined in this session
353 FairGeoMedia* Media = FairGeoLoader::Instance()->getGeoInterface()->getMedia();
354 FairGeoBuilder* geobuild = FairGeoLoader::Instance()->getGeoBuilder();
355 TGeoMedium* med1 = v1->GetMedium();
356 if (med1)
357 {
358 TGeoMaterial* mat1 = v1->GetMaterial();
359 TGeoMaterial* newMat = gGeoManager->GetMaterial(mat1->GetName());
360 if (newMat == 0)
361 {
362 // The Material is not defined in the TGeoManager, we try to create one if we have enough information about it
363 FairGeoMedium* FairMedium = Media->getMedium(mat1->GetName());
364 if (!FairMedium)
365 {
366 fLogger->Fatal(MESSAGE_ORIGIN,"Material %s is not defined in ASCII file nor in Root file we Stop creating
367 geometry", mat1->GetName());
368 // FairMedium=new FairGeoMedium(mat1->GetName());
369 // Media->addMedium(FairMedium);
370 }
371
372 Int_t nmed = geobuild->createMedium(FairMedium);
373 v1->SetMedium(gGeoManager->GetMedium(nmed));
374 gGeoManager->SetAllIndex();
375 }
376 else
377 {
378 // Material is already available in the TGeoManager and we can set it
379 TGeoMedium* med2 = gGeoManager->GetMedium(mat1->GetName());
380 v1->SetMedium(med2);
381 }
382 }
383 else
384 {
385 if (strcmp(v1->ClassName(),"TGeoVolumeAssembly") != 0)
386 {
387 //[R.K.-3.3.08] // When there is NO material defined, set it to avoid conflicts in Geant
388 fLogger->Fatal(MESSAGE_ORIGIN,"The volume %s Has no medium information and not an Assembly so we have to
389 quit", v1->GetName());
390 }
391 }*/
392
393 // now go through the herachy and set the materials properly, this is important becase the CAD converter
394 // produce TGeoVolumes with materials that have only names and no properties
395 ExpandNode(n);
396
397 // Define output ROOT file with sts geometry
398 TFile* outfile = new TFile("sts.root", "RECREATE");
399 v1->Write();
400 outfile->Close();
401}
402// ----------------------------------------------------------------------------
403
404// ----- CheckIfSensitive -------------------------------------------------
405Bool_t CbmSts::CheckIfSensitive(std::string name)
406{
407
408 TString volName = name;
409 if (volName.Contains("Sensor"))
410 return kTRUE;
411 return kFALSE;
412}
413// ----------------------------------------------------------------------------
414
415// ----- Private method AddHit --------------------------------------------
416CbmStsPoint* CbmSts::AddHit(Int_t trackID,
417 Int_t detID,
418 TVector3 posIn,
419 TVector3 posOut,
420 TVector3 momIn,
421 TVector3 momOut,
422 Double_t time,
423 Double_t length,
424 Double_t eLoss,
425 Int_t stationNum,
426 Int_t moduleNum)
427{
428 TClonesArray& clref = *fStsCollection;
429 Int_t size = clref.GetEntriesFast();
430 if (fVerboseLevel > 1)
431 cout << "-I- CbmSts: Adding Point at (" << posIn.X() << ", " << posIn.Y() << ", " << posIn.Z()
432 << ") cm, detector " << detID << ", track " << trackID << ", energy loss " << eLoss * 1e06 << " keV"
433 << endl;
434
435 // return new(clref[size]) CbmStsPoint(trackID, detID, posIn, posOut,
436 // momIn, momOut, time, length, eLoss);
437 new (clref[size]) CbmStsPoint(trackID, detID, posIn, posOut, momIn, momOut, time, length, eLoss);
438
439 CbmStsPoint* hit = (CbmStsPoint*)clref.At(clref.GetEntriesFast() - 1);
440 hit->SetStation(stationNum);
441 hit->SetModule(moduleNum);
442 return hit;
443}
444// ----------------------------------------------------------------------------
int i
Definition P4_F32vec4.h:22
@ kGEM
TObjArray * GetGeoPassiveNodes()
TObjArray * GetGeoSensitiveNodes()
void AddPoint(DetectorId iDet)
Definition CbmStack.cxx:351
void SetStation(Int_t station)
void SetModule(Int_t module)
virtual void SetTrackID(Int_t id)
Definition CbmStsPoint.h:97
virtual void Reset()
Definition CbmSts.cxx:227
CbmSts()
Definition CbmSts.cxx:39
virtual void ConstructAsciiGeometry()
Definition CbmSts.cxx:272
virtual TClonesArray * GetCollection(Int_t iColl) const
Definition CbmSts.cxx:209
virtual void Print(Option_t *) const
Definition CbmSts.cxx:219
virtual Bool_t ProcessHits(FairVolume *vol=0)
Definition CbmSts.cxx:98
virtual void ConstructGDMLGeometry(TGeoMatrix *)
Definition CbmSts.cxx:311
virtual void Register()
Definition CbmSts.cxx:202
virtual void ConstructGeometry()
Definition CbmSts.cxx:254
virtual void BeginEvent()
Definition CbmSts.cxx:187
virtual Bool_t CheckIfSensitive(std::string name)
Definition CbmSts.cxx:405
virtual ~CbmSts()
Definition CbmSts.cxx:88
virtual void EndOfEvent()
Definition CbmSts.cxx:189
virtual void CopyClones(TClonesArray *cl1, TClonesArray *cl2, Int_t offset)
Definition CbmSts.cxx:236