00001 #include <fstream>
00002
00003 #include "TFile.h"
00004 #include "TTree.h"
00005
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 #include "FWCore/Framework/interface/EventSetup.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00010
00011 #include "TrackingTools/PatternTools/interface/Trajectory.h"
00012 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00013
00014 #include "Alignment/CommonAlignment/interface/Alignable.h"
00015 #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"
00016 #include "Alignment/CommonAlignment/interface/AlignableObjectId.h"
00017 #include "Alignment/CommonAlignment/interface/AlignmentParameters.h"
00018 #include "Alignment/CommonAlignment/interface/SurveyResidual.h"
00019 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
00020 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterSelector.h"
00021 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPUserVariables.h"
00022 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPUserVariablesIORoot.h"
00023 #include "Alignment/MuonAlignment/interface/AlignableMuon.h"
00024 #include <DataFormats/GeometrySurface/interface/LocalError.h>
00025 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00026 #include "Alignment/CommonAlignment/interface/AlignableExtras.h"
00027 #include "DataFormats/TrackReco/interface/Track.h"
00028 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
00029 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00030
00031 #include "Alignment/HIPAlignmentAlgorithm/interface/HIPAlignmentAlgorithm.h"
00032
00033
00034
00035
00036
00037 HIPAlignmentAlgorithm::HIPAlignmentAlgorithm(const edm::ParameterSet& cfg):
00038 AlignmentAlgorithmBase( cfg )
00039 {
00040
00041
00042
00043 verbose = cfg.getParameter<bool>("verbosity");
00044
00045 outpath = cfg.getParameter<string>("outpath");
00046 outfile = cfg.getParameter<string>("outfile");
00047 outfile2 = cfg.getParameter<string>("outfile2");
00048 struefile = cfg.getParameter<string>("trueFile");
00049 smisalignedfile = cfg.getParameter<string>("misalignedFile");
00050 salignedfile = cfg.getParameter<string>("alignedFile");
00051 siterationfile = cfg.getParameter<string>("iterationFile");
00052 suvarfile = cfg.getParameter<string>("uvarFile");
00053 sparameterfile = cfg.getParameter<string>("parameterFile");
00054 ssurveyfile = cfg.getParameter<string>("surveyFile");
00055
00056 outfile =outpath+outfile;
00057 outfile2 =outpath+outfile2;
00058 struefile =outpath+struefile;
00059 smisalignedfile=outpath+smisalignedfile;
00060 salignedfile =outpath+salignedfile;
00061 siterationfile =outpath+siterationfile;
00062 suvarfile =outpath+suvarfile;
00063 sparameterfile =outpath+sparameterfile;
00064 ssurveyfile =outpath+ssurveyfile;
00065
00066
00067 theApplyAPE = cfg.getParameter<bool>("applyAPE");
00068 theAPEParameterSet = cfg.getParameter<std::vector<edm::ParameterSet> >("apeParam");
00069
00070 theMaxAllowedHitPull = cfg.getParameter<double>("maxAllowedHitPull");
00071 theMinimumNumberOfHits = cfg.getParameter<int>("minimumNumberOfHits");
00072 theMaxRelParameterError = cfg.getParameter<double>("maxRelParameterError");
00073
00074
00075 isCollector=cfg.getParameter<bool>("collectorActive");
00076 theCollectorNJobs=cfg.getParameter<int>("collectorNJobs");
00077 theCollectorPath=cfg.getParameter<string>("collectorPath");
00078 theFillTrackMonitoring=cfg.getUntrackedParameter<bool>("fillTrackMonitoring");
00079
00080 if (isCollector) edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] Collector mode";
00081
00082 theEventPrescale = cfg.getParameter<int>("eventPrescale");
00083 theCurrentPrescale = theEventPrescale;
00084
00085 for (std::string &s : cfg.getUntrackedParameter<std::vector<std::string> >("surveyResiduals")) {
00086 theLevels.push_back(AlignableObjectId::stringToId(s) );
00087 }
00088
00089 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] constructed.";
00090
00091 }
00092
00093
00094
00095 void
00096 HIPAlignmentAlgorithm::initialize( const edm::EventSetup& setup,
00097 AlignableTracker* tracker, AlignableMuon* muon, AlignableExtras* extras,
00098 AlignmentParameterStore* store )
00099 {
00100 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] Initializing...";
00101
00102
00103 if ( !muon )
00104 theAlignableDetAccessor = new AlignableNavigator(tracker);
00105 else if ( !tracker )
00106 theAlignableDetAccessor = new AlignableNavigator(muon);
00107 else
00108 theAlignableDetAccessor = new AlignableNavigator(tracker, muon);
00109
00110
00111 theAlignmentParameterStore=store;
00112
00113
00114 theAlignables = theAlignmentParameterStore->alignables();
00115
00116
00117 theAPEParameters.clear();
00118
00119
00120 if(theApplyAPE){
00121 AlignmentParameterSelector selector(tracker, muon);
00122 for (std::vector<edm::ParameterSet>::const_iterator setiter = theAPEParameterSet.begin();
00123 setiter != theAPEParameterSet.end();
00124 ++setiter) {
00125 std::vector<Alignable*> alignables;
00126
00127 selector.clear();
00128 edm::ParameterSet selectorPSet = setiter->getParameter<edm::ParameterSet>("Selector");
00129 std::vector<std::string> alignParams = selectorPSet.getParameter<std::vector<std::string> >("alignParams");
00130 if (alignParams.size() == 1 && alignParams[0] == std::string("selected")) {
00131 alignables = theAlignables;
00132 }
00133 else {
00134 selector.addSelections(selectorPSet);
00135 alignables = selector.selectedAlignables();
00136 }
00137
00138 std::vector<double> apeSPar = setiter->getParameter<std::vector<double> >("apeSPar");
00139 std::vector<double> apeRPar = setiter->getParameter<std::vector<double> >("apeRPar");
00140 std::string function = setiter->getParameter<std::string>("function");
00141
00142 if (apeSPar.size() != 3 || apeRPar.size() != 3)
00143 throw cms::Exception("BadConfig") << "apeSPar and apeRPar must have 3 values each" << std::endl;
00144
00145 for (std::vector<double>::const_iterator i = apeRPar.begin(); i != apeRPar.end(); ++i) {
00146 apeSPar.push_back(*i);
00147 }
00148
00149 if (function == std::string("linear")) {
00150 apeSPar.push_back(0.);
00151 }
00152 else if (function == std::string("exponential")) {
00153 apeSPar.push_back(1.);
00154 }
00155 else if (function == std::string("step")) {
00156 apeSPar.push_back(2.);
00157 }
00158 else {
00159 throw cms::Exception("BadConfig") << "APE function must be \"linear\" or \"exponential\"." << std::endl;
00160 }
00161
00162 theAPEParameters.push_back(std::pair<std::vector<Alignable*>, std::vector<double> >(alignables, apeSPar));
00163 }
00164 }
00165 }
00166
00167
00168 void HIPAlignmentAlgorithm::startNewLoop( void )
00169 {
00170
00171
00172 for( vector<Alignable*>::const_iterator it=theAlignables.begin();
00173 it!=theAlignables.end(); it++ )
00174 {
00175 AlignmentParameters* ap = (*it)->alignmentParameters();
00176 int npar=ap->numSelected();
00177 HIPUserVariables* userpar = new HIPUserVariables(npar);
00178 ap->setUserVariables(userpar);
00179 }
00180
00181
00182 AlignablePositions theAlignablePositionsFromFile =
00183 theIO.readAlignableAbsolutePositions(theAlignables,
00184 salignedfile.c_str(),-1,ioerr);
00185
00186 int numAlignablesFromFile = theAlignablePositionsFromFile.size();
00187
00188 if (numAlignablesFromFile==0) {
00189
00190
00191 if (isCollector) theIteration=0;
00192 else theIteration=1;
00193 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] File not found => iteration "<<theIteration;
00194
00195
00196
00197 theIO.writeAlignableOriginalPositions(theAlignables,
00198 struefile.c_str(),1,false,ioerr);
00199
00200
00201
00202 theIO.writeAlignableAbsolutePositions(theAlignables,
00203 smisalignedfile.c_str(),1,false,ioerr);
00204
00205 }
00206
00207 else {
00208
00209 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] Alignables Read "
00210 << numAlignablesFromFile;
00211
00212
00213 theIteration = readIterationFile(siterationfile);
00214
00215
00216 theIteration++;
00217 edm::LogWarning("Alignment") <<"[HIPAlignmentAlgorithm] Iteration increased by one!";
00218
00219
00220 edm::LogWarning("Alignment") <<"[HIPAlignmentAlgorithm] Apply positions from file ...";
00221 theAlignmentParameterStore->applyAlignableAbsolutePositions(theAlignables,
00222 theAlignablePositionsFromFile,ioerr);
00223
00224 }
00225
00226 edm::LogWarning("Alignment") <<"[HIPAlignmentAlgorithm] Current Iteration number: "
00227 << theIteration;
00228
00229
00230
00231 bookRoot();
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 setAlignmentPositionError();
00295
00296
00297 if (isCollector) collector();
00298
00299 }
00300
00301
00302
00303 void HIPAlignmentAlgorithm::terminate(const edm::EventSetup& iSetup)
00304 {
00305
00306 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] Terminating";
00307
00308
00309 if (theLevels.size() > 0)
00310 {
00311 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] Using survey constraint";
00312
00313 unsigned int nAlignable = theAlignables.size();
00314
00315 for (unsigned int i = 0; i < nAlignable; ++i)
00316 {
00317 const Alignable* ali = theAlignables[i];
00318
00319 AlignmentParameters* ap = ali->alignmentParameters();
00320
00321 HIPUserVariables* uservar =
00322 dynamic_cast<HIPUserVariables*>(ap->userVariables());
00323
00324 for (unsigned int l = 0; l < theLevels.size(); ++l)
00325 {
00326 SurveyResidual res(*ali, theLevels[l], true);
00327
00328 if ( res.valid() )
00329 {
00330 AlgebraicSymMatrix invCov = res.inverseCovariance();
00331
00332
00333 AlgebraicVector sensResid = res.sensorResidual();
00334 m3_Id = ali->id();
00335 m3_ObjId = theLevels[l];
00336 m3_par[0] = sensResid[0]; m3_par[1] = sensResid[1]; m3_par[2] = sensResid[2];
00337 m3_par[3] = sensResid[3]; m3_par[4] = sensResid[4]; m3_par[5] = sensResid[5];
00338
00339 uservar->jtvj += invCov;
00340 uservar->jtve += invCov * sensResid;
00341
00342 theTree3->Fill();
00343 }
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 }
00364 }
00365
00366
00367 HIPUserVariablesIORoot HIPIO;
00368 HIPIO.writeHIPUserVariables (theAlignables,suvarfile.c_str(),
00369 theIteration,false,ioerr);
00370
00371
00372 int ialigned=0;
00373
00374 for(vector<Alignable*>::const_iterator
00375 it=theAlignables.begin(); it!=theAlignables.end(); it++) {
00376 Alignable* ali=(*it);
00377
00378 AlignmentParameters* par = ali->alignmentParameters();
00379
00380
00381 bool test = calcParameters(ali);
00382
00383
00384 if (test) {
00385 edm::LogInfo("Alignment") << "now apply params";
00386 theAlignmentParameterStore->applyParameters(ali);
00387
00388 ali->alignmentParameters()->setValid(true);
00389
00390 ialigned++;
00391 }
00392 else par->setValid(false);
00393 }
00394 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::terminate] Aligned units: " << ialigned;
00395
00396
00397 fillRoot(iSetup);
00398
00399 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm] Writing aligned parameters to file: " << theAlignables.size();
00400
00401
00402 theIO.writeAlignableAbsolutePositions(theAlignables,
00403 salignedfile.c_str(),theIteration,false,ioerr);
00404
00405
00406 theIO.writeAlignmentParameters(theAlignables,
00407 sparameterfile.c_str(),theIteration,false,ioerr);
00408
00409
00410 writeIterationFile(siterationfile,theIteration);
00411
00412
00413
00414
00415 theFile->cd();
00416 theTree->Write();
00417 delete theFile;
00418
00419 if (theLevels.size() > 0){
00420 theFile3->cd();
00421 theTree3->Write();
00422 delete theFile3;
00423 }
00424
00425
00426 if (theIteration==1) {
00427 theFile2->cd();
00428 theTree2->Write();
00429 delete theFile2;
00430 }
00431
00432 }
00433
00434 bool HIPAlignmentAlgorithm::processHit1D(const AlignableDetOrUnitPtr alidet,
00435 const Alignable* ali,
00436 const TrajectoryStateOnSurface & tsos,
00437 const TransientTrackingRecHit* hit)
00438 {
00439 static const unsigned int hitDim = 1;
00440
00441
00442 LocalPoint alvec = tsos.localPosition();
00443 AlgebraicVector pos(hitDim);
00444 pos[0] = alvec.x();
00445
00446
00447 AlgebraicSymMatrix ipcovmat(hitDim);
00448 ipcovmat[0][0] = tsos.localError().positionError().xx();
00449
00450
00451 AlgebraicVector coor(hitDim);
00452 coor[0] = hit->localPosition().x();
00453
00454 AlgebraicSymMatrix covmat(hitDim);
00455 covmat[0][0] = hit->localPositionError().xx();
00456
00457
00458 covmat = covmat + ipcovmat;
00459
00460
00461 double xpull = 0.;
00462 if (covmat[0][0] != 0.) xpull = (pos[0] - coor[0])/sqrt(fabs(covmat[0][0]));
00463
00464
00465 AlignmentParameters* params = ali->alignmentParameters();
00466
00467 AlgebraicMatrix derivs2D = params->selectedDerivatives(tsos, alidet);
00468
00469 int npar = derivs2D.num_row();
00470
00471 AlgebraicMatrix derivs(npar, hitDim, 0);
00472
00473 for (int ipar=0;ipar<npar;ipar++) {
00474 for (unsigned int idim=0;idim<hitDim;idim++) {
00475 derivs[ipar][idim] = derivs2D[ipar][idim];
00476 }
00477 }
00478
00479
00480 int ierr;
00481
00482
00483 covmat.invert(ierr);
00484 if (ierr != 0) {
00485 edm::LogError("Alignment") << "Matrix inversion failed!";
00486 return false;
00487 }
00488
00489 bool useThisHit = (theMaxAllowedHitPull <= 0.);
00490
00491 useThisHit |= (fabs(xpull) < theMaxAllowedHitPull);
00492
00493
00494 if (!useThisHit) return false;
00495
00496
00497
00498
00499
00500
00501 AlgebraicMatrix covtmp(covmat);
00502
00503 AlgebraicMatrix jtvjtmp(derivs * covtmp *derivs.T());
00504
00505 AlgebraicSymMatrix thisjtvj(npar);
00506 AlgebraicVector thisjtve(npar);
00507
00508
00509 thisjtvj.assign(jtvjtmp);
00510
00511 thisjtve=derivs * covmat * (pos-coor);
00512
00513 AlgebraicVector hitresidual(hitDim);
00514 hitresidual[0] = (pos[0] - coor[0]);
00515
00516 AlgebraicMatrix hitresidualT;
00517 hitresidualT = hitresidual.T();
00518
00519
00520
00521 HIPUserVariables* uservar = dynamic_cast<HIPUserVariables*>(params->userVariables());
00522 uservar->jtvj += thisjtvj;
00523 uservar->jtve += thisjtve;
00524 uservar->nhit++;
00525
00526
00527
00528
00529
00530 float thischi2;
00531 thischi2 = (hitresidualT *covmat *hitresidual)[0];
00532
00533 if ( verbose && ((thischi2/ static_cast<float>(uservar->nhit)) >10.0) ) {
00534 edm::LogWarning("Alignment") << "Added to Chi2 the number " << thischi2 <<" having "
00535 << uservar->nhit << " dof " << std::endl << "X-resid "
00536 << hitresidual[0] << " Y-resid "
00537 << hitresidual[1] << std::endl << " Cov^-1 matr (covmat): [0][0]= "
00538 << covmat[0][0] << " [0][1]= "
00539 << covmat[0][1] << " [1][0]= "
00540 << covmat[1][0] << " [1][1]= "
00541 << covmat[1][1] << std::endl;
00542 }
00543
00544 uservar->alichi2 += thischi2;
00545 uservar->alindof += hitDim;
00546
00547 return true;
00548 }
00549
00550 bool HIPAlignmentAlgorithm::processHit2D(const AlignableDetOrUnitPtr alidet,
00551 const Alignable* ali,
00552 const TrajectoryStateOnSurface & tsos,
00553 const TransientTrackingRecHit* hit)
00554 {
00555 static const unsigned int hitDim = 2;
00556
00557
00558 LocalPoint alvec = tsos.localPosition();
00559 AlgebraicVector pos(hitDim);
00560 pos[0] = alvec.x();
00561 pos[1] = alvec.y();
00562
00563
00564 AlgebraicSymMatrix ipcovmat(hitDim);
00565 ipcovmat[0][0] = tsos.localError().positionError().xx();
00566 ipcovmat[1][1] = tsos.localError().positionError().yy();
00567 ipcovmat[0][1] = tsos.localError().positionError().xy();
00568
00569
00570 AlgebraicVector coor(hitDim);
00571 coor[0] = hit->localPosition().x();
00572 coor[1] = hit->localPosition().y();
00573
00574 AlgebraicSymMatrix covmat(hitDim);
00575 covmat[0][0] = hit->localPositionError().xx();
00576 covmat[1][1] = hit->localPositionError().yy();
00577 covmat[0][1] = hit->localPositionError().xy();
00578
00579
00580 covmat = covmat + ipcovmat;
00581
00582
00583 double xpull = 0.;
00584 double ypull = 0.;
00585 if (covmat[0][0] != 0.) xpull = (pos[0] - coor[0])/sqrt(fabs(covmat[0][0]));
00586 if (covmat[1][1] != 0.) ypull = (pos[1] - coor[1])/sqrt(fabs(covmat[1][1]));
00587
00588
00589 AlignmentParameters* params = ali->alignmentParameters();
00590
00591 AlgebraicMatrix derivs2D = params->selectedDerivatives(tsos, alidet);
00592
00593 int npar = derivs2D.num_row();
00594
00595 AlgebraicMatrix derivs(npar, hitDim, 0);
00596
00597 for (int ipar=0;ipar<npar;ipar++) {
00598 for (unsigned int idim=0;idim<hitDim;idim++) {
00599 derivs[ipar][idim] = derivs2D[ipar][idim];
00600 }
00601 }
00602
00603
00604 int ierr;
00605
00606
00607 covmat.invert(ierr);
00608 if (ierr != 0) {
00609 edm::LogError("Alignment") << "Matrix inversion failed!";
00610 return false;
00611 }
00612
00613 bool useThisHit = (theMaxAllowedHitPull <= 0.);
00614
00615 useThisHit |= (fabs(xpull) < theMaxAllowedHitPull && fabs(ypull) < theMaxAllowedHitPull);
00616
00617
00618 if (!useThisHit) return false;
00619
00620
00621
00622
00623
00624
00625 AlgebraicMatrix covtmp(covmat);
00626
00627 AlgebraicMatrix jtvjtmp(derivs * covtmp *derivs.T());
00628
00629 AlgebraicSymMatrix thisjtvj(npar);
00630 AlgebraicVector thisjtve(npar);
00631
00632
00633 thisjtvj.assign(jtvjtmp);
00634
00635 thisjtve=derivs * covmat * (pos-coor);
00636
00637 AlgebraicVector hitresidual(hitDim);
00638 hitresidual[0] = (pos[0] - coor[0]);
00639 hitresidual[1] = (pos[1] - coor[1]);
00640
00641
00642
00643
00644
00645 AlgebraicMatrix hitresidualT;
00646 hitresidualT = hitresidual.T();
00647
00648
00649 HIPUserVariables* uservar = dynamic_cast<HIPUserVariables*>(params->userVariables());
00650 uservar->jtvj += thisjtvj;
00651 uservar->jtve += thisjtve;
00652 uservar->nhit++;
00653
00654
00655
00656
00657
00658 float thischi2;
00659 thischi2 = (hitresidualT *covmat *hitresidual)[0];
00660
00661 if ( verbose && ((thischi2/ static_cast<float>(uservar->nhit)) >10.0) ) {
00662 edm::LogWarning("Alignment") << "Added to Chi2 the number " << thischi2 <<" having "
00663 << uservar->nhit << " dof " << std::endl << "X-resid "
00664 << hitresidual[0] << " Y-resid "
00665 << hitresidual[1] << std::endl << " Cov^-1 matr (covmat): [0][0]= "
00666 << covmat[0][0] << " [0][1]= "
00667 << covmat[0][1] << " [1][0]= "
00668 << covmat[1][0] << " [1][1]= "
00669 << covmat[1][1] << std::endl;
00670 }
00671
00672 uservar->alichi2 += thischi2;
00673 uservar->alindof += hitDim;
00674
00675 return true;
00676 }
00677
00678
00679 void HIPAlignmentAlgorithm::run(const edm::EventSetup& setup, const EventInfo &eventInfo)
00680 {
00681 if (isCollector) return;
00682
00683 TrajectoryStateCombiner tsoscomb;
00684
00685
00686
00687 int itr=0;
00688 m_Ntracks=0;
00689 for(itr=0;itr<MAXREC;++itr){
00690 m_Nhits[itr]=0;
00691 m_Pt[itr]=-5.0;
00692 m_P[itr]=-5.0;
00693 m_nhPXB[itr]=0;
00694 m_nhPXF[itr]=0;
00695 m_Eta[itr]=-99.0;
00696 m_Phi[itr]=-4.0;
00697 m_Chi2n[itr]=-11.0;
00698 m_d0[itr]=-999;
00699 m_dz[itr]=-999;
00700 }
00701 itr=0;
00702
00703
00704 theFile->cd();
00705
00706
00707 const ConstTrajTrackPairCollection &tracks = eventInfo.trajTrackPairs_;
00708 for (ConstTrajTrackPairCollection::const_iterator it=tracks.begin();
00709 it!=tracks.end();
00710 ++it) {
00711
00712 const Trajectory* traj = (*it).first;
00713 const reco::Track* track = (*it).second;
00714
00715 float pt = track->pt();
00716 float eta = track->eta();
00717 float phi = track->phi();
00718 float p = track->p();
00719 float chi2n = track->normalizedChi2();
00720 int nhit = track->numberOfValidHits();
00721 float d0 = track->d0();
00722 float dz = track->dz();
00723
00724 int nhpxb = track->hitPattern().numberOfValidPixelBarrelHits();
00725 int nhpxf = track->hitPattern().numberOfValidPixelEndcapHits();
00726
00727 if (verbose) edm::LogInfo("Alignment") << "New track pt,eta,phi,chi2n,hits: "
00728 << pt << ","
00729 << eta << ","
00730 << phi << ","
00731 << chi2n << ","
00732 << nhit;
00733
00734
00735
00736
00737
00738
00739
00740
00741 if (itr<MAXREC) {
00742 m_Nhits[itr]=nhit;
00743 m_Pt[itr]=pt;
00744 m_P[itr]=p;
00745 m_Eta[itr]=eta;
00746 m_Phi[itr]=phi;
00747 m_Chi2n[itr]=chi2n;
00748 m_nhPXB[itr]=nhpxb;
00749 m_nhPXF[itr]=nhpxf;
00750 m_d0[itr]=d0;
00751 m_dz[itr]=dz;
00752 itr++;
00753 m_Ntracks=itr;
00754 }
00755
00756
00757 vector<const TransientTrackingRecHit*> hitvec;
00758 vector<TrajectoryStateOnSurface> tsosvec;
00759
00760
00761 vector<TrajectoryMeasurement> measurements = traj->measurements();
00762 for (vector<TrajectoryMeasurement>::iterator im=measurements.begin();
00763 im!=measurements.end();
00764 ++im) {
00765
00766 TrajectoryMeasurement meas = *im;
00767
00768
00769
00770 const TransientTrackingRecHit* hit = &(*meas.recHit());
00771
00772 if (hit->isValid() && theAlignableDetAccessor->detAndSubdetInMap( hit->geographicalId() )) {
00773
00774
00775
00776
00777
00778
00780 bool skiphit = false;
00781 if (eventInfo.clusterValueMap_) {
00782
00783
00784
00785 AlignmentClusterFlag myflag;
00786
00787 int subDet = hit->geographicalId().subdetId();
00788
00789 const TrackingRecHit *rechit=hit->hit();
00790 if (subDet>2) {
00791 const std::type_info &type = typeid(*rechit);
00792
00793 if (type == typeid(SiStripRecHit1D)) {
00794
00795 const SiStripRecHit1D* stripHit1D = dynamic_cast<const SiStripRecHit1D*>(rechit);
00796 if (stripHit1D) {
00797 SiStripRecHit1D::ClusterRef stripclust(stripHit1D->cluster());
00798
00799 myflag = (*eventInfo.clusterValueMap_)[stripclust];
00800 } else {
00801 edm::LogError("HIPAlignmentAlgorithm")
00802 << "ERROR in <HIPAlignmentAlgorithm::run>: Dynamic cast of Strip RecHit failed! "
00803 << "TypeId of the RecHit: " << className(*hit) <<endl;
00804 }
00805
00806 }
00807 else if(type == typeid(SiStripRecHit2D)){
00808
00809 const SiStripRecHit2D* stripHit2D = dynamic_cast<const SiStripRecHit2D*>(rechit);
00810 if (stripHit2D) {
00811 SiStripRecHit2D::ClusterRef stripclust(stripHit2D->cluster());
00812
00813 myflag = (*eventInfo.clusterValueMap_)[stripclust];
00814 } else {
00815 edm::LogError("HIPAlignmentAlgorithm")
00816 << "ERROR in <HIPAlignmentAlgorithm::run>: Dynamic cast of Strip RecHit failed! "
00817
00818 << "TypeId of the TTRH: " << className(*hit) << endl;
00819 }
00820 }
00821 }
00822 else {
00823 const SiPixelRecHit* pixelhit= dynamic_cast<const SiPixelRecHit*>(rechit);
00824 if (pixelhit) {
00825 SiPixelClusterRefNew pixelclust(pixelhit->cluster());
00826
00827 myflag = (*eventInfo.clusterValueMap_)[pixelclust];
00828 }
00829 else {
00830 edm::LogError("HIPAlignmentAlgorithm")
00831 << "ERROR in <HIPAlignmentAlgorithm::run>: Dynamic cast of Pixel RecHit failed! "
00832
00833 << "TypeId of the TTRH: " << className(*hit) << endl;
00834 }
00835 }
00836
00837 if (!myflag.isTaken()) {
00838 skiphit=true;
00839
00840 continue;
00841 }
00842 }
00844 if (skiphit) {
00845 throw cms::Exception("LogicError")
00846 << "ERROR in <HIPAlignmentAlgorithm::run>: this hit should have been skipped!"
00847 << endl;
00848 }
00849
00850 TrajectoryStateOnSurface tsos = tsoscomb.combine(meas.forwardPredictedState(),
00851 meas.backwardPredictedState());
00852
00853 if(tsos.isValid()){
00854
00855 hitvec.push_back(hit);
00856 tsosvec.push_back(tsos);
00857 }
00858
00859 }
00860 }
00861
00862
00863 vector <AlignableDetOrUnitPtr> alidetvec = theAlignableDetAccessor->alignablesFromHits(hitvec);
00864
00865
00866 CompositeAlignmentParameters aap = theAlignmentParameterStore->selectParameters(alidetvec);
00867
00868 vector<TrajectoryStateOnSurface>::const_iterator itsos=tsosvec.begin();
00869 vector<const TransientTrackingRecHit*>::const_iterator ihit=hitvec.begin();
00870
00871
00872 while (itsos != tsosvec.end()) {
00873
00874
00875 const GeomDet* det = (*ihit)->det();
00876
00877 uint32_t nhitDim = (*ihit)->dimension();
00878
00879 AlignableDetOrUnitPtr alidet = theAlignableDetAccessor->alignableFromGeomDet(det);
00880
00881
00882 Alignable* ali = aap.alignableFromAlignableDet(alidet);
00883
00884 if (ali!=0) {
00885 if (nhitDim==1) {
00886 processHit1D(alidet, ali, *itsos, *ihit);
00887 } else if (nhitDim==2) {
00888 processHit2D(alidet, ali, *itsos, *ihit);
00889 }
00890 }
00891
00892 itsos++;
00893 ihit++;
00894 }
00895 }
00896
00897
00898 if (theFillTrackMonitoring) {
00899 theCurrentPrescale--;
00900 if (theCurrentPrescale<=0) {
00901 theTree->Fill();
00902 theCurrentPrescale = theEventPrescale;
00903 }
00904 }
00905 }
00906
00907
00908
00909 int HIPAlignmentAlgorithm::readIterationFile(string filename)
00910 {
00911 int result;
00912
00913 ifstream inIterFile(filename.c_str(), ios::in);
00914 if (!inIterFile) {
00915 edm::LogError("Alignment") << "[HIPAlignmentAlgorithm::readIterationFile] ERROR! "
00916 << "Unable to open Iteration file";
00917 result = -1;
00918 }
00919 else {
00920 inIterFile >> result;
00921 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::readIterationFile] "
00922 << "Read last iteration number from file: " << result;
00923 }
00924 inIterFile.close();
00925
00926 return result;
00927 }
00928
00929
00930
00931 void HIPAlignmentAlgorithm::writeIterationFile(string filename,int iter)
00932 {
00933 ofstream outIterFile((filename.c_str()), ios::out);
00934 if (!outIterFile) {
00935 edm::LogError("Alignment") << "[HIPAlignmentAlgorithm::writeIterationFile] ERROR: Unable to write Iteration file";
00936 }
00937 else {
00938 outIterFile << iter;
00939 edm::LogWarning("Alignment") <<"[HIPAlignmentAlgorithm::writeIterationFile] writing iteration number to file: " << iter;
00940 }
00941 outIterFile.close();
00942 }
00943
00944
00945
00946
00947
00948 void HIPAlignmentAlgorithm::setAlignmentPositionError(void)
00949 {
00950
00951
00952
00953 if ( !theApplyAPE )
00954 {
00955 edm::LogWarning("Alignment") <<"[HIPAlignmentAlgorithm::setAlignmentPositionError] No APE applied";
00956 return;
00957 }
00958
00959
00960 edm::LogWarning("Alignment") <<"[HIPAlignmentAlgorithm::setAlignmentPositionError] Apply APE!";
00961
00962 double apeSPar[3], apeRPar[3];
00963 for (std::vector<std::pair<std::vector<Alignable*>, std::vector<double> > >::const_iterator alipars = theAPEParameters.begin();
00964 alipars != theAPEParameters.end();
00965 ++alipars) {
00966 const std::vector<Alignable*> &alignables = alipars->first;
00967 const std::vector<double> &pars = alipars->second;
00968
00969 apeSPar[0] = pars[0];
00970 apeSPar[1] = pars[1];
00971 apeSPar[2] = pars[2];
00972 apeRPar[0] = pars[3];
00973 apeRPar[1] = pars[4];
00974 apeRPar[2] = pars[5];
00975
00976 double function = pars[6];
00977
00978
00979 printf("APE: %u alignables\n", (unsigned int)alignables.size());
00980 for ( int i=0; i<21; ++i ) {
00981 double apelinstest=calcAPE(apeSPar,i,0.);
00982 double apeexpstest=calcAPE(apeSPar,i,1.);
00983 double apelinrtest=calcAPE(apeRPar,i,0.);
00984 double apeexprtest=calcAPE(apeRPar,i,1.);
00985 printf("APE: iter slin sexp rlin rexp: %5d %12.5f %12.5f %12.5f %12.5f\n",
00986 i,apelinstest,apeexpstest,apelinrtest,apeexprtest);
00987 }
00988
00989
00990 double apeshift=calcAPE(apeSPar,theIteration,function);
00991 double aperot =calcAPE(apeRPar,theIteration,function);
00992 theAlignmentParameterStore->setAlignmentPositionError( alignables, apeshift, aperot );
00993 }
00994
00995 }
00996
00997
00998
00999
01000 double
01001 HIPAlignmentAlgorithm::calcAPE(double* par, int iter, double function)
01002 {
01003 double diter=(double)iter;
01004
01005
01006
01007
01008
01009
01010 if (function == 0.) {
01011 return max(par[1],par[0]+((par[1]-par[0])/par[2])*diter);
01012 }
01013 else if (function == 1.) {
01014 return max(0.,par[0]*(exp(-pow(diter,par[1])/par[2])));
01015 }
01016 else if (function == 2.) {
01017 int ipar2 = (int) par[2];
01018 int step = iter/ipar2;
01019 double dstep = (double) step;
01020 return max(0.0, par[0] - par[1]*dstep);
01021 }
01022 else assert(false);
01023 }
01024
01025
01026
01027
01028
01029 void HIPAlignmentAlgorithm::bookRoot(void)
01030 {
01031
01032 theFile = new TFile(outfile.c_str(),"update");
01033 theFile->cd();
01034
01035
01036
01037 TString tname="T1";
01038 char iterString[5];
01039 snprintf(iterString, sizeof(iterString), "%i",theIteration);
01040 tname.Append("_");
01041 tname.Append(iterString);
01042
01043 theTree = new TTree(tname,"Eventwise tree");
01044
01045
01046
01047 theTree->Branch("Ntracks", &m_Ntracks, "Ntracks/I");
01048 theTree->Branch("Nhits", m_Nhits, "Nhits[Ntracks]/I");
01049 theTree->Branch("nhPXB", m_nhPXB, "nhPXB[Ntracks]/I");
01050 theTree->Branch("nhPXF", m_nhPXF, "nhPXF[Ntracks]/I");
01051 theTree->Branch("Pt", m_Pt, "Pt[Ntracks]/F");
01052 theTree->Branch("P", m_P, "P[Ntracks]/F");
01053 theTree->Branch("Eta", m_Eta, "Eta[Ntracks]/F");
01054 theTree->Branch("Phi", m_Phi, "Phi[Ntracks]/F");
01055 theTree->Branch("Chi2n", m_Chi2n, "Chi2n[Ntracks]/F");
01056 theTree->Branch("d0", m_d0, "d0[Ntracks]/F");
01057 theTree->Branch("dz", m_dz, "dz[Ntracks]/F");
01058
01059
01060
01061 theFile2 = new TFile(outfile2.c_str(),"update");
01062 theFile2->cd();
01063
01064 theTree2 = new TTree("T2","Alignablewise tree");
01065
01066 theTree2->Branch("Nhit", &m2_Nhit, "Nhit/I");
01067 theTree2->Branch("Type", &m2_Type, "Type/I");
01068 theTree2->Branch("Layer", &m2_Layer, "Layer/I");
01069 theTree2->Branch("Xpos", &m2_Xpos, "Xpos/F");
01070 theTree2->Branch("Ypos", &m2_Ypos, "Ypos/F");
01071 theTree2->Branch("Zpos", &m2_Zpos, "Zpos/F");
01072 theTree2->Branch("Eta", &m2_Eta, "Eta/F");
01073 theTree2->Branch("Phi", &m2_Phi, "Phi/F");
01074 theTree2->Branch("Id", &m2_Id, "Id/i");
01075 theTree2->Branch("ObjId", &m2_ObjId, "ObjId/I");
01076
01077
01078 if (theLevels.size() > 0){
01079
01080 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::bookRoot] Survey trees booked.";
01081 theFile3 = new TFile(ssurveyfile.c_str(),"update");
01082 theFile3->cd();
01083 theTree3 = new TTree(tname, "Survey Tree");
01084 theTree3->Branch("Id", &m3_Id, "Id/i");
01085 theTree3->Branch("ObjId", &m3_ObjId, "ObjId/I");
01086 theTree3->Branch("Par", &m3_par, "Par[6]/F");
01087 }
01088
01089 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::bookRoot] Root trees booked.";
01090
01091 }
01092
01093
01094
01095
01096 void HIPAlignmentAlgorithm::fillRoot(const edm::EventSetup& iSetup)
01097 {
01098 theFile2->cd();
01099
01100 int naligned=0;
01101
01102
01103 edm::ESHandle<TrackerTopology> tTopoHandle;
01104 iSetup.get<IdealGeometryRecord>().get(tTopoHandle);
01105 const TrackerTopology* const tTopo = tTopoHandle.product();
01106
01107 for (vector<Alignable*>::const_iterator it=theAlignables.begin();
01108 it!=theAlignables.end();
01109 ++it) {
01110 Alignable* ali = (*it);
01111 AlignmentParameters* dap = ali->alignmentParameters();
01112
01113
01114 if (dap->isValid()) {
01115
01116
01117 HIPUserVariables* uservar = dynamic_cast<HIPUserVariables*>(dap->userVariables());
01118 m2_Nhit = uservar->nhit;
01119
01120
01121 std::pair<int,int> tl = theAlignmentParameterStore->typeAndLayer(ali, tTopo);
01122 m2_Type = tl.first;
01123 m2_Layer = tl.second;
01124
01125
01126 m2_Id = ali->id();
01127 m2_ObjId = ali->alignableObjectId();
01128
01129
01130 GlobalPoint pos = ali->surface().position();
01131 m2_Xpos = pos.x();
01132 m2_Ypos = pos.y();
01133 m2_Zpos = pos.z();
01134 m2_Eta = pos.eta();
01135 m2_Phi = pos.phi();
01136
01137 AlgebraicVector pars = dap->parameters();
01138
01139 if (verbose) {
01140 edm::LogVerbatim("Alignment")
01141 << "------------------------------------------------------------------------\n"
01142 << " ALIGNABLE: " << setw(6) << naligned
01143 << '\n'
01144 << "hits: " << setw(4) << m2_Nhit
01145 << " type: " << setw(4) << m2_Type
01146 << " layer: " << setw(4) << m2_Layer
01147 << " id: " << setw(4) << m2_Id
01148 << " objId: " << setw(4) << m2_ObjId
01149 << '\n'
01150 << fixed << setprecision(5)
01151 << "x,y,z: "
01152 << setw(12) << m2_Xpos
01153 << setw(12) << m2_Ypos
01154 << setw(12) << m2_Zpos
01155 << " eta,phi: "
01156 << setw(12) << m2_Eta
01157 << setw(12) << m2_Phi
01158 << '\n'
01159 << "params: "
01160 << setw(12) << pars[0]
01161 << setw(12) << pars[1]
01162 << setw(12) << pars[2]
01163 << setw(12) << pars[3]
01164 << setw(12) << pars[4]
01165 << setw(12) << pars[5];
01166 }
01167
01168 naligned++;
01169 theTree2->Fill();
01170 }
01171 }
01172 }
01173
01174
01175
01176 bool HIPAlignmentAlgorithm::calcParameters(Alignable* ali)
01177 {
01178
01179 AlignmentParameters* par = ali->alignmentParameters();
01180
01181 HIPUserVariables* uservar = dynamic_cast<HIPUserVariables*>(par->userVariables());
01182 int nhit = uservar->nhit;
01183
01184
01185
01186
01187 if (nhit < theMinimumNumberOfHits) {
01188 par->setValid(false);
01189 return false;
01190 }
01191
01192 AlgebraicSymMatrix jtvj = uservar->jtvj;
01193 AlgebraicVector jtve = uservar->jtve;
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225 int ierr;
01226 AlgebraicSymMatrix jtvjinv = jtvj.inverse(ierr);
01227
01228 if (ierr !=0) {
01229 edm::LogError("Alignment") << "Matrix inversion failed!";
01230 return false;
01231 }
01232
01233
01234 AlgebraicVector params = - (jtvjinv * jtve);
01235 AlgebraicSymMatrix cov = jtvjinv;
01236
01237 edm::LogInfo("Alignment") << "parameters " << params;
01238
01239
01240 int npar = params.num_row();
01241 AlgebraicVector paramerr(npar);
01242 AlgebraicVector relerr(npar);
01243 for (int i=0;i<npar;i++) {
01244 if (abs(cov[i][i])>0) paramerr[i] = sqrt(abs(cov[i][i]));
01245 else paramerr[i] = params[i];
01246 relerr[i] = abs(paramerr[i]/params[i]);
01247 if (relerr[i] >= theMaxRelParameterError) {
01248 params[i] = 0;
01249 paramerr[i]=0;
01250 }
01251 }
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284 AlignmentParameters* parnew = par->cloneFromSelected(params,cov);
01285 ali->setAlignmentParameters(parnew);
01286 parnew->setValid(true);
01287
01288 return true;
01289 }
01290
01291
01292
01293 void HIPAlignmentAlgorithm::collector(void)
01294 {
01295 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::collector] called for iteration "
01296 << theIteration << std::endl;
01297
01298 HIPUserVariablesIORoot HIPIO;
01299
01300 for (int ijob=1;ijob<=theCollectorNJobs;ijob++) {
01301
01302 edm::LogWarning("Alignment") << "reading uservar for job " << ijob;
01303
01304 stringstream ss;
01305 string str;
01306 ss << ijob;
01307 ss >> str;
01308 string uvfile = theCollectorPath+"/job"+str+"/IOUserVariables.root";
01309
01310 vector<AlignmentUserVariables*> uvarvec =
01311 HIPIO.readHIPUserVariables(theAlignables, uvfile.c_str(),
01312 theIteration, ioerr);
01313
01314 if (ioerr!=0) {
01315 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::collector] could not read user variable files for job "
01316 << ijob;
01317 continue;
01318 }
01319
01320
01321 vector<AlignmentUserVariables*> uvarvecadd;
01322 vector<AlignmentUserVariables*>::const_iterator iuvarnew=uvarvec.begin();
01323 for (vector<Alignable*>::const_iterator it=theAlignables.begin();
01324 it!=theAlignables.end();
01325 ++it) {
01326 Alignable* ali = *it;
01327 AlignmentParameters* ap = ali->alignmentParameters();
01328
01329 HIPUserVariables* uvarold = dynamic_cast<HIPUserVariables*>(ap->userVariables());
01330 HIPUserVariables* uvarnew = dynamic_cast<HIPUserVariables*>(*iuvarnew);
01331
01332 HIPUserVariables* uvar = uvarold->clone();
01333 if (uvarnew!=0) {
01334 uvar->nhit = (uvarold->nhit)+(uvarnew->nhit);
01335 uvar->jtvj = (uvarold->jtvj)+(uvarnew->jtvj);
01336 uvar->jtve = (uvarold->jtve)+(uvarnew->jtve);
01337 uvar->alichi2 = (uvarold->alichi2)+(uvarnew->alichi2);
01338 uvar->alindof = (uvarold->alindof)+(uvarnew->alindof);
01339 delete uvarnew;
01340 }
01341
01342 uvarvecadd.push_back(uvar);
01343 iuvarnew++;
01344 }
01345
01346 theAlignmentParameterStore->attachUserVariables(theAlignables, uvarvecadd, ioerr);
01347
01348
01349 if (theFillTrackMonitoring) {
01350 uvfile = theCollectorPath+"/job"+str+"/HIPAlignmentEvents.root";
01351 edm::LogWarning("Alignment") << "Added to the tree "
01352 << fillEventwiseTree(uvfile.c_str(), theIteration, ioerr)
01353 << "tracks";
01354 }
01355
01356 }
01357 }
01358
01359
01360
01361 int HIPAlignmentAlgorithm::fillEventwiseTree(const char* filename, int iter, int ierr)
01362 {
01363 int totntrk = 0;
01364 char treeName[64];
01365 snprintf(treeName, sizeof(treeName), "T1_%d", iter);
01366
01367 TFile *jobfile = new TFile(filename, "READ");
01368
01369 TTree *jobtree = (TTree*)jobfile->Get(treeName);
01370
01371 static const int nmaxtrackperevent = 1000;
01372 int jobNtracks, jobNhitspertrack[nmaxtrackperevent], jobnhPXB[nmaxtrackperevent], jobnhPXF[nmaxtrackperevent];
01373 float jobP[nmaxtrackperevent], jobPt[nmaxtrackperevent], jobEta[nmaxtrackperevent] , jobPhi[nmaxtrackperevent];
01374 float jobd0[nmaxtrackperevent], jobdz[nmaxtrackperevent] , jobChi2n[nmaxtrackperevent];
01375
01376 jobtree->SetBranchAddress("Ntracks", &jobNtracks);
01377 jobtree->SetBranchAddress("Nhits", jobNhitspertrack);
01378 jobtree->SetBranchAddress("nhPXB", jobnhPXB);
01379 jobtree->SetBranchAddress("nhPXF", jobnhPXF);
01380 jobtree->SetBranchAddress("Pt", jobPt);
01381 jobtree->SetBranchAddress("P", jobP);
01382 jobtree->SetBranchAddress("d0", jobd0);
01383 jobtree->SetBranchAddress("dz", jobdz);
01384 jobtree->SetBranchAddress("Eta", jobEta);
01385 jobtree->SetBranchAddress("Phi", jobPhi);
01386 jobtree->SetBranchAddress("Chi2n", jobChi2n);
01387 int ievent = 0;
01388 for (ievent=0;ievent<jobtree->GetEntries();++ievent) {
01389 jobtree->GetEntry(ievent);
01390
01391
01392
01393
01394 m_Ntracks = jobNtracks;
01395 int ntrk = 0;
01396 while (ntrk<m_Ntracks) {
01397 if (ntrk<MAXREC) {
01398 totntrk = ntrk+1;
01399 m_Nhits[ntrk] = jobNhitspertrack[ntrk];
01400 m_Pt[ntrk] = jobPt[ntrk];
01401 m_P[ntrk] = jobP[ntrk];
01402 m_nhPXB[ntrk] = jobnhPXB[ntrk];
01403 m_nhPXF[ntrk] = jobnhPXF[ntrk];
01404 m_Eta[ntrk] = jobEta[ntrk];
01405 m_Phi[ntrk] = jobPhi[ntrk];
01406 m_Chi2n[ntrk] = jobChi2n[ntrk];
01407 m_d0[ntrk] = jobd0[ntrk];
01408 m_dz[ntrk] = jobdz[ntrk];
01409 }
01410 else{
01411 edm::LogWarning("Alignment") << "[HIPAlignmentAlgorithm::fillEventwiseTree] Number of tracks in Eventwise tree exceeds MAXREC: "
01412 << m_Ntracks << " Skipping exceeding tracks.";
01413 ntrk = m_Ntracks+1;
01414 }
01415 ++ntrk;
01416 }
01417 theTree->Fill();
01418 }
01419
01420
01421 delete jobtree;
01422 delete jobfile;
01423
01424 return totntrk;
01425 }