ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/CMSSW/Alignment/CommonAlignmentAlgorithm/src/AlignmentParameterStore.cc
Revision: 1.18
Committed: Wed Sep 12 14:26:01 2007 UTC (17 years, 7 months ago) by flucke
Content type: text/plain
Branch: MAIN
CVS Tags: V01-04-00-08
Changes since 1.17: +8 -9 lines
Log Message:
flag to construct always 6 hierarchy constraints - even if some parameters of 'master' are deselected

File Contents

# User Rev Content
1 flucke 1.9 /**
2     * \file AlignmentParameterStore.cc
3     *
4 flucke 1.18 * $Revision: 1.17 $
5     * $Date: 2007/06/12 15:08:19 $
6     * (last update by $Author: ewidl $)
7 flucke 1.9 */
8    
9 fronga 1.1 #include "FWCore/MessageLogger/interface/MessageLogger.h"
10     #include "FWCore/Utilities/interface/Exception.h"
11    
12 cklae 1.16 #include "Alignment/CommonAlignment/interface/Alignable.h"
13 flucke 1.9 #include "Alignment/TrackerAlignment/interface/TrackerAlignableId.h"
14    
15 cklae 1.10 #include "Alignment/CommonAlignment/interface/Utilities.h"
16 flucke 1.9 #include "Alignment/CommonAlignmentParametrization/interface/RigidBodyAlignmentParameters.h"
17 flucke 1.12 #include "Alignment/CommonAlignmentParametrization/interface/FrameToFrameDerivative.h"
18 cklae 1.10 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentCorrelationsStore.h"
19     #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentExtendedCorrelationsStore.h"
20 fronga 1.1
21     // This class's header
22     #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
23    
24     //__________________________________________________________________________________________________
25 flucke 1.9 AlignmentParameterStore::AlignmentParameterStore( const 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     theTrackerAlignableId = new TrackerAlignableId;
37    
38 flucke 1.9 edm::LogInfo("Alignment") << "@SUB=AlignmentParameterStore"
39     << "Created with " << theAlignables.size() << " alignables.";
40     }
41 fronga 1.1
42 flucke 1.9 //__________________________________________________________________________________________________
43     AlignmentParameterStore::~AlignmentParameterStore()
44     {
45     delete theCorrelationsStore;
46     delete theTrackerAlignableId;
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     std::vector<Alignable*> 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     std::vector<Alignable*>::const_iterator it1;
98     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     std::vector<Alignable*>::const_iterator it2;
119     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     AlignmentParameterStore::selectParameters( const std::vector<Alignable*>& alignables ) const
138     {
139    
140     std::vector<Alignable*> selectedAlignables;
141     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     std::vector<Alignable*>::const_iterator ita;
148     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     std::vector<Alignable*>::const_iterator it1;
165     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     std::vector<Alignable*>::const_iterator it2;
186     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     std::vector<Alignable*> 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 ewidl 1.6 for( std::vector<Alignable*>::const_iterator it=alignables.begin(); it != alignables.end(); ++it )
214     {
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     for( std::vector<Alignable*>::const_iterator it2 = alignables.begin(); it2 != it; ++it2 )
226     {
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     std::vector<Alignable*> AlignmentParameterStore::validAlignables(void) const
239     {
240     std::vector<Alignable*> result;
241     for (std::vector<Alignable*>::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 ewidl 1.6 std::vector<Alignable*>::const_iterator iali;
267     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     // Get alignment parameters
277     RigidBodyAlignmentParameters* ap =
278 ewidl 1.6 dynamic_cast<RigidBodyAlignmentParameters*>( alignable->alignmentParameters() );
279 fronga 1.1
280     if ( !ap )
281 ewidl 1.6 throw cms::Exception("BadAlignable")
282     << "applyParameters: provided alignable does not have rigid body alignment parameters";
283 fronga 1.1
284     // Translation in local frame
285 cklae 1.10 AlgebraicVector shift = ap->translation(); // fixme: should be LocalVector
286 fronga 1.1
287     // Translation local->global
288 cklae 1.10 align::LocalVector lv(shift[0], shift[1], shift[2]);
289     alignable->move( alignable->surface().toGlobal(lv) );
290 fronga 1.1
291     // Rotation in local frame
292 cklae 1.10 align::EulerAngles angles = ap->rotation();
293 cklae 1.16 alignable->rotateInLocalFrame( align::toMatrix(angles) );
294 fronga 1.1 }
295    
296    
297     //__________________________________________________________________________________________________
298     void AlignmentParameterStore::resetParameters(void)
299     {
300     // Erase contents of correlation map
301 ewidl 1.6 theCorrelationsStore->resetCorrelations();
302 fronga 1.1
303     // Iterate over alignables in the store and reset parameters
304 ewidl 1.6 std::vector<Alignable*>::const_iterator iali;
305     for ( iali = theAlignables.begin(); iali != theAlignables.end(); ++iali )
306     resetParameters( *iali );
307 fronga 1.1 }
308    
309    
310     //__________________________________________________________________________________________________
311     void AlignmentParameterStore::resetParameters( Alignable* ali )
312     {
313     if ( ali )
314 ewidl 1.6 {
315     // Get alignment parameters for this alignable
316     AlignmentParameters* ap = ali->alignmentParameters();
317     if ( ap )
318 flucke 1.4 {
319 ewidl 1.6 int npar=ap->numSelected();
320 flucke 1.4
321 ewidl 1.6 AlgebraicVector par(npar,0);
322     AlgebraicSymMatrix cov(npar,0);
323     AlignmentParameters* apnew = ap->cloneFromSelected(par,cov);
324     ali->setAlignmentParameters(apnew);
325     apnew->setValid(false);
326 flucke 1.4 }
327 ewidl 1.6 else
328     edm::LogError("BadArgument") << "@SUB=AlignmentParameterStore::resetParameters"
329     << "alignable has no alignment parameter";
330     }
331 fronga 1.1 else
332 flucke 1.4 edm::LogError("BadArgument") << "@SUB=AlignmentParameterStore::resetParameters"
333     << "argument is NULL";
334 fronga 1.1 }
335    
336    
337     //__________________________________________________________________________________________________
338     void AlignmentParameterStore::acquireRelativeParameters(void)
339     {
340    
341 cklae 1.10 unsigned int nAlignables = theAlignables.size();
342    
343     for (unsigned int i = 0; i < nAlignables; ++i)
344 ewidl 1.6 {
345 cklae 1.10 Alignable* ali = theAlignables[i];
346    
347 ewidl 1.6 RigidBodyAlignmentParameters* ap =
348 cklae 1.10 dynamic_cast<RigidBodyAlignmentParameters*>( ali->alignmentParameters() );
349 ewidl 1.6
350     if ( !ap )
351     throw cms::Exception("BadAlignable")
352     << "acquireRelativeParameters: "
353     << "provided alignable does not have rigid body alignment parameters";
354 fronga 1.1
355 ewidl 1.6 AlgebraicVector par( ap->size(),0 );
356     AlgebraicSymMatrix cov( ap->size(), 0 );
357 fronga 1.1
358 ewidl 1.6 // Get displacement and transform global->local
359 cklae 1.10 align::LocalVector dloc = ali->surface().toLocal( ali->displacement() );
360 ewidl 1.6 par[0]=dloc.x();
361     par[1]=dloc.y();
362     par[2]=dloc.z();
363    
364     // Transform to local euler angles
365 cklae 1.10 align::EulerAngles euloc = align::toAngles( ali->surface().toLocal( ali->rotation() ) );
366     par[3]=euloc(1);
367     par[4]=euloc(2);
368     par[5]=euloc(3);
369 fronga 1.1
370 ewidl 1.6 // Clone parameters
371     RigidBodyAlignmentParameters* apnew = ap->clone(par,cov);
372 fronga 1.1
373 cklae 1.10 ali->setAlignmentParameters(apnew);
374 ewidl 1.6 }
375 fronga 1.1 }
376    
377    
378     //__________________________________________________________________________________________________
379     // Get type/layer from Alignable
380     // type: -6 -5 -4 -3 -2 -1 1 2 3 4 5 6
381     // TEC- TOB- TID- TIB- PxEC- PxBR- PxBr+ PxEC+ TIB+ TID+ TOB+ TEC+
382     // Layers start from zero
383 flucke 1.9 std::pair<int,int> AlignmentParameterStore::typeAndLayer(const Alignable* ali) const
384 fronga 1.1 {
385     return theTrackerAlignableId->typeAndLayerFromAlignable( ali );
386     }
387    
388    
389     //__________________________________________________________________________________________________
390     void AlignmentParameterStore::
391     applyAlignableAbsolutePositions( const Alignables& alivec,
392 ewidl 1.6 const AlignablePositions& newpos,
393     int& ierr )
394 fronga 1.1 {
395     unsigned int nappl=0;
396     ierr=0;
397    
398     // Iterate over list of alignables
399 ewidl 1.6 for ( Alignables::const_iterator iali = alivec.begin(); iali != alivec.end(); ++iali )
400     {
401     Alignable* ali = *iali;
402     unsigned int detId = theTrackerAlignableId->alignableId(ali);
403     int typeId = theTrackerAlignableId->alignableTypeId(ali);
404    
405     // Find corresponding entry in AlignablePositions
406     bool found=false;
407     for ( AlignablePositions::const_iterator ipos = newpos.begin(); ipos != newpos.end(); ++ipos )
408     if ( detId == ipos->id() && typeId == ipos->objId() )
409     if ( found )
410     edm::LogError("DuplicatePosition")
411     << "New positions for alignable found more than once!";
412     else
413     {
414     // New position/rotation
415 cklae 1.10 const align::PositionType& pnew = ipos->pos();
416     const align::RotationType& rnew = ipos->rot();
417 ewidl 1.6 // Current position / rotation
418 cklae 1.10 const align::PositionType& pold = ali->globalPosition();
419     const align::RotationType& rold = ali->globalRotation();
420 fronga 1.1
421 ewidl 1.6 // shift needed to move from current to new position
422 cklae 1.10 align::GlobalVector posDiff = pnew - pold;
423     align::RotationType rotDiff = rold.multiplyInverse(rnew);
424     align::rectify(rotDiff); // correct for rounding errors
425     ali->move( posDiff );
426     ali->rotateInGlobalFrame( rotDiff );
427     LogDebug("NewPosition") << "moving by:" << posDiff;
428     LogDebug("NewRotation") << "rotating by:\n" << rotDiff;
429    
430 ewidl 1.6 // add position error
431     // AlignmentPositionError ape(shift.x(),shift.y(),shift.z());
432     // (*iali)->addAlignmentPositionError(ape);
433     // (*iali)->addAlignmentPositionErrorFromRotation(rot);
434 fronga 1.1
435 ewidl 1.6 found=true;
436     ++nappl;
437 fronga 1.1 }
438 ewidl 1.6 }
439 fronga 1.1
440     if ( nappl< newpos.size() )
441 ewidl 1.6 edm::LogError("Mismatch") << "Applied only " << nappl << " new positions"
442     << " out of " << newpos.size();
443 fronga 1.1
444 flucke 1.4 LogDebug("NewPositions") << "Applied new positions for " << nappl
445     << " out of " << alivec.size() <<" alignables.";
446 fronga 1.1
447     }
448    
449    
450     //__________________________________________________________________________________________________
451     void AlignmentParameterStore::
452 ewidl 1.6 applyAlignableRelativePositions( const Alignables& alivec, const AlignableShifts& shifts, int& ierr )
453 fronga 1.1 {
454    
455 cklae 1.10 ierr=0;
456 fronga 1.1 unsigned int nappl=0;
457 cklae 1.10 unsigned int nAlignables = alivec.size();
458 fronga 1.1
459 cklae 1.10 for (unsigned int i = 0; i < nAlignables; ++i)
460 ewidl 1.6 {
461 cklae 1.10 Alignable* ali = alivec[i];
462    
463     unsigned int detId = theTrackerAlignableId->alignableId(ali);
464     int typeId=theTrackerAlignableId->alignableTypeId(ali);
465 ewidl 1.6
466     // Find corresponding entry in AlignableShifts
467     bool found = false;
468     for ( AlignableShifts::const_iterator ipos = shifts.begin(); ipos != shifts.end(); ++ipos )
469     {
470     if ( detId == ipos->id() && typeId == ipos->objId() )
471     if ( found )
472     edm::LogError("DuplicatePosition")
473     << "New positions for alignable found more than once!";
474     else
475     {
476 cklae 1.10 ali->move( ipos->pos() );
477     ali->rotateInGlobalFrame( ipos->rot() );
478 ewidl 1.6
479     // Add position error
480     //AlignmentPositionError ape(pnew.x(),pnew.y(),pnew.z());
481 cklae 1.10 //ali->addAlignmentPositionError(ape);
482     //ali->addAlignmentPositionErrorFromRotation(rnew);
483 fronga 1.1
484 ewidl 1.6 found=true;
485     ++nappl;
486 fronga 1.1 }
487 ewidl 1.6 }
488     }
489 fronga 1.1
490     if ( nappl < shifts.size() )
491 ewidl 1.6 edm::LogError("Mismatch") << "Applied only " << nappl << " new positions"
492     << " out of " << shifts.size();
493 fronga 1.1
494 flucke 1.4 LogDebug("NewPositions") << "Applied new positions for " << nappl << " alignables.";
495 fronga 1.1 }
496    
497    
498    
499     //__________________________________________________________________________________________________
500     void AlignmentParameterStore::attachAlignmentParameters( const Parameters& parvec, int& ierr )
501     {
502     attachAlignmentParameters( theAlignables, parvec, ierr);
503     }
504    
505    
506    
507     //__________________________________________________________________________________________________
508     void AlignmentParameterStore::attachAlignmentParameters( const Alignables& alivec,
509 ewidl 1.6 const Parameters& parvec, int& ierr )
510 fronga 1.1 {
511     int ipass = 0;
512     int ifail = 0;
513     ierr = 0;
514    
515     // Iterate over alignables
516 ewidl 1.6 for ( Alignables::const_iterator iali = alivec.begin(); iali != alivec.end(); ++iali )
517     {
518     // Iterate over Parameters
519     bool found=false;
520     for ( Parameters::const_iterator ipar = parvec.begin(); ipar != parvec.end(); ++ipar)
521     {
522     // Get new alignment parameters
523     RigidBodyAlignmentParameters* ap = dynamic_cast<RigidBodyAlignmentParameters*>(*ipar);
524    
525     // Check if parameters belong to alignable
526     if ( ap->alignable() == (*iali) )
527     {
528     if (!found)
529     {
530     (*iali)->setAlignmentParameters(ap);
531     ++ipass;
532     found=true;
533     }
534     else edm::LogError("DuplicateParameters") << "More than one parameters for Alignable";
535     }
536     }
537     if (!found) ++ifail;
538     }
539 fronga 1.1 if (ifail>0) ierr=-1;
540    
541 ewidl 1.6 LogDebug("attachAlignmentParameters") << " Parameters, Alignables: " << parvec.size() << ","
542     << alivec.size() << "\n pass,fail: " << ipass << ","<< ifail;
543 fronga 1.1 }
544    
545    
546     //__________________________________________________________________________________________________
547     void AlignmentParameterStore::attachCorrelations( const Correlations& cormap,
548 ewidl 1.6 bool overwrite, int& ierr )
549 fronga 1.1 {
550     attachCorrelations( theAlignables, cormap, overwrite, ierr );
551     }
552    
553    
554     //__________________________________________________________________________________________________
555     void AlignmentParameterStore::attachCorrelations( const Alignables& alivec,
556 ewidl 1.6 const Correlations& cormap,
557     bool overwrite, int& ierr )
558 fronga 1.1 {
559     ierr=0;
560     int icount=0;
561    
562     // Iterate over correlations
563 ewidl 1.6 for ( Correlations::const_iterator icor = cormap.begin(); icor!=cormap.end(); ++icor )
564     {
565     AlgebraicMatrix mat=(*icor).second;
566     Alignable* ali1 = (*icor).first.first;
567     Alignable* ali2 = (*icor).first.second;
568    
569     // Check if alignables exist
570     if ( find( alivec.begin(), alivec.end(), ali1 ) != alivec.end() &&
571     find( alivec.begin(), alivec.end(), ali2 ) != alivec.end() )
572     {
573     // Check if correlations already existing between these alignables
574     if ( !theCorrelationsStore->correlationsAvailable(ali1,ali2) || (overwrite) )
575     {
576     theCorrelationsStore->setCorrelations(ali1,ali2,mat);
577     ++icount;
578     }
579     else edm::LogInfo("AlreadyExists") << "Correlation existing and not overwritten";
580     }
581     else edm::LogInfo("IgnoreCorrelation") << "Ignoring correlation with no alignables!";
582     }
583 fronga 1.1
584 ewidl 1.6 LogDebug( "attachCorrelations" ) << " Alignables,Correlations: " << alivec.size() <<","<< cormap.size()
585     << "\n applied: " << icount ;
586 fronga 1.1
587     }
588    
589    
590     //__________________________________________________________________________________________________
591     void AlignmentParameterStore::
592     attachUserVariables( const Alignables& alivec,
593 ewidl 1.6 const std::vector<AlignmentUserVariables*>& uvarvec, int& ierr )
594 fronga 1.1 {
595     ierr=0;
596    
597 flucke 1.4 LogDebug("DumpArguments") << "size of alivec: " << alivec.size()
598     << "\nsize of uvarvec: " << uvarvec.size();
599 fronga 1.1
600     std::vector<AlignmentUserVariables*>::const_iterator iuvar=uvarvec.begin();
601    
602 ewidl 1.6 for ( Alignables::const_iterator iali=alivec.begin(); iali!=alivec.end(); ++iali, ++iuvar )
603     {
604     AlignmentParameters* ap = (*iali)->alignmentParameters();
605     AlignmentUserVariables* uvarnew = (*iuvar);
606     ap->setUserVariables(uvarnew);
607     }
608 fronga 1.1 }
609    
610    
611     //__________________________________________________________________________________________________
612     void AlignmentParameterStore::setAlignmentPositionError( const Alignables& alivec,
613 cklae 1.11 double valshift, double valrot )
614 fronga 1.1 {
615 cklae 1.10 unsigned int nAlignables = alivec.size();
616    
617     for (unsigned int i = 0; i < nAlignables; ++i)
618 ewidl 1.6 {
619 cklae 1.10 Alignable* ali = alivec[i];
620 fronga 1.1
621 fronga 1.7 // First reset APE
622     AlignmentPositionError nulApe(0,0,0);
623 cklae 1.10 ali->setAlignmentPositionError(nulApe);
624 fronga 1.7
625     // Set APE from displacement
626     AlignmentPositionError ape(valshift,valshift,valshift);
627 cklae 1.10 if ( valshift > 0. ) ali->addAlignmentPositionError(ape);
628     else ali->setAlignmentPositionError(ape);
629 fronga 1.7
630     // Set APE from rotation
631 cklae 1.10 align::EulerAngles r(3);
632     r(1)=valrot; r(2)=valrot; r(3)=valrot;
633     ali->addAlignmentPositionErrorFromRotation( align::toMatrix(r) );
634 ewidl 1.6 }
635 cklae 1.11
636 flucke 1.18 LogDebug("StoreAPE") << "Store APE from shift: " << valshift
637     << "\nStore APE from rotation: " << valrot;
638 fronga 1.1 }
639 flucke 1.12
640     //__________________________________________________________________________________________________
641     bool AlignmentParameterStore
642     ::hierarchyConstraints(const Alignable *ali, const Alignables &aliComps,
643     std::vector<std::vector<ParameterId> > &paramIdsVecOut,
644     std::vector<std::vector<float> > &factorsVecOut,
645 flucke 1.18 bool all6, float epsilon) const
646 flucke 1.12 {
647 flucke 1.18 // Weak point if all6 = false:
648 flucke 1.12 // Ignores constraints between non-subsequent levels in case the parameter is not considered in
649     // the intermediate level, e.g. global z for dets and layers is aligned, but not for rods!
650     if (!ali || !ali->alignmentParameters()) return false;
651    
652     const std::vector<bool> &aliSel= ali->alignmentParameters()->selector();
653     paramIdsVecOut.clear();
654     factorsVecOut.clear();
655     FrameToFrameDerivative f2fDerivMaker;
656    
657     bool firstComp = true;
658     for (Alignables::const_iterator iComp = aliComps.begin(), iCompE = aliComps.end();
659     iComp != iCompE; ++iComp) {
660     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     }