Belle II Software  release-06-02-00
CsiGeometryPar.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #include <geometry/Materials.h>
9 #include <framework/gearbox/GearDir.h>
10 #include <framework/logging/Logger.h>
11 #include <boost/format.hpp>
12 #include <boost/lexical_cast.hpp>
13 #include <boost/algorithm/string.hpp>
14 #include <beast/csi/geometry/CsiGeometryPar.h>
15 #include <cmath>
16 #include <fstream>
17 
18 
19 using namespace std;
20 using namespace boost;
21 using namespace Belle2;
22 using namespace csi;
23 
24 #define PI 3.14159265358979323846
25 
26 
27 CsiGeometryPar* CsiGeometryPar::m_B4CsiGeometryParDB = 0;
28 
29 CsiGeometryPar* CsiGeometryPar::Instance()
30 {
31  if (!m_B4CsiGeometryParDB) m_B4CsiGeometryParDB = new CsiGeometryPar();
32  return m_B4CsiGeometryParDB;
33 }
34 
35 CsiGeometryPar::CsiGeometryPar()
36 {
37  clear();
38  read();
39 
40  PrintAll();
41 }
42 
43 CsiGeometryPar::~CsiGeometryPar()
44 {
45  if (m_B4CsiGeometryParDB) {
46  delete m_B4CsiGeometryParDB;
47  B2INFO("m_B4CsiGeometryParDB deleted ");
48  }
49 }
50 
51 void CsiGeometryPar::clear()
52 {
53  m_cellID = 0;
54 
55  m_Position.clear();
56  m_Orientation.clear();
57  m_BoxID.clear();
58  m_SlotID.clear();
59  m_thetaID.clear();
60  m_phiID.clear();
61 
62 }
63 
64 void CsiGeometryPar::read()
65 {
66 
67  GearDir content = GearDir("/Detector/DetectorComponent[@name=\"CSI\"]/Content/");
68  string gearPath = "Enclosures/Enclosure";
69  int nEnc = content.getNumberNodes(gearPath);
70 
71  int iCell = 0;
72 
73  for (int iEnc = 1; iEnc <= nEnc; iEnc++) {
74  // Build the box (same for all)
75  //double length = content.getLength("Enclosures/Length") * CLHEP::cm;
76  //double thk = content.getLength("Enclosures/Thickness") * CLHEP::cm;
77  //double halflength = 15.0 * CLHEP::cm;
78  //double zshift = 0.5 * length - thk - halflength; /*< Shift of the box along z-axis (cry touches panel) **/
79 
80  string enclosurePath = (boost::format("/%1%[%2%]") % gearPath % iEnc).str();
81 
82  // Connect the appropriate Gearbox path
83  GearDir enclosureContent(content);
84  enclosureContent.append(enclosurePath);
85 
86  // Read position
87  double PosZ = enclosureContent.getLength("PosZ") * CLHEP::cm;
88  double PosR = enclosureContent.getLength("PosR") * CLHEP::cm;
89  double PosT = enclosureContent.getAngle("PosT") ;
90 
91  // Read Orientation
92  double Phi1 = enclosureContent.getAngle("AngPhi1") ;
93  double Theta = enclosureContent.getAngle("AngTheta") ;
94  double Phi2 = enclosureContent.getAngle("AngPhi2") ;
95 
96  //Transform3D zsh = Translate3D(0, 0, zshift);
97  Transform3D m1 = RotateZ3D(Phi1);
98  Transform3D m2 = RotateY3D(Theta);
99  Transform3D m3 = RotateZ3D(Phi2);
100  Transform3D position = Translate3D(PosR * cos(PosT), PosR * sin(PosT), PosZ);
101 
103  Transform3D Tr = position * m3 * m2 * m1;
104 
105  int nSlots = enclosureContent.getNumberNodes("CrystalInSlot");
106  for (int iSlot = 1; iSlot <= nSlots; iSlot++) {
107  iCell++;
108 
109  //Thread the strings
110  string slotPath = (boost::format("/Enclosures/Slot[%1%]") % iSlot).str();
111 
112  GearDir slotContent(content);
113  slotContent.append(slotPath);
114 
115  double SlotX = slotContent.getLength("PosX") * CLHEP::cm;
116  double SlotY = slotContent.getLength("PosY") * CLHEP::cm;
117  double SlotZ = slotContent.getLength("PosZ") * CLHEP::cm;
118  Transform3D Pos = Translate3D(SlotX, SlotY, SlotZ);
119 
120  Transform3D CrystalPos = Tr * Pos;
121  RotationMatrix CrystalRot = CrystalPos.getRotation();
122 
123  m_Position.push_back(CrystalPos.getTranslation() * 1.0 / CLHEP::cm);
124  m_Orientation.push_back(CrystalRot.colZ());
125 
126  m_thetaID.push_back(CrystalPos.getTranslation().z() > 0 ? 0 : 1);
127  m_phiID.push_back(iCell - 9 * m_thetaID.back());
128 
129  m_BoxID.push_back(iEnc - 1);
130  m_SlotID.push_back(iSlot - 1);
131  }
132  //
133  }
134 
135  //comnvert all that to tvector3's for speed
136 
137  vector<ThreeVector>::iterator it;
138  for (it = m_Position.begin(); it != m_Position.end(); ++it) {
139  m_PositionTV3.push_back(ConvertToTVector3(*it));
140  }
141  for (it = m_Orientation.begin(); it != m_Orientation.end(); ++it) {
142  m_OrientationTV3.push_back(ConvertToTVector3(*it));
143  }
144 
145 
146 }
147 
148 
149 int CsiGeometryPar::CsiVolNameToCellID(const G4String VolumeName)
150 {
151  int cellID = 0;
152 
153  vector< string > partName;
154  split(partName, VolumeName, is_any_of("_"));
155 
156  int iEnclosure = -1;
157  int iCrystal = -1;
158  for (std::vector<string>::iterator it = partName.begin() ; it != partName.end(); ++it) {
159  if (equals(*it, "Enclosure")) iEnclosure = boost::lexical_cast<int>(*(it + 1)) - 1;
160  else if (equals(*it, "Crystal")) iCrystal = boost::lexical_cast<int>(*(it + 1)) - 1;
161  }
162 
163  cellID = 3 * iEnclosure + iCrystal;
164 
165  if (cellID < 0) B2WARNING("CsiGeometryPar: volume " << VolumeName << " is not a crystal");
166 
167  return cellID;
168 }
169 
170 
171 G4Material* CsiGeometryPar::GetMaterial(int cid)
172 {
173  int iEnclosure = GetEnclosureID(cid) + 1;
174  int iSlot = GetSlotID(cid) + 1;
175 
176  GearDir content = GearDir("/Detector/DetectorComponent[@name=\"CSI\"]/Content/");
177 
178  GearDir enclosureContent(content);
179  string gearPath = "Enclosures/Enclosure";
180  string enclosurePath = (format("/%1%[%2%]") % gearPath % iEnclosure).str();
181  enclosureContent.append(enclosurePath);
182 
183  string slotName = (format("CrystalInSlot[%1%]") % iSlot).str();
184  int iCry = enclosureContent.getInt(slotName);
185 
186 
187  GearDir crystalContent(content);
188  crystalContent.append((format("/EndCapCrystals/EndCapCrystal[%1%]/") % (iCry)).str());
189  string strMatCrystal = crystalContent.getString("Material", "Air");
190 
191  return geometry::Materials::get(strMatCrystal);
192 
193 }
194 
195 
196 double CsiGeometryPar::GetMaterialProperty(int cid, const char* propertyname)
197 {
198  G4Material* mat = GetMaterial(cid);
199  G4MaterialPropertiesTable* properties = mat->GetMaterialPropertiesTable();
200  G4MaterialPropertyVector* property = properties->GetProperty(propertyname);
201 
202  return property->Value(0);
203 }
204 
205 void CsiGeometryPar::Print(int cid, int debuglevel)
206 {
207  B2DEBUG(debuglevel, "Cell ID : " << cid);
208 
209  B2DEBUG(debuglevel, " Position x : " << GetPosition(cid).x());
210  B2DEBUG(debuglevel, " Position y : " << GetPosition(cid).y());
211  B2DEBUG(debuglevel, " Position z : " << GetPosition(cid).z());
212 
213  B2DEBUG(debuglevel, " Orientation x : " << GetOrientation(cid).x());
214  B2DEBUG(debuglevel, " Orientation y : " << GetOrientation(cid).y());
215  B2DEBUG(debuglevel, " Orientation z : " << GetOrientation(cid).z());
216 
217  B2DEBUG(debuglevel, " Material : " << GetMaterial(cid)->GetName());
218 
219  B2DEBUG(debuglevel, " Slow time constatnt : " << GetMaterialProperty(cid, "SLOWTIMECONSTANT"));
220  B2DEBUG(debuglevel, " Fast time constatnt : " << GetMaterialProperty(cid, "FASTTIMECONSTANT"));
221  B2DEBUG(debuglevel, " Light yield : " << GetMaterialProperty(cid, "SCINTILLATIONYIELD"));
222 
223  //GetMaterial(cid)->GetMaterialPropertiesTable()->DumpTable();
224 }
225 
226 void CsiGeometryPar::PrintAll(int debuglevel)
227 {
228  for (uint i = 0; i < m_thetaID.size(); i++)
229  Print(i, debuglevel);
230 }
GearDir is the basic class used for accessing the parameter store.
Definition: GearDir.h:31
void append(const std::string &path)
Append something to the current path, modifying the GearDir in place.
Definition: GearDir.h:52
virtual int getNumberNodes(const std::string &path="") const override
Return the number of nodes a given path will expand to.
Definition: GearDir.h:58
virtual std::string getString(const std::string &path="") const noexcept(false) override
Get the parameter path as a string.
Definition: GearDir.h:69
The Class for CSI Geometry Parameters.
double getAngle(const std::string &path="") const noexcept(false)
Get the parameter path as a double converted to the standard angle unit.
Definition: Interface.h:299
double getLength(const std::string &path="") const noexcept(false)
Get the parameter path as a double converted to the standard length unit.
Definition: Interface.h:259
int getInt(const std::string &path="") const noexcept(false)
Get the parameter path as a int.
Definition: Interface.cc:60
Abstract base class for different kinds of events.