Belle II Software  release-05-01-25
DriftLengthBasedEventTimeExtractor.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2018 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/eventTimeExtraction/findlets/DriftLengthBasedEventTimeExtractor.h>
11 
12 #include <tracking/trackFindingCDC/eventdata/utils/DriftTimeUtil.h>
13 
14 #include <genfit/KalmanFitterInfo.h>
15 #include <tracking/trackFitting/fitter/base/TrackFitter.h>
16 #include <tracking/dataobjects/RecoHitInformation.h>
17 #include <tracking/dataobjects/RecoTrack.h>
18 
19 using namespace Belle2;
20 using namespace TrackFindingCDC;
21 
22 namespace {
23  void addEventT0AndWeight(const genfit::MeasuredStateOnPlane& mSoP,
24  const CDCHit& cdcHit,
25  const RecoTrack& recoTrack,
26  const RecoHitInformation* recoHitInformation,
27  std::vector<std::pair<double, double>>& eventT0WithWeights)
28  {
29  WireID wireID(cdcHit.getID());
30  unsigned short iCLayer = wireID.getICLayer();
31  const TVector3& pos = mSoP.getPos();
32  const TVector3& mom = mSoP.getMom();
33 
34  // measured time = channel t0 - tdc * tdc bin width (ok)
35  const bool smear = false;
36  const double measuredTime = DriftTimeUtil::getMeasuredTime(wireID, cdcHit.getTDCCount(), smear);
37 
38  /*
39  * prop time = distance * prop speed
40  * distance = z distance * stereo factor
41  * stereo factor = (forward - backward).Mag() / (forward - backward).Z()
42  * z distance = z hit - backward.Z()
43  * distance += backward delta Z (if ZposMode == 1)
44  */
45  const double propTime = DriftTimeUtil::getPropTime(wireID, pos.Z());
46 
47  double flightTime = mSoP.getTime();
48 
49  // time walk taken from geometry parameters
50  const double timeWalk = DriftTimeUtil::getTimeWalk(wireID, cdcHit.getADCCount());
51 
52  // The state at index 3 is equivalent to the drift length -> see CDCRecoHit HMatrix
53  // The rest is handled by the CDCGeometryPar
54  const TVector3& wirePositon = mSoP.getPlane()->getO();
55  const double alpha = -wirePositon.DeltaPhi(mom);
56  const double theta = mom.Theta();
57 
58  const TVectorD& stateOnPlane = mSoP.getState();
59  const double driftLengthEstimate = std::abs(stateOnPlane(3));
60  const bool rl = stateOnPlane(3) > 0;
61  const double driftTime = DriftTimeUtil::getDriftTime(driftLengthEstimate, iCLayer, rl, alpha, theta);
62 
63  // daf weight taken from track point
64  const genfit::TrackPoint* trackPoint = recoTrack.getCreatedTrackPoint(recoHitInformation);
65  const auto* kalmanFitterInfo = trackPoint->getKalmanFitterInfo();
66  const double dafWeight = kalmanFitterInfo->getWeights().at(0);
67 
68  const double eventT0 = measuredTime - propTime - flightTime - timeWalk - driftTime;
69 
70  eventT0WithWeights.emplace_back(eventT0, dafWeight);
71  }
72 }
73 
74 void DriftLengthBasedEventTimeExtractor::apply(std::vector<RecoTrack*>& recoTracks)
75 {
76  TrackFitter trackFitter;
77  m_wasSuccessful = false;
78 
79 
80  // TODO: in principle this should also be possible for CDCTracks
81  // Collect all event t0 hypothesis with their weights
82  std::vector<std::pair<double, double>> eventT0WithWeights;
83 
84  for (RecoTrack* recoTrack : recoTracks) {
85  if (not trackFitter.fit(*recoTrack)) {
86  continue;
87  }
88 
89  const std::vector<CDCHit*>& cdcHits = recoTrack->getSortedCDCHitList();
90  for (CDCHit* cdcHit : cdcHits) {
91  RecoHitInformation* recoHitInformation = recoTrack->getRecoHitInformation(cdcHit);
92  try {
93  const genfit::MeasuredStateOnPlane& mSoP = recoTrack->getMeasuredStateOnPlaneFromRecoHit(recoHitInformation);
94  addEventT0AndWeight(mSoP, *cdcHit, *recoTrack, recoHitInformation, eventT0WithWeights);
95  } catch (...) {
96  continue;
97  }
98  }
99  }
100 
101  // calculate the weighted median
102  std::sort(eventT0WithWeights.begin(), eventT0WithWeights.end(), [](const auto & lhs, const auto & rhs) {
103  return lhs.first < rhs.first;
104  });
105 
106  double extractedEventT0 = NAN;
107 
108  double weightSum = 0;
109  for (const auto& pair : eventT0WithWeights) {
110  // cppcheck-suppress useStlAlgorithm
111  weightSum += pair.second;
112  }
113 
114  double cumSum = 0;
115  for (const auto& pair : eventT0WithWeights) {
116  cumSum += pair.second;
117  if (cumSum > 0.5 * weightSum) {
118  extractedEventT0 = pair.first;
119  break;
120  }
121  }
122 
123  if (not std::isnan(extractedEventT0)) {
124  EventT0::EventT0Component eventT0Component(extractedEventT0, NAN, Const::CDC, "drift length");
125  m_eventT0->setEventT0(eventT0Component);
126  m_wasSuccessful = true;
127  B2DEBUG(50, "Drift length gave a result of " << extractedEventT0);
128  } else {
129  B2DEBUG(50, "Extracted event t0 is nan.");
130  }
131 }
genfit::TrackPoint
Object containing AbsMeasurement and AbsFitterInfo objects.
Definition: TrackPoint.h:46
Belle2::WireID
Class to identify a wire inside the CDC.
Definition: WireID.h:44
Belle2::TrackFitter::fit
bool fit(RecoTrack &recoTrack, genfit::AbsTrackRep *trackRepresentation) const
Fit a reco track with a given non-default track representation.
Definition: TrackFitter.cc:109
genfit::MeasuredStateOnPlane
#StateOnPlane with additional covariance matrix.
Definition: MeasuredStateOnPlane.h:39
Belle2::CDCHit::getID
unsigned short getID() const
Getter for encoded wire number.
Definition: CDCHit.h:204
Belle2::RecoTrack::getMeasuredStateOnPlaneFromRecoHit
const genfit::MeasuredStateOnPlane & getMeasuredStateOnPlaneFromRecoHit(const RecoHitInformation *recoHitInfo, const genfit::AbsTrackRep *representation=nullptr) const
Return genfit's MeasuredStateOnPlane on plane for associated with one RecoHitInformation.
Definition: RecoTrack.cc:554
Belle2::CDCHit
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition: CDCHit.h:51
Belle2::CDCHit::getTDCCount
short getTDCCount() const
Getter for TDC count.
Definition: CDCHit.h:230
genfit::TrackPoint::getKalmanFitterInfo
KalmanFitterInfo * getKalmanFitterInfo(const AbsTrackRep *rep=nullptr) const
Helper to avoid casting.
Definition: TrackPoint.cc:180
Belle2::TrackFindingCDC::DriftTimeUtil::getMeasuredTime
static double getMeasuredTime(const WireID &wireID, unsigned short tdcCount, bool smear)
Returns the time measured at the readout board.
Definition: DriftTimeUtil.h:129
Belle2::TrackFindingCDC::DriftTimeUtil::getTimeWalk
static double getTimeWalk(const WireID &wireID, unsigned short adcCount)
Returns time-walk.
Definition: DriftTimeUtil.h:118
Belle2::DriftLengthBasedEventTimeExtractor::apply
void apply(std::vector< RecoTrack * > &) override final
Timing extraction for this findlet.
Definition: DriftLengthBasedEventTimeExtractor.cc:74
Belle2::TrackFindingCDC::DriftTimeUtil::getDriftTime
static double getDriftTime(double dist, unsigned short iCLayer, unsigned short lr, double alpha, double theta)
Return the drift time to the sense wire.
Definition: DriftTimeUtil.h:76
Belle2::EventT0::EventT0Component
Structure for storing the extracted event t0s together with its detector and its uncertainty.
Definition: EventT0.h:44
Belle2::CDCHit::getADCCount
unsigned short getADCCount() const
Getter for integrated charge.
Definition: CDCHit.h:241
Belle2::RecoTrack::getCreatedTrackPoint
const genfit::TrackPoint * getCreatedTrackPoint(const RecoHitInformation *recoHitInformation) const
Get a pointer to the TrackPoint that was created from this hit.
Definition: RecoTrack.cc:220
Belle2::TrackFitter
Algorithm class to handle the fitting of RecoTrack objects.
Definition: TrackFitter.h:116
Belle2::RecoTrack
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:78
Belle2::RecoTrack::getRecoHitInformation
RecoHitInformation * getRecoHitInformation(HitType *hit) const
Return the reco hit information for a generic hit from the storeArray.
Definition: RecoTrack.h:309
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RecoTrack::getSortedCDCHitList
std::vector< Belle2::RecoTrack::UsedCDCHit * > getSortedCDCHitList() const
Return a sorted list of cdc hits. Sorted by the sortingParameter.
Definition: RecoTrack.h:461
Belle2::TrackFindingCDC::DriftTimeUtil::getPropTime
static double getPropTime(const WireID &wireID, double z)
Getter for the in wire propagation time.
Definition: DriftTimeUtil.h:90
Belle2::RecoHitInformation
This class stores additional information to every CDC/SVD/PXD hit stored in a RecoTrack.
Definition: RecoHitInformation.h:48
genfit::KalmanFitterInfo::getWeights
std::vector< double > getWeights() const
Get weights of measurements.
Definition: KalmanFitterInfo.cc:168