ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/CMSSW/Alignment/CommonAlignmentAlgorithm/src/TkModuleGroupSelector.cc
Revision: 1.5
Committed: Wed Jun 19 08:33:03 2013 UTC (11 years, 10 months ago) by jbehr
Content type: text/plain
Branch: MAIN
CVS Tags: CMSSW_6_2_0, V04-00-14, HEAD
Changes since 1.4: +5 -5 lines
Log Message:
bug fix: consistency of reference run and specified run dependence was verified
also when no reference run was provided.

File Contents

# User Rev Content
1 flucke 1.2 /**
2     * \file TkModuleGroupSelector.cc
3     *
4     * \author Joerg Behr
5     * \date May 2013
6 jbehr 1.5 * $Revision: 1.4 $
7     * $Date: 2013/05/31 14:50:52 $
8 jbehr 1.4 * (last update by $Author: jbehr $)
9 flucke 1.2 */
10    
11     #include "Alignment/CommonAlignmentAlgorithm/interface/TkModuleGroupSelector.h"
12     #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
13     #include "Alignment/CommonAlignment/interface/Alignable.h"
14     #include "DataFormats/DetId/interface/DetId.h"
15     #include "FWCore/MessageLogger/interface/MessageLogger.h"
16    
17     #include <vector>
18     #include <map>
19     #include <set>
20    
21     //============================================================================
22     TkModuleGroupSelector::TkModuleGroupSelector(AlignableTracker *aliTracker,
23     const edm::ParameterSet &cfg,
24     const std::vector<int> &sdets
25     ) : nparameters_(0),
26     subdetids_(sdets)
27     {
28     //verify that all provided options are known
29     std::vector<std::string> parameterNames = cfg.getParameterNames();
30     for ( std::vector<std::string>::const_iterator iParam = parameterNames.begin();
31     iParam != parameterNames.end(); ++iParam) {
32     const std::string name = (*iParam);
33     if(
34     name != "RunRange" && name != "ReferenceRun" && name != "Granularity"
35     ) {
36     throw cms::Exception("BadConfig")
37     << "@SUB=TkModuleGroupSelector::TkModuleGroupSelector:"
38     << " Unknown parameter name '" << name << "' in PSet. Maybe a typo?";
39     }
40     }
41    
42    
43     //extract the reference run range if defined
44     const edm::RunNumber_t defaultReferenceRun
45     = (cfg.exists("ReferenceRun") ? cfg.getParameter<edm::RunNumber_t>("ReferenceRun") : 0);
46    
47     //extract run range to be used for all module groups (if not locally overwritten)
48     const std::vector<edm::RunNumber_t> defaultRunRange
49     = (cfg.exists("RunRange") ? cfg.getParameter<std::vector<edm::RunNumber_t> >("RunRange")
50     : std::vector<edm::RunNumber_t>());
51    
52     // finally create everything from configuration
53     this->createModuleGroups(aliTracker, cfg.getParameter<edm::VParameterSet>("Granularity"),
54     defaultRunRange, defaultReferenceRun);
55     }
56    
57     //============================================================================
58     void TkModuleGroupSelector::fillDetIdMap(const unsigned int detid, const unsigned int groupid)
59     {
60     //only add new entries
61     if(mapDetIdGroupId_.find(detid) == mapDetIdGroupId_.end()) {
62     mapDetIdGroupId_.insert(std::pair<unsigned int, unsigned int>(detid, groupid));
63     } else {
64     throw cms::Exception("BadConfig")
65     << "@SUB=TkModuleGroupSelector:fillDetIdMap:"
66     << " Module with det ID " << detid << " configured in group " << groupid
67     << " but it was already selected"
68     << " in group " << mapDetIdGroupId_[detid] << ".";
69     }
70     }
71    
72     //============================================================================
73     const bool TkModuleGroupSelector::testSplitOption(const edm::ParameterSet &pset) const
74     {
75     bool split = false;
76     if(pset.exists("split")) {
77     split = pset.getParameter<bool>("split");
78     }
79     return split;
80     }
81    
82     //============================================================================
83     bool TkModuleGroupSelector::createGroup(
84     unsigned int &Id,
85     const std::vector<edm::RunNumber_t> &range,
86     const std::list<Alignable*> &selected_alis,
87     const edm::RunNumber_t refrun
88     )
89     {
90     bool modules_selected = false;
91    
92     referenceRun_.push_back(refrun);
93     firstId_.push_back(Id);
94     runRange_.push_back(range);
95     for(std::list<Alignable*>::const_iterator it = selected_alis.begin();
96     it != selected_alis.end(); it++) {
97     this->fillDetIdMap((*it)->id(), firstId_.size()-1);
98     modules_selected = true;
99     }
100     if(refrun > 0 && range.size() > 0) { //FIXME: last condition not really needed?
101     Id += range.size() - 1;
102     nparameters_ += range.size() - 1;
103     } else {
104     Id += range.size();
105     nparameters_ += range.size();
106     }
107 jbehr 1.4
108 jbehr 1.5 if(refrun > 0 && range.front() > refrun) { //range.size() > 0 checked before
109 jbehr 1.4 throw cms::Exception("BadConfig")
110     << "@SUB=TkModuleGroupSelector::createGroup:\n"
111     << "Invalid combination of reference run number and specified run dependence"
112     << "\n in module group " << firstId_.size() << "."
113 jbehr 1.5 << "\n Reference run number (" << refrun << ") is smaller than starting run "
114     << "\n number (" << range.front() << ") of first IOV.";
115 jbehr 1.4 }
116 flucke 1.2 return modules_selected;
117     }
118    
119     //============================================================================
120     void TkModuleGroupSelector::verifyParameterNames(const edm::ParameterSet &pset, unsigned int psetnr) const
121     {
122     std::vector<std::string> parameterNames = pset.getParameterNames();
123     for ( std::vector<std::string>::const_iterator iParam = parameterNames.begin();
124     iParam != parameterNames.end(); ++iParam) {
125     const std::string name = (*iParam);
126     if(
127     name != "levels" && name != "RunRange"
128     && name != "split" && name != "ReferenceRun"
129     ) {
130     throw cms::Exception("BadConfig")
131     << "@SUB=TkModuleGroupSelector::verifyParameterNames:"
132     << " Unknown parameter name '" << name << "' in PSet number " << psetnr << ". Maybe a typo?";
133     }
134     }
135     }
136    
137    
138     //============================================================================
139     void TkModuleGroupSelector::createModuleGroups(AlignableTracker *aliTracker,
140     const edm::VParameterSet &granularityConfig,
141     const std::vector<edm::RunNumber_t> &defaultRunRange,
142     edm::RunNumber_t defaultReferenceRun)
143     {
144     std::set<edm::RunNumber_t> localRunRange;
145     nparameters_ = 0;
146     unsigned int Id = 0;
147     unsigned int psetnr = 0;
148     //loop over all LA groups
149     for(edm::VParameterSet::const_iterator pset = granularityConfig.begin();
150     pset != granularityConfig.end();
151     ++pset) {
152    
153     //test for unknown parameters
154     this->verifyParameterNames((*pset),psetnr);
155     psetnr++;
156    
157     bool modules_selected = false; //track whether at all a module has been selected in this group
158     const std::vector<edm::RunNumber_t> range =
159     ((*pset).exists("RunRange") ? pset->getParameter<std::vector<edm::RunNumber_t> >("RunRange")
160     : defaultRunRange);
161     if(range.empty()) {
162     throw cms::Exception("BadConfig")
163     << "@SUB=TkModuleGroupSelector::createModuleGroups:\n"
164     << "Run range array empty!";
165     }
166     const bool split = this->testSplitOption((*pset));
167    
168     edm::RunNumber_t refrun = 0;
169     if((*pset).exists("ReferenceRun")) {
170     refrun = (*pset).getParameter<edm::RunNumber_t>("ReferenceRun");
171     } else {
172     refrun = defaultReferenceRun;
173     }
174    
175     AlignmentParameterSelector selector(aliTracker);
176     selector.clear();
177     selector.addSelections((*pset).getParameter<edm::ParameterSet> ("levels"));
178    
179     const std::vector<Alignable*> &alis = selector.selectedAlignables();
180     std::list<Alignable*> selected_alis;
181     for(std::vector<Alignable*>::const_iterator it = alis.begin(); it != alis.end(); ++it) {
182     const std::vector<Alignable*> &aliDaughts = (*it)->deepComponents();
183     for (std::vector<Alignable*>::const_iterator iD = aliDaughts.begin();
184     iD != aliDaughts.end(); ++iD) {
185     if((*iD)->alignableObjectId() == align::AlignableDetUnit || (*iD)->alignableObjectId() == align::AlignableDet) {
186     if(split) {
187     modules_selected = this->createGroup(Id, range, std::list<Alignable*>(1,(*iD)), refrun);
188     } else {
189     selected_alis.push_back((*iD));
190     }
191     }
192     }
193     }
194    
195     if(!split) {
196     modules_selected = this->createGroup(Id, range, selected_alis, refrun);
197     }
198    
199     edm::RunNumber_t firstRun = 0;
200     for(std::vector<edm::RunNumber_t>::const_iterator iRun = range.begin();
201     iRun != range.end(); ++iRun) {
202     localRunRange.insert((*iRun));
203     if((*iRun) > firstRun) {
204     firstRun = (*iRun);
205     } else {
206     throw cms::Exception("BadConfig")
207     << "@SUB=TkModuleGroupSelector::createModuleGroups:"
208     << " Run range not sorted.";
209     }
210     }
211    
212     if(!modules_selected) {
213     throw cms::Exception("BadConfig")
214     << "@SUB=TkModuleGroupSelector:createModuleGroups:"
215     << " No module was selected in the module group selector in group " << (firstId_.size()-1)<< ".";
216     }
217     }
218    
219     //copy local set into the global vector of run boundaries
220     for(std::set<edm::RunNumber_t>::const_iterator itRun = localRunRange.begin();
221     itRun != localRunRange.end(); itRun++) {
222     globalRunRange_.push_back((*itRun));
223     }
224     }
225    
226     //============================================================================
227     unsigned int TkModuleGroupSelector::getNumberOfParameters() const
228     {
229     return nparameters_;
230     }
231    
232     //============================================================================
233     unsigned int TkModuleGroupSelector::numIovs() const
234     {
235     return globalRunRange_.size();
236     }
237    
238     //============================================================================
239     edm::RunNumber_t TkModuleGroupSelector::firstRunOfIOV(unsigned int iovNum) const
240     {
241     return iovNum < this->numIovs() ? globalRunRange_.at(iovNum) : 0;
242     }
243    
244     //======================================================================
245     int TkModuleGroupSelector::getParameterIndexFromDetId(unsigned int detId,
246     edm::RunNumber_t run) const
247     {
248     // Return the index of the parameter that is used for this DetId.
249     // If this DetId is not treated, return values < 0.
250    
251     const DetId temp_id(detId);
252    
253     int index = -1;
254    
255     bool sel = false;
256     for(std::vector<int>::const_iterator itSubDets = subdetids_.begin();
257     itSubDets != subdetids_.end();
258     itSubDets++) {
259     if (temp_id.det() == DetId::Tracker && temp_id.subdetId() == (*itSubDets)) {
260     sel = true;
261     break;
262     }
263     }
264    
265     if (temp_id.det() != DetId::Tracker || !sel) return -1;
266    
267     std::map<unsigned int, unsigned int>::const_iterator it = mapDetIdGroupId_.find(detId);
268     if(it != mapDetIdGroupId_.end()) {
269     const unsigned int iAlignableGroup = (*it).second;
270     const std::vector<edm::RunNumber_t> &runs = runRange_.at(iAlignableGroup);
271     const unsigned int id0 = firstId_.at(iAlignableGroup);
272     const edm::RunNumber_t refrun = referenceRun_.at(iAlignableGroup);
273     // assuming runs is never empty (checked in createModuleGroups(..))
274     if (runs[0] > run) {
275     throw cms::Exception("BadConfig")
276     << "@SUB=TkModuleGroupSelector::getParameterIndexFromDetId:\n"
277 jbehr 1.4 << "Run " << run << " not foreseen for detid ('"<< detId <<"')"
278     << " in module group " << iAlignableGroup << ".";
279 flucke 1.2 }
280     unsigned int iovNum = 0;
281     for ( ; iovNum < runs.size(); ++iovNum) {
282     if (run >= runs[iovNum]) break;
283     }
284     //test whether the iov contains the reference run
285     if(refrun > 0) { //if > 0 a reference run number has been provided
286     if(iovNum+1 == runs.size()) {
287     if(refrun >= runs[iovNum])
288     return -1;
289     } else if( (iovNum+1) < runs.size()) {
290     if(refrun >= runs[iovNum] && refrun < runs[iovNum+1]) {
291     return -1;
292     }
293 jbehr 1.4 }
294     if(run > refrun) {
295     //iovNum > 0 due to checks in createGroup(...) and createModuleGroups(...)
296     //remove IOV in which the reference run can be found
297     iovNum -= 1;
298 flucke 1.2 }
299     }
300    
301     index = id0 + iovNum;
302     }
303     return index;
304     }