CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CalibTracker/SiStripCommon/plugins/ShallowGainCalibration.cc

Go to the documentation of this file.
00001 #include "CalibTracker/SiStripCommon/interface/ShallowGainCalibration.h"
00002 #include "CalibFormats/SiStripObjects/interface/SiStripGain.h" 
00003 #include "CalibTracker/Records/interface/SiStripGainRcd.h"  
00004 
00005 using namespace edm;
00006 using namespace reco;
00007 using namespace std;
00008 
00009 
00010 
00011 
00012 ShallowGainCalibration::ShallowGainCalibration(const edm::ParameterSet& iConfig)
00013   :  theTracksLabel( iConfig.getParameter<edm::InputTag>("Tracks") ),
00014      Suffix       ( iConfig.getParameter<std::string>("Suffix")    ),
00015      Prefix       ( iConfig.getParameter<std::string>("Prefix") )
00016 {
00017   produces <std::vector<int> >            ( Prefix + "trackindex"     + Suffix );
00018   produces <std::vector<unsigned int> >   ( Prefix + "rawid"          + Suffix );
00019   produces <std::vector<float> >          ( Prefix + "localdirx"      + Suffix );
00020   produces <std::vector<float> >          ( Prefix + "localdiry"      + Suffix );
00021   produces <std::vector<float> >          ( Prefix + "localdirz"      + Suffix );
00022   produces <std::vector<unsigned short> > ( Prefix + "firststrip"     + Suffix );
00023   produces <std::vector<unsigned short> > ( Prefix + "nstrips"        + Suffix );
00024   produces <std::vector<bool> >           ( Prefix + "saturation"     + Suffix );
00025   produces <std::vector<bool> >           ( Prefix + "overlapping"    + Suffix );
00026   produces <std::vector<bool> >           ( Prefix + "farfromedge"    + Suffix );
00027   produces <std::vector<unsigned int> >   ( Prefix + "charge"         + Suffix );
00028   produces <std::vector<float> >          ( Prefix + "path"           + Suffix );
00029   produces <std::vector<float> >          ( Prefix + "chargeoverpath" + Suffix );
00030   produces <std::vector<unsigned char> >  ( Prefix + "amplitude"      + Suffix );
00031   produces <std::vector<double> >         ( Prefix + "gainused"       + Suffix );
00032 }
00033 
00034 void ShallowGainCalibration::
00035 produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
00036   std::auto_ptr<std::vector<int> >            trackindex    ( new std::vector<int>            );
00037   std::auto_ptr<std::vector<unsigned int> >   rawid         ( new std::vector<unsigned int>   );
00038   std::auto_ptr<std::vector<float>  >         localdirx     ( new std::vector<float>          );
00039   std::auto_ptr<std::vector<float>  >         localdiry     ( new std::vector<float>          );
00040   std::auto_ptr<std::vector<float>  >         localdirz     ( new std::vector<float>          );
00041   std::auto_ptr<std::vector<unsigned short> > firststrip    ( new std::vector<unsigned short> );
00042   std::auto_ptr<std::vector<unsigned short> > nstrips       ( new std::vector<unsigned short> );
00043   std::auto_ptr<std::vector<bool> >           saturation    ( new std::vector<bool>           );
00044   std::auto_ptr<std::vector<bool> >           overlapping   ( new std::vector<bool>           );
00045   std::auto_ptr<std::vector<bool> >           farfromedge   ( new std::vector<bool>           );
00046   std::auto_ptr<std::vector<unsigned int> >   charge        ( new std::vector<unsigned int>   );
00047   std::auto_ptr<std::vector<float>  >         path          ( new std::vector<float>          );
00048   std::auto_ptr<std::vector<float>  >         chargeoverpath( new std::vector<float>          );
00049   std::auto_ptr<std::vector<unsigned char> >  amplitude     ( new std::vector<unsigned char>  );
00050   std::auto_ptr<std::vector<double>  >        gainused      ( new std::vector<double>          );
00051 
00052   edm::ESHandle<TrackerGeometry> theTrackerGeometry;         iSetup.get<TrackerDigiGeometryRecord>().get( theTrackerGeometry );  
00053   m_tracker=&(* theTrackerGeometry );
00054   edm::ESHandle<SiStripGain> gainHandle;                     iSetup.get<SiStripGainRcd>().get(gainHandle);
00055   edm::Handle<edm::View<reco::Track> > tracks;               iEvent.getByLabel(theTracksLabel, tracks);   
00056   edm::Handle<TrajTrackAssociationCollection> associations;  iEvent.getByLabel(theTracksLabel, associations);
00057 
00058   for( TrajTrackAssociationCollection::const_iterator association = associations->begin(); association != associations->end(); association++) {
00059        const Trajectory*  traj  = association->key.get();
00060        const reco::Track* track = association->val.get();
00061 
00062        vector<TrajectoryMeasurement> measurements = traj->measurements();
00063        for(vector<TrajectoryMeasurement>::const_iterator measurement_it = measurements.begin(); measurement_it!=measurements.end(); measurement_it++){
00064           TrajectoryStateOnSurface trajState = measurement_it->updatedState();
00065           if( !trajState.isValid() ) continue;     
00066 
00067           const TrackingRecHit*         hit                 = (*measurement_it->recHit()).hit();
00068           const SiStripRecHit1D*        sistripsimple1dhit  = dynamic_cast<const SiStripRecHit1D*>(hit);
00069           const SiStripRecHit2D*        sistripsimplehit    = dynamic_cast<const SiStripRecHit2D*>(hit);
00070           const SiStripMatchedRecHit2D* sistripmatchedhit   = dynamic_cast<const SiStripMatchedRecHit2D*>(hit);
00071 
00072           const SiStripCluster*   Cluster = NULL;
00073           uint32_t                DetId = 0;
00074 
00075           for(unsigned int h=0;h<2;h++){
00076             if(!sistripmatchedhit && h==1){
00077                continue;
00078             }else if(sistripmatchedhit  && h==0){
00079                Cluster = &sistripmatchedhit->monoCluster();
00080                DetId = sistripmatchedhit->monoId();
00081             }else if(sistripmatchedhit  && h==1){
00082                Cluster = &sistripmatchedhit->stereoCluster();;
00083                DetId = sistripmatchedhit->stereoId();
00084             }else if(sistripsimplehit){
00085                Cluster = (sistripsimplehit->cluster()).get();
00086                DetId = sistripsimplehit->geographicalId().rawId();
00087             }else if(sistripsimple1dhit){
00088                Cluster = (sistripsimple1dhit->cluster()).get();
00089                DetId = sistripsimple1dhit->geographicalId().rawId();
00090             }else{
00091                continue;
00092             }
00093 
00094             LocalVector             trackDirection = trajState.localDirection();
00095             double                  cosine         = trackDirection.z()/trackDirection.mag();
00096             const vector<uint8_t>&  Ampls          = Cluster->amplitudes();
00097             int                     FirstStrip     = Cluster->firstStrip();
00098             int                     APVId          = FirstStrip/128;
00099             bool                    Saturation     = false;
00100             bool                    Overlapping    = false;
00101             unsigned int            Charge         = 0;
00102             double                  Path           = (10.0*thickness(DetId))/fabs(cosine);
00103             double                  PrevGain       = -1;
00104 
00105             if(gainHandle.isValid()){ 
00106                SiStripApvGain::Range detGainRange = gainHandle->getRange(DetId);
00107                PrevGain = *(detGainRange.first + APVId);
00108             }
00109 
00110             for(unsigned int a=0;a<Ampls.size();a++){               
00111                Charge+=Ampls[a];
00112                if(Ampls[a] >=254)Saturation =true;
00113                amplitude->push_back( Ampls[a] );
00114             }
00115             double                   ChargeOverPath = (double)Charge / Path ;
00116 
00117             if(FirstStrip==0                                  )Overlapping=true;
00118             if(FirstStrip==128                                )Overlapping=true;
00119             if(FirstStrip==256                                )Overlapping=true;
00120             if(FirstStrip==384                                )Overlapping=true;
00121             if(FirstStrip==512                                )Overlapping=true;
00122             if(FirstStrip==640                                )Overlapping=true;
00123 
00124             if(FirstStrip<=127 && FirstStrip+Ampls.size()>127)Overlapping=true;
00125             if(FirstStrip<=255 && FirstStrip+Ampls.size()>255)Overlapping=true;
00126             if(FirstStrip<=383 && FirstStrip+Ampls.size()>383)Overlapping=true;
00127             if(FirstStrip<=511 && FirstStrip+Ampls.size()>511)Overlapping=true;
00128             if(FirstStrip<=639 && FirstStrip+Ampls.size()>639)Overlapping=true;
00129 
00130             if(FirstStrip+Ampls.size()==127                   )Overlapping=true;
00131             if(FirstStrip+Ampls.size()==255                   )Overlapping=true;
00132             if(FirstStrip+Ampls.size()==383                   )Overlapping=true;
00133             if(FirstStrip+Ampls.size()==511                   )Overlapping=true;
00134             if(FirstStrip+Ampls.size()==639                   )Overlapping=true;
00135             if(FirstStrip+Ampls.size()==767                   )Overlapping=true;
00136 
00137             trackindex    ->push_back( shallow::findTrackIndex(tracks, track) ); 
00138             rawid         ->push_back( DetId );         
00139             localdirx     ->push_back( trackDirection.x() );
00140             localdiry     ->push_back( trackDirection.y() );
00141             localdirz     ->push_back( trackDirection.z() );
00142             firststrip    ->push_back( FirstStrip );
00143             nstrips       ->push_back( Ampls.size() );
00144             saturation    ->push_back( Saturation );
00145             overlapping   ->push_back( Overlapping );
00146             farfromedge   ->push_back( IsFarFromBorder(&trajState,DetId, &iSetup) );
00147             charge        ->push_back( Charge );
00148             path          ->push_back( Path );
00149             chargeoverpath->push_back( ChargeOverPath );
00150             gainused      ->push_back( PrevGain );  
00151           }
00152        }
00153   }
00154 
00155   iEvent.put(trackindex,    Prefix + "trackindex"    + Suffix );
00156   iEvent.put(rawid     ,    Prefix + "rawid"         + Suffix );
00157   iEvent.put(localdirx ,    Prefix + "localdirx"     + Suffix );
00158   iEvent.put(localdiry ,    Prefix + "localdiry"     + Suffix );
00159   iEvent.put(localdirz ,    Prefix + "localdirz"     + Suffix );
00160   iEvent.put(firststrip,    Prefix + "firststrip"    + Suffix );
00161   iEvent.put(nstrips,       Prefix + "nstrips"       + Suffix );
00162   iEvent.put(saturation,    Prefix + "saturation"    + Suffix );
00163   iEvent.put(overlapping,   Prefix + "overlapping"   + Suffix );
00164   iEvent.put(farfromedge,   Prefix + "farfromedge"   + Suffix );
00165   iEvent.put(charge,        Prefix + "charge"        + Suffix );
00166   iEvent.put(path,          Prefix + "path"          + Suffix );
00167   iEvent.put(chargeoverpath,Prefix + "chargeoverpath"+ Suffix );
00168   iEvent.put(amplitude,     Prefix + "amplitude"     + Suffix );
00169   iEvent.put(gainused,      Prefix + "gainused"      + Suffix );
00170 }
00171 
00172 /*
00173 void ShallowGainCalibration::beginJob(const edm::EventSetup& iSetup)
00174 {
00175    printf("Befin JOB\n");
00176 
00177    edm::ESHandle<TrackerGeometry> tkGeom;
00178    iSetup.get<TrackerDigiGeometryRecord>().get( tkGeom );
00179    vector<GeomDet*> Det = tkGeom->dets();
00180 
00181    edm::ESHandle<SiStripGain> gainHandle;
00182    iSetup.get<SiStripGainRcd>().get(gainHandle);
00183    if(!gainHandle.isValid()){printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");exit(0);}
00184 
00185    for(unsigned int i=0;i<Det.size();i++){
00186       DetId  Detid  = Det[i]->geographicalId();
00187       int    SubDet = Detid.subdetId();
00188 
00189       if( SubDet == StripSubdetector::TIB ||  SubDet == StripSubdetector::TID ||
00190           SubDet == StripSubdetector::TOB ||  SubDet == StripSubdetector::TEC  ){
00191 
00192           StripGeomDetUnit* DetUnit     = dynamic_cast<StripGeomDetUnit*> (Det[i]);
00193           if(!DetUnit)continue;
00194 
00195           const StripTopology& Topo     = DetUnit->specificTopology();
00196           unsigned int         NAPV     = Topo.nstrips()/128;
00197    
00198           for(unsigned int j=0;j<NAPV;j++){
00199                 stAPVGain* APV = new stAPVGain;
00200                 APV->DetId         = Detid.rawId();
00201                 APV->APVId         = j;
00202                 APV->PreviousGain  = 1;
00203 
00204                 APVsCollOrdered.push_back(APV);
00205                 APVsColl[(APV->DetId<<3) | APV->APVId] = APV;
00206           }
00207       }
00208    }
00209 }
00210 
00211 
00212 void ShallowGainCalibration::beginRun(edm::Run &, const edm::EventSetup &iSetup){
00213     printf("BEFIN RUN\n");
00214 
00215     edm::ESHandle<SiStripGain> gainHandle;
00216     iSetup.get<SiStripGainRcd>().get(gainHandle);
00217     if(!gainHandle.isValid()){printf("\n#####################\n\nERROR --> gainHandle is not valid\n\n#####################\n\n");exit(0);}
00218 
00219     for(std::vector<stAPVGain*>::iterator it = APVsCollOrdered.begin();it!=APVsCollOrdered.end();it++){
00220        stAPVGain* APV = *it;
00221        SiStripApvGain::Range detGainRange = gainHandle->getRange(APV->DetId);
00222        APV->PreviousGain = *(detGainRange.first + APV->APVId);
00223     }
00224 }
00225 */
00226 
00227 bool ShallowGainCalibration::IsFarFromBorder(TrajectoryStateOnSurface* trajState, const uint32_t detid, const edm::EventSetup* iSetup)
00228 { 
00229   edm::ESHandle<TrackerGeometry> tkGeom; iSetup->get<TrackerDigiGeometryRecord>().get( tkGeom );
00230 
00231   LocalPoint  HitLocalPos   = trajState->localPosition();
00232   LocalError  HitLocalError = trajState->localError().positionError() ;
00233 
00234   const GeomDetUnit* it = tkGeom->idToDetUnit(DetId(detid));
00235   if (dynamic_cast<const StripGeomDetUnit*>(it)==0 && dynamic_cast<const PixelGeomDetUnit*>(it)==0) {
00236      std::cout << "this detID doesn't seem to belong to the Tracker" << std::endl;
00237      return false;
00238   }
00239 
00240   const BoundPlane plane = it->surface();
00241   const TrapezoidalPlaneBounds* trapezoidalBounds( dynamic_cast<const TrapezoidalPlaneBounds*>(&(plane.bounds())));
00242   const RectangularPlaneBounds* rectangularBounds( dynamic_cast<const RectangularPlaneBounds*>(&(plane.bounds())));
00243 
00244   double DistFromBorder = 1.0;    
00245   double HalfLength     = it->surface().bounds().length() /2.0;
00246 
00247   if(trapezoidalBounds)
00248   {
00249      std::vector<float> const & parameters = (*trapezoidalBounds).parameters();
00250      HalfLength     = parameters[3];
00251   }else if(rectangularBounds){
00252      HalfLength     = it->surface().bounds().length() /2.0;
00253   }else{return false;}
00254 
00255   if (fabs(HitLocalPos.y())+HitLocalError.yy() >= (HalfLength - DistFromBorder) ) return false;
00256 
00257   return true;
00258 }
00259 
00260 
00261 double ShallowGainCalibration::thickness(DetId id)
00262 {
00263  map<DetId,double>::iterator th=m_thicknessMap.find(id);
00264  if(th!=m_thicknessMap.end())
00265    return (*th).second;
00266  else {
00267    double detThickness=1.;
00268    //compute thickness normalization
00269    const GeomDetUnit* it = m_tracker->idToDetUnit(DetId(id));
00270    bool isPixel = dynamic_cast<const PixelGeomDetUnit*>(it)!=0;
00271    bool isStrip = dynamic_cast<const StripGeomDetUnit*>(it)!=0;
00272    if (!isPixel && ! isStrip) {
00273    //FIXME throw exception
00274       edm::LogWarning("DeDxHitsProducer") << "\t\t this detID doesn't seem to belong to the Tracker";
00275       detThickness = 1.;
00276   }else{
00277       detThickness = it->surface().bounds().thickness();
00278   }
00279 
00280    m_thicknessMap[id]=detThickness;//computed value
00281    return detThickness;
00282  }
00283 
00284 }
00285