ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/CMSSW/Alignment/CommonAlignmentAlgorithm/src/AlignmentParameterStore.cc
Revision: 1.23
Committed: Tue Sep 2 15:31:23 2008 UTC (16 years, 8 months ago) by flucke
Content type: text/plain
Branch: MAIN
CVS Tags: CMSSW_3_0_0_pre10, CMSSW_3_0_0_pre9, CMSSW_3_1_0_pre1, CMSSW_3_0_0_pre8, CMSSW_3_0_0_pre7, CMSSW_3_0_0_pre6, CMSSW_3_0_0_pre5, CMSSW_3_0_0_pre4, V02-03-01, CMSSW_3_0_0_pre3, V02-03-00
Changes since 1.22: +27 -27 lines
Log Message:
Make choice of AlignmentParameters configurable,
it was hardcoded to RigidBody before.
RigidBody will become default in configuration of AlignmentProducer.

File Contents

# User Rev Content
1 flucke 1.9 /**
2     * \file AlignmentParameterStore.cc
3     *
4 flucke 1.23 * $Revision: 1.22 $
5     * $Date: 2008/02/12 22:50:54 $
6     * (last update by $Author: cklae $)
7 flucke 1.9 */
8    
9 flucke 1.23 // This class's header should be first
10     #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
11    
12 fronga 1.1 #include "FWCore/MessageLogger/interface/MessageLogger.h"
13 flucke 1.23 #include "FWCore/Utilities/interface/Exception.h"
14 cklae 1.19 #include "FWCore/ParameterSet/interface/ParameterSet.h"
15 fronga 1.1
16 cklae 1.16 #include "Alignment/CommonAlignment/interface/Alignable.h"
17 cklae 1.19 #include "Alignment/CommonAlignment/interface/AlignableDetOrUnitPtr.h"
18 flucke 1.9 #include "Alignment/TrackerAlignment/interface/TrackerAlignableId.h"
19    
20     #include "Alignment/CommonAlignmentParametrization/interface/RigidBodyAlignmentParameters.h"
21 flucke 1.12 #include "Alignment/CommonAlignmentParametrization/interface/FrameToFrameDerivative.h"
22 flucke 1.23 #include "Alignment/CommonAlignmentParametrization/interface/AlignmentParametersFactory.h"
23 cklae 1.10 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentExtendedCorrelationsStore.h"
24 cklae 1.19 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
25 fronga 1.1
26    
27     //__________________________________________________________________________________________________
28 cklae 1.19 AlignmentParameterStore::AlignmentParameterStore( const align::Alignables &alis,
29 ewidl 1.6 const edm::ParameterSet& config ) :
30 flucke 1.9 theAlignables(alis)
31 fronga 1.1 {
32 flucke 1.9 if (config.getUntrackedParameter<bool>("UseExtendedCorrelations")) {
33     theCorrelationsStore = new AlignmentExtendedCorrelationsStore
34     (config.getParameter<edm::ParameterSet>("ExtendedCorrelationsConfig"));
35     } else {
36 ewidl 1.6 theCorrelationsStore = new AlignmentCorrelationsStore();
37     }
38 fronga 1.1
39 flucke 1.9 edm::LogInfo("Alignment") << "@SUB=AlignmentParameterStore"
40     << "Created with " << theAlignables.size() << " alignables.";
41     }
42 fronga 1.1
43 flucke 1.9 //__________________________________________________________________________________________________
44     AlignmentParameterStore::~AlignmentParameterStore()
45     {
46     delete theCorrelationsStore;
47 fronga 1.1 }
48    
49     //__________________________________________________________________________________________________
50 ewidl 1.6 CompositeAlignmentParameters
51 fronga 1.1 AlignmentParameterStore::selectParameters( const std::vector<AlignableDet*>& alignabledets ) const
52     {
53 cklae 1.16 std::vector<AlignableDetOrUnitPtr> detOrUnits;
54     detOrUnits.reserve(alignabledets.size());
55 ewidl 1.17
56     std::vector<AlignableDet*>::const_iterator it, iEnd;
57     for ( it = alignabledets.begin(), iEnd = alignabledets.end(); it != iEnd; ++it)
58     detOrUnits.push_back(AlignableDetOrUnitPtr(*it));
59 cklae 1.16
60     return this->selectParameters(detOrUnits);
61     }
62    
63     //__________________________________________________________________________________________________
64     CompositeAlignmentParameters
65     AlignmentParameterStore::selectParameters( const std::vector<AlignableDetOrUnitPtr>& alignabledets ) const
66     {
67 fronga 1.1
68 cklae 1.19 align::Alignables alignables;
69 cklae 1.16 std::map <AlignableDetOrUnitPtr,Alignable*> alidettoalimap;
70 fronga 1.1 std::map <Alignable*,int> aliposmap;
71     std::map <Alignable*,int> alilenmap;
72     int nparam=0;
73    
74     // iterate over AlignableDet's
75 cklae 1.16 for( std::vector<AlignableDetOrUnitPtr>::const_iterator iad = alignabledets.begin();
76     iad != alignabledets.end(); ++iad )
77 ewidl 1.6 {
78 flucke 1.9 Alignable* ali = alignableFromAlignableDet( *iad );
79 ewidl 1.6 if ( ali )
80     {
81     alidettoalimap[ *iad ] = ali; // Add to map
82     // Check if Alignable already there, insert into vector if not
83     if ( find(alignables.begin(),alignables.end(),ali) == alignables.end() )
84     {
85     alignables.push_back(ali);
86     AlignmentParameters* ap = ali->alignmentParameters();
87     nparam += ap->numSelected();
88     }
89     }
90     }
91    
92     AlgebraicVector* selpar = new AlgebraicVector( nparam, 0 );
93     AlgebraicSymMatrix* selcov = new AlgebraicSymMatrix( nparam, 0 );
94 fronga 1.1
95 ewidl 1.6 // Fill in parameters and corresponding covariance matricess
96     int ipos = 1; // NOTE: .sub indices start from 1
97 cklae 1.19 align::Alignables::const_iterator it1;
98 ewidl 1.6 for( it1 = alignables.begin(); it1 != alignables.end(); ++it1 )
99     {
100     AlignmentParameters* ap = (*it1)->alignmentParameters();
101     selpar->sub( ipos, ap->selectedParameters() );
102     selcov->sub( ipos, ap->selectedCovariance() );
103     int npar = ap->numSelected();
104     aliposmap[*it1]=ipos;
105     alilenmap[*it1]=npar;
106     ipos +=npar;
107     }
108 fronga 1.1
109 ewidl 1.6 // Fill in the correlations. Has to be an extra loop, because the
110     // AlignmentExtendedCorrelationsStore (if used) needs the
111     // alignables' covariance matrices already present.
112     ipos = 1;
113     for( it1 = alignables.begin(); it1 != alignables.end(); ++it1 )
114     {
115     int jpos=1;
116    
117     // Look for correlations between alignables
118 cklae 1.19 align::Alignables::const_iterator it2;
119 ewidl 1.6 for( it2 = alignables.begin(); it2 != it1; ++it2 )
120     {
121     theCorrelationsStore->correlations( *it1, *it2, *selcov, ipos-1, jpos-1 );
122     jpos += (*it2)->alignmentParameters()->numSelected();
123     }
124 fronga 1.1
125 ewidl 1.6 ipos += (*it1)->alignmentParameters()->numSelected();
126 fronga 1.1 }
127    
128 ewidl 1.6 AlignmentParametersData::DataContainer data( new AlignmentParametersData( selpar, selcov ) );
129     CompositeAlignmentParameters aap( data, alignables, alidettoalimap, aliposmap, alilenmap );
130    
131 fronga 1.1 return aap;
132     }
133    
134    
135     //__________________________________________________________________________________________________
136 ewidl 1.17 CompositeAlignmentParameters
137 cklae 1.19 AlignmentParameterStore::selectParameters( const align::Alignables& alignables ) const
138 ewidl 1.17 {
139    
140 cklae 1.19 align::Alignables selectedAlignables;
141 ewidl 1.17 std::map <AlignableDetOrUnitPtr,Alignable*> alidettoalimap; // This map won't be filled!!!
142     std::map <Alignable*,int> aliposmap;
143     std::map <Alignable*,int> alilenmap;
144     int nparam=0;
145    
146     // iterate over Alignable's
147 cklae 1.19 align::Alignables::const_iterator ita;
148 ewidl 1.17 for ( ita = alignables.begin(); ita != alignables.end(); ++ita )
149     {
150     // Check if Alignable already there, insert into vector if not
151     if ( find(selectedAlignables.begin(), selectedAlignables.end(), *ita) == selectedAlignables.end() )
152     {
153     selectedAlignables.push_back( *ita );
154     AlignmentParameters* ap = (*ita)->alignmentParameters();
155     nparam += ap->numSelected();
156     }
157     }
158    
159     AlgebraicVector* selpar = new AlgebraicVector( nparam, 0 );
160     AlgebraicSymMatrix* selcov = new AlgebraicSymMatrix( nparam, 0 );
161    
162     // Fill in parameters and corresponding covariance matrices
163     int ipos = 1; // NOTE: .sub indices start from 1
164 cklae 1.19 align::Alignables::const_iterator it1;
165 ewidl 1.17 for( it1 = selectedAlignables.begin(); it1 != selectedAlignables.end(); ++it1 )
166     {
167     AlignmentParameters* ap = (*it1)->alignmentParameters();
168     selpar->sub( ipos, ap->selectedParameters() );
169     selcov->sub( ipos, ap->selectedCovariance() );
170     int npar = ap->numSelected();
171     aliposmap[*it1]=ipos;
172     alilenmap[*it1]=npar;
173     ipos +=npar;
174     }
175    
176     // Fill in the correlations. Has to be an extra loop, because the
177     // AlignmentExtendedCorrelationsStore (if used) needs the
178     // alignables' covariance matrices already present.
179     ipos = 1;
180     for( it1 = selectedAlignables.begin(); it1 != selectedAlignables.end(); ++it1 )
181     {
182     int jpos=1;
183    
184     // Look for correlations between alignables
185 cklae 1.19 align::Alignables::const_iterator it2;
186 ewidl 1.17 for( it2 = selectedAlignables.begin(); it2 != it1; ++it2 )
187     {
188     theCorrelationsStore->correlations( *it1, *it2, *selcov, ipos-1, jpos-1 );
189     jpos += (*it2)->alignmentParameters()->numSelected();
190     }
191    
192     ipos += (*it1)->alignmentParameters()->numSelected();
193     }
194    
195     AlignmentParametersData::DataContainer data( new AlignmentParametersData( selpar, selcov ) );
196     CompositeAlignmentParameters aap( data, selectedAlignables, alidettoalimap, aliposmap, alilenmap );
197    
198     return aap;
199     }
200    
201    
202     //__________________________________________________________________________________________________
203 fronga 1.1 void AlignmentParameterStore::updateParameters( const CompositeAlignmentParameters& aap )
204     {
205    
206 cklae 1.19 align::Alignables alignables = aap.components();
207 ewidl 1.6 const AlgebraicVector& parameters = aap.parameters();
208     const AlgebraicSymMatrix& covariance = aap.covariance();
209 fronga 1.1
210 ewidl 1.6 int ipos = 1; // NOTE: .sub indices start from 1
211 fronga 1.1
212     // Loop over alignables
213 cklae 1.19 for( align::Alignables::const_iterator it=alignables.begin(); it != alignables.end(); ++it )
214 ewidl 1.6 {
215     // Update parameters and local covariance
216     AlignmentParameters* ap = (*it)->alignmentParameters();
217     int nsel = ap->numSelected();
218     AlgebraicVector subvec = parameters.sub( ipos, ipos+nsel-1 );
219     AlgebraicSymMatrix subcov = covariance.sub( ipos, ipos+nsel-1 );
220     AlignmentParameters* apnew = ap->cloneFromSelected( subvec, subcov );
221     (*it)->setAlignmentParameters( apnew );
222 fronga 1.1
223 ewidl 1.6 // Now update correlations between detectors
224     int jpos = 1;
225 cklae 1.19 for( align::Alignables::const_iterator it2 = alignables.begin(); it2 != it; ++it2 )
226 ewidl 1.6 {
227     theCorrelationsStore->setCorrelations( *it, *it2, covariance, ipos-1, jpos-1 );
228     jpos += (*it2)->alignmentParameters()->numSelected();
229     }
230    
231     ipos+=nsel;
232     }
233 fronga 1.1
234     }
235    
236    
237     //__________________________________________________________________________________________________
238 cklae 1.19 align::Alignables AlignmentParameterStore::validAlignables(void) const
239 fronga 1.1 {
240 cklae 1.19 align::Alignables result;
241     for (align::Alignables::const_iterator iali = theAlignables.begin();
242 ewidl 1.6 iali != theAlignables.end(); ++iali)
243     if ( (*iali)->alignmentParameters()->isValid() ) result.push_back(*iali);
244 fronga 1.1
245 flucke 1.4 LogDebug("Alignment") << "@SUB=AlignmentParameterStore::validAlignables"
246     << "Valid alignables: " << result.size()
247     << "out of " << theAlignables.size();
248 fronga 1.1 return result;
249     }
250    
251     //__________________________________________________________________________________________________
252 cklae 1.16 Alignable* AlignmentParameterStore::alignableFromAlignableDet( AlignableDetOrUnitPtr alignableDet ) const
253 fronga 1.1 {
254 flucke 1.9 Alignable *mother = alignableDet;
255     while (mother) {
256     if (mother->alignmentParameters()) return mother;
257     mother = mother->mother();
258     }
259 fronga 1.1
260 flucke 1.9 return 0;
261 fronga 1.1 }
262    
263     //__________________________________________________________________________________________________
264     void AlignmentParameterStore::applyParameters(void)
265     {
266 cklae 1.19 align::Alignables::const_iterator iali;
267 ewidl 1.6 for ( iali = theAlignables.begin(); iali != theAlignables.end(); ++iali)
268     applyParameters( *iali );
269 fronga 1.1 }
270    
271    
272     //__________________________________________________________________________________________________
273     void AlignmentParameterStore::applyParameters(Alignable* alignable)
274     {
275    
276 flucke 1.23 AlignmentParameters *pars = (alignable ? alignable->alignmentParameters() : 0);
277     if (!pars) {
278 ewidl 1.6 throw cms::Exception("BadAlignable")
279 flucke 1.23 << "applyParameters: provided alignable does not have alignment parameters";
280     }
281     pars->apply();
282 fronga 1.1 }
283    
284    
285     //__________________________________________________________________________________________________
286     void AlignmentParameterStore::resetParameters(void)
287     {
288     // Erase contents of correlation map
289 ewidl 1.6 theCorrelationsStore->resetCorrelations();
290 fronga 1.1
291     // Iterate over alignables in the store and reset parameters
292 cklae 1.19 align::Alignables::const_iterator iali;
293 ewidl 1.6 for ( iali = theAlignables.begin(); iali != theAlignables.end(); ++iali )
294     resetParameters( *iali );
295 fronga 1.1 }
296    
297    
298     //__________________________________________________________________________________________________
299     void AlignmentParameterStore::resetParameters( Alignable* ali )
300     {
301     if ( ali )
302 ewidl 1.6 {
303     // Get alignment parameters for this alignable
304     AlignmentParameters* ap = ali->alignmentParameters();
305     if ( ap )
306 flucke 1.4 {
307 ewidl 1.6 int npar=ap->numSelected();
308 flucke 1.4
309 ewidl 1.6 AlgebraicVector par(npar,0);
310     AlgebraicSymMatrix cov(npar,0);
311     AlignmentParameters* apnew = ap->cloneFromSelected(par,cov);
312     ali->setAlignmentParameters(apnew);
313     apnew->setValid(false);
314 flucke 1.4 }
315 ewidl 1.6 else
316     edm::LogError("BadArgument") << "@SUB=AlignmentParameterStore::resetParameters"
317     << "alignable has no alignment parameter";
318     }
319 fronga 1.1 else
320 flucke 1.4 edm::LogError("BadArgument") << "@SUB=AlignmentParameterStore::resetParameters"
321     << "argument is NULL";
322 fronga 1.1 }
323    
324    
325     //__________________________________________________________________________________________________
326     void AlignmentParameterStore::acquireRelativeParameters(void)
327     {
328    
329 cklae 1.10 unsigned int nAlignables = theAlignables.size();
330    
331     for (unsigned int i = 0; i < nAlignables; ++i)
332 ewidl 1.6 {
333 cklae 1.10 Alignable* ali = theAlignables[i];
334    
335 ewidl 1.6 RigidBodyAlignmentParameters* ap =
336 cklae 1.10 dynamic_cast<RigidBodyAlignmentParameters*>( ali->alignmentParameters() );
337 ewidl 1.6
338     if ( !ap )
339     throw cms::Exception("BadAlignable")
340     << "acquireRelativeParameters: "
341     << "provided alignable does not have rigid body alignment parameters";
342 fronga 1.1
343 ewidl 1.6 AlgebraicVector par( ap->size(),0 );
344     AlgebraicSymMatrix cov( ap->size(), 0 );
345 fronga 1.1
346 ewidl 1.6 // Get displacement and transform global->local
347 cklae 1.10 align::LocalVector dloc = ali->surface().toLocal( ali->displacement() );
348 ewidl 1.6 par[0]=dloc.x();
349     par[1]=dloc.y();
350     par[2]=dloc.z();
351    
352     // Transform to local euler angles
353 cklae 1.10 align::EulerAngles euloc = align::toAngles( ali->surface().toLocal( ali->rotation() ) );
354     par[3]=euloc(1);
355     par[4]=euloc(2);
356     par[5]=euloc(3);
357 fronga 1.1
358 ewidl 1.6 // Clone parameters
359     RigidBodyAlignmentParameters* apnew = ap->clone(par,cov);
360 fronga 1.1
361 cklae 1.10 ali->setAlignmentParameters(apnew);
362 ewidl 1.6 }
363 fronga 1.1 }
364    
365    
366     //__________________________________________________________________________________________________
367     // Get type/layer from Alignable
368     // type: -6 -5 -4 -3 -2 -1 1 2 3 4 5 6
369     // TEC- TOB- TID- TIB- PxEC- PxBR- PxBr+ PxEC+ TIB+ TID+ TOB+ TEC+
370     // Layers start from zero
371 flucke 1.9 std::pair<int,int> AlignmentParameterStore::typeAndLayer(const Alignable* ali) const
372 fronga 1.1 {
373 cklae 1.19 return TrackerAlignableId().typeAndLayerFromDetId( ali->id() );
374 fronga 1.1 }
375    
376    
377     //__________________________________________________________________________________________________
378     void AlignmentParameterStore::
379 cklae 1.19 applyAlignableAbsolutePositions( const align::Alignables& alivec,
380 ewidl 1.6 const AlignablePositions& newpos,
381     int& ierr )
382 fronga 1.1 {
383     unsigned int nappl=0;
384     ierr=0;
385    
386     // Iterate over list of alignables
387 cklae 1.19 for ( align::Alignables::const_iterator iali = alivec.begin(); iali != alivec.end(); ++iali )
388 ewidl 1.6 {
389     Alignable* ali = *iali;
390 cklae 1.19 align::ID id = ali->id();
391     align::StructureType typeId = ali->alignableObjectId();
392 ewidl 1.6
393     // Find corresponding entry in AlignablePositions
394     bool found=false;
395     for ( AlignablePositions::const_iterator ipos = newpos.begin(); ipos != newpos.end(); ++ipos )
396 cklae 1.19 if ( id == ipos->id() && typeId == ipos->objId() )
397 ewidl 1.6 if ( found )
398     edm::LogError("DuplicatePosition")
399     << "New positions for alignable found more than once!";
400     else
401     {
402     // New position/rotation
403 cklae 1.10 const align::PositionType& pnew = ipos->pos();
404     const align::RotationType& rnew = ipos->rot();
405 ewidl 1.6 // Current position / rotation
406 cklae 1.10 const align::PositionType& pold = ali->globalPosition();
407     const align::RotationType& rold = ali->globalRotation();
408 fronga 1.1
409 ewidl 1.6 // shift needed to move from current to new position
410 cklae 1.10 align::GlobalVector posDiff = pnew - pold;
411     align::RotationType rotDiff = rold.multiplyInverse(rnew);
412     align::rectify(rotDiff); // correct for rounding errors
413     ali->move( posDiff );
414     ali->rotateInGlobalFrame( rotDiff );
415     LogDebug("NewPosition") << "moving by:" << posDiff;
416     LogDebug("NewRotation") << "rotating by:\n" << rotDiff;
417    
418 ewidl 1.6 // add position error
419     // AlignmentPositionError ape(shift.x(),shift.y(),shift.z());
420     // (*iali)->addAlignmentPositionError(ape);
421     // (*iali)->addAlignmentPositionErrorFromRotation(rot);
422 fronga 1.1
423 ewidl 1.6 found=true;
424     ++nappl;
425 fronga 1.1 }
426 ewidl 1.6 }
427 fronga 1.1
428     if ( nappl< newpos.size() )
429 ewidl 1.6 edm::LogError("Mismatch") << "Applied only " << nappl << " new positions"
430     << " out of " << newpos.size();
431 fronga 1.1
432 flucke 1.4 LogDebug("NewPositions") << "Applied new positions for " << nappl
433     << " out of " << alivec.size() <<" alignables.";
434 fronga 1.1
435     }
436    
437    
438     //__________________________________________________________________________________________________
439     void AlignmentParameterStore::
440 cklae 1.19 applyAlignableRelativePositions( const align::Alignables& alivec, const AlignableShifts& shifts, int& ierr )
441 fronga 1.1 {
442    
443 cklae 1.10 ierr=0;
444 fronga 1.1 unsigned int nappl=0;
445 cklae 1.10 unsigned int nAlignables = alivec.size();
446 fronga 1.1
447 cklae 1.10 for (unsigned int i = 0; i < nAlignables; ++i)
448 ewidl 1.6 {
449 cklae 1.10 Alignable* ali = alivec[i];
450    
451 cklae 1.19 align::ID id = ali->id();
452     align::StructureType typeId = ali->alignableObjectId();
453 ewidl 1.6
454     // Find corresponding entry in AlignableShifts
455     bool found = false;
456     for ( AlignableShifts::const_iterator ipos = shifts.begin(); ipos != shifts.end(); ++ipos )
457     {
458 cklae 1.19 if ( id == ipos->id() && typeId == ipos->objId() )
459 ewidl 1.6 if ( found )
460     edm::LogError("DuplicatePosition")
461     << "New positions for alignable found more than once!";
462     else
463     {
464 cklae 1.10 ali->move( ipos->pos() );
465     ali->rotateInGlobalFrame( ipos->rot() );
466 ewidl 1.6
467     // Add position error
468     //AlignmentPositionError ape(pnew.x(),pnew.y(),pnew.z());
469 cklae 1.10 //ali->addAlignmentPositionError(ape);
470     //ali->addAlignmentPositionErrorFromRotation(rnew);
471 fronga 1.1
472 ewidl 1.6 found=true;
473     ++nappl;
474 fronga 1.1 }
475 ewidl 1.6 }
476     }
477 fronga 1.1
478     if ( nappl < shifts.size() )
479 ewidl 1.6 edm::LogError("Mismatch") << "Applied only " << nappl << " new positions"
480     << " out of " << shifts.size();
481 fronga 1.1
482 flucke 1.4 LogDebug("NewPositions") << "Applied new positions for " << nappl << " alignables.";
483 fronga 1.1 }
484    
485    
486    
487     //__________________________________________________________________________________________________
488     void AlignmentParameterStore::attachAlignmentParameters( const Parameters& parvec, int& ierr )
489     {
490     attachAlignmentParameters( theAlignables, parvec, ierr);
491     }
492    
493    
494    
495     //__________________________________________________________________________________________________
496 cklae 1.19 void AlignmentParameterStore::attachAlignmentParameters( const align::Alignables& alivec,
497 ewidl 1.6 const Parameters& parvec, int& ierr )
498 fronga 1.1 {
499     int ipass = 0;
500     int ifail = 0;
501     ierr = 0;
502    
503     // Iterate over alignables
504 cklae 1.19 for ( align::Alignables::const_iterator iali = alivec.begin(); iali != alivec.end(); ++iali )
505 ewidl 1.6 {
506     // Iterate over Parameters
507     bool found=false;
508     for ( Parameters::const_iterator ipar = parvec.begin(); ipar != parvec.end(); ++ipar)
509     {
510     // Get new alignment parameters
511 flucke 1.23 AlignmentParameters* ap = *ipar;
512 ewidl 1.6
513     // Check if parameters belong to alignable
514     if ( ap->alignable() == (*iali) )
515     {
516     if (!found)
517     {
518     (*iali)->setAlignmentParameters(ap);
519     ++ipass;
520     found=true;
521     }
522 flucke 1.23 else edm::LogError("Alignment") << "@SUB=AlignmentParameterStore::attachAlignmentParameters"
523     << "More than one parameters for Alignable.";
524 ewidl 1.6 }
525     }
526     if (!found) ++ifail;
527     }
528 fronga 1.1 if (ifail>0) ierr=-1;
529    
530 ewidl 1.6 LogDebug("attachAlignmentParameters") << " Parameters, Alignables: " << parvec.size() << ","
531     << alivec.size() << "\n pass,fail: " << ipass << ","<< ifail;
532 fronga 1.1 }
533    
534    
535     //__________________________________________________________________________________________________
536     void AlignmentParameterStore::attachCorrelations( const Correlations& cormap,
537 ewidl 1.6 bool overwrite, int& ierr )
538 fronga 1.1 {
539     attachCorrelations( theAlignables, cormap, overwrite, ierr );
540     }
541    
542    
543     //__________________________________________________________________________________________________
544 cklae 1.19 void AlignmentParameterStore::attachCorrelations( const align::Alignables& alivec,
545 ewidl 1.6 const Correlations& cormap,
546     bool overwrite, int& ierr )
547 fronga 1.1 {
548     ierr=0;
549     int icount=0;
550    
551     // Iterate over correlations
552 ewidl 1.6 for ( Correlations::const_iterator icor = cormap.begin(); icor!=cormap.end(); ++icor )
553     {
554     AlgebraicMatrix mat=(*icor).second;
555     Alignable* ali1 = (*icor).first.first;
556     Alignable* ali2 = (*icor).first.second;
557    
558     // Check if alignables exist
559     if ( find( alivec.begin(), alivec.end(), ali1 ) != alivec.end() &&
560     find( alivec.begin(), alivec.end(), ali2 ) != alivec.end() )
561     {
562     // Check if correlations already existing between these alignables
563     if ( !theCorrelationsStore->correlationsAvailable(ali1,ali2) || (overwrite) )
564     {
565     theCorrelationsStore->setCorrelations(ali1,ali2,mat);
566     ++icount;
567     }
568     else edm::LogInfo("AlreadyExists") << "Correlation existing and not overwritten";
569     }
570     else edm::LogInfo("IgnoreCorrelation") << "Ignoring correlation with no alignables!";
571     }
572 fronga 1.1
573 ewidl 1.6 LogDebug( "attachCorrelations" ) << " Alignables,Correlations: " << alivec.size() <<","<< cormap.size()
574     << "\n applied: " << icount ;
575 fronga 1.1
576     }
577    
578    
579     //__________________________________________________________________________________________________
580     void AlignmentParameterStore::
581 cklae 1.19 attachUserVariables( const align::Alignables& alivec,
582 ewidl 1.6 const std::vector<AlignmentUserVariables*>& uvarvec, int& ierr )
583 fronga 1.1 {
584     ierr=0;
585    
586 flucke 1.4 LogDebug("DumpArguments") << "size of alivec: " << alivec.size()
587     << "\nsize of uvarvec: " << uvarvec.size();
588 fronga 1.1
589     std::vector<AlignmentUserVariables*>::const_iterator iuvar=uvarvec.begin();
590    
591 cklae 1.19 for ( align::Alignables::const_iterator iali=alivec.begin(); iali!=alivec.end(); ++iali, ++iuvar )
592 ewidl 1.6 {
593     AlignmentParameters* ap = (*iali)->alignmentParameters();
594     AlignmentUserVariables* uvarnew = (*iuvar);
595     ap->setUserVariables(uvarnew);
596     }
597 fronga 1.1 }
598    
599    
600     //__________________________________________________________________________________________________
601 cklae 1.19 void AlignmentParameterStore::setAlignmentPositionError( const align::Alignables& alivec,
602 cklae 1.11 double valshift, double valrot )
603 fronga 1.1 {
604 cklae 1.10 unsigned int nAlignables = alivec.size();
605    
606     for (unsigned int i = 0; i < nAlignables; ++i)
607 ewidl 1.6 {
608 cklae 1.10 Alignable* ali = alivec[i];
609 fronga 1.1
610 fronga 1.7 // First reset APE
611     AlignmentPositionError nulApe(0,0,0);
612 cklae 1.10 ali->setAlignmentPositionError(nulApe);
613 fronga 1.7
614     // Set APE from displacement
615     AlignmentPositionError ape(valshift,valshift,valshift);
616 cklae 1.10 if ( valshift > 0. ) ali->addAlignmentPositionError(ape);
617     else ali->setAlignmentPositionError(ape);
618 fronga 1.7
619     // Set APE from rotation
620 cklae 1.10 align::EulerAngles r(3);
621     r(1)=valrot; r(2)=valrot; r(3)=valrot;
622     ali->addAlignmentPositionErrorFromRotation( align::toMatrix(r) );
623 ewidl 1.6 }
624 cklae 1.11
625 flucke 1.18 LogDebug("StoreAPE") << "Store APE from shift: " << valshift
626     << "\nStore APE from rotation: " << valrot;
627 fronga 1.1 }
628 flucke 1.12
629     //__________________________________________________________________________________________________
630     bool AlignmentParameterStore
631 cklae 1.19 ::hierarchyConstraints(const Alignable *ali, const align::Alignables &aliComps,
632 flucke 1.12 std::vector<std::vector<ParameterId> > &paramIdsVecOut,
633     std::vector<std::vector<float> > &factorsVecOut,
634 flucke 1.18 bool all6, float epsilon) const
635 flucke 1.12 {
636 flucke 1.18 // Weak point if all6 = false:
637 flucke 1.12 // Ignores constraints between non-subsequent levels in case the parameter is not considered in
638     // the intermediate level, e.g. global z for dets and layers is aligned, but not for rods!
639     if (!ali || !ali->alignmentParameters()) return false;
640    
641 flucke 1.23 if (ali->alignmentParameters()->type() != AlignmentParametersFactory::kRigidBody) {
642     throw cms::Exception("BadConfig") << "AlignmentParameterStore::hierarchyConstraints"
643     << " requires 'ali' to have rigid body alignment parameters.";
644     }
645    
646 flucke 1.12 const std::vector<bool> &aliSel= ali->alignmentParameters()->selector();
647     paramIdsVecOut.clear();
648     factorsVecOut.clear();
649     FrameToFrameDerivative f2fDerivMaker;
650    
651     bool firstComp = true;
652 cklae 1.19 for (align::Alignables::const_iterator iComp = aliComps.begin(), iCompE = aliComps.end();
653 flucke 1.12 iComp != iCompE; ++iComp) {
654 flucke 1.23 if ((*iComp)->alignmentParameters()->type() != AlignmentParametersFactory::kRigidBody) {
655     throw cms::Exception("BadConfig")
656     << "AlignmentParameterStore::hierarchyConstraints"
657     << " requires all 'aliComps' to have rigid body alignment parameters.";
658     }
659    
660 flucke 1.12 const AlgebraicMatrix f2fDeriv(f2fDerivMaker.frameToFrameDerivative(*iComp, ali));
661     const std::vector<bool> &aliCompSel = (*iComp)->alignmentParameters()->selector();
662     for (unsigned int iParMast = 0, iParMastUsed = 0; iParMast < aliSel.size(); ++iParMast) {
663 flucke 1.18 if (!all6 && !aliSel[iParMast]) continue;// no higher level parameter & constraint deselected
664 flucke 1.12 if (firstComp) { // fill output with empty arrays
665     paramIdsVecOut.push_back(std::vector<ParameterId>());
666     factorsVecOut.push_back(std::vector<float>());
667     }
668     for (int iParComp = 0; iParComp < f2fDeriv.num_col(); ++iParComp) {
669     if (aliCompSel[iParComp]) {
670     const float factor = f2fDeriv[iParMast][iParComp]; // switch col/row? GF: Should be fine.
671     if (fabs(factor) > epsilon) {
672     paramIdsVecOut[iParMastUsed].push_back(ParameterId(*iComp, iParComp));
673     factorsVecOut[iParMastUsed].push_back(factor);
674     }
675     }
676     }
677     ++iParMastUsed;
678     }
679     firstComp = false;
680     } // end loop on components
681    
682     return true;
683     }