Belle II Software development
CDCXtRelations.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 <framework/logging/Logger.h>
11
12#include <map>
13#include <array>
14#include <iostream>
15#include <fstream>
16#include <iomanip>
17#include <algorithm>
18#include <TObject.h>
19
20namespace Belle2 {
28 class CDCXtRelations: public TObject {
29
30 typedef std::array<float, 3> array3;
31 typedef unsigned short XtID;
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 setXtParamMode(unsigned short mode)
88 {
89 m_xtParamMode = mode;
90 }
91
95 void setXtParams(const XtID xtID, const std::vector<float>& params)
96 {
97 unsigned short nXtParams = params.size();
98
99 if (nXtParams <= c_maxNXtParams) {
100 m_nXtParams = nXtParams;
101 m_xts.insert(std::pair<XtID, std::vector<float>>(xtID, params));
102 // std::cout <<"xtID in setXtParams= " << xtID << std::endl;
103 } else {
104 B2FATAL("The no. of xt params. > limit !");
105 }
106 }
107
111 void setXtParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
112 const std::vector<float>& params)
113 {
114 const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
115 setXtParams(xtID, params);
116 }
117
121 void addXTParams(const XtID xtID, const std::vector<float>& delta)
122 {
123 std::map<XtID, std::vector<float>>::iterator it = m_xts.find(xtID);
124
125 if (it != m_xts.end()) {
126 for (unsigned short i = 0; i < m_nXtParams; ++i) {
127 (it->second)[i] += delta[i];
128 }
129 } else {
130 B2FATAL("Specified xt params not found in addXTParams !");
131 }
132 }
133
137 void addXTParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
138 const std::vector<float>& delta)
139 {
140 const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
141 addXTParams(xtID, delta);
142 }
143
147 void replaceXTParams(const XtID xtID, const std::vector<float>& param)
148 {
149 std::map<XtID, std::vector<float>>::iterator it = m_xts.find(xtID);
150
151 if (it != m_xts.end()) {
152 for (unsigned short i = 0; i < m_nXtParams; ++i) {
153 (it->second)[i] = param[i];
154 }
155 } else {
156 B2FATAL("Specified xt params not found in replaceXTParams !");
157 }
158 }
159
163 void replaceXTParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta,
164 const std::vector<float>& param)
165 {
166 const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
167 replaceXTParams(xtID, param);
168 }
169
170
174 unsigned short getNoOfAlphaBins() const
175 {
176 return m_alphaBins.size();
177 }
178
182 unsigned short getNoOfThetaBins() const
183 {
184 return m_thetaBins.size();
185 }
186
190 const array3& getAlphaBin(unsigned short i) const
191 {
192 return m_alphaBins[i];
193 }
194
198 float getAlphaPoint(unsigned short i) const
199 {
200 return m_alphaBins[i][2];
201 }
202
206 const array3& getThetaBin(unsigned short i) const
207 {
208 return m_thetaBins[i];
209 }
210
214 float getThetaPoint(unsigned short i) const
215 {
216 return m_thetaBins[i][2];
217 }
218
222 unsigned short getXtParamMode() const
223 {
224 return m_xtParamMode;
225 }
226
234 XtID getXtID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
235 {
236 XtID id = iCLayer + 64 * iLR + 128 * iAlpha + 4096 * iTheta;
237 return id;
238 }
239
243 XtID getXtID(unsigned short iCLayer, unsigned short iLR, float alpha, float theta) const
244 {
245 /*
246 unsigned short iTheta = 999;
247 unsigned short ibin = 0;
248 for (std::vector<array3>::const_iterator it = m_thetaBins.begin(); it != m_thetaBins.end(); ++it) {
249 if ((*it)[0] <= theta && theta <= (*it)[1]) {
250 iTheta = ibin;
251 break;
252 }
253 ++ibin;
254 }
255 */
256 unsigned short iTheta = 999;
257 unsigned short ibin = 0;
258 for (auto const& it : m_thetaBins) {
259 if (it[0] <= theta && theta <= it[1]) {
260 iTheta = ibin;
261 break;
262 }
263 ++ibin;
264 }
265 if (iTheta == 999) B2FATAL("Theta bin not found !");
266
267 /*
268 unsigned short iAlpha = 999;
269 ibin = 0;
270 for (std::vector<array3>::const_iterator it = m_alphaBins.begin(); it != m_alphaBins.end(); ++it) {
271 if ((*it)[0] <= alpha && alpha <= (*it)[1]) {
272 iAlpha = ibin;
273 break;
274 }
275 ++ibin;
276 }
277 */
278 unsigned short iAlpha = 999;
279 ibin = 0;
280 for (auto const& it : m_alphaBins) {
281 if (it[0] <= alpha && alpha <= it[1]) {
282 iAlpha = ibin;
283 break;
284 }
285 ++ibin;
286 }
287 if (iAlpha == 999) B2FATAL("Alpha bin not found ! " << alpha);
288
289 return getXtID(iCLayer, iLR, iAlpha, iTheta);
290 }
291
295 const std::vector<float>& getXtParams(const XtID xtID) const
296 {
297 std::map<XtID, std::vector<float>>::const_iterator it = m_xts.find(xtID);
298 if (it != m_xts.end()) {
299 return it->second;
300 } else {
301 B2FATAL("Specified xt params. not found in getXtParams !");
302 }
303 }
304
308 const std::vector<float>& getXtParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha,
309 unsigned short iTheta) const
310 {
311 const XtID xtID = getXtID(iCLayer, iLR, iAlpha, iTheta);
312 // std::cout <<"xtID in getXtParams= " << xtID << std::endl;
313 return getXtParams(xtID);
314 }
315
319 void dump() const
320 {
321 std::cout << " " << std::endl;
322 std::cout << "Contents of xt db" << std::endl;
323 std::cout << "alpha bins" << std::endl;
324
325 const double deg = 180. / M_PI;
326
327 unsigned short nAlphaBins = m_alphaBins.size();
328 for (unsigned short i = 0; i < nAlphaBins; ++i) {
329 std::cout << " " << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << " " << std::endl;
330 }
331
332 std::cout << " " << std::endl;
333 std::cout << "theta bins" << std::endl;
334
335 unsigned short nThetaBins = m_thetaBins.size();
336 for (unsigned short i = 0; i < nThetaBins; ++i) {
337 std::cout << " " << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << " " << std::endl;
338 }
339
340 std::cout << " " << std::endl;
341 std::cout << "coefficients for xt" << std::endl;
342
343 for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
344 for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
345 for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
346 for (unsigned short iLR = 0; iLR < 2; ++iLR) {
347 unsigned short iLRp = abs(iLR - 1);
348 std::cout << iCL << " " << deg* m_thetaBins[iT][2] << " " << deg* m_alphaBins[iA][2] << " " << iLRp;
349 const std::vector<float> params = getXtParams(iCL, iLRp, iA, iT);
350 for (unsigned short i = 0; i < m_nXtParams; ++i) {
351 std::cout << " " << params[i];
352 }
353 std::cout << " " << std::endl;
354 }
355 }
356 }
357 }
358 }
359
363 void outputToFile(std::string fileName) const
364 {
365 std::ofstream fout(fileName);
366
367 if (fout.bad()) {
368 B2ERROR("Specified output file could not be opened!");
369 } else {
370 const double deg = 180. / M_PI;
371
372 unsigned short nAlphaBins = m_alphaBins.size();
373 fout << nAlphaBins << std::endl;
374
375 for (unsigned short i = 0; i < nAlphaBins; ++i) {
376 fout << deg* m_alphaBins[i][0] << " " << deg* m_alphaBins[i][1] << " " << deg* m_alphaBins[i][2] << std::endl;
377 }
378
379 unsigned short nThetaBins = m_thetaBins.size();
380 fout << nThetaBins << std::endl;
381
382 for (unsigned short i = 0; i < nThetaBins; ++i) {
383 fout << deg* m_thetaBins[i][0] << " " << deg* m_thetaBins[i][1] << " " << deg* m_thetaBins[i][2] << std::endl;
384 }
385
386 fout << m_xtParamMode << " " << m_nXtParams << std::endl;
387
388 signed short phiAngle = 0.;
389 for (unsigned short iT = 0; iT < nThetaBins; ++iT) {
390 for (unsigned short iA = 0; iA < nAlphaBins; ++iA) {
391 for (unsigned short iCL = 0; iCL < c_nSLayers; ++iCL) {
392 for (unsigned short iLR = 0; iLR < 2; ++iLR) {
393 unsigned short iLRp = abs(iLR - 1);
394 fout << std::setw(2) << std::right << std::fixed << iCL << " " << std::setw(5) << std::setprecision(
395 1) << deg* m_thetaBins[iT][2] << " " << std::setw(5) << std::right << deg* m_alphaBins[iA][2] << " " << std::setw(
396 1) << std::setprecision(1) << phiAngle << " " << std::setw(1) << iLRp;
397 const std::vector<float> params = getXtParams(iCL, iLRp, iA, iT);
398 for (unsigned short i = 0; i < m_nXtParams; ++i) {
399 fout << " " << std::setw(15) << std::scientific << std::setprecision(8) << params[i];
400 }
401 fout << std::endl;
402 }
403 }
404 }
405 }
406 fout.close();
407 }
408 }
409
410 // ------------- Interface to global Millepede calibration ----------------
412 static unsigned short getGlobalUniqueID() {return 29;}
414 double getGlobalParam(unsigned short xtId, unsigned short i) const
415 {
416 return getXtParams(xtId).at(i);
417 }
419 void setGlobalParam(double value, unsigned short xtId, unsigned short i)
420 {
421
422 std::map<XtID, std::vector<float>>::const_iterator it = m_xts.find(xtId);
423 if (it != m_xts.end()) {
424 std::vector<float> allParams = it->second;
425 allParams.at(i) = value;
426 setXtParams(xtId, allParams);
427 } else {
428 B2INFO("Specified xt params. not found in getXtParams.");
429 std::vector<float> allParams {0., 0., 0., 0., 0., 0., 0., 0.};
430 allParams.at(i) = value;
431 setXtParams(xtId, allParams);
432 }
433 }
435 std::vector<std::pair<unsigned short, unsigned short>> listGlobalParams() const
436 {
437 std::vector<std::pair<unsigned short, unsigned short>> result;
438 for (auto ixt : m_xts) {
439 for (int i = 0; i < 8; ++i) {
440 result.push_back({ixt.first, i});
441 }
442 }
443 return result;
444 }
445 private:
446 unsigned short m_xtParamMode;
447 unsigned short m_nXtParams;
448 std::vector<array3> m_alphaBins;
449 std::vector<array3> m_thetaBins;
450 std::map<XtID, std::vector<float>>
454 };
455
457} // end namespace Belle2
Database object for xt-relations.
static bool comp(const array3 &lhs, const array3 &rhs)
Static function for sorting.
ClassDef(CDCXtRelations, 2)
ClassDef.
void replaceXTParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta, const std::vector< float > &param)
Replace xt parameters for the specified id.
const std::vector< float > & getXtParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
Get xt parameters for the specified id.
void setThetaBin(const array3 &theta)
Set theta-angle bin (rad)
static unsigned short getGlobalUniqueID()
Get global unique id.
void outputToFile(std::string fileName) const
Output the contents in test file format.
XtID getXtID(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta) const
Get xt id.
CDCXtRelations()
Default constructor.
const array3 & getThetaBin(unsigned short i) const
Get i-th theta-angle bin info.
void setGlobalParam(double value, unsigned short xtId, unsigned short i)
Set global parameter for i-th component of the specified xtId.
const array3 & getAlphaBin(unsigned short i) const
Get i-th alpha-angle bin info.
unsigned short getNoOfAlphaBins() const
Get no.
unsigned short m_xtParamMode
Mode for xt parameterization.
unsigned short m_nXtParams
no.
std::map< XtID, std::vector< float > > m_xts
XT-relation coefficients for each layer, Left/Right, entrance angle and polar angle.
XtID getXtID(unsigned short iCLayer, unsigned short iLR, float alpha, float theta) const
Get xt id.
void setXtParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta, const std::vector< float > &params)
Set xt parameters for the specified id.
const std::vector< float > & getXtParams(const XtID xtID) const
Get xt parameters for the specified id.
unsigned short XtID
id.
void replaceXTParams(const XtID xtID, const std::vector< float > &param)
Replace xt parameters for the specified id.
float getAlphaPoint(unsigned short i) const
Get i-th alpha-angle point (rad)
unsigned short getNoOfThetaBins() const
Get no.
std::vector< std::pair< unsigned short, unsigned short > > listGlobalParams() const
list stored global parameters TODO FIXME CDC not ready
void addXTParams(unsigned short iCLayer, unsigned short iLR, unsigned short iAlpha, unsigned short iTheta, const std::vector< float > &delta)
Update xt parameters for the specified id.
unsigned short getXtParamMode() const
Get xt parameterization mode.
void setXtParamMode(unsigned short mode)
Set xt parameterization mode.
void setXtParams(const XtID xtID, const std::vector< float > &params)
Set xt parameters for the specified id.
std::vector< array3 > m_alphaBins
alpha bins for xt (rad)
std::vector< array3 > m_thetaBins
theta bins for xt (rad)
void addXTParams(const XtID xtID, const std::vector< float > &delta)
Update xt parameters for the specified id.
float getThetaPoint(unsigned short i) const
Get i-th theta-angle point (rad)
void setAlphaBin(const array3 &alpha)
Set alpha-angle bin (rad)
double getGlobalParam(unsigned short xtId, unsigned short i) const
Get global parameter for i-th component of the specified xtId.
std::array< float, 3 > array3
angle bin info.
void dump() const
Print all contents.
Abstract base class for different kinds of events.