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

StraightTrackAlignment.cxx

Go to the documentation of this file.
00001 #include "Alignment/StraightTrackAlignment.h"
00002 #include "Alignment/ScintModule.h"
00003 #include "Alignment/AlignHists.h"
00004 
00005 #include "RecoBase/CandStripHandle.h"
00006 #include "RecoBase/CandTrackHandle.h"
00007 #include "UgliGeometry/UgliScintPlnHandle.h"
00008 #include "Navigation/NavSet.h"
00009 
00010 #include <fstream>
00011 #include <vector>
00012 #include <cassert>
00013 using std::vector;
00014 using std::list;
00015 
00016 using std::cerr;
00017 using std::cout;
00018 using std::endl;
00019 
00020 StraightTrackAlignment::StraightTrackAlignment()
00021     : fHister(0)
00022       ,fMinPlanes(14)            // hard code for now.
00023       ,fHistName(0)
00024 {
00025 }
00026 
00027 StraightTrackAlignment::~StraightTrackAlignment()
00028 {
00029     if (fHister) delete fHister;
00030 }
00031 
00032 // Create the histogrammer
00033 void StraightTrackAlignment::Init(UgliGeomHandle& ugh, const char* histname)
00034 {
00035     fHistName = histname;
00036     vector<UgliScintPlnHandle> planevec = ugh.GetScintPlnHandleVector();
00037     fNplanes = planevec.size();
00038     fNmdlperplane = planevec[0].GetScintMdlHandleVector().size();
00039 }
00040 
00041 // return the ScintModule containing the given USH.
00042 ScintModule* StraightTrackAlignment::LookupScintModule(UgliStripHandle ush)
00043 {
00044     UgliScintMdlHandle usmh = ush.GetScintMdlHandle();
00045     PlexScintMdlId smid = usmh.GetPlexScintMdlId();
00046 
00047     ScintModuleLookup::iterator it = fSMLookup.find(smid);
00048     if (it == fSMLookup.end()) {  // D.N.E.
00049         ScintModule* smod = new ScintModule(usmh);
00050         assert(smod);
00051         fSMLookup[smid] = smod;
00052         return smod;
00053     }
00054 
00055     assert(it->second);
00056     return it->second;
00057 }
00058 
00059 // Save any tracks
00060 void StraightTrackAlignment::AddTrack (const CandTrackHandle& track_handle)
00061 {
00062     int nhits = track_handle.GetNDaughters();
00063     if (nhits < fMinPlanes) return;
00064 
00065 //    cerr << nhits << " hits\n";
00066 
00067     UgliGeomHandle ugh(*(track_handle.GetVldContext()));
00068     CandStripHandleItr sitr(track_handle.GetDaughterIterator());
00069     
00070     StraightTrack st[2];
00071     CandStripHandle* csh = 0;
00072     while ((csh = sitr())) {
00073         cerr << ".";
00074         PlexStripEndId seid = csh->GetStripEndId();
00075         int view = 0;
00076         if (seid.GetPlaneView() != PlaneView::kV) view = 1;
00077         st[view].push_back(ugh.GetStripHandle(seid));
00078     }
00079     cerr << endl;
00080 
00081     if (st[0].size()) {
00082         fTracks[0].push_back(st[0]);
00083     }
00084     if (st[1].size()) {
00085         fTracks[1].push_back(st[1]);
00086     }
00087 }
00088 
00089 
00090 // Fits a linear track to the StraighTrack's hits between "lit" and
00091 // "end" while skipping the hit "skip".  The predicted transverse
00092 // position of "skip" is fed to skip's ScintModule.
00093 double
00094 StraightTrackAlignment::FitTrackLessOne(StraightTrack::iterator lit,
00095                                         StraightTrack::iterator end,
00096                                         StraightTrack::iterator skip)
00097 {
00098     // Linear regression parameters.  See for eg. Num Rec Ch 15.
00099     // x is Z, y is TPos (U or V).
00100     double S, Sx, Sy, Sxx, Sxy;
00101     S=Sx=Sy=Sxx=Sxy=0.0;
00102 
00103     int count = 0;
00104 
00105     for (;lit != end; ++lit) {
00106         if (lit == skip) continue;
00107 
00108         UgliStripHandle ush = (*lit);
00109         ScintModule* smod = this->LookupScintModule(ush);
00110 
00111         float x = ush.GlobalPos(0)(2);
00112         float y = ush.GetTPos() + smod->GetOffset();
00113         
00114         Sx += x;
00115         Sy += y;
00116         Sxx += x*x;
00117         Sxy += x*y;        
00118 
00119         ++count;
00120     }
00121     S = count;
00122 
00123     // Note, since I assume a constant uncertainty in the transverse
00124     // position measurement, it drops out of hte final result.
00125 
00126     // The linear fit: y = a + bx
00127     double delta = S*Sxx - Sx*Sx;
00128     double a = (Sxx*Sy - Sx*Sxy)/delta;
00129     double b = (S*Sxy - Sx*Sy)/delta;
00130 
00131     double predict = a + b*(*skip).GlobalPos(0)(2);
00132     predict -= (*skip).GetTPos();
00133     ScintModule* smod = this->LookupScintModule(*skip);
00134     if (smod) smod->AccumResid(predict);
00135     else {
00136         cerr << "Failed to lookup scint module!\n";
00137     }
00138     return predict;
00139 }
00140 
00141 // Fits track once for each digit.
00142 double StraightTrackAlignment::ApplyTrack(StraightTrack st, 
00143                                           PlaneView::PlaneView_t view)
00144 {
00145     double tot = 0;
00146     StraightTrack::iterator lit, first=st.begin(), end = st.end();
00147 
00148     vector<int> planev, mdlv, stripv;
00149     vector<double> residv;
00150 
00151     int count = 0;
00152     for (lit = first; lit != end; ++lit) {
00153 //        cerr << ".";
00154         double resid = this->FitTrackLessOne(first,end,lit);
00155         if (fHister) {
00156             UgliScintMdlHandle usmh = (*lit).GetScintMdlHandle();
00157             PlexPlaneId ppid = usmh.GetPlexPlaneId();
00158             residv.push_back(resid);
00159             planev.push_back(ppid.GetPlane());
00160             stripv.push_back((*lit).GetSEId().GetStrip());
00161             mdlv.push_back(usmh.GetModuleNum());
00162         }
00163         tot += resid;
00164         ++count;
00165     }
00166     if (fHister) fHister->ApplyTrack(view,stripv,planev,mdlv,residv);
00167     
00168     return count ? tot/count : 0;
00169 }
00170 
00171 
00172 double StraightTrackAlignment::ApplyAllTracks(PlaneView::PlaneView_t the_view)
00173 {
00174     int view = 0;
00175     if (the_view == PlaneView::kV) view = 1;
00176     list<StraightTrack>::iterator lit, end = fTracks[view].end();
00177 
00178     cerr << "Applying " << fTracks[view].size() << " tracks in view " 
00179          << view << endl;
00180 
00181     double tot = 0;
00182     int count = 0;
00183     for (lit=fTracks[view].begin(); lit != end; ++lit) {
00184         tot += this->ApplyTrack(*lit,the_view);
00185         ++ count;
00186     }
00187     cerr << "Done applying tracks\n";
00188     return count ? tot/count : 0;
00189 }
00190 double StraightTrackAlignment::ApplyAllTracks()
00191 {
00192     static bool been_here = false;
00193     if (! been_here) {
00194         fHister = new AlignHists(fNplanes,fNmdlperplane,fHistName);
00195         been_here = true;
00196     }
00197                                  
00198 
00199     double ret = 0;
00200     ret += this->ApplyAllTracks(PlaneView::kU);
00201     ret += this->ApplyAllTracks(PlaneView::kV);
00202     return ret/2.0;
00203 }
00204 
00205 void StraightTrackAlignment::ApplyAllOffsets(void)
00206 {
00207     ScintModuleLookup::iterator mit, end = fSMLookup.end();
00208     for (mit = fSMLookup.begin(); mit != end; ++mit) {
00209         ScintModule* sm = mit->second;
00210         if (!sm) {
00211             cerr << "Bad ScintModule in lookup\n";
00212             continue;
00213         }
00214         sm->UpdateOffsets();
00215         sm->ClearStatistics();
00216     }
00217     if (fHister) fHister->IncrementIteration();
00218 }
00219 

Generated on Mon Feb 15 11:07:40 2010 for loon by  doxygen 1.3.9.1