Belle II Software  release-08-01-10
CDCSpaceResols.h
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 #pragma once
9 
10 #include <map>
11 #include <array>
12 #include <iostream>
13 #include <fstream>
14 #include <iomanip>
15 #include <algorithm>
16 #include <TObject.h>
17 
18 namespace Belle2 {
26  class CDCSpaceResols: public TObject {
27 
28  typedef std::array<float, 3> array3;
29  typedef unsigned short SigmaID;
31  public:
35  enum {c_nSLayers = 56,
39  };
40 
45  {}
46 
50  void setAlphaBin(const array3& alpha)
51  {
52  if (m_alphaBins.size() <= c_maxNAlphaBins) {
53  m_alphaBins.push_back(alpha);
54  sort(m_alphaBins.begin(), m_alphaBins.end(), comp);
55  } else {
56  // std::cout<< m_alphaBins.size() <<" "<< c_maxNAlphaBins <<std::endl;
57  B2FATAL("The no. of alpha bins > limit !");
58  }
59  }
60 
64  void setThetaBin(const array3& theta)
65  {
66  if (m_thetaBins.size() <= c_maxNThetaBins) {
67  m_thetaBins.push_back(theta);
68  sort(m_thetaBins.begin(), m_thetaBins.end(), comp);
69  } else {
70  B2FATAL("The no. of theta bins > limit !");
71  }
72  }
73 
77  static bool comp(const array3& lhs, const array3& rhs)
78  {
79  return lhs[0] < rhs[0];
80  }
81 
85  void setSigmaParamMode(unsigned short mode)
86  {
87  m_sigmaParamMode = mode;
88  }
89 
93  void setMaxSpaceResol(float sigma)
94  {
95  m_maxSpaceResol = sigma;
96  }
97 
101  void setSigmaParams(const SigmaID sigmaID, const std::vector<float>& params)
102  {
103  unsigned short nSigmaPars = params.size();
104 
105  if (nSigmaPars <= c_maxNSigmaParams) {
106  m_nSigmaParams = nSigmaPars;
107  m_sigmas.insert(std::pair<SigmaID, std::vector<float>>(sigmaID, params));
108  // std::cout <<"sigmaID in setSigmaParams= " << sigmaID << std::endl;
109  } else {
110  B2FATAL("The no. of sigma params. > limit !");
111  }
112  }
113 
117  void setSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
118  const std::vector<float>& params)
119  {
120  const SigmaID sigmaID = getSigmaID(iCLayer, iLR, iAlpha, iTheta);
121  setSigmaParams(sigmaID, params);
122  }
123 
127  void addSigmaParams(const SigmaID sigmaID, const std::vector<float>& delta)
128  {
129  std::map<SigmaID, std::vector<float>>::iterator it = m_sigmas.find(sigmaID);
130 
131  if (it != m_sigmas.end()) {
132  for (unsigned short i = 0; i < m_nSigmaParams; ++i) {
133  (it->second)[i] += delta[i];
134  }
135  } else {
136  B2FATAL("Specified params not found in addSigmaParams !");
137  }
138  }
139 
143  void addSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
144  const std::vector<float>& delta)
145  {
146  const SigmaID sigmaID = getSigmaID(iCLayer, iLR, iAlpha, iTheta);
147  addSigmaParams(sigmaID, delta);
148  }
149 
150 
154  unsigned short getNoOfAlphaBins() const
155  {
156  return m_alphaBins.size();
157  }
158 
162  unsigned short getNoOfThetaBins() const
163  {
164  return m_thetaBins.size();
165  }
166 
170  const array3& getAlphaBin(unsigned short i) const
171  {
172  return m_alphaBins[i];
173  }
174 
178  float getAlphaPoint(unsigned short i) const
179  {
180  return m_alphaBins[i][2];
181  }
182 
186  const array3& getThetaBin(unsigned short i) const
187  {
188  return m_thetaBins[i];
189  }
190 
194  float getThetaPoint(unsigned short i) const
195  {
196  return m_thetaBins[i][2];
197  }
198 
202  unsigned short getSigmaParamMode() const
203  {
204  return m_sigmaParamMode;
205  }
206 
210  float getMaxSpaceResol() const
211  {
212  return m_maxSpaceResol;
213  }
214 
222  SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
223  {
224  SigmaID id = iCLayer + 64 * iLR + 128 * iAlpha + 4096 * iTheta;
225  return id;
226  }
227 
231  SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, float alpha, float theta) const
232  {
233  /*
234  unsigned short iTheta = 999;
235  unsigned short ibin = 0;
236  for (std::vector<array3>::const_iterator it = m_thetaBins.begin(); it != m_thetaBins.end(); ++it) {
237  if ((*it)[0] <= theta && theta <= (*it)[1]) {
238  iTheta = ibin;
239  break;
240  }
241  ++ibin;
242  }
243  */
244  unsigned short iTheta = 999;
245  unsigned short ibin = 0;
246  for (auto const& it : m_thetaBins) {
247  if (it[0] <= theta && theta <= it[1]) {
248  iTheta = ibin;
249  break;
250  }
251  ++ibin;
252  }
253  if (iTheta == 999) B2FATAL("Theta bin not found !");
254 
255  /*
256  unsigned short iAlpha = 999;
257  ibin = 0;
258  for (std::vector<array3>::const_iterator it = m_alphaBins.begin(); it != m_alphaBins.end(); ++it) {
259  if ((*it)[0] <= alpha && alpha <= (*it)[1]) {
260  iAlpha = ibin;
261  break;
262  }
263  ++ibin;
264  }
265  */
266  unsigned short iAlpha = 999;
267  ibin = 0;
268  for (auto const& it : m_alphaBins) {
269  if (it[0] <= alpha && alpha <= it[1]) {
270  iAlpha = ibin;
271  break;
272  }
273  ++ibin;
274  }
275  if (iAlpha == 999) B2FATAL("Alpha bin not found !");
276 
277  return getSigmaID(iCLayer, iLR, iAlpha, iTheta);
278  }
279 
283  const std::vector<float>& getSigmaParams(const SigmaID sigmaID) const
284  {
285  std::map<SigmaID, std::vector<float>>::const_iterator it = m_sigmas.find(sigmaID);
286  if (it != m_sigmas.end()) {
287  return it->second;
288  } else {
289  B2FATAL("Specified params. not found in getSigmaParams !");
290  }
291  }
292 
296  const std::vector<float>& getSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha,
297  unsigned short iTheta) const
298  {
299  const SigmaID sigmaID = getSigmaID(iCLayer, iLR, iAlpha, iTheta);
300  // std::cout <<"sigmaID in getSigmaParams= " << sigmaID << std::endl;
301  return getSigmaParams(sigmaID);
302  }
303 
307  void dump() const
308  {
309  std::cout << " " << std::endl;
310  std::cout << "Contents of sigma db" << std::endl;
311  std::cout << "alpha bins" << std::endl;
312 
313  const double deg = 180. / M_PI;
314 
315  unsigned short nAlphaBins = m_alphaBins.size();
316  for (unsigned short i = 0; i < nAlphaBins; ++i) {
317  std::cout << " " << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << " " << std::endl;
318  }
319 
320  std::cout << " " << std::endl;
321  std::cout << "theta bins" << std::endl;
322 
323  unsigned short nThetaBins = m_thetaBins.size();
324  for (unsigned short i = 0; i < nThetaBins; ++i) {
325  std::cout << " " << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << " " << std::endl;
326  }
327 
328  std::cout << " " << std::endl;
329  std::cout << "coefficients for sigma" << std::endl;
330 
331  for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
332  for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
333  for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
334  for (unsigned short iLR = 0; iLR < 2; ++iLR) {
335  unsigned short iLRp = abs(iLR - 1);
336  std::cout << iCL << " " << deg* m_thetaBins[iT][2] << " " << deg* m_alphaBins[iA][2] << " " << iLRp;
337  const std::vector<float> params = getSigmaParams(iCL, iLRp, iA, iT);
338  for (unsigned short i = 0; i < m_nSigmaParams; ++i) {
339  std::cout << " " << params[i];
340  }
341  std::cout << " " << std::endl;
342  }
343  }
344  }
345  }
346  }
347 
351  void outputToFile(std::string fileName) const
352  {
353  std::ofstream fout(fileName);
354 
355  if (fout.bad()) {
356  B2ERROR("Specified output file could not be opened!");
357  } else {
358  const double deg = 180. / M_PI;
359 
360  unsigned short nAlphaBins = m_alphaBins.size();
361  fout << nAlphaBins << std::endl;
362 
363  for (unsigned short i = 0; i < nAlphaBins; ++i) {
364  fout << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << std::endl;
365  }
366 
367  unsigned short nThetaBins = m_thetaBins.size();
368  fout << nThetaBins << std::endl;
369 
370  for (unsigned short i = 0; i < nThetaBins; ++i) {
371  fout << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << std::endl;
372  }
373 
374  fout << m_sigmaParamMode << " " << m_nSigmaParams << std::endl;
375 
376  fout << m_maxSpaceResol << std::endl;
377 
378  for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
379  for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
380  for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
381  for (unsigned short iLR = 0; iLR < 2; ++iLR) {
382  unsigned short iLRp = abs(iLR - 1);
383  fout << std::setw(2) << std::right << std::fixed << iCL << " " << std::setw(5) << std::setprecision(
384  1) << deg* m_thetaBins[iT][2] << " " << std::setw(5) << std::right << deg* m_alphaBins[iA][2] << " " << std::setw(1) << iLRp;
385  const std::vector<float> params = getSigmaParams(iCL, iLRp, iA, iT);
386  for (unsigned short i = 0; i < m_nSigmaParams; ++i) {
387  fout << " " << std::setw(15) << std::scientific << std::setprecision(8) << params[i];
388  }
389  fout << std::endl;
390  }
391  }
392  }
393  }
394  fout.close();
395  }
396  }
397 
398  private:
399  unsigned short m_sigmaParamMode;
400  unsigned short m_nSigmaParams;
401  float m_maxSpaceResol = 0.07;
402  std::vector<array3> m_alphaBins;
403  std::vector<array3> m_thetaBins;
404  std::map<SigmaID, std::vector<float>>
408  // Version histroy:
409  // v1: original.
410  // v2: added m_maxSpaceResol and related functions.
411  // m_maxSpaceResol for an object with ver=1 is set in linkdef.h.
412  };
413 
415 } // end namespace Belle2
Database object for space resolutions.
void setMaxSpaceResol(float sigma)
Set max.
static bool comp(const array3 &lhs, const array3 &rhs)
Static function for sorting.
void setSigmaParams(const SigmaID sigmaID, const std::vector< float > &params)
Set sigma parameters for the specified id.
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
void outputToFile(std::string fileName) const
Output the contents in text file format.
std::map< SigmaID, std::vector< float > > m_sigmas
Sigma coefficients for each layer, Left/Right, entrance angle and polar angle.
unsigned short m_nSigmaParams
no.
const std::vector< float > & getSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
Get sigma parameters for the specified id.
unsigned short getNoOfAlphaBins() const
Get no.
unsigned short SigmaID
id.
unsigned short m_sigmaParamMode
Mode for sigma parameterization.
const std::vector< float > & getSigmaParams(const SigmaID sigmaID) const
Get sigma parameters for the specified id.
SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, float alpha, float theta) const
Get id.
void setSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta, const std::vector< float > &params)
Set sigma parameters for the specified id.
void addSigmaParams(const SigmaID sigmaID, const std::vector< float > &delta)
Update parameters for the specified id.
float getAlphaPoint(unsigned short i) const
Get i-th alpha-angle point (rad)
void addSigmaParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta, const std::vector< float > &delta)
Update parameters for the specified id.
unsigned short getNoOfThetaBins() const
Get no.
SigmaID getSigmaID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
Get id.
float getMaxSpaceResol() const
Get max.
ClassDef(CDCSpaceResols, 2)
ClassDef.
const array3 & getAlphaBin(unsigned short i) const
Get i-th alpha-angle bin info.
unsigned short getSigmaParamMode() const
Get parameterization mode.
std::vector< array3 > m_alphaBins
alpha bins for sigma (rad)
std::vector< array3 > m_thetaBins
theta bins for sigma (rad)
float getThetaPoint(unsigned short i) const
Get i-th theta-angle point (rad)
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
const array3 & getThetaBin(unsigned short i) const
Get i-th theta-angle bin info.
CDCSpaceResols()
Default constructor.
void setSigmaParamMode(unsigned short mode)
Set sigma parameterization mode.
std::array< float, 3 > array3
angle bin info.
void dump() const
Print all contents.
Abstract base class for different kinds of events.