Belle II Software  release-08-01-10
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 #include <Math/VectorUtil.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 ROOT::Math::XYZVector& pos = ROOT::Math::XYZVector(mSoP.getPos());
32  const ROOT::Math::XYZVector& mom = ROOT::Math::XYZVector(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 ROOT::Math::XYZVector& wirePositon = ROOT::Math::XYZVector(mSoP.getPlane()->getO());
55  const double alpha = ROOT::Math::VectorUtil::DeltaPhi(wirePositon, 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  weightSum += pair.second;
111  }
112 
113  double cumSum = 0;
114  for (const auto& pair : eventT0WithWeights) {
115  cumSum += pair.second;
116  if (cumSum > 0.5 * weightSum) {
117  extractedEventT0 = pair.first;
118  break;
119  }
120  }
121 
122  if (not std::isnan(extractedEventT0)) {
123  EventT0::EventT0Component eventT0Component(extractedEventT0, NAN, Const::CDC, "drift length");
124  m_eventT0->setEventT0(eventT0Component);
125  m_wasSuccessful = true;
126  B2DEBUG(25, "Drift length gave a result of " << extractedEventT0);
127  } else {
128  B2DEBUG(25, "Extracted event t0 is nan.");
129  }
130 }
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:79
const genfit::TrackPoint * getCreatedTrackPoint(const RecoHitInformation *recoHitInformation) const
Get a pointer to the TrackPoint that was created from this hit.
Definition: RecoTrack.cc:230
RecoHitInformation * getRecoHitInformation(HitType *hit) const
Return the reco hit information for a generic hit from the storeArray.
Definition: RecoTrack.h:312
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:579
std::vector< Belle2::RecoTrack::UsedCDCHit * > getSortedCDCHitList() const
Return a sorted list of cdc hits. Sorted by the sortingParameter.
Definition: RecoTrack.h:470
Algorithm class to handle the fitting of RecoTrack objects.
Definition: TrackFitter.h:116
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:33
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.