Project
Loading...
Searching...
No Matches
TopologyDictionary.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
13
16#include "ITS3Base/SpecsV2.h"
19#include <TFile.h>
20#include <iostream>
21
23
24namespace o2::its3
25{
26
27void TopologyDictionaryData::print() const noexcept
28{
29 LOG(info) << "Number of keys: " << mVectorOfIDs.size();
30 LOG(info) << "Number of common topologies: " << mCommonMap.size();
31 LOG(info) << "Number of groups of rare topologies: " << mGroupMap.size();
32}
33
38
39TopologyDictionary::TopologyDictionary(const std::string& fileName)
40{
41 readFromFile(fileName);
42}
43
44void TopologyDictionary::print() const noexcept
45{
46 LOG(info) << "ITS3 TopologyDictionary";
47 LOG(info) << "InnerBarrel";
48 mDataIB.print();
49 LOG(info) << "OuterBarrel";
50 mDataOB.print();
51}
52
54{
55 mDataIB.mSmallTopologiesLUT.fill(-1);
56 mDataOB.mSmallTopologiesLUT.fill(-1);
57 mDataIB.mVectorOfIDs.clear();
58 mDataOB.mVectorOfIDs.clear();
59}
60
61void TopologyDictionary::resetMaps(bool IB) noexcept
62{
63 auto& data = (IB) ? mDataIB : mDataOB;
64 data.mCommonMap.clear();
65 data.mGroupMap.clear();
66}
67
68std::ostream& operator<<(std::ostream& os, const its3::TopologyDictionary& dict)
69{
70 int ID = 0;
71 os << "--- InnerBarrel:\n";
72 for (auto& p : dict.mDataIB.mVectorOfIDs) {
73 os << "ID: " << ID++ << " Hash: " << p.mHash << " ErrX: " << p.mErrX << " ErrZ : " << p.mErrZ << " xCOG: " << p.mXCOG << " zCOG: " << p.mZCOG << " Npixles: " << p.mNpixels << " Frequency: " << p.mFrequency << " isGroup : " << std::boolalpha << p.mIsGroup << '\n'
74 << p.mPattern << '\n'
75 << "*********************************************************" << '\n'
76 << '\n';
77 }
78 ID = 0;
79 os << "--- OuterBarrel:\n";
80 for (auto& p : dict.mDataOB.mVectorOfIDs) {
81 os << "ID: " << ID++ << " Hash: " << p.mHash << " ErrX: " << p.mErrX << " ErrZ : " << p.mErrZ << " xCOG: " << p.mXCOG << " zCOG: " << p.mZCOG << " Npixles: " << p.mNpixels << " Frequency: " << p.mFrequency << " isGroup : " << std::boolalpha << p.mIsGroup << '\n'
82 << p.mPattern << '\n'
83 << "*********************************************************" << '\n'
84 << '\n';
85 }
86 return os;
87}
88
89void TopologyDictionary::writeBinaryFile(const std::string& outputfile)
90{
91 std::ofstream file_output(outputfile, std::ios::out | std::ios::binary);
92 if (!file_output) {
93 throw std::runtime_error(fmt::format("Cannot open output file %s!", outputfile));
94 }
95
96 auto writeData = [](auto& file_output, auto& data) {
97 auto size = data.mVectorOfIDs.size();
98 file_output.write(reinterpret_cast<char*>(&size), sizeof(size));
99 for (auto& p : data.mVectorOfIDs) {
100 file_output.write(reinterpret_cast<char*>(&p.mHash), sizeof(unsigned long));
101 file_output.write(reinterpret_cast<char*>(&p.mErrX), sizeof(float));
102 file_output.write(reinterpret_cast<char*>(&p.mErrZ), sizeof(float));
103 file_output.write(reinterpret_cast<char*>(&p.mErr2X), sizeof(float));
104 file_output.write(reinterpret_cast<char*>(&p.mErr2Z), sizeof(float));
105 file_output.write(reinterpret_cast<char*>(&p.mXCOG), sizeof(float));
106 file_output.write(reinterpret_cast<char*>(&p.mZCOG), sizeof(float));
107 file_output.write(reinterpret_cast<char*>(&p.mNpixels), sizeof(int));
108 file_output.write(reinterpret_cast<char*>(&p.mFrequency), sizeof(double));
109 file_output.write(reinterpret_cast<char*>(&p.mIsGroup), sizeof(bool));
110 file_output.write(const_cast<char*>(reinterpret_cast<const char*>(&p.mPattern.getPattern())),
112 }
113 };
114
115 writeData(file_output, mDataIB);
116 writeData(file_output, mDataOB);
117
118 file_output.close();
119}
120
121void TopologyDictionary::readFromFile(const std::string& fname)
122{
123 LOGP(info, "Reading TopologyDictionary from File '{}'", fname);
124 if (o2::utils::Str::endsWith(fname, ".root")) {
125 std::unique_ptr<TopologyDictionary> d{loadFrom(fname)};
126 *this = *d;
127 } else if (o2::utils::Str::endsWith(fname, ".bin")) {
128 readBinaryFile(fname);
129 } else {
130 throw std::runtime_error(fmt::format("Unrecognized format {}", fname));
131 }
132}
133
134void TopologyDictionary::readBinaryFile(const std::string& fname)
135{
136 reset();
137
138 std::ifstream in(fname.data(), std::ios::in | std::ios::binary);
139 if (!in.is_open()) {
140 LOG(error) << "The file " << fname << " coud not be opened";
141 throw std::runtime_error("The file coud not be opened");
142 } else {
143
144 auto readData = [](auto& in, auto& data) {
145 int groupID = 0;
146 std::size_t size{}, cur{};
148 in.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
149 while (cur++ != size) {
150 in.read(reinterpret_cast<char*>(&gr.mHash), sizeof(unsigned long));
151 in.read(reinterpret_cast<char*>(&gr.mErrX), sizeof(float));
152 in.read(reinterpret_cast<char*>(&gr.mErrZ), sizeof(float));
153 in.read(reinterpret_cast<char*>(&gr.mErr2X), sizeof(float));
154 in.read(reinterpret_cast<char*>(&gr.mErr2Z), sizeof(float));
155 in.read(reinterpret_cast<char*>(&gr.mXCOG), sizeof(float));
156 in.read(reinterpret_cast<char*>(&gr.mZCOG), sizeof(float));
157 in.read(reinterpret_cast<char*>(&gr.mNpixels), sizeof(int));
158 in.read(reinterpret_cast<char*>(&gr.mFrequency), sizeof(double));
159 in.read(reinterpret_cast<char*>(&gr.mIsGroup), sizeof(bool));
160 in.read(const_cast<char*>(reinterpret_cast<const char*>(&gr.mPattern.getPattern())), sizeof(unsigned char) * (itsmft::ClusterPattern::kExtendedPatternBytes));
161 data.mVectorOfIDs.push_back(gr);
162 if (!gr.mIsGroup) {
163 data.mCommonMap.insert(std::make_pair(gr.mHash, groupID));
164 if (gr.mPattern.getUsedBytes() == 1) {
165 data.mSmallTopologiesLUT[(gr.mPattern.getColumnSpan() - 1) * 255 + (int)gr.mPattern.getByte(2)] = groupID;
166 }
167 } else {
168 data.mGroupMap.insert(std::make_pair((int)(gr.mHash >> 32) & 0x00000000ffffffff, groupID));
169 }
170 groupID++;
171 }
172 };
173
174 readData(in, mDataIB);
175 readData(in, mDataOB);
176 }
177 in.close();
178}
179
180TH1F* TopologyDictionary::getTopologyDistribution(const std::string_view hname, bool IB) const
181{
182 int dictSize = getSize(IB);
183 auto* histo = new TH1F(hname.data(), Form("%s;Topology ID;Frequency", (IB) ? "InnerBarrel" : "OuterBarrel"), dictSize, -0.5, dictSize - 0.5);
184 histo->SetFillColor(kRed);
185 histo->SetFillStyle(3005);
186 histo->SetDrawOption("histo");
187 for (int i = 0; i < dictSize; i++) {
188 histo->Fill(i, getFrequency(i, IB));
189 }
190 return histo;
191}
192
193template <typename T>
195{
196 static std::array<o2::its3::SegmentationMosaix, 3> mIBSegmentations{0, 1, 2};
200 locCl.SetX(locCl.X() + this->getXCOG(cl.getPatternID(), false) * itsmft::SegmentationAlpide::PitchRow);
201 locCl.SetZ(locCl.Z() + this->getZCOG(cl.getPatternID(), false) * itsmft::SegmentationAlpide::PitchCol);
202 } else {
204 mIBSegmentations[layer].detectorToLocalUnchecked(cl.getRow(), cl.getCol(), locCl);
205 locCl.SetX(locCl.X() + this->getXCOG(cl.getPatternID(), true) * its3::SegmentationMosaix::PitchRow);
206 locCl.SetY(its3::constants::pixelarray::pixels::apts::responseYShift);
207 locCl.SetZ(locCl.Z() + this->getZCOG(cl.getPatternID(), true) * its3::SegmentationMosaix::PitchCol);
208 float xCurved{0.f}, yCurved{0.f};
209 mIBSegmentations[layer].flatToCurved(locCl.X(), locCl.Y(), xCurved, yCurved);
210 locCl.SetXYZ(xCurved, yCurved, locCl.Z());
211 }
212 return locCl;
213}
214
215template <typename T>
217{
218 static std::array<o2::its3::SegmentationMosaix, 3> mIBSegmentations{0, 1, 2};
219 auto refRow = cl.getRow();
220 auto refCol = cl.getCol();
221 float xCOG = 0, zCOG = 0;
222 patt.getCOG(xCOG, zCOG);
223 if (isGroup) {
224 refRow -= round(xCOG);
225 refCol -= round(zCOG);
226 }
229 o2::itsmft::SegmentationAlpide::detectorToLocalUnchecked(refRow + xCOG, refCol + zCOG, locCl);
230 } else {
232 mIBSegmentations[layer].detectorToLocalUnchecked(refRow + xCOG, refCol + zCOG, locCl);
233 float xCurved{0.f}, yCurved{0.f};
234 mIBSegmentations[layer].flatToCurved(locCl.X(), locCl.Y(), xCurved, yCurved);
235 locCl.SetXYZ(xCurved, yCurved, locCl.Z());
236 }
237 return locCl;
238}
239
240TopologyDictionary* TopologyDictionary::loadFrom(const std::string& fname, const std::string& objName)
241{
242 LOGP(info, "Loading TopologyDictionary from {} with name {}", fname, objName);
243 // load object from file
244 TFile fl(fname.c_str(), "READ");
245 if (fl.IsZombie() || !fl.IsOpen()) {
246 throw std::runtime_error(fmt::format("Failed to open {} file", fname));
247 }
248 auto dict = fl.Get<its3::TopologyDictionary>(objName.c_str());
249 if (dict == nullptr) {
250 throw std::runtime_error(fmt::format("Failed to load {} from {}", objName, fname));
251 }
252 return dict;
253}
254
255// Explicitly instaniate templates
256template math_utils::Point3D<float> TopologyDictionary::getClusterCoordinates<float>(const itsmft::CompClusterExt& cl) const;
257template math_utils::Point3D<float> TopologyDictionary::getClusterCoordinates<float>(const itsmft::CompClusterExt& cl, const itsmft::ClusterPattern& patt, bool isGroup);
258
259} // namespace o2::its3
Definition of the BuildTopologyDictionary class for ITS3.
ClassImp(o2::its3::TopologyDictionary)
int32_t i
Definition of the SegmentationAlpide class.
Definition of the SegmentationMosaix class.
static constexpr float PitchCol
static constexpr float PitchRow
void readFromFile(const std::string &fileName)
math_utils::Point3D< T > getClusterCoordinates(const itsmft::CompClusterExt &cl) const
Returns the local position of a compact cluster.
static TopologyDictionary * loadFrom(const std::string &fileName="", const std::string &objName="ccdb_object")
void reset() noexcept
Resets internal structures.
void readBinaryFile(const std::string &fileName)
Reads the dictionary from a binary file.
double getFrequency(int n, bool IB=true) const
Returns the frequency of the n_th element;.
void writeBinaryFile(const std::string &outputFile)
Prints the dictionary in a binary file.
int getSize(bool IB) const
Returns the number of elements in the dicionary;.
bool isGroup(int n, bool IB=true) const
Returns true if the element corresponds to a group of rare topologies.
void resetMaps(bool IB=true) noexcept
TH1F * getTopologyDistribution(const std::string_view hname, bool IB=true) const
Fills a hostogram with the distribution of the IDs.
int getColumnSpan() const
Returns the number of columns.
static int getCOG(int rowSpan, int colSpan, const unsigned char patt[MaxPatternBytes], float &xCOG, float &zCOG)
Static: Compute pattern's COG position. Returns the number of fired pixels.
static constexpr int kExtendedPatternBytes
Maximum number of bytes for the cluster puttern + 2 bytes respectively for the number of rows and col...
unsigned char getByte(int n) const
Returns a specific byte of the pattern.
const std::array< unsigned char, kExtendedPatternBytes > & getPattern() const
Returns the pattern.
int getUsedBytes() const
Returns the number of bytes used for the pattern.
UShort_t getSensorID() const
UShort_t getRow() const
Definition CompCluster.h:57
UShort_t getPatternID() const
Definition CompCluster.h:59
UShort_t getCol() const
Definition CompCluster.h:58
static void detectorToLocalUnchecked(L row, L col, T &xRow, T &zCol)
static constexpr float PitchCol
static constexpr float PitchRow
GLsizeiptr size
Definition glcorearb.h:659
GLboolean * data
Definition glcorearb.h:298
GLenum GLuint GLint GLint layer
Definition glcorearb.h:1310
bool isDetITS3(T detID)
Definition SpecsV2.h:209
std::ostream & operator<<(std::ostream &os, const BuildTopologyDictionary &DB)
void readData(o2::tpc::GBTFrameContainer &container, std::vector< std::ofstream * > &outfiles, int &run, int &done)
std::unordered_map< unsigned long, int > mCommonMap
Map of pair <hash, position in mVectorOfIDs>
std::vector< itsmft::GroupStruct > mVectorOfIDs
Vector of topologies and groups.
std::unordered_map< int, int > mGroupMap
Map of pair <groudID, position in mVectorOfIDs>
std::array< int, STopoSize > mSmallTopologiesLUT
Look-Up Table for the topologies with 1-byte linearised matrix.
Structure containing the most relevant pieces of information of a topology.
float mErrX
Error associated to the hit point in the x direction.
float mErr2X
Squared Error associated to the hit point in the x direction.
float mErrZ
Error associated to the hit point in the z direction.
unsigned long mHash
Hashcode.
float mXCOG
x position of the COG wrt the bottom left corner of the bounding box
int mNpixels
Number of fired pixels.
double mFrequency
Frequency of the topology.
bool mIsGroup
false: common topology; true: group of rare topologies
float mErr2Z
Squared Error associated to the hit point in the z direction.
float mZCOG
z position of the COG wrt the bottom left corner of the bounding box
static bool endsWith(const std::string &s, const std::string &ending)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"