Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:21:35

0001 #include "L1Trigger/RegionalCaloTrigger/interface/L1RCTProducer.h"
0002 
0003 // RunInfo stuff
0004 #include "CondFormats/RunInfo/interface/RunInfo.h"
0005 #include "FWCore/Framework/interface/LuminosityBlock.h"
0006 #include "FWCore/Framework/interface/Run.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 #include <vector>
0010 using std::vector;
0011 #include <iostream>
0012 
0013 using std::cout;
0014 using std::endl;
0015 namespace {
0016   constexpr int crateFED[18][6] = {{613, 614, 603, 702, 718, 1118},
0017                                    {611, 612, 602, 700, 718, 1118},
0018                                    {627, 610, 601, 716, 722, 1122},
0019                                    {625, 626, 609, 714, 722, 1122},
0020                                    {623, 624, 608, 712, 722, 1122},
0021                                    {621, 622, 607, 710, 720, 1120},
0022                                    {619, 620, 606, 708, 720, 1120},
0023                                    {617, 618, 605, 706, 720, 1120},
0024                                    {615, 616, 604, 704, 718, 1118},
0025                                    {631, 632, 648, 703, 719, 1118},
0026                                    {629, 630, 647, 701, 719, 1118},
0027                                    {645, 628, 646, 717, 723, 1122},
0028                                    {643, 644, 654, 715, 723, 1122},
0029                                    {641, 642, 653, 713, 723, 1122},
0030                                    {639, 640, 652, 711, 721, 1120},
0031                                    {637, 638, 651, 709, 721, 1120},
0032                                    {635, 636, 650, 707, 721, 1120},
0033                                    {633, 634, 649, 705, 719, 1118}};
0034 }
0035 L1RCTProducer::L1RCTProducer(const edm::ParameterSet &conf)
0036     : rctLookupTables(new L1RCTLookupTables),
0037       rct(new L1RCT(rctLookupTables.get())),
0038       useEcal(conf.getParameter<bool>("useEcal")),
0039       useHcal(conf.getParameter<bool>("useHcal")),
0040       ecalDigis(conf.getParameter<std::vector<edm::InputTag>>("ecalDigis")),
0041       hcalDigis(conf.getParameter<std::vector<edm::InputTag>>("hcalDigis")),
0042       bunchCrossings(conf.getParameter<std::vector<int>>("BunchCrossings")),
0043       getFedsFromOmds(conf.getParameter<bool>("getFedsFromOmds")),
0044       queryDelayInLS(conf.getParameter<unsigned int>("queryDelayInLS")),
0045       queryIntervalInLS(conf.getParameter<unsigned int>("queryIntervalInLS")),
0046       conditionsLabel(conf.getParameter<std::string>("conditionsLabel")),
0047       fedUpdatedMask(nullptr),
0048 
0049       rctParamsToken_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", conditionsLabel))),
0050       emScaleToken_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", conditionsLabel))),
0051       ecalScaleToken_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", conditionsLabel))),
0052       hcalScaleToken_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", conditionsLabel))),
0053 
0054       beginRunRunInfoToken_(esConsumes<edm::Transition::BeginRun>()),
0055 
0056       beginRunChannelMaskToken_(esConsumes<edm::Transition::BeginRun>()),
0057       beginRunHotChannelMaskToken_(esConsumes<edm::Transition::BeginRun>()) {
0058   produces<L1CaloEmCollection>();
0059   produces<L1CaloRegionCollection>();
0060 
0061   if (getFedsFromOmds) {
0062     beginLumiChannelMaskToken_ = esConsumes<edm::Transition::BeginLuminosityBlock>();
0063     beginLumiHotChannelMaskToken_ = esConsumes<edm::Transition::BeginLuminosityBlock>();
0064     beginLumiRunInfoToken_ = esConsumes<edm::Transition::BeginLuminosityBlock>();
0065     omdsRunInfoToken_ = esConsumes<edm::Transition::BeginLuminosityBlock>(edm::ESInputTag("", "OmdsFedVector"));
0066   }
0067 
0068   for (unsigned int ihc = 0; ihc < hcalDigis.size(); ihc++) {
0069     consumes<edm::SortedCollection<HcalTriggerPrimitiveDigi, edm::StrictWeakOrdering<HcalTriggerPrimitiveDigi>>>(
0070         hcalDigis[ihc]);
0071   }
0072 
0073   for (unsigned int iec = 0; iec < ecalDigis.size(); iec++) {
0074     consumes<edm::SortedCollection<EcalTriggerPrimitiveDigi, edm::StrictWeakOrdering<EcalTriggerPrimitiveDigi>>>(
0075         ecalDigis[iec]);
0076   }
0077 }
0078 
0079 void L1RCTProducer::beginRun(edm::Run const &run, const edm::EventSetup &eventSetup) {
0080   //  std::cout << "getFedsFromOmds is " << getFedsFromOmds << std::endl;
0081 
0082   updateConfiguration(eventSetup);
0083 
0084   // list of RCT channels to mask
0085   L1RCTChannelMask const &channelMask = eventSetup.getData(beginRunChannelMaskToken_);
0086 
0087   // list of Noisy RCT channels to mask
0088   L1RCTNoisyChannelMask const &hotChannelMask = eventSetup.getData(beginRunHotChannelMaskToken_);
0089 
0090   updateFedVector(channelMask, hotChannelMask, getFedVectorFromRunInfo(beginRunRunInfoToken_, eventSetup));
0091 }
0092 
0093 void L1RCTProducer::beginLuminosityBlock(edm::LuminosityBlock const &lumiSeg, const edm::EventSetup &context) {
0094   // check LS number every LS, if the checkOMDS flag is set AND it's the right
0095   // LS, update the FED vector from OMDS can pass the flag as the bool??  but
0096   // only check LS number if flag is true anyhow
0097   if (getFedsFromOmds) {
0098     throw cms::Exception("L1RCTProducer Configuration")
0099         << "L1RCTProducer is being run with the configuration parameter getFedsFromOmds set true. "
0100         << "Underlying Framework changes have broken the implementation of that option. "
0101         << "It was not fixed because we believe this option is no longer used or needed. "
0102         << "If you actually need this option, please report this failure to the Framework. "
0103         << "For more details see GitHub Issue 43697.";
0104     /*
0105     unsigned int nLumi = lumiSeg.luminosityBlock();  // doesn't even need the (unsigned int) cast
0106                                                      // because LuminosityBlockNumber_t is already
0107                                                      // an unsigned int
0108     // LS count starts at 1, want to be able to delay 0 LS's intuitively
0109     if (((nLumi - 1) == queryDelayInLS) ||
0110         (queryIntervalInLS > 0 &&
0111          nLumi % queryIntervalInLS == 0))  // to guard against problems if online DQM crashes; every 100
0112                                            // LS is ~20-30 minutes, not too big a load, hopefully not too
0113                                            // long between
0114     {
0115       //      std::cout << "Lumi section for this FED vector update is " <<
0116       // nLumi << std::endl;
0117 
0118       // list of RCT channels to mask
0119       L1RCTChannelMask const &channelMask = context.getData(beginLumiChannelMaskToken_);
0120 
0121       // list of Noisy RCT channels to mask
0122       L1RCTNoisyChannelMask const &hotChannelMask = context.getData(beginLumiHotChannelMaskToken_);
0123 
0124       updateFedVector(channelMask, hotChannelMask, getFedVectorFromOmds(context));
0125     } else if (queryIntervalInLS <= 0) {
0126       // don't do interval checking... cout message??
0127     }
0128     */
0129   }
0130 }
0131 
0132 void L1RCTProducer::updateConfiguration(const edm::EventSetup &eventSetup) {
0133   // Refresh configuration information every event
0134   // Hopefully, this does not take too much time
0135   // There should be a call back function in future to
0136   // handle changes in configuration
0137   // parameters to configure RCT (thresholds, etc)
0138   const L1RCTParameters *r = &eventSetup.getData(rctParamsToken_);
0139 
0140   // SCALES
0141 
0142   // energy scale to convert eGamma output
0143   const L1CaloEtScale *s = &eventSetup.getData(emScaleToken_);
0144 
0145   // get energy scale to convert input from ECAL
0146   const L1CaloEcalScale *e = &eventSetup.getData(ecalScaleToken_);
0147 
0148   // get energy scale to convert input from HCAL
0149   const L1CaloHcalScale *h = &eventSetup.getData(hcalScaleToken_);
0150 
0151   // set scales
0152   rctLookupTables->setEcalScale(e);
0153   rctLookupTables->setHcalScale(h);
0154 
0155   rctLookupTables->setRCTParameters(r);
0156   rctLookupTables->setL1CaloEtScale(s);
0157 }
0158 
0159 void L1RCTProducer::updateFedVector(const L1RCTChannelMask &channelMask,
0160                                     const L1RCTNoisyChannelMask &hotChannelMask,
0161                                     const std::vector<int> &Feds)
0162 // http://cmslxr.fnal.gov/lxr/source/FWCore/Framework/interface/EventSetup.h
0163 {
0164   rctLookupTables->setNoisyChannelMask(&hotChannelMask);
0165 
0166   // Update the channel mask according to the FED VECTOR
0167   // This is the beginning of run. We delete the old
0168   // create the new and set it in the LUTs
0169 
0170   fedUpdatedMask = std::make_unique<L1RCTChannelMask>();
0171   // copy a constant object
0172   for (int i = 0; i < 18; i++) {
0173     for (int j = 0; j < 2; j++) {
0174       for (int k = 0; k < 28; k++) {
0175         fedUpdatedMask->ecalMask[i][j][k] = channelMask.ecalMask[i][j][k];
0176         fedUpdatedMask->hcalMask[i][j][k] = channelMask.hcalMask[i][j][k];
0177       }
0178       for (int k = 0; k < 4; k++) {
0179         fedUpdatedMask->hfMask[i][j][k] = channelMask.hfMask[i][j][k];
0180       }
0181     }
0182   }
0183 
0184   // so can create/initialize/assign const quantity in one line accounting for
0185   // if statement wikipedia says this is exactly what it's for:
0186   // http://en.wikipedia.org/wiki/%3F:#C.2B.2B
0187 
0188   //   std::cout << "Contents of ";
0189   //   std::cout << (getFromOmds ? "OMDS RunInfo" : "standard RunInfo");
0190   //   std::cout << " FED vector" << std::endl;
0191   //   printFedVector(Feds);
0192 
0193   bool useUpgradedHF = false;
0194 
0195   std::vector<int> caloFeds;  // pare down the feds to the interesting ones
0196   // is this unneccesary?
0197   // Mike B : This will decrease the find speed so better do it
0198   for (std::vector<int>::const_iterator cf = Feds.begin(); cf != Feds.end(); ++cf) {
0199     int fedNum = *cf;
0200     if ((fedNum > 600 && fedNum < 724) || fedNum == 1118 || fedNum == 1120 || fedNum == 1122)
0201       caloFeds.push_back(fedNum);
0202 
0203     if (fedNum == 1118 || fedNum == 1120 || fedNum == 1122)
0204       useUpgradedHF = true;
0205   }
0206 
0207   for (int cr = 0; cr < 18; ++cr) {
0208     for (crateSection cs = c_min; cs <= c_max; cs = crateSection(cs + 1)) {
0209       bool fedFound = false;
0210 
0211       // Try to find the FED
0212       std::vector<int>::iterator fv = std::find(caloFeds.begin(), caloFeds.end(), crateFED[cr][cs]);
0213       if (fv != caloFeds.end())
0214         fedFound = true;
0215 
0216       if (!fedFound) {
0217         int eta_min = 0;
0218         int eta_max = 0;
0219         bool phi_even[2] = {false};  //, phi_odd = false;
0220         bool ecal = false;
0221 
0222         switch (cs) {
0223           case ebEvenFed:
0224             eta_min = minBarrel;
0225             eta_max = maxBarrel;
0226             phi_even[0] = true;
0227             ecal = true;
0228             break;
0229 
0230           case ebOddFed:
0231             eta_min = minBarrel;
0232             eta_max = maxBarrel;
0233             phi_even[1] = true;
0234             ecal = true;
0235             break;
0236 
0237           case eeFed:
0238             eta_min = minEndcap;
0239             eta_max = maxEndcap;
0240             phi_even[0] = true;
0241             phi_even[1] = true;
0242             ecal = true;
0243             break;
0244 
0245           case hbheFed:
0246             eta_min = minBarrel;
0247             eta_max = maxEndcap;
0248             phi_even[0] = true;
0249             phi_even[1] = true;
0250             ecal = false;
0251             break;
0252 
0253           case hfFed:
0254             if (useUpgradedHF)
0255               break;
0256 
0257             eta_min = minHF;
0258             eta_max = maxHF;
0259 
0260             phi_even[0] = true;
0261             phi_even[1] = true;
0262             ecal = false;
0263             break;
0264 
0265           case hfFedUp:
0266             if (!useUpgradedHF)
0267               break;
0268 
0269             eta_min = minHF;
0270             eta_max = maxHF;
0271 
0272             phi_even[0] = true;
0273             phi_even[1] = true;
0274             ecal = false;
0275             break;
0276 
0277           default:
0278             break;
0279         }
0280         for (int ieta = eta_min; ieta <= eta_max; ++ieta) {
0281           if (ieta <= 28)  // barrel and endcap
0282             for (int even = 0; even <= 1; even++) {
0283               if (phi_even[even]) {
0284                 if (ecal)
0285                   fedUpdatedMask->ecalMask[cr][even][ieta - 1] = true;
0286                 else
0287                   fedUpdatedMask->hcalMask[cr][even][ieta - 1] = true;
0288               }
0289             }
0290           else
0291             for (int even = 0; even <= 1; even++)
0292               if (phi_even[even])
0293                 fedUpdatedMask->hfMask[cr][even][ieta - 29] = true;
0294         }
0295       }
0296     }
0297   }
0298 
0299   rctLookupTables->setChannelMask(fedUpdatedMask.get());
0300 }
0301 
0302 const std::vector<int> L1RCTProducer::getFedVectorFromRunInfo(const edm::ESGetToken<RunInfo, RunInfoRcd> &token,
0303                                                               const edm::EventSetup &eventSetup) const {
0304   //  std::cout << "Getting FED vector from standard RunInfo object" <<
0305   //  std::endl;
0306   // get FULL FED vector from RUNINFO
0307   return eventSetup.getData(token).m_fed_in;
0308 }
0309 
0310 const std::vector<int> L1RCTProducer::getFedVectorFromOmds(const edm::EventSetup &eventSetup) const {
0311   //  std::cout << "Getting FED vector from my specific ES RunInfo object" <<
0312   //  std::endl;
0313 
0314   // get FULL FED vector from RunInfo object specifically created to have OMDS
0315   // fed vector
0316   edm::ESHandle<RunInfo> sum = eventSetup.getHandle(omdsRunInfoToken_);
0317   if (sum.isValid()) {
0318     return sum->m_fed_in;
0319   } else {
0320     return getFedVectorFromRunInfo(beginLumiRunInfoToken_, eventSetup);
0321   }
0322 }
0323 
0324 void L1RCTProducer::produce(edm::Event &event, const edm::EventSetup &eventSetup) {
0325   std::unique_ptr<L1CaloEmCollection> rctEmCands(new L1CaloEmCollection);
0326   std::unique_ptr<L1CaloRegionCollection> rctRegions(new L1CaloRegionCollection);
0327 
0328   if (!(ecalDigis.size() == hcalDigis.size() && hcalDigis.size() == bunchCrossings.size()))
0329     throw cms::Exception("BadInput") << "From what I see the number of your your ECAL input digi "
0330                                         "collections.\n"
0331                                      << "is different from the size of your HCAL digi input collections\n"
0332                                      << "or the size of your BX factor collection"
0333                                      << "They must be the same to correspond to the same Bxs\n"
0334                                      << "It does not matter if one of them is empty\n";
0335 
0336   // loop through and process each bx
0337   for (unsigned short sample = 0; sample < bunchCrossings.size(); sample++) {
0338     edm::Handle<EcalTrigPrimDigiCollection> ecal;
0339     edm::Handle<HcalTrigPrimDigiCollection> hcal;
0340 
0341     EcalTrigPrimDigiCollection ecalIn;
0342     HcalTrigPrimDigiCollection hcalIn;
0343 
0344     if (useHcal && event.getByLabel(hcalDigis[sample], hcal))
0345       hcalIn = *hcal;
0346 
0347     if (useEcal && event.getByLabel(ecalDigis[sample], ecal))
0348       ecalIn = *ecal;
0349 
0350     rct->digiInput(ecalIn, hcalIn);
0351     rct->processEvent();
0352 
0353     // Stuff to create
0354     for (int j = 0; j < 18; j++) {
0355       L1CaloEmCollection isolatedEGObjects = rct->getIsolatedEGObjects(j);
0356       L1CaloEmCollection nonisolatedEGObjects = rct->getNonisolatedEGObjects(j);
0357       for (int i = 0; i < 4; i++) {
0358         isolatedEGObjects.at(i).setBx(bunchCrossings[sample]);
0359         nonisolatedEGObjects.at(i).setBx(bunchCrossings[sample]);
0360         rctEmCands->push_back(isolatedEGObjects.at(i));
0361         rctEmCands->push_back(nonisolatedEGObjects.at(i));
0362       }
0363     }
0364 
0365     for (int i = 0; i < 18; i++) {
0366       std::vector<L1CaloRegion> regions = rct->getRegions(i);
0367       for (int j = 0; j < 22; j++) {
0368         regions.at(j).setBx(bunchCrossings[sample]);
0369         rctRegions->push_back(regions.at(j));
0370       }
0371     }
0372   }
0373 
0374   // putting stuff back into event
0375   event.put(std::move(rctEmCands));
0376   event.put(std::move(rctRegions));
0377 }
0378 
0379 // print contents of (FULL) FED vector
0380 void L1RCTProducer::printFedVector(const std::vector<int> &fedVector) {
0381   std::cout << "Contents of given fedVector: ";
0382   std::copy(fedVector.begin(), fedVector.end(), std::ostream_iterator<int>(std::cout, ", "));
0383   std::cout << std::endl;
0384 }
0385 
0386 // print contents of RCT channel mask fedUpdatedMask
0387 void L1RCTProducer::printUpdatedFedMask() {
0388   if (fedUpdatedMask != nullptr) {
0389     fedUpdatedMask->print(std::cout);
0390   } else {
0391     std::cout << "Trying to print contents of fedUpdatedMask, but it doesn't exist!" << std::endl;
0392   }
0393 }
0394 
0395 // print contents of RCT channel mask fedUpdatedMask
0396 void L1RCTProducer::printUpdatedFedMaskVerbose() {
0397   if (fedUpdatedMask != nullptr) {
0398     // print contents of fedvector
0399     std::cout << "Contents of fedUpdatedMask: ";
0400     //       std::copy(fedUpdatedMask.begin(), fedUpdatedMask.end(),
0401     //       std::ostream_iterator<int>(std::cout, ", "));
0402     std::cout << "--> ECAL mask: " << std::endl;
0403     for (int i = 0; i < 18; i++) {
0404       for (int j = 0; j < 2; j++) {
0405         for (int k = 0; k < 28; k++) {
0406           std::cout << fedUpdatedMask->ecalMask[i][j][k] << ", ";
0407         }
0408       }
0409     }
0410     std::cout << "--> HCAL mask: " << std::endl;
0411     for (int i = 0; i < 18; i++) {
0412       for (int j = 0; j < 2; j++) {
0413         for (int k = 0; k < 28; k++) {
0414           std::cout << fedUpdatedMask->hcalMask[i][j][k] << ", ";
0415         }
0416       }
0417     }
0418     std::cout << "--> HF mask: " << std::endl;
0419     for (int i = 0; i < 18; i++) {
0420       for (int j = 0; j < 2; j++) {
0421         for (int k = 0; k < 4; k++) {
0422           std::cout << fedUpdatedMask->hfMask[i][j][k] << ", ";
0423         }
0424       }
0425     }
0426 
0427     std::cout << std::endl;
0428   } else {
0429     // print error message
0430     std::cout << "Trying to print contents of fedUpdatedMask, but it doesn't exist!" << std::endl;
0431   }
0432 }