CMS 3D CMS Logo

CovarianceParameterization.cc
Go to the documentation of this file.
1 //#include <iostream>
2 
3 #include <fmt/printf.h>
4 
5 #include <TParameter.h>
6 #include <TVector.h>
7 #include <TFolder.h>
8 
13 
14 uint16_t CompressionElement::pack(float value, float ref) const {
15  float toCompress = 0;
16  switch (target) {
17  case (realValue):
18  toCompress = value;
19  break;
20  case (ratioToRef):
21  toCompress = value / ref;
22  break;
23  case (differenceToRef):
24  toCompress = value - ref;
25  break;
26  }
27  switch (method) {
28  case (float16):
29  return MiniFloatConverter::float32to16(toCompress * params[0]);
30  break;
31  case (reduceMantissa):
33  break;
34  case (zero):
35  return 0;
36  break;
37  case (one):
38  return 1.0;
39  break;
40  case (tanLogPack):
41  return 0; //FIXME: should be implemented
42  break;
43  case (logPack):
44  int16_t r = logintpack::pack16log(toCompress, params[0], params[1], bits);
45  return *reinterpret_cast<uint16_t *>(&r);
46  break;
47  }
48  return 0;
49 }
50 float CompressionElement::unpack(uint16_t packed, float ref) const {
51  float unpacked = 0;
52  switch (method) {
53  case (float16):
54  unpacked = MiniFloatConverter::float16to32(packed) / params[0];
55  break;
56  case (reduceMantissa):
57  unpacked = packed;
58  break;
59  case (logPack):
60  unpacked = logintpack::unpack16log(*reinterpret_cast<int16_t *>(&packed), params[0], params[1], bits);
61  break;
62  case (zero):
63  unpacked = 0;
64  break;
65  case (one):
66  case (tanLogPack):
67  unpacked = 1; //FIXME: should be implemented
68  }
69  switch (target) {
70  case (realValue):
71  return unpacked;
72  case (ratioToRef):
73  return unpacked * ref;
74  case (differenceToRef):
75  return unpacked + ref;
76  }
77 
78  return ref;
79 }
80 
82  edm::FileInPath fip(
83  fmt::sprintf("DataFormats/PatCandidates/data/CovarianceParameterization_version%d.root", version));
84  fileToRead_ = TFile::Open(fip.fullPath().c_str());
85  TFile &fileToRead = *fileToRead_;
86  //Read files from here fip.fullPath().c_str();
87  if (fileToRead.IsOpen()) {
88  readFile(fileToRead);
89 
90  TIter next(((TDirectoryFile *)fileToRead.Get("schemas"))->GetListOfKeys());
91  TKey *key;
92  while ((key = (TKey *)next())) {
93  TClass *cl = gROOT->GetClass(key->GetClassName());
94  if (!cl->InheritsFrom("TDirectoryFile"))
95  continue;
96  std::string schemaNumber = key->ReadObj()->GetName();
97  uint16_t schemaN = std::stoi(schemaNumber);
98  //for (int folderNumber = 0; folderNumber < 6 ; folderNumber++) {
99  CompressionSchema schema;
100  for (int i = 0; i < 5; i++) {
101  for (int j = i; j < 5; j++) { //FILLING ONLY THE SCHEMA OF SOME ELEMENTS
102  std::string folder = "schemas/" + schemaNumber + "/" + char(48 + i) + char(48 + j);
103  std::string methodString = folder + "/method";
104  std::string targetString = folder + "/target";
105  std::string bitString = folder + "/bit";
106  std::vector<float> vParams;
107  TVector *p = (TVector *)fileToRead.Get((folder + "/param").c_str());
108  vParams.reserve(p->GetNoElements());
109  for (int k = 0; k < p->GetNoElements(); k++) {
110  vParams.push_back((*p)[k]);
111  }
112 
113  schema(i, j) = CompressionElement(
114  (CompressionElement::Method)((TParameter<int> *)fileToRead.Get(methodString.c_str()))->GetVal(),
115  (CompressionElement::Target)((TParameter<int> *)fileToRead.Get(targetString.c_str()))->GetVal(),
116  (int)((TParameter<int> *)fileToRead.Get(bitString.c_str()))->GetVal(),
117  vParams);
118  }
119  }
120  schemas[schemaN] = schema;
121  }
122 
124  } else {
125  loadedVersion_ = -1;
126  }
127 }
128 
130  for (int i = 0; i < 5; i++) {
131  for (int j = i; j < 5; j++) {
132  std::string String_first_positive = "_pixel_";
133  std::string String_second_positive = "_noPixel_";
134 
135  addTheHistogram(&cov_elements_pixelHit, String_first_positive, i, j, f);
136  addTheHistogram(&cov_elements_noPixelHit, String_second_positive, i, j, f);
137  }
138  }
139 }
140 
142  std::vector<TH3D *> *HistoVector, std::string StringToAddInTheName, int i, int j, TFile &fileToRead) {
143  std::string List_covName[5] = {"qoverp", "lambda", "phi", "dxy", "dsz"};
144 
145  std::string histoNameString = "covariance_" + List_covName[i] + "_" + List_covName[j] + StringToAddInTheName +
146  "parametrization"; // + "_entries";
147  TH3D *matrixElememtHistogramm = (TH3D *)fileToRead.Get(histoNameString.c_str());
148  HistoVector->push_back(matrixElememtHistogramm);
149 }
150 
152  int i, int j, int sign, float pt, float eta, int nHits, int pixelHits, float cii, float cjj) const {
153  int hitNumberToUse = nHits;
154  if (hitNumberToUse < 2)
155  hitNumberToUse = 2;
156  if (hitNumberToUse > 32)
157  hitNumberToUse = 32;
158  int ptBin = cov_elements_pixelHit[0]->GetXaxis()->FindBin(pt);
159  int etaBin = cov_elements_pixelHit[0]->GetYaxis()->FindBin(std::abs(eta));
160  int hitBin = cov_elements_pixelHit[0]->GetZaxis()->FindBin(hitNumberToUse);
161  int min_idx = i;
162  int max_idx = j;
163 
164  if (i > j) {
165  min_idx = j;
166  max_idx = i;
167  }
168 
169  int indexOfTheHitogramInTheList = ((9 - min_idx) * min_idx) / 2 + max_idx;
170 
171  double meanValue = 0.;
172  if (pixelHits > 0) {
173  meanValue = sign * cov_elements_pixelHit[indexOfTheHitogramInTheList]->GetBinContent(ptBin, etaBin, hitBin);
174  } else {
175  meanValue = sign * cov_elements_noPixelHit[indexOfTheHitogramInTheList]->GetBinContent(ptBin, etaBin, hitBin);
176  }
177  return meanValue;
178 }
179 
181  float value, int schema, int i, int j, float pt, float eta, int nHits, int pixelHits, float cii, float cjj) const {
182  if (i > j)
183  std::swap(i, j);
184  float ref = meanValue(i, j, 1., pt, eta, nHits, pixelHits, cii, cjj);
185  if (ref == 0) {
186  schema = 0;
187  }
188  if (schema == 0 && i == j && (i == 2 || i == 0))
189  ref = 1. / (pt * pt);
190  /* //Used for debugging, to be later removed
191  uint16_t p=(*schemas.find(schema)).second(i,j).pack(value,ref);
192  float up=(*schemas.find(schema)).second(i,j).unpack(p,ref);
193  std::cout << "check " << i << " " << j << " " << value << " " << up << " " << p << " " << ref << " " << schema<< std::endl;*/
194  return (*schemas.find(schema)).second(i, j).pack(value, ref);
195 }
197  uint16_t packed, int schema, int i, int j, float pt, float eta, int nHits, int pixelHits, float cii, float cjj)
198  const {
199  if (i > j)
200  std::swap(i, j);
201  float ref = meanValue(i, j, 1., pt, eta, nHits, pixelHits, cii, cjj);
202  if (ref == 0) {
203  schema = 0;
204  }
205  if (schema == 0 && i == j && (i == 2 || i == 0))
206  ref = 1. / (pt * pt);
207  if (i == j && (*schemas.find(schema)).second(i, j).unpack(packed, ref) == 0)
208  return 1e-9;
209  else
210  return (*schemas.find(schema)).second(i, j).unpack(packed, ref);
211 }
std::string fullPath() const
Definition: FileInPath.cc:161
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:62
static float float16to32(uint16_t h)
Definition: libminifloat.h:13
U second(std::pair< T, U > const &p)
std::vector< float > params
float unpack(uint16_t packed, float ref=0.) const
float meanValue(int i, int j, int sign, float pt, float eta, int nHits, int pixelHits, float cii=1., float cjj=1.) const
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
static uint16_t float32to16(float x)
Definition: libminifloat.h:17
float unpack(uint16_t packed, int schema, int i, int j, float pt, float eta, int nHits, int pixelHits, float cii=1., float cjj=1.) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
uint16_t pack(float value, float ref=0.) const
float pack(float value, int schema, int i, int j, float pt, float eta, int nHits, int pixelHits, float cii=1., float cjj=1.) const
Definition: value.py:1
std::vector< TH3D * > cov_elements_pixelHit
void addTheHistogram(std::vector< TH3D *> *HistoVector, std::string StringToAddInTheName, int i, int j, TFile &fileToRead)
TupleMultiplicity< TrackerTraits > const *__restrict__ uint32_t nHits
std::unordered_map< uint16_t, CompressionSchema > schemas
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:27
static float reduceMantissaToNbits(const float &f)
Definition: libminifloat.h:38
std::vector< TH3D * > cov_elements_noPixelHit