Project
Loading...
Searching...
No Matches
IOUtils.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.
15
16#include "ITStracking/IOUtils.h"
17
18#include <iostream>
19#include <cstdlib>
20#include <fstream>
21#include <sstream>
22#include <tuple>
23#include <unordered_set>
24#include <utility>
25
28#include "ITStracking/json.h"
29#include "MathUtils/Utils.h"
32#include "GPUCommonLogger.h"
33
34namespace
35{
36constexpr int PrimaryVertexLayerId{-1};
37constexpr int EventLabelsSeparator{-1};
38} // namespace
39
40namespace o2
41{
42namespace its
43{
44
47
49void ioutils::convertCompactClusters(gsl::span<const itsmft::CompClusterExt> clusters,
50 gsl::span<const unsigned char>::iterator& pattIt,
51 std::vector<o2::BaseCluster<float>>& output,
53{
55 bool applyMisalignment = false;
56 const auto& conf = TrackerParamConfig::Instance();
57 const auto& chmap = getChipMappingITS();
58 for (int il = 0; il < chmap.NLayers; il++) {
59 if (conf.sysErrY2[il] > 0.f || conf.sysErrZ2[il] > 0.f) {
60 applyMisalignment = true;
61 break;
62 }
63 }
64
65 for (auto& c : clusters) {
66 float sigmaY2, sigmaZ2, sigmaYZ = 0;
67 auto locXYZ = extractClusterData(c, pattIt, dict, sigmaY2, sigmaZ2);
68 auto& cl3d = output.emplace_back(c.getSensorID(), geom->getMatrixT2L(c.getSensorID()) ^ locXYZ); // local --> tracking
69 if (applyMisalignment) {
70 auto lrID = chmap.getLayer(c.getSensorID());
71 sigmaY2 += conf.sysErrY2[lrID];
72 sigmaZ2 += conf.sysErrZ2[lrID];
73 }
74 cl3d.setErrors(sigmaY2, sigmaZ2, sigmaYZ);
75 }
76}
77
78void ioutils::loadEventData(ROframe& event, gsl::span<const itsmft::CompClusterExt> clusters,
79 gsl::span<const unsigned char>::iterator& pattIt, const itsmft::TopologyDictionary* dict,
81{
82 if (clusters.empty()) {
83 std::cerr << "Missing clusters." << std::endl;
84 return;
85 }
86 event.clear();
89 int clusterId{0};
90
91 for (auto& c : clusters) {
92 int layer = geom->getLayer(c.getSensorID());
93 float sigmaY2, sigmaZ2, sigmaYZ = 0;
94 auto locXYZ = extractClusterData(c, pattIt, dict, sigmaY2, sigmaZ2);
95 auto sensorID = c.getSensorID();
96 // Inverse transformation to the local --> tracking
97 auto trkXYZ = geom->getMatrixT2L(sensorID) ^ locXYZ;
98 // Transformation to the local --> global
99 auto gloXYZ = geom->getMatrixL2G(sensorID) * locXYZ;
100
101 event.addTrackingFrameInfoToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), trkXYZ.x(), geom->getSensorRefAlpha(sensorID),
102 std::array<float, 2>{trkXYZ.y(), trkXYZ.z()},
103 std::array<float, 3>{sigmaY2, sigmaYZ, sigmaZ2});
104
106 event.addClusterToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), event.getClustersOnLayer(layer).size());
107 if (clsLabels) {
108 // event.addClusterLabelToLayer(layer, *(clsLabels->getLabels(clusterId).begin()));
109 event.setMClabelsContainer(clsLabels);
110 }
111 event.addClusterExternalIndexToLayer(layer, clusterId);
112 clusterId++;
113 }
114}
115
116int ioutils::loadROFrameData(const o2::itsmft::ROFRecord& rof, ROframe& event, gsl::span<const itsmft::CompClusterExt> clusters, gsl::span<const unsigned char>::iterator& pattIt, const itsmft::TopologyDictionary* dict,
118{
119 event.clear();
122 int clusterId{0};
123
124 auto first = rof.getFirstEntry();
125 auto clusters_in_frame = rof.getROFData(clusters);
126 for (auto& c : clusters_in_frame) {
127 int layer = geom->getLayer(c.getSensorID());
128 float sigmaY2, sigmaZ2, sigmaYZ = 0;
129 auto locXYZ = extractClusterData(c, pattIt, dict, sigmaY2, sigmaZ2);
130 auto sensorID = c.getSensorID();
131 // Inverse transformation to the local --> tracking
132 auto trkXYZ = geom->getMatrixT2L(sensorID) ^ locXYZ;
133 // Transformation to the local --> global
134 auto gloXYZ = geom->getMatrixL2G(sensorID) * locXYZ;
135
136 event.addTrackingFrameInfoToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), trkXYZ.x(), geom->getSensorRefAlpha(sensorID),
137 std::array<float, 2>{trkXYZ.y(), trkXYZ.z()},
138 std::array<float, 3>{sigmaY2, sigmaYZ, sigmaZ2});
139
141 event.addClusterToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), event.getClustersOnLayer(layer).size());
142 if (mcLabels) {
143 // event.addClusterLabelToLayer(layer, *(mcLabels->getLabels(first + clusterId).begin()));
144 event.setMClabelsContainer(mcLabels);
145 }
146 event.addClusterExternalIndexToLayer(layer, first + clusterId);
147 clusterId++;
148 }
149 return clusters_in_frame.size();
150}
151
152std::vector<std::unordered_map<int, Label>> ioutils::loadLabels(const int eventsNum, const std::string& fileName)
153{
154 std::vector<std::unordered_map<int, Label>> labelsMap{};
155 std::unordered_map<int, Label> currentEventLabelsMap{};
156 std::ifstream inputStream{};
157 std::string line{};
158 int monteCarloId{}, pdgCode{}, numberOfClusters{};
159 float transverseMomentum{}, phi{}, pseudorapidity{};
160
161 labelsMap.reserve(eventsNum);
162
163 inputStream.open(fileName);
164 std::getline(inputStream, line);
165
166 while (std::getline(inputStream, line)) {
167
168 std::istringstream inputStringStream(line);
169
170 if (inputStringStream >> monteCarloId) {
171
172 if (monteCarloId == EventLabelsSeparator) {
173
174 labelsMap.emplace_back(currentEventLabelsMap);
175 currentEventLabelsMap.clear();
176
177 } else {
178
179 if (inputStringStream >> transverseMomentum >> phi >> pseudorapidity >> pdgCode >> numberOfClusters) {
180
181 if (std::abs(pdgCode) == constants::pdgcodes::PionCode && numberOfClusters == 7) {
182
183 currentEventLabelsMap.emplace(std::piecewise_construct, std::forward_as_tuple(monteCarloId),
184 std::forward_as_tuple(monteCarloId, transverseMomentum, phi,
185 pseudorapidity, pdgCode, numberOfClusters));
186 }
187 }
188 }
189 }
190 }
191
192 labelsMap.emplace_back(currentEventLabelsMap);
193
194 return labelsMap;
195}
196
197// void ioutils::writeRoadsReport(std::ofstream& correctRoadsOutputStream, std::ofstream& duplicateRoadsOutputStream,
198// std::ofstream& fakeRoadsOutputStream, const std::vector<std::vector<Road<5>>>& roads,
199// const std::unordered_map<int, Label>& labelsMap)
200// {
201// const int numVertices{static_cast<int>(roads.size())};
202// std::unordered_set<int> foundMonteCarloIds{};
203
204// correctRoadsOutputStream << EventLabelsSeparator << std::endl;
205// fakeRoadsOutputStream << EventLabelsSeparator << std::endl;
206
207// for (int iVertex{0}; iVertex < numVertices; ++iVertex) {
208
209// const std::vector<Road<5>>& currentVertexRoads{roads[iVertex]};
210// const int numRoads{static_cast<int>(currentVertexRoads.size())};
211
212// for (int iRoad{0}; iRoad < numRoads; ++iRoad) {
213
214// const Road<5>& currentRoad{currentVertexRoads[iRoad]};
215// const int currentRoadLabel{currentRoad.getLabel()};
216
217// if (!labelsMap.count(currentRoadLabel)) {
218
219// continue;
220// }
221
222// const Label& currentLabel{labelsMap.at(currentRoadLabel)};
223
224// if (currentRoad.isFakeRoad()) {
225
226// fakeRoadsOutputStream << currentLabel << std::endl;
227
228// } else {
229
230// if (foundMonteCarloIds.count(currentLabel.monteCarloId)) {
231
232// duplicateRoadsOutputStream << currentLabel << std::endl;
233
234// } else {
235
236// correctRoadsOutputStream << currentLabel << std::endl;
237// foundMonteCarloIds.emplace(currentLabel.monteCarloId);
238// }
239// }
240// }
241// }
242// }
243
244} // namespace its
245} // namespace o2
General auxilliary methods.
Definition of the GeometryTGeo class.
void output(const std::map< std::string, ChannelStat > &channels)
Definition rawdump.cxx:197
Definition of a container to keep Monte Carlo truth external to simulation objects.
uint32_t j
Definition RawData.h:0
uint32_t c
Definition RawData.h:2
A container to hold and manage MC truth information/labels.
const Mat3D & getMatrixL2G(int sensID) const
const Mat3D & getMatrixT2L(int lay, int hba, int sta, int det) const
int getLayer(int index) const
Get chip layer, from 0.
float getSensorRefAlpha(int isn) const
static GeometryTGeo * Instance()
void fillMatrixCache(int mask) override
gsl::span< const T > getROFData(const gsl::span< const T > tfdata) const
Definition ROFRecord.h:73
int getFirstEntry() const
Definition ROFRecord.h:63
struct _cl_event * event
Definition glcorearb.h:2982
GLenum GLuint GLint GLint layer
Definition glcorearb.h:1310
void convertCompactClusters(gsl::span< const itsmft::CompClusterExt > clusters, gsl::span< const unsigned char >::iterator &pattIt, std::vector< o2::BaseCluster< float > > &output, const itsmft::TopologyDictionary *dict)
convert compact clusters to 3D spacepoints
Definition IOUtils.cxx:49
o2::math_utils::Point3D< T > extractClusterData(const itsmft::CompClusterExt &c, iterator &iter, const itsmft::TopologyDictionary *dict, T &sig2y, T &sig2z)
Definition IOUtils.h:80
void loadEventData(ROframe &events, gsl::span< const itsmft::CompClusterExt > clusters, gsl::span< const unsigned char >::iterator &pattIt, const itsmft::TopologyDictionary *dict, const dataformats::MCTruthContainer< MCCompLabel > *clsLabels=nullptr)
Definition IOUtils.cxx:78
int loadROFrameData(const o2::itsmft::ROFRecord &rof, ROframe &events, gsl::span< const itsmft::CompClusterExt > clusters, gsl::span< const unsigned char >::iterator &pattIt, const itsmft::TopologyDictionary *dict, const dataformats::MCTruthContainer< MCCompLabel > *mClsLabels=nullptr)
Definition IOUtils.cxx:116
std::vector< std::unordered_map< int, Label > > loadLabels(const int, const std::string &)
Definition IOUtils.cxx:152
void from_json(const nlohmann::json &j, TrackingParameters &par)
void to_json(nlohmann::json &j, const TrackingParameters &par)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
static constexpr int L2G
Definition Cartesian.h:54
static constexpr int T2L
Definition Cartesian.h:55
std::vector< Cluster > clusters