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/SiPixelRecHits/interface/CPEFromDetPosition.h"
00022 #include "RecoLocalTracker/ClusterParameterEstimator/interface/PixelClusterParameterEstimator.h"
00023 #include "RecoLocalTracker/Records/interface/TrackerCPERecord.h"
00024 #include "RecoLocalTracker/SiStripRecHitConverter/interface/SiStripRecHitMatcher.h"
00025 #include "RecoLocalTracker/SiStripRecHitConverter/interface/StripCPE.h"
00026
00027 #include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00028 #include "RecoTracker/Record/interface/TrackerRecoGeometryRecord.h"
00029 #include "RecoTracker/MeasurementDet/interface/TkStripMeasurementDet.h"
00030 #include "RecoTracker/MeasurementDet/interface/TkPixelMeasurementDet.h"
00031 #include "RecoTracker/MeasurementDet/interface/TkGluedMeasurementDet.h"
00032
00033 #include "Utilities/Timing/interface/TimingReport.h"
00034
00035 #include "CondFormats/SiStripObjects/interface/SiStripNoises.h"
00036 #include "CondFormats/DataRecord/interface/SiStripNoisesRcd.h"
00037
00038 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00039 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00040 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00041 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
00042 #include "CalibTracker/Records/interface/SiStripRegionCablingRcd.h"
00043
00044 #include <iostream>
00045 #include <typeinfo>
00046 #include <map>
00047
00048 #include <DataFormats/GeometrySurface/interface/BoundPlane.h>
00049 #include "DataFormats/Math/interface/deltaR.h"
00050
00051 #include "FWCore/ServiceRegistry/interface/Service.h"
00052 #include "FWCore/Services/interface/UpdaterService.h"
00053
00054 using namespace std;
00055
00056 OnDemandMeasurementTracker::OnDemandMeasurementTracker(const edm::ParameterSet& conf,
00057 const PixelClusterParameterEstimator* pixelCPE,
00058 const StripClusterParameterEstimator* stripCPE,
00059 const SiStripRecHitMatcher* hitMatcher,
00060 const TrackerGeometry* trackerGeom,
00061 const GeometricSearchTracker* geometricSearchTracker,
00062 const SiStripQuality *stripQuality,
00063 int qualityFlags,
00064 int qualityDebugFlags,
00065 const SiStripRegionCabling * stripRegionCabling,
00066 bool isRegional):
00067 MeasurementTracker(conf,pixelCPE,stripCPE,hitMatcher,trackerGeom,geometricSearchTracker,
00068 stripQuality,qualityFlags,qualityDebugFlags,isRegional)
00069 , category_("OnDemandMeasurementTracker")
00070 , StayPacked_(true)
00071 , StripOnDemand_(true)
00072 , PixelOnDemand_(false)
00073 , theStripRegionCabling(stripRegionCabling)
00074 {
00075
00076
00077
00078
00079
00080 for (DetContainer::iterator it=theDetMap.begin(); it!= theDetMap.end();++it)
00081 {
00082 DetODContainer::iterator inserted = theDetODMap.insert(make_pair(it->first,DetODStatus(const_cast<MeasurementDet*>(it->second)))).first;
00083
00084
00085 GeomDet::SubDetector subdet = it->second->geomDet().subDetector();
00086 if (subdet == GeomDetEnumerators::PixelBarrel || subdet == GeomDetEnumerators::PixelEndcap ){
00087
00088
00089 inserted->second.defined=false;
00090 inserted->second.updated=true;
00091 }
00092 else if (subdet == GeomDetEnumerators::TIB || subdet == GeomDetEnumerators::TOB ||
00093 subdet == GeomDetEnumerators::TID || subdet == GeomDetEnumerators::TEC )
00094 {
00095
00096 inserted->second.defined=false;
00097 inserted->second.updated=false;
00098
00099
00100 GlobalPoint center = it->second->geomDet().position();
00101 double eta = center.eta();
00102 double phi = center.phi();
00103 uint32_t id = it->first.rawId();
00104 SiStripRegionCabling::ElementIndex eIndex = theStripRegionCabling->elementIndex(SiStripRegionCabling::Position(eta,phi),
00105 SiStripRegionCabling::subdetFromDetId(id),
00106 SiStripRegionCabling::layerFromDetId(id));
00107 LogDebug(category_)<<"region selected (from "<<id<<" center) is:\n"
00108 <<"position: "<<center
00109 <<"\n center absolute index: "<<theStripRegionCabling->region(theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi)))
00110 <<"\n center position index: "<<theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi)).first<<
00111 " "<<theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi)).second
00112 <<"\n center postion: "<<theStripRegionCabling->position(theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi))).first<<
00113 " "<<theStripRegionCabling->position(theStripRegionCabling->positionIndex(SiStripRegionCabling::Position(eta,phi))).second
00114 <<"\n eta: "<<eta
00115 <<"\n phi: "<<phi
00116 <<"\n subedet: "<<SiStripRegionCabling::subdetFromDetId(id)
00117 <<" layer: "<<SiStripRegionCabling::layerFromDetId(id);
00118
00119
00120
00121 region_mapping[eIndex].push_back(inserted);
00122 }
00123 else{
00124
00125 edm::LogError(category_)<<"not a tracker geomdet in constructor: "<<it->first.rawId();
00126 throw MeasurementDetException("OnDemandMeasurementTracker dealing with a non tracker GeomDet.");
00127 }
00128 }
00129 }
00130
00131
00132 void OnDemandMeasurementTracker::define( const edm::Handle< LazyGetter> & theLazyGetterH,
00133 std::auto_ptr< RefGetter > & theGetter ) const
00134 {
00135
00136
00137
00138
00139
00140
00141
00142
00143 for (DetODContainer::iterator it=theDetODMap.begin(); it!= theDetODMap.end();++it)
00144 {
00145 if (it->second.updated && !it->second.defined) continue;
00146 it->second.defined= false;
00147 it->second.updated = false;
00148 }
00149
00150
00151 for( std::map<SiStripRegionCabling::ElementIndex, std::vector<DetODContainer::iterator> >::iterator eIt= region_mapping.begin();
00152 eIt!=region_mapping.end();++eIt){
00153 std::pair<uint, uint> region_range;
00154
00155
00156 region_range.first = theGetter->size();
00157
00158 theStripRegionCabling->updateSiStripRefGetter<SiStripCluster> (*theGetter, theLazyGetterH, eIt->first);
00159
00160 region_range.second = theGetter->size();
00161
00162 LogDebug(category_)<<"between index: "<<region_range.first<<" "<<region_range.second
00163 <<"\n"<<dumpRegion(region_range,*theGetter,StayPacked_);
00164
00165
00166 for (std::vector<DetODContainer::iterator>::iterator dIt=eIt->second.begin();
00167 dIt!=eIt->second.end();++dIt){
00168 (*dIt)->second.region_range = region_range;
00169 (*dIt)->second.defined=true;
00170 LogDebug(category_)<<"detId: "<<(*dIt)->first.rawId()<<" in region range: "<<region_range.first<<" "<<region_range.second;
00171 }
00172 }
00173 }
00174
00175 void OnDemandMeasurementTracker::updateStrips( const edm::Event& event) const
00176 {
00177 bool oncePerEvent= edm::Service<UpdaterService>()->checkOnce("OnDemandMeasurementTracker::updateStrips::"+name_);
00178 bool failedToGet = false;
00179 if (!oncePerEvent)
00180 failedToGet = theRefGetterH.failedToGet() || theLazyGetterH.failedToGet();
00181
00182 if (oncePerEvent || failedToGet)
00183 {
00184 LogDebug(category_)<<"Updating siStrip on event: "<< (uint) event.id().run() <<" : "<<(uint) event.id().event();
00185
00186
00187 std::string stripClusterProducer = pset_.getParameter<std::string>("stripClusterProducer");
00188 event.getByLabel(stripClusterProducer,theRefGetterH);
00189
00190 std::string stripLazyGetter = pset_.getParameter<std::string>("stripLazyGetterProducer");
00191 event.getByLabel(stripLazyGetter,theLazyGetterH);
00192 }
00193 }
00194
00195 void OnDemandMeasurementTracker::update( const edm::Event& event) const
00196 {
00197
00198
00199
00200
00201
00202 if (!PixelOnDemand_) {
00203 LogDebug(category_)<<"pixel are not OnDemand. updating them a la MeasurmentTracker.";
00204 MeasurementTracker::updatePixels(event);}
00205 else{
00206 edm::LogError(category_)<<"trying to update siPixel as on-demand. Not Implemented yet.";
00207 }
00208
00209 if (!StripOnDemand_) {
00210 LogDebug(category_)<<"strip are not OnDemand. updating them a la MeasurmentTracker.";
00211 MeasurementTracker::updateStrips(event);}
00212 else{
00213 LogDebug(category_)<<"strip are OnDemand. updating them a la OnDemandMeasurmentTracker.";
00214 updateStrips(event);
00215 }
00216 }
00217
00218 #include <sstream>
00219
00220 std::string OnDemandMeasurementTracker::dumpCluster(const std::vector<SiStripCluster> ::const_iterator & begin,const std::vector<SiStripCluster> ::const_iterator & end)const
00221 {
00222
00223 std::string tab=" ";
00224 std::stringstream ss;
00225 std::vector<SiStripCluster> ::const_iterator it = begin;
00226 uint i=0;
00227 for (;it!=end;++it){
00228 ss<<tab<<i++<<") center: "<<it->barycenter()<<",id: "<<it->geographicalId()<<" with: "<<it->amplitudes().size()<<" strips\n"<<tab<<tab<<"{";
00229 for (uint is=0;is!=it->amplitudes().size();++is){
00230 ss<<it->amplitudes()[is]<<" ";
00231 }ss<<"}\n";
00232 }
00233 return ss.str();
00234 }
00235
00236 std::string OnDemandMeasurementTracker::dumpRegion(std::pair<uint,uint> indexes,
00237 const RefGetter & theGetter,
00238 bool stayPacked)const
00239 {
00240
00241 std::stringstream ss;
00242 ss<<"cluster between: "<<indexes.first<<" and: "<<indexes.second<<"\n";
00243 for (uint iRegion = indexes.first; iRegion != indexes.second; ++iRegion){
00244 uint32_t reg = SiStripRegionCabling::region((theGetter)[iRegion].region());
00245 SiStripRegionCabling::Position pos = theStripRegionCabling->position(reg);
00246 SiStripRegionCabling::PositionIndex posI = theStripRegionCabling->positionIndex(reg);
00247
00248 ss<<"Clusters for region:["<<iRegion<<"]"
00249 <<"\n element index: "<<(theGetter)[iRegion].region()
00250 <<"\n region absolute index: "<<reg
00251 <<"\n region position index: "<<posI.first<<" "<<posI.second
00252 <<"\n region position: "<<pos.first<<" "<<pos.second
00253 <<"\n"<< (stayPacked? " hidden to avoid unpacking." : dumpCluster((theGetter)[iRegion].begin(),(theGetter)[iRegion].end()));
00254 }
00255 return ss.str();
00256 }
00257
00258 void OnDemandMeasurementTracker::assign(const TkStripMeasurementDet * csmdet,
00259 DetODContainer::iterator * alreadyFound)const {
00260
00261
00262 TkStripMeasurementDet * smdet = const_cast<TkStripMeasurementDet *>(csmdet);
00263 DetId id = smdet->geomDet().geographicalId();
00264
00265 LogDebug(category_)<<"assigning: "<<id.rawId();
00266
00267
00268 DetODContainer::iterator elementInMap;
00269 if (alreadyFound){ elementInMap=*alreadyFound;}
00270 else{ elementInMap = theDetODMap.find(id);}
00271
00272 if ( elementInMap != theDetODMap.end()){
00273
00274 elementInMap->second.updated = true;
00275
00276 std::pair<uint,uint> & indexes =elementInMap->second.region_range;
00277
00278
00279 LogDebug(category_)<<"between index: "<<indexes.first<<" and: "<<indexes.second
00280 <<"\nretrieved for module: "<<id.rawId()
00281 <<"\n"<<dumpRegion(indexes,*theRefGetterH);
00282
00283
00284 for (uint iRegion = indexes.first; iRegion != indexes.second; ++iRegion){
00285 RefGetter::record_pair range = (*theRefGetterH)[iRegion].find(id.rawId());
00286 if (range.first!=range.second){
00287
00288
00289 smdet->update(range.first, range.second, theLazyGetterH, id);
00290 LogDebug(category_)<<"Valid clusters for: "<<id.rawId()
00291 <<"\nnumber of regions defined here: "<< indexes.second-indexes.first
00292 <<"\n"<<dumpCluster(range.first,range.second);
00293
00294 return;}
00295 }
00296
00297
00298 smdet->setEmpty();
00299
00300 }
00301 else{
00302
00303 edm::LogError(category_)<<"failed to find the MeasurementDet for: "<<id.rawId();
00304 throw MeasurementDetException("failed to find the MeasurementDet for: <see message logger>");
00305 }
00306 }
00307
00308
00309
00310 const MeasurementDet*
00311 OnDemandMeasurementTracker::idToDet(const DetId& id) const
00312 {
00313
00314
00315
00316 DetODContainer::iterator it = theDetODMap.find(id);
00317 if ( it != theDetODMap.end()) {
00318
00319
00320 if (it->second.updated){LogDebug(category_)<<"found id: "<<id.rawId()<<" as aleardy updated."; return it->second.mdet;}
00321
00322 if (StripOnDemand_){
00323
00324 std::vector< const GeomDet*> comp = it->second.mdet->geomDet().components();
00325 if (!comp.empty()){
00326
00327 LogDebug(category_)<<"updating glued id: "<<id.rawId()<<" ("<<comp.size()<<").";
00328
00329 TkGluedMeasurementDet* theConcreteDet = dynamic_cast<TkGluedMeasurementDet*>(it->second.mdet);
00330
00331
00332 if (!theConcreteDet){
00333 edm::LogError(category_)<<"failed to cast to glued measurement det:"<<it->first.rawId();
00334 throw MeasurementDetException("failed to cast. see log file.");}
00335
00336
00337
00338 assign(theConcreteDet->monoDet());
00339
00340 assign(theConcreteDet->stereoDet());
00341
00342
00343 it->second.updated=true;
00344 }
00345 else{
00346
00347 LogDebug(category_)<<"updating singel id: "<<id.rawId();
00348
00349 TkStripMeasurementDet* theConcreteDet = dynamic_cast<TkStripMeasurementDet*>(it->second.mdet);
00350
00351
00352 if (!theConcreteDet){
00353 edm::LogError(category_)<<"failed to cast to strip measurement det:"<<it->first.rawId();
00354 throw MeasurementDetException("failed to cast. see log file.");}
00355
00356
00357 assign(theConcreteDet,&it);
00358 }
00359 }
00360
00361 return it->second.mdet;
00362 }
00363 else{
00364
00365 edm::LogError(category_)<<"failed to find the MeasurementDet for: "<<id.rawId();
00366 throw MeasurementDetException("failed to find the MeasurementDet for: <see message logger>");
00367 }
00368 return 0;
00369 }
00370
00371