CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_4/src/DetectorDescription/ExprAlgo/src/AlgoPos.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/ExprAlgo/interface/AlgoPos.h"
00002 #include "DetectorDescription/ExprAlgo/interface/AlgoImpl.h"
00003 #include "DetectorDescription/ExprAlgo/interface/AlgoCheck.h"
00004 #include "FWCore/Utilities/interface/Exception.h"
00005 
00006 using namespace std;
00007 
00008 AlgoPos::AlgoPos(AlgoCheck * check)
00009 : start_(0), end_(0), incr_(0), curr_(0), count_(0),
00010 terminate_(false), selAlgo_(0), checkAlgo_(check)
00011 { }
00012 
00013 
00014 AlgoPos::~AlgoPos()
00015 { 
00016    //FIXME: delete all registered AlgoImpls!!!
00017    std::vector<AlgoImpl*>::iterator it = regAlgos_.begin();
00018    for (; it != regAlgos_.end(); ++it) {
00019       delete *it;
00020    }  
00021    delete checkAlgo_; 
00022 }
00023 
00024 
00025 void AlgoPos::setParameters(int start, int end, int incr,
00026                             const parS_type & ps, const parE_type & pe)
00027 {
00028    // init the algorithm when parameters are set
00029    terminate_ = false;
00030    start_     = start;
00031    end_       = end;
00032    incr_      = incr;
00033    if (incr>0) curr_ = start;
00034    if (incr<0) curr_ = end;  
00035    count_ = 1;
00036    
00037    // now check mandatory parameters, then let the algorithm check all parametes itself.
00038    // collect all error messages in std::string err_
00039    
00040    bool check = true;
00041    if (incr) {
00042       if (start>=end) {
00043          check = false;
00044       }  
00045    }
00046    if (!check) err_ = "\twrong range specification: (start<end && incr!=0)==false, [start,end,incr]=["
00047       + AlgoImpl::d2s(start) + "," 
00048       + AlgoImpl::d2s(end) + "," 
00049       + AlgoImpl::d2s(incr) + "]\n" ;
00050    
00051    if (incr==0) {
00052       if ( (start!=0) || (end!=0) ) {
00053          err_ += "\tincr==0 requires start==0 and end==0 in the range. range: [start,end,incr]=["
00054          + AlgoImpl::d2s(start) + "," 
00055          + AlgoImpl::d2s(end) + "," 
00056                    + AlgoImpl::d2s(incr) + "]\n" ;
00057          check = false;            
00058          
00059       }
00060    }    
00061    
00062    // select one of the registered algorithm implementations
00063    ParS_ = ps;
00064    ParE_ = pe;
00065    if (!select()) check = false;
00066    
00067    if (!check) 
00068       throw cms::Exception("DDException") << err_;
00069 }
00070 
00071 
00072 void AlgoPos::registerAlgo(AlgoImpl*a)
00073 {
00074    regAlgos_.push_back(a);
00075 }
00076 
00077 size_t AlgoPos::numRegistered()
00078 {
00079    return regAlgos_.size();
00080 }
00081 
00082 int AlgoPos::copyno() const
00083 {
00084    return selAlgo_->copyno();
00085 }
00086 
00087 
00096 void AlgoPos::next()
00097 {
00098    // increase the invocation count of the algorithm
00099    ++count_;
00100    
00101    
00102    // iterate to the next position in the range [start_,end_,incr_]
00103    // only if incr_ != 0
00104    
00105    if (incr_>0) {
00106       curr_ += incr_;
00107       if (curr_>end_) {
00108          terminate();
00109       }  
00110    }
00111    
00112    if (incr_<0) {
00113       curr_ += incr_;
00114       if (curr_<start_) {
00115          terminate();
00116       }  
00117    }
00118    
00119    // incr_==0: the algorithm has to self-check whether to terminate
00120    if (incr_==0) {
00121       checkTermination();
00122    }
00123 }
00124 
00125 
00126 
00127 void AlgoPos::checkTermination()
00128 { 
00129    selAlgo_->checkTermination();
00130 }
00131 
00132 
00133 void AlgoPos::terminate()
00134 { 
00135    terminate_=true; 
00136 }
00137 
00138 
00139 bool AlgoPos::go() const 
00140 { 
00141    return !terminate_; 
00142 }
00143 
00144 
00145 DD3Vector AlgoPos::translation()
00146 {
00147    return selAlgo_->translation();
00148 }
00149 
00150 
00151 DDRotationMatrix AlgoPos::rotation()
00152 {
00153    return selAlgo_->rotation();
00154 }
00155 
00156 
00157 bool AlgoPos::select()
00158 {
00159    bool result = false;
00160    
00161    // if constraints checking is enabled (object checkAlgo_ is there) ,
00162    // check the contraints of the parameters as specified in the schema.
00163    if (checkAlgo_) {
00164       result = (checkAlgo_->check(ParS_,ParE_,err_));
00165    }
00166    else {
00167       result = true;
00168    }  
00169    
00170    
00171    if (result) { // select an algorithm-implementation only if the parameters are ok
00172       std::vector<AlgoImpl*>::iterator it = regAlgos_.begin();
00173       for (; it != regAlgos_.end(); ++it) {
00174          std::string::size_type s = err_.size();
00175          result = (*it)->checkParameters();
00176          if (s!=err_.size()) { // uups, error-std::string was modified! tell it, where:
00177             err_ += std::string("\tin algo.implementation labeled \"") + (*it)->label_ + std::string("\"\n");
00178          }
00179          if (result) { // select the algorithm
00180             selAlgo_ = *it;
00181             break;
00182          }  
00183       }
00184    }  
00185    // parameters are not ok, put something into the error message
00186    /*
00187     if (!result) { 
00188     err_ += "\tin algorithm named " + std::string("[") 
00189     +  ns() + std::string(":") + name() + std::string("]\n");
00190     }   
00191     */   
00192    
00193    return result;
00194 }
00195 
00196 // stream information about the alog-parameters
00197 void AlgoPos::stream(ostream & os) const
00198 {
00199    os <<  "start=" << start_ << " end=" << end_ << " incr=" << incr_ << std::endl;
00200    parE_type::const_iterator eit = ParE_.begin();
00201    for (; eit != ParE_.end() ; ++eit) {
00202       std::vector<double>::const_iterator sit = eit->second.begin();
00203       os << "parE name=" << eit->first;
00204       for (; sit != eit->second.end(); ++sit) {
00205          os << " val=" << *sit << std::endl;
00206       }
00207    }
00208    parS_type::const_iterator stit = ParS_.begin();
00209    for (; stit != ParS_.end() ; ++stit) {
00210       std::vector<std::string>::const_iterator sit = stit->second.begin();
00211       os << "parS name=" << stit->first;
00212       for (; sit != stit->second.end(); ++sit) {
00213          os << " val=" << *sit << std::endl;
00214       }
00215    }
00216 }