CMS 3D CMS Logo

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