Project
Loading...
Searching...
No Matches
BaselineCalibData.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
12#include <TMath.h>
13#include <TH1.h>
14#include <TFile.h>
15#include <TDirectory.h>
16#include "Framework/Logger.h"
18
19using namespace o2::zdc;
20
21//______________________________________________________________________________
23{
24 LOGF(info, "BaselineCalibData::print ts (%llu:%llu)", mCTimeBeg, mCTimeEnd);
25 for (int is = 0; is < NChannels; is++) {
26 uint64_t en = 0;
27 double mean = 0, var = 0;
28 if (getStat(is, en, mean, var) == 0) {
29 LOGF(info, "Baseline %2d %s with %10llu events mean %8.1f rms %8.1f (ch)", is, ChannelNames[is].data(), en, mean, TMath::Sqrt(var));
30 }
31 }
32}
33
34//______________________________________________________________________________
36{
37 if (other.mOverflow) {
38 // Refusing to add an overflow
39 LOG(warn) << "BaselineCalibData" << __func__ << " Refusing to add an overflow. BREAK!";
40 return *this;
41 }
42 for (int32_t is = 0; is < NChannels; is++) {
43 if (other.mHisto[is].mOverflow) {
44 // Refusing to add an overflow histogram
45 LOG(warn) << "BaselineCalibData::" << __func__ << " Refusing to add an overflow histogram for ch " << is << " BREAK!";
46 return *this;
47 }
48 // Check if sum will result into an overflow
49 for (int32_t i = 0; i < BaselineCalibChData::NW; i++) {
50 uint64_t sum = mHisto[is].mData[i] + other.mHisto[is].mData[i];
51 if (sum > 0xffffffff) {
52 LOG(warn) << "BaselineCalibData::" << __func__ << " Addition would result in an overflow for ch " << is << " BREAK!";
53 return *this;
54 }
55 }
56 }
57 // No problems with overflow
58 for (int32_t is = 0; is < NChannels; is++) {
59 for (int32_t i = 0; i < BaselineCalibChData::NW; i++) {
60 mHisto[is].mData[i] = mHisto[is].mData[i] + other.mHisto[is].mData[i];
61 }
62 }
63 if (mCTimeBeg == 0 || other.mCTimeBeg < mCTimeBeg) {
64 mCTimeBeg = other.mCTimeBeg;
65 }
66 if (other.mCTimeEnd > mCTimeEnd) {
67 mCTimeEnd = other.mCTimeEnd;
68 }
69#ifdef O2_ZDC_DEBUG
70 LOG(info) << __func__;
71 print();
72#endif
73 return *this;
74}
75
76//______________________________________________________________________________
78{
79#ifdef O2_ZDC_DEGUG
80 LOGF(info) << "BaselineCalibData assigning BaselineCalibSummaryData" s.print();
81#endif
82 mCTimeBeg = s.mCTimeBeg;
83 mCTimeEnd = s.mCTimeEnd;
84 mOverflow = s.mOverflow;
85 for (int32_t is = 0; is < NChannels; is++) {
86 // Need to clear all bins since summary data have info only on filled channels
87 mHisto[is].clear();
88 mHisto[is].mOverflow = s.mOverflowCh[is];
89 // Cross check
90 if (mHisto[is].mOverflow && mOverflow == false) {
91 LOG(warn) << "Overflow bit set on signal " << is;
92 mOverflow = true;
93 }
94 }
95 for (const BaselineCalibBinData& bin : s.mData) {
96 mHisto[bin.id].mData[bin.ibin] = bin.cont;
97 }
98 return *this;
99}
100
101//______________________________________________________________________________
103{
104 if (s == nullptr) {
105 LOG(error) << "BaselineCalibData::operator+=(const BaselineCalibSummaryData* s): null pointer";
106 return *this;
107 }
108#ifdef O2_ZDC_DEGUG
109 LOGF(info) << "BaselineCalibData cumulating BaselineCalibSummaryData" s.print();
110#endif
111 if (s->mOverflow) {
112 // Refusing to add an overflow
113 LOG(warn) << __func__ << " Refusing to add an overflow BaselineCalibSummaryData BREAK!";
114 s->print();
115 return *this;
116 }
117 for (int32_t is = 0; is < NChannels; is++) {
118 if (s->mOverflowCh[is]) {
119 // Refusing to add an overflow histogram
120 LOG(warn) << __func__ << " Refusing to add an overflow histogram for ch " << is << " BREAK!";
121 s->print();
122 return *this;
123 }
124 }
125 // Check if sum will result into an overflow
126 for (auto& bin : s->mData) {
127 uint64_t sum = mHisto[bin.id].mData[bin.ibin] + bin.cont;
128 if (sum > 0xffffffff) {
129 LOG(warn) << __func__ << " Addition would result in an overflow for ch " << bin.id << " BREAK!";
130 return *this;
131 }
132 }
133 if (mCTimeBeg == 0 || s->mCTimeBeg < mCTimeBeg) {
134 mCTimeBeg = s->mCTimeBeg;
135 }
136 if (s->mCTimeEnd > mCTimeEnd) {
137 mCTimeEnd = s->mCTimeEnd;
138 }
139 for (auto& bin : s->mData) {
140 mHisto[bin.id].mData[bin.ibin] = mHisto[bin.id].mData[bin.ibin] + bin.cont;
141 }
142 return *this;
143}
144
145//______________________________________________________________________________
147{
148 mCTimeBeg = ctime;
149 mCTimeEnd = ctime;
150#ifdef O2_ZDC_DEBUG
151 LOGF(info, "BaselineCalibData::setCreationTime %llu", ctime);
152#endif
153}
154
155//______________________________________________________________________________
157{
158 if (mCTimeBeg == 0 || ctime < mCTimeBeg) {
159 mCTimeBeg = ctime;
160 }
161 if (ctime > mCTimeEnd) {
162 mCTimeEnd = ctime;
163 }
164#ifdef O2_ZDC_DEBUG
165 LOGF(info, "BaselineCalibData::mergeCreationTime %llu", ctime);
166#endif
167}
168
169//______________________________________________________________________________
170uint64_t BaselineCalibData::getEntries(int is) const
171{
172 if (is < 0 || is >= NChannels) {
173 LOGF(error, "BaselineCalibData::getEntries channel index %d is out of range", is);
174 return 0;
175 }
176 return mHisto[is].getEntries();
177}
178
180{
181 uint64_t sum = 0;
182 for (int32_t i = 0; i < NW; i++) {
183 sum += mData[i];
184 }
185 return sum;
186}
187
188//______________________________________________________________________________
189int BaselineCalibData::getStat(int is, uint64_t& en, double& mean, double& var) const
190{
191 if (is < 0 || is >= NChannels) {
192 LOGF(error, "BaselineCalibData::getStat channel index %d is out of range", is);
193 return 1;
194 }
195 return mHisto[is].getStat(en, mean, var);
196}
197
198int BaselineCalibChData::getStat(uint64_t& en, double& mean, double& var) const
199{
200 en = 0;
201 uint64_t sum = 0;
202 for (uint64_t i = 0; i < NW; i++) {
203 en += mData[i];
204 sum += i * mData[i];
205 }
206 if (en == 0) {
207 return 1;
208 }
209 mean = double(sum) / double(en);
210 if (en > 1) {
211 double sums = 0;
212 for (int32_t i = 0; i < NW; i++) {
213 double diff = i - mean;
214 sums += (mData[i] * diff * diff);
215 }
216 var = sums / (en - 1);
217 } else {
218 var = 0;
219 }
220 // Convert mean to correct range
221 mean = mean + BaselineMin;
222 return 0;
223}
224
225//______________________________________________________________________________
226int BaselineCalibData::saveDebugHistos(const std::string fn, float factor = 1)
227{
228 TDirectory* cwd = gDirectory;
229 TFile* f = new TFile(fn.data(), "recreate");
230 if (f->IsZombie()) {
231 LOG(error) << "Cannot create file: " << fn;
232 return 1;
233 }
234 for (int32_t is = 0; is < NChannels; is++) {
235 uint64_t nen = mHisto[is].getEntries();
236 if (nen > 0) {
237 TString n = TString::Format("h%d", is);
238 TString t = TString::Format("Baseline %d %s", is, ChannelNames[is].data());
239 TH1F h(n, t, BaselineRange, (BaselineMin - 0.5) * factor, (BaselineMax + 0.5) * factor);
240 for (int ibx = 0; ibx < BaselineRange; ibx++) {
241 h.SetBinContent(ibx + 1, mHisto[is].mData[ibx]);
242 }
243 h.SetEntries(nen);
244 h.Write("", TObject::kOverwrite);
245 }
246 }
247 f->Close();
248 cwd->cd();
249 return 0;
250}
251
252//______________________________________________________________________________
254{
255 mCTimeBeg = 0;
256 mCTimeEnd = 0;
257 mOverflow = false;
258 mSummary.clear();
259 for (int32_t is = 0; is < NChannels; is++) {
260 mHisto[is].clear();
261 }
262}
263
265{
266 mOverflow = false;
267 for (int iw = 0; iw < NW; iw++) {
268 mData[iw] = 0;
269 }
270}
271
273{
274 mCTimeBeg = 0;
275 mCTimeEnd = 0;
276 mOverflow = false;
277 mOverflowCh.fill(false);
278 mData.clear();
279}
280
281//______________________________________________________________________________
283{
284 mSummary.clear();
288 for (int32_t is = 0; is < NChannels; is++) {
290 for (int32_t ib = 0; ib < BaselineRange; ib++) {
291 uint32_t cont = mHisto[is].mData[ib];
292 if (cont > 0) {
293 mSummary.mData.emplace_back(is, ib, cont);
294 }
295 }
296 }
297 return mSummary;
298}
299
300//______________________________________________________________________________
302{
303 LOGF(info, "BaselineCalibSummaryData: %llu:%llu %d bins%s", mCTimeBeg, mCTimeEnd, mData.size(), (mOverflow ? " OVERFLOW_BIT" : ""));
304 if (mOverflow) {
305 printf("OVERFLOW:");
306 for (int ich = 0; ich < NChannels; ich++) {
307 if (mOverflowCh[ich]) {
308 printf(" %s", ChannelNames[ich].data());
309 }
310 }
311 printf("\n");
312 }
313 int nbin[NChannels] = {0}; // Number of bin populated
314 uint64_t ccont[NChannels] = {0}; // Entries in the histogram
315 for (auto& bin : mData) {
316 nbin[bin.id]++;
317 ccont[bin.id] += bin.cont;
318 }
319 for (int32_t is = 0; is < NChannels; is++) {
320 LOGF(info, "Summary ch %2d nbin = %d events = %llu", is, nbin[is], ccont[is]);
321 }
322}
Baseline calibration intermediate data.
int32_t i
Class for time synchronization of RawReader instances.
float sum(float s, o2::dcs::DataPointValue v)
Definition dcs-ccdb.cxx:39
GLdouble n
Definition glcorearb.h:1982
GLdouble f
Definition glcorearb.h:310
GLboolean * data
Definition glcorearb.h:298
constexpr int NChannels
Definition Constants.h:65
constexpr int BaselineMax
Definition Constants.h:353
constexpr int BaselineMin
Definition Constants.h:353
constexpr int BaselineRange
Definition Constants.h:353
constexpr std::string_view ChannelNames[]
Definition Constants.h:147
uint64_t getEntries() const
Overflow flag (cannot accept more data)
bool mOverflow
Histogram container.
int getStat(uint64_t &en, double &mean, double &var) const
std::array< uint32_t, NW > mData
2^16 bins
BaselineCalibSummaryData & getSummary()
uint64_t getEntries(int is) const
bool mOverflow
Time of processed time frame.
BaselineCalibData & operator=(const BaselineCalibSummaryData &s)
void setCreationTime(uint64_t ctime)
BaselineCalibData & operator+=(const BaselineCalibData &other)
int saveDebugHistos(const std::string fn, float factor)
BaselineCalibSummaryData mSummary
Histogram for single channel.
int getStat(int is, uint64_t &en, double &mean, double &var) const
uint64_t mCTimeEnd
Time of processed time frame.
BaselineCalibChData mHisto[NChannels]
Overflow at least one ZDC channel.
void mergeCreationTime(uint64_t ctime)
std::vector< BaselineCalibBinData > mData
Channel overflow information.
std::array< bool, NChannels > mOverflowCh
Overflow of one channel.
uint64_t mCTimeEnd
Time of processed time frame.
bool mOverflow
Time of processed time frame.
void clear()
Data of not empty bins.
VectorOfTObjectPtrs other
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"