9 #include <analysis/variables/ParameterVariables.h>
10 #include <analysis/VariableManager/Manager.h>
11 #include <analysis/dataobjects/Particle.h>
12 #include <analysis/utility/PCmsLabTransform.h>
13 #include <analysis/utility/ReferenceFrame.h>
15 #include <framework/logging/Logger.h>
16 #include <framework/datastore/StoreArray.h>
18 #include <mdst/dataobjects/MCParticle.h>
20 #include <mdst/dataobjects/Track.h>
21 #include <mdst/dataobjects/TrackFitResult.h>
23 #include <TLorentzVector.h>
37 bool almostContains(
const std::vector<double>& vector,
int value)
39 for (
const auto& item : vector)
40 if (std::abs(value - item) < 1e-3)
45 double RandomChoice(
const Particle*,
const std::vector<double>& choices)
47 int r = std::rand() % choices.size() + 1;
48 auto it = choices.begin();
53 double NumberOfMCParticlesInEvent(
const Particle*,
const std::vector<double>& pdgs)
55 StoreArray<MCParticle> mcParticles;
57 for (
int i = 0; i < mcParticles.getEntries(); ++i) {
64 double isAncestorOf(
const Particle* part,
const std::vector<double>& daughterIDs)
67 return std::numeric_limits<float>::quiet_NaN();
70 const MCParticle* mcpart = part->getMCParticle();
71 if (mcpart ==
nullptr)
72 return std::numeric_limits<float>::quiet_NaN();
74 if (daughterIDs.empty())
75 B2FATAL(
"Wrong number of arguments for parameter function isAncestorOf. At least one needed!");
78 const Particle* curParticle = part;
79 double isAncestor = 0.0;
81 for (
unsigned int i = 0; i < daughterIDs.size(); i++) {
82 int nCurDaughters = curParticle->getNDaughters();
83 if (nCurDaughters == 0)
84 B2FATAL(
"Assumed mother of particle at argument " << i <<
" has no daughters!");
85 if (daughterIDs[i] >= nCurDaughters)
86 B2FATAL(
"Assumed mother of particle at argument " << i <<
" has only " << nCurDaughters
87 <<
" daughters, but daughter at position " << daughterIDs[i] <<
" expected!");
88 const Particle* curDaughter = curParticle->getDaughter(daughterIDs[i]);
89 if (curDaughter ==
nullptr)
90 return std::numeric_limits<float>::quiet_NaN();
91 curParticle = curDaughter;
95 const MCParticle* finalMCDaughter = curParticle->
getMCParticle();
96 if (finalMCDaughter ==
nullptr)
97 return std::numeric_limits<float>::quiet_NaN();
100 const MCParticle* curMCParticle = finalMCDaughter;
102 while (curMCParticle !=
nullptr) {
103 const MCParticle* curMCMother = curMCParticle->getMother();
104 if (curMCMother ==
nullptr)
107 if (curMCMother->getArrayIndex() == mcpart->getArrayIndex()) {
111 curMCParticle = curMCMother;
119 double hasAncestor(
const Particle* part,
const std::vector<double>& args)
122 return std::numeric_limits<float>::quiet_NaN();
125 const MCParticle* mcpart = part->getMCParticle();
126 if (mcpart ==
nullptr)
127 return std::numeric_limits<float>::quiet_NaN();
129 int m_PDG, m_sign = 0;
132 B2FATAL(
"Wrong number of arguments for variable hasAncestor!");
133 else if (args.size() == 1) {
135 B2FATAL(
"PDG code in variable hasAncestor is 0!");
138 }
else if (args.size() == 2) {
139 if (args[0] == 0 or (args[1] != 0 and args[1] != 1))
140 B2FATAL(
"PDG code in variable hasAncestor is 0 or second argument is not 0 or 1!");
146 B2FATAL(
"Too many arguments for variable hasAncestor!");
149 unsigned int nLevels = 0;
151 const MCParticle* curMCParticle = mcpart;
153 while (curMCParticle !=
nullptr) {
154 const MCParticle* curMCMother = curMCParticle->getMother();
155 if (curMCMother ==
nullptr)
158 int pdg = curMCMother->getPDG();
167 curMCParticle = curMCMother;
174 double daughterInvariantMass(
const Particle* particle,
const std::vector<double>& daughter_indexes)
177 return std::numeric_limits<float>::quiet_NaN();
180 const auto& daughters = particle->getDaughters();
181 int nDaughters =
static_cast<int>(daughters.size());
183 for (
auto& double_daughter : daughter_indexes) {
184 long daughter = std::lround(double_daughter);
185 if (daughter >= nDaughters)
186 return std::numeric_limits<float>::quiet_NaN();
188 sum += daughters[daughter]->get4Vector();
194 double daughterMCInvariantMass(
const Particle* particle,
const std::vector<double>& daughter_indexes)
197 return std::numeric_limits<float>::quiet_NaN();
200 const auto& daughters = particle->getDaughters();
201 int nDaughters =
static_cast<int>(daughters.size());
203 for (
auto& double_daughter : daughter_indexes) {
204 long daughter = std::lround(double_daughter);
205 if (daughter >= nDaughters)
206 return std::numeric_limits<float>::quiet_NaN();
208 const MCParticle* mcdaughter = daughters[daughter]->
getMCParticle();
210 return std::numeric_limits<float>::quiet_NaN();
212 sum += mcdaughter->get4Vector();
219 double massDifference(
const Particle* particle,
const std::vector<double>& daughters)
222 return std::numeric_limits<float>::quiet_NaN();
224 long daughter = std::lround(daughters[0]);
225 if (daughter >=
static_cast<int>(particle->getNDaughters()))
226 return std::numeric_limits<float>::quiet_NaN();
228 double motherMass = particle->getMass();
229 double daughterMass = particle->getDaughter(daughter)->getMass();
231 return motherMass - daughterMass;
234 double massDifferenceError(
const Particle* particle,
const std::vector<double>& daughters)
237 return std::numeric_limits<float>::quiet_NaN();
239 long daughter = std::lround(daughters[0]);
240 if (daughter >=
static_cast<int>(particle->getNDaughters()))
241 return std::numeric_limits<float>::quiet_NaN();
245 TLorentzVector thisDaughterMomentum = particle->getDaughter(daughter)->get4Vector();
247 TMatrixFSym thisDaughterCovM(Particle::c_DimMomentum);
248 thisDaughterCovM = particle->getDaughter(daughter)->getMomentumErrorMatrix();
249 TMatrixFSym othrDaughterCovM(Particle::c_DimMomentum);
251 for (
int j = 0; j < int(particle->getNDaughters()); ++j) {
255 othrDaughterCovM += particle->getDaughter(j)->getMomentumErrorMatrix();
258 TMatrixFSym covarianceMatrix(2 * Particle::c_DimMomentum);
259 covarianceMatrix.SetSub(0, thisDaughterCovM);
260 covarianceMatrix.SetSub(4, othrDaughterCovM);
262 double motherMass = particle->getMass();
263 double daughterMass = particle->getDaughter(daughter)->getMass();
265 TVectorF jacobian(2 * Particle::c_DimMomentum);
266 jacobian[0] = thisDaughterMomentum.Px() / daughterMass - particle->getPx() / motherMass;
267 jacobian[1] = thisDaughterMomentum.Py() / daughterMass - particle->getPy() / motherMass;
268 jacobian[2] = thisDaughterMomentum.Pz() / daughterMass - particle->getPz() / motherMass;
269 jacobian[3] = particle->getEnergy() / motherMass - thisDaughterMomentum.E() / daughterMass;
270 jacobian[4] = -1.0 * particle->getPx() / motherMass;
271 jacobian[5] = -1.0 * particle->getPy() / motherMass;
272 jacobian[6] = -1.0 * particle->getPz() / motherMass;
273 jacobian[7] = 1.0 * particle->getEnergy() / motherMass;
275 result = jacobian * (covarianceMatrix * jacobian);
280 return TMath::Sqrt(result);
283 double massDifferenceSignificance(
const Particle* particle,
const std::vector<double>& daughters)
286 return std::numeric_limits<float>::quiet_NaN();
288 long daughter = std::lround(daughters[0]);
289 if (daughter >=
static_cast<int>(particle->getNDaughters()))
290 return std::numeric_limits<float>::quiet_NaN();
292 double massDiff = massDifference(particle, daughters);
293 double massDiffErr = massDifferenceError(particle, daughters);
295 double massDiffNominal = particle->getPDGMass() - particle->getDaughter(daughter)->getPDGMass();
297 return (massDiff - massDiffNominal) / massDiffErr;
301 double particleDecayAngle(
const Particle* particle,
const std::vector<double>& daughters)
304 return std::numeric_limits<float>::quiet_NaN();
307 TLorentzVector m = - T.getBeamFourMomentum();
309 TLorentzVector motherMomentum = particle->get4Vector();
310 TVector3 motherBoost = -(motherMomentum.BoostVector());
312 long daughter = std::lround(daughters[0]);
313 if (daughter >=
static_cast<int>(particle->getNDaughters()))
314 return std::numeric_limits<float>::quiet_NaN();
316 TLorentzVector daugMomentum = particle->getDaughter(daughter)->get4Vector();
317 daugMomentum.Boost(motherBoost);
319 m.Boost(motherBoost);
321 return daugMomentum.Angle(m.Vect());
324 double pointingAngle(
const Particle* particle,
const std::vector<double>& daughters)
327 return std::numeric_limits<float>::quiet_NaN();
329 long daughter = std::lround(daughters[0]);
330 if (daughter >=
static_cast<int>(particle->getNDaughters()))
331 return std::numeric_limits<float>::quiet_NaN();
333 if (particle->getDaughter(daughter)->getNDaughters() < 2)
334 return std::numeric_limits<float>::quiet_NaN();
336 TVector3 productionVertex = particle->getVertex();
337 TVector3 decayVertex = particle->getDaughter(daughter)->getVertex();
339 TVector3 vertexDiffVector = decayVertex - productionVertex;
342 TVector3 daughterMomentumVector = frame.getMomentum(particle->getDaughter(daughter)).Vect();
344 return daughterMomentumVector.Angle(vertexDiffVector);
347 double azimuthalAngleInDecayPlane(
const Particle* particle,
const std::vector<double>& daughters)
350 return std::numeric_limits<float>::quiet_NaN();
352 int nDaughters =
static_cast<int>(particle->getNDaughters());
354 long daughter1 = std::lround(daughters[0]);
355 long daughter2 = std::lround(daughters[1]);
356 if (daughter1 >= nDaughters || daughter2 >= nDaughters)
357 return std::numeric_limits<float>::quiet_NaN();
360 TLorentzVector m = T.getBeamFourMomentum();
361 TLorentzVector p = particle->get4Vector();
362 TLorentzVector d1 = particle->getDaughter(daughter1)->get4Vector();
363 TLorentzVector d2 = particle->getDaughter(daughter2)->get4Vector();
366 l.SetX(p.Py() * (d1.Pz() * d2.E() - d1.E() * d2.Pz()) + p.Pz() * (d1.E() * d2.Py() - d1.Py() * d2.E())
367 + p.E() * (d1.Py() * d2.Pz() - d1.Pz() * d2.Py()));
368 l.SetY(p.Px() * (d1.E() * d2.Pz() - d1.Pz() * d2.E()) + p.Pz() * (d1.Px() * d2.E() - d1.E() * d2.Px())
369 + p.E() * (d1.Pz() * d2.Px() - d1.Px() * d2.Pz()));
370 l.SetZ(p.Px() * (d1.Py() * d2.E() - d1.E() * d2.Py()) + p.Py() * (d1.E() * d2.Px() - d1.Px() * d2.E())
371 + p.E() * (d1.Px() * d2.Py() - d1.Py() * d2.Px()));
372 l.SetE(-(p.Px() * (d1.Pz() * d2.Py() - d1.Py() * d2.Pz()) + p.Py() * (d1.Px() * d2.Pz() - d1.Pz() * d2.Px())
373 + p.Pz() * (d1.Py() * d2.Px() - d1.Px() * d2.Py())));
375 double m_times_p = m * p;
376 double m_times_l = m * l;
377 double m_times_d1 = m * d1;
378 double l_times_d1 = l * d1;
379 double d1_times_p = d1 * p;
380 double m_abs = TMath::Sqrt(pow(m_times_p / p.M(), 2) - m.M2());
381 double d1_abs = TMath::Sqrt(pow(d1_times_p / p.M(), 2) - d1.M2());
382 double cos_phi = -m_times_l / (m_abs * TMath::Sqrt(-l.M2()));
383 double m_parallel_abs = m_abs * TMath::Sqrt(1 - cos_phi * cos_phi);
384 double m_parallel_times_d1 = m_times_p * d1_times_p / p.M2() + m_times_l * l_times_d1 / l.M2() - m_times_d1;
386 return TMath::ACos(-m_parallel_times_d1 / (m_parallel_abs * d1_abs));
389 double Constant(
const Particle*,
const std::vector<double>& constant)
396 VARIABLE_GROUP(
"ParameterFunctions");
397 REGISTER_VARIABLE(
"NumberOfMCParticlesInEvent(pdgcode)", NumberOfMCParticlesInEvent , R
"DOC(
398 Returns number of MC Particles (including anti-particles) with the given pdgcode in the event.
400 Used in the FEI to determine to calculate reconstruction efficiencies.
402 The variable is event-based and does not need a valid particle pointer as input.)DOC");
403 REGISTER_VARIABLE("isAncestorOf(i, j, ...)", isAncestorOf, R
"DOC(
404 Returns a positive integer if daughter at position particle->daughter(i)->daughter(j)... is an ancestor of the related MC particle, 0 otherwise.
406 Positive integer represents the number of steps needed to get from final MC daughter to ancestor.
407 If any particle or MCparticle is a nullptr, NaN is returned. If MC relations of any particle doesn't exist, -1.0 is returned.)DOC");
408 REGISTER_VARIABLE("hasAncestor(PDG, abs)", hasAncestor, R
"DOC(
410 Returns a positive integer if an ancestor with the given PDG code is found, 0 otherwise.
412 The integer is the level where the ancestor was found, 1: first mother, 2: grandmother, etc.
414 Second argument is optional, 1 means that the sign of the PDG code is taken into account, default is 0.
416 If there is no MC relations found, -1 is returned. In case of nullptr particle, NaN is returned.)DOC");
417 REGISTER_VARIABLE("daughterInvariantMass(i, j, ...)", daughterInvariantMass , R
"DOC(
418 Returns invariant mass of the given daughter particles. E.g.:
420 * daughterInvariantMass(0, 1) returns the invariant mass of the first and second daughter.
421 * daughterInvariantMass(0, 1, 2) returns the invariant mass of the first, second and third daughter.
423 Useful to identify intermediate resonances in a decay, which weren't reconstructed explicitly.
425 Returns NaN if particle is nullptr or if the given daughter-index is out of bound (>= amount of daughters).)DOC");
426 REGISTER_VARIABLE("daughterMCInvariantMass(i, j, ...)", daughterMCInvariantMass ,
427 "Returns true invariant mass of the given daughter particles, same behaviour as daughterInvariantMass variable.");
428 REGISTER_VARIABLE(
"decayAngle(i)", particleDecayAngle,
429 "Angle in the mother's rest frame between the reverted CMS momentum vector and the direction of the i-th daughter");
430 REGISTER_VARIABLE(
"pointingAngle(i)", pointingAngle, R
"DOC(
431 Angle between i-th daughter's momentum vector and vector connecting production and decay vertex of i-th daughter.
432 This makes only sense if the i-th daughter has itself daughter particles and therefore a properly defined vertex.)DOC");
433 REGISTER_VARIABLE("azimuthalAngleInDecayPlane(i, j)", azimuthalAngleInDecayPlane, R
"DOC(
434 Azimuthal angle of i-th daughter in decay plane towards projection of particle momentum into decay plane.
436 First we define the following symbols:
438 * P: four-momentum vector of decaying particle in whose decay plane the azimuthal angle is measured
439 * M: "mother" of p, however not necessarily the direct mother but any higher state, here the CMS itself is chosen
440 * D1: daughter for which the azimuthal angle is supposed to be calculated
441 * D2: another daughter needed to span the decay plane
442 * L: normal to the decay plane (four-component vector)
444 L can be defined via the following relation:
446 .. math:: L^{\sigma} = \delta^{\sigma\nu} \epsilon_{\mu\nu\alpha\beta} P^{\mu}D1^{\alpha}D2^{\beta}
448 The azimuthal angle is given by
450 .. math:: \phi \equiv \cos^{-1} \left(\frac{-\vec{M_{\parallel}} \cdot \vec{D1}}{|\vec{M_{\parallel}}| \cdot |\vec{D1}|}\right)
452 For a frame independent formulation the three component vectors need to be written via invariant four-momentum vectors.
456 -\vec{M_{\parallel}} \cdot \vec{D1} &= \biggl[M - \frac{(M \cdot L)L}{L^2}\biggr] \cdot D1 - \frac{(M \cdot P)(D1 \cdot P)}{m^2_P}\\
457 |\vec{M_{\parallel}}| &= |\vec{M}| \sqrt{1 - \cos^2 \psi}\\
458 |\vec{M}| &= \sqrt{\frac{(M \cdot P)^2}{m^2_P} - m^2_M}\\
459 \cos \psi &= \frac{\vec{M} \cdot \vec{L}}{|\vec{M}| \cdot |\vec{L}|} = \frac{-M \cdot L}{|\vec{M}| \cdot \sqrt{-L^2}}\\
460 |\vec{D1}| &= \sqrt{\frac{(D1 \cdot P)^2}{m^2_P} - m^2_{D1}}
464 REGISTER_VARIABLE("massDifference(i)", massDifference,
"Difference in invariant masses of this particle and its i-th daughter");
465 REGISTER_VARIABLE(
"massDifferenceError(i)", massDifferenceError,
466 "Estimated uncertainty on difference in invariant masses of this particle and its i-th daughter");
467 REGISTER_VARIABLE(
"massDifferenceSignificance(i)", massDifferenceSignificance,
468 "Signed significance of the deviation from the nominal mass difference of this particle and its i-th daughter [(massDiff - NOMINAL_MASS_DIFF)/ErrMassDiff]");
470 REGISTER_VARIABLE(
"constant(float i)", Constant, R
"DOC(
473 Useful for debugging purposes and in conjunction with the formula meta-variable.)DOC");
475 REGISTER_VARIABLE("randomChoice(i, j, ...)", RandomChoice, R
"DOC(
476 Returns random element of given numbers.
478 Useful for testing purposes.)DOC");
@ c_PrimaryParticle
bit 0: Particle is primary particle.
const MCParticle * getMCParticle() const
Returns the pointer to the MCParticle object that was used to create this Particle (ParticleType == c...
static const ReferenceFrame & GetCurrent()
Get current rest frame.
Abstract base class for different kinds of events.