Belle II Software development
ECLLeakagePosition.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
9//..ECL
10#include <ecl/geometry/ECLLeakagePosition.h>
11#include <ecl/dbobjects/ECLCrystalCalib.h>
12#include <ecl/geometry/ECLNeighbours.h>
13
14//..Other
15#include <iostream>
16#include <TMath.h>
17
18using namespace Belle2;
19using namespace ECL;
20
21//..Constructor.
23 m_ECLCrystalThetaEdge("ECLCrystalThetaEdge"),
24 m_ECLCrystalPhiEdge("ECLCrystalPhiEdge"),
25 m_ECLCrystalThetaWidth("ECLCrystalThetaWidth"),
26 m_ECLCrystalPhiWidth("ECLCrystalPhiWidth")
27{
28
29 //..Obtain the vectors of crystal edges and widths from the database
30 if (m_ECLCrystalThetaEdge.hasChanged()) {
31 m_thetaEdge = m_ECLCrystalThetaEdge->getCalibVector();
32 }
33 if (m_ECLCrystalPhiEdge.hasChanged()) {
34 m_phiEdge = m_ECLCrystalPhiEdge->getCalibVector();
35 }
36 if (m_ECLCrystalThetaWidth.hasChanged()) {
37 m_thetaWidth = m_ECLCrystalThetaWidth->getCalibVector();
38 }
39 if (m_ECLCrystalPhiWidth.hasChanged()) {
40 m_phiWidth = m_ECLCrystalPhiWidth->getCalibVector();
41 }
42
43 //..Eight nearest neighbours, plus crystal itself. Uses cellID, 1--8736
44 m_neighbours = new ECLNeighbours("N", 1);
45
46 //..Record the thetaID and phiID of each cellID
47 for (int thID = 0; thID < 69; thID++) {
48 for (int phID = 0; phID < m_neighbours->getCrystalsPerRing(thID); phID++) {
49 m_thetaIDofCrysID.push_back(thID);
50 m_phiIDofCrysID.push_back(phID);
51 }
52 }
53
54 //..Crystals between mechanical structure for each thetaID
55 for (int thID = 0; thID < m_firstBarrelThetaID; thID++) {
56 int nCrys = m_neighbours->getCrystalsPerRing(thID) / 16;
57 m_crysBetweenMech.push_back(nCrys);
58 }
59 for (int thID = m_firstBarrelThetaID; thID <= m_lastBarrelThetaID; thID++) {
60 m_crysBetweenMech.push_back(2);
61 }
62 for (int thID = m_lastBarrelThetaID + 1; thID < 69; thID++) {
63 int nCrys = m_neighbours->getCrystalsPerRing(thID) / 16;
64 m_crysBetweenMech.push_back(nCrys);
65 }
66}
67
69{
70 delete m_neighbours;
71}
72
73std::vector<int> ECLLeakagePosition::getLeakagePosition(const int cellIDFromEnergy, const float theta, const float phi,
74 const int nPositions)
75{
76
77 //..Start by checking if the crystal with the most energy is the correct one
78 int crysID = cellIDFromEnergy - 1;
79 int iStatus = -1;
80
81 //..theta and phi need to be within the crystal
82 float dTheta = theta - m_thetaEdge[crysID];
83 float dPhi = phi - m_phiEdge[crysID];
84 if (dPhi > TMath::Pi()) {dPhi -= 2.*TMath::Pi();}
85 if (dPhi < -TMath::Pi()) {dPhi += 2.*TMath::Pi();}
86 if (dTheta >= 0 and dTheta <= m_thetaWidth[crysID] and dPhi >= 0 and dPhi <= m_phiWidth[crysID]) {
87 iStatus = 0;
88 }
89
90 //..Theta and phi are not located in the maximum energy crystal. Check the
91 // eight nearest neighbours.
92 if (iStatus == -1) {
93
94 //..Nearest neighbours (plus the central crystal)
95 for (const auto& tempCellID : m_neighbours->getNeighbours(cellIDFromEnergy)) {
96 int tempCrysID = tempCellID - 1;
97 dTheta = theta - m_thetaEdge[tempCrysID];
98 dPhi = phi - m_phiEdge[tempCrysID];
99 if (dPhi > TMath::Pi()) {dPhi -= 2.*TMath::Pi();}
100 if (dPhi < -TMath::Pi()) {dPhi += 2.*TMath::Pi();}
101
102 //..This one is correct
103 if (dTheta >= 0 and dTheta <= m_thetaWidth[tempCrysID] and dPhi >= 0 and dPhi <= m_phiWidth[tempCrysID]) {
104 iStatus = 1;
105 crysID = tempCrysID;
106 break;
107 }
108 }
109 }
110
111 //..Need to do this one by brute force
112 if (iStatus == -1) {
113
114 //..Start at the last crystal and work backwards
115 for (int crys = 8735; crys >= 0; crys--) {
116 dTheta = theta - m_thetaEdge[crys];
117 dPhi = phi - m_phiEdge[crys];
118 if (dPhi > TMath::Pi()) {dPhi -= 2.*TMath::Pi();}
119 if (dPhi < -TMath::Pi()) {dPhi += 2.*TMath::Pi();}
120 if (dPhi >= 0 and dPhi <= m_phiWidth[crys] and dTheta >= 0) {
121 iStatus = 2;
122 crysID = crys;
123 break;
124 }
125 }
126 }
127
128 //..Above should cover all cases, but set sensible values if we missed something
129 if (iStatus == -1) {
130 iStatus = 3;
131 crysID = cellIDFromEnergy - 1;
132 dTheta = 0.5;
133 dPhi = 0.5;
134 }
135
136 //..Return values
137 int cellID = crysID + 1;
138 int thetaID = m_thetaIDofCrysID[crysID];
139 int iRegion = 0;
140 if (thetaID >= m_firstBarrelThetaID and thetaID <= m_lastBarrelThetaID) {iRegion = 1;}
141 if (thetaID > m_lastBarrelThetaID) {iRegion = 2;}
142
143 //..Mechanical structure is on lower edge of phiID 0, and every 2 (barrel) or n (endcap)
144 // crystals thereafter
145 int iPhiMech = m_phiIDofCrysID[crysID] % m_crysBetweenMech[thetaID];
146 bool reversePhi;
147
148 //..Mechanical structure on lower edge
149 if ((iPhiMech == 0 and iRegion != 1) or (iPhiMech == m_crysBetweenMech[thetaID] - 1 and iRegion == 1)) {
150 iPhiMech = 0;
151 reversePhi = false;
152
153 //..Mechanical structure on upper edge
154 } else if ((iPhiMech == m_crysBetweenMech[thetaID] - 1 and iRegion != 1) or (iPhiMech == 0 and iRegion == 1)) {
155 iPhiMech = 0;
156 reversePhi = true;
157
158 //..No mechanical structure adjacent
159 } else {
160 iPhiMech = 1;
161 reversePhi = false;
162 }
163
164 //..Divide crystal into nPositions in width, starting from lower edge
165 int iLocalTheta = (int)(nPositions * dTheta / m_thetaWidth[crysID]);
166 if (iLocalTheta < 0) {iLocalTheta = 0;}
167 if (iLocalTheta >= nPositions) {iLocalTheta = nPositions - 1;}
168
169 int iLocalPhi = (int)(nPositions * dPhi / m_phiWidth[crysID]);
170 if (iLocalPhi < 0) {iLocalPhi = 0;}
171 if (iLocalPhi >= nPositions) {iLocalPhi = nPositions - 1;}
172
173 //..iLocalPhi is measured from edge with mechanical structure, if present,
174 // so reverse order of iLocalPhi if mech structure is on the upper edge
175 if (reversePhi) {iLocalPhi = nPositions - iLocalPhi - 1;}
176
177 //..Return as a std::vector
178 std::vector<int> position;
179 position.push_back(cellID);
180 position.push_back(thetaID);
181 position.push_back(iRegion);
182 position.push_back(iLocalTheta);
183 position.push_back(iLocalPhi);
184 position.push_back(iPhiMech);
185 position.push_back(iStatus);
186 return position;
187}
DBObjPtr< ECLCrystalCalib > m_ECLCrystalPhiEdge
lower edges of crystals, phi
std::vector< int > m_crysBetweenMech
crystals between phi mechanical structure per thetaID
ECL::ECLNeighbours * m_neighbours
8 nearest neighbours to crystal
std::vector< int > m_thetaIDofCrysID
thetaID of each crystal ID
std::vector< float > m_thetaEdge
lower theta edges from DB object
std::vector< int > m_phiIDofCrysID
phiID of each crystal ID
std::vector< float > m_thetaWidth
crystal theta widths from DB object
DBObjPtr< ECLCrystalCalib > m_ECLCrystalThetaWidth
width in theta
std::vector< float > m_phiWidth
crystal phi widths from DB object
DBObjPtr< ECLCrystalCalib > m_ECLCrystalPhiWidth
width in phi
const int m_firstBarrelThetaID
first barrel thetaID
std::vector< int > getLeakagePosition(const int cellIDFromEnergy, const float theta, const float phi, const int nPositions)
Return postion.
std::vector< float > m_phiEdge
lower phi edges from DB object
const int m_lastBarrelThetaID
last barrel thetaID
DBObjPtr< ECLCrystalCalib > m_ECLCrystalThetaEdge
Required geometry payloads.
Class to get the neighbours for a given cell id.
Definition: ECLNeighbours.h:25
const std::vector< short int > & getNeighbours(short int cid) const
Return the neighbours for a given cell ID.
short int getCrystalsPerRing(const short int thetaid) const
return number of crystals in a given theta ring
Definition: ECLNeighbours.h:39
Abstract base class for different kinds of events.