00001 #include <iostream>
00002 using std::cout;
00003 using std::endl;
00004
00005 #include <fstream>
00006 #include <string>
00007
00008 #include "FWCore/Utilities/interface/Exception.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010
00011 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTLookupTables.h"
00012
00013 #include "CondFormats/L1TObjects/interface/L1RCTParameters.h"
00014 #include "CondFormats/L1TObjects/interface/L1RCTChannelMask.h"
00015 #include "CondFormats/L1TObjects/interface/L1CaloEcalScale.h"
00016 #include "CondFormats/L1TObjects/interface/L1CaloHcalScale.h"
00017 #include "CondFormats/L1TObjects/interface/L1CaloEtScale.h"
00018
00019 unsigned int L1RCTLookupTables::lookup(unsigned short ecalInput,
00020 unsigned short hcalInput,
00021 unsigned short fgbit,
00022 unsigned short crtNo,
00023 unsigned short crdNo,
00024 unsigned short twrNo) const
00025 {
00026 if(rctParameters_ == 0)
00027 throw cms::Exception("L1RCTParameters Invalid")
00028 << "L1RCTParameters should be set every event" << rctParameters_;
00029 if(channelMask_ == 0)
00030 throw cms::Exception("L1RCTChannelMask Invalid")
00031 << "L1RCTChannelMask should be set every event" << channelMask_;
00032 if(ecalInput > 0xFF)
00033 throw cms::Exception("Invalid Data")
00034 << "ECAL compressedET should be less than 0xFF, is " << ecalInput;
00035 if(hcalInput > 0xFF)
00036 throw cms::Exception("Invalid Data")
00037 << "HCAL compressedET should be less than 0xFF, is " << hcalInput;
00038 if(fgbit > 1)
00039 throw cms::Exception("Invalid Data")
00040 << "ECAL finegrain should be a single bit, is " << fgbit;
00041 short iEta = (short) rctParameters_->calcIEta(crtNo, crdNo, twrNo);
00042 unsigned short iAbsEta = (unsigned short) abs(iEta);
00043 short sign = iEta/iAbsEta;
00044 unsigned short iPhi = rctParameters_->calcIPhi(crtNo, crdNo, twrNo);
00045 unsigned short phiSide = (iPhi/4)%2;
00046 if(iAbsEta < 1 || iAbsEta > 28)
00047 throw cms::Exception("Invalid Data")
00048 << "1 <= |IEta| <= 28, is " << iAbsEta;
00049 float ecal;
00050 float hcal;
00051
00052 if (channelMask_->ecalMask[crtNo][phiSide][iAbsEta])
00053 {
00054 ecal = 0;
00055 }
00056 else
00057 {
00058 ecal = convertEcal(ecalInput, iAbsEta, sign);
00059 }
00060
00061 if (channelMask_->hcalMask[crtNo][phiSide][iAbsEta])
00062 {
00063 hcal = 0;
00064 }
00065 else
00066 {
00067 hcal = convertHcal(hcalInput, iAbsEta, sign);
00068 }
00069
00070
00071
00072 unsigned long etIn7Bits;
00073 unsigned long etIn9Bits;
00074
00075
00076
00077
00078
00079
00080 if((ecalInput == 0 && hcalInput > 0) &&
00081 ((rctParameters_->noiseVetoHB() && iAbsEta > 0 && iAbsEta < 18)
00082 || (rctParameters_->noiseVetoHEplus() && iAbsEta>17 && crtNo>8)
00083 || (rctParameters_->noiseVetoHEminus() && iAbsEta>17 && crtNo<9)))
00084 {
00085 etIn7Bits = 0;
00086 etIn9Bits = 0;
00087 }
00088 else
00089 {
00090 etIn7Bits = eGammaETCode(ecal, hcal, iAbsEta);
00091 etIn9Bits = jetMETETCode(ecal, hcal, iAbsEta);
00092 }
00093
00094 if((ecalInput == 0xFF &&
00095 rctParameters_->eGammaECalScaleFactors()[iAbsEta-1] != 0. )
00096 || (hcalInput == 0xFF &&
00097 rctParameters_->eGammaHCalScaleFactors()[iAbsEta-1] != 0. )
00098 )
00099 {
00100 etIn7Bits = 0x7F;
00101 }
00102 if((ecalInput == 0xFF &&
00103 rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] != 0. )
00104 || (hcalInput == 0xFF &&
00105 rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] != 0. ))
00106 {
00107 etIn9Bits = 0x1FF;
00108 }
00109
00110 unsigned long shiftEtIn9Bits = etIn9Bits<<8;
00111 unsigned long shiftHE_FGBit = hOeFGVetoBit(ecal, hcal, fgbit)<<7;
00112 unsigned long shiftActivityBit = 0;
00113 if ( rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] == 0.
00114 && rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] == 0. )
00115 {
00116
00117 }
00118 else if (rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] == 0. )
00119 {
00120 shiftActivityBit = activityBit(0., hcal)<<17;
00121 }
00122 else if (rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] == 0. )
00123 {
00124 shiftActivityBit = activityBit(ecal, 0.)<<17;
00125 }
00126 else
00127 {
00128 shiftActivityBit = activityBit(ecal, hcal)<<17;
00129 }
00130 unsigned long output=etIn7Bits+shiftHE_FGBit+shiftEtIn9Bits+shiftActivityBit;
00131 return output;
00132 }
00133
00134 unsigned int L1RCTLookupTables::lookup(unsigned short hfInput,
00135 unsigned short crtNo,
00136 unsigned short crdNo,
00137 unsigned short twrNo
00138 ) const
00139 {
00140 if(rctParameters_ == 0)
00141 throw cms::Exception("L1RCTParameters Invalid")
00142 << "L1RCTParameters should be set every event" << rctParameters_;
00143 if(channelMask_ == 0)
00144 throw cms::Exception("L1RCTChannelMask Invalid")
00145 << "L1RCTChannelMask should be set every event" << channelMask_;
00146 if(hfInput > 0xFF)
00147 throw cms::Exception("Invalid Data")
00148 << "HF compressedET should be less than 0xFF, is " << hfInput;
00149 short iEta = rctParameters_->calcIEta(crtNo, crdNo, twrNo);
00150 unsigned short iAbsEta = abs(iEta);
00151 short sign = (iEta/iAbsEta);
00152 unsigned short phiSide = twrNo/4;
00153 if(iAbsEta < 29 || iAbsEta > 32)
00154 throw cms::Exception("Invalid Data")
00155 << "29 <= |iEta| <= 32, is " << iAbsEta;
00156 float et;
00157 if (channelMask_->hfMask[crtNo][phiSide][iAbsEta-29])
00158 {
00159 et = 0;
00160 }
00161 else
00162 {
00163 et = convertHcal(hfInput, iAbsEta, sign);
00164 }
00165 unsigned int result = convertToInteger(et, rctParameters_->jetMETLSB(), 8);
00166
00167
00168
00169
00170
00171
00172 return result;
00173 }
00174
00175 bool L1RCTLookupTables::hOeFGVetoBit(float ecal, float hcal, bool fgbit) const
00176 {
00177 if(rctParameters_ == 0)
00178 throw cms::Exception("L1RCTParameters Invalid")
00179 << "L1RCTParameters should be set every event" << rctParameters_;
00180 bool veto = false;
00181 if(ecal > rctParameters_->eMinForFGCut() &&
00182 ecal < rctParameters_->eMaxForFGCut())
00183 {
00184 if(fgbit) veto = true;
00185 }
00186 if(ecal > rctParameters_->eMinForHoECut() &&
00187 ecal < rctParameters_->eMaxForHoECut())
00188 {
00189 if((hcal / ecal) > rctParameters_->hOeCut()) veto = true;
00190 }
00191
00192 if(ecal < rctParameters_->eMinForHoECut())
00193 {
00194 if(hcal > rctParameters_->hMinForHoECut()) veto = true;
00195 }
00196 return veto;
00197 }
00198
00199 bool L1RCTLookupTables::activityBit(float ecal, float hcal) const
00200 {
00201 if(rctParameters_ == 0)
00202 throw cms::Exception("L1RCTParameters Invalid")
00203 << "L1RCTParameters should be set every event" << rctParameters_;
00204 return ((ecal > rctParameters_->eActivityCut()) ||
00205 (hcal > rctParameters_->hActivityCut()));
00206 }
00207
00208
00209 unsigned int L1RCTLookupTables::emRank(unsigned short energy) const
00210 {
00211 if(etScale_)
00212 {
00213 return etScale_->rank(energy);
00214 }
00215 else
00216
00217
00218 return energy;
00219 }
00220
00221
00222 float L1RCTLookupTables::convertEcal(unsigned short ecal, unsigned short iAbsEta, short sign) const
00223 {
00224 if(ecalScale_)
00225 {
00226
00227
00228 float dummy = 0;
00229 dummy = float (ecalScale_->et( ecal, iAbsEta, sign ));
00230
00231
00232
00233
00234
00235
00236
00237 return dummy;
00238 }
00239
00240
00241
00242
00243
00244 else
00245 {
00246 return ((float) ecal) * rctParameters_->eGammaLSB();
00247 }
00248 }
00249
00250
00251 float L1RCTLookupTables::convertHcal(unsigned short hcal, unsigned short iAbsEta, short sign) const
00252 {
00253 if (hcalScale_ != 0)
00254 {
00255 return (hcalScale_->et( hcal, iAbsEta, sign ));
00256 }
00257 else
00258 {
00259
00260
00261 return ((float) hcal) * rctParameters_->jetMETLSB();
00262 }
00263 }
00264
00265
00266 unsigned long L1RCTLookupTables::convertToInteger(float et,
00267 float lsb,
00268 int precision) const
00269 {
00270 unsigned long etBits = (unsigned long)(et/lsb);
00271 unsigned long maxValue = (1 << precision) - 1;
00272 if(etBits > maxValue)
00273 return maxValue;
00274 else
00275 return etBits;
00276 }
00277
00278 unsigned int L1RCTLookupTables::eGammaETCode(float ecal, float hcal, int iAbsEta) const
00279 {
00280 if(rctParameters_ == 0)
00281 throw cms::Exception("L1RCTParameters Invalid")
00282 << "L1RCTParameters should be set every event" << rctParameters_;
00283 float etLinear =
00284 rctParameters_->eGammaECalScaleFactors()[iAbsEta-1] * ecal +
00285 rctParameters_->eGammaHCalScaleFactors()[iAbsEta-1] * hcal;
00286 return convertToInteger(etLinear, rctParameters_->eGammaLSB(), 7);
00287 }
00288
00289 unsigned int L1RCTLookupTables::jetMETETCode(float ecal, float hcal, int iAbsEta) const
00290 {
00291 if(rctParameters_ == 0)
00292 throw cms::Exception("L1RCTParameters Invalid")
00293 << "L1RCTParameters should be set every event" << rctParameters_;
00294 float etLinear =
00295 rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] * ecal +
00296 rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] * hcal;
00297 return convertToInteger(etLinear, rctParameters_->jetMETLSB(), 9);
00298 }