File indexing completed on 2024-04-06 12:19:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include "L1Trigger/GlobalCaloTrigger/interface/L1GctElectronSorter.h"
0013 #include "DataFormats/L1GlobalCaloTrigger/interface/L1GctEmCand.h"
0014 #include "DataFormats/L1CaloTrigger/interface/L1CaloEmCand.h"
0015 #include "FWCore/Utilities/interface/Exception.h"
0016
0017
0018 #include <fstream> //for file IO
0019 #include <string>
0020 #include <vector>
0021 #include <iostream>
0022 #include <exception> //for exception handling
0023 #include <stdexcept> //for std::runtime_error()
0024
0025 using namespace std;
0026
0027
0028 typedef vector<L1CaloEmCand> EmInputCandVec;
0029 typedef vector<L1GctEmCand> EmOutputCandVec;
0030 ifstream file;
0031 ofstream ofile;
0032 EmInputCandVec indata;
0033 EmOutputCandVec gctData;
0034
0035
0036 void LoadFileData(const string& inputFile, int electrons, bool isolation);
0037
0038 void WriteFileData(EmOutputCandVec outputs);
0039
0040 void print(EmOutputCandVec cands);
0041
0042
0043 void convertToGct(EmInputCandVec cands);
0044
0045 int main() {
0046 indata.clear();
0047 gctData.clear();
0048
0049 cout << "**************************************" << endl;
0050 cout << " L1GctElectronSorter class unit tester." << endl;
0051 cout << "****************************************" << endl;
0052
0053 EmInputCandVec inputs;
0054 EmOutputCandVec outputs;
0055 bool checkIn = false;
0056 bool checkOut = false;
0057 bool nInputs = false;
0058 bool nOutputs = false;
0059
0060
0061 unsigned int noElectrons = 16;
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 const string testFile = "data/testElectrons_0";
0075
0076 try {
0077
0078 L1GctElectronSorter* testSort = new L1GctElectronSorter(noElectrons / 4, 1);
0079
0080
0081 inputs = testSort->getInputCands();
0082 if (inputs.size() != noElectrons) {
0083 throw cms::Exception("ErrorSizeOfInput")
0084 << "The electronSorter constructor holds the wrong number of inputs" << endl;
0085 nInputs = true;
0086 }
0087
0088
0089 outputs = testSort->getOutputCands();
0090 if (outputs.size() != 4) {
0091 throw cms::Exception("ErrorSizeOfOutput")
0092 << "The size of the output vector in electron sorter is incorrect" << endl;
0093 nOutputs = true;
0094 }
0095
0096
0097
0098 LoadFileData(testFile, noElectrons, 1);
0099 cout << " Data loaded in from input file" << endl;
0100 print(gctData);
0101 for (unsigned int i = 0; i < indata.size(); i++) {
0102 testSort->setInputEmCand(indata[i]);
0103 }
0104 inputs = testSort->getInputCands();
0105
0106
0107
0108 for (unsigned int i = 0; i != indata.size(); i++) {
0109 if (indata[i].rank() != inputs[i].rank()) {
0110 throw cms::Exception("ErrorInPrivateVectorRank")
0111 << "Error in data: Discrepancy between Rank in file and input buffer!" << endl;
0112 checkIn = true;
0113 }
0114 if (indata[i].rctRegion() != inputs[i].rctRegion()) {
0115 throw cms::Exception("ErrorInRegion")
0116 << "Error in data:Discrepancy between region in file and input buffer!" << endl;
0117 checkIn = true;
0118 }
0119 if (indata[i].rctCard() != inputs[i].rctCard()) {
0120 throw cms::Exception("ErrorInCard")
0121 << "Error in data:Discrepancy between card in file and input buffer!" << endl;
0122 checkIn = true;
0123 }
0124 }
0125
0126
0127 testSort->process();
0128
0129
0130 outputs = testSort->getOutputCands();
0131 cout << "Output from sort algorithm: " << endl;
0132 print(outputs);
0133 for (unsigned int n = 0; n != outputs.size(); n++) {
0134 int count = 0;
0135 for (unsigned int i = 0; i != indata.size(); i++) {
0136 if (indata[i].rank() > outputs[n].rank()) {
0137 count = count + 1;
0138 if (n == 0 && count > 1) {
0139 cout << "Error in getOutput method, highest ranking electron candidate isn't returned" << endl;
0140 checkOut = true;
0141 }
0142 if (n == 1 && count > 2) {
0143 cout << "Error in getOutput method, 2nd highest ranking electron candidate isn't returned" << endl;
0144 checkOut = true;
0145 }
0146 if (n == 2 && count > 3) {
0147 cout << "Error in getOutput method, 3rd highest ranking electron candidate isn't returned" << endl;
0148 checkOut = true;
0149 }
0150 if (n == 3 && count > 4) {
0151 cout << "Error in getOutput method, 4th highest ranking electron candidate isn't returned" << endl;
0152 checkOut = true;
0153 }
0154 }
0155 }
0156 }
0157
0158 if (checkIn) {
0159 cout << "Error: Discrepancy between data read in and data stored as inputs!" << endl;
0160 }
0161 if (checkIn) {
0162 cout << "Error: Discrepancy between the sorted data and data outputted!" << endl;
0163 }
0164 if (nInputs) {
0165 cout << "The number of inputs/size of input vector in electron sorter is incorrect" << endl;
0166 }
0167 if (nOutputs) {
0168 cout << "The number of outputs/size of output vector in electron sorter is incorrect" << endl;
0169 }
0170 if (!nInputs && !nOutputs && !checkIn && !checkOut) {
0171 cout << "============================================" << endl;
0172 cout << " Class ElectronSorter has passed unit tester" << endl;
0173 cout << "============================================" << endl;
0174 }
0175
0176 WriteFileData(outputs);
0177 delete testSort;
0178
0179 } catch (cms::Exception& e) {
0180 if (e.category() == "ErrorOpenFile") {
0181 std::cout << "No input file - exiting" << std::endl;
0182 } else {
0183 cerr << e.what() << endl;
0184 }
0185 }
0186 return 0;
0187 }
0188
0189
0190 void LoadFileData(const string& inputFile, int elecs, bool iso) {
0191
0192 int bx = elecs / 4;
0193
0194
0195 file.open(inputFile.c_str(), ios::in);
0196
0197 if (!file) {
0198 throw cms::Exception("ErrorOpenFile") << "Cannot open input data file" << endl;
0199 }
0200
0201 unsigned candRank = 0, candRegion = 0, candCard = 0, candCrate = 0;
0202 short dummy;
0203 string bxNo = "poels";
0204 bool candIso = 0;
0205
0206
0207
0208
0209
0210 for (int n = 0; n < bx; n++) {
0211 file >> bxNo;
0212 file >> std::dec >> dummy;
0213 for (int i = 0; i < 58; i++) {
0214 if (i < 8) {
0215 if (i > 3 && iso) {
0216 file >> std::hex >> dummy;
0217 candRank = dummy & 0x3f;
0218 candRegion = (dummy >> 6) & 0x1;
0219 candCard = (dummy >> 7) & 0x7;
0220 candIso = 1;
0221 L1CaloEmCand electrons(candRank, candRegion, candCard, candCrate, candIso);
0222 indata.push_back(electrons);
0223 } else {
0224 if (i < 4 && !iso) {
0225 file >> std::hex >> dummy;
0226 candRank = dummy & 0x3f;
0227 candRegion = (dummy >> 6) & 0x1;
0228 candCard = (dummy >> 7) & 0x7;
0229 candIso = 0;
0230 L1CaloEmCand electrons(candRank, candRegion, candCard, candCrate, candIso);
0231 indata.push_back(electrons);
0232 } else {
0233 file >> dummy;
0234 }
0235 }
0236 } else {
0237 file >> std::hex >> dummy;
0238 }
0239 }
0240 }
0241 file.close();
0242 convertToGct(indata);
0243
0244 return;
0245 }
0246
0247
0248 void WriteFileData(EmOutputCandVec outputs) {
0249 EmOutputCandVec writeThis = outputs;
0250 ofile.open("sortOutput.txt", ios::out);
0251 for (unsigned int i = 0; i != writeThis.size(); i++) {
0252 ofile << std::hex << writeThis[i].rank();
0253 ofile << " ";
0254 ofile << std::hex << writeThis[i].etaIndex();
0255 ofile << " ";
0256 ofile << std::hex << writeThis[i].phiIndex();
0257 ofile << "\n";
0258 }
0259 return;
0260 }
0261
0262 void print(EmOutputCandVec candidates) {
0263 EmOutputCandVec cands = candidates;
0264 for (unsigned int i = 0; i != cands.size(); i++) {
0265 cout << " Rank: " << cands[i].rank() << " Eta: " << cands[i].etaIndex()
0266 << " Phi: " << cands[i].phiIndex() << endl;
0267 }
0268 return;
0269 }
0270
0271
0272 void convertToGct(EmInputCandVec candidates) {
0273 EmInputCandVec cand = candidates;
0274 for (unsigned int i = 0; i != cand.size(); i++) {
0275 unsigned rank = cand[i].rank();
0276 unsigned card = cand[i].rctCard();
0277 unsigned region = cand[i].rctRegion();
0278
0279
0280 bool isolation = cand[i].isolated();
0281 unsigned eta = 10;
0282 unsigned phi = 50;
0283
0284 switch (card) {
0285 case 0:
0286 phi = 1;
0287 if (region == 0) {
0288 eta = 0;
0289 } else {
0290 eta = 1;
0291 }
0292 break;
0293 case 1:
0294 phi = 1;
0295 if (region == 0) {
0296 eta = 2;
0297 } else {
0298 eta = 3;
0299 }
0300 break;
0301 case 2:
0302 phi = 1;
0303 if (region == 0) {
0304 eta = 4;
0305 } else {
0306 eta = 5;
0307 }
0308 [[fallthrough]];
0309 case 3:
0310 phi = 0;
0311 if (region == 0) {
0312 eta = 0;
0313 } else {
0314 eta = 1;
0315 }
0316 break;
0317 case 4:
0318 phi = 0;
0319 if (region == 0) {
0320 eta = 2;
0321 } else {
0322 eta = 3;
0323 }
0324 [[fallthrough]];
0325 case 5:
0326 phi = 0;
0327 if (region == 0) {
0328 eta = 4;
0329 } else {
0330 eta = 5;
0331 }
0332 break;
0333 case 6:
0334 if (region == 0) {
0335 eta = 6;
0336 phi = 1;
0337 } else {
0338 eta = 6;
0339 phi = 0;
0340 }
0341 break;
0342 }
0343 L1GctEmCand gctTemp(rank, phi, eta, isolation);
0344 gctData.push_back(gctTemp);
0345 }
0346 return;
0347 }