Project
Loading...
Searching...
No Matches
DataContainer3D.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
16#include "TPCBase/Mapper.h"
17#include "Framework/Logger.h"
18#include "TFile.h"
19#include "TStopwatch.h"
20#include "TTree.h"
21
22#include <memory>
23#include <iomanip>
24#include <algorithm>
25
26using namespace o2::tpc;
27
28template <typename DataT>
29template <typename DataTOut>
30int DataContainer3D<DataT>::writeToFile(TFile& outf, const char* name) const
31{
32 if (outf.IsZombie()) {
33 LOGP(error, "Failed to write to file: {}", outf.GetName());
34 return -1;
35 }
36
37 DataContainer3D<DataTOut> containerTmp(mZVertices, mRVertices, mPhiVertices);
38 containerTmp.getData() = std::vector<DataTOut>(mData.begin(), mData.end());
39
40 outf.WriteObjectAny(&containerTmp, DataContainer3D<DataTOut>::Class(), name);
41 return 0;
42}
43
44
45
47template <typename DataT>
48template <typename DataTIn>
49bool DataContainer3D<DataT>::initFromFile(TFile& inpf, const char* name)
50{
51 if (inpf.IsZombie()) {
52 LOGP(error, "Failed to read from file: {}", inpf.GetName());
53 return false;
54 }
55 DataContainer3D<DataTIn>* dataCont{nullptr};
56 dataCont = reinterpret_cast<DataContainer3D<DataTIn>*>(inpf.GetObjectChecked(name, DataContainer3D<DataTIn>::Class()));
57
58 if (!dataCont) {
59 LOGP(error, "Failed to load {} from {}", name, inpf.GetName());
60 return false;
61 }
62
63 if (mZVertices != dataCont->getNZ() || mRVertices != dataCont->getNR() || mPhiVertices != dataCont->getNPhi()) {
64 LOGP(error, "Data from input file has different definition of vertices!");
65 LOGP(error, "set vertices before creating the sc object to: SpaceCharge<>::setGrid({}, {}, {})", dataCont->getNZ(), dataCont->getNR(), dataCont->getNPhi());
66 delete dataCont;
67 return false;
68 }
69
70 mData = std::vector<DataT>(dataCont->getData().begin(), dataCont->getData().end());
71 delete dataCont;
72 return true;
73}
74
75template <typename DataT>
77{
78 if (inpf.IsZombie()) {
79 LOGP(error, "Failed to read from file {}", inpf.GetName());
80 return nullptr;
81 }
82 DataContainer3D<DataT>* dataCont{nullptr};
83
84 dataCont = reinterpret_cast<DataContainer3D<DataT>*>(inpf.GetObjectChecked(name, DataContainer3D<DataT>::Class()));
85 if (!dataCont) {
86 LOGP(error, "Failed to load {} from {}", name, inpf.GetName());
87 return nullptr;
88 }
89 return dataCont;
90}
91
92template <typename DataT>
94{
95 std::stringstream stream;
96 stream.precision(3);
97 auto&& w = std::setw(9);
98 stream << std::endl;
99
100 for (unsigned int iz = 0; iz < mPhiVertices; ++iz) {
101 stream << "z layer: " << iz << "\n";
102 // print top x row
103 stream << "⎡" << w << (*this)(0, 0, iz);
104 for (unsigned int ix = 1; ix < mZVertices; ++ix) {
105 stream << ", " << w << (*this)(ix, 0, iz);
106 }
107 stream << " ⎤ \n";
108
109 for (unsigned int iy = 1; iy < mRVertices - 1; ++iy) {
110 stream << "⎢" << w << (*this)(0, iy, iz);
111 for (unsigned int ix = 1; ix < mZVertices; ++ix) {
112 stream << ", " << w << (*this)(ix, iy, iz);
113 }
114 stream << " ⎥ \n";
115 }
116
117 stream << "⎣" << w << (*this)(0, mRVertices - 1, iz);
118 for (unsigned int ix = 1; ix < mZVertices; ++ix) {
119 stream << ", " << w << (*this)(ix, mRVertices - 1, iz);
121 stream << " ⎦ \n \n";
122 }
123 LOGP(info, "{} \n \n", stream.str());
125
126
127template <typename DataT>
129{
130 std::transform(mData.begin(), mData.end(), mData.begin(), [value = value](auto& val) { return val * value; });
131 return *this;
132}
133
134template <typename DataT>
136{
137 std::transform(mData.begin(), mData.end(), other.mData.begin(), mData.begin(), std::plus<>());
138 return *this;
139}
140
141template <typename DataT>
144 std::transform(mData.begin(), mData.end(), other.mData.begin(), mData.begin(), std::minus<>());
145 return *this;
146}
147
148template <typename DataT>
150{
151 std::transform(mData.begin(), mData.end(), other.mData.begin(), mData.begin(), std::multiplies<>());
152 return *this;
153}
154
155template <typename DataT>
156size_t DataContainer3D<DataT>::getIndexZ(size_t index, const int nz, const int nr, const int nphi)
157{
158 const size_t iphi = index / (nz * nr);
159 index -= (iphi * nz * nr);
160 const size_t iz = index % nz;
161 return iz;
162}
163
164template <typename DataT>
165size_t DataContainer3D<DataT>::getIndexR(size_t index, const int nz, const int nr, const int nphi)
166{
167 const size_t iphi = index / (nz * nr);
168 index -= (iphi * nz * nr);
169 const size_t ir = index / nz;
170 return ir;
171}
172
173template <typename DataT>
174size_t DataContainer3D<DataT>::getIndexPhi(size_t index, const int nz, const int nr, const int nphi)
175{
176 return index / (nz * nr);
177}
178
179template <typename DataT>
181{
182 tree->SetAlias("ir", "o2::tpc::DataContainer3D<float>::getIndexR(first + Iteration$, nz, nr, nphi)");
183 tree->SetAlias("iz", "o2::tpc::DataContainer3D<float>::getIndexZ(first + Iteration$, nz, nr, nphi)");
184 tree->SetAlias("iphi", "o2::tpc::DataContainer3D<float>::getIndexPhi(first + Iteration$, nz, nr, nphi)");
185 tree->SetAlias("r", "o2::tpc::GridProperties<float>::getRMin() + o2::tpc::GridProperties<float>::getGridSpacingR(nr) * ir");
186 tree->SetAlias("z", "o2::tpc::GridProperties<float>::getZMin() + o2::tpc::GridProperties<float>::getGridSpacingZ(nz) * iz");
187 tree->SetAlias("phi", "o2::tpc::GridProperties<float>::getPhiMin() + o2::tpc::GridProperties<float>::getGridSpacingPhi(nphi) * iphi");
188}
189
190template <typename DataT>
192{
193 // actuall stored value
194 tree->SetAlias("val", "_0");
196 // some meta data
197 tree->SetAlias("iz", "_1");
198 tree->SetAlias("ir", "_2");
199 tree->SetAlias("iphi", "_3");
200 tree->SetAlias("z", "_4");
201 tree->SetAlias("r", "_5");
202 tree->SetAlias("phi", "_6");
203 tree->SetAlias("lpos", "_7");
204 tree->SetAlias("lx", "lpos.fCoordinates.fX");
205 tree->SetAlias("ly", "lpos.fCoordinates.fY");
206 tree->SetAlias("index", "_8");
207}
208
209template <typename DataT>
210void DataContainer3D<DataT>::setGrid(unsigned short nZ, unsigned short nR, unsigned short nPhi, const bool resize)
211{
212 mZVertices = nZ;
213 mRVertices = nR;
214 mPhiVertices = nPhi;
215 if (resize) {
216 mData.resize(nZ * nR * static_cast<size_t>(nPhi));
217 }
218}
219
220
221template <typename DataT>
223{
224 TriCubicInterpolator<DataT> interpolator(*this, grid);
225 return interpolator(z, r, phi);
226}
227
228
229template <typename DataT>
230bool DataContainer3D<DataT>::getVertices(std::string_view treename, std::string_view fileIn, unsigned short& nR, unsigned short& nZ, unsigned short& nPhi)
231{
232 TFile fTmp(fileIn.data(), "READ");
233 TTree* tree = (TTree*)fTmp.Get(treename.data());
234 if (!tree) {
235 LOGP(warning, "Tree {} not found in input file {}", treename, fileIn);
236 return false;
237 }
238 tree->SetBranchAddress("nz", &nZ);
239 tree->SetBranchAddress("nr", &nR);
240 tree->SetBranchAddress("nphi", &nPhi);
241 tree->GetEntry(0);
242 delete tree;
243 return true;
244}
245
246template <typename DataT>
248{
249 const int nZNew = gridNew.getNZ();
250 const int nRNew = gridNew.getNR();
251 const int nPhiNew = gridNew.getNPhi();
252 DataContainer3D<DataT> contCont(nZNew, nRNew, nPhiNew);
253#pragma omp parallel for num_threads(threads)
254 for (size_t iPhi = 0; iPhi < nPhiNew; ++iPhi) {
255 const DataT phi = gridNew.getPhiVertex(iPhi);
256 for (size_t iR = 0; iR < nRNew; ++iR) {
257 const DataT radius = gridNew.getRVertex(iR);
258 for (size_t iZ = 0; iZ < nZNew; ++iZ) {
259 const DataT z = gridNew.getZVertex(iZ);
260 contCont(iZ, iR, iPhi) = interpolate(z, radius, phi, gridRef);
261 }
262 }
263 }
264 return contCont;
265}
266
269
270// deprecated functions (to be removed...)
271template int o2::tpc::DataContainer3D<float>::writeToFile<float>(TFile&, const char*) const;
272template int o2::tpc::DataContainer3D<float>::writeToFile<double>(TFile&, const char*) const;
273template int o2::tpc::DataContainer3D<double>::writeToFile<float>(TFile&, const char*) const;
274template int o2::tpc::DataContainer3D<double>::writeToFile<double>(TFile&, const char*) const;
275template bool o2::tpc::DataContainer3D<float>::initFromFile<float>(TFile&, const char*);
276template bool o2::tpc::DataContainer3D<float>::initFromFile<double>(TFile&, const char*);
277template bool o2::tpc::DataContainer3D<double>::initFromFile<float>(TFile&, const char*);
278template bool o2::tpc::DataContainer3D<double>::initFromFile<double>(TFile&, const char*);
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.
size_t getNPhi() const
DataT getRVertex(const size_t vertexY) const
DataT getPhiVertex(const size_t vertexZ) const
DataT getZVertex(const size_t vertexX) const
GLuint index
Definition glcorearb.h:781
GLuint const GLchar * name
Definition glcorearb.h:781
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint GLfloat * val
Definition glcorearb.h:1582
GLboolean r
Definition glcorearb.h:1233
GLuint GLuint stream
Definition glcorearb.h:1806
GLubyte GLubyte GLubyte GLubyte w
Definition glcorearb.h:852
GLdouble GLdouble GLdouble z
Definition glcorearb.h:843
Global TPC definitions and constants.
Definition SimTraits.h:168
const auto & getData() const
static size_t getIndexR(size_t index, const int nz, const int nr, const int nphi)
void print() const
print the matrix
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....
VectorOfTObjectPtrs other
o2::InteractionRecord ir(0, 0)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
pedsdata resize(norbits)