Project
Loading...
Searching...
No Matches
ClusterTopology.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
16
18#include "Framework/Logger.h"
19#include <iostream>
20#include <TMath.h>
21
23
24namespace o2
25{
26namespace itsmft
27{
28ClusterTopology::ClusterTopology() : mPattern{}, mHash{0} {}
29
30ClusterTopology::ClusterTopology(int nRow, int nCol, const unsigned char patt[ClusterPattern::MaxPatternBytes]) : mHash{0}
31{
32 setPattern(nRow, nCol, patt);
33}
34
39
40void ClusterTopology::setPattern(int nRow, int nCol, const unsigned char patt[ClusterPattern::MaxPatternBytes])
41{
42 mPattern.setPattern(nRow, nCol, patt);
43 mHash = getCompleteHash(*this);
44}
45
47{
48 mPattern = patt;
49 mHash = getCompleteHash(*this);
50}
51
52unsigned int ClusterTopology::hashFunction(const void* key, int len)
53{
54 //
55 // Developed from https://github.com/rurban/smhasher , function MurMur2
56 //
57 // 'm' and 'r' are mixing constants generated offline.
58 const unsigned int m = 0x5bd1e995;
59 const int r = 24;
60 // Initialize the hash
61 unsigned int h = len ^ 0xdeadbeef;
62 // Mix 4 bytes at a time into the hash
63 const unsigned char* data = (const unsigned char*)key;
64 // int recIndex=0;
65 while (len >= 4) {
66 unsigned int k = *(unsigned int*)data;
67 k *= m;
68 k ^= k >> r;
69 k *= m;
70 h *= m;
71 h ^= k;
72 data += 4;
73 len -= 4;
74 }
75 // Handle the last few bytes of the input array
76 // ATTENTION: DO NOT INSERT BREAK after case, its absence is intended!!!
77 switch (len) {
78 case 3:
79 h ^= data[2] << 16;
80 case 2:
81 h ^= data[1] << 8;
82 case 1:
83 h ^= data[0];
84 h *= m;
85 };
86 // Do a few final mixes of the hash to ensure the last few
87 // bytes are well-incorporated.
88 h ^= h >> 13;
89 h *= m;
90 h ^= h >> 15;
91 return h;
92}
93
94unsigned long ClusterTopology::getCompleteHash(int nRow, int nCol,
95 const unsigned char patt[ClusterPattern::MaxPatternBytes])
96{
97 unsigned char extended_pattern[ClusterPattern::kExtendedPatternBytes] = {0};
98 extended_pattern[0] = (unsigned char)nRow;
99 extended_pattern[1] = (unsigned char)nCol;
100 int nBits = nRow * nCol;
101 int nBytes = nBits / 8;
102 if (nBits % 8 != 0) {
103 nBytes++;
104 }
105 memcpy(&extended_pattern[2], patt, nBytes);
106
107 unsigned long partialHash = (unsigned long)hashFunction(extended_pattern, nBytes);
108 // The first four bytes are directly taken from partialHash
109 unsigned long completeHash = partialHash << 32;
110 // The last four bytes of the hash are the first 32 pixels of the topology.
111 // The bits reserved for the pattern that are not used are set to 0.
112 if (nBytes == 1) {
113 completeHash += ((((unsigned long)extended_pattern[2]) << 24));
114 } else if (nBytes == 2) {
115 completeHash += ((((unsigned long)extended_pattern[2]) << 24) + (((unsigned long)extended_pattern[3]) << 16));
116 } else if (nBytes == 3) {
117 completeHash += ((((unsigned long)extended_pattern[2]) << 24) + (((unsigned long)extended_pattern[3]) << 16) +
118 (((unsigned long)extended_pattern[4]) << 8));
119 } else if (nBytes >= 4) {
120 completeHash += ((((unsigned long)extended_pattern[2]) << 24) + (((unsigned long)extended_pattern[3]) << 16) +
121 (((unsigned long)extended_pattern[4]) << 8) + ((unsigned long)extended_pattern[5]));
122 } else {
123 LOG(error) << "No fired pixels in small topology";
124 throw std::runtime_error("No fired pixels in small topology");
125 }
126 return completeHash;
127}
128
130{
131 const auto& patt = topology.getPattern();
132 int nBytesUsed = topology.getUsedBytes();
133 unsigned long partialHash = (unsigned long)hashFunction(patt.data(), nBytesUsed);
134 // The first four bytes are directly taken from partialHash
135 unsigned long completeHash = partialHash << 32;
136 // The last four bytes of the hash are the first 32 pixels of the topology.
137 // The bits reserved for the pattern that are not used are set to 0.
138 if (nBytesUsed == 1) {
139 completeHash += ((((unsigned long)patt[2]) << 24));
140 } else if (nBytesUsed == 2) {
141 completeHash += ((((unsigned long)patt[2]) << 24) + (((unsigned long)patt[3]) << 16));
142 } else if (nBytesUsed == 3) {
143 completeHash +=
144 ((((unsigned long)patt[2]) << 24) + (((unsigned long)patt[3]) << 16) + (((unsigned long)patt[4]) << 8));
145 } else if (nBytesUsed >= 4) {
146 completeHash += ((((unsigned long)patt[2]) << 24) + (((unsigned long)patt[3]) << 16) +
147 (((unsigned long)patt[4]) << 8) + ((unsigned long)patt[5]));
148 } else {
149 LOG(error) << "No fired pixels in small topology";
150 throw std::runtime_error("No fired pixels in small topology");
151 }
152 return completeHash;
153}
154
156{
157 std::cout << (*this) << "\n";
158}
159
160std::ostream& operator<<(std::ostream& os, const ClusterTopology& topology)
161{
162 os << topology.mPattern << std::endl;
163 return os;
164}
165
166} // namespace itsmft
167} // namespace o2
ClassImp(o2::itsmft::ClusterTopology)
Definition of the ClusterTopology class.
StringRef key
Class for time synchronization of RawReader instances.
static constexpr int MaxPatternBytes
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...
const std::array< unsigned char, ClusterPattern::kExtendedPatternBytes > & getPattern() const
Returns the pattern.
int getUsedBytes() const
Returns the number of used bytes.
void setPattern(int nRow, int nCol, const unsigned char patt[ClusterPattern::MaxPatternBytes])
Sets the pattern.
static unsigned int hashFunction(const void *key, int len)
MurMur2 hash fucntion.
void print() const
Prints to the stdout.
static unsigned long getCompleteHash(int nRow, int nCol, const unsigned char patt[ClusterPattern::MaxPatternBytes])
Compute the complete hash as defined for mHash.
ClusterTopology()
Default constructor.
const GLfloat * m
Definition glcorearb.h:4066
GLboolean * data
Definition glcorearb.h:298
GLboolean r
Definition glcorearb.h:1233
GLenum GLenum GLsizei len
Definition glcorearb.h:4232
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 ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"