Belle II Software  release-05-01-25
CDCSpaceResols.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: CDC group *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <map>
13 #include <array>
14 #include <iostream>
15 #include <fstream>
16 #include <iomanip>
17 #include <algorithm>
18 #include <TObject.h>
19 
20 namespace Belle2 {
28  class CDCSpaceResols: public TObject {
29 
30  typedef std::array<float, 3> array3;
31  typedef unsigned short SigmaID;
33  public:
37  enum {c_nSLayers = 56,
41  };
42 
47  {}
48 
52  void setAlphaBin(const array3& alpha)
53  {
54  if (m_alphaBins.size() <= c_maxNAlphaBins) {
55  m_alphaBins.push_back(alpha);
56  sort(m_alphaBins.begin(), m_alphaBins.end(), comp);
57  } else {
58  // std::cout<< m_alphaBins.size() <<" "<< c_maxNAlphaBins <<std::endl;
59  B2FATAL("The no. of alpha bins > limit !");
60  }
61  }
62 
66  void setThetaBin(const array3& theta)
67  {
68  if (m_thetaBins.size() <= c_maxNThetaBins) {
69  m_thetaBins.push_back(theta);
70  sort(m_thetaBins.begin(), m_thetaBins.end(), comp);
71  } else {
72  B2FATAL("The no. of theta bins > limit !");
73  }
74  }
75 
79  static bool comp(const array3& lhs, const array3& rhs)
80  {
81  return lhs[0] < rhs[0];
82  }
83 
87  void setSigmaParamMode(unsigned short mode)
88  {
89  m_sigmaParamMode = mode;
90  }
91 
95  void setSigmaParams(const SigmaID sigmaID, const std::vector<float>& params)
96  {
97  unsigned short nSigmaPars = params.size();
98 
99  if (nSigmaPars <= c_maxNSigmaParams) {
100  m_nSigmaParams = nSigmaPars;
101  m_sigmas.insert(std::pair<SigmaID, std::vector<float>>(sigmaID, params));
102  // std::cout <<"sigmaID in setSigmaParams= " << sigmaID << std::endl;
103  } else {
104  B2FATAL("The no. of sigma params. > limit !");
105  }
106  }
107 
111  void setSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
112  const std::vector<float>& params)
113  {
114  const SigmaID sigmaID = getSigmaID(iCLayer, iLR, iAlpha, iTheta);
115  setSigmaParams(sigmaID, params);
116  }
117 
121  void addSigmaParams(const SigmaID sigmaID, const std::vector<float>& delta)
122  {
123  std::map<SigmaID, std::vector<float>>::iterator it = m_sigmas.find(sigmaID);
124 
125  if (it != m_sigmas.end()) {
126  for (unsigned short i = 0; i < m_nSigmaParams; ++i) {
127  (it->second)[i] += delta[i];
128  }
129  } else {
130  B2FATAL("Specified params not found in addSigmaParams !");
131  }
132  }
133 
137  void addSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
138  const std::vector<float>& delta)
139  {
140  const SigmaID sigmaID = getSigmaID(iCLayer, iLR, iAlpha, iTheta);
141  addSigmaParams(sigmaID, delta);
142  }
143 
144 
148  unsigned short getNoOfAlphaBins() const
149  {
150  return m_alphaBins.size();
151  }
152 
156  unsigned short getNoOfThetaBins() const
157  {
158  return m_thetaBins.size();
159  }
160 
164  const array3& getAlphaBin(unsigned short i) const
165  {
166  return m_alphaBins[i];
167  }
168 
172  float getAlphaPoint(unsigned short i) const
173  {
174  return m_alphaBins[i][2];
175  }
176 
180  const array3& getThetaBin(unsigned short i) const
181  {
182  return m_thetaBins[i];
183  }
184 
188  float getThetaPoint(unsigned short i) const
189  {
190  return m_thetaBins[i][2];
191  }
192 
196  unsigned short getSigmaParamMode() const
197  {
198  return m_sigmaParamMode;
199  }
200 
208  SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
209  {
210  SigmaID id = iCLayer + 64 * iLR + 128 * iAlpha + 4096 * iTheta;
211  return id;
212  }
213 
217  SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, float alpha, float theta) const
218  {
219  /*
220  unsigned short iTheta = 999;
221  unsigned short ibin = 0;
222  for (std::vector<array3>::const_iterator it = m_thetaBins.begin(); it != m_thetaBins.end(); ++it) {
223  if ((*it)[0] <= theta && theta <= (*it)[1]) {
224  iTheta = ibin;
225  break;
226  }
227  ++ibin;
228  }
229  */
230  unsigned short iTheta = 999;
231  unsigned short ibin = 0;
232  for (auto const& it : m_thetaBins) {
233  if (it[0] <= theta && theta <= it[1]) {
234  iTheta = ibin;
235  break;
236  }
237  ++ibin;
238  }
239  if (iTheta == 999) B2FATAL("Theta bin not found !");
240 
241  /*
242  unsigned short iAlpha = 999;
243  ibin = 0;
244  for (std::vector<array3>::const_iterator it = m_alphaBins.begin(); it != m_alphaBins.end(); ++it) {
245  if ((*it)[0] <= alpha && alpha <= (*it)[1]) {
246  iAlpha = ibin;
247  break;
248  }
249  ++ibin;
250  }
251  */
252  unsigned short iAlpha = 999;
253  ibin = 0;
254  for (auto const& it : m_alphaBins) {
255  if (it[0] <= alpha && alpha <= it[1]) {
256  iAlpha = ibin;
257  break;
258  }
259  ++ibin;
260  }
261  if (iAlpha == 999) B2FATAL("Alpha bin not found !");
262 
263  return getSigmaID(iCLayer, iLR, iAlpha, iTheta);
264  }
265 
269  const std::vector<float>& getSigmaParams(const SigmaID sigmaID) const
270  {
271  std::map<SigmaID, std::vector<float>>::const_iterator it = m_sigmas.find(sigmaID);
272  if (it != m_sigmas.end()) {
273  return it->second;
274  } else {
275  B2FATAL("Specified params. not found in getSigmaParams !");
276  }
277  }
278 
282  const std::vector<float>& getSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha,
283  unsigned short iTheta) const
284  {
285  const SigmaID sigmaID = getSigmaID(iCLayer, iLR, iAlpha, iTheta);
286  // std::cout <<"sigmaID in getSigmaParams= " << sigmaID << std::endl;
287  return getSigmaParams(sigmaID);
288  }
289 
293  void dump() const
294  {
295  std::cout << " " << std::endl;
296  std::cout << "Contents of sigma db" << std::endl;
297  std::cout << "alpha bins" << std::endl;
298 
299  const double deg = 180. / M_PI;
300 
301  unsigned short nAlphaBins = m_alphaBins.size();
302  for (unsigned short i = 0; i < nAlphaBins; ++i) {
303  std::cout << " " << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << " " << std::endl;
304  }
305 
306  std::cout << " " << std::endl;
307  std::cout << "theta bins" << std::endl;
308 
309  unsigned short nThetaBins = m_thetaBins.size();
310  for (unsigned short i = 0; i < nThetaBins; ++i) {
311  std::cout << " " << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << " " << std::endl;
312  }
313 
314  std::cout << " " << std::endl;
315  std::cout << "coefficients for sigma" << std::endl;
316 
317  for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
318  for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
319  for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
320  for (unsigned short iLR = 0; iLR < 2; ++iLR) {
321  unsigned short iLRp = abs(iLR - 1);
322  std::cout << iCL << " " << deg* m_thetaBins[iT][2] << " " << deg* m_alphaBins[iA][2] << " " << iLRp;
323  const std::vector<float> params = getSigmaParams(iCL, iLRp, iA, iT);
324  for (unsigned short i = 0; i < m_nSigmaParams; ++i) {
325  std::cout << " " << params[i];
326  }
327  std::cout << " " << std::endl;
328  }
329  }
330  }
331  }
332  }
333 
337  void outputToFile(std::string fileName) const
338  {
339  std::ofstream fout(fileName);
340 
341  if (fout.bad()) {
342  B2ERROR("Specified output file could not be opened!");
343  } else {
344  const double deg = 180. / M_PI;
345 
346  unsigned short nAlphaBins = m_alphaBins.size();
347  fout << nAlphaBins << std::endl;
348 
349  for (unsigned short i = 0; i < nAlphaBins; ++i) {
350  fout << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << std::endl;
351  }
352 
353  unsigned short nThetaBins = m_thetaBins.size();
354  fout << nThetaBins << std::endl;
355 
356  for (unsigned short i = 0; i < nThetaBins; ++i) {
357  fout << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << std::endl;
358  }
359 
360  fout << m_sigmaParamMode << " " << m_nSigmaParams << std::endl;
361 
362  for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
363  for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
364  for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
365  for (unsigned short iLR = 0; iLR < 2; ++iLR) {
366  unsigned short iLRp = abs(iLR - 1);
367  fout << std::setw(2) << std::right << std::fixed << iCL << " " << std::setw(5) << std::setprecision(
368  1) << deg* m_thetaBins[iT][2] << " " << std::setw(5) << std::right << deg* m_alphaBins[iA][2] << " " << std::setw(1) << iLRp;
369  const std::vector<float> params = getSigmaParams(iCL, iLRp, iA, iT);
370  for (unsigned short i = 0; i < m_nSigmaParams; ++i) {
371  fout << " " << std::setw(15) << std::scientific << std::setprecision(8) << params[i];
372  }
373  fout << std::endl;
374  }
375  }
376  }
377  }
378  fout.close();
379  }
380  }
381 
382  private:
383  unsigned short m_sigmaParamMode;
384  unsigned short m_nSigmaParams;
385  std::vector<array3> m_alphaBins;
386  std::vector<array3> m_thetaBins;
387  std::map<SigmaID, std::vector<float>>
388  m_sigmas;
391  };
392 
394 } // end namespace Belle2
Belle2::CDCSpaceResols::c_nSLayers
@ c_nSLayers
no.
Definition: CDCSpaceResols.h:45
Belle2::CDCSpaceResols::dump
void dump() const
Print all contents.
Definition: CDCSpaceResols.h:301
Belle2::CDCSpaceResols::getAlphaBin
const array3 & getAlphaBin(unsigned short i) const
Get i-th alpha-angle bin info.
Definition: CDCSpaceResols.h:172
Belle2::CDCSpaceResols::setAlphaBin
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
Definition: CDCSpaceResols.h:60
Belle2::CDCSpaceResols::setSigmaParamMode
void setSigmaParamMode(unsigned short mode)
Set sigma parameterization mode.
Definition: CDCSpaceResols.h:95
Belle2::CDCSpaceResols::m_sigmaParamMode
unsigned short m_sigmaParamMode
Mode for sigma parameterization.
Definition: CDCSpaceResols.h:391
Belle2::CDCSpaceResols::m_thetaBins
std::vector< array3 > m_thetaBins
theta bins for sigma (rad)
Definition: CDCSpaceResols.h:394
Belle2::CDCSpaceResols::getSigmaID
SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
Get id.
Definition: CDCSpaceResols.h:216
Belle2::CDCSpaceResols::getSigmaParamMode
unsigned short getSigmaParamMode() const
Get parameterization mode.
Definition: CDCSpaceResols.h:204
Belle2::CDCSpaceResols::comp
static bool comp(const array3 &lhs, const array3 &rhs)
Static function for sorting.
Definition: CDCSpaceResols.h:87
Belle2::CDCSpaceResols::getAlphaPoint
float getAlphaPoint(unsigned short i) const
Get i-th alpha-angle point (rad)
Definition: CDCSpaceResols.h:180
Belle2::CDCSpaceResols::getThetaBin
const array3 & getThetaBin(unsigned short i) const
Get i-th theta-angle bin info.
Definition: CDCSpaceResols.h:188
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CDCSpaceResols::CDCSpaceResols
CDCSpaceResols()
Default constructor.
Definition: CDCSpaceResols.h:54
Belle2::CDCSpaceResols::array3
std::array< float, 3 > array3
angle bin info.
Definition: CDCSpaceResols.h:38
Belle2::CDCSpaceResols::m_alphaBins
std::vector< array3 > m_alphaBins
alpha bins for sigma (rad)
Definition: CDCSpaceResols.h:393
Belle2::CDCSpaceResols::ClassDef
ClassDef(CDCSpaceResols, 1)
ClassDef.
Belle2::CDCSpaceResols::c_maxNThetaBins
@ c_maxNThetaBins
max.
Definition: CDCSpaceResols.h:47
Belle2::CDCSpaceResols::m_nSigmaParams
unsigned short m_nSigmaParams
no.
Definition: CDCSpaceResols.h:392
Belle2::CDCSpaceResols::SigmaID
unsigned short SigmaID
id.
Definition: CDCSpaceResols.h:39
Belle2::CDCSpaceResols::getNoOfAlphaBins
unsigned short getNoOfAlphaBins() const
Get no.
Definition: CDCSpaceResols.h:156
Belle2::CDCSpaceResols::getThetaPoint
float getThetaPoint(unsigned short i) const
Get i-th theta-angle point (rad)
Definition: CDCSpaceResols.h:196
Belle2::CDCSpaceResols::setSigmaParams
void setSigmaParams(const SigmaID sigmaID, const std::vector< float > &params)
Set sigma parameters for the specified id.
Definition: CDCSpaceResols.h:103
Belle2::CDCSpaceResols::m_sigmas
std::map< SigmaID, std::vector< float > > m_sigmas
Sigma coefficients for each layer, Left/Right, entrance angle and polar angle.
Definition: CDCSpaceResols.h:396
Belle2::CDCSpaceResols::getNoOfThetaBins
unsigned short getNoOfThetaBins() const
Get no.
Definition: CDCSpaceResols.h:164
Belle2::CDCSpaceResols::c_maxNAlphaBins
@ c_maxNAlphaBins
max.
Definition: CDCSpaceResols.h:46
Belle2::CDCSpaceResols::c_maxNSigmaParams
@ c_maxNSigmaParams
max.
Definition: CDCSpaceResols.h:48
Belle2::CDCSpaceResols::getSigmaParams
const std::vector< float > & getSigmaParams(const SigmaID sigmaID) const
Get sigma parameters for the specified id.
Definition: CDCSpaceResols.h:277
Belle2::CDCSpaceResols::setThetaBin
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
Definition: CDCSpaceResols.h:74
Belle2::CDCSpaceResols::outputToFile
void outputToFile(std::string fileName) const
Output the contents in text file format.
Definition: CDCSpaceResols.h:345
Belle2::CDCSpaceResols::addSigmaParams
void addSigmaParams(const SigmaID sigmaID, const std::vector< float > &delta)
Update parameters for the specified id.
Definition: CDCSpaceResols.h:129