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 "MathUtils/Utils.h"
31
32namespace
33{
34constexpr int PrimaryVertexLayerId{-1};
35constexpr int EventLabelsSeparator{-1};
36} // namespace
37
38namespace o2
39{
40namespace its
41{
42
44void ioutils::convertCompactClusters(gsl::span<const itsmft::CompClusterExt> clusters,
45 gsl::span<const unsigned char>::iterator& pattIt,
46 std::vector<o2::BaseCluster<float>>& output,
48{
50 bool applyMisalignment = false;
51 const auto& conf = TrackerParamConfig::Instance();
52 const auto& chmap = getChipMappingITS();
53 for (int il = 0; il < chmap.NLayers; il++) {
54 if (conf.sysErrY2[il] > 0.f || conf.sysErrZ2[il] > 0.f) {
55 applyMisalignment = true;
56 break;
57 }
58 }
59
60 for (auto& c : clusters) {
61 float sigmaY2, sigmaZ2, sigmaYZ = 0;
62 auto locXYZ = extractClusterData(c, pattIt, dict, sigmaY2, sigmaZ2);
63 auto& cl3d = output.emplace_back(c.getSensorID(), geom->getMatrixT2L(c.getSensorID()) ^ locXYZ); // local --> tracking
64 if (applyMisalignment) {
65 auto lrID = chmap.getLayer(c.getSensorID());
66 sigmaY2 += conf.sysErrY2[lrID];
67 sigmaZ2 += conf.sysErrZ2[lrID];
68 }
69 cl3d.setErrors(sigmaY2, sigmaZ2, sigmaYZ);
70 }
71}
72
73void ioutils::loadEventData(ROframe& event, gsl::span<const itsmft::CompClusterExt> clusters,
74 gsl::span<const unsigned char>::iterator& pattIt, const itsmft::TopologyDictionary* dict,
76{
77 if (clusters.empty()) {
78 std::cerr << "Missing clusters." << std::endl;
79 return;
80 }
81 event.clear();
84 int clusterId{0};
85
86 for (auto& c : clusters) {
87 int layer = geom->getLayer(c.getSensorID());
88 float sigmaY2, sigmaZ2, sigmaYZ = 0;
89 auto locXYZ = extractClusterData(c, pattIt, dict, sigmaY2, sigmaZ2);
90 auto sensorID = c.getSensorID();
91 // Inverse transformation to the local --> tracking
92 auto trkXYZ = geom->getMatrixT2L(sensorID) ^ locXYZ;
93 // Transformation to the local --> global
94 auto gloXYZ = geom->getMatrixL2G(sensorID) * locXYZ;
95
96 event.addTrackingFrameInfoToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), trkXYZ.x(), geom->getSensorRefAlpha(sensorID),
97 std::array<float, 2>{trkXYZ.y(), trkXYZ.z()},
98 std::array<float, 3>{sigmaY2, sigmaYZ, sigmaZ2});
99
101 event.addClusterToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), event.getClustersOnLayer(layer).size());
102 if (clsLabels) {
103 // event.addClusterLabelToLayer(layer, *(clsLabels->getLabels(clusterId).begin()));
104 event.setMClabelsContainer(clsLabels);
105 }
106 event.addClusterExternalIndexToLayer(layer, clusterId);
107 clusterId++;
108 }
109}
110
111int 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,
113{
114 event.clear();
117 int clusterId{0};
118
119 auto first = rof.getFirstEntry();
120 auto clusters_in_frame = rof.getROFData(clusters);
121 for (auto& c : clusters_in_frame) {
122 int layer = geom->getLayer(c.getSensorID());
123 float sigmaY2, sigmaZ2, sigmaYZ = 0;
124 auto locXYZ = extractClusterData(c, pattIt, dict, sigmaY2, sigmaZ2);
125 auto sensorID = c.getSensorID();
126 // Inverse transformation to the local --> tracking
127 auto trkXYZ = geom->getMatrixT2L(sensorID) ^ locXYZ;
128 // Transformation to the local --> global
129 auto gloXYZ = geom->getMatrixL2G(sensorID) * locXYZ;
130
131 event.addTrackingFrameInfoToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), trkXYZ.x(), geom->getSensorRefAlpha(sensorID),
132 std::array<float, 2>{trkXYZ.y(), trkXYZ.z()},
133 std::array<float, 3>{sigmaY2, sigmaYZ, sigmaZ2});
134
136 event.addClusterToLayer(layer, gloXYZ.x(), gloXYZ.y(), gloXYZ.z(), event.getClustersOnLayer(layer).size());
137 if (mcLabels) {
138 // event.addClusterLabelToLayer(layer, *(mcLabels->getLabels(first + clusterId).begin()));
139 event.setMClabelsContainer(mcLabels);
140 }
141 event.addClusterExternalIndexToLayer(layer, first + clusterId);
142 clusterId++;
143 }
144 return clusters_in_frame.size();
145}
146
147std::vector<std::unordered_map<int, Label>> ioutils::loadLabels(const int eventsNum, const std::string& fileName)
148{
149 std::vector<std::unordered_map<int, Label>> labelsMap{};
150 std::unordered_map<int, Label> currentEventLabelsMap{};
151 std::ifstream inputStream{};
152 std::string line{};
153 int monteCarloId{}, pdgCode{}, numberOfClusters{};
154 float transverseMomentum{}, phi{}, pseudorapidity{};
155
156 labelsMap.reserve(eventsNum);
157
158 inputStream.open(fileName);
159 std::getline(inputStream, line);
160
161 while (std::getline(inputStream, line)) {
162
163 std::istringstream inputStringStream(line);
164
165 if (inputStringStream >> monteCarloId) {
166
167 if (monteCarloId == EventLabelsSeparator) {
168
169 labelsMap.emplace_back(currentEventLabelsMap);
170 currentEventLabelsMap.clear();
171
172 } else {
173
174 if (inputStringStream >> transverseMomentum >> phi >> pseudorapidity >> pdgCode >> numberOfClusters) {
175
176 if (std::abs(pdgCode) == constants::pdgcodes::PionCode && numberOfClusters == 7) {
177
178 currentEventLabelsMap.emplace(std::piecewise_construct, std::forward_as_tuple(monteCarloId),
179 std::forward_as_tuple(monteCarloId, transverseMomentum, phi,
180 pseudorapidity, pdgCode, numberOfClusters));
181 }
182 }
183 }
184 }
185 }
186
187 labelsMap.emplace_back(currentEventLabelsMap);
188
189 return labelsMap;
190}
191
192// void ioutils::writeRoadsReport(std::ofstream& correctRoadsOutputStream, std::ofstream& duplicateRoadsOutputStream,
193// std::ofstream& fakeRoadsOutputStream, const std::vector<std::vector<Road<5>>>& roads,
194// const std::unordered_map<int, Label>& labelsMap)
195// {
196// const int numVertices{static_cast<int>(roads.size())};
197// std::unordered_set<int> foundMonteCarloIds{};
198
199// correctRoadsOutputStream << EventLabelsSeparator << std::endl;
200// fakeRoadsOutputStream << EventLabelsSeparator << std::endl;
201
202// for (int iVertex{0}; iVertex < numVertices; ++iVertex) {
203
204// const std::vector<Road<5>>& currentVertexRoads{roads[iVertex]};
205// const int numRoads{static_cast<int>(currentVertexRoads.size())};
206
207// for (int iRoad{0}; iRoad < numRoads; ++iRoad) {
208
209// const Road<5>& currentRoad{currentVertexRoads[iRoad]};
210// const int currentRoadLabel{currentRoad.getLabel()};
211
212// if (!labelsMap.count(currentRoadLabel)) {
213
214// continue;
215// }
216
217// const Label& currentLabel{labelsMap.at(currentRoadLabel)};
218
219// if (currentRoad.isFakeRoad()) {
220
221// fakeRoadsOutputStream << currentLabel << std::endl;
222
223// } else {
224
225// if (foundMonteCarloIds.count(currentLabel.monteCarloId)) {
226
227// duplicateRoadsOutputStream << currentLabel << std::endl;
228
229// } else {
230
231// correctRoadsOutputStream << currentLabel << std::endl;
232// foundMonteCarloIds.emplace(currentLabel.monteCarloId);
233// }
234// }
235// }
236// }
237// }
238
239} // namespace its
240} // 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 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:44
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:73
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:111
std::vector< std::unordered_map< int, Label > > loadLabels(const int, const std::string &)
Definition IOUtils.cxx:147
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