Belle II Software  release-05-01-25
GlobalDerivatives.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Tadeas Bilka *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <alignment/GlobalDerivatives.h>
12 
13 namespace Belle2 {
18  namespace alignment {
19  GlobalDerivatives::GlobalDerivatives(const std::pair< std::vector< int >, TMatrixD >& globals)
20  {
21  m_globals.second.ResizeTo(globals.second);
22  m_globals.first = globals.first;
23  m_globals.second = globals.second;
24  }
25  GlobalDerivatives::GlobalDerivatives(const std::vector< int >& labels, const TMatrixD& derivs)
26  {
27  m_globals.second.ResizeTo(derivs);
28  m_globals.first = labels;
29  m_globals.second = derivs;
30  }
31  void GlobalDerivatives::add(const std::pair<std::vector<int>, TMatrixD>& globals)
32  {
33  if (globals.first.empty())
34  return;
35 
36  auto& main = m_globals;
37 
38  // Create composed matrix of derivatives
39  //TODO: check main and globals matrix has the same number of rows
40  TMatrixD allDerivatives(main.second.GetNrows(), main.second.GetNcols() + globals.second.GetNcols());
41  allDerivatives.Zero();
42  allDerivatives.SetSub(0, 0, main.second);
43  allDerivatives.SetSub(0, main.second.GetNcols(), globals.second);
44 
45  // Merge labels
46  main.first.insert(main.first.end(), globals.first.begin(), globals.first.end());
47  // Update matrix
48  main.second.ResizeTo(allDerivatives);
49  main.second = allDerivatives;
50 
51  }
52  void GlobalDerivatives::add(int paramLabel, std::vector< double > dResiduals_dParam)
53  {
54  int nRows = m_globals.second.GetNrows();
55  int nCols = m_globals.second.GetNcols();
56 
57  m_globals.first.push_back(paramLabel);
58  m_globals.second.ResizeTo(nRows, nCols + 1);
59  for (int iRow = 0; iRow < nRows; ++iRow) {
60  m_globals.second(iRow, nCols) = dResiduals_dParam.at(iRow);
61  }
62  }
63  void GlobalDerivatives::add(int paramLabel, double drudp)
64  {
65  std::vector<double> dResiduals_dParam(m_globals.second.GetNrows(), 0.);
66  dResiduals_dParam.at(0) = drudp;
67  add(paramLabel, dResiduals_dParam);
68  }
69  std::pair< std::vector< int >, TMatrixD > GlobalDerivatives::passGlobals(std::pair< std::vector< int >, TMatrixD > globals)
70  {
71  TMatrixD newMatrix(globals.second.GetNrows(), 0);
72  std::vector<int> newLabels;
73 
74  for (unsigned int iOldCol = 0; iOldCol < globals.first.size(); ++iOldCol) {
75  auto label = globals.first.at(iOldCol);
76  if (label == 0)
77  continue;
78 
79  newLabels.push_back(label);
80  newMatrix.ResizeTo(globals.second.GetNrows(), newMatrix.GetNcols() + 1);
81  for (int iRow = 0; iRow < globals.second.GetNrows(); ++iRow) {
82  newMatrix(iRow, newMatrix.GetNcols() - 1) = globals.second(iRow, iOldCol);
83  }
84  }
85  return {newLabels, newMatrix};
86  }
87  }
89 }
Belle2::alignment::GlobalDerivatives::m_globals
std::pair< std::vector< int >, TMatrixD > m_globals
The global labels and derivatives matrix.
Definition: GlobalDerivatives.h:73
Belle2::alignment::GlobalDerivatives::GlobalDerivatives
GlobalDerivatives(int dim=2)
Constructor for empty derivative matrix and label vector.
Definition: GlobalDerivatives.h:42
Belle2::alignment::GlobalDerivatives::passGlobals
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....
Definition: GlobalDerivatives.cc:77
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::alignment::GlobalDerivatives::add
void add(const std::pair< std::vector< int >, TMatrixD > &globals)
Add another set of global labels and derivatives.
Definition: GlobalDerivatives.cc:39