Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-11-21 00:29:08

0001 #ifndef HcalSimAlgos_HcalTriggerPrimitiveAlgo_h
0002 #define HcalSimAlgos_HcalTriggerPrimitiveAlgo_h
0003 
0004 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
0005 #include "DataFormats/HcalDigi/interface/HcalDigiCollections.h"
0006 
0007 #include "CalibCalorimetry/HcalTPGAlgos/interface/HcaluLUTTPGCoder.h"
0008 #include "CalibFormats/CaloTPG/interface/HcalTPGCompressor.h"
0009 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
0010 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
0011 
0012 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0013 
0014 #include "Geometry/HcalTowerAlgo/interface/HcalTrigTowerGeometry.h"
0015 
0016 #include "SimCalorimetry/HcalTrigPrimAlgos/interface/HcalFeatureHFEMBit.h"
0017 #include "SimCalorimetry/HcalTrigPrimAlgos/interface/HcalFinegrainBit.h"
0018 
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 
0021 #include <map>
0022 #include <vector>
0023 
0024 class CaloGeometry;
0025 class IntegerCaloSamples;
0026 
0027 class HcalTriggerPrimitiveAlgo {
0028 public:
0029   HcalTriggerPrimitiveAlgo(bool pf,
0030                            const std::vector<double>& w,
0031                            int latency,
0032                            uint32_t FG_threshold,
0033                            const std::vector<uint32_t>& FG_HF_thresholds,
0034                            uint32_t ZS_threshold,
0035                            int numberOfSamples,
0036                            int numberOfPresamples,
0037                            int numberOfFilterPresamplesHBQIE11,
0038                            int numberOfFilterPresamplesHEQIE11,
0039                            int numberOfSamplesHF,
0040                            int numberOfPresamplesHF,
0041                            int numberOfSamplesZDC,
0042                            int numberOfPresamplesZDC,
0043                            bool useTDCInMinBiasBits,
0044                            uint32_t minSignalThreshold = 0,
0045                            uint32_t PMT_NoiseThreshold = 0);
0046   ~HcalTriggerPrimitiveAlgo();
0047 
0048   template <typename... Digis>
0049   void run(const HcalTPGCoder* incoder,
0050            const HcalTPGCompressor* outcoder,
0051            const HcalDbService* conditions,
0052            HcalTrigPrimDigiCollection& result,
0053            const HcalTrigTowerGeometry* trigTowerGeometry,
0054            float rctlsb,
0055            const HcalFeatureBit* LongvrsShortCut,
0056            const Digis&... digis);
0057 
0058   template <typename T, typename... Args>
0059   void addDigis(const T& collection, const Args&... digis) {
0060     addDigis(collection);
0061     addDigis(digis...);
0062   };
0063 
0064   template <typename T>
0065   void addDigis(const T& collection) {
0066     for (const auto& digi : collection) {
0067       addSignal(digi);
0068     }
0069   };
0070 
0071   template <typename D>
0072   void addDigis(const HcalDataFrameContainer<D>& collection) {
0073     for (auto i = collection.begin(); i != collection.end(); ++i) {
0074       D digi(*i);
0075       addSignal(digi);
0076     }
0077   };
0078 
0079   void runZS(HcalTrigPrimDigiCollection& tp);
0080   void runFEFormatError(const FEDRawDataCollection* rawraw,
0081                         const HcalElectronicsMap* emap,
0082                         HcalTrigPrimDigiCollection& result);
0083   void setPeakFinderAlgorithm(int algo);
0084   void setWeightsQIE11(const edm::ParameterSet& weightsQIE11);
0085   void setWeightQIE11(int aieta, int weight);
0086   void setNCTScaleShift(int);
0087   void setRCTScaleShift(int);
0088 
0089   void setNumFilterPresamplesHBQIE11(int presamples) { numberOfFilterPresamplesHBQIE11_ = presamples; }
0090 
0091   void setNumFilterPresamplesHEQIE11(int presamples) { numberOfFilterPresamplesHEQIE11_ = presamples; }
0092 
0093   void setUpgradeFlags(bool hb, bool he, bool hf);
0094   void setFixSaturationFlag(bool fix_saturation);
0095   void overrideParameters(const edm::ParameterSet& ps);
0096 
0097 private:
0098   /// adds the signal to the map
0099   void addSignal(const HBHEDataFrame& frame);
0100   void addSignal(const HFDataFrame& frame);
0101   //void addSignal(const ZDCDataFrame& frame);
0102   void addSignal(const QIE10DataFrame& frame);
0103   void addSignal(const QIE11DataFrame& frame);
0104   void addSignal(const IntegerCaloSamples& samples);
0105   void addFG(const HcalTrigTowerDetId& id, std::vector<bool>& msb);
0106   void addUpgradeFG(const HcalTrigTowerDetId& id, int depth, const std::vector<std::bitset<2>>& bits);
0107   void addUpgradeTDCFG(const HcalTrigTowerDetId& id, const QIE11DataFrame& frame);
0108 
0109   bool passTDC(const QIE10DataFrame& digi, int ts) const;
0110   bool validUpgradeFG(const HcalTrigTowerDetId& id, int depth) const;
0111   bool validChannel(const QIE10DataFrame& digi, int ts) const;
0112   bool needLegacyFG(const HcalTrigTowerDetId& id) const;
0113   bool needUpgradeID(const HcalTrigTowerDetId& id, int depth) const;
0114 
0115   /// adds the actual digis
0116   void analyze(IntegerCaloSamples& samples, HcalTriggerPrimitiveDigi& result);
0117   // 2017 and later: QIE11
0118   void analyzeQIE11(IntegerCaloSamples& samples,
0119                     std::vector<bool> sample_saturation,
0120                     HcalTriggerPrimitiveDigi& result,
0121                     const HcalFinegrainBit& fg_algo);
0122   // Version 0: RCT
0123   void analyzeHF(IntegerCaloSamples& samples, HcalTriggerPrimitiveDigi& result, const int hf_lumi_shift);
0124   // Version 1: 1x1
0125   void analyzeHF2016(const IntegerCaloSamples& SAMPLES,
0126                      HcalTriggerPrimitiveDigi& result,
0127                      const int HF_LUMI_SHIFT,
0128                      const HcalFeatureBit* HCALFEM);
0129   // With dual anode readout
0130   void analyzeHFQIE10(const IntegerCaloSamples& SAMPLES,
0131                       HcalTriggerPrimitiveDigi& result,
0132                       const int HF_LUMI_SHIFT,
0133                       const HcalFeatureBit* HCALFEM);
0134 
0135   void analyzeZDC(IntegerCaloSamples& samples, HcalTriggerPrimitiveDigi& result);
0136 
0137   // Member initialized by constructor
0138   const HcaluLUTTPGCoder* incoder_;
0139   const HcalTPGCompressor* outcoder_;
0140   const HcalDbService* conditions_;
0141   double theThreshold;
0142   bool peakfind_;
0143   std::vector<double> weights_;
0144   std::array<std::array<int, 2>, 29> weightsQIE11_;
0145   int latency_;
0146   uint32_t FG_threshold_;
0147   std::vector<uint32_t> FG_HF_thresholds_;
0148   uint32_t ZS_threshold_;
0149   int ZS_threshold_I_;
0150   int numberOfSamples_;
0151   int numberOfPresamples_;
0152   int numberOfFilterPresamplesHBQIE11_;
0153   int numberOfFilterPresamplesHEQIE11_;
0154   int numberOfSamplesHF_;
0155   int numberOfPresamplesHF_;
0156   int numberOfSamplesZDC_;
0157   int numberOfPresamplesZDC_;
0158   bool useTDCInMinBiasBits_;
0159   uint32_t minSignalThreshold_;
0160   uint32_t PMT_NoiseThreshold_;
0161   int NCTScaleShift;
0162   int RCTScaleShift;
0163 
0164   // Algo1: isPeak = TS[i-1] < TS[i] && TS[i] >= TS[i+1]
0165   // Algo2: isPeak = TSS[i-1] < TSS[i] && TSS[i] >= TSS[i+1],
0166   // TSS[i] = TS[i] + TS[i+1]
0167   // Default: Algo2
0168   int peak_finder_algorithm_;
0169 
0170   // Member not initialzed
0171   //std::vector<HcalTrigTowerDetId> towerIds(const HcalDetId & id) const;
0172 
0173   const HcalTrigTowerGeometry* theTrigTowerGeometry;
0174 
0175   typedef std::map<HcalTrigTowerDetId, IntegerCaloSamples> SumMap;
0176   SumMap theSumMap;
0177 
0178   typedef std::map<HcalTrigTowerDetId, std::vector<bool>> SatMap;
0179   SatMap theSatMap;
0180 
0181   struct HFDetails {
0182     IntegerCaloSamples long_fiber;
0183     IntegerCaloSamples short_fiber;
0184     HFDataFrame ShortDigi;
0185     HFDataFrame LongDigi;
0186   };
0187   typedef std::map<HcalTrigTowerDetId, std::map<uint32_t, HFDetails>> HFDetailMap;
0188   HFDetailMap theHFDetailMap;
0189 
0190   struct HFUpgradeDetails {
0191     IntegerCaloSamples samples;
0192     QIE10DataFrame digi;
0193     std::vector<bool> validity;
0194     std::vector<std::bitset<2>> fgbits;
0195     std::vector<bool> passTDC;
0196   };
0197   typedef std::map<HcalTrigTowerDetId, std::map<uint32_t, std::array<HFUpgradeDetails, 4>>> HFUpgradeDetailMap;
0198   HFUpgradeDetailMap theHFUpgradeDetailMap;
0199 
0200   typedef std::vector<IntegerCaloSamples> SumFGContainer;
0201   typedef std::map<HcalTrigTowerDetId, SumFGContainer> TowerMapFGSum;
0202   TowerMapFGSum theTowerMapFGSum;
0203 
0204   // ==============================
0205   // =  HF Veto
0206   // ==============================
0207   // Sum = Long + Short;" // intermediate calculation.
0208   //  if ((Short < MinSignalThresholdET OR Long  < MinSignalThresholdET)
0209   //     AND Sum > PMTNoiseThresholdET) VetoedSum = 0;
0210   //  else VetoedSum = Sum;
0211   // ==============================
0212   // Map from FG id to veto booleans
0213   HcalFeatureBit* LongvrsShortCut;
0214   typedef std::map<uint32_t, std::vector<bool>> TowerMapVeto;
0215   TowerMapVeto HF_Veto;
0216 
0217   typedef std::map<HcalTrigTowerDetId, std::vector<bool>> FGbitMap;
0218   FGbitMap fgMap_;
0219 
0220   typedef std::vector<HcalFinegrainBit::Tower> FGUpgradeContainer;
0221   typedef std::map<HcalTrigTowerDetId, FGUpgradeContainer> FGUpgradeMap;
0222   FGUpgradeMap fgUpgradeMap_;
0223 
0224   typedef std::vector<HcalFinegrainBit::TowerTDC> FGUpgradeTDCContainer;
0225   typedef std::map<HcalTrigTowerDetId, FGUpgradeTDCContainer> FGUpgradeTDCMap;
0226   FGUpgradeTDCMap fgUpgradeTDCMap_;
0227 
0228   bool upgrade_hb_ = false;
0229   bool upgrade_he_ = false;
0230   bool upgrade_hf_ = false;
0231 
0232   bool fix_saturation_ = false;
0233 
0234   edm::ParameterSet override_parameters_;
0235 
0236   bool override_adc_hf_ = false;
0237   uint32_t override_adc_hf_value_;
0238   bool override_tdc_hf_ = false;
0239   unsigned long long override_tdc_hf_value_;
0240 
0241   // HE constants
0242   static const int HBHE_OVERLAP_TOWER = 16;
0243   static const int FIRST_DEPTH7_TOWER = 26;
0244   static const int LAST_FINEGRAIN_DEPTH = 6;
0245   static const int LAST_FINEGRAIN_TOWER = 28;
0246 
0247   // Fine-grain in HF ignores tower 29, and starts with 30
0248   static const int FIRST_FINEGRAIN_TOWER = 30;
0249 
0250   static const int QIE8_LINEARIZATION_ET = HcaluLUTTPGCoder::QIE8_LUT_BITMASK;
0251   static const int QIE10_LINEARIZATION_ET = HcaluLUTTPGCoder::QIE10_LUT_BITMASK;
0252   static const int QIE11_LINEARIZATION_ET = HcaluLUTTPGCoder::QIE11_LUT_BITMASK;
0253   // Consider CaloTPGTranscoderULUT.h for values
0254   static const int QIE10_MAX_LINEARIZATION_ET = 0x7FF;
0255   static const int QIE11_MAX_LINEARIZATION_ET = 0x7FF;
0256   static const int QIE10_ZDC_MAX_LINEARIZATION_ET = 0x3FF;
0257 };
0258 
0259 template <typename... Digis>
0260 void HcalTriggerPrimitiveAlgo::run(const HcalTPGCoder* incoder,
0261                                    const HcalTPGCompressor* outcoder,
0262                                    const HcalDbService* conditions,
0263                                    HcalTrigPrimDigiCollection& result,
0264                                    const HcalTrigTowerGeometry* trigTowerGeometry,
0265                                    float rctlsb,
0266                                    const HcalFeatureBit* LongvrsShortCut,
0267                                    const Digis&... digis) {
0268   theTrigTowerGeometry = trigTowerGeometry;
0269 
0270   incoder_ = dynamic_cast<const HcaluLUTTPGCoder*>(incoder);
0271   outcoder_ = outcoder;
0272   conditions_ = conditions;
0273 
0274   theSumMap.clear();
0275   theSatMap.clear();
0276   theTowerMapFGSum.clear();
0277   HF_Veto.clear();
0278   fgMap_.clear();
0279   fgUpgradeMap_.clear();
0280   fgUpgradeTDCMap_.clear();
0281   theHFDetailMap.clear();
0282   theHFUpgradeDetailMap.clear();
0283 
0284   // Add all digi collections
0285   addDigis(digis...);
0286 
0287   // Prepare the fine-grain calculation algorithm for HB/HE
0288   int version = 0;
0289   if (upgrade_he_ or upgrade_hb_)
0290     version = conditions_->getHcalTPParameters()->getFGVersionHBHE();
0291   if (override_parameters_.exists("FGVersionHBHE"))
0292     version = override_parameters_.getParameter<uint32_t>("FGVersionHBHE");
0293   HcalFinegrainBit fg_algo(version);
0294 
0295   // VME produces additional bits on the front used by lumi but not the
0296   // trigger, this shift corrects those out by right shifting over them.
0297   for (auto& item : theSumMap) {
0298     result.push_back(HcalTriggerPrimitiveDigi(item.first));
0299     HcalTrigTowerDetId detId(item.second.id());
0300     if (detId.ietaAbs() >= theTrigTowerGeometry->firstHFTower(detId.version()) && detId.ietaAbs() < 42) {
0301       if (detId.version() == 0) {
0302         analyzeHF(item.second, result.back(), RCTScaleShift);
0303       } else if (detId.version() == 1) {
0304         if (upgrade_hf_)
0305           analyzeHFQIE10(item.second, result.back(), NCTScaleShift, LongvrsShortCut);
0306         else
0307           analyzeHF2016(item.second, result.back(), NCTScaleShift, LongvrsShortCut);
0308       } else {
0309         // Things are going to go poorly
0310       }
0311     } else if (detId.ietaAbs() >= theTrigTowerGeometry->firstHFTower(detId.version()) && detId.ietaAbs() > 41) {
0312       analyzeZDC(item.second, result.back());
0313     } else {
0314       // Determine which energy reconstruction path to take based on the
0315       // fine-grain availability:
0316       //  * QIE8 TP add entries into fgMap_
0317       //  * QIE11 TP add entries into fgUpgradeMap_
0318       //    (not for tower 16 unless HB is upgraded, too)
0319       if (fgMap_.find(item.first) != fgMap_.end()) {
0320         analyze(item.second, result.back());
0321       } else if (fgUpgradeMap_.find(item.first) != fgUpgradeMap_.end()) {
0322         SatMap::iterator item_sat = theSatMap.find(detId);
0323         if (item_sat == theSatMap.end())
0324           analyzeQIE11(item.second, std::vector<bool>(), result.back(), fg_algo);
0325         else
0326           analyzeQIE11(item.second, item_sat->second, result.back(), fg_algo);
0327       }
0328     }
0329   }
0330 
0331   // Free up some memory
0332   theSumMap.clear();
0333   theSatMap.clear();
0334   theTowerMapFGSum.clear();
0335   HF_Veto.clear();
0336   fgMap_.clear();
0337   fgUpgradeMap_.clear();
0338   fgUpgradeTDCMap_.clear();
0339   theHFDetailMap.clear();
0340   theHFUpgradeDetailMap.clear();
0341 
0342   return;
0343 }
0344 
0345 #endif