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