forked from cms-sw/cmssw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HcalDbService.cc
294 lines (253 loc) · 9.5 KB
/
HcalDbService.cc
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// F.Ratnikov (UMd), Aug. 9, 2005
//
#include "FWCore/Utilities/interface/typelookup.h"
#include "CalibFormats/HcalObjects/interface/HcalDbService.h"
#include "CalibFormats/HcalObjects/interface/HcalCoderDb.h"
#include "CalibFormats/HcalObjects/interface/HcalCalibrations.h"
#include "CalibFormats/HcalObjects/interface/HcalCalibrationWidths.h"
#include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
#include <cmath>
HcalDbService::HcalDbService (const edm::ParameterSet& cfg):
mPedestals (0), mPedestalWidths (0),
mGains (0), mGainWidths (0),
mQIEData(0),
mQIETypes(0),
mElectronicsMap(0),
mRespCorrs(0),
mL1TriggerObjects(0),
mTimeCorrs(0),
mLUTCorrs(0),
mPFCorrs(0),
mLutMetadata(0),
mCalibSet(nullptr), mCalibWidthSet(nullptr)
{}
const HcalTopology* HcalDbService::getTopologyUsed() const {
if (mPedestals && mPedestals->topo()) return mPedestals->topo();
if (mGains && mGains->topo()) return mGains->topo();
if (mRespCorrs && mRespCorrs->topo()) return mRespCorrs->topo();
if (mQIETypes && mQIETypes->topo()) return mQIETypes->topo();
if (mL1TriggerObjects && mL1TriggerObjects->topo()) return mL1TriggerObjects->topo();
if (mLutMetadata && mLutMetadata->topo()) return mLutMetadata->topo();
return 0;
}
const HcalCalibrations& HcalDbService::getHcalCalibrations(const HcalGenericDetId& fId) const
{
buildCalibrations();
return (*mCalibSet.load(std::memory_order_acquire)).getCalibrations(fId);
}
const HcalCalibrationWidths& HcalDbService::getHcalCalibrationWidths(const HcalGenericDetId& fId) const
{
buildCalibWidths();
return (*mCalibWidthSet.load(std::memory_order_acquire)).getCalibrationWidths(fId);
}
void HcalDbService::buildCalibrations() const {
// we use the set of ids for pedestals as the master list
if ((!mPedestals) || (!mGains) || (!mQIEData) || (!mQIETypes) || (!mRespCorrs) || (!mTimeCorrs) || (!mLUTCorrs) ) return;
if (!mCalibSet.load(std::memory_order_acquire)) {
auto ptr = new HcalCalibrationsSet();
std::vector<DetId> ids=mPedestals->getAllChannels();
bool pedsInADC = mPedestals->isADC();
// loop!
HcalCalibrations tool;
// std::cout << " length of id-vector: " << ids.size() << std::endl;
for (std::vector<DetId>::const_iterator id=ids.begin(); id!=ids.end(); ++id) {
// make
bool ok=makeHcalCalibration(*id,&tool,pedsInADC);
// store
if (ok) ptr->setCalibrations(*id,tool);
// std::cout << "Hcal calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
}
ptr->sort();
HcalCalibrationsSet const * cptr = ptr;
HcalCalibrationsSet const * expect = nullptr;
bool exchanged = mCalibSet.compare_exchange_strong(expect, cptr, std::memory_order_acq_rel);
if(!exchanged) {
delete ptr;
}
}
}
void HcalDbService::buildCalibWidths() const {
// we use the set of ids for pedestal widths as the master list
if ((!mPedestalWidths) || (!mGainWidths) || (!mQIEData) ) return;
if (!mCalibWidthSet.load(std::memory_order_acquire)) {
auto ptr = new HcalCalibrationWidthsSet();
const std::vector<DetId>& ids=mPedestalWidths->getAllChannels();
bool pedsInADC = mPedestalWidths->isADC();
// loop!
HcalCalibrationWidths tool;
// std::cout << " length of id-vector: " << ids.size() << std::endl;
for (std::vector<DetId>::const_iterator id=ids.begin(); id!=ids.end(); ++id) {
// make
bool ok=makeHcalCalibrationWidth(*id,&tool,pedsInADC);
// store
if (ok) ptr->setCalibrationWidths(*id,tool);
// std::cout << "Hcal calibrations built... detid no. " << HcalGenericDetId(*id) << std::endl;
}
ptr->sort();
HcalCalibrationWidthsSet const * cptr = ptr;
HcalCalibrationWidthsSet const * expect = nullptr;
bool exchanged = mCalibWidthSet.compare_exchange_strong(expect, cptr, std::memory_order_acq_rel);
if(!exchanged) {
delete ptr;
}
}
}
bool HcalDbService::makeHcalCalibration (const HcalGenericDetId& fId, HcalCalibrations* fObject, bool pedestalInADC) const {
if (fObject) {
const HcalPedestal* pedestal = getPedestal (fId);
const HcalGain* gain = getGain (fId);
const HcalRespCorr* respcorr = getHcalRespCorr (fId);
const HcalTimeCorr* timecorr = getHcalTimeCorr (fId);
const HcalLUTCorr* lutcorr = getHcalLUTCorr (fId);
if (pedestalInADC) {
const HcalQIECoder* coder=getHcalCoder(fId);
const HcalQIEShape* shape=getHcalShape(coder);
if (pedestal && gain && shape && coder && respcorr && timecorr && lutcorr) {
float pedTrue[4];
for (int i=0; i<4; i++) {
float x=pedestal->getValues()[i];
int x1=(int)std::floor(x);
int x2=(int)std::floor(x+1);
// y = (y2-y1)/(x2-x1) * (x - x1) + y1 [note: x2-x1=1]
float y2=coder->charge(*shape,x2,i);
float y1=coder->charge(*shape,x1,i);
pedTrue[i]=(y2-y1)*(x-x1)+y1;
}
*fObject = HcalCalibrations (gain->getValues (), pedTrue, respcorr->getValue(), timecorr->getValue(), lutcorr->getValue() );
return true;
}
} else {
if (pedestal && gain && respcorr && timecorr && lutcorr) {
*fObject = HcalCalibrations (gain->getValues (), pedestal->getValues (), respcorr->getValue(), timecorr->getValue(), lutcorr->getValue() );
return true;
}
}
}
return false;
}
bool HcalDbService::makeHcalCalibrationWidth (const HcalGenericDetId& fId,
HcalCalibrationWidths* fObject, bool pedestalInADC) const {
if (fObject) {
const HcalPedestalWidth* pedestalwidth = getPedestalWidth (fId);
const HcalGainWidth* gainwidth = getGainWidth (fId);
if (pedestalInADC) {
const HcalQIECoder* coder=getHcalCoder(fId);
const HcalQIEShape* shape=getHcalShape(coder);
if (pedestalwidth && gainwidth && shape && coder) {
float pedTrueWidth[4];
for (int i=0; i<4; i++) {
float x=pedestalwidth->getWidth(i);
// assume QIE is linear in low range and use x1=0 and x2=1
// y = (y2-y1) * (x) [do not add any constant, only scale!]
float y2=coder->charge(*shape,1,i);
float y1=coder->charge(*shape,0,i);
pedTrueWidth[i]=(y2-y1)*x;
}
*fObject = HcalCalibrationWidths (gainwidth->getValues (), pedTrueWidth);
return true;
}
} else {
if (pedestalwidth && gainwidth) {
float pedestalWidth [4];
for (int i = 0; i < 4; i++) pedestalWidth [i] = pedestalwidth->getWidth (i);
*fObject = HcalCalibrationWidths (gainwidth->getValues (), pedestalWidth);
return true;
}
}
}
return false;
}
const HcalQIEType* HcalDbService::getHcalQIEType (const HcalGenericDetId& fId) const {
if (mQIETypes) {
return mQIETypes->getValues (fId);
}
return 0;
}
const HcalRespCorr* HcalDbService::getHcalRespCorr (const HcalGenericDetId& fId) const {
if (mRespCorrs) {
return mRespCorrs->getValues (fId);
}
return 0;
}
const HcalPedestal* HcalDbService::getPedestal (const HcalGenericDetId& fId) const {
if (mPedestals) {
return mPedestals->getValues (fId);
}
return 0;
}
const HcalPedestalWidth* HcalDbService::getPedestalWidth (const HcalGenericDetId& fId) const {
if (mPedestalWidths) {
return mPedestalWidths->getValues (fId);
}
return 0;
}
const HcalGain* HcalDbService::getGain (const HcalGenericDetId& fId) const {
if (mGains) {
return mGains->getValues(fId);
}
return 0;
}
const HcalGainWidth* HcalDbService::getGainWidth (const HcalGenericDetId& fId) const {
if (mGainWidths) {
return mGainWidths->getValues (fId);
}
return 0;
}
const HcalQIECoder* HcalDbService::getHcalCoder (const HcalGenericDetId& fId) const {
if (mQIEData) {
return mQIEData->getCoder (fId);
}
return 0;
}
const HcalQIEShape* HcalDbService::getHcalShape (const HcalGenericDetId& fId) const {
if (mQIEData && mQIETypes) {
//currently 3 types of QIEs exist: QIE8, QIE10, QIE11
int qieType = mQIETypes->getValues(fId)->getValue();
//QIE10 and QIE11 have same shape (ADC ladder)
if(qieType>0) qieType = 1;
return &mQIEData->getShape(qieType);
}
return 0;
}
const HcalQIEShape* HcalDbService::getHcalShape (const HcalQIECoder *coder) const {
HcalGenericDetId fId(coder->rawId());
return getHcalShape(fId);
}
const HcalElectronicsMap* HcalDbService::getHcalMapping () const {
return mElectronicsMap;
}
const HcalL1TriggerObject* HcalDbService::getHcalL1TriggerObject (const HcalGenericDetId& fId) const
{
return mL1TriggerObjects->getValues (fId);
}
const HcalChannelStatus* HcalDbService::getHcalChannelStatus (const HcalGenericDetId& fId) const
{
return mChannelQuality->getValues (fId);
}
const HcalZSThreshold* HcalDbService::getHcalZSThreshold (const HcalGenericDetId& fId) const
{
return mZSThresholds->getValues (fId);
}
const HcalTimeCorr* HcalDbService::getHcalTimeCorr (const HcalGenericDetId& fId) const {
if (mTimeCorrs) {
return mTimeCorrs->getValues (fId);
}
return 0;
}
const HcalLUTCorr* HcalDbService::getHcalLUTCorr (const HcalGenericDetId& fId) const {
if (mLUTCorrs) {
return mLUTCorrs->getValues (fId);
}
return 0;
}
const HcalPFCorr* HcalDbService::getHcalPFCorr (const HcalGenericDetId& fId) const {
if (mPFCorrs) {
return mPFCorrs->getValues (fId);
}
return 0;
}
const HcalLutMetadata* HcalDbService::getHcalLutMetadata () const {
return mLutMetadata;
}
TYPELOOKUP_DATA_REG(HcalDbService);