36 const std::string& name,
37 const IInterface* parent)
42 declareInterface<IDerivCalcTool>(
this);
57 msg(MSG::DEBUG) <<
"in ShiftingDerivCalcTool initialize()"<<
endmsg;
78 return StatusCode::SUCCESS;
96 return StatusCode::SUCCESS;
101 const std::vector<AlignModule*>& alignModules)
105 const EventContext& ctx = Gaudi::Hive::currentContext();
123 const Track* refittedTrack =
m_fitter->alignmentFit(ctx, alignCache, *trackForRefit,
126 if (!refittedTrack) {
127 msg(MSG::WARNING) <<
"initial track refit failed" <<
endmsg;
141 for (std::vector<AlignModule*>::const_iterator moduleIt=alignModules.begin();
142 moduleIt!=alignModules.end(); ++moduleIt) {
147 alignParIt!=alignPars->
end(); ++alignParIt) {
149 for (
int ishift=0;ishift<2;ishift++) {
151 double shiftsize =
shiftSize(*alignParIt);
152 if (ishift>0) shiftsize*=-1.;
153 m_alignModuleTool->shiftModule(*moduleIt,alignTrack,(**alignParIt).paramType(),shiftsize);
158 if (!refittedTrack) {
159 msg(MSG::WARNING) <<
"track refit failed!"<<
endmsg;
184 const EventContext& ctx = Gaudi::Hive::currentContext();
200 if (!trackForRefit) {
205 const Track* refittedTrack =
m_fitter->alignmentFit( ctx, alignCache,
210 if (!refittedTrack) {
219 msg()<<MSG::DEBUG<<
"local Chi2(unshifted) in setChi2VAlignParam="<<localChi2<<
endmsg;
240 for (; atsosItr != alignTrack->
lastAtsos(); ++atsosItr) {
241 if (!(**atsosItr).isValid())
continue;
242 for (std::vector<Residual>::const_iterator itRes=(**atsosItr).firstResidual();
243 itRes!=(**atsosItr).lastResidual();++itRes,++imeas) {
244 double residual = itRes->residual();
245 double errSq = itRes->errSq();
246 (*m_unshiftedResiduals)[imeas]=residual;
247 (*m_unshiftedResErrors)[imeas]=std::sqrt(errSq);
254 msg(MSG::ERROR)<<
"problem with nmeas, imeas="<<imeas<<
", NMEAS="<<NMEAS<<
endmsg;
255 throw std::runtime_error(
"Error in ShiftingDerivCalcTool::setUnshiftedResiduals");
259 delete refittedTrack; refittedTrack=
nullptr;
272 std::vector<AlignModule*> alignModules;
274 atsosItr != alignTrack->
lastAtsos(); ++atsosItr) {
283 if (!(*atsosItr)->isValid() || !module)
continue;
284 if (
find(alignModules.begin(),alignModules.end(),module) == alignModules.end())
285 alignModules.push_back(module);
305 std::vector<AlignModuleDerivatives> * derivatives =
new std::vector<AlignModuleDerivatives>;
306 std::vector<AlignModuleDerivatives> * derivativeErr =
new std::vector<AlignModuleDerivatives>;
307 std::vector<std::pair<AlignModule*, std::vector<double> > > * actualSecondDerivatives =
308 new std::vector<std::pair<AlignModule*, std::vector<double> > >;
310 for (
auto *alignModule : alignModules) {
312 ATH_MSG_DEBUG(
"finding derivatives for module "<<(*alignModule).identify());
314 std::vector<Amg::VectorX> deriv_vec;
315 std::vector<Amg::VectorX> derivErr_vec;
316 std::vector<double> actualsecderiv_vec;
320 const int nAlignPar = alignPars->
size();
331 bool resetIPar=
false;
332 std::vector<Amg::VectorX> tmpderiv_vec;
333 std::vector<Amg::VectorX> tmpderivErr_vec;
334 std::vector<double> tmpactualsecderiv_vec;
339 alignTrack, alignModule,
340 tmpderiv_vec,tmpderivErr_vec,tmpactualsecderiv_vec,
344 delete derivativeErr;
345 delete actualSecondDerivatives;
355 tmpderiv_vec,tmpderivErr_vec,tmpactualsecderiv_vec,
361 delete derivativeErr;
362 delete actualSecondDerivatives;
372 tmpderiv_vec,tmpderivErr_vec,tmpactualsecderiv_vec,
378 delete derivativeErr;
379 delete actualSecondDerivatives;
386 if (success && !resetIPar) {
387 for (
int i=0;i<(int)tmpderiv_vec.size();i++) {
388 deriv_vec.push_back(tmpderiv_vec[i]);
389 derivErr_vec.push_back(tmpderivErr_vec[i]);
390 actualsecderiv_vec.push_back(tmpactualsecderiv_vec[i]);
395 delete derivativeErr;
396 delete actualSecondDerivatives;
415 derivatives->push_back(make_pair(alignModule,deriv_vec));
416 derivativeErr->push_back(make_pair(alignModule,derivErr_vec));
417 actualSecondDerivatives->push_back(make_pair(alignModule,actualsecderiv_vec));
438 double& actualSecondDeriv)
440 const EventContext& ctx = Gaudi::Hive::currentContext();
450 ATH_MSG_ERROR(
"set m_fitter before calling getDerivatives (by calling setUnshiftedResiduals)");
457 module->setNChamberShifts(m_nFits);
460 double** residuals=
new double*[NFITS];
461 double** resErrors=
new double*[NFITS];
462 double* chi2Array =
new double[NFITS];
463 double* chi2ArrayX=
new double[NFITS];
470 for (
int ifit=0;ifit<NFITS;ifit++) {
471 residuals[ifit]=
new double[NMEAS];
472 resErrors[ifit]=
new double[NMEAS];
478 ATH_MSG_DEBUG(
"chi2Array["<<unshiftedTrackIndex<<
"]="<<chi2Array[unshiftedTrackIndex]);
479 chi2ArrayX[unshiftedTrackIndex] = 0.;
496 for (
int ifit=0;ifit<NFITS;ifit++) {
500 if (ifit>unshiftedTrackIndex) {
501 jfit=NFITS-ifit+unshiftedTrackIndex;
503 if (
m_doFits && ifit==unshiftedTrackIndex) {
505 residuals[ifit][i]=(*m_unshiftedResiduals)[i];
506 resErrors[ifit][i]=(*m_unshiftedResErrors)[i];
514 double currentshift = 0.;
516 currentshift = shiftsize * (double)(jfit-unshiftedTrackIndex);
518 currentshift = (ifit==0) ? -1.*shiftsize : shiftsize;
520 ATH_MSG_DEBUG(
"current shift="<<currentshift<<
" in getDerivatives");
527 const Track* refittedTrack=
m_fitter->alignmentFit(ctx, alignCache,
537 delete [] residuals;
delete [] resErrors;
538 delete [] chi2Array;
delete [] chi2ArrayX;
555 if (!refittedTrack) {
556 msg(MSG::WARNING) <<
"track refit failed for jfit "<<jfit <<
endmsg;
557 delete [] residuals;
delete [] resErrors;
558 delete [] chi2Array;
delete [] chi2ArrayX;
576 chi2ArrayX[jfit]= shiftsize * (double)(jfit-unshiftedTrackIndex);
577 chi2Array[jfit]=localChi2;
591 for (; atsosItr != alignTrack->
lastAtsos(); ++atsosItr) {
592 if (!(*atsosItr)->isValid())
continue;
593 for (std::vector<Residual>::const_iterator itRes=(**atsosItr).firstResidual();
594 itRes!=(**atsosItr).lastResidual();++itRes,++imeas) {
597 residuals[jfit][imeas]=itRes->residual();
598 resErrors[jfit][imeas]=std::sqrt(itRes->errSq());
601 residuals[jfit][imeas]=resErrors[jfit][imeas]=0.;
603 ATH_MSG_DEBUG(
"residuals["<<jfit<<
"]["<<imeas<<
"]="<<residuals[jfit][imeas]);
604 ATH_MSG_DEBUG(
"resErrors["<<jfit<<
"]["<<imeas<<
"]="<<resErrors[jfit][imeas]);
608 delete refittedTrack; refittedTrack=
nullptr;
615 for (; aatsosItr != alignTrack->
lastAtsos(); ++aatsosItr) {
616 if (!(*aatsosItr)->isValid())
continue;
617 for (std::vector<Residual>::const_iterator itRes=(**aatsosItr).firstResidual();
618 itRes!=(**aatsosItr).lastResidual();++itRes,++iimeas) {
619 for (
int ifit=0;ifit<NFITS;ifit++) {
620 ATH_MSG_DEBUG(
"["<<ifit<<
"]["<<iimeas<<
"] res="<<residuals[ifit][iimeas]<<
621 ", resErr="<<resErrors[ifit][iimeas]);
628 delete [] residuals;
delete [] resErrors;
629 delete [] chi2Array;
delete [] chi2ArrayX;
637 TGraph*
gr =
new TGraph(
m_nFits,chi2ArrayX,chi2Array);
638 gr->Fit(
"pol2",
"QF");
639 TF1* fit=
gr->GetFunction(
"pol2");
640 double chi2 =fit->GetChisquare()/double(
m_nFits-3);
641 double slope=fit->GetParameter(1);
642 actualSecondDeriv=fit->GetParameter(2);
649 if (
chi2>1.e-6 || std::fabs(slope)<1.e-10) {
651 StatusCode
sc=
evtStore()->retrieve(eventInfo);
667 ATH_MSG_DEBUG(
"created derivatives with "<<derivatives.rows()<<
" rows");
672 delete [] residuals;
delete [] resErrors;
673 delete [] chi2Array;
delete [] chi2ArrayX;
687 return emptyDerivatives;
691 TCanvas* canv(
nullptr);
692 std::vector<TGraph*> vecGraphs;
694 for (; atsosItr != alignTrack->
lastAtsos(); ++atsosItr) {
695 if (!(*atsosItr)->isValid())
continue;
696 for (
int idim=0;idim<(*atsosItr)->nResDim();idim++) {
698 double* gr_x =
new double[NFITS];
699 double* gr_y =
new double[NFITS];
701 for (
int ifit=0;ifit<NFITS;ifit++) {
702 double residual=residuals[ifit][imeas];
703 double resError=resErrors[ifit][imeas];
704 if (residual>-999.) {
705 gr_x[ngoodfits] =chi2ArrayX[ifit];
706 gr_y[ngoodfits] =residual/resError;
712 derivatives[imeas]=(residuals[1][imeas]-residuals[0][imeas])/(2.*shiftsize)*
713 resErrors[unshiftedTrackIndex][imeas];
716 TGraph*
gr=
new TGraph(ngoodfits,gr_x,gr_y);
719 gr->Fit(
"pol2",
"VF");
721 gr->Fit(
"pol2",
"QF");
722 TF1* fit=
gr->GetFunction(
"pol2");
725 ATH_MSG_DEBUG(
"deriv["<<imeas<<
"]="<<fit->GetParameter(1)<<
" +/- "<<fit->GetParError(1)
726 <<
", chi2="<<fit->GetChisquare());
727 derivatives[imeas]=fit->GetParameter(1)*resErrors[unshiftedTrackIndex][imeas];
728 derivativeErr[imeas]=fit->GetParError(1)*resErrors[unshiftedTrackIndex][imeas];
733 if (!canv) canv=
new TCanvas(
"resPlots",
"resPlots");
735 gr->SetMarkerStyle(20);
738 gr->GetXaxis()->SetTitle(
"shift in chamber pos. from nominal (CLHEP::mm)");
739 gr->GetYaxis()->SetTitle(
"residual (CLHEP::mm)");
741 TPaveText* pave=
new TPaveText(.4,.65,.97,.92,
"NDC");
742 pave->SetFillColor(0);
743 pave->SetBorderSize(1);
744 std::stringstream measType; measType<<
"meas type: ";
748 else measType<<
" undefined";
750 pave->AddText(measType.str().c_str());
752 std::stringstream firstderivtxt,secndderivtxt,aptxt,chi2txt;
753 firstderivtxt<<fit->GetParameter(1)<<
" +/- "<<fit->GetParError(1);
754 secndderivtxt<<fit->GetParameter(2)<<
" +/- "<<fit->GetParError(2);
755 aptxt <<
"alignPar "<<alignPar->
paramType()<<
", RIO in "<<(*atsosItr)->identify();
756 chi2txt<<
"chi2="<<fit->GetChisquare();
758 pave->AddText(firstderivtxt.str().c_str());
759 pave->AddText(secndderivtxt.str().c_str());
760 pave->AddText(aptxt.str().c_str());
761 pave->AddText(chi2txt.str().c_str());
764 std::stringstream canvName;
765 canvName<<
"resPlots_ap"<<alignPar->
paramType()<<
"_measType"
766 <<(*atsosItr)->measType()<<
"_"<<imeas<<
".eps";
767 canv->Print(canvName.str().c_str());
772 vecGraphs.push_back(
gr);
775 derivatives[imeas]=-999.;
776 derivativeErr[imeas]=-999.;
787 for (
auto & vecGraph : vecGraphs)
795 for (
int ifit=0;ifit<NFITS;ifit++) {
814 double sigma=alignPar->
sigma();
815 return shift * sigma;
824 ATH_MSG_ERROR(
"Need to assign this matrix correctly: ShiftingDerivCalcTool.cxx:888");
833 const double epsilon=1e-10;
834 for(
int irow=0; irow<W.rows(); ++irow) {
835 Wisvalid = Wisvalid && W(irow,irow)>0;
836 if( !(W(irow,irow)>0) )
837 msg(MSG::WARNING) <<
"matrix invalid: " << W(irow,irow) <<
endmsg;
839 for(
int icol=0; icol<=irow; ++icol) {
842 double Wcorr = W(irow,icol)/sqrt(W(irow,irow)*W(icol,icol));
843 bool Wcorrisvalid = Wcorr+epsilon>=-1 && Wcorr-epsilon<=1;
844 Wisvalid = Wisvalid && Wcorrisvalid;
846 msg(MSG::WARNING) <<
"matrix corr invalid: " << Wcorr-1 <<
" " << Wcorr+1 <<
endmsg;
869 delete [] i; i=
nullptr;
878 std::vector<Amg::VectorX>& deriv_vec,
879 std::vector<Amg::VectorX>& derivErr_vec,
880 std::vector<double>& actualsecderiv_vec,
886 derivErr_vec.clear();
887 actualsecderiv_vec.clear();
896 double actualSecondDeriv(0.);
901 if (resetIPar)
continue;
903 if (
vec.rows()<1)
return false;
905 deriv_vec.push_back(
vec);
906 derivErr_vec.push_back(derivErr);
907 actualsecderiv_vec.push_back(actualSecondDeriv);
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
std::vector< size_t > vec
ServiceHandle< StoreGateSvc > & evtStore()
DataModel_detail::const_iterator< DataVector > const_iterator
DataModel_detail::iterator< DataVector > iterator
Standard iterator.
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
double sigma() const
returns sigma
AlignModule::TransformParameters paramType() const
returns the type of parameter (i.e.
const AlignModule * alignModule() const
returns the AlignModule
void setWeightMatrix(Amg::SymMatrixX *mat)
AlignTSOSCollection::const_iterator lastAtsos() const
returns iterator pointer to last element in collection
const Amg::SymMatrixX * localErrorMatrixInv() const
inverse local error matrix, calculated by AlignTrack by calling atsos->hitDistanceVar()
int nAlignTSOSMeas() const
number of alignTSOS (including scatterers if included on AlignTrack
void setActualSecondDerivatives(std::vector< std::pair< AlignModule *, std::vector< double > > > *vec)
void setTrackAlignParamQuality(int i, double val)
void setDerivatives(std::vector< AlignModuleDerivatives > *vec)
AlignTSOSCollection::const_iterator firstAtsos() const
retrieve iterator pointer to first element in collection
void setWeightMatrixFirstDeriv(Amg::SymMatrixX *mat)
const Trk::Track * trackWithoutScattering() const
returns track with ScatteringAngle pointers all set to zero (used for refit by iPat)
void setDerivativeErr(std::vector< AlignModuleDerivatives > *vec)
void setResidualVector(Amg::VectorX *vec)
bool isSLTrack() const
method to determine whether a straight line track or not
double chiSquared() const
returns the of the overall track fit
const FitQuality * fitQuality() const
return a pointer to the fit quality const-overload
uint32_t runNumber() const
The current event's run number.
uint64_t eventNumber() const
The current event's event number.
double chi2(TH1 *h0, TH1 *h1)
std::string find(const std::string &s)
return a remapped string
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > MatrixX
Dynamic Matrix - dynamic allocation.
Eigen::Matrix< double, Eigen::Dynamic, 1 > VectorX
Dynamic Vector - dynamic allocation.
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
@ NumberOfMeasurementTypes
Ensure that the ATLAS eigen extensions are properly loaded.
EventInfo_v1 EventInfo
Definition of the latest event info version.
int m_minIterations
sets the minimum number of iterations to be used in the track fit.
int m_iterationsOfLastFit
returns the number of iterations used by the last fit (count starts at 1 for a single-iteration fit)
int run(int argc, char *argv[])