Project
Loading...
Searching...
No Matches
CalibTOFapi.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 "TOFBase/CalibTOFapi.h"
13#include <fairlogger/Logger.h> // for LOG
14
15using namespace o2::tof;
16
18
20{
21 memset(mEmptyCrateProb, 0., Geo::kNCrate * 4);
22 mTRMerrorProb.clear();
23 mTRMmask.clear();
24 mNoisy.clear();
25}
26
27//______________________________________________________________________
28
29CalibTOFapi::CalibTOFapi(const std::string url)
30{
31 // setting the URL to the CCDB manager
32
33 setURL(url);
34}
35
36//______________________________________________________________________
38{
39 auto& mgr = CcdbManager::instance();
40 long timems = long(mTimeStamp) * 1000;
41 LOG(info) << "TOF get active map with timestamp (ms) = " << timems;
42 auto fee = mgr.getForTimeStamp<TOFFEElightInfo>("TOF/Calib/FEELIGHT", timems);
43 loadActiveMap(fee);
44}
45
46//______________________________________________________________________
48{
49 // getting the active TOF map
50 memset(mIsOffCh, false, Geo::NCHANNELS);
51
52 if (fee) {
53 LOG(info) << "read Active map (TOFFEELIGHT) for TOF ";
54 for (int ich = 0; ich < TOFFEElightInfo::NCHANNELS; ich++) {
55 //printf("%d) Enabled= %d\n",ich,fee->mChannelEnabled[ich]);
56 if (!fee->getChannelEnabled(ich)) {
57 mIsOffCh[ich] = true;
58 }
59 }
60 } else {
61 LOG(info) << "Active map (TOFFEELIGHT) not available in ccdb";
62 }
63}
64
65//______________________________________________________________________
66
68{
69
70 // getting the LHCphase calibration
71
72 auto& mgr = CcdbManager::instance();
73 long timems = long(mTimeStamp) * 1000;
74 LOG(info) << "TOF get LHCphase with timestamp (ms) = " << timems;
75 mLHCphase = mgr.getForTimeStamp<LhcPhase>("TOF/Calib/LHCphase", timems);
76 if (mLHCphase) {
77 LOG(info) << "read LHCphase for TOF " << mLHCphase->getLHCphase(mTimeStamp);
78 } else {
79 LOG(info) << "LHC phase not available in ccdb";
80 }
81}
82
83//______________________________________________________________________
84
86{
87
88 // getting the TimeSlewing calibration
89 // it includes also offset and information on problematic
90
91 auto& mgr = CcdbManager::instance();
92 long timems = long(mTimeStamp) * 1000;
93 LOG(info) << "TOF get time calibrations with timestamp (ms) = " << timems;
94 mSlewParam = mgr.getForTimeStamp<SlewParam>("TOF/Calib/ChannelCalib", timems);
95 if (mSlewParam) {
96 LOG(info) << "read TimeSlewingParam for TOF";
97 } else {
98 LOG(info) << "TimeSlewingParam for TOF not available in ccdb";
99 }
100}
101
102//______________________________________________________________________
104{
105 TFile* f = TFile::Open(filename);
106 if (f) {
107 mSlewParam = (SlewParam*)f->Get("ccdb_object");
108 } else {
109 LOG(info) << "File " << filename << " not found";
110 }
111}
112
113//______________________________________________________________________
114
116{
117 auto& mgr = CcdbManager::instance();
118 long timems = long(mTimeStamp) * 1000;
119 LOG(info) << "TOF get Diagnostics with timestamp (ms) = " << timems;
120 mDiaFreq = mgr.getForTimeStamp<Diagnostic>("TOF/Calib/Diagnostic", timems);
121
123}
124//______________________________________________________________________
125
127{
128 mDiaFreq->print();
129
130 static const int NCH_PER_CRATE = Geo::NSTRIPXSECTOR * Geo::NPADS;
131 // getting the Diagnostic Frequency calibration
132 // needed for simulation
133
134 memset(mIsNoisy, false, Geo::NCHANNELS);
135
136 resetDia();
137
138 if (!mDiaFreq->getFrequencyROW()) {
139 return;
140 }
141
142 float nrow = (float)mDiaFreq->getFrequencyROW();
143 mEmptyTOF = mDiaFreq->getFrequencyEmptyTOF() / nrow;
144
145 nrow -= mDiaFreq->getFrequencyEmptyTOF();
146
147 if (nrow < 1) {
148 return;
149 }
150
151 // fill empty crates
152 int ncrate[Geo::kNCrate];
153 for (int i = 0; i < Geo::kNCrate; i++) {
154 ncrate[i] = mDiaFreq->getFrequencyEmptyCrate(i) - mDiaFreq->getFrequencyEmptyTOF(); // counts of empty crate for non-empty event
155 mEmptyCrateProb[i] = ncrate[i] / nrow;
156 }
157
158 const auto vectorDia = mDiaFreq->getVector();
159 // fill TRM errors and noisy
160 for (auto pair : vectorDia) {
161 auto key = pair.first;
162 int slot = mDiaFreq->getSlot(key);
163
164 if (slot < 13 && slot > 2) { // is TRM
165 int icrate = mDiaFreq->getCrate(key);
166 int crateslot = icrate * 100 + slot;
167 mTRMerrorProb.push_back(std::make_pair(crateslot, pair.second / (nrow - ncrate[icrate])));
168 mTRMmask.push_back(key - mDiaFreq->getTRMKey(icrate, slot)); // remove crate and slot from the key (28 bit errors remaining)
169 continue;
170 }
171
172 int channel = mDiaFreq->getChannel(key);
173 if (channel > -1) { // noisy
174 if (!mDiaFreq->isNoisyChannel(channel, mNoisyThreshold)) {
175 continue;
176 }
177
178 int crate = channel / NCH_PER_CRATE;
179 float prob = pair.second / (nrow - ncrate[crate]);
180 mNoisy.push_back(std::make_pair(channel, prob));
181 continue;
182 }
183 }
184
185 std::sort(mTRMerrorProb.begin(), mTRMerrorProb.end(), [](const auto& a, const auto& b) {
186 return a.first < b.first;
187 });
188
189 std::sort(mNoisy.begin(), mNoisy.end(), [](const auto& a, const auto& b) {
190 return a.first < b.first;
191 });
192
193 int ich = -1;
194 float prob = 0;
195 for (auto [ch, p] : mNoisy) {
196 if (ch != ich) { // new channel
197 if (ich != -1 && prob > 0.5) {
198 mIsNoisy[ich] = true;
199 }
200 ich = ch;
201 prob = p;
202 } else {
203 prob += p;
204 }
205 }
206 if (ich != -1 && prob > 0.5) {
207 mIsNoisy[ich] = true;
208 }
209}
210
211//______________________________________________________________________
212
213void CalibTOFapi::writeLHCphase(LhcPhase* phase, std::map<std::string, std::string> metadataLHCphase, uint64_t minTimeStamp, uint64_t maxTimeStamp)
214{
215
216 // write LHCphase object to CCDB
217
218 auto& mgr = CcdbManager::instance();
219 CcdbApi api;
220 api.init(mgr.getURL());
221 api.storeAsTFileAny(phase, "TOF/Calib/LHCphase", metadataLHCphase, minTimeStamp, maxTimeStamp);
222}
223
224//______________________________________________________________________
225
226void CalibTOFapi::writeTimeSlewingParam(SlewParam* param, std::map<std::string, std::string> metadataChannelCalib, uint64_t minTimeStamp, uint64_t maxTimeStamp)
227{
228
229 // write TiemSlewing object to CCDB (it includes offset + problematic)
230
231 auto& mgr = CcdbManager::instance();
232 CcdbApi api;
233 api.init(mgr.getURL());
234 if (maxTimeStamp == 0) {
235 api.storeAsTFileAny(param, "TOF/Calib/ChannelCalib", metadataChannelCalib, minTimeStamp);
236 } else {
237 api.storeAsTFileAny(param, "TOF/Calib/ChannelCalib", metadataChannelCalib, minTimeStamp, maxTimeStamp);
238 }
239}
240
241//______________________________________________________________________
242
244{
245
246 // method to know if the channel was problematic or not
247
248 return (mSlewParam->getFractionUnderPeak(ich) < 0.5 || mSlewParam->getSigmaPeak(ich) > 1000);
249 // return mSlewParam->isProblematic(ich);
250}
251
252//______________________________________________________________________
253
255{
256 return mIsNoisy[ich];
257}
258
259//______________________________________________________________________
260
262{
263 return mIsOffCh[ich];
264}
265
266//______________________________________________________________________
267
268float CalibTOFapi::getTimeCalibration(int ich, float tot) const
269{
270
271 // time calibration to correct measured TOF times
272
273 float corr = 0;
274 if (!mLHCphase || !mSlewParam) {
275 LOG(warning) << "Either LHC phase or slewing object null: mLHCphase = " << mLHCphase << ", mSlewParam = " << mSlewParam;
276 return corr;
277 }
278 // printf("LHC phase apply\n");
279 // LHCphase
280 corr += mLHCphase->getLHCphase(mTimeStamp); // timestamp that we use in LHCPhase is in seconds
281 // time slewing + channel offset
282 //printf("eval time sleweing calibration: ch=%d tot=%f (lhc phase = %f)\n",ich,tot,corr);
283 corr += mSlewParam->evalTimeSlewing(ich, tot);
284 //printf("corr = %f\n",corr);
285 return corr;
286}
287
288//______________________________________________________________________
289
290float CalibTOFapi::getTimeCalibration(int ich, float tot, float phase) const
291{
292
293 // time calibration to correct measured TOF times
294
295 float corr = 0;
296 if (!mSlewParam) {
297 LOG(warning) << "slewing object null: mSlewParam = " << mSlewParam;
298 return corr;
299 }
300 // printf("LHC phase apply\n");
301 // LHCphase
302 corr += phase; // timestamp that we use in LHCPhase is in seconds
303 // time slewing + channel offset
304 //printf("eval time sleweing calibration: ch=%d tot=%f (lhc phase = %f)\n",ich,tot,corr);
305 corr += mSlewParam->evalTimeSlewing(ich, tot);
306 //printf("corr = %f\n",corr);
307 return corr;
308}
309
310//______________________________________________________________________
311
312float CalibTOFapi::getTimeDecalibration(int ich, float tot) const
313{
314
315 // time decalibration for simulation (it is just the opposite of the calibration)
316
317 return -getTimeCalibration(ich, tot);
318}
319
320//______________________________________________________________________
321
323{
324 for (auto index : mFillErrChannel) {
325 mIsErrorCh[index] = false;
326 }
327
328 mFillErrChannel.clear();
329}
330
331//______________________________________________________________________
332
333void CalibTOFapi::processError(int crate, int trm, int mask)
334{
335 if (checkTRMPolicy(mask)) { // check the policy of TRM -> true=good TRM
336 return;
337 }
338 int ech = (crate << 12) + ((trm - 3) << 8);
339 for (int i = ech; i < ech + 256; i++) {
340 int channel = Geo::getCHFromECH(i);
341 if (channel == -1) {
342 continue;
343 }
344 mIsErrorCh[channel] = true;
345 mFillErrChannel.push_back(channel);
346 }
347}
348
349//______________________________________________________________________
350
352{
353 return false;
354}
355
356//______________________________________________________________________
357
358bool CalibTOFapi::isChannelError(int channel) const
359{
360 return mIsErrorCh[channel];
361}
ClassImp(o2::tof::CalibTOFapi)
Class to use TOF calibration (decalibration, calibration)
uint64_t phase
Definition RawEventData.h:7
int32_t i
StringRef key
static BasicCCDBManager & instance()
int storeAsTFileAny(const T *obj, std::string const &path, std::map< std::string, std::string > const &metadata, long startValidityTimestamp=-1, long endValidityTimestamp=-1, std::vector< char >::size_type maxSize=0) const
Definition CcdbApi.h:157
void init(std::string const &hosts)
Definition CcdbApi.cxx:165
float getLHCphase(int timestamp) const
float evalTimeSlewing(int channel, float tot) const
float getFractionUnderPeak(int sector, int channel) const
float getSigmaPeak(int sector, int channel) const
bool isChannelError(int channel) const
void writeLHCphase(LhcPhase *phase, std::map< std::string, std::string > metadataLHCphase, uint64_t minTimeSTamp, uint64_t maxTimeStamp)
void readTimeSlewingParamFromFile(const char *filename)
void writeTimeSlewingParam(SlewParam *param, std::map< std::string, std::string > metadataChannelCalib, uint64_t minTimeSTamp, uint64_t maxTimeStamp=0)
bool checkTRMPolicy(int mask) const
bool isNoisy(int ich)
void processError(int crate, int trm, int mask)
void loadActiveMap(TOFFEElightInfo *fee)
void setURL(const std::string url)
Definition CalibTOFapi.h:62
float getTimeDecalibration(int ich, float tot) const
bool isProblematic(int ich)
float getTimeCalibration(int ich, float tot) const
Diagnostic class for TOF.
Definition Diagnostic.h:32
uint32_t getFrequencyROW() const
Definition Diagnostic.h:38
void print(bool longFormat=false) const
uint32_t getFrequencyEmptyCrate(int crate) const
Definition Diagnostic.h:39
static int getCrate(ULong64_t pattern)
Definition Diagnostic.h:66
static ULong64_t getTRMKey(int crate, int trm)
static int getChannel(ULong64_t pattern)
Definition Diagnostic.h:67
uint32_t getFrequencyEmptyTOF() const
Definition Diagnostic.h:40
const std::map< ULong64_t, uint32_t > & getVector() const
Definition Diagnostic.h:88
bool isNoisyChannel(int channel, int thr=0) const
static int getSlot(ULong64_t pattern)
Definition Diagnostic.h:65
static constexpr Int_t NSTRIPXSECTOR
Definition Geo.h:116
static constexpr Int_t NPADS
Definition Geo.h:110
static Int_t getCHFromECH(int echan)
Definition Geo.h:352
@ kNCrate
Definition Geo.h:95
static constexpr int NCHANNELS
Definition Geo.h:124
GLuint index
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLenum GLfloat param
Definition glcorearb.h:271
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLint GLuint mask
Definition glcorearb.h:291
std::string filename()
bool getChannelEnabled(int idx) const
static constexpr int NCHANNELS
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"