-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathMatLayerCylSet.cxx
More file actions
659 lines (601 loc) · 24.4 KB
/
MatLayerCylSet.cxx
File metadata and controls
659 lines (601 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file MatLayerCylSet.cxx
/// \brief Implementation of the wrapper for the set of cylindrical material layers
#include "DetectorsBase/MatLayerCylSet.h"
#include "CommonConstants/MathConstants.h"
#ifndef GPUCA_ALIGPUCODE // this part is unvisible on GPU version
#include "GPUCommonLogger.h"
#include <TFile.h>
#include "CommonUtils/TreeStreamRedirector.h"
//#define _DBG_LOC_ // for local debugging only
#endif // !GPUCA_ALIGPUCODE
#undef NDEBUG
using namespace o2::base;
using flatObject = o2::gpu::FlatObject;
#ifndef GPUCA_ALIGPUCODE // this part is unvisible on GPU version
//________________________________________________________________________________
void MatLayerCylSet::addLayer(float rmin, float rmax, float zmax, float dz, float drphi)
{
// add new layer checking for overlaps
assert(mConstructionMask != Constructed);
assert(rmin < rmax && zmax > 0 && dz > 0 && drphi > 0);
mConstructionMask = InProgress;
int nlr = getNLayers();
if (!nlr) {
// book local storage
auto sz = sizeof(MatLayerCylSetLayout);
o2::gpu::resizeArray(mFlatBufferContainer, 0, sz);
mFlatBufferPtr = mFlatBufferContainer;
mFlatBufferSize = sz;
//--------------????
get()->mRMin = 1.e99;
get()->mRMax = 0.;
}
for (int il = 0; il < nlr; il++) {
const auto& lr = getLayer(il);
if (lr.getRMax() > rmin && rmax > lr.getRMin()) {
LOG(fatal) << "new layer overlaps with layer " << il;
}
}
auto* oldLayers = o2::gpu::resizeArray(get()->mLayers, nlr, nlr + 1);
// dynamyc buffers of old layers were used in new ones, detach them
for (int i = nlr; i--;) {
oldLayers[i].clearInternalBufferPtr();
}
delete[] oldLayers;
get()->mLayers[nlr].initSegmentation(rmin, rmax, zmax, dz, drphi);
get()->mNLayers++;
get()->mRMin = get()->mRMin > rmin ? rmin : get()->mRMin;
get()->mRMax = get()->mRMax < rmax ? rmax : get()->mRMax;
get()->mZMax = get()->mZMax < zmax ? zmax : get()->mZMax;
get()->mRMin2 = get()->mRMin * get()->mRMin;
get()->mRMax2 = get()->mRMax * get()->mRMax;
}
//________________________________________________________________________________
void MatLayerCylSet::populateFromTGeo(int ntrPerCell)
{
///< populate layers, using ntrPerCell test tracks per cell
assert(mConstructionMask == InProgress);
int nlr = getNLayers();
if (!nlr) {
LOG(error) << "The LUT is not yet initialized";
return;
}
if (get()->mR2Intervals) {
LOG(error) << "The LUT is already populated";
return;
}
for (int i = 0; i < nlr; i++) {
printf("Populating with %d trials Lr %3d ", ntrPerCell, i);
get()->mLayers[i].print();
get()->mLayers[i].populateFromTGeo(ntrPerCell);
}
finalizeStructures();
}
//________________________________________________________________________________
void MatLayerCylSet::finalizeStructures()
{
// build layer search structures
assert(mConstructionMask == InProgress);
int nlr = getNLayers();
int nR2Int = 2 * (nlr + 1);
o2::gpu::resizeArray(get()->mR2Intervals, 0, nR2Int);
o2::gpu::resizeArray(get()->mInterval2LrID, 0, nR2Int);
get()->mR2Intervals[0] = get()->mRMin2;
get()->mR2Intervals[1] = get()->mRMax2;
get()->mInterval2LrID[0] = 0;
auto& nRIntervals = get()->mNRIntervals;
nRIntervals = 1;
for (int i = 1; i < nlr; i++) {
const auto& lr = getLayer(i);
if (o2::math_utils::sqrt(lr.getRMin2()) > o2::math_utils::sqrt(get()->mR2Intervals[nRIntervals] + Ray::Tiny)) {
// register gap
get()->mInterval2LrID[nRIntervals] = -1;
get()->mR2Intervals[++nRIntervals] = lr.getRMin2();
}
get()->mInterval2LrID[nRIntervals] = i;
get()->mR2Intervals[++nRIntervals] = lr.getRMax2();
}
delete[] o2::gpu::resizeArray(get()->mInterval2LrID, nR2Int, nRIntervals); // rebook with precise size
delete[] o2::gpu::resizeArray(get()->mR2Intervals, nR2Int, ++nRIntervals); // rebook with precise size
//
}
//________________________________________________________________________________
void MatLayerCylSet::dumpToTree(const std::string& outName) const
{
/// dump per cell info to the tree
o2::utils::TreeStreamRedirector dump(outName.data(), "recreate");
for (int i = 0; i < getNLayers(); i++) {
const auto& lr = getLayer(i);
float r = 0.5 * (lr.getRMin() + lr.getRMax());
// per cell dump
int nphib = lr.getNPhiBins();
for (int ip = 0; ip < nphib; ip++) {
float phi = 0.5 * (lr.getPhiBinMin(ip) + lr.getPhiBinMax(ip));
float sn, cs;
int ips = lr.phiBin2Slice(ip);
char merge = 0; // not mergeable
if (ip + 1 < nphib) {
int ips1 = lr.phiBin2Slice(ip + 1);
merge = ips == ips1 ? -1 : lr.canMergePhiSlices(ips, ips1); // -1 for already merged
} else {
merge = -2; // last one
}
o2::math_utils::sincos(phi, sn, cs);
float x = r * cs, y = r * sn;
for (int iz = 0; iz < lr.getNZBins(); iz++) {
float z = 0.5 * (lr.getZBinMin(iz) + lr.getZBinMax(iz));
auto cell = lr.getCellPhiBin(ip, iz);
dump << "cell"
<< "ilr=" << i << "r=" << r << "phi=" << phi << "x=" << x << "y=" << y << "z=" << z << "ip=" << ip << "ips=" << ips << "iz=" << iz
<< "mrgnxt=" << merge << "val=" << cell << "\n";
}
}
//
// statistics per layer
MatCell mean, rms;
lr.getMeanRMS(mean, rms);
dump << "lay"
<< "ilr=" << i << "r=" << r << "mean=" << mean << "rms=" << rms << "\n";
}
}
//________________________________________________________________________________
void MatLayerCylSet::writeToFile(const std::string& outFName)
{
/// store to file
TFile outf(outFName.data(), "recreate");
if (outf.IsZombie()) {
return;
}
outf.WriteObjectAny(this, Class(), "ccdb_object");
outf.Close();
}
void MatLayerCylSet::initLayerVoxelLU()
{
if (mInitializedLayerVoxelLU) {
LOG(info) << "Layer voxel already initialized; Aborting";
return;
}
LOG(info) << "Initializing voxel layer lookup";
// do some check if voxels are dimensioned correctly
if (LayerRMax < get()->mRMax) {
LOG(fatal) << "Cannot initialized layer voxel lookup due to dimension problem (fix constants in MatLayerCylSet.h)";
}
for (int voxel = 0; voxel < NumVoxels; ++voxel) {
// check the 2 extremes of this voxel "covering"
const auto lowerR = voxel * VoxelRDelta;
const auto upperR = lowerR + VoxelRDelta;
const auto lowerSegment = searchSegment(lowerR * lowerR);
const auto upperSegment = searchSegment(upperR * upperR);
mLayerVoxelLU[2 * voxel] = lowerSegment;
mLayerVoxelLU[2 * voxel + 1] = upperSegment;
}
mInitializedLayerVoxelLU = true;
}
//________________________________________________________________________________
MatLayerCylSet* MatLayerCylSet::loadFromFile(const std::string& inpFName)
{
TFile inpf(inpFName.data());
if (inpf.IsZombie()) {
LOG(error) << "Failed to open input file " << inpFName;
return nullptr;
}
MatLayerCylSet* mb = reinterpret_cast<MatLayerCylSet*>(inpf.GetObjectChecked("ccdb_object", Class()));
if (!mb && !(mb = reinterpret_cast<MatLayerCylSet*>(inpf.GetObjectChecked("MatBud", Class())))) { // for old objects
LOG(error) << "Failed to load mat.LUT from " << inpFName;
return nullptr;
}
auto rptr = rectifyPtrFromFile(mb);
return rptr;
}
//________________________________________________________________________________
MatLayerCylSet* MatLayerCylSet::rectifyPtrFromFile(MatLayerCylSet* ptr)
{
// rectify object loaded from file
if (ptr && !ptr->get()) {
ptr->fixPointers();
}
ptr->initLayerVoxelLU();
return ptr;
}
//________________________________________________________________________________
void MatLayerCylSet::optimizePhiSlices(float maxRelDiff)
{
// merge similar (whose relative budget does not differ within maxRelDiff) phi slices
assert(mConstructionMask == InProgress);
for (int i = getNLayers(); i--;) {
get()->mLayers[i].optimizePhiSlices(maxRelDiff);
}
// flatten(); // RS: TODO
}
//________________________________________________________________________________
void MatLayerCylSet::print(bool data) const
{
///< print layer data
if (!get()) {
printf("Not initialized yet\n");
return;
}
if (mConstructionMask != Constructed) {
LOG(warning) << "Object is not yet flattened";
}
for (int i = 0; i < getNLayers(); i++) {
printf("#%3d | ", i);
getLayer(i).print(data);
}
printf("%.2f < R < %.2f %d layers with total size %.2f MB\n", getRMin(), getRMax(), getNLayers(),
float(getFlatBufferSize()) / 1024 / 1024);
}
//________________________________________________________________________________
void MatLayerCylSet::scaleLayersByID(int lrFrom, int lrTo, float factor, bool _x2x0, bool _rho)
{
lrFrom = std::max(0, std::min(lrFrom, get()->mNLayers - 1));
lrTo = std::max(0, std::min(lrTo, get()->mNLayers - 1));
int dir = lrFrom >= lrTo ? -1 : 1;
lrTo += dir;
for (int i = lrFrom; i != lrTo; i += dir) {
get()->mLayers[i].scale(factor, _x2x0, _rho);
}
}
//________________________________________________________________________________
void MatLayerCylSet::scaleLayersByR(float rFrom, float rTo, float factor, bool _x2x0, bool _rho)
{
if (rFrom > rTo) {
std::swap(rFrom, rTo);
}
Ray ray(std::max(getRMin(), rFrom), 0., 0., std::min(getRMax(), rTo), 0., 0.);
short lmin, lmax;
if (!getLayersRange(ray, lmin, lmax)) {
LOGP(warn, "No layers found for {} < r < {}", rFrom, rTo);
return;
}
scaleLayersByID(lmin, lmax, factor, _x2x0, _rho);
}
#endif //!GPUCA_ALIGPUCODE
#ifndef GPUCA_GPUCODE
//________________________________________________________________________________
std::size_t MatLayerCylSet::estimateFlatBufferSize() const
{
std::size_t sz = alignSize(sizeof(MatLayerCylSetLayout), getBufferAlignmentBytes()); // hold data members
sz = alignSize(sz + get()->mNLayers * sizeof(MatLayerCyl), MatLayerCyl::getClassAlignmentBytes());
sz = alignSize(sz + (get()->mNRIntervals + 1) * sizeof(float), getBufferAlignmentBytes());
sz = alignSize(sz + get()->mNRIntervals * sizeof(int), getBufferAlignmentBytes());
for (int i = 0; i < getNLayers(); i++) {
sz = alignSize(sz + getLayer(i).estimateFlatBufferSize(), getBufferAlignmentBytes());
}
return sz;
}
#endif // ! GPUCA_GPUCODE
//_________________________________________________________________________________________________
GPUd() MatBudget MatLayerCylSet::getMatBudget(float x0, float y0, float z0, float x1, float y1, float z1) const
{
// get material budget traversed on the line between point0 and point1
MatBudget rval;
Ray ray(x0, y0, z0, x1, y1, z1);
short lmin, lmax; // get innermost and outermost relevant layer
if (ray.isTooShort() || !getLayersRange(ray, lmin, lmax)) {
rval.length = ray.getDist();
return rval;
}
short lrID = lmax;
while (lrID >= lmin) { // go from outside to inside
const auto& lr = getLayer(lrID);
int nphiSlices = lr.getNPhiSlices();
int nc = ray.crossLayer(lr); // determines how many crossings this ray has with this tubular layer
for (int ic = nc; ic--;) {
float cross1, cross2;
ray.getCrossParams(ic, cross1, cross2); // tmax,tmin of crossing the layer
auto phi0 = ray.getPhi(cross1), phi1 = ray.getPhi(cross2), dPhi = phi0 - phi1;
auto phiID = lr.getPhiSliceID(phi0), phiIDLast = lr.getPhiSliceID(phi1);
// account for eventual wrapping around 0
if (dPhi > 0.f) {
if (dPhi > o2::constants::math::PI) { // wraps around phi=0
phiIDLast += nphiSlices;
}
} else {
if (dPhi < -o2::constants::math::PI) { // wraps around phi=0
phiID += nphiSlices;
}
}
int stepPhiID = phiID > phiIDLast ? -1 : 1;
bool checkMorePhi = true;
auto tStartPhi = cross1, tEndPhi = 0.f;
do {
// get the path in the current phi slice
if (phiID == phiIDLast) {
tEndPhi = cross2;
checkMorePhi = false;
} else { // last phi slice still not reached
tEndPhi = ray.crossRadial(lr, (stepPhiID > 0 ? phiID + 1 : phiID) % nphiSlices);
if (tEndPhi == Ray::InvalidT) {
break; // ray parallel to radial line, abandon check for phi bin change
}
}
auto zID = lr.getZBinID(ray.getZ(tStartPhi));
auto zIDLast = lr.getZBinID(ray.getZ(tEndPhi));
// check if Zbins are crossed
#ifdef _DBG_LOC_
printf("-- Zdiff (%3d : %3d) mode: t: %+e %+e\n", zID, zIDLast, tStartPhi, tEndPhi);
#endif
if (zID != zIDLast) {
auto stepZID = zID < zIDLast ? 1 : -1;
bool checkMoreZ = true;
auto tStartZ = tStartPhi, tEndZ = 0.f;
do {
if (zID == zIDLast) {
tEndZ = tEndPhi;
checkMoreZ = false;
} else {
tEndZ = ray.crossZ(lr.getZBinMin(stepZID > 0 ? zID + 1 : zID));
if (tEndZ == Ray::InvalidT) { // track normal to Z axis, abandon Zbin change test
break;
}
}
// account materials of this step
float step = tEndZ > tStartZ ? tEndZ - tStartZ : tStartZ - tEndZ; // the real step is ray.getDist(tEnd-tStart), will rescale all later
const auto& cell = lr.getCell(phiID % nphiSlices, zID);
rval.meanRho += cell.meanRho * step;
rval.meanX2X0 += cell.meanX2X0 * step;
rval.length += step;
#ifdef _DBG_LOC_
float pos0[3] = {ray.getPos(tStartZ, 0), ray.getPos(tStartZ, 1), ray.getPos(tStartZ, 2)};
float pos1[3] = {ray.getPos(tEndZ, 0), ray.getPos(tEndZ, 1), ray.getPos(tEndZ, 2)};
printf(
"Lr#%3d / cross#%d : account %f<t<%f at phiSlice %d | Zbin: %3d (%3d) |[%+e %+e +%e]:[%+e %+e %+e] "
"Step: %.3e StrpCor: %.3e\n",
lrID, ic, tEndZ, tStartZ, phiID % nphiSlices, zID, zIDLast,
pos0[0], pos0[1], pos0[2], pos1[0], pos1[1], pos1[2], step, ray.getDist(step));
#endif
tStartZ = tEndZ;
zID += stepZID;
} while (checkMoreZ);
} else {
float step = tEndPhi > tStartPhi ? tEndPhi - tStartPhi : tStartPhi - tEndPhi; // the real step is |ray.getDist(tEnd-tStart)|, will rescale all later
const auto& cell = lr.getCell(phiID % nphiSlices, zID);
rval.meanRho += cell.meanRho * step;
rval.meanX2X0 += cell.meanX2X0 * step;
rval.length += step;
#ifdef _DBG_LOC_
float pos0[3] = {ray.getPos(tStartPhi, 0), ray.getPos(tStartPhi, 1), ray.getPos(tStartPhi, 2)};
float pos1[3] = {ray.getPos(tEndPhi, 0), ray.getPos(tEndPhi, 1), ray.getPos(tEndPhi, 2)};
printf(
"Lr#%3d / cross#%d : account %f<t<%f at phiSlice %d | Zbin: %3d ----- |[%+e %+e +%e]:[%+e %+e %+e]"
"Step: %.3e StrpCor: %.3e\n",
lrID, ic, tEndPhi, tStartPhi, phiID % nphiSlices, zID,
pos0[0], pos0[1], pos0[2], pos1[0], pos1[1], pos1[2], step, ray.getDist(step));
#endif
}
//
tStartPhi = tEndPhi;
phiID += stepPhiID;
} while (checkMorePhi);
}
lrID--;
} // loop over layers
if (rval.length != 0.f) {
rval.meanRho /= rval.length; // average
rval.meanX2X0 *= ray.getDist(); // normalize
}
rval.length = ray.getDist();
#ifdef _DBG_LOC_
printf("<rho> = %e, x2X0 = %e | step = %e\n", rval.meanRho, rval.meanX2X0, rval.length);
#endif
return rval;
}
//_________________________________________________________________________________________________
GPUd() bool MatLayerCylSet::getLayersRange(const Ray& ray, short& lmin, short& lmax) const
{
// get range of layers corresponding to rmin/rmax
//
lmin = lmax = -1;
float rmin2, rmax2;
ray.getMinMaxR2(rmin2, rmax2);
if (rmin2 >= getRMax2() || rmax2 <= getRMin2()) {
return false;
}
int lmxInt, lmnInt;
if (!mInitializedLayerVoxelLU) {
lmxInt = rmax2 < getRMax2() ? searchSegment(rmax2, 0) : get()->mNRIntervals - 2;
lmnInt = rmin2 >= getRMin2() ? searchSegment(rmin2, 0, lmxInt + 1) : 0;
} else {
lmxInt = rmax2 < getRMax2() ? searchLayerFast(rmax2, 0) : get()->mNRIntervals - 2;
lmnInt = rmin2 >= getRMin2() ? searchLayerFast(rmin2, 0, lmxInt + 1) : 0;
}
const auto* interval2LrID = get()->mInterval2LrID;
lmax = interval2LrID[lmxInt];
lmin = interval2LrID[lmnInt];
// make sure lmnInt and/or lmxInt are not in the gap
if (lmax < 0) {
lmax = interval2LrID[lmxInt - 1]; // rmax2 is in the gap, take highest layer below rmax2
}
if (lmin < 0) {
lmin = interval2LrID[lmnInt + 1]; // rmin2 is in the gap, take lowest layer above rmin2
}
return lmin <= lmax; // valid if both are not in the same gap
}
GPUd() int MatLayerCylSet::searchLayerFast(float r2, int low, int high) const
{
// we can avoid the sqrt .. at the cost of more memory in the lookup
const auto index = 2 * int(o2::gpu::CAMath::Sqrt(r2) * InvVoxelRDelta);
const auto layersfirst = mLayerVoxelLU[index];
const auto layerslast = mLayerVoxelLU[index + 1];
if (layersfirst != layerslast) {
// this means the voxel is undecided and we revert to search
return searchSegment(r2, layersfirst, layerslast + 1);
}
return layersfirst;
}
GPUd() int MatLayerCylSet::searchSegment(float val, int low, int high) const
{
///< search segment val belongs to. The val MUST be within the boundaries
if (low < 0) {
low = 0;
}
if (high < 0) {
high = get()->mNRIntervals;
}
int mid = (low + high) >> 1;
const auto* r2Intervals = get()->mR2Intervals;
while (mid != low) {
if (val < r2Intervals[mid]) {
high = mid;
} else {
low = mid;
}
mid = (low + high) >> 1;
}
return mid;
}
#ifndef GPUCA_ALIGPUCODE // this part is unvisible on GPU version
void MatLayerCylSet::flatten()
{
// make object flat: move all content to single internally allocated buffer
assert(mConstructionMask == InProgress);
int sz = estimateFlatBufferSize();
// create new internal buffer with total size and copy data
delete[] o2::gpu::resizeArray(mFlatBufferContainer, mFlatBufferSize, sz);
mFlatBufferPtr = mFlatBufferContainer;
mFlatBufferSize = sz;
int nLr = getNLayers();
auto offs = alignSize(sizeof(MatLayerCylSetLayout), getBufferAlignmentBytes()); // account for the alignment
// move array of layer pointers to the flat array
auto* oldLayers = o2::gpu::resizeArray(get()->mLayers, nLr, nLr, (MatLayerCyl*)(mFlatBufferPtr + offs));
// dynamyc buffers of old layers were used in new ones, detach them
for (int i = nLr; i--;) {
oldLayers[i].clearInternalBufferPtr();
}
delete[] oldLayers;
offs = alignSize(offs + nLr * sizeof(MatLayerCyl), MatLayerCyl::getClassAlignmentBytes()); // account for the alignment
// move array of R2 boundaries to the flat array
delete[] o2::gpu::resizeArray(get()->mR2Intervals, nLr + 1, nLr + 1, (float*)(mFlatBufferPtr + offs));
offs = alignSize(offs + (nLr + 1) * sizeof(float), getBufferAlignmentBytes()); // account for the alignment
// move array of R2 boundaries to the flat array
delete[] o2::gpu::resizeArray(get()->mInterval2LrID, nLr, nLr, (int*)(mFlatBufferPtr + offs));
offs = alignSize(offs + nLr * sizeof(int), getBufferAlignmentBytes()); // account for the alignment
for (int il = 0; il < nLr; il++) {
MatLayerCyl& lr = get()->mLayers[il];
lr.flatten(mFlatBufferPtr + offs);
offs = alignSize(offs + lr.getFlatBufferSize(), getBufferAlignmentBytes()); // account for the alignment
}
mConstructionMask = Constructed;
}
//______________________________________________
void MatLayerCylSet::moveBufferTo(char* newFlatBufferPtr)
{
/// sets buffer pointer to the new address, move the buffer content there.
flatObject::moveBufferTo(newFlatBufferPtr);
setActualBufferAddress(mFlatBufferPtr);
}
#endif // !GPUCA_ALIGPUCODE
#ifndef GPUCA_GPUCODE
//______________________________________________
void MatLayerCylSet::setFutureBufferAddress(char* futureFlatBufferPtr)
{
/// Sets the actual location of the external flat buffer before it was created
///
fixPointers(mFlatBufferPtr, futureFlatBufferPtr, false); // flag that futureFlatBufferPtr is not valid yet
flatObject::setFutureBufferAddress(futureFlatBufferPtr);
}
//______________________________________________
void MatLayerCylSet::setActualBufferAddress(char* actualFlatBufferPtr)
{
/// Sets the actual location of the external flat buffer after it has been moved (i.e. to another machine)
///
fixPointers(actualFlatBufferPtr);
}
//______________________________________________
void MatLayerCylSet::cloneFromObject(const MatLayerCylSet& obj, char* newFlatBufferPtr)
{
/// Initializes from another object, copies data to newBufferPtr
flatObject::cloneFromObject(obj, newFlatBufferPtr);
fixPointers(mFlatBufferPtr);
}
//______________________________________________
void MatLayerCylSet::fixPointers(char* newBasePtr)
{
// fix pointers on the internal structure of the flat buffer after retrieving it from the file
if (newBasePtr) {
mFlatBufferPtr = newBasePtr; // used to impose external pointer
} else {
mFlatBufferPtr = mFlatBufferContainer; // impose pointer after reading from file
}
auto offs = alignSize(sizeof(MatLayerCylSetLayout), getBufferAlignmentBytes()); // account for the alignment
char* newPtr = mFlatBufferPtr + offs; // correct pointer on MatLayerCyl*
char* oldPtr = reinterpret_cast<char*>(get()->mLayers); // old pointer read from the file
fixPointers(oldPtr, newPtr);
}
//______________________________________________
void MatLayerCylSet::fixPointers(char* oldPtr, char* newPtr, bool newPtrValid)
{
// fix pointers on the internal structure of the flat buffer after retrieving it from the file
auto* layPtr = get()->mLayers;
get()->mLayers = flatObject::relocatePointer(oldPtr, newPtr, get()->mLayers);
get()->mR2Intervals = flatObject::relocatePointer(oldPtr, newPtr, get()->mR2Intervals);
get()->mInterval2LrID = flatObject::relocatePointer(oldPtr, newPtr, get()->mInterval2LrID);
if (newPtrValid) {
layPtr = get()->mLayers;
}
for (int i = 0; i < getNLayers(); i++) {
layPtr[i].setFlatPointer(flatObject::relocatePointer(oldPtr, newPtr, layPtr[i].getFlatBufferPtr()));
layPtr[i].fixPointers(oldPtr, newPtr);
}
}
#endif // !GPUCA_GPUCODE
#ifndef GPUCA_ALIGPUCODE // this part is unvisible on GPU version
MatLayerCylSet* MatLayerCylSet::extractCopy(float rmin, float rmax, float tolerance, const MatLayerCylSet* addTo) const
{
// extract layers in the covering rmin-rmax range. If addTo is provided, simply substitute its layers by those from this
if (addTo && addTo->getNLayers() != getNLayers()) {
LOGP(fatal, "addTo has {} layers, this has {}", addTo->getNLayers(), getNLayers());
}
Ray ray(std::max(getRMin(), rmin), 0., 0., std::min(getRMax(), rmax), 0., 0.);
short lmin, lmax;
if (!getLayersRange(ray, lmin, lmax)) {
LOGP(warn, "No layers found for {} < r < {}", rmin, rmax);
return nullptr;
}
LOGP(info, "Will extract layers {}:{} (out of {} layers) for {} < r < {}", lmin, lmax, getNLayers(), rmin, rmax);
MatLayerCylSet* copy = new MatLayerCylSet();
int lrCount = 0, lrCounOld = 0, lrCountTot = 0;
auto addLr = [copy, &lrCountTot](const MatLayerCyl& lr) {
float drphi = lr.getDPhi() * (lr.getRMin() + lr.getRMax()) / 2. * 0.999;
copy->addLayer(lr.getRMin(), lr.getRMax(), lr.getZMax(), lr.getDZ(), drphi);
auto& lrNew = copy->getLayer(lrCountTot++);
for (int iz = 0; iz < lrNew.getNZBins(); iz++) {
for (int ip = 0; ip < lrNew.getNPhiBins(); ip++) {
lrNew.getCellPhiBin(ip, iz).set(lr.getCellPhiBin(ip, iz));
}
}
};
if (addTo) {
for (int il = 0; il < lmin; il++) {
addLr(addTo->getLayer(il));
lrCounOld++;
}
}
for (int il = lmin; il <= lmax; il++) {
addLr(getLayer(il));
lrCount++;
}
if (addTo) {
for (int il = lmax + 1; il < getNLayers(); il++) {
addLr(addTo->getLayer(il));
lrCounOld++;
}
}
copy->finalizeStructures();
copy->optimizePhiSlices(tolerance);
copy->flatten();
LOGP(info, "Added layers {}:{} for {}<r<{} {}", lmin, lmax, rmin, rmax, fmt::format(", {} layers were transferred from additional set", lrCounOld));
return copy;
}
#endif