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 }
logintpack::pack16log
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:27
mps_fire.i
i
Definition: mps_fire.py:428
CovarianceParameterization::readFile
void readFile(TFile &)
Definition: CovarianceParameterization.cc:129
etaBin
int etaBin(const l1t::HGCalMulticluster *cl)
Definition: L1EGammaEEProducer.cc:19
CompressionElement::target
Target target
Definition: CovarianceParameterization.h:17
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
CompressionElement::params
std::vector< float > params
Definition: CovarianceParameterization.h:19
DiDispStaMuonMonitor_cfi.pt
pt
Definition: DiDispStaMuonMonitor_cfi.py:39
printsummarytable.folder
folder
Definition: printsummarytable.py:7
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CompressionElement::method
Method method
Definition: CovarianceParameterization.h:16
liblogintpack.h
CompressionElement::realValue
Definition: CovarianceParameterization.h:12
Validation_hcalonly_cfi.sign
sign
Definition: Validation_hcalonly_cfi.py:32
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
CompressionElement::Method
Method
Definition: CovarianceParameterization.h:11
MiniFloatConverter::reduceMantissaToNbits
static float reduceMantissaToNbits(const float &f)
Definition: libminifloat.h:49
CovarianceParameterization::pack
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: CovarianceParameterization.cc:180
CompressionElement::ratioToRef
Definition: CovarianceParameterization.h:12
muonRecoAnalyzer_cfi.ptBin
ptBin
Definition: muonRecoAnalyzer_cfi.py:45
CovarianceParameterization::loadedVersion_
int loadedVersion_
Definition: CovarianceParameterization.h:69
CompressionElement::tanLogPack
Definition: CovarianceParameterization.h:11
FileInPath.h
GetRecoTauVFromDQM_MC_cff.cl
cl
Definition: GetRecoTauVFromDQM_MC_cff.py:38
edm::FileInPath
Definition: FileInPath.h:64
CovarianceParameterization::meanValue
float meanValue(int i, int j, int sign, float pt, float eta, int nHits, int pixelHits, float cii=1., float cjj=1.) const
Definition: CovarianceParameterization.cc:151
CovarianceParameterization::cov_elements_pixelHit
std::vector< TH3D * > cov_elements_pixelHit
Definition: CovarianceParameterization.h:72
libminifloat.h
std::swap
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
Definition: DataFrameContainer.h:209
nHits
const caConstants::TupleMultiplicity *__restrict__ const HitsOnGPU *__restrict__ double *__restrict__ float *__restrict__ double *__restrict__ uint32_t nHits
Definition: BrokenLineFitOnGPU.h:27
PVValHelper::eta
Definition: PVValidationHelpers.h:70
CompressionElement::unpack
float unpack(uint16_t packed, float ref=0.) const
Definition: CovarianceParameterization.cc:50
CovarianceParameterization::schemas
std::unordered_map< uint16_t, CompressionSchema > schemas
Definition: CovarianceParameterization.h:71
CovarianceParameterization::cov_elements_noPixelHit
std::vector< TH3D * > cov_elements_noPixelHit
Definition: CovarianceParameterization.h:73
CompressionElement
Definition: CovarianceParameterization.h:9
logintpack::unpack16log
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:62
dqmdumpme.k
k
Definition: dqmdumpme.py:60
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
CompressionElement::pack
uint16_t pack(float value, float ref=0.) const
Definition: CovarianceParameterization.cc:14
CompressionElement::Target
Target
Definition: CovarianceParameterization.h:12
CovarianceParameterization::unpack
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
Definition: CovarianceParameterization.cc:196
CovarianceParameterization::addTheHistogram
void addTheHistogram(std::vector< TH3D * > *HistoVector, std::string StringToAddInTheName, int i, int j, TFile &fileToRead)
Definition: CovarianceParameterization.cc:141
CompressionElement::reduceMantissa
Definition: CovarianceParameterization.h:11
MiniFloatConverter::float16to32
static float float16to32(uint16_t h)
Definition: libminifloat.h:12
createfilelist.int
int
Definition: createfilelist.py:10
value
Definition: value.py:1
CompressionElement::bits
int bits
Definition: CovarianceParameterization.h:18
alignCSCRings.r
r
Definition: alignCSCRings.py:93
CompressionElement::zero
Definition: CovarianceParameterization.h:11
CompressionElement::one
Definition: CovarianceParameterization.h:11
CompressionElement::float16
Definition: CovarianceParameterization.h:11
CovarianceParameterization::fileToRead_
TFile * fileToRead_
Definition: CovarianceParameterization.h:70
relativeConstraints.value
value
Definition: relativeConstraints.py:53
CovarianceParameterization::CompressionSchema
Definition: CovarianceParameterization.h:32
CovarianceParameterization::load
void load(int version)
Definition: CovarianceParameterization.cc:81
CompressionElement::logPack
Definition: CovarianceParameterization.h:11
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
CompressionElement::differenceToRef
Definition: CovarianceParameterization.h:12
crabWrapper.key
key
Definition: crabWrapper.py:19
BeamSplash_cfg.version
version
Definition: BeamSplash_cfg.py:45
MiniFloatConverter::float32to16
static uint16_t float32to16(float x)
Definition: libminifloat.h:20
edm::FileInPath::fullPath
std::string fullPath() const
Definition: FileInPath.cc:161
GetRecoTauVFromDQM_MC_cff.next
next
Definition: GetRecoTauVFromDQM_MC_cff.py:31
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
CovarianceParameterization.h