1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#include "CalibCalorimetry/HcalTPGAlgos/interface/HcaluLUTTPGCoder.h"
#include "Geometry/HcalTowerAlgo/src/HcalHardcodeGeometryData.h"
#include "FWCore/Utilities/interface/Exception.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include <iostream>
#include <fstream>
#include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
#include "CalibFormats/HcalObjects/interface/HcalCoderDb.h"
#include "CalibFormats/HcalObjects/interface/HcalCalibrations.h"
#include "CalibFormats/HcalObjects/interface/HcalDbService.h"
#include "Geometry/CaloTopology/interface/HcalTopology.h"
#include <cmath>
const float HcaluLUTTPGCoder::nominal_gain = 0.177;
HcaluLUTTPGCoder::HcaluLUTTPGCoder(const char* filename) {
AllocateLUTs();
getRecHitCalib(filename);
}
HcaluLUTTPGCoder::HcaluLUTTPGCoder(const char* filename, const char* fname2) {
throw cms::Exception("PROBLEM: This constructor should never be invoked!");
}
void HcaluLUTTPGCoder::compress(const IntegerCaloSamples& ics, const std::vector<bool>& featureBits, HcalTriggerPrimitiveDigi& tp) const {
throw cms::Exception("PROBLEM: This method should never be invoked!");
}
HcaluLUTTPGCoder::~HcaluLUTTPGCoder() {
for (int i = 0; i < nluts; i++) {
if (inputLUT[i] != 0) delete [] inputLUT[i];
}
delete [] _gain;
delete [] _ped;
}
void HcaluLUTTPGCoder::AllocateLUTs() {
HcalTopology theTopo;
HcalDetId did;
_ped = new float[nluts];
_gain = new float[nluts];
for (int i = 0; i < nluts; i++) inputLUT[i] = 0;
int maxid = 0, minid = 0x7FFFFFFF, rawid = 0;
for (int ieta=-41; ieta <= 41; ieta++) {
for (int iphi = 1; iphi <= 72; iphi++) {
for (int depth = 1; depth <= 3; depth++) {
did=HcalDetId(HcalBarrel,ieta,iphi,depth);
if (theTopo.valid(did)) {
rawid = GetLUTID(HcalBarrel, ieta, iphi, depth);
if (inputLUT[rawid] != 0) std::cout << "Error: LUT with (ieta,iphi,depth) = (" << ieta << "," << iphi << "," << depth << ") has been previously allocated!" << std::endl;
else inputLUT[rawid] = new LUT[INPUT_LUT_SIZE];
if (rawid < minid) minid = rawid;
if (rawid > maxid) maxid = rawid;
}
did=HcalDetId(HcalEndcap,ieta,iphi,depth);
if (theTopo.valid(did)) {
rawid = GetLUTID(HcalEndcap, ieta, iphi, depth);
if (inputLUT[rawid] != 0) std::cout << "Error: LUT with (ieta,iphi,depth) = (" << ieta << "," << iphi << "," << depth << ") has been previously allocated!" << std::endl;
else inputLUT[rawid] = new LUT[INPUT_LUT_SIZE];
if (rawid < minid) minid = rawid;
if (rawid > maxid) maxid = rawid;
}
did=HcalDetId(HcalForward,ieta,iphi,depth);
if (theTopo.valid(did)) {
rawid = GetLUTID(HcalForward, ieta, iphi, depth);
if (inputLUT[rawid] != 0) std::cout << "Error: LUT with (ieta,iphi,depth) = (" << ieta << "," << iphi << "," << depth << ") has been previously allocated!" << std::endl;
else inputLUT[rawid] = new LUT[INPUT_LUT_SIZE];
if (rawid < minid) minid = rawid;
if (rawid > maxid) maxid = rawid;
}
}
}
}
}
int HcaluLUTTPGCoder::GetLUTID(HcalSubdetector id, int ieta, int iphi, int depth) const {
int detid = 0;
if (id == HcalEndcap) detid = 1;
else if (id == HcalForward) detid = 2;
return iphi + 72 * ((ieta + 41) + 83 * (depth + 3 * detid)) - 7777;
}
void HcaluLUTTPGCoder::getRecHitCalib(const char* filename) {
std::ifstream userfile;
userfile.open(filename);
int tool;
float Rec_calib_[87];
if (userfile) {
userfile >> tool;
if (tool != 86) {
std::cout << "Wrong RecHit calibration filesize: " << tool << " (expect 86)" << std::endl;
}
for (int j=1; j<87; j++) {
userfile >> Rec_calib_[j]; // Read the Calib factors
Rcalib[j] = Rec_calib_[j] ;
}
userfile.close();
}
else std::cout << "File " << filename << " with RecHit calibration factors not found" << std::endl;
}
void HcaluLUTTPGCoder::update(const HcalDbService& conditions) {
const HcalQIEShape* shape = conditions.getHcalShape();
HcalCalibrations calibrations;
int id;
float divide;
HcalTopology theTopo;
float cosheta_[41], lsb_ = 1./16.;
for (int i = 0; i < 13; i++) cosheta_[i+29] = cosh((theHFEtaBounds[i+1] + theHFEtaBounds[i])/2.);
for (int depth = 1; depth <= 3; depth++) {
for (int iphi = 1; iphi <= 72; iphi++) {
divide = 1.*nominal_gain;
for (int ieta=-16; ieta <= 16; ieta++) {
HcalDetId cell(HcalBarrel,ieta,iphi,depth);
if (theTopo.valid(cell)) {
id = GetLUTID(HcalBarrel,ieta,iphi,depth);
if (inputLUT[id] == 0) throw cms::Exception("PROBLEM: inputLUT has not been initialized for HB, ieta, iphi, depth, id = ") << ieta << "," << iphi << "," << depth << "," << id << std::endl;
conditions.makeHcalCalibration (cell, &calibrations);
const HcalQIECoder* channelCoder = conditions.getHcalCoder (cell);
HcalCoderDb coder (*channelCoder, *shape);
//float ped_ = (calibrations.pedestal(0)+calibrations.pedestal(1)+calibrations.pedestal(2)+calibrations.pedestal(3))/4;
float gain_= (calibrations.gain(0)+calibrations.gain(1)+calibrations.gain(2)+calibrations.gain(3))/4;
float ped_ = 0;
HBHEDataFrame frame(cell);
frame.setSize(1);
CaloSamples samples(cell, 1);
_ped[id] = ped_;
_gain[id] = gain_;
for (int j = 0; j <= 0x7F; j++) {
HcalQIESample adc(j);
frame.setSample(0,adc);
coder.adc2fC(frame,samples);
float adc2fC_ = samples[0];
if (ieta <0 )inputLUT[id][j] = (LUT) std::min(std::max(0,int(((adc2fC_*gain_)-ped_)*Rcalib[abs(ieta)]/divide)-2), 0x3FF);
else inputLUT[id][j] = (LUT) std::min(std::max(0,int(((adc2fC_*gain_)-ped_)*Rcalib[abs(ieta)+43]/divide)-2), 0x3FF);
//if (ieta <0 )inputLUT[id][j] = (LUT) std::min(std::max(0,int((adc2fC_ - ped_)*gain_*Rcalib[abs(ieta)]/divide)), 0x3FF);
//else inputLUT[id][j] = (LUT) std::min(std::max(0,int((adc2fC_ - ped_)*gain_*Rcalib[abs(ieta)+43]/divide)), 0x3FF);
}
}
}
for (int ieta=-29; ieta <= 29; ieta++) {
HcalDetId cell(HcalEndcap,ieta,iphi,depth);
if (theTopo.valid(cell)) {
if (abs(ieta) < 21) divide = 1.*nominal_gain;
else if (abs(ieta) < 27) divide = 2.*nominal_gain;
else divide = 5.*nominal_gain;
id = GetLUTID(HcalEndcap,ieta,iphi,depth);
if (inputLUT[id] == 0) throw cms::Exception("PROBLEM: inputLUT has not been initialized for HE, ieta, iphi, depth, id = ") << ieta << "," << iphi << "," << depth << "," << id << std::endl;
conditions.makeHcalCalibration (cell, &calibrations);
const HcalQIECoder* channelCoder = conditions.getHcalCoder (cell);
HcalCoderDb coder (*channelCoder, *shape);
float ped_ = 0;//(calibrations.pedestal(0)+calibrations.pedestal(1)+calibrations.pedestal(2)+calibrations.pedestal(3))/4;
float gain_= (calibrations.gain(0)+calibrations.gain(1)+calibrations.gain(2)+calibrations.gain(3))/4;
HBHEDataFrame frame(cell);
frame.setSize(1);
CaloSamples samples(cell, 1);
_ped[id] = ped_;
_gain[id] = gain_;
for (int j = 0; j <= 0x7F; j++) {
HcalQIESample adc(j);
frame.setSample(0,adc);
coder.adc2fC(frame,samples);
float adc2fC_ = samples[0];
if ( ieta < 0 ) inputLUT[id][j] = (LUT) std::min(std::max(0,int(((adc2fC_*gain_)-ped_)*Rcalib[abs(ieta)+1]/divide)-2), 0x3FF);
else inputLUT[id][j] = (LUT) std::min(std::max(0,int(((adc2fC_*gain_)-ped_)*Rcalib[abs(ieta)+44]/divide)-2), 0x3FF);
}
}
}
for (int ieta=-41; ieta <= 41; ieta++) {
HcalDetId cell(HcalForward,ieta,iphi,depth);
if (theTopo.valid(cell)) {
id = GetLUTID(HcalForward,ieta,iphi,depth);
if (inputLUT[id] == 0) throw cms::Exception("PROBLEM: inputLUT has not been initialized for HF, ieta, iphi, depth, id = ") << ieta << "," << iphi << "," << depth << "," << id << std::endl;
conditions.makeHcalCalibration (cell, &calibrations);
const HcalQIECoder* channelCoder = conditions.getHcalCoder (cell);
HcalCoderDb coder (*channelCoder, *shape);
float ped_ = 0;//(calibrations.pedestal(0)+calibrations.pedestal(1)+calibrations.pedestal(2)+calibrations.pedestal(3))/4;
float gain_= (calibrations.gain(0)+calibrations.gain(1)+calibrations.gain(2)+calibrations.gain(3))/4;
HFDataFrame frame(cell);
frame.setSize(1);
CaloSamples samples(cell, 1);
_ped[id] = ped_;
_gain[id] = gain_;
for (int j = 0; j <= 0x7F; j++) {
HcalQIESample adc(j);
frame.setSample(0,adc);
coder.adc2fC(frame,samples);
float adc2fC_ = samples[0];
if (ieta < 0 ) inputLUT[id][j] = (LUT) std::min(std::max(0,int(((adc2fC_*gain_)-ped_)*Rcalib[abs(ieta)+2]*gain_/lsb_/cosheta_[abs(ieta)])-2), 0x3FF);
else inputLUT[id][j] = (LUT) std::min(std::max(0,int(((adc2fC_*gain_)-ped_)*Rcalib[abs(ieta)+45]*gain_/lsb_/cosheta_[abs(ieta)])-2), 0x3FF);
//if (ieta < 0 ) inputLUT[id][j] = (LUT) std::min(std::max(0,int((adc2fC_ - ped_)*Rcalib[abs(ieta)+2]*gain_/lsb_/cosheta_[abs(ieta)])), 0x3FF);
//else inputLUT[id][j] = (LUT) std::min(std::max(0,int((adc2fC_ - ped_)*Rcalib[abs(ieta)+45]*gain_/lsb_/cosheta_[abs(ieta)])), 0x3FF);
}
}
}
}
}
}
void HcaluLUTTPGCoder::adc2Linear(const HBHEDataFrame& df, IntegerCaloSamples& ics) const {
int id = GetLUTID(df.id().subdet(), df.id().ieta(), df.id().iphi(), df.id().depth());
if (inputLUT[id]==0) {
throw cms::Exception("Missing Data") << "No LUT for " << df.id();
} else {
for (int i=0; i<df.size(); i++){
if (df[i].adc() >= INPUT_LUT_SIZE || df[i].adc() < 0) throw cms::Exception("ADC overflow for HBHE tower: ") << i << " adc= " << df[i].adc();
ics[i]=inputLUT[id][df[i].adc()];
}
}
}
void HcaluLUTTPGCoder::adc2Linear(const HFDataFrame& df, IntegerCaloSamples& ics) const{
int id = GetLUTID(df.id().subdet(), df.id().ieta(), df.id().iphi(), df.id().depth());
if (inputLUT[id]==0) {
throw cms::Exception("Missing Data") << "No LUT for " << df.id();
} else {
for (int i=0; i<df.size(); i++){
if (df[i].adc() >= INPUT_LUT_SIZE || df[i].adc() < 0)
throw cms::Exception("ADC overflow for HF tower: ") << i << " adc= " << df[i].adc();
ics[i]=inputLUT[id][df[i].adc()];
}
}
}
unsigned short HcaluLUTTPGCoder::adc2Linear(HcalQIESample sample, HcalDetId id) const {
int ref = GetLUTID(id.subdet(), id.ieta(), id.iphi(), id.depth());
return inputLUT[ref][sample.adc()];
}
float HcaluLUTTPGCoder::getLUTPedestal(HcalDetId id) const {
int ref = GetLUTID(id.subdet(), id.ieta(), id.iphi(), id.depth());
return _ped[ref];
}
float HcaluLUTTPGCoder::getLUTGain(HcalDetId id) const {
int ref = GetLUTID(id.subdet(), id.ieta(), id.iphi(), id.depth());
return _gain[ref];
}
|