19#include "ROOT/RDataFrame.hxx"
20#include "TStopwatch.h"
29template <
typename DataT>
30template <
typename DataTOut>
33 if (outf.IsZombie()) {
34 LOGP(error,
"Failed to write to file: {}", outf.GetName());
39 containerTmp.
getData() = std::vector<DataTOut>(mData.begin(), mData.end());
45template <
typename DataT>
49 const size_t maxvalues =
sizeof(float) * 1024 * 1024;
52 const size_t nsize = getNDataPoints();
55 size_t entries = ((nsize / nthreads) > maxvalues) ? (nsize / maxvalues) : nthreads;
57 if (entries > nsize) {
62 const size_t values_per_entry = nsize / entries;
65 const size_t values_lastEntry = nsize % entries;
66 if (values_lastEntry) {
71 if (ROOT::IsImplicitMTEnabled() && (ROOT::GetThreadPoolSize() != nthreads)) {
72 ROOT::DisableImplicitMT();
74 ROOT::EnableImplicitMT(nthreads);
77 ROOT::RDataFrame dFrame(entries);
81 dfStore = dfStore.Define(
"nz", [mZVertices = mZVertices]() {
return mZVertices; });
82 dfStore = dfStore.Define(
"nr", [mRVertices = mRVertices]() {
return mRVertices; });
83 dfStore = dfStore.Define(
"nphi", [mPhiVertices = mPhiVertices]() {
return mPhiVertices; });
86 ROOT::RDF::RSnapshotOptions opt;
88 opt.fOverwriteIfExists =
true;
92 dfStore.Snapshot(
name,
file, {
name.data(),
"nz",
"nr",
"nphi"}, opt);
97template <
typename DataT>
101 if (ROOT::IsImplicitMTEnabled() && (ROOT::GetThreadPoolSize() != nthreads)) {
102 ROOT::DisableImplicitMT();
104 ROOT::EnableImplicitMT(nthreads);
108 ROOT::RDataFrame dFrame(
name,
file);
111 auto comp = [mZVertices = mZVertices, mRVertices = mRVertices, mPhiVertices = mPhiVertices](
const unsigned short nz,
const unsigned short nr,
const unsigned short nphi) {
112 if ((nz == mZVertices) && (nr == mRVertices) && (nphi == mPhiVertices)) {
118 auto count = dFrame.Filter(comp, {
"nz",
"nr",
"nphi"}).Count();
120 LOGP(error,
"Data from input file has different number of vertices! Found {} same vertices", *
count);
125 auto readData = [&mData = mData](
const std::pair<long, std::vector<float>>&
data) {
126 std::copy(
data.second.begin(),
data.second.end(), mData.begin() +
data.first);
129 LOGP(info,
"Reading {} from file {}",
name,
file);
139template <
typename DataT>
140template <
typename DataTIn>
143 if (inpf.IsZombie()) {
144 LOGP(error,
"Failed to read from file: {}", inpf.GetName());
151 LOGP(error,
"Failed to load {} from {}",
name, inpf.GetName());
155 if (mZVertices != dataCont->getNZ() || mRVertices != dataCont->getNR() || mPhiVertices != dataCont->getNPhi()) {
156 LOGP(error,
"Data from input file has different definition of vertices!");
157 LOGP(error,
"set vertices before creating the sc object to: SpaceCharge<>::setGrid({}, {}, {})", dataCont->getNZ(), dataCont->getNR(), dataCont->getNPhi());
162 mData = std::vector<DataT>(dataCont->getData().begin(), dataCont->getData().end());
167template <
typename DataT>
170 if (inpf.IsZombie()) {
171 LOGP(error,
"Failed to read from file {}", inpf.GetName());
178 LOGP(error,
"Failed to load {} from {}",
name, inpf.GetName());
184template <
typename DataT>
189 auto&&
w = std::setw(9);
192 for (
unsigned int iz = 0; iz < mPhiVertices; ++iz) {
196 for (
unsigned int ix = 1; ix < mZVertices; ++ix) {
197 stream <<
", " <<
w << (*this)(ix, 0, iz);
201 for (
unsigned int iy = 1; iy < mRVertices - 1; ++iy) {
202 stream <<
"⎢" <<
w << (*this)(0, iy, iz);
203 for (
unsigned int ix = 1; ix < mZVertices; ++ix) {
204 stream <<
", " <<
w << (*this)(ix, iy, iz);
209 stream <<
"⎣" <<
w << (*this)(0, mRVertices - 1, iz);
210 for (
unsigned int ix = 1; ix < mZVertices; ++ix) {
211 stream <<
", " <<
w << (*this)(ix, mRVertices - 1, iz);
215 LOGP(info,
"{} \n \n",
stream.str());
218template <
typename DataT>
221 const long indStart =
entry * values_per_entry;
222 if (
entry < (entries - 1)) {
223 return std::pair(indStart, std::vector<float>(
data.begin() + indStart,
data.begin() + indStart + values_per_entry));
224 }
else if (
entry == (entries - 1)) {
226 return std::pair(indStart, std::vector<float>(
data.begin() + indStart,
data.end()));
228 return std::pair(indStart, std::vector<float>());
231template <
typename DataT>
234 std::transform(mData.begin(), mData.end(), mData.begin(), [
value =
value](
auto&
val) { return val * value; });
238template <
typename DataT>
241 std::transform(mData.begin(), mData.end(),
other.mData.begin(), mData.begin(), std::plus<>());
245template <
typename DataT>
248 std::transform(mData.begin(), mData.end(),
other.mData.begin(), mData.begin(), std::minus<>());
252template <
typename DataT>
255 std::transform(mData.begin(), mData.end(),
other.mData.begin(), mData.begin(), std::multiplies<>());
259template <
typename DataT>
262 const size_t iphi =
index / (nz * nr);
263 index -= (iphi * nz * nr);
264 const size_t iz =
index % nz;
268template <
typename DataT>
271 const size_t iphi =
index / (nz * nr);
272 index -= (iphi * nz * nr);
277template <
typename DataT>
280 return index / (nz * nr);
283template <
typename DataT>
286 tree->SetAlias(
"ir",
"o2::tpc::DataContainer3D<float>::getIndexR(first + Iteration$, nz, nr, nphi)");
287 tree->SetAlias(
"iz",
"o2::tpc::DataContainer3D<float>::getIndexZ(first + Iteration$, nz, nr, nphi)");
288 tree->SetAlias(
"iphi",
"o2::tpc::DataContainer3D<float>::getIndexPhi(first + Iteration$, nz, nr, nphi)");
289 tree->SetAlias(
"r",
"o2::tpc::GridProperties<float>::getRMin() + o2::tpc::GridProperties<float>::getGridSpacingR(nr) * ir");
290 tree->SetAlias(
"z",
"o2::tpc::GridProperties<float>::getZMin() + o2::tpc::GridProperties<float>::getGridSpacingZ(nz) * iz");
291 tree->SetAlias(
"phi",
"o2::tpc::GridProperties<float>::getPhiMin() + o2::tpc::GridProperties<float>::getGridSpacingPhi(nphi) * iphi");
294template <
typename DataT>
298 tree->SetAlias(
"val",
"_0");
301 tree->SetAlias(
"iz",
"_1");
302 tree->SetAlias(
"ir",
"_2");
303 tree->SetAlias(
"iphi",
"_3");
304 tree->SetAlias(
"z",
"_4");
305 tree->SetAlias(
"r",
"_5");
306 tree->SetAlias(
"phi",
"_6");
307 tree->SetAlias(
"lpos",
"_7");
308 tree->SetAlias(
"lx",
"lpos.fCoordinates.fX");
309 tree->SetAlias(
"ly",
"lpos.fCoordinates.fY");
310 tree->SetAlias(
"index",
"_8");
313template <
typename DataT>
320 mData.resize(nZ * nR *
static_cast<size_t>(nPhi));
324template <
typename DataT>
325void DataContainer3D<DataT>::dumpSlice(std::string_view treename, std::string_view fileIn, std::string_view fileOut, std::string_view option, std::pair<unsigned short, unsigned short> rangeiR, std::pair<unsigned short, unsigned short> rangeiZ, std::pair<unsigned short, unsigned short> rangeiPhi,
const int nthreads)
327 if (ROOT::IsImplicitMTEnabled() && (ROOT::GetThreadPoolSize() != nthreads)) {
328 ROOT::DisableImplicitMT();
330 ROOT::EnableImplicitMT(nthreads);
331 ROOT::RDataFrame dFrame(treename, fileIn);
333 auto df = dFrame.Define(
"slice", [rangeiZ, rangeiR, rangeiPhi](
const std::pair<
long, std::vector<float>>&
values,
unsigned short nz,
unsigned short nr,
unsigned short nphi) {
334 std::vector<size_t>
ir;
335 std::vector<size_t> iphi;
336 std::vector<size_t> iz;
337 std::vector<float>
r;
338 std::vector<float> phi;
339 std::vector<float>
z;
340 std::vector<float> vals;
341 std::vector<size_t> globalIdx;
342 std::vector<LocalPosition3D> lPos;
343 const auto nvalues =
values.second.size();
345 iphi.reserve(nvalues);
348 phi.reserve(nvalues);
350 vals.reserve(nvalues);
351 lPos.reserve(nvalues);
352 globalIdx.reserve(nvalues);
353 for (
size_t i = 0;
i < nvalues; ++
i) {
354 const size_t idx =
values.first +
i;
356 if ((rangeiZ.first < rangeiZ.second) && ((iZTmp < rangeiZ.first) || (iZTmp > rangeiZ.second))) {
361 if ((rangeiR.first < rangeiR.second) && ((iRTmp < rangeiR.first) || (iRTmp > rangeiR.second))) {
366 if ((rangeiPhi.first < rangeiPhi.second) && ((iPhiTmp < rangeiPhi.first) || (iPhiTmp > rangeiPhi.second))) {
374 const float x = rTmp * std::cos(phiTmp);
375 const float y = rTmp * std::sin(phiTmp);
377 unsigned char secNum = std::floor(phiTmp /
SECPHIWIDTH);
381 lPos.emplace_back(lPosTmp);
382 ir.emplace_back(iRTmp);
383 iphi.emplace_back(iPhiTmp);
384 iz.emplace_back(iZTmp);
385 r.emplace_back(rTmp);
386 phi.emplace_back(phiTmp);
387 z.emplace_back(zTmp);
388 vals.emplace_back(
values.second[
i]);
389 globalIdx.emplace_back(idx);
391 return std::make_tuple(vals, iz,
ir, iphi,
z,
r, phi, lPos, globalIdx);
393 {treename.data(),
"nz",
"nr",
"nphi"});
396 ROOT::RDF::RSnapshotOptions opt;
398 df.Snapshot(treename, fileOut, {
"slice"}, opt);
401template <
typename DataT>
405 return interpolator(
z,
r, phi);
408template <
typename DataT>
409void DataContainer3D<DataT>::dumpInterpolation(std::string_view treename, std::string_view fileIn, std::string_view fileOut, std::string_view option, std::pair<float, float> rangeR, std::pair<float, float> rangeZ, std::pair<float, float> rangePhi,
const int nR,
const int nZ,
const int nPhi,
const int nthreads)
411 if (ROOT::IsImplicitMTEnabled() && (ROOT::GetThreadPoolSize() != nthreads)) {
412 ROOT::DisableImplicitMT();
414 ROOT::EnableImplicitMT(nthreads);
415 ROOT::RDataFrame dFrame(nPhi);
418 unsigned short nr, nz, nphi;
419 if (!getVertices(treename, fileIn, nr, nz, nphi)) {
426 data.initFromFile(fileIn, treename, nthreads);
432 auto interpolate = [&mGrid3D = std::as_const(mGrid3D), &
data = std::as_const(
data), rangeR, rangeZ, rangePhi, nR, nZ, nPhi](
unsigned int, ULong64_t iPhi) {
433 std::vector<size_t>
ir;
434 std::vector<size_t> iphi;
435 std::vector<size_t> iz;
436 std::vector<float>
r;
437 std::vector<float> phi;
438 std::vector<float>
z;
439 std::vector<float> vals;
440 std::vector<size_t> globalIdx;
441 std::vector<LocalPosition3D> lPos;
442 const auto nvalues = nR * nZ;
444 iphi.reserve(nvalues);
447 phi.reserve(nvalues);
449 vals.reserve(nvalues);
450 lPos.reserve(nvalues);
451 globalIdx.reserve(nvalues);
453 const float rSpacing = (rangeR.second - rangeR.first) / (nR - 1);
454 const float zSpacing = (rangeZ.second - rangeZ.first) / (nZ - 1);
455 const float phiSpacing = (rangePhi.second - rangePhi.first) / (nPhi - 1);
456 const DataT phiPos = rangePhi.first + iPhi * phiSpacing;
458 for (
int iR = 0; iR < nR; ++iR) {
459 const DataT rPos = rangeR.first + iR * rSpacing;
460 for (
int iZ = 0; iZ < nZ; ++iZ) {
461 const size_t idx = (iZ + nZ * (iR + iPhi * nR));
462 const DataT zPos = rangeZ.first + iZ * zSpacing;
464 iphi.emplace_back(iPhi);
466 r.emplace_back(rPos);
467 phi.emplace_back(phiPos);
468 z.emplace_back(zPos);
469 vals.emplace_back(
data.interpolate(zPos, rPos, phiPos, mGrid3D));
470 globalIdx.emplace_back(idx);
471 const float x = rPos * std::cos(phiPos);
472 const float y = rPos * std::sin(phiPos);
474 unsigned char secNum = std::floor(phiPos /
SECPHIWIDTH);
477 lPos.emplace_back(lPosTmp);
480 return std::make_tuple(vals, iz,
ir, iphi,
z,
r, phi, lPos, globalIdx);
484 auto dfStore = dFrame.DefineSlotEntry(treename, interpolate);
487 ROOT::RDF::RSnapshotOptions opt;
492 dfStore.Snapshot(treename, fileOut, {treename.data()}, opt);
496template <
typename DataT>
499 TFile fTmp(fileIn.data(),
"READ");
500 TTree*
tree = (TTree*)fTmp.Get(treename.data());
502 LOGP(warning,
"Tree {} not found in input file {}", treename, fileIn);
505 tree->SetBranchAddress(
"nz", &nZ);
506 tree->SetBranchAddress(
"nr", &nR);
507 tree->SetBranchAddress(
"nphi", &nPhi);
513template <
typename DataT>
516 const int nZNew = gridNew.
getNZ();
517 const int nRNew = gridNew.
getNR();
518 const int nPhiNew = gridNew.
getNPhi();
520#pragma omp parallel for num_threads(threads)
521 for (
size_t iPhi = 0; iPhi < nPhiNew; ++iPhi) {
523 for (
size_t iR = 0; iR < nRNew; ++iR) {
525 for (
size_t iZ = 0; iZ < nZNew; ++iZ) {
527 contCont(iZ, iR, iPhi) = interpolate(
z, radius, phi, gridRef);
This class provides a simple method to store values on a large 3-Dim grid with ROOT io functionality.
Definition of RegularGrid3D class.
Definition of TriCubic class.
static LocalPosition3D GlobalToLocal(const GlobalPosition3D &pos, const double alpha)
DataT getRVertex(const size_t vertexY) const
DataT getPhiVertex(const size_t vertexZ) const
DataT getZVertex(const size_t vertexX) const
GLuint const GLchar * name
GLsizei const GLfloat * value
GLenum GLsizei GLsizei GLint * values
GLubyte GLubyte GLubyte GLubyte w
GLdouble GLdouble GLdouble z
Global TPC definitions and constants.
constexpr double SECPHIWIDTH
constexpr unsigned char SECTORSPERSIDE
void readData(o2::tpc::GBTFrameContainer &container, std::vector< std::ofstream * > &outfiles, int &run, int &done)
const auto & getData() const
static size_t getIndexR(size_t index, const int nz, const int nr, const int nphi)
static void dumpSlice(std::string_view treename, std::string_view fileIn, std::string_view fileOut, std::string_view option, std::pair< unsigned short, unsigned short > rangeiR, std::pair< unsigned short, unsigned short > rangeiZ, std::pair< unsigned short, unsigned short > rangeiPhi, const int nthreads=1)
void print() const
print the matrix
static void dumpInterpolation(std::string_view treename, std::string_view fileIn, std::string_view fileOut, std::string_view option, std::pair< float, float > rangeR, std::pair< float, float > rangeZ, std::pair< float, float > rangePhi, const int nR, const int nZ, const int nPhi, const int nthreads=1)
bool initFromFile(TFile &inpf, const char *name="data")
set values from file
int writeToFile(TFile &outf, const char *name="data") const
void setGrid(unsigned short nZ, unsigned short nR, unsigned short nPhi, const bool resize)
set the grid points
static void setAliasesForDump(TTree *tree)
static void setAliases(TTree *tree)
DataT interpolate(const DataT z, const DataT r, const DataT phi, const o2::tpc::RegularGrid3D< DataT > &grid) const
DataContainer3D< DataT > & operator*=(const DataT value)
operator overload
static size_t getIndexZ(size_t index, const int nz, const int nr, const int nphi)
static size_t getIndexPhi(size_t index, const int nz, const int nr, const int nphi)
static bool getVertices(std::string_view treename, std::string_view fileIn, unsigned short &nR, unsigned short &nZ, unsigned short &nPhi)
static DataContainer3D< DataT > * loadFromFile(TFile &inpf, const char *name="data")
get pointer to object from file (deprecated!)
DataContainer3D< DataT > & operator-=(const DataContainer3D< DataT > &other)
DataContainer3D< DataT > & operator+=(const DataContainer3D< DataT > &other)
DataContainer3D< DataT > convert(const o2::tpc::RegularGrid3D< DataT > &gridNew, const o2::tpc::RegularGrid3D< DataT > &gridRef, const int threads=1) const
convert a data container to a new datacontainer with different grid definition (e....
static constexpr DataT getRMin()
static constexpr DataT getZMin()
static constexpr DataT getGridSpacingR(const unsigned int nR)
static constexpr DataT getPhiMin()
static constexpr DataT getGridSpacingZ(const unsigned int nZ)
static constexpr DataT getGridSpacingPhi(const unsigned int nPhi)
static int normalizeGridToNSector
the grid in phi direction is squashed from 2 Pi to (2 Pi / SECTORSPERSIDE). This can used to get the ...
VectorOfTObjectPtrs other
o2::InteractionRecord ir(0, 0)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))