CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DataFormats/SiStripCommon/src/SiStripDetKey.cc

Go to the documentation of this file.
00001 // Last commit: $Id: SiStripDetKey.cc,v 1.11 2009/12/10 08:44:16 elmer Exp $
00002 
00003 #include "DataFormats/SiStripCommon/interface/SiStripDetKey.h"
00004 #include "DataFormats/SiStripCommon/interface/Constants.h" 
00005 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00006 #include <iomanip>
00007 
00008 // -----------------------------------------------------------------------------
00009 // 
00010 SiStripDetKey::SiStripDetKey( const uint16_t& partition ) :
00011   SiStripKey(),
00012   partition_(partition),
00013   apvPairNumber_(sistrip::invalid_), 
00014   apvWithinPair_(sistrip::invalid_)
00015 {
00016   // order is important!
00017   initFromValue();
00018   initFromKey();
00019   initFromPath();
00020   initGranularity();
00021 }
00022 
00023 // -----------------------------------------------------------------------------
00024 // 
00025 SiStripDetKey::SiStripDetKey( const DetId& det_id,
00026                               const uint16_t& apv_pair_number,
00027                               const uint16_t& apv_within_pair ) :
00028   SiStripKey(),
00029   partition_(sistrip::invalid_),
00030   apvPairNumber_(apv_pair_number), 
00031   apvWithinPair_(apv_within_pair)
00032 {
00033   // order is important!
00034   initFromValue();
00035   initFromKey();
00036   initFromPath();
00037   initGranularity();
00038 }
00039 
00040 // -----------------------------------------------------------------------------
00041 // 
00042 SiStripDetKey::SiStripDetKey( const SiStripDetId& det_id ) :
00043   SiStripKey(),
00044   partition_(sistrip::invalid_),
00045   apvPairNumber_(sistrip::invalid_), 
00046   apvWithinPair_(sistrip::invalid_)
00047 {
00048   // order is important!
00049   initFromValue();
00050   initFromKey();
00051   initFromPath();
00052   initGranularity();
00053 }
00054 
00055 // -----------------------------------------------------------------------------
00056 // 
00057 SiStripDetKey::SiStripDetKey( const uint32_t& det_key ) :
00058   SiStripKey(det_key),
00059   partition_(sistrip::invalid_),
00060   apvPairNumber_(sistrip::invalid_), 
00061   apvWithinPair_(sistrip::invalid_)
00062 {
00063   // order is important!
00064   initFromKey(); 
00065   initFromValue();
00066   initFromPath();
00067   initGranularity();
00068 }
00069 
00070 // -----------------------------------------------------------------------------
00071 // 
00072 SiStripDetKey::SiStripDetKey( const std::string& path ) :
00073   SiStripKey(path),
00074   partition_(sistrip::invalid_),
00075   apvPairNumber_(sistrip::invalid_), 
00076   apvWithinPair_(sistrip::invalid_)
00077 {
00078   // order is important!
00079   initFromPath();
00080   initFromValue();
00081   initFromKey();
00082   initGranularity();
00083 }
00084 
00085 // -----------------------------------------------------------------------------
00086 // 
00087 SiStripDetKey::SiStripDetKey( const SiStripDetKey& input ) :
00088   SiStripKey(),
00089   partition_(input.partition()),
00090   apvPairNumber_(input.apvPairNumber()), 
00091   apvWithinPair_(input.apvWithinPair())
00092 {
00093   key(input.key());
00094   path(input.path());
00095   granularity(input.granularity());
00096 }
00097 
00098 // -----------------------------------------------------------------------------
00099 // 
00100 SiStripDetKey::SiStripDetKey( const SiStripKey& input ) :
00101   SiStripKey(),
00102   partition_(sistrip::invalid_),
00103   apvPairNumber_(sistrip::invalid_), 
00104   apvWithinPair_(sistrip::invalid_)
00105 {
00106   SiStripKey& temp = const_cast<SiStripKey&>(input);
00107   SiStripDetKey& det_key = dynamic_cast<SiStripDetKey&>(temp);
00108   if ( (&det_key) ) {
00109     key(det_key.key());
00110     path(det_key.path());
00111     granularity(det_key.granularity());
00112     partition_ = det_key.partition();
00113     apvPairNumber_ = det_key.apvPairNumber();
00114     apvWithinPair_ = det_key.apvWithinPair();
00115   }
00116 }
00117 
00118 // -----------------------------------------------------------------------------
00119 // 
00120 SiStripDetKey::SiStripDetKey( const SiStripKey& input,
00121                               const sistrip::Granularity& gran ) :
00122   SiStripKey(),
00123   partition_(0),
00124   apvPairNumber_(0),
00125   apvWithinPair_(0)
00126 {
00127   SiStripKey& temp = const_cast<SiStripKey&>(input);
00128   SiStripDetKey& det_key = dynamic_cast<SiStripDetKey&>(temp);
00129   if ( (&det_key) ) {
00130 
00131     if ( gran == sistrip::PARTITION ) {
00132       partition_ = det_key.partition(); 
00133     }
00134 
00135     initFromValue();
00136     initFromKey();
00137     initFromPath();
00138     initGranularity();
00139     
00140   }
00141 
00142 }
00143 
00144 // -----------------------------------------------------------------------------
00145 // 
00146 SiStripDetKey::SiStripDetKey() : 
00147   SiStripKey(),
00148   partition_(sistrip::invalid_),
00149   apvPairNumber_(sistrip::invalid_), 
00150   apvWithinPair_(sistrip::invalid_)
00151 {;}
00152 
00153 // -----------------------------------------------------------------------------
00154 // 
00155 bool SiStripDetKey::isEqual( const SiStripKey& key ) const {
00156   SiStripKey& temp = const_cast<SiStripKey&>(key);
00157   SiStripDetKey& input = dynamic_cast<SiStripDetKey&>(temp);
00158   if ( !(&input) ) { return false; }
00159   if ( partition_ == input.partition() &&
00160        apvPairNumber_ == input.apvPairNumber() &&
00161        apvWithinPair_ == input.apvWithinPair() ) {
00162     return true;
00163   } else { return false; }
00164 }
00165 
00166 // -----------------------------------------------------------------------------
00167 // 
00168 bool SiStripDetKey::isConsistent( const SiStripKey& key ) const {
00169   SiStripKey& temp = const_cast<SiStripKey&>(key);
00170   SiStripDetKey& input = dynamic_cast<SiStripDetKey&>(temp);
00171   if ( !(&input) ) { return false; }
00172   if ( isEqual(input) ) { return false; }
00173   else if ( ( partition_ == 0 || input.partition() == 0 ) &&
00174             ( apvPairNumber_ == 0 || input.apvPairNumber() == 0 ) &&
00175             ( apvWithinPair_ == 0 || input.apvWithinPair() == 0 ) ) {
00176     return true;
00177   } else { return false; }
00178 }
00179 
00180 // -----------------------------------------------------------------------------
00181 //
00182 bool SiStripDetKey::isValid() const { 
00183   return isValid(sistrip::APV); 
00184 }
00185 
00186 // -----------------------------------------------------------------------------
00187 //
00188 bool SiStripDetKey::isValid( const sistrip::Granularity& gran ) const {
00189   if ( gran == sistrip::TRACKER ) { return true; }
00190   else if ( gran == sistrip::UNDEFINED_GRAN ||
00191             gran == sistrip::UNKNOWN_GRAN ) { return false; }
00192 
00193   if ( partition_ != sistrip::invalid_ ) {
00194     if ( gran == sistrip::PARTITION ) { return true; }
00195   }
00196   return false;
00197 }
00198 
00199 // -----------------------------------------------------------------------------
00200 //
00201 bool SiStripDetKey::isInvalid() const { 
00202   return isInvalid(sistrip::APV); 
00203 }
00204 
00205 // -----------------------------------------------------------------------------
00206 //
00207 bool SiStripDetKey::isInvalid( const sistrip::Granularity& gran ) const {
00208   if ( gran == sistrip::TRACKER ) { return false; }
00209   else if ( gran == sistrip::UNDEFINED_GRAN ||
00210             gran == sistrip::UNKNOWN_GRAN ) { return false; }
00211 
00212   if ( partition_ == sistrip::invalid_ ) {
00213     if ( gran == sistrip::PARTITION ) { return true; }
00214   }
00215   return false;
00216 }
00217 
00218 // -----------------------------------------------------------------------------
00219 // 
00220 void SiStripDetKey::initFromValue() {
00221 
00222   // partition
00223   if ( partition_ >= 1 && //sistrip::PARTITION_MIN &&
00224        partition_ <= 4 ) { //sistrip::PARTITION_MAX ) {
00225     partition_ = partition_;
00226   } else if ( partition_ == 0 ) {
00227     partition_ = 0;
00228   } else { partition_ = sistrip::invalid_; }
00229 
00230 }
00231 
00232 // -----------------------------------------------------------------------------
00233 //
00234 void SiStripDetKey::initFromKey() {
00235 
00236   if ( key() == sistrip::invalid32_ ) {
00237 
00238     // ---------- Set DetKey based on member data ----------
00239     
00240     // Initialise to null value
00241     key(0);
00242     
00243     // Extract partition
00244     if ( partition_ >= 1 && //sistrip::PARTITION_MIN &&
00245          partition_ <= 4 ) { //sistrip::PARTITION_MAX ) {
00246       key( key() | (partition_<<partitionOffset_) );
00247     } else if ( partition_ == 0 ) {
00248       key( key() | (partition_<<partitionOffset_) );
00249     } else {
00250       key( key() | (partitionMask_<<partitionOffset_) );
00251     }
00252     
00253   } else {
00254     
00255     // ---------- Set member data based on Det key ----------
00256 
00257     partition_ = ( key()>>partitionOffset_ ) & partitionMask_;
00258 
00259     if ( partition_ == partitionMask_ ) { partition_ = sistrip::invalid_; }
00260 
00261   }
00262 
00263 }
00264 
00265 // -----------------------------------------------------------------------------
00266 // 
00267 void SiStripDetKey::initFromPath() {
00268 
00269   if ( path() == sistrip::null_ ) {
00270 
00271     // ---------- Set directory path based on member data ----------
00272 
00273     std::stringstream dir;
00274 
00275     dir << sistrip::root_ << sistrip::dir_ 
00276         << sistrip::detectorView_ << sistrip::dir_;
00277 
00278     // Add partition
00279     if ( partition_ ) {
00280       dir << sistrip::partition_ << partition_ << sistrip::dir_;
00281     }
00282 
00283     std::string temp( dir.str() );
00284     path( temp );
00285 
00286   } else {
00287 
00288     // ---------- Set member data based on directory path ----------
00289 
00290     partition_ = 0;
00291 
00292     // Check if root is found
00293     if ( path().find( sistrip::root_ ) == std::string::npos ) {
00294       std::string temp = path();
00295       path( std::string(sistrip::root_) + sistrip::dir_ + temp );
00296     }
00297 
00298     size_t curr = 0; // current string position
00299     size_t next = 0; // next string position
00300     next = path().find( sistrip::detectorView_, curr );
00301 
00302     // Extract view
00303     curr = next;
00304     if ( curr != std::string::npos ) {
00305       next = path().find( sistrip::partition_, curr );
00306       std::string detector_view( path(),
00307                                  curr+(sizeof(sistrip::detectorView_) - 1),
00308                                  next-(sizeof(sistrip::dir_) - 1)-curr );
00309       // Extract partition
00310       curr = next;
00311       if ( curr != std::string::npos ) { 
00312         next = std::string::npos;
00313         std::string partition( path(), 
00314                                curr+(sizeof(sistrip::partition_) - 1),
00315                                next-(sizeof(sistrip::dir_) - 1)-curr );
00316         partition_ = std::atoi( partition.c_str() );
00317       }
00318     } else {
00319       std::stringstream ss;
00320       ss << sistrip::root_ << sistrip::dir_;
00321       //ss << sistrip::root_ << sistrip::dir_
00322       //<< sistrip::unknownView_ << sistrip::dir_;
00323       std::string temp( ss.str() );
00324       path( temp );
00325     }
00326     
00327   }
00328 
00329 }
00330 
00331 // -----------------------------------------------------------------------------
00332 // 
00333 void SiStripDetKey::initGranularity() {
00334 
00335   granularity( sistrip::TRACKER );
00336   channel(0);
00337   if ( partition_ && partition_ != sistrip::invalid_ ) {
00338     granularity( sistrip::PARTITION );
00339     channel(partition_);
00340   } else if ( partition_ == sistrip::invalid_ ) { 
00341     granularity( sistrip::UNKNOWN_GRAN );
00342     channel(sistrip::invalid_);
00343   }
00344 
00345 }
00346 
00347 // -----------------------------------------------------------------------------
00348 //
00349 void SiStripDetKey::terse( std::stringstream& ss ) const {
00350   ss << "DET:partition= "
00351      << partition();
00352 }
00353 
00354 // -----------------------------------------------------------------------------
00355 //
00356 void SiStripDetKey::print( std::stringstream& ss ) const {
00357   ss << " [SiStripDetKey::print]" << std::endl
00358      << std::hex
00359      << " 32-bit Det key : 0x"
00360      << std::setfill('0') 
00361      << std::setw(8) << key() << std::endl
00362      << std::setfill(' ')
00363      << std::dec
00364      << " Partition      : " << partition() << std::endl
00365      << " Directory      : " << path() << std::endl
00366      << " Granularity    : "
00367      << SiStripEnumsAndStrings::granularity( granularity() ) << std::endl
00368      << " Channel        : " << channel() << std::endl
00369      << " isValid        : " << isValid();
00370 }
00371 
00372 // -----------------------------------------------------------------------------
00373 //
00374 std::ostream& operator<< ( std::ostream& os, const SiStripDetKey& input ) {
00375   std::stringstream ss;
00376   input.print(ss);
00377   os << ss.str();
00378   return os;
00379 }