Project
Loading...
Searching...
No Matches
Millepede2Record.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
18#include "Align/utils.h"
20#include "Framework/Logger.h"
21#include <TMath.h>
22#include <cstdio>
23
24using namespace TMath;
25using namespace o2::align::utils;
26
27namespace o2
28{
29namespace align
30{
31
32//_________________________________________________________
34{
35 // d-tor
36 delete[] mNDLoc;
37 delete[] mNDGlo;
38 delete[] mVolID;
39 delete[] mResid;
40 delete[] mMeasType;
41 delete[] mResErr;
42 delete[] mIDLoc;
43 delete[] mIDGlo;
44 delete[] mDLoc;
45 delete[] mDGlo;
46}
47
48//_________________________________________________________
49void Millepede2Record::dummyRecord(float res, float err, float dGlo, int labGlo)
50{
51 // create dummy residuals record
52 if (!mNDGlo) {
53 resize(1, 1, 1);
54 }
55 mChi2Ini = 0;
56 mNMeas = 1;
57 mNResid = 1;
58 mNVarLoc = 0;
59 mNVarGlo = 1;
60 mIDGlo[0] = labGlo;
61 mDGlo[0] = dGlo;
62 mNDGlo[0] = 1;
63 mVolID[0] = -1;
64 mResid[0] = res;
65 mMeasType[0] = -1;
66 mResErr[0] = err;
67 //
68 mIDLoc[0] = 0;
69 mNDLoc[0] = 0;
70 mDLoc[0] = 0;
71 mNDGloTot = 1;
72 mNDLocTot = 0;
73 //
74}
75
76//_________________________________________________________
77bool Millepede2Record::fillTrack(AlignmentTrack& trc, const std::vector<int>& id2Lab)
78{
79 // fill track info, optionally substitutind glopar par ID by label
80 //
81 if (!trc.getDerivDone()) {
82 LOG(error) << "Track derivatives are not yet evaluated";
83 return false;
84 }
85 mNVarLoc = trc.getNLocPar(); // number of local degrees of freedom in the track
86 mNResid = 0;
87 mNDLocTot = 0;
88 mNDGloTot = 0;
89 mChi2Ini = trc.getChi2Ini();
90 mQ2Pt = trc.getQ2Pt();
91 mTgl = trc.getTgl();
92 mNMeas = 0;
93 setCosmic(trc.isCosmic());
94 // 1) check sizes for buffers, expand if needed
95 int np = trc.getNPoints();
96 int nres = 0;
97 int nlocd = 0;
98 int nglod = 0;
99 for (int ip = 0; ip < np; ip++) {
100 auto pnt = trc.getPoint(ip);
101 int ngl = pnt->getNGloDOFs(); // number of DOF's this point depends on
102 if (pnt->containsMeasurement()) {
103 nres += 2; // every point has 2 residuals
104 nlocd += mNVarLoc + mNVarLoc; // each residual has max mNVarLoc local derivatives
105 nglod += ngl + ngl; // number of global derivatives
106 mNMeas++;
107 }
108 if (pnt->containsMaterial()) {
109 int nmatpar = pnt->getNMatPar();
110 nres += nmatpar; // each point with materials has nmatpar fake residuals
111 nlocd += nmatpar; // and nmatpar non-0 local derivatives (orthogonal)
112 }
113 }
114 //
115 resize(nres, nlocd, nglod);
116 int nParETP = trc.getNLocExtPar(); // numnber of local parameters for reference track param
117 //
118 const int* gloParID = trc.getGloParID(); // IDs of global DOFs this track depends on
119 for (int ip = 0; ip < np; ip++) {
120 auto* pnt = trc.getPoint(ip);
121 if (pnt->containsMeasurement()) {
122 int gloOffs = pnt->getDGloOffs(); // 1st entry of global derivatives for this point
123 int nDGlo = pnt->getNGloDOFs(); // number of global derivatives (number of DOFs it depends on)
124 if (!pnt->isStatOK()) {
125 pnt->incrementStat();
126 }
127 //
128 for (int idim = 0; idim < 2; idim++) { // 2 dimensional orthogonal measurement
129 mNDGlo[mNResid] = 0;
130 mVolID[mNResid] = pnt->getSensor()->getVolID() + 1;
131 //
132 // measured residual/error
133 mMeasType[mNResid] = idim;
134 mResid[mNResid] = trc.getResidual(idim, ip);
135 mResErr[mNResid] = Sqrt(pnt->getErrDiag(idim));
136 //
137 // derivatives over local params
138 const double* deriv = trc.getDResDLoc(idim, ip); // array of Dresidual/Dparams_loc
139 int nnon0 = 0;
140 for (int j = 0; j < nParETP; j++) { // derivatives over reference track parameters
141 if (isZeroAbs(deriv[j])) {
142 continue;
143 }
144 nnon0++;
145 mDLoc[mNDLocTot] = deriv[j]; // store non-0 derivative
146 mIDLoc[mNDLocTot] = j; // and variable id
147 mNDLocTot++;
148 }
149 int lp0 = pnt->getMinLocVarID(); // point may depend on material variables starting from this one
150 int lp1 = pnt->getMaxLocVarID(); // and up to this one (exclusive)
151 for (int j = lp0; j < lp1; j++) { // derivatives over material variables
152 if (TMath::IsNaN(deriv[j])) {
153 LOGP(error, "Deriv {} of {}:{} is NAN", j, lp0, lp1);
154 }
155 if (isZeroAbs(deriv[j])) {
156 continue;
157 }
158 nnon0++;
159 mDLoc[mNDLocTot] = deriv[j]; // store non-0 derivative
160 mIDLoc[mNDLocTot] = j; // and variable id
161 mNDLocTot++;
162 }
163 //
164 mNDLoc[mNResid] = nnon0; // local derivatives done, store their number for this residual
165 //
166 // derivatives over global params
167 nnon0 = 0;
168 deriv = trc.getDResDGlo(idim, gloOffs);
169 const int* gloIDP = gloParID + gloOffs;
170 for (int j = 0; j < nDGlo; j++) {
171 if (isZeroAbs(deriv[j])) {
172 continue;
173 }
174 nnon0++;
175 mDGlo[mNDGloTot] = deriv[j]; // value of derivative
176 mIDGlo[mNDGloTot] = id2Lab.empty() ? gloIDP[j] + 1 : id2Lab[gloIDP[j]]; // global DOF ID
177 mNDGloTot++;
178 }
179 mNDGlo[mNResid] = nnon0;
180 //
181 mNResid++;
182 }
183 }
184 if (pnt->containsMaterial()) { // material point can add 4 or 5 otrhogonal pseudo-measurements
185 int nmatpar = pnt->getNMatPar(); // residuals (correction expectation value)
186 // const float* expMatCorr = pnt->getMatCorrExp(); // expected corrections (diagonalized)
187 const float* expMatCov = pnt->getMatCorrCov(); // their diagonalized error matrix
188 int offs = pnt->getMaxLocVarID() - nmatpar; // start of material variables
189 // here all derivatives are 1 = dx/dx
190 for (int j = 0; j < nmatpar; j++) {
191 mMeasType[mNResid] = 10 + j;
192 mNDGlo[mNResid] = 0; // mat corrections don't depend on global params
193 mVolID[mNResid] = 0; // not associated to global parameter
194 mResid[mNResid] = 0; // expectation for MS effects is 0
195 mResErr[mNResid] = Sqrt(expMatCov[j]);
196 mNDLoc[mNResid] = 1; // only 1 non-0 derivative
197 mDLoc[mNDLocTot] = 1.0;
198 mIDLoc[mNDLocTot] = offs + j; // variable id
199 mNDLocTot++;
200 mNResid++;
201 }
202 }
203 }
204 //
205 if (!mNDGloTot) {
206 LOG(info) << "Track does not depend on free global parameters, discard";
207 return false;
208 }
209 return true;
210}
211
212//________________________________________________
213void Millepede2Record::resize(int nresid, int nloc, int nglo)
214{
215 // resize container
216 if (nresid > mNResidBook) {
217 delete[] mMeasType;
218 delete[] mNDLoc;
219 delete[] mNDGlo;
220 delete[] mVolID;
221 delete[] mResid;
222 delete[] mResErr;
223 mMeasType = new int16_t[nresid];
224 mNDLoc = new int16_t[nresid];
225 mNDGlo = new int[nresid];
226 mVolID = new int[nresid];
227 mResid = new float[nresid];
228 mResErr = new float[nresid];
229 mNResidBook = nresid;
230 memset(mMeasType, 0, nresid * sizeof(int16_t));
231 memset(mNDLoc, 0, nresid * sizeof(int16_t));
232 memset(mNDGlo, 0, nresid * sizeof(int));
233 memset(mVolID, 0, nresid * sizeof(int));
234 memset(mResid, 0, nresid * sizeof(float));
235 memset(mResErr, 0, nresid * sizeof(float));
236 }
237 if (nloc > mNDLocTotBook) {
238 delete[] mIDLoc;
239 delete[] mDLoc;
240 mIDLoc = new int16_t[nloc];
241 mDLoc = new float[nloc];
242 mNDLocTotBook = nloc;
243 memset(mIDLoc, 0, nloc * sizeof(int16_t));
244 memset(mDLoc, 0, nloc * sizeof(float));
245 }
246 if (nglo > mNDGloTotBook) {
247 delete[] mIDGlo;
248 delete[] mDGlo;
249 mIDGlo = new int[nglo];
250 mDGlo = new float[nglo];
251 mNDGloTotBook = nglo;
252 memset(mIDGlo, 0, nglo * sizeof(int));
253 memset(mDGlo, 0, nglo * sizeof(float));
254 }
255 //
256}
257
258//____________________________________________
260{
261 // reset record
262 mBits = 0;
263 mNResid = 0;
264 mNVarLoc = 0;
265 mNVarGlo = 0;
266 mNDLocTot = 0;
267 mNDGloTot = 0;
268}
269
270//____________________________________________
272{
273 // print info
274 //
275 printf("Track %s TForbit:%d Run:%d\n", mTrackID.asString().c_str(), mFirstTFOrbit, mRunNumber);
276 printf("Nmeas:%3d Q/pt:%+.2e Tgl:%+.2e Chi2Ini:%.1f\n", mNMeas, mQ2Pt, mTgl, mChi2Ini);
277 printf("NRes: %3d NLoc: %3d NGlo:%3d | Stored: Loc:%3d Glo:%5d\n",
279 //
280 int curLoc = 0, curGlo = 0;
281 const int kNColLoc = 5;
282 for (int ir = 0; ir < mNResid; ir++) {
283 int ndloc = mNDLoc[ir], ndglo = mNDGlo[ir];
284 printf("Res:%3d Type:%d %+e (%+e) | NDLoc:%3d NDGlo:%4d [VID:%5d]\n",
285 ir, mMeasType[ir], mResid[ir], mResErr[ir], ndloc, ndglo, getVolID(ir));
286 //
287 printf("Local Derivatives:\n");
288 bool eolOK = true;
289 for (int id = 0; id < ndloc; id++) {
290 int jd = id + curLoc;
291 printf("[%3d] %+.2e ", mIDLoc[jd], mDLoc[jd]);
292 if (((id + 1) % kNColLoc) == 0) {
293 printf("\n");
294 eolOK = true;
295 } else {
296 eolOK = false;
297 }
298 }
299 if (!eolOK) {
300 printf("\n");
301 }
302 curLoc += ndloc;
303 //
304 //
305 printf("Global Derivatives:\n");
306 // eolOK = true;
307 // const int kNColGlo=6;
308 int prvID = -1;
309 for (int id = 0; id < ndglo; id++) {
310 int jd = id + curGlo;
311 // eolOK==false;
312 if (prvID > mIDGlo[jd] % 100) {
313 printf("\n"); /* eolOK = true;*/
314 }
315 printf("[%5d] %+.2e ", mIDGlo[jd], mDGlo[jd]);
316 // if (((id+1)%kNColGlo)==0)
317 // else eolOK = false;
318 prvID = mIDGlo[jd] % 100;
319 }
320 // if (!eolOK) printf("\n");
321 printf("\n");
322 curGlo += ndglo;
323 //
324 }
325}
326
327} // namespace align
328} // namespace o2
Track model for the alignment.
Collection of auxillary methods.
Millepede record in root format (can be converted to proper pede binary format.
uint32_t j
Definition RawData.h:0
uint32_t res
Definition RawData.h:0
double getResidual(int dim, int pntID) const
AlignmentPoint * getPoint(int i)
const double * getDResDGlo(int dim, int id) const
const int * getGloParID() const
const double * getDResDLoc(int dim, int pntID) const
int mNDGloTotBook
number of slots booked for local derivatives
o2::dataformats::GlobalTrackID mTrackID
int mNDLocTotBook
number of slots booked for residuals
void resize(int nresid, int nloc, int nglo)
bool fillTrack(AlignmentTrack &trc, const std::vector< int > &id2Lab)
void dummyRecord(float res, float err, float dGlo, int labGlo)
constexpr bool isZeroAbs(double d) noexcept
Definition utils.h:63
void align(gsl::span< ElinkEncoder< BareFormat, CHARGESUM > > elinks)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
o2::InteractionRecord ir(0, 0)