CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CondFormats/SiStripObjects/src/ApvTimingAnalysis.cc

Go to the documentation of this file.
00001 #include "CondFormats/SiStripObjects/interface/ApvTimingAnalysis.h"
00002 #include "DataFormats/SiStripCommon/interface/SiStripHistoTitle.h"
00003 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00005 #include <iostream>
00006 #include <sstream>
00007 #include <iomanip>
00008 #include <cmath>
00009 
00010 using namespace sistrip;
00011 
00012 // ----------------------------------------------------------------------------
00013 // 
00014 const float ApvTimingAnalysis::optimumSamplingPoint_ = 15.; // [ns]
00015 
00016 // ----------------------------------------------------------------------------
00017 // 
00018 const float ApvTimingAnalysis::tickMarkHeightThreshold_ = 50.; // [ADC]
00019 
00020 // ----------------------------------------------------------------------------
00021 // 
00022 const float ApvTimingAnalysis::frameFindingThreshold_ = (2./3.); // fraction of tick mark height
00023 
00024 // ----------------------------------------------------------------------------
00025 // 
00026 float ApvTimingAnalysis::refTime_ = 1.*sistrip::invalid_;
00027 
00028 // ----------------------------------------------------------------------------
00029 // 
00030 ApvTimingAnalysis::ApvTimingAnalysis( const uint32_t& key ) 
00031   : CommissioningAnalysis(key,sistrip::apvTimingAnalysis_),
00032     time_(1.*sistrip::invalid_), 
00033     error_(1.*sistrip::invalid_), 
00034     delay_(1.*sistrip::invalid_), 
00035     height_(1.*sistrip::invalid_),
00036     base_(1.*sistrip::invalid_), 
00037     peak_(1.*sistrip::invalid_), 
00038     synchronized_(false)
00039 {;}
00040 
00041 // ----------------------------------------------------------------------------
00042 // 
00043 ApvTimingAnalysis::ApvTimingAnalysis() 
00044   : CommissioningAnalysis(sistrip::apvTimingAnalysis_),
00045     time_(1.*sistrip::invalid_), 
00046     error_(1.*sistrip::invalid_), 
00047     delay_(1.*sistrip::invalid_), 
00048     height_(1.*sistrip::invalid_),
00049     base_(1.*sistrip::invalid_), 
00050     peak_(1.*sistrip::invalid_), 
00051     synchronized_(false)
00052 {;}
00053 
00054 // ----------------------------------------------------------------------------
00055 // 
00056 void ApvTimingAnalysis::reset() {
00057   time_   = 1.*sistrip::invalid_; 
00058   error_  = 1.*sistrip::invalid_; 
00059   delay_  = 1.*sistrip::invalid_; 
00060   height_ = 1.*sistrip::invalid_;
00061   base_   = 1.*sistrip::invalid_; 
00062   peak_   = 1.*sistrip::invalid_; 
00063   synchronized_ = false;
00064 }
00065 
00066 // ----------------------------------------------------------------------------
00067 // 
00068 void ApvTimingAnalysis::refTime( const float& time, const float& targetDelay ) { 
00069 
00070   // Checks synchronization to reference time is done only once
00071   if ( synchronized_ ) { 
00072     edm::LogWarning(mlCommissioning_)
00073       << "[" << myName() << "::" << __func__ << "]"
00074       << " Attempting to re-synchronize with reference time!"
00075       << " Not allowed!";
00076     return; 
00077   }
00078   synchronized_ = true;
00079 
00080   // Set reference time and check if tick mark time is valid
00081   refTime_ = time;
00082   if ( time_ > sistrip::valid_ ) { return; }
00083   
00084   // Calculate position of "sampling point" of last tick;
00085   int32_t position;
00086   if ( targetDelay == -1 ) {   // by default use latest tick
00087     position = static_cast<int32_t>( rint( refTime_ + optimumSamplingPoint_ ) );
00088   } else {
00089     position = static_cast<int32_t>( rint( targetDelay + optimumSamplingPoint_ ) );
00090   }
00091 
00092   // Calculate adjustment so that sampling point is multiple of 25 (ie, synched with FED sampling)
00093   float adjustment = 25 - position % 25;
00094 
00095   // Calculate delay required to synchronise with this adjusted sampling position
00096   if ( targetDelay == -1 ) {   // by default align forward to the latest tick
00097     delay_ = ( refTime_ + adjustment ) - time_;
00098   } else {                     // otherwise use the supplied target delay
00099     if ( adjustment > 25/2 ) adjustment -= 25; // go as close as possible to desired target
00100     delay_ = ( targetDelay + adjustment ) - time_;
00101   }
00102 
00103   // Check reference time
00104   if ( refTime_ < 0. || refTime_ > sistrip::valid_ ) { 
00105     refTime_ = sistrip::invalid_;
00106     addErrorCode(sistrip::invalidRefTime_);
00107   }
00108   
00109   // Check delay is valid
00110   if ( delay_ < -sistrip::valid_ || delay_ > sistrip::valid_ ) { 
00111     delay_ = sistrip::invalid_;
00112     addErrorCode(sistrip::invalidDelayTime_);
00113   }
00114 
00115 }
00116 
00117 // ----------------------------------------------------------------------------
00118 // 
00119 uint16_t ApvTimingAnalysis::frameFindingThreshold() const { 
00120   if ( ( getErrorCodes().empty() || getErrorCodes()[0] == "TickMarkRecovered" ) &&
00121        time_   < sistrip::valid_ &&
00122        base_   < sistrip::valid_ && 
00123        peak_   < sistrip::valid_ && 
00124        height_ < sistrip::valid_  &&
00125        height_ > tickMarkHeightThreshold_ ) { 
00126     return ( ( static_cast<uint16_t>( base_ + 
00127                                       height_ * 
00128                                       ApvTimingAnalysis::frameFindingThreshold_ ) / 32 ) * 32 );
00129   } else { return sistrip::invalid_; }
00130 }
00131 
00132 // ----------------------------------------------------------------------------
00133 // 
00134 bool ApvTimingAnalysis::foundTickMark() const {
00135   return ( ( getErrorCodes().empty() || getErrorCodes()[0] == "TickMarkRecovered") &&
00136            time_   < sistrip::valid_ &&
00137            base_   < sistrip::valid_ &&
00138            peak_   < sistrip::valid_ &&
00139            height_ < sistrip::valid_ &&
00140            frameFindingThreshold() < sistrip::valid_ );
00141 } 
00142 
00143 // ----------------------------------------------------------------------------
00144 // 
00145 bool ApvTimingAnalysis::isValid() const {
00146   return ( ( getErrorCodes().empty() || getErrorCodes()[0] == "TickMarkRecovered" ) &&
00147            time_   < sistrip::valid_ &&
00148            base_   < sistrip::valid_ &&
00149            peak_   < sistrip::valid_ &&
00150            height_ < sistrip::valid_ &&
00151            frameFindingThreshold() < sistrip::valid_ &&
00152            synchronized_ &&
00153            refTime_ < sistrip::valid_ && 
00154            delay_   < sistrip::valid_ );
00155 }
00156 
00157 // ----------------------------------------------------------------------------
00158 // 
00159 void ApvTimingAnalysis::print( std::stringstream& ss, uint32_t not_used ) { 
00160   header( ss );
00161 
00162   float sampling1 = sistrip::invalid_;
00163   if ( time_ <= sistrip::valid_ ) { sampling1 = time_ + optimumSamplingPoint_; }
00164   
00165   float sampling2 = sistrip::invalid_;
00166   if ( refTime_ <= sistrip::valid_ ) { sampling2 = refTime_ + optimumSamplingPoint_; }
00167   
00168   float adjust = sistrip::invalid_;
00169   if ( sampling1 <= sistrip::valid_ && delay_ <= sistrip::valid_ ) { adjust = sampling1 + delay_; }
00170 
00171   ss <<  std::fixed << std::setprecision(2)
00172      << " Tick mark: time of rising edge     [ns] : " << time_ << std::endl 
00173     //<< " Error on time of rising edge     [ns] : " << error_ << std::endl
00174      << " Tick mark: time of sampling point  [ns] : " << sampling1 << std::endl 
00175      << " Ref tick: time of rising edge      [ns] : " << refTime_ << std::endl 
00176      << " Ref tick: time of sampling point   [ns] : " << sampling2 << std::endl 
00177      << " Ref tick: adjusted sampling point  [ns] : " << adjust << std::endl 
00178      << " Delay required to synchronise      [ns] : " << delay_ << std::endl 
00179      << " Tick mark bottom (baseline)       [ADC] : " << base_ << std::endl 
00180      << " Tick mark top                     [ADC] : " << peak_ << std::endl 
00181      << " Tick mark height                  [ADC] : " << height_ << std::endl
00182      << " Frame finding threshold           [ADC] : " << frameFindingThreshold() << std::endl
00183      << std::boolalpha 
00184      << " Tick mark found                         : " << foundTickMark()  << std::endl
00185      << " isValid                                 : " << isValid()  << std::endl
00186      << std::noboolalpha
00187      << " Error codes (found "
00188      << std::setw(3) << std::setfill(' ') << getErrorCodes().size() 
00189      << ")                 : ";
00190   if ( getErrorCodes().empty() ) { ss << "(none)"; }
00191   else { 
00192     VString::const_iterator istr = getErrorCodes().begin();
00193     VString::const_iterator jstr = getErrorCodes().end();
00194     for ( ; istr != jstr; ++istr ) { ss << *istr << " "; }
00195   }
00196   ss << std::endl;
00197 }
00198