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