ATLAS Offline Software
GlobalChi2AlignTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include "CLHEP/Matrix/Matrix.h"
7 #include "CLHEP/Matrix/SymMatrix.h"
8 #include "CLHEP/Matrix/DiagMatrix.h"
9 
10 #include "GaudiKernel/StatusCode.h"
11 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/AlgTool.h"
13 
14 #include "TrkTrack/Track.h"
19 
24 #include "TrkAlignEvent/AlignPar.h"
26 
28 
31 
32 #include "TTree.h"
33 #include "TFile.h"
34 #include "TMatrixD.h"
35 
36 namespace Trk {
37 
38  constexpr int MAXNCHAMBERS=50;
39  constexpr int MAXNINDICES =50*6;
40 
41  //_______________________________________________________________________
42  GlobalChi2AlignTool::GlobalChi2AlignTool(const std::string& type, const std::string& name,
43  const IInterface* parent)
45  , m_matrixTool("Trk::MatrixTool",this)
46  , m_alignModuleTool("Trk::AlignModuleTool/AlignModuleTool")
47  , m_ntracks(0)
48  , m_nmeas(0)
49  , m_nhits(0)
50  , m_chi2(0.)
51  , m_nDoF(0)
52  , m_ntuple(nullptr)
53  , m_tree(nullptr)
54  , m_run{}
55  , m_event{}
56  , m_materialOnTrack{}
57  , m_momentum{}
58  , m_nChambers(0)
59  , m_chamberIds(new int[MAXNCHAMBERS])
60  , m_nMatrixIndices(0)
61  , m_matrixIndices(new int[MAXNINDICES])
62  , m_nSecndDeriv(0)
63  , m_secndDeriv(new double[MAXNINDICES*MAXNINDICES])
64  , m_firstDeriv(new double[MAXNINDICES])
65  , m_actualSecndDeriv(new double[MAXNINDICES])
66  , m_eta{}
67  , m_phi{}
68  , m_perigee_x{}, m_perigee_y{}, m_perigee_z{}
69  , m_trackInfo{}
70  , m_bremFit{}
71  , m_bremFitSuccessful{}
72  , m_straightTrack{}
73  , m_slimmedTrack{}
74  , m_hardScatterOrKink{}
75  , m_lowPtTrack{}
76  , m_fromFiles(false)
77  {
78  declareInterface<IAlignTool>(this);
79 
80  declareProperty("MatrixTool", m_matrixTool, "tool for storing and inverting matrix");
81  declareProperty("AlignModuleTool", m_alignModuleTool);
82 
83  declareProperty("SecondDerivativeCut", m_secondDerivativeCut = -1e5);
84 
85  declareProperty("DoTree", m_doTree = false);
86  declareProperty("WriteActualSecDeriv", m_writeActualSecDeriv = false);
87 
88  declareProperty("StoreLocalDerivOnly", m_storeLocalDerivOnly = false);
89 
90  m_logStream = nullptr;
91  }
92 
93  //_______________________________________________________________________
95  {
96  ATH_MSG_DEBUG("in GlobalChi2AlignTool d'tor");
97  //delete m_tree;
98 
99  delete [] m_chamberIds;
100  delete [] m_matrixIndices;
101  delete [] m_secndDeriv;
102  delete [] m_firstDeriv;
103  delete [] m_actualSecndDeriv;
104 
105  ATH_MSG_DEBUG("done with GlobalChi2AlignTool d'tor");
106  }
107 
108  //_______________________________________________________________________
110  {
111  ATH_MSG_DEBUG("initialize() of GlobalChi2AlignTool");
112  // Get MatrixTool
113  ATH_CHECK( m_matrixTool.retrieve() );
114  ATH_CHECK(m_alignModuleTool.retrieve() );
115  return StatusCode::SUCCESS;
116  }
117 
118  //________________________________________________________________________
120  {
121  m_logStream = os;
122  m_matrixTool->setLogStream(m_logStream);
123  }
124 
125  //________________________________________________________________________
127  {
128  ATH_MSG_DEBUG("in GlobalGhi2AlignTool::firstEventInitialize()");
129 
130  int nDoF=m_alignModuleTool->nAlignParameters();
131  ATH_MSG_DEBUG("allocating matrix with "<<nDoF<<" degrees of freedom");
132 
133  if( m_matrixTool->allocateMatrix(nDoF).isFailure()) {
134  msg(MSG::FATAL)<<"Problem with allocateMatrix"<<endmsg;
135  return StatusCode::FAILURE;
136  }
137  ATH_MSG_DEBUG("done with firstEventInitialize()");
138 
139  return StatusCode::SUCCESS;
140  }
141 
142  //________________________________________________________________________
144  {
145  ATH_MSG_DEBUG("in GlobalChi2AlignTool::accumulate()");
146 
147  // prepare ROOT tree for derivatives if it doesn't exist yet
148  // Shouldn't this be in firstEventInitialize() ?
149  if (m_doTree && !m_tree && m_ntuple) {
150 
151  m_ntuple->cd();
152  m_tree=new TTree("globalChi2Deriv","globalChi2Deriv");
153  m_tree->Branch("run", &m_run, "run/I");
154  m_tree->Branch("event", &m_event, "event/I");
155  m_tree->Branch("nChambers", &m_nChambers, "nChambers/I");
156  m_tree->Branch("chamberIds", m_chamberIds, "chamberIds[nChambers]/I");
157  m_tree->Branch("nMatrixIndices", &m_nMatrixIndices, "nMatrixIndices/I");
158  m_tree->Branch("matrixIndices", m_matrixIndices, "matrixIndices[nMatrixIndices]/I");
159  m_tree->Branch("nSecndDeriv", &m_nSecndDeriv, "nSecndDeriv/I");
160  m_tree->Branch("secndDeriv", m_secndDeriv, "secndDeriv[nSecndDeriv]/D");
161  m_tree->Branch("firstDeriv", m_firstDeriv, "firstDeriv[nMatrixIndices]/D");
163  m_tree->Branch("actualSecndDeriv", m_actualSecndDeriv, "actualSecndDeriv[nMatrixIndices]/D");
164 
165  m_tree->Branch("materialOnTrack", &m_materialOnTrack, "materialOnTrack/D");
166  m_tree->Branch("momentum", &m_momentum, "momentum/D");
167  m_tree->Branch("eta", &m_eta, "eta/D");
168  m_tree->Branch("phi", &m_phi, "phi/D");
169  m_tree->Branch("perigee_x", &m_perigee_x, "perigee_x/D");
170  m_tree->Branch("perigee_y", &m_perigee_y, "perigee_y/D");
171  m_tree->Branch("perigee_z", &m_perigee_z, "perigee_z/D");
172  m_tree->Branch("trackInfo", &m_trackInfo, "trackInfo/I");
173  m_tree->Branch("bremFit", &m_bremFit, "bremFit/I");
174  m_tree->Branch("bremFitSuccessful", &m_bremFitSuccessful, "bremFitSuccessful/I");
175  m_tree->Branch("straightTrack", &m_straightTrack, "straightTrack/I");
176  m_tree->Branch("slimmedTrack", &m_slimmedTrack, "slimmedTrack/I");
177  m_tree->Branch("hardScatterOrKink", &m_hardScatterOrKink, "hardScatterOrKink/I");
178  m_tree->Branch("lowPtTrack", &m_lowPtTrack, "lowPtTrack/I");
179  }
180 
181 
182  // check if the track belongs to a vertex. If yes, retrieve the relevant objects:
183  bool fullVertex = false;
184  AlignVertex * ptrVertex = alignTrack->getVtx();
185  const Amg::Vector3D * ptrPosition = nullptr;
186  const AmgSymMatrix(3) * ptrCovariance = nullptr;
187  const std::vector<AlignModuleVertexDerivatives> * ptrX = nullptr;
189  if( ptrVertex ) {
190  fullVertex = true;
191  ptrPosition = ptrVertex->position();
192  ptrCovariance = ptrVertex->covariance();
193  ptrX = ptrVertex->derivatives();
194  vtxType = ptrVertex->type();
195 
196  // check if pointers are valid
197  if ( !ptrPosition || !ptrCovariance || !ptrX || vtxType<AlignVertex::Refitted ) {
198  msg(MSG::ERROR)<<"something missing from alignVertex!"<<endmsg;
199  if (!ptrPosition) msg(MSG::ERROR)<<"no fitted position!"<<endmsg;
200  if (!ptrCovariance) msg(MSG::ERROR)<<"no covariance!"<<endmsg;
201  if (!ptrX) msg(MSG::ERROR)<<"no link to the X object!"<<endmsg;
202  if (vtxType<AlignVertex::Refitted) msg(MSG::ERROR)<<"Vertex type = "<< vtxType << endmsg;
203  return false;
204  }
205  }
206 
207 
208 
209  // reset tree variables
210  m_nChambers=0;
212 
213  // get pointers so we can reuse them if they're valid
214  const Amg::SymMatrixX * ptrWeights = alignTrack->weightMatrix();
215  const Amg::SymMatrixX * ptrWeightsFD = alignTrack->weightMatrixFirstDeriv();
216  const Amg::VectorX * ptrResiduals = alignTrack->residualVector();
217  const std::vector<AlignModuleDerivatives> * ptrDerivs = alignTrack->derivatives();
218 
219  // check if pointers are valid
220  if (!ptrWeights || !ptrWeightsFD || !ptrResiduals || !ptrDerivs) {
221  msg(MSG::ERROR)<<"something missing from alignTrack!"<<endmsg;
222  if (!ptrWeights) msg(MSG::ERROR)<<"no weights!"<<endmsg;
223  if (!ptrWeightsFD) msg(MSG::ERROR)<<"no weights for first deriv!"<<endmsg;
224  if (!ptrResiduals) msg(MSG::ERROR)<<"no residuals!"<<endmsg;
225  if (!ptrDerivs) msg(MSG::ERROR)<<"no derivatives!"<<endmsg;
226  return false;
227  }
228 
229  // get weight matrices
230  const Amg::SymMatrixX& weights = *ptrWeights;
231  const Amg::SymMatrixX& weightsFirstDeriv = *ptrWeightsFD;
232  ATH_MSG_VERBOSE("weights="<<weights);
233  ATH_MSG_VERBOSE("weightsFirstDeriv="<<weightsFirstDeriv);
234 
235  // get vectors
236  const Amg::VectorX& residuals = *ptrResiduals;
237  std::vector<AlignModuleDerivatives> derivatives = *ptrDerivs;
238  const std::vector<AlignModuleDerivatives>* derivativeErr = alignTrack->derivativeErr();
239  const std::vector<std::pair<AlignModule*,std::vector<double> > >* secDerivs = alignTrack->actualSecondDerivatives();
240 
241 
242 
243  std::vector<AlignPar*> allAlignPars;
244  std::vector<Amg::VectorX*> allDerivatives;
245  std::vector<const Amg::VectorX*> allDerivativeErr;
246  std::vector<double> allActualSecDeriv;
247  int WSize(weights.cols());
248  Amg::MatrixX matrix_F(3,WSize );
249  matrix_F.setZero();
250 
251  // get all alignPars and all derivatives
252  msg(MSG::DEBUG) << "accumulate: The derivative vector size is " << derivatives.size() << endmsg;
253  std::vector<AlignModuleDerivatives>::iterator derivIt = derivatives.begin();
254  std::vector<AlignModuleDerivatives>::iterator derivIt_end = derivatives.end();
255 
256  std::vector<AlignModuleDerivatives>::const_iterator derivErrIt;
257  std::vector<std::pair<AlignModule*,std::vector<double> > >::const_iterator secDerivIt;
258  if (derivativeErr) derivErrIt=derivativeErr->begin();
259  if (secDerivs) secDerivIt=secDerivs->begin();
260  for ( ; derivIt!=derivIt_end ; ++derivIt) {
261 
262  // get AlignModule
263  AlignModule* module=derivIt->first;
264 
265  // increment track counter
266  module->addTrack();
267  ATH_MSG_DEBUG("add track to module "<<module->identify().get_identifier32().get_compact());
268  module->addNDoF(alignTrack->fitQuality()->numberDoF());
269  module->addTrackChi2(alignTrack->fitQuality()->chiSquared());
270 
271  // tree variables
273  m_chamberIds[m_nChambers]=module->identify().get_identifier32().get_compact();
274  m_nChambers++;
275  }
276  else ATH_MSG_WARNING("number of chambers exceeded maximum");
277 
278  // get alignment parameters
279  std::vector<Amg::VectorX>& deriv_vec = derivIt->second;
280  ATH_MSG_DEBUG( "accumulate: The deriv_vec size is " << deriv_vec.size() );
281  DataVector<AlignPar>* alignPars = m_alignModuleTool->getAlignPars(module);
282  int nModPars = alignPars->size();
283  if (nModPars != (int)deriv_vec.size() && (nModPars+3) != (int)deriv_vec.size()) {
284  ATH_MSG_ERROR("accumulate: wrong size of the deriv_vec!");
285  return false;
286  }
287  for (int i=0;i<3;i++) {
288  for (int j=0;j<WSize;j++) {
289  matrix_F(i,j) += deriv_vec[nModPars+i][j]; // in the derivIT loop the complete F matrix is built
290  }
291  }
292 
293  // get derivatives and derivative errors
294  for (int i=0;i<nModPars;i++) {
295  ATH_MSG_DEBUG("align par for module "<<(*alignPars)[i]->alignModule()->identify());
296  allAlignPars.push_back((*alignPars)[i]);
297  allDerivatives.push_back(&deriv_vec[i]);
298  if (derivativeErr)
299  allDerivativeErr.push_back(&(derivErrIt->second)[i]);
300  if (secDerivs)
301  allActualSecDeriv.push_back((secDerivIt->second)[i]);
302  }
303  if (derivativeErr) ++derivErrIt;
304  if (secDerivs) ++secDerivIt;
305  }
306 
307  // increment hit counters in modules
308  AlignTSOSCollection::iterator iatsos = alignTrack->firstAtsos();
309  AlignTSOSCollection::iterator iatsos_last = alignTrack->lastAtsos();
310  for (; iatsos != iatsos_last; ++iatsos)
311  if ((*iatsos)->module())
312  (*iatsos)->module()->addHit();
313 
314  // determine all derivatives and set in matrix tool
315  int nAllPars = allAlignPars.size();
316  m_nSecndDeriv=0;
317  for (int ipar=0;ipar<nAllPars;ipar++) {
318 
319  // calculate first derivative
320  Amg::MatrixX derivativesT = (*allDerivatives[ipar]).transpose();
321  ATH_MSG_DEBUG("derivativesT (size "<<derivativesT.cols()<<"): "<<derivativesT);
322 
323  Amg::MatrixX RHM= weightsFirstDeriv * residuals;
324  ATH_MSG_DEBUG("RHM: "<<RHM);
325 
326  // get global parameter index
327  int index1 = allAlignPars[ipar]->index();
328 
329  Amg::MatrixX firstderivM = 2.* derivativesT * RHM;
330  ATH_MSG_DEBUG("firstderivM for i="<<index1 <<firstderivM);
331  if (firstderivM(0,0)==0.) {
332  ATH_MSG_DEBUG("firstderivM[0][0] is zero : "<<firstderivM(0,0));
333  continue;
334  }
335 
336 
337 
338  // tree variables
341  ATH_MSG_DEBUG("ipar: "<<ipar<<", m_nMatrixIndices="<<m_nMatrixIndices);
342  if (m_writeActualSecDeriv) {
343  ATH_MSG_DEBUG("alActualSecDeriv size: "<<allActualSecDeriv.size());
344  m_actualSecndDeriv[m_nMatrixIndices]=allActualSecDeriv[ipar];
345  }
347  }
348 
349  // get module index
350  int imodule1 = allAlignPars[ipar]->alignModule()->identifyHash();
351 
352  // calculate second derivatives
353  bool goodSecondDeriv = true;
354  for (int jpar=ipar;jpar<nAllPars;jpar++) {
355 
356  // get global parameter index
357  int index2=allAlignPars[jpar]->index();
358 
359  // get module index
360  int imodule2 = allAlignPars[jpar]->alignModule()->identifyHash();
361 
362  // if only local Chi2 method is used, the 2nd derivatives (correlations)
363  // between different modules don't need to be calculated/stored
365  if (imodule1 != imodule2)
366  continue;
367 
368  Amg::MatrixX RHM2 = weights * (*allDerivatives[jpar]);
369  Amg::MatrixX secondderivM = 2. * derivativesT * RHM2;
370 
371  // if second derivative on diagonal is below cut
372  // ignore the whole row/column
373  if (jpar==ipar && secondderivM(0,0)<0.) {
374  ATH_MSG_WARNING("second deriv < 0 !!!");
375  }
376  if (jpar==ipar && secondderivM(0,0)<m_secondDerivativeCut) {
377  ATH_MSG_DEBUG("secondderivM[0][0] is below threshold : "<<
378  secondderivM(0,0)<<" < "<<m_secondDerivativeCut);
379  goodSecondDeriv = false;
380  break;
381  }
382 
383  ATH_MSG_DEBUG("secondderivM for i "<<index1<<", j "<<index2<<": "<<secondderivM(0,0));
384  m_matrixTool->addSecondDerivative(index1, index2, secondderivM(0,0));
385 
386  // store derivatives and indices for ntuple
387  m_secndDeriv[m_nSecndDeriv]=secondderivM(0,0);
388  m_nSecndDeriv++;
389  }
390 
391  // if second derivative on diagonal didn't pass the cut,
392  // ignore also the first derivative
393  if (goodSecondDeriv) {
394  m_matrixTool->addFirstDerivative(index1, firstderivM(0,0));
395  m_firstDeriv[ipar]=firstderivM(0,0);
396  }
397  else
398  m_firstDeriv[ipar]=-999.;
399  }
400 
401 
402 
403 
404  if( fullVertex ) { // this track is associated to a vertex.
405 
406 
407  ATH_MSG_DEBUG( "accumulate: Contribution from the fullVTX will be added " );
408 
409  Amg::MatrixX RHM = (*ptrCovariance) * (matrix_F) * (weightsFirstDeriv * residuals);
410 
411  std::vector<AlignPar*> vtxAlignPars;
412  std::vector<const Amg::VectorX*> vtxDerivatives;
413 
414  // get all alignPars and all derivatives
415  msg(MSG::DEBUG) << "accumulate: The Vertex derivative vector size is " << ptrX->size() << endmsg;
416  std::vector<AlignModuleVertexDerivatives>::const_iterator XIt = ptrX->begin();
417  std::vector<AlignModuleVertexDerivatives>::const_iterator XIt_end = ptrX->end();
418 
419  for ( ; XIt!=XIt_end ; ++XIt) {
420 
421  // get AlignModule
422  const AlignModule* module=XIt->first;
423 
424  // get alignment parameters
425  const std::vector<Amg::VectorX>& X_vec = XIt->second;
426  msg(MSG::DEBUG) << "accumulate: The X_vec size is " << X_vec.size() << endmsg;
427  DataVector<AlignPar>* alignPars = m_alignModuleTool->getAlignPars(module);
428  int nModPars = alignPars->size();
429  if (nModPars != (int)X_vec.size()) {
430  ATH_MSG_ERROR("accumulate: wrong size of the X_vec!");
431  return false;
432  }
433 
434  // get derivatives and derivative errors
435  for (int i=0;i<nModPars;i++) {
436  ATH_MSG_DEBUG("align par for module "<<(*alignPars)[i]->alignModule()->identify());
437  vtxAlignPars.push_back((*alignPars)[i]);
438  vtxDerivatives.push_back(&X_vec[i]);
439  }
440 
441  }
442 
443  // bit responsible for the constraint:
444  int ierr(0);
445  AmgSymMatrix(3) Qinv;
446  Qinv.setZero();
447  Amg::Vector3D vtemp;
448  vtemp.setZero();
449  if( vtxType==AlignVertex::Refitted && ptrVertex->Constrained() && ptrVertex->Qmatrix()->determinant() > 1.0e-24 ) { // just my guess sigma>0.1 micron ???) only once per vertex!
450 
451  bool invertible;
452  double determinant;
453  ptrVertex->Qmatrix()->computeInverseAndDetWithCheck(Qinv,determinant,invertible);
454 
455  if(!invertible) {
456  std::cout <<"accumulate: Q inversion failed. CLHEP status flag = "<<ierr<< std::endl;
457  return false;
458  }
459  vtemp(0) = -ptrVertex->originalPosition()->x()+(*(ptrVertex->Vvector()))(0);
460  vtemp(1) = -ptrVertex->originalPosition()->y()+(*(ptrVertex->Vvector()))(1);
461  vtemp(2) = -ptrVertex->originalPosition()->z()+(*(ptrVertex->Vvector()))(2);
462  }
463 
464 
465  // determine all derivatives and set in matrix tool
466  int nvtxPars = vtxAlignPars.size();
467  for (int ipar=0;ipar<nvtxPars;ipar++) {
468 
469  // calculate first derivative
470  Amg::MatrixX derivativesT = (*vtxDerivatives[ipar]).transpose();
471  Amg::MatrixX firstderivM = -2.* derivativesT * RHM; // -2 seems correct :)
472 
473  // bit responsible for the constraint:
474  if( vtxType==AlignVertex::Refitted && ptrVertex->Constrained() && ptrVertex->Qmatrix()->determinant() > 1.0e-24 ) { // just my guess sigma>0.1 micron ???) only once per vertex!
475  firstderivM += 2.* derivativesT * ((*ptrCovariance) * Qinv) * vtemp;
476  }
477 
478  // get global parameter index
479  int index1 = vtxAlignPars[ipar]->index();
480 
481  ATH_MSG_DEBUG("vtx firstderivM for i="<<index1 <<firstderivM);
482 
483 
484  if( vtxType==AlignVertex::Refitted ) { // only once per vertex!
485  ATH_MSG_DEBUG("accumulate: Filling second derivative for this vertex... ");
486  for (int jpar=0;jpar<nvtxPars;jpar++) {
487 
488  // calculate second derivative
489  Amg::MatrixX secondderivM = -1.* derivativesT * (*ptrCovariance) * (*vtxDerivatives[jpar]); // -1 is the right factor :)
490 
491  // bit responsible for the constraint:
492  if( ptrVertex->Constrained() && ptrVertex->Qmatrix()->determinant() > 1.0e-24 ) { // just my guess sigma>0.1 micron ???)
493  secondderivM += 2.* derivativesT * ((*ptrCovariance) * Qinv * (*ptrCovariance)) * (*vtxDerivatives[jpar]);
494  }
495 
496  // get global parameter index
497  int index2=vtxAlignPars[jpar]->index();
498 
499  if( index1<=index2 ) {
500  ATH_MSG_DEBUG("vtx secondderivM for i="<<index1<<", j="<<index2<<": "<<secondderivM(0,0));
501  m_matrixTool->addSecondDerivative(index1, index2, secondderivM(0,0));
502  }
503 
504  }
505  ptrVertex->setType(AlignVertex::Accumulated); // only once per vertex!
506  }
507 
508  m_matrixTool->addFirstDerivative(index1, firstderivM(0,0));
509 
510  }
511 
512  }
513 
514 
515 
516 
517 
518  // fill tree
519  if (m_tree) {
520 
521  // get tree variables
522  const xAOD::EventInfo* currentEvent;
523  StatusCode sc = evtStore()->retrieve(currentEvent);
524  if (sc==StatusCode::SUCCESS) {
525  m_run = currentEvent->runNumber();
526  m_event = currentEvent->eventNumber();
527  }
528  else {
529  m_run = -999;
530  m_event = -999;
531  }
532 
534  ATH_MSG_DEBUG("materialOnTrack: "<<m_materialOnTrack);
535 
536  double pT=alignTrack->originalTrack()->perigeeParameters()->pT();
537  double eta=alignTrack->originalTrack()->perigeeParameters()->eta();
538  m_momentum=std::fabs(pT)*std::sqrt(1.+sinh(eta)*sinh(eta));
539  m_eta=eta;
540  m_phi=alignTrack->originalTrack()->perigeeParameters()->momentum().phi();
541 
542  Amg::Vector3D pos=alignTrack->originalTrack()->perigeeParameters()->position();
543  m_perigee_x=pos.x();
544  m_perigee_y=pos.y();
545  m_perigee_z=pos.z();
546 
547  const TrackInfo info=alignTrack->originalTrack()->info();
548  m_trackInfo = (int)info.trackFitter();
549  m_bremFit = (int)info.trackProperties(Trk::TrackInfo::BremFit);
554  m_lowPtTrack = (int)info.trackProperties(Trk::TrackInfo::LowPtTrack);
555 
556  m_ntuple->cd();
557  m_tree->Fill();
558  }
559 
560  // increment total number of accumulated tracks, hits and the total chi2
561  m_ntracks++;
562  m_nmeas += alignTrack->nAlignTSOSMeas();
563  m_nhits += alignTrack->alignTSOSCollection()->size();
564  m_chi2 += alignTrack->fitQuality()->chiSquared();
565  m_nDoF += alignTrack->fitQuality()->numberDoF();
566 
567  if(m_ntracks%50==0)
568  ATH_MSG_INFO(">>>> Number of accumulated tracks: "<<m_ntracks);
569 
570  return true;
571  }
572 
573  //________________________________________________________________________
575  {
576  ATH_MSG_DEBUG("in accumulate");
577  bool success = m_matrixTool->accumulateFromFiles();
578 
579  if (!success) {
580  ATH_MSG_FATAL("failure in MatrixTool accumulateFromFiles");
581  return false;
582  }
583 
584  m_fromFiles = true;
585 
586  return true;
587  }
588 
589  //________________________________________________________________________
591  {
592  if(m_fromFiles)
593  m_nhits = m_matrixTool->nHits();
594  else {
595  m_matrixTool->setNTracks(m_ntracks);
596  m_matrixTool->setNMeasurements(m_nmeas);
597  m_matrixTool->setNHits(m_nhits);
598  }
599 
600  // print statistics
601  if(m_logStream) {
602  *m_logStream<<"*************************************************************"<<std::endl;
603  *m_logStream<<"****** Global Chi2 alignment solving ******"<<std::endl;
604  *m_logStream<<"*"<<std::endl;
605  if(!m_fromFiles)
606  *m_logStream<<"* number of accumulated tracks: "<<m_ntracks<<std::endl;
607  *m_logStream<<"* number of processed hits: "<<m_nhits<<std::endl;
608  if(!m_fromFiles) {
609  *m_logStream<<"* number of used measurements: "<<m_nmeas<<std::endl;
610  *m_logStream<<"* total chi2 / nDoF: "<<m_chi2<<" / "<<m_nDoF<<std::endl;
611  }
612  *m_logStream<<"*"<<std::endl;
613  *m_logStream<<"* number of alignable objects: "<<m_alignModuleTool->alignModules1D()->size()<<std::endl;
614  *m_logStream<<"* total number of alignment DoFs: "<<m_alignModuleTool->nAlignParameters()<<std::endl;
615  *m_logStream<<"*"<<std::endl;
616  }
617 
618  ATH_MSG_INFO(">>>> -----------------------------------------------");
619  ATH_MSG_INFO(">>>> Total number of accumulated tracks: "<<m_ntracks);
620  ATH_MSG_INFO(">>>> -----------------------------------------------");
621 
622  ATH_MSG_DEBUG("calling matrixTool->solve()");
623  if (m_matrixTool->solve()>0)
624  return StatusCode::SUCCESS;
625 
626  return StatusCode::FAILURE;
627  }
628 
629  //________________________________________________________________________
631  {
632  ATH_MSG_DEBUG("writing tree");
633  int success=1;
634  if (m_tree) {
635  m_ntuple->cd();
636  success = m_tree->Write();
637  }
638  return success>0 ? StatusCode::SUCCESS : StatusCode::FAILURE;
639  }
640 
641  //_______________________________________________________________________
643  {
644  ATH_MSG_DEBUG("finalize() of GlobalChi2AlignTool");
645  return StatusCode::SUCCESS;
646  }
647 
648  //________________________________________________________________________
650  {
651 
652  double materialOnTrack(0.);
653 
654  const Trk::TrackStates* states = track->trackStateOnSurfaces();
655  if ( !states ) {
656  ATH_MSG_WARNING("no states");
657  return 0;
658  }
659 
660  // loop over TSOSs, find MDT hits, and mark them as outliers:
662  Trk::TrackStates::const_iterator tsit_end = states->end();
663  ATH_MSG_DEBUG("looping over TSOS");
664 
665  for ( ; tsit!=tsit_end ; ++tsit ) {
666  const Trk:: MaterialEffectsBase* materialEffects = (*tsit)->materialEffectsOnTrack();
667  if (materialEffects) {
668  const Trk::MaterialEffectsOnTrack* meot=dynamic_cast<const Trk::MaterialEffectsOnTrack*>(materialEffects);
669  if (meot) {
670  materialOnTrack += meot->thicknessInX0();
671  ATH_MSG_DEBUG("new materialOnTrack: "<<materialOnTrack);
672  }
673  else {
674  ATH_MSG_DEBUG("not material on track");
675  }
676  }
677  }
678 
679  return materialOnTrack;
680  }
681 
682 } // end of namespace
683 
Trk::IAlignTool::m_logStream
std::ostream * m_logStream
logfile output stream
Definition: IAlignTool.h:54
grepfile.info
info
Definition: grepfile.py:38
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
Trk::GlobalChi2AlignTool::m_fromFiles
bool m_fromFiles
Definition: GlobalChi2AlignTool.h:132
Trk::GlobalChi2AlignTool::m_lowPtTrack
int m_lowPtTrack
Definition: GlobalChi2AlignTool.h:130
Trk::AlignVertex
Definition: AlignVertex.h:39
CalculateHighPtTerm.pT
pT
Definition: ICHEP2016/CalculateHighPtTerm.py:57
Trk::AlignVertex::AlignVertexType
AlignVertexType
Definition: AlignVertex.h:43
Trk::TrackInfo
Contains information about the 'fitter' of this track.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:32
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
Trk::GlobalChi2AlignTool::m_matrixIndices
int * m_matrixIndices
Definition: GlobalChi2AlignTool.h:114
Trk::Track::fitQuality
const FitQuality * fitQuality() const
return a pointer to the fit quality const-overload
Amg::VectorX
Eigen::Matrix< double, Eigen::Dynamic, 1 > VectorX
Dynamic Vector - dynamic allocation.
Definition: EventPrimitives.h:30
Amg::MatrixX
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Definition: EventPrimitives.h:27
Trk::GlobalChi2AlignTool::setLogStream
void setLogStream(std::ostream *os)
sets the output stream for the logfile
Definition: GlobalChi2AlignTool.cxx:119
TrackParameters.h
Trk::GlobalChi2AlignTool::m_firstDeriv
double * m_firstDeriv
Definition: GlobalChi2AlignTool.h:117
MeasurementBase.h
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
xAOD::EventInfo_v1::eventNumber
uint64_t eventNumber() const
The current event's event number.
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
Trk::GlobalChi2AlignTool::m_secondDerivativeCut
double m_secondDerivativeCut
Definition: GlobalChi2AlignTool.h:99
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Trk::Track::info
const TrackInfo & info() const
Returns a const ref to info of a const tracks.
Trk::TrackInfo::BremFitSuccessful
@ BremFitSuccessful
A brem fit was performed on this track and this fit was successful.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:81
Trk::AlignVertex::derivatives
const std::vector< AlignModuleVertexDerivatives > * derivatives() const
The Amg::VectorX is a vector of first-derivatives of the alignTSOS on the alignTrack w....
Definition: AlignVertex.h:95
Trk::GlobalChi2AlignTool::m_perigee_z
double m_perigee_z
Definition: GlobalChi2AlignTool.h:123
Trk::AlignVertex::Unknown
@ Unknown
default type
Definition: AlignVertex.h:44
Trk::TrackInfo::LowPtTrack
@ LowPtTrack
A LowPt track.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:93
Trk::GlobalChi2AlignTool::m_chamberIds
int * m_chamberIds
Definition: GlobalChi2AlignTool.h:112
Trk::GlobalChi2AlignTool::GlobalChi2AlignTool
GlobalChi2AlignTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor.
Definition: GlobalChi2AlignTool.cxx:42
Trk::AlignModule
Definition: AlignModule.h:45
Trk::AlignTrack::weightMatrixFirstDeriv
const Amg::SymMatrixX * weightMatrixFirstDeriv() const
First deriv weight matrix can be either W from Si alignment (see Eqn.
Definition: AlignTrack.h:161
xAOD::identify
Identifier identify(const UncalibratedMeasurement *meas)
Returns the associated identifier.
Definition: MuonSpectrometer/MuonPhaseII/Event/xAOD/xAODMuonPrepData/Root/UtilFunctions.cxx:61
Trk::MaterialEffectsBase::thicknessInX0
double thicknessInX0() const
returns the actually traversed material .
Trk::GlobalChi2AlignTool::m_perigee_y
double m_perigee_y
Definition: GlobalChi2AlignTool.h:122
Trk::AlignVertex::Constrained
bool Constrained() const
Definition: AlignVertex.h:123
Trk::AlignVertex::setType
void setType(AlignVertexType type)
Definition: AlignVertex.h:86
Trk::AlignVertex::Accumulated
@ Accumulated
accumulated by the GX algorithm.
Definition: AlignVertex.h:47
Trk::GlobalChi2AlignTool::m_nChambers
int m_nChambers
Definition: GlobalChi2AlignTool.h:111
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Trk::MaterialEffectsBase
base class to integrate material effects on Trk::Track in a flexible way.
Definition: MaterialEffectsBase.h:35
Trk::GlobalChi2AlignTool::m_bremFit
int m_bremFit
Definition: GlobalChi2AlignTool.h:125
Trk::GlobalChi2AlignTool::finalize
virtual StatusCode finalize()
finalize
Definition: GlobalChi2AlignTool.cxx:642
Trk::GlobalChi2AlignTool::m_materialOnTrack
double m_materialOnTrack
Definition: GlobalChi2AlignTool.h:109
Trk::GlobalChi2AlignTool::m_trackInfo
int m_trackInfo
Definition: GlobalChi2AlignTool.h:124
xAOD::EventInfo_v1::runNumber
uint32_t runNumber() const
The current event's run number.
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AlignTSOS.h
Trk::GlobalChi2AlignTool::m_doTree
bool m_doTree
Definition: GlobalChi2AlignTool.h:100
Trk::GlobalChi2AlignTool::m_phi
double m_phi
Definition: GlobalChi2AlignTool.h:120
Trk::GlobalChi2AlignTool::m_chi2
double m_chi2
total chi2
Definition: GlobalChi2AlignTool.h:96
Trk::GlobalChi2AlignTool::m_momentum
double m_momentum
Definition: GlobalChi2AlignTool.h:110
Track.h
Trk::AlignVertex::position
const Amg::Vector3D * position() const
get the vertex position and covariance
Definition: AlignVertex.h:89
MaterialEffectsOnTrack.h
Trk::TrackInfo::StraightTrack
@ StraightTrack
A straight track.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:84
Trk::AmgSymMatrix
AmgSymMatrix(5) &GXFTrackState
Definition: GXFTrackState.h:156
Trk::GlobalChi2AlignTool::m_matrixTool
ToolHandle< Trk::IMatrixTool > m_matrixTool
Pointer to MatrixTool, used to write to and solve matrix.
Definition: GlobalChi2AlignTool.h:84
Trk::TrackInfo::SlimmedTrack
@ SlimmedTrack
A slimmed track.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:87
python.PyAthena.module
module
Definition: PyAthena.py:131
Trk::AlignTrack::derivativeErr
const std::vector< AlignModuleDerivatives > * derivativeErr() const
The Amg::VectorX is a vector of errors in first-derivatives of the alignTSOS on the alignTrack w....
Definition: AlignTrack.h:140
Trk::MaterialEffectsOnTrack
represents the full description of deflection and e-loss of a track in material.
Definition: MaterialEffectsOnTrack.h:40
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
Trk::AlignVertex::originalPosition
const Amg::Vector3D * originalPosition() const
Definition: AlignVertex.h:79
Trk::GlobalChi2AlignTool::m_alignModuleTool
ToolHandle< Trk::IAlignModuleTool > m_alignModuleTool
Pointer to AlignModuleTool.
Definition: GlobalChi2AlignTool.h:87
AlignVertex.h
Trk::AlignTrack::derivatives
const std::vector< AlignModuleDerivatives > * derivatives() const
The Amg::VectorX is a vector of first-derivatives of the alignTSOS on the alignTrack w....
Definition: AlignTrack.h:133
AlignTrack.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Trk::index1
@ index1
Definition: BoundarySurfaceFace.h:48
Trk::AlignTrack::firstAtsos
AlignTSOSCollection::const_iterator firstAtsos() const
retrieve iterator pointer to first element in collection
Definition: AlignTrack.h:279
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
urldecode::states
states
Definition: urldecode.h:39
Trk::GlobalChi2AlignTool::m_nDoF
unsigned int m_nDoF
number of degrees of freedom
Definition: GlobalChi2AlignTool.h:97
Trk::GlobalChi2AlignTool::m_slimmedTrack
int m_slimmedTrack
Definition: GlobalChi2AlignTool.h:128
test_pyathena.parent
parent
Definition: test_pyathena.py:15
Trk::MAXNCHAMBERS
constexpr int MAXNCHAMBERS
Definition: GlobalChi2AlignTool.cxx:38
Trk::GlobalChi2AlignTool::m_hardScatterOrKink
int m_hardScatterOrKink
Definition: GlobalChi2AlignTool.h:129
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::GlobalChi2AlignTool::accumulate
bool accumulate(AlignTrack *alignTrack)
accumulates information from an AlignTrack
Definition: GlobalChi2AlignTool.cxx:143
Trk::AlignTrack::getVtx
const AlignVertex * getVtx() const
set and get pointer to the associated vertex
Definition: AlignTrack.h:188
Trk::TrackInfo::BremFit
@ BremFit
A brem fit was performed on this track.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:78
Trk::AlignTrack::residualVector
const Amg::VectorX * residualVector() const
Vector of residuals of the alignTSOS on the alignTrack, to be set by AlignTrackDresser.
Definition: AlignTrack.h:150
Trk::GlobalChi2AlignTool::initialize
virtual StatusCode initialize()
initialize
Definition: GlobalChi2AlignTool.cxx:109
Trk::GlobalChi2AlignTool::m_tree
TTree * m_tree
Definition: GlobalChi2AlignTool.h:106
DataVector< AlignPar >
AlignModuleList.h
ReadFromCoolCompare.os
os
Definition: ReadFromCoolCompare.py:231
Trk::AlignVertex::Vvector
const Amg::Vector3D * Vvector() const
Definition: AlignVertex.h:119
Trk::GlobalChi2AlignTool::m_ntracks
unsigned int m_ntracks
number of accumulated tracks
Definition: GlobalChi2AlignTool.h:93
Trk::AlignTrack::weightMatrix
const Amg::SymMatrixX * weightMatrix() const
Weight matrix is W from Si alignment (see Eqn.
Definition: AlignTrack.h:156
Trk::GlobalChi2AlignTool::m_ntuple
TFile * m_ntuple
output ntuple
Definition: GlobalChi2AlignTool.h:105
Trk::AlignTrack::actualSecondDerivatives
const std::vector< std::pair< AlignModule *, std::vector< double > > > * actualSecondDerivatives() const
The Amg::VectorX is a vector of first-derivatives of the alignTSOS on the alignTrack w....
Definition: AlignTrack.h:146
Trk::Track::perigeeParameters
const Perigee * perigeeParameters() const
return Perigee.
Definition: Tracking/TrkEvent/TrkTrack/src/Track.cxx:163
Trk::MAXNINDICES
constexpr int MAXNINDICES
Definition: GlobalChi2AlignTool.cxx:39
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::GlobalChi2AlignTool::firstEventInitialize
virtual StatusCode firstEventInitialize()
sets up matrix
Definition: GlobalChi2AlignTool.cxx:126
Trk::index2
@ index2
Definition: BoundarySurfaceFace.h:49
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
Trk::GlobalChi2AlignTool::~GlobalChi2AlignTool
virtual ~GlobalChi2AlignTool()
Virtual destructor.
Definition: GlobalChi2AlignTool.cxx:94
Trk::GlobalChi2AlignTool::m_actualSecndDeriv
double * m_actualSecndDeriv
Definition: GlobalChi2AlignTool.h:118
AlignModule.h
Trk::TrackInfo::HardScatterOrKink
@ HardScatterOrKink
A track with a kink or a hard scatter.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/TrackInfo.h:90
weights
Definition: herwig7_interface.h:44
Trk::GlobalChi2AlignTool::getMaterialOnTrack
double getMaterialOnTrack(const Trk::Track *track)
Definition: GlobalChi2AlignTool.cxx:649
Trk::GlobalChi2AlignTool::m_nhits
unsigned int m_nhits
number of accumulated hits
Definition: GlobalChi2AlignTool.h:95
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
Trk::AlignTrack::nAlignTSOSMeas
int nAlignTSOSMeas() const
number of alignTSOS (including scatterers if included on AlignTrack
Definition: AlignTrack.h:128
Trk::AlignTrack::originalTrack
const Track * originalTrack() const
retrieve pointer to original track
Definition: AlignTrack.h:90
Trk::AlignTrack
Definition: AlignTrack.h:41
EventInfo.h
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
Trk::GlobalChi2AlignTool::m_nmeas
unsigned int m_nmeas
number of accumulated measurements
Definition: GlobalChi2AlignTool.h:94
GlobalChi2AlignTool.h
Trk::AlignTrack::alignTSOSCollection
const AlignTSOSCollection * alignTSOSCollection() const
returns collection of alignTSOS
Definition: AlignTrack.h:267
Trk::GlobalChi2AlignTool::m_event
int m_event
Definition: GlobalChi2AlignTool.h:108
Trk::GlobalChi2AlignTool::m_secndDeriv
double * m_secndDeriv
Definition: GlobalChi2AlignTool.h:116
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::GlobalChi2AlignTool::m_storeLocalDerivOnly
bool m_storeLocalDerivOnly
Definition: GlobalChi2AlignTool.h:102
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
DEBUG
#define DEBUG
Definition: page_access.h:11
Trk::GlobalChi2AlignTool::m_run
int m_run
Definition: GlobalChi2AlignTool.h:107
AthCommonMsg< AlgTool >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
IAlignModuleTool.h
Trk::GlobalChi2AlignTool::m_writeActualSecDeriv
bool m_writeActualSecDeriv
Definition: GlobalChi2AlignTool.h:101
Trk::GlobalChi2AlignTool::m_eta
double m_eta
Definition: GlobalChi2AlignTool.h:119
AlignPar.h
Trk::FitQuality::chiSquared
double chiSquared() const
returns the of the overall track fit
Definition: FitQuality.h:56
Trk::GlobalChi2AlignTool::fillNtuple
StatusCode fillNtuple()
writes tree to ntuple
Definition: GlobalChi2AlignTool.cxx:630
Trk::GlobalChi2AlignTool::m_nMatrixIndices
int m_nMatrixIndices
Definition: GlobalChi2AlignTool.h:113
Trk::GlobalChi2AlignTool::accumulateFromFiles
bool accumulateFromFiles()
accumulates information from binary files
Definition: GlobalChi2AlignTool.cxx:574
Trk::FitQuality::numberDoF
int numberDoF() const
returns the number of degrees of freedom of the overall track or vertex fit as integer
Definition: FitQuality.h:60
Trk::GlobalChi2AlignTool::m_perigee_x
double m_perigee_x
Definition: GlobalChi2AlignTool.h:121
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
AthAlgTool
Definition: AthAlgTool.h:26
Trk::AlignVertex::Refitted
@ Refitted
normally refitted, without adding any pseudo-measurement
Definition: AlignVertex.h:46
Trk::AlignVertex::type
AlignVertexType type() const
get and set the refit type
Definition: AlignVertex.h:85
Trk::GlobalChi2AlignTool::solve
virtual StatusCode solve()
solves for alignment parameters
Definition: GlobalChi2AlignTool.cxx:590
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrackStateOnSurface.h
Trk::GlobalChi2AlignTool::m_nSecndDeriv
int m_nSecndDeriv
Definition: GlobalChi2AlignTool.h:115
Trk::GlobalChi2AlignTool::m_bremFitSuccessful
int m_bremFitSuccessful
Definition: GlobalChi2AlignTool.h:126
Trk::GlobalChi2AlignTool::m_straightTrack
int m_straightTrack
Definition: GlobalChi2AlignTool.h:127
Trk::AlignTrack::lastAtsos
AlignTSOSCollection::const_iterator lastAtsos() const
returns iterator pointer to last element in collection
Definition: AlignTrack.h:280
Amg::SymMatrixX
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > SymMatrixX
Definition: EventPrimitives.h:28