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
27{
28 memset(mSmallTopologiesLUT, -1, STopoSize * sizeof(int));
29}
30
31TopologyDictionary::TopologyDictionary(const std::string& fileName)
32{
33 readFromFile(fileName);
34}
35
36std::ostream& operator<<(std::ostream& os, const its3::TopologyDictionary& dict)
37{
38 int ID = 0;
39 for (auto& p : dict.mVectorOfIDs) {
40 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'
41 << p.mPattern << '\n'
42 << "*********************************************************" << '\n'
43 << '\n';
44 }
45 return os;
46}
47
48void TopologyDictionary::writeBinaryFile(const std::string& outputfile)
49{
50 std::ofstream file_output(outputfile, std::ios::out | std::ios::binary);
51 for (auto& p : mVectorOfIDs) {
52 file_output.write(reinterpret_cast<char*>(&p.mHash), sizeof(unsigned long));
53 file_output.write(reinterpret_cast<char*>(&p.mErrX), sizeof(float));
54 file_output.write(reinterpret_cast<char*>(&p.mErrZ), sizeof(float));
55 file_output.write(reinterpret_cast<char*>(&p.mErr2X), sizeof(float));
56 file_output.write(reinterpret_cast<char*>(&p.mErr2Z), sizeof(float));
57 file_output.write(reinterpret_cast<char*>(&p.mXCOG), sizeof(float));
58 file_output.write(reinterpret_cast<char*>(&p.mZCOG), sizeof(float));
59 file_output.write(reinterpret_cast<char*>(&p.mNpixels), sizeof(int));
60 file_output.write(reinterpret_cast<char*>(&p.mFrequency), sizeof(double));
61 file_output.write(reinterpret_cast<char*>(&p.mIsGroup), sizeof(bool));
62 file_output.write(const_cast<char*>(reinterpret_cast<const char*>(&p.mPattern.getPattern())),
64 }
65 file_output.close();
66}
67
68int TopologyDictionary::readFromFile(const std::string& fname)
69{
70 LOGP(info, "Reading TopologyDictionary from File '{}'", fname);
71 if (o2::utils::Str::endsWith(fname, ".root")) {
72 std::unique_ptr<TopologyDictionary> d{loadFrom(fname)};
73 *this = *d;
74 } else if (o2::utils::Str::endsWith(fname, ".bin")) {
75 readBinaryFile(fname);
76 } else {
77 throw std::runtime_error(fmt::format("Unrecognized format {}", fname));
78 }
79 return 0;
80}
81
82int TopologyDictionary::readBinaryFile(const std::string& fname)
83{
84 mVectorOfIDs.clear();
85 mCommonMap.clear();
86 for (auto& p : mSmallTopologiesLUT) {
87 p = -1;
88 }
89 std::ifstream in(fname.data(), std::ios::in | std::ios::binary);
91 int groupID = 0;
92 if (!in.is_open()) {
93 LOG(error) << "The file " << fname << " coud not be opened";
94 throw std::runtime_error("The file coud not be opened");
95 } else {
96 while (in.read(reinterpret_cast<char*>(&gr.mHash), sizeof(unsigned long))) {
97 in.read(reinterpret_cast<char*>(&gr.mErrX), sizeof(float));
98 in.read(reinterpret_cast<char*>(&gr.mErrZ), sizeof(float));
99 in.read(reinterpret_cast<char*>(&gr.mErr2X), sizeof(float));
100 in.read(reinterpret_cast<char*>(&gr.mErr2Z), sizeof(float));
101 in.read(reinterpret_cast<char*>(&gr.mXCOG), sizeof(float));
102 in.read(reinterpret_cast<char*>(&gr.mZCOG), sizeof(float));
103 in.read(reinterpret_cast<char*>(&gr.mNpixels), sizeof(int));
104 in.read(reinterpret_cast<char*>(&gr.mFrequency), sizeof(double));
105 in.read(reinterpret_cast<char*>(&gr.mIsGroup), sizeof(bool));
106 in.read(const_cast<char*>(reinterpret_cast<const char*>(&gr.mPattern.getPattern())), sizeof(unsigned char) * (itsmft::ClusterPattern::kExtendedPatternBytes));
107 mVectorOfIDs.push_back(gr);
108 if (!gr.mIsGroup) {
109 mCommonMap.insert(std::make_pair(gr.mHash, groupID));
110 if (gr.mPattern.getUsedBytes() == 1) {
111 mSmallTopologiesLUT[(gr.mPattern.getColumnSpan() - 1) * 255 + (int)gr.mPattern.getByte(2)] = groupID;
112 }
113 } else {
114 mGroupMap.insert(std::make_pair((int)(gr.mHash >> 32) & 0x00000000ffffffff, groupID));
115 }
116 groupID++;
117 }
118 }
119 in.close();
120 return 0;
121}
122
123TH1F* TopologyDictionary::getTopologyDistribution(const std::string_view hname) const
124{
125 int dictSize = getSize();
126 auto* histo = new TH1F(hname.data(), ";Topology ID;Frequency", dictSize, -0.5, dictSize - 0.5);
127 histo->SetFillColor(kRed);
128 histo->SetFillStyle(3005);
129 histo->SetDrawOption("histo");
130 for (int i = 0; i < dictSize; i++) {
131 histo->Fill(i, getFrequency(i));
132 }
133 return histo;
134}
135
136template <typename T>
138{
142 locCl.SetX(locCl.X() + this->getXCOG(cl.getPatternID()) * itsmft::SegmentationAlpide::PitchRow);
143 locCl.SetZ(locCl.Z() + this->getZCOG(cl.getPatternID()) * itsmft::SegmentationAlpide::PitchCol);
144 } else {
146 its3::SuperSegmentations[layer].detectorToLocalUnchecked(cl.getRow(), cl.getCol(), locCl);
147 locCl.SetX(locCl.X() + this->getXCOG(cl.getPatternID()) * its3::SegmentationSuperAlpide::mPitchRow);
148 locCl.SetZ(locCl.Z() + this->getZCOG(cl.getPatternID()) * its3::SegmentationSuperAlpide::mPitchCol);
149 float xCurved{0.f}, yCurved{0.f};
150 its3::SuperSegmentations[layer].flatToCurved(locCl.X(), locCl.Y(), xCurved, yCurved);
151 locCl.SetXYZ(xCurved, yCurved, locCl.Z());
152 }
153 return locCl;
154}
155
156template <typename T>
158{
159 auto refRow = cl.getRow();
160 auto refCol = cl.getCol();
161 float xCOG = 0, zCOG = 0;
162 patt.getCOG(xCOG, zCOG);
163 if (isGroup) {
164 refRow -= round(xCOG);
165 refCol -= round(zCOG);
166 }
169 o2::itsmft::SegmentationAlpide::detectorToLocalUnchecked(refRow + xCOG, refCol + zCOG, locCl);
170 } else {
172 its3::SuperSegmentations[layer].detectorToLocalUnchecked(refRow + xCOG, refCol + zCOG, locCl);
173 float xCurved{0.f}, yCurved{0.f};
174 its3::SuperSegmentations[layer].flatToCurved(locCl.X(), locCl.Y(), xCurved, yCurved);
175 locCl.SetXYZ(xCurved, yCurved, locCl.Z());
176 }
177 return locCl;
178}
179
180TopologyDictionary* TopologyDictionary::loadFrom(const std::string& fname, const std::string& objName)
181{
182 LOGP(info, "Loading TopologyDictionary from {} with name {}", fname, objName);
183 // load object from file
184 TFile fl(fname.c_str(), "READ");
185 if (fl.IsZombie() || !fl.IsOpen()) {
186 throw std::runtime_error(fmt::format("Failed to open {} file", fname));
187 }
188 auto dict = fl.Get<its3::TopologyDictionary>(objName.c_str());
189 if (dict == nullptr) {
190 throw std::runtime_error(fmt::format("Failed to load {} from {}", objName, fname));
191 }
192 return dict;
193}
194
195// Explicitly instaniate templates
196template math_utils::Point3D<float> TopologyDictionary::getClusterCoordinates<float>(const itsmft::CompClusterExt& cl) const;
197template math_utils::Point3D<float> TopologyDictionary::getClusterCoordinates<float>(const itsmft::CompClusterExt& cl, const itsmft::ClusterPattern& patt, bool isGroup);
198
199} // 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 SegmentationSuperAlpide class.
math_utils::Point3D< T > getClusterCoordinates(const itsmft::CompClusterExt &cl) const
Returns the local position of a compact cluster.
int getSize() const
Returns the number of elements in the dicionary;.
static TopologyDictionary * loadFrom(const std::string &fileName="", const std::string &objName="ccdb_object")
int readBinaryFile(const std::string &fileName)
Reads the dictionary from a binary file.
bool isGroup(int n) const
Returns true if the element corresponds to a group of rare topologies.
TH1F * getTopologyDistribution(const std::string_view hname="h_topo_dist") const
Fills a hostogram with the distribution of the IDs.
void writeBinaryFile(const std::string &outputFile)
Prints the dictionary in a binary file.
double getFrequency(int n) const
Returns the frequency of the n_th element;.
int readFromFile(const std::string &fileName)
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
GLenum GLuint GLint GLint layer
Definition glcorearb.h:1310
std::ostream & operator<<(std::ostream &os, const o2::header::RDHAny &rdh)
Definition RDHManip.cxx:84
bool isDetITS3(T detID)
Definition SpecsV2.h:161
const std::array< SegmentationSuperAlpide, constants::nLayers > SuperSegmentations
Segmentation array.
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"