CMS 3D CMS Logo

AlignableNavigator.cc

Go to the documentation of this file.
00001 //  \file AlignableNavigator.cc
00002 //
00003 //   $Revision: 1.20 $
00004 //   $Date: 2007/10/08 13:21:29 $
00005 //   (last update by $Author: cklae $)
00006 
00007 #include "Alignment/CommonAlignment/interface/AlignableDet.h"
00008 #include "Alignment/CommonAlignment/interface/AlignableDetUnit.h"
00009 #include "DataFormats/DetId/interface/DetId.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 
00012 #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"
00013 
00014 //_____________________________________________________________________________
00015 
00016 AlignableNavigator::AlignableNavigator( Alignable* tracker, Alignable* muon )
00017 {
00018   theMap.clear();
00019 
00020   const unsigned int numNonDets = this->recursiveGetId(tracker) + this->recursiveGetId(muon);
00021   if (numNonDets) {
00022     edm::LogWarning("Alignment") <<"@SUB=AlignableNavigator" << "Created with map of size "
00023                                  << theMap.size() << ", but found also " << numNonDets 
00024                                  << " Alignables that have DetId!=0,\nbeing neither "
00025                                  << "AlignableDet nor AlignableDetUnit. This will "
00026                                  << "lead to an exception in case alignableFromDetId(..) "
00027                                  << "is called for one of these DetIds.\n" 
00028                                  << "If there is no exception, you can ignore this message.";
00029   } else {
00030     edm::LogInfo("Alignment") <<"@SUB=AlignableNavigator" << "Created with map of size "
00031                               << theMap.size() << ".";
00032   }
00033 }
00034 
00035 
00036 //_____________________________________________________________________________
00037 
00038 AlignableNavigator::AlignableNavigator( std::vector<Alignable*> alignables )
00039 {
00040   theMap.clear();
00041 
00042   unsigned int numNonDets = 0;
00043   for ( std::vector<Alignable*>::iterator it = alignables.begin(); it != alignables.end(); ++it ) {
00044     numNonDets += this->recursiveGetId(*it);
00045   }
00046   if (numNonDets) {
00047     edm::LogWarning("Alignment") <<"@SUB=AlignableNavigator" << "Created with map of size "
00048                                  << theMap.size() << ", but found also " << numNonDets 
00049                                  << " Alignables that have DetId!=0,\nbeing neither "
00050                                  << "AlignableDet nor AlignableDetUnit. This will "
00051                                  << "lead to an exception in case alignableFromDetId(..) "
00052                                  << "is called for one of these DetIds.\n" 
00053                                  << "If there is no exception, you can ignore this message.";
00054   } else {
00055     edm::LogInfo("Alignment") <<"@SUB=AlignableNavigator" << "created with map of size "
00056                               << theMap.size() << ".";
00057   }
00058 }
00059 
00060 //_____________________________________________________________________________
00061 AlignableDetOrUnitPtr AlignableNavigator::alignableFromGeomDet( const GeomDet* geomDet )
00062 {
00063   return alignableFromDetId( geomDet->geographicalId() );
00064 }
00065 
00066 //_____________________________________________________________________________
00067 AlignableDetOrUnitPtr AlignableNavigator::alignableFromDetId( const DetId& detid )
00068 {
00069 
00070   MapType::iterator position = theMap.find( detid );
00071   if ( position != theMap.end() ) return position->second;
00072   throw cms::Exception("BadLogic") 
00073     << "[AlignableNavigator::alignableDetFromDetId] DetId " << detid.rawId() << " not found";
00074 
00075   return static_cast<AlignableDet*>(0);
00076 }
00077 
00078 //_____________________________________________________________________________
00079 AlignableDet* AlignableNavigator::alignableDetFromGeomDet( const GeomDet* geomDet )
00080 {
00081   return alignableDetFromDetId( geomDet->geographicalId() );
00082 }
00083 
00084 //_____________________________________________________________________________
00085 AlignableDet* AlignableNavigator::alignableDetFromDetId( const DetId &detId )
00086 {
00087   AlignableDetOrUnitPtr ali = this->alignableFromDetId(detId);
00088   AlignableDet *aliDet = ali.alignableDet();
00089   if (!aliDet) {
00090     AlignableDetUnit *aliDetUnit = ali.alignableDetUnit();
00091     if (!aliDetUnit) {
00092       throw cms::Exception("BadAssociation") 
00093         << "[AlignableNavigator::alignableDetFromDetId]" 
00094         << " Neither AlignableDet nor AlignableDetUnit";
00095     }
00096     aliDet = dynamic_cast<AlignableDet*>(aliDetUnit->mother());
00097     if (!aliDet) {
00098       throw cms::Exception("BadLogic") 
00099         << "[AlignableNavigator::alignableDetFromDetId]" << " AlignableDetUnit, but "
00100         << (aliDetUnit->mother() ? " mother not an AlignableDet." : "without mother.");
00101     }
00102     edm::LogWarning("Alignment") << "@SUB=AlignableNavigator::alignableDetFromDetId"
00103                                  << "Returning AlignableDet although DetId belongs"
00104                                  << " to AlignableDetUnit. Might become exception in future,"
00105                                  << " use alignableFromDetId/GeomDet instead!";
00106   }
00107   return aliDet; 
00108 }
00109 
00110 //_____________________________________________________________________________
00111 
00112 unsigned int AlignableNavigator::recursiveGetId( Alignable* alignable )
00113 {
00114   // Recursive method to get the detIds of an alignable and its childs
00115   // and add the to the map.
00116   // Returns number of Alignables with DetId which are neither AlignableDet
00117   // nor AlignableDetUnit and are thus not added to the map.
00118 
00119   if (!alignable) return 0;
00120 
00121   unsigned int nProblem = 0;
00122   const DetId detId(alignable->geomDetId());
00123   if ( detId.rawId()) {
00124     AlignableDet *aliDet = dynamic_cast<AlignableDet*>(alignable);
00125     if (aliDet) {
00126       theMap.insert( PairType( detId, aliDet ) );
00127     } else {
00128       AlignableDetUnit *aliDetUnit = dynamic_cast<AlignableDetUnit*>(alignable);
00129       if (aliDetUnit) {
00130         theMap.insert( PairType( detId, aliDetUnit ) );
00131       } else {
00132         nProblem = 1; // equivalent to '++nProblem;' which could confuse to be ina loop...
00133 // Cannot be an exception since it happens (illegaly) in Muon DT hierarchy:
00134 //         throw cms::Exception("BadLogic") 
00135 //           << "[AlignableNavigator::recursiveGetId] Alignable with DetId " << detId.rawId() 
00136 //           << " neither AlignableDet nor AlignableDetUnit";
00137       }
00138     }
00139     if (!nProblem && !this->detAndSubdetInMap(detId)) {
00140       theDetAndSubdet.push_back(std::pair<int, int>( detId.det(), detId.subdetId() ));
00141     }
00142   }
00143   std::vector<Alignable*> comp = alignable->components();
00144   if ( alignable->alignableObjectId() != align::AlignableDet
00145        || comp.size() > 1 ) { // Non-glued AlignableDets contain themselves
00146     for ( std::vector<Alignable*>::iterator it = comp.begin(); it != comp.end(); ++it ) {
00147       nProblem += this->recursiveGetId(*it);
00148     }
00149   }
00150   return nProblem;
00151 }
00152 
00153 //_____________________________________________________________________________
00154 std::vector<AlignableDetOrUnitPtr>
00155 AlignableNavigator::alignablesFromHits( const std::vector<const TransientTrackingRecHit*>& hitvec )
00156 {
00157   std::vector<AlignableDetOrUnitPtr> result;
00158   result.reserve(hitvec.size());
00159 
00160   for(std::vector<const TransientTrackingRecHit*>::const_iterator ih
00161         = hitvec.begin(), iEnd = hitvec.end(); ih != iEnd; ++ih) {
00162     result.push_back(this->alignableFromDetId((*ih)->geographicalId()));
00163   }
00164 
00165   return result;
00166 }
00167 
00168 //_____________________________________________________________________________
00169 std::vector<AlignableDetOrUnitPtr>
00170 AlignableNavigator::alignablesFromHits
00171 (const TransientTrackingRecHit::ConstRecHitContainer &hitVec)
00172 {
00173 
00174   std::vector<AlignableDetOrUnitPtr> result;
00175   result.reserve(hitVec.size());
00176   
00177   for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator it
00178          = hitVec.begin(), iEnd = hitVec.end(); it != iEnd; ++it) {
00179     result.push_back(this->alignableFromDetId((*it)->geographicalId()));
00180   }
00181 
00182   return result;
00183 }
00184 
00185 //_____________________________________________________________________________
00186 std::vector<AlignableDet*>
00187 AlignableNavigator::alignableDetsFromHits( const std::vector<const TransientTrackingRecHit*>& hitvec )
00188 {
00189   std::vector<AlignableDet*> result;
00190   result.reserve(hitvec.size());
00191 
00192   for(std::vector<const TransientTrackingRecHit*>::const_iterator ih
00193         = hitvec.begin(), iEnd = hitvec.end(); ih != iEnd; ++ih) {
00194     result.push_back(this->alignableDetFromDetId((*ih)->geographicalId()));
00195   }
00196 
00197   return result;
00198 }
00199 
00200 //_____________________________________________________________________________
00201 std::vector<AlignableDet*>
00202 AlignableNavigator::alignableDetsFromHits
00203 (const TransientTrackingRecHit::ConstRecHitContainer &hitVec)
00204 {
00205   std::vector<AlignableDet*> result;
00206   result.reserve(hitVec.size());
00207   
00208   for (TransientTrackingRecHit::ConstRecHitContainer::const_iterator it
00209          = hitVec.begin(), iEnd = hitVec.end(); it != iEnd; ++it) {
00210     result.push_back(this->alignableDetFromDetId((*it)->geographicalId()));
00211   }
00212 
00213   return result;
00214 }
00215 
00216 //_____________________________________________________________________________
00217 
00218 bool AlignableNavigator::detAndSubdetInMap( const DetId& detid ) const
00219 {
00220    int det = detid.det();
00221    int subdet = detid.subdetId();
00222    for (std::vector<std::pair<int, int> >::const_iterator i = theDetAndSubdet.begin();  i != theDetAndSubdet.end();  ++i) {
00223       if (det == i->first  &&  subdet == i->second) return true;
00224    }
00225    return false;
00226 }

Generated on Tue Jun 9 17:23:45 2009 for CMSSW by  doxygen 1.5.4