Belle II Software  release-06-01-15
TimeDependentVariables.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 // Own include
10 #include <analysis/variables/TimeDependentVariables.h>
11 #include <analysis/VariableManager/Manager.h>
12 
13 #include <analysis/utility/PCmsLabTransform.h>
14 #include <framework/dbobjects/BeamParameters.h>
15 
16 // dataobjects
17 #include <analysis/dataobjects/Particle.h>
18 #include <analysis/dataobjects/TagVertex.h>
19 #include <mdst/dataobjects/MCParticle.h>
20 
21 //utilities
22 #include <analysis/utility/DistanceTools.h>
23 #include <analysis/utility/RotationTools.h>
24 
25 // framework aux
26 #include <framework/gearbox/Const.h>
27 
28 #include <TMatrixD.h>
29 #include <TVectorD.h>
30 #include <TVector3.h>
31 
32 //#include <iostream>
33 #include <cmath>
34 
35 using namespace std;
36 
37 namespace Belle2 {
42  namespace Variable {
43 
44  // from RotationTools.h
45  using RotationTools::getUnitOrthogonal;
46  using RotationTools::toVec;
47 
48  static const double realNaN = std::numeric_limits<double>::quiet_NaN();
49  static const TVector3 vecNaN(realNaN, realNaN, realNaN);
50 
51  // ############################################## Time Dependent CPV Analysis Variables ###############################################
52 
53 
54  // TagV x, y, z
55  double particleTagVx(const Particle* particle)
56  {
57  auto* vert = particle->getRelatedTo<TagVertex>();
58  if (!vert) return realNaN;
59  return vert->getTagVertex().X();
60  }
61 
62  double particleTagVy(const Particle* particle)
63  {
64  auto* vert = particle->getRelatedTo<TagVertex>();
65  if (!vert) return realNaN;
66  return vert->getTagVertex().Y();
67  }
68 
69  double particleTagVz(const Particle* particle)
70  {
71  auto* vert = particle->getRelatedTo<TagVertex>();
72  if (!vert) return realNaN;
73  return vert->getTagVertex().Z();
74  }
75 
76  double particleTruthTagVx(const Particle* particle)
77  {
78  auto* vert = particle->getRelatedTo<TagVertex>();
79  if (!vert) return realNaN;
80  return vert->getMCTagVertex().X();
81  }
82 
83  double particleTruthTagVy(const Particle* particle)
84  {
85  auto* vert = particle->getRelatedTo<TagVertex>();
86  if (!vert) return realNaN;
87  return vert->getMCTagVertex().Y();
88  }
89 
90  double particleTruthTagVz(const Particle* particle)
91  {
92  auto* vert = particle->getRelatedTo<TagVertex>();
93  if (!vert) return realNaN;
94  return vert->getMCTagVertex().Z();
95  }
96 
97  double particleTagVxErr(const Particle* particle)
98  {
99  auto* vert = particle->getRelatedTo<TagVertex>();
100  if (!vert) return realNaN;
101  TMatrixDSym TagVErr = vert->getTagVertexErrMatrix();
102  return sqrt(TagVErr(0, 0));
103  }
104 
105  double particleTagVyErr(const Particle* particle)
106  {
107  auto* vert = particle->getRelatedTo<TagVertex>();
108  if (!vert) return realNaN;
109  TMatrixDSym TagVErr = vert->getTagVertexErrMatrix();
110  return sqrt(TagVErr(1, 1));
111  }
112 
113  double particleTagVzErr(const Particle* particle)
114  {
115  auto* vert = particle->getRelatedTo<TagVertex>();
116  if (!vert) return realNaN;
117  TMatrixDSym TagVErr = vert->getTagVertexErrMatrix();
118  return sqrt(TagVErr(2, 2));
119  }
120 
121  double particleTagVpVal(const Particle* particle)
122  {
123  auto* vert = particle->getRelatedTo<TagVertex>();
124  if (!vert) return realNaN;
125  return vert->getTagVertexPval();
126  }
127 
128  double particleTagVNTracks(const Particle* particle)
129  {
130  auto* vert = particle->getRelatedTo<TagVertex>();
131  if (!vert) return realNaN;
132  return vert->getNTracks();
133  }
134 
135  double particleTagVNFitTracks(const Particle* particle)
136  {
137  auto* vert = particle->getRelatedTo<TagVertex>();
138  if (!vert) return realNaN;
139  return vert->getNFitTracks();
140  }
141 
142  double particleTagVType(const Particle* particle)
143  {
144  auto* vert = particle->getRelatedTo<TagVertex>();
145  if (!vert) return realNaN;
146  return vert->getFitType();
147  }
148 
149  double particleTagVNDF(const Particle* particle)
150  {
151  auto* vert = particle->getRelatedTo<TagVertex>();
152  if (!vert) return realNaN;
153  return vert->getTagVNDF();
154  }
155 
156  double particleTagVChi2(const Particle* particle)
157  {
158  auto* vert = particle->getRelatedTo<TagVertex>();
159  if (!vert) return realNaN;
160  return vert->getTagVChi2();
161  }
162 
163  double particleTagVChi2IP(const Particle* particle)
164  {
165  auto* vert = particle->getRelatedTo<TagVertex>();
166  if (!vert) return realNaN;
167  return vert->getTagVChi2IP();
168  }
169 
170 
171  // Delta t and related
172 
173  double particleDeltaT(const Particle* particle)
174  {
175  auto* vert = particle->getRelatedTo<TagVertex>();
176  if (!vert) return realNaN;
177  return vert->getDeltaT();
178  }
179 
180  double particleDeltaTErr(const Particle* particle)
181  {
182  auto* vert = particle->getRelatedTo<TagVertex>();
183  if (!vert) return realNaN;
184  return vert->getDeltaTErr();
185  }
186  double particleDeltaTRes(const Particle* particle)
187  {
188  return particleDeltaT(particle) - particleMCDeltaT(particle);
189  }
190 
191  double particleDeltaTBelle(const Particle* particle)
192  {
193  double beta = PCmsLabTransform().getBoostVector().Mag();
194  double bg = beta / sqrt(1 - beta * beta);
195  double c = Const::speedOfLight / 1000.; // cm ps-1
196  return particleDeltaZ(particle) / bg / c;
197  }
198  double particleMCDeltaTau(const Particle* particle)
199  {
200  auto* vert = particle->getRelatedTo<TagVertex>();
201  if (!vert) return realNaN;
202  return vert->getMCDeltaTau();
203  }
204 
205  double particleMCDeltaT(const Particle* particle)
206  {
207  auto* vert = particle->getRelatedTo<TagVertex>();
208  if (!vert) return realNaN;
209  return vert->getMCDeltaT();
210  }
211 
212  double particleMCDeltaBoost(const Particle* particle)
213  {
214  auto* vert = particle->getRelatedTo<TagVertex>();
215  if (!vert) return realNaN;
216 
217  double beta = PCmsLabTransform().getBoostVector().Mag();
218  double bg = beta / sqrt(1 - beta * beta);
219  double c = Const::speedOfLight / 1000.; // cm ps-1
220  return vert->getMCDeltaT() * bg * c;
221  }
222 
223 
224  double particleDeltaZ(const Particle* particle)
225  {
226  auto* vert = particle->getRelatedTo<TagVertex>();
227  if (!vert) return realNaN;
228  return particle->getZ() - vert->getTagVertex().Z();
229  }
230 
231  double particleDeltaZErr(const Particle* particle)
232  {
233  auto* vert = particle->getRelatedTo<TagVertex>();
234  if (!vert) return realNaN;
235 
236  double zVariance = particle->getVertexErrorMatrix()(2, 2);
237  double TagVZVariance = vert->getTagVertexErrMatrix()(2, 2);
238  double result = sqrt(zVariance + TagVZVariance);
239  if (!std::isfinite(result)) return realNaN;
240 
241  return result;
242  }
243 
244  double particleDeltaB(const Particle* particle)
245  {
246  auto* vert = particle->getRelatedTo<TagVertex>();
247  if (!vert) return realNaN;
248 
249  double beta = PCmsLabTransform().getBoostVector().Mag();
250  double bg = beta / sqrt(1 - beta * beta);
251  double c = Const::speedOfLight / 1000.; // cm ps-1
252  return vert->getDeltaT() * bg * c;
253  }
254 
255  double particleDeltaBErr(const Particle* particle)
256  {
257  auto* vert = particle->getRelatedTo<TagVertex>();
258  if (!vert) return realNaN;
259 
260  double beta = PCmsLabTransform().getBoostVector().Mag();
261  double bg = beta / sqrt(1 - beta * beta);
262  double c = Const::speedOfLight / 1000.; // cm ps-1
263  return vert->getDeltaTErr() * bg * c;
264  }
265 
266  // Vertex boost direction
267 
268  double vertexBoostDirection(const Particle* part)
269  {
270  TVector3 boostDir = PCmsLabTransform().getBoostVector().Unit();
271  TVector3 pos = part->getVertex();
272  return pos.Dot(boostDir);
273  }
274 
275  double vertexOrthogonalBoostDirection(const Particle* part)
276  {
277  TVector3 boost = PCmsLabTransform().getBoostVector();
278  TVector3 orthBoostDir = getUnitOrthogonal(boost);
279 
280  TVector3 pos = part->getVertex();
281  return pos.Dot(orthBoostDir);
282  }
283 
284  double vertexTruthBoostDirection(const Particle* part)
285  {
286  static DBObjPtr<BeamParameters> beamParamsDB;
287  TVector3 boostDir = (beamParamsDB->getHER() + beamParamsDB->getLER()).BoostVector().Unit();
288 
289  const MCParticle* mcPart = part->getMCParticle();
290  if (!mcPart) return realNaN;
291  TVector3 pos = mcPart->getDecayVertex();
292  return pos.Dot(boostDir);
293  }
294 
295  double vertexTruthOrthogonalBoostDirection(const Particle* part)
296  {
297  static DBObjPtr<BeamParameters> beamParamsDB;
298  TVector3 boost = (beamParamsDB->getHER() + beamParamsDB->getLER()).BoostVector();
299  TVector3 orthBoostDir = getUnitOrthogonal(boost);
300 
301  const MCParticle* mcPart = part->getMCParticle();
302  if (!mcPart) return realNaN;
303  TVector3 pos = mcPart->getDecayVertex();
304  return pos.Dot(orthBoostDir);
305  }
306 
307 
308  double vertexErrBoostDirection(const Particle* part)
309  {
310  TVectorD bDir = toVec(PCmsLabTransform().getBoostVector().Unit());
311  // sqrt(bDir^T * Mat * bDir)
312  return sqrt(part->getVertexErrorMatrix().Similarity(bDir));
313  }
314 
315 
316  double vertexErrOrthBoostDirection(const Particle* part)
317  {
318  TVectorD oDir = toVec(getUnitOrthogonal(PCmsLabTransform().getBoostVector()));
319  // sqrt(oDir^T * Mat * oDir)
320  return sqrt(part->getVertexErrorMatrix().Similarity(oDir));
321  }
322 
323 
324 
325  // TagV boost direction
326 
327  double tagVBoostDirection(const Particle* part)
328  {
329  auto* vert = part->getRelatedTo<TagVertex>();
330  if (!vert) return realNaN;
331  return vert->getTagVl();
332  }
333 
334 
335  double tagVOrthogonalBoostDirection(const Particle* part)
336  {
337  auto* vert = part->getRelatedTo<TagVertex>();
338  if (!vert) return realNaN;
339  return vert->getTagVol();
340  }
341 
342 
343  double tagVTruthBoostDirection(const Particle* part)
344  {
345  auto* vert = part->getRelatedTo<TagVertex>();
346  if (!vert) return realNaN;
347  return vert->getTruthTagVl();
348  }
349 
350 
351  double tagVTruthOrthogonalBoostDirection(const Particle* part)
352  {
353  auto* vert = part->getRelatedTo<TagVertex>();
354  if (!vert) return realNaN;
355  return vert->getTruthTagVol();
356  }
357 
358  double tagVErrBoostDirection(const Particle* part)
359  {
360  auto* vert = part->getRelatedTo<TagVertex>();
361  if (!vert) return realNaN;
362  return vert->getTagVlErr();
363  }
364 
365 
366  double tagVErrOrthogonalBoostDirection(const Particle* part)
367  {
368  auto* vert = part->getRelatedTo<TagVertex>();
369  if (!vert) return realNaN;
370  return vert->getTagVolErr();
371  }
372 
373 
374  double particleInternalTagVMCFlavor(const Particle* part)
375  {
376  auto* vert = part->getRelatedTo<TagVertex>();
377  if (!vert) return realNaN;
378  return vert->getMCTagBFlavor();
379  }
380 
381  double tagTrackMomentum(const Particle* part, const std::vector<double>& trackIndex)
382  {
383  auto* vert = part->getRelatedTo<TagVertex>();
384  if (!vert) return realNaN;
385 
386  if (trackIndex.size() != 1) return realNaN;
387  unsigned trackIndexInt = trackIndex.at(0);
388 
389  return vert->getVtxFitTrackP(trackIndexInt).Mag();
390  }
391 
392  double tagTrackMomentumX(const Particle* part, const std::vector<double>& trackIndex)
393  {
394  auto* vert = part->getRelatedTo<TagVertex>();
395  if (!vert) return realNaN;
396 
397  if (trackIndex.size() != 1) return realNaN;
398  unsigned trackIndexInt = trackIndex.at(0);
399 
400  return vert->getVtxFitTrackPComponent(trackIndexInt, 0);
401  }
402 
403  double tagTrackMomentumY(const Particle* part, const std::vector<double>& trackIndex)
404  {
405  auto* vert = part->getRelatedTo<TagVertex>();
406  if (!vert) return realNaN;
407 
408  if (trackIndex.size() != 1) return realNaN;
409  unsigned trackIndexInt = trackIndex.at(0);
410 
411  return vert->getVtxFitTrackPComponent(trackIndexInt, 1);
412  }
413 
414  double tagTrackMomentumZ(const Particle* part, const std::vector<double>& trackIndex)
415  {
416  auto* vert = part->getRelatedTo<TagVertex>();
417  if (!vert) return realNaN;
418 
419  if (trackIndex.size() != 1) return realNaN;
420  unsigned trackIndexInt = trackIndex.at(0);
421 
422  return vert->getVtxFitTrackPComponent(trackIndexInt, 2);
423  }
424 
425  double tagTrackZ0(const Particle* part, const std::vector<double>& trackIndex)
426  {
427  auto* vert = part->getRelatedTo<TagVertex>();
428  if (!vert) return realNaN;
429 
430  if (trackIndex.size() != 1) return realNaN;
431  unsigned trackIndexInt = trackIndex.at(0);
432 
433  return vert->getVtxFitTrackZ0(trackIndexInt);
434  }
435 
436  double tagTrackD0(const Particle* part, const std::vector<double>& trackIndex)
437  {
438  auto* vert = part->getRelatedTo<TagVertex>();
439  if (!vert) return realNaN;
440 
441  if (trackIndex.size() != 1) return realNaN;
442  unsigned trackIndexInt = trackIndex.at(0);
443 
444  return vert->getVtxFitTrackD0(trackIndexInt);
445  }
446 
447  double tagTrackRaveWeight(const Particle* part, const std::vector<double>& trackIndex)
448  {
449  auto* vert = part->getRelatedTo<TagVertex>();
450  if (!vert) return realNaN;
451 
452  if (trackIndex.size() != 1) return realNaN;
453  unsigned trackIndexInt = trackIndex.at(0);
454 
455  return vert->getRaveWeight(trackIndexInt);
456  }
457 
458  double tagTrackDistanceToConstraint(const Particle* part, const std::vector<double>& trackIndex)
459  {
460  auto* vert = part->getRelatedTo<TagVertex>();
461  if (!vert) return realNaN;
462 
463  if (trackIndex.size() != 1) return realNaN;
464  unsigned trackIndexInt = trackIndex.at(0);
465 
466  if (vert->getConstraintType() == "noConstraint") return realNaN;
467  const Particle* tagParticle(vert->getVtxFitParticle(trackIndexInt));
468  if (!tagParticle) return realNaN;
469 
470  return DistanceTools::trackToVtxDist(tagParticle->getTrackFitResult()->getPosition(),
471  tagParticle->getMomentum(),
472  vert->getConstraintCenter());
473  }
474 
475  double tagTrackDistanceToConstraintErr(const Particle* part, const std::vector<double>& trackIndex)
476  {
477  auto* vert = part->getRelatedTo<TagVertex>();
478  if (!vert) return realNaN;
479 
480  if (trackIndex.size() != 1) return realNaN;
481  unsigned trackIndexInt = trackIndex.at(0);
482 
483  if (vert->getConstraintType() == "noConstraint") return realNaN;
484  const Particle* tagParticle(vert->getVtxFitParticle(trackIndexInt));
485  if (!tagParticle) return realNaN;
486 
487  //recover the covariance matrix associated to the position of the tag track
488  TMatrixDSym trackPosCovMat = tagParticle->getVertexErrorMatrix();
489 
490  return DistanceTools::trackToVtxDistErr(tagParticle->getTrackFitResult()->getPosition(),
491  tagParticle->getMomentum(),
492  vert->getConstraintCenter(),
493  trackPosCovMat,
494  vert->getConstraintCov());
495  }
496 
497  double tagTrackDistanceToConstraintSignificance(const Particle* part, const std::vector<double>& trackIndex)
498  {
499  double val = tagTrackDistanceToConstraint(part, trackIndex);
500  if (isinf(val) || isnan(val)) return realNaN;
501  double err = tagTrackDistanceToConstraintErr(part, trackIndex);
502  if (isinf(err) || isnan(err)) return realNaN;
503 
504  return val / err;
505  }
506 
507  double tagVDistanceToConstraint(const Particle* part)
508  {
509  auto* vert = part->getRelatedTo<TagVertex>();
510  if (!vert) return realNaN;
511 
512  if (vert->getConstraintType() == "noConstraint") return realNaN;
513 
514  return DistanceTools::vtxToVtxDist(vert->getConstraintCenter(),
515  vert->getTagVertex());
516  }
517 
518  double tagVDistanceToConstraintErr(const Particle* part)
519  {
520  auto* vert = part->getRelatedTo<TagVertex>();
521  if (!vert) return realNaN;
522 
523  if (vert->getConstraintType() == "noConstraint") return realNaN;
524 
525  //To compute the uncertainty, the tag vtx uncertainty is NOT taken into account
526  //The error computed is the the one used in the chi2.
527  //To change that, emptyMat has to be replaced by m_TagVertexErrMatrix
528  TMatrixDSym emptyMat(3);
529 
530  return DistanceTools::vtxToVtxDistErr(vert->getConstraintCenter(),
531  vert->getTagVertex(),
532  vert->getConstraintCov(),
533  emptyMat);
534  }
535 
536  double tagVDistanceToConstraintSignificance(const Particle* part)
537  {
538  double val = tagVDistanceToConstraint(part);
539  if (isinf(val) || isnan(val)) return realNaN;
540  double err = tagVDistanceToConstraintErr(part);
541  if (isinf(err) || isnan(err)) return realNaN;
542 
543  return val / err;
544  }
545 
546  double tagTrackDistanceToTagV(const Particle* part, const std::vector<double>& trackIndex)
547  {
548  auto* vert = part->getRelatedTo<TagVertex>();
549  if (!vert) return realNaN;
550 
551  if (trackIndex.size() != 1) return realNaN;
552  unsigned trackIndexInt = trackIndex.at(0);
553 
554  const Particle* particle = vert->getVtxFitParticle(trackIndexInt);
555  if (!particle) return realNaN;
556 
557  return DistanceTools::trackToVtxDist(particle->getTrackFitResult()->getPosition(),
558  particle->getMomentum(),
559  vert->getTagVertex());
560  }
561 
562  double tagTrackDistanceToTagVErr(const Particle* part, const std::vector<double>& trackIndex)
563  {
564  auto* vert = part->getRelatedTo<TagVertex>();
565  if (!vert) return realNaN;
566 
567  if (trackIndex.size() != 1) return realNaN;
568  unsigned trackIndexInt = trackIndex.at(0);
569 
570  const Particle* tagParticle(vert->getVtxFitParticle(trackIndexInt));
571  if (!tagParticle) return realNaN;
572 
573  //recover the covariance matrix associated to the position of the tag track
574  TMatrixDSym trackPosCovMat = tagParticle->getVertexErrorMatrix();
575 
576  //To compute the uncertainty, the tag vtx uncertainty is NOT taken into account
577  //The error computed is then the one in the chi2.
578  //To change that, emptyMat has to be replaced by m_TagVertexErrMatrix
579 
580  TMatrixDSym emptyMat(3);
581 
582  return DistanceTools::trackToVtxDistErr(tagParticle->getTrackFitResult()->getPosition(),
583  tagParticle->getMomentum(),
584  vert->getTagVertex(),
585  trackPosCovMat,
586  emptyMat);
587 
588  }
589 
590  double tagTrackDistanceToTagVSignificance(const Particle* part, const std::vector<double>& trackIndex)
591  {
592  double val = tagTrackDistanceToTagV(part, trackIndex);
593  if (isinf(val) || isnan(val)) return realNaN;
594  double err = tagTrackDistanceToTagVErr(part, trackIndex);
595  if (isinf(err) || isnan(err)) return realNaN;
596 
597  return val / err;
598  }
599 
600  double tagTrackTrueDistanceToTagV(const Particle* part, const std::vector<double>& trackIndex)
601  {
602  auto* vert = part->getRelatedTo<TagVertex>();
603  if (!vert) return realNaN;
604 
605  if (trackIndex.size() != 1) return realNaN;
606  unsigned trackIndexInt = trackIndex.at(0);
607 
608  const MCParticle* mcParticle(vert->getVtxFitMCParticle(trackIndexInt));
609  if (!mcParticle) return realNaN;
610 
611  TVector3 mcTagV = vert->getMCTagVertex();
612  if (mcTagV(0) == realNaN) return realNaN;
613  if (mcTagV(0) == 0 && mcTagV(1) == 0 && mcTagV(2) == 0) return realNaN;
614 
615  return DistanceTools::trackToVtxDist(mcParticle->getProductionVertex(),
616  mcParticle->getMomentum(),
617  mcTagV);
618  }
619 
620  TVector3 tagTrackTrueVecToTagV(const Particle* part, const std::vector<double>& trackIndex)
621  {
622  auto* vert = part->getRelatedTo<TagVertex>();
623  if (!vert) return vecNaN;
624 
625  if (trackIndex.size() != 1) return vecNaN;
626  unsigned trackIndexInt = trackIndex.at(0);
627 
628  const MCParticle* mcParticle(vert->getVtxFitMCParticle(trackIndexInt));
629  if (!mcParticle) return vecNaN;
630 
631  TVector3 mcTagV = vert->getMCTagVertex();
632  if (mcTagV(0) == realNaN) return vecNaN;
633  if (mcTagV(0) == 0 && mcTagV(1) == 0 && mcTagV(2) == 0) return vecNaN;
634 
635  return DistanceTools::trackToVtxVec(mcParticle->getProductionVertex(),
636  mcParticle->getMomentum(),
637  mcTagV);
638  }
639 
640  double tagTrackTrueVecToTagVX(const Particle* part, const std::vector<double>& trackIndex)
641  {
642  TVector3 result = tagTrackTrueVecToTagV(part, trackIndex);
643  return result(0);
644  }
645 
646  double tagTrackTrueVecToTagVY(const Particle* part, const std::vector<double>& trackIndex)
647  {
648  TVector3 result = tagTrackTrueVecToTagV(part, trackIndex);
649  return result(1);
650  }
651 
652  double tagTrackTrueVecToTagVZ(const Particle* part, const std::vector<double>& trackIndex)
653  {
654  TVector3 result = tagTrackTrueVecToTagV(part, trackIndex);
655  return result(2);
656  }
657 
658  TVector3 tagTrackTrueMomentum(const Particle* part, const std::vector<double>& trackIndex)
659  {
660  auto* vert = part->getRelatedTo<TagVertex>();
661  if (!vert) return vecNaN;
662 
663  if (trackIndex.size() != 1) return vecNaN;
664  unsigned trackIndexInt = trackIndex.at(0);
665 
666  const MCParticle* mcParticle(vert->getVtxFitMCParticle(trackIndexInt));
667  if (!mcParticle) return vecNaN;
668 
669  return mcParticle->getMomentum();
670  }
671 
672  double tagTrackTrueMomentumX(const Particle* part, const std::vector<double>& trackIndex)
673  {
674  TVector3 pTrue = tagTrackTrueMomentum(part, trackIndex);
675  return pTrue(0);
676  }
677 
678  double tagTrackTrueMomentumY(const Particle* part, const std::vector<double>& trackIndex)
679  {
680  TVector3 pTrue = tagTrackTrueMomentum(part, trackIndex);
681  return pTrue(1);
682  }
683 
684  double tagTrackTrueMomentumZ(const Particle* part, const std::vector<double>& trackIndex)
685  {
686  TVector3 pTrue = tagTrackTrueMomentum(part, trackIndex);
687  return pTrue(2);
688  }
689 
690  TVector3 tagTrackTrueOrigin(const Particle* part, const std::vector<double>& trackIndex)
691  {
692  auto* vert = part->getRelatedTo<TagVertex>();
693  if (!vert) return vecNaN;
694 
695  if (trackIndex.size() != 1) return vecNaN;
696  unsigned trackIndexInt = trackIndex.at(0);
697 
698  const MCParticle* mcParticle(vert->getVtxFitMCParticle(trackIndexInt));
699  if (!mcParticle) return vecNaN;
700 
701  return mcParticle->getProductionVertex();
702  }
703 
704  double tagTrackTrueOriginX(const Particle* part, const std::vector<double>& trackIndex)
705  {
706  TVector3 origin = tagTrackTrueOrigin(part, trackIndex);
707  return origin(0);
708  }
709 
710  double tagTrackTrueOriginY(const Particle* part, const std::vector<double>& trackIndex)
711  {
712  TVector3 origin = tagTrackTrueOrigin(part, trackIndex);
713  return origin(1);
714  }
715 
716  double tagTrackTrueOriginZ(const Particle* part, const std::vector<double>& trackIndex)
717  {
718  TVector3 origin = tagTrackTrueOrigin(part, trackIndex);
719  return origin(2);
720  }
721 
722  int fitTruthStatus(const Particle* part)
723  {
724  auto* vert = part->getRelatedTo<TagVertex>();
725  if (!vert) return std::numeric_limits<int>::quiet_NaN();
726  return vert->getFitTruthStatus();
727  }
728 
729  int rollbackStatus(const Particle* part)
730  {
731  auto* vert = part->getRelatedTo<TagVertex>();
732  if (!vert) return std::numeric_limits<int>::quiet_NaN();
733  return vert->getRollBackStatus();
734  }
735 
736  //**********************************
737  //Meta variables
738  //**********************************
739 
745  TagTrFPtr getTagTrackFunctionFromName(string const& name)
746  {
747  if (name == "TagTrackMomentum") return tagTrackMomentum;
748  else if (name == "TagTrackMomentumX") return tagTrackMomentumX;
749  else if (name == "TagTrackMomentumY") return tagTrackMomentumY;
750  else if (name == "TagTrackMomentumZ") return tagTrackMomentumZ;
751  else if (name == "TagTrackZ0") return tagTrackZ0;
752  else if (name == "TagTrackD0") return tagTrackD0;
753  else if (name == "TagTrackRaveWeight") return tagTrackRaveWeight;
754  else if (name == "TagTrackDistanceToConstraint") return tagTrackDistanceToConstraint;
755  else if (name == "TagTrackDistanceToConstraintErr") return tagTrackDistanceToConstraintErr;
756  else if (name == "TagTrackDistanceToConstraintSignificance") return tagTrackDistanceToConstraintSignificance;
757  else if (name == "TagTrackDistanceToTagV") return tagTrackDistanceToTagV;
758  else if (name == "TagTrackDistanceToTagVErr") return tagTrackDistanceToTagVErr;
759  else if (name == "TagTrackDistanceToTagVSignificance") return tagTrackDistanceToTagVSignificance;
760  else if (name == "TagTrackTrueDistanceToTagV") return tagTrackTrueDistanceToTagV;
761  else if (name == "TagTrackTrueVecToTagVX") return tagTrackTrueVecToTagVX;
762  else if (name == "TagTrackTrueVecToTagVY") return tagTrackTrueVecToTagVY;
763  else if (name == "TagTrackTrueVecToTagVZ") return tagTrackTrueVecToTagVZ;
764  else if (name == "TagTrackTrueMomentumX") return tagTrackTrueMomentumX;
765  else if (name == "TagTrackTrueMomentumY") return tagTrackTrueMomentumY;
766  else if (name == "TagTrackTrueMomentumZ") return tagTrackTrueMomentumZ;
767  else if (name == "TagTrackTrueOriginX") return tagTrackTrueOriginX;
768  else if (name == "TagTrackTrueOriginY") return tagTrackTrueOriginY;
769  else if (name == "TagTrackTrueOriginZ") return tagTrackTrueOriginZ;
770 
771  B2ERROR("From TimeDependentVariables: Trying to access unknown tagTrack function");
772  return nullptr;
773  }
774 
775 
776  double cumulate(const Particle* part, const std::vector<std::string>& variable, double start,
777  std::function<double(double, double, double)> fun)
778  {
779  TagTrFPtr fptr(getTagTrackFunctionFromName(variable.at(0)));
780  auto* vert = part->getRelatedTo<TagVertex>();
781  if (!vert) return realNaN;
782  int nTracks = vert->getNFitTracks();
783 
784  //calculate cumulative quantity
785  double acum = start;
786  for (int i = 0; i < nTracks; ++i) {
787  vector<double> indx(1, i);
788  double w = tagTrackRaveWeight(part, indx);
789  double f = (*fptr)(part, indx);
790  if (w > 0) {
791  acum = fun(acum, f, w);
792  }
793  }
794  return acum;
795  }
796 
797  Manager::FunctionPtr tagTrackAverage(const std::vector<std::string>& variable)
798  {
799  if (variable.size() != 1) {
800  B2FATAL("Wrong number of arguments for meta function tagTrackAverage");
801  return nullptr;
802  }
803 
804  return [variable](const Particle * part) {
805  double sum = cumulate(part, variable, 0, [](double s, double f, double) {return s + f;});
806  double tot = cumulate(part, variable, 0, [](double s, double , double) {return s + 1;});
807  return (tot > 0) ? sum / tot : realNaN;
808  };
809  }
810 
811  Manager::FunctionPtr tagTrackAverageSquares(const std::vector<std::string>& variable)
812  {
813  if (variable.size() != 1) {
814  B2FATAL("Wrong number of arguments for meta function tagTrackAverageSquares");
815  return nullptr;
816  }
817 
818  return [variable](const Particle * part) {
819  double sum = cumulate(part, variable, 0, [](double s, double f, double) {return s + f * f;});
820  double tot = cumulate(part, variable, 0, [](double s, double , double) {return s + 1;});
821  return (tot > 0) ? sum / tot : realNaN;
822  };
823  }
824 
825 
826  Manager::FunctionPtr tagTrackMax(const std::vector<std::string>& variable)
827  {
828  if (variable.size() != 1) {
829  B2FATAL("Wrong number of arguments for meta function tagTrackMax");
830  return nullptr;
831  }
832 
833  return [variable](const Particle * part) {
834  double Max = cumulate(part, variable, -DBL_MAX, [](double s, double f, double) {return std::max(s, f);});
835  return (Max != -DBL_MAX) ? Max : realNaN;
836  };
837  }
838 
839  Manager::FunctionPtr tagTrackMin(const std::vector<std::string>& variable)
840  {
841  if (variable.size() != 1) {
842  B2FATAL("Wrong number of arguments for meta function tagTrackMin");
843  return nullptr;
844  }
845 
846  return [variable](const Particle * part) {
847  double Min = cumulate(part, variable, +DBL_MAX, [](double s, double f, double) {return std::min(s, f);});
848  return (Min != DBL_MAX) ? Min : realNaN;
849  };
850  }
851 
852  Manager::FunctionPtr tagTrackWeightedAverage(const std::vector<std::string>& variable)
853  {
854  if (variable.size() != 1) {
855  B2FATAL("Wrong number of arguments for meta function tagTrackWeightedAverage");
856  return nullptr;
857  }
858 
859  return [variable](const Particle * part) {
860  double num = cumulate(part, variable, 0, [](double s, double f, double w) {return s + w * f;});
861  double den = cumulate(part, variable, 0, [](double s, double , double w) {return s + w;});
862  return (den > 0) ? num / den : realNaN;
863  };
864  }
865 
866  Manager::FunctionPtr tagTrackWeightedAverageSquares(const std::vector<std::string>& variable)
867  {
868  if (variable.size() != 1) {
869  B2FATAL("Wrong number of arguments for meta function tagTrackWeightedAverageSquares");
870  return nullptr;
871  }
872 
873  return [variable](const Particle * part) -> double {
874  double num = cumulate(part, variable, 0, [](double s, double f, double w) {return s + w * f * f;});
875  double den = cumulate(part, variable, 0, [](double s, double , double w) {return s + w;});
876  return (den > 0) ? num / den : realNaN;
877  };
878  }
879 
880  Manager::FunctionPtr tagTrackSum(const std::vector<std::string>& variable)
881  {
882  if (variable.size() != 1) {
883  B2FATAL("Wrong number of arguments for meta function tagTrackSum");
884  return nullptr;
885  }
886 
887  return [variable](const Particle * part) {
888  return cumulate(part, variable, 0, [](double s, double f, double) {return s + f;});
889  };
890  }
891 
892 
893  //**********************************
894  //VARIABLE REGISTRATION
895  //**********************************
896 
897  VARIABLE_GROUP("Time Dependent CPV Analysis Variables");
898 
899  REGISTER_VARIABLE("TagVx", particleTagVx, "Tag vertex X component");
900  REGISTER_VARIABLE("TagVy", particleTagVy, "Tag vertex Y component");
901  REGISTER_VARIABLE("TagVz", particleTagVz, "Tag vertex Z component");
902  REGISTER_VARIABLE("mcTagVx", particleTruthTagVx, "Generated Tag vertex X component");
903  REGISTER_VARIABLE("mcTagVy", particleTruthTagVy, "Generated Tag vertex Y component");
904  REGISTER_VARIABLE("mcTagVz", particleTruthTagVz, "Generated Tag vertex Z component");
905  REGISTER_VARIABLE("TagVxErr", particleTagVxErr, "Tag vertex X component uncertainty");
906  REGISTER_VARIABLE("TagVyErr", particleTagVyErr, "Tag vertex Y component uncertainty");
907  REGISTER_VARIABLE("TagVzErr", particleTagVzErr, "Tag vertex Z component uncertainty");
908  REGISTER_VARIABLE("TagVpVal", particleTagVpVal, "Tag vertex p-Value");
909  REGISTER_VARIABLE("TagVNTracks", particleTagVNTracks, "Number of tracks in the tag vertex");
910  REGISTER_VARIABLE("TagVType", particleTagVType,
911  R"DOC(Type of algo for the tag vertex. -1: failed (1,2: single track, deprecated), 3: standard, 4: standard_PXD, 5: no constraint)DOC");
912  REGISTER_VARIABLE("TagVNDF", particleTagVNDF, "Number of degrees of freedom in the tag vertex fit");
913  REGISTER_VARIABLE("TagVChi2", particleTagVChi2, "chi2 value of the tag vertex fit");
914  REGISTER_VARIABLE("TagVChi2IP", particleTagVChi2IP, "IP component of chi2 value of the tag vertex fit");
915 
916  REGISTER_VARIABLE("DeltaT", particleDeltaT,
917  R"DOC(Proper decay time difference :math:`\Delta t` between signal B-meson :math:`(B_{rec})` and tag B-meson :math:`(B_{tag})` in ps.)DOC");
918  REGISTER_VARIABLE("DeltaTErr", particleDeltaTErr,
919  R"DOC(Proper decay time difference :math:`\Delta t` uncertainty in ps)DOC");
920  REGISTER_VARIABLE("DeltaTRes", particleDeltaTRes,
921  R"DOC(:math:`\Delta t` residual in ps, to be used for resolution function studies)DOC");
922  REGISTER_VARIABLE("DeltaTBelle", particleDeltaTBelle,
923  R"DOC([Legacy] :math:`\Delta t` in ps, as it was used in Belle)DOC");
924  REGISTER_VARIABLE("mcDeltaTau", particleMCDeltaTau,
925  R"DOC(Generated proper decay time difference :math:`\Delta t` in ps: :math:`\tau(B_{\rm rec})-\tau(B_{\rm tag})`)DOC");
926  REGISTER_VARIABLE("mcDeltaT", particleMCDeltaT,
927  R"DOC(Generated proper decay time difference (in z-difference approximation) :math:`\Delta t` in ps: :math:`(l(B_{\rm rec}) - l(B_{\rm tag}))/\beta_{\Upsilon(4S)}\gamma_{\Upsilon(4S)}`)DOC");
928  REGISTER_VARIABLE("mcDeltaBoost", particleMCDeltaBoost,
929  R"DOC(True difference of decay vertex boost-direction components between signal B-meson :math:`(B_{rec})` and tag B-meson :math:`(B_{tag})`:
930 :math:`\Delta l = l(B_{rec}) - l(B_{tag})`)DOC");
931  REGISTER_VARIABLE("DeltaZ", particleDeltaZ,
932  R"DOC(Difference of decay vertex longitudinal components between signal B-meson :math:`(B_{rec})` and tag B-meson :math:`(B_{tag})`:
933 :math:`\Delta z = z(B_{rec}) - z(B_{tag})`)DOC");
934  REGISTER_VARIABLE("DeltaZErr", particleDeltaZErr,
935  R"DOC(Uncertainty of the difference :math:`z(B_{rec}) - z(B_{tag})`)DOC");
936  REGISTER_VARIABLE("DeltaBoost", particleDeltaB, R"DOC(:math:`\Delta z` in the boost direction)DOC");
937  REGISTER_VARIABLE("DeltaBoostErr", particleDeltaBErr, R"DOC(Uncertanty of :math:`\Delta z` in the boost direction)DOC");
938 
939  REGISTER_VARIABLE("LBoost", vertexBoostDirection,
940  "Returns the vertex component in the boost direction");
941  REGISTER_VARIABLE("OBoost", vertexOrthogonalBoostDirection,
942  "Returns the vertex component in the direction orthogonal to the boost");
943  REGISTER_VARIABLE("mcLBoost", vertexTruthBoostDirection,
944  "Returns the MC vertex component in the boost direction");
945  REGISTER_VARIABLE("mcOBoost", vertexTruthOrthogonalBoostDirection,
946  "Returns the MC vertex component in the direction orthogonal to the boost");
947  REGISTER_VARIABLE("LBoostErr", vertexErrBoostDirection,
948  "Returns the error of the vertex in the boost direction");
949  REGISTER_VARIABLE("OBoostErr", vertexErrOrthBoostDirection,
950  "Returns the error of the vertex in the direction orthogonal to the boost");
951 
952  REGISTER_VARIABLE("TagVLBoost", tagVBoostDirection,
953  "Returns the TagV component in the boost direction");
954  REGISTER_VARIABLE("TagVOBoost", tagVOrthogonalBoostDirection,
955  "Returns the TagV component in the direction orthogonal to the boost");
956  REGISTER_VARIABLE("mcTagVLBoost", tagVTruthBoostDirection,
957  "Returns the MC TagV component in the boost direction");
958  REGISTER_VARIABLE("mcTagVOBoost", tagVTruthOrthogonalBoostDirection,
959  "Returns the MC TagV component in the direction orthogonal to the boost");
960  REGISTER_VARIABLE("TagVLBoostErr", tagVErrBoostDirection,
961  "Returns the error of TagV in the boost direction");
962  REGISTER_VARIABLE("TagVOBoostErr", tagVErrOrthogonalBoostDirection,
963  "Returns the error of TagV in the direction orthogonal to the boost");
964 
965  REGISTER_VARIABLE("internalTagVMCFlavor", particleInternalTagVMCFlavor,
966  "[Expert] [Debugging] This variable is only for internal checks of the TagV module by developers. \n"
967  "It returns the internal mc flavor information of the tag-side B provided by the TagV module.");
968 
969  REGISTER_VARIABLE("TagTrackMomentum(i) ", tagTrackMomentum,
970  "return the magnitude of the momentum of the ith track used in the tag vtx fit.");
971  REGISTER_VARIABLE("TagTrackMomentumX(i) ", tagTrackMomentumX,
972  "return the X component of the momentum of the ith track used in the tag vtx fit.");
973  REGISTER_VARIABLE("TagTrackMomentumY(i) ", tagTrackMomentumY,
974  "return the Y component of the momentum of the ith track used in the tag vtx fit.");
975  REGISTER_VARIABLE("TagTrackMomentumZ(i) ", tagTrackMomentumZ,
976  "return the Z component of the momentum of the ith track used in the tag vtx fit.");
977 
978  REGISTER_VARIABLE("TagTrackZ0(i)", tagTrackZ0, "return the z0 parameter of the ith track used in the tag vtx fit");
979  REGISTER_VARIABLE("TagTrackD0(i)", tagTrackD0, "return the d0 parameter of the ith track used in the tag vtx fit");
980 
981 
982  REGISTER_VARIABLE("TagTrackRaveWeight(i)", tagTrackRaveWeight, "returns the weight assigned by Rave to track i");
983 
984  REGISTER_VARIABLE("TagVNFitTracks", particleTagVNFitTracks,
985  "returns the number of tracks used by rave to fit the vertex (not counting the ones coming from Kshorts)");
986 
987  REGISTER_VARIABLE("TagTrackDistanceToConstraint(i)", tagTrackDistanceToConstraint,
988  "returns the measured distance between the ith tag track and the centre of the constraint.");
989 
990  REGISTER_VARIABLE("TagTrackDistanceToConstraintErr(i)", tagTrackDistanceToConstraintErr,
991  "returns the estimated error on the distance between the ith tag track and the centre of the constraint.");
992 
993  REGISTER_VARIABLE("TagTrackDistanceToConstraintSignificance(i)", tagTrackDistanceToConstraintSignificance,
994  "returns the significance of the distance between the centre of the constraint and the tag track indexed by track index (computed as distance / uncertainty)");
995 
996 
997  REGISTER_VARIABLE("TagVDistanceToConstraint", tagVDistanceToConstraint,
998  "returns the measured distance between the tag vtx and the centre of the constraint.");
999 
1000  REGISTER_VARIABLE("TagVDistanceToConstraintErr", tagVDistanceToConstraintErr,
1001  "returns the estimated error on the distance between the tag vtx and the centre of the constraint.");
1002 
1003  REGISTER_VARIABLE("TagVDistanceToConstraintSignificance", tagVDistanceToConstraintSignificance,
1004  "returns the significance of the distance between the tag vtx and the centre of the constraint (computed as distance / uncertainty)");
1005 
1006  REGISTER_VARIABLE("TagTrackDistanceToTagV(i)", tagTrackDistanceToTagV,
1007  "returns the measured distance between the ith tag track and the tag vtx.");
1008 
1009  REGISTER_VARIABLE("TagTrackDistanceToTagVErr(i)", tagTrackDistanceToTagVErr,
1010  "returns the estimated error on the distance between the ith tag track and the tag vtx. Warning: only the uncertainties on the track position parameters are taken into account.");
1011 
1012  REGISTER_VARIABLE("TagTrackDistanceToTagVSignificance(i)", tagTrackDistanceToTagVSignificance,
1013  "returns the significance of the distance between the tag vtx and the tag track indexed by trackIndex (computed as distance / uncertainty)");
1014 
1015  REGISTER_VARIABLE("TagTrackTrueDistanceToTagV(i)", tagTrackTrueDistanceToTagV,
1016  "return the true distance between the true B Tag decay vertex and the p'cle corresponding to the ith tag vtx track.")
1017 
1018  REGISTER_VARIABLE("TagTrackTrueVecToTagVX(i)", tagTrackTrueVecToTagVX,
1019  "return the X coordinate of the vector between the mc particle corresponding to the ith tag vtx track and the true tag B decay vertex.")
1020 
1021  REGISTER_VARIABLE("TagTrackTrueVecToTagVY(i)", tagTrackTrueVecToTagVY,
1022  "return the Y coordinate of the vector between the mc particle corresponding to the ith tag vtx track and the true tag B decay vertex.")
1023 
1024  REGISTER_VARIABLE("TagTrackTrueVecToTagVZ(i)", tagTrackTrueVecToTagVZ,
1025  "return the Z coordinate of the vector between the mc particle corresponding to the ith tag vtx track and the true tag B decay vertex.")
1026 
1027  REGISTER_VARIABLE("TagTrackTrueMomentumX(i)", tagTrackTrueMomentumX,
1028  "return the X component of the true momentum of the MC particle corresponding to the ith tag vtx track.")
1029 
1030  REGISTER_VARIABLE("TagTrackTrueMomentumY(i)", tagTrackTrueMomentumY,
1031  "return the Y component of the true momentum of the MC particle corresponding to the ith tag vtx track.")
1032 
1033  REGISTER_VARIABLE("TagTrackTrueMomentumZ(i)", tagTrackTrueMomentumZ,
1034  "return the Z component of the true momentum of the MC particle corresponding to the ith tag vtx track.")
1035 
1036  REGISTER_VARIABLE("TagTrackTrueOriginX(i)", tagTrackTrueOriginX,
1037  "return the X component of the true origin of the MC particle corresponding to the ith tag vtx track.")
1038 
1039  REGISTER_VARIABLE("TagTrackTrueOriginY(i)", tagTrackTrueOriginY,
1040  "return the Y component of the true origin of the MC particle corresponding to the ith tag vtx track.")
1041 
1042  REGISTER_VARIABLE("TagTrackTrueOriginZ(i)", tagTrackTrueOriginZ,
1043  "return the Z component of the true origin of the MC particle corresponding to the ith tag vtx track.")
1044 
1045  REGISTER_VARIABLE("TagVFitTruthStatus", fitTruthStatus,
1046  "Returns the status of the fit performed with the truth info. Possible values are: 0: fit performed with measured parameters, 1: fit performed with true parameters, 2: unable to recover truth parameters")
1047 
1048  REGISTER_VARIABLE("TagVRollBackStatus", rollbackStatus,
1049  "Returns the status of the fit performed with rolled back tracks. Possible values are: 0: fit performed with measured parameters, 1: fit performed with rolled back tracks, 2: unable to recover truth parameters")
1050 
1051  REGISTER_VARIABLE("TagTrackMax(var)", tagTrackMax,
1052  "return the maximum value of the variable ``var`` evaluated for each tag track. ``var`` must be one of the TagTrackXXX variables, for example: ``TagTrackMax(TagTrackDistanceToConstraint)``. The tracks that are assigned a zero weight are ignored.")
1053 
1054  REGISTER_VARIABLE("TagTrackMin(var)", tagTrackMin,
1055  "return the minimum value of the variable ``var`` evaluated for each tag track. ``var`` must be one of the TagTrackXXX variables, for example: ``TagTrackMin(TagTrackDistanceToConstraint)``. The tracks that are assigned a zero weight are ignored.")
1056 
1057  REGISTER_VARIABLE("TagTrackAverage(var)", tagTrackAverage,
1058  "return the average over the tag tracks of the variable ``var``. ``var`` must be one of the TagTrackXXX variables, for example: ``TagTrackAverage(TagTrackDistanceToConstraint)``. The tracks that are assigned a zero weight are ignored.")
1059 
1060  REGISTER_VARIABLE("TagTrackAverageSquares(var)", tagTrackAverageSquares,
1061  "return the average over the tag tracks of the square of the variable ``var``. ``var`` must be one of the TagTrackXXX variables, for example: ``TagTrackAverageSquares(TagTrackDistanceToConstraint)``. The tracks that are assigned a zero weight are ignored.")
1062 
1063  REGISTER_VARIABLE("TagTrackWeightedAverage(var)", tagTrackWeightedAverage,
1064  "return the average over the tag tracks of the variable ``var``, weighted by weights of the tag vertex fitter. ``var`` must be one of the TagTrackXXX variables, for example: ``TagTrackWeightedAverage(TagTrackDistanceToConstraint)``.")
1065 
1066  REGISTER_VARIABLE("TagTrackWeightedAverageSquares(var)", tagTrackWeightedAverageSquares,
1067  "return the average over the tag tracks of the variable ``var``, weighted by weights of the tag vertex fitter. ``var`` must be one of the TagTrackXXX variables, for example: ``TagTrackWeightedAverageSquares(TagTrackDistanceToConstraint)``.")
1068  }
1070 }
1071 
1072 
1073 
1074 
static const double realNaN
shortcut for NaN of double type
static const TVector3 vecNaN(realNaN, realNaN, realNaN)
vector with NaN entries
Abstract base class for different kinds of events.