ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/CMSSW/Alignment/CommonAlignmentAlgorithm/src/AlignmentParameterStore.cc
Revision: 1.20
Committed: Tue Dec 4 23:25:34 2007 UTC (17 years, 5 months ago) by ratnik
Content type: text/plain
Branch: MAIN
CVS Tags: CMSSW_2_0_0_pre2, CMSSW_2_0_0_pre1, CMSSW_1_8_0_pre8, CMSSW_1_8_0_pre7, CMSSW_1_8_0_pre6, CMSSW_1_8_0_pre5, V02-00-02
Changes since 1.19: +4 -3 lines
Log Message:
QA campaign: includes cleanup based on includechecker results.

File Contents

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