Belle II Software  release-05-01-25
CDCXtRelations.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 <framework/logging/Logger.h>
13 
14 #include <map>
15 #include <array>
16 #include <iostream>
17 #include <fstream>
18 #include <iomanip>
19 #include <algorithm>
20 #include <TObject.h>
21 
22 namespace Belle2 {
30  class CDCXtRelations: public TObject {
31 
32  typedef std::array<float, 3> array3;
33  typedef unsigned short XtID;
35  public:
39  enum {c_nSLayers = 56,
42  c_maxNXtParams = 8
43  };
44 
49  {}
50 
54  void setAlphaBin(const array3& alpha)
55  {
56  if (m_alphaBins.size() <= c_maxNAlphaBins) {
57  m_alphaBins.push_back(alpha);
58  sort(m_alphaBins.begin(), m_alphaBins.end(), comp);
59  } else {
60  // std::cout<< m_alphaBins.size() <<" "<< c_maxNAlphaBins <<std::endl;
61  B2FATAL("The no. of alpha bins > limit !");
62  }
63  }
64 
68  void setThetaBin(const array3& theta)
69  {
70  if (m_thetaBins.size() <= c_maxNThetaBins) {
71  m_thetaBins.push_back(theta);
72  sort(m_thetaBins.begin(), m_thetaBins.end(), comp);
73  } else {
74  B2FATAL("The no. of theta bins > limit !");
75  }
76  }
77 
81  static bool comp(const array3& lhs, const array3& rhs)
82  {
83  return lhs[0] < rhs[0];
84  }
85 
89  void setXtParamMode(unsigned short mode)
90  {
91  m_xtParamMode = mode;
92  }
93 
97  void setXtParams(const XtID xtID, const std::vector<float>& params)
98  {
99  unsigned short nXtParams = params.size();
100 
101  if (nXtParams <= c_maxNXtParams) {
102  m_nXtParams = nXtParams;
103  m_xts.insert(std::pair<XtID, std::vector<float>>(xtID, params));
104  // std::cout <<"xtID in setXtParams= " << xtID << std::endl;
105  } else {
106  B2FATAL("The no. of xt params. > limit !");
107  }
108  }
109 
113  void setXtParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
114  const std::vector<float>& params)
115  {
116  const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
117  setXtParams(xtID, params);
118  }
119 
123  void addXTParams(const XtID xtID, const std::vector<float>& delta)
124  {
125  std::map<XtID, std::vector<float>>::iterator it = m_xts.find(xtID);
126 
127  if (it != m_xts.end()) {
128  for (unsigned short i = 0; i < m_nXtParams; ++i) {
129  (it->second)[i] += delta[i];
130  }
131  } else {
132  B2FATAL("Specified xt params not found in addXTParams !");
133  }
134  }
135 
139  void addXTParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
140  const std::vector<float>& delta)
141  {
142  const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
143  addXTParams(xtID, delta);
144  }
145 
149  void replaceXTParams(const XtID xtID, const std::vector<float>& param)
150  {
151  std::map<XtID, std::vector<float>>::iterator it = m_xts.find(xtID);
152 
153  if (it != m_xts.end()) {
154  for (unsigned short i = 0; i < m_nXtParams; ++i) {
155  (it->second)[i] = param[i];
156  }
157  } else {
158  B2FATAL("Specified xt params not found in replaceXTParams !");
159  }
160  }
161 
165  void replaceXTParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
166  const std::vector<float>& param)
167  {
168  const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
169  replaceXTParams(xtID, param);
170  }
171 
172 
176  unsigned short getNoOfAlphaBins() const
177  {
178  return m_alphaBins.size();
179  }
180 
184  unsigned short getNoOfThetaBins() const
185  {
186  return m_thetaBins.size();
187  }
188 
192  const array3& getAlphaBin(unsigned short i) const
193  {
194  return m_alphaBins[i];
195  }
196 
200  float getAlphaPoint(unsigned short i) const
201  {
202  return m_alphaBins[i][2];
203  }
204 
208  const array3& getThetaBin(unsigned short i) const
209  {
210  return m_thetaBins[i];
211  }
212 
216  float getThetaPoint(unsigned short i) const
217  {
218  return m_thetaBins[i][2];
219  }
220 
224  unsigned short getXtParamMode() const
225  {
226  return m_xtParamMode;
227  }
228 
236  XtID getXtID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
237  {
238  XtID id = iCLayer + 64 * iLR + 128 * iAlpha + 4096 * iTheta;
239  return id;
240  }
241 
245  XtID getXtID(unsigned short iCLayer, unsigned short iLR, float alpha, float theta) const
246  {
247  /*
248  unsigned short iTheta = 999;
249  unsigned short ibin = 0;
250  for (std::vector<array3>::const_iterator it = m_thetaBins.begin(); it != m_thetaBins.end(); ++it) {
251  if ((*it)[0] <= theta && theta <= (*it)[1]) {
252  iTheta = ibin;
253  break;
254  }
255  ++ibin;
256  }
257  */
258  unsigned short iTheta = 999;
259  unsigned short ibin = 0;
260  for (auto const& it : m_thetaBins) {
261  if (it[0] <= theta && theta <= it[1]) {
262  iTheta = ibin;
263  break;
264  }
265  ++ibin;
266  }
267  if (iTheta == 999) B2FATAL("Theta bin not found !");
268 
269  /*
270  unsigned short iAlpha = 999;
271  ibin = 0;
272  for (std::vector<array3>::const_iterator it = m_alphaBins.begin(); it != m_alphaBins.end(); ++it) {
273  if ((*it)[0] <= alpha && alpha <= (*it)[1]) {
274  iAlpha = ibin;
275  break;
276  }
277  ++ibin;
278  }
279  */
280  unsigned short iAlpha = 999;
281  ibin = 0;
282  for (auto const& it : m_alphaBins) {
283  if (it[0] <= alpha && alpha <= it[1]) {
284  iAlpha = ibin;
285  break;
286  }
287  ++ibin;
288  }
289  if (iAlpha == 999) B2FATAL("Alpha bin not found !");
290 
291  return getXtID(iCLayer, iLR, iAlpha, iTheta);
292  }
293 
297  const std::vector<float>& getXtParams(const XtID xtID) const
298  {
299  std::map<XtID, std::vector<float>>::const_iterator it = m_xts.find(xtID);
300  if (it != m_xts.end()) {
301  return it->second;
302  } else {
303  B2FATAL("Specified xt params. not found in getXtParams !");
304  }
305  }
306 
310  const std::vector<float>& getXtParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha,
311  unsigned short iTheta) const
312  {
313  const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
314  // std::cout <<"xtID in getXtParams= " << xtID << std::endl;
315  return getXtParams(xtID);
316  }
317 
321  void dump() const
322  {
323  std::cout << " " << std::endl;
324  std::cout << "Contents of xt db" << std::endl;
325  std::cout << "alpha bins" << std::endl;
326 
327  const double deg = 180. / M_PI;
328 
329  unsigned short nAlphaBins = m_alphaBins.size();
330  for (unsigned short i = 0; i < nAlphaBins; ++i) {
331  std::cout << " " << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << " " << std::endl;
332  }
333 
334  std::cout << " " << std::endl;
335  std::cout << "theta bins" << std::endl;
336 
337  unsigned short nThetaBins = m_thetaBins.size();
338  for (unsigned short i = 0; i < nThetaBins; ++i) {
339  std::cout << " " << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << " " << std::endl;
340  }
341 
342  std::cout << " " << std::endl;
343  std::cout << "coefficients for xt" << std::endl;
344 
345  for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
346  for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
347  for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
348  for (unsigned short iLR = 0; iLR < 2; ++iLR) {
349  unsigned short iLRp = abs(iLR - 1);
350  std::cout << iCL << " " << deg* m_thetaBins[iT][2] << " " << deg* m_alphaBins[iA][2] << " " << iLRp;
351  const std::vector<float> params = getXtParams(iCL, iLRp, iA, iT);
352  for (unsigned short i = 0; i < m_nXtParams; ++i) {
353  std::cout << " " << params[i];
354  }
355  std::cout << " " << std::endl;
356  }
357  }
358  }
359  }
360  }
361 
365  void outputToFile(std::string fileName) const
366  {
367  std::ofstream fout(fileName);
368 
369  if (fout.bad()) {
370  B2ERROR("Specified output file could not be opened!");
371  } else {
372  const double deg = 180. / M_PI;
373 
374  unsigned short nAlphaBins = m_alphaBins.size();
375  fout << nAlphaBins << std::endl;
376 
377  for (unsigned short i = 0; i < nAlphaBins; ++i) {
378  fout << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << std::endl;
379  }
380 
381  unsigned short nThetaBins = m_thetaBins.size();
382  fout << nThetaBins << std::endl;
383 
384  for (unsigned short i = 0; i < nThetaBins; ++i) {
385  fout << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << std::endl;
386  }
387 
388  fout << m_xtParamMode << " " << m_nXtParams << std::endl;
389 
390  signed short phiAngle = 0.;
391  for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
392  for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
393  for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
394  for (unsigned short iLR = 0; iLR < 2; ++iLR) {
395  unsigned short iLRp = abs(iLR - 1);
396  fout << std::setw(2) << std::right << std::fixed << iCL << " " << std::setw(5) << std::setprecision(
397  1) << deg* m_thetaBins[iT][2] << " " << std::setw(5) << std::right << deg* m_alphaBins[iA][2] << " " << std::setw(
398  1) << std::setprecision(1) << phiAngle << " " << std::setw(1) << iLRp;
399  const std::vector<float> params = getXtParams(iCL, iLRp, iA, iT);
400  for (unsigned short i = 0; i < m_nXtParams; ++i) {
401  fout << " " << std::setw(15) << std::scientific << std::setprecision(8) << params[i];
402  }
403  fout << std::endl;
404  }
405  }
406  }
407  }
408  fout.close();
409  }
410  }
411 
412  // ------------- Interface to global Millepede calibration ----------------
414  static unsigned short getGlobalUniqueID() {return 29;}
416  double getGlobalParam(unsigned short xtId, unsigned short xtParam) const
417  {
418  return getXtParams(xtId).at(xtParam);
419  }
421  void setGlobalParam(double value, unsigned short xtId, unsigned short xtParam)
422  {
423  std::vector<float> allParams = getXtParams(xtId);
424  allParams.at(xtParam) = value;
425  setXtParams(xtId, allParams);
426  }
428  std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() const
429  {
430  return {};
431  }
432  private:
433  unsigned short m_xtParamMode;
434  unsigned short m_nXtParams;
435  std::vector<array3> m_alphaBins;
436  std::vector<array3> m_thetaBins;
437  std::map<XtID, std::vector<float>>
438  m_xts;
441  };
442 
444 } // end namespace Belle2
Belle2::CDCXtRelations
Database object for xt-relations.
Definition: CDCXtRelations.h:38
Belle2::CDCXtRelations::getThetaPoint
float getThetaPoint(unsigned short i) const
Get i-th theta-angle point (rad)
Definition: CDCXtRelations.h:224
Belle2::CDCXtRelations::CDCXtRelations
CDCXtRelations()
Default constructor.
Definition: CDCXtRelations.h:56
Belle2::CDCXtRelations::c_maxNThetaBins
@ c_maxNThetaBins
max.
Definition: CDCXtRelations.h:49
Belle2::CDCXtRelations::setThetaBin
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
Definition: CDCXtRelations.h:76
Belle2::CDCXtRelations::XtID
unsigned short XtID
id.
Definition: CDCXtRelations.h:41
Belle2::CDCXtRelations::setAlphaBin
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
Definition: CDCXtRelations.h:62
Belle2::CDCXtRelations::getThetaBin
const array3 & getThetaBin(unsigned short i) const
Get i-th theta-angle bin info.
Definition: CDCXtRelations.h:216
Belle2::CDCXtRelations::addXTParams
void addXTParams(const XtID xtID, const std::vector< float > &delta)
Update xt parameters for the specified id.
Definition: CDCXtRelations.h:131
Belle2::CDCXtRelations::getGlobalUniqueID
static unsigned short getGlobalUniqueID()
Get global unique id.
Definition: CDCXtRelations.h:422
Belle2::CDCXtRelations::m_xtParamMode
unsigned short m_xtParamMode
Mode for xt parameterization.
Definition: CDCXtRelations.h:441
Belle2::CDCXtRelations::getXtID
XtID getXtID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
Get xt id.
Definition: CDCXtRelations.h:244
Belle2::CDCXtRelations::c_nSLayers
@ c_nSLayers
no.
Definition: CDCXtRelations.h:47
Belle2::CDCXtRelations::getGlobalParam
double getGlobalParam(unsigned short xtId, unsigned short xtParam) const
Get global parameter FIXME does nothing because CDC is not ready.
Definition: CDCXtRelations.h:424
Belle2::CDCXtRelations::m_thetaBins
std::vector< array3 > m_thetaBins
theta bins for xt (rad)
Definition: CDCXtRelations.h:444
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::CDCXtRelations::comp
static bool comp(const array3 &lhs, const array3 &rhs)
Static function for sorting.
Definition: CDCXtRelations.h:89
Belle2::CDCXtRelations::c_maxNXtParams
@ c_maxNXtParams
max.
Definition: CDCXtRelations.h:50
Belle2::CDCXtRelations::listGlobalParams
std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams() const
list stored global parameters TODO FIXME CDC not ready
Definition: CDCXtRelations.h:436
Belle2::CDCXtRelations::array3
std::array< float, 3 > array3
angle bin info.
Definition: CDCXtRelations.h:40
Belle2::CDCXtRelations::getXtParamMode
unsigned short getXtParamMode() const
Get xt parameterization mode.
Definition: CDCXtRelations.h:232
Belle2::CDCXtRelations::setXtParamMode
void setXtParamMode(unsigned short mode)
Set xt parameterization mode.
Definition: CDCXtRelations.h:97
Belle2::CDCXtRelations::ClassDef
ClassDef(CDCXtRelations, 2)
ClassDef.
Belle2::CDCXtRelations::setXtParams
void setXtParams(const XtID xtID, const std::vector< float > &params)
Set xt parameters for the specified id.
Definition: CDCXtRelations.h:105
Belle2::CDCXtRelations::getXtParams
const std::vector< float > & getXtParams(const XtID xtID) const
Get xt parameters for the specified id.
Definition: CDCXtRelations.h:305
Belle2::CDCXtRelations::setGlobalParam
void setGlobalParam(double value, unsigned short xtId, unsigned short xtParam)
Set global parameter FIXME does nothing because CDC is not ready.
Definition: CDCXtRelations.h:429
Belle2::CDCXtRelations::getNoOfAlphaBins
unsigned short getNoOfAlphaBins() const
Get no.
Definition: CDCXtRelations.h:184
Belle2::CDCXtRelations::replaceXTParams
void replaceXTParams(const XtID xtID, const std::vector< float > &param)
Replace xt parameters for the specified id.
Definition: CDCXtRelations.h:157
Belle2::CDCXtRelations::m_nXtParams
unsigned short m_nXtParams
no.
Definition: CDCXtRelations.h:442
Belle2::CDCXtRelations::m_xts
std::map< XtID, std::vector< float > > m_xts
XT-relation coefficients for each layer, Left/Right, entrance angle and polar angle.
Definition: CDCXtRelations.h:446
Belle2::CDCXtRelations::c_maxNAlphaBins
@ c_maxNAlphaBins
max.
Definition: CDCXtRelations.h:48
Belle2::CDCXtRelations::getAlphaPoint
float getAlphaPoint(unsigned short i) const
Get i-th alpha-angle point (rad)
Definition: CDCXtRelations.h:208
Belle2::CDCXtRelations::getAlphaBin
const array3 & getAlphaBin(unsigned short i) const
Get i-th alpha-angle bin info.
Definition: CDCXtRelations.h:200
Belle2::CDCXtRelations::dump
void dump() const
Print all contents.
Definition: CDCXtRelations.h:329
Belle2::CDCXtRelations::outputToFile
void outputToFile(std::string fileName) const
Output the contents in test file format.
Definition: CDCXtRelations.h:373
Belle2::CDCXtRelations::m_alphaBins
std::vector< array3 > m_alphaBins
alpha bins for xt (rad)
Definition: CDCXtRelations.h:443
Belle2::CDCXtRelations::getNoOfThetaBins
unsigned short getNoOfThetaBins() const
Get no.
Definition: CDCXtRelations.h:192