Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
compress_clu_res_float32.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file compress_clu_res_float32.cc
1 
8 #include "compressor.h"
9 
10 #include <TFile.h>
11 #include <TTree.h>
12 
13 #include <iostream>
14 #include <fstream>
15 #include <string>
16 #include <utility>
17 
18 using namespace std;
19 
20 void doCompression(ofstream &myfile, TString filename);
21 Float_t computeSd(Float_t* avg, Int_t n_entries, TTree* t, Float_t* gen_);
22 
24  TString filename,
25  vector<UShort_t>* order,
26  vector<Float_t>* dict,
27  Int_t n_entries,
28  TTree* t,
29  Float_t* gen_
30 );
31 
33 {
34  using namespace std::chrono;
35  return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
36 }
37 
39 {
40  ofstream logFile;
41  logFile.open ("log.csv");
42  logFile << "file,points,variable,dltSD,bits,dltdltSD/dltSD,avg,LB,UB,msec" << endl;
43  doCompression(logFile, filename);
44  logFile.close();
45 }
46 
47 void doCompression(ofstream &myfile, TString filename)
48 {
49  TFile *f = new TFile(filename,"read");
50  TTree *t = (TTree*)f->Get("ntp_trkres");
51  Float_t genv1_;
52  Float_t genv2_;
53  //get branches
54  t->SetBranchAddress("v1",&genv1_);
55  t->SetBranchAddress("v2",&genv2_);
56  Int_t n_entries = (Int_t)t->GetEntries();
57 
58  // start compress v1
59  Float_t avg;
60  Float_t dltSD = computeSd(&avg, n_entries, t, &genv1_);
61  for (Int_t numBits = 3; numBits < 12; ++numBits) {
62  vector<UShort_t> v1_order; // order, to replace v1 and save to the new ROOT file
63  vector<Float_t> v1_dict; // dictionay, to save to the new ROOT file
64  vector<size_t> v1_cnt; // how many data points in the dictionary
65  uint64_t start = timeSinceEpochMillisec();
66  Float_t dltdltSD = approx(&v1_order, &v1_dict, &v1_cnt, n_entries, t, &genv1_, (size_t) pow(2, numBits));
67  myfile
68  << filename << ","
69  << v1_order.size() << ",dphi,"
70  << dltSD << ","
71  << numBits << ","
72  << dltdltSD / dltSD << ","
73  << avg << ","
74  << avg - 4 * dltSD << ","
75  << avg + 4 * dltSD << ","
76  << timeSinceEpochMillisec() - start << endl;
77  output_before_vs_after(filename + "_dltPHI_" + numBits + "bits.csv", &v1_order, &v1_dict, n_entries, t, &genv1_);
78  }
79  // end compress v1
80 
81  // start compress v2
82  dltSD = computeSd(&avg, n_entries, t, &genv2_);
83  for (Int_t numBits = 3; numBits < 12; ++numBits) {
84  vector<UShort_t> v2_order; // order, to replace v2 and save to the new ROOT file
85  vector<Float_t> v2_dict; // dictionay, to save to the new ROOT file
86  vector<size_t> v2_cnt; // how many data points in the dictionary
87  uint64_t start = timeSinceEpochMillisec();
88  Float_t dltdltSD = approx(&v2_order, &v2_dict, &v2_cnt, n_entries, t, &genv2_, (size_t) pow(2, numBits));
89  myfile
90  << filename << ","
91  << v2_order.size() << ",dz,"
92  << dltSD << ","
93  << numBits << ","
94  << dltdltSD / dltSD << ","
95  << avg << ","
96  << avg - 4 * dltSD << ","
97  << avg + 4 * dltSD << ","
98  << timeSinceEpochMillisec() - start << endl;
99  output_before_vs_after(filename + "_dltZ_" + numBits + "bits.csv", &v2_order, &v2_dict, n_entries, t, &genv2_);
100  }
101 }
102 
103 Float_t computeSd(Float_t* avg, Int_t n_entries, TTree* t, Float_t* gen_)
104 {
105  Double_t sumSquared = 0;
106  Double_t sum = 0;
107  for (Int_t j = 0 ; j < n_entries; j++){
108  t->GetEntry(j);
109 
110  sumSquared += (Double_t) *gen_ * (Double_t) *gen_;
111  sum += (Double_t) *gen_;
112  }
113 
114  Double_t _avg = sum / (Double_t) n_entries;
115  *avg = _avg;
116 
117  return sqrt((sumSquared / (Double_t) n_entries) - _avg * _avg);
118 }
119 
120 void output_before_vs_after(TString filename, vector<UShort_t>* order, vector<Float_t>* dict, Int_t n_entries, TTree* t, Float_t* gen_)
121 {
122  ofstream myfile;
123  myfile.open(filename);
124  myfile << "before,after" << endl;
125  for (Int_t j = 0 ; j < n_entries; j++){
126  t->GetEntry(j);
127  myfile << *gen_ << "," << dict->at(order->at(j)) << endl;
128  }
129  myfile.close();
130 }
131