Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ConvergenceMaster.cxx

Go to the documentation of this file.
00001 //_____________________________________________________________________________
00015 
00016 #include <vector>
00017 #include <algorithm>
00018 #include <cassert>
00019 #include <numeric>
00020 
00021 #include "MessageService/MsgService.h"
00022 
00023 #include "CandFitTrackSA/DataFT.h"
00024 #include "CandFitTrackSA/TrackContext.h"
00025 #include "CandFitTrackSA/TracerSA.h"
00026 
00027 #include "ConvergenceMaster.h"
00028 
00029 CVSID("$Id: ConvergenceMaster.cxx,v 1.4 2006/12/03 01:41:37 gmieg Exp $");
00030 
00034 ConvergenceMaster::ConvergenceMaster(Int_t nhitsinviewmin) :
00035     fNHitsInViewMin(nhitsinviewmin)
00036 {
00037     TracerSA trace("ConvergenceMaster::ConvergenceMaster(Int_t)");
00038 }
00039 
00043 ConvergenceMaster::~ConvergenceMaster()
00044 {
00045 }
00046 
00047 
00051 void ConvergenceMaster::BuildMasks(const DataFT& data)
00052 {
00053     TracerSA trace("ConvergenceMaster::BuildMasks(const DataFT& )");
00054     
00055     ViewMask_t uMask = data.GetUHitUse();
00056     ViewMaskList_t uMaskList;
00057     BuildViewMasks(uMask, uMaskList);    
00058 
00059     ViewMask_t vMask = data.GetVHitUse();
00060     ViewMaskList_t vMaskList;
00061     BuildViewMasks(vMask, vMaskList);    
00062 
00063     // make uMaskList, vMaskList equal size
00064     // by copying the last element of the shorter
00065     // list n times
00066     PadMaskLists(uMaskList, vMaskList);
00067     
00068     // make MaskStep objects - each MaskStep holds
00069     // masks for U and V and pointers to current
00070     // # of planes fit, max, min #planes, etc   
00071     BuildMaskSteps(uMaskList, vMaskList);
00072     
00073     SetInitialMask();
00074     
00075     PrintMasks();    
00076     return;
00077 }
00078 
00079 
00084 void ConvergenceMaster::PadMaskLists( ViewMaskList_t& uMaskList, 
00085                                       ViewMaskList_t& vMaskList  )
00086 {
00087     TracerSA trace("ConvergenceMaster::PadMaskLists(ViewMaskList_t&, ViewMaskList_t&)");
00088     UInt_t sizeU = uMaskList.size();
00089     UInt_t sizeV = vMaskList.size();
00090     
00091     // if size equal - nothing to do, return
00092     if ( sizeU == sizeV ) return;
00093 
00094     // pad one of the lists    
00095     if ( sizeU < sizeV ) {
00096         PadWithLastElement(uMaskList, sizeV-sizeU);
00097     } else {
00098         PadWithLastElement(vMaskList, sizeU-sizeV);
00099     }
00100     return;
00101 }
00102 
00103 
00107 void ConvergenceMaster::PadWithLastElement(ViewMaskList_t& maskList, UInt_t n)
00108 {
00109     TracerSA trace("ConvergenceMaster::PadWithLastElement(ViewMaskList_t&, UInt_t)");
00110     ViewMask_t last = *maskList.rbegin();
00111     
00112     for ( UInt_t i = 0; i < n; ++i ) {
00113         maskList.push_back(last);
00114     }
00115     
00116     return;
00117 } 
00118 
00119 
00124 void ConvergenceMaster::SetInitialMask() 
00125 {
00126     TracerSA trace("ConvergenceMaster::SetInitialMask()");
00127     fMaskCur = fMaskList.begin();
00128     fMaskIsValid = kTRUE;
00129 }
00130 
00131 
00135 void ConvergenceMaster::BuildMaskSteps( const ViewMaskList_t& uMaskList, 
00136                                         const ViewMaskList_t& vMaskList )   
00137 {
00138     TracerSA trace("ConvergenceMaster::BuildMaskSteps(const ViewMaskList_t&, const ViewMaskList_t&)");
00139 
00140     assert( uMaskList.size() == vMaskList.size() && 
00141             "U, V mask lists are not equal size!"    );
00142             
00143     ViewMaskListCItr itrU = uMaskList.begin();
00144     ViewMaskListCItr endU = uMaskList.end();
00145     ViewMaskListCItr itrV = vMaskList.begin();
00146     
00147     while ( itrU != endU ) {
00148         fMaskList.push_back(MaskStep( *itrU, *itrV, fNHitsInViewMin ) );
00149         ++itrU;
00150         ++itrV;
00151     }
00152     
00153     return;
00154 }    
00155 
00156 
00161 void ConvergenceMaster::BuildViewMasks(  const ViewMask_t&     maskIn, 
00162                                             ViewMaskList_t&   maskList   )   
00163 {
00164     TracerSA trace("ConvergenceMaster::BuildViewMasks(const ViewMask_t&, ViewMaskList_t&)");
00165     ViewMask_t mask(maskIn);
00166     
00167     maskList.push_front(mask);
00168     
00169     while ( UnsetEverySecond(mask) ) {
00170         maskList.push_front(mask);
00171     }
00172     
00173     return;
00174 }    
00175 
00176 
00180 Bool_t ConvergenceMaster::UnsetEverySecond( ViewMask_t& mask ) 
00181 { 
00182     TracerSA trace("ConvergenceMaster::UnsetEverySecond(vector<Int_t>&)");
00183     ViewMaskItr begin   = mask.begin();
00184     ViewMaskItr end     = mask.end();
00185     
00186     // if total number of hits is less than 7 - stop
00187     Int_t nhits = accumulate(begin, end, 0);
00188     MSG("FitTrackSA", Msg::kDebug) << "#hits = " << nhits << "\n";
00189     if ( nhits < (2*fNHitsInViewMin-1) ) return kFALSE;
00190     
00191     ViewMaskItr it = begin;
00192     while ( (it = find(it, end, 1)) != end ) {
00193         // skip this occurance of 1
00194         MSG("FitTrackSA", Msg::kDebug) << "found hit at " << it-begin << ", skipping\n";
00195         ++it;
00196         if ( it == end ) break;
00197         
00198         // find next 1, set it to 0
00199         it = find(it, end, 1);
00200         if ( it == end ) break;        
00201         MSG("FitTrackSA", Msg::kDebug) << "found hit at " << it-begin 
00202             << ", setting to 0\n";
00203         *it = 0;
00204         ++it;
00205         if ( it == end ) break;
00206     }
00207     return kTRUE;
00208 }    
00209 
00210 
00214 const ConvergenceMaster::ViewMask_t& ConvergenceMaster::GetMaskUCur() const
00215 {
00216     TracerSA trace("ConvergenceMaster::GetMaskUCur()");
00217     return fMaskCur->GetMaskU();
00218 }
00219 
00220 
00224 const ConvergenceMaster::ViewMask_t& ConvergenceMaster::GetMaskVCur() const
00225 {
00226     TracerSA trace("ConvergenceMaster::GetMaskVCur()");
00227     return fMaskCur->GetMaskV();
00228 }
00229 
00230 
00234 void ConvergenceMaster::PrintMasks() const
00235 {
00236     TracerSA trace("ConvergenceMaster::PrintMasks()");
00237 
00238     MaskListCItr beg = fMaskList.begin();
00239     MaskListCItr end = fMaskList.end();
00240       
00241     MsgStream *mftsa = &MSGSTREAM("FitTrackSA", Msg::kDebug);
00242     (*mftsa) << "\nmasks:\n";
00243 
00244     for (MaskListCItr it = beg; it!=end; ++it) {
00245         (*mftsa) << "Level " << it-beg << "\n";
00246         it->PrintMasks();
00247         (*mftsa) << "\n";       
00248     }
00249     
00250     return;
00251 }
00252 
00253 
00254 
00259 Bool_t ConvergenceMaster::NextStep()
00260 {
00261     TracerSA trace("ConvergenceMaster::NextStep()");
00262     // possibilities
00263     // - cur step converged and not at max and prev < cur -> increment
00264     // - cur step converged and ( at max or prev > cur ) -> go to next mask
00265     // - cur step diverged and not at min and prev > cur -> decrement
00266     // - cur step diverged and ( at min or prev < cur ) -> go to next mask
00267     
00268     fMaskCur->Print();
00269         
00270     if (    fMaskCur->GetConvergedCur()     && 
00271             fMaskCur->GetPlaneCountCur() < fMaskCur->GetPlaneCountMax() && 
00272             fMaskCur->GetPlaneCountCur() >= fMaskCur->GetPlaneCountLast()   ) {
00273             
00274         return IncrementPlaneCount();
00275     
00276     } else if ( fMaskCur->GetConvergedCur()        && 
00277                 (fMaskCur->GetPlaneCountCur() == fMaskCur->GetPlaneCountMax()||  
00278                  fMaskCur->GetPlaneCountCur() <=  fMaskCur->GetPlaneCountLast()) 
00279                                                                             ) {
00280         
00281         return NextMask(fMaskCur->GetPlaneCountCur());
00282     
00283     } else if ( ! fMaskCur->GetConvergedCur() && 
00284                 fMaskCur->GetPlaneCountCur() > fMaskCur->GetPlaneCountMin()&&
00285                 fMaskCur->GetPlaneCountCur() <= fMaskCur->GetPlaneCountLast()
00286                                                                              ){
00287         
00288         return DecrementPlaneCount();
00289     
00290     } else if ( ! fMaskCur->GetConvergedCur() && 
00291                 ( fMaskCur->GetPlaneCountCur() == fMaskCur->GetPlaneCountMin() ||  
00292                   fMaskCur->GetPlaneCountCur() >=fMaskCur->GetPlaneCountLast())
00293                                                                             ){
00294         return NextMask(fMaskCur->GetPlaneCountCur());
00295     }
00296     
00297     assert ( ! "Should not get here!");
00298 
00299     return kTRUE;
00300 }
00301 
00302 
00306 Bool_t ConvergenceMaster::IncrementPlaneCount()
00307 {
00308     TracerSA trace("ConvergenceMaster::IncrementPlaneCount()");
00309     return fMaskCur->IncrementPlaneCount();            
00310 }
00311 
00312 
00316 Bool_t ConvergenceMaster::DecrementPlaneCount()
00317 {
00318     TracerSA trace("ConvergenceMaster::DecrementPlaneCount()");
00319     return fMaskCur->DecrementPlaneCount();            
00320 }
00321 
00322 
00326 Bool_t ConvergenceMaster::NextMask(Count_t planeCount)
00327 {
00328     TracerSA trace("ConvergenceMaster::NextMask()");
00329     ++fMaskCur;
00330     
00331     if ( fMaskCur == fMaskList.end() ) return kFALSE;
00332     
00333     fMaskCur->SetPlaneCountCur(planeCount);            
00334     fMaskCur->SetPlaneCountLast(planeCount);            
00335     
00336     fMaskIsValid = kFALSE;
00337     return kTRUE;
00338 }
00339 
00340 
00344 ConvergenceMaster::Count_t ConvergenceMaster::GetNPlanesCur() const
00345 {
00346     TracerSA trace("ConvergenceMaster::GetNPlanesCur()");
00347     return fMaskCur->GetPlaneCountCur();
00348 } 
00349    
00350 
00354 ConvergenceMaster::Count_t ConvergenceMaster::GetNPlanesMin() const
00355 {
00356     TracerSA trace("ConvergenceMaster::GetNPlanesMin()");
00357     return fMaskCur->GetPlaneCountMin();
00358 }
00359 
00360 
00364 ConvergenceMaster::Count_t ConvergenceMaster::GetNPlanesMax() const
00365 {
00366     TracerSA trace("ConvergenceMaster::GetNPlanesMax()");
00367     return fMaskCur->GetPlaneCountMax();
00368 }
00369 
00370 
00374 ConvergenceMaster::MaskStep::MaskStep(  const ViewMask_t& umask, 
00375                                         const ViewMask_t& vmask,
00376                                         Int_t nhitsinviewmin     ) :
00377     fMaskU(umask), fMaskV(vmask)
00378 {
00379     TracerSA trace("ConvergenceMaster::MaskStep::MaskStep(...,Int_t)");
00380     // make sum of U and V masks
00381     fMaskUVSum.resize(fMaskU.size());
00382     for (UInt_t i = 0; i < fMaskUVSum.size(); ++i) {
00383         fMaskUVSum[i] = fMaskU[i] + fMaskV[i];
00384     }
00385     
00386     Count_t uMin = FindPlaneCountMin(umask, nhitsinviewmin);
00387     Count_t vMin = FindPlaneCountMin(vmask, nhitsinviewmin);    
00388     fPlaneCountMin = max(uMin, vMin);
00389     
00390     Count_t uMax = FindPlaneCountMax(umask);
00391     Count_t vMax = FindPlaneCountMax(vmask);
00392     fPlaneCountMax = max(uMax, vMax);
00393     
00394     fPlaneCountCur  = fPlaneCountMax;
00395     fConvergedCur   = kTRUE;
00396     fPlaneCountLast = fPlaneCountCur;
00397 }
00398 
00399 
00403 ConvergenceMaster::MaskStep::~MaskStep()
00404 {}
00405 
00406 
00410 ConvergenceMaster::Count_t 
00411 ConvergenceMaster::MaskStep::FindPlaneCountMin( const ViewMask_t& mask, 
00412                                                       Count_t nhitsmin  )
00413 {
00414     TracerSA trace("ConvergenceMaster::MaskStep::FindPlaneCountMin(ViewMask_t&, Count_t)");
00415     ViewMaskCItr beg = mask.begin();
00416     ViewMaskCItr end = mask.end();
00417     ViewMaskCItr it = beg;
00418     
00419     for ( Count_t i = 0; i < nhitsmin; ++i) {
00420         it = find(it, end, 1);
00421         ++it;
00422     }
00423     
00424     return it - beg;
00425 }
00426 
00427 
00431 ConvergenceMaster::Count_t 
00432 ConvergenceMaster::MaskStep::FindPlaneCountMax( const ViewMask_t& mask )
00433 {
00434     TracerSA trace("ConvergenceMaster::MaskStep::FindPlaneCountMax(ViewMask_t&)");
00435     ViewMaskCRItr beg = mask.rbegin();
00436     ViewMaskCRItr end = mask.rend();
00437     
00438     ViewMaskCRItr it  = find(beg, end, 1);
00439     Count_t size = mask.size();    
00440     
00441     return (size - (it - beg));
00442 }
00443 
00444 
00448 void ConvergenceMaster::MaskStep::Print() const
00449 {
00450     TracerSA trace("ConvergenceMaster::MaskStep::Print()");
00451 
00452     MsgStream *mftsa = &MSGSTREAM("FitTrackSA", Msg::kDebug);
00453     (*mftsa) << "min = " << fPlaneCountMin 
00454              << "; max = " << fPlaneCountMax 
00455              << "; cur = " << fPlaneCountCur 
00456              << "; last = " << fPlaneCountLast 
00457              << "; conv = " << fConvergedCur << "\n";
00458 }
00459 
00460 
00464 void ConvergenceMaster::MaskStep::PrintMasks() const
00465 {
00466     TracerSA trace("ConvergenceMaster::MaskStep::PrintMask()");
00467 
00468     MsgStream *mftsa = &MSGSTREAM("FitTrackSA", Msg::kDebug);
00469     (*mftsa) << "min = " << fPlaneCountMin 
00470              << "; max = " << fPlaneCountMax << "\n";
00471 
00472     (*mftsa) << "U: ";
00473     ViewMaskCItr beg = fMaskU.begin();
00474     ViewMaskCItr end = fMaskU.end();      
00475     for (ViewMaskCItr it = beg; it!=end; ++it) {
00476         (*mftsa) << *it << " ";
00477     }
00478     (*mftsa) << "\n";       
00479     
00480     (*mftsa) << "V: ";
00481     beg = fMaskV.begin();
00482     end = fMaskV.end();      
00483     for (ViewMaskCItr it = beg; it!=end; ++it) {
00484         (*mftsa) << *it << " ";
00485     }
00486     (*mftsa) << "\n";       
00487     
00488     return;
00489 }
00490 
00491 
00495 Bool_t ConvergenceMaster::MaskStep::IncrementPlaneCount()
00496 {
00497     TracerSA trace("ConvergenceMaster::MaskStep::IncrementPlaneCount()");
00498     assert( fPlaneCountCur < fPlaneCountMax && 
00499             "Can't increment plane count, already at max!" );
00500 
00501     Print();
00502         
00503     ViewMaskCItr beg = fMaskUVSum.begin();
00504     ViewMaskCItr end = fMaskUVSum.end();
00505     
00506     ViewMaskCItr it = beg + fPlaneCountCur;
00507     
00508     it = find(it, end, 1);
00509     ++it;
00510     
00511     fPlaneCountLast = fPlaneCountCur;
00512     fPlaneCountCur = it - beg;
00513     
00514     Print();
00515         
00516     return kTRUE;
00517 }
00518 
00519 
00523 Bool_t ConvergenceMaster::MaskStep::DecrementPlaneCount()
00524 {
00525     TracerSA trace("ConvergenceMaster::MaskStep::DecrementPlaneCount()");
00526     assert( fPlaneCountCur > fPlaneCountMin && 
00527             "Can't decrement plane count, already at min!" );
00528     
00529     Print();
00530         
00531     ViewMaskCRItr beg = fMaskUVSum.rbegin();
00532     ViewMaskCRItr end = fMaskUVSum.rend();
00533     
00534     ViewMaskCRItr it = end - fPlaneCountCur + 1;
00535     
00536     it = find(it, end, 1);
00537     
00538     fPlaneCountLast = fPlaneCountCur;
00539     fPlaneCountCur = end - it;
00540     
00541     Print();
00542         
00543     return kTRUE;
00544 }
00545 
00546 

Generated on Mon Feb 15 11:06:33 2010 for loon by  doxygen 1.3.9.1