Belle II Software  release-08-01-10
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 CsiGeometryPar* CsiGeometryPar::m_B4CsiGeometryParDB = 0;
25 
26 CsiGeometryPar* CsiGeometryPar::Instance()
27 {
28  if (!m_B4CsiGeometryParDB) m_B4CsiGeometryParDB = new CsiGeometryPar();
29  return m_B4CsiGeometryParDB;
30 }
31 
32 CsiGeometryPar::CsiGeometryPar()
33 {
34  clear();
35  read();
36 
37  PrintAll();
38 }
39 
40 CsiGeometryPar::~CsiGeometryPar()
41 {
42  if (m_B4CsiGeometryParDB) {
43  delete m_B4CsiGeometryParDB;
44  B2INFO("m_B4CsiGeometryParDB deleted ");
45  }
46 }
47 
48 void CsiGeometryPar::clear()
49 {
50  m_cellID = 0;
51 
52  m_Position.clear();
53  m_Orientation.clear();
54  m_BoxID.clear();
55  m_SlotID.clear();
56  m_thetaID.clear();
57  m_phiID.clear();
58 
59 }
60 
61 void CsiGeometryPar::read()
62 {
63 
64  GearDir content = GearDir("/Detector/DetectorComponent[@name=\"CSI\"]/Content/");
65  string gearPath = "Enclosures/Enclosure";
66  int nEnc = content.getNumberNodes(gearPath);
67 
68  int iCell = 0;
69 
70  for (int iEnc = 1; iEnc <= nEnc; iEnc++) {
71  // Build the box (same for all)
72  //double length = content.getLength("Enclosures/Length") * CLHEP::cm;
73  //double thk = content.getLength("Enclosures/Thickness") * CLHEP::cm;
74  //double halflength = 15.0 * CLHEP::cm;
75  //double zshift = 0.5 * length - thk - halflength; /*< Shift of the box along z-axis (cry touches panel) **/
76 
77  string enclosurePath = (boost::format("/%1%[%2%]") % gearPath % iEnc).str();
78 
79  // Connect the appropriate Gearbox path
80  GearDir enclosureContent(content);
81  enclosureContent.append(enclosurePath);
82 
83  // Read position
84  double PosZ = enclosureContent.getLength("PosZ") * CLHEP::cm;
85  double PosR = enclosureContent.getLength("PosR") * CLHEP::cm;
86  double PosT = enclosureContent.getAngle("PosT") ;
87 
88  // Read Orientation
89  double Phi1 = enclosureContent.getAngle("AngPhi1") ;
90  double Theta = enclosureContent.getAngle("AngTheta") ;
91  double Phi2 = enclosureContent.getAngle("AngPhi2") ;
92 
93  //Transform3D zsh = Translate3D(0, 0, zshift);
94  Transform3D m1 = RotateZ3D(Phi1);
95  Transform3D m2 = RotateY3D(Theta);
96  Transform3D m3 = RotateZ3D(Phi2);
97  Transform3D position = Translate3D(PosR * cos(PosT), PosR * sin(PosT), PosZ);
98 
100  Transform3D Tr = position * m3 * m2 * m1;
101 
102  int nSlots = enclosureContent.getNumberNodes("CrystalInSlot");
103  for (int iSlot = 1; iSlot <= nSlots; iSlot++) {
104  iCell++;
105 
106  //Thread the strings
107  string slotPath = (boost::format("/Enclosures/Slot[%1%]") % iSlot).str();
108 
109  GearDir slotContent(content);
110  slotContent.append(slotPath);
111 
112  double SlotX = slotContent.getLength("PosX") * CLHEP::cm;
113  double SlotY = slotContent.getLength("PosY") * CLHEP::cm;
114  double SlotZ = slotContent.getLength("PosZ") * CLHEP::cm;
115  Transform3D Pos = Translate3D(SlotX, SlotY, SlotZ);
116 
117  Transform3D CrystalPos = Tr * Pos;
118  RotationMatrix CrystalRot = CrystalPos.getRotation();
119 
120  m_Position.push_back(CrystalPos.getTranslation() * 1.0 / CLHEP::cm);
121  m_Orientation.push_back(CrystalRot.colZ());
122 
123  m_thetaID.push_back(CrystalPos.getTranslation().z() > 0 ? 0 : 1);
124  m_phiID.push_back(iCell - 9 * m_thetaID.back());
125 
126  m_BoxID.push_back(iEnc - 1);
127  m_SlotID.push_back(iSlot - 1);
128  }
129  //
130  }
131 
132  //comnvert all that to tvector3's for speed
133 
134  vector<ThreeVector>::iterator it;
135  for (it = m_Position.begin(); it != m_Position.end(); ++it) {
136  m_PositionTV3.push_back(ConvertToTVector3(*it));
137  }
138  for (it = m_Orientation.begin(); it != m_Orientation.end(); ++it) {
139  m_OrientationTV3.push_back(ConvertToTVector3(*it));
140  }
141 
142 
143 }
144 
145 
146 int CsiGeometryPar::CsiVolNameToCellID(const G4String VolumeName)
147 {
148  int cellID = 0;
149 
150  vector< string > partName;
151  split(partName, VolumeName, is_any_of("_"));
152 
153  int iEnclosure = -1;
154  int iCrystal = -1;
155  for (std::vector<string>::iterator it = partName.begin() ; it != partName.end(); ++it) {
156  if (equals(*it, "Enclosure")) iEnclosure = boost::lexical_cast<int>(*(it + 1)) - 1;
157  else if (equals(*it, "Crystal")) iCrystal = boost::lexical_cast<int>(*(it + 1)) - 1;
158  }
159 
160  cellID = 3 * iEnclosure + iCrystal;
161 
162  if (cellID < 0) B2WARNING("CsiGeometryPar: volume " << VolumeName << " is not a crystal");
163 
164  return cellID;
165 }
166 
167 
168 G4Material* CsiGeometryPar::GetMaterial(int cid)
169 {
170  int iEnclosure = GetEnclosureID(cid) + 1;
171  int iSlot = GetSlotID(cid) + 1;
172 
173  GearDir content = GearDir("/Detector/DetectorComponent[@name=\"CSI\"]/Content/");
174 
175  GearDir enclosureContent(content);
176  string gearPath = "Enclosures/Enclosure";
177  string enclosurePath = (format("/%1%[%2%]") % gearPath % iEnclosure).str();
178  enclosureContent.append(enclosurePath);
179 
180  string slotName = (format("CrystalInSlot[%1%]") % iSlot).str();
181  int iCry = enclosureContent.getInt(slotName);
182 
183 
184  GearDir crystalContent(content);
185  crystalContent.append((format("/EndCapCrystals/EndCapCrystal[%1%]/") % (iCry)).str());
186  string strMatCrystal = crystalContent.getString("Material", "Air");
187 
188  return geometry::Materials::get(strMatCrystal);
189 
190 }
191 
192 
193 double CsiGeometryPar::GetMaterialProperty(int cid, const char* propertyname)
194 {
195  G4Material* mat = GetMaterial(cid);
196  G4MaterialPropertiesTable* properties = mat->GetMaterialPropertiesTable();
197  G4MaterialPropertyVector* property = properties->GetProperty(propertyname);
198 
199  return property->Value(0);
200 }
201 
202 void CsiGeometryPar::Print(int cid, int debuglevel)
203 {
204  B2DEBUG(debuglevel, "Cell ID : " << cid);
205 
206  B2DEBUG(debuglevel, " Position x : " << GetPosition(cid).x());
207  B2DEBUG(debuglevel, " Position y : " << GetPosition(cid).y());
208  B2DEBUG(debuglevel, " Position z : " << GetPosition(cid).z());
209 
210  B2DEBUG(debuglevel, " Orientation x : " << GetOrientation(cid).x());
211  B2DEBUG(debuglevel, " Orientation y : " << GetOrientation(cid).y());
212  B2DEBUG(debuglevel, " Orientation z : " << GetOrientation(cid).z());
213 
214  B2DEBUG(debuglevel, " Material : " << GetMaterial(cid)->GetName());
215 
216  B2DEBUG(debuglevel, " Slow time constatnt : " << GetMaterialProperty(cid, "SLOWTIMECONSTANT"));
217  B2DEBUG(debuglevel, " Fast time constatnt : " << GetMaterialProperty(cid, "FASTTIMECONSTANT"));
218  B2DEBUG(debuglevel, " Light yield : " << GetMaterialProperty(cid, "SCINTILLATIONYIELD"));
219 
220  //GetMaterial(cid)->GetMaterialPropertiesTable()->DumpTable();
221 }
222 
223 void CsiGeometryPar::PrintAll(int debuglevel)
224 {
225  for (uint i = 0; i < m_thetaID.size(); i++)
226  Print(i, debuglevel);
227 }
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.