Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:22:24

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