29 accountTopologyImpl(cluster,
30 ((IB) ? mMapInfoIB : mMapInfoOB),
31 ((IB) ? mTopologyMapIB : mTopologyMapOB),
32 ((IB) ? mTotClustersIB : mTotClustersOB),
38void BuildTopologyDictionary::accountTopologyImpl(
const itsmft::ClusterTopology& cluster, TopoInfo& tinfo, TopoStat& tstat,
unsigned int& tot,
float sigmaX,
float sigmaZ,
float dX,
float dZ)
45 auto& topoStat = tstat[cluster.
getHash()];
46 topoStat.countsTotal++;
47 if (topoStat.countsTotal == 1) {
48 topoStat.topology = cluster;
59 topoStat.countsWithBias = 1;
61 topInf.
mXsigma2 = sigmaX * sigmaX / 12.f / (float)std::min(10, topInf.
mSizeX);
62 topInf.
mZsigma2 = sigmaZ * sigmaZ / (float)std::min(10, topInf.
mSizeZ);
64 tinfo.emplace(cluster.
getHash(), topInf);
67 auto num = topoStat.countsWithBias++;
68 auto ind = tinfo.find(cluster.
getHash());
69 float tmpxMean = ind->second.mXmean;
70 float newxMean = ind->second.mXmean = ((tmpxMean)*
num + dX) / (
num + 1);
71 float tmpxSigma2 = ind->second.mXsigma2;
72 ind->second.mXsigma2 = (
num * tmpxSigma2 + (dX - tmpxMean) * (dX - newxMean)) / (
num + 1);
73 float tmpzMean = ind->second.mZmean;
74 float newzMean = ind->second.mZmean = ((tmpzMean)*
num + dZ) / (
num + 1);
75 float tmpzSigma2 = ind->second.mZsigma2;
76 ind->second.mZsigma2 = (
num * tmpzSigma2 + (dZ - tmpzMean) * (dZ - newzMean)) / (
num + 1);
85 auto& freqTopo = ((IB) ? mTopologyFrequencyIB : mTopologyFrequencyOB);
86 auto& freqThres = ((IB) ? mFrequencyThresholdIB : mFrequencyThresholdOB);
87 auto& comTopo = ((IB) ? mNCommonTopologiesIB : mNCommonTopologiesOB);
88 auto ntot = ((IB) ? mTotClustersIB : mTotClustersOB);
90 setNCommonImpl(nCommon,
92 ((IB) ? mTopologyMapIB : mTopologyMapOB),
96 freqThres = ((double)freqTopo[comTopo - 1].
first) / ntot;
99void BuildTopologyDictionary::setNCommonImpl(
unsigned int ncom, TopoFreq& tfreq, TopoStat& tstat,
unsigned int& ncommon,
unsigned int ntot)
106 for (
auto&& p : tstat) {
107 tfreq.emplace_back(p.second.countsTotal, p.first);
109 std::sort(tfreq.begin(), tfreq.end(),
110 [](
const std::pair<unsigned long, unsigned long>& couple1,
111 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
118 setThresholdImpl(thr,
119 ((IB) ? mTopologyFrequencyIB : mTopologyFrequencyOB),
120 ((IB) ? mMapInfoIB : mMapInfoOB),
121 ((IB) ? mTopologyMapIB : mTopologyMapOB),
122 ((IB) ? mNCommonTopologiesIB : mNCommonTopologiesOB),
123 ((IB) ? mFrequencyThresholdIB : mFrequencyThresholdOB),
124 ((IB) ? mTotClustersIB : mTotClustersOB));
127void BuildTopologyDictionary::setThresholdImpl(
double thr, TopoFreq& tfreq, TopoInfo& tinfo, TopoStat& tstat,
unsigned int& ncommon,
double& freqthres,
unsigned int ntot)
129 setNCommonImpl(0, tfreq, tstat, ncommon, ntot);
131 for (
auto& q : tfreq) {
132 if (((
double)q.first) / ntot > thr) {
140 LOGP(warning,
"Redefining prob. threshold from {} to {} to be below InvalidPatternID (was {})", thr, freqthres, ntot);
147 if (cumulative <= 0. || cumulative >= 1.) {
151 auto& freqTopo = ((IB) ? mTopologyFrequencyIB : mTopologyFrequencyOB);
152 auto& freqThres = ((IB) ? mFrequencyThresholdIB : mFrequencyThresholdOB);
153 auto& statTopo = ((IB) ? mTopologyMapIB : mTopologyMapOB);
154 auto& comTopo = ((IB) ? mNCommonTopologiesIB : mNCommonTopologiesOB);
155 auto ntot = ((IB) ? mTotClustersIB : mTotClustersOB);
158 setNCommonImpl(0, freqTopo, statTopo, comTopo, ntot);
159 setThresholdCumulativeImpl(cumulative, freqTopo, comTopo, freqThres, ntot);
162void BuildTopologyDictionary::setThresholdCumulativeImpl(
double cumulative, TopoFreq& tfreq,
unsigned int& ncommon,
double& freqthres,
unsigned int ntot)
165 for (
auto& q : tfreq) {
166 totFreq += ((double)(q.first)) / ntot;
167 if (totFreq < cumulative) {
170 totFreq -= ((double)(q.first)) / ntot;
172 LOGP(warning,
"Redefining cumulative threshould from {} to {} to be below InvalidPatternID)", cumulative, totFreq);
178 freqthres = ((double)(tfreq[--ncommon].
first)) / ntot;
179 while (std::fabs(((
double)tfreq[ncommon--].
first) / ntot - freqthres) < 1.e-15) {
181 freqthres = ((double)tfreq[ncommon++].
first) / ntot;
186 LOG(info) <<
"Dictionary finalisation";
187 LOG(info) <<
"Number of IB clusters: " << mTotClustersIB;
188 LOG(info) <<
"Number of OB clusters: " << mTotClustersOB;
190 groupRareTopologiesImpl(mTopologyFrequencyIB, mMapInfoIB, mTopologyMapIB, mNCommonTopologiesIB, mFrequencyThresholdIB, mDictionary.mDataIB, mNCommonTopologiesIB);
191 groupRareTopologiesImpl(mTopologyFrequencyOB, mMapInfoOB, mTopologyMapOB, mNCommonTopologiesOB, mFrequencyThresholdOB, mDictionary.mDataOB, mNCommonTopologiesOB);
193 LOG(info) <<
"Dictionay finalised";
195 mDictionary.mDataIB.
print();
197 mDictionary.mDataOB.
print();
200void BuildTopologyDictionary::groupRareTopologiesImpl(TopoFreq& tfreq, TopoInfo& tinfo, TopoStat& tstat,
unsigned int& ncommon,
double& freqthres,
TopologyDictionaryData&
data,
unsigned int ntot)
203 for (
unsigned int j = 0;
j < ncommon;
j++) {
205 gr.
mHash = tfreq[
j].second;
209 const auto& topo = tinfo.find(gr.
mHash)->second;
210 gr.
mErrX = std::sqrt(topo.mXsigma2);
211 gr.
mErrZ = std::sqrt(topo.mZsigma2);
212 gr.
mErr2X = topo.mXsigma2;
213 gr.
mErr2Z = topo.mZsigma2;
214 gr.
mXCOG = -1 * topo.mCOGx;
215 gr.
mZCOG = topo.mCOGz;
219 data.mVectorOfIDs.push_back(gr);
221 LOGP(warning,
"Limiting N unique topologies to {}, threshold freq. to {}, cumulative freq. to {} to be below InvalidPatternID",
j, gr.
mFrequency, totFreq);
230 std::unordered_map<int, std::pair<itsmft::GroupStruct, unsigned long>> tmp_GroupMap;
241 itsmft::GroupStruct gr;
242 gr.
mHash = (((
unsigned long)(grNum)) << 32) & 0xffffffff00000000;
245 gr.mErr2X = gr.mErrX * gr.mErrX;
246 gr.mErr2Z = gr.mErrZ * gr.mErrZ;
249 gr.mNpixels = rowBinEdge * colBinEdge;
254 dummyPattern[0] = (
unsigned char)rowBinEdge;
255 dummyPattern[1] = (
unsigned char)colBinEdge;
256 int nBits = rowBinEdge * colBinEdge;
257 int nBytes = nBits / 8;
258 for (
int iB = 2; iB < nBytes + 2; iB++) {
259 dummyPattern[iB] = (
unsigned char)255;
261 int residualBits = nBits % 8;
262 if (residualBits != 0) {
263 unsigned char tempChar = 0;
264 while (residualBits > 0) {
266 tempChar |= 1 << (7 - residualBits);
268 dummyPattern[nBytes + 2] = tempChar;
270 gr.mPattern.setPattern(dummyPattern);
272 tmp_GroupMap[grNum] = std::make_pair(gr, 0);
275 int rs{}, cs{},
index{};
278 for (
auto j{ncommon};
j < tfreq.size();
j++) {
279 unsigned long hash1 = tfreq[
j].second;
280 rs = tstat.find(hash1)->second.topology.getRowSpan();
281 cs = tstat.find(hash1)->second.topology.getColumnSpan();
283 tmp_GroupMap[
index].second += tfreq[
j].first;
286 for (
auto&& p : tmp_GroupMap) {
287 itsmft::GroupStruct&
group = p.second.first;
288 group.mFrequency = ((double)p.second.second) / ntot;
293 std::sort(
data.mVectorOfIDs.begin(),
data.mVectorOfIDs.end(), [](
const itsmft::GroupStruct&
a,
const itsmft::GroupStruct&
b) {
294 return (!a.mIsGroup) && b.mIsGroup ? true : (a.mIsGroup && (!b.mIsGroup) ? false : (a.mFrequency > b.mFrequency));
301 std::sort(
data.mVectorOfIDs.begin(),
data.mVectorOfIDs.end(), [](
const itsmft::GroupStruct&
a,
const itsmft::GroupStruct&
b) { return a.mFrequency > b.mFrequency; });
303 for (
int iKey = 0; iKey <
data.mVectorOfIDs.size(); iKey++) {
304 itsmft::GroupStruct& gr =
data.mVectorOfIDs[iKey];
306 data.mCommonMap.emplace(gr.mHash, iKey);
307 if (gr.mPattern.getUsedBytes() == 1) {
308 data.mSmallTopologiesLUT[(gr.mPattern.getColumnSpan() - 1) * 255 + (
int)gr.mPattern.getByte(2)] = iKey;
311 data.mGroupMap.emplace((
int)(gr.mHash >> 32) & 0x00000000ffffffff, iKey);
318 os <<
"--- InnerBarrel\n";
319 for (
unsigned int i = 0;
i < DB.mNCommonTopologiesIB;
i++) {
320 const unsigned long& hash = DB.mTopologyFrequencyIB[
i].second;
321 os <<
"Hash: " << hash <<
'\n';
322 os <<
"counts: " << DB.mTopologyMapIB.find(hash)->second.countsTotal;
323 os <<
" (with bias provided: " << DB.mTopologyMapIB.find(hash)->second.countsWithBias <<
")" <<
'\n';
324 os <<
"sigmaX: " << std::sqrt(DB.mMapInfoIB.find(hash)->second.mXsigma2) <<
'\n';
325 os <<
"sigmaZ: " << std::sqrt(DB.mMapInfoIB.find(hash)->second.mZsigma2) <<
'\n';
326 os << DB.mTopologyMapIB.find(hash)->second.topology;
328 os <<
"--- OuterBarrel\n";
329 for (
unsigned int i = 0;
i < DB.mNCommonTopologiesOB;
i++) {
330 const unsigned long& hash = DB.mTopologyFrequencyOB[
i].second;
331 os <<
"Hash: " << hash <<
'\n';
332 os <<
"counts: " << DB.mTopologyMapOB.find(hash)->second.countsTotal;
333 os <<
" (with bias provided: " << DB.mTopologyMapOB.find(hash)->second.countsWithBias <<
")" <<
'\n';
334 os <<
"sigmaX: " << std::sqrt(DB.mMapInfoOB.find(hash)->second.mXsigma2) <<
'\n';
335 os <<
"sigmaZ: " << std::sqrt(DB.mMapInfoOB.find(hash)->second.mZsigma2) <<
'\n';
336 os << DB.mTopologyMapOB.find(hash)->second.topology;
343 LOG(info) <<
"Saving the the dictionary in binary format: ";
344 std::ofstream out(fname);
347 LOG(info) <<
" `-> done!";
352 LOG(info) <<
"Printing the dictionary: ";
353 std::ofstream out(fname);
356 LOG(info) <<
" `-> done!";
361 LOG(info) <<
"Saving the the dictionary in a ROOT file: " << fname;
362 TFile
output(fname.c_str(),
"recreate");
363 output.WriteObjectAny(&mDictionary, mDictionary.Class(),
"ccdb_object");
365 LOG(info) <<
" `-> done!";
Definition of the ITSMFT compact cluster.
ClassImp(o2::itsmft::BuildTopologyDictionary)
Definition of the SegmentationAlpide class.
Definition of the SegmentationMosaix class.
Definition of the LookUp class for its3.
void saveDictionaryRoot(const std::string &fname)
void setNCommon(unsigned int nCommon, bool IB)
void setThreshold(double thr, bool IB)
void setThresholdCumulative(double cumulative, bool IB)
void groupRareTopologies()
void accountTopology(const itsmft::ClusterTopology &cluster, bool IB, float dX=IgnoreVal, float dZ=IgnoreVal)
void printDictionary(const std::string &fname)
void printDictionaryBinary(const std::string &fname)
static constexpr float IgnoreVal
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.
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.
void resetMaps(bool IB=true) noexcept
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 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 constexpr float PitchCol
static constexpr float PitchRow
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
std::ostream & operator<<(std::ostream &os, const BuildTopologyDictionary &DB)
void print() const noexcept
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"