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