Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:47

0001 #include "L1Trigger/DTTriggerPhase2/interface/LateralityCoarsedProvider.h"
0002 #include <cmath>
0003 #include <memory>
0004 
0005 using namespace edm;
0006 using namespace std;
0007 using namespace cmsdt;
0008 // ============================================================================
0009 // Constructors and destructor
0010 // ============================================================================
0011 LateralityCoarsedProvider::LateralityCoarsedProvider(const ParameterSet &pset, edm::ConsumesCollector &iC)
0012     : LateralityProvider(pset, iC),
0013       debug_(pset.getUntrackedParameter<bool>("debug")),
0014       laterality_filename_(pset.getParameter<edm::FileInPath>("laterality_filename")) {
0015   if (debug_)
0016     LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: constructor";
0017 
0018   fill_lat_combinations();
0019 }
0020 
0021 LateralityCoarsedProvider::~LateralityCoarsedProvider() {
0022   if (debug_)
0023     LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: destructor";
0024 }
0025 
0026 // ============================================================================
0027 // Main methods (initialise, run, finish)
0028 // ============================================================================
0029 void LateralityCoarsedProvider::initialise(const edm::EventSetup &iEventSetup) {
0030   if (debug_)
0031     LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider::initialiase";
0032 }
0033 
0034 void LateralityCoarsedProvider::run(edm::Event &iEvent,
0035                                     const edm::EventSetup &iEventSetup,
0036                                     MuonPathPtrs &muonpaths,
0037                                     std::vector<lat_vector> &lateralities) {
0038   if (debug_)
0039     LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: run";
0040 
0041   // fit per SL (need to allow for multiple outputs for a single mpath)
0042   for (auto &muonpath : muonpaths) {
0043     analyze(muonpath, lateralities);
0044   }
0045 }
0046 
0047 void LateralityCoarsedProvider::finish() {
0048   if (debug_)
0049     LogDebug("LateralityCoarsedProvider") << "LateralityCoarsedProvider: finish";
0050 };
0051 
0052 //------------------------------------------------------------------
0053 //--- Metodos privados
0054 //------------------------------------------------------------------
0055 
0056 void LateralityCoarsedProvider::analyze(MuonPathPtr &inMPath, std::vector<lat_vector> &lateralities) {
0057   if (debug_)
0058     LogDebug("LateralityCoarsedProvider") << "DTp2:analyze \t\t\t\t starts";
0059 
0060   auto coarsified_times = coarsify_times(inMPath);
0061 
0062   for (auto &lat_combination : lat_combinations) {
0063     if (inMPath->missingLayer() == lat_combination.missing_layer &&
0064         inMPath->cellLayout()[0] == lat_combination.cellLayout[0] &&
0065         inMPath->cellLayout()[1] == lat_combination.cellLayout[1] &&
0066         inMPath->cellLayout()[2] == lat_combination.cellLayout[2] &&
0067         inMPath->cellLayout()[3] == lat_combination.cellLayout[3] &&
0068         coarsified_times[0] == lat_combination.coarsed_times[0] &&
0069         coarsified_times[1] == lat_combination.coarsed_times[1] &&
0070         coarsified_times[2] == lat_combination.coarsed_times[2] &&
0071         coarsified_times[3] == lat_combination.coarsed_times[3]) {
0072       lateralities.push_back(lat_combination.latcombs);
0073       return;
0074     }
0075   }
0076   lateralities.push_back(LAT_VECTOR_NULL);
0077   return;
0078 }
0079 
0080 std::vector<short> LateralityCoarsedProvider::coarsify_times(MuonPathPtr &inMPath) {
0081   int max_time = -999;
0082   // obtain the maximum time to do the coarsification
0083   for (int layer = 0; layer < cmsdt::NUM_LAYERS; layer++) {
0084     if (inMPath->missingLayer() == layer)
0085       continue;
0086     if (inMPath->primitive(layer)->tdcTimeStamp() > max_time)
0087       max_time = inMPath->primitive(layer)->tdcTimeStamp();
0088   }
0089 
0090   // do the coarsification
0091   std::vector<short> coarsified_times;
0092   for (int layer = 0; layer < cmsdt::NUM_LAYERS; layer++) {
0093     if (inMPath->missingLayer() == layer) {
0094       coarsified_times.push_back(-1);
0095       continue;
0096     }
0097     auto coarsified_time = max_time - inMPath->primitive(layer)->tdcTimeStamp();
0098     // transform into tdc counts
0099     coarsified_time = (int)round(((float)TIME_TO_TDC_COUNTS / (float)LHC_CLK_FREQ) * coarsified_time);
0100     // keep the LAT_MSB_BITS
0101     coarsified_time = coarsified_time >> (LAT_TOTAL_BITS - LAT_MSB_BITS);
0102 
0103     if (inMPath->missingLayer() == -1) {  // 4-hit candidates
0104       if (coarsified_time <= LAT_P0_4H)
0105         coarsified_times.push_back(0);
0106       else if (coarsified_time <= LAT_P1_4H)
0107         coarsified_times.push_back(1);
0108       else if (coarsified_time <= LAT_P2_4H)
0109         coarsified_times.push_back(2);
0110       else
0111         coarsified_times.push_back(3);
0112     } else {  // 3-hit candidates
0113       if (coarsified_time <= LAT_P0_3H)
0114         coarsified_times.push_back(0);
0115       else if (coarsified_time <= LAT_P1_3H)
0116         coarsified_times.push_back(1);
0117       else if (coarsified_time <= LAT_P2_3H)
0118         coarsified_times.push_back(2);
0119       else
0120         coarsified_times.push_back(3);
0121     }
0122   }
0123   return coarsified_times;
0124 }
0125 
0126 void LateralityCoarsedProvider::fill_lat_combinations() {
0127   std::ifstream latFile(laterality_filename_.fullPath());  // Open file
0128   if (latFile.fail()) {
0129     throw cms::Exception("Missing Input File")
0130         << "LateralityCoarsedProvider::fill_lat_combinations() -  Cannot find " << laterality_filename_.fullPath();
0131     return;
0132   }
0133 
0134   std::string line;
0135 
0136   short line_counter = 0;  // Line counter
0137 
0138   // Bit masks for every parameter
0139   int _12bitMask = 0xFFF;   // 12 bits
0140   int _layoutMask = 0xE00;  // 3 bits
0141   int _is4HitMask = 0x100;  // 1 bit
0142   int _coarsedMask = 0xFF;  // 8 bits
0143   int _layerMask = 0xC0;    // 2 bits
0144 
0145   while (std::getline(latFile, line)) {
0146     if (line == "000000000000") {
0147       line_counter++;
0148       continue;
0149     }  //skip zeros
0150 
0151     if (line.size() == 12) {
0152       std::vector<std::vector<short>> transformedVector = convertString(line);
0153       latcomb lat0 = {
0154           transformedVector[0][0], transformedVector[0][1], transformedVector[0][2], transformedVector[0][3]};
0155       latcomb lat1 = {
0156           transformedVector[1][0], transformedVector[1][1], transformedVector[1][2], transformedVector[1][3]};
0157       latcomb lat2 = {
0158           transformedVector[2][0], transformedVector[2][1], transformedVector[2][2], transformedVector[2][3]};
0159 
0160       //Transforming line number to binary
0161       short address = line_counter & _12bitMask;  // 12 bits
0162 
0163       short layout =
0164           (address & _layoutMask) >> 9;  //Doing AND and displacing 9 bits to the right to obtain 3 bits of layout
0165       short is4Hit = (address & _is4HitMask) >> 8;
0166       short coarsed = address & _coarsedMask;
0167 
0168       short bit1Layout = (layout & (1));
0169       short bit2Layout = (layout & (1 << 1)) >> 1;
0170       short bit3Layout = (layout & (1 << 2)) >> 2;
0171 
0172       //Logic implementation
0173       short missingLayer = -1;
0174       short layout_comb[NUM_LAYERS] = {bit3Layout, bit2Layout, bit1Layout, -1};
0175       short coarsedTimes[NUM_LAYERS] = {0, 0, 0, 0};
0176 
0177       if (is4Hit != 1) {  //3 hit case
0178         missingLayer =
0179             (coarsed & _layerMask) >> 6;  //Missing layer is given by the two most significative bits of coarsed vector
0180         coarsedTimes[missingLayer] = -1;  //Missing layer set to -1
0181       }
0182 
0183       // Filling coarsedTimes vector without the missing layer
0184       if (missingLayer != -1) {
0185         switch (missingLayer) {
0186           case 0:
0187             coarsedTimes[1] = (coarsed & 0x30) >> 4;
0188             coarsedTimes[2] = (coarsed & 0x0C) >> 2;
0189             coarsedTimes[3] = coarsed & 0x03;
0190             lat0 = {-1, transformedVector[0][1], transformedVector[0][2], transformedVector[0][3]};
0191             lat1 = {-1, transformedVector[1][1], transformedVector[1][2], transformedVector[1][3]};
0192             lat2 = {-1, transformedVector[2][1], transformedVector[2][2], transformedVector[2][3]};
0193             break;
0194           case 1:
0195             coarsedTimes[0] = (coarsed & 0x30) >> 4;
0196             coarsedTimes[2] = (coarsed & 0x0C) >> 2;
0197             coarsedTimes[3] = coarsed & 0x03;
0198             lat0 = {transformedVector[0][0], -1, transformedVector[0][2], transformedVector[0][3]};
0199             lat1 = {transformedVector[1][0], -1, transformedVector[1][2], transformedVector[1][3]};
0200             lat2 = {transformedVector[2][0], -1, transformedVector[2][2], transformedVector[2][3]};
0201             break;
0202           case 2:
0203             coarsedTimes[0] = (coarsed & 0x30) >> 4;
0204             coarsedTimes[1] = (coarsed & 0x0C) >> 2;
0205             coarsedTimes[3] = coarsed & 0x03;
0206             lat0 = {transformedVector[0][0], transformedVector[0][1], -1, transformedVector[0][3]};
0207             lat1 = {transformedVector[1][0], transformedVector[1][1], -1, transformedVector[1][3]};
0208             lat2 = {transformedVector[2][0], transformedVector[2][1], -1, transformedVector[2][3]};
0209             break;
0210           case 3:
0211             coarsedTimes[0] = (coarsed & 0x30) >> 4;
0212             coarsedTimes[1] = (coarsed & 0x0C) >> 2;
0213             coarsedTimes[2] = coarsed & 0x03;
0214             lat0 = {transformedVector[0][0], transformedVector[0][1], transformedVector[0][2], -1};
0215             lat1 = {transformedVector[1][0], transformedVector[1][1], transformedVector[1][2], -1};
0216             lat2 = {transformedVector[2][0], transformedVector[2][1], transformedVector[2][2], -1};
0217             break;
0218 
0219           default:
0220             break;
0221         }
0222 
0223       } else {  //4 hit case
0224         coarsedTimes[0] = (coarsed & 0xC0) >> 6;
0225         coarsedTimes[1] = (coarsed & 0x30) >> 4;
0226         coarsedTimes[2] = (coarsed & 0x0C) >> 2;
0227         coarsedTimes[3] = coarsed & 0x03;
0228       }
0229 
0230       lat_coarsed_combination lat_temp = {missingLayer,
0231                                           {layout_comb[0], layout_comb[1], layout_comb[2], layout_comb[3]},
0232                                           {coarsedTimes[0], coarsedTimes[1], coarsedTimes[2], coarsedTimes[3]},
0233                                           {lat0, lat1, lat2}};
0234       lat_combinations.push_back(lat_temp);
0235 
0236     } else {  //size different from 12
0237       std::cerr << "Error: line " << line_counter << " does not contain 12 bits." << std::endl;
0238     }
0239     line_counter++;
0240   };
0241 
0242   //closing lateralities file
0243   latFile.close();
0244 };
0245 
0246 // Function to convert a 12 bit string in a a vector of 4 bit vectors
0247 std::vector<std::vector<short>> LateralityCoarsedProvider::convertString(std::string chain) {
0248   std::vector<std::vector<short>> result;
0249 
0250   for (size_t i = 0; i < chain.size(); i += 4) {
0251     std::vector<short> group;
0252     for (size_t j = 0; j < 4; j++) {
0253       group.push_back(chain[i + j] - '0');  // Convert the character to integer
0254     }
0255     result.push_back(group);
0256   }
0257 
0258   return result;
0259 }