CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/L1Trigger/RegionalCaloTrigger/src/L1RCTLookupTables.cc

Go to the documentation of this file.
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/L1RCTNoisyChannelMask.h"
00016 #include "CondFormats/L1TObjects/interface/L1CaloEcalScale.h"
00017 #include "CondFormats/L1TObjects/interface/L1CaloHcalScale.h"
00018 #include "CondFormats/L1TObjects/interface/L1CaloEtScale.h"
00019 
00020 unsigned int L1RCTLookupTables::lookup(unsigned short ecalInput,
00021                                        unsigned short hcalInput,
00022                                        unsigned short fgbit,
00023                                        unsigned short crtNo,
00024                                        unsigned short crdNo,
00025                                        unsigned short twrNo) const
00026 {
00027   if(rctParameters_ == 0)
00028     throw cms::Exception("L1RCTParameters Invalid")
00029       << "L1RCTParameters should be set every event" << rctParameters_;
00030   if(channelMask_ == 0)
00031     throw cms::Exception("L1RCTChannelMask Invalid")
00032       << "L1RCTChannelMask should be set every event" << channelMask_;
00033   if(noisyChannelMask_ == 0)
00034     throw cms::Exception("L1RCTNoisyChannelMask Invalid")
00035       << "L1RCTNoisyChannelMask should be set every event" << noisyChannelMask_;
00036   if(ecalInput > 0xFF) 
00037     throw cms::Exception("Invalid Data") 
00038       << "ECAL compressedET should be less than 0xFF, is " << ecalInput;
00039   if(hcalInput > 0xFF) 
00040     throw cms::Exception("Invalid Data") 
00041       << "HCAL compressedET should be less than 0xFF, is " << hcalInput;
00042   if(fgbit > 1) 
00043     throw cms::Exception("Invalid Data") 
00044       << "ECAL finegrain should be a single bit, is " << fgbit;
00045   short iEta = (short) rctParameters_->calcIEta(crtNo, crdNo, twrNo);
00046   unsigned short iAbsEta = (unsigned short) abs(iEta);
00047   short sign = iEta/iAbsEta;
00048   unsigned short iPhi = rctParameters_->calcIPhi(crtNo, crdNo, twrNo);
00049   unsigned short phiSide = (iPhi/4)%2;
00050   if(iAbsEta < 1 || iAbsEta > 28) 
00051     throw cms::Exception("Invalid Data") 
00052       << "1 <= |IEta| <= 28, is " << iAbsEta;
00053 
00054 
00055   //Pre Input bits
00056   unsigned short ecalAfterMask=0;
00057   unsigned short hcalAfterMask=0;
00058 
00059 
00060   // using channel mask to mask off ecal channels
00061   //Mike: Introducing the hot channel mask 
00062   //If the Et is above the threshold then mask it as well
00063 
00064 
00065   
00066   float ecalBeforeMask = convertEcal(ecalInput, iAbsEta, sign);
00067 
00068 
00069   bool resetECAL = (channelMask_->ecalMask[crtNo][phiSide][iAbsEta-1]) || //channel mask
00070     (noisyChannelMask_->ecalMask[crtNo][phiSide][iAbsEta-1] &&
00071      ecalBeforeMask<noisyChannelMask_->ecalThreshold)||//hot mask
00072       (rctParameters_->eGammaECalScaleFactors()[iAbsEta-1] == 0.&&
00073        rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] == 0.);       
00074     
00075 
00076 
00077   if (resetECAL)    {
00078       ecalAfterMask=0;
00079     }
00080   else    {
00081       ecalAfterMask=ecalInput;
00082     }
00083 
00084   float ecal =  convertEcal(ecalAfterMask, iAbsEta, sign);
00085 
00086 
00087   // masking off hcal for channels in channel mask
00088   float hcalBeforeMask = convertHcal(hcalInput, iAbsEta, sign);
00089 
00090   bool resetHCAL = channelMask_->hcalMask[crtNo][phiSide][iAbsEta-1]||
00091     (noisyChannelMask_->hcalMask[crtNo][phiSide][iAbsEta-1] &&
00092      hcalBeforeMask<noisyChannelMask_->hcalThreshold)||//hot mask
00093       (rctParameters_->eGammaHCalScaleFactors()[iAbsEta-1] == 0.&&
00094        rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] == 0.);
00095 
00096   if (resetHCAL)    {
00097       hcalAfterMask=0;
00098     }
00099   else    {
00100       hcalAfterMask=hcalInput;
00101     }
00102 
00103   float hcal = convertHcal(hcalAfterMask, iAbsEta, sign);
00104 
00105   unsigned long etIn7Bits;
00106   unsigned long etIn9Bits;
00107 
00108   if((ecalAfterMask == 0 && hcalAfterMask > 0) &&
00109      ((rctParameters_->noiseVetoHB() && iAbsEta > 0 && iAbsEta < 18)
00110       || (rctParameters_->noiseVetoHEplus() && iAbsEta>17 && crtNo>8)
00111       || (rctParameters_->noiseVetoHEminus() && iAbsEta>17 && crtNo<9)))
00112     {
00113       etIn7Bits = 0;
00114       etIn9Bits = 0;
00115     }
00116   else
00117     {
00118       etIn7Bits = eGammaETCode(ecal, hcal, iAbsEta);
00119       etIn9Bits = jetMETETCode(ecal, hcal, iAbsEta);
00120     }
00121   // Saturated input towers cause tower ET pegging at the highest value
00122   if((ecalAfterMask == 0xFF && 
00123       rctParameters_->eGammaECalScaleFactors()[iAbsEta-1] != 0. ) 
00124      || (hcalAfterMask == 0xFF &&
00125          rctParameters_->eGammaHCalScaleFactors()[iAbsEta-1] != 0. )
00126      )
00127     {
00128       etIn7Bits = 0x7F; // egamma path
00129     }
00130   if((ecalAfterMask == 0xFF &&
00131       rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] != 0. )
00132      || (hcalAfterMask == 0xFF &&
00133          rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] != 0. ))
00134     {
00135       etIn9Bits = 0x1FF; // sums path
00136     }
00137 
00138   unsigned long shiftEtIn9Bits = etIn9Bits<<8;
00139   unsigned long shiftHE_FGBit = hOeFGVetoBit(ecal, hcal, fgbit)<<7;
00140   unsigned long shiftActivityBit = 0;
00141   if ( rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] == 0.
00142        && rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] == 0. )
00143     {
00144       // do nothing, it's already zero
00145     }
00146   else if (rctParameters_->jetMETECalScaleFactors()[iAbsEta-1] == 0. )
00147     {
00148       shiftActivityBit = activityBit(0., hcal)<<17;
00149     }
00150   else if (rctParameters_->jetMETHCalScaleFactors()[iAbsEta-1] == 0. )
00151     {
00152       shiftActivityBit = activityBit(ecal, 0.)<<17;
00153     }
00154   else
00155     {
00156       shiftActivityBit = activityBit(ecal, hcal)<<17;
00157     }
00158   unsigned long output=etIn7Bits+shiftHE_FGBit+shiftEtIn9Bits+shiftActivityBit;
00159   return output;
00160 }
00161 
00162 unsigned int L1RCTLookupTables::lookup(unsigned short hfInput,
00163                                        unsigned short crtNo,
00164                                        unsigned short crdNo,
00165                                        unsigned short twrNo
00166                                        ) const
00167 {
00168   if(rctParameters_ == 0)
00169     throw cms::Exception("L1RCTParameters Invalid")
00170       << "L1RCTParameters should be set every event" << rctParameters_;
00171   if(channelMask_ == 0)
00172     throw cms::Exception("L1RCTChannelMask Invalid")
00173       << "L1RCTChannelMask should be set every event" << channelMask_;
00174   if(hfInput > 0xFF) 
00175     throw cms::Exception("Invalid Data") 
00176       << "HF compressedET should be less than 0xFF, is " << hfInput;
00177   short iEta = rctParameters_->calcIEta(crtNo, crdNo, twrNo);
00178   unsigned short iAbsEta = abs(iEta);
00179   short sign = (iEta/iAbsEta);
00180   unsigned short phiSide = twrNo/4;
00181   if(iAbsEta < 29 || iAbsEta > 32) 
00182     throw cms::Exception("Invalid Data") 
00183       << "29 <= |iEta| <= 32, is " << iAbsEta;
00184 
00185   float et = convertHcal(hfInput, iAbsEta, sign);;
00186 
00187 
00188 
00189   if (channelMask_->hfMask[crtNo][phiSide][iAbsEta-29]||
00190       (noisyChannelMask_->hfMask[crtNo][phiSide][iAbsEta-29]&&
00191        et<noisyChannelMask_->hfThreshold))
00192     {
00193       et = 0;
00194     }
00195 
00196   unsigned int result = convertToInteger(et, rctParameters_->jetMETLSB(), 8);
00197   return result;
00198 }
00199 
00200 bool L1RCTLookupTables::hOeFGVetoBit(float ecal, float hcal, bool fgbit) const
00201 {
00202   if(rctParameters_ == 0)
00203     throw cms::Exception("L1RCTParameters Invalid")
00204       << "L1RCTParameters should be set every event" << rctParameters_;
00205   bool veto = false;
00206   if(ecal > rctParameters_->eMinForFGCut() && 
00207      ecal < rctParameters_->eMaxForFGCut())
00208     {
00209       if(fgbit) veto = true;
00210     }
00211   if(ecal >= rctParameters_->eMinForHoECut() && 
00212      ecal < rctParameters_->eMaxForHoECut())
00213     {
00214       if((hcal / ecal) > rctParameters_->hOeCut()) veto = true;
00215     }
00216   //  else
00217   if (ecal < rctParameters_->eMinForHoECut())
00218     {
00219       if(hcal >= rctParameters_->hMinForHoECut()) veto = true;  // Changed from eMinForHoECut() - JLL 2008-Feb-13
00220     }
00221   return veto;
00222 }
00223 
00224 bool L1RCTLookupTables::activityBit(float ecal, float hcal) const
00225 {
00226   // Redefined for upgrade as EM activity only
00227   if(rctParameters_ == 0)
00228     throw cms::Exception("L1RCTParameters Invalid")
00229       << "L1RCTParameters should be set every event" << rctParameters_;
00230   bool aBit = false;
00231   if(rctParameters_->eMinForHoECut() < rctParameters_->eMaxForHoECut()) {
00232     // For RCT operations HoE cut and tauVeto are used
00233     aBit = ((ecal > rctParameters_->eActivityCut()) || 
00234             (hcal > rctParameters_->hActivityCut()));
00235   }
00236   else {
00237     // We redefine tauVeto() for upgrade as EM activity only  -- 
00238     // both EG and Tau make it through the EIC and JSC to CTP cards
00239     // In the CTP card we want to rechannel EG/Tau candidates to EG and Tau
00240     if(ecal > rctParameters_->eActivityCut()) {
00241       if((hcal/ecal) < rctParameters_->hOeCut()) {
00242         aBit = true;
00243       }
00244     }
00245   }
00246   return aBit;
00247 }
00248 
00249 // uses etScale
00250 unsigned int L1RCTLookupTables::emRank(unsigned short energy) const 
00251 {
00252   if(etScale_)
00253     {
00254       return etScale_->rank(energy);
00255     }
00256   else
00257     //    edm::LogInfo("L1RegionalCaloTrigger") 
00258     //      << "CaloEtScale was not used - energy instead of rank" << std::endl;
00259   return energy;
00260 }
00261 
00262 // converts compressed ecal energy to linear (real) scale
00263 float L1RCTLookupTables::convertEcal(unsigned short ecal, unsigned short iAbsEta, short sign) const
00264 {
00265   if(ecalScale_)
00266     {
00267       //std::cout << "[luts] energy " << ecal << " sign " << sign 
00268       //<< " iAbsEta " << iAbsEta << " iPhi "   << iPhi << std::endl;
00269       float dummy = 0;
00270       dummy = float (ecalScale_->et( ecal, iAbsEta, sign ));
00271       /*
00272       if (ecal > 0)
00273         {
00274           std::cout << "[luts] ecal converted from " << ecal << " to " 
00275                     << dummy << " with iAbsEta " << iAbsEta << std::endl;
00276         }
00277       */
00278       return dummy;
00279     }
00280   //else if(rctParameters_ == 0)
00281   //  {
00282   //    throw cms::Exception("L1RCTParameters Invalid")
00283   //    << "L1RCTParameters should be set every event" << rctParameters_;
00284   //  }
00285   else
00286     {
00287       return ((float) ecal) * rctParameters_->eGammaLSB();
00288     }
00289 }
00290 
00291 // converts compressed hcal energy to linear (real) scale
00292 float L1RCTLookupTables::convertHcal(unsigned short hcal, unsigned short iAbsEta, short sign) const
00293 {
00294   if (hcalScale_ != 0)
00295     {
00296       return (hcalScale_->et( hcal, iAbsEta, sign ));
00297     }
00298   else
00299     {
00300       //      edm::LogInfo("L1RegionalCaloTrigger") 
00301       //        << "CaloTPGTranscoder was not used" << std::endl;
00302       return ((float) hcal) * rctParameters_->jetMETLSB();
00303     }
00304 }
00305 
00306 // integerize given an LSB and set maximum value of 2^precision-1
00307 unsigned long L1RCTLookupTables::convertToInteger(float et, 
00308                                                   float lsb, 
00309                                                   int precision) const
00310 {
00311   unsigned long etBits = (unsigned long)(et/lsb);
00312   unsigned long maxValue = (1 << precision) - 1;
00313   if(etBits > maxValue)
00314     return maxValue;
00315   else
00316     return etBits;
00317 }
00318 
00319 unsigned int L1RCTLookupTables::eGammaETCode(float ecal, float hcal, int iAbsEta) const
00320 {
00321   if(rctParameters_ == 0)
00322     throw cms::Exception("L1RCTParameters Invalid")
00323       << "L1RCTParameters should be set every event" << rctParameters_;
00324   float etLinear = rctParameters_->EGammaTPGSum(ecal,hcal,iAbsEta);
00325   return convertToInteger(etLinear, rctParameters_->eGammaLSB(), 7);
00326 }
00327 
00328 unsigned int L1RCTLookupTables::jetMETETCode(float ecal, float hcal, int iAbsEta) const
00329 {
00330   if(rctParameters_ == 0)
00331     throw cms::Exception("L1RCTParameters Invalid")
00332       << "L1RCTParameters should be set every event" << rctParameters_;
00333   float etLinear = rctParameters_->JetMETTPGSum(ecal,hcal,iAbsEta);
00334   return convertToInteger(etLinear, rctParameters_->jetMETLSB(), 9);
00335 }