ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/CMSSW/Alignment/CommonAlignmentAlgorithm/src/AlignmentParameterStore.cc
Revision: 1.21
Committed: Tue Jan 22 18:46:13 2008 UTC (17 years, 3 months ago) by muzaffar
Content type: text/plain
Branch: MAIN
CVS Tags: V02-01-00
Changes since 1.20: +3 -4 lines
Log Message:
include cleanup. Only for cc/cpp files

File Contents

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