FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairTutorialDet4GeoHandler.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 // -------------------------------------------------------------------------
9 // ----- CbmTrdGeoHandler source file -----
10 // ----- Created 13/08/10 by F. Uhlig -----
11 // -------------------------------------------------------------------------
12 
14 
15 #include "FairLogger.h" // for FairLogger, etc
16 
17 #include <TGeoBBox.h> // for TGeoBBox
18 #include <TGeoManager.h> // for TGeoManager, gGeoManager
19 #include <TGeoNode.h> // for TGeoNode
20 #include <TGeoVolume.h> // for TGeoVolume
21 #include <TVirtualMC.h> // for TVirtualMC
22 #include <cstdio> // for printf
23 #include <cstring> // for strlen, strncpy
24 
26  : TObject()
27  , fIsSimulation(kFALSE)
28  , fLastUsedDetectorID(0)
29  , fGeoPathHash(0)
30  , fCurrentVolume(nullptr)
31  , fVolumeShape(nullptr)
32  , fGlobal()
33  , fGlobalMatrix(nullptr)
34 {}
35 
36 Int_t FairTutorialDet4GeoHandler::Init(Bool_t isSimulation)
37 {
38  // Int_t geoVersion = CheckGeometryVersion();
39 
40  fIsSimulation = isSimulation;
41 
42  return 1;
43 }
44 
45 void FairTutorialDet4GeoHandler::LocalToGlobal(Double_t* local, Double_t* global, Int_t detID)
46 {
47  TString path = ConstructFullPathFromDetID(detID);
48  NavigateTo(path);
49  gGeoManager->LocalToMaster(local, global);
50 }
51 
52 TString FairTutorialDet4GeoHandler::ConstructFullPathFromDetID(Int_t detID)
53 {
54  TString volStr = "/cave_1/tutorial4_0/tut4_det_";
55  TString volPath = volStr;
56  volPath += detID;
57  return volPath;
58 }
59 
61 {
62  if (fGeoPathHash != volName.Hash()) {
63  NavigateTo(volName);
64  }
65  return GetUniqueDetectorId();
66 }
67 
69 {
70 
71  Int_t detectorNr = 0;
72 
73  CurrentVolOffID(0, detectorNr);
74 
75  return detectorNr;
76 }
77 
78 Int_t FairTutorialDet4GeoHandler::VolIdGeo(const char* name) const
79 {
80  //
81  // Return the unique numeric identifier for volume name
82  //
83 
84  Int_t uid = gGeoManager->GetUID(name);
85  if (uid < 0) {
86  printf("VolId: Volume %s not found\n", name);
87  return 0;
88  }
89  return uid;
90 }
91 
92 Int_t FairTutorialDet4GeoHandler::VolId(const Text_t* name) const
93 {
94  if (fIsSimulation) {
95  return TVirtualMC::GetMC()->VolId(name);
96  } else {
97  //
98  // Return the unique numeric identifier for volume name
99  //
100  std::string volname(name);
101  while (!volname.empty() && volname.back() == ' ') {
102  volname.pop_back();
103  }
104  return VolIdGeo(volname.c_str());
105  }
106 }
107 
109 {
110  if (fIsSimulation) {
111  return TVirtualMC::GetMC()->CurrentVolID(copy);
112  } else {
113  //
114  // Returns the current volume ID and copy number
115  //
116  if (gGeoManager->IsOutside()) {
117  return 0;
118  }
119  TGeoNode* node = gGeoManager->GetCurrentNode();
120  copy = node->GetNumber();
121  Int_t id = node->GetVolume()->GetNumber();
122  return id;
123  }
124 }
125 
126 Int_t FairTutorialDet4GeoHandler::CurrentVolOffID(Int_t off, Int_t& copy) const
127 {
128  if (fIsSimulation) {
129  return TVirtualMC::GetMC()->CurrentVolOffID(off, copy);
130  } else {
131  //
132  // Return the current volume "off" upward in the geometrical tree
133  // ID and copy number
134  //
135  if (off < 0 || off > gGeoManager->GetLevel()) {
136  return 0;
137  }
138  if (off == 0) {
139  return CurrentVolID(copy);
140  }
141  TGeoNode* node = gGeoManager->GetMother(off);
142  if (!node) {
143  return 0;
144  }
145  copy = node->GetNumber();
146  return node->GetVolume()->GetNumber();
147  }
148 }
149 
151 {
152  if (fIsSimulation) {
153  return TVirtualMC::GetMC()->CurrentVolName();
154  } else {
155  //
156  // Returns the current volume name
157  //
158  if (gGeoManager->IsOutside()) {
159  return gGeoManager->GetTopVolume()->GetName();
160  }
161  return gGeoManager->GetCurrentVolume()->GetName();
162  }
163 }
164 
166 {
167  if (fIsSimulation) {
168  return TVirtualMC::GetMC()->CurrentVolOffName(off);
169  } else {
170  //
171  // Return the current volume "off" upward in the geometrical tree
172  // ID, name and copy number
173  // if name=0 no name is returned
174  //
175  if (off < 0 || off > gGeoManager->GetLevel()) {
176  return 0;
177  }
178  if (off == 0) {
179  return CurrentVolName();
180  }
181  TGeoNode* node = gGeoManager->GetMother(off);
182  if (!node) {
183  return 0;
184  }
185  return node->GetVolume()->GetName();
186  }
187 }
188 
190 {
191  if (fIsSimulation) {
192  LOG(fatal) << "This methode is not supported in simulation mode";
193  } else {
194  gGeoManager->cd(volName.Data());
195  fGeoPathHash = volName.Hash();
196  fCurrentVolume = gGeoManager->GetCurrentVolume();
197  fVolumeShape = static_cast<TGeoBBox*>(fCurrentVolume->GetShape());
198  Double_t local[3] = {0., 0., 0.}; // Local centre of volume
199  gGeoManager->LocalToMaster(local, fGlobal);
200  LOG(debug2) << "Pos: " << fGlobal[0] << " , " << fGlobal[1] << " , " << fGlobal[2];
201  // fGlobalMatrix = gGeoManager->GetCurrentMatrix();
202  }
203 }
204 
Int_t CurrentVolID(Int_t &copy) const
ClassImp(FairEventBuilder)
Int_t VolIdGeo(const char *name) const
const char * CurrentVolOffName(Int_t off) const
Int_t VolId(const Text_t *name) const
Int_t Init(Bool_t isSimulation=kFALSE)
void LocalToGlobal(Double_t *local, Double_t *global, Int_t detID)
Int_t CurrentVolOffID(Int_t off, Int_t &copy) const