Belle II Software development
GlobalDerivatives.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#include <alignment/GlobalDerivatives.h>
10
11namespace Belle2 {
16 namespace alignment {
17 GlobalDerivatives::GlobalDerivatives(const std::pair< std::vector< int >, TMatrixD >& globals)
18 {
19 m_globals.second.ResizeTo(globals.second);
20 m_globals.first = globals.first;
21 m_globals.second = globals.second;
22 }
23 GlobalDerivatives::GlobalDerivatives(const std::vector< int >& labels, const TMatrixD& derivs)
24 {
25 m_globals.second.ResizeTo(derivs);
26 m_globals.first = labels;
27 m_globals.second = derivs;
28 }
29 void GlobalDerivatives::add(const std::pair<std::vector<int>, TMatrixD>& globals)
30 {
31 if (globals.first.empty())
32 return;
33
34 auto& main = m_globals;
35
36 // Create composed matrix of derivatives
37 //TODO: check main and globals matrix has the same number of rows
38 TMatrixD allDerivatives(main.second.GetNrows(), main.second.GetNcols() + globals.second.GetNcols());
39 allDerivatives.Zero();
40 allDerivatives.SetSub(0, 0, main.second);
41 allDerivatives.SetSub(0, main.second.GetNcols(), globals.second);
42
43 // Merge labels
44 main.first.insert(main.first.end(), globals.first.begin(), globals.first.end());
45 // Update matrix
46 main.second.ResizeTo(allDerivatives);
47 main.second = allDerivatives;
48
49 }
50 void GlobalDerivatives::add(int paramLabel, std::vector< double > dResiduals_dParam)
51 {
52 int nRows = m_globals.second.GetNrows();
53 int nCols = m_globals.second.GetNcols();
54
55 m_globals.first.push_back(paramLabel);
56 m_globals.second.ResizeTo(nRows, nCols + 1);
57 for (int iRow = 0; iRow < nRows; ++iRow) {
58 m_globals.second(iRow, nCols) = dResiduals_dParam.at(iRow);
59 }
60 }
61 void GlobalDerivatives::add(int paramLabel, double drudp)
62 {
63 std::vector<double> dResiduals_dParam(m_globals.second.GetNrows(), 0.);
64 dResiduals_dParam.at(0) = drudp;
65 add(paramLabel, dResiduals_dParam);
66 }
67 std::pair< std::vector< int >, TMatrixD > GlobalDerivatives::passGlobals(std::pair< std::vector< int >, TMatrixD > globals)
68 {
69 TMatrixD newMatrix(globals.second.GetNrows(), 0);
70 std::vector<int> newLabels;
71
72 for (unsigned int iOldCol = 0; iOldCol < globals.first.size(); ++iOldCol) {
73 auto label = globals.first.at(iOldCol);
74 if (label == 0)
75 continue;
76
77 newLabels.push_back(label);
78 newMatrix.ResizeTo(globals.second.GetNrows(), newMatrix.GetNcols() + 1);
79 for (int iRow = 0; iRow < globals.second.GetNrows(); ++iRow) {
80 newMatrix(iRow, newMatrix.GetNcols() - 1) = globals.second(iRow, iOldCol);
81 }
82 }
83 return {newLabels, newMatrix};
84 }
85 }
87}
static std::pair< std::vector< int >, TMatrixD > passGlobals(std::pair< std::vector< int >, TMatrixD > globals)
Static convenient function to remove columns with zero labels (make error in Pede btw....
GlobalDerivatives(int dim=2)
Constructor for empty derivative matrix and label vector.
void add(const std::pair< std::vector< int >, TMatrixD > &globals)
Add another set of global labels and derivatives.
std::pair< std::vector< int >, TMatrixD > m_globals
The global labels and derivatives matrix.
Abstract base class for different kinds of events.