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