32 auto& topoStat = mTopologyMap[cluster.
getHash()];
33 topoStat.countsTotal++;
34 if (topoStat.countsTotal == 1) {
35 topoStat.topology = cluster;
46 topoStat.countsWithBias = 1;
51 mMapInfo.emplace(cluster.
getHash(), topInf);
54 auto num = topoStat.countsWithBias++;
55 auto ind = mMapInfo.find(cluster.
getHash());
56 float tmpxMean = ind->second.mXmean;
57 float newxMean = ind->second.mXmean = ((tmpxMean)*
num + dX) / (
num + 1);
58 float tmpxSigma2 = ind->second.mXsigma2;
59 ind->second.mXsigma2 = (
num * tmpxSigma2 + (dX - tmpxMean) * (dX - newxMean)) / (
num + 1);
60 float tmpzMean = ind->second.mZmean;
61 float newzMean = ind->second.mZmean = ((tmpzMean)*
num + dZ) / (
num + 1);
62 float tmpzSigma2 = ind->second.mZsigma2;
63 ind->second.mZsigma2 = (
num * tmpzSigma2 + (dZ - tmpzMean) * (dZ - newzMean)) / (
num + 1);
70 mTopologyFrequency.clear();
71 for (
auto&& p : mTopologyMap) {
72 mTopologyFrequency.emplace_back(p.second.countsTotal, p.first);
74 std::sort(mTopologyFrequency.begin(), mTopologyFrequency.end(),
75 [](
const std::pair<unsigned long, unsigned long>& couple1,
76 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
77 mNCommonTopologies = 0;
78 mDictionary.mCommonMap.clear();
79 mDictionary.mGroupMap.clear();
80 mFrequencyThreshold = thr;
81 for (
auto& q : mTopologyFrequency) {
82 if (((
double)q.first) / mTotClusters > thr) {
90 LOGP(warning,
"Redefining prob. threshould from {} to {} to be below InvalidPatternID (was {})", thr, mFrequencyThreshold, mNCommonTopologies);
101 mTopologyFrequency.clear();
102 for (
auto&& p : mTopologyMap) {
103 mTopologyFrequency.emplace_back(p.second.countsTotal, p.first);
105 std::sort(mTopologyFrequency.begin(), mTopologyFrequency.end(),
106 [](
const std::pair<unsigned long, unsigned long>& couple1,
107 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
108 mNCommonTopologies = nCommon;
109 mDictionary.mCommonMap.clear();
110 mDictionary.mGroupMap.clear();
111 mFrequencyThreshold = ((double)mTopologyFrequency[mNCommonTopologies - 1].
first) / mTotClusters;
116 mTopologyFrequency.clear();
117 if (cumulative <= 0. || cumulative >= 1.) {
121 for (
auto&& p : mTopologyMap) {
122 mTopologyFrequency.emplace_back(p.second.countsTotal, p.first);
124 std::sort(mTopologyFrequency.begin(), mTopologyFrequency.end(),
125 [](
const std::pair<unsigned long, unsigned long>& couple1,
126 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
127 mNCommonTopologies = 0;
128 mDictionary.mCommonMap.clear();
129 mDictionary.mGroupMap.clear();
130 for (
auto& q : mTopologyFrequency) {
131 totFreq += ((double)(q.first)) / mTotClusters;
132 if (totFreq < cumulative) {
133 mNCommonTopologies++;
135 totFreq -= ((double)(q.first)) / mTotClusters;
136 mNCommonTopologies--;
137 LOGP(warning,
"Redefining cumulative threshould from {} to {} to be below InvalidPatternID)", cumulative, totFreq);
143 mFrequencyThreshold = ((double)(mTopologyFrequency[--mNCommonTopologies].
first)) / mTotClusters;
144 while (std::fabs(((
double)mTopologyFrequency[mNCommonTopologies].
first) / mTotClusters - mFrequencyThreshold) < 1.e-15) {
145 mNCommonTopologies--;
147 mFrequencyThreshold = ((double)mTopologyFrequency[mNCommonTopologies++].
first) / mTotClusters;
152 LOG(info) <<
"Dictionary finalisation";
153 LOG(info) <<
"Number of clusters: " << mTotClusters;
156 for (
unsigned int j = 0;
j < mNCommonTopologies;
j++) {
158 gr.
mHash = mTopologyFrequency[
j].second;
162 const auto& topo = mMapInfo.find(gr.
mHash)->second;
163 gr.
mErrX = std::sqrt(topo.mXsigma2);
164 gr.
mErrZ = std::sqrt(topo.mZsigma2);
165 gr.
mErr2X = topo.mXsigma2;
166 gr.
mErr2Z = topo.mZsigma2;
167 gr.
mXCOG = -1 * topo.mCOGx;
168 gr.
mZCOG = topo.mCOGz;
172 mDictionary.mVectorOfIDs.push_back(gr);
174 LOGP(warning,
"Limiting N unique topologies to {}, threshold freq. to {}, cumulative freq. to {} to be below InvalidPatternID",
j, gr.
mFrequency, totFreq);
175 mNCommonTopologies =
j;
183 std::unordered_map<int, std::pair<itsmft::GroupStruct, unsigned long>> tmp_GroupMap;
195 gr.
mHash = (((
unsigned long)(grNum)) << 32) & 0xffffffff00000000;
202 gr.
mNpixels = rowBinEdge * colBinEdge;
207 dummyPattern[0] = (
unsigned char)rowBinEdge;
208 dummyPattern[1] = (
unsigned char)colBinEdge;
209 int nBits = rowBinEdge * colBinEdge;
210 int nBytes = nBits / 8;
211 for (
int iB = 2; iB < nBytes + 2; iB++) {
212 dummyPattern[iB] = (
unsigned char)255;
214 int residualBits = nBits % 8;
215 if (residualBits != 0) {
216 unsigned char tempChar = 0;
217 while (residualBits > 0) {
219 tempChar |= 1 << (7 - residualBits);
221 dummyPattern[nBytes + 2] = tempChar;
225 tmp_GroupMap[grNum] = std::make_pair(gr, 0);
228 int rs{}, cs{},
index{};
231 for (
auto j{mNCommonTopologies};
j < mTopologyFrequency.size();
j++) {
232 unsigned long hash1 = mTopologyFrequency[
j].second;
233 rs = mTopologyMap.find(hash1)->second.topology.getRowSpan();
234 cs = mTopologyMap.find(hash1)->second.topology.getColumnSpan();
236 tmp_GroupMap[
index].second += mTopologyFrequency[
j].first;
239 for (
auto&& p : tmp_GroupMap) {
242 mDictionary.mVectorOfIDs.push_back(
group);
247 return (!a.mIsGroup) && b.mIsGroup ? true : (a.mIsGroup && (!b.mIsGroup) ? false : (a.mFrequency > b.mFrequency));
256 for (
int iKey = 0; iKey < mDictionary.
getSize(); iKey++) {
259 mDictionary.mCommonMap.emplace(gr.
mHash, iKey);
264 mDictionary.mGroupMap.emplace((
int)(gr.
mHash >> 32) & 0x00000000ffffffff, iKey);
267 LOG(info) <<
"Dictionay finalised";
268 LOG(info) <<
"Number of keys: " << mDictionary.
getSize();
269 LOG(info) <<
"Number of common topologies: " << mDictionary.mCommonMap.size();
270 LOG(info) <<
"Number of groups of rare topologies: " << mDictionary.mGroupMap.size();
275 for (
unsigned int i = 0;
i < DB.mNCommonTopologies;
i++) {
276 const unsigned long& hash = DB.mTopologyFrequency[
i].second;
277 os <<
"Hash: " << hash <<
'\n';
278 os <<
"counts: " << DB.mTopologyMap.find(hash)->second.countsTotal;
279 os <<
" (with bias provided: " << DB.mTopologyMap.find(hash)->second.countsWithBias <<
")" <<
'\n';
280 os <<
"sigmaX: " << std::sqrt(DB.mMapInfo.find(hash)->second.mXsigma2) <<
'\n';
281 os <<
"sigmaZ: " << std::sqrt(DB.mMapInfo.find(hash)->second.mZsigma2) <<
'\n';
282 os << DB.mTopologyMap.find(hash)->second.topology;
289 LOG(info) <<
"Saving the the dictionary in binary format: ";
290 std::ofstream out(fname);
293 LOG(info) <<
" `-> done!";
298 LOG(info) <<
"Printing the dictionary: ";
299 std::ofstream out(fname);
302 LOG(info) <<
" `-> done!";
307 LOG(info) <<
"Saving the the dictionary in a ROOT file: " << fname;
308 TFile
output(fname.c_str(),
"recreate");
309 output.WriteObjectAny(&mDictionary, mDictionary.Class(),
"ccdb_object");
311 LOG(info) <<
" `-> done!";
Definition of the ITSMFT compact cluster.
ClassImp(o2::itsmft::BuildTopologyDictionary)
Definition of the SegmentationSuperAlpide class.
Definition of the LookUp class for its3.
void saveDictionaryRoot(const std::string &fname)
void setThresholdCumulative(double cumulative)
void groupRareTopologies()
void setThreshold(double thr)
void printDictionary(const std::string &fname)
void setNCommon(unsigned int nCommon)
void accountTopology(const itsmft::ClusterTopology &cluster, float dX=IgnoreVal, float dZ=IgnoreVal)
void printDictionaryBinary(const std::string &fname)
static constexpr float IgnoreVal
static int groupFinder(int nRow, int nCol)
static constexpr int ColClassSpan
Column span of the classes of rare topologies.
int getSize() const
Returns the number of elements in the dicionary;.
static constexpr int MaxNumberOfRowClasses
Maximum number of row classes for the groups of rare topologies.
static constexpr int MaxNumberOfColClasses
Maximum number of col classes for the groups of rare topologies.
static constexpr int RowClassSpan
Row span of the classes of rare topologies.
void writeBinaryFile(const std::string &outputFile)
Prints the dictionary in a binary file.
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.
void setPattern(int nRow, int nCol, const unsigned char patt[MaxPatternBytes])
Sets the pattern.
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.
int getUsedBytes() const
Returns the number of bytes used for the pattern.
int getRowSpan() const
Returns the number of rows.
int getColumnSpan() const
Returns the number of columns.
const std::array< unsigned char, ClusterPattern::kExtendedPatternBytes > & getPattern() const
Returns the pattern.
unsigned long getHash() const
Returns the hashcode.
const ClusterPattern & getClusterPattern() const
static constexpr unsigned short InvalidPatternID
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
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
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"