Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:17:55

0001 #ifndef G4EVAL_DSTCOMPRESSOR_H
0002 #define G4EVAL_DSTCOMPRESSOR_H
0003 
0004 #include "RtypesCore.h"
0005 #include "compressor_generator.h"
0006 
0007 #include <bitset>
0008 #include <iostream>
0009 
0010 class DSTCompressor
0011 {
0012  public:
0013   // whether to print the debug info
0014   const bool debug = false;
0015   // exit prematurely after this much residuals
0016   const int earlybreak = 1000 * 1000 * 1000;
0017 
0018   const float dist_phi = 0.05;
0019   const float dist_z = 3;
0020   int phiBits;
0021   int zBits;
0022 
0023   using bit5 = std::bitset<5>;
0024   using bit8 = std::bitset<8>;
0025   using bit10 = std::bitset<10>;
0026   using bstream = std::vector<u_char>;
0027   using Dict = std::vector<float>;
0028 
0029   const int wordwidth = 8;  // a char has 8 bits
0030   // phi mean, phi sigma, z mean, z sigma
0031   using Pars = std::array<double, 4>;
0032 
0033   Dict phiDict;
0034   Dict zDict;
0035 
0036   DSTCompressor(float phiMean = -3e-5,
0037                 float phiSigma = 0.015,
0038                 float zMean = -0.00063,
0039                 float zSigma = 0.07036,
0040                 int nBitPhi = 8,
0041                 int nBitZ = 8)
0042     : phiBits(nBitPhi)
0043     , zBits(nBitZ)
0044   {
0045     int numPoints = 1000000;
0046     phiDict = compress_gaussian_dist(phiMean, phiSigma, numPoints, phiBits);
0047     zDict = compress_gaussian_dist(zMean, zSigma, numPoints, zBits);
0048   }
0049 
0050   /** Generate the lookup table with a Gaussian model
0051    */
0052   Dict compress_gaussian_dist(double mean, double stddev,
0053                               int numPoints, int numBits)
0054   {
0055     std::vector<ushort> order;
0056     std::vector<float> dict;
0057     std::vector<unsigned long> cnt;
0058     std::default_random_engine generator;
0059     std::normal_distribution<double> distribution(mean, stddev);
0060     float maxAbsError = approx(&order, &dict, &cnt, numPoints, generator,
0061                                distribution, (size_t) pow(2, numBits));
0062     std::cout << "Compressing with " << numBits << " bits" << std::endl;
0063     std::cout << "Number of clusters = " << dict.size() << std::endl;
0064     std::cout << "Maximum absolute error = " << maxAbsError << std::endl;
0065     return dict;
0066   }
0067 
0068   unsigned short compressPhi(float inPhi)
0069   {
0070     return residesIn(inPhi, &phiDict);
0071   };
0072 
0073   unsigned short compressZ(float inZ)
0074   {
0075     return residesIn(inZ, &zDict);
0076   };
0077 
0078   float decompressPhi(unsigned short key)
0079   {
0080     return phiDict.at(key);
0081   }
0082 
0083   float decompressZ(unsigned short key)
0084   {
0085     return zDict.at(key);
0086   }
0087 };
0088 
0089 #endif  // G4EVAL_DSTCOMPRESSOR_H