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)
43 auto& topoStat = tstat[cluster.
getHash()];
44 topoStat.countsTotal++;
45 if (topoStat.countsTotal == 1) {
46 topoStat.topology = cluster;
57 topoStat.countsWithBias = 1;
59 topInf.
mXsigma2 = sigmaX * sigmaX / 12.f / (float)std::min(10, topInf.
mSizeX);
60 topInf.
mZsigma2 = sigmaZ * sigmaZ / 12.f / (float)std::min(10, topInf.
mSizeZ);
62 tinfo.emplace(cluster.
getHash(), topInf);
65 auto num = topoStat.countsWithBias++;
66 auto ind = tinfo.find(cluster.
getHash());
67 float tmpxMean = ind->second.mXmean;
68 float newxMean = ind->second.mXmean = ((tmpxMean)*
num + dX) / (
num + 1);
69 float tmpxSigma2 = ind->second.mXsigma2;
70 ind->second.mXsigma2 = (
num * tmpxSigma2 + (dX - tmpxMean) * (dX - newxMean)) / (
num + 1);
71 float tmpzMean = ind->second.mZmean;
72 float newzMean = ind->second.mZmean = ((tmpzMean)*
num + dZ) / (
num + 1);
73 float tmpzSigma2 = ind->second.mZsigma2;
74 ind->second.mZsigma2 = (
num * tmpzSigma2 + (dZ - tmpzMean) * (dZ - newzMean)) / (
num + 1);
83 auto& freqTopo = ((IB) ? mTopologyFrequencyIB : mTopologyFrequencyOB);
84 auto& freqThres = ((IB) ? mFrequencyThresholdIB : mFrequencyThresholdOB);
85 auto& comTopo = ((IB) ? mNCommonTopologiesIB : mNCommonTopologiesOB);
86 auto ntot = ((IB) ? mTotClustersIB : mTotClustersOB);
88 setNCommonImpl(nCommon,
90 ((IB) ? mTopologyMapIB : mTopologyMapOB),
94 freqThres = ((double)freqTopo[comTopo - 1].
first) / ntot;
97void BuildTopologyDictionary::setNCommonImpl(
unsigned int ncom, TopoFreq& tfreq, TopoStat& tstat,
unsigned int& ncommon,
unsigned int ntot)
104 for (
auto&& p : tstat) {
105 tfreq.emplace_back(p.second.countsTotal, p.first);
107 std::sort(tfreq.begin(), tfreq.end(),
108 [](
const std::pair<unsigned long, unsigned long>& couple1,
109 const std::pair<unsigned long, unsigned long>& couple2) { return (couple1.first > couple2.first); });
116 setThresholdImpl(thr,
117 ((IB) ? mTopologyFrequencyIB : mTopologyFrequencyOB),
118 ((IB) ? mMapInfoIB : mMapInfoOB),
119 ((IB) ? mTopologyMapIB : mTopologyMapOB),
120 ((IB) ? mNCommonTopologiesIB : mNCommonTopologiesOB),
121 ((IB) ? mFrequencyThresholdIB : mFrequencyThresholdOB),
122 ((IB) ? mTotClustersIB : mTotClustersOB));
125void BuildTopologyDictionary::setThresholdImpl(
double thr, TopoFreq& tfreq, TopoInfo& tinfo, TopoStat& tstat,
unsigned int& ncommon,
double& freqthres,
unsigned int ntot)
127 setNCommonImpl(0, tfreq, tstat, ncommon, ntot);
129 for (
const auto& q : tfreq) {
130 if (((
double)q.first) / ntot > thr) {
138 LOGP(warning,
"Redefining prob. threshold from {} to {} to be below InvalidPatternID (was {})", thr, freqthres, ntot);
145 if (cumulative <= 0. || cumulative >= 1.) {
149 auto& freqTopo = ((IB) ? mTopologyFrequencyIB : mTopologyFrequencyOB);
150 auto& freqThres = ((IB) ? mFrequencyThresholdIB : mFrequencyThresholdOB);
151 auto& statTopo = ((IB) ? mTopologyMapIB : mTopologyMapOB);
152 auto& comTopo = ((IB) ? mNCommonTopologiesIB : mNCommonTopologiesOB);
153 auto ntot = ((IB) ? mTotClustersIB : mTotClustersOB);
156 setNCommonImpl(0, freqTopo, statTopo, comTopo, ntot);
157 setThresholdCumulativeImpl(cumulative, freqTopo, comTopo, freqThres, ntot);
160void BuildTopologyDictionary::setThresholdCumulativeImpl(
double cumulative, TopoFreq& tfreq,
unsigned int& ncommon,
double& freqthres,
unsigned int ntot)
163 for (
auto& q : tfreq) {
164 totFreq += ((double)(q.first)) / ntot;
165 if (totFreq < cumulative) {
168 totFreq -= ((double)(q.first)) / ntot;
170 LOGP(warning,
"Redefining cumulative threshould from {} to {} to be below InvalidPatternID)", cumulative, totFreq);
176 freqthres = ((double)(tfreq[--ncommon].
first)) / ntot;
177 while (std::fabs(((
double)tfreq[ncommon--].
first) / ntot - freqthres) < 1.e-15) {
179 freqthres = ((double)tfreq[ncommon++].
first) / ntot;
184 LOG(info) <<
"Dictionary finalisation";
185 LOG(info) <<
"Number of IB clusters: " << mTotClustersIB;
186 LOG(info) <<
"Number of OB clusters: " << mTotClustersOB;
188 groupRareTopologiesImpl(mTopologyFrequencyIB, mMapInfoIB, mTopologyMapIB, mNCommonTopologiesIB, mFrequencyThresholdIB, mDictionary.mDataIB, mNCommonTopologiesIB);
189 groupRareTopologiesImpl(mTopologyFrequencyOB, mMapInfoOB, mTopologyMapOB, mNCommonTopologiesOB, mFrequencyThresholdOB, mDictionary.mDataOB, mNCommonTopologiesOB);
191 LOG(info) <<
"Dictionay finalised";
193 mDictionary.mDataIB.
print();
195 mDictionary.mDataOB.
print();
198void BuildTopologyDictionary::groupRareTopologiesImpl(TopoFreq& tfreq, TopoInfo& tinfo, TopoStat& tstat,
unsigned int& ncommon,
double& freqthres,
TopologyDictionaryData&
data,
unsigned int ntot)
201 for (
unsigned int j = 0;
j < ncommon;
j++) {
203 gr.
mHash = tfreq[
j].second;
207 const auto& topo = tinfo.find(gr.
mHash)->second;
208 gr.
mErrX = std::sqrt(topo.mXsigma2);
209 gr.
mErrZ = std::sqrt(topo.mZsigma2);
210 gr.
mErr2X = topo.mXsigma2;
211 gr.
mErr2Z = topo.mZsigma2;
212 gr.
mXCOG = -1 * topo.mCOGx;
213 gr.
mZCOG = topo.mCOGz;
217 data.mVectorOfIDs.push_back(gr);
219 LOGP(warning,
"Limiting N unique topologies to {}, threshold freq. to {}, cumulative freq. to {} to be below InvalidPatternID",
j, gr.
mFrequency, totFreq);
228 std::unordered_map<int, std::pair<itsmft::GroupStruct, unsigned long>> tmp_GroupMap;
239 itsmft::GroupStruct gr;
240 gr.
mHash = (((
unsigned long)(grNum)) << 32) & 0xffffffff00000000;
243 gr.mErr2X = gr.mErrX * gr.mErrX;
244 gr.mErr2Z = gr.mErrZ * gr.mErrZ;
247 gr.mNpixels = rowBinEdge * colBinEdge;
252 dummyPattern[0] = (
unsigned char)rowBinEdge;
253 dummyPattern[1] = (
unsigned char)colBinEdge;
254 int nBits = rowBinEdge * colBinEdge;
255 int nBytes = nBits / 8;
256 for (
int iB = 2; iB < nBytes + 2; iB++) {
257 dummyPattern[iB] = (
unsigned char)255;
259 int residualBits = nBits % 8;
260 if (residualBits != 0) {
261 unsigned char tempChar = 0;
262 while (residualBits > 0) {
264 tempChar |= 1 << (7 - residualBits);
266 dummyPattern[nBytes + 2] = tempChar;
268 gr.mPattern.setPattern(dummyPattern);
270 tmp_GroupMap[grNum] = std::make_pair(gr, 0);
273 int rs{}, cs{},
index{};
276 for (
auto j{ncommon};
j < tfreq.size();
j++) {
277 unsigned long hash1 = tfreq[
j].second;
278 rs = tstat.find(hash1)->second.topology.getRowSpan();
279 cs = tstat.find(hash1)->second.topology.getColumnSpan();
281 tmp_GroupMap[
index].second += tfreq[
j].first;
284 for (
auto&& p : tmp_GroupMap) {
285 itsmft::GroupStruct&
group = p.second.first;
286 group.mFrequency = ((double)p.second.second) / ntot;
291 std::sort(
data.mVectorOfIDs.begin(),
data.mVectorOfIDs.end(), [](
const itsmft::GroupStruct&
a,
const itsmft::GroupStruct&
b) {
292 return (!a.mIsGroup) && b.mIsGroup ? true : (a.mIsGroup && (!b.mIsGroup) ? false : (a.mFrequency > b.mFrequency));
299 std::sort(
data.mVectorOfIDs.begin(),
data.mVectorOfIDs.end(), [](
const itsmft::GroupStruct&
a,
const itsmft::GroupStruct&
b) { return a.mFrequency > b.mFrequency; });
301 for (
int iKey = 0; iKey <
data.mVectorOfIDs.size(); iKey++) {
302 itsmft::GroupStruct& gr =
data.mVectorOfIDs[iKey];
304 data.mCommonMap.emplace(gr.mHash, iKey);
305 if (gr.mPattern.getUsedBytes() == 1) {
306 data.mSmallTopologiesLUT[(gr.mPattern.getColumnSpan() - 1) * 255 + (
int)gr.mPattern.getByte(2)] = iKey;
309 data.mGroupMap.emplace((
int)(gr.mHash >> 32) & 0x00000000ffffffff, iKey);
316 os <<
"--- InnerBarrel\n";
317 for (
unsigned int i = 0;
i < DB.mNCommonTopologiesIB;
i++) {
318 const unsigned long& hash = DB.mTopologyFrequencyIB[
i].second;
319 os <<
"Hash: " << hash <<
'\n';
320 os <<
"counts: " << DB.mTopologyMapIB.find(hash)->second.countsTotal;
321 os <<
" (with bias provided: " << DB.mTopologyMapIB.find(hash)->second.countsWithBias <<
")" <<
'\n';
322 os <<
"sigmaX: " << std::sqrt(DB.mMapInfoIB.find(hash)->second.mXsigma2) <<
'\n';
323 os <<
"sigmaZ: " << std::sqrt(DB.mMapInfoIB.find(hash)->second.mZsigma2) <<
'\n';
324 os << DB.mTopologyMapIB.find(hash)->second.topology;
326 os <<
"--- OuterBarrel\n";
327 for (
unsigned int i = 0;
i < DB.mNCommonTopologiesOB;
i++) {
328 const unsigned long& hash = DB.mTopologyFrequencyOB[
i].second;
329 os <<
"Hash: " << hash <<
'\n';
330 os <<
"counts: " << DB.mTopologyMapOB.find(hash)->second.countsTotal;
331 os <<
" (with bias provided: " << DB.mTopologyMapOB.find(hash)->second.countsWithBias <<
")" <<
'\n';
332 os <<
"sigmaX: " << std::sqrt(DB.mMapInfoOB.find(hash)->second.mXsigma2) <<
'\n';
333 os <<
"sigmaZ: " << std::sqrt(DB.mMapInfoOB.find(hash)->second.mZsigma2) <<
'\n';
334 os << DB.mTopologyMapOB.find(hash)->second.topology;
341 LOG(info) <<
"Saving the the dictionary in binary format: ";
342 std::ofstream out(fname);
345 LOG(info) <<
" `-> done!";
350 LOG(info) <<
"Printing the dictionary: ";
351 std::ofstream out(fname);
354 LOG(info) <<
" `-> done!";
359 LOG(info) <<
"Saving the the dictionary in a ROOT file: " << fname;
360 TFile
output(fname.c_str(),
"recreate");
361 output.WriteObjectAny(&mDictionary, mDictionary.Class(),
"ccdb_object");
363 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"