Belle II Software  release-06-00-14
CDCT0CalibrationCollector.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 
9 #include "cdc/modules/cdcCalibrationCollector/CDCT0CalibrationCollector.h"
10 
11 #include <framework/datastore/RelationArray.h>
12 
13 #include <genfit/TrackPoint.h>
14 #include <genfit/KalmanFitterInfo.h>
15 #include <genfit/MeasuredStateOnPlane.h>
16 #include <Math/ProbFuncMathCore.h>
17 #include "TH1F.h"
18 #include <cdc/dataobjects/WireID.h>
19 #include <cdc/geometry/CDCGeometryPar.h>
20 
21 using namespace std;
22 using namespace Belle2;
23 using namespace CDC;
24 using namespace genfit;
25 
26 REG_MODULE(CDCT0CalibrationCollector)
27 
29 {
30  setDescription("Collector module for cdc T0 calibration");
31  setPropertyFlags(c_ParallelProcessingCertified);
32  addParam("RecoTracksColName", m_recoTrackArrayName, "Name of collection hold genfit::Track", std::string(""));
33  addParam("BField", m_BField, "If true -> #Params ==5 else #params ==4 for calculate P-Val", false);
34  addParam("EventT0Extraction", m_EventT0Extraction, "use event t0 extract t0 or not", false);
35  addParam("MinimumPt", m_MinimumPt, "Tracks whose Pt lower than this value will not be recoreded", 0.);
36  addParam("PvalCut", m_PvalCut, "Tracks which Pval small than this will not be recoreded", 0.);
37  addParam("NDFCut", m_ndfCut, "Tracks which NDF small than this will not be recoreded", 0.);
38  addParam("Xmin", m_xmin, "minimum dift length", 0.1);
39 }
40 
41 CDCT0CalibrationCollectorModule::~CDCT0CalibrationCollectorModule()
42 {
43 }
44 
45 void CDCT0CalibrationCollectorModule::prepare()
46 {
47  m_Tracks.isRequired(m_trackArrayName);
48  m_RecoTracks.isRequired(m_recoTrackArrayName);
49  m_TrackFitResults.isRequired(m_trackFitResultArrayName);
50  m_CDCHits.isRequired(m_cdcHitArrayName);
51  RelationArray relRecoTrackTrack(m_RecoTracks, m_Tracks, m_relRecoTrackTrackName);
52  //Store names to speed up creation later
53  m_relRecoTrackTrackName = relRecoTrackTrack.getName();
54 
55  auto m_hNDF = new TH1D("hNDF", "NDF of fitted track;NDF;Tracks", 71, -1, 70);
56  auto m_hPval = new TH1D("hPval", "p-values of tracks;pVal;Tracks", 1000, 0, 1);
57 
58  registerObject<TH1D>("hNDF", m_hNDF);
59  registerObject<TH1D>("hPval", m_hPval);
60 
61  static CDCGeometryPar& cdcgeo = CDCGeometryPar::Instance();
62  for (int i = 0; i < 56; ++i) {
63  double R = cdcgeo.senseWireR(i);
64  double nW = cdcgeo.nWiresInLayer(i);
65  halfCSize[i] = M_PI * R / nW;
66  }
67 
68  auto m_hTotal = new TH1F("hTotal", "hTotal", 30, -15, 15);
69  registerObject<TH1F>("hTotal", m_hTotal);
70  //for each channel
71  TH1F* m_h1[56][385];
72  TH1F* m_hT0b[300];
73  for (int il = 0; il < 56; il++) {
74  const int nW = cdcgeo.nWiresInLayer(il);
75  for (int ic = 0; ic < nW; ++ic) {
76  m_h1[il][ic] = new TH1F(Form("hdT_lay%d_cell%d", il, ic), Form("Lay%d_cell%d", il, ic), 30, -15, 15);
77  registerObject<TH1F>(Form("hdT_lay%d_cell%d", il, ic), m_h1[il][ic]);
78  }
79  }
80  //for each board
81  for (int ib = 0; ib < 300; ++ib) {
82  m_hT0b[ib] = new TH1F(Form("hT0b%d", ib), Form("boardID_%d", ib), 100, -20, 20);
83  registerObject<TH1F>(Form("hT0b%d", ib), m_hT0b[ib]);
84  }
85 
86 }
87 
88 void CDCT0CalibrationCollectorModule::collect()
89 {
90  const RelationArray relTrackTrack(m_RecoTracks, m_Tracks, m_relRecoTrackTrackName);
91 
92  /* CDCHit distribution */
93  // make evt t0 in case we don't use evt t0
94  double evtT0 = 0;
95  const int nTr = m_RecoTracks.getEntries();
96 
97  for (int i = 0; i < nTr; ++i) {
98  RecoTrack* track = m_RecoTracks[i];
99  if (track->getDirtyFlag()) continue;
100  const genfit::FitStatus* fs = track->getTrackFitStatus();
101  if (!fs || !fs->isFitted()) continue;
102  if (!fs->isFitConverged()) continue; //not fully converged
103 
104  const Belle2::Track* b2track = track->getRelatedFrom<Belle2::Track>();
105  if (!b2track) {
106  B2DEBUG(99, "No relation found");
107  continue;
108  }
109 
110  const Belle2::TrackFitResult* fitresult = b2track->getTrackFitResult(Const::muon);
111 
112  if (!fitresult) {
113  B2WARNING("track was fitted but Relation not found");
114  continue;
115  }
116 
117  double ndf = 0;
118  double Pval = 0;
119  if (m_BField) {
120  ndf = fs->getNdf();
121  Pval = fs->getPVal();
122  } else {
123  ndf = fs->getNdf() + 1;
124  double Chi2 = fs->getChi2();
125  Pval = std::max(0., ROOT::Math::chisquared_cdf_c(Chi2, ndf));
126  }
127 
128  getObjectPtr<TH1D>("hPval")->Fill(Pval);
129  getObjectPtr<TH1D>("hNDF")->Fill(ndf);
130 
131  if (Pval < m_PvalCut || ndf < m_ndfCut) continue;
132  //cut at Pt
133  if (fitresult->getMomentum().Perp() < m_MinimumPt) continue;
134  //reject events don't have eventT0
135  if (m_EventT0Extraction) {
136  // event with is fail to extract t0 will be exclude from analysis
137  if (m_eventTimeStoreObject.isValid() && m_eventTimeStoreObject->hasEventT0()) {
138  evtT0 = m_eventTimeStoreObject->getEventT0();
139  } else {
140  continue;
141  }
142  }
143 
144  B2DEBUG(99, "start collect hit");
145  static CDCGeometryPar& cdcgeo = CDCGeometryPar::Instance();
146 
147  for (const RecoHitInformation::UsedCDCHit* hit : track->getCDCHitList()) {
148  const genfit::TrackPoint* tp = track->getCreatedTrackPoint(track->getRecoHitInformation(hit));
149  int lay = hit->getICLayer();
150  int IWire = hit->getIWire();
151  unsigned short tdc = hit->getTDCCount();
152  unsigned short adc = hit->getADCCount();
153  WireID wireid(lay, IWire);
155  if (!kfi) {B2DEBUG(199, "No Fitter Info: Layer " << hit->getICLayer()); continue;}
156  for (unsigned int iMeas = 0; iMeas < kfi->getNumMeasurements(); ++iMeas) {
157  if ((kfi->getWeights().at(iMeas)) > 0.5) {
158  int boardID = cdcgeo.getBoardID(WireID(lay, IWire));
159  const genfit::MeasuredStateOnPlane& mop = kfi->getFittedState();
160  const TVector3 pocaOnWire = mop.getPlane()->getO();//Local wire position
161  // const TVector3 pocaOnTrack = mop.getPlane()->getU();//residual direction
162  const TVector3 pocaMom = mop.getMom();
163  double alpha = cdcgeo.getAlpha(pocaOnWire, pocaMom) ;
164  double theta = cdcgeo.getTheta(pocaMom);
165  double x_u = kfi->getFittedState(false).getState()(3);//x fit unbiased
166  // double weight = kfi->getWeights().at(iMeas);
167 
168  double xmax = halfCSize[lay] - 0.1;
169  if ((fabs(x_u) < m_xmin) || (fabs(x_u) > xmax)) continue;
170 
171  int lr;
172  if (x_u > 0) lr = 1;
173  else lr = 0;
174 
175  //Convert to outgoing
176  if (fabs(alpha) > M_PI / 2) {
177  x_u *= -1;
178  }
179 
180  lr = cdcgeo.getOutgoingLR(lr, alpha);
181  theta = cdcgeo.getOutgoingTheta(alpha, theta);
182  alpha = cdcgeo.getOutgoingAlpha(alpha);
183 
184  double t_fit = cdcgeo.getDriftTime(std::abs(x_u), lay, lr, alpha , theta);
185  //estimate drift time
186  double dt_flight;
187  double dt_prop;
188  double t = cdcgeo.getT0(wireid) - tdc * cdcgeo.getTdcBinWidth(); // - dt_flight - dt_prop;
189  dt_flight = mop.getTime();
190  if (dt_flight < 50) {
191  t -= dt_flight;
192  } else {
193  continue;
194  }
195 
196  double z = pocaOnWire.Z();
197  TVector3 m_backWirePos = cdcgeo.wireBackwardPosition(wireid, CDCGeometryPar::c_Aligned);
198  double z_prop = z - m_backWirePos.Z();
199  B2DEBUG(99, "z_prop = " << z_prop << " |z " << z << " |back wire poss: " << m_backWirePos.Z());
200  dt_prop = z_prop * cdcgeo.getPropSpeedInv(lay);
201  if (z_prop < 240) {
202  t -= dt_prop;
203  } else {
204  continue;
205  }
206  // Time Walk
207  t -= cdcgeo.getTimeWalk(wireid, adc);
208  // substract event t0;
209  t -= evtT0;
210 
211  getObjectPtr<TH1F>(Form("hdT_lay%d_cell%d", lay, IWire))->Fill(t - t_fit);
212  getObjectPtr<TH1F>(Form("hT0b%d", boardID))->Fill(t - t_fit);
213  getObjectPtr<TH1F>("hTotal")->Fill(t - t_fit);
214  } //NDF
215  // }//end of if isU
216  }//end of for
217  }//end of for tp
218  }//end of func
219 }
220 
221 void CDCT0CalibrationCollectorModule::finish()
222 {
223 }
224 
225 
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
Definition: CDCHit.h:40
The Class for CDC Geometry Parameters.
double getTimeWalk(const WireID &wID, unsigned short adcCount) const
Returns time-walk.
unsigned short getBoardID(const WireID &wID) const
Returns frontend board id. corresponding to the wire id.
const TVector3 wireBackwardPosition(int layerId, int cellId, EWirePosition set=c_Base) const
Returns the backward position of the input sense wire.
double getAlpha(const TVector3 &posOnWire, const TVector3 &momentum) const
Returns track incident angle in rphi plane (alpha in rad.).
double getTdcBinWidth() const
Return TDC bin width (nsec).
double getOutgoingAlpha(const double alpha) const
Converts incoming- to outgoing-alpha.
float getT0(const WireID &wireID) const
Returns t0 parameter of the specified sense wire.
unsigned short getOutgoingLR(const unsigned short lr, const double alpha) const
Converts incoming-lr to outgoing-lr.
double getOutgoingTheta(const double alpha, const double theta) const
Converts incoming- to outgoing-theta.
unsigned nWiresInLayer(int layerId) const
Returns wire numbers in a layer.
double getTheta(const TVector3 &momentum) const
Returns track incident angle (theta in rad.).
double getPropSpeedInv(const unsigned int layerID) const
Get the inversel of propagation speed in the sense wire.
double getDriftTime(double dist, unsigned short layer, unsigned short lr, double alpha, double theta) const
Return the drift time to the sense wire.
double senseWireR(int layerId) const
Returns radius of sense wire in each layer.
Collect hit information for cdc calibration with CAF.
Calibration collector module base class.
This is the Reconstruction Event-Data Model Track.
Definition: RecoTrack.h:76
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
const std::string & getName() const
Return name under which the object is saved in the DataStore.
Values of the result of a track fit with a given particle hypothesis.
TVector3 getMomentum() const
Getter for vector of momentum at closest approach of track in r/phi projection.
Class that bundles various TrackFitResults.
Definition: Track.h:25
const TrackFitResult * getTrackFitResult(const Const::ChargedStable &chargedStable) const
Access to TrackFitResults.
Definition: Track.cc:17
Class to identify a wire inside the CDC.
Definition: WireID.h:34
Class where important numbers and properties of a fit can be stored.
Definition: FitStatus.h:80
bool isFitConverged(bool inAllPoints=true) const
Did the fit converge (in all Points or only partially)?
Definition: FitStatus.h:105
virtual double getPVal() const
Get the p value of the fit.
Definition: FitStatus.h:128
double getChi2() const
Get chi^2 of the fit.
Definition: FitStatus.h:120
bool isFitted() const
Has the track been fitted?
Definition: FitStatus.h:94
double getNdf() const
Get the degrees of freedom of the fit.
Definition: FitStatus.h:122
Collects information needed and produced by a AbsKalmanFitter implementations and is specific to one ...
const MeasuredStateOnPlane & getFittedState(bool biased=true) const override
Get unbiased or biased (default) smoothed state.
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
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.
Defines for I/O streams used for error and debug printing.