Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:04

0001 #include "CommonTools/RecoAlgos/interface/ClusterStorer.h"
0002 
0003 #include "FWCore/Utilities/interface/Exception.h"
0004 
0005 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
0006 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHit.h"
0007 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit1D.h"
0008 #include "DataFormats/TrackerRecHit2D/interface/SiStripRecHit2D.h"
0009 #include "DataFormats/TrackerRecHit2D/interface/ProjectedSiStripRecHit2D.h"
0010 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
0011 #include "DataFormats/TrackerRecHit2D/interface/Phase2TrackerRecHit1D.h"
0012 #include "DataFormats/TrackerRecHit2D/interface/VectorHit.h"
0013 // FastSim hits:
0014 #include "DataFormats/TrackerRecHit2D/interface/FastTrackerRecHit.h"
0015 #include "DataFormats/TrackerRecHit2D/interface/FastProjectedTrackerRecHit.h"
0016 #include "DataFormats/TrackerRecHit2D/interface/FastMatchedTrackerRecHit.h"
0017 
0018 #include "DataFormats/SiStripDetId/interface/SiStripDetId.h"
0019 
0020 namespace helper {
0021 
0022   // -------------------------------------------------------------
0023   //FIXME (push cluster pointers...)
0024   void ClusterStorer::addCluster(TrackingRecHitCollection &hits, size_t index) {
0025     TrackingRecHit &newHit = hits[index];
0026     const std::type_info &hit_type = typeid(newHit);
0027     if (hit_type == typeid(SiPixelRecHit)) {
0028       //std::cout << "|  It is a Pixel hit !!" << std::endl;
0029       pixelClusterRecords_.push_back(PixelClusterHitRecord(static_cast<SiPixelRecHit &>(newHit), hits, index));
0030     } else if (hit_type == typeid(SiStripRecHit1D)) {
0031       //std::cout << "|   It is a SiStripRecHit1D hit !!" << std::endl;
0032       stripClusterRecords_.push_back(StripClusterHitRecord(static_cast<SiStripRecHit1D &>(newHit), hits, index));
0033     } else if (hit_type == typeid(SiStripRecHit2D)) {
0034       //std::cout << "|   It is a SiStripRecHit2D hit !!" << std::endl;
0035       stripClusterRecords_.push_back(StripClusterHitRecord(static_cast<SiStripRecHit2D &>(newHit), hits, index));
0036     } else if (hit_type == typeid(SiStripMatchedRecHit2D)) {
0037       //std::cout << "|   It is a SiStripMatchedRecHit2D hit !!" << std::endl;
0038       SiStripMatchedRecHit2D &mhit = static_cast<SiStripMatchedRecHit2D &>(newHit);
0039       stripClusterRecords_.push_back(StripClusterHitRecord(mhit.monoHit(), hits, index));
0040       stripClusterRecords_.push_back(StripClusterHitRecord(mhit.stereoHit(), hits, index));
0041     } else if (hit_type == typeid(ProjectedSiStripRecHit2D)) {
0042       //std::cout << "|   It is a ProjectedSiStripRecHit2D hit !!" << std::endl;
0043       ProjectedSiStripRecHit2D &phit = static_cast<ProjectedSiStripRecHit2D &>(newHit);
0044       stripClusterRecords_.push_back(StripClusterHitRecord(phit.originalHit(), hits, index));
0045     } else if (hit_type == typeid(Phase2TrackerRecHit1D)) {
0046       //FIXME:: this is just temporary solution for phase2,
0047       //it is not really running in the phase2 tracking wf - yet...
0048       phase2OTClusterRecords_.push_back(
0049           Phase2OTClusterHitRecord(static_cast<Phase2TrackerRecHit1D &>(newHit), hits, index));
0050     } else if (hit_type == typeid(VectorHit)) {
0051       //FIXME:: this is just temporary solution for phase2,
0052       //the VectorHit has 2 clusters but just a hit!
0053       phase2OTClusterRecords_.push_back(Phase2OTClusterHitRecord(static_cast<VectorHit &>(newHit), hits, index));
0054     } else {
0055       if (hit_type == typeid(FastTrackerRecHit) || hit_type == typeid(FastProjectedTrackerRecHit) ||
0056           hit_type == typeid(FastMatchedTrackerRecHit)) {
0057         //std::cout << "|   It is a " << hit_type.name() << " hit !!" << std::endl;
0058         // FastSim hits: Do nothing instead of caring about FastSim clusters,
0059         //               not even sure whether these really exist.
0060         //               At least predecessor code in TrackSelector and MuonSelector
0061         //               did not treat them.
0062       } else {
0063         // through for unknown types
0064         throw cms::Exception("UnknownHitType") << "helper::ClusterStorer::addCluster: "
0065                                                << "Unknown hit type " << hit_type.name() << ".\n";
0066       }
0067     }  // end 'switch' on hit type
0068   }
0069 
0070   // -------------------------------------------------------------
0071   void ClusterStorer::clear() {
0072     pixelClusterRecords_.clear();
0073     stripClusterRecords_.clear();
0074   }
0075 
0076   // -------------------------------------------------------------
0077   void ClusterStorer::processAllClusters(edmNew::DetSetVector<SiPixelCluster> &pixelDsvToFill,
0078                                          edm::RefProd<edmNew::DetSetVector<SiPixelCluster> > refPixelClusters,
0079                                          edmNew::DetSetVector<SiStripCluster> &stripDsvToFill,
0080                                          edm::RefProd<edmNew::DetSetVector<SiStripCluster> > refStripClusters) {
0081     if (!pixelClusterRecords_.empty()) {
0082       this->processClusters<SiPixelRecHit, SiPixelCluster>(pixelClusterRecords_, pixelDsvToFill, refPixelClusters);
0083     }
0084     if (!stripClusterRecords_.empty()) {
0085       // All we need from the HitType 'SiStripRecHit2D' is the
0086       // typedef for 'SiStripRecHit2D::ClusterRef'.
0087       // The fact that rekey<SiStripRecHit2D> is called is irrelevant since
0088       // ClusterHitRecord<typename SiStripRecHit2D::ClusterRef>::rekey<RecHitType>
0089       // is specialised such that 'RecHitType' is not used...
0090       this->processClusters<SiStripRecHit2D, SiStripCluster>(stripClusterRecords_, stripDsvToFill, refStripClusters);
0091     }
0092   }
0093 
0094   //-------------------------------------------------------------
0095   template <typename HitType, typename ClusterType>
0096   void ClusterStorer::processClusters(std::vector<ClusterHitRecord<typename HitType::ClusterRef> > &clusterRecords,
0097                                       edmNew::DetSetVector<ClusterType> &dsvToFill,
0098                                       edm::RefProd<edmNew::DetSetVector<ClusterType> > &refprod) {
0099     std::sort(clusterRecords.begin(), clusterRecords.end());  // this sorts them by detid
0100     typedef typename std::vector<ClusterHitRecord<typename HitType::ClusterRef> >::const_iterator RIT;
0101     RIT it = clusterRecords.begin(), end = clusterRecords.end();
0102     size_t clusters = 0;
0103     while (it != end) {
0104       RIT it2 = it;
0105       uint32_t detid = it->detid();
0106 
0107       // first isolate all clusters on the same detid
0108       while ((it2 != end) && (it2->detid() == detid)) {
0109         ++it2;
0110       }
0111       // now [it, it2] bracket one detid
0112 
0113       // then prepare to copy the clusters
0114       typename edmNew::DetSetVector<ClusterType>::FastFiller filler(dsvToFill, detid);
0115       typename HitType::ClusterRef lastRef, newRef;
0116       for (; it != it2; ++it) {  // loop on the detid
0117         // first check if we need to clone the hit
0118         if (it->clusterRef() != lastRef) {
0119           lastRef = it->clusterRef();
0120           // clone cluster
0121           filler.push_back(*lastRef);
0122           // make new ref
0123           newRef = typename HitType::ClusterRef(refprod, clusters++);
0124         }
0125         it->template rekey<HitType>(newRef);
0126       }  // end of the loop on a single detid
0127 
0128     }  // end of the loop on all clusters
0129   }
0130 
0131   //-------------------------------------------------------------
0132   // helper classes
0133   //-------------------------------------------------------------
0134   // FIXME (migrate to new RTTI and interface)
0135   // generic rekey (in practise for pixel only...)
0136   template <typename ClusterRefType>  // template for class
0137   template <typename RecHitType>      // template for member function
0138   void ClusterStorer::ClusterHitRecord<ClusterRefType>::rekey(const ClusterRefType &newRef) const {
0139     TrackingRecHit &genericHit = (*hits_)[index_];
0140     RecHitType *hit = nullptr;
0141     if (genericHit.geographicalId().rawId() == detid_) {  // a hit on this det, so it's simple
0142       hit = dynamic_cast<RecHitType *>(&genericHit);      //static_cast<RecHitType *>(&genericHit);
0143     }
0144     assert(hit != nullptr);
0145     assert(hit->cluster() == ref_);  // otherwise something went wrong
0146     hit->setClusterRef(newRef);
0147   }
0148 
0149   // -------------------------------------------------------------
0150   // Specific rekey for class template ClusterRefType = SiStripRecHit2D::ClusterRef,
0151   // RecHitType is not used.
0152   template <>
0153   template <typename RecHitType>  // or template<> to specialise also here?
0154   void ClusterStorer::ClusterHitRecord<SiStripRecHit2D::ClusterRef>::
0155       //  rekey<SiStripRecHit2D>(const SiStripRecHit2D::ClusterRef &newRef) const
0156       rekey(const SiStripRecHit2D::ClusterRef &newRef) const {
0157     TrackingRecHit &genericHit = (*hits_)[index_];
0158     const std::type_info &hit_type = typeid(genericHit);
0159 
0160     OmniClusterRef *cluRef = nullptr;
0161     if (typeid(SiStripRecHit1D) == hit_type) {
0162       cluRef = &static_cast<SiStripRecHit1D &>(genericHit).omniCluster();
0163     } else if (typeid(SiStripRecHit2D) == hit_type) {
0164       cluRef = &static_cast<SiStripRecHit2D &>(genericHit).omniCluster();
0165     } else if (typeid(SiStripMatchedRecHit2D) == hit_type) {
0166       SiStripMatchedRecHit2D &mhit = static_cast<SiStripMatchedRecHit2D &>(genericHit);
0167       cluRef = (SiStripDetId(detid_).stereo() ? &mhit.stereoClusterRef() : &mhit.monoClusterRef());
0168     } else if (typeid(ProjectedSiStripRecHit2D) == hit_type) {
0169       cluRef = &static_cast<ProjectedSiStripRecHit2D &>(genericHit).originalHit().omniCluster();
0170     }
0171 
0172     assert(cluRef != nullptr);            // to catch missing RecHit types
0173     assert(cluRef->key() == ref_.key());  // otherwise something went wrong
0174     (*cluRef) = OmniClusterRef(newRef);
0175   }
0176 
0177 }  // namespace helper