Belle II Software development
PedeResult.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/PedeResult.h>
10
11#include <fstream>
12#include <iostream>
13#include <sstream>
14
15using namespace std;
16
17namespace Belle2 {
22 namespace alignment {
23
24 // ----------
25 // PedeResult
26 // ----------
27 int PedeResult::getParameterIndex(int parameterLabel)
28 {
29 auto index = labelIndices.find(parameterLabel);
30 if (index != labelIndices.end())
31 return index->second;
32 else
33 return -1;
34 }
35 void PedeResult::readEigenFile(string filename)
36 {
37 if (!valid)
38 return;
39
40 string line;
41 ifstream file(filename);
42
43 if (!file.is_open())
44 return;
45
46 int eigenPairIndex = -1;
47
48 for (auto& pData : data) {
49 pData.eigenweights.clear();
50 pData.eigenweights.reserve(20);
51 }
52
53 while (getline(file, line)) {
54 cout << line << endl;
55 stringstream ss;
56 ss << line;
57 string tmp1, tmp2, tmp3;
58 int eigenPairNumber;
59 double eigenvalue;
60 ss >> tmp1 >> eigenPairNumber >> tmp2 >> tmp3 >> eigenvalue;
61 cout << tmp1 << " " << tmp2 << " " << tmp3 << " " << eigenPairNumber << " " << eigenvalue << endl;
62 if (tmp1 == "Eigenvector")
63 ++eigenPairIndex;
64 else
65 continue;
66
67 // Read elements
68 vector<pair<int, double>> elements;
69 while (getline(file, line) && line.length() > 2) {
70 int lab1 = 0;
71 int lab2 = 0;
72 int lab3 = 0;
73 double w1, w2, w3;
74 stringstream triplet;
75 triplet << line;
76 triplet >> lab1 >> w1 >> lab2 >> w2 >> lab3 >> w3;
77 if (lab1) elements.push_back({lab1, w1});
78 if (lab2) elements.push_back({lab2, w2});
79 if (lab3) elements.push_back({lab3, w3});
80 //cout << lab1 << w1 << lab2 << w2 << lab3 << w3 << endl;
81 }
82 for (auto& pData : data) {
83 pData.eigenweights.push_back(0.);
84 }
85 for (auto el : elements) {
86 int index = getParameterIndex(el.first);
87 if (index >= 0)
88 data[index].eigenweights[eigenPairIndex] = el.second;
89 }
90 eigenNumbers.push_back(eigenvalue);
91 }
92
93
94 }
95 void PedeResult::read(string filename)
96 {
97 string line;
98 ifstream res(filename);
99 // skip comment line
100 getline(res, line);
101
103 data.clear();
104 labelIndices.clear();
105 int index(-1);
106
107 while (getline(res, line)) {
108 ++index;
109 int label = -1;
110 double param = 0.;
111 double presigma = 0.;
112 double differ = 0.;
113 double error = 0.;
114 stringstream ss;
115 ss << line;
116 ss >> label >> param >> presigma >> differ >> error;
117 data.push_back(parameterData(index, label, param, error, presigma));
118 labelIndices.insert(make_pair(label, index));
119 if (isParameterDetermined(index))
121 }
122
123 if (data.size() > 0)
124 valid = true;
125 }
126 PedeResult::PedeResult(const string& filename): data(), eigenNumbers(), valid(false), labelIndices()
127 {
128 read(filename);
129 }
130
131 unsigned int PedeResult::getParameterLabel(unsigned int parameterIndex)
132 {
133 if (outOfBounds(parameterIndex)) return 0;
134 return data[parameterIndex].label;
135 }
136 double PedeResult::getParameterCorrection(unsigned int parameterIndex)
137 {
138 if (outOfBounds(parameterIndex)) return 0.;
139 return data[parameterIndex].correction;
140 }
141 double PedeResult::getParameterError(unsigned int parameterIndex)
142 {
143 if (outOfBounds(parameterIndex)) return 0.;
144 return data[parameterIndex].error;
145 }
146 double PedeResult::getParameterPresigma(unsigned int parameterIndex)
147 {
148 if (outOfBounds(parameterIndex)) return 0.;
149 return data[parameterIndex].presigma;
150 }
151 bool PedeResult::isParameterFixed(unsigned int parameterIndex)
152 {
153 if (outOfBounds(parameterIndex)) return false;
154 return data[parameterIndex].presigma < 0;
155 }
156 bool PedeResult::isParameterDetermined(unsigned int parameterIndex)
157 {
158 if (outOfBounds(parameterIndex)) return false;
159 return !isParameterFixed(parameterIndex) && data[parameterIndex].correction != 0. && data[parameterIndex].error != 0.;
160 }
161 double PedeResult::getEigenNumber(unsigned int eigenPairIndex)
162 {
163 if (eigenPairIndex >= eigenNumbers.size()) return 0.;
164 return eigenNumbers[eigenPairIndex];
165 }
166 double PedeResult::getEigenVectorElement(unsigned int eigenPairIndex, unsigned int parameterIndex)
167 {
168 if (outOfBounds(parameterIndex))
169 return 0.;
170 parameterData& element = data[parameterIndex];
171 if (eigenPairIndex >= element.eigenweights.size())
172 return 0.;
173
174 return data[parameterIndex].eigenweights[eigenPairIndex];
175 }
177 {
178 for (auto pdata : data) {
179 std::cout << pdata.index << " : " << pdata.label << " corr: " << pdata.correction << " err: " << pdata.error << " presigma: " <<
180 pdata.presigma << endl;
181 }
182 }
183 }
185}
186
187
void readEigenFile(std::string filename="millepede.eve")
Reads file with eigen-vector/numbers.
Definition: PedeResult.cc:35
bool valid
Flag to check if data wa loaded.
Definition: PedeResult.h:105
double getParameterCorrection(unsigned int parameterIndex)
Get determined correction of parameter at index.
Definition: PedeResult.cc:136
bool isParameterDetermined(unsigned int parameterIndex)
Is parameter at given index determined?
Definition: PedeResult.cc:156
int noDeterminedParams
Number of parameters actually determined.
Definition: PedeResult.h:109
PedeResult()
Default constructor, use read(...) to init the object.
Definition: PedeResult.h:25
double getEigenNumber(unsigned int eigenPairIndex)
Get eigennumber at given index.
Definition: PedeResult.cc:161
int getParameterIndex(int parameterLabel)
Get index of parameter with given label.
Definition: PedeResult.cc:27
void read(std::string filename="millepede.res")
Reads the result file and inits the object.
Definition: PedeResult.cc:95
std::map< int, int > labelIndices
Map to link parameter labels and their indices in result.
Definition: PedeResult.h:107
bool outOfBounds(int iparam)
Is index out of valid range?
Definition: PedeResult.h:64
bool isParameterFixed(unsigned int parameterIndex)
Is parameter at given index fixed?
Definition: PedeResult.cc:151
unsigned int getParameterLabel(unsigned int parameterIndex)
Get label of parameter at index.
Definition: PedeResult.cc:131
double getParameterPresigma(unsigned int parameterIndex)
Get presigma of parameter at index.
Definition: PedeResult.cc:146
std::vector< double > eigenNumbers
Vector of loaded eigennumbers.
Definition: PedeResult.h:103
double getParameterError(unsigned int parameterIndex)
Get correction error of parameter at index.
Definition: PedeResult.cc:141
double getEigenVectorElement(unsigned int eigenPairIndex, unsigned int parameterIndex)
Get eigenvector at given index.
Definition: PedeResult.cc:166
std::vector< parameterData > data
Vector with all the parameter data.
Definition: PedeResult.h:101
void dump()
Dump the content to std::cout.
Definition: PedeResult.cc:176
Abstract base class for different kinds of events.
STL namespace.
Struct to hold data for a parameter.
Definition: PedeResult.h:68