41 auto& topoStat = mTopologyMap[cluster.
getHash()];
42 topoStat.countsTotal++;
43 if (topoStat.countsTotal == 1) {
44 topoStat.topology = cluster;
55 topoStat.countsWithBias = 1;
60 mMapInfo.insert(std::make_pair(cluster.
getHash(), topInf));
63 auto num = topoStat.countsWithBias++;
64 auto ind = mMapInfo.find(cluster.
getHash());
65 float tmpxMean = ind->second.mXmean;
66 float newxMean = ind->second.mXmean = ((tmpxMean)*
num + dX) / (
num + 1);
67 float tmpxSigma2 = ind->second.mXsigma2;
68 ind->second.mXsigma2 = (
num * tmpxSigma2 + (dX - tmpxMean) * (dX - newxMean)) / (
num + 1);
69 float tmpzMean = ind->second.mZmean;
70 float newzMean = ind->second.mZmean = ((tmpzMean)*
num + dZ) / (
num + 1);
71 float tmpzSigma2 = ind->second.mZsigma2;
72 ind->second.mZsigma2 = (
num * tmpzSigma2 + (dZ - tmpzMean) * (dZ - newzMean)) / (
num + 1);
79 mTopologyFrequency.clear();
80 for (
auto&& p : mTopologyMap) {
81 mTopologyFrequency.emplace_back(std::make_pair(p.second.countsTotal, p.first));
83 std::sort(mTopologyFrequency.begin(), mTopologyFrequency.end(),
84 [](
const std::pair<unsigned long, unsigned long>& couple1,
85 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
86 mNCommonTopologies = 0;
87 mDictionary.mCommonMap.clear();
88 mDictionary.mGroupMap.clear();
89 mFrequencyThreshold = thr;
90 for (
auto& q : mTopologyFrequency) {
91 if (((
double)q.first) / mTotClusters > thr) {
99 LOGP(warning,
"Redefining prob. threshould from {} to {} to be below InvalidPatternID (was {})", thr, mFrequencyThreshold, mNCommonTopologies);
110 mTopologyFrequency.clear();
111 for (
auto&& p : mTopologyMap) {
112 mTopologyFrequency.emplace_back(std::make_pair(p.second.countsTotal, p.first));
114 std::sort(mTopologyFrequency.begin(), mTopologyFrequency.end(),
115 [](
const std::pair<unsigned long, unsigned long>& couple1,
116 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
117 mNCommonTopologies = nCommon;
118 mDictionary.mCommonMap.clear();
119 mDictionary.mGroupMap.clear();
120 mFrequencyThreshold = ((double)mTopologyFrequency[mNCommonTopologies - 1].
first) / mTotClusters;
125 mTopologyFrequency.clear();
126 if (cumulative <= 0. || cumulative >= 1.) {
130 for (
auto&& p : mTopologyMap) {
131 mTopologyFrequency.emplace_back(std::make_pair(p.second.countsTotal, p.first));
133 std::sort(mTopologyFrequency.begin(), mTopologyFrequency.end(),
134 [](
const std::pair<unsigned long, unsigned long>& couple1,
135 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
136 mNCommonTopologies = 0;
137 mDictionary.mCommonMap.clear();
138 mDictionary.mGroupMap.clear();
139 for (
auto& q : mTopologyFrequency) {
140 totFreq += ((double)(q.first)) / mTotClusters;
141 if (totFreq < cumulative) {
142 mNCommonTopologies++;
144 totFreq -= ((double)(q.first)) / mTotClusters;
145 mNCommonTopologies--;
146 LOGP(warning,
"Redefining cumulative threshould from {} to {} to be below InvalidPatternID)", cumulative, totFreq);
152 mFrequencyThreshold = ((double)(mTopologyFrequency[--mNCommonTopologies].
first)) / mTotClusters;
153 while (std::fabs(((
double)mTopologyFrequency[mNCommonTopologies].
first) / mTotClusters - mFrequencyThreshold) < 1.e-15) {
154 mNCommonTopologies--;
156 mFrequencyThreshold = ((double)mTopologyFrequency[mNCommonTopologies++].
first) / mTotClusters;
161 std::cout <<
"Dictionary finalisation" << std::endl;
162 std::cout <<
"Number of clusters: " << mTotClusters << std::endl;
165 for (
int j = 0;
j < mNCommonTopologies;
j++) {
167 gr.
mHash = mTopologyFrequency[
j].second;
171 gr.
mErrX = std::sqrt(mMapInfo.find(gr.
mHash)->second.mXsigma2);
172 gr.
mErrZ = std::sqrt(mMapInfo.find(gr.
mHash)->second.mZsigma2);
173 gr.
mErr2X = mMapInfo.find(gr.
mHash)->second.mXsigma2;
174 gr.
mErr2Z = mMapInfo.find(gr.
mHash)->second.mZsigma2;
180 mDictionary.mVectorOfIDs.push_back(gr);
182 LOGP(warning,
"Limiting N unique topologies to {}, threshold freq. to {}, cumulative freq. to {} to be below InvalidPatternID",
j, gr.
mFrequency, totFreq);
183 mNCommonTopologies =
j;
191 std::unordered_map<int, std::pair<GroupStruct, unsigned long>> tmp_GroupMap;
203 gr.
mHash = (((
unsigned long)(grNum)) << 32) & 0xffffffff00000000;
210 gr.
mNpixels = rowBinEdge * colBinEdge;
215 dummyPattern[0] = (
unsigned char)rowBinEdge;
216 dummyPattern[1] = (
unsigned char)colBinEdge;
217 int nBits = rowBinEdge * colBinEdge;
218 int nBytes = nBits / 8;
219 for (
int iB = 2; iB < nBytes + 2; iB++) {
220 dummyPattern[iB] = (
unsigned char)255;
222 int residualBits = nBits % 8;
224 unsigned char tempChar = 0;
225 while (residualBits > 0) {
227 tempChar |= 1 << (7 - residualBits);
229 dummyPattern[nBytes + 2] = tempChar;
233 tmp_GroupMap[grNum] = std::make_pair(gr, 0);
241 for (
unsigned int j = (
unsigned int)mNCommonTopologies;
j < mTopologyFrequency.size();
j++) {
242 unsigned long hash1 = mTopologyFrequency[
j].second;
243 rs = mTopologyMap.find(hash1)->second.topology.getRowSpan();
244 cs = mTopologyMap.find(hash1)->second.topology.getColumnSpan();
246 tmp_GroupMap[
index].second += mTopologyFrequency[
j].first;
249 for (
auto&& p : tmp_GroupMap) {
252 mDictionary.mVectorOfIDs.push_back(
group);
256 std::sort(mDictionary.mVectorOfIDs.begin(), mDictionary.mVectorOfIDs.end(), [](
const GroupStruct&
a,
const GroupStruct&
b) {
257 return (!a.mIsGroup) && b.mIsGroup ? true : (a.mIsGroup && (!b.mIsGroup) ? false : (a.mFrequency > b.mFrequency));
264 std::sort(mDictionary.mVectorOfIDs.begin(), mDictionary.mVectorOfIDs.end(), [](
const GroupStruct&
a,
const GroupStruct&
b) { return a.mFrequency > b.mFrequency; });
266 for (
int iKey = 0; iKey < mDictionary.
getSize(); iKey++) {
269 mDictionary.mCommonMap.insert(std::make_pair(gr.
mHash, iKey));
274 mDictionary.mGroupMap.insert(std::make_pair((
int)(gr.
mHash >> 32) & 0x00000000ffffffff, iKey));
277 std::cout <<
"Dictionay finalised" << std::endl;
278 std::cout <<
"Number of keys: " << mDictionary.
getSize() << std::endl;
279 std::cout <<
"Number of common topologies: " << mDictionary.mCommonMap.size() << std::endl;
280 std::cout <<
"Number of groups of rare topologies: " << mDictionary.mGroupMap.size() << std::endl;
285 for (
int i = 0;
i < DB.mNCommonTopologies;
i++) {
286 const unsigned long& hash = DB.mTopologyFrequency[
i].second;
287 os <<
"Hash: " << hash << std::endl;
288 os <<
"counts: " << DB.mTopologyMap.find(hash)->second.countsTotal;
289 os <<
" (with bias provided: " << DB.mTopologyMap.find(hash)->second.countsWithBias <<
")" << std::endl;
290 os <<
"sigmaX: " << std::sqrt(DB.mMapInfo.find(hash)->second.mXsigma2) << std::endl;
291 os <<
"sigmaZ: " << std::sqrt(DB.mMapInfo.find(hash)->second.mZsigma2) << std::endl;
292 os << DB.mTopologyMap.find(hash)->second.topology;
299 std::cout <<
"Saving the the dictionary in binary format: ";
300 std::ofstream out(fname);
303 std::cout <<
"done!" << std::endl;
308 std::cout <<
"Printing the dictionary: ";
309 std::ofstream out(fname);
312 std::cout <<
"done!" << std::endl;
317 std::cout <<
"Saving the the dictionary in a ROOT file: ";
318 TFile
output(fname.c_str(),
"recreate");
319 output.WriteObjectAny(&mDictionary, mDictionary.Class(),
"ccdb_object");
321 std::cout <<
"done!" << std::endl;
Definition of the ITSMFT compact cluster.
Definition of the BuildTopologyDictionary class.
Definition of the LookUp class.
ClassImp(o2::itsmft::BuildTopologyDictionary)
Definition of the SegmentationAlpide class.
static constexpr float IgnoreVal
BuildTopologyDictionary()
void accountTopology(const ClusterTopology &cluster, float dX=IgnoreVal, float dZ=IgnoreVal)
void setNCommon(unsigned int nCommon)
void groupRareTopologies()
void setThreshold(double thr)
void printDictionaryBinary(const std::string &fname)
void setThresholdCumulative(double cumulative)
void printDictionary(const std::string &fname)
void saveDictionaryRoot(const std::string &fname)
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...
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
static int groupFinder(int nRow, int nCol)
static constexpr float PitchCol
static constexpr float PitchRow
static constexpr int ColClassSpan
Column span of the classes of rare topologies.
int getSize() const
Returns the number of elements in the dicionary;.
void writeBinaryFile(const std::string &outputFile)
Prints the dictionary in a binary file.
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.
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
std::ostream & operator<<(std::ostream &os, const ClusterPattern &pattern)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
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