Skip to content

Commit bf6faca

Browse files
mleschmlesch
andauthored
TPC Update CheckOfSlices with Masking (#2245)
* Save WIP * TPC Update CheckOfSlices with Masking * Adding changes --------- Co-authored-by: mlesch <ga94pez@mytum.de>
1 parent 0cdcdf3 commit bf6faca

4 files changed

Lines changed: 101 additions & 15 deletions

File tree

Modules/TPC/include/TPC/CheckOfSlices.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CheckOfSlices : public o2::quality_control::checker::CheckInterface
5555
double mRangeMedium;
5656
double mRangeBad;
5757
bool mSliceTrend;
58+
std::vector<int> mMaskedPoints;
5859

5960
double mMean = 0;
6061
double mStdev;

Modules/TPC/include/TPC/Utility.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void getTimestamp(const std::string& metaInfo, std::vector<long>& timeStamps);
7373
/// \param limit Most recent timestamp to be processed
7474
std::vector<long> getDataTimestamps(const o2::ccdb::CcdbApi& cdbApi, const std::string_view path, const unsigned int nFiles, const long limit);
7575

76-
/// \brief Calculates mean and stddev from yValues of a TGraph
76+
/// \brief Calculates mean and stddev from yValues of a TGraph. Overloaded function, actual calculation in retrieveStatistics
7777
/// \param yValues const double* pointer to yValues of TGraph (via TGraph->GetY())
7878
/// \param yErrors const double* pointer to y uncertainties of TGraph (via TGraph->GetEY())
7979
/// \param useErrors bool whether uncertainties should be used in calculation of mean and stddev of mean
@@ -82,5 +82,24 @@ std::vector<long> getDataTimestamps(const o2::ccdb::CcdbApi& cdbApi, const std::
8282
/// \param mean double&, reference to double that should store mean
8383
/// \param stddevOfMean double&, reference to double that should store stddev of mean
8484
void calculateStatistics(const double* yValues, const double* yErrors, bool useErrors, const int firstPoint, const int lastPoint, double& mean, double& stddevOfMean);
85+
86+
/// \brief Calculates mean and stddev from yValues of a TGraph. Overloaded function
87+
/// \param yValues const double* pointer to yValues of TGraph (via TGraph->GetY())
88+
/// \param yErrors const double* pointer to y uncertainties of TGraph (via TGraph->GetEY())
89+
/// \param useErrors bool whether uncertainties should be used in calculation of mean and stddev of mean
90+
/// \param firstPoint const int, first point of yValues to include in calculation
91+
/// \param lastPoint const int, last point of yValues to include in calculation
92+
/// \param mean double&, reference to double that should store mean
93+
/// \param stddevOfMean double&, reference to double that should store stddev of mean
94+
/// \param maskPoints std::vector<int>&, points of the selected TGraph-points that should be masked
95+
void calculateStatistics(const double* yValues, const double* yErrors, bool useErrors, const int firstPoint, const int lastPoint, double& mean, double& stddevOfMean, std::vector<int>& maskPoints);
96+
97+
/// \brief Calculates mean and stddev from yValues of a TGraph. Overloaded function, actual calculation in retrieveStatistics
98+
/// \param values std::vector<double>& vector that contains the data points
99+
/// \param errors std::vector<double>& vector that contains the data errors
100+
/// \param useErrors bool whether uncertainties should be used in calculation of mean and stddev of mean
101+
/// \param mean double&, reference to double that should store mean
102+
/// \param stddevOfMean double&, reference to double that should store stddev of mean
103+
void retrieveStatistics(std::vector<double>& values, std::vector<double>& errors, bool useErrors, double& mean, double& stddevOfMean);
85104
} // namespace o2::quality_control_modules::tpc
86105
#endif // QUALITYCONTROL_TPCUTILITY_H

Modules/TPC/src/CheckOfSlices.cxx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <fmt/format.h>
2222
#include "Common/Utils.h"
2323
#include "TPC/Utility.h"
24+
#include "Algorithm/RangeTokenizer.h"
2425

2526
#include <TCanvas.h>
2627
#include <TGraphErrors.h>
@@ -32,6 +33,7 @@
3233

3334
#include <vector>
3435
#include <numeric>
36+
#include <sstream>
3537

3638
namespace o2::quality_control_modules::tpc
3739
{
@@ -98,6 +100,12 @@ void CheckOfSlices::configure()
98100
}
99101
mExpectedPhysicsValue = common::getFromConfig<double>(mCustomParameters, "expectedPhysicsValue", 1);
100102
}
103+
104+
std::string maskingValues = common::getFromConfig<string>(mCustomParameters, "maskedPoints", "");
105+
mMaskedPoints.clear();
106+
if (maskingValues != "") {
107+
mMaskedPoints = RangeTokenizer::tokenize<int>(maskingValues);
108+
}
101109
}
102110

103111
Quality CheckOfSlices::check(std::map<std::string, std::shared_ptr<MonitorObject>>* moMap)
@@ -168,7 +176,7 @@ Quality CheckOfSlices::check(std::map<std::string, std::shared_ptr<MonitorObject
168176
}
169177
}
170178

171-
calculateStatistics(yValues, yErrors, useErrors, 0, NBins, mMean, mStdev);
179+
calculateStatistics(yValues, yErrors, useErrors, 0, NBins, mMean, mStdev, mMaskedPoints);
172180

173181
for (size_t i = 0; i < v.size(); ++i) {
174182

Modules/TPC/src/Utility.cxx

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
// external includes
3131
#include <Framework/Logger.h>
3232
#include <bitset>
33+
#include <algorithm>
3334

3435
namespace o2::quality_control_modules::tpc
3536
{
@@ -210,44 +211,101 @@ void calculateStatistics(const double* yValues, const double* yErrors, bool useE
210211
return;
211212
}
212213

214+
if (useErrors && !yErrors) {
215+
ILOG(Error, Support) << "In calculateStatistics(): requested to use errors of data but TGraph does not contain errors." << ENDM;
216+
useErrors = false;
217+
}
218+
219+
std::vector<double> v(yValues + firstPoint, yValues + lastPoint);
220+
std::vector<double> vErr;
221+
222+
if (useErrors) {
223+
const std::vector<double> vErr_temp(yErrors + firstPoint, yErrors + lastPoint);
224+
for (int i = 0; i < vErr_temp.size(); i++) {
225+
vErr.push_back(vErr_temp[i]);
226+
}
227+
}
228+
229+
retrieveStatistics(v, vErr, useErrors, mean, stddevOfMean);
230+
}
231+
232+
void calculateStatistics(const double* yValues, const double* yErrors, bool useErrors, const int firstPoint, const int lastPoint, double& mean, double& stddevOfMean, std::vector<int>& maskPoints)
233+
{
234+
// yErrors returns nullptr for TGraph (no errors)
235+
if (lastPoint - firstPoint <= 0) {
236+
ILOG(Error, Support) << "In calculateStatistics(), the first and last point of the range have to differ!" << ENDM;
237+
return;
238+
}
239+
240+
if (useErrors && !yErrors) {
241+
ILOG(Error, Support) << "In calculateStatistics(): requested to use errors of data but TGraph does not contain errors." << ENDM;
242+
useErrors = false;
243+
}
244+
245+
std::vector<double> v;
246+
const std::vector<double> v_temp(yValues + firstPoint, yValues + lastPoint);
247+
for (int i = 0; i < v_temp.size(); i++) {
248+
if (std::find(maskPoints.begin(), maskPoints.end(), i) == maskPoints.end()) { // i is not in the masked points
249+
v.push_back(v_temp[i]);
250+
}
251+
}
252+
253+
std::vector<double> vErr;
254+
if (useErrors) {
255+
const std::vector<double> vErr_temp(yErrors + firstPoint, yErrors + lastPoint);
256+
for (int i = 0; i < vErr_temp.size(); i++) {
257+
if (std::find(maskPoints.begin(), maskPoints.end(), i) == maskPoints.end()) { // i is not in the masked points
258+
vErr.push_back(vErr_temp[i]);
259+
}
260+
}
261+
}
262+
263+
retrieveStatistics(v, vErr, useErrors, mean, stddevOfMean);
264+
}
265+
266+
void retrieveStatistics(std::vector<double>& values, std::vector<double>& errors, bool useErrors, double& mean, double& stddevOfMean)
267+
{
268+
if ((errors.size() != values.size()) && useErrors) {
269+
ILOG(Error, Support) << "In retrieveStatistics(): errors do not match data points, omitting errors" << ENDM;
270+
useErrors = false;
271+
}
272+
213273
double sum = 0.;
214274
double sumSquare = 0.;
215275
double sumOfWeights = 0.; // sum w_i
216276
double sumOfSquaredWeights = 0.; // sum (w_i)^2
217277
double weight = 0.;
218278

219-
const std::vector<double> v(yValues + firstPoint, yValues + lastPoint);
220279
if (!useErrors) {
221280
// In case of no errors, we set our weights equal to 1
222-
sum = std::accumulate(v.begin(), v.end(), 0.0);
223-
sumOfWeights = v.size();
224-
sumOfSquaredWeights = v.size();
281+
sum = std::accumulate(values.begin(), values.end(), 0.0);
282+
sumOfWeights = values.size();
283+
sumOfSquaredWeights = values.size();
225284
} else {
226285
// In case of errors, we set our weights equal to 1/sigma_i^2
227-
const std::vector<double> vErr(yErrors + firstPoint, yErrors + lastPoint);
228-
for (size_t i = 0; i < v.size(); i++) {
229-
weight = 1. / std::pow(vErr[i], 2.);
230-
sum += v[i] * weight;
231-
sumSquare += v[i] * v[i] * weight;
286+
for (size_t i = 0; i < values.size(); i++) {
287+
weight = 1. / std::pow(errors[i], 2.);
288+
sum += values[i] * weight;
289+
sumSquare += values[i] * values[i] * weight;
232290
sumOfWeights += weight;
233291
sumOfSquaredWeights += weight * weight;
234292
}
235293
}
236294

237295
mean = sum / sumOfWeights;
238296

239-
if (v.size() == 1) { // we only have one point, we keep it's uncertainty
297+
if (values.size() == 1) { // we only have one point, we keep it's uncertainty
240298
if (!useErrors) {
241299
stddevOfMean = 0.;
242300
} else {
243301
stddevOfMean = sqrt(1. / sumOfWeights);
244302
}
245303
} else { // for >= 2 points, we calculate the spread
246304
if (!useErrors) {
247-
std::vector<double> diff(v.size());
248-
std::transform(v.begin(), v.end(), diff.begin(), [mean](double x) { return x - mean; });
305+
std::vector<double> diff(values.size());
306+
std::transform(values.begin(), values.end(), diff.begin(), [mean](double x) { return x - mean; });
249307
double sq_sum = std::inner_product(diff.begin(), diff.end(), diff.begin(), 0.0);
250-
stddevOfMean = std::sqrt(sq_sum / (v.size() * (v.size() - 1.)));
308+
stddevOfMean = std::sqrt(sq_sum / (values.size() * (values.size() - 1.)));
251309
} else {
252310
double ratioSumWeight = sumOfSquaredWeights / (sumOfWeights * sumOfWeights);
253311
stddevOfMean = sqrt((sumSquare / sumOfWeights - mean * mean) * (1. / (1. - ratioSumWeight)) * ratioSumWeight);

0 commit comments

Comments
 (0)