Project
Loading...
Searching...
No Matches
NoiseCalibEPN.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 <TROOT.h>
13#include <TFile.h>
14#include <TH1.h>
15#include <TString.h>
16#include <TStyle.h>
17#include <TDirectory.h>
20#include "Framework/Logger.h"
21
22using namespace o2::zdc;
23
25{
26 if (mVerbosity > DbgMedium) {
27 mModuleConfig->print();
28 }
29
30 // Inspect reconstruction parameters
32 ropt.print();
33 mRopt = (o2::zdc::RecoParamZDC*)&ropt;
34
35 // Inspect calibration parameters
36 const auto& opt = CalibParamZDC::Instance();
37 if (mVerbosity > DbgMedium) {
38 opt.print();
39 }
40 if (opt.debugOutput == true) {
42 }
43
44 int nbx = 4096 * NTimeBinsPerBC - NTimeBinsPerBC + 1;
45 double xmin = -2048 * NTimeBinsPerBC - 0.5;
46 double xmax = 2047 * NTimeBinsPerBC + 0.5;
47
48 for (int isig = 0; isig < NChannels; isig++) {
49 // Baseline single samples
50 if (mH[0][isig] == nullptr) {
51 mH[0][isig] = new o2::dataformats::FlatHisto1D<double>(4096, -2048.5, 2047.5);
52 } else {
53 mH[0][isig]->clear();
54 }
55 // Baseline single samples (cumulated)
56 if (mHSum[0][isig] == nullptr) {
57 if (mSaveDebugHistos) {
58 mHSum[0][isig] = new o2::dataformats::FlatHisto1D<double>(4096, -2048.5, 2047.5);
59 }
60 } else {
61 mHSum[0][isig]->clear();
62 }
63 // Bunch average of baseline samples
64 if (mH[1][isig] == nullptr) {
65 mH[1][isig] = new o2::dataformats::FlatHisto1D<double>(nbx, xmin, xmax);
66 } else {
67 mH[1][isig]->clear();
68 }
69 // Bunch average of baseline samples (cumulated)
70 if (mHSum[1][isig] == nullptr) {
71 if (mSaveDebugHistos) {
72 mHSum[1][isig] = new o2::dataformats::FlatHisto1D<double>(nbx, xmin, xmax);
73 }
74 } else {
75 mHSum[1][isig]->clear();
76 }
77 // Difference between single samples and average
78 if (mH[2][isig] == nullptr) {
79 mH[2][isig] = new o2::dataformats::FlatHisto1D<double>(nbx, xmin / double(NTimeBinsPerBC), xmax / double(NTimeBinsPerBC));
80 } else {
81 mH[2][isig]->clear();
82 }
83 // Difference between single samples and average (cumulated)
84 if (mHSum[2][isig] == nullptr) {
85 if (mSaveDebugHistos) {
86 mHSum[2][isig] = new o2::dataformats::FlatHisto1D<double>(nbx, xmin / double(NTimeBinsPerBC), xmax / double(NTimeBinsPerBC));
87 }
88 } else {
89 mHSum[2][isig]->clear();
90 }
91 }
92
93 mData.clear();
95
96 // Fill maps to decode the pattern of channels with hit
97 for (int ich = 0; ich < NChannels; ich++) {
98 // If the reconstruction parameters were not manually set
99 if (ropt.amod[ich] < 0 || ropt.ach[ich] < 0) {
100 for (int im = 0; im < NModules; im++) {
101 for (uint32_t ic = 0; ic < NChPerModule; ic++) {
102 if (mModuleConfig->modules[im].channelID[ic] == ich && mModuleConfig->modules[im].readChannel[ic]) {
103 ropt.amod[ich] = im;
104 ropt.ach[ich] = ic;
105 // Fill mask to identify all channels
106 mChMask[ich] = (0x1 << (4 * im + ic));
107 goto next_ich;
108 }
109 }
110 }
111 }
112 next_ich:;
113 if (mVerbosity > DbgMinimal) {
114 LOG(info) << "Channel " << ich << "(" << ChannelNames[ich] << ") mod " << ropt.amod[ich] << " ch " << ropt.ach[ich];
115 }
116 }
117
118 mInitDone = true;
119 return 0;
120}
121
122//______________________________________________________________________________
124{
125 mData.clear();
126 for (int ihis = 0; ihis < NoiseCalibData::NHA; ihis++) {
127 for (int isig = 0; isig < NChannels; isig++) {
128 mH[ihis][isig]->clear();
129 }
130 }
131}
132
133//______________________________________________________________________________
134int NoiseCalibEPN::process(const gsl::span<const o2::zdc::OrbitData>& orbitData, const gsl::span<const o2::zdc::BCData>& bcdata, const gsl::span<const o2::zdc::ChannelData>& chdata)
135{
136 if (!mInitDone) {
137 init();
138 }
139
140 // Prepare orbit information only if we need to fill debug histograms
141 std::map<uint32_t, int> orbit; // Orbit map for fast access
142 float offset[NChannels]; // Offset in current orbit
143 for (int ich = 0; ich < NChannels; ich++) {
144 offset[ich] = std::numeric_limits<float>::infinity();
145 }
146 if (mSaveDebugHistos) {
147 int norb = orbitData.size();
148 // if (mVerbosity >= DbgFull) {
149 // LOG(info) << "Dump of pedestal data lookup table";
150 // }
151 for (int iorb = 0; iorb < norb; iorb++) {
152 orbit[orbitData[iorb].ir.orbit] = iorb;
153 // if (mVerbosity >= DbgFull) {
154 // LOG(info) << "orbitData[" << orbitData[iorb].ir.orbit << "] = " << iorb;
155 // }
156 }
157 }
158
159 auto nbc = bcdata.size();
160 for (int ibc = 1; ibc < nbc; ibc++) {
161 auto& bcp = bcdata[ibc - 1];
162 auto& bcc = bcdata[ibc];
163 if (bcc.ir.bc != 0 || bcp.ir.bc != 3563 || (bcp.ir.orbit + 1) != bcc.ir.orbit) {
164 continue;
165 }
166 auto chEnt = bcc.ref.getFirstEntry();
167 auto nch = bcc.ref.getEntries();
168 // Extract pedestal information only if we need to fill debug histograms
169 if (mSaveDebugHistos) {
170 std::map<uint32_t, int>::iterator it = orbit.find(bcc.ir.orbit);
171 if (it != orbit.end()) {
172 auto& orbitdata = orbitData[it->second];
173 for (int ich = 0; ich < NChannels; ich++) {
174 auto myped = float(orbitdata.data[ich]) * mModuleConfig->baselineFactor;
175 if (myped >= ADCMin && myped <= ADCMax) {
176 // Pedestal information is present for this channel
177 offset[ich] = myped;
178 } else {
179 offset[ich] = std::numeric_limits<float>::infinity();
180 }
181 }
182 } else {
183 for (int ich = 0; ich < NChannels; ich++) {
184 offset[ich] = std::numeric_limits<float>::infinity();
185 }
186 }
187 }
188
189 for (int ich = 0; ich < nch; ich++) {
190 const auto& chd = chdata[chEnt++];
191 if (chd.id < NChannels) {
192 // Check trigger flags
193 ModuleTriggerMapData mtc, mtp;
194 mtp.w = bcp.moduleTriggers[mRopt->amod[chd.id]];
195 mtc.w = bcc.moduleTriggers[mRopt->amod[chd.id]];
196 if (mtp.f.Auto_m // Auto trigger in bunch -2
197 || mtp.f.Auto_0 || mtp.f.Alice_0 || (bcp.triggers & mChMask[chd.id]) // Trigger or hit in bunch -1
198 || mtc.f.Auto_0 || mtc.f.Alice_0 || (bcc.triggers & mChMask[chd.id]) // Trigger or hit in bunch -2
199 || mtc.f.Auto_1 || mtc.f.Alice_1 // Trigger in bunch +1
200 ) {
201#ifdef O2_ZDC_DEBUG
202 printf("%u.%04u SKIP %s%s%s%s%s%s%s%s%s\n",
203 mtp.f.Auto_m ? "p.Auto_m" : "",
204 mtp.f.Auto_0 ? "p.Auto_0" : "",
205 mtp.f.Alice_0 ? "p.Alice_0" : "",
206 (bcp.triggers & mChMask[chd.id]) ? "p.HIT" : "",
207 mtc.f.Auto_0 ? "c.Auto_0" : "",
208 mtc.f.Alice_0 ? "c.Alice_0" : "",
209 (bcc.triggers & mChMask[chd.id]) ? "c.HIT" : "",
210 mtc.f.Auto_1 ? "c.Auto_1" : "",
211 mtc.f.Alice_1 ? "c.Alice_1" : "");
212#endif
213 continue;
214 }
215 int ss = 0;
216 int sq = 0;
217 for (int is = 0; is < NTimeBinsPerBC; is++) {
218 auto s = chd.data[is];
219 mH[0][chd.id]->fill(s);
220 if (mSaveDebugHistos) {
221 mHSum[0][chd.id]->fill(s);
222 }
223 ss += s;
224 sq += s * s;
225 }
226 int v = NTimeBinsPerBC * sq - ss * ss;
227 if (v > 0) {
228 // This should always be the case
229 mData.addEntry(chd.id, v);
230 if (mSaveDebugHistos) {
231 mDataSum.addEntry(chd.id, v);
232 }
233 }
234 // Debug histograms
235 mH[1][chd.id]->fill(ss);
236 mH[2][chd.id]->fill(ss / double(NTimeBinsPerBC) - offset[chd.id]);
237 if (mSaveDebugHistos) {
238 mHSum[1][chd.id]->fill(ss);
239 mHSum[2][chd.id]->fill(ss / double(NTimeBinsPerBC) - offset[chd.id]);
240 }
241 }
242 }
243 }
244 return 0;
245}
246
247//______________________________________________________________________________
249{
250 if (mSaveDebugHistos) {
251 if (mVerbosity > DbgZero) {
252 mDataSum.print();
253 }
255 }
256 return 0;
257}
258
259//______________________________________________________________________________
260int NoiseCalibEPN::saveDebugHistos(const std::string fn)
261{
262 if (mVerbosity > DbgZero) {
263 LOG(info) << "Saving EPN debug histograms on file " << fn;
264 }
265 int ierr = mDataSum.saveDebugHistos(fn, true);
266 if (ierr != 0) {
267 return ierr;
268 }
269 TDirectory* cwd = gDirectory;
270 // N.B. We update file here because it has just been recreated
271 TFile* f = new TFile(fn.data(), "update");
272 if (f->IsZombie()) {
273 LOG(error) << "Cannot update file: " << fn;
274 return 1;
275 }
276 for (int32_t is = 0; is < NChannels; is++) {
277 auto p = mHSum[0][is]->createTH1F(TString::Format("hs%d", is).Data());
278 p->SetTitle(TString::Format("EPN Baseline samples %s", ChannelNames[is].data()));
279 p->Write("", TObject::kOverwrite);
280 }
281 for (int32_t is = 0; is < NChannels; is++) {
282 auto p = mHSum[1][is]->createTH1F(TString::Format("hss%d", is).Data());
283 p->SetTitle(TString::Format("EPN Bunch sum of samples %s", ChannelNames[is].data()));
284 p->Write("", TObject::kOverwrite);
285 }
286 for (int32_t is = 0; is < NChannels; is++) {
287 auto p = mHSum[2][is]->createTH1F(TString::Format("hsd%d", is).Data());
288 p->SetTitle(TString::Format("EPN Baseline estimation difference %s", ChannelNames[is].data()));
289 p->Write("", TObject::kOverwrite);
290 }
291 f->Close();
292 cwd->cd();
293 return 0;
294}
ZDC calibration common parameters.
uint64_t orbit
Definition RawEventData.h:6
NoiseCalibData mDataSum
int saveDebugHistos(const std::string fn="ZDCNoiseCalibEPN.root")
std::array< std::array< o2::dataformats::FlatHisto1D< double > *, NChannels >, NoiseCalibData::NHA > mHSum
int process(const gsl::span< const o2::zdc::OrbitData > &orbitdata, const gsl::span< const o2::zdc::BCData > &bcdata, const gsl::span< const o2::zdc::ChannelData > &chdata)
std::array< std::array< o2::dataformats::FlatHisto1D< double > *, NChannels >, NoiseCalibData::NHA > mH
const GLdouble * v
Definition glcorearb.h:832
GLdouble f
Definition glcorearb.h:310
GLboolean * data
Definition glcorearb.h:298
GLintptr offset
Definition glcorearb.h:660
constexpr int NModules
Definition Constants.h:68
constexpr int NTimeBinsPerBC
Definition Constants.h:53
constexpr int NChPerModule
Definition Constants.h:69
constexpr int NChannels
Definition Constants.h:65
constexpr int DbgMinimal
Definition Constants.h:208
constexpr int DbgZero
Definition Constants.h:207
constexpr int ADCMin
Definition Constants.h:76
constexpr std::string_view ChannelNames[]
Definition Constants.h:147
constexpr int DbgMedium
Definition Constants.h:209
constexpr int ADCMax
Definition Constants.h:76
std::array< Module, MaxNModules > modules
int saveDebugHistos(const std::string fn, bool is_epn=false)
static constexpr int NHA
void addEntry(int isig, uint32_t val)
int32_t amod[NChannels]
int32_t ach[NChannels]
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
struct ModuleTriggerMap f
Definition BCData.h:48