Belle II Software  release-05-02-19
BaseTrackTimeEstimatorModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nils Braun *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #include <tracking/modules/fitter/timeEstimator/BaseTrackTimeEstimatorModule.h>
11 
12 #include <tracking/trackFitting/fitter/base/TrackFitter.h>
13 #include <tracking/dataobjects/RecoTrack.h>
14 
15 #include <TVector3.h>
16 
17 using namespace std;
18 using namespace Belle2;
19 
20 namespace {
22  double calculateVelocity(const TVector3& momentum, const Const::ChargedStable& particleHypothesis)
23  {
24  // Particle velocity in cm / ns using the typical relation between E and p.
25  const double m = particleHypothesis.getMass();
26  const double p = momentum.Mag();
27  const double E = hypot(m, p);
28  const double beta = p / E;
29  const double v = beta * Const::speedOfLight;
30 
31  return v;
32  }
33 }
34 
35 BaseTrackTimeEstimatorModule::BaseTrackTimeEstimatorModule() :
36  Module()
37 {
38  setDescription("Module estimating the track time of RecoTracks - before or after the fit. "
39  "Loops over all RecoTracks and set their time seed correctly. In case of using the fitted information,"
40  "it also sets the track seeds of the position and momentum into the first measurement (where the time seed"
41  "is calculated). It also deletes all fitted information. Do not forget to refit "
42  "the tracks afterwards.");
44 
45  addParam("recoTracksStoreArrayName", m_param_recoTracksStoreArrayName, "StoreArray name of the input and output reco tracks.",
47 
48  addParam("useFittedInformation", m_param_useFittedInformation, "Whether to use the information in the measurements (after fit)"
49  " or the tracking seeds for doing the extrapolation. Of course, the track fit has to be performed to use the fitted information.",
51 
52  addParam("pdgCodeToUseForEstimation", m_param_pdgCodeToUseForEstimation,
53  "Which PDG code to use for creating the time estimate. How this information is used"
54  "depends on the implementation details of the child modules. Please only use the positive pdg code.",
56 
57  addParam("timeOffset", m_param_timeOffset, "If you want to subtract or add a certain time, you can use this variable.",
59 
60  addParam("readoutPosition", m_param_readoutPosition,
61  "In cases where the readout of the trigger is not located at the trigger directly and the signal has to"
62  "propagate a non-vanashing distance, you can set the readout position here. Please note that you have to"
63  "enable this feature by using the useReadoutPosition flag. You can control the propagation speed with the"
64  "flag readoutPositionPropagationSpeed.", m_param_readoutPosition);
65  addParam("useReadoutPosition", m_param_useReadoutPosition, "Enable the usage of the readout position."
66  "When this feature is enabled, the length from the incident of the particle in the trigger to the position"
67  "set by the readoutPosition flag is calculated and using the readoutPositionPropagationSpeed, a time is"
68  "calculated which is used in the time estimation as an offset."
69  "In the moment, this feature is only possible when using the fitted information." , m_param_useReadoutPosition);
70  addParam("readoutPositionPropagationSpeed", m_param_readoutPositionPropagationSpeed,
71  "Speed of the propagation from the hit on the trigger to the readoutPosition. Only is used when the"
72  "flag useReadoutPosition is enabled.", m_param_readoutPositionPropagationSpeed);
73 }
74 
76 {
77  // Read and write out RecoTracks
79  recoTracks.isRequired();
80 
82  B2FATAL("The combination of using the seed information and the readout position is not implemented in the moment.");
83  }
84 }
85 
87 {
89 
91 
92  // Estimate the track time for each reco track depending on the settings of the module.
93  for (auto& recoTrack : recoTracks) {
94  double timeSeed;
96  try {
97  timeSeed = estimateTimeSeedUsingFittedInformation(recoTrack, particleHypothesis);
98  } catch (genfit::Exception& e) {
99  B2WARNING("Time extraction from fitted state failed because of " << e.what());
100  timeSeed = -9999;
101  }
102  } else {
103  timeSeed = estimateTimeSeedUsingSeedInformation(recoTrack, particleHypothesis);
104  }
105 
106  if (!(timeSeed > -1000)) {
107  // Guard against NaN or just something silly.
108  B2WARNING("Fixing calculated seed Time " << timeSeed << " to zero.");
109  timeSeed = 0;
110  } else {
111  // Add the constant time offset only in non-silly cases.
112  timeSeed += m_param_timeOffset;
113  }
114 
115  B2DEBUG(100, "Setting seed to " << timeSeed);
116  recoTrack.setTimeSeed(timeSeed);
117  }
118 }
119 
121  const Const::ChargedStable& particleHypothesis) const
122 {
123  const int currentPdgCode = TrackFitter::createCorrectPDGCodeForChargedStable(particleHypothesis, recoTrack);
124  const genfit::AbsTrackRep* trackRepresentation = recoTrack.getTrackRepresentationForPDG(std::abs(currentPdgCode));
125 
126  if (not trackRepresentation or not recoTrack.wasFitSuccessful(trackRepresentation)) {
127  B2WARNING("Could not estimate a correct time, as the last fit failed.");
128  return 0;
129  } else {
130  // If the flight length is clear, just use the s = v * t relation.
131  genfit::MeasuredStateOnPlane measuredState = recoTrack.getMeasuredStateOnPlaneFromFirstHit(trackRepresentation);
132 
133  // Fix the position and momentum seed to the same place as where we calculation the time seed: the first measured state on plane
134  recoTrack.setPositionAndMomentum(measuredState.getPos(), measuredState.getMom());
135 
136  const double flightLength = estimateFlightLengthUsingFittedInformation(measuredState);
137 
138  // Be aware that we use the measured state on plane after the extrapolation to compile the momentum.
139  const TVector3& momentum = measuredState.getMom();
140  const double v = calculateVelocity(momentum, particleHypothesis);
141 
142  const double flightTime = flightLength / v;
143 
144  // When the readout position should be used, calculate the propagation time of the signal from the hit to the
145  // readout position.
147  const TVector3& position = measuredState.getPos();
148  B2ASSERT("Readout Position must have 3 components.", m_param_readoutPosition.size() == 3);
149  const TVector3 readoutPositionAsTVector3(m_param_readoutPosition[0], m_param_readoutPosition[1], m_param_readoutPosition[2]);
150  const double propagationLength = (position - readoutPositionAsTVector3).Mag();
151  const double propagationTime = propagationLength / m_param_readoutPositionPropagationSpeed;
152 
153  return flightTime - propagationTime;
154  } else {
155  return flightTime;
156  }
157  }
158 }
159 
161  const Const::ChargedStable& particleHypothesis) const
162 {
163  // If the flight length is clear, just use the s = v * t relation.
164  const double s = estimateFlightLengthUsingSeedInformation(recoTrack);
165 
166  const TVector3& momentum = recoTrack.getMomentumSeed();
167  const double v = calculateVelocity(momentum, particleHypothesis);
168 
169  return s / v;
170 }
genfit::Exception
Exception class for error handling in GENFIT (provides storage for diagnostic information)
Definition: Exception.h:48
Belle2::BaseTrackTimeEstimatorModule::m_param_pdgCodeToUseForEstimation
unsigned int m_param_pdgCodeToUseForEstimation
Which PDG code to use for creating the time estimate.
Definition: BaseTrackTimeEstimatorModule.h:62
Belle2::RecoTrack::wasFitSuccessful
bool wasFitSuccessful(const genfit::AbsTrackRep *representation=nullptr) const
Returns true if the last fit with the given representation was successful.
Definition: RecoTrack.cc:326
Belle2::BaseTrackTimeEstimatorModule::m_param_useReadoutPosition
bool m_param_useReadoutPosition
Enable the usage of the readout position.
Definition: BaseTrackTimeEstimatorModule.h:78
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::BaseTrackTimeEstimatorModule::event
void event() override
Loop over all RecoTracks and set their time seed correctly.
Definition: BaseTrackTimeEstimatorModule.cc:86
Belle2::BaseTrackTimeEstimatorModule::m_param_readoutPosition
std::vector< double > m_param_readoutPosition
In cases where the readout of the trigger is not located at the trigger directly and the signal has t...
Definition: BaseTrackTimeEstimatorModule.h:70
genfit::MeasuredStateOnPlane
#StateOnPlane with additional covariance matrix.
Definition: MeasuredStateOnPlane.h:39
Belle2::Module::c_ParallelProcessingCertified
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition: Module.h:82
Belle2::RecoTrack::getMeasuredStateOnPlaneFromFirstHit
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneFromFirstHit(const genfit::AbsTrackRep *representation=nullptr) const
Return genfit's MeasuredStateOnPlane for the first hit in a fit useful for extrapolation of measureme...
Definition: RecoTrack.cc:580
Belle2::BaseTrackTimeEstimatorModule::m_param_useFittedInformation
bool m_param_useFittedInformation
Whether to use the information in the measurements (after fit) or the tracking seeds for doing the ex...
Definition: BaseTrackTimeEstimatorModule.h:50
Belle2::RecoTrack::getTrackRepresentationForPDG
genfit::AbsTrackRep * getTrackRepresentationForPDG(int pdgCode)
Return an already created track representation of the given reco track for the PDG.
Definition: RecoTrack.cc:453
Belle2::BaseTrackTimeEstimatorModule::estimateTimeSeedUsingSeedInformation
double estimateTimeSeedUsingSeedInformation(RecoTrack &recoTrack, const Const::ChargedStable &particleHypothesis) const
Private helper function which calls the estimateFlightLengthUsingSeedInformation and computes the fli...
Definition: BaseTrackTimeEstimatorModule.cc:160
genfit::AbsTrackRep
Abstract base class for a track representation.
Definition: AbsTrackRep.h:66
Belle2::TrackFitter::createCorrectPDGCodeForChargedStable
static int createCorrectPDGCodeForChargedStable(const Const::ChargedStable &particleType, const RecoTrack &recoTrack)
Helper function to multiply the PDG code of a charged stable with the charge of the reco track (if ne...
Definition: TrackFitter.cc:26
Belle2::BaseTrackTimeEstimatorModule::estimateFlightLengthUsingSeedInformation
virtual double estimateFlightLengthUsingSeedInformation(const RecoTrack &recoTrack) const =0
Overload this function to implement a specific extrapolation mechanism for track seeds.
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Module::setPropertyFlags
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:210
Belle2::RecoTrack
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:78
Belle2::BaseTrackTimeEstimatorModule::estimateTimeSeedUsingFittedInformation
double estimateTimeSeedUsingFittedInformation(RecoTrack &recoTrack, const Const::ChargedStable &particleHypothesis) const
Private helper function which calls the estimateFlightLengthUsingFittedInformation with the correct m...
Definition: BaseTrackTimeEstimatorModule.cc:120
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::BaseTrackTimeEstimatorModule::initialize
void initialize() override
Initialize the needed StoreArrays and ensure they are created properly.
Definition: BaseTrackTimeEstimatorModule.cc:75
Belle2::BaseTrackTimeEstimatorModule::m_param_recoTracksStoreArrayName
std::string m_param_recoTracksStoreArrayName
StoreArray name of the input and output reco tracks.
Definition: BaseTrackTimeEstimatorModule.h:44
Belle2::BaseTrackTimeEstimatorModule::m_param_timeOffset
double m_param_timeOffset
If you want to subtract or add a certain time, you can use this variable.
Definition: BaseTrackTimeEstimatorModule.h:55
Belle2::RecoTrack::getMomentumSeed
TVector3 getMomentumSeed() const
Return the momentum seed stored in the reco track. ATTENTION: This is not the fitted momentum.
Definition: RecoTrack.h:484
Belle2::Module::addParam
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:562
Belle2::Const::ChargedStable
Provides a type-safe way to pass members of the chargedStableSet set.
Definition: Const.h:465
Belle2::StoreArray
Accessor to arrays stored in the data store.
Definition: ECLMatchingPerformanceExpertModule.h:33
Belle2::RecoTrack::setPositionAndMomentum
void setPositionAndMomentum(const TVector3 &positionSeed, const TVector3 &momentumSeed)
Set the position and momentum seed of the reco track. ATTENTION: This is not the fitted position or m...
Definition: RecoTrack.h:506
Belle2::BaseTrackTimeEstimatorModule::m_param_readoutPositionPropagationSpeed
double m_param_readoutPositionPropagationSpeed
Speed of the propagation from the hit on the trigger to the readoutPosition.
Definition: BaseTrackTimeEstimatorModule.h:84
Belle2::BaseTrackTimeEstimatorModule::estimateFlightLengthUsingFittedInformation
virtual double estimateFlightLengthUsingFittedInformation(genfit::MeasuredStateOnPlane &measuredStateOnPlane) const =0
Overload this function to implement a specific extrapolation mechanism for fitted tracks....
Belle2::Const::ParticleType::getMass
double getMass() const
Particle mass.
Definition: UnitConst.cc:310