Project
Loading...
Searching...
No Matches
CalibdEdxCorrection.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
13
14#include <algorithm>
15#include <string_view>
16
17// o2 includes
18#include "Framework/Logger.h"
19#include "DataFormatsTPC/Defs.h"
21
22// root includes
23#include "TFile.h"
24
25using namespace o2::tpc;
26
27void CalibdEdxCorrection::clear()
28{
29 for (auto& row : mParams) {
30 for (auto& x : row) {
31 x = 0.f;
32 }
33 }
34 for (auto& x : mChi2) {
35 x = 0.f;
36 }
37 mDims = -1;
38}
39
40void CalibdEdxCorrection::writeToFile(std::string_view fileName, std::string_view objName) const
41{
42 std::unique_ptr<TFile> file(TFile::Open(fileName.data(), "recreate"));
43 if (!file) {
44 LOGP(error, "Failed to open file {} for writing", fileName.data());
45 return;
46 }
47
48 file->WriteObject(this, objName.data());
49}
50
51void CalibdEdxCorrection::loadFromFile(std::string_view fileName, std::string_view objName)
52{
53 std::unique_ptr<TFile> file(TFile::Open(fileName.data()));
54 if (!file || file->IsZombie()) {
55 LOGP(error, "Failed to open file {}", fileName.data());
56 return;
57 }
58
59 auto tmp = file->Get<CalibdEdxCorrection>(objName.data());
60 if (tmp != nullptr) {
61 *this = *tmp;
62 } else {
63 LOGP(error, "Failed to load object with name {} from file {}", objName.data(), fileName.data());
64 }
65}
66
67void CalibdEdxCorrection::dumpToTree(const char* outFileName) const
68{
69 o2::utils::TreeStreamRedirector pcstream(outFileName, "RECREATE");
70 pcstream.GetFile()->cd();
71
72 for (int sector = 0; sector < 2 * SECTORSPERSIDE; ++sector) {
73 for (int roc = 0; roc < GEMSTACKSPERSECTOR; ++roc) {
75 sector,
76 static_cast<tpc::GEMstack>(roc)};
77
78 std::vector<float> qMaxCorrOut;
79 std::vector<float> qTotCorrOut;
80 std::vector<float> tglOut;
81 std::vector<float> snpOut;
82
83 for (float tgl = 0; tgl < 2; tgl += 0.01) {
84 for (float snp = 0; snp < 1; snp += 0.1) {
85 qMaxCorrOut.emplace_back(getCorrection(stack, ChargeType::Max, tgl, snp));
86 qTotCorrOut.emplace_back(getCorrection(stack, ChargeType::Tot, tgl, snp));
87 tglOut.emplace_back(tgl);
88 snpOut.emplace_back(snp);
89 }
90 }
91
92 pcstream << "tree"
93 << "qMaxCorr=" << qMaxCorrOut
94 << "qTotCorr=" << qTotCorrOut
95 << "tgl=" << tglOut
96 << "snp=" << snpOut
97 << "roc=" << roc
98 << "sector=" << sector
99 << "\n";
100 }
101 }
102}
103
104const std::array<float, CalibdEdxCorrection::ParamSize> CalibdEdxCorrection::getMeanParams(ChargeType charge) const
105{
106 std::array<float, ParamSize> params{};
107
108 for (int index = 0; index < FitSize / 2; ++index) {
109 std::transform(params.begin(), params.end(), mParams[index + charge * FitSize / 2], params.begin(), std::plus<>());
110 }
111 std::for_each(params.begin(), params.end(), [](auto& val) { val /= (0.5f * FitSize); });
112 return params;
113}
114
115const std::array<float, CalibdEdxCorrection::ParamSize> CalibdEdxCorrection::getMeanParams(const GEMstack stack, ChargeType charge) const
116{
117 std::array<float, ParamSize> params{};
118
119 for (int index = 0; index < SECTORSPERSIDE * SIDES; ++index) {
120 std::transform(params.begin(), params.end(), getParams(StackID{index, stack}, charge), params.begin(), std::plus<>());
121 }
122 std::for_each(params.begin(), params.end(), [](auto& val) { val /= (SECTORSPERSIDE * SIDES); });
123 return params;
124}
125
127{
128 if (param >= ParamSize) {
129 return 0.f;
130 }
131 float mean{};
132 for (int index = 0; index < FitSize / 2; ++index) {
133 mean += mParams[index + charge * FitSize / 2][param];
134 }
135
136 return mean / (0.5f * FitSize);
137}
138
140{
141 if (param >= ParamSize) {
142 return 0.f;
143 }
144 float mean{};
145 for (int index = 0; index < SECTORSPERSIDE * SIDES; ++index) {
146 mean += getParams(StackID{index, stack}, charge)[param];
147 }
148
149 return mean / (SECTORSPERSIDE * SIDES);
150}
151
153{
154 float mean{};
155 for (int index = 0; index < FitSize / 2; ++index) {
156 mean += mEntries[index + charge * FitSize / 2];
157 }
158
159 return mean / (0.5f * FitSize);
160}
161
163{
164 float mean{};
165 for (int index = 0; index < SECTORSPERSIDE * SIDES; ++index) {
166 mean += getEntries(StackID{index, stack}, charge);
167 }
168
169 return mean / (SECTORSPERSIDE * SIDES);
170}
int16_t charge
Definition RawEventData.h:5
uint32_t roc
Definition RawData.h:3
uint32_t stack
Definition RawData.h:1
void dumpToTree(const char *outFileName="calib_dedx.root") const
static constexpr int ParamSize
Number of params per fit.
const std::array< float, ParamSize > getMeanParams(ChargeType charge) const
Parameters averaged over all stacks.
void writeToFile(std::string_view fileName, std::string_view objName="ccdb_object") const
void loadFromFile(std::string_view fileName, std::string_view objName="ccdb_object")
float getMeanParam(ChargeType charge, uint32_t param) const
Single fit parameters averaged over all sectors for a stack type.
float getMeanEntries(ChargeType charge) const
Single fit parameters averaged over all sectors for a stack type.
static constexpr int FitSize
Number of fitted corrections.
GLint GLenum GLint x
Definition glcorearb.h:403
GLuint index
Definition glcorearb.h:781
GLenum const GLfloat * params
Definition glcorearb.h:272
GLuint GLfloat * val
Definition glcorearb.h:1582
GLenum GLfloat param
Definition glcorearb.h:271
Global TPC definitions and constants.
Definition SimTraits.h:167
GEMstack
TPC GEM stack types.
Definition Defs.h:53
ChargeType
Definition Defs.h:70
@ Tot
Definition Defs.h:72
@ Max
Definition Defs.h:71
constexpr unsigned char SECTORSPERSIDE
Definition Defs.h:40
constexpr unsigned char SIDES
Definition Defs.h:41
constexpr unsigned short GEMSTACKSPERSECTOR
Definition Defs.h:57
GEM stack identification.
Definition Defs.h:77
std::vector< int > row