Project
Loading...
Searching...
No Matches
CalibTreeDump.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 <fmt/format.h>
13#include <cstdlib>
14#include <string_view>
15#include <fstream>
16#include <iostream>
17#include <sstream>
18#include <cassert>
19
20#include "TFile.h"
21#include "TTree.h"
22#include "TMath.h"
23
24#include "MathUtils/fit.h"
25#include "TPCBase/CalArray.h"
26#include "TPCBase/CalDet.h"
27#include "TPCBase/CRU.h"
28#include "TPCBase/Mapper.h"
29#include "TPCBase/Utils.h"
30#include "TPCBase/Sector.h"
31
33
35
36using namespace o2::tpc;
37
39{
40 for (auto& [name, calDet] : calibs) {
41 calDet.setName(name);
42 mCalDetObjects.emplace_back(&calDet);
43 }
44}
45
46void CalibTreeDump::addCalPads(const std::string_view file, const std::string_view calPadNames)
47{
48 auto calPads = utils::readCalPads(file, calPadNames);
49 for (auto calPad : calPads) {
50 add(calPad);
51 }
52}
53
54//______________________________________________________________________________
55void CalibTreeDump::dumpToFile(const std::string filename)
56{
57 // ===| open file and crate tree |============================================
58 std::unique_ptr<TFile> file(TFile::Open(filename.c_str(), "recreate"));
59 auto tree = new TTree("calibTree", "Calibration data object tree");
60
61 // ===| add default mappings |================================================
62 addDefaultMapping(tree);
63
64 // ===| add FEE mapping |=====================================================
65 if (mAddFEEInfo) {
66 addFEEMapping(tree);
67 }
68
69 // ===| fill calDet objects |=================================================
70 addCalDetObjects(tree);
71
72 // ===| default aliases |=====================================================
73 setDefaultAliases(tree);
74
75 file->Write();
76}
77
78//______________________________________________________________________________
79void CalibTreeDump::addDefaultMapping(TTree* tree)
80{
81 // loop over ROCs
82 //
83
84 // ===| mapper |==============================================================
85 const auto& mapper = Mapper::instance();
86
87 // ===| default mapping objects |=============================================
88 uint16_t rocNumber = 0;
89 // positions
90 std::vector<float> gx;
91 std::vector<float> gy;
92 std::vector<float> lx;
93 std::vector<float> ly;
94 // row and pad
95 std::vector<unsigned char> row;
96 std::vector<unsigned char> pad;
97 std::vector<short> cpad;
98
99 // ===| add branches with default mappings |==================================
100 tree->Branch("roc", &rocNumber);
101 tree->Branch("gx", &gx);
102 tree->Branch("gy", &gy);
103 tree->Branch("lx", &lx);
104 tree->Branch("ly", &ly);
105 tree->Branch("row", &row);
106 tree->Branch("pad", &pad);
107 tree->Branch("cpad", &cpad);
108
109 // ===| loop over readout chambers |==========================================
110 for (ROC roc; !roc.looped(); ++roc) {
111 rocNumber = roc;
112
113 // ===| clear position vectors |============================================
114 gx.clear();
115 gy.clear();
116 lx.clear();
117 ly.clear();
118 row.clear();
119 pad.clear();
120 cpad.clear();
121
122 // ===| loop over pad rows |================================================
123 const int numberOfRows = mapper.getNumberOfRowsROC(roc);
124 for (int irow = 0; irow < numberOfRows; ++irow) {
125
126 // ===| loop over pads in row |===========================================
127 const int numberOfPadsInRow = mapper.getNumberOfPadsInRowROC(roc, irow);
128 for (int ipad = 0; ipad < numberOfPadsInRow; ++ipad) {
129 const PadROCPos padROCPos(rocNumber, irow, ipad);
130 const PadPos padPos = mapper.getGlobalPadPos(padROCPos); // pad and row in sector
131 const PadCentre& localPadXY = mapper.getPadCentre(padPos);
132 const LocalPosition2D globalPadXY = mapper.getPadCentre(padROCPos);
133
134 gx.emplace_back(globalPadXY.X());
135 gy.emplace_back(globalPadXY.Y());
136 lx.emplace_back(localPadXY.X());
137 ly.emplace_back(localPadXY.Y());
138
139 row.emplace_back(irow);
140 pad.emplace_back(ipad);
141 cpad.emplace_back(ipad - numberOfPadsInRow / 2);
142 }
143 }
144
145 tree->Fill();
146 }
147}
148
149//______________________________________________________________________________
150void CalibTreeDump::addFEEMapping(TTree* tree)
151{
152 // loop over ROCs
153 //
154
155 // ===| mapper |==============================================================
156 const auto& mapper = Mapper::instance();
157
158 mTraceLengthIROC = mapper.getTraceLengthsIROC();
159 mTraceLengthOROC = mapper.getTraceLengthsOROC();
160
161 // ===| default mapping objects |=============================================
162 // FEC
163 std::vector<unsigned char> fecInSector;
164 std::vector<unsigned char> sampaOnFEC;
165 std::vector<unsigned char> channelOnSampa;
166 // trace length
167 std::vector<float>* traceLength = nullptr;
168
169 // ===| add branches with default mappings |==================================
170 auto brFecInSector = tree->Branch("fecInSector", &fecInSector);
171 auto brSampaOnFEC = tree->Branch("sampaOnFEC", &sampaOnFEC);
172 auto brChannelOnSampa = tree->Branch("channelOnSampa", &channelOnSampa);
173 auto brTraceLength = tree->Branch("traceLength", &traceLength);
174
175 // ===| loop over readout chambers |==========================================
176 for (ROC roc; !roc.looped(); ++roc) {
177 int rocNumber = roc;
178 // tree->GetEntry(rocNumber);
179 traceLength = ((roc.rocType() == RocType::IROC) ? &mTraceLengthIROC : &mTraceLengthOROC);
180
181 // ===| clear position vectors |============================================
182 fecInSector.clear();
183 sampaOnFEC.clear();
184 channelOnSampa.clear();
185
186 const int rowOffset = (roc.rocType() == RocType::OROC) ? mapper.getPadsInIROC() : 0;
187 // ===| loop over pad rows |================================================
188 const int numberOfRows = mapper.getNumberOfRowsROC(roc);
189 for (int irow = 0; irow < numberOfRows; ++irow) {
190
191 // ===| loop over pads in row |===========================================
192 const int numberOfPadsInRow = mapper.getNumberOfPadsInRowROC(roc, irow);
193 for (int ipad = 0; ipad < numberOfPadsInRow; ++ipad) {
194 const PadROCPos padROCPos(rocNumber, irow, ipad);
195 const auto& fecInfo = mapper.getFECInfo(padROCPos);
196 const auto globalPadNumber = mapper.globalPadNumber(fecInfo);
197
198 const CRU cru(mapper.getCRU(roc.getSector(), globalPadNumber));
199
200 fecInSector.emplace_back(fecInfo.getIndex());
201 sampaOnFEC.emplace_back(fecInfo.getSampaChip());
202 channelOnSampa.emplace_back(fecInfo.getSampaChannel());
203 }
204 }
205 brFecInSector->Fill();
206 brSampaOnFEC->Fill();
207 brChannelOnSampa->Fill();
208 brTraceLength->Fill();
209 }
210}
211
212//______________________________________________________________________________
213void CalibTreeDump::addCalDetObjects(TTree* tree)
214{
215
216 int iter = 0;
217 for (auto pcalDet : mCalDetObjects) {
218 // ===| branch names |===
219 if (!pcalDet) {
220 continue;
221 }
222 auto& calDet = *pcalDet;
223 std::string name = calDet.getName();
224
225 if (name == "PadCalibrationObject" || name.size() == 0) {
226 name = fmt::format(fmt::runtime("calDet_{%02d}"), iter);
227 }
228
229 std::string meanName = fmt::format("{}_mean", name);
230 std::string stdDevName = fmt::format("{}_stdDev", name);
231 std::string medianName = fmt::format("{}_median", name);
232 std::string median1Name = fmt::format("{}_median1", name);
233 std::string median2Name = fmt::format("{}_median2", name);
234 std::string median3Name = fmt::format("{}_median3", name);
235
236 // ===| branch variables |===
237 std::vector<float>* data = nullptr;
238 float mean{};
239 float stdDev{};
240 float median[4]{};
241
242 // ===| branch definitions |===
243 TBranch* brMean = tree->Branch(meanName.data(), &mean);
244 TBranch* brStdDev = tree->Branch(stdDevName.data(), &stdDev);
245 TBranch* brMedian = tree->Branch(medianName.data(), &median[0]);
246 TBranch* brMedian1 = tree->Branch(median1Name.data(), &median[1]);
247 TBranch* brMedian2 = tree->Branch(median2Name.data(), &median[2]);
248 TBranch* brMedian3 = tree->Branch(median3Name.data(), &median[3]);
249 TBranch* brData = tree->Branch(name.data(), &data);
250
251 // ===| loop over ROCs and fill |===
252 int roc = 0;
253 for (auto& calArray : calDet.getData()) {
254 // ---| set data |---
255 data = &calArray.getData();
256
257 // ---| statistics |---
258 mean = TMath::Mean(data->begin(), data->end());
259 stdDev = TMath::StdDev(data->begin(), data->end());
260 median[0] = median[1] = median[2] = median[3] = TMath::Median(data->size(), data->data());
261 if (roc > 35) {
262 median[1] = TMath::Median(Mapper::getPadsInOROC1(), data->data());
263 median[2] = TMath::Median(Mapper::getPadsInOROC2(), data->data() + Mapper::getPadsInOROC1());
265 }
266
267 // ---| filling |---
268 brData->Fill();
269 brMean->Fill();
270 brStdDev->Fill();
271 brMedian->Fill();
272 brMedian1->Fill();
273 brMedian2->Fill();
274 brMedian3->Fill();
275 ++roc;
276 }
277 }
278}
279
280//______________________________________________________________________________
281void CalibTreeDump::setDefaultAliases(TTree* tree)
282{
283 tree->SetAlias("sector", "roc%36");
284 tree->SetAlias("padsPerRow", "2*(pad-cpad)");
285 tree->SetAlias("isEdgePad", "(pad==0) || (pad==padsPerRow-1)");
286 tree->SetAlias("rowInSector", "row + (roc>35)*63");
287 tree->SetAlias("padWidth", "0.4 + (roc > 35) * 0.2");
288 tree->SetAlias("padHeight", "0.75 + (rowInSector > 62) * 0.25 + (rowInSector > 96) * 0.2 + (rowInSector > 126) * 0.3");
289 tree->SetAlias("padArea", "padWidth * padHeight");
290
291 tree->SetAlias("cruInSector", "(rowInSector >= 17) + (rowInSector >= 32) + (rowInSector >= 48) + (rowInSector >= 63) + (rowInSector >= 81) + (rowInSector >= 97) + (rowInSector >= 113) + (rowInSector >= 127) + (rowInSector >= 140)");
292 tree->SetAlias("cruID", "cruInSector + sector*10");
293 tree->SetAlias("region", "cruInSector");
294 tree->SetAlias("partition", "int(cruInSector / 2)");
295
296 tree->SetAlias("padWidth", "(region == 0) * 0.416 + (region == 1) * 0.42 + (region == 2) * 0.42 + (region == 3) * 0.436 + (region == 4) * 0.6 + (region == 5) * 0.6 + (region == 6) * 0.608 + (region == 7) * 0.588 + (region == 8) * 0.604 + (region == 9) * 0.607");
297 tree->SetAlias("padHeight", "0.75 + (region>3)*0.25 + (region>5)*0.2 + (region>7)*0.3");
298 tree->SetAlias("padArea", "padHeight * padWidth");
299
300 tree->SetAlias("IROC", "roc < 36");
301 tree->SetAlias("OROC", "roc >= 36");
302 tree->SetAlias("OROC1", "partition == 2");
303 tree->SetAlias("OROC2", "partition == 3");
304 tree->SetAlias("OROC3", "partition == 4");
305
306 tree->SetAlias("A_Side", "sector < 18");
307 tree->SetAlias("C_Side", "sector >= 18");
308
309 if (mAddFEEInfo) {
310 tree->SetAlias("fecID", "fecInSector + sector * 91");
311 tree->SetAlias("sampaInSector", "sampaOnFEC + 5 * fecInSector");
312 tree->SetAlias("channelOnFEC", "channelOnSampa + 32 * sampaOnFEC");
313 tree->SetAlias("channelInSector", "channelOnFEC + 160 * fecInSector");
314 }
315}
uint32_t roc
Definition RawData.h:3
const auto & getData()
void addCalPads(const std::string_view file, const std::string_view calPadNames)
Add CalPad objects from a file.
std::unordered_map< std::string, DataTypes > CalPadMapType
void dumpToFile(const std::string filename="CalibTree.root")
Dump the registered calibration data to file.
void add(CalDet< T > *calDet)
Add CalDet object.
static constexpr unsigned short getPadsInOROC3()
Definition Mapper.h:412
static constexpr unsigned short getPadsInOROC1()
Definition Mapper.h:410
static Mapper & instance(const std::string mappingDir="")
Definition Mapper.h:44
static constexpr unsigned short getPadsInOROC2()
Definition Mapper.h:411
Pad and row inside a ROC.
Definition PadROCPos.h:37
bool looped() const
if increment operator went above MaxROC
Definition ROC.h:108
GLuint const GLchar * name
Definition glcorearb.h:781
GLboolean * data
Definition glcorearb.h:298
double mean(std::vector< double >::const_iterator first, std::vector< double >::const_iterator last)
R median(std::vector< T > v)
Definition fit.h:519
std::vector< CalPad * > readCalPads(const std::string_view fileName, const std::vector< std::string > &calPadNames)
Definition Utils.cxx:190
Global TPC definitions and constants.
Definition SimTraits.h:167
@ OROC
Definition Defs.h:49
@ IROC
Definition Defs.h:48
std::string filename()
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
std::vector< int > row