CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Alignment/CommonAlignmentAlgorithm/src/TkModuleGroupSelector.cc

Go to the documentation of this file.
00001 
00011 #include "Alignment/CommonAlignmentAlgorithm/interface/TkModuleGroupSelector.h"
00012 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
00013 #include "Alignment/CommonAlignment/interface/Alignable.h"
00014 #include "DataFormats/DetId/interface/DetId.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 
00017 #include <vector>
00018 #include <map>
00019 #include <set>
00020 
00021 //============================================================================
00022 TkModuleGroupSelector::TkModuleGroupSelector(AlignableTracker *aliTracker,
00023                                              const edm::ParameterSet &cfg,
00024                                              const std::vector<int> &sdets
00025                                              ) : nparameters_(0),
00026                                                  subdetids_(sdets)
00027 {
00028   //verify that all provided options are known
00029   std::vector<std::string> parameterNames = cfg.getParameterNames();
00030   for ( std::vector<std::string>::const_iterator iParam = parameterNames.begin(); 
00031         iParam != parameterNames.end(); ++iParam) {
00032     const std::string name = (*iParam);
00033     if(
00034        name != "RunRange" && name != "ReferenceRun" && name != "Granularity"
00035        ) {
00036       throw cms::Exception("BadConfig")
00037         << "@SUB=TkModuleGroupSelector::TkModuleGroupSelector:"
00038         << " Unknown parameter name '" << name << "' in PSet. Maybe a typo?";
00039     }
00040   }
00041   
00042 
00043   //extract the reference run range if defined
00044   const edm::RunNumber_t defaultReferenceRun
00045     = (cfg.exists("ReferenceRun") ? cfg.getParameter<edm::RunNumber_t>("ReferenceRun") : 0);
00046 
00047   //extract run range to be used for all module groups (if not locally overwritten)
00048   const std::vector<edm::RunNumber_t> defaultRunRange
00049     = (cfg.exists("RunRange") ? cfg.getParameter<std::vector<edm::RunNumber_t> >("RunRange")
00050        : std::vector<edm::RunNumber_t>());
00051 
00052   // finally create everything from configuration
00053   this->createModuleGroups(aliTracker, cfg.getParameter<edm::VParameterSet>("Granularity"),
00054                            defaultRunRange, defaultReferenceRun);
00055 }
00056 
00057 //============================================================================
00058 void TkModuleGroupSelector::fillDetIdMap(const unsigned int detid, const unsigned int groupid)
00059 {
00060   //only add new entries 
00061   if(mapDetIdGroupId_.find(detid) == mapDetIdGroupId_.end()) {
00062     mapDetIdGroupId_.insert(std::pair<unsigned int, unsigned int>(detid, groupid));
00063   } else {
00064     throw cms::Exception("BadConfig")
00065       << "@SUB=TkModuleGroupSelector:fillDetIdMap:"
00066       << " Module with det ID " << detid << " configured in group " << groupid
00067       << " but it was already selected"
00068       << " in group " << mapDetIdGroupId_[detid] << ".";
00069   }
00070 }
00071 
00072 //============================================================================
00073 const bool TkModuleGroupSelector::testSplitOption(const edm::ParameterSet &pset) const
00074 {
00075   bool split = false;
00076   if(pset.exists("split")) {
00077     split = pset.getParameter<bool>("split");
00078   }
00079   return split;
00080 }
00081 
00082 //============================================================================
00083 bool TkModuleGroupSelector::createGroup(
00084                                         unsigned int &Id, 
00085                                         const std::vector<edm::RunNumber_t> &range, 
00086                                         const std::list<Alignable*> &selected_alis,
00087                                         const edm::RunNumber_t refrun
00088                                         )
00089 {
00090   bool modules_selected = false;
00091 
00092   referenceRun_.push_back(refrun);
00093   firstId_.push_back(Id);
00094   runRange_.push_back(range);
00095   for(std::list<Alignable*>::const_iterator it = selected_alis.begin();
00096       it != selected_alis.end(); it++) {
00097     this->fillDetIdMap((*it)->id(), firstId_.size()-1);
00098     modules_selected = true;
00099   }
00100   if(refrun > 0 && range.size() > 0) { //FIXME: last condition not really needed?
00101     Id += range.size() - 1;
00102     nparameters_ += range.size() - 1;
00103   } else {
00104     Id += range.size();
00105     nparameters_ += range.size();  
00106   }
00107 
00108   if(refrun > 0 && range.front() > refrun) { //range.size() > 0 checked before
00109     throw cms::Exception("BadConfig")
00110       << "@SUB=TkModuleGroupSelector::createGroup:\n"
00111       << "Invalid combination of reference run number and specified run dependence"
00112       << "\n in module group " << firstId_.size() << "."
00113       << "\n Reference run number (" << refrun << ") is smaller than starting run "
00114       << "\n number (" << range.front() << ") of first IOV.";
00115   }
00116   return modules_selected;
00117 }
00118 
00119 //============================================================================
00120 void TkModuleGroupSelector::verifyParameterNames(const edm::ParameterSet &pset, unsigned int psetnr) const
00121 {
00122   std::vector<std::string> parameterNames = pset.getParameterNames();
00123   for ( std::vector<std::string>::const_iterator iParam = parameterNames.begin(); 
00124         iParam != parameterNames.end(); ++iParam) {
00125     const std::string name = (*iParam);
00126     if(
00127        name != "levels" && name != "RunRange"
00128        && name != "split" && name != "ReferenceRun"
00129        ) {
00130       throw cms::Exception("BadConfig")
00131         << "@SUB=TkModuleGroupSelector::verifyParameterNames:"
00132         << " Unknown parameter name '" << name << "' in PSet number " << psetnr << ". Maybe a typo?";
00133     }
00134   }
00135 }
00136 
00137 
00138 //============================================================================
00139 void TkModuleGroupSelector::createModuleGroups(AlignableTracker *aliTracker,
00140                                                const edm::VParameterSet &granularityConfig,
00141                                                const std::vector<edm::RunNumber_t> &defaultRunRange,
00142                                                edm::RunNumber_t defaultReferenceRun)
00143 {
00144   std::set<edm::RunNumber_t> localRunRange;
00145   nparameters_ = 0;
00146   unsigned int Id = 0;
00147   unsigned int psetnr = 0;
00148   //loop over all LA groups
00149   for(edm::VParameterSet::const_iterator pset = granularityConfig.begin();
00150       pset != granularityConfig.end();
00151       ++pset) {
00152 
00153     //test for unknown parameters
00154     this->verifyParameterNames((*pset),psetnr);
00155     psetnr++;
00156 
00157     bool modules_selected = false; //track whether at all a module has been selected in this group
00158     const std::vector<edm::RunNumber_t> range =
00159       ((*pset).exists("RunRange") ? pset->getParameter<std::vector<edm::RunNumber_t> >("RunRange")
00160        : defaultRunRange);
00161     if(range.empty()) {
00162       throw cms::Exception("BadConfig")
00163         << "@SUB=TkModuleGroupSelector::createModuleGroups:\n"
00164         << "Run range array empty!";
00165     }
00166     const bool split = this->testSplitOption((*pset));
00167 
00168     edm::RunNumber_t refrun = 0;
00169     if((*pset).exists("ReferenceRun")) {
00170       refrun = (*pset).getParameter<edm::RunNumber_t>("ReferenceRun");
00171     } else {
00172       refrun = defaultReferenceRun;
00173     }
00174     
00175     AlignmentParameterSelector selector(aliTracker);
00176     selector.clear();
00177     selector.addSelections((*pset).getParameter<edm::ParameterSet> ("levels"));
00178 
00179     const std::vector<Alignable*> &alis = selector.selectedAlignables();
00180     std::list<Alignable*> selected_alis;
00181     for(std::vector<Alignable*>::const_iterator it = alis.begin(); it != alis.end(); ++it) {
00182       const std::vector<Alignable*> &aliDaughts = (*it)->deepComponents();
00183       for (std::vector<Alignable*>::const_iterator iD = aliDaughts.begin();
00184            iD != aliDaughts.end(); ++iD) {
00185         if((*iD)->alignableObjectId() == align::AlignableDetUnit || (*iD)->alignableObjectId() == align::AlignableDet) {
00186           if(split) {
00187             modules_selected = this->createGroup(Id, range, std::list<Alignable*>(1,(*iD)), refrun);
00188           } else {
00189             selected_alis.push_back((*iD));
00190           }
00191         }
00192       }
00193     }
00194     
00195     if(!split) {
00196       modules_selected = this->createGroup(Id, range, selected_alis, refrun);
00197     }
00198         
00199     edm::RunNumber_t firstRun = 0; 
00200     for(std::vector<edm::RunNumber_t>::const_iterator iRun = range.begin(); 
00201         iRun != range.end(); ++iRun)  {
00202       localRunRange.insert((*iRun));
00203       if((*iRun) > firstRun) {
00204         firstRun = (*iRun);
00205       } else {
00206         throw cms::Exception("BadConfig")
00207           << "@SUB=TkModuleGroupSelector::createModuleGroups:"
00208           << " Run range not sorted.";
00209       }
00210     }
00211 
00212     if(!modules_selected) {
00213       throw cms::Exception("BadConfig") 
00214         << "@SUB=TkModuleGroupSelector:createModuleGroups:"
00215         << " No module was selected in the module group selector in group " << (firstId_.size()-1)<< ".";
00216     }
00217   }
00218 
00219   //copy local set into the global vector of run boundaries
00220   for(std::set<edm::RunNumber_t>::const_iterator itRun = localRunRange.begin();
00221       itRun != localRunRange.end(); itRun++) {
00222     globalRunRange_.push_back((*itRun));
00223   }
00224 }
00225 
00226 //============================================================================
00227 unsigned int TkModuleGroupSelector::getNumberOfParameters() const
00228 {
00229   return nparameters_;
00230 }
00231 
00232 //============================================================================
00233 unsigned int TkModuleGroupSelector::numIovs() const
00234 {
00235   return globalRunRange_.size();
00236 }
00237 
00238 //============================================================================
00239 edm::RunNumber_t TkModuleGroupSelector::firstRunOfIOV(unsigned int iovNum) const
00240 {
00241   return iovNum < this->numIovs() ? globalRunRange_.at(iovNum) : 0;
00242 }
00243 
00244 //======================================================================
00245 int TkModuleGroupSelector::getParameterIndexFromDetId(unsigned int detId,
00246                                                       edm::RunNumber_t run) const
00247 {
00248   // Return the index of the parameter that is used for this DetId.
00249   // If this DetId is not treated, return values < 0.
00250 
00251   const DetId temp_id(detId);
00252 
00253   int index = -1;
00254 
00255   bool sel = false;
00256   for(std::vector<int>::const_iterator itSubDets = subdetids_.begin();
00257       itSubDets != subdetids_.end();
00258       itSubDets++) {
00259     if (temp_id.det() == DetId::Tracker && temp_id.subdetId() == (*itSubDets)) {
00260       sel = true;
00261       break;
00262     }
00263   }
00264 
00265   if (temp_id.det() != DetId::Tracker || !sel) return -1;
00266   
00267   std::map<unsigned int, unsigned int>::const_iterator it = mapDetIdGroupId_.find(detId);
00268   if(it != mapDetIdGroupId_.end()) {
00269     const unsigned int iAlignableGroup = (*it).second;
00270     const std::vector<edm::RunNumber_t> &runs = runRange_.at(iAlignableGroup);
00271     const unsigned int id0 = firstId_.at(iAlignableGroup);
00272     const edm::RunNumber_t refrun = referenceRun_.at(iAlignableGroup);
00273 
00274 
00275     unsigned int iovNum = 0;
00276     for ( ; iovNum < runs.size(); ++iovNum) {
00277       if (runs[iovNum] > run) break;
00278     }
00279     if (iovNum == 0) {
00280       throw cms::Exception("BadConfig") << "@SUB=TkModuleGroupSelector::getParameterIndexFromDetId:\n"
00281                                         << "Run " << run << " not foreseen for detid '"<< detId <<"'"
00282                                         << " in module group " << iAlignableGroup << ".";        
00283     } else {
00284       --iovNum;
00285     }
00286 
00287     //test whether the iov contains the reference run
00288     if(refrun > 0) { //if > 0 a reference run number has been provided
00289       if(iovNum+1 == runs.size()) {
00290         if(refrun >= runs[iovNum])
00291           return -1;
00292       } else if( (iovNum+1) < runs.size()) {
00293         if(refrun >= runs[iovNum] && refrun < runs[iovNum+1]) {
00294           return -1;
00295         }
00296       } 
00297       if(run > refrun) {
00298         //iovNum > 0 due to checks in createGroup(...) and createModuleGroups(...)
00299         //remove IOV in which the reference run can be found
00300         iovNum -= 1;
00301       }
00302     }
00303 
00304     index = id0 + iovNum;
00305   }
00306   return index;
00307 }