CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/RecoTracker/MeasurementDet/src/OnDemandMeasurementTracker.cc

Go to the documentation of this file.
00001 #include "RecoTracker/MeasurementDet/interface/OnDemandMeasurementTracker.h"
00002 
00003 #include "FWCore/Framework/interface/ESHandle.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 
00006 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00007 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00008 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00009 #include "Geometry/TrackerGeometryBuilder/interface/GluedGeomDet.h"
00010 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00011 
00012 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00013 
00014 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00015 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00016 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00017 #include "DataFormats/SiStripCluster/interface/SiStripClusterCollection.h"
00018 
00019 #include "TrackingTools/MeasurementDet/interface/MeasurementDetException.h"
00020 
00021 #include "RecoLocalTracker/ClusterParameterEstimator/interface/PixelClusterParameterEstimator.h"
00022 #include "RecoLocalTracker/Records/interface/TrackerCPERecord.h"
00023 #include "RecoLocalTracker/SiStripRecHitConverter/interface/SiStripRecHitMatcher.h"
00024 #include "RecoLocalTracker/SiStripRecHitConverter/interface/StripCPE.h"  
00025 
00026 #include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00027 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00028 #include "RecoTracker/MeasurementDet/interface/TkStripMeasurementDet.h"
00029 #include "RecoTracker/MeasurementDet/interface/TkPixelMeasurementDet.h"
00030 #include "RecoTracker/MeasurementDet/interface/TkGluedMeasurementDet.h"
00031 
00032 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
00033 #include "CondFormats/DataRecord/interface/SiStripNoisesRcd.h"
00034 
00035 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00036 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00037 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00038 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
00039 #include "CalibTracker/Records/interface/SiStripRegionCablingRcd.h"
00040 
00041 #include <iostream>
00042 #include <typeinfo>
00043 #include <map>
00044 
00045 #include <DataFormats/GeometrySurface/interface/BoundPlane.h>
00046 #include "DataFormats/Math/interface/deltaR.h"
00047 
00048 #include "FWCore/ServiceRegistry/interface/Service.h"
00049 #include "FWCore/Services/interface/UpdaterService.h"
00050 
00051 using namespace std;
00052 
00053 OnDemandMeasurementTracker::OnDemandMeasurementTracker(const edm::ParameterSet&              conf,
00054                                                        const PixelClusterParameterEstimator* pixelCPE,
00055                                                        const StripClusterParameterEstimator* stripCPE,
00056                                                        const SiStripRecHitMatcher*  hitMatcher,
00057                                                        const TrackerGeometry*  trackerGeom,
00058                                                        const GeometricSearchTracker* geometricSearchTracker,
00059                                                        const SiStripQuality *stripQuality,
00060                                                        int   stripQualityFlags,
00061                                                        int   stripQualityDebugFlags,
00062                                                        const SiPixelQuality *pixelQuality,
00063                                                        const SiPixelFedCabling *pixelCabling,
00064                                                        int   pixelQualityFlags,
00065                                                        int   pixelQualityDebugFlags,
00066                                                        const SiStripRegionCabling * stripRegionCabling,
00067                                                        bool isRegional):
00068   MeasurementTracker(conf,pixelCPE,stripCPE,hitMatcher,trackerGeom,geometricSearchTracker,
00069         stripQuality,stripQualityFlags,stripQualityDebugFlags,
00070         pixelQuality,pixelCabling,pixelQualityFlags,pixelQualityDebugFlags,
00071         isRegional)
00072   , category_("OnDemandMeasurementTracker")
00073   , StayPacked_(true)
00074   , StripOnDemand_(true)
00075   , PixelOnDemand_(false)
00076   , theStripRegionCabling(stripRegionCabling)
00077 {
00078   //  the constructor does construct the regular MeasurementTracker
00079   //  then a smart copy of the DetMap is made into DetODMap: this could be avoided with modification to MeasurementDet interface
00080   //  the elementIndex to be defined in the refgetter is mapped to the detId
00081   //  flags are set to initialize the DetODMap
00082 
00083   for (DetContainer::iterator it=theDetMap.begin(); it!= theDetMap.end();++it)
00084     {
00085       DetODContainer::iterator inserted = theDetODMap.insert(make_pair(it->first,DetODStatus(const_cast<MeasurementDet*>(it->second)))).first;
00086 
00087 
00088       GeomDet::SubDetector subdet = it->second->geomDet().subDetector();
00089       if (subdet == GeomDetEnumerators::PixelBarrel  || subdet == GeomDetEnumerators::PixelEndcap ){
00090         //special flag treatement for pixels
00091         //one can never be updated and not defined. except if we don't need to care about them: pixels
00092         inserted->second.defined=false;
00093         inserted->second.updated=true;
00094       }//pixel module
00095       else if (subdet == GeomDetEnumerators::TIB || subdet == GeomDetEnumerators::TOB ||
00096           subdet == GeomDetEnumerators::TID || subdet == GeomDetEnumerators::TEC )
00097         {
00098           //set flag to false
00099           inserted->second.defined=false;
00100           inserted->second.updated=false;
00101 
00102           //what will be the element index in the refgetter
00103           GlobalPoint center = it->second->geomDet().position();
00104           double eta = center.eta();
00105           double phi = center.phi();
00106           uint32_t id = it->first.rawId();
00107           SiStripRegionCabling::ElementIndex eIndex = theStripRegionCabling->elementIndex(SiStripRegionCabling::Position(eta,phi),
00108                                                                                           SiStripRegionCabling::subdetFromDetId(id),
00109                                                                                           SiStripRegionCabling::layerFromDetId(id));
00110           LogDebug(category_)<<"region selected (from "<<id<<" center) is:\n"
00111                              <<"position: "<<center
00112                              <<"\n center absolute index: "<<theStripRegionCabling->region(theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi)))
00113                              <<"\n center position index: "<<theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi)).first<<
00114             " "<<theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi)).second
00115                              <<"\n center postion: "<<theStripRegionCabling->position(theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi))).first<<
00116             " "<<theStripRegionCabling->position(theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi))).second
00117                              <<"\n eta: "<<eta
00118                              <<"\n phi: "<<phi
00119                              <<"\n subedet: "<<SiStripRegionCabling::subdetFromDetId(id)
00120                              <<" layer: "<<SiStripRegionCabling::layerFromDetId(id);
00121 
00122           //      register those in a map
00123           //to be able to know what are the detid in a given elementIndex
00124           region_mapping[eIndex].push_back(inserted);
00125         }//strip module
00126       else{
00127         //abort
00128         edm::LogError(category_)<<"not a tracker geomdet in constructor: "<<it->first.rawId();
00129         throw MeasurementDetException("OnDemandMeasurementTracker dealing with a non tracker GeomDet.");
00130       }//abort
00131     }//loop over DetMap
00132 
00133   if (theInactiveStripDetectorLabels.size()!=0)
00134     theRawInactiveStripDetIds.reserve(200);
00135 }
00136 
00137 
00138 void OnDemandMeasurementTracker::define( const edm::Handle< LazyGetter> & theLazyGetterH,
00139                                          std::auto_ptr< RefGetter > &  theGetter ) const
00140 {
00141   //  define is supposed to be call by an EDProducer module, which wil put the RefGetter in the event
00142   //  so that reference can be made to it.
00143   //  the lazy getter is retrieved by the calling module and passed along with the event
00144   //  the map is cleared, except for pixel
00145   //  then the known elementIndex are defined to the RefGetter. no unpacking is done at this time
00146   //  the defined region range is registered in the DetODMap for further use.
00147 
00148   //clear all defined tags to start from scratch (except for pixel)
00149   for (DetODContainer::iterator it=theDetODMap.begin(); it!= theDetODMap.end();++it)
00150     {
00151       if (it->second.updated && !it->second.defined) continue; //special treatement for pixels
00152       it->second.defined= false; 
00153       it->second.updated = false;
00154     }
00155 
00156   //define all the elementindex in the refgetter
00157   for( std::map<SiStripRegionCabling::ElementIndex, std::vector<DetODContainer::iterator> >::iterator eIt= region_mapping.begin();
00158        eIt!=region_mapping.end();++eIt){
00159     std::pair<unsigned int, unsigned int> region_range; 
00160     
00161     //before update of the refgetter
00162     region_range.first = theGetter->size();
00163     //update the refegetter with the elementindex
00164     theStripRegionCabling->updateSiStripRefGetter<SiStripCluster> (*theGetter, theLazyGetterH, eIt->first);
00165     //after update of the refgetter
00166     region_range.second = theGetter->size();
00167 
00168     LogDebug(category_)<<"between index: "<<region_range.first<<" "<<region_range.second
00169                        <<"\n"<<dumpRegion(region_range,*theGetter,StayPacked_);
00170     
00171     //now assign to each measurement det for that element index
00172     for (std::vector<DetODContainer::iterator>::iterator dIt=eIt->second.begin();
00173          dIt!=eIt->second.end();++dIt){
00174       (*dIt)->second.region_range = region_range;
00175       (*dIt)->second.defined=true;
00176       LogDebug(category_)<<"detId: "<<(*dIt)->first.rawId()<<" in region range: "<<region_range.first<<" "<<region_range.second;
00177     }//loop over MeasurementDet attached to that elementIndex
00178   }//loop over know elementindex
00179 }
00180 
00181 void OnDemandMeasurementTracker::updateStrips( const edm::Event& event) const 
00182 {
00183   bool oncePerEvent= edm::Service<UpdaterService>()->checkOnce("OnDemandMeasurementTracker::updateStrips::"+name_);
00184   bool failedToGet = false;
00185   if (!oncePerEvent)
00186     failedToGet = theRefGetterH.failedToGet() || theLazyGetterH.failedToGet();
00187 
00188   if (oncePerEvent || failedToGet)
00189     {
00190       LogDebug(category_)<<"Updating siStrip on event: "<< (unsigned int) event.id().run() <<" : "<<(unsigned int) event.id().event();
00191       
00192       //get the ref getter back from the event
00193       std::string stripClusterProducer = pset_.getParameter<std::string>("stripClusterProducer");
00194       event.getByLabel(stripClusterProducer,theRefGetterH);
00195       
00196       std::string stripLazyGetter = pset_.getParameter<std::string>("stripLazyGetterProducer");
00197       event.getByLabel(stripLazyGetter,theLazyGetterH);
00198 
00199       //get the skip clusters
00200       if (selfUpdateSkipClusters_){
00201         theSkipClusterRefs=true;
00202         event.getByLabel(pset_.getParameter<edm::InputTag>("skipClusters"),theStripClusterRefs);
00203       }
00204 
00205       //get the detid that are inactive
00206       theRawInactiveStripDetIds.clear();
00207       getInactiveStrips(event,theRawInactiveStripDetIds);
00208     }
00209 }
00210 
00211 void OnDemandMeasurementTracker::update( const edm::Event& event) const
00212 {
00213   //  update is supposed to be called by any module that is useing the MeasurementTracker
00214   //  after checking the the event has not yet been seen
00215   //  update the pixel using MeasurementTracekr specific function
00216   //  retreive the RefGetter from the event: the very one that has been pass to define(...) and put into the event
00217 
00218   if (!PixelOnDemand_) {
00219     LogDebug(category_)<<"pixel are not OnDemand. updating them a la MeasurmentTracker.";
00220     MeasurementTracker::updatePixels(event);}
00221   else{
00222     edm::LogError(category_)<<"trying to update siPixel as on-demand. Not Implemented yet.";
00223   }
00224 
00225   if (!StripOnDemand_) {
00226     LogDebug(category_)<<"strip are not OnDemand. updating them a la MeasurmentTracker.";
00227     MeasurementTracker::updateStrips(event);}
00228   else{
00229     LogDebug(category_)<<"strip are OnDemand. updating them a la OnDemandMeasurmentTracker."; 
00230     updateStrips(event);
00231   }
00232 }
00233 
00234 #include <sstream>
00235 
00236 std::string OnDemandMeasurementTracker::dumpCluster(const std::vector<SiStripCluster> ::const_iterator & begin,const  std::vector<SiStripCluster> ::const_iterator & end)const
00237 {
00238   //  dumpCluster is a printout of all the clusters between the iterator. returns a string
00239   std::string tab="      ";
00240   std::stringstream ss;
00241   std::vector<SiStripCluster> ::const_iterator it = begin;
00242   unsigned int i=0;
00243   for (;it!=end;++it){
00244     ss<<tab<<i++<<") center: "<<it->barycenter()<<",id: "<<it->geographicalId()<<" with: "<<it->amplitudes().size()<<" strips\n"<<tab<<tab<<"{";
00245     for (unsigned int is=0;is!=it->amplitudes().size();++is){
00246       ss<<it->amplitudes()[is]<<" ";
00247     }ss<<"}\n";
00248   }
00249   return ss.str();
00250 }
00251 
00252 std::string OnDemandMeasurementTracker::dumpRegion(std::pair<unsigned int,unsigned int> indexes,
00253                                              const RefGetter & theGetter,
00254                                              bool stayPacked)const
00255 {
00256   //  dumpRegion is a printout of all the clusters in a region defined on the RefGetter. returns a string
00257   std::stringstream ss;
00258   ss<<"cluster between: "<<indexes.first<<" and: "<<indexes.second<<"\n";
00259   for (unsigned int iRegion = indexes.first; iRegion != indexes.second; ++iRegion){    
00260     uint32_t reg = SiStripRegionCabling::region((theGetter)[iRegion].region());
00261     SiStripRegionCabling::Position pos = theStripRegionCabling->position(reg);
00262     SiStripRegionCabling::PositionIndex posI = theStripRegionCabling->positionIndex(reg);
00263     
00264     ss<<"Clusters for region:["<<iRegion<<"]"
00265       <<"\n element index: "<<(theGetter)[iRegion].region()
00266       <<"\n region absolute index: "<<reg
00267       <<"\n region position index: "<<posI.first<<" "<<posI.second
00268       <<"\n region position: "<<pos.first<<" "<<pos.second
00269       <<"\n"<< (stayPacked? " hidden to avoid unpacking." : dumpCluster((theGetter)[iRegion].begin(),(theGetter)[iRegion].end()));
00270   }
00271   return ss.str();
00272 }
00273 
00274 void OnDemandMeasurementTracker::assign(const TkStripMeasurementDet * csmdet,
00275                                   DetODContainer::iterator * alreadyFound)const {
00276   //  assign is using the handle to the refgetter and the region index range to update the MeasurementDet with their clusters
00277   
00278   TkStripMeasurementDet * smdet = const_cast<TkStripMeasurementDet *>(csmdet);
00279   DetId id = smdet->geomDet().geographicalId();
00280   
00281   LogDebug(category_)<<"assigning: "<<id.rawId();
00282 
00283   // what is the iterator. do not look again if already found and provided with
00284   DetODContainer::iterator elementInMap;
00285   if (alreadyFound){ elementInMap=*alreadyFound;}
00286   else{ elementInMap = theDetODMap.find(id);}
00287   
00288   if  ( elementInMap != theDetODMap.end()){
00289     //flag it as updated
00290     elementInMap->second.updated = true;
00291 
00292     if (!theRawInactiveStripDetIds.empty() && std::binary_search(theRawInactiveStripDetIds.begin(), theRawInactiveStripDetIds.end(), id)) {
00293                 smdet->setActiveThisEvent(false);
00294                           return;
00295     }
00296 
00297     //retrieve the region range index for this module
00298     std::pair<unsigned int,unsigned int> & indexes =elementInMap->second.region_range;
00299 
00300     //this printout will trigger unpacking. no problem. it is done on the next regular line (find(id.rawId())
00301     LogDebug(category_)<<"between index: "<<indexes.first<<" and: "<<indexes.second
00302                        <<"\nretrieved for module: "<<id.rawId()
00303                        <<"\n"<<dumpRegion(indexes,*theRefGetterH);
00304     
00305     //look for iterator range in the regions defined for that module
00306     for (unsigned int iRegion = indexes.first; iRegion != indexes.second; ++iRegion){
00307       RefGetter::record_pair range = (*theRefGetterH)[iRegion].find(id.rawId());
00308       if (range.first!=range.second){
00309         //      found something not empty
00310         //update the measurementDet
00311         smdet->update(range.first, range.second, theLazyGetterH, id);
00312         LogDebug(category_)<<"Valid clusters for: "<<id.rawId()
00313                            <<"\nnumber of regions defined here: "<< indexes.second-indexes.first
00314                            <<"\n"<<dumpCluster(range.first,range.second);
00315         if (selfUpdateSkipClusters_){
00316           //assign skip clusters
00317           edmNew::DetSetVector<TkStripMeasurementDet::SiStripRegionalClusterRef>::const_iterator f=theStripClusterRefs->find(id);
00318           if (f!=theStripClusterRefs->end())
00319             smdet->setRegionalClustersToSkip(f->begin(),f->end());
00320           else
00321             smdet->unset();
00322         }
00323         //and you are done
00324         return;}
00325     }//loop over regions, between indexes
00326 
00327     //if reached. no cluster are found. set the TkStripMeasurementDet to be empty
00328     smdet->setEmpty();
00329 
00330   }//found in the map
00331   else{
00332     //throw excpetion
00333     edm::LogError(category_)<<"failed to find the MeasurementDet for: "<<id.rawId();
00334     throw MeasurementDetException("failed to find the MeasurementDet for: <see message logger>");
00335   }
00336 }
00337 
00338 
00339 
00340 const MeasurementDet* 
00341 OnDemandMeasurementTracker::idToDet(const DetId& id) const
00342 {
00343   //  overloaded from MeasurementTracker
00344   //  find the detid. if not found throw exception
00345   //  if already updated: always for pixel or strip already queried. return it
00346   DetODContainer::iterator it = theDetODMap.find(id);
00347   if ( it != theDetODMap.end()) {
00348     
00349     //it has already been queried. and so already updated: nothing to be done here (valid for pixels too)
00350     if (it->second.updated){LogDebug(category_)<<"found id: "<<id.rawId()<<" as aleardy updated."; return it->second.mdet;}
00351     
00352     if (StripOnDemand_){
00353       //check glued or single
00354       std::vector< const GeomDet*> comp = it->second.mdet->geomDet().components();
00355       if (!comp.empty()){
00356         //glued det
00357         LogDebug(category_)<<"updating glued id: "<<id.rawId()<<" ("<<comp.size()<<").";
00358         //cast to specific type
00359         TkGluedMeasurementDet*  theConcreteDet = static_cast<TkGluedMeasurementDet*>(it->second.mdet);
00360                 
00361         //      update the two components
00362         //update the mono
00363         assign(theConcreteDet->monoDet());
00364         //update the stereo
00365         assign(theConcreteDet->stereoDet());
00366               
00367         //flag the glued det as updated (components are flagged in assign)
00368         it->second.updated=true;
00369       }//glued det
00370       else{
00371         //single layer 
00372         LogDebug(category_)<<"updating singel id: "<<id.rawId();
00373         //cast to specific type
00374         TkStripMeasurementDet*  theConcreteDet = static_cast<TkStripMeasurementDet*>(it->second.mdet);
00375 
00376         //update the single layer
00377         assign(theConcreteDet,&it);
00378       }//single det
00379     }
00380     //eventually return it
00381     return it->second.mdet;
00382   }//found in the DetODMap
00383   else{
00384     //throw excpetion
00385     edm::LogError(category_)<<"failed to find the MeasurementDet for: "<<id.rawId();
00386     throw MeasurementDetException("failed to find the MeasurementDet for: <see message logger>");
00387   }
00388   return 0;
00389 }
00390 
00391