Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:12

0001 #ifndef StMeasurementDetSet_H
0002 #define StMeasurementDetSet_H
0003 
0004 #include <vector>
0005 class TkStripMeasurementDet;
0006 class TkStripMeasurementDet;
0007 class TkPixelMeasurementDet;
0008 class SiStripRecHitMatcher;
0009 class StripClusterParameterEstimator;
0010 class PixelClusterParameterEstimator;
0011 class Phase2StripCPE;
0012 
0013 #include "DataFormats/Common/interface/DetSetVectorNew.h"
0014 #include "DataFormats/SiStripCluster/interface/SiStripCluster.h"
0015 #include "DataFormats/SiPixelCluster/interface/SiPixelCluster.h"
0016 #include "DataFormats/Phase2TrackerCluster/interface/Phase2TrackerCluster1D.h"
0017 #include "RecoLocalTracker/Phase2TrackerRecHits/interface/Phase2StripCPE.h"
0018 #include "DataFormats/Common/interface/Handle.h"
0019 
0020 #include "CondFormats/SiStripObjects/interface/SiStripBadStrip.h"
0021 #include "CalibFormats/SiStripObjects/interface/SiStripQuality.h"
0022 
0023 #include "FWCore/Utilities/interface/thread_safety_macros.h"
0024 
0025 #include <atomic>
0026 #include <unordered_map>
0027 
0028 // #define VISTAT
0029 
0030 #ifdef VISTAT
0031 #include <iostream>
0032 #define COUT std::cout
0033 #else
0034 #define COUT LogDebug("")
0035 #endif
0036 
0037 /* Struct of arrays supporting "members of Tk...MeasurementDet
0038  * implemented with vectors, to be optimized...
0039    ITEMS THAT DO NOT DEPEND ON THE EVENT
0040  */
0041 class StMeasurementConditionSet {
0042 public:
0043   enum QualityFlags {
0044     BadModules = 1,  // for everybody
0045     /* Strips: */ BadAPVFibers = 2,
0046     BadStrips = 4,
0047     MaskBad128StripBlocks = 8,
0048     /* Pixels: */ BadROCs = 2
0049   };
0050 
0051   struct BadStripCuts {
0052     BadStripCuts() : maxBad(9999), maxConsecutiveBad(9999) {}
0053     BadStripCuts(uint32_t iMaxBad, uint32_t iMaxConsecutiveBad)
0054         : maxBad(iMaxBad), maxConsecutiveBad(iMaxConsecutiveBad) {}
0055     uint16_t maxBad, maxConsecutiveBad;
0056   };
0057 
0058   struct BadStripBlock {
0059     short first;
0060     short last;
0061     BadStripBlock(const SiStripBadStrip::data& data) : first(data.firstStrip), last(data.firstStrip + data.range - 1) {}
0062   };
0063 
0064   StMeasurementConditionSet(const SiStripRecHitMatcher* matcher, const StripClusterParameterEstimator* cpe)
0065       : theMatcher(matcher), theCPE(cpe) {}
0066 
0067   void init(int size);
0068 
0069   const SiStripRecHitMatcher* matcher() const { return theMatcher; }
0070   const StripClusterParameterEstimator* stripCPE() const { return theCPE; }
0071 
0072   int nDet() const { return id_.size(); }
0073   unsigned int id(int i) const { return id_[i]; }
0074   unsigned char subId(int i) const { return subId_[i]; }
0075 
0076   int find(unsigned int jd, int i = 0) const { return std::lower_bound(id_.begin() + i, id_.end(), jd) - id_.begin(); }
0077 
0078   bool isActiveThisPeriod(int i) const { return activeThisPeriod_[i]; }
0079 
0080   /** \brief Turn on/off the module for reconstruction, for the full run or lumi (using info from DB, usually) */
0081   void setActive(int i, bool active) { activeThisPeriod_[i] = active; }
0082 
0083   int totalStrips(int i) const { return totalStrips_[i]; }
0084 
0085   void setMaskBad128StripBlocks(bool maskThem) { maskBad128StripBlocks_ = maskThem; }
0086   const BadStripCuts& badStripCuts(int i) const { return badStripCuts_[subId_[i]]; }
0087 
0088   bool maskBad128StripBlocks() const { return maskBad128StripBlocks_; }
0089   bool hasAny128StripBad(int i) const { return hasAny128StripBad_[i]; }
0090 
0091   /// note: index is 6*detector index + offset!
0092   bool bad128Strip(int offset) const { return bad128Strip_[offset]; }
0093   bool bad128Strip(int index, int strip) const { return bad128Strip_[nbad128 * index + (strip >> 7)]; }
0094 
0095   std::vector<BadStripBlock>& getBadStripBlocks(int i) { return badStripBlocks_[i]; }
0096   std::vector<BadStripBlock> const& badStripBlocks(int i) const { return badStripBlocks_[i]; }
0097 
0098   bool isMasked(int i, const SiStripCluster& cluster) const {
0099     int offset = nbad128 * i;
0100     if (bad128Strip_[offset + (cluster.firstStrip() >> 7)]) {
0101       if (bad128Strip_[offset + ((cluster.firstStrip() + cluster.amplitudes().size()) >> 7)] ||
0102           bad128Strip_[offset + (static_cast<int32_t>(cluster.barycenter() - 0.499999) >> 7)]) {
0103         return true;
0104       }
0105     } else {
0106       if (bad128Strip_[offset + ((cluster.firstStrip() + cluster.amplitudes().size()) >> 7)] &&
0107           bad128Strip_[offset + (static_cast<int32_t>(cluster.barycenter() - 0.499999) >> 7)]) {
0108         return true;
0109       }
0110     }
0111     return false;
0112   }
0113 
0114   void set128StripStatus(int i, bool good, int idx = -1);
0115 
0116 private:
0117   friend class MeasurementTrackerImpl;
0118 
0119   // globals
0120   const SiStripRecHitMatcher* theMatcher;
0121   const StripClusterParameterEstimator* theCPE;
0122 
0123   bool maskBad128StripBlocks_;
0124   BadStripCuts badStripCuts_[4];
0125 
0126   // members of TkStripMeasurementDet
0127   std::vector<unsigned int> id_;
0128   std::vector<unsigned char> subId_;
0129 
0130   std::vector<int> totalStrips_;
0131 
0132   static const int nbad128 = 6;
0133   std::vector<bool> bad128Strip_;
0134   std::vector<bool> hasAny128StripBad_;
0135 
0136   std::vector<std::vector<BadStripBlock>> badStripBlocks_;
0137 
0138   std::vector<bool> activeThisPeriod_;
0139 };
0140 
0141 class StMeasurementDetSet {
0142 public:
0143   typedef edmNew::DetSet<SiStripCluster> StripDetset;
0144   typedef StripDetset::const_iterator new_const_iterator;
0145 
0146   StMeasurementDetSet(const StMeasurementConditionSet& cond)
0147       : conditionSet_(&cond),
0148         activeThisEvent_(cond.nDet(), true),
0149         detSet_(cond.nDet()),
0150         detIndex_(cond.nDet(), -1),
0151         theRawInactiveStripDetIds_(),
0152         stripDefined_(0),
0153         stripUpdated_(0),
0154         stripRegions_(0) {}
0155 
0156   ~StMeasurementDetSet() { printStat(); }
0157 
0158   const StMeasurementConditionSet& conditions() const { return *conditionSet_; }
0159 
0160   void update(int i, const StripDetset& detSet) {
0161     detSet_[i].detSet_ = detSet;
0162     detSet_[i].empty_ = false;
0163   }
0164 
0165   void update(int i, int j) {
0166     assert(j >= 0);
0167     assert(detSet_[i].empty_);
0168     assert(detSet_[i].ready_ == ReadyState::kUnset);
0169     detIndex_[i] = j;
0170     detSet_[i].empty_ = false;
0171     incReady();
0172   }
0173 
0174   int size() const { return conditions().nDet(); }
0175   int nDet() const { return size(); }
0176   unsigned int id(int i) const { return conditions().id(i); }
0177   int find(unsigned int jd, int i = 0) const { return conditions().find(jd, i); }
0178 
0179   bool empty(int i) const { return detSet_[i].empty_.load(std::memory_order_relaxed); }
0180   bool isActive(int i) const { return activeThisEvent_[i] && conditions().isActiveThisPeriod(i); }
0181 
0182   void setEmpty(int i) {
0183     detSet_[i].empty_ = true;
0184     activeThisEvent_[i] = true;
0185   }
0186   void setUpdated(int i) { stripUpdated_[i] = true; }
0187 
0188   void setEmpty() {
0189     printStat();
0190     for (auto& d : detSet_) {
0191       d.empty_ = true;
0192       d.ready_ = ReadyState::kUnset;
0193     }
0194     std::fill(detIndex_.begin(), detIndex_.end(), -1);
0195     std::fill(activeThisEvent_.begin(), activeThisEvent_.end(), true);
0196     incTot(size());
0197   }
0198 
0199   /** \brief Turn on/off the module for reconstruction for one events.
0200       This per-event flag is cleared by any call to 'update' or 'setEmpty'  */
0201   void setActiveThisEvent(int i, bool active) {
0202     activeThisEvent_[i] = active;
0203     if (!active)
0204       detSet_[i].empty_ = true;
0205   }
0206 
0207   edm::Handle<edmNew::DetSetVector<SiStripCluster>>& handle() { return handle_; }
0208   const edm::Handle<edmNew::DetSetVector<SiStripCluster>>& handle() const { return handle_; }
0209   // StripDetset & detSet(int i) { return detSet_[i]; }
0210   const StripDetset& detSet(int i) const {
0211     if (detSet_[i].ready_.load(std::memory_order_acquire) != ReadyState::kSet)
0212       getDetSet(i);
0213     return detSet_[i].detSet_;
0214   }
0215 
0216   //// ------- pieces for on-demand unpacking --------
0217   std::vector<uint32_t>& rawInactiveStripDetIds() { return theRawInactiveStripDetIds_; }
0218   const std::vector<uint32_t>& rawInactiveStripDetIds() const { return theRawInactiveStripDetIds_; }
0219 
0220   void resetOnDemandStrips() {
0221     std::fill(stripDefined_.begin(), stripDefined_.end(), false);
0222     std::fill(stripUpdated_.begin(), stripUpdated_.end(), false);
0223   }
0224   const bool stripDefined(int i) const { return stripDefined_[i]; }
0225   const bool stripUpdated(int i) const { return stripUpdated_[i]; }
0226   void defineStrip(int i, std::pair<unsigned int, unsigned int> range) {
0227     stripDefined_[i] = true;
0228     stripUpdated_[i] = false;
0229     stripRegions_[i] = range;
0230   }
0231 
0232 private:
0233   void getDetSet(int i) const {
0234     const auto& det = detSet_[i];
0235 
0236     ReadyState expected = ReadyState::kUnset;
0237     if (det.ready_.compare_exchange_strong(expected, ReadyState::kSetting, std::memory_order_acq_rel)) {
0238       if (detIndex_[i] >= 0) {
0239         det.detSet_ = StripDetset(*handle_, handle_->item(detIndex_[i]), true);
0240         det.empty_.store(false, std::memory_order_relaxed);  // better be false already
0241         incAct();
0242       } else {  // we should not be here
0243         det.detSet_ = StripDetset();
0244         det.empty_.store(true, std::memory_order_relaxed);
0245       }
0246       det.ready_.store(ReadyState::kSet, std::memory_order_release);
0247       incSet();
0248     } else {
0249       // need to wait
0250       while (ReadyState::kSet != det.ready_.load(std::memory_order_acquire)) {
0251       }
0252     }
0253   }
0254 
0255   friend class MeasurementTrackerImpl;
0256 
0257   const StMeasurementConditionSet* conditionSet_;
0258 
0259   // Globals, per-event
0260   edm::Handle<edmNew::DetSetVector<SiStripCluster>> handle_;
0261 
0262   enum class ReadyState : char { kUnset, kSetting, kSet };
0263 
0264   // Helper struct to define only the vector elements as mutable and
0265   // to have a vector of atomics without an explicit loop over
0266   // elements to set their values
0267   struct DetSetHelper {
0268     mutable std::atomic<bool> empty_ = true;
0269     mutable std::atomic<ReadyState> ready_ = ReadyState::kUnset;  // to be cleaned
0270     CMS_THREAD_GUARD(ready_) mutable StripDetset detSet_;
0271   };
0272 
0273   std::vector<bool> activeThisEvent_;
0274 
0275   // full reco
0276   std::vector<DetSetHelper> detSet_;
0277   std::vector<int> detIndex_;
0278 
0279   // note: not aligned to the index
0280   std::vector<uint32_t> theRawInactiveStripDetIds_;
0281   // keyed on si-strip index
0282   std::vector<bool> stripDefined_, stripUpdated_;
0283   std::vector<std::pair<unsigned int, unsigned int>> stripRegions_;
0284   // keyed on glued
0285   // std::vector<bool> gluedUpdated_;
0286 
0287 #ifdef VISTAT
0288   struct Stat {
0289     int totDet = 0;    // all dets
0290     int detReady = 0;  // dets "updated"
0291     int detSet = 0;    // det actually set not empty
0292     int detAct = 0;    // det actually set with content
0293   };
0294 
0295   mutable Stat stat;
0296   void zeroStat() const { stat = Stat(); }
0297   void incTot(int n) const { stat.totDet = n; }
0298   void incReady() const { stat.detReady++; }
0299   void incSet() const { stat.detSet++; }
0300   void incAct() const { stat.detAct++; }
0301   void printStat() const {
0302     COUT << "VI detsets " << stat.totDet << ',' << stat.detReady << ',' << stat.detSet << ',' << stat.detAct
0303          << std::endl;
0304   }
0305 
0306 #else
0307   static void zeroStat() {}
0308   static void incTot(int) {}
0309   static void incReady() {}
0310   static void incSet() {}
0311   static void incAct() {}
0312   static void printStat() {}
0313 #endif
0314 };
0315 
0316 class PxMeasurementConditionSet {
0317 public:
0318   PxMeasurementConditionSet(const PixelClusterParameterEstimator* cpe) : theCPE(cpe) {}
0319 
0320   void init(int size);
0321 
0322   int nDet() const { return id_.size(); }
0323   unsigned int id(int i) const { return id_[i]; }
0324   int find(unsigned int jd, int i = 0) const { return std::lower_bound(id_.begin() + i, id_.end(), jd) - id_.begin(); }
0325 
0326   const PixelClusterParameterEstimator* pixelCPE() const { return theCPE; }
0327   bool isActiveThisPeriod(int i) const { return activeThisPeriod_[i]; }
0328 
0329   /** \brief Turn on/off the module for reconstruction, for the full run or lumi (using info from DB, usually).
0330       This also resets the 'setActiveThisEvent' to true */
0331   void setActive(int i, bool active) { activeThisPeriod_[i] = active; }
0332 
0333 private:
0334   friend class MeasurementTrackerImpl;
0335 
0336   // Globals (not-per-event)
0337   const PixelClusterParameterEstimator* theCPE;
0338 
0339   // Locals, per-event
0340   std::vector<unsigned int> id_;
0341   std::vector<bool> activeThisPeriod_;
0342 };
0343 
0344 class PxMeasurementDetSet {
0345 public:
0346   typedef edm::Ref<edmNew::DetSetVector<SiPixelCluster>, SiPixelCluster> SiPixelClusterRef;
0347   typedef edmNew::DetSet<SiPixelCluster> PixelDetSet;
0348   typedef std::vector<std::pair<LocalPoint, LocalPoint>> BadFEDChannelPositions;
0349 
0350   PxMeasurementDetSet(const PxMeasurementConditionSet& cond)
0351       : conditionSet_(&cond), detSet_(cond.nDet()), empty_(cond.nDet(), true), activeThisEvent_(cond.nDet(), true) {}
0352 
0353   const PxMeasurementConditionSet& conditions() const { return *conditionSet_; }
0354 
0355   int size() const { return conditions().nDet(); }
0356   int nDet() const { return size(); }
0357   unsigned int id(int i) const { return conditions().id(i); }
0358   int find(unsigned int jd, int i = 0) const { return conditions().find(jd, i); }
0359 
0360   void update(int i, const PixelDetSet& detSet) {
0361     detSet_[i] = detSet;
0362     empty_[i] = false;
0363   }
0364 
0365   bool empty(int i) const { return empty_[i]; }
0366   bool isActive(int i) const { return activeThisEvent_[i] && conditions().isActiveThisPeriod(i); }
0367 
0368   void setEmpty(int i) {
0369     empty_[i] = true;
0370     activeThisEvent_[i] = true;
0371     auto found = badFEDChannelPositionsSet_.find(i);
0372     if (found != badFEDChannelPositionsSet_.end()) {
0373       badFEDChannelPositionsSet_.erase(found);
0374     }
0375   }
0376 
0377   void setEmpty() {
0378     std::fill(empty_.begin(), empty_.end(), true);
0379     std::fill(activeThisEvent_.begin(), activeThisEvent_.end(), true);
0380     badFEDChannelPositionsSet_.clear();
0381   }
0382   void setActiveThisEvent(bool active) { std::fill(activeThisEvent_.begin(), activeThisEvent_.end(), active); }
0383 
0384   const BadFEDChannelPositions* getBadFEDChannelPositions(int i) const {
0385     auto found = badFEDChannelPositionsSet_.find(i);
0386     if (found == badFEDChannelPositionsSet_.end())
0387       return nullptr;
0388     return &(found->second);
0389   }
0390   void addBadFEDChannelPositions(int i, BadFEDChannelPositions& positions) {
0391     auto found = badFEDChannelPositionsSet_.find(i);
0392     if (found == badFEDChannelPositionsSet_.end()) {
0393       badFEDChannelPositionsSet_.emplace(i, positions);
0394     } else {
0395       found->second.insert(found->second.end(), positions.begin(), positions.end());
0396     }
0397   }
0398 
0399   /** \brief Turn on/off the module for reconstruction for one events.
0400       This per-event flag is cleared by any call to 'update' or 'setEmpty'  */
0401   void setActiveThisEvent(int i, bool active) {
0402     activeThisEvent_[i] = active;
0403     if (!active)
0404       empty_[i] = true;
0405   }
0406   const edm::Handle<edmNew::DetSetVector<SiPixelCluster>>& handle() const { return handle_; }
0407   edm::Handle<edmNew::DetSetVector<SiPixelCluster>>& handle() { return handle_; }
0408   const PixelDetSet& detSet(int i) const { return detSet_[i]; }
0409 
0410 private:
0411   friend class MeasurementTrackerImpl;
0412 
0413   const PxMeasurementConditionSet* conditionSet_;
0414 
0415   // Globals, per-event
0416   edm::Handle<edmNew::DetSetVector<SiPixelCluster>> handle_;
0417 
0418   // Locals, per-event
0419   std::vector<PixelDetSet> detSet_;
0420   std::vector<bool> empty_;
0421   std::vector<bool> activeThisEvent_;
0422   std::unordered_map<int, BadFEDChannelPositions> badFEDChannelPositionsSet_;
0423 };
0424 
0425 //FIXME:just temporary solution for phase2 OT that works!
0426 class Phase2OTMeasurementConditionSet {
0427 public:
0428   Phase2OTMeasurementConditionSet(const ClusterParameterEstimator<Phase2TrackerCluster1D>* cpe) : theCPE(cpe) {}
0429 
0430   void init(int size);
0431 
0432   int nDet() const { return id_.size(); }
0433   unsigned int id(int i) const { return id_[i]; }
0434   int find(unsigned int jd, int i = 0) const { return std::lower_bound(id_.begin() + i, id_.end(), jd) - id_.begin(); }
0435 
0436   const ClusterParameterEstimator<Phase2TrackerCluster1D>* cpe() const { return theCPE; }
0437   bool isActiveThisPeriod(int i) const { return activeThisPeriod_[i]; }
0438 
0439   /** \brief Turn on/off the module for reconstruction, for the full run or lumi (using info from DB, usually).
0440  *       This also resets the 'setActiveThisEvent' to true */
0441   void setActive(int i, bool active) { activeThisPeriod_[i] = active; }
0442 
0443 private:
0444   friend class MeasurementTrackerImpl;
0445 
0446   // Globals (not-per-event)
0447   const ClusterParameterEstimator<Phase2TrackerCluster1D>* theCPE;
0448 
0449   // Locals, per-event
0450   std::vector<unsigned int> id_;
0451   std::vector<bool> activeThisPeriod_;
0452 };
0453 
0454 class Phase2OTMeasurementDetSet {
0455 public:
0456   typedef edm::Ref<edmNew::DetSetVector<Phase2TrackerCluster1D>, Phase2TrackerCluster1D> Phase2TrackerCluster1DRef;
0457   typedef edmNew::DetSet<Phase2TrackerCluster1D> Phase2DetSet;
0458 
0459   Phase2OTMeasurementDetSet(const Phase2OTMeasurementConditionSet& cond)
0460       : conditionSet_(&cond), detSet_(cond.nDet()), empty_(cond.nDet(), true), activeThisEvent_(cond.nDet(), true) {}
0461 
0462   const Phase2OTMeasurementConditionSet& conditions() const { return *conditionSet_; }
0463 
0464   int size() const { return conditions().nDet(); }
0465   int nDet() const { return size(); }
0466   unsigned int id(int i) const { return conditions().id(i); }
0467   int find(unsigned int jd, int i = 0) const { return conditions().find(jd, i); }
0468 
0469   void update(int i, const Phase2DetSet& detSet) {
0470     detSet_[i] = detSet;
0471     empty_[i] = false;
0472   }
0473 
0474   bool empty(int i) const { return empty_[i]; }
0475   bool isActive(int i) const { return activeThisEvent_[i] && conditions().isActiveThisPeriod(i); }
0476 
0477   void setEmpty(int i) {
0478     empty_[i] = true;
0479     activeThisEvent_[i] = true;
0480   }
0481 
0482   void setEmpty() {
0483     std::fill(empty_.begin(), empty_.end(), true);
0484     std::fill(activeThisEvent_.begin(), activeThisEvent_.end(), true);
0485   }
0486   void setActiveThisEvent(bool active) { std::fill(activeThisEvent_.begin(), activeThisEvent_.end(), active); }
0487   void setActiveThisEvent(int i, bool active) {
0488     activeThisEvent_[i] = active;
0489     if (!active)
0490       empty_[i] = true;
0491   }
0492   const edm::Handle<edmNew::DetSetVector<Phase2TrackerCluster1D>>& handle() const { return handle_; }
0493   edm::Handle<edmNew::DetSetVector<Phase2TrackerCluster1D>>& handle() { return handle_; }
0494   const Phase2DetSet& detSet(int i) const { return detSet_[i]; }
0495 
0496 private:
0497   friend class MeasurementTrackerImpl;
0498 
0499   const Phase2OTMeasurementConditionSet* conditionSet_;
0500 
0501   //Globals, per-event
0502   edm::Handle<edmNew::DetSetVector<Phase2TrackerCluster1D>> handle_;
0503 
0504   // Locals, per-event
0505   std::vector<Phase2DetSet> detSet_;
0506   std::vector<bool> empty_;
0507   std::vector<bool> activeThisEvent_;
0508 };
0509 
0510 #endif  // StMeasurementDetSet_H