FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairTutorialDet4.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 #include "FairTutorialDet4.h"
9 
10 #include "FairDetectorList.h" // for DetectorId::kTutDet
11 #include "FairLogger.h" // for FairLogger, etc
12 #include "FairModule.h" // for FairModule
13 #include "FairRootManager.h" // for FairRootManager
14 #include "FairRun.h" // for FairRun
15 #include "FairRuntimeDb.h" // for FairRuntimeDb
16 #include "FairStack.h" // for FairStack
17 #include "FairTutorialDet4Geo.h" // for FairTutorialDet4Geo
18 #include "FairTutorialDet4GeoHandler.h" // for FairTutorialDet4GeoHandler
19 #include "FairTutorialDet4GeoPar.h" // for FairTutorialDet4GeoPar
21 #include "FairTutorialDet4Point.h" // for FairTutorialDet4Point
22 
23 #include <TGeoMatrix.h> // for TGeoHMatrix, TGeoCombiTrans, etc
24 #include <TString.h> // for TString, operator<<, Form
25 #include <TVirtualMC.h> // for TVirtualMC
26 #include <TVirtualMCStack.h> // for TVirtualMCStack
27 
28 FairTutorialDet4Geo* FairTutorialDet4::fgGeo;
29 
31  : FairDetector("TutorialDet", kTRUE, kTutDet)
32  , fTrackID(-1)
33  , fVolumeID(-1)
34  , fPos()
35  , fMom()
36  , fTime(-1.)
37  , fLength(-1.)
38  , fELoss(-1)
39  , fFairTutorialDet4PointCollection(new TClonesArray("FairTutorialDet4Point"))
40  , fGeoHandler(new FairTutorialDet4GeoHandler())
41  , fMisalignPar(nullptr)
42  , fNrOfDetectors(-1)
43  , fShiftX()
44  , fShiftY()
45  , fShiftZ()
46  , fRotX()
47  , fRotY()
48  , fRotZ()
49  , fModifyGeometry(kFALSE)
50  , fGlobalCoordinates(kFALSE)
51 {}
52 
53 FairTutorialDet4::FairTutorialDet4(const char* name, Bool_t active)
54  : FairDetector(name, active, kTutDet)
55  , fTrackID(-1)
56  , fVolumeID(-1)
57  , fPos()
58  , fMom()
59  , fTime(-1.)
60  , fLength(-1.)
61  , fELoss(-1)
62  , fFairTutorialDet4PointCollection(new TClonesArray("FairTutorialDet4Point"))
63  , fGeoHandler(new FairTutorialDet4GeoHandler())
64  , fMisalignPar(nullptr)
65  , fNrOfDetectors(-1)
66  , fShiftX()
67  , fShiftY()
68  , fShiftZ()
69  , fRotX()
70  , fRotY()
71  , fRotZ()
72  , fModifyGeometry(kFALSE)
73  , fGlobalCoordinates(kFALSE)
74 {}
75 
77  : FairDetector(rhs)
78  , fTrackID(-1)
79  , fVolumeID(-1)
80  , fPos()
81  , fMom()
82  , fTime(-1.)
83  , fLength(-1.)
84  , fELoss(-1)
85  , fFairTutorialDet4PointCollection(new TClonesArray("FairTutorialDet4Point"))
86  , fGeoHandler(new FairTutorialDet4GeoHandler())
87  , fMisalignPar(nullptr)
88  , fNrOfDetectors(-1)
89  , fShiftX()
90  , fShiftY()
91  , fShiftZ()
92  , fRotX()
93  , fRotY()
94  , fRotZ()
95  , fModifyGeometry(kFALSE)
96  , fGlobalCoordinates(kFALSE)
97 {}
98 
100 {
101  LOG(debug4) << "Entering Destructor of FairTutorialDet4";
102  if (fFairTutorialDet4PointCollection) {
103  fFairTutorialDet4PointCollection->Delete();
104  delete fFairTutorialDet4PointCollection;
105  }
106  LOG(debug4) << "Leaving Destructor of FairTutorialDet4";
107 }
108 
110 {
111 
112  LOG(info) << "Set tutdet missallign parameters";
113  // Get Base Container
114  FairRun* sim = FairRun::Instance();
115  LOG_IF(FATAL, !sim) << "No run object";
116  FairRuntimeDb* rtdb = sim->GetRuntimeDb();
117  LOG_IF(FATAL, !rtdb) << "No runtime database";
118 
119  fMisalignPar = static_cast<FairTutorialDet4MisalignPar*>(rtdb->getContainer("FairTutorialDet4MissallignPar"));
120 }
121 
123 {
126  FairTutorialDet4GeoPar* par = static_cast<FairTutorialDet4GeoPar*>(rtdb->getContainer("FairTutorialDet4GeoPar"));
127 
128  if (fModifyGeometry) {
129  if (fGlobalCoordinates) {
130  LOG(warn) << "Storing MCPoints in global coordinates and modifying the geometry was set.";
131  LOG(warn) << "When modifying the geometry is set the MCPoints has to be stored in local coordinates.";
132  LOG(warn) << "Store MCPoints in local coordinate system.";
133  fGlobalCoordinates = kFALSE;
134  }
135  }
136 
137  par->SetGlobalCoordinates(fGlobalCoordinates);
138  par->setChanged();
139  par->setInputVersion(FairRun::Instance()->GetRunId(), 1);
140 
141  Bool_t isSimulation = kTRUE;
142  fGeoHandler->Init(isSimulation);
143 }
144 
146 {
147  LOG(info) << "Initialize tutdet missallign parameters";
148  fNrOfDetectors = fMisalignPar->GetNrOfDetectors();
149  fShiftX = fMisalignPar->GetShiftX();
150  fShiftY = fMisalignPar->GetShiftY();
151  fShiftZ = fMisalignPar->GetShiftZ();
152  fRotX = fMisalignPar->GetRotX();
153  fRotY = fMisalignPar->GetRotY();
154  fRotZ = fMisalignPar->GetRotZ();
155 }
156 
158 {
161  // Set parameters at entrance of volume. Reset ELoss.
162  if (TVirtualMC::GetMC()->IsTrackEntering()) {
163  fELoss = 0.;
164  fTime = TVirtualMC::GetMC()->TrackTime() * 1.0e09;
165  fLength = TVirtualMC::GetMC()->TrackLength();
166  TVirtualMC::GetMC()->TrackPosition(fPos);
167  TVirtualMC::GetMC()->TrackMomentum(fMom);
168  }
169 
170  // Sum energy loss for all steps in the active volume
171  fELoss += TVirtualMC::GetMC()->Edep();
172 
173  // Create FairTutorialDet4Point at exit of active volume
174  if (TVirtualMC::GetMC()->IsTrackExiting() || TVirtualMC::GetMC()->IsTrackStop()
175  || TVirtualMC::GetMC()->IsTrackDisappeared()) {
176  fTrackID = TVirtualMC::GetMC()->GetStack()->GetCurrentTrackNumber();
177  // fVolumeID = vol->getMCid();
178  fVolumeID = fGeoHandler->GetUniqueDetectorId();
179  if (fELoss == 0.) {
180  return kFALSE;
181  }
182 
183  if (!fGlobalCoordinates) {
184  // Save positions in local coordinate system, so transform the
185  // global coordinates into local ones.
186  Double_t master[3] = {fPos.X(), fPos.Y(), fPos.Z()};
187  Double_t local[3];
188 
189  TVirtualMC::GetMC()->Gmtod(master, local, 1);
190  AddHit(fTrackID,
191  fVolumeID,
192  TVector3(local[0], local[1], local[2]),
193  TVector3(fMom.Px(), fMom.Py(), fMom.Pz()),
194  fTime,
195  fLength,
196  fELoss);
197  } else {
198  AddHit(fTrackID,
199  fVolumeID,
200  TVector3(fPos.X(), fPos.Y(), fPos.Z()),
201  TVector3(fMom.Px(), fMom.Py(), fMom.Pz()),
202  fTime,
203  fLength,
204  fELoss);
205  }
206 
207  // Increment number of tutorial det points in TParticle
208  FairStack* stack = static_cast<FairStack*>(TVirtualMC::GetMC()->GetStack());
209  stack->AddPoint(kTutDet);
210  }
211 
212  return kTRUE;
213 }
214 
215 void FairTutorialDet4::EndOfEvent() { fFairTutorialDet4PointCollection->Clear(); }
216 
218 {
225  FairRootManager::Instance()->Register("TutorialDetPoint", "TutorialDet", fFairTutorialDet4PointCollection, kTRUE);
226 }
227 
228 TClonesArray* FairTutorialDet4::GetCollection(Int_t iColl) const
229 {
230  if (iColl == 0) {
231  return fFairTutorialDet4PointCollection;
232  } else {
233  return nullptr;
234  }
235 }
236 
237 void FairTutorialDet4::Reset() { fFairTutorialDet4PointCollection->Clear(); }
238 
240 {
241  TString fileName = GetGeometryFileName();
242  if (fileName.EndsWith(".geo")) {
243  LOG(info) << "Constructing Tutorial4 geometry from ASCII file " << fileName;
244  ConstructASCIIGeometry();
245  } else if (fileName.EndsWith(".root")) {
246  LOG(info) << "Constructing Tutorial4 geometry from ROOT file " << fileName;
248  } else {
249  LOG(fatal) << "Geometry format not supported.";
250  }
251 }
252 
253 Bool_t FairTutorialDet4::IsSensitive(const std::string& name)
254 {
255  if (name.find("tut4") != std::string::npos) {
256  return kTRUE;
257  }
258  return kFALSE;
259 }
260 
261 void FairTutorialDet4::ConstructASCIIGeometry()
262 {
267  FairModule::ConstructASCIIGeometry<FairTutorialDet4Geo, FairTutorialDet4GeoPar>(fgGeo, "FairTutorialDet4GeoPar");
268 }
269 
270 std::map<std::string, TGeoHMatrix> FairTutorialDet4::getMisalignmentMatrices()
271 {
272  TString volPath;
273  TString volStr = "/cave_1/tutorial4_0/tut4_det_";
274 
275  std::map<std::string, TGeoHMatrix> matrices;
276 
277  for (Int_t iDet = 0; iDet < fNrOfDetectors; ++iDet) {
278  LOG(debug) << "Create Matrix for detector nr " << iDet;
279  volPath = volStr;
280  volPath += iDet;
281 
282  LOG(debug) << "Path: " << volPath;
283 
284  // we have to express the displacements as regards the old local RS (non misaligned BTOF)
285  Double_t dx = fShiftX[iDet];
286  Double_t dy = fShiftY[iDet];
287  Double_t dz = fShiftZ[iDet];
288  Double_t dphi = fRotX[iDet];
289  Double_t dtheta = fRotY[iDet];
290  Double_t dpsi = fRotZ[iDet];
291 
292  TGeoRotation* rrot = new TGeoRotation("rot", dphi, dtheta, dpsi);
293  TGeoCombiTrans localdelta = *(new TGeoCombiTrans(dx, dy, dz, rrot));
294  TGeoHMatrix ldm = TGeoHMatrix(localdelta);
295 
296  std::string thisPath(volPath.Data());
297  matrices[thisPath] = ldm;
298  }
299 
300  LOG(info) << fNrOfDetectors << " misalignment matrices created!";
301 
302  return matrices;
303 }
304 
306 {
307  if (fModifyGeometry) {
308  TString volPath;
309  TString volStr = "/cave_1/tutorial4_0/tut4_det_";
310 
311  std::map<std::string, TGeoHMatrix> matrices;
312 
313  for (Int_t iDet = 0; iDet < fNrOfDetectors; ++iDet) {
314  LOG(debug) << "Create Matrix for detector nr " << iDet;
315  volPath = volStr;
316  volPath += iDet;
317 
318  LOG(debug) << "Path: " << volPath;
319 
320  // we have to express the displacements as regards the old local RS (non misaligned BTOF)
321  Double_t dx = fShiftX[iDet];
322  Double_t dy = fShiftY[iDet];
323  Double_t dz = fShiftZ[iDet];
324  Double_t dphi = fRotX[iDet];
325  Double_t dtheta = fRotY[iDet];
326  Double_t dpsi = fRotZ[iDet];
327 
328  TGeoRotation* rrot = new TGeoRotation("rot", dphi, dtheta, dpsi);
329  TGeoCombiTrans localdelta = *(new TGeoCombiTrans(dx, dy, dz, rrot));
330  TGeoHMatrix ldm = TGeoHMatrix(localdelta);
331 
332  std::string thisPath(volPath.Data());
333  matrices[thisPath] = ldm;
334  }
335 
336  LOG(info) << fNrOfDetectors << " misalignment matrices created!";
337 
338  FairRun* run = FairRun::Instance();
339  run->AddAlignmentMatrices(matrices);
340  }
341 }
342 
344  Int_t detID,
345  TVector3 pos,
346  TVector3 mom,
347  Double_t time,
348  Double_t length,
349  Double_t eLoss)
350 {
351  TClonesArray& clref = *fFairTutorialDet4PointCollection;
352  Int_t size = clref.GetEntriesFast();
353  return new (clref[size]) FairTutorialDet4Point(trackID, detID, pos, mom, time, length, eLoss);
354 }
355 
357 
std::map< std::string, TGeoHMatrix > getMisalignmentMatrices()
virtual void see Tutorial4 for examples LOG(warn)<< "This function is deprecated. Use FairAlignmentHandler instead
list of container factories
Definition: FairRuntimeDb.h:24
void SetGlobalCoordinates(Bool_t globalCoordinates)
virtual void Register()
virtual void InitParContainers()
static FairRun * Instance()
Definition: FairRun.cxx:31
virtual ~FairTutorialDet4()
void setChanged(Bool_t flag=kTRUE)
Definition: FairParSet.h:72
static FairRootManager * Instance()
ClassImp(FairEventBuilder)
virtual void RegisterAlignmentMatrices()
virtual FairModule * CloneModule() const
FairParSet * getContainer(const Text_t *)
virtual void Initialize()
virtual void EndOfEvent()
virtual void Initialize()
virtual void Reset()
FairMQExParamsParOne * par
void AddAlignmentMatrices(const std::map< std::string, TGeoHMatrix > &alignmentMatrices, bool invertMatrices=false)
Definition: FairRun.cxx:183
virtual Bool_t ProcessHits(FairVolume *v=0)
void setInputVersion(Int_t v=-1, Int_t i=0)
Definition: FairParSet.h:51
virtual void ConstructRootGeometry(TGeoMatrix *shiftM=nullptr)
Definition: FairModule.cxx:327
FairTutorialDet4Point * AddHit(Int_t trackID, Int_t detID, TVector3 pos, TVector3 mom, Double_t time, Double_t length, Double_t eLoss)
FairRuntimeDb * GetRuntimeDb(void)
Definition: FairRun.h:80
void Register(const char *name, const char *Foldername, TNamed *obj, Bool_t toFile)
virtual TString GetGeometryFileName()
Definition: FairModule.h:60
virtual TClonesArray * GetCollection(Int_t iColl) const
void AddPoint(DetectorId iDet)
Definition: FairStack.cxx:344
Int_t Init(Bool_t isSimulation=kFALSE)
virtual Bool_t IsSensitive(const std::string &name)