CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/L1Trigger/RegionalCaloTrigger/src/L1RCTReceiverCard.cc

Go to the documentation of this file.
00001 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTReceiverCard.h"
00002 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTLookupTables.h"
00003 
00004 #include <vector>
00005 using std::vector;
00006 
00007 #include <bitset>
00008 using std::bitset;
00009 
00010 #include <iostream>
00011 using std::cout;
00012 using std::cerr;
00013 using std::endl;
00014 
00015 #include <string>
00016 using std::string;
00017 
00018 L1RCTReceiverCard::L1RCTReceiverCard(int crateNumber,int cardNumber, const L1RCTLookupTables* rctLookupTables) :
00019   regions(2),crtNo(crateNumber),cardNo(cardNumber),
00020   rctLookupTables_(rctLookupTables),
00021   etIn10Bits(2), overFlowBits(2),muonBits(2),tauBits(2)
00022 {
00023 }
00024 
00025 L1RCTReceiverCard::~L1RCTReceiverCard(){}
00026 
00027 void L1RCTReceiverCard::randomInput(){
00028   std::vector<unsigned short> input(64);
00029   for(int i = 0; i<64;i++)
00030     input.at(i) = rand()&511;
00031   fillInput(input);
00032 }
00033 
00034 void L1RCTReceiverCard::fileInput(char* filename){
00035   std::vector<unsigned short> input(64);
00036   unsigned short x;
00037   std::ifstream instream(filename);
00038   if(instream){
00039     for(int i = 0; i<64; i++){
00040       if(!(instream >> x))
00041         break;
00042       else
00043         input.at(i) = x;
00044     }
00045   }
00046   fillInput(input);
00047 }
00048 
00049 
00050 //First layer is ecal the second is hcal.
00051 //goes in order of (for crate 0,card 0)
00052 // (Region 1)   (Region 0)
00053 // 28 24 20 16 12 08 04 00
00054 // 29 25 21 17 13 09 05 01
00055 // 30 26 22 18 14 10 06 02
00056 // 31 27 23 19 15 11 07 03 
00057 //
00058 // For card 6 of crate 0 it would look like 
00059 //
00060 // 12 08 04 00
00061 // 13 09 05 01
00062 // 14 10 06 02
00063 // 15 11 07 03
00064 // 16 20 24 28
00065 // 17 21 25 29
00066 // 18 22 26 30
00067 // 19 23 27 31
00068 
00069 
00070 void L1RCTReceiverCard::fillInput(std::vector<unsigned short> input){
00071   
00072   std::vector<unsigned short> ecalInput(32);
00073   std::vector<unsigned short> ecalFG(32);
00074   std::vector<unsigned short> hcalInput(32);
00075   std::vector<unsigned short> hcalMuon(32);
00076 
00077   for(int i = 0; i<32; i++){
00078     ecalInput.at(i) = input.at(i)/2;
00079     ecalFG.at(i) = input.at(i) & 1;
00080     hcalInput.at(i) = input.at(i+32)/2;
00081     hcalMuon.at(i) = input.at(i+32) & 1;
00082     unsigned long lookup = rctLookupTables_->lookup(ecalInput.at(i),hcalInput.at(i),ecalFG.at(i),crtNo, cardNo, i); // tower number 0-31 now
00083     unsigned short etIn7Bits = lookup&127;
00084     unsigned short etIn9Bits = (lookup >> 8)&511;
00085     unsigned short HE_FGBit = (lookup>>7)&1;
00086     unsigned short activityBit = (lookup>>17)&1;
00087     std::vector<unsigned short> indices = towerToRegionMap(i);
00088     unsigned short r = indices.at(0);
00089     unsigned short row = indices.at(1);
00090     unsigned short col = indices.at(2);
00091     regions.at(r).setEtIn7Bits(row,col,etIn7Bits);
00092     regions.at(r).setEtIn9Bits(row,col,etIn9Bits);
00093     regions.at(r).setHE_FGBit(row,col,HE_FGBit);
00094     regions.at(r).setMuonBit(row,col,hcalMuon.at(i));
00095     regions.at(r).setActivityBit(row,col,activityBit);
00096   }
00097 
00098 }
00099 
00100 
00101 vector<unsigned short> L1RCTReceiverCard::towerToRegionMap(int towernum){
00102   std::vector<unsigned short> returnVec(3);
00103   unsigned short region;
00104   unsigned short towerrow;
00105   unsigned short towercol;
00106   if(crtNo <9){
00107     if(cardNo != 6){
00108       if(towernum < 16){
00109         region = 0;
00110         towerrow = towernum%4;
00111         towercol = 3-(towernum/4);
00112       }
00113       else{
00114         region = 1;
00115         towerrow = towernum%4;
00116         towercol = 7-(towernum/4);
00117       }
00118     }
00119     else{
00120       if(towernum < 16){
00121         region = 0;
00122         towerrow = towernum%4;
00123         towercol = 3-(towernum/4);
00124       }
00125       else{
00126         region = 1;
00127         towerrow = towernum%4;
00128         towercol = (towernum/4)-4;
00129       }
00130     }
00131   }
00132   else{
00133     if(cardNo != 6){
00134       if(towernum < 16){
00135         region = 0;
00136         towerrow = towernum%4;
00137         towercol = towernum/4;
00138       }
00139       else{
00140         region = 1;
00141         towerrow = towernum%4;
00142         towercol = (towernum/4)-4;
00143       }
00144     }
00145     else{
00146       if(towernum < 16){
00147         region = 0;
00148         towerrow = towernum%4;
00149         towercol = towernum/4;
00150       }
00151       else{
00152         region = 1;
00153         towerrow = towernum%4;
00154         towercol = 7-(towernum/4);
00155       }
00156     }
00157   }
00158   returnVec.at(0)=region;
00159   returnVec.at(1)=towerrow;
00160   returnVec.at(2)=towercol;
00161   return returnVec;
00162 }    
00163   
00164 
00165 
00166 void L1RCTReceiverCard::fillTauBits(){
00167   for(int i = 0; i<2; i++)
00168     tauBits.at(i) = calcTauBit(regions.at(i));
00169 }
00170 
00171 unsigned short L1RCTReceiverCard::calcTauBit(L1RCTRegion region){
00172   bitset<4> etaPattern;
00173   bitset<4> phiPattern;
00174 
00175   bitset<4> badPattern5(string("0101"));
00176   bitset<4> badPattern7(string("0111"));
00177   bitset<4> badPattern9(string("1001"));
00178   bitset<4> badPattern10(string("1010"));
00179   bitset<4> badPattern11(string("1011"));
00180   bitset<4> badPattern13(string("1101"));
00181   bitset<4> badPattern14(string("1110"));
00182   bitset<4> badPattern15(string("1111"));
00183 
00184   for(int i = 0; i<4; i++){
00185     phiPattern[i] = region.getActivityBit(i,0) || region.getActivityBit(i,1) ||
00186       region.getActivityBit(i,2) || region.getActivityBit(i,3);
00187     etaPattern[i] = region.getActivityBit(0,i) || region.getActivityBit(1,i) ||
00188       region.getActivityBit(2,i) || region.getActivityBit(3,i);
00189   }
00190 
00191   bool answer;
00192   
00193   if(etaPattern != badPattern5 && etaPattern != badPattern7 && 
00194      etaPattern != badPattern10 && etaPattern != badPattern11 &&
00195      etaPattern != badPattern13 && etaPattern != badPattern14 &&
00196      etaPattern != badPattern15 && phiPattern != badPattern5 && 
00197      phiPattern != badPattern7 && phiPattern != badPattern10 && 
00198      phiPattern != badPattern11 && phiPattern != badPattern13 && 
00199      phiPattern != badPattern14 && phiPattern != badPattern15 &&
00200      etaPattern != badPattern9 && phiPattern != badPattern9){       // adding in "9"
00201     //return false;
00202     answer = false;
00203   }
00204   //else return true;
00205   else {
00206     answer = true;
00207   }
00208   // std::cout << "Tau veto set to " << answer << std::endl;
00209   return answer;
00210 }
00211 
00212 void L1RCTReceiverCard::fillRegionSums(){
00213   for(int i = 0; i<2; i++){
00214     etIn10Bits.at(i) = (calcRegionSum(regions.at(i)))/2;
00215     overFlowBits.at(i) = (calcRegionSum(regions.at(i)) & 1);
00216   }
00217 }
00218 
00219 unsigned short L1RCTReceiverCard::calcRegionSum(L1RCTRegion region){
00220   unsigned short sum = 0;
00221   unsigned short overflow = 0;
00222   for(int i = 0; i<4; i++){
00223     for(int j = 0; j<4; j++){
00224       unsigned short towerEt = region.getEtIn9Bits(i,j);
00225       // If tower is saturated, peg the region to max value
00226       //if(towerEt == 0x1FF) sum = 0x3FF;  // HARDWARE DOESN'T DO THIS!!
00227       //else 
00228       sum = sum + towerEt;
00229     }
00230   }
00231   if(sum > 1023){
00232     sum = 1023;
00233     overflow = 1;
00234   }
00235   unsigned short sumFullInfo = sum*2 + overflow;
00236   return sumFullInfo;
00237 }
00238 
00239 void L1RCTReceiverCard::fillMuonBits(){
00240   for(int i = 0; i<2; i++)
00241     muonBits.at(i) = calcMuonBit(regions.at(i));
00242 }
00243 
00244 unsigned short L1RCTReceiverCard::calcMuonBit(L1RCTRegion region){
00245   unsigned short muonBit = 0;
00246   for(int i = 0; i<4; i++){
00247     for(int j = 0; j<4; j++){
00248       muonBit = muonBit || region.getMuonBit(i,j);
00249     }
00250   }
00251   return muonBit;
00252 
00253 }
00254 
00255 void L1RCTReceiverCard::print(){
00256   std::cout <<"Receiver Card " << cardNo << " in Crate " << crtNo <<std::endl;
00257 
00258   for(int i=0;i<2;i++){
00259     std::cout << "Region " << i << " information" << std::endl;
00260     regions.at(i).print();
00261     std::cout << "Region Et sum " << etIn10Bits.at(i) << std::endl;
00262     std::cout << "Tau Veto Bit " << tauBits.at(i) << std::endl;
00263     std::cout << "Muon Bit " << muonBits.at(i) << std::endl;
00264   }
00265 }