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
18#include <TFile.h>
19#include <iostream>
20
22
23namespace o2::its3
24{
25
26void TopologyDictionaryData::print() const noexcept
27{
28 LOG(info) << "Number of keys: " << mVectorOfIDs.size();
29 LOG(info) << "Number of common topologies: " << mCommonMap.size();
30 LOG(info) << "Number of groups of rare topologies: " << mGroupMap.size();
31}
32
37
38TopologyDictionary::TopologyDictionary(const std::string& fileName)
39{
40 readFromFile(fileName);
41}
42
43void TopologyDictionary::print() const noexcept
44{
45 LOG(info) << "ITS3 TopologyDictionary";
46 LOG(info) << "InnerBarrel";
47 mDataIB.print();
48 LOG(info) << "OuterBarrel";
49 mDataOB.print();
50}
51
53{
54 mDataIB.mSmallTopologiesLUT.fill(-1);
55 mDataOB.mSmallTopologiesLUT.fill(-1);
56 mDataIB.mVectorOfIDs.clear();
57 mDataOB.mVectorOfIDs.clear();
58}
59
60void TopologyDictionary::resetMaps(bool IB) noexcept
61{
62 auto& data = (IB) ? mDataIB : mDataOB;
63 data.mCommonMap.clear();
64 data.mGroupMap.clear();
65}
66
67std::ostream& operator<<(std::ostream& os, const its3::TopologyDictionary& dict)
68{
69 int ID = 0;
70 os << "--- InnerBarrel:\n";
71 for (auto& p : dict.mDataIB.mVectorOfIDs) {
72 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'
73 << p.mPattern << '\n'
74 << "*********************************************************" << '\n'
75 << '\n';
76 }
77 ID = 0;
78 os << "--- OuterBarrel:\n";
79 for (auto& p : dict.mDataOB.mVectorOfIDs) {
80 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'
81 << p.mPattern << '\n'
82 << "*********************************************************" << '\n'
83 << '\n';
84 }
85 return os;
86}
87
88void TopologyDictionary::writeBinaryFile(const std::string& outputfile)
89{
90 std::ofstream file_output(outputfile, std::ios::out | std::ios::binary);
91 if (!file_output) {
92 throw std::runtime_error(fmt::format("Cannot open output file %s!", outputfile));
93 }
94
95 auto writeData = [](auto& file_output, auto& data) {
96 auto size = data.mVectorOfIDs.size();
97 file_output.write(reinterpret_cast<char*>(&size), sizeof(size));
98 for (auto& p : data.mVectorOfIDs) {
99 file_output.write(reinterpret_cast<char*>(&p.mHash), sizeof(unsigned long));
100 file_output.write(reinterpret_cast<char*>(&p.mErrX), sizeof(float));
101 file_output.write(reinterpret_cast<char*>(&p.mErrZ), sizeof(float));
102 file_output.write(reinterpret_cast<char*>(&p.mErr2X), sizeof(float));
103 file_output.write(reinterpret_cast<char*>(&p.mErr2Z), sizeof(float));
104 file_output.write(reinterpret_cast<char*>(&p.mXCOG), sizeof(float));
105 file_output.write(reinterpret_cast<char*>(&p.mZCOG), sizeof(float));
106 file_output.write(reinterpret_cast<char*>(&p.mNpixels), sizeof(int));
107 file_output.write(reinterpret_cast<char*>(&p.mFrequency), sizeof(double));
108 file_output.write(reinterpret_cast<char*>(&p.mIsGroup), sizeof(bool));
109 file_output.write(const_cast<char*>(reinterpret_cast<const char*>(&p.mPattern.getPattern())),
111 }
112 };
113
114 writeData(file_output, mDataIB);
115 writeData(file_output, mDataOB);
116
117 file_output.close();
118}
119
120void TopologyDictionary::readFromFile(const std::string& fname)
121{
122 LOGP(info, "Reading TopologyDictionary from File '{}'", fname);
123 if (o2::utils::Str::endsWith(fname, ".root")) {
124 std::unique_ptr<TopologyDictionary> d{loadFrom(fname)};
125 *this = *d;
126 } else if (o2::utils::Str::endsWith(fname, ".bin")) {
127 readBinaryFile(fname);
128 } else {
129 throw std::runtime_error(fmt::format("Unrecognized format {}", fname));
130 }
131}
132
133void TopologyDictionary::readBinaryFile(const std::string& fname)
134{
135 reset();
136
137 std::ifstream in(fname.data(), std::ios::in | std::ios::binary);
138 if (!in.is_open()) {
139 LOG(error) << "The file " << fname << " coud not be opened";
140 throw std::runtime_error("The file coud not be opened");
141 } else {
142
143 auto readData = [](auto& in, auto& data) {
144 int groupID = 0;
145 std::size_t size{}, cur{};
147 in.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
148 while (cur++ != size) {
149 in.read(reinterpret_cast<char*>(&gr.mHash), sizeof(unsigned long));
150 in.read(reinterpret_cast<char*>(&gr.mErrX), sizeof(float));
151 in.read(reinterpret_cast<char*>(&gr.mErrZ), sizeof(float));
152 in.read(reinterpret_cast<char*>(&gr.mErr2X), sizeof(float));
153 in.read(reinterpret_cast<char*>(&gr.mErr2Z), sizeof(float));
154 in.read(reinterpret_cast<char*>(&gr.mXCOG), sizeof(float));
155 in.read(reinterpret_cast<char*>(&gr.mZCOG), sizeof(float));
156 in.read(reinterpret_cast<char*>(&gr.mNpixels), sizeof(int));
157 in.read(reinterpret_cast<char*>(&gr.mFrequency), sizeof(double));
158 in.read(reinterpret_cast<char*>(&gr.mIsGroup), sizeof(bool));
159 in.read(const_cast<char*>(reinterpret_cast<const char*>(&gr.mPattern.getPattern())), sizeof(unsigned char) * (itsmft::ClusterPattern::kExtendedPatternBytes));
160 data.mVectorOfIDs.push_back(gr);
161 if (!gr.mIsGroup) {
162 data.mCommonMap.insert(std::make_pair(gr.mHash, groupID));
163 if (gr.mPattern.getUsedBytes() == 1) {
164 data.mSmallTopologiesLUT[(gr.mPattern.getColumnSpan() - 1) * 255 + (int)gr.mPattern.getByte(2)] = groupID;
165 }
166 } else {
167 data.mGroupMap.insert(std::make_pair((int)(gr.mHash >> 32) & 0x00000000ffffffff, groupID));
168 }
169 groupID++;
170 }
171 };
172
173 readData(in, mDataIB);
174 readData(in, mDataOB);
175 }
176 in.close();
177}
178
179TH1F* TopologyDictionary::getTopologyDistribution(const std::string_view hname, bool IB) const
180{
181 int dictSize = getSize(IB);
182 auto* histo = new TH1F(hname.data(), Form("%s;Topology ID;Frequency", (IB) ? "InnerBarrel" : "OuterBarrel"), dictSize, -0.5, dictSize - 0.5);
183 histo->SetFillColor(kRed);
184 histo->SetFillStyle(3005);
185 histo->SetDrawOption("histo");
186 for (int i = 0; i < dictSize; i++) {
187 histo->Fill(i, getFrequency(i, IB));
188 }
189 return histo;
190}
191
192template <typename T>
194{
195 static std::array<o2::its3::SegmentationMosaix, 3> mIBSegmentations{0, 1, 2};
199 locCl.SetX(locCl.X() + this->getXCOG(cl.getPatternID(), false) * itsmft::SegmentationAlpide::PitchRow);
200 locCl.SetZ(locCl.Z() + this->getZCOG(cl.getPatternID(), false) * itsmft::SegmentationAlpide::PitchCol);
201 } else {
203 mIBSegmentations[layer].detectorToLocalUnchecked(cl.getRow(), cl.getCol(), locCl);
204 locCl.SetX(locCl.X() + this->getXCOG(cl.getPatternID(), true) * its3::SegmentationMosaix::PitchRow);
205 locCl.SetZ(locCl.Z() + this->getZCOG(cl.getPatternID(), true) * its3::SegmentationMosaix::PitchCol);
206 float xCurved{0.f}, yCurved{0.f};
207 mIBSegmentations[layer].flatToCurved(locCl.X(), locCl.Y(), xCurved, yCurved);
208 locCl.SetXYZ(xCurved, yCurved, locCl.Z());
209 }
210 return locCl;
211}
212
213template <typename T>
215{
216 static std::array<o2::its3::SegmentationMosaix, 3> mIBSegmentations{0, 1, 2};
217 auto refRow = cl.getRow();
218 auto refCol = cl.getCol();
219 float xCOG = 0, zCOG = 0;
220 patt.getCOG(xCOG, zCOG);
221 if (isGroup) {
222 refRow -= round(xCOG);
223 refCol -= round(zCOG);
224 }
227 o2::itsmft::SegmentationAlpide::detectorToLocalUnchecked(refRow + xCOG, refCol + zCOG, locCl);
228 } else {
230 mIBSegmentations[layer].detectorToLocalUnchecked(refRow + xCOG, refCol + zCOG, locCl);
231 float xCurved{0.f}, yCurved{0.f};
232 mIBSegmentations[layer].flatToCurved(locCl.X(), locCl.Y(), xCurved, yCurved);
233 locCl.SetXYZ(xCurved, yCurved, locCl.Z());
234 }
235 return locCl;
236}
237
238TopologyDictionary* TopologyDictionary::loadFrom(const std::string& fname, const std::string& objName)
239{
240 LOGP(info, "Loading TopologyDictionary from {} with name {}", fname, objName);
241 // load object from file
242 TFile fl(fname.c_str(), "READ");
243 if (fl.IsZombie() || !fl.IsOpen()) {
244 throw std::runtime_error(fmt::format("Failed to open {} file", fname));
245 }
246 auto dict = fl.Get<its3::TopologyDictionary>(objName.c_str());
247 if (dict == nullptr) {
248 throw std::runtime_error(fmt::format("Failed to load {} from {}", objName, fname));
249 }
250 return dict;
251}
252
253// Explicitly instaniate templates
254template math_utils::Point3D<float> TopologyDictionary::getClusterCoordinates<float>(const itsmft::CompClusterExt& cl) const;
255template math_utils::Point3D<float> TopologyDictionary::getClusterCoordinates<float>(const itsmft::CompClusterExt& cl, const itsmft::ClusterPattern& patt, bool isGroup);
256
257} // 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:211
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"