CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Alignment/MuonAlignmentAlgorithms/plugins/MuonMillepedeAlgorithm.cc

Go to the documentation of this file.
00001         #include <fstream>
00002 
00003         #include "TFile.h"
00004         #include "TTree.h"
00005         #include "TKey.h"
00006 
00007         #include "FWCore/MessageLogger/interface/MessageLogger.h"
00008         #include "FWCore/ParameterSet/interface/ParameterSet.h"
00009         #include "FWCore/Framework/interface/MakerMacros.h"
00010         #include "FWCore/Framework/interface/Frameworkfwd.h"
00011 
00012 
00013         #include "TrackingTools/PatternTools/interface/Trajectory.h"
00014         #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00015 
00016         #include "Alignment/CommonAlignment/interface/Alignable.h"  
00017         #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"  
00018         #include "Alignment/CommonAlignment/interface/AlignableObjectId.h"  
00019         #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00020         #include "Alignment/CommonAlignment/interface/SurveyResidual.h"
00021         #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
00022         #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
00023         #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentIORoot.h"
00024         #include "Alignment/MuonAlignment/interface/AlignableMuon.h"
00025         #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00026         #include <DataFormats/GeometrySurface/interface/LocalError.h> 
00027         #include "DataFormats/TrackReco/interface/Track.h"
00028 
00029         #include "Alignment/MuonAlignmentAlgorithms/plugins/MuonMillepedeAlgorithm.h"
00030 
00031         #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmPluginFactory.h"
00032          
00033 
00034         // Constructor ----------------------------------------------------------------
00035 
00036         MuonMillepedeAlgorithm::MuonMillepedeAlgorithm(const edm::ParameterSet& cfg):
00037           AlignmentAlgorithmBase( cfg )
00038         {
00039 
00040           // parse parameters
00041 
00042           edm::LogWarning("Alignment") << "[MuonMillepedeAlgorithm] constructed.";
00043 
00044 
00045           collec_f = cfg.getParameter<std::string>( "CollectionFile" );
00046 
00047           isCollectionJob = cfg.getParameter<bool>( "isCollectionJob" );
00048          
00049           collec_path = cfg.getParameter<std::string>( "collectionPath" );
00050           
00051           collec_number = cfg.getParameter<int>( "collectionNumber" );
00052             
00053           outputCollName =  cfg.getParameter<std::string>( "outputCollName" );
00054          
00055           ptCut = cfg.getParameter<double>( "ptCut" );
00056           
00057           chi2nCut = cfg.getParameter<double>( "chi2nCut" );
00058 
00059 
00060         }
00061 
00062         // Call at beginning of job ---------------------------------------------------
00063 
00064         void 
00065         MuonMillepedeAlgorithm::initialize( const edm::EventSetup& setup, 
00066                                             AlignableTracker* tracker, AlignableMuon* muon, 
00067                                             AlignableExtras* extras,
00068                                             AlignmentParameterStore* store )
00069         {
00070           
00071           edm::LogWarning("Alignment") << "[MuonMillepedeAlgorithm] Initializing...";
00072 
00073           // accessor Det->AlignableDet
00074           theAlignableDetAccessor = new AlignableNavigator(tracker, muon);
00075 
00076           // set alignmentParameterStore
00077           theAlignmentParameterStore=store;
00078 
00079           // get alignables
00080           theAlignables = theAlignmentParameterStore->alignables();
00081 
00082         }
00083 
00084         void MuonMillepedeAlgorithm::collect()
00085         {
00086 
00087           std::map<std::string, TMatrixD *> map;
00088 
00089           for(int c_job = 0; c_job < collec_number; ++c_job)
00090           {
00091             char name_f[40];
00092             snprintf(name_f, sizeof(name_f), "%s_%d/%s", collec_path.c_str(), c_job, collec_f.c_str());
00093             TFile file_it(name_f);
00094 
00095             if(file_it.IsZombie())
00096               continue;
00097 
00098             TList *m_list = file_it.GetListOfKeys();
00099             if(m_list == 0) {
00100               return;
00101             }
00102             TKey *index = (TKey *)m_list->First();
00103             if(index == 0) {
00104             }
00105             if( index != 0 )
00106             {
00107               do
00108               {
00109 
00110                 std::string objectName(index->GetName());
00111                 TMatrixD *mat = (TMatrixD *)index->ReadObj();
00112                 std::map<std::string, TMatrixD *>::iterator node = map.find(objectName);
00113                 if(node == map.end())
00114                 {
00115                   TMatrixD *n_mat = new TMatrixD(mat->GetNrows(), mat->GetNcols());
00116                   map.insert(make_pair(objectName, n_mat));
00117                 }
00118                 *(map[objectName]) += *mat;
00119                 index = (TKey*)m_list->After(index);
00120               } while(index != 0);
00121             }
00122             file_it.Close();    
00123           }
00124           
00125 
00126           TFile theFile2(outputCollName.c_str(), "recreate");
00127           theFile2.cd();
00128 
00129           std::map<std::string, TMatrixD *>::iterator m_it = map.begin();
00130           for(; m_it != map.end(); ++m_it)
00131           {  
00132             if(m_it->first.find("_invCov") != std::string::npos)
00133             {
00134                std::string id_s = m_it->first.substr(0, m_it->first.find("_invCov"));
00135                std::string id_w = id_s + "_weightRes";
00136                std::string id_n = id_s + "_N";
00137                std::string cov = id_s + "_cov";
00138                std::string sol = id_s + "_sol";
00139 
00140                //Covariance calculation 
00141                TMatrixD invMat( m_it->second->GetNrows(), m_it->second->GetNcols()); 
00142                invMat = *(m_it->second);
00143                invMat.Invert();
00144                //weighted residuals
00145                TMatrixD weightMat( m_it->second->GetNcols(), 1);
00146                weightMat = *(map[id_w]);
00147                //Solution of the linear system
00148                TMatrixD solution( m_it->second->GetNrows(), 1);
00149                solution = invMat * weightMat;
00150                //Number of Tracks
00151                TMatrixD n(1,1);
00152                n = *(map[id_n]);     
00153                
00154                invMat.Write(cov.c_str());
00155                n.Write(id_n.c_str());
00156                solution.Write(sol.c_str());
00157             }
00158           }
00159           theFile2.Write();
00160           theFile2.Close();
00161         }
00162 
00163 
00164         // Call at end of job ---------------------------------------------------------
00165 
00166         void MuonMillepedeAlgorithm::terminate(void)
00167         {
00168 
00169           if( isCollectionJob )
00170           {
00171              this->collect();
00172              return;
00173           }
00174 
00175 
00176           edm::LogWarning("Alignment") << "[MuonMillepedeAlgorithm] Terminating";
00177 
00178           // iterate over alignment parameters
00179           for(std::vector<Alignable*>::const_iterator
00180             it=theAlignables.begin(); it!=theAlignables.end(); it++) {
00181             Alignable* ali=(*it);
00182             // Alignment parameters
00183             // AlignmentParameters* par = ali->alignmentParameters();
00184             edm::LogInfo("Alignment") << "now apply params";
00185             theAlignmentParameterStore->applyParameters(ali);
00186             // set these parameters 'valid'
00187             ali->alignmentParameters()->setValid(true);
00188 
00189           }
00190 
00191           edm::LogWarning("Alignment") << "[MuonMillepedeAlgorithm] Writing aligned parameters to file: " << theAlignables.size();
00192 
00193           TFile *theFile = new TFile(collec_f.c_str(), "recreate");
00194           theFile->cd();
00195           std::map<std::string, AlgebraicMatrix *>::iterator invCov_it = map_invCov.begin();
00196           std::map<std::string, AlgebraicMatrix *>::iterator weightRes_it = map_weightRes.begin();
00197           std::map<std::string, AlgebraicMatrix *>::iterator n_it = map_N.begin();
00198           for(; n_it != map_N.end(); ++invCov_it, ++weightRes_it, ++n_it)
00199           {
00200             TMatrixD tmat_invcov(0,0);
00201             this->toTMat(invCov_it->second, &tmat_invcov);
00202             TMatrixD tmat_weightres(0,0);
00203             this->toTMat(weightRes_it->second, &tmat_weightres);
00204             TMatrixD tmat_n(0,0);
00205             this->toTMat(n_it->second, &tmat_n);
00206             
00207             tmat_invcov.Write(invCov_it->first.c_str());
00208             tmat_weightres.Write(weightRes_it->first.c_str());
00209             tmat_n.Write(n_it->first.c_str());
00210           }
00211 
00212           theFile->Write();
00213           theFile->Close();
00214 
00215         }
00216 
00217 
00218         void MuonMillepedeAlgorithm::toTMat(AlgebraicMatrix *am_mat, TMatrixD *tmat_mat)
00219         {
00220           tmat_mat->ResizeTo(am_mat->num_row(), am_mat->num_col());
00221           for(int c_i = 0; c_i < am_mat->num_row(); ++c_i) {
00222             for(int c_j = 0; c_j < am_mat->num_col(); ++c_j) {
00223               (*tmat_mat)(c_i, c_j) = (*am_mat)[c_i][c_j];
00224             }
00225           }
00226         }
00227 
00228 
00229 
00230         // Run the algorithm on trajectories and tracks -------------------------------
00231 
00232         void MuonMillepedeAlgorithm::run(const edm::EventSetup& setup, const EventInfo &eventInfo)
00233         {
00234           
00235           if( isCollectionJob )
00236           {
00237             return;
00238           }
00239 
00240 
00241           // loop over tracks  
00242           //int t_counter = 0;
00243           const ConstTrajTrackPairCollection &tracks = eventInfo.trajTrackPairs_;
00244           for( ConstTrajTrackPairCollection::const_iterator it=tracks.begin();
00245                it!=tracks.end();it++) {
00246 
00247             const Trajectory* traj = (*it).first;
00248             const reco::Track* track = (*it).second;
00249 
00250             float pt    = track->pt();
00251             float eta   = track->eta();
00252             float phi   = track->phi();
00253             float chi2n = track->normalizedChi2();
00254             //int   ndof = track->ndof();
00255             int   nhit  = track->numberOfValidHits();
00256 
00257             if (0) edm::LogInfo("Alignment") << "New track pt,eta,phi,chi2n,hits: " << pt <<","<< eta <<","<< phi <<","<< chi2n << ","<<nhit;
00258 
00259             //Accept or not accept the track
00260             if( pt > ptCut && chi2n < chi2nCut ) 
00261             {
00262 
00263 
00264               std::vector<const TransientTrackingRecHit*> hitvec;
00265               std::vector<TrajectoryStateOnSurface> tsosvec;
00266               
00267               std::vector<TrajectoryMeasurement> measurements = traj->measurements();
00268              
00269               //In this loop the measurements and hits are extracted and put on two vectors 
00270               for (std::vector<TrajectoryMeasurement>::iterator im=measurements.begin();
00271                          im!=measurements.end(); im++)
00272               {
00273                 TrajectoryMeasurement meas = *im;
00274                 const TransientTrackingRecHit* hit = &(*meas.recHit());
00275                 //We are not very strict at this point
00276                 if (hit->isValid()  && theAlignableDetAccessor->detAndSubdetInMap( hit->geographicalId() ))
00277                 {
00278                   //***Forward
00279                   TrajectoryStateOnSurface tsos = meas.forwardPredictedState();
00280                   if (tsos.isValid())
00281                   {
00282                     hitvec.push_back(hit);
00283                     tsosvec.push_back(tsos);
00284                   }
00285                 }
00286               }
00287             
00288               // transform RecHit vector to AlignableDet vector
00289               std::vector <AlignableDetOrUnitPtr> alidetvec = 
00290                 theAlignableDetAccessor->alignablesFromHits(hitvec);
00291 
00292               // get concatenated alignment parameters for list of alignables
00293               CompositeAlignmentParameters aap = 
00294                 theAlignmentParameterStore->selectParameters(alidetvec);
00295 
00296               std::vector<TrajectoryStateOnSurface>::const_iterator itsos=tsosvec.begin();
00297               std::vector<const TransientTrackingRecHit*>::const_iterator ihit=hitvec.begin();
00298 
00299               
00300               //int ch_counter = 0;
00301 
00302               while (itsos != tsosvec.end()) 
00303               {
00304                 // get AlignableDet for this hit
00305                 const GeomDet* det=(*ihit)->det();
00306                 AlignableDetOrUnitPtr alidet = 
00307                   theAlignableDetAccessor->alignableFromGeomDet(det);
00308               
00309                 // get relevant Alignable
00310                 Alignable* ali=aap.alignableFromAlignableDet(alidet);
00311              
00312                 //To be sure that the ali is not null and that it's a DT segment 
00313                 if ( ali!=0 && (*ihit)->geographicalId().subdetId() == 1)
00314                 {
00315                   DTChamberId m_Chamber(det->geographicalId());
00316                   //Station 4 does not contain Theta SL 
00317                   if((*ihit)->dimension() == 4 || ((*ihit)->dimension() == 2 && m_Chamber.station() == 4))
00318                   //if((*ihit)->dimension() == 4)
00319                   {
00320                     edm::LogInfo("Alignment") << "Entrando";
00321                   
00322                     AlignmentParameters* params = ali->alignmentParameters();
00323                   
00324                     edm::LogInfo("Alignment") << "Entrando";
00325                     //(dx/dz,dy/dz,x,y) for a 4DSegment
00326                     AlgebraicVector ihit4D = (*ihit)->parameters();
00327 
00328                     //The innerMostState always contains the Z
00329                     //(q/pt,dx/dz,dy/dz,x,y)
00330                     AlgebraicVector5 alivec = (*itsos).localParameters().mixedFormatVector();
00331                     
00332                     //The covariance matrix follows the sequence
00333                     //(q/pt,dx/dz,dy/dz,x,y) but we reorder to
00334                     //(x,y,dx/dz,dy/dz)
00335                     AlgebraicSymMatrix55 rawCovMat = (*itsos).localError().matrix();
00336                     AlgebraicMatrix CovMat(4,4);
00337                     int m_index[] = {2,3,0,1};
00338                     for(int c_ei = 0; c_ei < 4; ++c_ei)
00339                     {
00340                       for(int c_ej = 0; c_ej < 4; ++c_ej)
00341                       {
00342                         CovMat[m_index[c_ei]][m_index[c_ej]] = rawCovMat(c_ei+1,c_ej+1);
00343                       }
00344                     }
00345                     
00346                     int inv_check;
00347                     //printM(CovMat);
00348                     CovMat.invert(inv_check);
00349                     if (inv_check != 0) { 
00350                       edm::LogError("Alignment") << "Covariance Matrix inversion failed"; 
00351                       return; 
00352                     } 
00353 
00354 
00355 
00356                     //The order is changed to:
00357                     // (x,0,dx/dz,0) MB4 Chamber
00358                     // (x,y,dx/dz,dy/dz) Not MB4 Chamber
00359                     AlgebraicMatrix residuals(4,1);
00360                     if(m_Chamber.station() == 4)
00361                     {
00362                       //Filling Residuals
00363                       residuals[0][0] = ihit4D[2]-alivec[3];
00364                       residuals[1][0] = 0.0;
00365                       residuals[2][0] = ihit4D[0]-alivec[1];
00366                       residuals[3][0] = 0.0;
00367                       //The error in the Theta coord is set to infinite
00368                       CovMat[1][0] = 0.0; CovMat[1][1] = 0.0; CovMat[1][2] = 0.0;
00369                       CovMat[1][3] = 0.0; CovMat[0][1] = 0.0; CovMat[2][1] = 0.0; 
00370                       CovMat[3][1] = 0.0; CovMat[3][0] = 0.0; CovMat[3][2] = 0.0;
00371                       CovMat[3][3] = 0.0; CovMat[0][3] = 0.0; CovMat[2][3] = 0.0; 
00372                     }
00373                     else
00374                     {
00375                       //Filling Residuals
00376                       residuals[0][0] = ihit4D[2]-alivec[3];
00377                       residuals[1][0] = ihit4D[3]-alivec[4];
00378                       residuals[2][0] = ihit4D[0]-alivec[1];
00379                       residuals[3][0] = ihit4D[1]-alivec[2];
00380                     }
00381           
00382                     // Derivatives 
00383                     AlgebraicMatrix derivsAux = params->selectedDerivatives(*itsos,alidet);
00384                     
00385                     std::vector<bool> mb4_mask;
00386                     std::vector<bool> selection;
00387                     //To be checked 
00388                     mb4_mask.push_back(true); mb4_mask.push_back(false); mb4_mask.push_back(true); 
00389                     mb4_mask.push_back(true); mb4_mask.push_back(true); mb4_mask.push_back(false); 
00390                     selection.push_back(true); selection.push_back(true); selection.push_back(false); 
00391                     selection.push_back(false); selection.push_back(false); selection.push_back(false); 
00392                     int nAlignParam = 0;
00393                     if(m_Chamber.station() == 4)
00394                     {  
00395                       for(int icor = 0; icor < 6; ++icor)
00396                       {
00397                         if(mb4_mask[icor] && selection[icor]) nAlignParam++;                      
00398                       }
00399                     }
00400                     else
00401                     {
00402                       nAlignParam = derivsAux.num_row();
00403                     }
00404                     
00405                     AlgebraicMatrix derivs(nAlignParam, 4);
00406                     if(m_Chamber.station() == 4)
00407                     { 
00408                       int der_c = 0;
00409                       for(int icor = 0; icor < 6; ++icor)
00410                       {
00411                         if(mb4_mask[icor] && selection[icor])
00412                         {
00413                            for(int ccor = 0; ccor < 4; ++ccor)
00414                            {
00415                              derivs[der_c][ccor] = derivsAux[icor][ccor];
00416                              ++der_c;
00417                            }
00418                         } 
00419                       }
00420                     }
00421                     else
00422                     {
00423                       derivs = derivsAux;
00424                     }
00425          
00426 
00427                     //for(int co = 0; co < derivs.num_row(); ++co)
00428                     //{
00429                     //  for(int ci = 0; ci < derivs.num_col(); ++ci)
00430                     //  {
00431                     //     edm::LogInfo("Alignment") << "Derivatives: " << co << " " << ci << " " << derivs[co][ci] << " ";
00432                     //  }
00433                     //} 
00434           
00435                     AlgebraicMatrix derivsT = derivs.T();
00436                     AlgebraicMatrix invCov = derivs*CovMat*derivsT;
00437                     AlgebraicMatrix weightRes = derivs*CovMat*residuals;
00438 
00439                     //this->printM(derivs);
00440                     //this->printM(CovMat);
00441                     //this->printM(residuals);
00442                     
00443                     char name[40];
00444                     snprintf(name, sizeof(name),
00445                              "Chamber_%d_%d_%d", m_Chamber.wheel(), m_Chamber.station(), m_Chamber.sector());
00446                     std::string chamId(name);
00447                     //MB4 need a special treatment
00448                     /*AlgebraicMatrix invCovMB4(2,2);
00449                     AlgebraicMatrix weightResMB4(2,1); 
00450                     if( m_Chamber.station() == 4 )
00451                     { 
00452                       int m_index_2[] = {0,2};
00453                       for(int c_i = 0; c_i < 2; ++c_i)
00454                       {
00455                         weightResMB4[c_i][0] = weightRes[m_index_2[c_i]][0];
00456                         for(int c_j = 0; c_j < 2; ++c_j)
00457                         {
00458                           invCovMB4[c_i][c_j] = invCov[m_index_2[c_i]][m_index_2[c_j]];
00459                         }  
00460                       }
00461                       this->updateInfo(invCovMB4, weightResMB4, residuals, chamId); 
00462                     }
00463                     else
00464                     {
00465                       this->updateInfo(invCov, weightRes, residuals, chamId); 
00466                     }*/
00467                     this->updateInfo(invCov, weightRes, residuals, chamId); 
00468                   }
00469                 }
00470                 itsos++;
00471                 ihit++;
00472               }
00473             }
00474           } // end of track loop
00475 
00476         }
00477 
00478 
00479         //Auxiliar
00480         void MuonMillepedeAlgorithm::printM(AlgebraicMatrix m)
00481         {
00482           //for(int i = 0; i < m.num_row(); ++i)
00483           // {
00484           //  for(int j = 0; j < m.num_col(); ++j)
00485           //  {
00486           //    std::cout << m[i][j] << " ";
00487           //  }
00488           //  std::cout << std::endl;
00489           //}
00490         }
00491 
00492         void MuonMillepedeAlgorithm::updateInfo(AlgebraicMatrix m_invCov,
00493                                                  AlgebraicMatrix m_weightRes,
00494                                                  AlgebraicMatrix m_res,
00495                                                  std::string id)
00496         {
00497 
00498           
00499 
00500           std::string id_invCov = id + "_invCov";
00501           std::string id_weightRes = id + "_weightRes";
00502           std::string id_n = id + "_N";
00503           
00504           edm::LogInfo("Alignment") << "Entrando";
00505 
00506           std::map<std::string, AlgebraicMatrix *>::iterator node = map_invCov.find(id_invCov);
00507           if(node == map_invCov.end())
00508           {
00509             AlgebraicMatrix *f_invCov = new AlgebraicMatrix(m_invCov.num_row(), m_invCov.num_col());
00510             AlgebraicMatrix *f_weightRes = new AlgebraicMatrix(m_weightRes.num_row(), m_weightRes.num_col());
00511             AlgebraicMatrix *f_n = new AlgebraicMatrix(1,1);
00512 
00513             map_invCov.insert(make_pair(id_invCov, f_invCov));
00514             map_weightRes.insert(make_pair(id_weightRes, f_weightRes));
00515             map_N.insert(make_pair(id_n, f_n));
00516 
00517             for(int iCount = 0; iCount < 4; ++iCount)
00518             {
00519               char name[40];
00520               snprintf(name, sizeof(name), "%s_var_%d", id.c_str(), iCount);
00521               std::string idName(name);
00522               float range = 5.0;
00523               //if( iCount == 0 || iCount == 1 ) {
00524               //  range = 0.01;
00525               //}
00526               TH1D *histo = fs->make<TH1D>(idName.c_str(), idName.c_str(), 200, -range, range );
00527               histoMap.insert(make_pair(idName, histo));
00528             }
00529           }
00530           
00531           *map_invCov[id_invCov] = *map_invCov[id_invCov] + m_invCov;
00532           *map_weightRes[id_weightRes] = *map_weightRes[id_weightRes] + m_weightRes;
00533           (*map_N[id_n])[0][0]++;
00534 
00535           for(int iCount = 0; iCount < 4; ++iCount)
00536           {
00537             char name[40];
00538             snprintf(name, sizeof(name), "%s_var_%d", id.c_str(), iCount);
00539             std::string idName(name); 
00540             histoMap[idName]->Fill(m_res[iCount][0]);
00541           }
00542         } 
00543 
00544 
00545 
00546 DEFINE_EDM_PLUGIN( AlignmentAlgorithmPluginFactory, MuonMillepedeAlgorithm, "MuonMillepedeAlgorithm" );
00547