Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:36

0001 /**
0002  * Using 16-bit short integers to store 32-bit floating-point values.
0003  * Joint work with Massachusetts Institute of Technology (MIT) and Institute for Information Industry (III)
0004  * Questions please contact fishyu@iii.org.tw
0005  * Date: May 22, 2021
0006  */
0007 
0008 #include "compressor.h"
0009 
0010 #include <TFile.h>
0011 #include <TTree.h>
0012 
0013 #include <iostream>
0014 #include <fstream>
0015 #include <string>
0016 #include <utility>
0017 
0018 using namespace std;
0019 
0020 void doCompression(ofstream &myfile, const TString& filename);
0021 Float_t computeSd(Float_t* avg, Int_t n_entries, TTree* t, Float_t* gen_);
0022 
0023 void output_before_vs_after(
0024  const TString& filename,
0025  vector<UShort_t>* order, 
0026  vector<Float_t>* dict, 
0027  Int_t n_entries, 
0028  TTree* t, 
0029  Float_t* gen_
0030 );
0031 
0032 uint64_t timeSinceEpochMillisec()
0033 {
0034    using namespace std::chrono;
0035    return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
0036 }
0037 
0038 void compress_clu_res_float32(std::string &filename)
0039 {
0040  ofstream logFile;
0041  logFile.open ("log.csv");
0042  logFile << "file,points,variable,dltSD,bits,dltdltSD/dltSD,avg,LB,UB,msec" << endl;
0043  doCompression(logFile, filename);
0044  logFile.close();
0045 }
0046 
0047 void doCompression(ofstream &myfile, const TString& filename)
0048 {
0049  TFile *f = new TFile(filename,"read");
0050  TTree *t = (TTree*)f->Get("ntp_trkres");
0051  Float_t genv1_;
0052  Float_t genv2_;
0053  //get branches
0054  t->SetBranchAddress("v1",&genv1_);
0055  t->SetBranchAddress("v2",&genv2_);
0056  Int_t n_entries = (Int_t)t->GetEntries();
0057 
0058  // start compress v1
0059  Float_t avg;
0060  Float_t dltSD = computeSd(&avg, n_entries, t, &genv1_);
0061  for (Int_t numBits = 3; numBits < 12; ++numBits) {
0062    vector<UShort_t> v1_order; // order, to replace v1 and save to the new ROOT file
0063    vector<Float_t> v1_dict; // dictionay, to save to the new ROOT file
0064    vector<size_t> v1_cnt; // how many data points in the dictionary
0065    uint64_t start = timeSinceEpochMillisec();
0066    Float_t dltdltSD = approx(&v1_order, &v1_dict, &v1_cnt, n_entries, t, &genv1_, (size_t) pow(2, numBits));
0067    myfile 
0068    << filename << "," 
0069    << v1_order.size() << ",dphi," 
0070    << dltSD << "," 
0071    << numBits << "," 
0072    << dltdltSD / dltSD << "," 
0073    << avg << "," 
0074    << avg - 4 * dltSD << "," 
0075    << avg + 4 * dltSD << "," 
0076    << timeSinceEpochMillisec() - start << endl;
0077    output_before_vs_after(filename + "_dltPHI_" + numBits + "bits.csv", &v1_order, &v1_dict, n_entries, t, &genv1_);
0078  }
0079  // end compress v1
0080  
0081  // start compress v2
0082  dltSD = computeSd(&avg, n_entries, t, &genv2_);
0083  for (Int_t numBits = 3; numBits < 12; ++numBits) {
0084    vector<UShort_t> v2_order; // order, to replace v2 and save to the new ROOT file
0085    vector<Float_t> v2_dict; // dictionay, to save to the new ROOT file
0086    vector<size_t> v2_cnt; // how many data points in the dictionary
0087    uint64_t start = timeSinceEpochMillisec();
0088    Float_t dltdltSD = approx(&v2_order, &v2_dict, &v2_cnt, n_entries, t, &genv2_, (size_t) pow(2, numBits));
0089    myfile 
0090      << filename << "," 
0091      << v2_order.size() << ",dz," 
0092      << dltSD << "," 
0093      << numBits << "," 
0094      << dltdltSD / dltSD << "," 
0095      << avg << "," 
0096      << avg - 4 * dltSD << "," 
0097      << avg + 4 * dltSD << "," 
0098      << timeSinceEpochMillisec() - start << endl;
0099      output_before_vs_after(filename + "_dltZ_" + numBits + "bits.csv", &v2_order, &v2_dict, n_entries, t, &genv2_);
0100   }
0101 }
0102 
0103 Float_t computeSd(Float_t* avg, Int_t n_entries, TTree* t, Float_t* gen_)
0104 {
0105   Double_t sumSquared = 0;
0106   Double_t sum = 0;
0107   for (Int_t j = 0 ; j < n_entries; j++){
0108     t->GetEntry(j);
0109     
0110     sumSquared += (Double_t) *gen_ * (Double_t) *gen_;
0111     sum += (Double_t) *gen_;
0112   }
0113   
0114   Double_t _avg = sum / (Double_t) n_entries;
0115   *avg = _avg;
0116 
0117   return sqrt((sumSquared / (Double_t) n_entries) - _avg * _avg);
0118 }
0119 
0120 void output_before_vs_after(const TString& filename, vector<UShort_t>* order, vector<Float_t>* dict, Int_t n_entries, TTree* t, Float_t* gen_)
0121 {
0122   ofstream myfile;
0123   myfile.open(filename);
0124   myfile << "before,after" << endl;
0125   for (Int_t j = 0 ; j < n_entries; j++){
0126     t->GetEntry(j);
0127     myfile << *gen_ << "," << dict->at(order->at(j)) << endl;
0128   }
0129   myfile.close();
0130 }
0131