00001 #include "CalibCalorimetry/CaloTPG/src/CaloTPGTranscoderULUT.h"
00002 #include "FWCore/Utilities/interface/Exception.h"
00003 #include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h"
00004 #include <iostream>
00005 #include <fstream>
00006
00007 using namespace std;
00008
00009 HcalTrigTowerGeometry theTrigTowerGeometry;
00010
00011 CaloTPGTranscoderULUT::CaloTPGTranscoderULUT(const std::string& hcalFile1,
00012 const std::string& hcalFile2) :
00013 hcalITower_(N_TOWER,(const LUTType*)0)
00014 {
00015 loadHCAL(hcalFile1);
00016 loadhcalUncompress(hcalFile2);
00017 }
00018
00019 CaloTPGTranscoderULUT::~CaloTPGTranscoderULUT() {
00020 for (int i = 0; i < noutluts; i++) {
00021 if (outputLUT[i] != 0) delete [] outputLUT[i];
00022 }
00023 }
00024
00025
00026 void CaloTPGTranscoderULUT::loadHCAL(const std::string& filename) {
00027 int tool;
00028 std::ifstream userfile;
00029
00030
00031 for (int i = 0; i < noutluts; i++) outputLUT[i] = 0;
00032 int maxid = 0, minid = 0x7FFFFFFF, rawid = 0;
00033 for (int ieta=-32; ieta <= 32; ieta++) {
00034 for (int iphi = 1; iphi <= 72; iphi++) {
00035 if (HTvalid(ieta,iphi)) {
00036 rawid = GetOutputLUTId(ieta, iphi);
00037 if (outputLUT[rawid] != 0) std::cout << "Error: LUT with (ieta,iphi) = (" << ieta << "," << iphi << ") has been previously allocated!" << std::endl;
00038 else outputLUT[rawid] = new LUT[OUTPUT_LUT_SIZE];
00039 if (rawid < minid) minid = rawid;
00040 if (rawid > maxid) maxid = rawid;
00041 }
00042 }
00043 }
00044
00045 userfile.open((char *)filename.data());
00046
00047 if( userfile ) {
00048 int nluts = 0;
00049 std::string s;
00050 std::vector<int> loieta,hiieta;
00051 std::vector<int> loiphi,hiiphi;
00052 getline(userfile,s);
00053
00054 getline(userfile,s);
00055
00056 unsigned int index = 0;
00057 while (index < s.length()) {
00058 while (isspace(s[index])) index++;
00059 if (index < s.length()) nluts++;
00060 while (!isspace(s[index])) index++;
00061 }
00062
00063 for (unsigned int i=0; i<=s.length(); i++) userfile.unget();
00064
00065 outputluts_.resize(nluts);
00066 for (int i=0; i<nluts; i++) {
00067 outputluts_[i].resize(OUTPUT_LUT_SIZE);
00068 }
00069
00070
00071 for (int i=0; i<nluts; i++) {
00072 userfile >> tool;
00073 loieta.push_back(tool);
00074
00075 }
00076
00077 for (int i=0; i<nluts; i++) {
00078 userfile >> tool;
00079 hiieta.push_back(tool);
00080
00081 }
00082
00083 for (int i=0; i<nluts; i++) {
00084 userfile >> tool;
00085 loiphi.push_back(tool);
00086
00087 }
00088
00089 for (int i=0; i<nluts; i++) {
00090 userfile >> tool;
00091 hiiphi.push_back(tool);
00092
00093 }
00094
00095
00096 for (unsigned int j=0; j<OUTPUT_LUT_SIZE; j++) {
00097 for(int i=0; i <nluts; i++) {
00098 userfile >> tool;
00099 if (tool < 0) {
00100 std::cout << "Error: LUT can't have negative numbers; 0 used instead: " << i << ", " << j << " : = " << tool << std::endl;
00101 tool = 0;
00102 } else if (tool > 0xff) {
00103 std::cout << "Error: LUT can't have >8-bit numbers; 0xff used instead: " << i << ", " << j << " : = " << tool << std::endl;
00104 tool = 0xff;
00105 }
00106 outputluts_[i][j] = tool;
00107 if (userfile.eof()) std::cout << "Error: LUT file is truncated or has a wrong format: " << i << "," << j << std::endl;
00108 }
00109 }
00110 userfile.close();
00111
00112 HcalDetId cell;
00113 int id, ntot = 0;
00114 for (int i=0; i < nluts; i++) {
00115 int nini = 0;
00116 for (int iphi = loiphi[i]; iphi <= hiiphi[i]; iphi++) {
00117 for (int ieta=loieta[i]; ieta <= hiieta[i]; ieta++) {
00118 if (HTvalid(ieta,iphi)) {
00119 id = GetOutputLUTId(ieta,iphi);
00120 if (outputLUT[id] == 0) throw cms::Exception("PROBLEM: outputLUT has not been initialized for ieta, iphi, id = ") << ieta << ", " << iphi << ", " << id << std::endl;
00121 for (int j = 0; j <= 0x3ff; j++) outputLUT[id][j] = outputluts_[i][j];
00122 nini++;
00123 ntot++;
00124 }
00125 }
00126 }
00127
00128 }
00129
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 }
00149
00150 void CaloTPGTranscoderULUT::loadhcalUncompress(const std::string& filename) {
00151 std::ifstream userfile;
00152 userfile.open(filename.c_str());
00153 static const int etabound = 32;
00154 static const int tpgmax = 256;
00155 if( userfile )
00156 {
00157 for (int i=0; i<tpgmax; i++) {
00158 for(int j = 1; j <=etabound; j++) {
00159 userfile >> hcaluncomp_[j][i];}
00160 }
00161
00162 userfile.close();
00163
00164 }
00165 else {
00166 throw cms::Exception("Invalid Data") << "Unable to read uncompress file" << filename;
00167 }
00168 }
00169
00170 HcalTriggerPrimitiveSample CaloTPGTranscoderULUT::hcalCompress(const HcalTrigTowerDetId& id, unsigned int sample, bool fineGrain) const {
00171 int ieta = id.ieta();
00172 int iphi = id.iphi();
00173 if (abs(ieta) > 28) iphi = iphi/4 + 1;
00174 int itower = GetOutputLUTId(ieta,iphi);
00175
00176 if (itower < 0) cms::Exception("Invalid Data") << "No trigger tower found for ieta, iphi = " << ieta << ", " << iphi;
00177 if (sample >= OUTPUT_LUT_SIZE) {
00178
00179 throw cms::Exception("Out of Range") << "LUT has 1024 entries for " << itower << " but " << sample << " was requested.";
00180 sample=OUTPUT_LUT_SIZE - 1;
00181 }
00182
00183 return HcalTriggerPrimitiveSample(outputLUT[itower][sample],fineGrain,0,0);
00184 }
00185
00186
00187 double CaloTPGTranscoderULUT::hcaletValue(const int& ieta, const int& compET) const {
00188 double etvalue = hcaluncomp_[ieta][compET];
00189 return(etvalue);
00190 }
00191
00192 double CaloTPGTranscoderULUT::hcaletValue(const HcalTrigTowerDetId& hid, const HcalTriggerPrimitiveSample& hc) const {
00193 int ieta = hid.ietaAbs();
00194 int compET = hc.compressedEt();
00195 double etvalue = hcaluncomp_[ieta][compET];
00196 return(etvalue);
00197 }
00198
00199 EcalTriggerPrimitiveSample CaloTPGTranscoderULUT::ecalCompress(const EcalTrigTowerDetId& id, unsigned int sample, bool fineGrain) const {
00200 throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::ecalCompress";
00201 }
00202
00203 void CaloTPGTranscoderULUT::rctEGammaUncompress(const HcalTrigTowerDetId& hid, const HcalTriggerPrimitiveSample& hc,
00204 const EcalTrigTowerDetId& eid, const EcalTriggerPrimitiveSample& ec,
00205 unsigned int& et, bool& egVecto, bool& activity) const {
00206 throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctEGammaUncompress";
00207 }
00208 void CaloTPGTranscoderULUT::rctJetUncompress(const HcalTrigTowerDetId& hid, const HcalTriggerPrimitiveSample& hc,
00209 const EcalTrigTowerDetId& eid, const EcalTriggerPrimitiveSample& ec,
00210 unsigned int& et) const {
00211 throw cms::Exception("Not Implemented") << "CaloTPGTranscoderULUT::rctJetUncompress";
00212 }
00213
00214 bool CaloTPGTranscoderULUT::HTvalid(const int ieta, const int iphi) const {
00215 if (iphi <= 0 || ieta == 0) return false;
00216 if (abs(ieta) > 32) return false;
00217 else if (abs(ieta) > 28 && iphi > 18) return false;
00218 else if (iphi > 72) return false;
00219 return true;
00220 }
00221
00222 int CaloTPGTranscoderULUT::GetOutputLUTId(const int ieta, const int iphi) const {
00223 if (HTvalid(ieta, iphi)) {
00224 int offset = 0, ietaabs;
00225 ietaabs = abs(ieta);
00226 if (ieta < 0) offset = noutluts/2;
00227 if (ietaabs < 29) return 72*(ietaabs - 1) + (iphi - 1) + offset;
00228 else return 18*(ietaabs - 29) + iphi + 2015 + offset;
00229 } else return -1;
00230 }